Debian Codegraph @

closure-library/closure/goog/ui/tristatemenuitem.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 menu item class that supports three state checkbox semantics.
17 *
18 * @author [email protected] (Emil A Eklund)
19 */
20
21 goog.provide('goog.ui.TriStateMenuItem');
22 goog.provide('goog.ui.TriStateMenuItem.State');
23
24 goog.require('goog.dom.classlist');
25 goog.require('goog.ui.Component');
26 goog.require('goog.ui.MenuItem');
27 goog.require('goog.ui.TriStateMenuItemRenderer');
28 goog.require('goog.ui.registry');
29
30
31
32 /**
33 * Class representing a three state checkbox menu item.
34 *
35 * @param {goog.ui.ControlContent} content Text caption or DOM structure
36 * to display as the content of the item (use to add icons or styling to
37 * menus).
38 * @param {Object=} opt_model Data/model associated with the menu item.
39 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper used for
40 * document interactions.
41 * @param {goog.ui.MenuItemRenderer=} opt_renderer Optional renderer.
42 * @param {boolean=} opt_alwaysAllowPartial If true, always allow partial
43 * state.
44 * @constructor
45 * @extends {goog.ui.MenuItem}
46 * TODO(attila): Figure out how to better integrate this into the
47 * goog.ui.Control state management framework.
48 * @final
49 */
50 goog.ui.TriStateMenuItem = function(
51 content, opt_model, opt_domHelper, opt_renderer, opt_alwaysAllowPartial) {
52 goog.ui.MenuItem.call(
53 this, content, opt_model, opt_domHelper,
54 opt_renderer || new goog.ui.TriStateMenuItemRenderer());
55 this.setCheckable(true);
56 this.alwaysAllowPartial_ = opt_alwaysAllowPartial || false;
57 };
58 goog.inherits(goog.ui.TriStateMenuItem, goog.ui.MenuItem);
59
60
61 /**
62 * Checked states for component.
63 * @enum {number}
64 */
65 goog.ui.TriStateMenuItem.State = {
66 /**
67 * Component is not checked.
68 */
69 NOT_CHECKED: 0,
70
71 /**
72 * Component is partially checked.
73 */
74 PARTIALLY_CHECKED: 1,
75
76 /**
77 * Component is fully checked.
78 */
79 FULLY_CHECKED: 2
80 };
81
82
83 /**
84 * Menu item's checked state.
85 * @type {goog.ui.TriStateMenuItem.State}
86 * @private
87 */
88 goog.ui.TriStateMenuItem.prototype.checkState_ =
89 goog.ui.TriStateMenuItem.State.NOT_CHECKED;
90
91
92 /**
93 * Whether the partial state can be toggled.
94 * @type {boolean}
95 * @private
96 */
97 goog.ui.TriStateMenuItem.prototype.allowPartial_ = false;
98
99
100 /**
101 * Used to override allowPartial_ to force the third state to always be
102 * permitted.
103 * @type {boolean}
104 * @private
105 */
106 goog.ui.TriStateMenuItem.prototype.alwaysAllowPartial_ = false;
107
108
109 /**
110 * @return {goog.ui.TriStateMenuItem.State} The menu item's check state.
111 */
112 goog.ui.TriStateMenuItem.prototype.getCheckedState = function() {
113 return this.checkState_;
114 };
115
116
117 /**
118 * Sets the checked state.
119 * @param {goog.ui.TriStateMenuItem.State} state The checked state.
120 */
121 goog.ui.TriStateMenuItem.prototype.setCheckedState = function(state) {
122 this.setCheckedState_(state);
123 this.allowPartial_ =
124 state == goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED;
125 };
126
127
128 /**
129 * Sets the checked state and updates the CSS styling. Dispatches a
130 * {@code CHECK} or {@code UNCHECK} event prior to changing the component's
131 * state, which may be caught and canceled to prevent the component from
132 * changing state.
133 * @param {goog.ui.TriStateMenuItem.State} state The checked state.
134 * @private
135 */
136 goog.ui.TriStateMenuItem.prototype.setCheckedState_ = function(state) {
137 if (this.dispatchEvent(
138 state != goog.ui.TriStateMenuItem.State.NOT_CHECKED ?
139 goog.ui.Component.EventType.CHECK :
140 goog.ui.Component.EventType.UNCHECK)) {
141 this.setState(
142 goog.ui.Component.State.CHECKED,
143 state != goog.ui.TriStateMenuItem.State.NOT_CHECKED);
144 this.checkState_ = state;
145 this.updatedCheckedStateClassNames_();
146 }
147 };
148
149
150 /** @override */
151 goog.ui.TriStateMenuItem.prototype.performActionInternal = function(e) {
152 switch (this.getCheckedState()) {
153 case goog.ui.TriStateMenuItem.State.NOT_CHECKED:
154 this.setCheckedState_(
155 this.alwaysAllowPartial_ || this.allowPartial_ ?
156 goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED :
157 goog.ui.TriStateMenuItem.State.FULLY_CHECKED);
158 break;
159 case goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED:
160 this.setCheckedState_(goog.ui.TriStateMenuItem.State.FULLY_CHECKED);
161 break;
162 case goog.ui.TriStateMenuItem.State.FULLY_CHECKED:
163 this.setCheckedState_(goog.ui.TriStateMenuItem.State.NOT_CHECKED);
164 break;
165 }
166
167 var checkboxClass =
168 goog.getCssName(this.getRenderer().getCssClass(), 'checkbox');
169 var clickOnCheckbox = e.target &&
170 goog.dom.classlist.contains(
171 /** @type {!Element} */ (e.target), checkboxClass);
172
173 return this.dispatchEvent(
174 clickOnCheckbox || this.allowPartial_ ?
175 goog.ui.Component.EventType.CHANGE :
176 goog.ui.Component.EventType.ACTION);
177 };
178
179
180 /**
181 * Updates the extra class names applied to the menu item element.
182 * @private
183 */
184 goog.ui.TriStateMenuItem.prototype.updatedCheckedStateClassNames_ = function() {
185 var renderer = this.getRenderer();
186 renderer.enableExtraClassName(
187 this, goog.getCssName(renderer.getCssClass(), 'partially-checked'),
188 this.getCheckedState() ==
189 goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED);
190 renderer.enableExtraClassName(
191 this, goog.getCssName(renderer.getCssClass(), 'fully-checked'),
192 this.getCheckedState() == goog.ui.TriStateMenuItem.State.FULLY_CHECKED);
193 };
194
195
196 // Register a decorator factory function for goog.ui.TriStateMenuItemRenderer.
197 goog.ui.registry.setDecoratorByClassName(
198 goog.ui.TriStateMenuItemRenderer.CSS_CLASS, function() {
199 // TriStateMenuItem defaults to using TriStateMenuItemRenderer.
200 return new goog.ui.TriStateMenuItem(null);
201 });