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