samplecomponent.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2007 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 A simple, sample component.
  16. *
  17. */
  18. goog.provide('goog.demos.SampleComponent');
  19. goog.require('goog.dom');
  20. goog.require('goog.dom.TagName');
  21. goog.require('goog.dom.classlist');
  22. goog.require('goog.events.EventType');
  23. goog.require('goog.events.KeyCodes');
  24. goog.require('goog.events.KeyHandler');
  25. goog.require('goog.ui.Component');
  26. /**
  27. * A simple box that changes colour when clicked. This class demonstrates the
  28. * goog.ui.Component API, and is keyboard accessible, as per
  29. * http://wiki/Main/ClosureKeyboardAccessible
  30. *
  31. * @param {string=} opt_label A label to display. Defaults to "Click Me" if none
  32. * provided.
  33. * @param {goog.dom.DomHelper=} opt_domHelper DOM helper to use.
  34. *
  35. * @extends {goog.ui.Component}
  36. * @constructor
  37. * @final
  38. */
  39. goog.demos.SampleComponent = function(opt_label, opt_domHelper) {
  40. goog.demos.SampleComponent.base(this, 'constructor', opt_domHelper);
  41. /**
  42. * The label to display.
  43. * @type {string}
  44. * @private
  45. */
  46. this.initialLabel_ = opt_label || 'Click Me';
  47. /**
  48. * The current color.
  49. * @type {string}
  50. * @private
  51. */
  52. this.color_ = 'red';
  53. /**
  54. * Keyboard handler for this object. This object is created once the
  55. * component's DOM element is known.
  56. *
  57. * @type {goog.events.KeyHandler?}
  58. * @private
  59. */
  60. this.kh_ = null;
  61. };
  62. goog.inherits(goog.demos.SampleComponent, goog.ui.Component);
  63. /**
  64. * Changes the color of the element.
  65. * @private
  66. */
  67. goog.demos.SampleComponent.prototype.changeColor_ = function() {
  68. if (this.color_ == 'red') {
  69. this.color_ = 'green';
  70. } else if (this.color_ == 'green') {
  71. this.color_ = 'blue';
  72. } else {
  73. this.color_ = 'red';
  74. }
  75. this.getElement().style.backgroundColor = this.color_;
  76. };
  77. /**
  78. * Creates an initial DOM representation for the component.
  79. * @override
  80. */
  81. goog.demos.SampleComponent.prototype.createDom = function() {
  82. this.decorateInternal(this.dom_.createElement(goog.dom.TagName.DIV));
  83. };
  84. /**
  85. * Decorates an existing HTML DIV element as a SampleComponent.
  86. *
  87. * @param {Element} element The DIV element to decorate. The element's
  88. * text, if any will be used as the component's label.
  89. * @override
  90. */
  91. goog.demos.SampleComponent.prototype.decorateInternal = function(element) {
  92. goog.demos.SampleComponent.base(this, 'decorateInternal', element);
  93. if (!this.getLabelText()) {
  94. this.setLabelText(this.initialLabel_);
  95. }
  96. var elem = this.getElement();
  97. goog.dom.classlist.add(elem, goog.getCssName('goog-sample-component'));
  98. elem.style.backgroundColor = this.color_;
  99. elem.tabIndex = 0;
  100. this.kh_ = new goog.events.KeyHandler(elem);
  101. this.getHandler().listen(
  102. this.kh_, goog.events.KeyHandler.EventType.KEY, this.onKey_);
  103. };
  104. /** @override */
  105. goog.demos.SampleComponent.prototype.disposeInternal = function() {
  106. goog.demos.SampleComponent.base(this, 'disposeInternal');
  107. if (this.kh_) {
  108. this.kh_.dispose();
  109. }
  110. };
  111. /**
  112. * Called when component's element is known to be in the document.
  113. * @override
  114. */
  115. goog.demos.SampleComponent.prototype.enterDocument = function() {
  116. goog.demos.SampleComponent.base(this, 'enterDocument');
  117. this.getHandler().listen(
  118. this.getElement(), goog.events.EventType.CLICK, this.onDivClicked_);
  119. };
  120. /**
  121. * Gets the current label text.
  122. *
  123. * @return {string} The current text set into the label, or empty string if
  124. * none set.
  125. */
  126. goog.demos.SampleComponent.prototype.getLabelText = function() {
  127. if (!this.getElement()) {
  128. return '';
  129. }
  130. return goog.dom.getTextContent(this.getElement());
  131. };
  132. /**
  133. * Handles DIV element clicks, causing the DIV's colour to change.
  134. * @param {goog.events.Event} event The click event.
  135. * @private
  136. */
  137. goog.demos.SampleComponent.prototype.onDivClicked_ = function(event) {
  138. this.changeColor_();
  139. };
  140. /**
  141. * Fired when user presses a key while the DIV has focus. If the user presses
  142. * space or enter, the color will be changed.
  143. * @param {goog.events.Event} event The key event.
  144. * @private
  145. */
  146. goog.demos.SampleComponent.prototype.onKey_ = function(event) {
  147. var keyCodes = goog.events.KeyCodes;
  148. if (event.keyCode == keyCodes.SPACE || event.keyCode == keyCodes.ENTER) {
  149. this.changeColor_();
  150. }
  151. };
  152. /**
  153. * Sets the current label text. Has no effect if component is not rendered.
  154. *
  155. * @param {string} text The text to set as the label.
  156. */
  157. goog.demos.SampleComponent.prototype.setLabelText = function(text) {
  158. if (this.getElement()) {
  159. goog.dom.setTextContent(this.getElement(), text);
  160. }
  161. };