]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/autosave.js
Wordpress 2.7.1
[autoinstalls/wordpress.git] / wp-includes / js / autosave.js
1 var autosaveLast = '';
2 var autosavePeriodical;
3 var autosaveOldMessage = '';
4 var autosaveDelayPreview = false;
5 var autosaveFirst = true;
6
7 jQuery(function($) {
8         autosaveLast = $('#post #title').val()+$('#post #content').val();
9         autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
10
11         //Disable autosave after the form has been submitted
12         $("#post").submit(function() { $.cancel(autosavePeriodical); });
13 });
14
15 function autosave_parse_response(response) {
16         var res = wpAjax.parseAjaxResponse(response, 'autosave'); // parse the ajax response
17         var message = '';
18
19         if ( res && res.responses && res.responses.length ) {
20                 message = res.responses[0].data; // The saved message or error.
21                 // someone else is editing: disable autosave, set errors
22                 if ( res.responses[0].supplemental ) {
23                         if ( 'disable' == res.responses[0].supplemental['disable_autosave'] ) {
24                                 autosave = function() {};
25                                 res = { errors: true };
26                         }
27                         jQuery.each(res.responses[0].supplemental, function(selector, value) {
28                                 if ( selector.match(/^replace-/) ) {
29                                         jQuery('#'+selector.replace('replace-', '')).val(value);
30                                 }
31                         });
32                 }
33
34                 // if no errors: add slug UI
35                 if ( !res.errors ) {
36                         var postID = parseInt( res.responses[0].id );
37                         if ( !isNaN(postID) && postID > 0 ) {
38                                 autosave_update_slug(postID);
39                         }
40                 }
41         }
42         if ( message ) { jQuery('#autosave').html(message); } // update autosave message
43         else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
44         return res;
45 }
46
47 // called when autosaving pre-existing post
48 function autosave_saved(response) {
49         autosave_parse_response(response); // parse the ajax response
50         autosave_enable_buttons(); // re-enable disabled form buttons
51 }
52
53 // called when autosaving new post
54 function autosave_saved_new(response) {
55         var res = autosave_parse_response(response); // parse the ajax response
56         // if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID
57         if ( res && res.responses.length && !res.errors ) {
58                 var tempID = jQuery('#post_ID').val();
59                 var postID = parseInt( res.responses[0].id );
60                 autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
61                 if ( tempID < 0 && postID > 0 ) // update media buttons
62                         jQuery('#media-buttons a').each(function(){
63                                 this.href = this.href.replace(tempID, postID);
64                         });
65                 // activate preview
66                 autosaveFirst = false;
67                 if ( autosaveDelayPreview )
68                         jQuery('#post-preview').click();
69         } else {
70                 autosave_enable_buttons(); // re-enable disabled form buttons
71         }
72 }
73
74 function autosave_update_post_ID( postID ) {
75         if ( !isNaN(postID) && postID > 0 ) {
76                 if ( postID == parseInt(jQuery('#post_ID').val()) ) { return; } // no need to do this more than once
77                 jQuery('#post_ID').attr({name: "post_ID"});
78                 jQuery('#post_ID').val(postID);
79                 // We need new nonces
80                 jQuery.post(autosaveL10n.requestFile, {
81                         action: "autosave-generate-nonces",
82                         post_ID: postID,
83                         autosavenonce: jQuery('#autosavenonce').val(),
84                         post_type: jQuery('#post_type').val()
85                 }, function(html) {
86                         jQuery('#_wpnonce').val(html);
87                         autosave_enable_buttons(); // re-enable disabled form buttons
88                 });
89                 jQuery('#hiddenaction').val('editpost');
90         }
91 }
92
93 function autosave_update_slug(post_id) {
94         // create slug area only if not already there
95         if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) {
96                 jQuery.post(
97                         slugL10n.requestFile,
98                         {
99                                 action: 'sample-permalink',
100                                 post_id: post_id,
101                                 new_title: jQuery('#title').val(),
102                                 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
103                         },
104                         function(data) {
105                                 jQuery('#edit-slug-box').html(data);
106                                 make_slugedit_clickable();
107                         }
108                 );
109         }
110 }
111
112 function autosave_loading() {
113         jQuery('#autosave').html(autosaveL10n.savingText);
114 }
115
116 function autosave_enable_buttons() {
117         jQuery(".submitbox :button:disabled, .submitbox :submit:disabled").attr('disabled', '');
118 }
119
120 function autosave_disable_buttons() {
121         jQuery(".submitbox :button:enabled, .submitbox :submit:enabled").attr('disabled', 'disabled');
122         setTimeout(autosave_enable_buttons, 5000); // Re-enable 5 sec later.  Just gives autosave a head start to avoid collisions.
123 }
124
125 var autosave = function() {
126         // (bool) is rich editor enabled and active
127         var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
128         var post_data = {
129                 action: "autosave",
130                 post_ID:  jQuery("#post_ID").val() || 0,
131                 post_title: jQuery("#title").val() || "",
132                 autosavenonce: jQuery('#autosavenonce').val(),
133                 tags_input: jQuery("#tags-input").val() || "",
134                 post_type: jQuery('#post_type').val() || "",
135                 autosave: 1
136         };
137
138         // We always send the ajax request in order to keep the post lock fresh.
139         // This (bool) tells whether or not to write the post to the DB during the ajax request.
140         var doAutoSave = true;
141
142         // No autosave while thickbox is open (media buttons)
143         if ( jQuery("#TB_window").css('display') == 'block' )
144                 doAutoSave = false;
145
146         /* Gotta do this up here so we can check the length when tinyMCE is in use */
147         if ( rich ) {
148                 var ed = tinyMCE.activeEditor;
149                 if ( 'mce_fullscreen' == ed.id )
150                         tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
151                 tinyMCE.get('content').save();
152         }
153
154         post_data["content"] = jQuery("#content").val();
155         if ( jQuery('#post_name').val() )
156                 post_data["post_name"] = jQuery('#post_name').val();
157
158         // Nothing to save or no change.
159         if( (post_data["post_title"].length==0 && post_data["content"].length==0) || post_data["post_title"] + post_data["content"] == autosaveLast) {
160                 doAutoSave = false
161         }
162
163         autosave_disable_buttons();
164
165         var origStatus = jQuery('#original_post_status').val();
166
167         autosaveLast = jQuery("#title").val()+jQuery("#content").val();
168         goodcats = ([]);
169         jQuery("[@name='post_category[]']:checked").each( function(i) {
170                 goodcats.push(this.value);
171         } );
172         post_data["catslist"] = goodcats.join(",");
173
174         if ( jQuery("#comment_status").attr("checked") )
175                 post_data["comment_status"] = 'open';
176         if ( jQuery("#ping_status").attr("checked") )
177                 post_data["ping_status"] = 'open';
178         if ( jQuery("#excerpt").size() )
179                 post_data["excerpt"] = jQuery("#excerpt").val();
180         if ( jQuery("#post_author").size() )
181                 post_data["post_author"] = jQuery("#post_author").val();
182         post_data["user_ID"] = jQuery("#user-id").val();
183
184         // Don't run while the TinyMCE spellcheck is on.  Why?  Who knows.
185         if ( rich && tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active ) {
186                 doAutoSave = false;
187         }
188
189         if(parseInt(post_data["post_ID"]) < 1) {
190                 post_data["temp_ID"] = post_data["post_ID"];
191                 var successCallback = autosave_saved_new; // new post
192         } else {
193                 var successCallback = autosave_saved; // pre-existing post
194         }
195
196         if ( !doAutoSave ) {
197                 post_data['autosave'] = 0;
198         }
199
200         autosaveOldMessage = jQuery('#autosave').html();
201
202         jQuery.ajax({
203                 data: post_data,
204                 beforeSend: doAutoSave ? autosave_loading : null,
205                 type: "POST",
206                 url: autosaveL10n.requestFile,
207                 success: successCallback
208         });
209 }