scratch – Diff between revs 58 and 125
?pathlinks?
Rev 58 | Rev 125 | |||
---|---|---|---|---|
1 | /*! |
1 | /*! |
|
2 | * JavaScript Cookie v2.1.3 |
2 | * JavaScript Cookie v2.1.4 |
|
3 | * https://github.com/js-cookie/js-cookie |
3 | * https://github.com/js-cookie/js-cookie |
|
4 | * |
4 | * |
|
5 | * Copyright 2006, 2015 Klaus Hartl & Fagner Brack |
5 | * Copyright 2006, 2015 Klaus Hartl & Fagner Brack |
|
6 | * Released under the MIT license |
6 | * Released under the MIT license |
|
7 | */ |
7 | */ |
|
8 | ;(function (factory) { |
8 | ;(function (factory) { |
|
9 | var registeredInModuleLoader = false; |
9 | var registeredInModuleLoader = false; |
|
10 | if (typeof define === 'function' && define.amd) { |
10 | if (typeof define === 'function' && define.amd) { |
|
11 | define(factory); |
11 | define(factory); |
|
12 | registeredInModuleLoader = true; |
12 | registeredInModuleLoader = true; |
|
13 | } |
13 | } |
|
14 | if (typeof exports === 'object') { |
14 | if (typeof exports === 'object') { |
|
15 | module.exports = factory(); |
15 | module.exports = factory(); |
|
16 | registeredInModuleLoader = true; |
16 | registeredInModuleLoader = true; |
|
17 | } |
17 | } |
|
18 | if (!registeredInModuleLoader) { |
18 | if (!registeredInModuleLoader) { |
|
19 | var OldCookies = window.Cookies; |
19 | var OldCookies = window.Cookies; |
|
20 | var api = window.Cookies = factory(); |
20 | var api = window.Cookies = factory(); |
|
21 | api.noConflict = function () { |
21 | api.noConflict = function () { |
|
22 | window.Cookies = OldCookies; |
22 | window.Cookies = OldCookies; |
|
23 | return api; |
23 | return api; |
|
24 | }; |
24 | }; |
|
25 | } |
25 | } |
|
26 | }(function () { |
26 | }(function () { |
|
27 | function extend () { |
27 | function extend () { |
|
28 | var i = 0; |
28 | var i = 0; |
|
29 | var result = {}; |
29 | var result = {}; |
|
30 | for (; i < arguments.length; i++) { |
30 | for (; i < arguments.length; i++) { |
|
31 | var attributes = arguments[ i ]; |
31 | var attributes = arguments[ i ]; |
|
32 | for (var key in attributes) { |
32 | for (var key in attributes) { |
|
33 | result[key] = attributes[key]; |
33 | result[key] = attributes[key]; |
|
34 | } |
34 | } |
|
35 | } |
35 | } |
|
36 | return result; |
36 | return result; |
|
37 | } |
37 | } |
|
38 | |
38 | |
|
39 | function init (converter) { |
39 | function init (converter) { |
|
40 | function api (key, value, attributes) { |
40 | function api (key, value, attributes) { |
|
41 | var result; |
41 | var result; |
|
42 | if (typeof document === 'undefined') { |
42 | if (typeof document === 'undefined') { |
|
43 | return; |
43 | return; |
|
44 | } |
44 | } |
|
45 | |
45 | |
|
46 | // Write |
46 | // Write |
|
47 | |
47 | |
|
48 | if (arguments.length > 1) { |
48 | if (arguments.length > 1) { |
|
49 | attributes = extend({ |
49 | attributes = extend({ |
|
50 | path: '/' |
50 | path: '/' |
|
51 | }, api.defaults, attributes); |
51 | }, api.defaults, attributes); |
|
52 | |
52 | |
|
53 | if (typeof attributes.expires === 'number') { |
53 | if (typeof attributes.expires === 'number') { |
|
54 | var expires = new Date(); |
54 | var expires = new Date(); |
|
55 | expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5); |
55 | expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5); |
|
56 | attributes.expires = expires; |
56 | attributes.expires = expires; |
|
57 | } |
57 | } |
|
- | 58 | |
||
- | 59 | // We're using "expires" because "max-age" is not supported by IE |
||
- | 60 | attributes.expires = attributes.expires ? attributes.expires.toUTCString() : ''; |
||
58 | |
61 | |
|
59 | try { |
62 | try { |
|
60 | result = JSON.stringify(value); |
63 | result = JSON.stringify(value); |
|
61 | if (/^[\{\[]/.test(result)) { |
64 | if (/^[\{\[]/.test(result)) { |
|
62 | value = result; |
65 | value = result; |
|
63 | } |
66 | } |
|
64 | } catch (e) {} |
67 | } catch (e) {} |
|
65 | |
68 | |
|
66 | if (!converter.write) { |
69 | if (!converter.write) { |
|
67 | value = encodeURIComponent(String(value)) |
70 | value = encodeURIComponent(String(value)) |
|
68 | .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); |
71 | .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); |
|
69 | } else { |
72 | } else { |
|
70 | value = converter.write(value, key); |
73 | value = converter.write(value, key); |
|
71 | } |
74 | } |
|
72 | |
75 | |
|
73 | key = encodeURIComponent(String(key)); |
76 | key = encodeURIComponent(String(key)); |
|
74 | key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent); |
77 | key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent); |
|
75 | key = key.replace(/[\(\)]/g, escape); |
78 | key = key.replace(/[\(\)]/g, escape); |
|
76 | |
79 | |
|
- | 80 | var stringifiedAttributes = ''; |
||
77 | return (document.cookie = [ |
81 | |
|
78 | key, '=', value, |
- | ||
79 | attributes.expires ? '; expires=' + attributes.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
82 | for (var attributeName in attributes) { |
|
- | 83 | if (!attributes[attributeName]) { |
||
- | 84 | continue; |
||
80 | attributes.path ? '; path=' + attributes.path : '', |
85 | } |
|
81 | attributes.domain ? '; domain=' + attributes.domain : '', |
86 | stringifiedAttributes += '; ' + attributeName; |
|
82 | attributes.secure ? '; secure' : '' |
87 | if (attributes[attributeName] === true) { |
|
- | 88 | continue; |
||
- | 89 | } |
||
- | 90 | stringifiedAttributes += '=' + attributes[attributeName]; |
||
- | 91 | } |
||
83 | ].join('')); |
92 | return (document.cookie = key + '=' + value + stringifiedAttributes); |
|
84 | } |
93 | } |
|
85 | |
94 | |
|
86 | // Read |
95 | // Read |
|
87 | |
96 | |
|
88 | if (!key) { |
97 | if (!key) { |
|
89 | result = {}; |
98 | result = {}; |
|
90 | } |
99 | } |
|
91 | |
100 | |
|
92 | // To prevent the for loop in the first place assign an empty array |
101 | // To prevent the for loop in the first place assign an empty array |
|
93 | // in case there are no cookies at all. Also prevents odd result when |
102 | // in case there are no cookies at all. Also prevents odd result when |
|
94 | // calling "get()" |
103 | // calling "get()" |
|
95 | var cookies = document.cookie ? document.cookie.split('; ') : []; |
104 | var cookies = document.cookie ? document.cookie.split('; ') : []; |
|
96 | var rdecode = /(%[0-9A-Z]{2})+/g; |
105 | var rdecode = /(%[0-9A-Z]{2})+/g; |
|
97 | var i = 0; |
106 | var i = 0; |
|
98 | |
107 | |
|
99 | for (; i < cookies.length; i++) { |
108 | for (; i < cookies.length; i++) { |
|
100 | var parts = cookies[i].split('='); |
109 | var parts = cookies[i].split('='); |
|
101 | var cookie = parts.slice(1).join('='); |
110 | var cookie = parts.slice(1).join('='); |
|
102 | |
111 | |
|
103 | if (cookie.charAt(0) === '"') { |
112 | if (cookie.charAt(0) === '"') { |
|
104 | cookie = cookie.slice(1, -1); |
113 | cookie = cookie.slice(1, -1); |
|
105 | } |
114 | } |
|
106 | |
115 | |
|
107 | try { |
116 | try { |
|
108 | var name = parts[0].replace(rdecode, decodeURIComponent); |
117 | var name = parts[0].replace(rdecode, decodeURIComponent); |
|
109 | cookie = converter.read ? |
118 | cookie = converter.read ? |
|
110 | converter.read(cookie, name) : converter(cookie, name) || |
119 | converter.read(cookie, name) : converter(cookie, name) || |
|
111 | cookie.replace(rdecode, decodeURIComponent); |
120 | cookie.replace(rdecode, decodeURIComponent); |
|
112 | |
121 | |
|
113 | if (this.json) { |
122 | if (this.json) { |
|
114 | try { |
123 | try { |
|
115 | cookie = JSON.parse(cookie); |
124 | cookie = JSON.parse(cookie); |
|
116 | } catch (e) {} |
125 | } catch (e) {} |
|
117 | } |
126 | } |
|
118 | |
127 | |
|
119 | if (key === name) { |
128 | if (key === name) { |
|
120 | result = cookie; |
129 | result = cookie; |
|
121 | break; |
130 | break; |
|
122 | } |
131 | } |
|
123 | |
132 | |
|
124 | if (!key) { |
133 | if (!key) { |
|
125 | result[name] = cookie; |
134 | result[name] = cookie; |
|
126 | } |
135 | } |
|
127 | } catch (e) {} |
136 | } catch (e) {} |
|
128 | } |
137 | } |
|
129 | |
138 | |
|
130 | return result; |
139 | return result; |
|
131 | } |
140 | } |
|
132 | |
141 | |
|
133 | api.set = api; |
142 | api.set = api; |
|
134 | api.get = function (key) { |
143 | api.get = function (key) { |
|
135 | return api.call(api, key); |
144 | return api.call(api, key); |
|
136 | }; |
145 | }; |
|
137 | api.getJSON = function () { |
146 | api.getJSON = function () { |
|
138 | return api.apply({ |
147 | return api.apply({ |
|
139 | json: true |
148 | json: true |
|
140 | }, [].slice.call(arguments)); |
149 | }, [].slice.call(arguments)); |
|
141 | }; |
150 | }; |
|
142 | api.defaults = {}; |
151 | api.defaults = {}; |
|
143 | |
152 | |
|
144 | api.remove = function (key, attributes) { |
153 | api.remove = function (key, attributes) { |
|
145 | api(key, '', extend(attributes, { |
154 | api(key, '', extend(attributes, { |
|
146 | expires: -1 |
155 | expires: -1 |
|
147 | })); |
156 | })); |
|
148 | }; |
157 | }; |
|
149 | |
158 | |
|
150 | api.withConverter = init; |
159 | api.withConverter = init; |
|
151 | |
160 | |
|
152 | return api; |
161 | return api; |
|
153 | } |
162 | } |
|
154 | |
163 | |
|
155 | return init(function () {}); |
164 | return init(function () {}); |
|
156 | })); |
165 | })); |
|
157 | |
166 | |