Changeset 498
- Timestamp:
- 01/14/06 07:02:19 (3 years ago)
- Files:
-
- mochikit/branches/scriptaculous/MochiKit/DragAndDrop.js (modified) (25 diffs)
- mochikit/branches/scriptaculous/MochiKit/New.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
mochikit/branches/scriptaculous/MochiKit/DragAndDrop.js
r497 r498 35 35 options = MochiKit.Base.update({ 36 36 greedy: true, 37 hoverclass: null 37 hoverclass: null, 38 activeclass: null 38 39 }, options || {}); 39 40 … … 69 70 }, 70 71 72 isAccepted: function (element, drop) { 73 return ((!drop.accept) || MochiKit.Iter.some(drop.accept, function (d) { 74 return MochiKit.Iter.some(element.className.split(' '), 75 function (c) { 76 return c == d; 77 } 78 )})); 79 }, 80 71 81 isAffected: function (point, element, drop) { 72 82 return ( 73 83 (drop.element != element) && 74 84 ((!drop._containers) || this.isContained(element, drop)) && 75 ((!drop.accept) || MochiKit.Iter.some(drop.accept, function (d) { 76 return MochiKit.Iter.some(element.className.split(' '), function (c) { 77 return c == d; 78 }) 79 })) && 85 (this.isAccepted(drop, element)) && 80 86 MochiKit.Position.within(drop.element, point[0], point[1])); 81 87 }, … … 93 99 } 94 100 this.last_active = drop; 101 }, 102 103 prepare: function (element) { 104 MochiKit.Iter.forEach(this.drops, function (drop) { 105 if (DragAndDrop.Droppables.isAccepted(element, drop)) { 106 if (drop.activeclass) { 107 MochiKit.DOM.addElementClass(drop.element, drop.activeclass); 108 } 109 } 110 }); 95 111 }, 96 112 … … 123 139 MochiKit.Position.prepare(); 124 140 125 if (this.isAffected([MochiKit.Event.pointerX(event), MochiKit.Event.pointerY(event)], element, this.last_active)) { 141 if (this.isAffected([MochiKit.Event.pointerX(event), 142 MochiKit.Event.pointerY(event)], element, this.last_active)) { 126 143 if (this.last_active.onDrop) { 127 this.last_active.onDrop(element, this.last_active.element, event); 144 this.last_active.onDrop(element, this.last_active.element, 145 event); 128 146 } 129 147 } … … 131 149 132 150 reset: function () { 151 MochiKit.Iter.forEach(this.drops, function (drop) { 152 if (drop.activeclass) { 153 MochiKit.DOM.removeElementClass(drop.element, drop.activeclass); 154 } 155 }); 133 156 if (this.last_active) { 134 this.deactivate(this.last_active);157 this.deactivate(this.last_active); 135 158 } 136 159 } … … 143 166 register: function (draggable) { 144 167 if (this.drags.length == 0) { 145 this.eventMouseUp = MochiKit.DOM.bindAsEventListener(this.endDrag, this); 146 this.eventMouseMove = MochiKit.DOM.bindAsEventListener(this.updateDrag, this); 147 this.eventKeypress = MochiKit.DOM.bindAsEventListener(this.keyPress, this); 168 this.eventMouseUp = MochiKit.DOM.bindAsEventListener( 169 this.endDrag, this); 170 this.eventMouseMove = MochiKit.DOM.bindAsEventListener( 171 this.updateDrag, this); 172 this.eventKeypress = MochiKit.DOM.bindAsEventListener( 173 this.keyPress, this); 148 174 149 175 MochiKit.Event.observe(document, 'mouseup', this.eventMouseUp); … … 159 185 }, this.drags); 160 186 if (this.drags.length == 0) { 161 MochiKit.Event.stopObserving(document, 'mouseup', this.eventMouseUp); 162 MochiKit.Event.stopObserving(document, 'mousemove', this.eventMouseMove); 163 MochiKit.Event.stopObserving(document, 'keypress', this.eventKeypress); 187 MochiKit.Event.stopObserving(document, 'mouseup', 188 this.eventMouseUp); 189 MochiKit.Event.stopObserving(document, 'mousemove', 190 this.eventMouseMove); 191 MochiKit.Event.stopObserving(document, 'keypress', 192 this.eventKeypress); 164 193 } 165 194 }, 166 195 167 196 activate: function (draggable) { 168 window.focus(); // allows keypress events if window isn't currently focused, fails for Safari 197 // allows keypress events if window isn't currently focused 198 // fails for Safari 199 window.focus(); 169 200 this.activeDraggable = draggable; 170 201 }, … … 178 209 return; 179 210 } 180 var pointer = [MochiKit.Event.pointerX(event), MochiKit.Event.pointerY(event)]; 211 var pointer = [MochiKit.Event.pointerX(event), 212 MochiKit.Event.pointerY(event)]; 181 213 // Mozilla-based browsers fire successive mousemove events with 182 214 // the same coordinates, prevent needless redrawing (moz bug?) 183 if (this._lastPointer && (MochiKit.Base.repr(this._lastPointer) == MochiKit.Base.repr(pointer))) { 215 if (this._lastPointer && (MochiKit.Base.repr(this._lastPointer) == 216 MochiKit.Base.repr(pointer))) { 184 217 return; 185 218 } … … 208 241 }, 209 242 210 removeObserver: function (element) { // element instead of observer fixes mem leaks 243 removeObserver: function (element) { 244 // element instead of observer fixes mem leaks 211 245 this.observers = MochiKit.Iter.ifilter(function (o) { 212 246 return o.element != element; … … 215 249 }, 216 250 217 notify: function (eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' 218 if (this[eventName+'Count'] > 0) { 251 notify: function (eventName, draggable, event) { 252 // 'onStart', 'onEnd', 'onDrag' 253 if (this[eventName + 'Count'] > 0) { 219 254 MochiKit.Iter.forEach(this.observers, function (o) { 220 255 if (o[eventName]) { … … 226 261 227 262 _cacheObserverCallbacks: function () { 228 MochiKit.Iter.forEach(['onStart', 'onEnd', 'onDrag'], function (eventName) { 229 DragAndDrop.Draggables[eventName + 'Count'] = MochiKit.Base.filter(function (o) { 230 return o[eventName]; 231 }, DragAndDrop.Draggables.observers).length; 263 MochiKit.Iter.forEach(['onStart', 'onEnd', 'onDrag'], 264 function (eventName) { 265 DragAndDrop.Draggables[eventName + 'Count'] = 266 MochiKit.Base.filter(function (o) { 267 return o[eventName]; 268 }, DragAndDrop.Draggables.observers).length; 232 269 }); 233 270 } … … 249 286 var dur = Math.sqrt(Math.abs(top_offset^2) + 250 287 Math.abs(left_offset^2))*0.02; 251 element._revert = new Effect.Move(element, 288 element._revert = new Effect.Move(element, 252 289 {x: -left_offset, y: -top_offset, duration: dur}); 253 290 }, … … 330 367 startDrag: function (event) { 331 368 this.dragging = true; 332 if (this.options.select Class) {369 if (this.options.selectclass) { 333 370 MochiKit.DOM.addElementClass(this.element, 334 this.options.select Class);371 this.options.selectclass); 335 372 } 336 373 if (this.options.zindex) { … … 345 382 this.element.parentNode.insertBefore(this._clone, this.element); 346 383 } 347 384 DragAndDrop.Droppables.prepare(this.element); 348 385 DragAndDrop.Draggables.notify('onStart', this, event); 349 386 if (this.options.starteffect) { … … 373 410 finishDrag: function (event, success) { 374 411 this.dragging = false; 375 if (this.options.select Class) {412 if (this.options.selectclass) { 376 413 MochiKit.DOM.removeElementClass(this.element, 377 this.options.select Class);414 this.options.selectclass); 378 415 } 379 416 380 417 if (this.options.ghosting) { 418 // XXX: from a user point of view, it would be better to remove 419 // the node only *after* the Effect.Move end 381 420 MochiKit.Position.relativize(this.element); 382 421 MochiKit.DOM.removeElement(this._clone); … … 397 436 if (revert && this.options.reverteffect) { 398 437 this.options.reverteffect(this.element, 399 d[1] -this.delta[1], d[0]-this.delta[0]);438 d[1] - this.delta[1], d[0] - this.delta[0]); 400 439 } else { 401 440 this.delta = d; … … 433 472 var pos = MochiKit.Position.cumulativeOffset(this.element); 434 473 var d = this.currentDelta(); 435 pos[0] -= d[0]; pos[1] -= d[1]; 474 pos[0] -= d[0]; 475 pos[1] -= d[1]; 436 476 437 477 var p = MochiKit.Base.map(MochiKit.Base.bind(function (i) { … … 445 485 if (this.options.snap instanceof Array) { 446 486 p = MochiKit.Base.map(MochiKit.Base.bind(function (v, i) { 447 return Math.round(v/this.options.snap[i])*this.options.snap[i] 487 return Math.round(v/this.options.snap[i]) * 488 this.options.snap[i] 448 489 }, this), p) 449 490 } else { 450 491 p = MochiKit.Base.map(MochiKit.Base.bind(function (v) { 451 return Math.round(v/this.options.snap)*this.options.snap 492 return Math.round(v/this.options.snap) * 493 this.options.snap 452 494 }, this), p) 453 495 } 454 496 } 455 497 } 456 457 498 var style = this.element.style; 458 if ((!this.options.constraint) || (this.options.constraint == 'horizontal')) { 499 if ((!this.options.constraint) || 500 (this.options.constraint == 'horizontal')) { 459 501 style.left = p[0] + 'px'; 460 502 } 461 if ((!this.options.constraint) || (this.options.constraint == 'vertical')) { 462 style.top = p[1] + 'px'; 503 if ((!this.options.constraint) || 504 (this.options.constraint == 'vertical')) { 505 style.top = p[1] + 'px'; 463 506 } 464 507 if (style.visibility == 'hidden') { … … 532 575 overlap: 'vertical', // one of 'vertical', 'horizontal' 533 576 constraint: 'vertical', // one of 'vertical', 'horizontal', false 534 containment: element, // also takes array of elements (or ids); or false 577 // also takes array of elements (or ids); or false 578 containment: element, 535 579 handle: false, // or a CSS class 536 580 only: false, … … 594 638 if (options.dropOnEmpty) { 595 639 DragAndDrop.Droppables.add(element, 596 {containment: options.containment, onHover: Sortable.onEmptyHover, greedy: false}); 640 {containment: options.containment, 641 onHover: Sortable.onEmptyHover, 642 greedy: false}); 597 643 options.droppables.push(element); 598 644 } 599 MochiKit.Iter.forEach((this.findElements(element, options) || []), function (e) { 645 MochiKit.Iter.forEach((this.findElements(element, options) || []), 646 function (e) { 600 647 // handles are per-draggable 601 648 var handle = options.handle ? 602 MochiKit.DOM.getElementsByTagAndClassName(null, options.handle, e)[0] : e; 649 MochiKit.DOM.getElementsByTagAndClassName(null, 650 options.handle, e)[0] : e; 603 651 options.draggables.push( 604 new DragAndDrop.Draggable(e, MochiKit.Base.update(options_for_draggable, 605 {handle: handle}))); 652 new DragAndDrop.Draggable(e, 653 MochiKit.Base.update(options_for_draggable, 654 {handle: handle}))); 606 655 DragAndDrop.Droppables.add(e, options_for_droppable); 607 656 options.droppables.push(e); … … 612 661 613 662 // for onupdate 614 DragAndDrop.Draggables.addObserver(new SortableObserver(element, options.onUpdate)); 663 DragAndDrop.Draggables.addObserver( 664 new SortableObserver(element, options.onUpdate)); 615 665 }, 616 666 … … 622 672 var elements = []; 623 673 MochiKit.Iter.forEach(element.childNodes, function (e) { 624 if (e.tagName && e.tagName.toUpperCase() == options.tag.toUpperCase() && 625 (!options.only || (MochiKit.DOM.hasElementClass(e, options.only)))) { 674 if (e.tagName && 675 e.tagName.toUpperCase() == options.tag.toUpperCase() && 676 (!options.only || 677 (MochiKit.DOM.hasElementClass(e, options.only)))) { 626 678 elements.push(e); 627 679 } … … 687 739 688 740 if (!Sortable._marker) { 689 Sortable._marker = MochiKit.DOM.getElement('dropmarker') || document.createElement('DIV'); 741 Sortable._marker = MochiKit.DOM.getElement('dropmarker') || 742 document.createElement('DIV'); 690 743 MochiKit.DOM.hideElement(Sortable._marker); 691 744 MochiKit.DOM.addElementClass(Sortable._marker, 'dropmarker'); 692 745 Sortable._marker.style.position = 'absolute'; 693 document.getElementsByTagName('body').item(0).appendChild(Sortable._marker); 746 document.getElementsByTagName('body').item(0).appendChild( 747 Sortable._marker); 694 748 } 695 749 var offsets = MochiKit.Position.cumulativeOffset(dropon); … … 699 753 if (position == 'after') { 700 754 if (sortable.overlap == 'horizontal') { 701 Sortable._marker.style.left = (offsets[0]+dropon.clientWidth) + 'px'; 755 Sortable._marker.style.left = (offsets[0] + 756 dropon.clientWidth) + 'px'; 702 757 } else { 703 Sortable._marker.style.top = (offsets[1]+dropon.clientHeight) + 'px'; 758 Sortable._marker.style.top = (offsets[1] + 759 dropon.clientHeight) + 'px'; 704 760 } 705 761 } … … 719 775 return MochiKit.Base.map(function (item) { 720 776 return (encodeURIComponent(options.name) + '[]=' + 721 encodeURIComponent(item.id.match(options.format) ? item.id.match(options.format)[1] : '')); 722 }, MochiKit.DOM.getElement(this.findElements(element, options) || [])).join('&'); 777 encodeURIComponent(item.id.match(options.format) ? 778 item.id.match(options.format)[1] : '')); 779 }, MochiKit.DOM.getElement( 780 this.findElements(element, options) || [])).join('&'); 723 781 } 724 782 }; mochikit/branches/scriptaculous/MochiKit/New.js
r495 r498 300 300 var valueT = 0, valueL = 0; 301 301 do { 302 valueT += element.offsetTop || 0; 303 valueL += element.offsetLeft || 0; 304 element = element.offsetParent; 302 valueT += element.offsetTop || 0; 303 valueL += element.offsetLeft || 0; 304 element = parent.offsetParent; 305 } while (element); 306 return [valueL, valueT]; 307 }, 308 309 realOffset: function(element) { 310 var valueT = 0, valueL = 0; 311 do { 312 valueT += element.scrollTop || 0; 313 valueL += element.scrollLeft || 0; 314 element = element.parentNode; 305 315 } while (element); 306 316 return [valueL, valueT];
