2012-01-25 14:40:18 -08:00
|
|
|
#ifdef 0
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This singleton provides a custom drop target detection. We need this because
|
|
|
|
* the default DnD target detection relies on the cursor's position. We want
|
|
|
|
* to pick a drop target based on the dragged site's position.
|
|
|
|
*/
|
|
|
|
let gDropTargetShim = {
|
|
|
|
/**
|
|
|
|
* Cache for the position of all cells, cleaned after drag finished.
|
|
|
|
*/
|
|
|
|
_cellPositions: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The last drop target that was hovered.
|
|
|
|
*/
|
|
|
|
_lastDropTarget: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the drop target shim.
|
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
init: function () {
|
|
|
|
gGrid.node.addEventListener("dragstart", this, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add all event listeners needed during a drag operation.
|
|
|
|
*/
|
|
|
|
_addEventListeners: function () {
|
|
|
|
gGrid.node.addEventListener("dragend", this);
|
2012-01-25 14:40:18 -08:00
|
|
|
|
2013-07-24 00:58:50 -07:00
|
|
|
let docElement = document.documentElement;
|
|
|
|
docElement.addEventListener("dragover", this);
|
|
|
|
docElement.addEventListener("dragenter", this);
|
|
|
|
docElement.addEventListener("drop", this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all event listeners that were needed during a drag operation.
|
|
|
|
*/
|
|
|
|
_removeEventListeners: function () {
|
|
|
|
gGrid.node.removeEventListener("dragend", this);
|
|
|
|
|
|
|
|
let docElement = document.documentElement;
|
|
|
|
docElement.removeEventListener("dragover", this);
|
|
|
|
docElement.removeEventListener("dragenter", this);
|
|
|
|
docElement.removeEventListener("drop", this);
|
2012-03-12 19:23:01 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles all shim events.
|
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
handleEvent: function (aEvent) {
|
2012-03-12 19:23:01 -07:00
|
|
|
switch (aEvent.type) {
|
|
|
|
case "dragstart":
|
2013-07-24 00:58:50 -07:00
|
|
|
this._dragstart(aEvent);
|
|
|
|
break;
|
|
|
|
case "dragenter":
|
|
|
|
aEvent.preventDefault();
|
2012-03-12 19:23:01 -07:00
|
|
|
break;
|
|
|
|
case "dragover":
|
|
|
|
this._dragover(aEvent);
|
|
|
|
break;
|
2013-07-24 00:58:50 -07:00
|
|
|
case "drop":
|
|
|
|
this._drop(aEvent);
|
|
|
|
break;
|
2012-03-12 19:23:01 -07:00
|
|
|
case "dragend":
|
2013-07-24 00:58:50 -07:00
|
|
|
this._dragend(aEvent);
|
2012-03-12 19:23:01 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-01-25 14:40:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the 'dragstart' event.
|
|
|
|
* @param aEvent The 'dragstart' event.
|
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
_dragstart: function (aEvent) {
|
2012-03-12 19:23:01 -07:00
|
|
|
if (aEvent.target.classList.contains("newtab-link")) {
|
2012-03-08 03:57:26 -08:00
|
|
|
gGrid.lock();
|
2013-07-24 00:58:50 -07:00
|
|
|
this._addEventListeners();
|
2012-03-08 03:57:26 -08:00
|
|
|
}
|
2012-01-25 14:40:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-07-24 00:58:50 -07:00
|
|
|
* Handles the 'dragover' event.
|
|
|
|
* @param aEvent The 'dragover' event.
|
2012-01-25 14:40:18 -08:00
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
_dragover: function (aEvent) {
|
|
|
|
// XXX bug 505521 - Use the dragover event to retrieve the
|
|
|
|
// current mouse coordinates while dragging.
|
|
|
|
let sourceNode = aEvent.dataTransfer.mozSourceNode.parentNode;
|
|
|
|
gDrag.drag(sourceNode._newtabSite, aEvent);
|
2012-01-25 14:40:18 -08:00
|
|
|
|
2013-07-24 00:58:50 -07:00
|
|
|
// Find the current drop target, if there's one.
|
|
|
|
this._updateDropTarget(aEvent);
|
2012-01-25 14:40:18 -08:00
|
|
|
|
2013-07-24 00:58:50 -07:00
|
|
|
// If we have a valid drop target,
|
|
|
|
// let the drag-and-drop service know.
|
|
|
|
if (this._lastDropTarget) {
|
|
|
|
aEvent.preventDefault();
|
2012-01-25 14:40:18 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-07-24 00:58:50 -07:00
|
|
|
* Handles the 'drop' event.
|
|
|
|
* @param aEvent The 'drop' event.
|
2012-01-25 14:40:18 -08:00
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
_drop: function (aEvent) {
|
|
|
|
// We're accepting all drops.
|
|
|
|
aEvent.preventDefault();
|
|
|
|
|
|
|
|
// Make sure to determine the current drop target
|
|
|
|
// in case the dragover event hasn't been fired.
|
|
|
|
this._updateDropTarget(aEvent);
|
2012-01-25 14:40:18 -08:00
|
|
|
|
2013-07-24 00:58:50 -07:00
|
|
|
// A site was successfully dropped.
|
|
|
|
this._dispatchEvent(aEvent, "drop", this._lastDropTarget);
|
2012-01-25 14:40:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the 'dragend' event.
|
|
|
|
* @param aEvent The 'dragend' event.
|
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
_dragend: function (aEvent) {
|
2012-01-25 14:40:18 -08:00
|
|
|
if (this._lastDropTarget) {
|
|
|
|
if (aEvent.dataTransfer.mozUserCancelled) {
|
|
|
|
// The drag operation was cancelled.
|
|
|
|
this._dispatchEvent(aEvent, "dragexit", this._lastDropTarget);
|
|
|
|
this._dispatchEvent(aEvent, "dragleave", this._lastDropTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up.
|
|
|
|
this._lastDropTarget = null;
|
|
|
|
this._cellPositions = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
gGrid.unlock();
|
2013-07-24 00:58:50 -07:00
|
|
|
this._removeEventListeners();
|
|
|
|
},
|
2012-01-25 14:40:18 -08:00
|
|
|
|
2013-07-24 00:58:50 -07:00
|
|
|
/**
|
|
|
|
* Tries to find the current drop target and will fire
|
|
|
|
* appropriate dragenter, dragexit, and dragleave events.
|
|
|
|
* @param aEvent The current drag event.
|
|
|
|
*/
|
|
|
|
_updateDropTarget: function (aEvent) {
|
|
|
|
// Let's see if we find a drop target.
|
|
|
|
let target = this._findDropTarget(aEvent);
|
|
|
|
|
|
|
|
if (target != this._lastDropTarget) {
|
|
|
|
if (this._lastDropTarget)
|
|
|
|
// We left the last drop target.
|
|
|
|
this._dispatchEvent(aEvent, "dragexit", this._lastDropTarget);
|
|
|
|
|
|
|
|
if (target)
|
|
|
|
// We're now hovering a (new) drop target.
|
|
|
|
this._dispatchEvent(aEvent, "dragenter", target);
|
|
|
|
|
|
|
|
if (this._lastDropTarget)
|
|
|
|
// We left the last drop target.
|
|
|
|
this._dispatchEvent(aEvent, "dragleave", this._lastDropTarget);
|
|
|
|
|
|
|
|
this._lastDropTarget = target;
|
|
|
|
}
|
2012-01-25 14:40:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the current drop target by matching the dragged site's position
|
|
|
|
* against all cells in the grid.
|
|
|
|
* @return The currently hovered drop target or null.
|
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
_findDropTarget: function () {
|
2012-01-25 14:40:18 -08:00
|
|
|
// These are the minimum intersection values - we want to use the cell if
|
|
|
|
// the site is >= 50% hovering its position.
|
|
|
|
let minWidth = gDrag.cellWidth / 2;
|
|
|
|
let minHeight = gDrag.cellHeight / 2;
|
|
|
|
|
|
|
|
let cellPositions = this._getCellPositions();
|
|
|
|
let rect = gTransformation.getNodePosition(gDrag.draggedSite.node);
|
|
|
|
|
|
|
|
// Compare each cell's position to the dragged site's position.
|
|
|
|
for (let i = 0; i < cellPositions.length; i++) {
|
|
|
|
let inter = rect.intersect(cellPositions[i].rect);
|
|
|
|
|
|
|
|
// If the intersection is big enough we found a drop target.
|
|
|
|
if (inter.width >= minWidth && inter.height >= minHeight)
|
|
|
|
return cellPositions[i].cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No drop target found.
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the positions of all cell nodes.
|
|
|
|
* @return The (cached) cell positions.
|
|
|
|
*/
|
|
|
|
_getCellPositions: function DropTargetShim_getCellPositions() {
|
|
|
|
if (this._cellPositions)
|
|
|
|
return this._cellPositions;
|
|
|
|
|
|
|
|
return this._cellPositions = gGrid.cells.map(function (cell) {
|
|
|
|
return {cell: cell, rect: gTransformation.getNodePosition(cell.node)};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches a custom DragEvent on the given target node.
|
|
|
|
* @param aEvent The source event.
|
|
|
|
* @param aType The event type.
|
|
|
|
* @param aTarget The target node that receives the event.
|
|
|
|
*/
|
2013-07-24 00:58:50 -07:00
|
|
|
_dispatchEvent: function (aEvent, aType, aTarget) {
|
2012-01-25 14:40:18 -08:00
|
|
|
let node = aTarget.node;
|
|
|
|
let event = document.createEvent("DragEvents");
|
|
|
|
|
2013-07-24 00:58:50 -07:00
|
|
|
// The event should not bubble to prevent recursion.
|
|
|
|
event.initDragEvent(aType, false, true, window, 0, 0, 0, 0, 0, false, false,
|
2012-01-25 14:40:18 -08:00
|
|
|
false, false, 0, node, aEvent.dataTransfer);
|
|
|
|
|
|
|
|
node.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
};
|