Skip to content

Commit

Permalink
Create storage interface implement it in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodeholic committed Dec 18, 2017
1 parent dd27016 commit ef5ca15
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 57 deletions.
77 changes: 55 additions & 22 deletions dist/js/lobipanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ $.fn.enableSelection = function () {
$(function () {
var STORAGE_PREFIX = 'lobipanel_';

var StorageLocal = function(){
this.saveChildPositions = function(parentInnerId, positions){
console.log(parentInnerId, positions);
if (positions !== undefined) {
localStorage.setItem(STORAGE_PREFIX + 'parent_' + parentInnerId, JSON.stringify(positions));
}
};

this.savePanelParams = function(innerId, storage){
localStorage.setItem(STORAGE_PREFIX + innerId, JSON.stringify(storage));
};

this.getAllPanelPositions = function(){
var parents = [];
for (var i in localStorage) {
if (i.indexOf(STORAGE_PREFIX + 'parent_') === 0) {
var innerParentId = i.replace(STORAGE_PREFIX + 'parent_', '');
var $parent = $('.lobipanel-parent-sortable[data-inner-id=' + innerParentId + ']');
if ($parent.length) {
parents[innerParentId] = JSON.parse(localStorage[i]);
}
}
}
return parents;
};

this.getPanelStorage = function(innerId){
var item = localStorage.getItem(STORAGE_PREFIX + innerId);
return JSON.parse(item || null) || {};
};

};

var LobiPanel = function ($el, options) {
var me = this;

Expand Down Expand Up @@ -169,8 +202,10 @@ $(function () {


if (!me.hasRandomId) {
me.storage = localStorage.getItem(STORAGE_PREFIX + me.innerId);
me.storage = JSON.parse(me.storage) || {};
if (!this.storageObject){
this.storageObject = new StorageLocal();
}
me.storage = this.storageObject.getPanelStorage(me.innerId);
}
var opts = me._getOptionsFromAttributes();
// window.console.log(opts);
Expand Down Expand Up @@ -1648,7 +1683,7 @@ $(function () {
},

savepanelPositions: function () {

var me = this;
var $parents = $('.lobipanel-parent-sortable');
$parents.each(function (index, parent) {
var $parent = $(parent);
Expand All @@ -1664,7 +1699,7 @@ $(function () {
var $el = $(el);
positions[$el.data('inner-id')] = index;
});
localStorage.setItem(STORAGE_PREFIX + 'parent_' + parentInnerId, JSON.stringify(positions));
me.storageObject.saveChildPositions(parentInnerId, positions);
});
},

Expand Down Expand Up @@ -1771,28 +1806,26 @@ $(function () {
},
_saveLocalStorage: function (storage) {
var me = this;
localStorage.setItem(STORAGE_PREFIX + me.innerId, JSON.stringify(storage));
me.storageObject.savePanelParams(me.innerId, storage);
},
_applyState: function (state, params) {
var me = this;
switch (state) {
case 'pinned':
// console.log(localStorage);
for (var i in localStorage) {
if (i.indexOf(STORAGE_PREFIX + 'parent_') === 0) {
var innerParentId = i.replace(STORAGE_PREFIX + 'parent_', '');
var $parent = $('.lobipanel-parent-sortable[data-inner-id=' + innerParentId + ']');
if ($parent.length) {
var panelPositions = JSON.parse(localStorage[i]);
// console.log(panelPositions);
for (var j in panelPositions) {
var $panel = $('[data-inner-id=' + j + ']');
me._removeInnerIdFromParent($panel.data('inner-id'));
me._appendInnerIdToParent($parent, $panel.data('inner-id'));
if (!$panel.hasClass('panel-unpin') && !$panel.hasClass('panel-expanded')) {
$panel.insertAt(panelPositions[j], $parent);
}
}
var allPanelPositions = me.storageObject.getAllPanelPositions();
// console.log(allPanelPositions);
for (var i in allPanelPositions) {
var panelPositions = allPanelPositions[i];
console.log("8888888888 p, anelPositions);
for (var j in panelPositions) {
var $panel = $('[data-inner-id=' + j + ']');
console.log($panel);
me._removeInnerIdFromParent($panel.data('inner-id'));
me._appendInnerIdToParent($parent, $panel.data('inner-id'));
if (!$panel.hasClass('panel-unpin') && !$panel.hasClass('panel-expanded')) {
debugger;
console.log(panelPositions[j], $parent);
// $panel.insertAt(panelPositions[j], $parent);
}
}
}
Expand Down Expand Up @@ -2002,7 +2035,7 @@ $(function () {
text: '#FFF'
}
],

storageObject: null,

// Events
/**
Expand Down
Loading

0 comments on commit ef5ca15

Please sign in to comment.