3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * These classes are derivatives of the similarly named classes in the YUI Library.
9 * The original license:
10 * Copyright (c) 2006, Yahoo! Inc. All rights reserved.
11 * Code licensed under the BSD License:
12 * http://developer.yahoo.net/yui/license.txt
17 var Event=Ext.EventManager;
21 * @class Ext.dd.DragDrop
22 * Defines the interface and base operation of items that that can be
23 * dragged or can be drop targets. It was designed to be extended, overriding
24 * the event handlers for startDrag, onDrag, onDragOver and onDragOut.
25 * Up to three html elements can be associated with a DragDrop instance:
27 * <li>linked element: the element that is passed into the constructor.
28 * This is the element which defines the boundaries for interaction with
29 * other DragDrop objects.</li>
30 * <li>handle element(s): The drag operation only occurs if the element that
31 * was clicked matches a handle element. By default this is the linked
32 * element, but there are times that you will want only a portion of the
33 * linked element to initiate the drag operation, and the setHandleElId()
34 * method provides a way to define this.</li>
35 * <li>drag element: this represents the element that would be moved along
36 * with the cursor during a drag operation. By default, this is the linked
37 * element itself as in {@link Ext.dd.DD}. setDragElId() lets you define
38 * a separate element that would be moved, as in {@link Ext.dd.DDProxy}.
41 * This class should not be instantiated until the onload event to ensure that
42 * the associated elements are available.
43 * The following would define a DragDrop obj that would interact with any
44 * other DragDrop obj in the "group1" group:
46 * dd = new Ext.dd.DragDrop("div1", "group1");
48 * Since none of the event handlers have been implemented, nothing would
49 * actually happen if you were to run the code above. Normally you would
50 * override this class or one of the default implementations, but you can
51 * also override the methods you want on an instance of the class...
53 * dd.onDragDrop = function(e, id) {
54 * alert("dd was dropped on " + id);
58 * @param {String} id of the element that is linked to this instance
59 * @param {String} sGroup the group of related DragDrop objects
60 * @param {object} config an object containing configurable attributes
61 * Valid properties for DragDrop:
62 * padding, isTarget, maintainOffset, primaryButtonOnly
64 Ext.dd.DragDrop = function(id, sGroup, config) {
66 this.init(id, sGroup, config);
70 Ext.dd.DragDrop.prototype = {
73 * Set to false to enable a DragDrop object to fire drag events while dragging
74 * over its own Element. Defaults to true - DragDrop objects do not by default
75 * fire drag events to themselves.
76 * @property ignoreSelf
81 * The id of the element associated with this object. This is what we
82 * refer to as the "linked element" because the size and position of
83 * this element is used to determine when the drag and drop objects have
91 * Configuration attributes passed into the constructor
98 * The id of the element that will be dragged. By default this is same
99 * as the linked element , but could be changed to another element. Ex:
108 * The ID of the element that initiates the drag operation. By default
109 * this is the linked element, but could be changed to be a child of this
110 * element. This lets us do things like only starting the drag when the
111 * header element within the linked html element is clicked.
112 * @property handleElId
119 * An object who's property names identify HTML tags to be considered invalid as drag handles.
120 * A non-null property value identifies the tag as invalid. Defaults to the
121 * following value which prevents drag operations from being initiated by <a> elements:<pre><code>
125 * @property invalidHandleTypes
128 invalidHandleTypes: null,
131 * An object who's property names identify the IDs of elements to be considered invalid as drag handles.
132 * A non-null property value identifies the ID as invalid. For example, to prevent
133 * dragging from being initiated on element ID "foo", use:<pre><code>
137 * @property invalidHandleIds
140 invalidHandleIds: null,
143 * An Array of CSS class names for elements to be considered in valid as drag handles.
144 * @property invalidHandleClasses
147 invalidHandleClasses: null,
150 * The linked element's absolute X position at the time the drag was
152 * @property startPageX
159 * The linked element's absolute X position at the time the drag was
161 * @property startPageY
168 * The group defines a logical collection of DragDrop objects that are
169 * related. Instances only get events when interacting with other
170 * DragDrop object in the same group. This lets us define multiple
171 * groups using a single DragDrop subclass if we want.
173 * @type object An object in the format {'group1':true, 'group2':true}
178 * Individual drag/drop instances can be locked. This will prevent
179 * onmousedown start drag.
190 lock: function() { this.locked = true; },
193 * When set to true, other DD objects in cooperating DDGroups do not receive
194 * notification events when this DD object is dragged over them. Defaults to false.
201 * Unlock this instace
204 unlock: function() { this.locked = false; },
207 * By default, all instances can be a drop target. This can be disabled by
208 * setting isTarget to false.
215 * The padding configured for this drag and drop object for calculating
216 * the drop zone intersection with this object.
218 * @type int[] An array containing the 4 padding values: [top, right, bottom, left]
223 * Cached reference to the linked element
230 * Internal typeof flag
231 * @property __ygDragDrop
237 * Set to true when horizontal contraints are applied
238 * @property constrainX
245 * Set to true when vertical contraints are applied
246 * @property constrainY
253 * The left constraint
261 * The right constraint
278 * The down constraint
286 * Maintain offsets when we resetconstraints. Set to true when you want
287 * the position of the element relative to its parent to stay the same
288 * when the page changes
290 * @property maintainOffset
293 maintainOffset: false,
296 * Array of pixel locations the element will snap to if we specified a
297 * horizontal graduation/interval. This array is generated automatically
298 * when you define a tick interval.
305 * Array of pixel locations the element will snap to if we specified a
306 * vertical graduation/interval. This array is generated automatically
307 * when you define a tick interval.
314 * By default the drag and drop instance will only respond to the primary
315 * button click (left button for a right-handed mouse). Set to true to
316 * allow drag and drop to start with any mouse click that is propogated
318 * @property primaryButtonOnly
321 primaryButtonOnly: true,
324 * The availabe property is false until the linked dom element is accessible.
325 * @property available
331 * By default, drags can only be initiated if the mousedown occurs in the
332 * region the linked element is. This is done in part to work around a
333 * bug in some browsers that mis-report the mousedown if the previous
334 * mouseup happened outside of the window. This property is set to true
335 * if outer handles are defined.
337 * @property hasOuterHandles
341 hasOuterHandles: false,
344 * Code that executes immediately before the startDrag event
345 * @method b4StartDrag
348 b4StartDrag: function(x, y) { },
351 * Abstract method called after a drag/drop object is clicked
352 * and the drag or mousedown time thresholds have beeen met.
354 * @param {int} X click location
355 * @param {int} Y click location
357 startDrag: function(x, y) { /* override this */ },
360 * Code that executes immediately before the onDrag event
364 b4Drag: function(e) { },
367 * Abstract method called during the onMouseMove event while dragging an
370 * @param {Event} e the mousemove event
372 onDrag: function(e) { /* override this */ },
375 * Abstract method called when this element fist begins hovering over
376 * another DragDrop obj
377 * @method onDragEnter
378 * @param {Event} e the mousemove event
379 * @param {String|DragDrop[]} id In POINT mode, the element
380 * id this is hovering over. In INTERSECT mode, an array of one or more
381 * dragdrop items being hovered over.
383 onDragEnter: function(e, id) { /* override this */ },
386 * Code that executes immediately before the onDragOver event
390 b4DragOver: function(e) { },
393 * Abstract method called when this element is hovering over another
396 * @param {Event} e the mousemove event
397 * @param {String|DragDrop[]} id In POINT mode, the element
398 * id this is hovering over. In INTERSECT mode, an array of dd items
399 * being hovered over.
401 onDragOver: function(e, id) { /* override this */ },
404 * Code that executes immediately before the onDragOut event
408 b4DragOut: function(e) { },
411 * Abstract method called when we are no longer hovering over an element
413 * @param {Event} e the mousemove event
414 * @param {String|DragDrop[]} id In POINT mode, the element
415 * id this was hovering over. In INTERSECT mode, an array of dd items
416 * that the mouse is no longer over.
418 onDragOut: function(e, id) { /* override this */ },
421 * Code that executes immediately before the onDragDrop event
425 b4DragDrop: function(e) { },
428 * Abstract method called when this item is dropped on another DragDrop
431 * @param {Event} e the mouseup event
432 * @param {String|DragDrop[]} id In POINT mode, the element
433 * id this was dropped on. In INTERSECT mode, an array of dd items this
436 onDragDrop: function(e, id) { /* override this */ },
439 * Abstract method called when this item is dropped on an area with no
441 * @method onInvalidDrop
442 * @param {Event} e the mouseup event
444 onInvalidDrop: function(e) { /* override this */ },
447 * Code that executes immediately before the endDrag event
451 b4EndDrag: function(e) { },
454 * Fired when we are done dragging the object
456 * @param {Event} e the mouseup event
458 endDrag: function(e) { /* override this */ },
461 * Code executed immediately before the onMouseDown event
462 * @method b4MouseDown
463 * @param {Event} e the mousedown event
466 b4MouseDown: function(e) { },
469 * Event handler that fires when a drag/drop obj gets a mousedown
470 * @method onMouseDown
471 * @param {Event} e the mousedown event
473 onMouseDown: function(e) { /* override this */ },
476 * Event handler that fires when a drag/drop obj gets a mouseup
478 * @param {Event} e the mouseup event
480 onMouseUp: function(e) { /* override this */ },
483 * Override the onAvailable method to do what is needed after the initial
484 * position was determined.
485 * @method onAvailable
487 onAvailable: function () {
491 * Provides default constraint padding to "constrainTo" elements (defaults to {left: 0, right:0, top:0, bottom:0}).
494 defaultPadding : {left:0, right:0, top:0, bottom:0},
497 * Initializes the drag drop object's constraints to restrict movement to a certain element.
501 var dd = new Ext.dd.DDProxy("dragDiv1", "proxytest",
502 { dragElId: "existingProxyDiv" });
503 dd.startDrag = function(){
504 this.constrainTo("parent-id");
507 * Or you can initalize it using the {@link Ext.Element} object:
509 Ext.get("dragDiv1").initDDProxy("proxytest", {dragElId: "existingProxyDiv"}, {
510 startDrag : function(){
511 this.constrainTo("parent-id");
515 * @param {Mixed} constrainTo The element to constrain to.
516 * @param {Object/Number} pad (optional) Pad provides a way to specify "padding" of the constraints,
517 * and can be either a number for symmetrical padding (4 would be equal to {left:4, right:4, top:4, bottom:4}) or
518 * an object containing the sides to pad. For example: {right:10, bottom:10}
519 * @param {Boolean} inContent (optional) Constrain the draggable in the content box of the element (inside padding and borders)
521 constrainTo : function(constrainTo, pad, inContent){
522 if(typeof pad == "number"){
523 pad = {left: pad, right:pad, top:pad, bottom:pad};
525 pad = pad || this.defaultPadding;
526 var b = Ext.get(this.getEl()).getBox();
527 var ce = Ext.get(constrainTo);
528 var s = ce.getScroll();
530 if(cd == document.body){
531 c = { x: s.left, y: s.top, width: Ext.lib.Dom.getViewWidth(), height: Ext.lib.Dom.getViewHeight()};
534 c = {x : xy[0]+s.left, y: xy[1]+s.top, width: cd.clientWidth, height: cd.clientHeight};
538 var topSpace = b.y - c.y;
539 var leftSpace = b.x - c.x;
541 this.resetConstraints();
542 this.setXConstraint(leftSpace - (pad.left||0), // left
543 c.width - leftSpace - b.width - (pad.right||0), //right
546 this.setYConstraint(topSpace - (pad.top||0), //top
547 c.height - topSpace - b.height - (pad.bottom||0), //bottom
553 * Returns a reference to the linked element
555 * @return {HTMLElement} the html element
559 this._domRef = Ext.getDom(this.id);
566 * Returns a reference to the actual element to drag. By default this is
567 * the same as the html element, but it can be assigned to another
568 * element. An example of this can be found in Ext.dd.DDProxy
570 * @return {HTMLElement} the html element
572 getDragEl: function() {
573 return Ext.getDom(this.dragElId);
577 * Sets up the DragDrop object. Must be called in the constructor of any
578 * Ext.dd.DragDrop subclass
580 * @param id the id of the linked element
581 * @param {String} sGroup the group of related items
582 * @param {object} config configuration attributes
584 init: function(id, sGroup, config) {
585 this.initTarget(id, sGroup, config);
586 Event.on(this.id, "mousedown", this.handleMouseDown, this);
587 // Event.on(this.id, "selectstart", Event.preventDefault);
591 * Initializes Targeting functionality only... the object does not
592 * get a mousedown handler.
594 * @param id the id of the linked element
595 * @param {String} sGroup the group of related items
596 * @param {object} config configuration attributes
598 initTarget: function(id, sGroup, config) {
600 // configuration attributes
601 this.config = config || {};
603 // create a local reference to the drag and drop manager
604 this.DDM = Ext.dd.DDM;
605 // initialize the groups array
608 // assume that we have an element reference instead of an id if the
609 // parameter is not a string
610 if (typeof id !== "string") {
617 // add to an interaction group
618 this.addToGroup((sGroup) ? sGroup : "default");
620 // We don't want to register this as the handle with the manager
621 // so we just set the id rather than calling the setter.
622 this.handleElId = id;
624 // the linked element is the element that gets dragged by default
625 this.setDragElId(id);
627 // by default, clicked anchors will not start drag operations.
628 this.invalidHandleTypes = { A: "A" };
629 this.invalidHandleIds = {};
630 this.invalidHandleClasses = [];
634 this.handleOnAvailable();
638 * Applies the configuration parameters that were passed into the constructor.
639 * This is supposed to happen at each level through the inheritance chain. So
640 * a DDProxy implentation will execute apply config on DDProxy, DD, and
641 * DragDrop in order to get all of the parameters that are available in
643 * @method applyConfig
645 applyConfig: function() {
647 // configurable properties:
648 // padding, isTarget, maintainOffset, primaryButtonOnly
649 this.padding = this.config.padding || [0, 0, 0, 0];
650 this.isTarget = (this.config.isTarget !== false);
651 this.maintainOffset = (this.config.maintainOffset);
652 this.primaryButtonOnly = (this.config.primaryButtonOnly !== false);
657 * Executed when the linked element is available
658 * @method handleOnAvailable
661 handleOnAvailable: function() {
662 this.available = true;
663 this.resetConstraints();
668 * Configures the padding for the target zone in px. Effectively expands
669 * (or reduces) the virtual object size for targeting calculations.
670 * Supports css-style shorthand; if only one parameter is passed, all sides
671 * will have that padding, and if only two are passed, the top and bottom
672 * will have the first param, the left and right the second.
674 * @param {int} iTop Top pad
675 * @param {int} iRight Right pad
676 * @param {int} iBot Bot pad
677 * @param {int} iLeft Left pad
679 setPadding: function(iTop, iRight, iBot, iLeft) {
680 // this.padding = [iLeft, iRight, iTop, iBot];
681 if (!iRight && 0 !== iRight) {
682 this.padding = [iTop, iTop, iTop, iTop];
683 } else if (!iBot && 0 !== iBot) {
684 this.padding = [iTop, iRight, iTop, iRight];
686 this.padding = [iTop, iRight, iBot, iLeft];
691 * Stores the initial placement of the linked element.
692 * @method setInitPosition
693 * @param {int} diffX the X offset, default 0
694 * @param {int} diffY the Y offset, default 0
696 setInitPosition: function(diffX, diffY) {
697 var el = this.getEl();
699 if (!this.DDM.verifyEl(el)) {
706 var p = Dom.getXY( el );
708 this.initPageX = p[0] - dx;
709 this.initPageY = p[1] - dy;
711 this.lastPageX = p[0];
712 this.lastPageY = p[1];
715 this.setStartPosition(p);
719 * Sets the start position of the element. This is set when the obj
720 * is initialized, the reset when a drag is started.
721 * @method setStartPosition
722 * @param pos current position (from previous lookup)
725 setStartPosition: function(pos) {
726 var p = pos || Dom.getXY( this.getEl() );
727 this.deltaSetXY = null;
729 this.startPageX = p[0];
730 this.startPageY = p[1];
734 * Add this instance to a group of related drag/drop objects. All
735 * instances belong to at least one group, and can belong to as many
738 * @param sGroup {string} the name of the group
740 addToGroup: function(sGroup) {
741 this.groups[sGroup] = true;
742 this.DDM.regDragDrop(this, sGroup);
746 * Remove's this instance from the supplied interaction group
747 * @method removeFromGroup
748 * @param {string} sGroup The group to drop
750 removeFromGroup: function(sGroup) {
751 if (this.groups[sGroup]) {
752 delete this.groups[sGroup];
755 this.DDM.removeDDFromGroup(this, sGroup);
759 * Allows you to specify that an element other than the linked element
760 * will be moved with the cursor during a drag
761 * @method setDragElId
762 * @param id {string} the id of the element that will be used to initiate the drag
764 setDragElId: function(id) {
769 * Allows you to specify a child of the linked element that should be
770 * used to initiate the drag operation. An example of this would be if
771 * you have a content div with text and links. Clicking anywhere in the
772 * content area would normally start the drag operation. Use this method
773 * to specify that an element inside of the content div is the element
774 * that starts the drag operation.
775 * @method setHandleElId
776 * @param id {string} the id of the element that will be used to
779 setHandleElId: function(id) {
780 if (typeof id !== "string") {
783 this.handleElId = id;
784 this.DDM.regHandle(this.id, id);
788 * Allows you to set an element outside of the linked element as a drag
790 * @method setOuterHandleElId
791 * @param id the id of the element that will be used to initiate the drag
793 setOuterHandleElId: function(id) {
794 if (typeof id !== "string") {
797 Event.on(id, "mousedown",
798 this.handleMouseDown, this);
799 this.setHandleElId(id);
801 this.hasOuterHandles = true;
805 * Remove all drag and drop hooks for this element
809 Event.un(this.id, "mousedown",
810 this.handleMouseDown);
812 this.DDM._remove(this);
815 destroy : function(){
820 * Returns true if this instance is locked, or the drag drop mgr is locked
821 * (meaning that all drag/drop is disabled on the page.)
823 * @return {boolean} true if this obj or all drag/drop is locked, else
826 isLocked: function() {
827 return (this.DDM.isLocked() || this.locked);
831 * Fired when this object is clicked
832 * @method handleMouseDown
834 * @param {Ext.dd.DragDrop} oDD the clicked dd object (this dd obj)
837 handleMouseDown: function(e, oDD){
838 if (this.primaryButtonOnly && e.button != 0) {
842 if (this.isLocked()) {
846 this.DDM.refreshCache(this.groups);
848 var pt = new Ext.lib.Point(Ext.lib.Event.getPageX(e), Ext.lib.Event.getPageY(e));
849 if (!this.hasOuterHandles && !this.DDM.isOverTarget(pt, this) ) {
851 if (this.clickValidator(e)) {
853 // set the initial element position
854 this.setStartPosition();
860 this.DDM.handleMouseDown(e, this);
862 this.DDM.stopEvent(e);
870 clickValidator: function(e) {
871 var target = e.getTarget();
872 return ( this.isValidHandleChild(target) &&
873 (this.id == this.handleElId ||
874 this.DDM.handleWasClicked(target, this.id)) );
878 * Allows you to specify a tag name that should not start a drag operation
879 * when clicked. This is designed to facilitate embedding links within a
880 * drag handle that do something other than start the drag.
881 * @method addInvalidHandleType
882 * @param {string} tagName the type of element to exclude
884 addInvalidHandleType: function(tagName) {
885 var type = tagName.toUpperCase();
886 this.invalidHandleTypes[type] = type;
890 * Lets you to specify an element id for a child of a drag handle
891 * that should not initiate a drag
892 * @method addInvalidHandleId
893 * @param {string} id the element id of the element you wish to ignore
895 addInvalidHandleId: function(id) {
896 if (typeof id !== "string") {
899 this.invalidHandleIds[id] = id;
903 * Lets you specify a css class of elements that will not initiate a drag
904 * @method addInvalidHandleClass
905 * @param {string} cssClass the class of the elements you wish to ignore
907 addInvalidHandleClass: function(cssClass) {
908 this.invalidHandleClasses.push(cssClass);
912 * Unsets an excluded tag name set by addInvalidHandleType
913 * @method removeInvalidHandleType
914 * @param {string} tagName the type of element to unexclude
916 removeInvalidHandleType: function(tagName) {
917 var type = tagName.toUpperCase();
918 // this.invalidHandleTypes[type] = null;
919 delete this.invalidHandleTypes[type];
923 * Unsets an invalid handle id
924 * @method removeInvalidHandleId
925 * @param {string} id the id of the element to re-enable
927 removeInvalidHandleId: function(id) {
928 if (typeof id !== "string") {
931 delete this.invalidHandleIds[id];
935 * Unsets an invalid css class
936 * @method removeInvalidHandleClass
937 * @param {string} cssClass the class of the element(s) you wish to
940 removeInvalidHandleClass: function(cssClass) {
941 for (var i=0, len=this.invalidHandleClasses.length; i<len; ++i) {
942 if (this.invalidHandleClasses[i] == cssClass) {
943 delete this.invalidHandleClasses[i];
949 * Checks the tag exclusion list to see if this click should be ignored
950 * @method isValidHandleChild
951 * @param {HTMLElement} node the HTMLElement to evaluate
952 * @return {boolean} true if this is a valid tag type, false if not
954 isValidHandleChild: function(node) {
957 // var n = (node.nodeName == "#text") ? node.parentNode : node;
960 nodeName = node.nodeName.toUpperCase();
962 nodeName = node.nodeName;
964 valid = valid && !this.invalidHandleTypes[nodeName];
965 valid = valid && !this.invalidHandleIds[node.id];
967 for (var i=0, len=this.invalidHandleClasses.length; valid && i<len; ++i) {
968 valid = !Ext.fly(node).hasClass(this.invalidHandleClasses[i]);
977 * Create the array of horizontal tick marks if an interval was specified
978 * in setXConstraint().
982 setXTicks: function(iStartX, iTickSize) {
984 this.xTickSize = iTickSize;
988 for (var i = this.initPageX; i >= this.minX; i = i - iTickSize) {
990 this.xTicks[this.xTicks.length] = i;
995 for (i = this.initPageX; i <= this.maxX; i = i + iTickSize) {
997 this.xTicks[this.xTicks.length] = i;
1002 this.xTicks.sort(this.DDM.numericSort) ;
1006 * Create the array of vertical tick marks if an interval was specified in
1011 setYTicks: function(iStartY, iTickSize) {
1013 this.yTickSize = iTickSize;
1017 for (var i = this.initPageY; i >= this.minY; i = i - iTickSize) {
1019 this.yTicks[this.yTicks.length] = i;
1024 for (i = this.initPageY; i <= this.maxY; i = i + iTickSize) {
1026 this.yTicks[this.yTicks.length] = i;
1031 this.yTicks.sort(this.DDM.numericSort) ;
1035 * By default, the element can be dragged any place on the screen. Use
1036 * this method to limit the horizontal travel of the element. Pass in
1037 * 0,0 for the parameters if you want to lock the drag to the y axis.
1038 * @method setXConstraint
1039 * @param {int} iLeft the number of pixels the element can move to the left
1040 * @param {int} iRight the number of pixels the element can move to the
1042 * @param {int} iTickSize optional parameter for specifying that the
1044 * should move iTickSize pixels at a time.
1046 setXConstraint: function(iLeft, iRight, iTickSize) {
1047 this.leftConstraint = iLeft;
1048 this.rightConstraint = iRight;
1050 this.minX = this.initPageX - iLeft;
1051 this.maxX = this.initPageX + iRight;
1052 if (iTickSize) { this.setXTicks(this.initPageX, iTickSize); }
1054 this.constrainX = true;
1058 * Clears any constraints applied to this instance. Also clears ticks
1059 * since they can't exist independent of a constraint at this time.
1060 * @method clearConstraints
1062 clearConstraints: function() {
1063 this.constrainX = false;
1064 this.constrainY = false;
1069 * Clears any tick interval defined for this instance
1070 * @method clearTicks
1072 clearTicks: function() {
1080 * By default, the element can be dragged any place on the screen. Set
1081 * this to limit the vertical travel of the element. Pass in 0,0 for the
1082 * parameters if you want to lock the drag to the x axis.
1083 * @method setYConstraint
1084 * @param {int} iUp the number of pixels the element can move up
1085 * @param {int} iDown the number of pixels the element can move down
1086 * @param {int} iTickSize optional parameter for specifying that the
1087 * element should move iTickSize pixels at a time.
1089 setYConstraint: function(iUp, iDown, iTickSize) {
1090 this.topConstraint = iUp;
1091 this.bottomConstraint = iDown;
1093 this.minY = this.initPageY - iUp;
1094 this.maxY = this.initPageY + iDown;
1095 if (iTickSize) { this.setYTicks(this.initPageY, iTickSize); }
1097 this.constrainY = true;
1102 * resetConstraints must be called if you manually reposition a dd element.
1103 * @method resetConstraints
1104 * @param {boolean} maintainOffset
1106 resetConstraints: function() {
1109 // Maintain offsets if necessary
1110 if (this.initPageX || this.initPageX === 0) {
1111 // figure out how much this thing has moved
1112 var dx = (this.maintainOffset) ? this.lastPageX - this.initPageX : 0;
1113 var dy = (this.maintainOffset) ? this.lastPageY - this.initPageY : 0;
1115 this.setInitPosition(dx, dy);
1117 // This is the first time we have detected the element's position
1119 this.setInitPosition();
1122 if (this.constrainX) {
1123 this.setXConstraint( this.leftConstraint,
1124 this.rightConstraint,
1128 if (this.constrainY) {
1129 this.setYConstraint( this.topConstraint,
1130 this.bottomConstraint,
1136 * Normally the drag element is moved pixel by pixel, but we can specify
1137 * that it move a number of pixels at a time. This method resolves the
1138 * location when we have it set up like this.
1140 * @param {int} val where we want to place the object
1141 * @param {int[]} tickArray sorted array of valid points
1142 * @return {int} the closest tick
1145 getTick: function(val, tickArray) {
1148 // If tick interval is not defined, it is effectively 1 pixel,
1149 // so we return the value passed to us.
1151 } else if (tickArray[0] >= val) {
1152 // The value is lower than the first tick, so we return the first
1154 return tickArray[0];
1156 for (var i=0, len=tickArray.length; i<len; ++i) {
1158 if (tickArray[next] && tickArray[next] >= val) {
1159 var diff1 = val - tickArray[i];
1160 var diff2 = tickArray[next] - val;
1161 return (diff2 > diff1) ? tickArray[i] : tickArray[next];
1165 // The value is larger than the last tick, so we return the last
1167 return tickArray[tickArray.length - 1];
1174 * @return {string} string representation of the dd obj
1176 toString: function() {
1177 return ("DragDrop " + this.id);
1184 * The drag and drop utility provides a framework for building drag and drop
1185 * applications. In addition to enabling drag and drop for specific elements,
1186 * the drag and drop elements are tracked by the manager class, and the
1187 * interactions between the various elements are tracked during the drag and
1188 * the implementing code is notified about these important moments.
1191 // Only load the library once. Rewriting the manager class would orphan
1192 // existing drag and drop instances.
1193 if (!Ext.dd.DragDropMgr) {
1196 * @class Ext.dd.DragDropMgr
1197 * DragDropMgr is a singleton that tracks the element interaction for
1198 * all DragDrop items in the window. Generally, you will not call
1199 * this class directly, but it does have helper methods that could
1200 * be useful in your DragDrop implementations.
1203 Ext.dd.DragDropMgr = function() {
1205 var Event = Ext.EventManager;
1210 * Two dimensional Array of registered DragDrop objects. The first
1211 * dimension is the DragDrop item group, the second the DragDrop
1214 * @type {string: string}
1221 * Array of element ids defined as drag handles. Used to determine
1222 * if the element that generated the mousedown event is actually the
1223 * handle and not the html element itself.
1224 * @property handleIds
1225 * @type {string: string}
1232 * the DragDrop object that is currently being dragged
1233 * @property dragCurrent
1241 * the DragDrop object(s) that are being hovered over
1242 * @property dragOvers
1250 * the X distance between the cursor and the object being dragged
1259 * the Y distance between the cursor and the object being dragged
1268 * Flag to determine if we should prevent the default behavior of the
1269 * events we define. By default this is true, but this can be set to
1270 * false if you need the default behavior (not recommended)
1271 * @property preventDefault
1275 preventDefault: true,
1278 * Flag to determine if we should stop the propagation of the events
1279 * we generate. This is true by default but you may want to set it to
1280 * false if the html element contains other features that require the
1282 * @property stopPropagation
1286 stopPropagation: true,
1289 * Internal flag that is set to true when drag and drop has been
1291 * @property initialized
1298 * All drag and drop can be disabled.
1306 * Called the first time an element is registered.
1312 this.initialized = true;
1316 * In point mode, drag and drop interaction is defined by the
1317 * location of the cursor during the drag/drop
1325 * In intersect mode, drag and drop interaction is defined by the
1326 * overlap of two or more drag and drop objects.
1327 * @property INTERSECT
1334 * The current drag and drop mode. Default: POINT
1342 * Runs method on all drag and drop objects
1343 * @method _execOnAll
1347 _execOnAll: function(sMethod, args) {
1348 for (var i in this.ids) {
1349 for (var j in this.ids[i]) {
1350 var oDD = this.ids[i][j];
1351 if (! this.isTypeOfDD(oDD)) {
1354 oDD[sMethod].apply(oDD, args);
1360 * Drag and drop initialization. Sets up the global event handlers
1365 _onLoad: function() {
1370 Event.on(document, "mouseup", this.handleMouseUp, this, true);
1371 Event.on(document, "mousemove", this.handleMouseMove, this, true);
1372 Event.on(window, "unload", this._onUnload, this, true);
1373 Event.on(window, "resize", this._onResize, this, true);
1374 // Event.on(window, "mouseout", this._test);
1379 * Reset constraints on all drag and drop objs
1384 _onResize: function(e) {
1385 this._execOnAll("resetConstraints", []);
1389 * Lock all drag and drop functionality
1393 lock: function() { this.locked = true; },
1396 * Unlock all drag and drop functionality
1400 unlock: function() { this.locked = false; },
1403 * Is drag and drop locked?
1405 * @return {boolean} True if drag and drop is locked, false otherwise.
1408 isLocked: function() { return this.locked; },
1411 * Location cache that is set for all drag drop objects when a drag is
1412 * initiated, cleared when the drag is finished.
1413 * @property locationCache
1420 * Set useCache to false if you want to force object the lookup of each
1421 * drag and drop linked element constantly during a drag.
1422 * @property useCache
1429 * The number of pixels that the mouse needs to move after the
1430 * mousedown before the drag is initiated. Default=3;
1431 * @property clickPixelThresh
1435 clickPixelThresh: 3,
1438 * The number of milliseconds after the mousedown event to initiate the
1439 * drag if we don't get a mouseup event. Default=1000
1440 * @property clickTimeThresh
1444 clickTimeThresh: 350,
1447 * Flag that indicates that either the drag pixel threshold or the
1448 * mousdown time threshold has been met
1449 * @property dragThreshMet
1454 dragThreshMet: false,
1457 * Timeout used for the click time threshold
1458 * @property clickTimeout
1466 * The X position of the mousedown event stored for later use when a
1467 * drag threshold is met.
1476 * The Y position of the mousedown event stored for later use when a
1477 * drag threshold is met.
1486 * Each DragDrop instance must be registered with the DragDropMgr.
1487 * This is executed in DragDrop.init()
1488 * @method regDragDrop
1489 * @param {DragDrop} oDD the DragDrop object to register
1490 * @param {String} sGroup the name of the group this element belongs to
1493 regDragDrop: function(oDD, sGroup) {
1494 if (!this.initialized) { this.init(); }
1496 if (!this.ids[sGroup]) {
1497 this.ids[sGroup] = {};
1499 this.ids[sGroup][oDD.id] = oDD;
1503 * Removes the supplied dd instance from the supplied group. Executed
1504 * by DragDrop.removeFromGroup, so don't call this function directly.
1505 * @method removeDDFromGroup
1509 removeDDFromGroup: function(oDD, sGroup) {
1510 if (!this.ids[sGroup]) {
1511 this.ids[sGroup] = {};
1514 var obj = this.ids[sGroup];
1515 if (obj && obj[oDD.id]) {
1521 * Unregisters a drag and drop item. This is executed in
1522 * DragDrop.unreg, use that method instead of calling this directly.
1527 _remove: function(oDD) {
1528 for (var g in oDD.groups) {
1529 if (g && this.ids[g] && this.ids[g][oDD.id]) {
1530 delete this.ids[g][oDD.id];
1533 delete this.handleIds[oDD.id];
1537 * Each DragDrop handle element must be registered. This is done
1538 * automatically when executing DragDrop.setHandleElId()
1540 * @param {String} sDDId the DragDrop id this element is a handle for
1541 * @param {String} sHandleId the id of the element that is the drag
1545 regHandle: function(sDDId, sHandleId) {
1546 if (!this.handleIds[sDDId]) {
1547 this.handleIds[sDDId] = {};
1549 this.handleIds[sDDId][sHandleId] = sHandleId;
1553 * Utility function to determine if a given element has been
1554 * registered as a drag drop item.
1555 * @method isDragDrop
1556 * @param {String} id the element id to check
1557 * @return {boolean} true if this element is a DragDrop item,
1561 isDragDrop: function(id) {
1562 return ( this.getDDById(id) ) ? true : false;
1566 * Returns the drag and drop instances that are in all groups the
1567 * passed in instance belongs to.
1568 * @method getRelated
1569 * @param {DragDrop} p_oDD the obj to get related data for
1570 * @param {boolean} bTargetsOnly if true, only return targetable objs
1571 * @return {DragDrop[]} the related instances
1574 getRelated: function(p_oDD, bTargetsOnly) {
1576 for (var i in p_oDD.groups) {
1577 for (var j in this.ids[i]) {
1578 var dd = this.ids[i][j];
1579 if (! this.isTypeOfDD(dd)) {
1582 if (!bTargetsOnly || dd.isTarget) {
1583 oDDs[oDDs.length] = dd;
1592 * Returns true if the specified dd target is a legal target for
1593 * the specifice drag obj
1594 * @method isLegalTarget
1595 * @param {DragDrop} the drag obj
1596 * @param {DragDrop} the target
1597 * @return {boolean} true if the target is a legal target for the
1601 isLegalTarget: function (oDD, oTargetDD) {
1602 var targets = this.getRelated(oDD, true);
1603 for (var i=0, len=targets.length;i<len;++i) {
1604 if (targets[i].id == oTargetDD.id) {
1613 * My goal is to be able to transparently determine if an object is
1614 * typeof DragDrop, and the exact subclass of DragDrop. typeof
1615 * returns "object", oDD.constructor.toString() always returns
1616 * "DragDrop" and not the name of the subclass. So for now it just
1617 * evaluates a well-known variable in DragDrop.
1618 * @method isTypeOfDD
1619 * @param {Object} the object to evaluate
1620 * @return {boolean} true if typeof oDD = DragDrop
1623 isTypeOfDD: function (oDD) {
1624 return (oDD && oDD.__ygDragDrop);
1628 * Utility function to determine if a given element has been
1629 * registered as a drag drop handle for the given Drag Drop object.
1631 * @param {String} id the element id to check
1632 * @return {boolean} true if this element is a DragDrop handle, false
1636 isHandle: function(sDDId, sHandleId) {
1637 return ( this.handleIds[sDDId] &&
1638 this.handleIds[sDDId][sHandleId] );
1642 * Returns the DragDrop instance for a given id
1644 * @param {String} id the id of the DragDrop object
1645 * @return {DragDrop} the drag drop object, null if it is not found
1648 getDDById: function(id) {
1649 for (var i in this.ids) {
1650 if (this.ids[i][id]) {
1651 return this.ids[i][id];
1658 * Fired after a registered DragDrop object gets the mousedown event.
1659 * Sets up the events required to track the object being dragged
1660 * @method handleMouseDown
1661 * @param {Event} e the event
1662 * @param oDD the DragDrop object being dragged
1666 handleMouseDown: function(e, oDD) {
1668 Ext.QuickTips.disable();
1670 if(this.dragCurrent){
1671 // the original browser mouseup wasn't handled (e.g. outside FF browser window)
1672 // so clean up first to avoid breaking the next drag
1673 this.handleMouseUp(e);
1676 this.currentTarget = e.getTarget();
1677 this.dragCurrent = oDD;
1679 var el = oDD.getEl();
1681 // track start position
1682 this.startX = e.getPageX();
1683 this.startY = e.getPageY();
1685 this.deltaX = this.startX - el.offsetLeft;
1686 this.deltaY = this.startY - el.offsetTop;
1688 this.dragThreshMet = false;
1690 this.clickTimeout = setTimeout(
1692 var DDM = Ext.dd.DDM;
1693 DDM.startDrag(DDM.startX, DDM.startY);
1695 this.clickTimeThresh );
1699 * Fired when either the drag pixel threshol or the mousedown hold
1700 * time threshold has been met.
1702 * @param x {int} the X position of the original mousedown
1703 * @param y {int} the Y position of the original mousedown
1706 startDrag: function(x, y) {
1707 clearTimeout(this.clickTimeout);
1708 if (this.dragCurrent) {
1709 this.dragCurrent.b4StartDrag(x, y);
1710 this.dragCurrent.startDrag(x, y);
1712 this.dragThreshMet = true;
1716 * Internal function to handle the mouseup event. Will be invoked
1717 * from the context of the document.
1718 * @method handleMouseUp
1719 * @param {Event} e the event
1723 handleMouseUp: function(e) {
1726 Ext.QuickTips.enable();
1728 if (! this.dragCurrent) {
1732 clearTimeout(this.clickTimeout);
1734 if (this.dragThreshMet) {
1735 this.fireEvents(e, true);
1745 * Utility to stop event propagation and event default, if these
1746 * features are turned on.
1748 * @param {Event} e the event as returned by this.getEvent()
1751 stopEvent: function(e){
1752 if(this.stopPropagation) {
1753 e.stopPropagation();
1756 if (this.preventDefault) {
1762 * Internal function to clean up event handlers after the drag
1763 * operation is complete
1765 * @param {Event} e the event
1769 stopDrag: function(e) {
1770 // Fire the drag end event for the item that was dragged
1771 if (this.dragCurrent) {
1772 if (this.dragThreshMet) {
1773 this.dragCurrent.b4EndDrag(e);
1774 this.dragCurrent.endDrag(e);
1777 this.dragCurrent.onMouseUp(e);
1780 this.dragCurrent = null;
1781 this.dragOvers = {};
1785 * Internal function to handle the mousemove event. Will be invoked
1786 * from the context of the html element.
1788 * @TODO figure out what we can do about mouse events lost when the
1789 * user drags objects beyond the window boundary. Currently we can
1790 * detect this in internet explorer by verifying that the mouse is
1791 * down during the mousemove event. Firefox doesn't give us the
1792 * button state on the mousemove event.
1793 * @method handleMouseMove
1794 * @param {Event} e the event
1798 handleMouseMove: function(e) {
1799 if (! this.dragCurrent) {
1802 // var button = e.which || e.button;
1804 // check for IE mouseup outside of page boundary
1805 if (Ext.isIE && (e.button !== 0 && e.button !== 1 && e.button !== 2)) {
1807 return this.handleMouseUp(e);
1810 if (!this.dragThreshMet) {
1811 var diffX = Math.abs(this.startX - e.getPageX());
1812 var diffY = Math.abs(this.startY - e.getPageY());
1813 if (diffX > this.clickPixelThresh ||
1814 diffY > this.clickPixelThresh) {
1815 this.startDrag(this.startX, this.startY);
1819 if (this.dragThreshMet) {
1820 this.dragCurrent.b4Drag(e);
1821 this.dragCurrent.onDrag(e);
1822 if(!this.dragCurrent.moveOnly){
1823 this.fireEvents(e, false);
1833 * Iterates over all of the DragDrop elements to find ones we are
1834 * hovering over or dropping on
1835 * @method fireEvents
1836 * @param {Event} e the event
1837 * @param {boolean} isDrop is this a drop op or a mouseover op?
1841 fireEvents: function(e, isDrop) {
1842 var dc = this.dragCurrent;
1844 // If the user did the mouse up outside of the window, we could
1845 // get here even though we have ended the drag.
1846 if (!dc || dc.isLocked()) {
1850 var pt = e.getPoint();
1852 // cache the previous dragOver array
1860 // Check to see if the object(s) we were hovering over is no longer
1861 // being hovered over so we can fire the onDragOut event
1862 for (var i in this.dragOvers) {
1864 var ddo = this.dragOvers[i];
1866 if (! this.isTypeOfDD(ddo)) {
1870 if (! this.isOverTarget(pt, ddo, this.mode)) {
1871 outEvts.push( ddo );
1875 delete this.dragOvers[i];
1878 for (var sGroup in dc.groups) {
1880 if ("string" != typeof sGroup) {
1884 for (i in this.ids[sGroup]) {
1885 var oDD = this.ids[sGroup][i];
1886 if (! this.isTypeOfDD(oDD)) {
1890 if (oDD.isTarget && !oDD.isLocked() && ((oDD != dc) || (dc.ignoreSelf === false))) {
1891 if (this.isOverTarget(pt, oDD, this.mode)) {
1892 // look for drop interactions
1894 dropEvts.push( oDD );
1895 // look for drag enter and drag over interactions
1898 // initial drag over: dragEnter fires
1899 if (!oldOvers[oDD.id]) {
1900 enterEvts.push( oDD );
1901 // subsequent drag overs: dragOver fires
1903 overEvts.push( oDD );
1906 this.dragOvers[oDD.id] = oDD;
1914 if (outEvts.length) {
1915 dc.b4DragOut(e, outEvts);
1916 dc.onDragOut(e, outEvts);
1919 if (enterEvts.length) {
1920 dc.onDragEnter(e, enterEvts);
1923 if (overEvts.length) {
1924 dc.b4DragOver(e, overEvts);
1925 dc.onDragOver(e, overEvts);
1928 if (dropEvts.length) {
1929 dc.b4DragDrop(e, dropEvts);
1930 dc.onDragDrop(e, dropEvts);
1934 // fire dragout events
1936 for (i=0, len=outEvts.length; i<len; ++i) {
1937 dc.b4DragOut(e, outEvts[i].id);
1938 dc.onDragOut(e, outEvts[i].id);
1941 // fire enter events
1942 for (i=0,len=enterEvts.length; i<len; ++i) {
1943 // dc.b4DragEnter(e, oDD.id);
1944 dc.onDragEnter(e, enterEvts[i].id);
1948 for (i=0,len=overEvts.length; i<len; ++i) {
1949 dc.b4DragOver(e, overEvts[i].id);
1950 dc.onDragOver(e, overEvts[i].id);
1954 for (i=0, len=dropEvts.length; i<len; ++i) {
1955 dc.b4DragDrop(e, dropEvts[i].id);
1956 dc.onDragDrop(e, dropEvts[i].id);
1961 // notify about a drop that did not find a target
1962 if (isDrop && !dropEvts.length) {
1963 dc.onInvalidDrop(e);
1969 * Helper function for getting the best match from the list of drag
1970 * and drop objects returned by the drag and drop events when we are
1971 * in INTERSECT mode. It returns either the first object that the
1972 * cursor is over, or the object that has the greatest overlap with
1973 * the dragged element.
1974 * @method getBestMatch
1975 * @param {DragDrop[]} dds The array of drag and drop objects
1977 * @return {DragDrop} The best single match
1980 getBestMatch: function(dds) {
1982 // Return null if the input is not what we expect
1983 //if (!dds || !dds.length || dds.length == 0) {
1985 // If there is only one item, it wins
1986 //} else if (dds.length == 1) {
1988 var len = dds.length;
1993 // Loop through the targeted items
1994 for (var i=0; i<len; ++i) {
1996 // If the cursor is over the object, it wins. If the
1997 // cursor is over multiple matches, the first one we come
1999 if (dd.cursorIsOver) {
2002 // Otherwise the object with the most overlap wins
2005 winner.overlap.getArea() < dd.overlap.getArea()) {
2016 * Refreshes the cache of the top-left and bottom-right points of the
2017 * drag and drop objects in the specified group(s). This is in the
2018 * format that is stored in the drag and drop instance, so typical
2021 * Ext.dd.DragDropMgr.refreshCache(ddinstance.groups);
2025 * Ext.dd.DragDropMgr.refreshCache({group1:true, group2:true});
2027 * @TODO this really should be an indexed array. Alternatively this
2028 * method could accept both.
2029 * @method refreshCache
2030 * @param {Object} groups an associative array of groups to refresh
2033 refreshCache: function(groups) {
2034 for (var sGroup in groups) {
2035 if ("string" != typeof sGroup) {
2038 for (var i in this.ids[sGroup]) {
2039 var oDD = this.ids[sGroup][i];
2041 if (this.isTypeOfDD(oDD)) {
2042 // if (this.isTypeOfDD(oDD) && oDD.isTarget) {
2043 var loc = this.getLocation(oDD);
2045 this.locationCache[oDD.id] = loc;
2047 delete this.locationCache[oDD.id];
2048 // this will unregister the drag and drop object if
2049 // the element is not in a usable state
2058 * This checks to make sure an element exists and is in the DOM. The
2059 * main purpose is to handle cases where innerHTML is used to remove
2060 * drag and drop objects from the DOM. IE provides an 'unspecified
2061 * error' when trying to access the offsetParent of such an element
2063 * @param {HTMLElement} el the element to check
2064 * @return {boolean} true if the element looks usable
2067 verifyEl: function(el) {
2072 parent = el.offsetParent;
2075 parent = el.offsetParent;
2086 * Returns a Region object containing the drag and drop element's position
2087 * and size, including the padding configured for it
2088 * @method getLocation
2089 * @param {DragDrop} oDD the drag and drop object to get the
2091 * @return {Ext.lib.Region} a Region object representing the total area
2092 * the element occupies, including any padding
2093 * the instance is configured for.
2096 getLocation: function(oDD) {
2097 if (! this.isTypeOfDD(oDD)) {
2101 var el = oDD.getEl(), pos, x1, x2, y1, y2, t, r, b, l;
2104 pos= Ext.lib.Dom.getXY(el);
2112 x2 = x1 + el.offsetWidth;
2114 y2 = y1 + el.offsetHeight;
2116 t = y1 - oDD.padding[0];
2117 r = x2 + oDD.padding[1];
2118 b = y2 + oDD.padding[2];
2119 l = x1 - oDD.padding[3];
2121 return new Ext.lib.Region( t, r, b, l );
2125 * Checks the cursor location to see if it over the target
2126 * @method isOverTarget
2127 * @param {Ext.lib.Point} pt The point to evaluate
2128 * @param {DragDrop} oTarget the DragDrop object we are inspecting
2129 * @return {boolean} true if the mouse is over the target
2133 isOverTarget: function(pt, oTarget, intersect) {
2134 // use cache if available
2135 var loc = this.locationCache[oTarget.id];
2136 if (!loc || !this.useCache) {
2137 loc = this.getLocation(oTarget);
2138 this.locationCache[oTarget.id] = loc;
2146 oTarget.cursorIsOver = loc.contains( pt );
2148 // DragDrop is using this as a sanity check for the initial mousedown
2149 // in this case we are done. In POINT mode, if the drag obj has no
2150 // contraints, we are also done. Otherwise we need to evaluate the
2151 // location of the target as related to the actual location of the
2153 var dc = this.dragCurrent;
2154 if (!dc || !dc.getTargetCoord ||
2155 (!intersect && !dc.constrainX && !dc.constrainY)) {
2156 return oTarget.cursorIsOver;
2159 oTarget.overlap = null;
2161 // Get the current location of the drag element, this is the
2162 // location of the mouse event less the delta that represents
2163 // where the original mousedown happened on the element. We
2164 // need to consider constraints and ticks as well.
2165 var pos = dc.getTargetCoord(pt.x, pt.y);
2167 var el = dc.getDragEl();
2168 var curRegion = new Ext.lib.Region( pos.y,
2169 pos.x + el.offsetWidth,
2170 pos.y + el.offsetHeight,
2173 var overlap = curRegion.intersect(loc);
2176 oTarget.overlap = overlap;
2177 return (intersect) ? true : oTarget.cursorIsOver;
2184 * unload event handler
2189 _onUnload: function(e, me) {
2190 Ext.dd.DragDropMgr.unregAll();
2194 * Cleans up the drag and drop events and objects.
2199 unregAll: function() {
2201 if (this.dragCurrent) {
2203 this.dragCurrent = null;
2206 this._execOnAll("unreg", []);
2208 for (var i in this.elementCache) {
2209 delete this.elementCache[i];
2212 this.elementCache = {};
2217 * A cache of DOM elements
2218 * @property elementCache
2225 * Get the wrapper for the DOM element specified
2226 * @method getElWrapper
2227 * @param {String} id the id of the element to get
2228 * @return {Ext.dd.DDM.ElementWrapper} the wrapped element
2230 * @deprecated This wrapper isn't that useful
2233 getElWrapper: function(id) {
2234 var oWrapper = this.elementCache[id];
2235 if (!oWrapper || !oWrapper.el) {
2236 oWrapper = this.elementCache[id] =
2237 new this.ElementWrapper(Ext.getDom(id));
2243 * Returns the actual DOM element
2244 * @method getElement
2245 * @param {String} id the id of the elment to get
2246 * @return {Object} The element
2247 * @deprecated use Ext.lib.Ext.getDom instead
2250 getElement: function(id) {
2251 return Ext.getDom(id);
2255 * Returns the style property for the DOM element (i.e.,
2256 * document.getElById(id).style)
2258 * @param {String} id the id of the elment to get
2259 * @return {Object} The style property of the element
2260 * @deprecated use Ext.lib.Dom instead
2263 getCss: function(id) {
2264 var el = Ext.getDom(id);
2265 return (el) ? el.style : null;
2269 * Inner class for cached elements
2270 * @class DragDropMgr.ElementWrapper
2275 ElementWrapper: function(el) {
2280 this.el = el || null;
2285 this.id = this.el && el.id;
2287 * A reference to the style property
2290 this.css = this.el && el.style;
2294 * Returns the X position of an html element
2296 * @param el the element for which to get the position
2297 * @return {int} the X coordinate
2299 * @deprecated use Ext.lib.Dom.getX instead
2302 getPosX: function(el) {
2303 return Ext.lib.Dom.getX(el);
2307 * Returns the Y position of an html element
2309 * @param el the element for which to get the position
2310 * @return {int} the Y coordinate
2311 * @deprecated use Ext.lib.Dom.getY instead
2314 getPosY: function(el) {
2315 return Ext.lib.Dom.getY(el);
2319 * Swap two nodes. In IE, we use the native method, for others we
2320 * emulate the IE behavior
2322 * @param n1 the first node to swap
2323 * @param n2 the other node to swap
2326 swapNode: function(n1, n2) {
2330 var p = n2.parentNode;
2331 var s = n2.nextSibling;
2334 p.insertBefore(n1, n2);
2335 } else if (n2 == n1.nextSibling) {
2336 p.insertBefore(n2, n1);
2338 n1.parentNode.replaceChild(n2, n1);
2339 p.insertBefore(n1, s);
2345 * Returns the current scroll position
2350 getScroll: function () {
2351 var t, l, dde=document.documentElement, db=document.body;
2352 if (dde && (dde.scrollTop || dde.scrollLeft)) {
2361 return { top: t, left: l };
2365 * Returns the specified element style property
2367 * @param {HTMLElement} el the element
2368 * @param {string} styleProp the style property
2369 * @return {string} The value of the style property
2370 * @deprecated use Ext.lib.Dom.getStyle
2373 getStyle: function(el, styleProp) {
2374 return Ext.fly(el).getStyle(styleProp);
2378 * Gets the scrollTop
2379 * @method getScrollTop
2380 * @return {int} the document's scrollTop
2383 getScrollTop: function () { return this.getScroll().top; },
2386 * Gets the scrollLeft
2387 * @method getScrollLeft
2388 * @return {int} the document's scrollTop
2391 getScrollLeft: function () { return this.getScroll().left; },
2394 * Sets the x/y position of an element to the location of the
2397 * @param {HTMLElement} moveEl The element to move
2398 * @param {HTMLElement} targetEl The position reference element
2401 moveToEl: function (moveEl, targetEl) {
2402 var aCoord = Ext.lib.Dom.getXY(targetEl);
2403 Ext.lib.Dom.setXY(moveEl, aCoord);
2407 * Numeric array sort function
2408 * @method numericSort
2411 numericSort: function(a, b) { return (a - b); },
2415 * @property _timeoutCount
2422 * Trying to make the load order less important. Without this we get
2423 * an error if this file is loaded before the Event Utility.
2424 * @method _addListeners
2428 _addListeners: function() {
2429 var DDM = Ext.dd.DDM;
2430 if ( Ext.lib.Event && document ) {
2433 if (DDM._timeoutCount > 2000) {
2435 setTimeout(DDM._addListeners, 10);
2436 if (document && document.body) {
2437 DDM._timeoutCount += 1;
2444 * Recursively searches the immediate parent and all child nodes for
2445 * the handle element in order to determine wheter or not it was
2447 * @method handleWasClicked
2448 * @param node the html element to inspect
2451 handleWasClicked: function(node, id) {
2452 if (this.isHandle(id, node.id)) {
2455 // check to see if this is a text node child of the one we want
2456 var p = node.parentNode;
2459 if (this.isHandle(id, p.id)) {
2474 // shorter alias, save a few bytes
2475 Ext.dd.DDM = Ext.dd.DragDropMgr;
2476 Ext.dd.DDM._addListeners();
2482 * A DragDrop implementation where the linked element follows the
2483 * mouse cursor during a drag.
2484 * @extends Ext.dd.DragDrop
2486 * @param {String} id the id of the linked element
2487 * @param {String} sGroup the group of related DragDrop items
2488 * @param {object} config an object containing configurable attributes
2489 * Valid properties for DD:
2492 Ext.dd.DD = function(id, sGroup, config) {
2494 this.init(id, sGroup, config);
2498 Ext.extend(Ext.dd.DD, Ext.dd.DragDrop, {
2501 * When set to true, the utility automatically tries to scroll the browser
2502 * window when a drag and drop element is dragged near the viewport boundary.
2510 * Sets the pointer offset to the distance between the linked element's top
2511 * left corner and the location the element was clicked
2512 * @method autoOffset
2513 * @param {int} iPageX the X coordinate of the click
2514 * @param {int} iPageY the Y coordinate of the click
2516 autoOffset: function(iPageX, iPageY) {
2517 var x = iPageX - this.startPageX;
2518 var y = iPageY - this.startPageY;
2519 this.setDelta(x, y);
2523 * Sets the pointer offset. You can call this directly to force the
2524 * offset to be in a particular location (e.g., pass in 0,0 to set it
2525 * to the center of the object)
2527 * @param {int} iDeltaX the distance from the left
2528 * @param {int} iDeltaY the distance from the top
2530 setDelta: function(iDeltaX, iDeltaY) {
2531 this.deltaX = iDeltaX;
2532 this.deltaY = iDeltaY;
2536 * Sets the drag element to the location of the mousedown or click event,
2537 * maintaining the cursor location relative to the location on the element
2538 * that was clicked. Override this if you want to place the element in a
2539 * location other than where the cursor is.
2540 * @method setDragElPos
2541 * @param {int} iPageX the X coordinate of the mousedown or drag event
2542 * @param {int} iPageY the Y coordinate of the mousedown or drag event
2544 setDragElPos: function(iPageX, iPageY) {
2545 // the first time we do this, we are going to check to make sure
2546 // the element has css positioning
2548 var el = this.getDragEl();
2549 this.alignElWithMouse(el, iPageX, iPageY);
2553 * Sets the element to the location of the mousedown or click event,
2554 * maintaining the cursor location relative to the location on the element
2555 * that was clicked. Override this if you want to place the element in a
2556 * location other than where the cursor is.
2557 * @method alignElWithMouse
2558 * @param {HTMLElement} el the element to move
2559 * @param {int} iPageX the X coordinate of the mousedown or drag event
2560 * @param {int} iPageY the Y coordinate of the mousedown or drag event
2562 alignElWithMouse: function(el, iPageX, iPageY) {
2563 var oCoord = this.getTargetCoord(iPageX, iPageY);
2564 var fly = el.dom ? el : Ext.fly(el, '_dd');
2565 if (!this.deltaSetXY) {
2566 var aCoord = [oCoord.x, oCoord.y];
2568 var newLeft = fly.getLeft(true);
2569 var newTop = fly.getTop(true);
2570 this.deltaSetXY = [ newLeft - oCoord.x, newTop - oCoord.y ];
2572 fly.setLeftTop(oCoord.x + this.deltaSetXY[0], oCoord.y + this.deltaSetXY[1]);
2575 this.cachePosition(oCoord.x, oCoord.y);
2576 this.autoScroll(oCoord.x, oCoord.y, el.offsetHeight, el.offsetWidth);
2581 * Saves the most recent position so that we can reset the constraints and
2582 * tick marks on-demand. We need to know this so that we can calculate the
2583 * number of pixels the element is offset from its original position.
2584 * @method cachePosition
2585 * @param iPageX the current x position (optional, this just makes it so we
2586 * don't have to look it up again)
2587 * @param iPageY the current y position (optional, this just makes it so we
2588 * don't have to look it up again)
2590 cachePosition: function(iPageX, iPageY) {
2592 this.lastPageX = iPageX;
2593 this.lastPageY = iPageY;
2595 var aCoord = Ext.lib.Dom.getXY(this.getEl());
2596 this.lastPageX = aCoord[0];
2597 this.lastPageY = aCoord[1];
2602 * Auto-scroll the window if the dragged object has been moved beyond the
2603 * visible window boundary.
2604 * @method autoScroll
2605 * @param {int} x the drag element's x position
2606 * @param {int} y the drag element's y position
2607 * @param {int} h the height of the drag element
2608 * @param {int} w the width of the drag element
2611 autoScroll: function(x, y, h, w) {
2614 // The client height
2615 var clientH = Ext.lib.Dom.getViewHeight();
2618 var clientW = Ext.lib.Dom.getViewWidth();
2620 // The amt scrolled down
2621 var st = this.DDM.getScrollTop();
2623 // The amt scrolled right
2624 var sl = this.DDM.getScrollLeft();
2626 // Location of the bottom of the element
2629 // Location of the right of the element
2632 // The distance from the cursor to the bottom of the visible area,
2633 // adjusted so that we don't scroll if the cursor is beyond the
2634 // element drag constraints
2635 var toBot = (clientH + st - y - this.deltaY);
2637 // The distance from the cursor to the right of the visible area
2638 var toRight = (clientW + sl - x - this.deltaX);
2641 // How close to the edge the cursor must be before we scroll
2642 // var thresh = (document.all) ? 100 : 40;
2645 // How many pixels to scroll per autoscroll op. This helps to reduce
2646 // clunky scrolling. IE is more sensitive about this ... it needs this
2647 // value to be higher.
2648 var scrAmt = (document.all) ? 80 : 30;
2650 // Scroll down if we are near the bottom of the visible page and the
2651 // obj extends below the crease
2652 if ( bot > clientH && toBot < thresh ) {
2653 window.scrollTo(sl, st + scrAmt);
2656 // Scroll up if the window is scrolled down and the top of the object
2657 // goes above the top border
2658 if ( y < st && st > 0 && y - st < thresh ) {
2659 window.scrollTo(sl, st - scrAmt);
2662 // Scroll right if the obj is beyond the right border and the cursor is
2664 if ( right > clientW && toRight < thresh ) {
2665 window.scrollTo(sl + scrAmt, st);
2668 // Scroll left if the window has been scrolled to the right and the obj
2669 // extends past the left border
2670 if ( x < sl && sl > 0 && x - sl < thresh ) {
2671 window.scrollTo(sl - scrAmt, st);
2677 * Finds the location the element should be placed if we want to move
2678 * it to where the mouse location less the click offset would place us.
2679 * @method getTargetCoord
2680 * @param {int} iPageX the X coordinate of the click
2681 * @param {int} iPageY the Y coordinate of the click
2682 * @return an object that contains the coordinates (Object.x and Object.y)
2685 getTargetCoord: function(iPageX, iPageY) {
2688 var x = iPageX - this.deltaX;
2689 var y = iPageY - this.deltaY;
2691 if (this.constrainX) {
2692 if (x < this.minX) { x = this.minX; }
2693 if (x > this.maxX) { x = this.maxX; }
2696 if (this.constrainY) {
2697 if (y < this.minY) { y = this.minY; }
2698 if (y > this.maxY) { y = this.maxY; }
2701 x = this.getTick(x, this.xTicks);
2702 y = this.getTick(y, this.yTicks);
2709 * Sets up config options specific to this class. Overrides
2710 * Ext.dd.DragDrop, but all versions of this method through the
2711 * inheritance chain are called
2713 applyConfig: function() {
2714 Ext.dd.DD.superclass.applyConfig.call(this);
2715 this.scroll = (this.config.scroll !== false);
2719 * Event that fires prior to the onMouseDown event. Overrides
2722 b4MouseDown: function(e) {
2723 // this.resetConstraints();
2724 this.autoOffset(e.getPageX(),
2729 * Event that fires prior to the onDrag event. Overrides
2732 b4Drag: function(e) {
2733 this.setDragElPos(e.getPageX(),
2737 toString: function() {
2738 return ("DD " + this.id);
2741 //////////////////////////////////////////////////////////////////////////
2742 // Debugging ygDragDrop events that can be overridden
2743 //////////////////////////////////////////////////////////////////////////
2745 startDrag: function(x, y) {
2748 onDrag: function(e) {
2751 onDragEnter: function(e, id) {
2754 onDragOver: function(e, id) {
2757 onDragOut: function(e, id) {
2760 onDragDrop: function(e, id) {
2763 endDrag: function(e) {
2770 * @class Ext.dd.DDProxy
2771 * A DragDrop implementation that inserts an empty, bordered div into
2772 * the document that follows the cursor during drag operations. At the time of
2773 * the click, the frame div is resized to the dimensions of the linked html
2774 * element, and moved to the exact location of the linked element.
2776 * References to the "frame" element refer to the single proxy element that
2777 * was created to be dragged in place of all DDProxy elements on the
2780 * @extends Ext.dd.DD
2782 * @param {String} id the id of the linked html element
2783 * @param {String} sGroup the group of related DragDrop objects
2784 * @param {object} config an object containing configurable attributes
2785 * Valid properties for DDProxy in addition to those in DragDrop:
2786 * resizeFrame, centerFrame, dragElId
2788 Ext.dd.DDProxy = function(id, sGroup, config) {
2790 this.init(id, sGroup, config);
2796 * The default drag frame div id
2797 * @property Ext.dd.DDProxy.dragElId
2801 Ext.dd.DDProxy.dragElId = "ygddfdiv";
2803 Ext.extend(Ext.dd.DDProxy, Ext.dd.DD, {
2806 * By default we resize the drag frame to be the same size as the element
2807 * we want to drag (this is to get the frame effect). We can turn it off
2808 * if we want a different behavior.
2809 * @property resizeFrame
2815 * By default the frame is positioned exactly where the drag element is, so
2816 * we use the cursor offset provided by Ext.dd.DD. Another option that works only if
2817 * you do not have constraints on the obj is to have the drag frame centered
2818 * around the cursor. Set centerFrame to true for this effect.
2819 * @property centerFrame
2825 * Creates the proxy element if it does not yet exist
2826 * @method createFrame
2828 createFrame: function() {
2830 var body = document.body;
2832 if (!body || !body.firstChild) {
2833 setTimeout( function() { self.createFrame(); }, 50 );
2837 var div = this.getDragEl();
2840 div = document.createElement("div");
2841 div.id = this.dragElId;
2844 s.position = "absolute";
2845 s.visibility = "hidden";
2847 s.border = "2px solid #aaa";
2850 // appendChild can blow up IE if invoked prior to the window load event
2851 // while rendering a table. It is possible there are other scenarios
2852 // that would cause this to happen as well.
2853 body.insertBefore(div, body.firstChild);
2858 * Initialization for the drag frame element. Must be called in the
2859 * constructor of all subclasses
2862 initFrame: function() {
2866 applyConfig: function() {
2867 Ext.dd.DDProxy.superclass.applyConfig.call(this);
2869 this.resizeFrame = (this.config.resizeFrame !== false);
2870 this.centerFrame = (this.config.centerFrame);
2871 this.setDragElId(this.config.dragElId || Ext.dd.DDProxy.dragElId);
2875 * Resizes the drag frame to the dimensions of the clicked object, positions
2876 * it over the object, and finally displays it
2878 * @param {int} iPageX X click position
2879 * @param {int} iPageY Y click position
2882 showFrame: function(iPageX, iPageY) {
2883 var el = this.getEl();
2884 var dragEl = this.getDragEl();
2885 var s = dragEl.style;
2887 this._resizeProxy();
2889 if (this.centerFrame) {
2890 this.setDelta( Math.round(parseInt(s.width, 10)/2),
2891 Math.round(parseInt(s.height, 10)/2) );
2894 this.setDragElPos(iPageX, iPageY);
2896 Ext.fly(dragEl).show();
2900 * The proxy is automatically resized to the dimensions of the linked
2901 * element when a drag is initiated, unless resizeFrame is set to false
2902 * @method _resizeProxy
2905 _resizeProxy: function() {
2906 if (this.resizeFrame) {
2907 var el = this.getEl();
2908 Ext.fly(this.getDragEl()).setSize(el.offsetWidth, el.offsetHeight);
2912 // overrides Ext.dd.DragDrop
2913 b4MouseDown: function(e) {
2914 var x = e.getPageX();
2915 var y = e.getPageY();
2916 this.autoOffset(x, y);
2917 this.setDragElPos(x, y);
2920 // overrides Ext.dd.DragDrop
2921 b4StartDrag: function(x, y) {
2922 // show the drag frame
2923 this.showFrame(x, y);
2926 // overrides Ext.dd.DragDrop
2927 b4EndDrag: function(e) {
2928 Ext.fly(this.getDragEl()).hide();
2931 // overrides Ext.dd.DragDrop
2932 // By default we try to move the element to the last location of the frame.
2933 // This is so that the default behavior mirrors that of Ext.dd.DD.
2934 endDrag: function(e) {
2936 var lel = this.getEl();
2937 var del = this.getDragEl();
2939 // Show the drag frame briefly so we can get its position
2940 del.style.visibility = "";
2943 // Hide the linked element before the move to get around a Safari
2945 lel.style.visibility = "hidden";
2946 Ext.dd.DDM.moveToEl(lel, del);
2947 del.style.visibility = "hidden";
2948 lel.style.visibility = "";
2953 beforeMove : function(){
2957 afterDrag : function(){
2961 toString: function() {
2962 return ("DDProxy " + this.id);
2967 * @class Ext.dd.DDTarget
2968 * A DragDrop implementation that does not move, but can be a drop
2969 * target. You would get the same result by simply omitting implementation
2970 * for the event callbacks, but this way we reduce the processing cost of the
2971 * event listener and the callbacks.
2972 * @extends Ext.dd.DragDrop
2974 * @param {String} id the id of the element that is a drop target
2975 * @param {String} sGroup the group of related DragDrop objects
2976 * @param {object} config an object containing configurable attributes
2977 * Valid properties for DDTarget in addition to those in
2981 Ext.dd.DDTarget = function(id, sGroup, config) {
2983 this.initTarget(id, sGroup, config);
2987 // Ext.dd.DDTarget.prototype = new Ext.dd.DragDrop();
2988 Ext.extend(Ext.dd.DDTarget, Ext.dd.DragDrop, {
2989 toString: function() {
2990 return ("DDTarget " + this.id);