Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu state #10

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 32 additions & 2 deletions src/metisMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

var pluginName = "metisMenu",
defaults = {
toggle: true
toggle: true,
cookieName: "MetisMenuState",
cookieExpiration: 1 //Number of days
};

function Plugin(element, options) {
Expand All @@ -17,7 +19,18 @@
init: function () {

var $this = $(this.element),
$toggle = this.settings.toggle;
$toggle = this.settings.toggle,
$cookieName = this.settings.cookieName,
$cookieExpiration = this.settings.cookieExpiration;

//Restores the menu state from cookies
var documentCookie = document.cookie;
$this.find("li").has("ul").each(function (i) {
var pos = documentCookie.indexOf($cookieName + "_" + i + "=");
if (pos > -1) {
documentCookie.substr(pos).split("=")[1].indexOf("false") ? $(this).addClass("active") : $(this).removeClass("active");
}
});

if (this.isIE() <= 9) {
$this.find("li.active").has("ul").children("ul").collapse("show");
Expand All @@ -35,6 +48,23 @@
if ($toggle) {
$(this).parent("li").siblings().removeClass("active").children("ul.in").collapse("hide");
}

//Deletes all cookies
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
if (name.trim().slice(0, $cookieName.length) === $cookieName) {
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
}
}
//Stores the menu state in cookies
var date = new Date();
date.setTime(date.getTime() + ($cookieExpiration * 24 * 60 * 60 * 1000));
$this.find("li").has("ul").each(function (i) {
document.cookie = $cookieName + "_" + i + "=" + $(this).hasClass("active") + ";expires=" + date.toGMTString() + ";path=/";
});
});
},

Expand Down