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