Requires

Provides

Drag.js

The base Drag Class. Can be used to drag and resize Elements using mouse events.

License:
MIT-style license
Authors:
Valerio Proietti, Tom Occhinno, Jan Kassens
  1. 30
  2. 31
  3. 32
  4. 33
  5. 34
  6. 35
  7. 36
  8. 37
  9. 38
  10. 39
  11. 40
  12. 41
  13. 42
  14. 43
  15. 44
  16. 45
  17. 46
  18. 47
  19. 48
  20. 49
  21. 50
  22. 51
  23. 52
  24. 53
  25. 54
  26. 55
  27. 56
  28. 57
  29. 58
  30. 59
  31. 60
  32. 61
  33. 62
  34. 63
  35. 64
  36. 65
  37. 66
  38. 67
  39. 68
  40. 69
  41. 70
  42. 71
  43. 72
  44. 73
  45. 74
  46. 75
  47. 76
  48. 77
  49. 78
  50. 79
  51. 80
  52. 81
  53. 82
  54. 83
  55. 84
  56. 85
  57. 86
  58. 87
  59. 88
  60. 89
  61. 90
  62. 91
  63. 92
  64. 93
  65. 94
  66. 95
  67. 96
  68. 97
  69. 98
  70. 99
  71. 100
  72. 101
  73. 102
  74. 103
  75. 104
  76. 105
  77. 106
  78. 107
  79. 108
  80. 109
  81. 110
  82. 111
  83. 112
  84. 113
  85. 114
  86. 115
  87. 116
  88. 117
  89. 118
  90. 119
  91. 120
  92. 121
  93. 122
  94. 123
  95. 124
  96. 125
  97. 126
  98. 127
  99. 128
  100. 129
  101. 130
  102. 131
  103. 132
  104. 133
  105. 134
  106. 135
  107. 136
  108. 137
  109. 138
  110. 139
  111. 140
  112. 141
  113. 142
  114. 143
  115. 144
  116. 145
  117. 146
  118. 147
  119. 148
  120. 149
  121. 150
  122. 151
  123. 152
  124. 153
  125. 154
  126. 155
  127. 156
  128. 157
  129. 158
  130. 159
  131. 160
  132. 161
  133. 162
  134. 163
  135. 164
  136. 165
  137. 166
  138. 167
  139. 168
  140. 169
  141. 170
  142. 171
  143. 172
  144. 173
  145. 174
  146. 175
  147. 176
  148. 177
  149. 178
  150. 179
  151. 180
  152. 181
  153. 182
  154. 183
  155. 184
  156. 185
  157. 186
  158. 187
  159. 188
  160. 189
  161. 190
  162. 191
  163. 192
  164. 193
  165. 194
  166. 195
  167. 196
  168. 197
  169. 198
  170. 199
  171. 200
  172. 201
  173. 202
  174. 203
  175. 204
  176. 205
  177. 206
  178. 207
  179. 208
  180. 209
  181. 210
  182. 211
  183. 212
  184. 213
  185. 214
  186. 215
  187. 216
  188. 217
  189. 218
