Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 10 additions & 47 deletions assets/js/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {});
Expand All @@ -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
*/
Expand All @@ -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');
Expand All @@ -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);
Expand Down