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