|
1 | | -/*! |
2 | | - * jQuery Cookie Plugin v1.3 |
3 | | - * https://github.com/carhartl/jquery-cookie |
4 | | - * |
5 | | - * Copyright 2011, Klaus Hartl |
6 | | - * Dual licensed under the MIT or GPL Version 2 licenses. |
7 | | - * http://www.opensource.org/licenses/mit-license.php |
8 | | - * http://www.opensource.org/licenses/GPL-2.0 |
9 | | - */ |
10 | | -import $ from 'jquery'; |
11 | | -const jQuery = $; |
12 | | - |
13 | | -(function($, document, undefined) { |
14 | | - var pluses = /\+/g; |
15 | | - |
16 | | - function raw(s) { |
17 | | - return s; |
18 | | - } |
19 | | - |
20 | | - function decoded(s) { |
21 | | - return decodeURIComponent(s.replace(pluses, ' ')); |
22 | | - } |
23 | | - |
24 | | - var config = ($.cookie = function(key, value, options) { |
25 | | - // write |
26 | | - if (value !== undefined) { |
27 | | - options = $.extend({}, config.defaults, options); |
28 | | - |
29 | | - if (value === null) { |
30 | | - options.expires = -1; |
31 | | - } |
32 | | - |
33 | | - if (typeof options.expires === 'number') { |
34 | | - var days = options.expires, |
35 | | - t = (options.expires = new Date()); |
36 | | - t.setDate(t.getDate() + days); |
37 | | - } |
38 | | - |
39 | | - value = config.json ? JSON.stringify(value) : String(value); |
40 | | - |
41 | | - return (document.cookie = [ |
42 | | - encodeURIComponent(key), |
43 | | - '=', |
44 | | - config.raw ? value : encodeURIComponent(value), |
45 | | - options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
46 | | - options.path ? '; path=' + options.path : '', |
47 | | - options.domain ? '; domain=' + options.domain : '', |
48 | | - options.secure ? '; secure' : '', |
49 | | - ].join('')); |
50 | | - } |
51 | | - |
52 | | - // read |
53 | | - var decode = config.raw ? raw : decoded; |
54 | | - var cookies = document.cookie.split('; '); |
55 | | - for (var i = 0, l = cookies.length; i < l; i++) { |
56 | | - var parts = cookies[i].split('='); |
57 | | - if (decode(parts.shift()) === key) { |
58 | | - var cookie = decode(parts.join('=')); |
59 | | - return config.json ? JSON.parse(cookie) : cookie; |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - return null; |
64 | | - }); |
65 | | - |
66 | | - config.defaults = {}; |
67 | | - |
68 | | - $.removeCookie = function(key, options) { |
69 | | - if ($.cookie(key) !== null) { |
70 | | - $.cookie(key, null, options); |
71 | | - return true; |
72 | | - } |
73 | | - return false; |
74 | | - }; |
75 | | -})(jQuery, document); |
76 | | - |
77 | 1 | /*! |
78 | 2 | * Data Saver |
79 | | - * |
80 | | - * Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com |
81 | | - * Licensed under the MIT license |
82 | 3 | */ |
83 | 4 |
|
| 5 | +import Cookies from 'js-cookie'; |
| 6 | +import deepmerge from 'deepmerge'; |
| 7 | + |
84 | 8 | export const DataSaver = { |
85 | | - // the name of the cookie to store the data in |
86 | | - cookieName: 'patternlab', |
| 9 | + // namespace all cookie names are prefixed with |
| 10 | + namespace: 'patternlab', |
87 | 11 |
|
88 | 12 | /** |
89 | 13 | * Add a given value to the cookie |
90 | 14 | * @param {String} the name of the key |
91 | 15 | * @param {String} the value |
92 | 16 | */ |
93 | | - addValue: function(name, val) { |
94 | | - var cookieVal = $.cookie(this.cookieName); |
95 | | - cookieVal = |
96 | | - cookieVal === null || cookieVal === '' |
97 | | - ? name + '~' + val |
98 | | - : cookieVal + '|' + name + '~' + val; |
99 | | - $.cookie(this.cookieName, cookieVal); |
| 17 | + addValue(name, val) { |
| 18 | + const newData = {}; |
| 19 | + newData[name] = val; |
| 20 | + |
| 21 | + let existingData = {}; |
| 22 | + |
| 23 | + if (Cookies.getJSON(DataSaver.namespace)) { |
| 24 | + existingData = Cookies.getJSON(DataSaver.namespace); |
| 25 | + } |
| 26 | + |
| 27 | + const mergedData = deepmerge(existingData, newData); |
| 28 | + |
| 29 | + Cookies.set(DataSaver.namespace, mergedData, { expires: 7 }); |
100 | 30 | }, |
101 | 31 |
|
102 | 32 | /** |
103 | 33 | * Update a value found in the cookie. If the key doesn't exist add the value |
104 | 34 | * @param {String} the name of the key |
105 | 35 | * @param {String} the value |
106 | 36 | */ |
107 | | - updateValue: function(name, val) { |
108 | | - if (this.findValue(name)) { |
109 | | - var updateCookieVals = ''; |
110 | | - var cookieVals = $.cookie(this.cookieName).split('|'); |
111 | | - for (var i = 0; i < cookieVals.length; i++) { |
112 | | - var fieldVals = cookieVals[i].split('~'); |
113 | | - if (fieldVals[0] == name) { |
114 | | - fieldVals[1] = val; |
115 | | - } |
116 | | - updateCookieVals += |
117 | | - i > 0 |
118 | | - ? '|' + fieldVals[0] + '~' + fieldVals[1] |
119 | | - : fieldVals[0] + '~' + fieldVals[1]; |
120 | | - } |
121 | | - $.cookie(this.cookieName, updateCookieVals); |
122 | | - } else { |
123 | | - this.addValue(name, val); |
124 | | - } |
| 37 | + updateValue(name, val) { |
| 38 | + DataSaver.addValue(name, val); |
125 | 39 | }, |
126 | 40 |
|
127 | 41 | /** |
128 | 42 | * Remove the given key |
129 | 43 | * @param {String} the name of the key |
130 | 44 | */ |
131 | | - removeValue: function(name) { |
132 | | - var updateCookieVals = ''; |
133 | | - var cookieVals = $.cookie(this.cookieName).split('|'); |
134 | | - var k = 0; |
135 | | - for (var i = 0; i < cookieVals.length; i++) { |
136 | | - var fieldVals = cookieVals[i].split('~'); |
137 | | - if (fieldVals[0] != name) { |
138 | | - updateCookieVals += |
139 | | - k === 0 |
140 | | - ? fieldVals[0] + '~' + fieldVals[1] |
141 | | - : '|' + fieldVals[0] + '~' + fieldVals[1]; |
142 | | - k++; |
143 | | - } |
| 45 | + removeValue(name) { |
| 46 | + const currentData = Cookies.getJSON(DataSaver.namespace); |
| 47 | + const updatedData = delete currentData[name]; |
| 48 | + |
| 49 | + if (updatedData.keys()) { |
| 50 | + Cookies.set(DataSaver.namespace, updatedData, { expires: 7 }); |
| 51 | + } else { |
| 52 | + Cookies.remove(DataSaver.namespace); |
144 | 53 | } |
145 | | - $.cookie(this.cookieName, updateCookieVals); |
146 | 54 | }, |
147 | 55 |
|
148 | 56 | /** |
149 | 57 | * Find the value using the given key |
150 | 58 | * @param {String} the name of the key |
151 | | - * |
152 | 59 | * @return {String} the value of the key or false if the value isn't found |
153 | 60 | */ |
154 | | - findValue: function(name) { |
155 | | - if ($.cookie(this.cookieName)) { |
156 | | - var cookieVals = $.cookie(this.cookieName).split('|'); |
157 | | - for (var i = 0; i < cookieVals.length; i++) { |
158 | | - var fieldVals = cookieVals[i].split('~'); |
159 | | - if (fieldVals[0] == name) { |
160 | | - return fieldVals[1]; |
161 | | - } |
| 61 | + findValue(name) { |
| 62 | + const existingData = Cookies.getJSON(DataSaver.namespace); |
| 63 | + |
| 64 | + if (existingData !== undefined) { |
| 65 | + if (existingData[name] !== undefined) { |
| 66 | + return existingData[name]; |
| 67 | + } else { |
| 68 | + return false; |
162 | 69 | } |
| 70 | + } else { |
| 71 | + return false; |
163 | 72 | } |
164 | | - return false; |
165 | 73 | }, |
166 | 74 | }; |
0 commit comments