Skip to content

Commit

Permalink
Support js-cookie
Browse files Browse the repository at this point in the history
Close #443
  • Loading branch information
mar10 committed Jun 23, 2015
1 parent 5bed4a6 commit cd4d6a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 2.10.0-0 / Unreleased
* [Changed] New dist folder layout: moved skin-* folders into src/ folder
* [Improved] #443 Support js-cookie (still compatible with jquery-cookie)
* [Fixed] #415 selected and unselectable shows unchecked checkbox
* [Fixed] #427 table + themeroller: apply color to TR
* [Fixed] #442 filterBranches shall use opts to allow autoExpand
Expand Down
8 changes: 4 additions & 4 deletions demo/sample-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
},
lazyLoad: function(event, data){
// Simulate a slow ajax request
var dfd = new $.Deferred()
data.result = dfd;
var dfd = new $.Deferred();
data.result = dfd.promise();
window.setTimeout(function(){
dfd.resolve([
{ title: 'Lazy node 1', lazy: true },
{ title: 'Simple node 2', select: true }
{ title: "Lazy node 1", lazy: true },
{ title: "Simple node 2", select: true }
]);
}, 1500);
}
Expand Down
3 changes: 3 additions & 0 deletions demo/sample-ext-persist.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>
<!--
<script src="../lib/jquery.cookie.js" type="text/javascript"></script>
-->
<script src="//cdnjs.cloudflare.com/ajax/libs/js-cookie/2.0.1/js.cookie.min.js"></script>

<link href="../src/skin-win7/ui.fancytree.css" rel="stylesheet" type="text/css">
<script src="../src/jquery.fancytree.js" type="text/javascript"></script>
Expand Down
25 changes: 18 additions & 7 deletions src/jquery.fancytree.persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Persist tree status in cookiesRemove or highlight tree nodes, based on a filter.
* (Extension module for jquery.fancytree.js: https://github.com/mar10/fancytree/)
*
* @depends: jquery.cookie.js
* @depends: js-cookie or jquery-cookie
*
* Copyright (c) 2008-2015, Martin Wendt (http://wwWendt.de)
*
Expand All @@ -18,17 +18,28 @@
;(function($, window, document, undefined) {

"use strict";

/* global Cookies:false */

/*******************************************************************************
* Private functions and variables
*/
var _assert = $.ui.fancytree.assert,
var cookieGetter, cookieRemover, cookieSetter,
_assert = $.ui.fancytree.assert,
ACTIVE = "active",
EXPANDED = "expanded",
FOCUS = "focus",
SELECTED = "selected";

if( typeof Cookies === "function" ) {
// Assume https://github.com/js-cookie/js-cookie
cookieSetter = Cookies.set;
cookieGetter = Cookies.get;
cookieRemover = Cookies.remove;
} else {
// Fall back to https://github.com/carhartl/jquery-cookie
cookieSetter = cookieGetter = $.cookie;
cookieRemover = $.removeCookie;
}

/* Recursively load lazy nodes
* @param {string} mode 'load', 'expand', false
Expand Down Expand Up @@ -163,18 +174,18 @@ $.ui.fancytree.registerExtension({
var ls = this._local.localStorage; // null, sessionStorage, or localStorage

if( value === undefined ) {
return ls ? ls.getItem(key) : $.cookie(key);
return ls ? ls.getItem(key) : cookieGetter(key);
} else if ( value === null ) {
if( ls ) {
ls.removeItem(key);
} else {
$.removeCookie(key);
cookieRemover(key);
}
} else {
if( ls ) {
ls.setItem(key, value);
} else {
$.cookie(key, value, this.options.persist.cookie);
cookieSetter(key, value, this.options.persist.cookie);
}
}
},
Expand Down Expand Up @@ -207,7 +218,7 @@ $.ui.fancytree.registerExtension({
instOpts = this.options.persist;

// For 'auto' or 'cookie' mode, the cookie plugin must be available
_assert(instOpts.store === "localStore" || $.cookie, "Missing required plugin for 'persist' extension: jquery.cookie.js");
_assert(instOpts.store === "localStore" || cookieGetter, "Missing required plugin for 'persist' extension: js.cookie.js or jquery.cookie.js");

local.cookiePrefix = instOpts.cookiePrefix || ("fancytree-" + tree._id + "-");
local.storeActive = instOpts.types.indexOf(ACTIVE) >= 0;
Expand Down

0 comments on commit cd4d6a1

Please sign in to comment.