Debian Codegraph @

closure-library/closure/goog/ui/cookieeditor.js

raw
1 // Copyright 2010 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Displays and edits the value of a cookie.
17 * Intended only for debugging.
18 */
19 goog.provide('goog.ui.CookieEditor');
20
21 goog.require('goog.asserts');
22 goog.require('goog.dom');
23 goog.require('goog.dom.TagName');
24 goog.require('goog.events.EventType');
25 goog.require('goog.net.cookies');
26 goog.require('goog.string');
27 goog.require('goog.style');
28 goog.require('goog.ui.Component');
29
30
31
32 /**
33 * Displays and edits the value of a cookie.
34 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
35 * @constructor
36 * @extends {goog.ui.Component}
37 * @final
38 */
39 goog.ui.CookieEditor = function(opt_domHelper) {
40 goog.ui.CookieEditor.base(this, 'constructor', opt_domHelper);
41 };
42 goog.inherits(goog.ui.CookieEditor, goog.ui.Component);
43
44
45 /**
46 * Cookie key.
47 * @type {?string}
48 * @private
49 */
50 goog.ui.CookieEditor.prototype.cookieKey_;
51
52
53 /**
54 * Text area.
55 * @type {HTMLTextAreaElement}
56 * @private
57 */
58 goog.ui.CookieEditor.prototype.textAreaElem_;
59
60
61 /**
62 * Clear button.
63 * @type {HTMLButtonElement}
64 * @private
65 */
66 goog.ui.CookieEditor.prototype.clearButtonElem_;
67
68
69 /**
70 * Invalid value warning text.
71 * @type {HTMLSpanElement}
72 * @private
73 */
74 goog.ui.CookieEditor.prototype.valueWarningElem_;
75
76
77 /**
78 * Update button.
79 * @type {HTMLButtonElement}
80 * @private
81 */
82 goog.ui.CookieEditor.prototype.updateButtonElem_;
83
84
85 // TODO(user): add combobox for user to select different cookies
86 /**
87 * Sets the cookie which this component will edit.
88 * @param {string} cookieKey Cookie key.
89 */
90 goog.ui.CookieEditor.prototype.selectCookie = function(cookieKey) {
91 goog.asserts.assert(goog.net.cookies.isValidName(cookieKey));
92 this.cookieKey_ = cookieKey;
93 if (this.textAreaElem_) {
94 this.textAreaElem_.value = goog.net.cookies.get(cookieKey) || '';
95 }
96 };
97
98
99 /** @override */
100 goog.ui.CookieEditor.prototype.canDecorate = function() {
101 return false;
102 };
103
104
105 /** @override */
106 goog.ui.CookieEditor.prototype.createDom = function() {
107 // Debug-only, so we don't need i18n.
108 this.clearButtonElem_ = goog.dom.createDom(
109 goog.dom.TagName.BUTTON, /* attributes */ null, 'Clear');
110 this.updateButtonElem_ = goog.dom.createDom(
111 goog.dom.TagName.BUTTON, /* attributes */ null, 'Update');
112 var value = this.cookieKey_ && goog.net.cookies.get(this.cookieKey_);
113 this.textAreaElem_ = goog.dom.createDom(
114 goog.dom.TagName.TEXTAREA, /* attibutes */ null, value || '');
115 this.valueWarningElem_ = goog.dom.createDom(
116 goog.dom.TagName.SPAN,
117 /* attibutes */ {'style': 'display:none;color:red'},
118 'Invalid cookie value.');
119 this.setElementInternal(
120 goog.dom.createDom(
121 goog.dom.TagName.DIV,
122 /* attibutes */ null, this.valueWarningElem_,
123 goog.dom.createDom(goog.dom.TagName.BR), this.textAreaElem_,
124 goog.dom.createDom(goog.dom.TagName.BR), this.clearButtonElem_,
125 this.updateButtonElem_));
126 };
127
128
129 /** @override */
130 goog.ui.CookieEditor.prototype.enterDocument = function() {
131 goog.ui.CookieEditor.base(this, 'enterDocument');
132 this.getHandler().listen(
133 this.clearButtonElem_, goog.events.EventType.CLICK, this.handleClear_);
134 this.getHandler().listen(
135 this.updateButtonElem_, goog.events.EventType.CLICK, this.handleUpdate_);
136 };
137
138
139 /**
140 * Handles user clicking clear button.
141 * @param {!goog.events.Event} e The click event.
142 * @private
143 */
144 goog.ui.CookieEditor.prototype.handleClear_ = function(e) {
145 if (this.cookieKey_) {
146 goog.net.cookies.remove(this.cookieKey_);
147 }
148 this.textAreaElem_.value = '';
149 };
150
151
152 /**
153 * Handles user clicking update button.
154 * @param {!goog.events.Event} e The click event.
155 * @private
156 */
157 goog.ui.CookieEditor.prototype.handleUpdate_ = function(e) {
158 if (this.cookieKey_) {
159 var value = this.textAreaElem_.value;
160 if (value) {
161 // Strip line breaks.
162 value = goog.string.stripNewlines(value);
163 }
164 if (goog.net.cookies.isValidValue(value)) {
165 goog.net.cookies.set(this.cookieKey_, value);
166 goog.style.setElementShown(this.valueWarningElem_, false);
167 } else {
168 goog.style.setElementShown(this.valueWarningElem_, true);
169 }
170 }
171 };
172
173
174 /** @override */
175 goog.ui.CookieEditor.prototype.disposeInternal = function() {
176 this.clearButtonElem_ = null;
177 this.cookieKey_ = null;
178 this.textAreaElem_ = null;
179 this.updateButtonElem_ = null;
180 this.valueWarningElem_ = null;
181 };