1 /* 2 Copyright 2008-2016 3 Matthias Ehmann, 4 Michael Gerhaeuser, 5 Carsten Miller, 6 Bianca Valentin, 7 Alfred Wassermann, 8 Peter Wilfahrt 9 10 This file is part of JSXGraph. 11 12 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 13 14 You can redistribute it and/or modify it under the terms of the 15 16 * GNU Lesser General Public License as published by 17 the Free Software Foundation, either version 3 of the License, or 18 (at your option) any later version 19 OR 20 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 21 22 JSXGraph is distributed in the hope that it will be useful, 23 but WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 GNU Lesser General Public License for more details. 26 27 You should have received a copy of the GNU Lesser General Public License and 28 the MIT License along with JSXGraph. If not, see <http://www.gnu.org/licenses/> 29 and <http://opensource.org/licenses/MIT/>. 30 */ 31 32 33 /*global JXG: true, define: true*/ 34 /*jslint nomen: true, plusplus: true*/ 35 36 /* depends: 37 jxg 38 math/math 39 base/constants 40 base/point 41 utils/type 42 elements: 43 point 44 group 45 segment 46 ticks 47 glider 48 text 49 */ 50 51 /** 52 * @fileoverview The geometry object slider is defined in this file. Slider stores all 53 * style and functional properties that are required to draw and use a slider on 54 * a board. 55 */ 56 57 define([ 58 'jxg', 'math/math', 'base/constants', 'utils/type', 'base/point', 'base/group', 59 'base/element', 'base/line', 'base/ticks', 'base/text' 60 ], function (JXG, Mat, Const, Type, Point, Group, GeometryElement, Line, Ticks, Text) { 61 62 "use strict"; 63 64 /** 65 * @class A slider can be used to choose values from a given range of numbers. 66 * @pseudo 67 * @description 68 * @name Slider 69 * @augments Glider 70 * @constructor 71 * @type JXG.Point 72 * @throws {Exception} If the element cannot be constructed with the given parent objects an exception is thrown. 73 * @param {Array_Array_Array} start,end,data The first two arrays give the start and the end where the slider is drawn 74 * on the board. The third array gives the start and the end of the range the slider operates as the first resp. the 75 * third component of the array. The second component of the third array gives its start value. 76 * @example 77 * // Create a slider with values between 1 and 10, initial position is 5. 78 * var s = board.create('slider', [[1, 2], [3, 2], [1, 5, 10]]); 79 * </pre><div class="jxgbox"id="cfb51cde-2603-4f18-9cc4-1afb452b374d" style="width: 200px; height: 200px;"></div> 80 * <script type="text/javascript"> 81 * (function () { 82 * var board = JXG.JSXGraph.initBoard('cfb51cde-2603-4f18-9cc4-1afb452b374d', {boundingbox: [-1, 5, 5, -1], axis: true, showcopyright: false, shownavigation: false}); 83 * var s = board.create('slider', [[1, 2], [3, 2], [1, 5, 10]]); 84 * })(); 85 * </script><pre> 86 * @example 87 * // Create a slider taking integer values between 1 and 50. Initial value is 50. 88 * var s = board.create('slider', [[1, 3], [3, 1], [1, 10, 50]], {snapWidth: 1}); 89 * </pre><div class="jxgbox"id="e17128e6-a25d-462a-9074-49460b0d66f4" style="width: 200px; height: 200px;"></div> 90 * <script type="text/javascript"> 91 * (function () { 92 * var board = JXG.JSXGraph.initBoard('e17128e6-a25d-462a-9074-49460b0d66f4', {boundingbox: [-1, 5, 5, -1], axis: true, showcopyright: false, shownavigation: false}); 93 * var s = board.create('slider', [[1, 3], [3, 1], [1, 10, 50]], {snapWidth: 1}); 94 * })(); 95 * </script><pre> 96 */ 97 JXG.createSlider = function (board, parents, attributes) { 98 var pos0, pos1, smin, start, smax, sdiff, 99 p1, p2, l1, ticks, ti, startx, starty, p3, l2, t, 100 g, 101 withText, withTicks, snapWidth, attr, precision, el; 102 103 attr = Type.copyAttributes(attributes, board.options, 'slider'); 104 withTicks = attr.withticks; 105 withText = attr.withlabel; 106 snapWidth = attr.snapwidth; 107 precision = attr.precision; 108 109 // start point 110 attr = Type.copyAttributes(attributes, board.options, 'slider', 'point1'); 111 p1 = board.create('point', parents[0], attr); 112 113 // end point 114 attr = Type.copyAttributes(attributes, board.options, 'slider', 'point2'); 115 p2 = board.create('point', parents[1], attr); 116 //g = board.create('group', [p1, p2]); 117 118 // slide line 119 attr = Type.copyAttributes(attributes, board.options, 'slider', 'baseline'); 120 l1 = board.create('segment', [p1, p2], attr); 121 122 // this is required for a correct projection of the glider onto the segment below 123 l1.updateStdform(); 124 125 pos0 = p1.coords.usrCoords.slice(1); 126 pos1 = p2.coords.usrCoords.slice(1); 127 smin = parents[2][0]; 128 start = parents[2][1]; 129 smax = parents[2][2]; 130 sdiff = smax - smin; 131 132 if (withTicks) { 133 attr = Type.copyAttributes(attributes, board.options, 'slider', 'ticks'); 134 ticks = 2; 135 ti = board.create('ticks', [ 136 l1, 137 p2.Dist(p1) / ticks, 138 139 function (tick) { 140 var dFull = p1.Dist(p2), 141 d = p1.coords.distance(Const.COORDS_BY_USER, tick); 142 143 if (dFull < Mat.eps) { 144 return 0; 145 } 146 147 return d / dFull * sdiff + smin; 148 } 149 ], attr); 150 } 151 152 startx = pos0[0] + (pos1[0] - pos0[0]) * (start - smin) / (smax - smin); 153 starty = pos0[1] + (pos1[1] - pos0[1]) * (start - smin) / (smax - smin); 154 155 // glider point 156 attr = Type.copyAttributes(attributes, board.options, 'slider'); 157 // overwrite this in any case; the sliders label is a special text element, not the gliders label. 158 // this will be set back to true after the text was created (and only if withlabel was true initially). 159 attr.withLabel = false; 160 // gliders set snapwidth=-1 by default (i.e. deactivate them) 161 p3 = board.create('glider', [startx, starty, l1], attr); 162 p3.setAttribute({snapwidth: snapWidth}); 163 164 // segment from start point to glider point 165 attr = Type.copyAttributes(attributes, board.options, 'slider', 'highline'); 166 l2 = board.create('segment', [p1, p3], attr); 167 168 /** 169 * Returns the current slider value. 170 * @memberOf Slider.prototype 171 * @name Value 172 * @returns {Number} 173 */ 174 p3.Value = function () { 175 var sdiff = this._smax - this._smin; 176 return p3.visProp.snapwidth === -1 ? this.position * sdiff + this._smin : Math.round((this.position * sdiff + this._smin) / this.visProp.snapwidth) * this.visProp.snapwidth; 177 }; 178 179 p3.methodMap = Type.deepCopy(p3.methodMap, { 180 Value: 'Value', 181 setValue: 'setValue', 182 smax: '_smax', 183 smin: '_smin', 184 setMax: 'setMax', 185 setMin: 'setMin' 186 }); 187 188 /** 189 * End value of the slider range. 190 * @memberOf Slider.prototype 191 * @name _smax 192 * @type Number 193 */ 194 p3._smax = smax; 195 196 /** 197 * Start value of the slider range. 198 * @memberOf Slider.prototype 199 * @name _smin 200 * @type Number 201 */ 202 p3._smin = smin; 203 204 /** 205 * Sets the maximum value of the slider. 206 * @memberOf Slider.prototype 207 * @name setMax 208 * @param {Number} val New maximum value 209 * @returns {Object} this object 210 */ 211 p3.setMax = function(val) { 212 this._smax = val; 213 return this; 214 }; 215 216 /** 217 * Sets the value of the slider. This call must be followed 218 * by a board update call. 219 * @memberOf Slider.prototype 220 * @name setValue 221 * @param {Number} val New value 222 * @returns {Object} this object 223 */ 224 p3.setValue = function(val) { 225 var sdiff = this._smax - this._smin; 226 227 if (Math.abs(sdiff) > Mat.eps) { 228 this.position = (val - this._smin) / sdiff; 229 } else { 230 this.position = 0.0; //this._smin; 231 } 232 this.position = Math.max(0.0, Math.min(1.0, this.position)); 233 return this; 234 }; 235 236 /** 237 * Sets the minimum value of the slider. 238 * @memberOf Slider.prototype 239 * @name setMin 240 * @param {Number} val New minimum value 241 * @returns {Object} this object 242 */ 243 p3.setMin = function(val) { 244 this._smin = val; 245 return this; 246 }; 247 248 if (withText) { 249 attr = Type.copyAttributes(attributes, board.options, 'slider', 'label'); 250 t = board.create('text', [ 251 function () { 252 return (p2.X() - p1.X()) * 0.05 + p2.X(); 253 }, 254 function () { 255 return (p2.Y() - p1.Y()) * 0.05 + p2.Y(); 256 }, 257 function () { 258 var n; 259 260 if (p3.name && p3.name !== '') { 261 n = p3.name + ' = '; 262 } else { 263 n = ''; 264 } 265 266 return n + (p3.Value()).toFixed(precision); 267 } 268 ], attr); 269 270 /** 271 * The text element to the right of the slider, indicating its current value. 272 * @memberOf Slider.prototype 273 * @name label 274 * @type JXG.Text 275 */ 276 p3.label = t; 277 278 // reset the withlabel attribute 279 p3.visProp.withlabel = true; 280 p3.hasLabel = true; 281 } 282 283 /** 284 * Start point of the base line. 285 * @memberOf Slider.prototype 286 * @name point1 287 * @type JXG.Point 288 */ 289 p3.point1 = p1; 290 291 /** 292 * End point of the base line. 293 * @memberOf Slider.prototype 294 * @name point2 295 * @type JXG.Point 296 */ 297 p3.point2 = p2; 298 299 /** 300 * The baseline the glider is bound to. 301 * @memberOf Slider.prototype 302 * @name baseline 303 * @type JXG.Line 304 */ 305 p3.baseline = l1; 306 307 /** 308 * A line on top of the baseline, indicating the slider's progress. 309 * @memberOf Slider.prototype 310 * @name highline 311 * @type JXG.Line 312 */ 313 p3.highline = l2; 314 315 if (withTicks) { 316 /** 317 * Ticks give a rough indication about the slider's current value. 318 * @memberOf Slider.prototype 319 * @name ticks 320 * @type JXG.Ticks 321 */ 322 p3.ticks = ti; 323 } 324 325 // override the point's remove method to ensure the removal of all elements 326 p3.remove = function () { 327 if (withText) { 328 board.removeObject(t); 329 } 330 331 board.removeObject(l2); 332 board.removeObject(l1); 333 board.removeObject(p2); 334 board.removeObject(p1); 335 336 337 Point.Point.prototype.remove.call(p3); 338 }; 339 340 p1.dump = false; 341 p2.dump = false; 342 l1.dump = false; 343 l2.dump = false; 344 345 p3.elType = 'slider'; 346 p3.parents = parents; 347 p3.subs = { 348 point1: p1, 349 point2: p2, 350 baseLine: l1, 351 highLine: l2 352 }; 353 354 if (withTicks) { 355 ti.dump = false; 356 p3.subs.ticks = ti; 357 } 358 359 // Save the visibility attribute of the sub-elements 360 for (el in p3.subs) { 361 p3.subs[el].status = { 362 visible: p3.subs[el].visProp.visible 363 }; 364 } 365 366 p3.hideElement = function () { 367 var el; 368 GeometryElement.prototype.hideElement.call(this); 369 370 for (el in this.subs) { 371 this.subs[el].status.visible = this.subs[el].visProp.visible; 372 this.subs[el].hideElement(); 373 } 374 }; 375 376 p3.showElement = function () { 377 var el; 378 GeometryElement.prototype.showElement.call(this); 379 380 for (el in this.subs) { 381 if (this.subs[el].status.visible) { 382 this.subs[el].showElement(); 383 } 384 } 385 }; 386 387 return p3; 388 }; 389 390 JXG.registerElement('slider', JXG.createSlider); 391 392 return { 393 createSlider: JXG.createSlider 394 }; 395 }); 396