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