From 7e2b160a9c935b5397fc8eb45751c6bcb422c9e2 Mon Sep 17 00:00:00 2001 From: Hugo Giraudel Date: Sat, 11 Oct 2014 19:43:02 +0200 Subject: [PATCH 1/2] Removed localStorage save but added an option to collapse groups on load --- assets/js/sidebar.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/assets/js/sidebar.js b/assets/js/sidebar.js index dd0ea5b..7dd1d2e 100644 --- a/assets/js/sidebar.js +++ b/assets/js/sidebar.js @@ -15,6 +15,9 @@ // Toggle button toggleBtn: '.js-btn-toggle', + // Initial collapse + initialCollapse: true, + // Automatic initialization init: true }, conf || {}); @@ -91,15 +94,16 @@ */ Sidebar.prototype.bind = function () { var $item, slug, fn, text; + var $toggleBtn = $(this.conf.toggleBtn); var collapsed = false; // Save index in localStorage global.onbeforeunload = $.proxy(function () { - this.save(); + // this.save(); // Seems to do more harm than good }, this); // Toggle all - $(this.conf.toggleBtn).on('click', $.proxy(function (event) { + $toggleBtn.on('click', $.proxy(function (event) { $node = $(event.target); text = $node.attr('data-alt'); @@ -121,6 +125,10 @@ this.save(); }, this)); + if (global.localStorage.getItem(this.conf.storageKey) === null && this.conf.initialCollapse !== false) { + $toggleBtn.trigger('click'); + } + // Toggle item this.conf.nodes.on('click', $.proxy(function (event) { $item = $(event.target); @@ -128,6 +136,7 @@ // Update index this.index[slug] = !this.index[slug]; + this.save(); // Update DOM $item.toggleClass(this.conf.collapsedClass); From b91905d85739cee3ac3a87a882588e18ccbe2b44 Mon Sep 17 00:00:00 2001 From: Hugo Giraudel Date: Mon, 24 Nov 2014 22:43:16 +0100 Subject: [PATCH 2/2] Removed localStorage altogether --- assets/js/sidebar.js | 50 ++------------------------------------------ 1 file changed, 2 insertions(+), 48 deletions(-) diff --git a/assets/js/sidebar.js b/assets/js/sidebar.js index 7dd1d2e..5cc1267 100644 --- a/assets/js/sidebar.js +++ b/assets/js/sidebar.js @@ -6,9 +6,6 @@ // Collapsed class collapsedClass: 'is-collapsed', - // Storage key - storageKey: '_sassdoc_sidebar_index', - // Index attribute indexAttribute: 'data-slug', @@ -33,22 +30,10 @@ Sidebar.prototype.initialize = function () { this.conf.nodes = $('[' + this.conf.indexAttribute + ']'); - this.load(); - this.updateDOM(); + this.index = this.buildIndex(); this.bind(); }; - /** - * Load data from storage or create fresh index - */ - Sidebar.prototype.load = function () { - var index = 'localStorage' in global ? - global.localStorage.getItem(this.conf.storageKey) : - null; - - this.index = index ? JSON.parse(index) : this.buildIndex(); - }; - /** * Build a fresh index */ @@ -65,30 +50,6 @@ return index; }; - /** - * Update DOM based on index - */ - Sidebar.prototype.updateDOM = function () { - var item; - - for (item in this.index) { - if (this.index[item] === false) { - $('[' + this.conf.indexAttribute + '="' + item + '"]').addClass(this.conf.collapsedClass); - } - } - }; - - /** - * Save index in storage - */ - Sidebar.prototype.save = function () { - if (!('localStorage' in global)) { - return; - } - - global.localStorage.setItem(this.conf.storageKey, JSON.stringify(this.index)); - }; - /** * Bind UI events */ @@ -97,11 +58,6 @@ var $toggleBtn = $(this.conf.toggleBtn); var collapsed = false; - // Save index in localStorage - global.onbeforeunload = $.proxy(function () { - // this.save(); // Seems to do more harm than good - }, this); - // Toggle all $toggleBtn.on('click', $.proxy(function (event) { $node = $(event.target); @@ -122,10 +78,9 @@ }, this)); collapsed = !collapsed; - this.save(); }, this)); - if (global.localStorage.getItem(this.conf.storageKey) === null && this.conf.initialCollapse !== false) { + if (this.conf.initialCollapse !== false) { $toggleBtn.trigger('click'); } @@ -136,7 +91,6 @@ // Update index this.index[slug] = !this.index[slug]; - this.save(); // Update DOM $item.toggleClass(this.conf.collapsedClass);