1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/newtab/drop.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +#ifdef 0 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +#endif 1.9 + 1.10 +// A little delay that prevents the grid from being too sensitive when dragging 1.11 +// sites around. 1.12 +const DELAY_REARRANGE_MS = 100; 1.13 + 1.14 +/** 1.15 + * This singleton implements site dropping functionality. 1.16 + */ 1.17 +let gDrop = { 1.18 + /** 1.19 + * The last drop target. 1.20 + */ 1.21 + _lastDropTarget: null, 1.22 + 1.23 + /** 1.24 + * Handles the 'dragenter' event. 1.25 + * @param aCell The drop target cell. 1.26 + */ 1.27 + enter: function Drop_enter(aCell) { 1.28 + this._delayedRearrange(aCell); 1.29 + }, 1.30 + 1.31 + /** 1.32 + * Handles the 'dragexit' event. 1.33 + * @param aCell The drop target cell. 1.34 + * @param aEvent The 'dragexit' event. 1.35 + */ 1.36 + exit: function Drop_exit(aCell, aEvent) { 1.37 + if (aEvent.dataTransfer && !aEvent.dataTransfer.mozUserCancelled) { 1.38 + this._delayedRearrange(); 1.39 + } else { 1.40 + // The drag operation has been cancelled. 1.41 + this._cancelDelayedArrange(); 1.42 + this._rearrange(); 1.43 + } 1.44 + }, 1.45 + 1.46 + /** 1.47 + * Handles the 'drop' event. 1.48 + * @param aCell The drop target cell. 1.49 + * @param aEvent The 'dragexit' event. 1.50 + */ 1.51 + drop: function Drop_drop(aCell, aEvent) { 1.52 + // The cell that is the drop target could contain a pinned site. We need 1.53 + // to find out where that site has gone and re-pin it there. 1.54 + if (aCell.containsPinnedSite()) 1.55 + this._repinSitesAfterDrop(aCell); 1.56 + 1.57 + // Pin the dragged or insert the new site. 1.58 + this._pinDraggedSite(aCell, aEvent); 1.59 + 1.60 + this._cancelDelayedArrange(); 1.61 + 1.62 + // Update the grid and move all sites to their new places. 1.63 + gUpdater.updateGrid(); 1.64 + }, 1.65 + 1.66 + /** 1.67 + * Re-pins all pinned sites in their (new) positions. 1.68 + * @param aCell The drop target cell. 1.69 + */ 1.70 + _repinSitesAfterDrop: function Drop_repinSitesAfterDrop(aCell) { 1.71 + let sites = gDropPreview.rearrange(aCell); 1.72 + 1.73 + // Filter out pinned sites. 1.74 + let pinnedSites = sites.filter(function (aSite) { 1.75 + return aSite && aSite.isPinned(); 1.76 + }); 1.77 + 1.78 + // Re-pin all shifted pinned cells. 1.79 + pinnedSites.forEach(function (aSite) aSite.pin(sites.indexOf(aSite)), this); 1.80 + }, 1.81 + 1.82 + /** 1.83 + * Pins the dragged site in its new place. 1.84 + * @param aCell The drop target cell. 1.85 + * @param aEvent The 'dragexit' event. 1.86 + */ 1.87 + _pinDraggedSite: function Drop_pinDraggedSite(aCell, aEvent) { 1.88 + let index = aCell.index; 1.89 + let draggedSite = gDrag.draggedSite; 1.90 + 1.91 + if (draggedSite) { 1.92 + // Pin the dragged site at its new place. 1.93 + if (aCell != draggedSite.cell) 1.94 + draggedSite.pin(index); 1.95 + } else { 1.96 + let link = gDragDataHelper.getLinkFromDragEvent(aEvent); 1.97 + if (link) { 1.98 + // A new link was dragged onto the grid. Create it by pinning its URL. 1.99 + gPinnedLinks.pin(link, index); 1.100 + 1.101 + // Make sure the newly added link is not blocked. 1.102 + gBlockedLinks.unblock(link); 1.103 + } 1.104 + } 1.105 + }, 1.106 + 1.107 + /** 1.108 + * Time a rearrange with a little delay. 1.109 + * @param aCell The drop target cell. 1.110 + */ 1.111 + _delayedRearrange: function Drop_delayedRearrange(aCell) { 1.112 + // The last drop target didn't change so there's no need to re-arrange. 1.113 + if (this._lastDropTarget == aCell) 1.114 + return; 1.115 + 1.116 + let self = this; 1.117 + 1.118 + function callback() { 1.119 + self._rearrangeTimeout = null; 1.120 + self._rearrange(aCell); 1.121 + } 1.122 + 1.123 + this._cancelDelayedArrange(); 1.124 + this._rearrangeTimeout = setTimeout(callback, DELAY_REARRANGE_MS); 1.125 + 1.126 + // Store the last drop target. 1.127 + this._lastDropTarget = aCell; 1.128 + }, 1.129 + 1.130 + /** 1.131 + * Cancels a timed rearrange, if any. 1.132 + */ 1.133 + _cancelDelayedArrange: function Drop_cancelDelayedArrange() { 1.134 + if (this._rearrangeTimeout) { 1.135 + clearTimeout(this._rearrangeTimeout); 1.136 + this._rearrangeTimeout = null; 1.137 + } 1.138 + }, 1.139 + 1.140 + /** 1.141 + * Rearrange all sites in the grid depending on the current drop target. 1.142 + * @param aCell The drop target cell. 1.143 + */ 1.144 + _rearrange: function Drop_rearrange(aCell) { 1.145 + let sites = gGrid.sites; 1.146 + 1.147 + // We need to rearrange the grid only if there's a current drop target. 1.148 + if (aCell) 1.149 + sites = gDropPreview.rearrange(aCell); 1.150 + 1.151 + gTransformation.rearrangeSites(sites, {unfreeze: !aCell}); 1.152 + } 1.153 +};