Debian Codegraph @

closure-library/closure/goog/ui/rangemodel.js

raw
1 // Copyright 2006 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 Implementation of a range model. This is an implementation of
17 * the BoundedRangeModel as described by Java at
18 * http://java.sun.com/javase/6/docs/api/javax/swing/BoundedRangeModel.html.
19 *
20 * One good way to understand the range model is to think of a scroll bar for
21 * a scrollable element. In that case minimum is 0, maximum is scrollHeight,
22 * value is scrollTop and extent is clientHeight.
23 *
24 * Based on http://webfx.eae.net/dhtml/slider/js/range.js
25 *
26 * @author [email protected] (Erik Arvidsson)
27 */
28
29 goog.provide('goog.ui.RangeModel');
30
31 goog.require('goog.events.EventTarget');
32 goog.require('goog.ui.Component');
33
34
35
36 /**
37 * Creates a range model
38 * @extends {goog.events.EventTarget}
39 * @constructor
40 */
41 goog.ui.RangeModel = function() {
42 goog.events.EventTarget.call(this);
43 };
44 goog.inherits(goog.ui.RangeModel, goog.events.EventTarget);
45 goog.tagUnsealableClass(goog.ui.RangeModel);
46
47
48 /**
49 * @type {number}
50 * @private
51 */
52 goog.ui.RangeModel.prototype.value_ = 0;
53
54
55 /**
56 * @type {number}
57 * @private
58 */
59 goog.ui.RangeModel.prototype.minimum_ = 0;
60
61
62 /**
63 * @type {number}
64 * @private
65 */
66 goog.ui.RangeModel.prototype.maximum_ = 100;
67
68
69 /**
70 * @type {number}
71 * @private
72 */
73 goog.ui.RangeModel.prototype.extent_ = 0;
74
75
76 /**
77 * @type {?number}
78 * @private
79 */
80 goog.ui.RangeModel.prototype.step_ = 1;
81
82
83 /**
84 * This is true if something is changed as a side effect. This happens when for
85 * example we set the maximum below the current value.
86 * @type {boolean}
87 * @private
88 */
89 goog.ui.RangeModel.prototype.isChanging_ = false;
90
91
92 /**
93 * If set to true, we do not fire any change events.
94 * @type {boolean}
95 * @private
96 */
97 goog.ui.RangeModel.prototype.mute_ = false;
98
99
100 /**
101 * Sets the model to mute / unmute.
102 * @param {boolean} muteValue Whether or not to mute the range, i.e.,
103 * suppress any CHANGE events.
104 */
105 goog.ui.RangeModel.prototype.setMute = function(muteValue) {
106 this.mute_ = muteValue;
107 };
108
109
110 /**
111 * Sets the value.
112 * @param {number} value The new value.
113 */
114 goog.ui.RangeModel.prototype.setValue = function(value) {
115 value = this.roundToStepWithMin(value);
116 if (this.value_ != value) {
117 if (value + this.extent_ > this.maximum_) {
118 this.value_ = this.maximum_ - this.extent_;
119 } else if (value < this.minimum_) {
120 this.value_ = this.minimum_;
121 } else {
122 this.value_ = value;
123 }
124 if (!this.isChanging_ && !this.mute_) {
125 this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
126 }
127 }
128 };
129
130
131 /**
132 * @return {number} the current value.
133 */
134 goog.ui.RangeModel.prototype.getValue = function() {
135 return this.roundToStepWithMin(this.value_);
136 };
137
138
139 /**
140 * Sets the extent. The extent is the 'size' of the value.
141 * @param {number} extent The new extent.
142 */
143 goog.ui.RangeModel.prototype.setExtent = function(extent) {
144 extent = this.roundToStepWithMin(extent);
145 if (this.extent_ != extent) {
146 if (extent < 0) {
147 this.extent_ = 0;
148 } else if (this.value_ + extent > this.maximum_) {
149 this.extent_ = this.maximum_ - this.value_;
150 } else {
151 this.extent_ = extent;
152 }
153 if (!this.isChanging_ && !this.mute_) {
154 this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
155 }
156 }
157 };
158
159
160 /**
161 * @return {number} The extent for the range model.
162 */
163 goog.ui.RangeModel.prototype.getExtent = function() {
164 return this.roundToStep(this.extent_);
165 };
166
167
168 /**
169 * Sets the minimum
170 * @param {number} minimum The new minimum.
171 */
172 goog.ui.RangeModel.prototype.setMinimum = function(minimum) {
173 // Don't round minimum because it is the base
174 if (this.minimum_ != minimum) {
175 var oldIsChanging = this.isChanging_;
176 this.isChanging_ = true;
177
178 this.minimum_ = minimum;
179
180 if (minimum + this.extent_ > this.maximum_) {
181 this.extent_ = this.maximum_ - this.minimum_;
182 }
183 if (minimum > this.value_) {
184 this.setValue(minimum);
185 }
186 if (minimum > this.maximum_) {
187 this.extent_ = 0;
188 this.setMaximum(minimum);
189 this.setValue(minimum);
190 }
191
192
193 this.isChanging_ = oldIsChanging;
194 if (!this.isChanging_ && !this.mute_) {
195 this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
196 }
197 }
198 };
199
200
201 /**
202 * @return {number} The minimum value for the range model.
203 */
204 goog.ui.RangeModel.prototype.getMinimum = function() {
205 return this.roundToStepWithMin(this.minimum_);
206 };
207
208
209 /**
210 * Sets the maximum
211 * @param {number} maximum The new maximum.
212 */
213 goog.ui.RangeModel.prototype.setMaximum = function(maximum) {
214 maximum = this.roundToStepWithMin(maximum);
215 if (this.maximum_ != maximum) {
216 var oldIsChanging = this.isChanging_;
217 this.isChanging_ = true;
218
219 this.maximum_ = maximum;
220
221 if (maximum < this.value_ + this.extent_) {
222 this.setValue(maximum - this.extent_);
223 }
224 if (maximum < this.minimum_) {
225 this.extent_ = 0;
226 this.setMinimum(maximum);
227 this.setValue(this.maximum_);
228 }
229 if (maximum < this.minimum_ + this.extent_) {
230 this.extent_ = this.maximum_ - this.minimum_;
231 }
232
233 this.isChanging_ = oldIsChanging;
234 if (!this.isChanging_ && !this.mute_) {
235 this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
236 }
237 }
238 };
239
240
241 /**
242 * @return {number} The maximimum value for the range model.
243 */
244 goog.ui.RangeModel.prototype.getMaximum = function() {
245 return this.roundToStepWithMin(this.maximum_);
246 };
247
248
249 /**
250 * Returns the step value. The step value is used to determine how to round the
251 * value.
252 * @return {?number} The maximimum value for the range model.
253 */
254 goog.ui.RangeModel.prototype.getStep = function() {
255 return this.step_;
256 };
257
258
259 /**
260 * Sets the step. The step value is used to determine how to round the value.
261 * @param {?number} step The step size.
262 */
263 goog.ui.RangeModel.prototype.setStep = function(step) {
264 if (this.step_ != step) {
265 this.step_ = step;
266
267 // adjust value, extent and maximum
268 var oldIsChanging = this.isChanging_;
269 this.isChanging_ = true;
270
271 this.setMaximum(this.getMaximum());
272 this.setExtent(this.getExtent());
273 this.setValue(this.getValue());
274
275 this.isChanging_ = oldIsChanging;
276 if (!this.isChanging_ && !this.mute_) {
277 this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
278 }
279 }
280 };
281
282
283 /**
284 * Rounds to the closest step using the minimum value as the base.
285 * @param {number} value The number to round.
286 * @return {number} The number rounded to the closest step.
287 */
288 goog.ui.RangeModel.prototype.roundToStepWithMin = function(value) {
289 if (this.step_ == null) return value;
290 return this.minimum_ +
291 Math.round((value - this.minimum_) / this.step_) * this.step_;
292 };
293
294
295 /**
296 * Rounds to the closest step.
297 * @param {number} value The number to round.
298 * @return {number} The number rounded to the closest step.
299 */
300 goog.ui.RangeModel.prototype.roundToStep = function(value) {
301 if (this.step_ == null) return value;
302 return Math.round(value / this.step_) * this.step_;
303 };