annotate WebContent/jscripts/tiny_mce/plugins/autosave/editor_plugin_src.js @ 10:0bdfe01e30b5

checking auth token works now.
author casties
date Tue, 20 Mar 2012 15:55:51 +0100
parents 0be9d53a6967
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
1 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
2 * editor_plugin_src.js
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
3 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
4 * Copyright 2009, Moxiecode Systems AB
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
5 * Released under LGPL License.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
6 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
7 * License: http://tinymce.moxiecode.com/license
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
8 * Contributing: http://tinymce.moxiecode.com/contributing
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
9 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
10 * Adds auto-save capability to the TinyMCE text editor to rescue content
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
11 * inadvertently lost. This plugin was originally developed by Speednet
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
12 * and that project can be found here: http://code.google.com/p/tinyautosave/
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
13 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
14 * TECHNOLOGY DISCUSSION:
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
15 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
16 * The plugin attempts to use the most advanced features available in the current browser to save
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
17 * as much content as possible. There are a total of four different methods used to autosave the
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
18 * content. In order of preference, they are:
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
19 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
20 * 1. localStorage - A new feature of HTML 5, localStorage can store megabytes of data per domain
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
21 * on the client computer. Data stored in the localStorage area has no expiration date, so we must
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
22 * manage expiring the data ourselves. localStorage is fully supported by IE8, and it is supposed
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
23 * to be working in Firefox 3 and Safari 3.2, but in reality is is flaky in those browsers. As
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
24 * HTML 5 gets wider support, the AutoSave plugin will use it automatically. In Windows Vista/7,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
25 * localStorage is stored in the following folder:
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
26 * C:\Users\[username]\AppData\Local\Microsoft\Internet Explorer\DOMStore\[tempFolder]
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
27 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
28 * 2. sessionStorage - A new feature of HTML 5, sessionStorage works similarly to localStorage,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
29 * except it is designed to expire after a certain amount of time. Because the specification
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
30 * around expiration date/time is very loosely-described, it is preferrable to use locaStorage and
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
31 * manage the expiration ourselves. sessionStorage has similar storage characteristics to
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
32 * localStorage, although it seems to have better support by Firefox 3 at the moment. (That will
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
33 * certainly change as Firefox continues getting better at HTML 5 adoption.)
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
34 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
35 * 3. UserData - A very under-exploited feature of Microsoft Internet Explorer, UserData is a
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
36 * way to store up to 128K of data per "document", or up to 1MB of data per domain, on the client
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
37 * computer. The feature is available for IE 5+, which makes it available for every version of IE
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
38 * supported by TinyMCE. The content is persistent across browser restarts and expires on the
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
39 * date/time specified, just like a cookie. However, the data is not cleared when the user clears
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
40 * cookies on the browser, which makes it well-suited for rescuing autosaved content. UserData,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
41 * like other Microsoft IE browser technologies, is implemented as a behavior attached to a
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
42 * specific DOM object, so in this case we attach the behavior to the same DOM element that the
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
43 * TinyMCE editor instance is attached to.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
44 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
45
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
46 (function(tinymce) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
47 // Setup constants to help the compressor to reduce script size
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
48 var PLUGIN_NAME = 'autosave',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
49 RESTORE_DRAFT = 'restoredraft',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
50 TRUE = true,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
51 undefined,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
52 unloadHandlerAdded,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
53 Dispatcher = tinymce.util.Dispatcher;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
54
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
55 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
56 * This plugin adds auto-save capability to the TinyMCE text editor to rescue content
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
57 * inadvertently lost. By using localStorage.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
58 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
59 * @class tinymce.plugins.AutoSave
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
60 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
61 tinymce.create('tinymce.plugins.AutoSave', {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
62 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
63 * Initializes the plugin, this will be executed after the plugin has been created.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
64 * This call is done before the editor instance has finished it's initialization so use the onInit event
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
65 * of the editor instance to intercept that event.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
66 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
67 * @method init
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
68 * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
69 * @param {string} url Absolute URL to where the plugin is located.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
70 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
71 init : function(ed, url) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
72 var self = this, settings = ed.settings;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
73
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
74 self.editor = ed;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
75
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
76 // Parses the specified time string into a milisecond number 10m, 10s etc.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
77 function parseTime(time) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
78 var multipels = {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
79 s : 1000,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
80 m : 60000
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
81 };
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
82
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
83 time = /^(\d+)([ms]?)$/.exec('' + time);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
84
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
85 return (time[2] ? multipels[time[2]] : 1) * parseInt(time);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
86 };
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
87
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
88 // Default config
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
89 tinymce.each({
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
90 ask_before_unload : TRUE,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
91 interval : '30s',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
92 retention : '20m',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
93 minlength : 50
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
94 }, function(value, key) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
95 key = PLUGIN_NAME + '_' + key;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
96
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
97 if (settings[key] === undefined)
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
98 settings[key] = value;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
99 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
100
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
101 // Parse times
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
102 settings.autosave_interval = parseTime(settings.autosave_interval);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
103 settings.autosave_retention = parseTime(settings.autosave_retention);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
104
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
105 // Register restore button
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
106 ed.addButton(RESTORE_DRAFT, {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
107 title : PLUGIN_NAME + ".restore_content",
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
108 onclick : function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
109 if (ed.getContent({draft: true}).replace(/\s|&nbsp;|<\/?p[^>]*>|<br[^>]*>/gi, "").length > 0) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
110 // Show confirm dialog if the editor isn't empty
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
111 ed.windowManager.confirm(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
112 PLUGIN_NAME + ".warning_message",
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
113 function(ok) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
114 if (ok)
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
115 self.restoreDraft();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
116 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
117 );
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
118 } else
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
119 self.restoreDraft();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
120 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
121 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
122
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
123 // Enable/disable restoredraft button depending on if there is a draft stored or not
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
124 ed.onNodeChange.add(function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
125 var controlManager = ed.controlManager;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
126
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
127 if (controlManager.get(RESTORE_DRAFT))
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
128 controlManager.setDisabled(RESTORE_DRAFT, !self.hasDraft());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
129 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
130
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
131 ed.onInit.add(function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
132 // Check if the user added the restore button, then setup auto storage logic
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
133 if (ed.controlManager.get(RESTORE_DRAFT)) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
134 // Setup storage engine
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
135 self.setupStorage(ed);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
136
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
137 // Auto save contents each interval time
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
138 setInterval(function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
139 self.storeDraft();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
140 ed.nodeChanged();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
141 }, settings.autosave_interval);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
142 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
143 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
144
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
145 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
146 * This event gets fired when a draft is stored to local storage.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
147 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
148 * @event onStoreDraft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
149 * @param {tinymce.plugins.AutoSave} sender Plugin instance sending the event.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
150 * @param {Object} draft Draft object containing the HTML contents of the editor.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
151 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
152 self.onStoreDraft = new Dispatcher(self);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
153
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
154 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
155 * This event gets fired when a draft is restored from local storage.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
156 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
157 * @event onStoreDraft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
158 * @param {tinymce.plugins.AutoSave} sender Plugin instance sending the event.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
159 * @param {Object} draft Draft object containing the HTML contents of the editor.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
160 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
161 self.onRestoreDraft = new Dispatcher(self);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
162
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
163 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
164 * This event gets fired when a draft removed/expired.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
165 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
166 * @event onRemoveDraft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
167 * @param {tinymce.plugins.AutoSave} sender Plugin instance sending the event.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
168 * @param {Object} draft Draft object containing the HTML contents of the editor.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
169 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
170 self.onRemoveDraft = new Dispatcher(self);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
171
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
172 // Add ask before unload dialog only add one unload handler
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
173 if (!unloadHandlerAdded) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
174 window.onbeforeunload = tinymce.plugins.AutoSave._beforeUnloadHandler;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
175 unloadHandlerAdded = TRUE;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
176 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
177 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
178
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
179 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
180 * Returns information about the plugin as a name/value array.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
181 * The current keys are longname, author, authorurl, infourl and version.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
182 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
183 * @method getInfo
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
184 * @return {Object} Name/value array containing information about the plugin.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
185 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
186 getInfo : function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
187 return {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
188 longname : 'Auto save',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
189 author : 'Moxiecode Systems AB',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
190 authorurl : 'http://tinymce.moxiecode.com',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
191 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autosave',
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
192 version : tinymce.majorVersion + "." + tinymce.minorVersion
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
193 };
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
194 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
195
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
196 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
197 * Returns an expiration date UTC string.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
198 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
199 * @method getExpDate
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
200 * @return {String} Expiration date UTC string.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
201 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
202 getExpDate : function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
203 return new Date(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
204 new Date().getTime() + this.editor.settings.autosave_retention
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
205 ).toUTCString();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
206 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
207
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
208 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
209 * This method will setup the storage engine. If the browser has support for it.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
210 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
211 * @method setupStorage
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
212 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
213 setupStorage : function(ed) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
214 var self = this, testKey = PLUGIN_NAME + '_test', testVal = "OK";
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
215
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
216 self.key = PLUGIN_NAME + ed.id;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
217
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
218 // Loop though each storage engine type until we find one that works
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
219 tinymce.each([
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
220 function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
221 // Try HTML5 Local Storage
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
222 if (localStorage) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
223 localStorage.setItem(testKey, testVal);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
224
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
225 if (localStorage.getItem(testKey) === testVal) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
226 localStorage.removeItem(testKey);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
227
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
228 return localStorage;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
229 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
230 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
231 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
232
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
233 function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
234 // Try HTML5 Session Storage
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
235 if (sessionStorage) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
236 sessionStorage.setItem(testKey, testVal);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
237
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
238 if (sessionStorage.getItem(testKey) === testVal) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
239 sessionStorage.removeItem(testKey);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
240
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
241 return sessionStorage;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
242 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
243 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
244 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
245
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
246 function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
247 // Try IE userData
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
248 if (tinymce.isIE) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
249 ed.getElement().style.behavior = "url('#default#userData')";
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
250
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
251 // Fake localStorage on old IE
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
252 return {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
253 autoExpires : TRUE,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
254
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
255 setItem : function(key, value) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
256 var userDataElement = ed.getElement();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
257
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
258 userDataElement.setAttribute(key, value);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
259 userDataElement.expires = self.getExpDate();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
260
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
261 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
262 userDataElement.save("TinyMCE");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
263 } catch (e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
264 // Ignore, saving might fail if "Userdata Persistence" is disabled in IE
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
265 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
266 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
267
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
268 getItem : function(key) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
269 var userDataElement = ed.getElement();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
270
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
271 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
272 userDataElement.load("TinyMCE");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
273 return userDataElement.getAttribute(key);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
274 } catch (e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
275 // Ignore, loading might fail if "Userdata Persistence" is disabled in IE
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
276 return null;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
277 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
278 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
279
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
280 removeItem : function(key) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
281 ed.getElement().removeAttribute(key);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
282 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
283 };
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
284 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
285 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
286 ], function(setup) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
287 // Try executing each function to find a suitable storage engine
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
288 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
289 self.storage = setup();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
290
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
291 if (self.storage)
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
292 return false;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
293 } catch (e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
294 // Ignore
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
295 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
296 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
297 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
298
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
299 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
300 * This method will store the current contents in the the storage engine.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
301 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
302 * @method storeDraft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
303 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
304 storeDraft : function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
305 var self = this, storage = self.storage, editor = self.editor, expires, content;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
306
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
307 // Is the contents dirty
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
308 if (storage) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
309 // If there is no existing key and the contents hasn't been changed since
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
310 // it's original value then there is no point in saving a draft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
311 if (!storage.getItem(self.key) && !editor.isDirty())
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
312 return;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
313
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
314 // Store contents if the contents if longer than the minlength of characters
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
315 content = editor.getContent({draft: true});
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
316 if (content.length > editor.settings.autosave_minlength) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
317 expires = self.getExpDate();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
318
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
319 // Store expiration date if needed IE userData has auto expire built in
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
320 if (!self.storage.autoExpires)
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
321 self.storage.setItem(self.key + "_expires", expires);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
322
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
323 self.storage.setItem(self.key, content);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
324 self.onStoreDraft.dispatch(self, {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
325 expires : expires,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
326 content : content
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
327 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
328 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
329 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
330 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
331
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
332 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
333 * This method will restore the contents from the storage engine back to the editor.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
334 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
335 * @method restoreDraft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
336 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
337 restoreDraft : function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
338 var self = this, storage = self.storage, content;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
339
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
340 if (storage) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
341 content = storage.getItem(self.key);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
342
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
343 if (content) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
344 self.editor.setContent(content);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
345 self.onRestoreDraft.dispatch(self, {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
346 content : content
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
347 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
348 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
349 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
350 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
351
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
352 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
353 * This method will return true/false if there is a local storage draft available.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
354 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
355 * @method hasDraft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
356 * @return {boolean} true/false state if there is a local draft.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
357 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
358 hasDraft : function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
359 var self = this, storage = self.storage, expDate, exists;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
360
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
361 if (storage) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
362 // Does the item exist at all
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
363 exists = !!storage.getItem(self.key);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
364 if (exists) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
365 // Storage needs autoexpire
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
366 if (!self.storage.autoExpires) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
367 expDate = new Date(storage.getItem(self.key + "_expires"));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
368
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
369 // Contents hasn't expired
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
370 if (new Date().getTime() < expDate.getTime())
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
371 return TRUE;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
372
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
373 // Remove it if it has
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
374 self.removeDraft();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
375 } else
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
376 return TRUE;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
377 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
378 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
379
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
380 return false;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
381 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
382
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
383 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
384 * Removes the currently stored draft.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
385 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
386 * @method removeDraft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
387 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
388 removeDraft : function() {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
389 var self = this, storage = self.storage, key = self.key, content;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
390
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
391 if (storage) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
392 // Get current contents and remove the existing draft
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
393 content = storage.getItem(key);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
394 storage.removeItem(key);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
395 storage.removeItem(key + "_expires");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
396
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
397 // Dispatch remove event if we had any contents
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
398 if (content) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
399 self.onRemoveDraft.dispatch(self, {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
400 content : content
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
401 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
402 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
403 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
404 },
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
405
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
406 "static" : {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
407 // Internal unload handler will be called before the page is unloaded
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
408 _beforeUnloadHandler : function(e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
409 var msg;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
410
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
411 tinymce.each(tinyMCE.editors, function(ed) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
412 // Store a draft for each editor instance
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
413 if (ed.plugins.autosave)
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
414 ed.plugins.autosave.storeDraft();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
415
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
416 // Never ask in fullscreen mode
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
417 if (ed.getParam("fullscreen_is_enabled"))
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
418 return;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
419
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
420 // Setup a return message if the editor is dirty
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
421 if (!msg && ed.isDirty() && ed.getParam("autosave_ask_before_unload"))
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
422 msg = ed.getLang("autosave.unload_msg");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
423 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
424
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
425 return msg;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
426 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
427 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
428 });
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
429
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
430 tinymce.PluginManager.add('autosave', tinymce.plugins.AutoSave);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
431 })(tinymce);