Ticket #3824: autosave.js.diff
File autosave.js.diff, 8.6 KB (added by , 17 years ago) |
---|
-
autosave.js
2 2 var autosavePeriodical; 3 3 4 4 function autosave_start_timer() { 5 var form = $('post'); 6 autosaveLast = form.post_title.value+form.content.value; 5 autosaveLast = jQuery('#post #title').val()+jQuery('#post #content').val(); 7 6 // Keep autosave_interval in sync with edit_post(). 8 7 autosavePeriodical = new PeriodicalExecuter(autosave, autosaveL10n.autosaveInterval); 9 8 //Disable autosave after the form has been submitted 10 if(form.addEventListener) { 11 form.addEventListener("submit", function () { autosavePeriodical.currentlyExecuting = true; }, false); 12 } 13 if(form.attachEvent) { 14 form.save ? form.save.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null; 15 form.submit ? form.submit.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null; 16 form.publish ? form.publish.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null; 17 form.deletepost ? form.deletepost.attachEvent("onclick", function () { autosavePeriodical.currentlyExecuting = true; }) : null; 18 } 9 jQuery("#post #submit").submit(function () { autosavePeriodical.currentlyExecuting = true; }); 10 11 jQuery("#post #save").click(function () { autosavePeriodical.currentlyExecuting = true; }); 12 jQuery("#post #submit").click(function () { autosavePeriodical.currentlyExecuting = true; }); 13 jQuery("#post #publish").click(function () { autosavePeriodical.currentlyExecuting = true; }); 14 jQuery("#post #deletepost").click(function () { autosavePeriodical.currentlyExecuting = true; }); 19 15 } 20 16 addLoadEvent(autosave_start_timer) 21 17 … … 26 22 ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds(); 27 23 } 28 24 29 function autosave_update_nonce() { 30 var response = nonceAjax.response; 31 document.getElementsByName('_wpnonce')[0].value = response; 32 } 33 34 function autosave_update_post_ID() { 35 var response = autosaveAjax.response; 25 function autosave_update_post_ID(response) { 36 26 var res = parseInt(response); 37 27 var message; 38 28 … … 40 30 message = autosaveL10n.errorText.replace(/%response%/g, response); 41 31 } else { 42 32 message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time()); 43 $('post_ID').name = "post_ID";44 $('post_ID').value = res;33 jQuery('#post_ID').attr({id: "post_ID"}); 34 jQuery('#post_ID').val(res); 45 35 // We need new nonces 46 nonceAjax = new sack();47 nonceAjax.element = null;48 nonceAjax.setVar("action", "autosave-generate-nonces");49 nonceAjax.setVar("post_ID", res);50 nonceAjax.setVar("cookie", document.cookie);51 nonceAjax.setVar("post_type", $('post_type').value);52 nonceAjax.requestFile = autosaveL10n.requestFile;53 nonceAjax.onCompletion = autosave_update_nonce;54 nonceAjax.method = "POST";55 nonceAjax.runAJAX();56 $('hiddenaction').value = 'editpost';36 jQuery.post(autosaveL10n.requestFile, { 37 data: { 38 action: "autosave-generate-nonces", 39 post_ID: res, 40 cookie: document.cookie, 41 post_type: jQuery('#post_type').val() 42 }, 43 }, function(html) { 44 jQuery('#_wpnonce').val(html); 45 }); 46 jQuery('#hiddenaction').val('editpost'); 57 47 } 58 $('autosave').innerHTML = message;48 jQuery('#autosave').html(message); 59 49 autosave_enable_buttons(); 60 50 } 61 51 62 52 function autosave_loading() { 63 $('autosave').innerHTML = autosaveL10n.savingText;53 jQuery('#autosave').html(autosaveL10n.savingText); 64 54 } 65 55 66 function autosave_saved() { 67 var response = autosaveAjax.response; 56 function autosave_saved(response) { 68 57 var res = parseInt(response); 69 58 var message; 70 59 … … 73 62 } else { 74 63 message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time()); 75 64 } 76 $('autosave').innerHTML = message;65 jQuery('#autosave').html(message); 77 66 autosave_enable_buttons(); 78 67 } 79 68 80 69 function autosave_disable_buttons() { 81 var form = $('post'); 82 form.save ? form.save.disabled = 'disabled' : null; 83 form.submit ? form.submit.disabled = 'disabled' : null; 84 form.publish ? form.publish.disabled = 'disabled' : null; 85 form.deletepost ? form.deletepost.disabled = 'disabled' : null; 70 jQuery("#post #save:enabled").attr('disabled', 'disabled'); 71 jQuery("#post #submit:enabled").attr('disabled', 'disabled'); 72 jQuery("#post #publish:enabled").attr('disabled', 'disabled'); 73 jQuery("#post #deletepost:enabled").attr('disabled', 'disabled'); 86 74 setTimeout('autosave_enable_buttons();', 1000); // Re-enable 1 sec later. Just gives autosave a head start to avoid collisions. 87 75 } 88 76 89 77 function autosave_enable_buttons() { 90 var form = $('post'); 91 form.save ? form.save.disabled = '' : null; 92 form.submit ? form.submit.disabled = '' : null; 93 form.publish ? form.publish.disabled = '' : null; 94 form.deletepost ? form.deletepost.disabled = '' : null; 78 jQuery("#post #save:disabled").attr('disabled', ''); 79 jQuery("#post #submit:disabled").attr('disabled', ''); 80 jQuery("#post #publish:disabled").attr('disabled', ''); 81 jQuery("#post #deletepost:disabled").attr('disabled', ''); 95 82 } 96 83 97 84 function autosave() { 98 var form = $('post');99 85 var rich = ((typeof tinyMCE != "undefined") && tinyMCE.getInstanceById('content')) ? true : false; 86 var post_data = { 87 action: "autosave", 88 post_ID: jQuery("#post_ID").val(), 89 post_title: jQuery("#title").val(), 90 cookie: document.cookie, 91 tags_input: jQuery("#tags_input").val() || "", 92 post_type: jQuery('#post_type').val() 93 }; 100 94 101 autosaveAjax = new sack();102 103 95 /* Gotta do this up here so we can check the length when tinyMCE is in use */ 104 96 if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 || rich == false ) { 105 autosaveAjax.setVar("content", form.content.value);97 post_data["content"] = jQuery("#content").val(); 106 98 } else { 107 99 // Don't run while the TinyMCE spellcheck is on. 108 100 if(tinyMCE.selectedInstance.spellcheckerOn) return; 109 101 tinyMCE.wpTriggerSave(); 110 autosaveAjax.setVar("content", form.content.value);102 post_data["content"] = jQuery("#content").val(); 111 103 } 112 104 113 if( form.post_title.value.length==0 || form.content.value.length==0 || form.post_title.value+form.content.value == autosaveLast)105 if(jQuery("#title").val()==null || jQuery("#content").val()==null || jQuery("#title").val()+jQuery("#content").val() == autosaveLast) { 114 106 return; 107 } 115 108 116 109 autosave_disable_buttons(); 117 110 118 autosaveLast = form.post_title.value+form.content.value;111 autosaveLast = jQuery("#title").val()+jQuery("#content").val(); 119 112 120 cats = document.getElementsByName("post_category[]");113 cats = jQuery("[@name='post_category[]']:checked"); 121 114 goodcats = ([]); 122 115 for(i=0;i<cats.length;i++) { 123 116 if(cats[i].checked) 124 117 goodcats.push(cats[i].value); 125 118 } 126 catslist= goodcats.join(",");119 post_data["catslist"] = goodcats.join(","); 127 120 128 autosaveAjax.setVar("action", "autosave"); 129 autosaveAjax.setVar("cookie", document.cookie); 130 autosaveAjax.setVar("catslist", catslist); 131 autosaveAjax.setVar("post_ID", $("post_ID").value); 132 autosaveAjax.setVar("post_title", form.post_title.value); 133 autosaveAjax.setVar("post_type", form.post_type.value); 134 autosaveAjax.setVar("tags_input", form.tags_input.value); 135 if ( form.comment_status.checked ) 136 autosaveAjax.setVar("comment_status", 'open'); 137 if ( form.ping_status.checked ) 138 autosaveAjax.setVar("ping_status", 'open'); 139 if(form.excerpt) 140 autosaveAjax.setVar("excerpt", form.excerpt.value); 121 if ( jQuery("#comment_status").attr("checked") ) 122 post_data["comment_status"] = 'open'; 123 if ( jQuery("#ping_status").attr("checked") ) 124 post_data["ping_status"] = 'open'; 125 if( jQuery("#excerpt")) 126 post_data["excerpt"] = jQuery("#excerpt").val(); 141 127 142 128 if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 || rich == false ) { 143 autosaveAjax.setVar("content", form.content.value);129 post_data["content"] = jQuery("#content").val(); 144 130 } else { 145 131 tinyMCE.wpTriggerSave(); 146 autosaveAjax.setVar("content", form.content.value);132 post_data["content"] = jQuery("#content").val(); 147 133 } 148 134 149 autosaveAjax.requestFile = autosaveL10n.requestFile; 150 autosaveAjax.method = "POST"; 151 autosaveAjax.element = null; 152 autosaveAjax.onLoading = autosave_loading; 153 autosaveAjax.onInteractive = autosave_loading; 154 if(parseInt($("post_ID").value) < 1) 155 autosaveAjax.onCompletion = autosave_update_post_ID; 156 else 157 autosaveAjax.onCompletion = autosave_saved; 158 autosaveAjax.runAJAX(); 135 if(parseInt(jQuery("#post_ID").val()) < 1) { 136 jQuery.ajaxSetup({ 137 success: function(html) { autosave_update_post_ID(html); } 138 }); 139 } else { 140 jQuery.ajaxSetup({ 141 success: function(html) { autosave_saved(html); } 142 }); 143 } 144 jQuery.ajax({ 145 data: post_data, 146 beforeSend: function() { autosave_loading() }, 147 type: "POST", 148 url: autosaveL10n.requestFile 149 }); 159 150 }