You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

979 lines
36 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-slider-Thumb'>/**
  19. </span> * @class Ext.slider.Thumb
  20. * @extends Object
  21. * Represents a single thumb element on a Slider. This would not usually be created manually and would instead
  22. * be created internally by an {@link Ext.slider.MultiSlider Ext.Slider}.
  23. */
  24. Ext.slider.Thumb = Ext.extend(Object, {
  25. <span id='Ext-slider-Thumb-property-dragging'> /**
  26. </span> * True while the thumb is in a drag operation
  27. * @type Boolean
  28. */
  29. dragging: false,
  30. <span id='Ext-slider-Thumb-cfg-slider'> /**
  31. </span> * @constructor
  32. * @cfg {Ext.slider.MultiSlider} slider The Slider to render to (required)
  33. */
  34. constructor: function(config) {
  35. <span id='Ext-slider-Thumb-property-slider'> /**
  36. </span> * @property slider
  37. * @type Ext.slider.MultiSlider
  38. * The slider this thumb is contained within
  39. */
  40. Ext.apply(this, config || {}, {
  41. cls: 'x-slider-thumb',
  42. <span id='Ext-slider-Thumb-cfg-constrain'> /**
  43. </span> * @cfg {Boolean} constrain True to constrain the thumb so that it cannot overlap its siblings
  44. */
  45. constrain: false
  46. });
  47. Ext.slider.Thumb.superclass.constructor.call(this, config);
  48. if (this.slider.vertical) {
  49. Ext.apply(this, Ext.slider.Thumb.Vertical);
  50. }
  51. },
  52. <span id='Ext-slider-Thumb-method-render'> /**
  53. </span> * Renders the thumb into a slider
  54. */
  55. render: function() {
  56. this.el = this.slider.innerEl.insertFirst({cls: this.cls});
  57. this.initEvents();
  58. },
  59. <span id='Ext-slider-Thumb-method-enable'> /**
  60. </span> * Enables the thumb if it is currently disabled
  61. */
  62. enable: function() {
  63. this.disabled = false;
  64. this.el.removeClass(this.slider.disabledClass);
  65. },
  66. <span id='Ext-slider-Thumb-method-disable'> /**
  67. </span> * Disables the thumb if it is currently enabled
  68. */
  69. disable: function() {
  70. this.disabled = true;
  71. this.el.addClass(this.slider.disabledClass);
  72. },
  73. <span id='Ext-slider-Thumb-method-initEvents'> /**
  74. </span> * Sets up an Ext.dd.DragTracker for this thumb
  75. */
  76. initEvents: function() {
  77. var el = this.el;
  78. el.addClassOnOver('x-slider-thumb-over');
  79. this.tracker = new Ext.dd.DragTracker({
  80. onBeforeStart: this.onBeforeDragStart.createDelegate(this),
  81. onStart : this.onDragStart.createDelegate(this),
  82. onDrag : this.onDrag.createDelegate(this),
  83. onEnd : this.onDragEnd.createDelegate(this),
  84. tolerance : 3,
  85. autoStart : 300
  86. });
  87. this.tracker.initEl(el);
  88. },
  89. <span id='Ext-slider-Thumb-method-onBeforeDragStart'> /**
  90. </span> * @private
  91. * This is tied into the internal Ext.dd.DragTracker. If the slider is currently disabled,
  92. * this returns false to disable the DragTracker too.
  93. * @return {Boolean} False if the slider is currently disabled
  94. */
  95. onBeforeDragStart : function(e) {
  96. if (this.disabled) {
  97. return false;
  98. } else {
  99. this.slider.promoteThumb(this);
  100. return true;
  101. }
  102. },
  103. <span id='Ext-slider-Thumb-method-onDragStart'> /**
  104. </span> * @private
  105. * This is tied into the internal Ext.dd.DragTracker's onStart template method. Adds the drag CSS class
  106. * to the thumb and fires the 'dragstart' event
  107. */
  108. onDragStart: function(e){
  109. this.el.addClass('x-slider-thumb-drag');
  110. this.dragging = true;
  111. this.dragStartValue = this.value;
  112. this.slider.fireEvent('dragstart', this.slider, e, this);
  113. },
  114. <span id='Ext-slider-Thumb-method-onDrag'> /**
  115. </span> * @private
  116. * This is tied into the internal Ext.dd.DragTracker's onDrag template method. This is called every time
  117. * the DragTracker detects a drag movement. It updates the Slider's value using the position of the drag
  118. */
  119. onDrag: function(e) {
  120. var slider = this.slider,
  121. index = this.index,
  122. newValue = this.getNewValue();
  123. if (this.constrain) {
  124. var above = slider.thumbs[index + 1],
  125. below = slider.thumbs[index - 1];
  126. if (below != undefined &amp;&amp; newValue &lt;= below.value) newValue = below.value;
  127. if (above != undefined &amp;&amp; newValue &gt;= above.value) newValue = above.value;
  128. }
  129. slider.setValue(index, newValue, false);
  130. slider.fireEvent('drag', slider, e, this);
  131. },
  132. <span id='Ext-slider-Thumb-method-getNewValue'> getNewValue: function() {
  133. </span> var slider = this.slider,
  134. pos = slider.innerEl.translatePoints(this.tracker.getXY());
  135. return Ext.util.Format.round(slider.reverseValue(pos.left), slider.decimalPrecision);
  136. },
  137. <span id='Ext-slider-Thumb-method-onDragEnd'> /**
  138. </span> * @private
  139. * This is tied to the internal Ext.dd.DragTracker's onEnd template method. Removes the drag CSS class and
  140. * fires the 'changecomplete' event with the new value
  141. */
  142. onDragEnd: function(e) {
  143. var slider = this.slider,
  144. value = this.value;
  145. this.el.removeClass('x-slider-thumb-drag');
  146. this.dragging = false;
  147. slider.fireEvent('dragend', slider, e);
  148. if (this.dragStartValue != value) {
  149. slider.fireEvent('changecomplete', slider, value, this);
  150. }
  151. },
  152. <span id='Ext-slider-Thumb-method-destroy'> /**
  153. </span> * @private
  154. * Destroys the thumb
  155. */
  156. destroy: function(){
  157. Ext.destroyMembers(this, 'tracker', 'el');
  158. }
  159. });
  160. <span id='Ext-slider-MultiSlider'>/**
  161. </span> * @class Ext.slider.MultiSlider
  162. * @extends Ext.BoxComponent
  163. * Slider which supports vertical or horizontal orientation, keyboard adjustments, configurable snapping, axis clicking and animation. Can be added as an item to any container. Example usage:
  164. &lt;pre&gt;
  165. new Ext.Slider({
  166. renderTo: Ext.getBody(),
  167. width: 200,
  168. value: 50,
  169. increment: 10,
  170. minValue: 0,
  171. maxValue: 100
  172. });
  173. &lt;/pre&gt;
  174. * Sliders can be created with more than one thumb handle by passing an array of values instead of a single one:
  175. &lt;pre&gt;
  176. new Ext.Slider({
  177. renderTo: Ext.getBody(),
  178. width: 200,
  179. values: [25, 50, 75],
  180. minValue: 0,
  181. maxValue: 100,
  182. //this defaults to true, setting to false allows the thumbs to pass each other
  183. {@link #constrainThumbs}: false
  184. });
  185. &lt;/pre&gt;
  186. */
  187. Ext.slider.MultiSlider = Ext.extend(Ext.BoxComponent, {
  188. <span id='Ext-slider-MultiSlider-cfg-value'> /**
  189. </span> * @cfg {Number} value The value to initialize the slider with. Defaults to minValue.
  190. */
  191. <span id='Ext-slider-MultiSlider-cfg-vertical'> /**
  192. </span> * @cfg {Boolean} vertical Orient the Slider vertically rather than horizontally, defaults to false.
  193. */
  194. vertical: false,
  195. <span id='Ext-slider-MultiSlider-cfg-minValue'> /**
  196. </span> * @cfg {Number} minValue The minimum value for the Slider. Defaults to 0.
  197. */
  198. minValue: 0,
  199. <span id='Ext-slider-MultiSlider-cfg-maxValue'> /**
  200. </span> * @cfg {Number} maxValue The maximum value for the Slider. Defaults to 100.
  201. */
  202. maxValue: 100,
  203. <span id='Ext-slider-MultiSlider-cfg-decimalPrecision'> /**
  204. </span> * @cfg {Number/Boolean} decimalPrecision.
  205. * &lt;p&gt;The number of decimal places to which to round the Slider's value. Defaults to 0.&lt;/p&gt;
  206. * &lt;p&gt;To disable rounding, configure as &lt;tt&gt;&lt;b&gt;false&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;
  207. */
  208. decimalPrecision: 0,
  209. <span id='Ext-slider-MultiSlider-cfg-keyIncrement'> /**
  210. </span> * @cfg {Number} keyIncrement How many units to change the Slider when adjusting with keyboard navigation. Defaults to 1. If the increment config is larger, it will be used instead.
  211. */
  212. keyIncrement: 1,
  213. <span id='Ext-slider-MultiSlider-cfg-increment'> /**
  214. </span> * @cfg {Number} increment How many units to change the slider when adjusting by drag and drop. Use this option to enable 'snapping'.
  215. */
  216. increment: 0,
  217. <span id='Ext-slider-MultiSlider-property-clickRange'> /**
  218. </span> * @private
  219. * @property clickRange
  220. * @type Array
  221. * Determines whether or not a click to the slider component is considered to be a user request to change the value. Specified as an array of [top, bottom],
  222. * the click event's 'top' property is compared to these numbers and the click only considered a change request if it falls within them. e.g. if the 'top'
  223. * value of the click event is 4 or 16, the click is not considered a change request as it falls outside of the [5, 15] range
  224. */
  225. clickRange: [5,15],
  226. <span id='Ext-slider-MultiSlider-cfg-clickToChange'> /**
  227. </span> * @cfg {Boolean} clickToChange Determines whether or not clicking on the Slider axis will change the slider. Defaults to true
  228. */
  229. clickToChange : true,
  230. <span id='Ext-slider-MultiSlider-cfg-animate'> /**
  231. </span> * @cfg {Boolean} animate Turn on or off animation. Defaults to true
  232. */
  233. animate: true,
  234. <span id='Ext-slider-MultiSlider-cfg-constrainThumbs'> /**
  235. </span> * @cfg {Boolean} constrainThumbs True to disallow thumbs from overlapping one another. Defaults to true
  236. */
  237. constrainThumbs: true,
  238. <span id='Ext-slider-MultiSlider-property-topThumbZIndex'> /**
  239. </span> * @private
  240. * @property topThumbZIndex
  241. * @type Number
  242. * The number used internally to set the z index of the top thumb (see promoteThumb for details)
  243. */
  244. topThumbZIndex: 10000,
  245. <span id='Ext-slider-MultiSlider-method-initComponent'> // private override
  246. </span> initComponent : function(){
  247. if(!Ext.isDefined(this.value)){
  248. this.value = this.minValue;
  249. }
  250. <span id='Ext-slider-MultiSlider-property-thumbs'> /**
  251. </span> * @property thumbs
  252. * @type Array
  253. * Array containing references to each thumb
  254. */
  255. this.thumbs = [];
  256. Ext.slider.MultiSlider.superclass.initComponent.call(this);
  257. this.keyIncrement = Math.max(this.increment, this.keyIncrement);
  258. this.addEvents(
  259. <span id='Ext-slider-MultiSlider-event-beforechange'> /**
  260. </span> * @event beforechange
  261. * Fires before the slider value is changed. By returning false from an event handler,
  262. * you can cancel the event and prevent the slider from changing.
  263. * @param {Ext.slider.MultiSlider} slider The slider
  264. * @param {Number} newValue The new value which the slider is being changed to.
  265. * @param {Number} oldValue The old value which the slider was previously.
  266. */
  267. 'beforechange',
  268. <span id='Ext-slider-MultiSlider-event-change'> /**
  269. </span> * @event change
  270. * Fires when the slider value is changed.
  271. * @param {Ext.slider.MultiSlider} slider The slider
  272. * @param {Number} newValue The new value which the slider has been changed to.
  273. * @param {Ext.slider.Thumb} thumb The thumb that was changed
  274. */
  275. 'change',
  276. <span id='Ext-slider-MultiSlider-event-changecomplete'> /**
  277. </span> * @event changecomplete
  278. * Fires when the slider value is changed by the user and any drag operations have completed.
  279. * @param {Ext.slider.MultiSlider} slider The slider
  280. * @param {Number} newValue The new value which the slider has been changed to.
  281. * @param {Ext.slider.Thumb} thumb The thumb that was changed
  282. */
  283. 'changecomplete',
  284. <span id='Ext-slider-MultiSlider-event-dragstart'> /**
  285. </span> * @event dragstart
  286. * Fires after a drag operation has started.
  287. * @param {Ext.slider.MultiSlider} slider The slider
  288. * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
  289. */
  290. 'dragstart',
  291. <span id='Ext-slider-MultiSlider-event-drag'> /**
  292. </span> * @event drag
  293. * Fires continuously during the drag operation while the mouse is moving.
  294. * @param {Ext.slider.MultiSlider} slider The slider
  295. * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
  296. */
  297. 'drag',
  298. <span id='Ext-slider-MultiSlider-event-dragend'> /**
  299. </span> * @event dragend
  300. * Fires after the drag operation has completed.
  301. * @param {Ext.slider.MultiSlider} slider The slider
  302. * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
  303. */
  304. 'dragend'
  305. );
  306. <span id='Ext-slider-MultiSlider-property-values'> /**
  307. </span> * @property values
  308. * @type Array
  309. * Array of values to initalize the thumbs with
  310. */
  311. if (this.values == undefined || Ext.isEmpty(this.values)) this.values = [0];
  312. var values = this.values;
  313. for (var i=0; i &lt; values.length; i++) {
  314. this.addThumb(values[i]);
  315. }
  316. if(this.vertical){
  317. Ext.apply(this, Ext.slider.Vertical);
  318. }
  319. },
  320. <span id='Ext-slider-MultiSlider-method-addThumb'> /**
  321. </span> * Creates a new thumb and adds it to the slider
  322. * @param {Number} value The initial value to set on the thumb. Defaults to 0
  323. */
  324. addThumb: function(value) {
  325. var thumb = new Ext.slider.Thumb({
  326. value : value,
  327. slider : this,
  328. index : this.thumbs.length,
  329. constrain: this.constrainThumbs
  330. });
  331. this.thumbs.push(thumb);
  332. //render the thumb now if needed
  333. if (this.rendered) thumb.render();
  334. },
  335. <span id='Ext-slider-MultiSlider-method-promoteThumb'> /**
  336. </span> * @private
  337. * Moves the given thumb above all other by increasing its z-index. This is called when as drag
  338. * any thumb, so that the thumb that was just dragged is always at the highest z-index. This is
  339. * required when the thumbs are stacked on top of each other at one of the ends of the slider's
  340. * range, which can result in the user not being able to move any of them.
  341. * @param {Ext.slider.Thumb} topThumb The thumb to move to the top
  342. */
  343. promoteThumb: function(topThumb) {
  344. var thumbs = this.thumbs,
  345. zIndex, thumb;
  346. for (var i = 0, j = thumbs.length; i &lt; j; i++) {
  347. thumb = thumbs[i];
  348. if (thumb == topThumb) {
  349. zIndex = this.topThumbZIndex;
  350. } else {
  351. zIndex = '';
  352. }
  353. thumb.el.setStyle('zIndex', zIndex);
  354. }
  355. },
  356. <span id='Ext-slider-MultiSlider-method-onRender'> // private override
  357. </span> onRender : function() {
  358. this.autoEl = {
  359. cls: 'x-slider ' + (this.vertical ? 'x-slider-vert' : 'x-slider-horz'),
  360. cn : {
  361. cls: 'x-slider-end',
  362. cn : {
  363. cls:'x-slider-inner',
  364. cn : [{tag:'a', cls:'x-slider-focus', href:&quot;#&quot;, tabIndex: '-1', hidefocus:'on'}]
  365. }
  366. }
  367. };
  368. Ext.slider.MultiSlider.superclass.onRender.apply(this, arguments);
  369. this.endEl = this.el.first();
  370. this.innerEl = this.endEl.first();
  371. this.focusEl = this.innerEl.child('.x-slider-focus');
  372. //render each thumb
  373. for (var i=0; i &lt; this.thumbs.length; i++) {
  374. this.thumbs[i].render();
  375. }
  376. //calculate the size of half a thumb
  377. var thumb = this.innerEl.child('.x-slider-thumb');
  378. this.halfThumb = (this.vertical ? thumb.getHeight() : thumb.getWidth()) / 2;
  379. this.initEvents();
  380. },
  381. <span id='Ext-slider-MultiSlider-method-initEvents'> /**
  382. </span> * @private
  383. * Adds keyboard and mouse listeners on this.el. Ignores click events on the internal focus element.
  384. * Creates a new DragTracker which is used to control what happens when the user drags the thumb around.
  385. */
  386. initEvents : function(){
  387. this.mon(this.el, {
  388. scope : this,
  389. mousedown: this.onMouseDown,
  390. keydown : this.onKeyDown
  391. });
  392. this.focusEl.swallowEvent(&quot;click&quot;, true);
  393. },
  394. <span id='Ext-slider-MultiSlider-method-onMouseDown'> /**
  395. </span> * @private
  396. * Mousedown handler for the slider. If the clickToChange is enabled and the click was not on the draggable 'thumb',
  397. * this calculates the new value of the slider and tells the implementation (Horizontal or Vertical) to move the thumb
  398. * @param {Ext.EventObject} e The click event
  399. */
  400. onMouseDown : function(e){
  401. if(this.disabled){
  402. return;
  403. }
  404. //see if the click was on any of the thumbs
  405. var thumbClicked = false;
  406. for (var i=0; i &lt; this.thumbs.length; i++) {
  407. thumbClicked = thumbClicked || e.target == this.thumbs[i].el.dom;
  408. }
  409. if (this.clickToChange &amp;&amp; !thumbClicked) {
  410. var local = this.innerEl.translatePoints(e.getXY());
  411. this.onClickChange(local);
  412. }
  413. this.focus();
  414. },
  415. <span id='Ext-slider-MultiSlider-method-onClickChange'> /**
  416. </span> * @private
  417. * Moves the thumb to the indicated position. Note that a Vertical implementation is provided in Ext.slider.Vertical.
  418. * Only changes the value if the click was within this.clickRange.
  419. * @param {Object} local Object containing top and left values for the click event.
  420. */
  421. onClickChange : function(local) {
  422. if (local.top &gt; this.clickRange[0] &amp;&amp; local.top &lt; this.clickRange[1]) {
  423. //find the nearest thumb to the click event
  424. var thumb = this.getNearest(local, 'left'),
  425. index = thumb.index;
  426. this.setValue(index, Ext.util.Format.round(this.reverseValue(local.left), this.decimalPrecision), undefined, true);
  427. }
  428. },
  429. <span id='Ext-slider-MultiSlider-method-getNearest'> /**
  430. </span> * @private
  431. * Returns the nearest thumb to a click event, along with its distance
  432. * @param {Object} local Object containing top and left values from a click event
  433. * @param {String} prop The property of local to compare on. Use 'left' for horizontal sliders, 'top' for vertical ones
  434. * @return {Object} The closest thumb object and its distance from the click event
  435. */
  436. getNearest: function(local, prop) {
  437. var localValue = prop == 'top' ? this.innerEl.getHeight() - local[prop] : local[prop],
  438. clickValue = this.reverseValue(localValue),
  439. nearestDistance = (this.maxValue - this.minValue) + 5, //add a small fudge for the end of the slider
  440. index = 0,
  441. nearest = null;
  442. for (var i=0; i &lt; this.thumbs.length; i++) {
  443. var thumb = this.thumbs[i],
  444. value = thumb.value,
  445. dist = Math.abs(value - clickValue);
  446. if (Math.abs(dist &lt;= nearestDistance)) {
  447. nearest = thumb;
  448. index = i;
  449. nearestDistance = dist;
  450. }
  451. }
  452. return nearest;
  453. },
  454. <span id='Ext-slider-MultiSlider-method-onKeyDown'> /**
  455. </span> * @private
  456. * Handler for any keypresses captured by the slider. If the key is UP or RIGHT, the thumb is moved along to the right
  457. * by this.keyIncrement. If DOWN or LEFT it is moved left. Pressing CTRL moves the slider to the end in either direction
  458. * @param {Ext.EventObject} e The Event object
  459. */
  460. onKeyDown : function(e){
  461. /*
  462. * The behaviour for keyboard handling with multiple thumbs is currently undefined.
  463. * There's no real sane default for it, so leave it like this until we come up
  464. * with a better way of doing it.
  465. */
  466. if(this.disabled || this.thumbs.length !== 1){
  467. e.preventDefault();
  468. return;
  469. }
  470. var k = e.getKey(),
  471. val;
  472. switch(k){
  473. case e.UP:
  474. case e.RIGHT:
  475. e.stopEvent();
  476. val = e.ctrlKey ? this.maxValue : this.getValue(0) + this.keyIncrement;
  477. this.setValue(0, val, undefined, true);
  478. break;
  479. case e.DOWN:
  480. case e.LEFT:
  481. e.stopEvent();
  482. val = e.ctrlKey ? this.minValue : this.getValue(0) - this.keyIncrement;
  483. this.setValue(0, val, undefined, true);
  484. break;
  485. default:
  486. e.preventDefault();
  487. }
  488. },
  489. <span id='Ext-slider-MultiSlider-method-doSnap'> /**
  490. </span> * @private
  491. * If using snapping, this takes a desired new value and returns the closest snapped
  492. * value to it
  493. * @param {Number} value The unsnapped value
  494. * @return {Number} The value of the nearest snap target
  495. */
  496. doSnap : function(value){
  497. if (!(this.increment &amp;&amp; value)) {
  498. return value;
  499. }
  500. var newValue = value,
  501. inc = this.increment,
  502. m = value % inc;
  503. if (m != 0) {
  504. newValue -= m;
  505. if (m * 2 &gt;= inc) {
  506. newValue += inc;
  507. } else if (m * 2 &lt; -inc) {
  508. newValue -= inc;
  509. }
  510. }
  511. return newValue.constrain(this.minValue, this.maxValue);
  512. },
  513. <span id='Ext-slider-MultiSlider-method-afterRender'> // private
  514. </span> afterRender : function(){
  515. Ext.slider.MultiSlider.superclass.afterRender.apply(this, arguments);
  516. for (var i=0; i &lt; this.thumbs.length; i++) {
  517. var thumb = this.thumbs[i];
  518. if (thumb.value !== undefined) {
  519. var v = this.normalizeValue(thumb.value);
  520. if (v !== thumb.value) {
  521. // delete this.value;
  522. this.setValue(i, v, false);
  523. } else {
  524. this.moveThumb(i, this.translateValue(v), false);
  525. }
  526. }
  527. };
  528. },
  529. <span id='Ext-slider-MultiSlider-method-getRatio'> /**
  530. </span> * @private
  531. * Returns the ratio of pixels to mapped values. e.g. if the slider is 200px wide and maxValue - minValue is 100,
  532. * the ratio is 2
  533. * @return {Number} The ratio of pixels to mapped values
  534. */
  535. getRatio : function(){
  536. var w = this.innerEl.getWidth(),
  537. v = this.maxValue - this.minValue;
  538. return v == 0 ? w : (w/v);
  539. },
  540. <span id='Ext-slider-MultiSlider-method-normalizeValue'> /**
  541. </span> * @private
  542. * Returns a snapped, constrained value when given a desired value
  543. * @param {Number} value Raw number value
  544. * @return {Number} The raw value rounded to the correct d.p. and constrained within the set max and min values
  545. */
  546. normalizeValue : function(v){
  547. v = this.doSnap(v);
  548. v = Ext.util.Format.round(v, this.decimalPrecision);
  549. v = v.constrain(this.minValue, this.maxValue);
  550. return v;
  551. },
  552. <span id='Ext-slider-MultiSlider-method-setMinValue'> /**
  553. </span> * Sets the minimum value for the slider instance. If the current value is less than the
  554. * minimum value, the current value will be changed.
  555. * @param {Number} val The new minimum value
  556. */
  557. setMinValue : function(val){
  558. this.minValue = val;
  559. var i = 0,
  560. thumbs = this.thumbs,
  561. len = thumbs.length,
  562. t;
  563. for(; i &lt; len; ++i){
  564. t = thumbs[i];
  565. t.value = t.value &lt; val ? val : t.value;
  566. }
  567. this.syncThumb();
  568. },
  569. <span id='Ext-slider-MultiSlider-method-setMaxValue'> /**
  570. </span> * Sets the maximum value for the slider instance. If the current value is more than the
  571. * maximum value, the current value will be changed.
  572. * @param {Number} val The new maximum value
  573. */
  574. setMaxValue : function(val){
  575. this.maxValue = val;
  576. var i = 0,
  577. thumbs = this.thumbs,
  578. len = thumbs.length,
  579. t;
  580. for(; i &lt; len; ++i){
  581. t = thumbs[i];
  582. t.value = t.value &gt; val ? val : t.value;
  583. }
  584. this.syncThumb();
  585. },
  586. <span id='Ext-slider-MultiSlider-method-setValue'> /**
  587. </span> * Programmatically sets the value of the Slider. Ensures that the value is constrained within
  588. * the minValue and maxValue.
  589. * @param {Number} index Index of the thumb to move
  590. * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)
  591. * @param {Boolean} animate Turn on or off animation, defaults to true
  592. */
  593. setValue : function(index, v, animate, changeComplete) {
  594. var thumb = this.thumbs[index],
  595. el = thumb.el;
  596. v = this.normalizeValue(v);
  597. if (v !== thumb.value &amp;&amp; this.fireEvent('beforechange', this, v, thumb.value, thumb) !== false) {
  598. thumb.value = v;
  599. if(this.rendered){
  600. this.moveThumb(index, this.translateValue(v), animate !== false);
  601. this.fireEvent('change', this, v, thumb);
  602. if(changeComplete){
  603. this.fireEvent('changecomplete', this, v, thumb);
  604. }
  605. }
  606. }
  607. },
  608. <span id='Ext-slider-MultiSlider-method-translateValue'> /**
  609. </span> * @private
  610. */
  611. translateValue : function(v) {
  612. var ratio = this.getRatio();
  613. return (v * ratio) - (this.minValue * ratio) - this.halfThumb;
  614. },
  615. <span id='Ext-slider-MultiSlider-method-reverseValue'> /**
  616. </span> * @private
  617. * Given a pixel location along the slider, returns the mapped slider value for that pixel.
  618. * E.g. if we have a slider 200px wide with minValue = 100 and maxValue = 500, reverseValue(50)
  619. * returns 200
  620. * @param {Number} pos The position along the slider to return a mapped value for
  621. * @return {Number} The mapped value for the given position
  622. */
  623. reverseValue : function(pos){
  624. var ratio = this.getRatio();
  625. return (pos + (this.minValue * ratio)) / ratio;
  626. },
  627. <span id='Ext-slider-MultiSlider-method-moveThumb'> /**
  628. </span> * @private
  629. * @param {Number} index Index of the thumb to move
  630. */
  631. moveThumb: function(index, v, animate){
  632. var thumb = this.thumbs[index].el;
  633. if(!animate || this.animate === false){
  634. thumb.setLeft(v);
  635. }else{
  636. thumb.shift({left: v, stopFx: true, duration:.35});
  637. }
  638. },
  639. <span id='Ext-slider-MultiSlider-method-focus'> // private
  640. </span> focus : function(){
  641. this.focusEl.focus(10);
  642. },
  643. <span id='Ext-slider-MultiSlider-method-onResize'> // private
  644. </span> onResize : function(w, h){
  645. var thumbs = this.thumbs,
  646. len = thumbs.length,
  647. i = 0;
  648. /*
  649. * If we happen to be animating during a resize, the position of the thumb will likely be off
  650. * when the animation stops. As such, just stop any animations before syncing the thumbs.
  651. */
  652. for(; i &lt; len; ++i){
  653. thumbs[i].el.stopFx();
  654. }
  655. // check to see if we're using an auto width
  656. if(Ext.isNumber(w)){
  657. this.innerEl.setWidth(w - (this.el.getPadding('l') + this.endEl.getPadding('r')));
  658. }
  659. this.syncThumb();
  660. Ext.slider.MultiSlider.superclass.onResize.apply(this, arguments);
  661. },
  662. <span id='Ext-slider-MultiSlider-method-onDisable'> //private
  663. </span> onDisable: function(){
  664. Ext.slider.MultiSlider.superclass.onDisable.call(this);
  665. for (var i=0; i &lt; this.thumbs.length; i++) {
  666. var thumb = this.thumbs[i],
  667. el = thumb.el;
  668. thumb.disable();
  669. if(Ext.isIE){
  670. //IE breaks when using overflow visible and opacity other than 1.
  671. //Create a place holder for the thumb and display it.
  672. var xy = el.getXY();
  673. el.hide();
  674. this.innerEl.addClass(this.disabledClass).dom.disabled = true;
  675. if (!this.thumbHolder) {
  676. this.thumbHolder = this.endEl.createChild({cls: 'x-slider-thumb ' + this.disabledClass});
  677. }
  678. this.thumbHolder.show().setXY(xy);
  679. }
  680. }
  681. },
  682. <span id='Ext-slider-MultiSlider-method-onEnable'> //private
  683. </span> onEnable: function(){
  684. Ext.slider.MultiSlider.superclass.onEnable.call(this);
  685. for (var i=0; i &lt; this.thumbs.length; i++) {
  686. var thumb = this.thumbs[i],
  687. el = thumb.el;
  688. thumb.enable();
  689. if (Ext.isIE) {
  690. this.innerEl.removeClass(this.disabledClass).dom.disabled = false;
  691. if (this.thumbHolder) this.thumbHolder.hide();
  692. el.show();
  693. this.syncThumb();
  694. }
  695. }
  696. },
  697. <span id='Ext-slider-MultiSlider-method-syncThumb'> /**
  698. </span> * Synchronizes the thumb position to the proper proportion of the total component width based
  699. * on the current slider {@link #value}. This will be called automatically when the Slider
  700. * is resized by a layout, but if it is rendered auto width, this method can be called from
  701. * another resize handler to sync the Slider if necessary.
  702. */
  703. syncThumb : function() {
  704. if (this.rendered) {
  705. for (var i=0; i &lt; this.thumbs.length; i++) {
  706. this.moveThumb(i, this.translateValue(this.thumbs[i].value));
  707. }
  708. }
  709. },
  710. <span id='Ext-slider-MultiSlider-method-getValue'> /**
  711. </span> * Returns the current value of the slider
  712. * @param {Number} index The index of the thumb to return a value for
  713. * @return {Number} The current value of the slider
  714. */
  715. getValue : function(index) {
  716. return this.thumbs[index].value;
  717. },
  718. <span id='Ext-slider-MultiSlider-method-getValues'> /**
  719. </span> * Returns an array of values - one for the location of each thumb
  720. * @return {Array} The set of thumb values
  721. */
  722. getValues: function() {
  723. var values = [];
  724. for (var i=0; i &lt; this.thumbs.length; i++) {
  725. values.push(this.thumbs[i].value);
  726. }
  727. return values;
  728. },
  729. <span id='Ext-slider-MultiSlider-method-beforeDestroy'> // private
  730. </span> beforeDestroy : function(){
  731. var thumbs = this.thumbs;
  732. for(var i = 0, len = thumbs.length; i &lt; len; ++i){
  733. thumbs[i].destroy();
  734. thumbs[i] = null;
  735. }
  736. Ext.destroyMembers(this, 'endEl', 'innerEl', 'focusEl', 'thumbHolder');
  737. Ext.slider.MultiSlider.superclass.beforeDestroy.call(this);
  738. }
  739. });
  740. Ext.reg('multislider', Ext.slider.MultiSlider);
  741. <span id='Ext-slider-SingleSlider'>/**
  742. </span> * @class Ext.slider.SingleSlider
  743. * @extends Ext.slider.MultiSlider
  744. * Slider which supports vertical or horizontal orientation, keyboard adjustments,
  745. * configurable snapping, axis clicking and animation. Can be added as an item to
  746. * any container. Example usage:
  747. &lt;pre&gt;&lt;code&gt;
  748. new Ext.slider.SingleSlider({
  749. renderTo: Ext.getBody(),
  750. width: 200,
  751. value: 50,
  752. increment: 10,
  753. minValue: 0,
  754. maxValue: 100
  755. });
  756. &lt;/code&gt;&lt;/pre&gt;
  757. * The class Ext.slider.SingleSlider is aliased to Ext.Slider for backwards compatibility.
  758. */
  759. Ext.slider.SingleSlider = Ext.extend(Ext.slider.MultiSlider, {
  760. <span id='Ext-slider-SingleSlider-method-constructor'> constructor: function(config) {
  761. </span> config = config || {};
  762. Ext.applyIf(config, {
  763. values: [config.value || 0]
  764. });
  765. Ext.slider.SingleSlider.superclass.constructor.call(this, config);
  766. },
  767. <span id='Ext-slider-SingleSlider-method-getValue'> /**
  768. </span> * Returns the current value of the slider
  769. * @return {Number} The current value of the slider
  770. */
  771. getValue: function() {
  772. //just returns the value of the first thumb, which should be the only one in a single slider
  773. return Ext.slider.SingleSlider.superclass.getValue.call(this, 0);
  774. },
  775. <span id='Ext-slider-SingleSlider-method-setValue'> /**
  776. </span> * Programmatically sets the value of the Slider. Ensures that the value is constrained within
  777. * the minValue and maxValue.
  778. * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)
  779. * @param {Boolean} animate Turn on or off animation, defaults to true
  780. */
  781. setValue: function(value, animate) {
  782. var args = Ext.toArray(arguments),
  783. len = args.length;
  784. //this is to maintain backwards compatiblity for sliders with only one thunb. Usually you must pass the thumb
  785. //index to setValue, but if we only have one thumb we inject the index here first if given the multi-slider
  786. //signature without the required index. The index will always be 0 for a single slider
  787. if (len == 1 || (len &lt;= 3 &amp;&amp; typeof arguments[1] != 'number')) {
  788. args.unshift(0);
  789. }
  790. return Ext.slider.SingleSlider.superclass.setValue.apply(this, args);
  791. },
  792. <span id='Ext-slider-SingleSlider-method-syncThumb'> /**
  793. </span> * Synchronizes the thumb position to the proper proportion of the total component width based
  794. * on the current slider {@link #value}. This will be called automatically when the Slider
  795. * is resized by a layout, but if it is rendered auto width, this method can be called from
  796. * another resize handler to sync the Slider if necessary.
  797. */
  798. syncThumb : function() {
  799. return Ext.slider.SingleSlider.superclass.syncThumb.apply(this, [0].concat(arguments));
  800. },
  801. <span id='Ext-slider-SingleSlider-method-getNearest'> // private
  802. </span> getNearest : function(){
  803. // Since there's only 1 thumb, it's always the nearest
  804. return this.thumbs[0];
  805. }
  806. });
  807. //backwards compatibility
  808. Ext.Slider = Ext.slider.SingleSlider;
  809. Ext.reg('slider', Ext.slider.SingleSlider);
  810. // private class to support vertical sliders
  811. Ext.slider.Vertical = {
  812. onResize : function(w, h){
  813. this.innerEl.setHeight(h - (this.el.getPadding('t') + this.endEl.getPadding('b')));
  814. this.syncThumb();
  815. },
  816. getRatio : function(){
  817. var h = this.innerEl.getHeight(),
  818. v = this.maxValue - this.minValue;
  819. return h/v;
  820. },
  821. moveThumb: function(index, v, animate) {
  822. var thumb = this.thumbs[index],
  823. el = thumb.el;
  824. if (!animate || this.animate === false) {
  825. el.setBottom(v);
  826. } else {
  827. el.shift({bottom: v, stopFx: true, duration:.35});
  828. }
  829. },
  830. onClickChange : function(local) {
  831. if (local.left &gt; this.clickRange[0] &amp;&amp; local.left &lt; this.clickRange[1]) {
  832. var thumb = this.getNearest(local, 'top'),
  833. index = thumb.index,
  834. value = this.minValue + this.reverseValue(this.innerEl.getHeight() - local.top);
  835. this.setValue(index, Ext.util.Format.round(value, this.decimalPrecision), undefined, true);
  836. }
  837. }
  838. };
  839. //private class to support vertical dragging of thumbs within a slider
  840. Ext.slider.Thumb.Vertical = {
  841. getNewValue: function() {
  842. var slider = this.slider,
  843. innerEl = slider.innerEl,
  844. pos = innerEl.translatePoints(this.tracker.getXY()),
  845. bottom = innerEl.getHeight() - pos.top;
  846. return slider.minValue + Ext.util.Format.round(bottom / slider.getRatio(), slider.decimalPrecision);
  847. }
  848. };
  849. </pre>
  850. </body>
  851. </html>