1 | /*
|
---|
2 | Copyright (c) 2006 Yahoo! Inc. All rights reserved.
|
---|
3 | version 0.9.0
|
---|
4 | */
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @class Defines the interface and base operation of items that that can be
|
---|
8 | * dragged or can be drop targets. It was designed to be extended, overriding
|
---|
9 | * the event handlers for startDrag, onDrag, onDragOver, onDragOut.
|
---|
10 | * Up to three html elements can be associated with a DragDrop instance:
|
---|
11 | * <ul>
|
---|
12 | * <li>linked element: the element that is passed into the constructor.
|
---|
13 | * This is the element which defines the boundaries for interaction with
|
---|
14 | * other DragDrop objects.</li>
|
---|
15 | * <li>handle element(s): The drag operation only occurs if the element that
|
---|
16 | * was clicked matches a handle element. By default this is the linked
|
---|
17 | * element, but there are times that you will want only a portion of the
|
---|
18 | * linked element to initiate the drag operation, and the setHandleElId()
|
---|
19 | * method provides a way to define this.</li>
|
---|
20 | * <li>drag element: this represents an the element that would be moved along
|
---|
21 | * with the cursor during a drag operation. By default, this is the linked
|
---|
22 | * element itself as in {@link YAHOO.util.DD}. setDragElId() lets you define
|
---|
23 | * a separate element that would be moved, as in {@link YAHOO.util.DDProxy}
|
---|
24 | * </li>
|
---|
25 | * </ul>
|
---|
26 | * This class should not be instantiated until the onload event to ensure that
|
---|
27 | * the associated elements are available.
|
---|
28 | * The following would define a DragDrop obj that would interact with any
|
---|
29 | * other * DragDrop obj in the "group1" group:
|
---|
30 | * <pre>
|
---|
31 | * dd = new YAHOO.util.DragDrop("div1", "group1");
|
---|
32 | * </pre>
|
---|
33 | * Since none of the event handlers have been implemented, nothing would
|
---|
34 | * actually happen if you were to run the code above. Normally you would
|
---|
35 | * override this class or one of the default implementations, but you can
|
---|
36 | * also override the methods you want on an instance of the class...
|
---|
37 | * <pre>
|
---|
38 | * dd.onDragDrop = function(e, id) {
|
---|
39 | * alert("dd was dropped on " + id);
|
---|
40 | * }
|
---|
41 | * </pre>
|
---|
42 | * @constructor
|
---|
43 | * @param {String} id of the element that is linked to this instance
|
---|
44 | * @param {String} sGroup the group of related DragDrop objects
|
---|
45 | */
|
---|
46 | YAHOO.util.DragDrop = function(id, sGroup) {
|
---|
47 | if (id) {
|
---|
48 | this.init(id, sGroup);
|
---|
49 | }
|
---|
50 | };
|
---|
51 |
|
---|
52 | YAHOO.util.DragDrop.prototype = {
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The id of the element associated with this object. This is what we
|
---|
56 | * refer to as the "linked element" because the size and position of
|
---|
57 | * this element is used to determine when the drag and drop objects have
|
---|
58 | * interacted.
|
---|
59 | *
|
---|
60 | * @type String
|
---|
61 | */
|
---|
62 | id: null,
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The id of the element that will be dragged. By default this is same
|
---|
66 | * as the linked element , but could be changed to another element. Ex:
|
---|
67 | * YAHOO.util.DDProxy
|
---|
68 | *
|
---|
69 | * @type String
|
---|
70 | * @private
|
---|
71 | */
|
---|
72 | dragElId: null,
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * the id of the element that initiates the drag operation. By default
|
---|
76 | * this is the linked element, but could be changed to be a child of this
|
---|
77 | * element. This lets us do things like only starting the drag when the
|
---|
78 | * header element within the linked html element is clicked.
|
---|
79 | *
|
---|
80 | * @type String
|
---|
81 | * @private
|
---|
82 | */
|
---|
83 | handleElId: null,
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * An array of HTML tags that will be ignored if clicked.
|
---|
87 | */
|
---|
88 | invalidHandleTypes: null,
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * The linked element's absolute X position at the time the drag was
|
---|
92 | * started
|
---|
93 | *
|
---|
94 | * @type int
|
---|
95 | * @private
|
---|
96 | */
|
---|
97 | startPageX: 0,
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * The linked element's absolute X position at the time the drag was
|
---|
101 | * started
|
---|
102 | *
|
---|
103 | * @type int
|
---|
104 | * @private
|
---|
105 | */
|
---|
106 | startPageY: 0,
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * The group defines a logical collection of DragDrop objects that are
|
---|
110 | * related. Instances only get events when interacting with other
|
---|
111 | * DragDrop object in the same group. This lets us define multiple
|
---|
112 | * groups using a single DragDrop subclass if we want.
|
---|
113 | *
|
---|
114 | */
|
---|
115 | groups: null,
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Individual drag/drop instances can be locked. This will prevent
|
---|
119 | * onmousedown start drag.
|
---|
120 | *
|
---|
121 | * @type boolean
|
---|
122 | * @private
|
---|
123 | */
|
---|
124 | locked: false,
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Lock this instance
|
---|
128 | */
|
---|
129 | lock: function() { this.locked = true; },
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Unlock this instace
|
---|
133 | */
|
---|
134 | unlock: function() { this.locked = false; },
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * By default, all insances can be a drop target. This can be disabled by
|
---|
138 | * setting isTarget to false.
|
---|
139 | *
|
---|
140 | * @type boolean
|
---|
141 | */
|
---|
142 | isTarget: true,
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * The padding configured for this drag and drop object for calculating
|
---|
146 | * the drop zone intersection with this object.
|
---|
147 | */
|
---|
148 | padding: null,
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @private
|
---|
152 | */
|
---|
153 | _domRef: null,
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Internal typeof flag
|
---|
157 | * @private
|
---|
158 | */
|
---|
159 | __ygDragDrop: true,
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Set to true when horizontal contraints are applied
|
---|
163 | *
|
---|
164 | * @type boolean
|
---|
165 | * @private
|
---|
166 | */
|
---|
167 | constrainX: false,
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Set to true when vertical contraints are applied
|
---|
171 | *
|
---|
172 | * @type boolean
|
---|
173 | * @private
|
---|
174 | */
|
---|
175 | constrainY: false,
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * The left constraint
|
---|
179 | *
|
---|
180 | * @type int
|
---|
181 | * @private
|
---|
182 | */
|
---|
183 | minX: 0,
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * The right constraint
|
---|
187 | *
|
---|
188 | * @type int
|
---|
189 | * @private
|
---|
190 | */
|
---|
191 | maxX: 0,
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * The up constraint
|
---|
195 | *
|
---|
196 | * @type int
|
---|
197 | * @private
|
---|
198 | */
|
---|
199 | minY: 0,
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * The down constraint
|
---|
203 | *
|
---|
204 | * @type int
|
---|
205 | * @private
|
---|
206 | */
|
---|
207 | maxY: 0,
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Maintain offsets when we resetconstraints. Used to maintain the
|
---|
211 | * slider thumb value, and this needs to be fixed.
|
---|
212 | * @type boolean
|
---|
213 | */
|
---|
214 | maintainOffset: false,
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Array of pixel locations the element will snap to if we specified a
|
---|
218 | * horizontal graduation/interval. This array is generated automatically
|
---|
219 | * when you define a tick interval.
|
---|
220 | * @type int[]
|
---|
221 | */
|
---|
222 | xTicks: null,
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Array of pixel locations the element will snap to if we specified a
|
---|
226 | * vertical graduation/interval. This array is generated automatically
|
---|
227 | * when you define a tick interval.
|
---|
228 | * @type int[]
|
---|
229 | */
|
---|
230 | yTicks: null,
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * By default the drag and drop instance will only respond to the primary
|
---|
234 | * button click (left button for a right-handed mouse). Set to true to
|
---|
235 | * allow drag and drop to start with any mouse click that is propogated
|
---|
236 | * by the browser
|
---|
237 | * @type boolean
|
---|
238 | */
|
---|
239 | primaryButtonOnly: true,
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Code that executes immediately before the startDrag event
|
---|
243 | * @private
|
---|
244 | */
|
---|
245 | b4StartDrag: function(x, y) { },
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Abstract method called after a drag/drop object is clicked
|
---|
249 | * and the drag or mousedown time thresholds have beeen met.
|
---|
250 | *
|
---|
251 | * @param {int} X click location
|
---|
252 | * @param {int} Y click location
|
---|
253 | */
|
---|
254 | startDrag: function(x, y) { /* override this */ },
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Code that executes immediately before the onDrag event
|
---|
258 | * @private
|
---|
259 | */
|
---|
260 | b4Drag: function(e) { },
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Abstract method called during the onMouseMove event while dragging an
|
---|
264 | * object.
|
---|
265 | *
|
---|
266 | * @param {Event} e
|
---|
267 | */
|
---|
268 | onDrag: function(e) { /* override this */ },
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Code that executes immediately before the onDragEnter event
|
---|
272 | * @private
|
---|
273 | */
|
---|
274 | // b4DragEnter: function(e) { },
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Abstract method called when this element fist begins hovering over
|
---|
278 | * another DragDrop obj
|
---|
279 | *
|
---|
280 | * @param {Event} e
|
---|
281 | * @param {String || YAHOO.util.DragDrop[]} id In POINT mode, the element
|
---|
282 | * id this is hovering over. In INTERSECT mode, an array of one or more
|
---|
283 | * dragdrop items being hovered over.
|
---|
284 | */
|
---|
285 | onDragEnter: function(e, id) { /* override this */ },
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Code that executes immediately before the onDragOver event
|
---|
289 | * @private
|
---|
290 | */
|
---|
291 | b4DragOver: function(e) { },
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Abstract method called when this element is hovering over another
|
---|
295 | * DragDrop obj
|
---|
296 | *
|
---|
297 | * @param {Event} e
|
---|
298 | * @param {String || YAHOO.util.DragDrop[]} id In POINT mode, the element
|
---|
299 | * id this is hovering over. In INTERSECT mode, an array of dd items
|
---|
300 | * being hovered over.
|
---|
301 | */
|
---|
302 | onDragOver: function(e, id) { /* override this */ },
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Code that executes immediately before the onDragOut event
|
---|
306 | * @private
|
---|
307 | */
|
---|
308 | b4DragOut: function(e) { },
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Abstract method called when we are no longer hovering over an element
|
---|
312 | *
|
---|
313 | * @param {Event} e
|
---|
314 | * @param {String || YAHOO.util.DragDrop[]} id In POINT mode, the element
|
---|
315 | * id this was hovering over. In INTERSECT mode, an array of dd items
|
---|
316 | * that the mouse is no longer over.
|
---|
317 | */
|
---|
318 | onDragOut: function(e, id) { /* override this */ },
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Code that executes immediately before the onDragDrop event
|
---|
322 | * @private
|
---|
323 | */
|
---|
324 | b4DragDrop: function(e) { },
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Abstract method called when this item is dropped on another DragDrop
|
---|
328 | * obj
|
---|
329 | *
|
---|
330 | * @param {Event} e
|
---|
331 | * @param {String || YAHOO.util.DragDrop[]} id In POINT mode, the element
|
---|
332 | * id this was dropped on. In INTERSECT mode, an array of dd items this
|
---|
333 | * was dropped on.
|
---|
334 | */
|
---|
335 | onDragDrop: function(e, id) { /* override this */ },
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Code that executes immediately before the endDrag event
|
---|
339 | * @private
|
---|
340 | */
|
---|
341 | b4EndDrag: function(e) { },
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Fired when we are done dragging the object
|
---|
345 | *
|
---|
346 | * @param {Event} e
|
---|
347 | */
|
---|
348 | endDrag: function(e) { /* override this */ },
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Code executed immediately before the onMouseDown event
|
---|
352 |
|
---|
353 | * @param {Event} e
|
---|
354 | * @private
|
---|
355 | */
|
---|
356 | b4MouseDown: function(e) { },
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Event handler that fires when a drag/drop obj gets a mousedown
|
---|
360 | * @param {Event} e
|
---|
361 | */
|
---|
362 | onMouseDown: function(e) { /* override this */ },
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Event handler that fires when a drag/drop obj gets a mouseup
|
---|
366 | * @param {Event} e
|
---|
367 | */
|
---|
368 | onMouseUp: function(e) { /* override this */ },
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Returns a reference to the linked element
|
---|
372 | *
|
---|
373 | * @return {Object} the html element
|
---|
374 | */
|
---|
375 | getEl: function() {
|
---|
376 | if (!this._domRef) {
|
---|
377 | this._domRef = this.DDM.getElement(this.id);
|
---|
378 | }
|
---|
379 |
|
---|
380 | return this._domRef;
|
---|
381 | },
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Returns a reference to the actual element to drag. By default this is
|
---|
385 | * the same as the html element, but it can be assigned to another
|
---|
386 | * element. An example of this can be found in YAHOO.util.DDProxy
|
---|
387 | *
|
---|
388 | * @return {Object} the html element
|
---|
389 | */
|
---|
390 | getDragEl: function() {
|
---|
391 | return this.DDM.getElement(this.dragElId);
|
---|
392 | },
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Sets up the DragDrop object. Must be called in the constructor of any
|
---|
396 | * YAHOO.util.DragDrop subclass
|
---|
397 | *
|
---|
398 | * @param id the id of the linked element
|
---|
399 | * @param {String} sGroup the group of related items
|
---|
400 | * element is supposed to be a target only, set to false.
|
---|
401 | */
|
---|
402 | init: function(id, sGroup) {
|
---|
403 | this.initTarget(id, sGroup);
|
---|
404 | YAHOO.util.Event.addListener(id, "mousedown",
|
---|
405 | this.handleMouseDown, this, true);
|
---|
406 | },
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * Initializes Targeting functionality only... the object does not
|
---|
410 | * get a mousedown handler.
|
---|
411 | *
|
---|
412 | * @param id the id of the linked element
|
---|
413 | * @param {String} sGroup the group of related items
|
---|
414 | * element is supposed to be a target only, set to false.
|
---|
415 | */
|
---|
416 | initTarget: function(id, sGroup) {
|
---|
417 |
|
---|
418 | // create a local reference to the drag and drop manager
|
---|
419 | this.DDM = YAHOO.util.DDM;
|
---|
420 |
|
---|
421 | // create a logger instance
|
---|
422 | this.logger = new ygLogger("DragDrop");
|
---|
423 |
|
---|
424 | // set the default padding
|
---|
425 | this.padding = [0, 0, 0, 0];
|
---|
426 |
|
---|
427 | // initialize the groups array
|
---|
428 | this.groups = {};
|
---|
429 |
|
---|
430 | // set the id
|
---|
431 | this.id = id;
|
---|
432 |
|
---|
433 | // the element is a drag handle by default
|
---|
434 | this.setDragElId(id);
|
---|
435 |
|
---|
436 | // by default, clicked anchors will not start drag operations
|
---|
437 | this.invalidHandleTypes = {a : "a"};
|
---|
438 |
|
---|
439 | // We don't want to register this as the handle with the manager
|
---|
440 | // so we just set the id rather than calling the setter
|
---|
441 | this.handleElId = id;
|
---|
442 |
|
---|
443 | // cache the position of the element if we can
|
---|
444 | if (document && document.body) {
|
---|
445 | this.setInitPosition();
|
---|
446 | }
|
---|
447 |
|
---|
448 | // add to an interaction group
|
---|
449 | this.addToGroup((sGroup) ? sGroup : "default");
|
---|
450 |
|
---|
451 | },
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Configures the padding for the target zone in px. Effectively expands
|
---|
455 | * (or reduces) the virtual object size for targeting calculations.
|
---|
456 | * Supports css-style shorthand; if only one parameter is passed, all sides
|
---|
457 | * will have that padding, and if only two are passed, the top and bottom
|
---|
458 | * will have the first param, the left and right the second.
|
---|
459 | * @param {int} iTop Top pad
|
---|
460 | * @param {int} iRight Right pad
|
---|
461 | * @param {int} iBot Bot pad
|
---|
462 | * @param {int} iLeft Left pad
|
---|
463 | */
|
---|
464 | setPadding: function(iTop, iRight, iBot, iLeft) {
|
---|
465 | // this.padding = [iLeft, iRight, iTop, iBot];
|
---|
466 | if (!iRight && 0 !== iRight) {
|
---|
467 | this.padding = [iTop, iTop, iTop, iTop];
|
---|
468 | } else if (!iBot && 0 !== iBot) {
|
---|
469 | this.padding = [iTop, iRight, iTop, iRight];
|
---|
470 | } else {
|
---|
471 | this.padding = [iTop, iRight, iBot, iLeft];
|
---|
472 | }
|
---|
473 | },
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Stores the initial placement of the dd element
|
---|
477 | */
|
---|
478 | setInitPosition: function(diffX, diffY) {
|
---|
479 | var el = this.getEl();
|
---|
480 |
|
---|
481 | if (!this.DDM.verifyEl(el)) {
|
---|
482 | this.logger.debug(this.id + " element is broken");
|
---|
483 | return;
|
---|
484 | }
|
---|
485 |
|
---|
486 | var dx = diffX || 0;
|
---|
487 | var dy = diffY || 0;
|
---|
488 |
|
---|
489 | var p = YAHOO.util.Dom.getXY( el );
|
---|
490 |
|
---|
491 | this.initPageX = p[0] - dx;
|
---|
492 | this.initPageY = p[1] - dy;
|
---|
493 |
|
---|
494 | this.lastPageX = p[0];
|
---|
495 | this.lastPageY = p[1];
|
---|
496 |
|
---|
497 | this.setStartPosition(p);
|
---|
498 | },
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Sets the start position of the element. This is set when the obj
|
---|
502 | * is initialized, the reset when a drag is started.
|
---|
503 | * @param pos current position (from previous lookup)
|
---|
504 | * @private
|
---|
505 | */
|
---|
506 | setStartPosition: function(pos) {
|
---|
507 |
|
---|
508 | var p = pos || YAHOO.util.Dom.getXY( this.getEl() );
|
---|
509 |
|
---|
510 | this.startPageX = p[0];
|
---|
511 | this.startPageY = p[1];
|
---|
512 | },
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Add this instance to a group of related drag/drop objects. All
|
---|
516 | * instances belong to at least one group, and can belong to as many
|
---|
517 | * groups as needed.
|
---|
518 | *
|
---|
519 | * @param sGroup {string} the name of the group
|
---|
520 | */
|
---|
521 | addToGroup: function(sGroup) {
|
---|
522 | this.groups[sGroup] = true;
|
---|
523 | this.DDM.regDragDrop(this, sGroup);
|
---|
524 | },
|
---|
525 |
|
---|
526 | /**
|
---|
527 | * Allows you to specify that an element other than the linked element
|
---|
528 | * will be moved with the cursor during a drag
|
---|
529 | *
|
---|
530 | * @param id the id of the element that will be used to initiate the drag
|
---|
531 | */
|
---|
532 | setDragElId: function(id) {
|
---|
533 | this.dragElId = id;
|
---|
534 | },
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Allows you to specify a child of the linked element that should be
|
---|
538 | * used to initiate the drag operation. An example of this would be if
|
---|
539 | * you have a content div with text and links. Clicking anywhere in the
|
---|
540 | * content area would normally start the drag operation. Use this method
|
---|
541 | * to specify that an element inside of the content div is the element
|
---|
542 | * that starts the drag operation.
|
---|
543 | *
|
---|
544 | * @param id the id of the element that will be used to initiate the drag
|
---|
545 | */
|
---|
546 | setHandleElId: function(id) {
|
---|
547 | this.handleElId = id;
|
---|
548 | this.DDM.regHandle(this.id, id);
|
---|
549 | },
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * Allows you to set an element outside of the linked element as a drag
|
---|
553 | * handle
|
---|
554 | */
|
---|
555 | setOuterHandleElId: function(id) {
|
---|
556 | this.logger.debug("Adding outer handle event: " + id);
|
---|
557 | YAHOO.util.Event.addListener(id, "mousedown",
|
---|
558 | this.handleMouseDown, this, true);
|
---|
559 | this.setHandleElId(id);
|
---|
560 | },
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Remove all drag and drop hooks for this element
|
---|
564 | */
|
---|
565 | unreg: function() {
|
---|
566 | this.logger.debug("DragDrop obj cleanup " + this.id);
|
---|
567 | YAHOO.util.Event.removeListener(this.id, "mousedown",
|
---|
568 | this.handleMouseDown);
|
---|
569 | this._domRef = null;
|
---|
570 | this.DDM._remove(this);
|
---|
571 | },
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Returns true if this instance is locked, or the drag drop mgr is locked
|
---|
575 | * (meaning that all drag/drop is disabled on the page.)
|
---|
576 | *
|
---|
577 | * @return {boolean} true if this obj or all drag/drop is locked, else
|
---|
578 | * false
|
---|
579 | */
|
---|
580 | isLocked: function() {
|
---|
581 | return (this.DDM.isLocked() || this.locked);
|
---|
582 | },
|
---|
583 |
|
---|
584 | /**
|
---|
585 | * Fired when this object is clicked
|
---|
586 | *
|
---|
587 | * @param {Event} e
|
---|
588 | * @param {YAHOO.util.DragDrop} oDD the clicked dd object (this dd obj)
|
---|
589 | * @private
|
---|
590 | */
|
---|
591 | handleMouseDown: function(e, oDD) {
|
---|
592 |
|
---|
593 | this.logger.debug("isLocked: " + this.isLocked());
|
---|
594 |
|
---|
595 | var EU = YAHOO.util.Event;
|
---|
596 |
|
---|
597 |
|
---|
598 | var button = e.which || e.button;
|
---|
599 | this.logger.debug("button: " + button);
|
---|
600 |
|
---|
601 | if (this.primaryButtonOnly && button > 1) {
|
---|
602 | this.logger.debug("Mousedown was not produced by the primary button");
|
---|
603 | return;
|
---|
604 | }
|
---|
605 |
|
---|
606 | if (this.isLocked()) {
|
---|
607 | this.logger.debug("Drag and drop is disabled, aborting");
|
---|
608 | return;
|
---|
609 | }
|
---|
610 |
|
---|
611 | this.logger.debug("mousedown " + this.id);
|
---|
612 |
|
---|
613 | this.DDM.refreshCache(this.groups);
|
---|
614 |
|
---|
615 | // Only process the event if we really clicked within the linked
|
---|
616 | // element. The reason we make this check is that in the case that
|
---|
617 | // another element was moved between the clicked element and the
|
---|
618 | // cursor in the time between the mousedown and mouseup events. When
|
---|
619 | // this happens, the element gets the next mousedown event
|
---|
620 | // regardless of where on the screen it happened.
|
---|
621 | var pt = new YAHOO.util.Point(EU.getPageX(e), EU.getPageY(e));
|
---|
622 | if ( this.DDM.isOverTarget(pt, this) ) {
|
---|
623 |
|
---|
624 | this.logger.debug("click is legit");
|
---|
625 |
|
---|
626 | // check to see if the handle was clicked
|
---|
627 | var srcEl = EU.getTarget(e);
|
---|
628 |
|
---|
629 | if (this.isValidHandleChild(srcEl) &&
|
---|
630 | (this.id == this.handleElId ||
|
---|
631 | this.DDM.handleWasClicked(srcEl, this.id)) ) {
|
---|
632 |
|
---|
633 | // set the initial element position
|
---|
634 | this.setStartPosition();
|
---|
635 |
|
---|
636 | this.logger.debug("firing onMouseDown events");
|
---|
637 |
|
---|
638 |
|
---|
639 | this.b4MouseDown(e);
|
---|
640 | this.onMouseDown(e);
|
---|
641 | this.DDM.handleMouseDown(e, this);
|
---|
642 |
|
---|
643 | this.DDM.stopEvent(e);
|
---|
644 | }
|
---|
645 | }
|
---|
646 | },
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Allows you to specify a tag name that should not start a drag operation
|
---|
650 | * when clicked. This is designed to facilitate embedding links within a
|
---|
651 | * drag handle that do something other than start the drag.
|
---|
652 | *
|
---|
653 | * @param {string} tagName the type of element to exclude
|
---|
654 | */
|
---|
655 | addInvalidHandleType: function(tagName) {
|
---|
656 | var type = tagName.toUpperCase();
|
---|
657 | this.invalidHandleTypes[type] = type;
|
---|
658 | },
|
---|
659 |
|
---|
660 | /**
|
---|
661 | * Unsets an excluded tag name set by addInvalidHandleType
|
---|
662 | *
|
---|
663 | * @param {string} tagName the type of element to unexclude
|
---|
664 | */
|
---|
665 | removeInvalidHandleType: function(tagName) {
|
---|
666 | var type = tagName.toUpperCase();
|
---|
667 | this.invalidHandleTypes[type] = null;
|
---|
668 | },
|
---|
669 |
|
---|
670 | /**
|
---|
671 | * Checks the tag exclusion list to see if this click should be ignored
|
---|
672 | *
|
---|
673 | * @param {ygNode} node
|
---|
674 | * @return {boolean} true if this is a valid tag type, false if not
|
---|
675 | */
|
---|
676 | isValidHandleChild: function(node) {
|
---|
677 | var type = node.nodeName;
|
---|
678 |
|
---|
679 | if (type == "#text") {
|
---|
680 | // this.logger.debug("text node, getting parent node type");
|
---|
681 | type = node.parentNode.nodeName;
|
---|
682 | }
|
---|
683 |
|
---|
684 | return (!this.invalidHandleTypes[type]);
|
---|
685 | },
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Create the array of horizontal tick marks if an interval was specified
|
---|
689 | * in setXConstraint().
|
---|
690 | *
|
---|
691 | * @private
|
---|
692 | */
|
---|
693 | setXTicks: function(iStartX, iTickSize) {
|
---|
694 | this.xTicks = [];
|
---|
695 | this.xTickSize = iTickSize;
|
---|
696 |
|
---|
697 | var tickMap = {};
|
---|
698 |
|
---|
699 | for (var i = this.initPageX; i >= this.minX; i = i - iTickSize) {
|
---|
700 | if (!tickMap[i]) {
|
---|
701 | this.xTicks[this.xTicks.length] = i;
|
---|
702 | tickMap[i] = true;
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | for (i = this.initPageX; i <= this.maxX; i = i + iTickSize) {
|
---|
707 | if (!tickMap[i]) {
|
---|
708 | this.xTicks[this.xTicks.length] = i;
|
---|
709 | tickMap[i] = true;
|
---|
710 | }
|
---|
711 | }
|
---|
712 |
|
---|
713 | this.xTicks.sort(this.DDM.numericSort) ;
|
---|
714 | this.logger.debug("xTicks: " + this.xTicks.join());
|
---|
715 | },
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Create the array of vertical tick marks if an interval was specified in
|
---|
719 | * setYConstraint().
|
---|
720 | *
|
---|
721 | * @private
|
---|
722 | */
|
---|
723 | setYTicks: function(iStartY, iTickSize) {
|
---|
724 | this.yTicks = [];
|
---|
725 | this.yTickSize = iTickSize;
|
---|
726 |
|
---|
727 | var tickMap = {};
|
---|
728 |
|
---|
729 | for (var i = this.initPageY; i >= this.minY; i = i - iTickSize) {
|
---|
730 | if (!tickMap[i]) {
|
---|
731 | this.yTicks[this.yTicks.length] = i;
|
---|
732 | tickMap[i] = true;
|
---|
733 | }
|
---|
734 | }
|
---|
735 |
|
---|
736 | for (i = this.initPageY; i <= this.maxY; i = i + iTickSize) {
|
---|
737 | if (!tickMap[i]) {
|
---|
738 | this.yTicks[this.yTicks.length] = i;
|
---|
739 | tickMap[i] = true;
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | this.yTicks.sort(this.DDM.numericSort) ;
|
---|
744 | this.logger.debug("yTicks: " + this.yTicks.join());
|
---|
745 | },
|
---|
746 |
|
---|
747 | /**
|
---|
748 | * By default, the element can be dragged any place on the screen. Use
|
---|
749 | * this method to limit the horizontal travel of the element. Pass in
|
---|
750 | * 0,0 for the parameters if you want to lock the drag to the y axis.
|
---|
751 | *
|
---|
752 | * @param {int} iLeft the number of pixels the element can move to the left
|
---|
753 | * @param {int} iRight the number of pixels the element can move to the
|
---|
754 | * right
|
---|
755 | * @param {int} iTickSize optional parameter for specifying that the
|
---|
756 | * element
|
---|
757 | * should move iTickSize pixels at a time.
|
---|
758 | */
|
---|
759 | setXConstraint: function(iLeft, iRight, iTickSize) {
|
---|
760 | this.leftConstraint = iLeft;
|
---|
761 | this.rightConstraint = iRight;
|
---|
762 |
|
---|
763 | this.minX = this.initPageX - iLeft;
|
---|
764 | this.maxX = this.initPageX + iRight;
|
---|
765 | if (iTickSize) { this.setXTicks(this.initPageX, iTickSize); }
|
---|
766 |
|
---|
767 | this.constrainX = true;
|
---|
768 | this.logger.debug("initPageX:" + this.initPageX + " minX:" + this.minX +
|
---|
769 | " maxX:" + this.maxX);
|
---|
770 | },
|
---|
771 |
|
---|
772 | /**
|
---|
773 | * By default, the element can be dragged any place on the screen. Set
|
---|
774 | * this to limit the vertical travel of the element. Pass in 0,0 for the
|
---|
775 | * parameters if you want to lock the drag to the x axis.
|
---|
776 | *
|
---|
777 | * @param {int} iUp the number of pixels the element can move up
|
---|
778 | * @param {int} iDown the number of pixels the element can move down
|
---|
779 | * @param {int} iTickSize optional parameter for specifying that the
|
---|
780 | * element should move iTickSize pixels at a time.
|
---|
781 | */
|
---|
782 | setYConstraint: function(iUp, iDown, iTickSize) {
|
---|
783 | this.topConstraint = iUp;
|
---|
784 | this.bottomConstraint = iDown;
|
---|
785 |
|
---|
786 | this.minY = this.initPageY - iUp;
|
---|
787 | this.maxY = this.initPageY + iDown;
|
---|
788 | if (iTickSize) { this.setYTicks(this.initPageY, iTickSize); }
|
---|
789 |
|
---|
790 | this.constrainY = true;
|
---|
791 |
|
---|
792 | this.logger.debug("initPageY:" + this.initPageY + " minY:" + this.minY +
|
---|
793 | " maxY:" + this.maxY);
|
---|
794 | },
|
---|
795 |
|
---|
796 | /**
|
---|
797 | * resetConstraints must be called if you manually reposition a dd element.
|
---|
798 | * @param {boolean} maintainOffset
|
---|
799 | */
|
---|
800 | resetConstraints: function() {
|
---|
801 |
|
---|
802 | // this.logger.debug("init pagexy: " + this.initPageX + ", " +
|
---|
803 | // this.initPageY);
|
---|
804 | // this.logger.debug("last pagexy: " + this.lastPageX + ", " +
|
---|
805 | // this.lastPageY);
|
---|
806 |
|
---|
807 | // figure out how much this thing has moved
|
---|
808 | var dx = (this.maintainOffset) ? this.lastPageX - this.initPageX : 0;
|
---|
809 | var dy = (this.maintainOffset) ? this.lastPageY - this.initPageY : 0;
|
---|
810 |
|
---|
811 | // this.logger.debug("diff: " + dx + ", " + dy);
|
---|
812 |
|
---|
813 | // reset the initial location
|
---|
814 | this.setInitPosition(dx, dy);
|
---|
815 |
|
---|
816 | if (this.constrainX) {
|
---|
817 | this.setXConstraint( this.leftConstraint,
|
---|
818 | this.rightConstraint,
|
---|
819 | this.xTickSize );
|
---|
820 | }
|
---|
821 |
|
---|
822 | if (this.constrainY) {
|
---|
823 | this.setYConstraint( this.topConstraint,
|
---|
824 | this.bottomConstraint,
|
---|
825 | this.yTickSize );
|
---|
826 | }
|
---|
827 | },
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Normally the drag element is moved pixel by pixel, but we can specify
|
---|
831 | * that it move a number of pixels at a time. This method resolves the
|
---|
832 | * location when we have it set up like this.
|
---|
833 | *
|
---|
834 | * @param {int} val where we want to place the object
|
---|
835 | * @param {int[]} tickArray sorted array of valid points
|
---|
836 | * @return {int} the closest tick
|
---|
837 | * @private
|
---|
838 | */
|
---|
839 | getTick: function(val, tickArray) {
|
---|
840 |
|
---|
841 | if (!tickArray) {
|
---|
842 | // If tick interval is not defined, it is effectively 1 pixel,
|
---|
843 | // so we return the value passed to us.
|
---|
844 | return val;
|
---|
845 | } else if (tickArray[0] >= val) {
|
---|
846 | // The value is lower than the first tick, so we return the first
|
---|
847 | // tick.
|
---|
848 | return tickArray[0];
|
---|
849 | } else {
|
---|
850 | for (var i = 0; i < tickArray.length; ++i) {
|
---|
851 | var next = i + 1;
|
---|
852 | if (tickArray[next] && tickArray[next] >= val) {
|
---|
853 | var diff1 = val - tickArray[i];
|
---|
854 | var diff2 = tickArray[next] - val;
|
---|
855 | return (diff2 > diff1) ? tickArray[i] : tickArray[next];
|
---|
856 | }
|
---|
857 | }
|
---|
858 |
|
---|
859 | // The value is larger than the last tick, so we return the last
|
---|
860 | // tick.
|
---|
861 | return tickArray[tickArray.length - 1];
|
---|
862 | }
|
---|
863 | },
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * toString method
|
---|
867 | * @return {string} string representation of the dd obj
|
---|
868 | */
|
---|
869 | toString: function(val, tickArray) {
|
---|
870 | return ("YAHOO.util.DragDrop {" + this.id + "}");
|
---|
871 | }
|
---|
872 |
|
---|
873 | };
|
---|
874 |
|
---|
875 | /* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
|
---|
876 |
|
---|
877 | // Only load the library once. Rewriting the manager class would orphan
|
---|
878 | // existing drag and drop instances.
|
---|
879 | if (!YAHOO.util.DragDropMgr) {
|
---|
880 |
|
---|
881 | /**
|
---|
882 | * @class Handles the element interaction for all DragDrop items in the
|
---|
883 | * window. Generally, you will not call this class directly, but it does
|
---|
884 | * have helper methods that could be useful in your DragDrop
|
---|
885 | * implementations. This class should not be instantiated; all methods
|
---|
886 | * are are static.
|
---|
887 | *
|
---|
888 | * @constructor
|
---|
889 | */
|
---|
890 | YAHOO.util.DragDropMgr = new function() {
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * utility package shorthand
|
---|
894 | * @private
|
---|
895 | */
|
---|
896 | var UTIL = YAHOO.util;
|
---|
897 |
|
---|
898 | /**
|
---|
899 | * Two dimensional Array of registered DragDrop objects. The first
|
---|
900 | * dimension is the DragDrop item group, the second the DragDrop
|
---|
901 | * object.
|
---|
902 | *
|
---|
903 | * @private
|
---|
904 | */
|
---|
905 | this.ids = {};
|
---|
906 |
|
---|
907 | /**
|
---|
908 | * Array of element ids defined as drag handles. Used to determine
|
---|
909 | * if the element that generated the mousedown event is actually the
|
---|
910 | * handle and not the html element itself.
|
---|
911 | *
|
---|
912 | * @private
|
---|
913 | */
|
---|
914 | this.handleIds = {};
|
---|
915 |
|
---|
916 | /**
|
---|
917 | * the DragDrop object that is currently being dragged
|
---|
918 | *
|
---|
919 | * @type DragDrop
|
---|
920 | * @private
|
---|
921 | **/
|
---|
922 | this.dragCurrent = null;
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * the DragDrop object(s) that are being hovered over
|
---|
926 | *
|
---|
927 | * @type Array
|
---|
928 | * @private
|
---|
929 | */
|
---|
930 | this.dragOvers = {};
|
---|
931 |
|
---|
932 | /**
|
---|
933 | * @private
|
---|
934 | */
|
---|
935 | this.logger = null;
|
---|
936 |
|
---|
937 | /**
|
---|
938 | * the X distance between the cursor and the object being dragged
|
---|
939 | *
|
---|
940 | * @type int
|
---|
941 | * @private
|
---|
942 | */
|
---|
943 | this.deltaX = 0;
|
---|
944 |
|
---|
945 | /**
|
---|
946 | * the Y distance between the cursor and the object being dragged
|
---|
947 | *
|
---|
948 | * @type int
|
---|
949 | * @private
|
---|
950 | */
|
---|
951 | this.deltaY = 0;
|
---|
952 |
|
---|
953 | /**
|
---|
954 | * Flag to determine if we should prevent the default behavior of the
|
---|
955 | * events we define. By default this is true, but this can be set to
|
---|
956 | * false if you need the default behavior (not recommended)
|
---|
957 | *
|
---|
958 | * @type boolean
|
---|
959 | */
|
---|
960 | this.preventDefault = true;
|
---|
961 |
|
---|
962 | /**
|
---|
963 | * Flag to determine if we should stop the propagation of the events
|
---|
964 | * we generate. This is true by default but you may want to set it to
|
---|
965 | * false if the html element contains other features that require the
|
---|
966 | * mouse click.
|
---|
967 | *
|
---|
968 | * @type boolean
|
---|
969 | */
|
---|
970 | this.stopPropagation = true;
|
---|
971 |
|
---|
972 | /**
|
---|
973 | * @private
|
---|
974 | */
|
---|
975 | this.initalized = false;
|
---|
976 |
|
---|
977 | /**
|
---|
978 | * All drag and drop can be disabled.
|
---|
979 | *
|
---|
980 | * @private
|
---|
981 | */
|
---|
982 | this.locked = false;
|
---|
983 |
|
---|
984 | /**
|
---|
985 | * Called the first time an element is registered.
|
---|
986 | *
|
---|
987 | * @private
|
---|
988 | */
|
---|
989 | this.init = function() {
|
---|
990 | this.logger = new ygLogger("DragDropMgr");
|
---|
991 | };
|
---|
992 |
|
---|
993 | /**
|
---|
994 | * In point mode, drag and drop interaction is defined by the
|
---|
995 | * location of the cursor during the drag/drop
|
---|
996 | * @type int
|
---|
997 | */
|
---|
998 | this.POINT = 0;
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * In intersect mode, drag and drop interactio nis defined by the
|
---|
1002 | * overlap of two or more drag and drop objects.
|
---|
1003 | * @type int
|
---|
1004 | */
|
---|
1005 | this.INTERSECT = 1;
|
---|
1006 |
|
---|
1007 | /**
|
---|
1008 | * The current drag and drop mode. Default it point mode
|
---|
1009 | * @type int
|
---|
1010 | */
|
---|
1011 | this.mode = this.POINT;
|
---|
1012 |
|
---|
1013 | /**
|
---|
1014 | * Runs method on all drag and drop objects
|
---|
1015 | * @private
|
---|
1016 | */
|
---|
1017 | this._execOnAll = function(sMethod, args) {
|
---|
1018 | for (var i in this.ids) {
|
---|
1019 | for (var j in this.ids[i]) {
|
---|
1020 | var oDD = this.ids[i][j];
|
---|
1021 | if (! this.isTypeOfDD(oDD)) {
|
---|
1022 | continue;
|
---|
1023 | }
|
---|
1024 | oDD[sMethod].apply(oDD, args);
|
---|
1025 | }
|
---|
1026 | }
|
---|
1027 | };
|
---|
1028 |
|
---|
1029 | /**
|
---|
1030 | * Drag and drop initialization. Sets up the global event handlers
|
---|
1031 | * @private
|
---|
1032 | */
|
---|
1033 | this._onLoad = function() {
|
---|
1034 |
|
---|
1035 | this._execOnAll("setInitPosition", []);
|
---|
1036 |
|
---|
1037 | this.logger = new ygLogger("DragDropMgr");
|
---|
1038 | this.logger.debug("DDM onload");
|
---|
1039 |
|
---|
1040 | var EU = UTIL.Event;
|
---|
1041 |
|
---|
1042 | EU.addListener(document, "mouseup", this.handleMouseUp, this, true);
|
---|
1043 | EU.addListener(document, "mousemove", this.handleMouseMove, this, true);
|
---|
1044 | EU.addListener(window, "unload", this._onUnload, this, true);
|
---|
1045 | EU.addListener(window, "resize", this._onResize, this, true);
|
---|
1046 | // EU.addListener(window, "mouseout", this._test);
|
---|
1047 |
|
---|
1048 | this.initalized = true;
|
---|
1049 |
|
---|
1050 | };
|
---|
1051 |
|
---|
1052 | /**
|
---|
1053 | * Reset constraints on all drag and drop objs
|
---|
1054 | * @private
|
---|
1055 | */
|
---|
1056 | this._onResize = function(e) {
|
---|
1057 | this.logger.debug("window resize");
|
---|
1058 | this._execOnAll("resetConstraints", []);
|
---|
1059 | };
|
---|
1060 |
|
---|
1061 | /**
|
---|
1062 | * Lock all drag and drop functionality
|
---|
1063 | */
|
---|
1064 | this.lock = function() { this.locked = true; };
|
---|
1065 |
|
---|
1066 | /**
|
---|
1067 | * Unlock all drag and drop functionality
|
---|
1068 | */
|
---|
1069 | this.unlock = function() { this.locked = false; };
|
---|
1070 |
|
---|
1071 | /**
|
---|
1072 | * Is drag and drop locked?
|
---|
1073 | *
|
---|
1074 | * @return {boolean} True if drag and drop is locked, false otherwise.
|
---|
1075 | */
|
---|
1076 | this.isLocked = function() { return this.locked; };
|
---|
1077 |
|
---|
1078 | /**
|
---|
1079 | * Location cache that is set for all drag drop objects when a drag is
|
---|
1080 | * initiated, cleared when the drag is finished.
|
---|
1081 | *
|
---|
1082 | * @private
|
---|
1083 | */
|
---|
1084 | this.locationCache = {};
|
---|
1085 |
|
---|
1086 | /**
|
---|
1087 | * Set useCache to false if you want to force object the lookup of each
|
---|
1088 | * drag and drop linked element constantly during a drag.
|
---|
1089 | * @type boolean
|
---|
1090 | */
|
---|
1091 | this.useCache = true;
|
---|
1092 |
|
---|
1093 | /**
|
---|
1094 | * The number of pixels that the mouse needs to move after the
|
---|
1095 | * mousedown before the drag is initiated. Default=3;
|
---|
1096 | * @type int
|
---|
1097 | */
|
---|
1098 | this.clickPixelThresh = 3;
|
---|
1099 |
|
---|
1100 | /**
|
---|
1101 | * The number of milliseconds after the mousedown event to initiate the
|
---|
1102 | * drag if we don't get a mouseup event. Default=1000
|
---|
1103 | * @type int
|
---|
1104 | */
|
---|
1105 | this.clickTimeThresh = 1000;
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Flag that indicates that either the drag pixel threshold or the
|
---|
1109 | * mousdown time threshold has been met
|
---|
1110 | * @type boolean
|
---|
1111 | * @private
|
---|
1112 | */
|
---|
1113 | this.dragThreshMet = false;
|
---|
1114 |
|
---|
1115 | /**
|
---|
1116 | * Timeout used for the click time threshold
|
---|
1117 | * @type Object
|
---|
1118 | * @private
|
---|
1119 | */
|
---|
1120 | this.clickTimeout = null;
|
---|
1121 |
|
---|
1122 | /**
|
---|
1123 | * The X position of the mousedown event stored for later use when a
|
---|
1124 | * drag threshold is met.
|
---|
1125 | * @type int
|
---|
1126 | * @private
|
---|
1127 | */
|
---|
1128 | this.startX = 0;
|
---|
1129 |
|
---|
1130 | /**
|
---|
1131 | * The Y position of the mousedown event stored for later use when a
|
---|
1132 | * drag threshold is met.
|
---|
1133 | * @type int
|
---|
1134 | * @private
|
---|
1135 | */
|
---|
1136 | this.startY = 0;
|
---|
1137 |
|
---|
1138 | /**
|
---|
1139 | * Each DragDrop instance must be registered with the DragDropMgr.
|
---|
1140 | * This is executed in ygDragDrop.init()
|
---|
1141 | *
|
---|
1142 | * @param {DragDrop} oDD the DragDrop object to register
|
---|
1143 | * @param {String} sGroup the name of the group this element belongs to
|
---|
1144 | */
|
---|
1145 | this.regDragDrop = function(oDD, sGroup) {
|
---|
1146 | if (!this.initialized) { this.init(); }
|
---|
1147 |
|
---|
1148 | if (!this.ids[sGroup]) {
|
---|
1149 | this.ids[sGroup] = {};
|
---|
1150 | }
|
---|
1151 | this.ids[sGroup][oDD.id] = oDD;
|
---|
1152 | };
|
---|
1153 |
|
---|
1154 | /**
|
---|
1155 | * Unregisters a drag and drop item. This is executed in
|
---|
1156 | * ygDragDrop.unreg, use that method instead of calling this directly.
|
---|
1157 | * @private
|
---|
1158 | */
|
---|
1159 | this._remove = function(oDD) {
|
---|
1160 | for (var g in oDD.groups) {
|
---|
1161 | if (g && this.ids[g][oDD.id]) {
|
---|
1162 | delete this.ids[g][oDD.id];
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 | delete this.handleIds[oDD.id];
|
---|
1166 | };
|
---|
1167 |
|
---|
1168 | /**
|
---|
1169 | * Each DragDrop handle element must be registered. This is done
|
---|
1170 | * automatically when executing ygDragDrop.setHandleElId()
|
---|
1171 | *
|
---|
1172 | * @param {String} sDDId the DragDrop id this element is a handle for
|
---|
1173 | * @param {String} sHandleId the id of the element that is the drag
|
---|
1174 | * handle
|
---|
1175 | */
|
---|
1176 | this.regHandle = function(sDDId, sHandleId) {
|
---|
1177 | if (!this.handleIds[sDDId]) {
|
---|
1178 | this.handleIds[sDDId] = {};
|
---|
1179 | }
|
---|
1180 | this.handleIds[sDDId][sHandleId] = sHandleId;
|
---|
1181 | };
|
---|
1182 |
|
---|
1183 | /**
|
---|
1184 | * Utility function to determine if a given element has been
|
---|
1185 | * registered as a drag drop item.
|
---|
1186 | *
|
---|
1187 | * @param {String} id the element id to check
|
---|
1188 | * @return {boolean} true if this element is a DragDrop item,
|
---|
1189 | * false otherwise
|
---|
1190 | */
|
---|
1191 | this.isDragDrop = function(id) {
|
---|
1192 | return ( this.getDDById(id) ) ? true : false;
|
---|
1193 | };
|
---|
1194 |
|
---|
1195 | /**
|
---|
1196 | * Returns the drag and drop instances that are in all groups the
|
---|
1197 | * passed in instance belongs to.
|
---|
1198 | *
|
---|
1199 | * @param {ygDragDrop} p_oDD the obj to get related data for
|
---|
1200 | * @param {boolean} bTargetsOnly if true, only return targetable objs
|
---|
1201 | * @return {ygDragDrop[]} the related instances
|
---|
1202 | */
|
---|
1203 | this.getRelated = function(p_oDD, bTargetsOnly) {
|
---|
1204 | var oDDs = [];
|
---|
1205 | for (var i in p_oDD.groups) {
|
---|
1206 | for (j in this.ids[i]) {
|
---|
1207 | var dd = this.ids[i][j];
|
---|
1208 | if (! this.isTypeOfDD(dd)) {
|
---|
1209 | continue;
|
---|
1210 | }
|
---|
1211 | if (!bTargetsOnly || dd.isTarget) {
|
---|
1212 | oDDs[oDDs.length] = dd;
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | return oDDs;
|
---|
1218 | };
|
---|
1219 |
|
---|
1220 | /**
|
---|
1221 | * Returns true if the specified dd target is a legal target for
|
---|
1222 | * the specifice drag obj
|
---|
1223 | *
|
---|
1224 | * @param {ygDragDrop} the drag obj
|
---|
1225 | * @param {ygDragDrop) the target
|
---|
1226 | * @return {boolean} true if the target is a legal target for the
|
---|
1227 | * dd obj
|
---|
1228 | */
|
---|
1229 | this.isLegalTarget = function (oDD, oTargetDD) {
|
---|
1230 | var targets = this.getRelated(oDD);
|
---|
1231 | for (var i =0;i<targets.length;++i) {
|
---|
1232 | if (targets[i].id == oTargetDD.id) {
|
---|
1233 | return true;
|
---|
1234 | }
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | return false;
|
---|
1238 | };
|
---|
1239 |
|
---|
1240 | /**
|
---|
1241 | * My goal is to be able to transparently determine if an object is
|
---|
1242 | * typeof ygDragDrop, and the exact subclass of ygDragDrop. typeof
|
---|
1243 | * returns "object", oDD.constructor.toString() always returns
|
---|
1244 | * "ygDragDrop" and not the name of the subclass. So for now it just
|
---|
1245 | * evaluates a well-known variable in ygDragDrop.
|
---|
1246 | *
|
---|
1247 | * @param {Object} the object to evaluate
|
---|
1248 | * @return {boolean} true if typeof oDD = ygDragDrop
|
---|
1249 | */
|
---|
1250 | this.isTypeOfDD = function (oDD) {
|
---|
1251 | return (oDD && oDD.__ygDragDrop);
|
---|
1252 | };
|
---|
1253 |
|
---|
1254 | /**
|
---|
1255 | * Utility function to determine if a given element has been
|
---|
1256 | * registered as a drag drop handle for the given Drag Drop object.
|
---|
1257 | *
|
---|
1258 | * @param {String} id the element id to check
|
---|
1259 | * @return {boolean} true if this element is a DragDrop handle, false
|
---|
1260 | * otherwise
|
---|
1261 | */
|
---|
1262 | this.isHandle = function(sDDId, sHandleId) {
|
---|
1263 | return ( this.handleIds[sDDId] &&
|
---|
1264 | this.handleIds[sDDId][sHandleId] );
|
---|
1265 | };
|
---|
1266 |
|
---|
1267 | /**
|
---|
1268 | * Returns the DragDrop instance for a given id
|
---|
1269 | *
|
---|
1270 | * @param {String} id the id of the DragDrop object
|
---|
1271 | * @return {DragDrop} the drag drop object, null if it is not found
|
---|
1272 | */
|
---|
1273 | this.getDDById = function(id) {
|
---|
1274 | for (var i in this.ids) {
|
---|
1275 | if (this.ids[i][id]) {
|
---|
1276 | return this.ids[i][id];
|
---|
1277 | }
|
---|
1278 | }
|
---|
1279 | return null;
|
---|
1280 | };
|
---|
1281 |
|
---|
1282 | /**
|
---|
1283 | * Fired after a registered DragDrop object gets the mousedown event.
|
---|
1284 | * Sets up the events required to track the object being dragged
|
---|
1285 | *
|
---|
1286 | * @param {Event} e the event
|
---|
1287 | * @param oDD the DragDrop object being dragged
|
---|
1288 | * @private
|
---|
1289 | */
|
---|
1290 | this.handleMouseDown = function(e, oDD) {
|
---|
1291 | this.logger.debug("mousedown - adding event handlers");
|
---|
1292 | this.dragCurrent = oDD;
|
---|
1293 |
|
---|
1294 | var el = oDD.getEl();
|
---|
1295 |
|
---|
1296 | // track start position
|
---|
1297 | this.startX = UTIL.Event.getPageX(e);
|
---|
1298 | this.startY = UTIL.Event.getPageY(e);
|
---|
1299 |
|
---|
1300 | this.deltaX = this.startX - el.offsetLeft;
|
---|
1301 | this.deltaY = this.startY - el.offsetTop;
|
---|
1302 |
|
---|
1303 | this.dragThreshMet = false;
|
---|
1304 |
|
---|
1305 | this.clickTimeout = setTimeout(
|
---|
1306 | "var DDM=YAHOO.util.DDM;DDM.startDrag(DDM.startX, DDM.startY)",
|
---|
1307 | this.clickTimeThresh );
|
---|
1308 | };
|
---|
1309 |
|
---|
1310 | /**
|
---|
1311 | * Fired when either the drag pixel threshol or the mousedown hold
|
---|
1312 | * time threshold has been met.
|
---|
1313 | *
|
---|
1314 | * @param x {int} the X position of the original mousedown
|
---|
1315 | * @param y {int} the Y position of the original mousedown
|
---|
1316 | */
|
---|
1317 | this.startDrag = function(x, y) {
|
---|
1318 | this.logger.debug("firing drag start events");
|
---|
1319 | clearTimeout(this.clickTimeout);
|
---|
1320 | if (this.dragCurrent) {
|
---|
1321 | this.dragCurrent.b4StartDrag(x, y);
|
---|
1322 | this.dragCurrent.startDrag(x, y);
|
---|
1323 | }
|
---|
1324 | this.dragThreshMet = true;
|
---|
1325 | };
|
---|
1326 |
|
---|
1327 | /**
|
---|
1328 | * Internal function to handle the mouseup event. Will be invoked
|
---|
1329 | * from the context of the document.
|
---|
1330 | *
|
---|
1331 | * @param {Event} e the event
|
---|
1332 | * @private
|
---|
1333 | */
|
---|
1334 | this.handleMouseUp = function(e) {
|
---|
1335 |
|
---|
1336 | if (! this.dragCurrent) {
|
---|
1337 | return;
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | clearTimeout(this.clickTimeout);
|
---|
1341 |
|
---|
1342 | if (this.dragThreshMet) {
|
---|
1343 | this.logger.debug("mouseup detected - completing drag");
|
---|
1344 | this.fireEvents(e, true);
|
---|
1345 | } else {
|
---|
1346 | this.logger.debug("drag threshold not met");
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | this.stopDrag(e);
|
---|
1350 |
|
---|
1351 | this.stopEvent(e);
|
---|
1352 | };
|
---|
1353 |
|
---|
1354 | /**
|
---|
1355 | * Utility to stop event propagation and event default, if these
|
---|
1356 | * features are turned on.
|
---|
1357 | *
|
---|
1358 | * @param {Event} e the event as returned by this.getEvent()
|
---|
1359 | */
|
---|
1360 | this.stopEvent = function(e) {
|
---|
1361 | if (this.stopPropagation) {
|
---|
1362 | UTIL.Event.stopPropagation(e);
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | if (this.preventDefault) {
|
---|
1366 | UTIL.Event.preventDefault(e);
|
---|
1367 | }
|
---|
1368 | };
|
---|
1369 |
|
---|
1370 | /**
|
---|
1371 | * Internal function to clean up event handlers after the drag
|
---|
1372 | * operation is complete
|
---|
1373 | *
|
---|
1374 | * @param {Event} e the event
|
---|
1375 | * @private
|
---|
1376 | */
|
---|
1377 | this.stopDrag = function(e) {
|
---|
1378 | // this.logger.debug("mouseup - removing event handlers");
|
---|
1379 |
|
---|
1380 | // Fire the drag end event for the item that was dragged
|
---|
1381 | if (this.dragCurrent) {
|
---|
1382 | if (this.dragThreshMet) {
|
---|
1383 | this.logger.debug("firing endDrag events");
|
---|
1384 | this.dragCurrent.b4EndDrag(e);
|
---|
1385 | this.dragCurrent.endDrag(e);
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | this.logger.debug("firing onMouseUp event");
|
---|
1389 | this.dragCurrent.onMouseUp(e);
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | this.dragCurrent = null;
|
---|
1393 | this.dragOvers = {};
|
---|
1394 | };
|
---|
1395 |
|
---|
1396 |
|
---|
1397 | /**
|
---|
1398 | * Internal function to handle the mousemove event. Will be invoked
|
---|
1399 | * from the context of the html element.
|
---|
1400 | *
|
---|
1401 | * @TODO figure out what we can do about mouse events lost when the
|
---|
1402 | * user drags objects beyond the window boundary. Currently we can
|
---|
1403 | * detect this in internet explorer by verifying that the mouse is
|
---|
1404 | * down during the mousemove event. Firefox doesn't give us the
|
---|
1405 | * button state on the mousemove event.
|
---|
1406 | *
|
---|
1407 | * @param {Event} e the event
|
---|
1408 | * @private
|
---|
1409 | */
|
---|
1410 | this.handleMouseMove = function(e) {
|
---|
1411 | if (! this.dragCurrent) {
|
---|
1412 | // this.logger.debug("no current drag obj");
|
---|
1413 | return;
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | // var button = e.which || e.button;
|
---|
1417 |
|
---|
1418 | this.logger.debug("which: " + e.which + ", button: "+ e.button);
|
---|
1419 |
|
---|
1420 |
|
---|
1421 | // check for IE mouseup outside of page boundary
|
---|
1422 | if (UTIL.Event.isIE && !e.button) {
|
---|
1423 | this.stopEvent(e);
|
---|
1424 | return this.handleMouseUp(e);
|
---|
1425 | // this.logger.debug("button failure");
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | if (!this.dragThreshMet) {
|
---|
1429 | var diffX = Math.abs(this.startX - UTIL.Event.getPageX(e));
|
---|
1430 | var diffY = Math.abs(this.startY - UTIL.Event.getPageY(e));
|
---|
1431 | // this.logger.debug("diffX: " + diffX + "diffY: " + diffY);
|
---|
1432 | if (diffX > this.clickPixelThresh ||
|
---|
1433 | diffY > this.clickPixelThresh) {
|
---|
1434 | this.logger.debug("pixel threshold met");
|
---|
1435 | this.startDrag(this.startX, this.startY);
|
---|
1436 | }
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | if (this.dragThreshMet) {
|
---|
1440 | this.dragCurrent.b4Drag(e);
|
---|
1441 | this.dragCurrent.onDrag(e);
|
---|
1442 | this.fireEvents(e, false);
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | this.stopEvent(e);
|
---|
1446 | };
|
---|
1447 |
|
---|
1448 | /**
|
---|
1449 | * Iterates over all of the DragDrop elements to find ones we are
|
---|
1450 | * hovering over or dropping on
|
---|
1451 | *
|
---|
1452 | * @param {Event} e the event
|
---|
1453 | * @param {boolean} isDrop is this a drop op or a mouseover op?
|
---|
1454 | * @private
|
---|
1455 | */
|
---|
1456 | this.fireEvents = function(e, isDrop) {
|
---|
1457 | var dc = this.dragCurrent;
|
---|
1458 |
|
---|
1459 | // If the user did the mouse up outside of the window, we could
|
---|
1460 | // get here even though we have ended the drag.
|
---|
1461 | if (!dc || dc.isLocked()) {
|
---|
1462 | return;
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | var x = UTIL.Event.getPageX(e);
|
---|
1466 | var y = UTIL.Event.getPageY(e);
|
---|
1467 | var pt = new YAHOO.util.Point(x,y);
|
---|
1468 |
|
---|
1469 | // cache the previous dragOver array
|
---|
1470 | var oldOvers = [];
|
---|
1471 |
|
---|
1472 | var outEvts = [];
|
---|
1473 | var overEvts = [];
|
---|
1474 | var dropEvts = [];
|
---|
1475 | var enterEvts = [];
|
---|
1476 |
|
---|
1477 | // Check to see if the object we were hovering over is no longer
|
---|
1478 | // being hovered over so we can fire the onDragOut event
|
---|
1479 | for (var i in this.dragOvers) {
|
---|
1480 |
|
---|
1481 | var ddo = this.dragOvers[i];
|
---|
1482 |
|
---|
1483 | if (! this.isTypeOfDD(ddo)) {
|
---|
1484 | continue;
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | if (! this.isOverTarget(pt, ddo, this.mode)) {
|
---|
1488 | outEvts.push( ddo );
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | oldOvers[i] = true;
|
---|
1492 | delete this.dragOvers[i];
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | for (var sGroup in dc.groups) {
|
---|
1496 | // this.logger.debug("Processing group " + sGroup);
|
---|
1497 |
|
---|
1498 | if ("string" != typeof sGroup) {
|
---|
1499 | continue;
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 | for (i in this.ids[sGroup]) {
|
---|
1503 | var oDD = this.ids[sGroup][i];
|
---|
1504 | if (! this.isTypeOfDD(oDD)) {
|
---|
1505 | continue;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | if (oDD.isTarget && !oDD.isLocked() && oDD != dc) {
|
---|
1509 | if (this.isOverTarget(pt, oDD, this.mode)) {
|
---|
1510 | // look for drop interactions
|
---|
1511 | if (isDrop) {
|
---|
1512 | dropEvts.push( oDD );
|
---|
1513 | // look for drag enter and drag over interactions
|
---|
1514 | } else {
|
---|
1515 |
|
---|
1516 | // initial drag over: dragEnter fires
|
---|
1517 | if (!oldOvers[oDD.id]) {
|
---|
1518 | enterEvts.push( oDD );
|
---|
1519 | // subsequent drag overs: dragOver fires
|
---|
1520 | } else {
|
---|
1521 | overEvts.push( oDD );
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | this.dragOvers[oDD.id] = oDD;
|
---|
1525 | }
|
---|
1526 | }
|
---|
1527 | }
|
---|
1528 | }
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | if (this.mode) {
|
---|
1532 | if (outEvts.length > 0) {
|
---|
1533 | dc.b4DragOut(e, outEvts);
|
---|
1534 | dc.onDragOut(e, outEvts);
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | if (enterEvts.length > 0) {
|
---|
1538 | dc.onDragEnter(e, enterEvts);
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | if (overEvts.length > 0) {
|
---|
1542 | dc.b4DragOver(e, overEvts);
|
---|
1543 | dc.onDragOver(e, overEvts);
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | if (dropEvts.length > 0) {
|
---|
1547 | dc.b4DragDrop(e, dropEvts);
|
---|
1548 | dc.onDragDrop(e, dropEvts);
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | } else {
|
---|
1552 | // fire dragout events
|
---|
1553 | for (i=0; i < outEvts.length; ++i) {
|
---|
1554 | this.logger.debug(dc.id+" onDragOut: " + outEvts[i].id);
|
---|
1555 | dc.b4DragOut(e, outEvts[i].id);
|
---|
1556 | dc.onDragOut(e, outEvts[i].id);
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | // fire enter events
|
---|
1560 | for (i=0; i < enterEvts.length; ++i) {
|
---|
1561 | this.logger.debug(dc.id + " dragEnter " + enterEvts[i].id);
|
---|
1562 | // dc.b4DragEnter(e, oDD.id);
|
---|
1563 | dc.onDragEnter(e, enterEvts[i].id);
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | // fire over events
|
---|
1567 | for (i=0; i < overEvts.length; ++i) {
|
---|
1568 | this.logger.debug(dc.id + " dragOver " + overEvts[i].id);
|
---|
1569 | dc.b4DragOver(e, overEvts[i].id);
|
---|
1570 | dc.onDragOver(e, overEvts[i].id);
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | // fire drop events
|
---|
1574 | for (i=0; i < dropEvts.length; ++i) {
|
---|
1575 | this.logger.debug(dc.id + " dragDrop " + dropEvts[i].id);
|
---|
1576 | dc.b4DragDrop(e, dropEvts[i].id);
|
---|
1577 | dc.onDragDrop(e, dropEvts[i].id);
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 | };
|
---|
1583 |
|
---|
1584 | /**
|
---|
1585 | * Helper function for getting the best match from the list of drag
|
---|
1586 | * and drop objects returned by the drag and drop events when we are
|
---|
1587 | * in INTERSECT mode. It returns either the first object that the
|
---|
1588 | * cursor is over, or the object that has the greatest overlap with
|
---|
1589 | * the dragged element.
|
---|
1590 | *
|
---|
1591 | * @param {ygDragDrop[]} dds The array of drag and drop objects
|
---|
1592 | * targeted
|
---|
1593 | * @return {ygDragDrop} The best single match
|
---|
1594 | */
|
---|
1595 | this.getBestMatch = function(dds) {
|
---|
1596 | var winner = null;
|
---|
1597 | // Return null if the input is not what we expect
|
---|
1598 | //if (!dds || !dds.length || dds.length == 0) {
|
---|
1599 | // winner = null;
|
---|
1600 | // If there is only one item, it wins
|
---|
1601 | //} else if (dds.length == 1) {
|
---|
1602 |
|
---|
1603 | if (dds.length == 1) {
|
---|
1604 | winner = dds[0];
|
---|
1605 | } else {
|
---|
1606 | // Loop through the targeted items
|
---|
1607 | for (var i=0; i<dds.length; ++i) {
|
---|
1608 | var dd = dds[i];
|
---|
1609 | // If the cursor is over the object, it wins. If the
|
---|
1610 | // cursor is over multiple matches, the first one we come
|
---|
1611 | // to wins.
|
---|
1612 | if (dd.cursorIsOver) {
|
---|
1613 | winner = dd;
|
---|
1614 | break;
|
---|
1615 | // Otherwise the object with the most overlap wins
|
---|
1616 | } else {
|
---|
1617 | if (!winner ||
|
---|
1618 | winner.overlap.getArea() < dd.overlap.getArea()) {
|
---|
1619 | winner = dd;
|
---|
1620 | }
|
---|
1621 | }
|
---|
1622 | }
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | return winner;
|
---|
1626 | };
|
---|
1627 |
|
---|
1628 | /**
|
---|
1629 | * Refreshes the cache of the top-left and bottom-right points of the
|
---|
1630 | * drag and drop objects in the specified groups
|
---|
1631 | *
|
---|
1632 | * @param {Array} aGroups an associative array of groups to refresh
|
---|
1633 | */
|
---|
1634 | this.refreshCache = function(aGroups) {
|
---|
1635 | this.logger.debug("refreshing element location cache");
|
---|
1636 | for (sGroup in aGroups) {
|
---|
1637 | if ("string" != typeof sGroup) {
|
---|
1638 | continue;
|
---|
1639 | }
|
---|
1640 | for (i in this.ids[sGroup]) {
|
---|
1641 | var oDD = this.ids[sGroup][i];
|
---|
1642 |
|
---|
1643 | if (this.isTypeOfDD(oDD)) {
|
---|
1644 | var loc = this.getLocation(oDD);
|
---|
1645 | if (loc) {
|
---|
1646 | this.locationCache[oDD.id] = loc;
|
---|
1647 | } else {
|
---|
1648 | delete this.locationCache[oDD.id];
|
---|
1649 | this.logger.debug("something is wrong with the element");
|
---|
1650 | // this will unregister the drag and drop object if
|
---|
1651 | // the element is not in a usable state
|
---|
1652 | oDD.unreg();
|
---|
1653 | }
|
---|
1654 | }
|
---|
1655 | }
|
---|
1656 | }
|
---|
1657 | };
|
---|
1658 |
|
---|
1659 | /**
|
---|
1660 | * This checks to make sure an element exists and is in the DOM. The
|
---|
1661 | * main purpose is to handle cases where innerHTML is used to remove
|
---|
1662 | * drag and drop objects from the DOM. IE provides an 'unspecified
|
---|
1663 | * error' when trying to access the offsetParent of such an element
|
---|
1664 | * @param {HTMLElement} el the element to check
|
---|
1665 | * @return {boolean} true if the element looks usable
|
---|
1666 | */
|
---|
1667 | this.verifyEl = function(el) {
|
---|
1668 | try {
|
---|
1669 | if (el) {
|
---|
1670 | var parent = el.offsetParent;
|
---|
1671 | if (parent) {
|
---|
1672 | return true;
|
---|
1673 | }
|
---|
1674 | }
|
---|
1675 | } catch(e) {
|
---|
1676 | this.logger.debug("detected problem with an element");
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | return false;
|
---|
1680 | };
|
---|
1681 |
|
---|
1682 | /**
|
---|
1683 | * Returns the an array containing the drag and drop element's position
|
---|
1684 | * and size, including the ygDragDrop.padding configured for it
|
---|
1685 | *
|
---|
1686 | * @param {ygDragDrop} oDD the drag and drop object to get the
|
---|
1687 | * location for
|
---|
1688 | * @return array containing the top left and bottom right points of the
|
---|
1689 | * element
|
---|
1690 | */
|
---|
1691 | this.getLocation = function(oDD) {
|
---|
1692 | if (! this.isTypeOfDD(oDD)) {
|
---|
1693 | this.logger.debug(oDD + " is not a DD obj");
|
---|
1694 | return null;
|
---|
1695 | }
|
---|
1696 |
|
---|
1697 | var el = oDD.getEl();
|
---|
1698 |
|
---|
1699 | if (!this.verifyEl(el)) {
|
---|
1700 | this.logger.debug(oDD + " element is not usable");
|
---|
1701 | return null;
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | // this.logger.debug(oDD.id + " padding: " + oDD.padding);
|
---|
1705 |
|
---|
1706 | // var aPos = ygPos.getPos(el);
|
---|
1707 | var aPos = YAHOO.util.Dom.getXY(el);
|
---|
1708 |
|
---|
1709 | x1 = aPos[0];
|
---|
1710 | x2 = x1 + el.offsetWidth;
|
---|
1711 |
|
---|
1712 | y1 = aPos[1];
|
---|
1713 | y2 = y1 + el.offsetHeight;
|
---|
1714 |
|
---|
1715 | var t = y1 - oDD.padding[0];
|
---|
1716 | var r = x2 + oDD.padding[1];
|
---|
1717 | var b = y2 + oDD.padding[2];
|
---|
1718 | var l = x1 - oDD.padding[3];
|
---|
1719 |
|
---|
1720 | return new YAHOO.util.Region( t, r, b, l );
|
---|
1721 |
|
---|
1722 | };
|
---|
1723 |
|
---|
1724 | /**
|
---|
1725 | * Checks the cursor location to see if it over the target
|
---|
1726 | *
|
---|
1727 | * @param {YAHOO.util.Point} pt The point to evaluate
|
---|
1728 | * @param {ygDragDrop} oDDTarget the DragDrop object we are inspecting
|
---|
1729 | * @return {boolean} true if the mouse is over the target
|
---|
1730 | * @private
|
---|
1731 | */
|
---|
1732 | this.isOverTarget = function(pt, oDDTarget, intersect) {
|
---|
1733 | // use cache if available
|
---|
1734 | var loc = this.locationCache[oDDTarget.id];
|
---|
1735 | if (!loc || !this.useCache) {
|
---|
1736 | this.logger.debug("cache not populated");
|
---|
1737 | loc = this.getLocation(oDDTarget);
|
---|
1738 | this.locationCache[oDDTarget.id] = loc;
|
---|
1739 |
|
---|
1740 | this.logger.debug("cache: " + loc);
|
---|
1741 | }
|
---|
1742 |
|
---|
1743 | // this.logger.debug("isOverTarget: " + x + "," + y + ", " + loc);
|
---|
1744 |
|
---|
1745 | // var cursorIsOver = (x >= loc[3] && x <= loc[1] && y >= loc[0] && y <= loc[2]);
|
---|
1746 | //oDDTarget.cursorIsOver = loc.contains( new YAHOO.util.Point(x, y) );
|
---|
1747 | oDDTarget.cursorIsOver = loc.contains( pt );
|
---|
1748 | oDDTarget.overlap = null;
|
---|
1749 |
|
---|
1750 | // if (this.INTERSECT == this.mode) {
|
---|
1751 | if (intersect) {
|
---|
1752 |
|
---|
1753 | var curRegion =
|
---|
1754 | YAHOO.util.Region.getRegion(this.dragCurrent.getDragEl());
|
---|
1755 | var overlap = curRegion.intersect(loc);
|
---|
1756 |
|
---|
1757 | if (overlap) {
|
---|
1758 | oDDTarget.overlap = overlap;
|
---|
1759 | return true;
|
---|
1760 | } else {
|
---|
1761 | return false;
|
---|
1762 | }
|
---|
1763 |
|
---|
1764 | } else {
|
---|
1765 | return oDDTarget.cursorIsOver;
|
---|
1766 | }
|
---|
1767 | };
|
---|
1768 |
|
---|
1769 | /**
|
---|
1770 | * @private
|
---|
1771 | */
|
---|
1772 | this._onUnload = function(e, me) {
|
---|
1773 | this.unregAll();
|
---|
1774 | };
|
---|
1775 |
|
---|
1776 | /**
|
---|
1777 | * Cleans up the drag and drop events and objects.
|
---|
1778 | *
|
---|
1779 | * @private
|
---|
1780 | */
|
---|
1781 | this.unregAll = function() {
|
---|
1782 | this.logger.debug("unregister all");
|
---|
1783 |
|
---|
1784 | if (this.dragCurrent) {
|
---|
1785 | this.stopDrag();
|
---|
1786 | this.dragCurrent = null;
|
---|
1787 | }
|
---|
1788 |
|
---|
1789 | this._execOnAll("unreg", []);
|
---|
1790 |
|
---|
1791 | for (i in this.elementCache) {
|
---|
1792 | delete this.elementCache[i];
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | this.elementCache = {};
|
---|
1796 | this.ids = {};
|
---|
1797 | };
|
---|
1798 |
|
---|
1799 | /**
|
---|
1800 | * A cache of DOM elements
|
---|
1801 | *
|
---|
1802 | * @private
|
---|
1803 | */
|
---|
1804 | this.elementCache = {};
|
---|
1805 |
|
---|
1806 | /**
|
---|
1807 | * Get the wrapper for the DOM element specified
|
---|
1808 | *
|
---|
1809 | * @param {String} id the id of the elment to get
|
---|
1810 | * @return {YAHOO.util.DDM.ElementWrapper} the wrapped element
|
---|
1811 | * @private
|
---|
1812 | */
|
---|
1813 | this.getElWrapper = function(id) {
|
---|
1814 | var oWrapper = this.elementCache[id];
|
---|
1815 | if (!oWrapper || !oWrapper.el) {
|
---|
1816 | // this.logger.debug("adding element cache: " + id);
|
---|
1817 | oWrapper = this.elementCache[id] =
|
---|
1818 | new this.ElementWrapper(document.getElementById(id));
|
---|
1819 | }
|
---|
1820 | return oWrapper;
|
---|
1821 | };
|
---|
1822 |
|
---|
1823 | /**
|
---|
1824 | * Returns the actual DOM element
|
---|
1825 | *
|
---|
1826 | * @param {String} id the id of the elment to get
|
---|
1827 | * @return {Object} The element
|
---|
1828 | */
|
---|
1829 | this.getElement = function(id) {
|
---|
1830 | // return this.getElWrapper(id).el;
|
---|
1831 | return document.getElementById(id);
|
---|
1832 | };
|
---|
1833 |
|
---|
1834 | /**
|
---|
1835 | * Returns the style property for the DOM element (i.e.,
|
---|
1836 | * document.getElById(id).style)
|
---|
1837 | *
|
---|
1838 | * @param {String} id the id of the elment to get
|
---|
1839 | * @return {Object} The style property of the element
|
---|
1840 | */
|
---|
1841 | this.getCss = function(id) {
|
---|
1842 | // return this.getElWrapper(id).css;
|
---|
1843 | var css = null;
|
---|
1844 | var el = document.getElementById(id);
|
---|
1845 | if (el) {
|
---|
1846 | css = el.style;
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | return css;
|
---|
1850 | };
|
---|
1851 |
|
---|
1852 | /**
|
---|
1853 | * Inner class for cached elements
|
---|
1854 | */
|
---|
1855 | this.ElementWrapper = function(el) {
|
---|
1856 | /**
|
---|
1857 | * @private
|
---|
1858 | */
|
---|
1859 | this.el = el || null;
|
---|
1860 | /**
|
---|
1861 | * @private
|
---|
1862 | */
|
---|
1863 | this.id = this.el && el.id;
|
---|
1864 | /**
|
---|
1865 | * @private
|
---|
1866 | */
|
---|
1867 | this.css = this.el && el.style;
|
---|
1868 | };
|
---|
1869 |
|
---|
1870 | /**
|
---|
1871 | * Returns the X position of an html element
|
---|
1872 | * @param el the element for which to get the position
|
---|
1873 | * @return {int} the X coordinate
|
---|
1874 | */
|
---|
1875 | this.getPosX = function(el) {
|
---|
1876 | return YAHOO.util.Dom.getX(el);
|
---|
1877 | };
|
---|
1878 |
|
---|
1879 | /**
|
---|
1880 | * Returns the Y position of an html element
|
---|
1881 | * @param el the element for which to get the position
|
---|
1882 | * @return {int} the Y coordinate
|
---|
1883 | */
|
---|
1884 | this.getPosY = function(el) {
|
---|
1885 | return YAHOO.util.Dom.getY(el);
|
---|
1886 | };
|
---|
1887 |
|
---|
1888 | /**
|
---|
1889 | * Swap two nodes. In IE, we use the native method, for others we
|
---|
1890 | * emulate the IE behavior
|
---|
1891 | *
|
---|
1892 | * @param n1 the first node to swap
|
---|
1893 | * @param n2 the other node to swap
|
---|
1894 | */
|
---|
1895 | this.swapNode = function(n1, n2) {
|
---|
1896 | if (n1.swapNode) {
|
---|
1897 | n1.swapNode(n2);
|
---|
1898 | } else {
|
---|
1899 | // the node reference order for the swap is a little tricky.
|
---|
1900 | var p = n2.parentNode;
|
---|
1901 | var s = n2.nextSibling;
|
---|
1902 | n1.parentNode.replaceChild(n2,n1);
|
---|
1903 | p.insertBefore(n1,s);
|
---|
1904 | }
|
---|
1905 | };
|
---|
1906 |
|
---|
1907 | /**
|
---|
1908 | * @private
|
---|
1909 | */
|
---|
1910 | this.getScroll = function () {
|
---|
1911 | var t, l;
|
---|
1912 | if (document.documentElement && document.documentElement.scrollTop) {
|
---|
1913 | t = document.documentElement.scrollTop;
|
---|
1914 | l = document.documentElement.scrollLeft;
|
---|
1915 | } else if (document.body) {
|
---|
1916 | t = document.body.scrollTop;
|
---|
1917 | l = document.body.scrollLeft;
|
---|
1918 | }
|
---|
1919 | return { top: t, left: l };
|
---|
1920 | };
|
---|
1921 |
|
---|
1922 | /**
|
---|
1923 | * Returns the specified element style property
|
---|
1924 | * @param {HTMLElement} el the element
|
---|
1925 | * @param {string} styleProp the style property
|
---|
1926 | * @return {string} The value of the style property
|
---|
1927 | */
|
---|
1928 | this.getStyle = function(el, styleProp) {
|
---|
1929 | if (el.style.styleProp) {
|
---|
1930 | return el.style.styleProp;
|
---|
1931 | } else if (el.currentStyle) {
|
---|
1932 | return el.currentStyle[styleProp];
|
---|
1933 | } else if (document.defaultView) {
|
---|
1934 | return document.defaultView.getComputedStyle(el, null).
|
---|
1935 | getPropertyValue(styleProp);
|
---|
1936 | }
|
---|
1937 | };
|
---|
1938 |
|
---|
1939 | /**
|
---|
1940 | * Gets the scrollTop
|
---|
1941 | *
|
---|
1942 | * @return {int} the document's scrollTop
|
---|
1943 | */
|
---|
1944 | this.getScrollTop = function () { return this.getScroll().top; };
|
---|
1945 |
|
---|
1946 | /**
|
---|
1947 | * Gets the scrollLeft
|
---|
1948 | *
|
---|
1949 | * @return {int} the document's scrollTop
|
---|
1950 | */
|
---|
1951 | this.getScrollLeft = function () { return this.getScroll().left; };
|
---|
1952 |
|
---|
1953 | this.moveToEl = function (moveEl, targetEl) {
|
---|
1954 | var aCoord = YAHOO.util.Dom.getXY(targetEl);
|
---|
1955 | this.logger.debug("moveToEl: " + aCoord);
|
---|
1956 | YAHOO.util.Dom.setXY(moveEl, aCoord);
|
---|
1957 | };
|
---|
1958 |
|
---|
1959 | /**
|
---|
1960 | * Gets the client height
|
---|
1961 | *
|
---|
1962 | * @return {int} client height in px
|
---|
1963 | */
|
---|
1964 | this.getClientHeight = function() {
|
---|
1965 | return (window.innerHeight) ? window.innerHeight :
|
---|
1966 | (document.documentElement && document.documentElement.clientHeight) ?
|
---|
1967 | document.documentElement.clientHeight : document.body.offsetHeight;
|
---|
1968 | };
|
---|
1969 |
|
---|
1970 | /**
|
---|
1971 | * Gets the client width
|
---|
1972 | *
|
---|
1973 | * @return {int} client width in px
|
---|
1974 | */
|
---|
1975 | this.getClientWidth = function() {
|
---|
1976 | return (window.innerWidth) ? window.innerWidth :
|
---|
1977 | (document.documentElement && document.documentElement.clientWidth) ?
|
---|
1978 | document.documentElement.clientWidth : document.body.offsetWidth;
|
---|
1979 | };
|
---|
1980 |
|
---|
1981 | /**
|
---|
1982 | * numeric array sort function
|
---|
1983 | */
|
---|
1984 | this.numericSort = function(a, b) { return (a - b); };
|
---|
1985 |
|
---|
1986 | /**
|
---|
1987 | * @private
|
---|
1988 | */
|
---|
1989 | this._timeoutCount = 0;
|
---|
1990 |
|
---|
1991 | /**
|
---|
1992 | * @private
|
---|
1993 | * Trying to make the load order less important. Without this we get
|
---|
1994 | * an error if this file is loaded before the Event Utility.
|
---|
1995 | */
|
---|
1996 | this._addListeners = function() {
|
---|
1997 | if ( UTIL.Event &&
|
---|
1998 | document &&
|
---|
1999 | document.body ) {
|
---|
2000 |
|
---|
2001 | this._onLoad();
|
---|
2002 | } else {
|
---|
2003 | if (this._timeoutCount > 500) {
|
---|
2004 | this.logger.debug("DragDrop requires the Event Utility");
|
---|
2005 | } else {
|
---|
2006 | setTimeout("YAHOO.util.DDM._addListeners()", 10);
|
---|
2007 | this._timeoutCount += 1;
|
---|
2008 | }
|
---|
2009 | }
|
---|
2010 |
|
---|
2011 | };
|
---|
2012 |
|
---|
2013 | /**
|
---|
2014 | * Recursively searches the immediate parent and all child nodes for
|
---|
2015 | * the handle element in order to determine wheter or not it was
|
---|
2016 | * clicked.
|
---|
2017 | *
|
---|
2018 | * @param node the html element to inspect
|
---|
2019 | */
|
---|
2020 | this.handleWasClicked = function(node, id) {
|
---|
2021 | if (this.isHandle(id, node.id)) {
|
---|
2022 | this.logger.debug("clicked node is a handle");
|
---|
2023 | return true;
|
---|
2024 | } else {
|
---|
2025 | // check to see if this is a text node child of the one we want
|
---|
2026 | var p = node.parentNode;
|
---|
2027 | // this.logger.debug("p: " + p);
|
---|
2028 |
|
---|
2029 | while (p) {
|
---|
2030 | if (this.isHandle(id, p.id)) {
|
---|
2031 | return true;
|
---|
2032 | } else {
|
---|
2033 | this.logger.debug(p.id + " is not a handle");
|
---|
2034 | p = p.parentNode;
|
---|
2035 | }
|
---|
2036 | }
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 | return false;
|
---|
2040 | };
|
---|
2041 |
|
---|
2042 | };
|
---|
2043 |
|
---|
2044 | // shorter alias, save a few bytes
|
---|
2045 | YAHOO.util.DDM = YAHOO.util.DragDropMgr;
|
---|
2046 | YAHOO.util.DDM._addListeners();
|
---|
2047 |
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | /* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
|
---|
2051 |
|
---|
2052 | /**
|
---|
2053 | * @class a DragDrop implementation where the linked element follows the
|
---|
2054 | * mouse cursor.
|
---|
2055 | *
|
---|
2056 | * @extends DragDrop
|
---|
2057 | * @constructor
|
---|
2058 | * @param {String} id the id of the linked element
|
---|
2059 | * @param {String} sGroup the group of related DragDrop items
|
---|
2060 | */
|
---|
2061 | YAHOO.util.DD = function(id, sGroup) {
|
---|
2062 | if (id) {
|
---|
2063 | this.init(id, sGroup);
|
---|
2064 | this.logger.setModuleName("DD");
|
---|
2065 | }
|
---|
2066 | };
|
---|
2067 |
|
---|
2068 | YAHOO.util.DD.prototype = new YAHOO.util.DragDrop();
|
---|
2069 |
|
---|
2070 | /**
|
---|
2071 | * Should we auto-scroll? Defaults to true
|
---|
2072 | *
|
---|
2073 | * @type boolean
|
---|
2074 | */
|
---|
2075 | YAHOO.util.DD.prototype.scroll = true;
|
---|
2076 |
|
---|
2077 | /**
|
---|
2078 | * Sets the pointer offset to the distance between the linked element's top
|
---|
2079 | * left corner and the location the element was clicked
|
---|
2080 | *
|
---|
2081 | * @param {int} iPageX the X coordinate of the click
|
---|
2082 | * @param {int} iPageY the Y coordinate of the click
|
---|
2083 | */
|
---|
2084 | YAHOO.util.DD.prototype.autoOffset = function(iPageX, iPageY) {
|
---|
2085 | var el = this.getEl();
|
---|
2086 | var aCoord = YAHOO.util.Dom.getXY(el);
|
---|
2087 | var x = iPageX - aCoord[0];
|
---|
2088 | var y = iPageY - aCoord[1];
|
---|
2089 | this.setDelta(x, y);
|
---|
2090 | this.logger.debug("autoOffset el pos: " + aCoord + ", delta: " + x + "," + y);
|
---|
2091 | };
|
---|
2092 |
|
---|
2093 | /**
|
---|
2094 | * Sets the pointer offset. You can call this directly to force the offset to
|
---|
2095 | * be in a particular location (e.g., pass in 0,0 to set it to the center of the
|
---|
2096 | * object, as done in ygDDSliderBG)
|
---|
2097 | *
|
---|
2098 | * @param {int} iDeltaX the distance from the left
|
---|
2099 | * @param {int} iDeltaY the distance from the top
|
---|
2100 | */
|
---|
2101 | YAHOO.util.DD.prototype.setDelta = function(iDeltaX, iDeltaY) {
|
---|
2102 | this.deltaX = iDeltaX;
|
---|
2103 | this.deltaY = iDeltaY;
|
---|
2104 | this.logger.debug("deltaX:" + this.deltaX + ", deltaY:" + this.deltaY);
|
---|
2105 | };
|
---|
2106 |
|
---|
2107 | /**
|
---|
2108 | * Sets the drag element to the location of the mousedown or click event,
|
---|
2109 | * maintaining the cursor location relative to the location on the element
|
---|
2110 | * that was clicked. Override this if you want to place the element in a
|
---|
2111 | * location other than where the cursor is.
|
---|
2112 | *
|
---|
2113 | * @param {int} iPageX the X coordinate of the mousedown or drag event
|
---|
2114 | * @param {int} iPageY the Y coordinate of the mousedown or drag event
|
---|
2115 | */
|
---|
2116 |
|
---|
2117 | YAHOO.util.DD.prototype.setDragElPos = function(iPageX, iPageY) {
|
---|
2118 | this.alignElWithMouse(this.getDragEl(), iPageX, iPageY);
|
---|
2119 | };
|
---|
2120 |
|
---|
2121 | /**
|
---|
2122 | * Sets the element to the location of the mousedown or click event,
|
---|
2123 | * maintaining the cursor location relative to the location on the element
|
---|
2124 | * that was clicked. Override this if you want to place the element in a
|
---|
2125 | * location other than where the cursor is.
|
---|
2126 | *
|
---|
2127 | * @param {HTMLElement} el the element to move
|
---|
2128 | * @param {int} iPageX the X coordinate of the mousedown or drag event
|
---|
2129 | * @param {int} iPageY the Y coordinate of the mousedown or drag event
|
---|
2130 | */
|
---|
2131 | YAHOO.util.DD.prototype.alignElWithMouse = function(el, iPageX, iPageY) {
|
---|
2132 | var oCoord = this.getTargetCoord(iPageX, iPageY);
|
---|
2133 | var aCoord = [oCoord.x, oCoord.y];
|
---|
2134 | // this.logger.debug("****alignElWithMouse : " + el.id + ", " + aCoord + ", " + el.style.display);
|
---|
2135 | YAHOO.util.Dom.setXY(el, aCoord);
|
---|
2136 |
|
---|
2137 | this.cachePosition(oCoord.x, oCoord.y);
|
---|
2138 |
|
---|
2139 | this.autoScroll(oCoord.x, oCoord.y, el.offsetHeight, el.offsetWidth);
|
---|
2140 | };
|
---|
2141 |
|
---|
2142 | /**
|
---|
2143 | * Saves the most recent position so that we can reset the constraints and
|
---|
2144 | * tick marks on-demand. We need to know this so that we can calculate the
|
---|
2145 | * number of pixels the element is offset from its original position.
|
---|
2146 | */
|
---|
2147 | YAHOO.util.DD.prototype.cachePosition = function(iPageX, iPageY) {
|
---|
2148 | if (iPageX) {
|
---|
2149 | this.lastPageX = iPageX;
|
---|
2150 | this.lastPageY = iPageY;
|
---|
2151 | } else {
|
---|
2152 | var aCoord = YAHOO.util.Dom.getXY(this.getEl());
|
---|
2153 | this.lastPageX = aCoord[0];
|
---|
2154 | this.lastPageY = aCoord[1];
|
---|
2155 | }
|
---|
2156 | };
|
---|
2157 |
|
---|
2158 | /**
|
---|
2159 | * Auto-scroll the window if the dragged object has been moved beyond the
|
---|
2160 | * visible window boundary.
|
---|
2161 | *
|
---|
2162 | * @param {int} x the drag element's x position
|
---|
2163 | * @param {int} y the drag element's y position
|
---|
2164 | * @param {int} h the height of the drag element
|
---|
2165 | * @param {int} w the width of the drag element
|
---|
2166 | * @private
|
---|
2167 | */
|
---|
2168 | YAHOO.util.DD.prototype.autoScroll = function(x, y, h, w) {
|
---|
2169 |
|
---|
2170 | if (this.scroll) {
|
---|
2171 | // The client height
|
---|
2172 | var clientH = this.DDM.getClientHeight();
|
---|
2173 |
|
---|
2174 | // The client width
|
---|
2175 | var clientW = this.DDM.getClientWidth();
|
---|
2176 |
|
---|
2177 | // The amt scrolled down
|
---|
2178 | var st = this.DDM.getScrollTop();
|
---|
2179 |
|
---|
2180 | // The amt scrolled right
|
---|
2181 | var sl = this.DDM.getScrollLeft();
|
---|
2182 |
|
---|
2183 | // Location of the bottom of the element
|
---|
2184 | var bot = h + y;
|
---|
2185 |
|
---|
2186 | // Location of the right of the element
|
---|
2187 | var right = w + x;
|
---|
2188 |
|
---|
2189 | // The distance from the cursor to the bottom of the visible area,
|
---|
2190 | // adjusted so that we don't scroll if the cursor is beyond the
|
---|
2191 | // element drag constraints
|
---|
2192 | var toBot = (clientH + st - y - this.deltaY);
|
---|
2193 |
|
---|
2194 | // The distance from the cursor to the right of the visible area
|
---|
2195 | var toRight = (clientW + sl - x - this.deltaX);
|
---|
2196 |
|
---|
2197 | // this.logger.debug( " x: " + x + " y: " + y + " h: " + h +
|
---|
2198 | // " clientH: " + clientH + " clientW: " + clientW +
|
---|
2199 | // " st: " + st + " sl: " + sl + " bot: " + bot +
|
---|
2200 | // " right: " + right + " toBot: " + toBot + " toRight: " + toRight);
|
---|
2201 |
|
---|
2202 | // How close to the edge the cursor must be before we scroll
|
---|
2203 | // var thresh = (document.all) ? 100 : 40;
|
---|
2204 | var thresh = 40;
|
---|
2205 |
|
---|
2206 | // How many pixels to scroll per autoscroll op. This helps to reduce
|
---|
2207 | // clunky scrolling. IE is more sensitive about this ... it needs this
|
---|
2208 | // value to be higher.
|
---|
2209 | var scrAmt = (document.all) ? 80 : 30;
|
---|
2210 |
|
---|
2211 | // Scroll down if we are near the bottom of the visible page and the
|
---|
2212 | // obj extends below the crease
|
---|
2213 | if ( bot > clientH && toBot < thresh ) {
|
---|
2214 | window.scrollTo(sl, st + scrAmt);
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 | // Scroll up if the window is scrolled down and the top of the object
|
---|
2218 | // goes above the top border
|
---|
2219 | if ( y < st && st > 0 && y - st < thresh ) {
|
---|
2220 | window.scrollTo(sl, st - scrAmt);
|
---|
2221 | }
|
---|
2222 |
|
---|
2223 | // Scroll right if the obj is beyond the right border and the cursor is
|
---|
2224 | // near the border.
|
---|
2225 | if ( right > clientW && toRight < thresh ) {
|
---|
2226 | window.scrollTo(sl + scrAmt, st);
|
---|
2227 | }
|
---|
2228 |
|
---|
2229 | // Scroll left if the window has been scrolled to the right and the obj
|
---|
2230 | // extends past the left border
|
---|
2231 | if ( x < sl && sl > 0 && x - sl < thresh ) {
|
---|
2232 | window.scrollTo(sl - scrAmt, st);
|
---|
2233 | }
|
---|
2234 | }
|
---|
2235 | };
|
---|
2236 |
|
---|
2237 | /**
|
---|
2238 | * Finds the location the element should be placed if we want to move
|
---|
2239 | * it to where the mouse location less the click offset would place us.
|
---|
2240 | *
|
---|
2241 | * @param {int} iPageX the X coordinate of the click
|
---|
2242 | * @param {int} iPageY the Y coordinate of the click
|
---|
2243 | * @return an object that contains the coordinates (Object.x and Object.y)
|
---|
2244 | * @private
|
---|
2245 | */
|
---|
2246 | YAHOO.util.DD.prototype.getTargetCoord = function(iPageX, iPageY) {
|
---|
2247 |
|
---|
2248 | // this.logger.debug("getTargetCoord: " + iPageX + ", " + iPageY);
|
---|
2249 |
|
---|
2250 | var x = iPageX - this.deltaX;
|
---|
2251 | var y = iPageY - this.deltaY;
|
---|
2252 |
|
---|
2253 | if (this.constrainX) {
|
---|
2254 | if (x < this.minX) { x = this.minX; }
|
---|
2255 | if (x > this.maxX) { x = this.maxX; }
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | if (this.constrainY) {
|
---|
2259 | if (y < this.minY) { y = this.minY; }
|
---|
2260 | if (y > this.maxY) { y = this.maxY; }
|
---|
2261 | }
|
---|
2262 |
|
---|
2263 | x = this.getTick(x, this.xTicks);
|
---|
2264 | y = this.getTick(y, this.yTicks);
|
---|
2265 |
|
---|
2266 | // this.logger.debug("getTargetCoord " +
|
---|
2267 | // " iPageX: " + iPageX +
|
---|
2268 | // " iPageY: " + iPageY +
|
---|
2269 | // " x: " + x + ", y: " + y);
|
---|
2270 |
|
---|
2271 | return {x:x, y:y};
|
---|
2272 | };
|
---|
2273 |
|
---|
2274 | // overrides YAHOO.util.DragDrop
|
---|
2275 | YAHOO.util.DD.prototype.b4MouseDown = function(e) {
|
---|
2276 | // this.resetConstraints();
|
---|
2277 | this.autoOffset(YAHOO.util.Event.getPageX(e),
|
---|
2278 | YAHOO.util.Event.getPageY(e));
|
---|
2279 | };
|
---|
2280 |
|
---|
2281 | // overrides YAHOO.util.DragDrop
|
---|
2282 | YAHOO.util.DD.prototype.b4Drag = function(e) {
|
---|
2283 | this.setDragElPos(YAHOO.util.Event.getPageX(e),
|
---|
2284 | YAHOO.util.Event.getPageY(e));
|
---|
2285 | };
|
---|
2286 |
|
---|
2287 |
|
---|
2288 | ///////////////////////////////////////////////////////////////////////////////
|
---|
2289 | // Debugging ygDragDrop events that can be overridden
|
---|
2290 | ///////////////////////////////////////////////////////////////////////////////
|
---|
2291 | /*
|
---|
2292 | YAHOO.util.DD.prototype.startDrag = function(x, y) {
|
---|
2293 | this.logger.debug(this.id.toString() + " startDrag");
|
---|
2294 | };
|
---|
2295 |
|
---|
2296 | YAHOO.util.DD.prototype.onDrag = function(e) {
|
---|
2297 | this.logger.debug(this.id.toString() + " onDrag");
|
---|
2298 | };
|
---|
2299 |
|
---|
2300 | YAHOO.util.DD.prototype.onDragEnter = function(e, id) {
|
---|
2301 | this.logger.debug(this.id.toString() + " onDragEnter: " + id);
|
---|
2302 | };
|
---|
2303 |
|
---|
2304 | YAHOO.util.DD.prototype.onDragOver = function(e, id) {
|
---|
2305 | this.logger.debug(this.id.toString() + " onDragOver: " + id);
|
---|
2306 | };
|
---|
2307 |
|
---|
2308 | YAHOO.util.DD.prototype.onDragOut = function(e, id) {
|
---|
2309 | this.logger.debug(this.id.toString() + " onDragOut: " + id);
|
---|
2310 | };
|
---|
2311 |
|
---|
2312 | YAHOO.util.DD.prototype.onDragDrop = function(e, id) {
|
---|
2313 | this.logger.debug(this.id.toString() + " onDragDrop: " + id);
|
---|
2314 | };
|
---|
2315 |
|
---|
2316 | YAHOO.util.DD.prototype.endDrag = function(e) {
|
---|
2317 | this.logger.debug(this.id.toString() + " endDrag");
|
---|
2318 | };
|
---|
2319 | */
|
---|
2320 |
|
---|
2321 | /* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
|
---|
2322 |
|
---|
2323 | /**
|
---|
2324 | * @class a DragDrop implementation that inserts an empty, bordered div into
|
---|
2325 | * the document that follows the cursor during drag operations. At the time of
|
---|
2326 | * the click, the frame div is resized to the dimensions of the linked html
|
---|
2327 | * element, and moved to the exact location of the linked element.
|
---|
2328 | *
|
---|
2329 | * @extends YAHOO.util.DD
|
---|
2330 | * @constructor
|
---|
2331 | * @param {String} id the id of the linked html element
|
---|
2332 | * @param {String} sGroup the group of related DragDrop objects
|
---|
2333 | */
|
---|
2334 | YAHOO.util.DDProxy = function(id, sGroup) {
|
---|
2335 | if (id) {
|
---|
2336 | this.init(id, sGroup);
|
---|
2337 | this.initFrame();
|
---|
2338 | this.logger.setModuleName("DDProxy");
|
---|
2339 | }
|
---|
2340 | };
|
---|
2341 |
|
---|
2342 | YAHOO.util.DDProxy.prototype = new YAHOO.util.DD();
|
---|
2343 |
|
---|
2344 | /**
|
---|
2345 | * A reference to the one div element we create for all instances of this class
|
---|
2346 | *
|
---|
2347 | * @type Object
|
---|
2348 | */
|
---|
2349 | YAHOO.util.DDProxy.frameDiv = null;
|
---|
2350 |
|
---|
2351 | /**
|
---|
2352 | * the drag frame div id
|
---|
2353 | *
|
---|
2354 | * @type String
|
---|
2355 | */
|
---|
2356 | YAHOO.util.DDProxy.dragElId = "ygddfdiv";
|
---|
2357 |
|
---|
2358 | /**
|
---|
2359 | * The border width of the frame. This is used when we resize the frame to
|
---|
2360 | * the size of the linked element. We substract the border width to make
|
---|
2361 | * the div the correct size.
|
---|
2362 | *
|
---|
2363 | * @TODO find a better way to handle this
|
---|
2364 | *
|
---|
2365 | * @type int
|
---|
2366 | */
|
---|
2367 | YAHOO.util.DDProxy.prototype.borderWidth = 2;
|
---|
2368 |
|
---|
2369 | /**
|
---|
2370 | * By default we resize the drag frame to be the same size as the element
|
---|
2371 | * we want to drag (this is to get the frame effect). We can turn it off
|
---|
2372 | * if we want a different behavior (ex: ygDDMy2)
|
---|
2373 | *
|
---|
2374 | * @type boolean
|
---|
2375 | */
|
---|
2376 | YAHOO.util.DDProxy.prototype.resizeFrame = true;
|
---|
2377 |
|
---|
2378 | /**
|
---|
2379 | * By default the frame is positioned exactly where the drag element is, so
|
---|
2380 | * we use the cursor offset provided by YAHOO.util.DD. Another option that works only if
|
---|
2381 | * you do not have constraints on the obj is to have the drag frame centered
|
---|
2382 | * around the cursor. Set centerFrame to true for this effect. Ex:
|
---|
2383 | * ygDDMy2
|
---|
2384 | *
|
---|
2385 | * @type boolean
|
---|
2386 | */
|
---|
2387 | YAHOO.util.DDProxy.prototype.centerFrame = false;
|
---|
2388 |
|
---|
2389 | /**
|
---|
2390 | * Create the drag frame if needed
|
---|
2391 | */
|
---|
2392 | YAHOO.util.DDProxy.createFrame = function() {
|
---|
2393 | var THIS = YAHOO.util.DDProxy;
|
---|
2394 |
|
---|
2395 | if (!document || !document.body) {
|
---|
2396 | setTimeout(THIS.createFrame, 50);
|
---|
2397 | return;
|
---|
2398 | }
|
---|
2399 |
|
---|
2400 | if (!THIS.frameDiv) {
|
---|
2401 | THIS.frameDiv = document.createElement("div");
|
---|
2402 | THIS.frameDiv.id = THIS.dragElId;
|
---|
2403 | var s = THIS.frameDiv.style;
|
---|
2404 | s.position = "absolute";
|
---|
2405 | s.visibility = "hidden";
|
---|
2406 | s.cursor = "move";
|
---|
2407 | s.border = "2px solid #aaa";
|
---|
2408 | s.zIndex = 999;
|
---|
2409 |
|
---|
2410 | document.body.appendChild(THIS.frameDiv);
|
---|
2411 | }
|
---|
2412 | };
|
---|
2413 |
|
---|
2414 | /**
|
---|
2415 | * Initialization for the drag frame element. Must be called in the
|
---|
2416 | * constructor of all subclasses
|
---|
2417 | */
|
---|
2418 | YAHOO.util.DDProxy.prototype.initFrame = function() {
|
---|
2419 | YAHOO.util.DDProxy.createFrame();
|
---|
2420 | this.setDragElId(YAHOO.util.DDProxy.dragElId);
|
---|
2421 | this.useAbsMath = true;
|
---|
2422 | };
|
---|
2423 |
|
---|
2424 | /**
|
---|
2425 | * Resizes the drag frame to the dimensions of the clicked object, positions
|
---|
2426 | * it over the object, and finally displays it
|
---|
2427 | *
|
---|
2428 | * @param {int} iPageX X click position
|
---|
2429 | * @param {int} iPageY Y click position
|
---|
2430 | * @private
|
---|
2431 | */
|
---|
2432 | YAHOO.util.DDProxy.prototype.showFrame = function(iPageX, iPageY) {
|
---|
2433 | var el = this.getEl();
|
---|
2434 |
|
---|
2435 | var s = this.getDragEl().style;
|
---|
2436 |
|
---|
2437 | if (this.resizeFrame) {
|
---|
2438 | s.width = (parseInt(el.offsetWidth) - (2*this.borderWidth)) + "px";
|
---|
2439 | s.height = (parseInt(el.offsetHeight) - (2*this.borderWidth)) + "px";
|
---|
2440 | }
|
---|
2441 |
|
---|
2442 | if (this.centerFrame) {
|
---|
2443 | this.setDelta(Math.round(parseInt(s.width)/2),
|
---|
2444 | Math.round(parseInt(s.width)/2));
|
---|
2445 | }
|
---|
2446 |
|
---|
2447 | this.setDragElPos(iPageX, iPageY);
|
---|
2448 |
|
---|
2449 | s.visibility = "";
|
---|
2450 | };
|
---|
2451 |
|
---|
2452 | // overrides YAHOO.util.DragDrop
|
---|
2453 | YAHOO.util.DDProxy.prototype.b4MouseDown = function(e) {
|
---|
2454 | var x = YAHOO.util.Event.getPageX(e);
|
---|
2455 | var y = YAHOO.util.Event.getPageY(e);
|
---|
2456 | this.autoOffset(x, y);
|
---|
2457 | this.setDragElPos(x, y);
|
---|
2458 | };
|
---|
2459 |
|
---|
2460 | // overrides YAHOO.util.DragDrop
|
---|
2461 | YAHOO.util.DDProxy.prototype.b4StartDrag = function(x, y) {
|
---|
2462 | // show the drag frame
|
---|
2463 | this.logger.debug("start drag show frame, x: " + x + ", y: " + y);
|
---|
2464 | this.showFrame(x, y);
|
---|
2465 | };
|
---|
2466 |
|
---|
2467 | // overrides YAHOO.util.DragDrop
|
---|
2468 | YAHOO.util.DDProxy.prototype.b4EndDrag = function(e) {
|
---|
2469 | this.logger.debug(this.id + " b4EndDrag");
|
---|
2470 |
|
---|
2471 | // hide the drag frame
|
---|
2472 | var s = this.getDragEl().style;
|
---|
2473 | s.visibility = "hidden";
|
---|
2474 | };
|
---|
2475 |
|
---|
2476 | // overrides YAHOO.util.DragDrop
|
---|
2477 | // By default we try to move the element to the last location of the frame.
|
---|
2478 | // This is so that the default behavior mirrors that of YAHOO.util.DD.
|
---|
2479 | YAHOO.util.DDProxy.prototype.endDrag = function(e) {
|
---|
2480 | this.logger.debug(this.id + " endDrag");
|
---|
2481 | var lel = this.getEl();
|
---|
2482 | var del = this.getDragEl();
|
---|
2483 |
|
---|
2484 | // Show the drag frame briefly so we can get its position
|
---|
2485 | del.style.visibility = "";
|
---|
2486 |
|
---|
2487 | // Hide the linked element before the move to get around a Safari
|
---|
2488 | // rendering bug.
|
---|
2489 | lel.style.visibility = "hidden";
|
---|
2490 | YAHOO.util.DDM.moveToEl(lel, del);
|
---|
2491 | del.style.visibility = "hidden";
|
---|
2492 | lel.style.visibility = "";
|
---|
2493 | };
|
---|
2494 |
|
---|
2495 | /* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
|
---|
2496 |
|
---|
2497 | /**
|
---|
2498 | * @class a DragDrop implementation that does not move, but can be a drop
|
---|
2499 | * target. You would get the same result by simply omitting implementation
|
---|
2500 | * for the event callbacks, but this way we reduce the processing cost of the
|
---|
2501 | * event listener and the callbacks.
|
---|
2502 | *
|
---|
2503 | * @extends YAHOO.util.DragDrop
|
---|
2504 | * @constructor
|
---|
2505 | * @param {String} id the id of the element that is a drop target
|
---|
2506 | * @param {String} sGroup the group of related DragDrop objects
|
---|
2507 | */
|
---|
2508 |
|
---|
2509 | YAHOO.util.DDTarget = function(id, sGroup) {
|
---|
2510 | if (id) {
|
---|
2511 | this.initTarget(id, sGroup);
|
---|
2512 | this.logger.setModuleName("DDTarget");
|
---|
2513 | }
|
---|
2514 | };
|
---|
2515 |
|
---|
2516 | YAHOO.util.DDTarget.prototype = new YAHOO.util.DragDrop();
|
---|
2517 |
|
---|