Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #ifdef 0 |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | #endif |
michael@0 | 6 | |
michael@0 | 7 | // A little delay that prevents the grid from being too sensitive when dragging |
michael@0 | 8 | // sites around. |
michael@0 | 9 | const DELAY_REARRANGE_MS = 100; |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * This singleton implements site dropping functionality. |
michael@0 | 13 | */ |
michael@0 | 14 | let gDrop = { |
michael@0 | 15 | /** |
michael@0 | 16 | * The last drop target. |
michael@0 | 17 | */ |
michael@0 | 18 | _lastDropTarget: null, |
michael@0 | 19 | |
michael@0 | 20 | /** |
michael@0 | 21 | * Handles the 'dragenter' event. |
michael@0 | 22 | * @param aCell The drop target cell. |
michael@0 | 23 | */ |
michael@0 | 24 | enter: function Drop_enter(aCell) { |
michael@0 | 25 | this._delayedRearrange(aCell); |
michael@0 | 26 | }, |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * Handles the 'dragexit' event. |
michael@0 | 30 | * @param aCell The drop target cell. |
michael@0 | 31 | * @param aEvent The 'dragexit' event. |
michael@0 | 32 | */ |
michael@0 | 33 | exit: function Drop_exit(aCell, aEvent) { |
michael@0 | 34 | if (aEvent.dataTransfer && !aEvent.dataTransfer.mozUserCancelled) { |
michael@0 | 35 | this._delayedRearrange(); |
michael@0 | 36 | } else { |
michael@0 | 37 | // The drag operation has been cancelled. |
michael@0 | 38 | this._cancelDelayedArrange(); |
michael@0 | 39 | this._rearrange(); |
michael@0 | 40 | } |
michael@0 | 41 | }, |
michael@0 | 42 | |
michael@0 | 43 | /** |
michael@0 | 44 | * Handles the 'drop' event. |
michael@0 | 45 | * @param aCell The drop target cell. |
michael@0 | 46 | * @param aEvent The 'dragexit' event. |
michael@0 | 47 | */ |
michael@0 | 48 | drop: function Drop_drop(aCell, aEvent) { |
michael@0 | 49 | // The cell that is the drop target could contain a pinned site. We need |
michael@0 | 50 | // to find out where that site has gone and re-pin it there. |
michael@0 | 51 | if (aCell.containsPinnedSite()) |
michael@0 | 52 | this._repinSitesAfterDrop(aCell); |
michael@0 | 53 | |
michael@0 | 54 | // Pin the dragged or insert the new site. |
michael@0 | 55 | this._pinDraggedSite(aCell, aEvent); |
michael@0 | 56 | |
michael@0 | 57 | this._cancelDelayedArrange(); |
michael@0 | 58 | |
michael@0 | 59 | // Update the grid and move all sites to their new places. |
michael@0 | 60 | gUpdater.updateGrid(); |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Re-pins all pinned sites in their (new) positions. |
michael@0 | 65 | * @param aCell The drop target cell. |
michael@0 | 66 | */ |
michael@0 | 67 | _repinSitesAfterDrop: function Drop_repinSitesAfterDrop(aCell) { |
michael@0 | 68 | let sites = gDropPreview.rearrange(aCell); |
michael@0 | 69 | |
michael@0 | 70 | // Filter out pinned sites. |
michael@0 | 71 | let pinnedSites = sites.filter(function (aSite) { |
michael@0 | 72 | return aSite && aSite.isPinned(); |
michael@0 | 73 | }); |
michael@0 | 74 | |
michael@0 | 75 | // Re-pin all shifted pinned cells. |
michael@0 | 76 | pinnedSites.forEach(function (aSite) aSite.pin(sites.indexOf(aSite)), this); |
michael@0 | 77 | }, |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Pins the dragged site in its new place. |
michael@0 | 81 | * @param aCell The drop target cell. |
michael@0 | 82 | * @param aEvent The 'dragexit' event. |
michael@0 | 83 | */ |
michael@0 | 84 | _pinDraggedSite: function Drop_pinDraggedSite(aCell, aEvent) { |
michael@0 | 85 | let index = aCell.index; |
michael@0 | 86 | let draggedSite = gDrag.draggedSite; |
michael@0 | 87 | |
michael@0 | 88 | if (draggedSite) { |
michael@0 | 89 | // Pin the dragged site at its new place. |
michael@0 | 90 | if (aCell != draggedSite.cell) |
michael@0 | 91 | draggedSite.pin(index); |
michael@0 | 92 | } else { |
michael@0 | 93 | let link = gDragDataHelper.getLinkFromDragEvent(aEvent); |
michael@0 | 94 | if (link) { |
michael@0 | 95 | // A new link was dragged onto the grid. Create it by pinning its URL. |
michael@0 | 96 | gPinnedLinks.pin(link, index); |
michael@0 | 97 | |
michael@0 | 98 | // Make sure the newly added link is not blocked. |
michael@0 | 99 | gBlockedLinks.unblock(link); |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | }, |
michael@0 | 103 | |
michael@0 | 104 | /** |
michael@0 | 105 | * Time a rearrange with a little delay. |
michael@0 | 106 | * @param aCell The drop target cell. |
michael@0 | 107 | */ |
michael@0 | 108 | _delayedRearrange: function Drop_delayedRearrange(aCell) { |
michael@0 | 109 | // The last drop target didn't change so there's no need to re-arrange. |
michael@0 | 110 | if (this._lastDropTarget == aCell) |
michael@0 | 111 | return; |
michael@0 | 112 | |
michael@0 | 113 | let self = this; |
michael@0 | 114 | |
michael@0 | 115 | function callback() { |
michael@0 | 116 | self._rearrangeTimeout = null; |
michael@0 | 117 | self._rearrange(aCell); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | this._cancelDelayedArrange(); |
michael@0 | 121 | this._rearrangeTimeout = setTimeout(callback, DELAY_REARRANGE_MS); |
michael@0 | 122 | |
michael@0 | 123 | // Store the last drop target. |
michael@0 | 124 | this._lastDropTarget = aCell; |
michael@0 | 125 | }, |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Cancels a timed rearrange, if any. |
michael@0 | 129 | */ |
michael@0 | 130 | _cancelDelayedArrange: function Drop_cancelDelayedArrange() { |
michael@0 | 131 | if (this._rearrangeTimeout) { |
michael@0 | 132 | clearTimeout(this._rearrangeTimeout); |
michael@0 | 133 | this._rearrangeTimeout = null; |
michael@0 | 134 | } |
michael@0 | 135 | }, |
michael@0 | 136 | |
michael@0 | 137 | /** |
michael@0 | 138 | * Rearrange all sites in the grid depending on the current drop target. |
michael@0 | 139 | * @param aCell The drop target cell. |
michael@0 | 140 | */ |
michael@0 | 141 | _rearrange: function Drop_rearrange(aCell) { |
michael@0 | 142 | let sites = gGrid.sites; |
michael@0 | 143 | |
michael@0 | 144 | // We need to rearrange the grid only if there's a current drop target. |
michael@0 | 145 | if (aCell) |
michael@0 | 146 | sites = gDropPreview.rearrange(aCell); |
michael@0 | 147 | |
michael@0 | 148 | gTransformation.rearrangeSites(sites, {unfreeze: !aCell}); |
michael@0 | 149 | } |
michael@0 | 150 | }; |