Web Interface For Lite Argus Control modules
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.

977 lines
34 KiB

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