var Drag = new Class({ Implements: [Events, Options], options: {/* onBeforeStart: function(thisElement){}, onStart: function(thisElement, event){}, onSnap: function(thisElement){}, onDrag: function(thisElement, event){}, onCancel: function(thisElement){}, onComplete: function(thisElement, event){},*/ snap: 6, unit: 'px', grid: false, style: true, limit: false, handle: false, invert: false, preventDefault: false, stopPropagation: false, modifiers: {x: 'left', y: 'top'} }, initialize: function(){ var params = Array.link(arguments, { 'options': Type.isObject, 'element': function(obj){ return obj != null; } }); this.element = document.id(params.element); this.document = this.element.getDocument(); this.setOptions(params.options || {}); var htype = typeOf(this.options.handle); this.handles = ((htype == 'array' || htype == 'collection') ? $$(this.options.handle) : document.id(this.options.handle)) || this.element; this.mouse = {'now': {}, 'pos': {}}; this.value = {'start': {}, 'now': {}}; this.selection = (Browser.ie) ? 'selectstart' : 'mousedown'; this.bound = { start: this.start.bind(this), check: this.check.bind(this), drag: this.drag.bind(this), stop: this.stop.bind(this), cancel: this.cancel.bind(this), eventStop: Function.from(false) }; this.attach(); }, attach: function(){ this.handles.addEvent('mousedown', this.bound.start); return this; }, detach: function(){ this.handles.removeEvent('mousedown', this.bound.start); return this; }, start: function(event){ var options = this.options; if (event.rightClick) return; if (options.preventDefault) event.preventDefault(); if (options.stopPropagation) event.stopPropagation(); this.mouse.start = event.page; this.fireEvent('beforeStart', this.element); var limit = options.limit; this.limit = {x: [], y: []}; for (var z in options.modifiers){ if (!options.modifiers[z]) continue; if (options.style) this.value.now[z] = this.element.getStyle(options.modifiers[z]).toInt(); else this.value.now[z] = this.element[options.modifiers[z]]; if (options.invert) this.value.now[z] *= -1; this.mouse.pos[z] = event.page[z] - this.value.now[z]; if (limit && limit[z]){ var i = 2; while (i--){ var limitZI = limit[z][i]; if (limitZI || limitZI === 0) this.limit[z][i] = (typeof limitZI == 'function') ? limitZI() : limitZI; } } } if (typeOf(this.options.grid) == 'number') this.options.grid = { x: this.options.grid, y: this.options.grid }; var events = { mousemove: this.bound.check, mouseup: this.bound.cancel }; events[this.selection] = this.bound.eventStop; this.document.addEvents(events); }, check: function(event){ if (this.options.preventDefault) event.preventDefault(); var distance = Math.round(Math.sqrt(Math.pow(event.page.x - this.mouse.start.x, 2) + Math.pow(event.page.y - this.mouse.start.y, 2))); if (distance > this.options.snap){ this.cancel(); this.document.addEvents({ mousemove: this.bound.drag, mouseup: this.bound.stop }); this.fireEvent('start', [this.element, event]).fireEvent('snap', this.element); } }, drag: function(event){ var options = this.options; if (options.preventDefault) event.preventDefault(); this.mouse.now = event.page; for (var z in options.modifiers){ if (!options.modifiers[z]) continue; this.value.now[z] = this.mouse.now[z] - this.mouse.pos[z]; if (options.invert) this.value.now[z] *= -1; if (options.limit && this.limit[z]){ if ((this.limit[z][1] || this.limit[z][1] === 0) && (this.value.now[z] > this.limit[z][1])){ this.value.now[z] = this.limit[z][1]; } else if ((this.limit[z][0] || this.limit[z][0] === 0) && (this.value.now[z] < this.limit[z][0])){ this.value.now[z] = this.limit[z][0]; } } if (options.grid[z]) this.value.now[z] -= ((this.value.now[z] - (this.limit[z][0]||0)) % options.grid[z]); if (options.style) { this.element.setStyle(options.modifiers[z], this.value.now[z] + options.unit); } else { this.element[options.modifiers[z]] = this.value.now[z]; } } this.fireEvent('drag', [this.element, event]); }, cancel: function(event){ this.document.removeEvents({ mousemove: this.bound.check, mouseup: this.bound.cancel }); if (event){ this.document.removeEvent(this.selection, this.bound.eventStop); this.fireEvent('cancel', this.element); } }, stop: function(event){ var events = { mousemove: this.bound.drag, mouseup: this.bound.stop }; events[this.selection] = this.bound.eventStop; this.document.removeEvents(events); if (event) this.fireEvent('complete', [this.element, event]); } }); Element.implement({ makeResizable: function(options){ var drag = new Drag(this, Object.merge({ modifiers: { x: 'width', y: 'height' } }, options)); this.store('resizer', drag); return drag.addEvent('drag', function(){ this.fireEvent('resize', drag); }.bind(this)); } });