Skip to content

Commit

Permalink
minor #10021 [WebProfilerBundle] Simplified session storage implement…
Browse files Browse the repository at this point in the history
…ation (bschussek)

This PR was merged into the 2.5-dev branch.

Discussion
----------

[WebProfilerBundle] Simplified session storage implementation

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Follow-up to #9967.

Commits
-------

cec05bf [WebProfilerBundle] Simplified session storage implementation
  • Loading branch information
fabpot committed Jan 15, 2014
2 parents fff29a3 + cec05bf commit 7da803f
Showing 1 changed file with 68 additions and 61 deletions.
Expand Up @@ -199,21 +199,36 @@
{% endif %}

<script>
function Toggler(togglerStorage) {
function Toggler(storage) {
"use strict";
var expand = function (button) {
var STORAGE_KEY = 'sf_toggle_data',
states = {},
isCollapsed = function (button) {
return Sfjs.hasClass(button, 'closed');
},
isExpanded = function (button) {
return !isCollapsed(button);
},
expand = function (button) {
var targetId = button.dataset.toggleTargetId,
target = document.getElementById(targetId);
if (!target) {
throw "Toggle target " + targetId + " does not exist";
}
togglerStorage.store(targetId, 'visible');
if (isCollapsed(button)) {
Sfjs.removeClass(button, 'closed');
Sfjs.removeClass(target, 'hidden');
Sfjs.removeClass(button, 'closed');
Sfjs.removeClass(target, 'hidden');
states[targetId] = 1;
storage.setItem(STORAGE_KEY, states);
}
},
collapse = function (button) {
Expand All @@ -224,10 +239,13 @@
throw "Toggle target " + targetId + " does not exist";
}
togglerStorage.store(targetId, 'hidden');
if (isExpanded(button)) {
Sfjs.addClass(button, 'closed');
Sfjs.addClass(target, 'hidden');
Sfjs.addClass(button, 'closed');
Sfjs.addClass(target, 'hidden');
states[targetId] = 0;
storage.setItem(STORAGE_KEY, states);
}
},
toggle = function (button) {
Expand All @@ -239,16 +257,26 @@
},
initButtons = function (buttons) {
states = storage.getItem(STORAGE_KEY, {});
// must be an object, not an array or anything else
// `typeof` returns "object" also for arrays, so the following
// check must be done
// see http://stackoverflow.com/questions/4775722/check-if-object-is-array
if ('[object Object]' !== Object.prototype.toString.call(states)) {
states = {};
}
for (var i = 0, l = buttons.length; i < l; ++i) {
var toggleTargetId = buttons[i].dataset.toggleTargetId,
toggleTarget = document.getElementById(toggleTargetId);
var targetId = buttons[i].dataset.toggleTargetId,
target = document.getElementById(targetId);
if (!toggleTarget) {
throw "Toggle target " + toggleTargetId + " does not exist";
if (!target) {
throw "Toggle target " + targetId + " does not exist";
}
// correct the initial state of the button
if (Sfjs.hasClass(toggleTarget, 'hidden')) {
if (Sfjs.hasClass(target, 'hidden')) {
Sfjs.addClass(buttons[i], 'closed');
}
Expand All @@ -261,6 +289,15 @@
return false;
});
if (states.hasOwnProperty(targetId)) {
// open or collapse based on stored data
if (0 === states[targetId]) {
collapse(buttons[i]);
} else {
expand(buttons[i]);
}
}
}
};
Expand All @@ -269,66 +306,38 @@
toggle: toggle,
isExpanded: isExpanded,
isCollapsed: isCollapsed,
expand: expand,
collapse: collapse
};
}
function TogglerStorage(key) {
var key = 'sf_' + (key || 'toggle_data'),
store = function (id, state) {
var toggleData = sessionStorage.getItem(key);
if (!toggleData) {
toggleData = [];
} else {
toggleData = toggleData.split('|');
}
if ('visible' == state) {
toggleData.push(id);
} else {
var index = toggleData.indexOf(id);
if (-1 < index) {
toggleData.splice(index, 1);
}
}
sessionStorage.setItem(key, toggleData.join('|'));
function JsonStorage(storage) {
var setItem = function (key, data) {
storage.setItem(key, JSON.stringify(data));
},
initStorage = function (buttonsSelector) {
var toggleData = sessionStorage.getItem(key);
if (!toggleData) {
return;
}
toggleData = toggleData.split('|');
var buttons = document.getElementsByClassName(buttonsSelector || 'toggle-button');
for (i in toggleData) {
var element = document.getElementById(toggleData[i]);
if (!element) {
continue;
}
if (Sfjs.hasClass(element, 'hidden')) {
for (var i = -1; button = buttons[++i]; ) {
if (button.dataset.toggleTargetId && button.dataset.toggleTargetId == element.getAttribute('id')) {
break;
}
}
getItem = function (key, defaultValue) {
var data = storage.getItem(key);
Sfjs.removeClass(element, 'hidden');
Sfjs.removeClass(button, 'closed');
if (null !== data) {
try {
return JSON.parse(data);
} catch(e) {
}
}
return defaultValue;
};
return {
store: store,
setItem: setItem,
initStorage: initStorage
getItem: getItem
};
}
Expand Down Expand Up @@ -396,12 +405,10 @@
}
var tabTarget = new TabView(),
storage = new TogglerStorage(),
toggler = new Toggler(storage);
toggler = new Toggler(new JsonStorage(sessionStorage));
tabTarget.initTabs(document.querySelectorAll('.tree .tree-inner'));
toggler.initButtons(document.querySelectorAll('a.toggle-button'));
storage.initStorage();
</script>
{% endblock %}

Expand Down

0 comments on commit 7da803f

Please sign in to comment.