-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redid MutationObserver logic. We now listen for attributes changes, b…
…ut disconnect and reconnect around the packery call to avoid infinite loops. Tabs to spaces in panels.dashboard.js
- Loading branch information
Matthew McNamara
committed
Feb 21, 2015
1 parent
aecb99e
commit e2408bc
Showing
2 changed files
with
114 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,108 @@ | ||
'use strict'; | ||
|
||
var $packeryContainer, | ||
isPackeryInit = false; | ||
isPackeryInit = false; | ||
|
||
function arrayUnique(array) { | ||
var a = array.concat(); | ||
for (var i = 0; i < a.length; ++i) { | ||
for (var j = i + 1; j < a.length; ++j) { | ||
if (a[i] === a[j]) | ||
a.splice(j--, 1); | ||
} | ||
} | ||
return a; | ||
var a = array.concat(); | ||
for (var i = 0; i < a.length; ++i) { | ||
for (var j = i + 1; j < a.length; ++j) { | ||
if (a[i] === a[j]) | ||
a.splice(j--, 1); | ||
} | ||
} | ||
return a; | ||
} | ||
|
||
function initPackery($panelsContainer) { | ||
var itemSelector = '.panel'; | ||
|
||
$packeryContainer = $panelsContainer.packery({ | ||
columnWidth: 128, //.panel-span-1 width = 128 | ||
gutter: 16, // gutter = 8*2 | ||
// disable initial layout | ||
isInitLayout: false | ||
}); | ||
|
||
var pckry = $packeryContainer.data('packery'); | ||
|
||
// Initial sort | ||
var sortOrder = []; // global variable for saving order, used later | ||
var storedSortOrder = localStorage.getItem('panelSortingOrder'); | ||
if (storedSortOrder) { | ||
storedSortOrder = JSON.parse(storedSortOrder); | ||
|
||
//create a hash of items | ||
var itemsByPanelName = {}, | ||
allPanels = []; | ||
var panelName; | ||
for (var i = 0, len = pckry.items.length; i < len; i++) { | ||
var item = pckry.items[i]; | ||
panelName = $(item.element).attr('data-panel'); | ||
allPanels[i] = panelName; | ||
itemsByPanelName[panelName] = item; | ||
} | ||
|
||
// Merge the saved panel array with our currently loaded panels, remove dupes | ||
var allPanelsOrdered = arrayUnique(storedSortOrder.concat(allPanels)); | ||
|
||
// Remove panels that no longer exist | ||
var removededOld = allPanelsOrdered.filter(function (val) { | ||
return allPanels.indexOf(val) !== -1; | ||
}); | ||
|
||
// overwrite packery item order | ||
i = 0; | ||
len = removededOld.length; | ||
for (; i < len; i++) { | ||
panelName = removededOld[i]; | ||
pckry.items[i] = itemsByPanelName[panelName]; | ||
} | ||
} | ||
|
||
// Manually trigger initial layout | ||
$packeryContainer.packery(); | ||
isPackeryInit = true; | ||
|
||
$packeryContainer.find(itemSelector).each(function (i, itemElem) { | ||
// make element draggable with Draggabilly | ||
var draggie = new Draggabilly(itemElem, { | ||
"handle": '.panel-heading' | ||
}); | ||
|
||
// bind Draggabilly events to Packery | ||
$packeryContainer.packery('bindDraggabillyEvents', draggie); | ||
}); | ||
|
||
// Daggabilly shtuff | ||
|
||
// Currently, there is no built-in option to force dragged elements to gravitate to their | ||
// nearest neighbour in a specific direction. This will reset their locations 100ms after a drag | ||
// causing them to gravitate. | ||
$packeryContainer.packery('on', 'dragItemPositioned', function (pckryInstance, draggedItem) { | ||
setTimeout(function () { | ||
$packeryContainer.packery(); | ||
}, 100); | ||
}); | ||
|
||
function orderItems() { | ||
var itemElems = pckry.getItemElements(); | ||
|
||
// reset / empty order array | ||
sortOrder.length = 0; | ||
|
||
for (var i = 0; i < itemElems.length; i++) { | ||
sortOrder[i] = itemElems[i].getAttribute('data-panel'); | ||
} | ||
|
||
// save ordering | ||
localStorage.setItem('panelSortingOrder', JSON.stringify(sortOrder)); | ||
} | ||
|
||
$packeryContainer.packery('on', 'layoutComplete', orderItems); | ||
$packeryContainer.packery('on', 'dragItemPositioned', orderItems); | ||
var itemSelector = '.panel'; | ||
|
||
$packeryContainer = $panelsContainer.packery({ | ||
columnWidth: 128, //.panel-span-1 width = 128 | ||
gutter: 16, // gutter = 8*2 | ||
// disable initial layout | ||
isInitLayout: false | ||
}); | ||
|
||
var pckry = $packeryContainer.data('packery'); | ||
|
||
// Initial sort | ||
var sortOrder = []; // global variable for saving order, used later | ||
var storedSortOrder = localStorage.getItem('panelSortingOrder'); | ||
if (storedSortOrder) { | ||
storedSortOrder = JSON.parse(storedSortOrder); | ||
|
||
//create a hash of items | ||
var itemsByPanelName = {}, | ||
allPanels = []; | ||
var panelName; | ||
for (var i = 0, len = pckry.items.length; i < len; i++) { | ||
var item = pckry.items[i]; | ||
panelName = $(item.element).attr('data-panel'); | ||
allPanels[i] = panelName; | ||
itemsByPanelName[panelName] = item; | ||
} | ||
|
||
// Merge the saved panel array with our currently loaded panels, remove dupes | ||
var allPanelsOrdered = arrayUnique(storedSortOrder.concat(allPanels)); | ||
|
||
// Remove panels that no longer exist | ||
var removededOld = allPanelsOrdered.filter(function (val) { | ||
return allPanels.indexOf(val) !== -1; | ||
}); | ||
|
||
// overwrite packery item order | ||
i = 0; | ||
len = removededOld.length; | ||
for (; i < len; i++) { | ||
panelName = removededOld[i]; | ||
pckry.items[i] = itemsByPanelName[panelName]; | ||
} | ||
} | ||
|
||
// Manually trigger initial layout | ||
$packeryContainer.packery(); | ||
isPackeryInit = true; | ||
|
||
$packeryContainer.find(itemSelector).each(function (i, itemElem) { | ||
// make element draggable with Draggabilly | ||
var draggie = new Draggabilly(itemElem, { | ||
"handle": '.panel-heading' | ||
}); | ||
|
||
// bind Draggabilly events to Packery | ||
$packeryContainer.packery('bindDraggabillyEvents', draggie); | ||
}); | ||
|
||
// Daggabilly shtuff | ||
|
||
// Currently, there is no built-in option to force dragged elements to gravitate to their | ||
// nearest neighbour in a specific direction. This will reset their locations 100ms after a drag | ||
// causing them to gravitate. | ||
$packeryContainer.packery('on', 'dragItemPositioned', function (pckryInstance, draggedItem) { | ||
setTimeout(function () { | ||
$packeryContainer.packery(); | ||
}, 100); | ||
}); | ||
|
||
function orderItems() { | ||
var itemElems = pckry.getItemElements(); | ||
|
||
// reset / empty order array | ||
sortOrder.length = 0; | ||
|
||
for (var i = 0; i < itemElems.length; i++) { | ||
sortOrder[i] = itemElems[i].getAttribute('data-panel'); | ||
} | ||
|
||
// save ordering | ||
localStorage.setItem('panelSortingOrder', JSON.stringify(sortOrder)); | ||
} | ||
|
||
$packeryContainer.packery('on', 'layoutComplete', orderItems); | ||
$packeryContainer.packery('on', 'dragItemPositioned', orderItems); | ||
} | ||
|
||
function applyPackery() { | ||
if (isPackeryInit) $packeryContainer.packery(); | ||
} | ||
if (isPackeryInit) $packeryContainer.packery(); | ||
} |