Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | // script.aculo.us dragdrop.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 |
michael@0 | 2 | |
michael@0 | 3 | // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) |
michael@0 | 4 | // (c) 2005-2007 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz) |
michael@0 | 5 | // |
michael@0 | 6 | // script.aculo.us is freely distributable under the terms of an MIT-style license. |
michael@0 | 7 | // For details, see the script.aculo.us web site: http://script.aculo.us/ |
michael@0 | 8 | |
michael@0 | 9 | if(typeof Effect == 'undefined') |
michael@0 | 10 | throw("dragdrop.js requires including script.aculo.us' effects.js library"); |
michael@0 | 11 | |
michael@0 | 12 | var Droppables = { |
michael@0 | 13 | drops: [], |
michael@0 | 14 | |
michael@0 | 15 | remove: function(element) { |
michael@0 | 16 | this.drops = this.drops.reject(function(d) { return d.element==$(element) }); |
michael@0 | 17 | }, |
michael@0 | 18 | |
michael@0 | 19 | add: function(element) { |
michael@0 | 20 | element = $(element); |
michael@0 | 21 | var options = Object.extend({ |
michael@0 | 22 | greedy: true, |
michael@0 | 23 | hoverclass: null, |
michael@0 | 24 | tree: false |
michael@0 | 25 | }, arguments[1] || {}); |
michael@0 | 26 | |
michael@0 | 27 | // cache containers |
michael@0 | 28 | if(options.containment) { |
michael@0 | 29 | options._containers = []; |
michael@0 | 30 | var containment = options.containment; |
michael@0 | 31 | if((typeof containment == 'object') && |
michael@0 | 32 | (containment.constructor == Array)) { |
michael@0 | 33 | containment.each( function(c) { options._containers.push($(c)) }); |
michael@0 | 34 | } else { |
michael@0 | 35 | options._containers.push($(containment)); |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | if(options.accept) options.accept = [options.accept].flatten(); |
michael@0 | 40 | |
michael@0 | 41 | Element.makePositioned(element); // fix IE |
michael@0 | 42 | options.element = element; |
michael@0 | 43 | |
michael@0 | 44 | this.drops.push(options); |
michael@0 | 45 | }, |
michael@0 | 46 | |
michael@0 | 47 | findDeepestChild: function(drops) { |
michael@0 | 48 | deepest = drops[0]; |
michael@0 | 49 | |
michael@0 | 50 | for (i = 1; i < drops.length; ++i) |
michael@0 | 51 | if (Element.isParent(drops[i].element, deepest.element)) |
michael@0 | 52 | deepest = drops[i]; |
michael@0 | 53 | |
michael@0 | 54 | return deepest; |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | isContained: function(element, drop) { |
michael@0 | 58 | var containmentNode; |
michael@0 | 59 | if(drop.tree) { |
michael@0 | 60 | containmentNode = element.treeNode; |
michael@0 | 61 | } else { |
michael@0 | 62 | containmentNode = element.parentNode; |
michael@0 | 63 | } |
michael@0 | 64 | return drop._containers.detect(function(c) { return containmentNode == c }); |
michael@0 | 65 | }, |
michael@0 | 66 | |
michael@0 | 67 | isAffected: function(point, element, drop) { |
michael@0 | 68 | return ( |
michael@0 | 69 | (drop.element!=element) && |
michael@0 | 70 | ((!drop._containers) || |
michael@0 | 71 | this.isContained(element, drop)) && |
michael@0 | 72 | ((!drop.accept) || |
michael@0 | 73 | (Element.classNames(element).detect( |
michael@0 | 74 | function(v) { return drop.accept.include(v) } ) )) && |
michael@0 | 75 | Position.within(drop.element, point[0], point[1]) ); |
michael@0 | 76 | }, |
michael@0 | 77 | |
michael@0 | 78 | deactivate: function(drop) { |
michael@0 | 79 | if(drop.hoverclass) |
michael@0 | 80 | Element.removeClassName(drop.element, drop.hoverclass); |
michael@0 | 81 | this.last_active = null; |
michael@0 | 82 | }, |
michael@0 | 83 | |
michael@0 | 84 | activate: function(drop) { |
michael@0 | 85 | if(drop.hoverclass) |
michael@0 | 86 | Element.addClassName(drop.element, drop.hoverclass); |
michael@0 | 87 | this.last_active = drop; |
michael@0 | 88 | }, |
michael@0 | 89 | |
michael@0 | 90 | show: function(point, element) { |
michael@0 | 91 | if(!this.drops.length) return; |
michael@0 | 92 | var affected = []; |
michael@0 | 93 | |
michael@0 | 94 | if(this.last_active) this.deactivate(this.last_active); |
michael@0 | 95 | this.drops.each( function(drop) { |
michael@0 | 96 | if(Droppables.isAffected(point, element, drop)) |
michael@0 | 97 | affected.push(drop); |
michael@0 | 98 | }); |
michael@0 | 99 | |
michael@0 | 100 | if(affected.length>0) { |
michael@0 | 101 | drop = Droppables.findDeepestChild(affected); |
michael@0 | 102 | Position.within(drop.element, point[0], point[1]); |
michael@0 | 103 | if(drop.onHover) |
michael@0 | 104 | drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); |
michael@0 | 105 | |
michael@0 | 106 | Droppables.activate(drop); |
michael@0 | 107 | } |
michael@0 | 108 | }, |
michael@0 | 109 | |
michael@0 | 110 | fire: function(event, element) { |
michael@0 | 111 | if(!this.last_active) return; |
michael@0 | 112 | Position.prepare(); |
michael@0 | 113 | |
michael@0 | 114 | if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) |
michael@0 | 115 | if (this.last_active.onDrop) { |
michael@0 | 116 | this.last_active.onDrop(element, this.last_active.element, event); |
michael@0 | 117 | return true; |
michael@0 | 118 | } |
michael@0 | 119 | }, |
michael@0 | 120 | |
michael@0 | 121 | reset: function() { |
michael@0 | 122 | if(this.last_active) |
michael@0 | 123 | this.deactivate(this.last_active); |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | var Draggables = { |
michael@0 | 128 | drags: [], |
michael@0 | 129 | observers: [], |
michael@0 | 130 | |
michael@0 | 131 | register: function(draggable) { |
michael@0 | 132 | if(this.drags.length == 0) { |
michael@0 | 133 | this.eventMouseUp = this.endDrag.bindAsEventListener(this); |
michael@0 | 134 | this.eventMouseMove = this.updateDrag.bindAsEventListener(this); |
michael@0 | 135 | this.eventKeypress = this.keyPress.bindAsEventListener(this); |
michael@0 | 136 | |
michael@0 | 137 | Event.observe(document, "mouseup", this.eventMouseUp); |
michael@0 | 138 | Event.observe(document, "mousemove", this.eventMouseMove); |
michael@0 | 139 | Event.observe(document, "keypress", this.eventKeypress); |
michael@0 | 140 | } |
michael@0 | 141 | this.drags.push(draggable); |
michael@0 | 142 | }, |
michael@0 | 143 | |
michael@0 | 144 | unregister: function(draggable) { |
michael@0 | 145 | this.drags = this.drags.reject(function(d) { return d==draggable }); |
michael@0 | 146 | if(this.drags.length == 0) { |
michael@0 | 147 | Event.stopObserving(document, "mouseup", this.eventMouseUp); |
michael@0 | 148 | Event.stopObserving(document, "mousemove", this.eventMouseMove); |
michael@0 | 149 | Event.stopObserving(document, "keypress", this.eventKeypress); |
michael@0 | 150 | } |
michael@0 | 151 | }, |
michael@0 | 152 | |
michael@0 | 153 | activate: function(draggable) { |
michael@0 | 154 | if(draggable.options.delay) { |
michael@0 | 155 | this._timeout = setTimeout(function() { |
michael@0 | 156 | Draggables._timeout = null; |
michael@0 | 157 | window.focus(); |
michael@0 | 158 | Draggables.activeDraggable = draggable; |
michael@0 | 159 | }.bind(this), draggable.options.delay); |
michael@0 | 160 | } else { |
michael@0 | 161 | window.focus(); // allows keypress events if window isn't currently focused, fails for Safari |
michael@0 | 162 | this.activeDraggable = draggable; |
michael@0 | 163 | } |
michael@0 | 164 | }, |
michael@0 | 165 | |
michael@0 | 166 | deactivate: function() { |
michael@0 | 167 | this.activeDraggable = null; |
michael@0 | 168 | }, |
michael@0 | 169 | |
michael@0 | 170 | updateDrag: function(event) { |
michael@0 | 171 | if(!this.activeDraggable) return; |
michael@0 | 172 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; |
michael@0 | 173 | // Mozilla-based browsers fire successive mousemove events with |
michael@0 | 174 | // the same coordinates, prevent needless redrawing (moz bug?) |
michael@0 | 175 | if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; |
michael@0 | 176 | this._lastPointer = pointer; |
michael@0 | 177 | |
michael@0 | 178 | this.activeDraggable.updateDrag(event, pointer); |
michael@0 | 179 | }, |
michael@0 | 180 | |
michael@0 | 181 | endDrag: function(event) { |
michael@0 | 182 | if(this._timeout) { |
michael@0 | 183 | clearTimeout(this._timeout); |
michael@0 | 184 | this._timeout = null; |
michael@0 | 185 | } |
michael@0 | 186 | if(!this.activeDraggable) return; |
michael@0 | 187 | this._lastPointer = null; |
michael@0 | 188 | this.activeDraggable.endDrag(event); |
michael@0 | 189 | this.activeDraggable = null; |
michael@0 | 190 | }, |
michael@0 | 191 | |
michael@0 | 192 | keyPress: function(event) { |
michael@0 | 193 | if(this.activeDraggable) |
michael@0 | 194 | this.activeDraggable.keyPress(event); |
michael@0 | 195 | }, |
michael@0 | 196 | |
michael@0 | 197 | addObserver: function(observer) { |
michael@0 | 198 | this.observers.push(observer); |
michael@0 | 199 | this._cacheObserverCallbacks(); |
michael@0 | 200 | }, |
michael@0 | 201 | |
michael@0 | 202 | removeObserver: function(element) { // element instead of observer fixes mem leaks |
michael@0 | 203 | this.observers = this.observers.reject( function(o) { return o.element==element }); |
michael@0 | 204 | this._cacheObserverCallbacks(); |
michael@0 | 205 | }, |
michael@0 | 206 | |
michael@0 | 207 | notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' |
michael@0 | 208 | if(this[eventName+'Count'] > 0) |
michael@0 | 209 | this.observers.each( function(o) { |
michael@0 | 210 | if(o[eventName]) o[eventName](eventName, draggable, event); |
michael@0 | 211 | }); |
michael@0 | 212 | if(draggable.options[eventName]) draggable.options[eventName](draggable, event); |
michael@0 | 213 | }, |
michael@0 | 214 | |
michael@0 | 215 | _cacheObserverCallbacks: function() { |
michael@0 | 216 | ['onStart','onEnd','onDrag'].each( function(eventName) { |
michael@0 | 217 | Draggables[eventName+'Count'] = Draggables.observers.select( |
michael@0 | 218 | function(o) { return o[eventName]; } |
michael@0 | 219 | ).length; |
michael@0 | 220 | }); |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | /*--------------------------------------------------------------------------*/ |
michael@0 | 225 | |
michael@0 | 226 | var Draggable = Class.create(); |
michael@0 | 227 | Draggable._dragging = {}; |
michael@0 | 228 | |
michael@0 | 229 | Draggable.prototype = { |
michael@0 | 230 | initialize: function(element) { |
michael@0 | 231 | var defaults = { |
michael@0 | 232 | handle: false, |
michael@0 | 233 | reverteffect: function(element, top_offset, left_offset) { |
michael@0 | 234 | var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; |
michael@0 | 235 | new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur, |
michael@0 | 236 | queue: {scope:'_draggable', position:'end'} |
michael@0 | 237 | }); |
michael@0 | 238 | }, |
michael@0 | 239 | endeffect: function(element) { |
michael@0 | 240 | var toOpacity = typeof element._opacity == 'number' ? element._opacity : 1.0; |
michael@0 | 241 | new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, |
michael@0 | 242 | queue: {scope:'_draggable', position:'end'}, |
michael@0 | 243 | afterFinish: function(){ |
michael@0 | 244 | Draggable._dragging[element] = false |
michael@0 | 245 | } |
michael@0 | 246 | }); |
michael@0 | 247 | }, |
michael@0 | 248 | zindex: 1000, |
michael@0 | 249 | revert: false, |
michael@0 | 250 | quiet: false, |
michael@0 | 251 | scroll: false, |
michael@0 | 252 | scrollSensitivity: 20, |
michael@0 | 253 | scrollSpeed: 15, |
michael@0 | 254 | snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] } |
michael@0 | 255 | delay: 0 |
michael@0 | 256 | }; |
michael@0 | 257 | |
michael@0 | 258 | if(!arguments[1] || typeof arguments[1].endeffect == 'undefined') |
michael@0 | 259 | Object.extend(defaults, { |
michael@0 | 260 | starteffect: function(element) { |
michael@0 | 261 | element._opacity = Element.getOpacity(element); |
michael@0 | 262 | Draggable._dragging[element] = true; |
michael@0 | 263 | new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); |
michael@0 | 264 | } |
michael@0 | 265 | }); |
michael@0 | 266 | |
michael@0 | 267 | var options = Object.extend(defaults, arguments[1] || {}); |
michael@0 | 268 | |
michael@0 | 269 | this.element = $(element); |
michael@0 | 270 | |
michael@0 | 271 | if(options.handle && (typeof options.handle == 'string')) |
michael@0 | 272 | this.handle = this.element.down('.'+options.handle, 0); |
michael@0 | 273 | |
michael@0 | 274 | if(!this.handle) this.handle = $(options.handle); |
michael@0 | 275 | if(!this.handle) this.handle = this.element; |
michael@0 | 276 | |
michael@0 | 277 | if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { |
michael@0 | 278 | options.scroll = $(options.scroll); |
michael@0 | 279 | this._isScrollChild = Element.childOf(this.element, options.scroll); |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | Element.makePositioned(this.element); // fix IE |
michael@0 | 283 | |
michael@0 | 284 | this.delta = this.currentDelta(); |
michael@0 | 285 | this.options = options; |
michael@0 | 286 | this.dragging = false; |
michael@0 | 287 | |
michael@0 | 288 | this.eventMouseDown = this.initDrag.bindAsEventListener(this); |
michael@0 | 289 | Event.observe(this.handle, "mousedown", this.eventMouseDown); |
michael@0 | 290 | |
michael@0 | 291 | Draggables.register(this); |
michael@0 | 292 | }, |
michael@0 | 293 | |
michael@0 | 294 | destroy: function() { |
michael@0 | 295 | Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); |
michael@0 | 296 | Draggables.unregister(this); |
michael@0 | 297 | }, |
michael@0 | 298 | |
michael@0 | 299 | currentDelta: function() { |
michael@0 | 300 | return([ |
michael@0 | 301 | parseInt(Element.getStyle(this.element,'left') || '0'), |
michael@0 | 302 | parseInt(Element.getStyle(this.element,'top') || '0')]); |
michael@0 | 303 | }, |
michael@0 | 304 | |
michael@0 | 305 | initDrag: function(event) { |
michael@0 | 306 | if(typeof Draggable._dragging[this.element] != 'undefined' && |
michael@0 | 307 | Draggable._dragging[this.element]) return; |
michael@0 | 308 | if(Event.isLeftClick(event)) { |
michael@0 | 309 | // abort on form elements, fixes a Firefox issue |
michael@0 | 310 | var src = Event.element(event); |
michael@0 | 311 | if((tag_name = src.tagName.toUpperCase()) && ( |
michael@0 | 312 | tag_name=='INPUT' || |
michael@0 | 313 | tag_name=='SELECT' || |
michael@0 | 314 | tag_name=='OPTION' || |
michael@0 | 315 | tag_name=='BUTTON' || |
michael@0 | 316 | tag_name=='TEXTAREA')) return; |
michael@0 | 317 | |
michael@0 | 318 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; |
michael@0 | 319 | var pos = Position.cumulativeOffset(this.element); |
michael@0 | 320 | this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); |
michael@0 | 321 | |
michael@0 | 322 | Draggables.activate(this); |
michael@0 | 323 | Event.stop(event); |
michael@0 | 324 | } |
michael@0 | 325 | }, |
michael@0 | 326 | |
michael@0 | 327 | startDrag: function(event) { |
michael@0 | 328 | this.dragging = true; |
michael@0 | 329 | |
michael@0 | 330 | if(this.options.zindex) { |
michael@0 | 331 | this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); |
michael@0 | 332 | this.element.style.zIndex = this.options.zindex; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | if(this.options.ghosting) { |
michael@0 | 336 | this._clone = this.element.cloneNode(true); |
michael@0 | 337 | Position.absolutize(this.element); |
michael@0 | 338 | this.element.parentNode.insertBefore(this._clone, this.element); |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | if(this.options.scroll) { |
michael@0 | 342 | if (this.options.scroll == window) { |
michael@0 | 343 | var where = this._getWindowScroll(this.options.scroll); |
michael@0 | 344 | this.originalScrollLeft = where.left; |
michael@0 | 345 | this.originalScrollTop = where.top; |
michael@0 | 346 | } else { |
michael@0 | 347 | this.originalScrollLeft = this.options.scroll.scrollLeft; |
michael@0 | 348 | this.originalScrollTop = this.options.scroll.scrollTop; |
michael@0 | 349 | } |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | Draggables.notify('onStart', this, event); |
michael@0 | 353 | |
michael@0 | 354 | if(this.options.starteffect) this.options.starteffect(this.element); |
michael@0 | 355 | }, |
michael@0 | 356 | |
michael@0 | 357 | updateDrag: function(event, pointer) { |
michael@0 | 358 | if(!this.dragging) this.startDrag(event); |
michael@0 | 359 | |
michael@0 | 360 | if(!this.options.quiet){ |
michael@0 | 361 | Position.prepare(); |
michael@0 | 362 | Droppables.show(pointer, this.element); |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | Draggables.notify('onDrag', this, event); |
michael@0 | 366 | |
michael@0 | 367 | this.draw(pointer); |
michael@0 | 368 | if(this.options.change) this.options.change(this); |
michael@0 | 369 | |
michael@0 | 370 | if(this.options.scroll) { |
michael@0 | 371 | this.stopScrolling(); |
michael@0 | 372 | |
michael@0 | 373 | var p; |
michael@0 | 374 | if (this.options.scroll == window) { |
michael@0 | 375 | with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; } |
michael@0 | 376 | } else { |
michael@0 | 377 | p = Position.page(this.options.scroll); |
michael@0 | 378 | p[0] += this.options.scroll.scrollLeft + Position.deltaX; |
michael@0 | 379 | p[1] += this.options.scroll.scrollTop + Position.deltaY; |
michael@0 | 380 | p.push(p[0]+this.options.scroll.offsetWidth); |
michael@0 | 381 | p.push(p[1]+this.options.scroll.offsetHeight); |
michael@0 | 382 | } |
michael@0 | 383 | var speed = [0,0]; |
michael@0 | 384 | if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity); |
michael@0 | 385 | if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity); |
michael@0 | 386 | if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity); |
michael@0 | 387 | if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity); |
michael@0 | 388 | this.startScrolling(speed); |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | // fix AppleWebKit rendering |
michael@0 | 392 | if(Prototype.Browser.WebKit) window.scrollBy(0,0); |
michael@0 | 393 | |
michael@0 | 394 | Event.stop(event); |
michael@0 | 395 | }, |
michael@0 | 396 | |
michael@0 | 397 | finishDrag: function(event, success) { |
michael@0 | 398 | this.dragging = false; |
michael@0 | 399 | |
michael@0 | 400 | if(this.options.quiet){ |
michael@0 | 401 | Position.prepare(); |
michael@0 | 402 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; |
michael@0 | 403 | Droppables.show(pointer, this.element); |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | if(this.options.ghosting) { |
michael@0 | 407 | Position.relativize(this.element); |
michael@0 | 408 | Element.remove(this._clone); |
michael@0 | 409 | this._clone = null; |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | var dropped = false; |
michael@0 | 413 | if(success) { |
michael@0 | 414 | dropped = Droppables.fire(event, this.element); |
michael@0 | 415 | if (!dropped) dropped = false; |
michael@0 | 416 | } |
michael@0 | 417 | if(dropped && this.options.onDropped) this.options.onDropped(this.element); |
michael@0 | 418 | Draggables.notify('onEnd', this, event); |
michael@0 | 419 | |
michael@0 | 420 | var revert = this.options.revert; |
michael@0 | 421 | if(revert && typeof revert == 'function') revert = revert(this.element); |
michael@0 | 422 | |
michael@0 | 423 | var d = this.currentDelta(); |
michael@0 | 424 | if(revert && this.options.reverteffect) { |
michael@0 | 425 | if (dropped == 0 || revert != 'failure') |
michael@0 | 426 | this.options.reverteffect(this.element, |
michael@0 | 427 | d[1]-this.delta[1], d[0]-this.delta[0]); |
michael@0 | 428 | } else { |
michael@0 | 429 | this.delta = d; |
michael@0 | 430 | } |
michael@0 | 431 | |
michael@0 | 432 | if(this.options.zindex) |
michael@0 | 433 | this.element.style.zIndex = this.originalZ; |
michael@0 | 434 | |
michael@0 | 435 | if(this.options.endeffect) |
michael@0 | 436 | this.options.endeffect(this.element); |
michael@0 | 437 | |
michael@0 | 438 | Draggables.deactivate(this); |
michael@0 | 439 | Droppables.reset(); |
michael@0 | 440 | }, |
michael@0 | 441 | |
michael@0 | 442 | keyPress: function(event) { |
michael@0 | 443 | if(event.keyCode!=Event.KEY_ESC) return; |
michael@0 | 444 | this.finishDrag(event, false); |
michael@0 | 445 | Event.stop(event); |
michael@0 | 446 | }, |
michael@0 | 447 | |
michael@0 | 448 | endDrag: function(event) { |
michael@0 | 449 | if(!this.dragging) return; |
michael@0 | 450 | this.stopScrolling(); |
michael@0 | 451 | this.finishDrag(event, true); |
michael@0 | 452 | Event.stop(event); |
michael@0 | 453 | }, |
michael@0 | 454 | |
michael@0 | 455 | draw: function(point) { |
michael@0 | 456 | var pos = Position.cumulativeOffset(this.element); |
michael@0 | 457 | if(this.options.ghosting) { |
michael@0 | 458 | var r = Position.realOffset(this.element); |
michael@0 | 459 | pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY; |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | var d = this.currentDelta(); |
michael@0 | 463 | pos[0] -= d[0]; pos[1] -= d[1]; |
michael@0 | 464 | |
michael@0 | 465 | if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { |
michael@0 | 466 | pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft; |
michael@0 | 467 | pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop; |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | var p = [0,1].map(function(i){ |
michael@0 | 471 | return (point[i]-pos[i]-this.offset[i]) |
michael@0 | 472 | }.bind(this)); |
michael@0 | 473 | |
michael@0 | 474 | if(this.options.snap) { |
michael@0 | 475 | if(typeof this.options.snap == 'function') { |
michael@0 | 476 | p = this.options.snap(p[0],p[1],this); |
michael@0 | 477 | } else { |
michael@0 | 478 | if(this.options.snap instanceof Array) { |
michael@0 | 479 | p = p.map( function(v, i) { |
michael@0 | 480 | return Math.round(v/this.options.snap[i])*this.options.snap[i] }.bind(this)) |
michael@0 | 481 | } else { |
michael@0 | 482 | p = p.map( function(v) { |
michael@0 | 483 | return Math.round(v/this.options.snap)*this.options.snap }.bind(this)) |
michael@0 | 484 | } |
michael@0 | 485 | }} |
michael@0 | 486 | |
michael@0 | 487 | var style = this.element.style; |
michael@0 | 488 | if((!this.options.constraint) || (this.options.constraint=='horizontal')) |
michael@0 | 489 | style.left = p[0] + "px"; |
michael@0 | 490 | if((!this.options.constraint) || (this.options.constraint=='vertical')) |
michael@0 | 491 | style.top = p[1] + "px"; |
michael@0 | 492 | |
michael@0 | 493 | if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering |
michael@0 | 494 | }, |
michael@0 | 495 | |
michael@0 | 496 | stopScrolling: function() { |
michael@0 | 497 | if(this.scrollInterval) { |
michael@0 | 498 | clearInterval(this.scrollInterval); |
michael@0 | 499 | this.scrollInterval = null; |
michael@0 | 500 | Draggables._lastScrollPointer = null; |
michael@0 | 501 | } |
michael@0 | 502 | }, |
michael@0 | 503 | |
michael@0 | 504 | startScrolling: function(speed) { |
michael@0 | 505 | if(!(speed[0] || speed[1])) return; |
michael@0 | 506 | this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed]; |
michael@0 | 507 | this.lastScrolled = new Date(); |
michael@0 | 508 | this.scrollInterval = setInterval(this.scroll.bind(this), 10); |
michael@0 | 509 | }, |
michael@0 | 510 | |
michael@0 | 511 | scroll: function() { |
michael@0 | 512 | var current = new Date(); |
michael@0 | 513 | var delta = current - this.lastScrolled; |
michael@0 | 514 | this.lastScrolled = current; |
michael@0 | 515 | if(this.options.scroll == window) { |
michael@0 | 516 | with (this._getWindowScroll(this.options.scroll)) { |
michael@0 | 517 | if (this.scrollSpeed[0] || this.scrollSpeed[1]) { |
michael@0 | 518 | var d = delta / 1000; |
michael@0 | 519 | this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] ); |
michael@0 | 520 | } |
michael@0 | 521 | } |
michael@0 | 522 | } else { |
michael@0 | 523 | this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; |
michael@0 | 524 | this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | Position.prepare(); |
michael@0 | 528 | Droppables.show(Draggables._lastPointer, this.element); |
michael@0 | 529 | Draggables.notify('onDrag', this); |
michael@0 | 530 | if (this._isScrollChild) { |
michael@0 | 531 | Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer); |
michael@0 | 532 | Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000; |
michael@0 | 533 | Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000; |
michael@0 | 534 | if (Draggables._lastScrollPointer[0] < 0) |
michael@0 | 535 | Draggables._lastScrollPointer[0] = 0; |
michael@0 | 536 | if (Draggables._lastScrollPointer[1] < 0) |
michael@0 | 537 | Draggables._lastScrollPointer[1] = 0; |
michael@0 | 538 | this.draw(Draggables._lastScrollPointer); |
michael@0 | 539 | } |
michael@0 | 540 | |
michael@0 | 541 | if(this.options.change) this.options.change(this); |
michael@0 | 542 | }, |
michael@0 | 543 | |
michael@0 | 544 | _getWindowScroll: function(w) { |
michael@0 | 545 | var T, L, W, H; |
michael@0 | 546 | with (w.document) { |
michael@0 | 547 | if (w.document.documentElement && documentElement.scrollTop) { |
michael@0 | 548 | T = documentElement.scrollTop; |
michael@0 | 549 | L = documentElement.scrollLeft; |
michael@0 | 550 | } else if (w.document.body) { |
michael@0 | 551 | T = body.scrollTop; |
michael@0 | 552 | L = body.scrollLeft; |
michael@0 | 553 | } |
michael@0 | 554 | if (w.innerWidth) { |
michael@0 | 555 | W = w.innerWidth; |
michael@0 | 556 | H = w.innerHeight; |
michael@0 | 557 | } else if (w.document.documentElement && documentElement.clientWidth) { |
michael@0 | 558 | W = documentElement.clientWidth; |
michael@0 | 559 | H = documentElement.clientHeight; |
michael@0 | 560 | } else { |
michael@0 | 561 | W = body.offsetWidth; |
michael@0 | 562 | H = body.offsetHeight |
michael@0 | 563 | } |
michael@0 | 564 | } |
michael@0 | 565 | return { top: T, left: L, width: W, height: H }; |
michael@0 | 566 | } |
michael@0 | 567 | } |
michael@0 | 568 | |
michael@0 | 569 | /*--------------------------------------------------------------------------*/ |
michael@0 | 570 | |
michael@0 | 571 | var SortableObserver = Class.create(); |
michael@0 | 572 | SortableObserver.prototype = { |
michael@0 | 573 | initialize: function(element, observer) { |
michael@0 | 574 | this.element = $(element); |
michael@0 | 575 | this.observer = observer; |
michael@0 | 576 | this.lastValue = Sortable.serialize(this.element); |
michael@0 | 577 | }, |
michael@0 | 578 | |
michael@0 | 579 | onStart: function() { |
michael@0 | 580 | this.lastValue = Sortable.serialize(this.element); |
michael@0 | 581 | }, |
michael@0 | 582 | |
michael@0 | 583 | onEnd: function() { |
michael@0 | 584 | Sortable.unmark(); |
michael@0 | 585 | if(this.lastValue != Sortable.serialize(this.element)) |
michael@0 | 586 | this.observer(this.element) |
michael@0 | 587 | } |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | var Sortable = { |
michael@0 | 591 | SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/, |
michael@0 | 592 | |
michael@0 | 593 | sortables: {}, |
michael@0 | 594 | |
michael@0 | 595 | _findRootElement: function(element) { |
michael@0 | 596 | while (element.tagName.toUpperCase() != "BODY") { |
michael@0 | 597 | if(element.id && Sortable.sortables[element.id]) return element; |
michael@0 | 598 | element = element.parentNode; |
michael@0 | 599 | } |
michael@0 | 600 | }, |
michael@0 | 601 | |
michael@0 | 602 | options: function(element) { |
michael@0 | 603 | element = Sortable._findRootElement($(element)); |
michael@0 | 604 | if(!element) return; |
michael@0 | 605 | return Sortable.sortables[element.id]; |
michael@0 | 606 | }, |
michael@0 | 607 | |
michael@0 | 608 | destroy: function(element){ |
michael@0 | 609 | var s = Sortable.options(element); |
michael@0 | 610 | |
michael@0 | 611 | if(s) { |
michael@0 | 612 | Draggables.removeObserver(s.element); |
michael@0 | 613 | s.droppables.each(function(d){ Droppables.remove(d) }); |
michael@0 | 614 | s.draggables.invoke('destroy'); |
michael@0 | 615 | |
michael@0 | 616 | delete Sortable.sortables[s.element.id]; |
michael@0 | 617 | } |
michael@0 | 618 | }, |
michael@0 | 619 | |
michael@0 | 620 | create: function(element) { |
michael@0 | 621 | element = $(element); |
michael@0 | 622 | var options = Object.extend({ |
michael@0 | 623 | element: element, |
michael@0 | 624 | tag: 'li', // assumes li children, override with tag: 'tagname' |
michael@0 | 625 | dropOnEmpty: false, |
michael@0 | 626 | tree: false, |
michael@0 | 627 | treeTag: 'ul', |
michael@0 | 628 | overlap: 'vertical', // one of 'vertical', 'horizontal' |
michael@0 | 629 | constraint: 'vertical', // one of 'vertical', 'horizontal', false |
michael@0 | 630 | containment: element, // also takes array of elements (or id's); or false |
michael@0 | 631 | handle: false, // or a CSS class |
michael@0 | 632 | only: false, |
michael@0 | 633 | delay: 0, |
michael@0 | 634 | hoverclass: null, |
michael@0 | 635 | ghosting: false, |
michael@0 | 636 | quiet: false, |
michael@0 | 637 | scroll: false, |
michael@0 | 638 | scrollSensitivity: 20, |
michael@0 | 639 | scrollSpeed: 15, |
michael@0 | 640 | format: this.SERIALIZE_RULE, |
michael@0 | 641 | |
michael@0 | 642 | // these take arrays of elements or ids and can be |
michael@0 | 643 | // used for better initialization performance |
michael@0 | 644 | elements: false, |
michael@0 | 645 | handles: false, |
michael@0 | 646 | |
michael@0 | 647 | onChange: Prototype.emptyFunction, |
michael@0 | 648 | onUpdate: Prototype.emptyFunction |
michael@0 | 649 | }, arguments[1] || {}); |
michael@0 | 650 | |
michael@0 | 651 | // clear any old sortable with same element |
michael@0 | 652 | this.destroy(element); |
michael@0 | 653 | |
michael@0 | 654 | // build options for the draggables |
michael@0 | 655 | var options_for_draggable = { |
michael@0 | 656 | revert: true, |
michael@0 | 657 | quiet: options.quiet, |
michael@0 | 658 | scroll: options.scroll, |
michael@0 | 659 | scrollSpeed: options.scrollSpeed, |
michael@0 | 660 | scrollSensitivity: options.scrollSensitivity, |
michael@0 | 661 | delay: options.delay, |
michael@0 | 662 | ghosting: options.ghosting, |
michael@0 | 663 | constraint: options.constraint, |
michael@0 | 664 | handle: options.handle }; |
michael@0 | 665 | |
michael@0 | 666 | if(options.starteffect) |
michael@0 | 667 | options_for_draggable.starteffect = options.starteffect; |
michael@0 | 668 | |
michael@0 | 669 | if(options.reverteffect) |
michael@0 | 670 | options_for_draggable.reverteffect = options.reverteffect; |
michael@0 | 671 | else |
michael@0 | 672 | if(options.ghosting) options_for_draggable.reverteffect = function(element) { |
michael@0 | 673 | element.style.top = 0; |
michael@0 | 674 | element.style.left = 0; |
michael@0 | 675 | }; |
michael@0 | 676 | |
michael@0 | 677 | if(options.endeffect) |
michael@0 | 678 | options_for_draggable.endeffect = options.endeffect; |
michael@0 | 679 | |
michael@0 | 680 | if(options.zindex) |
michael@0 | 681 | options_for_draggable.zindex = options.zindex; |
michael@0 | 682 | |
michael@0 | 683 | // build options for the droppables |
michael@0 | 684 | var options_for_droppable = { |
michael@0 | 685 | overlap: options.overlap, |
michael@0 | 686 | containment: options.containment, |
michael@0 | 687 | tree: options.tree, |
michael@0 | 688 | hoverclass: options.hoverclass, |
michael@0 | 689 | onHover: Sortable.onHover |
michael@0 | 690 | } |
michael@0 | 691 | |
michael@0 | 692 | var options_for_tree = { |
michael@0 | 693 | onHover: Sortable.onEmptyHover, |
michael@0 | 694 | overlap: options.overlap, |
michael@0 | 695 | containment: options.containment, |
michael@0 | 696 | hoverclass: options.hoverclass |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | // fix for gecko engine |
michael@0 | 700 | Element.cleanWhitespace(element); |
michael@0 | 701 | |
michael@0 | 702 | options.draggables = []; |
michael@0 | 703 | options.droppables = []; |
michael@0 | 704 | |
michael@0 | 705 | // drop on empty handling |
michael@0 | 706 | if(options.dropOnEmpty || options.tree) { |
michael@0 | 707 | Droppables.add(element, options_for_tree); |
michael@0 | 708 | options.droppables.push(element); |
michael@0 | 709 | } |
michael@0 | 710 | |
michael@0 | 711 | (options.elements || this.findElements(element, options) || []).each( function(e,i) { |
michael@0 | 712 | var handle = options.handles ? $(options.handles[i]) : |
michael@0 | 713 | (options.handle ? $(e).getElementsByClassName(options.handle)[0] : e); |
michael@0 | 714 | options.draggables.push( |
michael@0 | 715 | new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); |
michael@0 | 716 | Droppables.add(e, options_for_droppable); |
michael@0 | 717 | if(options.tree) e.treeNode = element; |
michael@0 | 718 | options.droppables.push(e); |
michael@0 | 719 | }); |
michael@0 | 720 | |
michael@0 | 721 | if(options.tree) { |
michael@0 | 722 | (Sortable.findTreeElements(element, options) || []).each( function(e) { |
michael@0 | 723 | Droppables.add(e, options_for_tree); |
michael@0 | 724 | e.treeNode = element; |
michael@0 | 725 | options.droppables.push(e); |
michael@0 | 726 | }); |
michael@0 | 727 | } |
michael@0 | 728 | |
michael@0 | 729 | // keep reference |
michael@0 | 730 | this.sortables[element.id] = options; |
michael@0 | 731 | |
michael@0 | 732 | // for onupdate |
michael@0 | 733 | Draggables.addObserver(new SortableObserver(element, options.onUpdate)); |
michael@0 | 734 | |
michael@0 | 735 | }, |
michael@0 | 736 | |
michael@0 | 737 | // return all suitable-for-sortable elements in a guaranteed order |
michael@0 | 738 | findElements: function(element, options) { |
michael@0 | 739 | return Element.findChildren( |
michael@0 | 740 | element, options.only, options.tree ? true : false, options.tag); |
michael@0 | 741 | }, |
michael@0 | 742 | |
michael@0 | 743 | findTreeElements: function(element, options) { |
michael@0 | 744 | return Element.findChildren( |
michael@0 | 745 | element, options.only, options.tree ? true : false, options.treeTag); |
michael@0 | 746 | }, |
michael@0 | 747 | |
michael@0 | 748 | onHover: function(element, dropon, overlap) { |
michael@0 | 749 | if(Element.isParent(dropon, element)) return; |
michael@0 | 750 | |
michael@0 | 751 | if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) { |
michael@0 | 752 | return; |
michael@0 | 753 | } else if(overlap>0.5) { |
michael@0 | 754 | Sortable.mark(dropon, 'before'); |
michael@0 | 755 | if(dropon.previousSibling != element) { |
michael@0 | 756 | var oldParentNode = element.parentNode; |
michael@0 | 757 | element.style.visibility = "hidden"; // fix gecko rendering |
michael@0 | 758 | dropon.parentNode.insertBefore(element, dropon); |
michael@0 | 759 | if(dropon.parentNode!=oldParentNode) |
michael@0 | 760 | Sortable.options(oldParentNode).onChange(element); |
michael@0 | 761 | Sortable.options(dropon.parentNode).onChange(element); |
michael@0 | 762 | } |
michael@0 | 763 | } else { |
michael@0 | 764 | Sortable.mark(dropon, 'after'); |
michael@0 | 765 | var nextElement = dropon.nextSibling || null; |
michael@0 | 766 | if(nextElement != element) { |
michael@0 | 767 | var oldParentNode = element.parentNode; |
michael@0 | 768 | element.style.visibility = "hidden"; // fix gecko rendering |
michael@0 | 769 | dropon.parentNode.insertBefore(element, nextElement); |
michael@0 | 770 | if(dropon.parentNode!=oldParentNode) |
michael@0 | 771 | Sortable.options(oldParentNode).onChange(element); |
michael@0 | 772 | Sortable.options(dropon.parentNode).onChange(element); |
michael@0 | 773 | } |
michael@0 | 774 | } |
michael@0 | 775 | }, |
michael@0 | 776 | |
michael@0 | 777 | onEmptyHover: function(element, dropon, overlap) { |
michael@0 | 778 | var oldParentNode = element.parentNode; |
michael@0 | 779 | var droponOptions = Sortable.options(dropon); |
michael@0 | 780 | |
michael@0 | 781 | if(!Element.isParent(dropon, element)) { |
michael@0 | 782 | var index; |
michael@0 | 783 | |
michael@0 | 784 | var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only}); |
michael@0 | 785 | var child = null; |
michael@0 | 786 | |
michael@0 | 787 | if(children) { |
michael@0 | 788 | var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); |
michael@0 | 789 | |
michael@0 | 790 | for (index = 0; index < children.length; index += 1) { |
michael@0 | 791 | if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) { |
michael@0 | 792 | offset -= Element.offsetSize (children[index], droponOptions.overlap); |
michael@0 | 793 | } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { |
michael@0 | 794 | child = index + 1 < children.length ? children[index + 1] : null; |
michael@0 | 795 | break; |
michael@0 | 796 | } else { |
michael@0 | 797 | child = children[index]; |
michael@0 | 798 | break; |
michael@0 | 799 | } |
michael@0 | 800 | } |
michael@0 | 801 | } |
michael@0 | 802 | |
michael@0 | 803 | dropon.insertBefore(element, child); |
michael@0 | 804 | |
michael@0 | 805 | Sortable.options(oldParentNode).onChange(element); |
michael@0 | 806 | droponOptions.onChange(element); |
michael@0 | 807 | } |
michael@0 | 808 | }, |
michael@0 | 809 | |
michael@0 | 810 | unmark: function() { |
michael@0 | 811 | if(Sortable._marker) Sortable._marker.hide(); |
michael@0 | 812 | }, |
michael@0 | 813 | |
michael@0 | 814 | mark: function(dropon, position) { |
michael@0 | 815 | // mark on ghosting only |
michael@0 | 816 | var sortable = Sortable.options(dropon.parentNode); |
michael@0 | 817 | if(sortable && !sortable.ghosting) return; |
michael@0 | 818 | |
michael@0 | 819 | if(!Sortable._marker) { |
michael@0 | 820 | Sortable._marker = |
michael@0 | 821 | ($('dropmarker') || Element.extend(document.createElement('DIV'))). |
michael@0 | 822 | hide().addClassName('dropmarker').setStyle({position:'absolute'}); |
michael@0 | 823 | document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); |
michael@0 | 824 | } |
michael@0 | 825 | var offsets = Position.cumulativeOffset(dropon); |
michael@0 | 826 | Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'}); |
michael@0 | 827 | |
michael@0 | 828 | if(position=='after') |
michael@0 | 829 | if(sortable.overlap == 'horizontal') |
michael@0 | 830 | Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'}); |
michael@0 | 831 | else |
michael@0 | 832 | Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'}); |
michael@0 | 833 | |
michael@0 | 834 | Sortable._marker.show(); |
michael@0 | 835 | }, |
michael@0 | 836 | |
michael@0 | 837 | _tree: function(element, options, parent) { |
michael@0 | 838 | var children = Sortable.findElements(element, options) || []; |
michael@0 | 839 | |
michael@0 | 840 | for (var i = 0; i < children.length; ++i) { |
michael@0 | 841 | var match = children[i].id.match(options.format); |
michael@0 | 842 | |
michael@0 | 843 | if (!match) continue; |
michael@0 | 844 | |
michael@0 | 845 | var child = { |
michael@0 | 846 | id: encodeURIComponent(match ? match[1] : null), |
michael@0 | 847 | element: element, |
michael@0 | 848 | parent: parent, |
michael@0 | 849 | children: [], |
michael@0 | 850 | position: parent.children.length, |
michael@0 | 851 | container: $(children[i]).down(options.treeTag) |
michael@0 | 852 | } |
michael@0 | 853 | |
michael@0 | 854 | /* Get the element containing the children and recurse over it */ |
michael@0 | 855 | if (child.container) |
michael@0 | 856 | this._tree(child.container, options, child) |
michael@0 | 857 | |
michael@0 | 858 | parent.children.push (child); |
michael@0 | 859 | } |
michael@0 | 860 | |
michael@0 | 861 | return parent; |
michael@0 | 862 | }, |
michael@0 | 863 | |
michael@0 | 864 | tree: function(element) { |
michael@0 | 865 | element = $(element); |
michael@0 | 866 | var sortableOptions = this.options(element); |
michael@0 | 867 | var options = Object.extend({ |
michael@0 | 868 | tag: sortableOptions.tag, |
michael@0 | 869 | treeTag: sortableOptions.treeTag, |
michael@0 | 870 | only: sortableOptions.only, |
michael@0 | 871 | name: element.id, |
michael@0 | 872 | format: sortableOptions.format |
michael@0 | 873 | }, arguments[1] || {}); |
michael@0 | 874 | |
michael@0 | 875 | var root = { |
michael@0 | 876 | id: null, |
michael@0 | 877 | parent: null, |
michael@0 | 878 | children: [], |
michael@0 | 879 | container: element, |
michael@0 | 880 | position: 0 |
michael@0 | 881 | } |
michael@0 | 882 | |
michael@0 | 883 | return Sortable._tree(element, options, root); |
michael@0 | 884 | }, |
michael@0 | 885 | |
michael@0 | 886 | /* Construct a [i] index for a particular node */ |
michael@0 | 887 | _constructIndex: function(node) { |
michael@0 | 888 | var index = ''; |
michael@0 | 889 | do { |
michael@0 | 890 | if (node.id) index = '[' + node.position + ']' + index; |
michael@0 | 891 | } while ((node = node.parent) != null); |
michael@0 | 892 | return index; |
michael@0 | 893 | }, |
michael@0 | 894 | |
michael@0 | 895 | sequence: function(element) { |
michael@0 | 896 | element = $(element); |
michael@0 | 897 | var options = Object.extend(this.options(element), arguments[1] || {}); |
michael@0 | 898 | |
michael@0 | 899 | return $(this.findElements(element, options) || []).map( function(item) { |
michael@0 | 900 | return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; |
michael@0 | 901 | }); |
michael@0 | 902 | }, |
michael@0 | 903 | |
michael@0 | 904 | setSequence: function(element, new_sequence) { |
michael@0 | 905 | element = $(element); |
michael@0 | 906 | var options = Object.extend(this.options(element), arguments[2] || {}); |
michael@0 | 907 | |
michael@0 | 908 | var nodeMap = {}; |
michael@0 | 909 | this.findElements(element, options).each( function(n) { |
michael@0 | 910 | if (n.id.match(options.format)) |
michael@0 | 911 | nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode]; |
michael@0 | 912 | n.parentNode.removeChild(n); |
michael@0 | 913 | }); |
michael@0 | 914 | |
michael@0 | 915 | new_sequence.each(function(ident) { |
michael@0 | 916 | var n = nodeMap[ident]; |
michael@0 | 917 | if (n) { |
michael@0 | 918 | n[1].appendChild(n[0]); |
michael@0 | 919 | delete nodeMap[ident]; |
michael@0 | 920 | } |
michael@0 | 921 | }); |
michael@0 | 922 | }, |
michael@0 | 923 | |
michael@0 | 924 | serialize: function(element) { |
michael@0 | 925 | element = $(element); |
michael@0 | 926 | var options = Object.extend(Sortable.options(element), arguments[1] || {}); |
michael@0 | 927 | var name = encodeURIComponent( |
michael@0 | 928 | (arguments[1] && arguments[1].name) ? arguments[1].name : element.id); |
michael@0 | 929 | |
michael@0 | 930 | if (options.tree) { |
michael@0 | 931 | return Sortable.tree(element, arguments[1]).children.map( function (item) { |
michael@0 | 932 | return [name + Sortable._constructIndex(item) + "[id]=" + |
michael@0 | 933 | encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); |
michael@0 | 934 | }).flatten().join('&'); |
michael@0 | 935 | } else { |
michael@0 | 936 | return Sortable.sequence(element, arguments[1]).map( function(item) { |
michael@0 | 937 | return name + "[]=" + encodeURIComponent(item); |
michael@0 | 938 | }).join('&'); |
michael@0 | 939 | } |
michael@0 | 940 | } |
michael@0 | 941 | } |
michael@0 | 942 | |
michael@0 | 943 | // Returns true if child is contained within element |
michael@0 | 944 | Element.isParent = function(child, element) { |
michael@0 | 945 | if (!child.parentNode || child == element) return false; |
michael@0 | 946 | if (child.parentNode == element) return true; |
michael@0 | 947 | return Element.isParent(child.parentNode, element); |
michael@0 | 948 | } |
michael@0 | 949 | |
michael@0 | 950 | Element.findChildren = function(element, only, recursive, tagName) { |
michael@0 | 951 | if(!element.hasChildNodes()) return null; |
michael@0 | 952 | tagName = tagName.toUpperCase(); |
michael@0 | 953 | if(only) only = [only].flatten(); |
michael@0 | 954 | var elements = []; |
michael@0 | 955 | $A(element.childNodes).each( function(e) { |
michael@0 | 956 | if(e.tagName && e.tagName.toUpperCase()==tagName && |
michael@0 | 957 | (!only || (Element.classNames(e).detect(function(v) { return only.include(v) })))) |
michael@0 | 958 | elements.push(e); |
michael@0 | 959 | if(recursive) { |
michael@0 | 960 | var grandchildren = Element.findChildren(e, only, recursive, tagName); |
michael@0 | 961 | if(grandchildren) elements.push(grandchildren); |
michael@0 | 962 | } |
michael@0 | 963 | }); |
michael@0 | 964 | |
michael@0 | 965 | return (elements.length>0 ? elements.flatten() : []); |
michael@0 | 966 | } |
michael@0 | 967 | |
michael@0 | 968 | Element.offsetSize = function (element, type) { |
michael@0 | 969 | return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')]; |
michael@0 | 970 | } |