aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/cookieeditor.js
blob: eaf8e5d3bb319b781be61d4e16a416bf0da05e1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2010 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Displays and edits the value of a cookie.
 * Intended only for debugging.
 */
goog.provide('goog.ui.CookieEditor');

goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events.EventType');
goog.require('goog.net.cookies');
goog.require('goog.string');
goog.require('goog.style');
goog.require('goog.ui.Component');



/**
 * Displays and edits the value of a cookie.
 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
 * @constructor
 * @extends {goog.ui.Component}
 */
goog.ui.CookieEditor = function(opt_domHelper) {
  goog.base(this, opt_domHelper);
};
goog.inherits(goog.ui.CookieEditor, goog.ui.Component);


/**
 * Cookie key.
 * @type {?string}
 * @private
 */
goog.ui.CookieEditor.prototype.cookieKey_;


/**
 * Text area.
 * @type {HTMLTextAreaElement}
 * @private
 */
goog.ui.CookieEditor.prototype.textAreaElem_;


/**
 * Clear button.
 * @type {HTMLButtonElement}
 * @private
 */
goog.ui.CookieEditor.prototype.clearButtonElem_;


/**
 * Invalid value warning text.
 * @type {HTMLSpanElement}
 * @private
 */
goog.ui.CookieEditor.prototype.valueWarningElem_;


/**
 * Update button.
 * @type {HTMLButtonElement}
 * @private
 */
goog.ui.CookieEditor.prototype.updateButtonElem_;


// TODO(user): add combobox for user to select different cookies
/**
 * Sets the cookie which this component will edit.
 * @param {string} cookieKey Cookie key.
 */
goog.ui.CookieEditor.prototype.selectCookie = function(cookieKey) {
  goog.asserts.assert(goog.net.cookies.isValidName(cookieKey));
  this.cookieKey_ = cookieKey;
  if (this.textAreaElem_) {
    this.textAreaElem_.value = goog.net.cookies.get(cookieKey) || '';
  }
};


/** @override */
goog.ui.CookieEditor.prototype.canDecorate = function() {
  return false;
};


/** @override */
goog.ui.CookieEditor.prototype.createDom = function() {
  // Debug-only, so we don't need i18n.
  this.clearButtonElem_ = /** @type {HTMLButtonElement} */ (goog.dom.createDom(
      goog.dom.TagName.BUTTON, /* attributes */ null, 'Clear'));
  this.updateButtonElem_ = /** @type {HTMLButtonElement} */ (goog.dom.createDom(
      goog.dom.TagName.BUTTON, /* attributes */ null, 'Update'));
  var value = this.cookieKey_ && goog.net.cookies.get(this.cookieKey_);
  this.textAreaElem_ = /** @type {HTMLTextAreaElement} */ (goog.dom.createDom(
      goog.dom.TagName.TEXTAREA, /* attibutes */ null, value || ''));
  this.valueWarningElem_ = /** @type {HTMLSpanElement} */ (goog.dom.createDom(
      goog.dom.TagName.SPAN, /* attibutes */ {
        'style': 'display:none;color:red'
      }, 'Invalid cookie value.'));
  this.setElementInternal(goog.dom.createDom(goog.dom.TagName.DIV,
      /* attibutes */ null,
      this.valueWarningElem_,
      goog.dom.createDom(goog.dom.TagName.BR),
      this.textAreaElem_,
      goog.dom.createDom(goog.dom.TagName.BR),
      this.clearButtonElem_,
      this.updateButtonElem_));
};


/** @override */
goog.ui.CookieEditor.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');
  this.getHandler().listen(this.clearButtonElem_,
      goog.events.EventType.CLICK,
      this.handleClear_);
  this.getHandler().listen(this.updateButtonElem_,
      goog.events.EventType.CLICK,
      this.handleUpdate_);
};


/**
 * Handles user clicking clear button.
 * @param {!goog.events.Event} e The click event.
 * @private
 */
goog.ui.CookieEditor.prototype.handleClear_ = function(e) {
  if (this.cookieKey_) {
    goog.net.cookies.remove(this.cookieKey_);
  }
  this.textAreaElem_.value = '';
};


/**
 * Handles user clicking update button.
 * @param {!goog.events.Event} e The click event.
 * @private
 */
goog.ui.CookieEditor.prototype.handleUpdate_ = function(e) {
  if (this.cookieKey_) {
    var value = this.textAreaElem_.value;
    if (value) {
      // Strip line breaks.
      value = goog.string.stripNewlines(value);
    }
    if (goog.net.cookies.isValidValue(value)) {
      goog.net.cookies.set(this.cookieKey_, value);
      goog.style.showElement(this.valueWarningElem_, false);
    } else {
      goog.style.showElement(this.valueWarningElem_, true);
    }
  }
};


/** @override */
goog.ui.CookieEditor.prototype.disposeInternal = function() {
  this.clearButtonElem_ = null;
  this.cookieKey_ = null;
  this.textAreaElem_ = null;
  this.updateButtonElem_ = null;
  this.valueWarningElem_ = null;
};