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