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