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