|
1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, interimLogin = false; |
|
2 |
|
3 jQuery(document).ready( function($) { |
|
4 var dotabkey = true; |
|
5 |
|
6 autosaveLast = $('#post #title').val() + $('#post #content').val(); |
|
7 autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true}); |
|
8 |
|
9 //Disable autosave after the form has been submitted |
|
10 $("#post").submit(function() { |
|
11 $.cancel(autosavePeriodical); |
|
12 }); |
|
13 |
|
14 $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){ |
|
15 blockSave = true; |
|
16 window.onbeforeunload = null; |
|
17 $(':button, :submit', '#submitpost').each(function(){ |
|
18 var t = $(this); |
|
19 if ( t.hasClass('button-primary') ) |
|
20 t.addClass('button-primary-disabled'); |
|
21 else |
|
22 t.addClass('button-disabled'); |
|
23 }); |
|
24 $('#ajax-loading').css('visibility', 'visible'); |
|
25 }); |
|
26 |
|
27 window.onbeforeunload = function(){ |
|
28 var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content; |
|
29 |
|
30 if ( mce && !mce.isHidden() ) { |
|
31 if ( mce.isDirty() ) |
|
32 return autosaveL10n.saveAlert; |
|
33 } else { |
|
34 title = $('#post #title').val(), content = $('#post #content').val(); |
|
35 if ( ( title || content ) && title + content != autosaveLast ) |
|
36 return autosaveL10n.saveAlert; |
|
37 } |
|
38 }; |
|
39 |
|
40 // preview |
|
41 $('#post-preview').click(function(){ |
|
42 if ( 1 > $('#post_ID').val() && notSaved ) { |
|
43 autosaveDelayPreview = true; |
|
44 autosave(); |
|
45 return false; |
|
46 } |
|
47 doPreview(); |
|
48 return false; |
|
49 }); |
|
50 |
|
51 doPreview = function() { |
|
52 $('input#wp-preview').val('dopreview'); |
|
53 $('form#post').attr('target', 'wp-preview').submit().attr('target', ''); |
|
54 $('input#wp-preview').val(''); |
|
55 } |
|
56 |
|
57 // This code is meant to allow tabbing from Title to Post if tinyMCE is defined. |
|
58 if ( typeof tinyMCE != 'undefined' ) { |
|
59 $('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) { |
|
60 if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) { |
|
61 if ( ($("#post_ID").val() < 1) && ($("#title").val().length > 0) ) { autosave(); } |
|
62 if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) { |
|
63 e.preventDefault(); |
|
64 dotabkey = false; |
|
65 tinyMCE.activeEditor.focus(); |
|
66 return false; |
|
67 } |
|
68 } |
|
69 }); |
|
70 } |
|
71 |
|
72 // autosave new posts after a title is typed but not if Publish or Save Draft is clicked |
|
73 if ( 0 > $('#post_ID').val() ) { |
|
74 $('#title').blur( function() { |
|
75 if ( !this.value || 0 < $('#post_ID').val() ) |
|
76 return; |
|
77 |
|
78 delayed_autosave(); |
|
79 }); |
|
80 } |
|
81 }); |
|
82 |
|
83 function autosave_parse_response(response) { |
|
84 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup, url; |
|
85 |
|
86 if ( res && res.responses && res.responses.length ) { |
|
87 message = res.responses[0].data; // The saved message or error. |
|
88 // someone else is editing: disable autosave, set errors |
|
89 if ( res.responses[0].supplemental ) { |
|
90 sup = res.responses[0].supplemental; |
|
91 if ( 'disable' == sup['disable_autosave'] ) { |
|
92 autosave = function() {}; |
|
93 res = { errors: true }; |
|
94 } |
|
95 if ( sup['session_expired'] && (url = sup['session_expired']) ) { |
|
96 if ( !interimLogin || interimLogin.closed ) { |
|
97 interimLogin = window.open(url, 'login', 'width=600,height=450,resizable=yes,scrollbars=yes,status=yes'); |
|
98 interimLogin.focus(); |
|
99 } |
|
100 delete sup['session_expired']; |
|
101 } |
|
102 jQuery.each(sup, function(selector, value) { |
|
103 if ( selector.match(/^replace-/) ) { |
|
104 jQuery('#'+selector.replace('replace-', '')).val(value); |
|
105 } |
|
106 }); |
|
107 } |
|
108 |
|
109 // if no errors: add slug UI |
|
110 if ( !res.errors ) { |
|
111 postID = parseInt( res.responses[0].id, 10 ); |
|
112 if ( !isNaN(postID) && postID > 0 ) { |
|
113 autosave_update_slug(postID); |
|
114 } |
|
115 } |
|
116 } |
|
117 if ( message ) { jQuery('#autosave').html(message); } // update autosave message |
|
118 else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); } |
|
119 return res; |
|
120 } |
|
121 |
|
122 // called when autosaving pre-existing post |
|
123 function autosave_saved(response) { |
|
124 autosave_parse_response(response); // parse the ajax response |
|
125 autosave_enable_buttons(); // re-enable disabled form buttons |
|
126 } |
|
127 |
|
128 // called when autosaving new post |
|
129 function autosave_saved_new(response) { |
|
130 var res = autosave_parse_response(response), tempID, postID; |
|
131 // if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID |
|
132 if ( res && res.responses.length && !res.errors ) { |
|
133 tempID = jQuery('#post_ID').val(); |
|
134 postID = parseInt( res.responses[0].id, 10 ); |
|
135 autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here |
|
136 if ( tempID < 0 && postID > 0 ) { // update media buttons |
|
137 notSaved = false; |
|
138 jQuery('#media-buttons a').each(function(){ |
|
139 this.href = this.href.replace(tempID, postID); |
|
140 }); |
|
141 } |
|
142 if ( autosaveDelayPreview ) { |
|
143 autosaveDelayPreview = false; |
|
144 doPreview(); |
|
145 } |
|
146 } else { |
|
147 autosave_enable_buttons(); // re-enable disabled form buttons |
|
148 } |
|
149 } |
|
150 |
|
151 function autosave_update_post_ID( postID ) { |
|
152 if ( !isNaN(postID) && postID > 0 ) { |
|
153 if ( postID == parseInt(jQuery('#post_ID').val(), 10) ) { return; } // no need to do this more than once |
|
154 jQuery('#post_ID').attr({name: "post_ID"}); |
|
155 jQuery('#post_ID').val(postID); |
|
156 // We need new nonces |
|
157 jQuery.post(autosaveL10n.requestFile, { |
|
158 action: "autosave-generate-nonces", |
|
159 post_ID: postID, |
|
160 autosavenonce: jQuery('#autosavenonce').val(), |
|
161 post_type: jQuery('#post_type').val() |
|
162 }, function(html) { |
|
163 jQuery('#_wpnonce').val(html.updateNonce); |
|
164 jQuery('#delete-action a.submitdelete').attr('href', html.deleteURL); |
|
165 autosave_enable_buttons(); // re-enable disabled form buttons |
|
166 jQuery('#delete-action a.submitdelete').fadeIn(); |
|
167 }, |
|
168 'json'); |
|
169 jQuery('#hiddenaction').val('editpost'); |
|
170 } |
|
171 } |
|
172 |
|
173 function autosave_update_slug(post_id) { |
|
174 // create slug area only if not already there |
|
175 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) { |
|
176 jQuery.post( |
|
177 ajaxurl, |
|
178 { |
|
179 action: 'sample-permalink', |
|
180 post_id: post_id, |
|
181 new_title: jQuery('#title').val(), |
|
182 samplepermalinknonce: jQuery('#samplepermalinknonce').val() |
|
183 }, |
|
184 function(data) { |
|
185 jQuery('#edit-slug-box').html(data); |
|
186 makeSlugeditClickable(); |
|
187 } |
|
188 ); |
|
189 } |
|
190 } |
|
191 |
|
192 function autosave_loading() { |
|
193 jQuery('#autosave').html(autosaveL10n.savingText); |
|
194 } |
|
195 |
|
196 function autosave_enable_buttons() { |
|
197 // delay that a bit to avoid some rare collisions while the DOM is being updated. |
|
198 setTimeout(function(){ |
|
199 jQuery(':button, :submit', '#submitpost').removeAttr('disabled'); |
|
200 jQuery('#ajax-loading').css('visibility', 'hidden'); |
|
201 }, 500); |
|
202 } |
|
203 |
|
204 function autosave_disable_buttons() { |
|
205 jQuery(':button, :submit', '#submitpost').attr('disabled', 'disabled'); |
|
206 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. |
|
207 setTimeout(autosave_enable_buttons, 5000); |
|
208 } |
|
209 |
|
210 function delayed_autosave() { |
|
211 setTimeout(function(){ |
|
212 if ( blockSave ) |
|
213 return; |
|
214 autosave(); |
|
215 }, 200); |
|
216 } |
|
217 |
|
218 autosave = function() { |
|
219 // (bool) is rich editor enabled and active |
|
220 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback; |
|
221 |
|
222 autosave_disable_buttons(); |
|
223 |
|
224 post_data = { |
|
225 action: "autosave", |
|
226 post_ID: jQuery("#post_ID").val() || 0, |
|
227 post_title: jQuery("#title").val() || "", |
|
228 autosavenonce: jQuery('#autosavenonce').val(), |
|
229 post_type: jQuery('#post_type').val() || "", |
|
230 autosave: 1 |
|
231 }; |
|
232 |
|
233 jQuery('.tags-input').each( function() { |
|
234 post_data[this.name] = this.value; |
|
235 } ); |
|
236 |
|
237 // We always send the ajax request in order to keep the post lock fresh. |
|
238 // This (bool) tells whether or not to write the post to the DB during the ajax request. |
|
239 doAutoSave = true; |
|
240 |
|
241 // No autosave while thickbox is open (media buttons) |
|
242 if ( jQuery("#TB_window").css('display') == 'block' ) |
|
243 doAutoSave = false; |
|
244 |
|
245 /* Gotta do this up here so we can check the length when tinyMCE is in use */ |
|
246 if ( rich && doAutoSave ) { |
|
247 ed = tinyMCE.activeEditor; |
|
248 // Don't run while the TinyMCE spellcheck is on. It resets all found words. |
|
249 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) { |
|
250 doAutoSave = false; |
|
251 } else { |
|
252 if ( 'mce_fullscreen' == ed.id ) |
|
253 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'}); |
|
254 tinyMCE.get('content').save(); |
|
255 } |
|
256 } |
|
257 |
|
258 post_data["content"] = jQuery("#content").val(); |
|
259 if ( jQuery('#post_name').val() ) |
|
260 post_data["post_name"] = jQuery('#post_name').val(); |
|
261 |
|
262 // Nothing to save or no change. |
|
263 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) { |
|
264 doAutoSave = false; |
|
265 } |
|
266 |
|
267 origStatus = jQuery('#original_post_status').val(); |
|
268 |
|
269 goodcats = ([]); |
|
270 jQuery("[name='post_category[]']:checked").each( function(i) { |
|
271 goodcats.push(this.value); |
|
272 } ); |
|
273 post_data["catslist"] = goodcats.join(","); |
|
274 |
|
275 if ( jQuery("#comment_status").attr("checked") ) |
|
276 post_data["comment_status"] = 'open'; |
|
277 if ( jQuery("#ping_status").attr("checked") ) |
|
278 post_data["ping_status"] = 'open'; |
|
279 if ( jQuery("#excerpt").size() ) |
|
280 post_data["excerpt"] = jQuery("#excerpt").val(); |
|
281 if ( jQuery("#post_author").size() ) |
|
282 post_data["post_author"] = jQuery("#post_author").val(); |
|
283 post_data["user_ID"] = jQuery("#user-id").val(); |
|
284 |
|
285 if ( doAutoSave ) { |
|
286 autosaveLast = jQuery("#title").val()+jQuery("#content").val(); |
|
287 } else { |
|
288 post_data['autosave'] = 0; |
|
289 } |
|
290 |
|
291 if ( parseInt(post_data["post_ID"], 10) < 1 ) { |
|
292 post_data["temp_ID"] = post_data["post_ID"]; |
|
293 successCallback = autosave_saved_new; // new post |
|
294 } else { |
|
295 successCallback = autosave_saved; // pre-existing post |
|
296 } |
|
297 |
|
298 autosaveOldMessage = jQuery('#autosave').html(); |
|
299 |
|
300 jQuery.ajax({ |
|
301 data: post_data, |
|
302 beforeSend: doAutoSave ? autosave_loading : null, |
|
303 type: "POST", |
|
304 url: autosaveL10n.requestFile, |
|
305 success: successCallback |
|
306 }); |
|
307 } |