cookieeditor.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * @fileoverview Displays and edits the value of a cookie.
  16. * Intended only for debugging.
  17. */
  18. goog.provide('goog.ui.CookieEditor');
  19. goog.require('goog.asserts');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.events.EventType');
  23. goog.require('goog.net.cookies');
  24. goog.require('goog.string');
  25. goog.require('goog.style');
  26. goog.require('goog.ui.Component');
  27. /**
  28. * Displays and edits the value of a cookie.
  29. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  30. * @constructor
  31. * @extends {goog.ui.Component}
  32. * @final
  33. */
  34. goog.ui.CookieEditor = function(opt_domHelper) {
  35. goog.ui.CookieEditor.base(this, 'constructor', opt_domHelper);
  36. };
  37. goog.inherits(goog.ui.CookieEditor, goog.ui.Component);
  38. /**
  39. * Cookie key.
  40. * @type {?string}
  41. * @private
  42. */
  43. goog.ui.CookieEditor.prototype.cookieKey_;
  44. /**
  45. * Text area.
  46. * @type {HTMLTextAreaElement}
  47. * @private
  48. */
  49. goog.ui.CookieEditor.prototype.textAreaElem_;
  50. /**
  51. * Clear button.
  52. * @type {HTMLButtonElement}
  53. * @private
  54. */
  55. goog.ui.CookieEditor.prototype.clearButtonElem_;
  56. /**
  57. * Invalid value warning text.
  58. * @type {HTMLSpanElement}
  59. * @private
  60. */
  61. goog.ui.CookieEditor.prototype.valueWarningElem_;
  62. /**
  63. * Update button.
  64. * @type {HTMLButtonElement}
  65. * @private
  66. */
  67. goog.ui.CookieEditor.prototype.updateButtonElem_;
  68. // TODO(user): add combobox for user to select different cookies
  69. /**
  70. * Sets the cookie which this component will edit.
  71. * @param {string} cookieKey Cookie key.
  72. */
  73. goog.ui.CookieEditor.prototype.selectCookie = function(cookieKey) {
  74. goog.asserts.assert(goog.net.cookies.isValidName(cookieKey));
  75. this.cookieKey_ = cookieKey;
  76. if (this.textAreaElem_) {
  77. this.textAreaElem_.value = goog.net.cookies.get(cookieKey) || '';
  78. }
  79. };
  80. /** @override */
  81. goog.ui.CookieEditor.prototype.canDecorate = function() {
  82. return false;
  83. };
  84. /** @override */
  85. goog.ui.CookieEditor.prototype.createDom = function() {
  86. // Debug-only, so we don't need i18n.
  87. this.clearButtonElem_ = goog.dom.createDom(
  88. goog.dom.TagName.BUTTON, /* attributes */ null, 'Clear');
  89. this.updateButtonElem_ = goog.dom.createDom(
  90. goog.dom.TagName.BUTTON, /* attributes */ null, 'Update');
  91. var value = this.cookieKey_ && goog.net.cookies.get(this.cookieKey_);
  92. this.textAreaElem_ = goog.dom.createDom(
  93. goog.dom.TagName.TEXTAREA, /* attibutes */ null, value || '');
  94. this.valueWarningElem_ = goog.dom.createDom(
  95. goog.dom.TagName.SPAN,
  96. /* attibutes */ {'style': 'display:none;color:red'},
  97. 'Invalid cookie value.');
  98. this.setElementInternal(
  99. goog.dom.createDom(
  100. goog.dom.TagName.DIV,
  101. /* attibutes */ null, this.valueWarningElem_,
  102. goog.dom.createDom(goog.dom.TagName.BR), this.textAreaElem_,
  103. goog.dom.createDom(goog.dom.TagName.BR), this.clearButtonElem_,
  104. this.updateButtonElem_));
  105. };
  106. /** @override */
  107. goog.ui.CookieEditor.prototype.enterDocument = function() {
  108. goog.ui.CookieEditor.base(this, 'enterDocument');
  109. this.getHandler().listen(
  110. this.clearButtonElem_, goog.events.EventType.CLICK, this.handleClear_);
  111. this.getHandler().listen(
  112. this.updateButtonElem_, goog.events.EventType.CLICK, this.handleUpdate_);
  113. };
  114. /**
  115. * Handles user clicking clear button.
  116. * @param {!goog.events.Event} e The click event.
  117. * @private
  118. */
  119. goog.ui.CookieEditor.prototype.handleClear_ = function(e) {
  120. if (this.cookieKey_) {
  121. goog.net.cookies.remove(this.cookieKey_);
  122. }
  123. this.textAreaElem_.value = '';
  124. };
  125. /**
  126. * Handles user clicking update button.
  127. * @param {!goog.events.Event} e The click event.
  128. * @private
  129. */
  130. goog.ui.CookieEditor.prototype.handleUpdate_ = function(e) {
  131. if (this.cookieKey_) {
  132. var value = this.textAreaElem_.value;
  133. if (value) {
  134. // Strip line breaks.
  135. value = goog.string.stripNewlines(value);
  136. }
  137. if (goog.net.cookies.isValidValue(value)) {
  138. goog.net.cookies.set(this.cookieKey_, value);
  139. goog.style.setElementShown(this.valueWarningElem_, false);
  140. } else {
  141. goog.style.setElementShown(this.valueWarningElem_, true);
  142. }
  143. }
  144. };
  145. /** @override */
  146. goog.ui.CookieEditor.prototype.disposeInternal = function() {
  147. this.clearButtonElem_ = null;
  148. this.cookieKey_ = null;
  149. this.textAreaElem_ = null;
  150. this.updateButtonElem_ = null;
  151. this.valueWarningElem_ = null;
  152. };