Requires

Provides

Slider.js

Class for creating horizontal and vertical slider controls.

License:
MIT-style license
Authors:
Valerio Proietti
  1. 27
  2. 28
  3. 29
  4. 30
  5. 31
  6. 32
  7. 33
  8. 34
  9. 35
  10. 36
  11. 37
  12. 38
  13. 39
  14. 40
  15. 41
  16. 42
  17. 43
  18. 44
  19. 45
  20. 46
  21. 47
  22. 48
  23. 49
  24. 50
  25. 51
  26. 52
  27. 53
  28. 54
  29. 55
  30. 56
  31. 57
  32. 58
  33. 59
  34. 60
  35. 61
  36. 62
  37. 63
  38. 64
  39. 65
  40. 66
  41. 67
  42. 68
  43. 69
  44. 70
  45. 71
  46. 72
  47. 73
  48. 74
  49. 75
  50. 76
  51. 77
  52. 78
  53. 79
  54. 80
  55. 81
  56. 82
  57. 83
  58. 84
  59. 85
  60. 86
  61. 87
  62. 88
  63. 89
  64. 90
  65. 91
  66. 92
  67. 93
  68. 94
  69. 95
  70. 96
  71. 97
  72. 98
  73. 99
  74. 100
  75. 101
  76. 102
  77. 103
  78. 104
  79. 105
  80. 106
  81. 107
  82. 108
  83. 109
  84. 110
  85. 111
  86. 112
  87. 113
  88. 114
  89. 115
  90. 116
  91. 117
  92. 118
  93. 119
  94. 120
  95. 121
  96. 122
  97. 123
  98. 124
  99. 125
  100. 126
  101. 127
  102. 128
  103. 129
  104. 130
  105. 131
  106. 132
  107. 133
  108. 134
  109. 135
  110. 136
  111. 137
  112. 138
  113. 139
  114. 140
  115. 141
  116. 142
  117. 143
  118. 144
  119. 145
  120. 146
  121. 147
  122. 148
  123. 149
  124. 150
  125. 151
  126. 152
  127. 153
  128. 154
  129. 155
  130. 156
  131. 157
  132. 158
  133. 159
  134. 160
  135. 161
  136. 162
  137. 163
  138. 164
  139. 165
  140. 166
  141. 167
  142. 168
  143. 169
  144. 170
  145. 171
  146. 172
  147. 173
  148. 174
  149. 175
  150. 176
  151. 177
  152. 178
  153. 179
  154. 180
  155. 181
  156. 182
  157. 183
  158. 184
  159. 185
  160. 186
  161. 187
  162. 188
  163. 189
  164. 190
  165. 191
  166. 192
var Slider = new Class({ Implements: [Events, Options], Binds: ['clickedElement', 'draggedKnob', 'scrolledElement'], options: {/* onTick: function(intPosition){}, onChange: function(intStep){}, onComplete: function(strStep){},*/ onTick: function(position){ if (this.options.snap) position = this.toPosition(this.step); this.knob.setStyle(this.property, position); }, initialStep: 0, snap: false, offset: 0, range: false, wheel: false, steps: 100, mode: 'horizontal' }, initialize: function(element, knob, options){ this.setOptions(options); this.element = document.id(element); this.knob = document.id(knob); this.previousChange = this.previousEnd = this.step = -1; var offset, limit = {}, modifiers = {'x': false, 'y': false}; switch (this.options.mode){ case 'vertical': this.axis = 'y'; this.property = 'top'; offset = 'offsetHeight'; break; case 'horizontal': this.axis = 'x'; this.property = 'left'; offset = 'offsetWidth'; } this.full = this.element.measure(function(){ this.half = this.knob[offset] / 2; return this.element[offset] - this.knob[offset] + (this.options.offset * 2); }.bind(this)); this.setRange(this.options.range); this.knob.setStyle('position', 'relative').setStyle(this.property, this.options.initialStep ? this.toPosition(this.options.initialStep) : - this.options.offset); modifiers[this.axis] = this.property; limit[this.axis] = [- this.options.offset, this.full - this.options.offset]; var dragOptions = { snap: 0, limit: limit, modifiers: modifiers, onDrag: this.draggedKnob, onStart: this.draggedKnob, onBeforeStart: (function(){ this.isDragging = true; }).bind(this), onCancel: function(){ this.isDragging = false; }.bind(this), onComplete: function(){ this.isDragging = false; this.draggedKnob(); this.end(); }.bind(this) }; if (this.options.snap){ dragOptions.grid = Math.ceil(this.stepWidth); dragOptions.limit[this.axis][1] = this.full; } this.drag = new Drag(this.knob, dragOptions); this.attach(); }, attach: function(){ this.element.addEvent('mousedown', this.clickedElement); if (this.options.wheel) this.element.addEvent('mousewheel', this.scrolledElement); this.drag.attach(); return this; }, detach: function(){ this.element.removeEvent('mousedown', this.clickedElement); this.element.removeEvent('mousewheel', this.scrolledElement); this.drag.detach(); return this; }, set: function(step){ if (!((this.range > 0) ^ (step < this.min))) step = this.min; if (!((this.range > 0) ^ (step > this.max))) step = this.max; this.step = Math.round(step); this.checkStep(); this.fireEvent('tick', this.toPosition(this.step)); this.end(); return this; }, setRange: function(range, pos){ this.min = Array.pick([range[0], 0]); this.max = Array.pick([range[1], this.options.steps]); this.range = this.max - this.min; this.steps = this.options.steps || this.full; this.stepSize = Math.abs(this.range) / this.steps; this.stepWidth = this.stepSize * this.full / Math.abs(this.range); this.set(Array.pick([pos, this.step]).floor(this.min).max(this.max)); return this; }, clickedElement: function(event){ if (this.isDragging || event.target == this.knob) return; var dir = this.range < 0 ? -1 : 1; var position = event.page[this.axis] - this.element.getPosition()[this.axis] - this.half; position = position.limit(-this.options.offset, this.full -this.options.offset); this.step = Math.round(this.min + dir * this.toStep(position)); this.checkStep(); this.fireEvent('tick', position); this.end(); }, scrolledElement: function(event){ var mode = (this.options.mode == 'horizontal') ? (event.wheel < 0) : (event.wheel > 0); this.set(mode ? this.step - this.stepSize : this.step + this.stepSize); event.stop(); }, draggedKnob: function(){ var dir = this.range < 0 ? -1 : 1; var position = this.drag.value.now[this.axis]; position = position.limit(-this.options.offset, this.full -this.options.offset); this.step = Math.round(this.min + dir * this.toStep(position)); this.checkStep(); }, checkStep: function(){ if (this.previousChange != this.step){ this.previousChange = this.step; this.fireEvent('change', this.step); } }, end: function(){ if (this.previousEnd !== this.step){ this.previousEnd = this.step; this.fireEvent('complete', this.step + ''); } }, toStep: function(position){ var step = (position + this.options.offset) * this.stepSize / this.full * this.steps; return this.options.steps ? Math.round(step -= step % this.stepSize) : step; }, toPosition: function(step){ return (this.full * Math.abs(this.min - step)) / (this.steps * this.stepSize) - this.options.offset; } });