| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A DragListGroup is a class representing a group of one or more |
| 17 |
* "drag lists" with items that can be dragged within them and between them. |
| 18 |
* |
| 19 |
* @see ../demos/draglistgroup.html |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
goog.provide('goog.fx.DragListDirection'); |
| 24 |
goog.provide('goog.fx.DragListGroup'); |
| 25 |
goog.provide('goog.fx.DragListGroup.EventType'); |
| 26 |
goog.provide('goog.fx.DragListGroupEvent'); |
| 27 |
|
| 28 |
goog.require('goog.array'); |
| 29 |
goog.require('goog.asserts'); |
| 30 |
goog.require('goog.dom'); |
| 31 |
goog.require('goog.dom.classlist'); |
| 32 |
goog.require('goog.events'); |
| 33 |
goog.require('goog.events.Event'); |
| 34 |
goog.require('goog.events.EventHandler'); |
| 35 |
goog.require('goog.events.EventId'); |
| 36 |
goog.require('goog.events.EventTarget'); |
| 37 |
goog.require('goog.events.EventType'); |
| 38 |
goog.require('goog.fx.Dragger'); |
| 39 |
goog.require('goog.math.Coordinate'); |
| 40 |
goog.require('goog.string'); |
| 41 |
goog.require('goog.style'); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* A class representing a group of one or more "drag lists" with items that can |
| 47 |
* be dragged within them and between them. |
| 48 |
* |
| 49 |
* Example usage: |
| 50 |
* var dragListGroup = new goog.fx.DragListGroup(); |
| 51 |
* dragListGroup.setDragItemHandleHoverClass(className1, className2); |
| 52 |
* dragListGroup.setDraggerElClass(className3); |
| 53 |
* dragListGroup.addDragList(vertList, goog.fx.DragListDirection.DOWN); |
| 54 |
* dragListGroup.addDragList(horizList, goog.fx.DragListDirection.RIGHT); |
| 55 |
* dragListGroup.init(); |
| 56 |
* |
| 57 |
* @extends {goog.events.EventTarget} |
| 58 |
* @constructor |
| 59 |
* @struct |
| 60 |
*/ |
| 61 |
goog.fx.DragListGroup = function() { |
| 62 |
goog.fx.DragListGroup.base(this, 'constructor'); |
| 63 |
|
| 64 |
/** |
| 65 |
* The user-supplied CSS classes to add to a drag item on hover (not during a |
| 66 |
* drag action). |
| 67 |
* @private {Array|undefined} |
| 68 |
*/ |
| 69 |
this.dragItemHoverClasses_; |
| 70 |
|
| 71 |
/** |
| 72 |
* The user-supplied CSS classes to add to a drag item handle on hover (not |
| 73 |
* during a drag action). |
| 74 |
* @private {Array|undefined} |
| 75 |
*/ |
| 76 |
this.dragItemHandleHoverClasses_; |
| 77 |
|
| 78 |
/** |
| 79 |
* The user-supplied CSS classes to add to the current drag item (during a |
| 80 |
* drag action). |
| 81 |
* @private {Array|undefined} |
| 82 |
*/ |
| 83 |
this.currDragItemClasses_; |
| 84 |
|
| 85 |
/** |
| 86 |
* The user-supplied CSS classes to add to the clone of the current drag item |
| 87 |
* that's actually being dragged around (during a drag action). |
| 88 |
* @private {Array<string>|undefined} |
| 89 |
*/ |
| 90 |
this.draggerElClasses_; |
| 91 |
|
| 92 |
/** |
| 93 |
* The current drag item being moved. |
| 94 |
* Note: This is only defined while a drag action is happening. |
| 95 |
* @private {Element} |
| 96 |
*/ |
| 97 |
this.currDragItem_; |
| 98 |
|
| 99 |
/** |
| 100 |
* The drag list that {@code this.currDragItem_} is currently hovering over, |
| 101 |
* or null if it is not hovering over a list. |
| 102 |
* @private {Element} |
| 103 |
*/ |
| 104 |
this.currHoverList_; |
| 105 |
|
| 106 |
/** |
| 107 |
* The original drag list that the current drag item came from. We need to |
| 108 |
* remember this in case the user drops the item outside of any lists, in |
| 109 |
* which case we return the item to its original location. |
| 110 |
* Note: This is only defined while a drag action is happening. |
| 111 |
* @private {Element} |
| 112 |
*/ |
| 113 |
this.origList_; |
| 114 |
|
| 115 |
/** |
| 116 |
* The original next item in the original list that the current drag item came |
| 117 |
* from. We need to remember this in case the user drops the item outside of |
| 118 |
* any lists, in which case we return the item to its original location. |
| 119 |
* Note: This is only defined while a drag action is happening. |
| 120 |
* @private {Element} |
| 121 |
*/ |
| 122 |
this.origNextItem_; |
| 123 |
|
| 124 |
/** |
| 125 |
* The current item in the list we are hovering over. We need to remember |
| 126 |
* this in case we do not update the position of the current drag item while |
| 127 |
* dragging (see {@code updateWhileDragging_}). In this case the current drag |
| 128 |
* item will be inserted into the list before this element when the drag ends. |
| 129 |
* @private {Element} |
| 130 |
*/ |
| 131 |
this.currHoverItem_; |
| 132 |
|
| 133 |
/** |
| 134 |
* The clone of the current drag item that's actually being dragged around. |
| 135 |
* Note: This is only defined while a drag action is happening. |
| 136 |
* @private {HTMLElement} |
| 137 |
*/ |
| 138 |
this.draggerEl_; |
| 139 |
|
| 140 |
/** |
| 141 |
* The dragger object. |
| 142 |
* Note: This is only defined while a drag action is happening. |
| 143 |
* @private {goog.fx.Dragger} |
| 144 |
*/ |
| 145 |
this.dragger_; |
| 146 |
|
| 147 |
/** |
| 148 |
* The amount of distance, in pixels, after which a mousedown or touchstart is |
| 149 |
* considered a drag. |
| 150 |
* @private {number} |
| 151 |
*/ |
| 152 |
this.hysteresisDistance_ = 0; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* The drag lists. |
| 157 |
* @private {Array<Element>} |
| 158 |
*/ |
| 159 |
this.dragLists_ = []; |
| 160 |
|
| 161 |
/** |
| 162 |
* All the drag items. Set by init(). |
| 163 |
* @private {Array<Element>} |
| 164 |
*/ |
| 165 |
this.dragItems_ = []; |
| 166 |
|
| 167 |
/** |
| 168 |
* Which drag item corresponds to a given handle. Set by init(). |
| 169 |
* Specifically, this maps from the unique ID (as given by goog.getUid) |
| 170 |
* of the handle to the drag item. |
| 171 |
* @private {Object} |
| 172 |
*/ |
| 173 |
this.dragItemForHandle_ = {}; |
| 174 |
|
| 175 |
/** |
| 176 |
* The event handler for this instance. |
| 177 |
* @private {goog.events.EventHandler<!goog.fx.DragListGroup>} |
| 178 |
*/ |
| 179 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 180 |
|
| 181 |
/** |
| 182 |
* Whether the setup has been done to make all items in all lists draggable. |
| 183 |
* @private {boolean} |
| 184 |
*/ |
| 185 |
this.isInitialized_ = false; |
| 186 |
|
| 187 |
/** |
| 188 |
* Whether the currDragItem is always displayed. By default the list |
| 189 |
* collapses, the currDragItem's display is set to none, when we do not |
| 190 |
* hover over a draglist. |
| 191 |
* @private {boolean} |
| 192 |
*/ |
| 193 |
this.isCurrDragItemAlwaysDisplayed_ = false; |
| 194 |
|
| 195 |
/** |
| 196 |
* Whether to update the position of the currDragItem as we drag, i.e., |
| 197 |
* insert the currDragItem each time to the position where it would land if |
| 198 |
* we were to end the drag at that point. Defaults to true. |
| 199 |
* @private {boolean} |
| 200 |
*/ |
| 201 |
this.updateWhileDragging_ = true; |
| 202 |
}; |
| 203 |
goog.inherits(goog.fx.DragListGroup, goog.events.EventTarget); |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Enum to indicate the direction that a drag list grows. |
| 208 |
* @enum {number} |
| 209 |
*/ |
| 210 |
goog.fx.DragListDirection = { |
| 211 |
DOWN: 0, // common |
| 212 |
RIGHT: 2, // common |
| 213 |
LEFT: 3, // uncommon (except perhaps for right-to-left interfaces) |
| 214 |
RIGHT_2D: 4, // common + handles multiple lines if items are wrapped |
| 215 |
LEFT_2D: 5 // for rtl languages |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Events dispatched by this class. |
| 221 |
* @enum {!goog.events.EventId<!goog.fx.DragListGroupEvent>} |
| 222 |
*/ |
| 223 |
goog.fx.DragListGroup.EventType = { |
| 224 |
/** |
| 225 |
* Raised on mouse down, when the dragger is first created. Handle this event |
| 226 |
* to customize the dragger element even if the drag never actually starts (if |
| 227 |
* the mouse never moves beyond hysteresis). |
| 228 |
*/ |
| 229 |
DRAGGERCREATED: |
| 230 |
new goog.events.EventId(goog.events.getUniqueId('draggercreated')), |
| 231 |
BEFOREDRAGSTART: new goog.events.EventId('beforedragstart'), |
| 232 |
DRAGSTART: new goog.events.EventId('dragstart'), |
| 233 |
BEFOREDRAGMOVE: new goog.events.EventId('beforedragmove'), |
| 234 |
DRAGMOVE: new goog.events.EventId('dragmove'), |
| 235 |
BEFOREDRAGEND: new goog.events.EventId('beforedragend'), |
| 236 |
/** Raised after the dragged item is moved to the new spot. */ |
| 237 |
DRAGEND: new goog.events.EventId('dragend'), |
| 238 |
/** |
| 239 |
* Raised whenever the dragger element is removed: |
| 240 |
* - When a drag completes successfully. |
| 241 |
* - If the drag never started due to mouseup within hysteresis. |
| 242 |
* - If the drag was cancelled by a BEFORE* event. |
| 243 |
* - If the drag was cancelled due to focus loss. |
| 244 |
*/ |
| 245 |
DRAGGERREMOVED: |
| 246 |
new goog.events.EventId(goog.events.getUniqueId('draggerremoved')) |
| 247 |
}; |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Sets the property of the currDragItem that it is always displayed in the |
| 252 |
* list. |
| 253 |
*/ |
| 254 |
goog.fx.DragListGroup.prototype.setIsCurrDragItemAlwaysDisplayed = function() { |
| 255 |
this.isCurrDragItemAlwaysDisplayed_ = true; |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* Sets the private property updateWhileDragging_ to false. This disables the |
| 261 |
* update of the position of the currDragItem while dragging. It will only be |
| 262 |
* placed to its new location once the drag ends. |
| 263 |
*/ |
| 264 |
goog.fx.DragListGroup.prototype.setNoUpdateWhileDragging = function() { |
| 265 |
this.updateWhileDragging_ = false; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Sets the distance the user has to drag the element before a drag operation |
| 271 |
* is started. |
| 272 |
* @param {number} distance The number of pixels after which a mousedown and |
| 273 |
* move is considered a drag. |
| 274 |
*/ |
| 275 |
goog.fx.DragListGroup.prototype.setHysteresis = function(distance) { |
| 276 |
this.hysteresisDistance_ = distance; |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* @return {number} distance The number of pixels after which a mousedown and |
| 282 |
* move is considered a drag. |
| 283 |
*/ |
| 284 |
goog.fx.DragListGroup.prototype.getHysteresis = function() { |
| 285 |
return this.hysteresisDistance_; |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
/** @return {boolean} true if the user is currently dragging an element. */ |
| 290 |
goog.fx.DragListGroup.prototype.isDragging = function() { |
| 291 |
return !!this.dragger_; |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
/** |
| 296 |
* Adds a drag list to this DragListGroup. |
| 297 |
* All calls to this method must happen before the call to init(). |
| 298 |
* Remember that all child nodes (except text nodes) will be made draggable to |
| 299 |
* any other drag list in this group. |
| 300 |
* |
| 301 |
* @param {Element} dragListElement Must be a container for a list of items |
| 302 |
* that should all be made draggable. |
| 303 |
* @param {goog.fx.DragListDirection} growthDirection The direction that this |
| 304 |
* drag list grows in (i.e. if an item is appended to the DOM, the list's |
| 305 |
* bounding box expands in this direction). |
| 306 |
* @param {boolean=} opt_unused Unused argument. |
| 307 |
* @param {string=} opt_dragHoverClass CSS class to apply to this drag list when |
| 308 |
* the draggerEl hovers over it during a drag action. If present, must be a |
| 309 |
* single, valid classname (not a string of space-separated classnames). |
| 310 |
*/ |
| 311 |
goog.fx.DragListGroup.prototype.addDragList = function( |
| 312 |
dragListElement, growthDirection, opt_unused, opt_dragHoverClass) { |
| 313 |
goog.asserts.assert(!this.isInitialized_); |
| 314 |
|
| 315 |
dragListElement.dlgGrowthDirection_ = growthDirection; |
| 316 |
dragListElement.dlgDragHoverClass_ = opt_dragHoverClass; |
| 317 |
this.dragLists_.push(dragListElement); |
| 318 |
}; |
| 319 |
|
| 320 |
|
| 321 |
/** |
| 322 |
* Sets a user-supplied function used to get the "handle" element for a drag |
| 323 |
* item. The function must accept exactly one argument. The argument may be |
| 324 |
* any drag item element. |
| 325 |
* |
| 326 |
* If not set, the default implementation uses the whole drag item as the |
| 327 |
* handle. |
| 328 |
* |
| 329 |
* @param {function(!Element): Element} getHandleForDragItemFn A function that, |
| 330 |
* given any drag item, returns a reference to its "handle" element |
| 331 |
* (which may be the drag item element itself). |
| 332 |
*/ |
| 333 |
goog.fx.DragListGroup.prototype.setFunctionToGetHandleForDragItem = function( |
| 334 |
getHandleForDragItemFn) { |
| 335 |
goog.asserts.assert(!this.isInitialized_); |
| 336 |
this.getHandleForDragItem_ = getHandleForDragItemFn; |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
/** |
| 341 |
* Sets a user-supplied CSS class to add to a drag item on hover (not during a |
| 342 |
* drag action). |
| 343 |
* @param {...string} var_args The CSS class or classes. |
| 344 |
*/ |
| 345 |
goog.fx.DragListGroup.prototype.setDragItemHoverClass = function(var_args) { |
| 346 |
goog.asserts.assert(!this.isInitialized_); |
| 347 |
this.dragItemHoverClasses_ = goog.array.slice(arguments, 0); |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* Sets a user-supplied CSS class to add to a drag item handle on hover (not |
| 353 |
* during a drag action). |
| 354 |
* @param {...string} var_args The CSS class or classes. |
| 355 |
*/ |
| 356 |
goog.fx.DragListGroup.prototype.setDragItemHandleHoverClass = function( |
| 357 |
var_args) { |
| 358 |
goog.asserts.assert(!this.isInitialized_); |
| 359 |
this.dragItemHandleHoverClasses_ = goog.array.slice(arguments, 0); |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
/** |
| 364 |
* Sets a user-supplied CSS class to add to the current drag item (during a |
| 365 |
* drag action). |
| 366 |
* |
| 367 |
* If not set, the default behavior adds visibility:hidden to the current drag |
| 368 |
* item so that it is a block of empty space in the hover drag list (if any). |
| 369 |
* If this class is set by the user, then the default behavior does not happen |
| 370 |
* (unless, of course, the class also contains visibility:hidden). |
| 371 |
* |
| 372 |
* @param {...string} var_args The CSS class or classes. |
| 373 |
*/ |
| 374 |
goog.fx.DragListGroup.prototype.setCurrDragItemClass = function(var_args) { |
| 375 |
goog.asserts.assert(!this.isInitialized_); |
| 376 |
this.currDragItemClasses_ = goog.array.slice(arguments, 0); |
| 377 |
}; |
| 378 |
|
| 379 |
|
| 380 |
/** |
| 381 |
* Sets a user-supplied CSS class to add to the clone of the current drag item |
| 382 |
* that's actually being dragged around (during a drag action). |
| 383 |
* @param {string} draggerElClass The CSS class. |
| 384 |
*/ |
| 385 |
goog.fx.DragListGroup.prototype.setDraggerElClass = function(draggerElClass) { |
| 386 |
goog.asserts.assert(!this.isInitialized_); |
| 387 |
// Split space-separated classes up into an array. |
| 388 |
this.draggerElClasses_ = goog.string.trim(draggerElClass).split(' '); |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
/** |
| 393 |
* Performs the initial setup to make all items in all lists draggable. |
| 394 |
*/ |
| 395 |
goog.fx.DragListGroup.prototype.init = function() { |
| 396 |
if (this.isInitialized_) { |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
for (var i = 0, numLists = this.dragLists_.length; i < numLists; i++) { |
| 401 |
var dragList = this.dragLists_[i]; |
| 402 |
|
| 403 |
var dragItems = goog.dom.getChildren(dragList); |
| 404 |
for (var j = 0, numItems = dragItems.length; j < numItems; ++j) { |
| 405 |
this.listenForDragEvents(dragItems[j]); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
this.isInitialized_ = true; |
| 410 |
}; |
| 411 |
|
| 412 |
|
| 413 |
/** |
| 414 |
* Adds a single item to the given drag list and sets up the drag listeners for |
| 415 |
* it. |
| 416 |
* If opt_index is specified the item is inserted at this index, otherwise the |
| 417 |
* item is added as the last child of the list. |
| 418 |
* |
| 419 |
* @param {!Element} list The drag list where to add item to. |
| 420 |
* @param {!Element} item The new element to add. |
| 421 |
* @param {number=} opt_index Index where to insert the item in the list. If not |
| 422 |
* specified item is inserted as the last child of list. |
| 423 |
*/ |
| 424 |
goog.fx.DragListGroup.prototype.addItemToDragList = function( |
| 425 |
list, item, opt_index) { |
| 426 |
if (goog.isDef(opt_index)) { |
| 427 |
goog.dom.insertChildAt(list, item, opt_index); |
| 428 |
} else { |
| 429 |
goog.dom.appendChild(list, item); |
| 430 |
} |
| 431 |
this.listenForDragEvents(item); |
| 432 |
}; |
| 433 |
|
| 434 |
|
| 435 |
/** @override */ |
| 436 |
goog.fx.DragListGroup.prototype.disposeInternal = function() { |
| 437 |
this.eventHandler_.dispose(); |
| 438 |
|
| 439 |
for (var i = 0, n = this.dragLists_.length; i < n; i++) { |
| 440 |
var dragList = this.dragLists_[i]; |
| 441 |
// Note: IE doesn't allow 'delete' for fields on HTML elements (because |
| 442 |
// they're not real JS objects in IE), so we just set them to undefined. |
| 443 |
dragList.dlgGrowthDirection_ = undefined; |
| 444 |
dragList.dlgDragHoverClass_ = undefined; |
| 445 |
} |
| 446 |
|
| 447 |
this.dragLists_.length = 0; |
| 448 |
this.dragItems_.length = 0; |
| 449 |
this.dragItemForHandle_ = null; |
| 450 |
|
| 451 |
// In the case where a drag event is currently in-progress and dispose is |
| 452 |
// called, this cleans up the extra state. |
| 453 |
this.cleanupDragDom_(); |
| 454 |
|
| 455 |
goog.fx.DragListGroup.superClass_.disposeInternal.call(this); |
| 456 |
}; |
| 457 |
|
| 458 |
|
| 459 |
/** |
| 460 |
* Caches the heights of each drag list and drag item, except for the current |
| 461 |
* drag item. |
| 462 |
* |
| 463 |
*/ |
| 464 |
goog.fx.DragListGroup.prototype.recacheListAndItemBounds = function() { |
| 465 |
this.recacheListAndItemBounds_(this.currDragItem_); |
| 466 |
}; |
| 467 |
|
| 468 |
|
| 469 |
/** |
| 470 |
* Caches the heights of each drag list and drag item, except for the current |
| 471 |
* drag item. |
| 472 |
* |
| 473 |
* @param {Element} currDragItem The item currently being dragged. |
| 474 |
* @private |
| 475 |
*/ |
| 476 |
goog.fx.DragListGroup.prototype.recacheListAndItemBounds_ = function( |
| 477 |
currDragItem) { |
| 478 |
for (var i = 0, n = this.dragLists_.length; i < n; i++) { |
| 479 |
var dragList = this.dragLists_[i]; |
| 480 |
dragList.dlgBounds_ = goog.style.getBounds(dragList); |
| 481 |
} |
| 482 |
|
| 483 |
for (var i = 0, n = this.dragItems_.length; i < n; i++) { |
| 484 |
var dragItem = this.dragItems_[i]; |
| 485 |
if (dragItem != currDragItem) { |
| 486 |
dragItem.dlgBounds_ = goog.style.getBounds(dragItem); |
| 487 |
} |
| 488 |
} |
| 489 |
}; |
| 490 |
|
| 491 |
|
| 492 |
/** |
| 493 |
* Listens for drag events on the given drag item. This method is currently used |
| 494 |
* to initialize drag items. |
| 495 |
* |
| 496 |
* @param {!Element} dragItem the element to initialize. This element has to be |
| 497 |
* in one of the drag lists. |
| 498 |
* @protected |
| 499 |
*/ |
| 500 |
goog.fx.DragListGroup.prototype.listenForDragEvents = function(dragItem) { |
| 501 |
var dragItemHandle = this.getHandleForDragItem_(dragItem); |
| 502 |
var uid = goog.getUid(dragItemHandle); |
| 503 |
this.dragItemForHandle_[uid] = dragItem; |
| 504 |
|
| 505 |
if (this.dragItemHoverClasses_) { |
| 506 |
this.eventHandler_.listen( |
| 507 |
dragItem, goog.events.EventType.MOUSEOVER, |
| 508 |
this.handleDragItemMouseover_); |
| 509 |
this.eventHandler_.listen( |
| 510 |
dragItem, goog.events.EventType.MOUSEOUT, this.handleDragItemMouseout_); |
| 511 |
} |
| 512 |
if (this.dragItemHandleHoverClasses_) { |
| 513 |
this.eventHandler_.listen( |
| 514 |
dragItemHandle, goog.events.EventType.MOUSEOVER, |
| 515 |
this.handleDragItemHandleMouseover_); |
| 516 |
this.eventHandler_.listen( |
| 517 |
dragItemHandle, goog.events.EventType.MOUSEOUT, |
| 518 |
this.handleDragItemHandleMouseout_); |
| 519 |
} |
| 520 |
|
| 521 |
this.dragItems_.push(dragItem); |
| 522 |
this.eventHandler_.listen( |
| 523 |
dragItemHandle, |
| 524 |
[goog.events.EventType.MOUSEDOWN, goog.events.EventType.TOUCHSTART], |
| 525 |
this.handlePotentialDragStart_); |
| 526 |
}; |
| 527 |
|
| 528 |
|
| 529 |
/** |
| 530 |
* Handles mouse and touch events which may start a drag action. |
| 531 |
* @param {!goog.events.BrowserEvent} e MOUSEDOWN or TOUCHSTART event. |
| 532 |
* @private |
| 533 |
*/ |
| 534 |
goog.fx.DragListGroup.prototype.handlePotentialDragStart_ = function(e) { |
| 535 |
var uid = goog.getUid(/** @type {Node} */ (e.currentTarget)); |
| 536 |
this.currDragItem_ = /** @type {Element} */ (this.dragItemForHandle_[uid]); |
| 537 |
|
| 538 |
this.draggerEl_ = /** @type {!HTMLElement} */ ( |
| 539 |
this.createDragElementInternal(this.currDragItem_)); |
| 540 |
if (this.draggerElClasses_) { |
| 541 |
// Add CSS class for the clone, if any. |
| 542 |
goog.dom.classlist.addAll( |
| 543 |
goog.asserts.assert(this.draggerEl_), this.draggerElClasses_ || []); |
| 544 |
} |
| 545 |
|
| 546 |
// Place the clone (i.e. draggerEl) at the same position as the actual |
| 547 |
// current drag item. This is a bit tricky since |
| 548 |
// goog.style.getPageOffset() gets the left-top pos of the border, but |
| 549 |
// goog.style.setPageOffset() sets the left-top pos of the margin. |
| 550 |
// It's difficult to adjust for the margins of the clone because it's |
| 551 |
// difficult to read it: goog.style.getComputedStyle() doesn't work for IE. |
| 552 |
// Instead, our workaround is simply to set the clone's margins to 0px. |
| 553 |
this.draggerEl_.style.margin = '0'; |
| 554 |
this.draggerEl_.style.position = 'absolute'; |
| 555 |
this.draggerEl_.style.visibility = 'hidden'; |
| 556 |
var doc = goog.dom.getOwnerDocument(this.currDragItem_); |
| 557 |
doc.body.appendChild(this.draggerEl_); |
| 558 |
|
| 559 |
// Important: goog.style.setPageOffset() only works correctly for IE when the |
| 560 |
// element is already in the document. |
| 561 |
var currDragItemPos = goog.style.getPageOffset(this.currDragItem_); |
| 562 |
goog.style.setPageOffset(this.draggerEl_, currDragItemPos); |
| 563 |
|
| 564 |
this.dragger_ = new goog.fx.Dragger(this.draggerEl_); |
| 565 |
this.dragger_.setHysteresis(this.hysteresisDistance_); |
| 566 |
|
| 567 |
// Listen to events on the dragger. These handlers will be unregistered at |
| 568 |
// DRAGEND, when the dragger is disposed of. We can't use eventHandler_, |
| 569 |
// because it creates new references to the handler functions at each |
| 570 |
// dragging action, and keeps them until DragListGroup is disposed of. |
| 571 |
goog.events.listen( |
| 572 |
this.dragger_, goog.fx.Dragger.EventType.START, this.handleDragStart_, |
| 573 |
false, this); |
| 574 |
goog.events.listen( |
| 575 |
this.dragger_, goog.fx.Dragger.EventType.END, this.handleDragEnd_, false, |
| 576 |
this); |
| 577 |
goog.events.listen( |
| 578 |
this.dragger_, goog.fx.Dragger.EventType.EARLY_CANCEL, this.cleanup_, |
| 579 |
false, this); |
| 580 |
this.dispatchEvent(new goog.fx.DragListGroupEvent( |
| 581 |
goog.fx.DragListGroup.EventType.DRAGGERCREATED, this, e, |
| 582 |
this.currDragItem_, this.draggerEl_, this.dragger_)); |
| 583 |
this.dragger_.startDrag(e); |
| 584 |
}; |
| 585 |
|
| 586 |
|
| 587 |
/** |
| 588 |
* Creates copy of node being dragged. |
| 589 |
* |
| 590 |
* @param {Element} sourceEl Element to copy. |
| 591 |
* @return {!Element} The clone of {@code sourceEl}. |
| 592 |
* @deprecated Use goog.fx.Dragger.cloneNode(). |
| 593 |
* @private |
| 594 |
*/ |
| 595 |
goog.fx.DragListGroup.prototype.cloneNode_ = function(sourceEl) { |
| 596 |
return goog.fx.Dragger.cloneNode(sourceEl); |
| 597 |
}; |
| 598 |
|
| 599 |
|
| 600 |
/** |
| 601 |
* Generates an element to follow the cursor during dragging, given a drag |
| 602 |
* source element. The default behavior is simply to clone the source element, |
| 603 |
* but this may be overridden in subclasses. This method is called by |
| 604 |
* {@code createDragElement()} before the drag class is added. |
| 605 |
* |
| 606 |
* @param {Element} sourceEl Drag source element. |
| 607 |
* @return {!Element} The new drag element. |
| 608 |
* @protected |
| 609 |
* @suppress {deprecated} |
| 610 |
*/ |
| 611 |
goog.fx.DragListGroup.prototype.createDragElementInternal = function(sourceEl) { |
| 612 |
return this.cloneNode_(sourceEl); |
| 613 |
}; |
| 614 |
|
| 615 |
|
| 616 |
/** |
| 617 |
* Handles the start of a drag action. |
| 618 |
* @param {!goog.fx.DragEvent} e goog.fx.Dragger.EventType.START event. |
| 619 |
* @private |
| 620 |
*/ |
| 621 |
goog.fx.DragListGroup.prototype.handleDragStart_ = function(e) { |
| 622 |
if (!this.dispatchEvent( |
| 623 |
new goog.fx.DragListGroupEvent( |
| 624 |
goog.fx.DragListGroup.EventType.BEFOREDRAGSTART, this, |
| 625 |
e.browserEvent, this.currDragItem_, null, null))) { |
| 626 |
e.preventDefault(); |
| 627 |
this.cleanup_(); |
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
// Record the original location of the current drag item. |
| 632 |
// Note: this.origNextItem_ may be null. |
| 633 |
this.origList_ = /** @type {Element} */ (this.currDragItem_.parentNode); |
| 634 |
this.origNextItem_ = goog.dom.getNextElementSibling(this.currDragItem_); |
| 635 |
this.currHoverItem_ = this.origNextItem_; |
| 636 |
this.currHoverList_ = this.origList_; |
| 637 |
|
| 638 |
// If there's a CSS class specified for the current drag item, add it. |
| 639 |
// Otherwise, make the actual current drag item hidden (takes up space). |
| 640 |
if (this.currDragItemClasses_) { |
| 641 |
goog.dom.classlist.addAll( |
| 642 |
goog.asserts.assert(this.currDragItem_), |
| 643 |
this.currDragItemClasses_ || []); |
| 644 |
} else { |
| 645 |
this.currDragItem_.style.visibility = 'hidden'; |
| 646 |
} |
| 647 |
|
| 648 |
// Precompute distances from top-left corner to center for efficiency. |
| 649 |
var draggerElSize = goog.style.getSize(this.draggerEl_); |
| 650 |
this.draggerEl_.halfWidth = draggerElSize.width / 2; |
| 651 |
this.draggerEl_.halfHeight = draggerElSize.height / 2; |
| 652 |
|
| 653 |
this.draggerEl_.style.visibility = ''; |
| 654 |
|
| 655 |
// Record the bounds of all the drag lists and all the other drag items. This |
| 656 |
// caching is for efficiency, so that we don't have to recompute the bounds on |
| 657 |
// each drag move. Do this in the state where the current drag item is not in |
| 658 |
// any of the lists, except when update while dragging is disabled, as in this |
| 659 |
// case the current drag item does not get removed until drag ends. |
| 660 |
if (this.updateWhileDragging_) { |
| 661 |
this.currDragItem_.style.display = 'none'; |
| 662 |
} |
| 663 |
this.recacheListAndItemBounds_(this.currDragItem_); |
| 664 |
this.currDragItem_.style.display = ''; |
| 665 |
|
| 666 |
// Listen to events on the dragger. |
| 667 |
goog.events.listen( |
| 668 |
this.dragger_, goog.fx.Dragger.EventType.DRAG, this.handleDragMove_, |
| 669 |
false, this); |
| 670 |
|
| 671 |
this.dispatchEvent( |
| 672 |
new goog.fx.DragListGroupEvent( |
| 673 |
goog.fx.DragListGroup.EventType.DRAGSTART, this, e.browserEvent, |
| 674 |
this.currDragItem_, this.draggerEl_, this.dragger_)); |
| 675 |
}; |
| 676 |
|
| 677 |
|
| 678 |
/** |
| 679 |
* Handles a drag movement (i.e. DRAG event fired by the dragger). |
| 680 |
* |
| 681 |
* @param {goog.fx.DragEvent} dragEvent Event object fired by the dragger. |
| 682 |
* @return {boolean} The return value for the event. |
| 683 |
* @private |
| 684 |
*/ |
| 685 |
goog.fx.DragListGroup.prototype.handleDragMove_ = function(dragEvent) { |
| 686 |
|
| 687 |
// Compute the center of the dragger element (i.e. the cloned drag item). |
| 688 |
var draggerElPos = goog.style.getPageOffset(this.draggerEl_); |
| 689 |
var draggerElCenter = new goog.math.Coordinate( |
| 690 |
draggerElPos.x + this.draggerEl_.halfWidth, |
| 691 |
draggerElPos.y + this.draggerEl_.halfHeight); |
| 692 |
|
| 693 |
// Check whether the center is hovering over one of the drag lists. |
| 694 |
var hoverList = this.getHoverDragList_(draggerElCenter); |
| 695 |
|
| 696 |
// If hovering over a list, find the next item (if drag were to end now). |
| 697 |
var hoverNextItem = |
| 698 |
hoverList ? this.getHoverNextItem_(hoverList, draggerElCenter) : null; |
| 699 |
|
| 700 |
var rv = this.dispatchEvent( |
| 701 |
new goog.fx.DragListGroupEvent( |
| 702 |
goog.fx.DragListGroup.EventType.BEFOREDRAGMOVE, this, dragEvent, |
| 703 |
this.currDragItem_, this.draggerEl_, this.dragger_, draggerElCenter, |
| 704 |
hoverList, hoverNextItem)); |
| 705 |
if (!rv) { |
| 706 |
return false; |
| 707 |
} |
| 708 |
|
| 709 |
if (hoverList) { |
| 710 |
if (this.updateWhileDragging_) { |
| 711 |
this.insertCurrDragItem_(hoverList, hoverNextItem); |
| 712 |
} else { |
| 713 |
// If update while dragging is disabled do not insert |
| 714 |
// the dragged item, but update the hovered item instead. |
| 715 |
this.updateCurrHoverItem(hoverNextItem, draggerElCenter); |
| 716 |
} |
| 717 |
this.currDragItem_.style.display = ''; |
| 718 |
// Add drag list's hover class (if any). |
| 719 |
if (hoverList.dlgDragHoverClass_) { |
| 720 |
goog.dom.classlist.add( |
| 721 |
goog.asserts.assert(hoverList), hoverList.dlgDragHoverClass_); |
| 722 |
} |
| 723 |
|
| 724 |
} else { |
| 725 |
// Not hovering over a drag list, so remove the item altogether unless |
| 726 |
// specified otherwise by the user. |
| 727 |
if (!this.isCurrDragItemAlwaysDisplayed_) { |
| 728 |
this.currDragItem_.style.display = 'none'; |
| 729 |
} |
| 730 |
|
| 731 |
// Remove hover classes (if any) from all drag lists. |
| 732 |
for (var i = 0, n = this.dragLists_.length; i < n; i++) { |
| 733 |
var dragList = this.dragLists_[i]; |
| 734 |
if (dragList.dlgDragHoverClass_) { |
| 735 |
goog.dom.classlist.remove( |
| 736 |
goog.asserts.assert(dragList), dragList.dlgDragHoverClass_); |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
// If the current hover list is different than the last, the lists may have |
| 742 |
// shrunk, so we should recache the bounds. |
| 743 |
if (hoverList != this.currHoverList_) { |
| 744 |
this.currHoverList_ = hoverList; |
| 745 |
this.recacheListAndItemBounds_(this.currDragItem_); |
| 746 |
} |
| 747 |
|
| 748 |
this.dispatchEvent( |
| 749 |
new goog.fx.DragListGroupEvent( |
| 750 |
goog.fx.DragListGroup.EventType.DRAGMOVE, this, dragEvent, |
| 751 |
/** @type {Element} */ (this.currDragItem_), this.draggerEl_, |
| 752 |
this.dragger_, draggerElCenter, hoverList, hoverNextItem)); |
| 753 |
|
| 754 |
// Return false to prevent selection due to mouse drag. |
| 755 |
return false; |
| 756 |
}; |
| 757 |
|
| 758 |
|
| 759 |
/** |
| 760 |
* Clear all our temporary fields that are only defined while dragging, and |
| 761 |
* all the bounds info stored on the drag lists and drag elements. |
| 762 |
* @param {!goog.events.Event=} opt_e EARLY_CANCEL event from the dragger if |
| 763 |
* cleanup_ was called as an event handler. |
| 764 |
* @private |
| 765 |
*/ |
| 766 |
goog.fx.DragListGroup.prototype.cleanup_ = function(opt_e) { |
| 767 |
this.cleanupDragDom_(); |
| 768 |
|
| 769 |
this.currDragItem_ = null; |
| 770 |
this.currHoverList_ = null; |
| 771 |
this.origList_ = null; |
| 772 |
this.origNextItem_ = null; |
| 773 |
this.draggerEl_ = null; |
| 774 |
this.dragger_ = null; |
| 775 |
|
| 776 |
// Note: IE doesn't allow 'delete' for fields on HTML elements (because |
| 777 |
// they're not real JS objects in IE), so we just set them to null. |
| 778 |
for (var i = 0, n = this.dragLists_.length; i < n; i++) { |
| 779 |
this.dragLists_[i].dlgBounds_ = null; |
| 780 |
} |
| 781 |
for (var i = 0, n = this.dragItems_.length; i < n; i++) { |
| 782 |
this.dragItems_[i].dlgBounds_ = null; |
| 783 |
} |
| 784 |
}; |
| 785 |
|
| 786 |
|
| 787 |
/** |
| 788 |
* Handles the end or the cancellation of a drag action, i.e. END or CLEANUP |
| 789 |
* event fired by the dragger. |
| 790 |
* |
| 791 |
* @param {!goog.fx.DragEvent} dragEvent Event object fired by the dragger. |
| 792 |
* @return {boolean} Whether the event was handled. |
| 793 |
* @private |
| 794 |
*/ |
| 795 |
goog.fx.DragListGroup.prototype.handleDragEnd_ = function(dragEvent) { |
| 796 |
var rv = this.dispatchEvent( |
| 797 |
new goog.fx.DragListGroupEvent( |
| 798 |
goog.fx.DragListGroup.EventType.BEFOREDRAGEND, this, dragEvent, |
| 799 |
/** @type {Element} */ (this.currDragItem_), this.draggerEl_, |
| 800 |
this.dragger_)); |
| 801 |
if (!rv) { |
| 802 |
return false; |
| 803 |
} |
| 804 |
|
| 805 |
// If update while dragging is disabled insert the current drag item into |
| 806 |
// its intended location. |
| 807 |
if (!this.updateWhileDragging_) { |
| 808 |
this.insertCurrHoverItem(); |
| 809 |
} |
| 810 |
|
| 811 |
// The DRAGEND handler may need the new order of the list items. Clean up the |
| 812 |
// garbage. |
| 813 |
// TODO(user): Regression test. |
| 814 |
this.cleanupDragDom_(); |
| 815 |
|
| 816 |
this.dispatchEvent( |
| 817 |
new goog.fx.DragListGroupEvent( |
| 818 |
goog.fx.DragListGroup.EventType.DRAGEND, this, dragEvent, |
| 819 |
this.currDragItem_, this.draggerEl_, this.dragger_)); |
| 820 |
|
| 821 |
this.cleanup_(); |
| 822 |
|
| 823 |
return true; |
| 824 |
}; |
| 825 |
|
| 826 |
|
| 827 |
/** |
| 828 |
* Cleans up DOM changes that are made by the {@code handleDrag*} methods. |
| 829 |
* @private |
| 830 |
*/ |
| 831 |
goog.fx.DragListGroup.prototype.cleanupDragDom_ = function() { |
| 832 |
// Disposes of the dragger and remove the cloned drag item. |
| 833 |
goog.dispose(this.dragger_); |
| 834 |
var hadDragger = this.draggerEl_ && this.draggerEl_.parentElement; |
| 835 |
if (this.draggerEl_) { |
| 836 |
goog.dom.removeNode(this.draggerEl_); |
| 837 |
} |
| 838 |
|
| 839 |
// If the current drag item is not in any list, put it back in its original |
| 840 |
// location. |
| 841 |
if (this.currDragItem_ && this.currDragItem_.style.display == 'none') { |
| 842 |
// Note: this.origNextItem_ may be null, but insertBefore() still works. |
| 843 |
this.origList_.insertBefore(this.currDragItem_, this.origNextItem_); |
| 844 |
this.currDragItem_.style.display = ''; |
| 845 |
} |
| 846 |
|
| 847 |
// If there's a CSS class specified for the current drag item, remove it. |
| 848 |
// Otherwise, make the current drag item visible (instead of empty space). |
| 849 |
if (this.currDragItemClasses_ && this.currDragItem_) { |
| 850 |
goog.dom.classlist.removeAll( |
| 851 |
goog.asserts.assert(this.currDragItem_), |
| 852 |
this.currDragItemClasses_ || []); |
| 853 |
} else if (this.currDragItem_) { |
| 854 |
this.currDragItem_.style.visibility = ''; |
| 855 |
} |
| 856 |
|
| 857 |
// Remove hover classes (if any) from all drag lists. |
| 858 |
for (var i = 0, n = this.dragLists_.length; i < n; i++) { |
| 859 |
var dragList = this.dragLists_[i]; |
| 860 |
if (dragList.dlgDragHoverClass_) { |
| 861 |
goog.dom.classlist.remove( |
| 862 |
goog.asserts.assert(dragList), dragList.dlgDragHoverClass_); |
| 863 |
} |
| 864 |
} |
| 865 |
if (hadDragger) { |
| 866 |
this.dispatchEvent(new goog.fx.DragListGroupEvent( |
| 867 |
goog.fx.DragListGroup.EventType.DRAGGERREMOVED, this, null, |
| 868 |
this.currDragItem_, this.draggerEl_, this.dragger_)); |
| 869 |
} |
| 870 |
}; |
| 871 |
|
| 872 |
|
| 873 |
/** |
| 874 |
* Default implementation of the function to get the "handle" element for a |
| 875 |
* drag item. By default, we use the whole drag item as the handle. Users can |
| 876 |
* change this by calling setFunctionToGetHandleForDragItem(). |
| 877 |
* |
| 878 |
* @param {!Element} dragItem The drag item to get the handle for. |
| 879 |
* @return {Element} The dragItem element itself. |
| 880 |
* @private |
| 881 |
*/ |
| 882 |
goog.fx.DragListGroup.prototype.getHandleForDragItem_ = function(dragItem) { |
| 883 |
return dragItem; |
| 884 |
}; |
| 885 |
|
| 886 |
|
| 887 |
/** |
| 888 |
* Handles a MOUSEOVER event fired on a drag item. |
| 889 |
* @param {goog.events.BrowserEvent} e The event. |
| 890 |
* @private |
| 891 |
*/ |
| 892 |
goog.fx.DragListGroup.prototype.handleDragItemMouseover_ = function(e) { |
| 893 |
var targetEl = goog.asserts.assertElement(e.currentTarget); |
| 894 |
goog.dom.classlist.addAll(targetEl, this.dragItemHoverClasses_ || []); |
| 895 |
}; |
| 896 |
|
| 897 |
|
| 898 |
/** |
| 899 |
* Handles a MOUSEOUT event fired on a drag item. |
| 900 |
* @param {goog.events.BrowserEvent} e The event. |
| 901 |
* @private |
| 902 |
*/ |
| 903 |
goog.fx.DragListGroup.prototype.handleDragItemMouseout_ = function(e) { |
| 904 |
var targetEl = goog.asserts.assertElement(e.currentTarget); |
| 905 |
goog.dom.classlist.removeAll(targetEl, this.dragItemHoverClasses_ || []); |
| 906 |
}; |
| 907 |
|
| 908 |
|
| 909 |
/** |
| 910 |
* Handles a MOUSEOVER event fired on the handle element of a drag item. |
| 911 |
* @param {goog.events.BrowserEvent} e The event. |
| 912 |
* @private |
| 913 |
*/ |
| 914 |
goog.fx.DragListGroup.prototype.handleDragItemHandleMouseover_ = function(e) { |
| 915 |
var targetEl = goog.asserts.assertElement(e.currentTarget); |
| 916 |
goog.dom.classlist.addAll(targetEl, this.dragItemHandleHoverClasses_ || []); |
| 917 |
}; |
| 918 |
|
| 919 |
|
| 920 |
/** |
| 921 |
* Handles a MOUSEOUT event fired on the handle element of a drag item. |
| 922 |
* @param {goog.events.BrowserEvent} e The event. |
| 923 |
* @private |
| 924 |
*/ |
| 925 |
goog.fx.DragListGroup.prototype.handleDragItemHandleMouseout_ = function(e) { |
| 926 |
var targetEl = goog.asserts.assertElement(e.currentTarget); |
| 927 |
goog.dom.classlist.removeAll( |
| 928 |
targetEl, this.dragItemHandleHoverClasses_ || []); |
| 929 |
}; |
| 930 |
|
| 931 |
|
| 932 |
/** |
| 933 |
* Helper for handleDragMove_(). |
| 934 |
* Given the position of the center of the dragger element, figures out whether |
| 935 |
* it's currently hovering over any of the drag lists. |
| 936 |
* |
| 937 |
* @param {goog.math.Coordinate} draggerElCenter The center position of the |
| 938 |
* dragger element. |
| 939 |
* @return {Element} If currently hovering over a drag list, returns the drag |
| 940 |
* list element. Else returns null. |
| 941 |
* @private |
| 942 |
*/ |
| 943 |
goog.fx.DragListGroup.prototype.getHoverDragList_ = function(draggerElCenter) { |
| 944 |
|
| 945 |
// If the current drag item was in a list last time we did this, then check |
| 946 |
// that same list first. |
| 947 |
var prevHoverList = null; |
| 948 |
if (this.currDragItem_.style.display != 'none') { |
| 949 |
prevHoverList = /** @type {Element} */ (this.currDragItem_.parentNode); |
| 950 |
// Important: We can't use the cached bounds for this list because the |
| 951 |
// cached bounds are based on the case where the current drag item is not |
| 952 |
// in the list. Since the current drag item is known to be in this list, we |
| 953 |
// must recompute the list's bounds. |
| 954 |
var prevHoverListBounds = goog.style.getBounds(prevHoverList); |
| 955 |
if (this.isInRect_(draggerElCenter, prevHoverListBounds)) { |
| 956 |
return prevHoverList; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
for (var i = 0, n = this.dragLists_.length; i < n; i++) { |
| 961 |
var dragList = this.dragLists_[i]; |
| 962 |
if (dragList == prevHoverList) { |
| 963 |
continue; |
| 964 |
} |
| 965 |
if (this.isInRect_(draggerElCenter, dragList.dlgBounds_)) { |
| 966 |
return dragList; |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
return null; |
| 971 |
}; |
| 972 |
|
| 973 |
|
| 974 |
/** |
| 975 |
* Checks whether a coordinate position resides inside a rectangle. |
| 976 |
* @param {goog.math.Coordinate} pos The coordinate position. |
| 977 |
* @param {goog.math.Rect} rect The rectangle. |
| 978 |
* @return {boolean} True if 'pos' is within the bounds of 'rect'. |
| 979 |
* @private |
| 980 |
*/ |
| 981 |
goog.fx.DragListGroup.prototype.isInRect_ = function(pos, rect) { |
| 982 |
return pos.x > rect.left && pos.x < rect.left + rect.width && |
| 983 |
pos.y > rect.top && pos.y < rect.top + rect.height; |
| 984 |
}; |
| 985 |
|
| 986 |
|
| 987 |
/** |
| 988 |
* Updates the value of currHoverItem_. |
| 989 |
* |
| 990 |
* This method is used for insertion only when updateWhileDragging_ is false. |
| 991 |
* The below implementation is the basic one. This method can be extended by |
| 992 |
* a subclass to support changes to hovered item (eg: highlighting). Parametr |
| 993 |
* opt_draggerElCenter can be used for more sophisticated effects. |
| 994 |
* |
| 995 |
* @param {Element} hoverNextItem element of the list that is hovered over. |
| 996 |
* @param {goog.math.Coordinate=} opt_draggerElCenter current position of |
| 997 |
* the dragged element. |
| 998 |
* @protected |
| 999 |
*/ |
| 1000 |
goog.fx.DragListGroup.prototype.updateCurrHoverItem = function( |
| 1001 |
hoverNextItem, opt_draggerElCenter) { |
| 1002 |
if (hoverNextItem) { |
| 1003 |
this.currHoverItem_ = hoverNextItem; |
| 1004 |
} |
| 1005 |
}; |
| 1006 |
|
| 1007 |
|
| 1008 |
/** |
| 1009 |
* Inserts the currently dragged item in its new place. |
| 1010 |
* |
| 1011 |
* This method is used for insertion only when updateWhileDragging_ is false |
| 1012 |
* (otherwise there is no need for that). In the basic implementation |
| 1013 |
* the element is inserted before the currently hovered over item (this can |
| 1014 |
* be changed by overriding the method in subclasses). |
| 1015 |
* |
| 1016 |
* @protected |
| 1017 |
*/ |
| 1018 |
goog.fx.DragListGroup.prototype.insertCurrHoverItem = function() { |
| 1019 |
this.origList_.insertBefore(this.currDragItem_, this.currHoverItem_); |
| 1020 |
}; |
| 1021 |
|
| 1022 |
|
| 1023 |
/** |
| 1024 |
* Helper for handleDragMove_(). |
| 1025 |
* Given the position of the center of the dragger element, plus the drag list |
| 1026 |
* that it's currently hovering over, figures out the next drag item in the |
| 1027 |
* list that follows the current position of the dragger element. (I.e. if |
| 1028 |
* the drag action ends right now, it would become the item after the current |
| 1029 |
* drag item.) |
| 1030 |
* |
| 1031 |
* @param {Element} hoverList The drag list that we're hovering over. |
| 1032 |
* @param {goog.math.Coordinate} draggerElCenter The center position of the |
| 1033 |
* dragger element. |
| 1034 |
* @return {Element} Returns the earliest item in the hover list that belongs |
| 1035 |
* after the current position of the dragger element. If all items in the |
| 1036 |
* list should come before the current drag item, then returns null. |
| 1037 |
* @private |
| 1038 |
*/ |
| 1039 |
goog.fx.DragListGroup.prototype.getHoverNextItem_ = function( |
| 1040 |
hoverList, draggerElCenter) { |
| 1041 |
if (hoverList == null) { |
| 1042 |
throw new Error('getHoverNextItem_ called with null hoverList.'); |
| 1043 |
} |
| 1044 |
|
| 1045 |
// The definition of what it means for the draggerEl to be "before" a given |
| 1046 |
// item in the hover drag list is not always the same. It changes based on |
| 1047 |
// the growth direction of the hover drag list in question. |
| 1048 |
/** @type {number} */ |
| 1049 |
var relevantCoord = 0; |
| 1050 |
var getRelevantBoundFn; |
| 1051 |
var isBeforeFn; |
| 1052 |
var pickClosestRow = false; |
| 1053 |
var distanceToClosestRow = undefined; |
| 1054 |
switch (hoverList.dlgGrowthDirection_) { |
| 1055 |
case goog.fx.DragListDirection.DOWN: |
| 1056 |
// "Before" means draggerElCenter.y is less than item's bottom y-value. |
| 1057 |
relevantCoord = draggerElCenter.y; |
| 1058 |
getRelevantBoundFn = goog.fx.DragListGroup.getBottomBound_; |
| 1059 |
isBeforeFn = goog.fx.DragListGroup.isLessThan_; |
| 1060 |
break; |
| 1061 |
case goog.fx.DragListDirection.RIGHT_2D: |
| 1062 |
pickClosestRow = true; |
| 1063 |
case goog.fx.DragListDirection.RIGHT: |
| 1064 |
// "Before" means draggerElCenter.x is less than item's right x-value. |
| 1065 |
relevantCoord = draggerElCenter.x; |
| 1066 |
getRelevantBoundFn = goog.fx.DragListGroup.getRightBound_; |
| 1067 |
isBeforeFn = goog.fx.DragListGroup.isLessThan_; |
| 1068 |
break; |
| 1069 |
case goog.fx.DragListDirection.LEFT_2D: |
| 1070 |
pickClosestRow = true; |
| 1071 |
case goog.fx.DragListDirection.LEFT: |
| 1072 |
// "Before" means draggerElCenter.x is greater than item's left x-value. |
| 1073 |
relevantCoord = draggerElCenter.x; |
| 1074 |
getRelevantBoundFn = goog.fx.DragListGroup.getLeftBound_; |
| 1075 |
isBeforeFn = goog.fx.DragListGroup.isGreaterThan_; |
| 1076 |
break; |
| 1077 |
} |
| 1078 |
|
| 1079 |
// This holds the earliest drag item found so far that should come after |
| 1080 |
// this.currDragItem_ in the hover drag list (based on draggerElCenter). |
| 1081 |
var earliestAfterItem = null; |
| 1082 |
// This is the position of the relevant bound for the earliestAfterItem, |
| 1083 |
// where "relevant" is determined by the growth direction of hoverList. |
| 1084 |
var earliestAfterItemRelevantBound; |
| 1085 |
|
| 1086 |
var hoverListItems = goog.dom.getChildren(hoverList); |
| 1087 |
for (var i = 0, n = hoverListItems.length; i < n; i++) { |
| 1088 |
var item = hoverListItems[i]; |
| 1089 |
if (item == this.currDragItem_) { |
| 1090 |
continue; |
| 1091 |
} |
| 1092 |
|
| 1093 |
var relevantBound = getRelevantBoundFn(item.dlgBounds_); |
| 1094 |
// When the hoverlist is broken into multiple rows (i.e., in the case of |
| 1095 |
// LEFT_2D and RIGHT_2D) it is no longer enough to only look at the |
| 1096 |
// x-coordinate alone in order to find the {@earliestAfterItem} in the |
| 1097 |
// hoverlist. Make sure it is chosen from the row closest to the |
| 1098 |
// {@code draggerElCenter}. |
| 1099 |
if (pickClosestRow) { |
| 1100 |
var distanceToRow = goog.fx.DragListGroup.verticalDistanceFromItem_( |
| 1101 |
item, draggerElCenter); |
| 1102 |
// Initialize the distance to the closest row to the current value if |
| 1103 |
// undefined. |
| 1104 |
if (!goog.isDef(distanceToClosestRow)) { |
| 1105 |
distanceToClosestRow = distanceToRow; |
| 1106 |
} |
| 1107 |
if (isBeforeFn(relevantCoord, relevantBound) && |
| 1108 |
(earliestAfterItemRelevantBound == undefined || |
| 1109 |
(distanceToRow < distanceToClosestRow) || |
| 1110 |
((distanceToRow == distanceToClosestRow) && |
| 1111 |
(isBeforeFn(relevantBound, earliestAfterItemRelevantBound) || |
| 1112 |
relevantBound == earliestAfterItemRelevantBound)))) { |
| 1113 |
earliestAfterItem = item; |
| 1114 |
earliestAfterItemRelevantBound = relevantBound; |
| 1115 |
} |
| 1116 |
// Update distance to closest row. |
| 1117 |
if (distanceToRow < distanceToClosestRow) { |
| 1118 |
distanceToClosestRow = distanceToRow; |
| 1119 |
} |
| 1120 |
} else if ( |
| 1121 |
isBeforeFn(relevantCoord, relevantBound) && |
| 1122 |
(earliestAfterItemRelevantBound == undefined || |
| 1123 |
isBeforeFn(relevantBound, earliestAfterItemRelevantBound))) { |
| 1124 |
earliestAfterItem = item; |
| 1125 |
earliestAfterItemRelevantBound = relevantBound; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
// If we ended up picking an element that is not in the closest row it can |
| 1129 |
// only happen if we should have picked the last one in which case there is |
| 1130 |
// no consecutive element. |
| 1131 |
if (!goog.isNull(earliestAfterItem) && |
| 1132 |
goog.fx.DragListGroup.verticalDistanceFromItem_( |
| 1133 |
earliestAfterItem, draggerElCenter) > distanceToClosestRow) { |
| 1134 |
return null; |
| 1135 |
} else { |
| 1136 |
return earliestAfterItem; |
| 1137 |
} |
| 1138 |
}; |
| 1139 |
|
| 1140 |
|
| 1141 |
/** |
| 1142 |
* Private helper for getHoverNextItem(). |
| 1143 |
* Given an item and a target determine the vertical distance from the item's |
| 1144 |
* center to the target. |
| 1145 |
* @param {Element} item The item to measure the distance from. |
| 1146 |
* @param {goog.math.Coordinate} target The (x,y) coordinate of the target |
| 1147 |
* to measure the distance to. |
| 1148 |
* @return {number} The vertical distance between the center of the item and |
| 1149 |
* the target. |
| 1150 |
* @private |
| 1151 |
*/ |
| 1152 |
goog.fx.DragListGroup.verticalDistanceFromItem_ = function(item, target) { |
| 1153 |
var itemBounds = item.dlgBounds_; |
| 1154 |
var itemCenterY = itemBounds.top + (itemBounds.height - 1) / 2; |
| 1155 |
return Math.abs(target.y - itemCenterY); |
| 1156 |
}; |
| 1157 |
|
| 1158 |
|
| 1159 |
/** |
| 1160 |
* Private helper for getHoverNextItem_(). |
| 1161 |
* Given the bounds of an item, computes the item's bottom y-value. |
| 1162 |
* @param {goog.math.Rect} itemBounds The bounds of the item. |
| 1163 |
* @return {number} The item's bottom y-value. |
| 1164 |
* @private |
| 1165 |
*/ |
| 1166 |
goog.fx.DragListGroup.getBottomBound_ = function(itemBounds) { |
| 1167 |
return itemBounds.top + itemBounds.height - 1; |
| 1168 |
}; |
| 1169 |
|
| 1170 |
|
| 1171 |
/** |
| 1172 |
* Private helper for getHoverNextItem_(). |
| 1173 |
* Given the bounds of an item, computes the item's right x-value. |
| 1174 |
* @param {goog.math.Rect} itemBounds The bounds of the item. |
| 1175 |
* @return {number} The item's right x-value. |
| 1176 |
* @private |
| 1177 |
*/ |
| 1178 |
goog.fx.DragListGroup.getRightBound_ = function(itemBounds) { |
| 1179 |
return itemBounds.left + itemBounds.width - 1; |
| 1180 |
}; |
| 1181 |
|
| 1182 |
|
| 1183 |
/** |
| 1184 |
* Private helper for getHoverNextItem_(). |
| 1185 |
* Given the bounds of an item, computes the item's left x-value. |
| 1186 |
* @param {goog.math.Rect} itemBounds The bounds of the item. |
| 1187 |
* @return {number} The item's left x-value. |
| 1188 |
* @private |
| 1189 |
*/ |
| 1190 |
goog.fx.DragListGroup.getLeftBound_ = function(itemBounds) { |
| 1191 |
return itemBounds.left || 0; |
| 1192 |
}; |
| 1193 |
|
| 1194 |
|
| 1195 |
/** |
| 1196 |
* Private helper for getHoverNextItem_(). |
| 1197 |
* @param {number} a Number to compare. |
| 1198 |
* @param {number} b Number to compare. |
| 1199 |
* @return {boolean} Whether a is less than b. |
| 1200 |
* @private |
| 1201 |
*/ |
| 1202 |
goog.fx.DragListGroup.isLessThan_ = function(a, b) { |
| 1203 |
return a < b; |
| 1204 |
}; |
| 1205 |
|
| 1206 |
|
| 1207 |
/** |
| 1208 |
* Private helper for getHoverNextItem_(). |
| 1209 |
* @param {number} a Number to compare. |
| 1210 |
* @param {number} b Number to compare. |
| 1211 |
* @return {boolean} Whether a is greater than b. |
| 1212 |
* @private |
| 1213 |
*/ |
| 1214 |
goog.fx.DragListGroup.isGreaterThan_ = function(a, b) { |
| 1215 |
return a > b; |
| 1216 |
}; |
| 1217 |
|
| 1218 |
|
| 1219 |
/** |
| 1220 |
* Inserts the current drag item to the appropriate location in the drag list |
| 1221 |
* that we're hovering over (if the current drag item is not already there). |
| 1222 |
* |
| 1223 |
* @param {Element} hoverList The drag list we're hovering over. |
| 1224 |
* @param {Element} hoverNextItem The next item in the hover drag list. |
| 1225 |
* @private |
| 1226 |
*/ |
| 1227 |
goog.fx.DragListGroup.prototype.insertCurrDragItem_ = function( |
| 1228 |
hoverList, hoverNextItem) { |
| 1229 |
if (this.currDragItem_.parentNode != hoverList || |
| 1230 |
goog.dom.getNextElementSibling(this.currDragItem_) != hoverNextItem) { |
| 1231 |
// The current drag item is not in the correct location, so we move it. |
| 1232 |
// Note: hoverNextItem may be null, but insertBefore() still works. |
| 1233 |
hoverList.insertBefore(this.currDragItem_, hoverNextItem); |
| 1234 |
} |
| 1235 |
}; |
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
/** |
| 1240 |
* The event object dispatched by DragListGroup. |
| 1241 |
* The fields draggerElCenter, hoverList, and hoverNextItem are only available |
| 1242 |
* for the BEFOREDRAGMOVE and DRAGMOVE events. |
| 1243 |
* |
| 1244 |
* @param {!goog.fx.DragListGroup.EventType} type |
| 1245 |
* @param {goog.fx.DragListGroup} dragListGroup A reference to the associated |
| 1246 |
* DragListGroup object. |
| 1247 |
* @param {goog.events.BrowserEvent|goog.fx.DragEvent} event The event fired |
| 1248 |
* by the browser or fired by the dragger. |
| 1249 |
* @param {Element} currDragItem The current drag item being moved. |
| 1250 |
* @param {Element} draggerEl The clone of the current drag item that's actually |
| 1251 |
* being dragged around. |
| 1252 |
* @param {goog.fx.Dragger} dragger The dragger object. |
| 1253 |
* @param {goog.math.Coordinate=} opt_draggerElCenter The current center |
| 1254 |
* position of the draggerEl. |
| 1255 |
* @param {Element=} opt_hoverList The current drag list that's being hovered |
| 1256 |
* over, or null if the center of draggerEl is outside of any drag lists. |
| 1257 |
* If not null and the drag action ends right now, then currDragItem will |
| 1258 |
* end up in this list. |
| 1259 |
* @param {Element=} opt_hoverNextItem The current next item in the hoverList |
| 1260 |
* that the draggerEl is hovering over. (I.e. If the drag action ends |
| 1261 |
* right now, then this item would become the next item after the new |
| 1262 |
* location of currDragItem.) May be null if not applicable or if |
| 1263 |
* currDragItem would be added to the end of hoverList. |
| 1264 |
* @constructor |
| 1265 |
* @struct |
| 1266 |
* @extends {goog.events.Event} |
| 1267 |
*/ |
| 1268 |
goog.fx.DragListGroupEvent = function( |
| 1269 |
type, dragListGroup, event, currDragItem, draggerEl, dragger, |
| 1270 |
opt_draggerElCenter, opt_hoverList, opt_hoverNextItem) { |
| 1271 |
goog.events.Event.call(this, type); |
| 1272 |
|
| 1273 |
/** |
| 1274 |
* A reference to the associated DragListGroup object. |
| 1275 |
* @type {goog.fx.DragListGroup} |
| 1276 |
*/ |
| 1277 |
this.dragListGroup = dragListGroup; |
| 1278 |
|
| 1279 |
/** |
| 1280 |
* The event fired by the browser or fired by the dragger. |
| 1281 |
* @type {goog.events.BrowserEvent|goog.fx.DragEvent} |
| 1282 |
*/ |
| 1283 |
this.event = event; |
| 1284 |
|
| 1285 |
/** |
| 1286 |
* The current drag item being move. |
| 1287 |
* @type {Element} |
| 1288 |
*/ |
| 1289 |
this.currDragItem = currDragItem; |
| 1290 |
|
| 1291 |
/** |
| 1292 |
* The clone of the current drag item that's actually being dragged around. |
| 1293 |
* @type {Element} |
| 1294 |
*/ |
| 1295 |
this.draggerEl = draggerEl; |
| 1296 |
|
| 1297 |
/** |
| 1298 |
* The dragger object. |
| 1299 |
* @type {goog.fx.Dragger} |
| 1300 |
*/ |
| 1301 |
this.dragger = dragger; |
| 1302 |
|
| 1303 |
/** |
| 1304 |
* The current center position of the draggerEl. |
| 1305 |
* @type {goog.math.Coordinate|undefined} |
| 1306 |
*/ |
| 1307 |
this.draggerElCenter = opt_draggerElCenter; |
| 1308 |
|
| 1309 |
/** |
| 1310 |
* The current drag list that's being hovered over, or null if the center of |
| 1311 |
* draggerEl is outside of any drag lists. (I.e. If not null and the drag |
| 1312 |
* action ends right now, then currDragItem will end up in this list.) |
| 1313 |
* @type {Element|undefined} |
| 1314 |
*/ |
| 1315 |
this.hoverList = opt_hoverList; |
| 1316 |
|
| 1317 |
/** |
| 1318 |
* The current next item in the hoverList that the draggerEl is hovering over. |
| 1319 |
* (I.e. If the drag action ends right now, then this item would become the |
| 1320 |
* next item after the new location of currDragItem.) May be null if not |
| 1321 |
* applicable or if currDragItem would be added to the end of hoverList. |
| 1322 |
* @type {Element|undefined} |
| 1323 |
*/ |
| 1324 |
this.hoverNextItem = opt_hoverNextItem; |
| 1325 |
}; |
| 1326 |
goog.inherits(goog.fx.DragListGroupEvent, goog.events.Event); |