Debian Codegraph @

closure-library/closure/goog/demos/samplecomponent.js

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