diff --git a/assets/js/sidebar.js b/assets/js/sidebar.js index dd0ea5b..5cc1267 100644 --- a/assets/js/sidebar.js +++ b/assets/js/sidebar.js @@ -6,15 +6,15 @@ // Collapsed class collapsedClass: 'is-collapsed', - // Storage key - storageKey: '_sassdoc_sidebar_index', - // Index attribute indexAttribute: 'data-slug', // Toggle button toggleBtn: '.js-btn-toggle', + // Initial collapse + initialCollapse: true, + // Automatic initialization init: true }, conf || {}); @@ -30,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 */ @@ -62,44 +50,16 @@ 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 */ 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); - // Toggle all - $(this.conf.toggleBtn).on('click', $.proxy(function (event) { + $toggleBtn.on('click', $.proxy(function (event) { $node = $(event.target); text = $node.attr('data-alt'); @@ -118,9 +78,12 @@ }, this)); collapsed = !collapsed; - this.save(); }, this)); + if (this.conf.initialCollapse !== false) { + $toggleBtn.trigger('click'); + } + // Toggle item this.conf.nodes.on('click', $.proxy(function (event) { $item = $(event.target);