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