source: other-projects/nz-flag-design/trunk/main-form/svg-edit-local/extensions/ext-storage.js@ 29966

Last change on this file since 29966 was 29966, checked in by davidb, 9 years ago

Minor tweak to code so it will always store without prompting

File size: 11.0 KB
Line 
1/*globals svgEditor, svgCanvas, $, widget*/
2/*jslint vars: true, eqeq: true, regexp: true, continue: true*/
3/*
4 * ext-storage.js
5 *
6 * Licensed under the MIT License
7 *
8 * Copyright(c) 2010 Brett Zamir
9 *
10 */
11/**
12* This extension allows automatic saving of the SVG canvas contents upon
13* page unload (which can later be automatically retrieved upon future
14* editor loads).
15*
16* The functionality was originally part of the SVG Editor, but moved to a
17* separate extension to make the setting behavior optional, and adapted
18* to inform the user of its setting of local data.
19*/
20
21/*
22TODOS
231. Revisit on whether to use $.pref over directly setting curConfig in all
24 extensions for a more public API (not only for extPath and imagePath,
25 but other currently used config in the extensions)
262. We might provide control of storage settings through the UI besides the
27 initial (or URL-forced) dialog.
28*/
29svgEditor.addExtension('storage', function() {
30 // We could empty any already-set data for users when they decline storage,
31 // but it would be a risk for users who wanted to store but accidentally
32 // said "no"; instead, we'll let those who already set it, delete it themselves;
33 // to change, set the "emptyStorageOnDecline" config setting to true
34 // in config.js.
35 var emptyStorageOnDecline = svgEditor.curConfig.emptyStorageOnDecline,
36 // When the code in svg-editor.js prevents local storage on load per
37 // user request, we also prevent storing on unload here so as to
38 // avoid third-party sites making XSRF requests or providing links
39 // which would cause the user's local storage not to load and then
40 // upon page unload (such as the user closing the window), the storage
41 // would thereby be set with an empty value, erasing any of the
42 // user's prior work. To change this behavior so that no use of storage
43 // or adding of new storage takes place regardless of settings, set
44 // the "noStorageOnLoad" config setting to true in config.js.
45 noStorageOnLoad = svgEditor.curConfig.noStorageOnLoad,
46 forceStorage = svgEditor.curConfig.forceStorage,
47 storage = svgEditor.storage;
48
49 function replaceStoragePrompt (val) {
50 val = val ? 'storagePrompt=' + val : '';
51 var loc = top.location; // Allow this to work with the embedded editor as well
52 if (loc.href.indexOf('storagePrompt=') > -1) {
53 loc.href = loc.href.replace(/([&?])storagePrompt=[^&]*(&?)/, function (n0, n1, amp) {
54 return (val ? n1 : '') + val + (!val && amp ? n1 : (amp || ''));
55 });
56 }
57 else {
58 loc.href += (loc.href.indexOf('?') > -1 ? '&' : '?') + val;
59 }
60 }
61 function setSVGContentStorage (val) {
62 if (storage) {
63 var name = 'svgedit-' + svgEditor.curConfig.canvasName;
64 if (!val) {
65 storage.removeItem(name);
66 }
67 else {
68 storage.setItem(name, val);
69 }
70 }
71 }
72
73 function expireCookie (cookie) {
74 document.cookie = encodeURIComponent(cookie) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT';
75 }
76
77 function removeStoragePrefCookie () {
78 expireCookie('store');
79 }
80
81 function emptyStorage() {
82 setSVGContentStorage('');
83 var name;
84 for (name in svgEditor.curPrefs) {
85 if (svgEditor.curPrefs.hasOwnProperty(name)) {
86 name = 'svg-edit-' + name;
87 if (storage) {
88 storage.removeItem(name);
89 }
90 expireCookie(name);
91 }
92 }
93 }
94
95// emptyStorage();
96
97 /**
98 * Listen for unloading: If and only if opted in by the user, set the content
99 * document and preferences into storage:
100 * 1. Prevent save warnings (since we're automatically saving unsaved
101 * content into storage)
102 * 2. Use localStorage to set SVG contents (potentially too large to allow in cookies)
103 * 3. Use localStorage (where available) or cookies to set preferences.
104 */
105 function setupBeforeUnloadListener () {
106 window.addEventListener('beforeunload', function(e) {
107 // Don't save anything unless the user opted in to storage
108 console.log("*** document cookie = " + document.cookie);
109 if (!document.cookie.match(/(?:^|;\s*)store=(?:prefsAndContent|prefsOnly)/)) {
110 return;
111 }
112 var key;
113 if (document.cookie.match(/(?:^|;\s*)store=prefsAndContent/)) {
114 setSVGContentStorage(svgCanvas.getSvgString());
115 }
116
117 svgEditor.setConfig({no_save_warning: true}); // No need for explicit saving at all once storage is on
118 // svgEditor.showSaveWarning = false;
119
120 var curPrefs = svgEditor.curPrefs;
121
122 for (key in curPrefs) {
123 if (curPrefs.hasOwnProperty(key)) { // It's our own config, so we don't need to iterate up the prototype chain
124 var val = curPrefs[key],
125 store = (val != undefined);
126 key = 'svg-edit-' + key;
127 if (!store) {
128 continue;
129 }
130 if (storage) {
131 storage.setItem(key, val);
132 }
133 else if (window.widget) {
134 widget.setPreferenceForKey(val, key);
135 }
136 else {
137 val = encodeURIComponent(val);
138 document.cookie = encodeURIComponent(key) + '=' + val + '; expires=Fri, 31 Dec 9999 23:59:59 GMT';
139 }
140 }
141 }
142 }, false);
143 }
144
145 /*
146 // We could add locales here instead (and also thereby avoid the need
147 // to keep our content within "langReady"), but this would be less
148 // convenient for translators.
149 $.extend(uiStrings, {confirmSetStorage: {
150 message: "By default and where supported, SVG-Edit can store your editor "+
151 "preferences and SVG content locally on your machine so you do not "+
152 "need to add these back each time you load SVG-Edit. If, for privacy "+
153 "reasons, you do not wish to store this information on your machine, "+
154 "you can change away from the default option below.",
155 storagePrefsAndContent: "Store preferences and SVG content locally",
156 storagePrefsOnly: "Only store preferences locally",
157 storagePrefs: "Store preferences locally",
158 storageNoPrefsOrContent: "Do not store my preferences or SVG content locally",
159 storageNoPrefs: "Do not store my preferences locally",
160 rememberLabel: "Remember this choice?",
161 rememberTooltip: "If you choose to opt out of storage while remembering this choice, the URL will change so as to avoid asking again."
162 }});
163 */
164 var loaded = false;
165 return {
166 name: 'storage',
167 langReady: function (data) {
168 var // lang = data.lang,
169 uiStrings = data.uiStrings, // No need to store as dialog should only run once
170 storagePrompt = $.deparam.querystring(true).storagePrompt;
171
172 // No need to run this one-time dialog again just because the user
173 // changes the language
174 if (loaded) {
175 return;
176 }
177 loaded = true;
178
179 // Note that the following can load even if "noStorageOnLoad" is
180 // set to false; to avoid any chance of storage, avoid this
181 // extension! (and to avoid using any prior storage, set the
182 // config option "noStorageOnLoad" to true).
183 if (!forceStorage && (
184 // If the URL has been explicitly set to always prompt the
185 // user (e.g., so one can be pointed to a URL where one
186 // can alter one's settings, say to prevent future storage)...
187 storagePrompt === true ||
188 (
189 // ...or...if the URL at least doesn't explicitly prevent a
190 // storage prompt (as we use for users who
191 // don't want to set cookies at all but who don't want
192 // continual prompts about it)...
193 storagePrompt !== false &&
194 // ...and this user hasn't previously indicated a desire for storage
195 !document.cookie.match(/(?:^|;\s*)store=(?:prefsAndContent|prefsOnly)/)
196 )
197 // ...then show the storage prompt.
198 )) {
199
200 var options = [];
201 if (storage) {
202 options.unshift(
203 {value: 'prefsAndContent', text: uiStrings.confirmSetStorage.storagePrefsAndContent},
204 {value: 'prefsOnly', text: uiStrings.confirmSetStorage.storagePrefsOnly},
205 {value: 'noPrefsOrContent', text: uiStrings.confirmSetStorage.storageNoPrefsOrContent}
206 );
207 }
208 else {
209 options.unshift(
210 {value: 'prefsOnly', text: uiStrings.confirmSetStorage.storagePrefs},
211 {value: 'noPrefsOrContent', text: uiStrings.confirmSetStorage.storageNoPrefs}
212 );
213 }
214
215 // Hack to temporarily provide a wide and high enough dialog
216 var oldContainerWidth = $('#dialog_container')[0].style.width,
217 oldContainerMarginLeft = $('#dialog_container')[0].style.marginLeft,
218 oldContentHeight = $('#dialog_content')[0].style.height,
219 oldContainerHeight = $('#dialog_container')[0].style.height;
220 $('#dialog_content')[0].style.height = '120px';
221 $('#dialog_container')[0].style.height = '170px';
222 $('#dialog_container')[0].style.width = '800px';
223 $('#dialog_container')[0].style.marginLeft = '-400px';
224
225 // Open select-with-checkbox dialog
226 $.select(
227 uiStrings.confirmSetStorage.message,
228 options,
229 function (pref, checked) {
230 if (pref && pref !== 'noPrefsOrContent') {
231 // Regardless of whether the user opted
232 // to remember the choice (and move to a URL which won't
233 // ask them again), we have to assume the user
234 // doesn't even want to remember their not wanting
235 // storage, so we don't set the cookie or continue on with
236 // setting storage on beforeunload
237 document.cookie = 'store=' + encodeURIComponent(pref) + '; expires=Fri, 31 Dec 9999 23:59:59 GMT'; // 'prefsAndContent' | 'prefsOnly'
238 // If the URL was configured to always insist on a prompt, if
239 // the user does indicate a wish to store their info, we
240 // don't want ask them again upon page refresh so move
241 // them instead to a URL which does not always prompt
242 if (storagePrompt === true && checked) {
243 replaceStoragePrompt();
244 return;
245 }
246 }
247 else { // The user does not wish storage (or cancelled, which we treat equivalently)
248 removeStoragePrefCookie();
249 if (pref && // If the user explicitly expresses wish for no storage
250 emptyStorageOnDecline
251 ) {
252 emptyStorage();
253 }
254 if (pref && checked) {
255 // Open a URL which won't set storage and won't prompt user about storage
256 replaceStoragePrompt('false');
257 return;
258 }
259 }
260
261 // Reset width/height of dialog (e.g., for use by Export)
262 $('#dialog_container')[0].style.width = oldContainerWidth;
263 $('#dialog_container')[0].style.marginLeft = oldContainerMarginLeft;
264 $('#dialog_content')[0].style.height = oldContentHeight;
265 $('#dialog_container')[0].style.height = oldContainerHeight;
266
267 // It should be enough to (conditionally) add to storage on
268 // beforeunload, but if we wished to update immediately,
269 // we might wish to try setting:
270 // svgEditor.setConfig({noStorageOnLoad: true});
271 // and then call:
272 // svgEditor.loadContentAndPrefs();
273
274 // We don't check for noStorageOnLoad here because
275 // the prompt gives the user the option to store data
276 setupBeforeUnloadListener();
277
278 svgEditor.storagePromptClosed = true;
279 },
280 null,
281 null,
282 {
283 label: uiStrings.confirmSetStorage.rememberLabel,
284 checked: false,
285 tooltip: uiStrings.confirmSetStorage.rememberTooltip
286 }
287 );
288 }
289 else if (!noStorageOnLoad || forceStorage) {
290 // **** forced!
291 document.cookie = 'store=' + encodeURIComponent('prefsAndContent') + '; expires=Fri, 31 Dec 9999 23:59:59 GMT';
292 setupBeforeUnloadListener();
293 }
294 }
295 };
296});
Note: See TracBrowser for help on using the repository browser.