📄 dragdrop.js
字号:
// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)// // Element.Class part Copyright (c) 2005 by Rick Olson// // See scriptaculous.js for full license./*--------------------------------------------------------------------------*/var Droppables = { drops: [], remove: function(element) { this.drops = this.drops.reject(function(d) { return d.element==element }); }, add: function(element) { element = $(element); var options = Object.extend({ greedy: true, hoverclass: null }, arguments[1] || {}); // cache containers if(options.containment) { options._containers = []; var containment = options.containment; if((typeof containment == 'object') && (containment.constructor == Array)) { containment.each( function(c) { options._containers.push($(c)) }); } else { options._containers.push($(containment)); } } Element.makePositioned(element); // fix IE options.element = element; this.drops.push(options); }, isContained: function(element, drop) { var parentNode = element.parentNode; return drop._containers.detect(function(c) { return parentNode == c }); }, isAffected: function(pX, pY, element, drop) { return ( (drop.element!=element) && ((!drop._containers) || this.isContained(element, drop)) && ((!drop.accept) || (Element.Class.has_any(element, drop.accept))) && Position.within(drop.element, pX, pY) ); }, deactivate: function(drop) { if(drop.hoverclass) Element.Class.remove(drop.element, drop.hoverclass); this.last_active = null; }, activate: function(drop) { if(this.last_active) this.deactivate(this.last_active); if(drop.hoverclass) Element.Class.add(drop.element, drop.hoverclass); this.last_active = drop; }, show: function(event, element) { if(!this.drops.length) return; var pX = Event.pointerX(event); var pY = Event.pointerY(event); Position.prepare(); var i = this.drops.length-1; do { var drop = this.drops[i]; if(this.isAffected(pX, pY, element, drop)) { if(drop.onHover) drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); if(drop.greedy) { this.activate(drop); return; } } } while (i--); if(this.last_active) this.deactivate(this.last_active); }, fire: function(event, element) { if(!this.last_active) return; Position.prepare(); if (this.isAffected(Event.pointerX(event), Event.pointerY(event), element, this.last_active)) if (this.last_active.onDrop) this.last_active.onDrop(element, this.last_active.element, event); }, reset: function() { if(this.last_active) this.deactivate(this.last_active); }}var Draggables = { observers: [], addObserver: function(observer) { this.observers.push(observer); }, removeObserver: function(element) { // element instead of obsever fixes mem leaks this.observers = this.observers.reject( function(o) { return o.element==element }); }, notify: function(eventName, draggable) { // 'onStart', 'onEnd' this.observers.invoke(eventName, draggable); }}/*--------------------------------------------------------------------------*/var Draggable = Class.create();Draggable.prototype = { initialize: function(element) { var options = Object.extend({ handle: false, starteffect: function(element) { new Effect.Opacity(element, {duration:0.2, from:1.0, to:0.7}); }, reverteffect: function(element, top_offset, left_offset) { var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; new Effect.MoveBy(element, -top_offset, -left_offset, {duration:dur}); }, endeffect: function(element) { new Effect.Opacity(element, {duration:0.2, from:0.7, to:1.0}); }, zindex: 1000, revert: false }, arguments[1] || {}); this.element = $(element); if(options.handle && (typeof options.handle == 'string')) this.handle = Element.Class.childrenWith(this.element, options.handle)[0]; if(!this.handle) this.handle = $(options.handle); if(!this.handle) this.handle = this.element; Element.makePositioned(this.element); // fix IE this.offsetX = 0; this.offsetY = 0; this.originalLeft = this.currentLeft(); this.originalTop = this.currentTop(); this.originalX = this.element.offsetLeft; this.originalY = this.element.offsetTop; this.options = options; this.active = false; this.dragging = false; this.eventMouseDown = this.startDrag.bindAsEventListener(this); this.eventMouseUp = this.endDrag.bindAsEventListener(this); this.eventMouseMove = this.update.bindAsEventListener(this); this.eventKeypress = this.keyPress.bindAsEventListener(this); this.registerEvents(); }, destroy: function() { Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); this.unregisterEvents(); }, registerEvents: function() { Event.observe(document, "mouseup", this.eventMouseUp); Event.observe(document, "mousemove", this.eventMouseMove); Event.observe(document, "keypress", this.eventKeypress); Event.observe(this.handle, "mousedown", this.eventMouseDown); }, unregisterEvents: function() { //if(!this.active) return; //Event.stopObserving(document, "mouseup", this.eventMouseUp); //Event.stopObserving(document, "mousemove", this.eventMouseMove); //Event.stopObserving(document, "keypress", this.eventKeypress); }, currentLeft: function() { return parseInt(this.element.style.left || '0'); }, currentTop: function() { return parseInt(this.element.style.top || '0') }, startDrag: function(event) { if(Event.isLeftClick(event)) { // abort on form elements, fixes a Firefox issue var src = Event.element(event); if(src.tagName && ( src.tagName=='INPUT' || src.tagName=='SELECT' || src.tagName=='BUTTON' || src.tagName=='TEXTAREA')) return; // this.registerEvents(); this.active = true; var pointer = [Event.pointerX(event), Event.pointerY(event)]; var offsets = Position.cumulativeOffset(this.element); this.offsetX = (pointer[0] - offsets[0]); this.offsetY = (pointer[1] - offsets[1]); Event.stop(event); } }, finishDrag: function(event, success) { // this.unregisterEvents(); this.active = false; this.dragging = false; if(this.options.ghosting) { Position.relativize(this.element); Element.remove(this._clone); this._clone = null; } if(success) Droppables.fire(event, this.element); Draggables.notify('onEnd', this); var revert = this.options.revert; if(revert && typeof revert == 'function') revert = revert(this.element); if(revert && this.options.reverteffect) { this.options.reverteffect(this.element, this.currentTop()-this.originalTop, this.currentLeft()-this.originalLeft); } else { this.originalLeft = this.currentLeft(); this.originalTop = this.currentTop(); } if(this.options.zindex) this.element.style.zIndex = this.originalZ; if(this.options.endeffect) this.options.endeffect(this.element); Droppables.reset(); }, keyPress: function(event) { if(this.active) { if(event.keyCode==Event.KEY_ESC) { this.finishDrag(event, false); Event.stop(event); } } }, endDrag: function(event) { if(this.active && this.dragging) { this.finishDrag(event, true); Event.stop(event); } this.active = false; this.dragging = false; },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -