comparison d3s_examples/python-neo4jrestclient/static/platin/lib/jquery/jquery.remember.js @ 8:18ef6948d689

new d3s examples
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Thu, 01 Oct 2015 17:17:27 +0200
parents
children
comparison
equal deleted inserted replaced
7:45dad9e38c82 8:18ef6948d689
1 /*!
2 *
3 * jQuery Remember Plugin
4 * Version 0.1.1
5 *
6 * Copyright Nick Dreckshage, licensed GPL & MIT
7 * https://github.com/ndreckshage/jquery-remember
8 *
9 * A fork of jQuery Cookie Plugin
10 * https://github.com/carhartl/jquery-cookie
11 * Copyright Klaus Hartl
12 * Released under the MIT licensed
13 *
14 */
15
16 (function($){
17
18 // recommend changing to return function(options) { if using require.js...
19 $.remember = function(options){
20 var settings,
21 remember,
22 controller;
23
24 settings = $.extend({
25 name: null, // name/key of the cookie/localstorage item
26 value: undefined, // value pair of cookie/localstorage
27 getSet: false, // if true, will get if available, set if not. default is to just get OR set
28 remove: false, // if true, will remove based on name/key
29 use: 'default', // whether to use localstorage or cookies. default localstorage with cookie fallback.
30 expires: null, // forces cookie (invalid localstorage attribue).
31 path: null, // forces cookie.
32 domain: null, // forces cookie.
33 secure: null, // forces cookie.
34 json: false, // will convert to json when set. parse with get.
35 fallback: true, // whether to fallback to cookies if localstorage not available.
36 raw: false, // if true, will skip uri encoding/decoding
37 modernizr: false // set true if youd rather handle localstorage detection through modernizr
38 }, options);
39
40 remember = {
41 init: function(){
42 var controller;
43
44 // controls what to do with the input. set - get - set/get - erase
45 // set if name exists, value exists, and not set to remove cookie.
46 // get if name exists, value does not exist, and not set to remove
47 // remove if remove set to true
48 if (settings.name !== null && settings.value !== undefined && settings.remove !== true){
49 if (settings.getSet === true){
50 var get = this.get();
51 // if the value of get exists, return it. otherwise, set the value as specified.
52 if (get === null){
53 this.set();
54 } else {
55 controller = get;
56 }
57 } else {
58 this.set();
59 }
60 } else if (settings.name !== null && settings.value === undefined && settings.remove !== true){
61 controller = this.get();
62 } else if (settings.name !== null && settings.remove === true){
63 this.erase();
64 }
65
66 // will return result of everything to calling js
67 return controller;
68 },
69 get: function(){
70 var use = this._use(),
71 value = null,
72 cookies,
73 parts,
74 name;
75
76 // grab the key value pair from localstorage
77 if (use === 'localstorage') {
78 value = localStorage.getItem(settings.name);
79 }
80
81 // hit if cookie requested, and when double checking a get on an empty localstorage item
82 if ((use === 'cookie') || (use === 'localstorage' && value === null && settings.fallback !== false)) {
83
84 // grab all the cookies from the browser, check if set, and loop through
85 cookies = document.cookie ? document.cookie : null;
86 if (cookies !== null){
87 cookies = cookies.split(';');
88 for (var i = 0; i < cookies.length; i++){
89 // separate the key value pair
90 parts = cookies[i].split('=');
91 // set name and value to split parts, cleaning up whitespace
92 name = parts.shift();
93 name = settings.raw === false ? this._trim(this._decode(name)) : this._trim(name);
94 value = parts[0];
95 // break when we hit a match, or cookie is empty
96 if (settings.name === name) {
97 break;
98 } else if (settings.fallback !== false) {
99 value = localStorage.getItem(settings.name) || null;
100 } else {
101 value = null;
102 }
103 }
104 }
105 }
106
107 // decode uri and if json is requested, parse the cookie/localstorage and return to controller
108 value = (value && settings.raw === false) ? this._decode(value) : value;
109 value = (value && settings.json === true) ? JSON.parse(value) : value;
110
111 return value;
112 },
113 set: function(){
114 var use = this._use();
115
116 // if set is hit, user has intentionally tried to set (get/set not hit)
117 // clear the storage alternative, so the same value isnt stored in both
118 this.erase();
119
120 // convert the value to store in json if requested
121 settings.value = settings.json === true ? JSON.stringify(settings.value) : settings.value;
122
123 // encode
124 settings.name = settings.raw === false ? encodeURIComponent(settings.name) : settings.name;
125 settings.value = settings.raw === false ? encodeURIComponent(settings.value) : settings.value;
126
127 // store the key value pair in appropriate storage. set unless storage requirements failed
128 if (use === 'localstorage'){
129 localStorage.setItem(settings.name, settings.value);
130 } else if (use !== false){
131 // convert values that cant be stored and set
132 settings.value = settings.value === null ? 'null' : settings.value;
133 this._setCookie();
134 }
135 },
136 erase: function(){
137 var use = this._use();
138
139 // clear localstorage and cookies by setting expiration to negative
140 if (use !== 'cookie' || settings.fallback !== false){
141 localStorage.removeItem(settings.name);
142 }
143 if (use !== 'localstorage' || settings.fallback !== false){
144 this._setCookie('', -1);
145 }
146 },
147 _use: function(){
148 var use,
149 localStorageSupport = this._localStorage();
150
151 // if cookie requested, or any options set that only apply to cookies
152 if (settings.use === 'cookie' || settings.expires !== null || settings.path !== null || settings.domain !== null || settings.secure !== null){
153 use = 'cookie';
154 } else {
155 // use local storage if available
156 if (localStorageSupport){
157 use = 'localstorage';
158 } else if (settings.fallback !== false) {
159 // default to cookie, unless fallback banned
160 use = 'cookie';
161 } else {
162 // if all this fails, nothing can be set
163 use = false;
164 }
165 }
166
167 return use;
168 },
169 _setCookie: function(){
170 // allow for varying parameters with defaults. value then expires as optional params
171 var value = arguments.length > 0 ? arguments[0] : settings.value,
172 expires = arguments.length > 1 ? arguments[1] : settings.expires,
173 expire;
174
175 // set a date in the future (or negative to delete) based on expires date offset
176 if (typeof expires === 'number') {
177 expire = new Date();
178 expire.setDate(expire.getDate() + expires);
179 }
180
181 // set the cookies with all the varying settings
182 document.cookie = [
183 settings.name,
184 '=',
185 value,
186 expire ? '; expires=' + expire.toUTCString() : '',
187 settings.path ? '; path=' + settings.path : '',
188 settings.domain ? '; domain=' + settings.domain : '',
189 settings.secure ? '; secure' : ''
190 ].join('');
191 },
192 _localStorage: function(){
193 if (settings.modernizr === true && typeof Modernizr !== 'undefined'){
194 return Modernizr.localstorage;
195 } else {
196 // check if a browser supports localstorage with simple try catch
197 try {
198 localStorage.setItem('jquery-remember-test','jquery-remember-test');
199 localStorage.removeItem('jquery-remember-test');
200 return true;
201 } catch(e){
202 return false;
203 }
204 }
205 },
206 _trim: function(s){
207 // trail a strings leading/ending whitespace
208 return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
209 },
210 _decode: function(s){
211 return decodeURIComponent(s.replace(/\+/g, ' '));
212 }
213 };
214
215 return remember.init();
216 };
217
218 return $.remember;
219
220 }(jQuery));