|
1 /** |
|
2 * LocalStorage.js |
|
3 * |
|
4 * Copyright, Moxiecode Systems AB |
|
5 * Released under LGPL License. |
|
6 * |
|
7 * License: http://www.tinymce.com/license |
|
8 * Contributing: http://www.tinymce.com/contributing |
|
9 */ |
|
10 |
|
11 /** |
|
12 * This class will simulate LocalStorage on IE 7 and return the native version on modern browsers. |
|
13 * Storage is done using userData on IE 7 and a special serialization format. The format is designed |
|
14 * to be as small as possible by making sure that the keys and values doesn't need to be encoded. This |
|
15 * makes it possible to store for example HTML data. |
|
16 * |
|
17 * Storage format for userData: |
|
18 * <base 32 key length>,<key string>,<base 32 value length>,<value>,... |
|
19 * |
|
20 * For example this data key1=value1,key2=value2 would be: |
|
21 * 4,key1,6,value1,4,key2,6,value2 |
|
22 * |
|
23 * @class tinymce.util.LocalStorage |
|
24 * @static |
|
25 * @version 4.0 |
|
26 * @example |
|
27 * tinymce.util.LocalStorage.setItem('key', 'value'); |
|
28 * var value = tinymce.util.LocalStorage.getItem('key'); |
|
29 */ |
|
30 define("tinymce/util/LocalStorage", [], function() { |
|
31 var LocalStorage, storageElm, items, keys, userDataKey, hasOldIEDataSupport; |
|
32 |
|
33 // Check for native support |
|
34 try { |
|
35 if (window.localStorage) { |
|
36 return localStorage; |
|
37 } |
|
38 } catch (ex) { |
|
39 // Ignore |
|
40 } |
|
41 |
|
42 userDataKey = "tinymce"; |
|
43 storageElm = document.documentElement; |
|
44 hasOldIEDataSupport = !!storageElm.addBehavior; |
|
45 |
|
46 if (hasOldIEDataSupport) { |
|
47 storageElm.addBehavior('#default#userData'); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Gets the keys names and updates LocalStorage.length property. Since IE7 doesn't have any getters/setters. |
|
52 */ |
|
53 function updateKeys() { |
|
54 keys = []; |
|
55 |
|
56 for (var key in items) { |
|
57 keys.push(key); |
|
58 } |
|
59 |
|
60 LocalStorage.length = keys.length; |
|
61 } |
|
62 |
|
63 /** |
|
64 * Loads the userData string and parses it into the items structure. |
|
65 */ |
|
66 function load() { |
|
67 var key, data, value, pos = 0; |
|
68 |
|
69 items = {}; |
|
70 |
|
71 // localStorage can be disabled on WebKit/Gecko so make a dummy storage |
|
72 if (!hasOldIEDataSupport) { |
|
73 return; |
|
74 } |
|
75 |
|
76 function next(end) { |
|
77 var value, nextPos; |
|
78 |
|
79 nextPos = end !== undefined ? pos + end : data.indexOf(',', pos); |
|
80 if (nextPos === -1 || nextPos > data.length) { |
|
81 return null; |
|
82 } |
|
83 |
|
84 value = data.substring(pos, nextPos); |
|
85 pos = nextPos + 1; |
|
86 |
|
87 return value; |
|
88 } |
|
89 |
|
90 storageElm.load(userDataKey); |
|
91 data = storageElm.getAttribute(userDataKey) || ''; |
|
92 |
|
93 do { |
|
94 var offset = next(); |
|
95 if (offset === null) { |
|
96 break; |
|
97 } |
|
98 |
|
99 key = next(parseInt(offset, 32) || 0); |
|
100 if (key !== null) { |
|
101 offset = next(); |
|
102 if (offset === null) { |
|
103 break; |
|
104 } |
|
105 |
|
106 value = next(parseInt(offset, 32) || 0); |
|
107 |
|
108 if (key) { |
|
109 items[key] = value; |
|
110 } |
|
111 } |
|
112 } while (key !== null); |
|
113 |
|
114 updateKeys(); |
|
115 } |
|
116 |
|
117 /** |
|
118 * Saves the items structure into a the userData format. |
|
119 */ |
|
120 function save() { |
|
121 var value, data = ''; |
|
122 |
|
123 // localStorage can be disabled on WebKit/Gecko so make a dummy storage |
|
124 if (!hasOldIEDataSupport) { |
|
125 return; |
|
126 } |
|
127 |
|
128 for (var key in items) { |
|
129 value = items[key]; |
|
130 data += (data ? ',' : '') + key.length.toString(32) + ',' + key + ',' + value.length.toString(32) + ',' + value; |
|
131 } |
|
132 |
|
133 storageElm.setAttribute(userDataKey, data); |
|
134 |
|
135 try { |
|
136 storageElm.save(userDataKey); |
|
137 } catch (ex) { |
|
138 // Ignore disk full |
|
139 } |
|
140 |
|
141 updateKeys(); |
|
142 } |
|
143 |
|
144 LocalStorage = { |
|
145 /** |
|
146 * Length of the number of items in storage. |
|
147 * |
|
148 * @property length |
|
149 * @type Number |
|
150 * @return {Number} Number of items in storage. |
|
151 */ |
|
152 //length:0, |
|
153 |
|
154 /** |
|
155 * Returns the key name by index. |
|
156 * |
|
157 * @method key |
|
158 * @param {Number} index Index of key to return. |
|
159 * @return {String} Key value or null if it wasn't found. |
|
160 */ |
|
161 key: function(index) { |
|
162 return keys[index]; |
|
163 }, |
|
164 |
|
165 /** |
|
166 * Returns the value if the specified key or null if it wasn't found. |
|
167 * |
|
168 * @method getItem |
|
169 * @param {String} key Key of item to retrive. |
|
170 * @return {String} Value of the specified item or null if it wasn't found. |
|
171 */ |
|
172 getItem: function(key) { |
|
173 return key in items ? items[key] : null; |
|
174 }, |
|
175 |
|
176 /** |
|
177 * Sets the value of the specified item by it's key. |
|
178 * |
|
179 * @method setItem |
|
180 * @param {String} key Key of the item to set. |
|
181 * @param {String} value Value of the item to set. |
|
182 */ |
|
183 setItem: function(key, value) { |
|
184 items[key] = "" + value; |
|
185 save(); |
|
186 }, |
|
187 |
|
188 /** |
|
189 * Removes the specified item by key. |
|
190 * |
|
191 * @method removeItem |
|
192 * @param {String} key Key of item to remove. |
|
193 */ |
|
194 removeItem: function(key) { |
|
195 delete items[key]; |
|
196 save(); |
|
197 }, |
|
198 |
|
199 /** |
|
200 * Removes all items. |
|
201 * |
|
202 * @method clear |
|
203 */ |
|
204 clear: function() { |
|
205 items = {}; |
|
206 save(); |
|
207 } |
|
208 }; |
|
209 |
|
210 load(); |
|
211 |
|
212 return LocalStorage; |
|
213 }); |