4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js">/*
19 * This is a derivative of the similarly named class in the YUI Library.
20 * The original license:
21 * Copyright (c) 2006, Yahoo! Inc. All rights reserved.
22 * Code licensed under the BSD License:
23 * http://developer.yahoo.net/yui/license.txt
27 <span id='Ext-dd-DD-method-constructor'><span id='Ext-dd-DD'>/**
28 </span></span> * @class Ext.dd.DD
29 * A DragDrop implementation where the linked element follows the
30 * mouse cursor during a drag.
31 * @extends Ext.dd.DragDrop
33 * @param {String} id the id of the linked element
34 * @param {String} sGroup the group of related DragDrop items
35 * @param {object} config an object containing configurable attributes
36 * Valid properties for DD:
40 Ext.define('Ext.dd.DD', {
41 extend: 'Ext.dd.DragDrop',
42 requires: ['Ext.dd.DragDropManager'],
43 constructor: function(id, sGroup, config) {
45 this.init(id, sGroup, config);
49 <span id='Ext-dd-DD-property-scroll'> /**
50 </span> * When set to true, the utility automatically tries to scroll the browser
51 * window when a drag and drop element is dragged near the viewport boundary.
58 <span id='Ext-dd-DD-method-autoOffset'> /**
59 </span> * Sets the pointer offset to the distance between the linked element's top
60 * left corner and the location the element was clicked
62 * @param {int} iPageX the X coordinate of the click
63 * @param {int} iPageY the Y coordinate of the click
65 autoOffset: function(iPageX, iPageY) {
66 var x = iPageX - this.startPageX;
67 var y = iPageY - this.startPageY;
71 <span id='Ext-dd-DD-method-setDelta'> /**
72 </span> * Sets the pointer offset. You can call this directly to force the
73 * offset to be in a particular location (e.g., pass in 0,0 to set it
74 * to the center of the object)
76 * @param {int} iDeltaX the distance from the left
77 * @param {int} iDeltaY the distance from the top
79 setDelta: function(iDeltaX, iDeltaY) {
80 this.deltaX = iDeltaX;
81 this.deltaY = iDeltaY;
84 <span id='Ext-dd-DD-method-setDragElPos'> /**
85 </span> * Sets the drag element to the location of the mousedown or click event,
86 * maintaining the cursor location relative to the location on the element
87 * that was clicked. Override this if you want to place the element in a
88 * location other than where the cursor is.
89 * @method setDragElPos
90 * @param {int} iPageX the X coordinate of the mousedown or drag event
91 * @param {int} iPageY the Y coordinate of the mousedown or drag event
93 setDragElPos: function(iPageX, iPageY) {
94 // the first time we do this, we are going to check to make sure
95 // the element has css positioning
97 var el = this.getDragEl();
98 this.alignElWithMouse(el, iPageX, iPageY);
101 <span id='Ext-dd-DD-method-alignElWithMouse'> /**
102 </span> * Sets the element to the location of the mousedown or click event,
103 * maintaining the cursor location relative to the location on the element
104 * that was clicked. Override this if you want to place the element in a
105 * location other than where the cursor is.
106 * @method alignElWithMouse
107 * @param {HTMLElement} el the element to move
108 * @param {int} iPageX the X coordinate of the mousedown or drag event
109 * @param {int} iPageY the Y coordinate of the mousedown or drag event
111 alignElWithMouse: function(el, iPageX, iPageY) {
112 var oCoord = this.getTargetCoord(iPageX, iPageY),
113 fly = el.dom ? el : Ext.fly(el, '_dd'),
114 elSize = fly.getSize(),
115 EL = Ext.core.Element,
118 if (!this.deltaSetXY) {
119 vpSize = this.cachedViewportSize = { width: EL.getDocumentWidth(), height: EL.getDocumentHeight() };
121 Math.max(0, Math.min(oCoord.x, vpSize.width - elSize.width)),
122 Math.max(0, Math.min(oCoord.y, vpSize.height - elSize.height))
125 var newLeft = fly.getLeft(true);
126 var newTop = fly.getTop(true);
127 this.deltaSetXY = [newLeft - oCoord.x, newTop - oCoord.y];
129 vpSize = this.cachedViewportSize;
131 Math.max(0, Math.min(oCoord.x + this.deltaSetXY[0], vpSize.width - elSize.width)),
132 Math.max(0, Math.min(oCoord.y + this.deltaSetXY[1], vpSize.height - elSize.height))
136 this.cachePosition(oCoord.x, oCoord.y);
137 this.autoScroll(oCoord.x, oCoord.y, el.offsetHeight, el.offsetWidth);
141 <span id='Ext-dd-DD-method-cachePosition'> /**
142 </span> * Saves the most recent position so that we can reset the constraints and
143 * tick marks on-demand. We need to know this so that we can calculate the
144 * number of pixels the element is offset from its original position.
145 * @method cachePosition
146 * @param iPageX the current x position (optional, this just makes it so we
147 * don't have to look it up again)
148 * @param iPageY the current y position (optional, this just makes it so we
149 * don't have to look it up again)
151 cachePosition: function(iPageX, iPageY) {
153 this.lastPageX = iPageX;
154 this.lastPageY = iPageY;
156 var aCoord = Ext.core.Element.getXY(this.getEl());
157 this.lastPageX = aCoord[0];
158 this.lastPageY = aCoord[1];
162 <span id='Ext-dd-DD-method-autoScroll'> /**
163 </span> * Auto-scroll the window if the dragged object has been moved beyond the
164 * visible window boundary.
166 * @param {int} x the drag element's x position
167 * @param {int} y the drag element's y position
168 * @param {int} h the height of the drag element
169 * @param {int} w the width of the drag element
172 autoScroll: function(x, y, h, w) {
176 var clientH = Ext.core.Element.getViewHeight();
179 var clientW = Ext.core.Element.getViewWidth();
181 // The amt scrolled down
182 var st = this.DDMInstance.getScrollTop();
184 // The amt scrolled right
185 var sl = this.DDMInstance.getScrollLeft();
187 // Location of the bottom of the element
190 // Location of the right of the element
193 // The distance from the cursor to the bottom of the visible area,
194 // adjusted so that we don't scroll if the cursor is beyond the
195 // element drag constraints
196 var toBot = (clientH + st - y - this.deltaY);
198 // The distance from the cursor to the right of the visible area
199 var toRight = (clientW + sl - x - this.deltaX);
202 // How close to the edge the cursor must be before we scroll
203 // var thresh = (document.all) ? 100 : 40;
206 // How many pixels to scroll per autoscroll op. This helps to reduce
207 // clunky scrolling. IE is more sensitive about this ... it needs this
208 // value to be higher.
209 var scrAmt = (document.all) ? 80 : 30;
211 // Scroll down if we are near the bottom of the visible page and the
212 // obj extends below the crease
213 if ( bot > clientH && toBot < thresh ) {
214 window.scrollTo(sl, st + scrAmt);
217 // Scroll up if the window is scrolled down and the top of the object
218 // goes above the top border
219 if ( y < st && st > 0 && y - st < thresh ) {
220 window.scrollTo(sl, st - scrAmt);
223 // Scroll right if the obj is beyond the right border and the cursor is
225 if ( right > clientW && toRight < thresh ) {
226 window.scrollTo(sl + scrAmt, st);
229 // Scroll left if the window has been scrolled to the right and the obj
230 // extends past the left border
231 if ( x < sl && sl > 0 && x - sl < thresh ) {
232 window.scrollTo(sl - scrAmt, st);
237 <span id='Ext-dd-DD-method-getTargetCoord'> /**
238 </span> * Finds the location the element should be placed if we want to move
239 * it to where the mouse location less the click offset would place us.
240 * @method getTargetCoord
241 * @param {int} iPageX the X coordinate of the click
242 * @param {int} iPageY the Y coordinate of the click
243 * @return an object that contains the coordinates (Object.x and Object.y)
246 getTargetCoord: function(iPageX, iPageY) {
247 var x = iPageX - this.deltaX;
248 var y = iPageY - this.deltaY;
250 if (this.constrainX) {
251 if (x < this.minX) {
254 if (x > this.maxX) {
259 if (this.constrainY) {
260 if (y < this.minY) {
263 if (y > this.maxY) {
268 x = this.getTick(x, this.xTicks);
269 y = this.getTick(y, this.yTicks);
275 <span id='Ext-dd-DD-method-applyConfig'> /**
276 </span> * Sets up config options specific to this class. Overrides
277 * Ext.dd.DragDrop, but all versions of this method through the
278 * inheritance chain are called
280 applyConfig: function() {
282 this.scroll = (this.config.scroll !== false);
285 <span id='Ext-dd-DD-method-b4MouseDown'> /**
286 </span> * Event that fires prior to the onMouseDown event. Overrides
289 b4MouseDown: function(e) {
290 // this.resetConstraints();
291 this.autoOffset(e.getPageX(), e.getPageY());
294 <span id='Ext-dd-DD-method-b4Drag'> /**
295 </span> * Event that fires prior to the onDrag event. Overrides
298 b4Drag: function(e) {
299 this.setDragElPos(e.getPageX(), e.getPageY());
302 toString: function() {
303 return ("DD " + this.id);
306 //////////////////////////////////////////////////////////////////////////
307 // Debugging ygDragDrop events that can be overridden
308 //////////////////////////////////////////////////////////////////////////
310 startDrag: function(x, y) {
313 onDrag: function(e) {
316 onDragEnter: function(e, id) {
319 onDragOver: function(e, id) {
322 onDragOut: function(e, id) {
325 onDragDrop: function(e, id) {
328 endDrag: function(e) {