Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ Preserve expanded sub-menus between session. If jquery.cookie is not included it

----------

#### storageType:
* **Type:** `string`
* **Default:** `cookie`

Choose the storage type you want between a cookie and an html session storage : 'cookie' or 'local'

----------

#### cookie:
* **Type:** `object`
* `name`: Cookie name
Expand Down
6 changes: 5 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
$(document).ready(function() {
// Initialize navgoco with default options
$("#demo1").navgoco({
storageType: 'local',
caretHtml: '',
accordion: false,
openClass: 'open',
Expand Down Expand Up @@ -49,7 +50,10 @@
</script>
<script type="text/javascript" id="demo2-javascript">
$(document).ready(function() {
$("#demo2").navgoco({accordion: true});
$("#demo2").navgoco({
storageType: 'local',
accordion: true
});
});
</script>
</head>
Expand Down
39 changes: 31 additions & 8 deletions src/jquery.navgoco.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*
* Copyright (c) 2014 Chris T (@tefra)
* BSD - https://github.com/tefra/navgoco/blob/master/LICENSE-BSD
*
* THIS IS A FORK
*/
(function($) {

Expand Down Expand Up @@ -170,15 +172,25 @@
* saved with a cookie. For size reasons only the open sub-menus indexes are stored. *
*/
_save: function() {

if (this.options.save) {
var save = {};
for (var key in this.state) {
if (this.state[key] === 1) {
save[key] = 1;
}
}
cookie[this.uuid] = this.state = save;
$.cookie(this.options.cookie.name, JSON.stringify(cookie), this.options.cookie);
//save data with a cookie or localStorage
if(this.options.storageType === 'local' && typeof(Storage) !== "undefined"){
lclStorage = (lclStorage === null) ? {}: lclStorage;
lclStorage[this.uuid] = this.state = save;
var $strLocal = JSON.stringify(lclStorage);
sessionStorage.setItem(this.options.cookie.name, $strLocal);
} else {
cookie[this.uuid] = this.state = save;
$.cookie(this.options.cookie.name, JSON.stringify(cookie), this.options.cookie);
}

}
},
/**
Expand All @@ -187,11 +199,20 @@
*/
_load: function() {
if (this.options.save) {
if (cookie === null) {
var data = $.cookie(this.options.cookie.name);
cookie = (data) ? JSON.parse(data) : {};

if(this.options.storageType === 'local' && typeof(Storage) !== "undefined"){
var dataStrg = sessionStorage.getItem(this.options.cookie.name);
var lclStorage = (dataStrg && dataStrg !== 'undefined') ? JSON.parse(dataStrg) : {};
this.state = lclStorage.hasOwnProperty(this.uuid) ? lclStorage[this.uuid] : {};
} else if(this.options.storageType === 'cookie') {
if (cookie === null) {
var data = $.cookie(this.options.cookie.name);
cookie = (data) ? JSON.parse(data) : {};
}
this.state = cookie.hasOwnProperty(this.uuid) ? cookie[this.uuid] : {};
}
this.state = cookie.hasOwnProperty(this.uuid) ? cookie[this.uuid] : {};


}
},
/**
Expand Down Expand Up @@ -263,7 +284,7 @@
args = Array.prototype.slice.call(arguments, 1);
} else {
options = $.extend({}, $.fn.navgoco.defaults, options || {});
if (!$.cookie) {
if (!$.cookie && options.storageType === 'cookie') {
options.save = false;
}
}
Expand All @@ -285,14 +306,16 @@
*
* @type {Object}
*/
var cookie = null;
var cookie = null,
lclStorage = null;

/**
* Default navgoco options
*
* @type {Object}
*/
$.fn.navgoco.defaults = {
storageType: 'cookie',
caretHtml: '',
accordion: false,
openClass: 'open',
Expand Down
9 changes: 1 addition & 8 deletions src/jquery.navgoco.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.