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