author | Anthony Ly <anthonyly.com@gmail.com> |
Wed, 19 Dec 2012 12:35:13 -0800 | |
changeset 203 | f507feede89a |
parent 194 | 32102edaa81b |
permissions | -rw-r--r-- |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1 |
var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true; |
136 | 2 |
|
3 |
jQuery(document).ready( function($) { |
|
4 |
var dotabkey = true; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
5 |
|
136 | 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() { |
|
11 |
$.cancel(autosavePeriodical); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
12 |
autosaveLockRelease = false; |
136 | 13 |
}); |
14 |
||
15 |
$('input[type="submit"], a.submitdelete', '#submitpost').click(function(){ |
|
16 |
blockSave = true; |
|
17 |
window.onbeforeunload = null; |
|
18 |
$(':button, :submit', '#submitpost').each(function(){ |
|
19 |
var t = $(this); |
|
20 |
if ( t.hasClass('button-primary') ) |
|
21 |
t.addClass('button-primary-disabled'); |
|
22 |
else |
|
23 |
t.addClass('button-disabled'); |
|
24 |
}); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
25 |
if ( $(this).attr('id') == 'publish' ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
26 |
$('#ajax-loading').css('visibility', 'visible'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
27 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
28 |
$('#draft-ajax-loading').css('visibility', 'visible'); |
136 | 29 |
}); |
30 |
||
31 |
window.onbeforeunload = function(){ |
|
32 |
var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content; |
|
33 |
||
34 |
if ( mce && !mce.isHidden() ) { |
|
35 |
if ( mce.isDirty() ) |
|
36 |
return autosaveL10n.saveAlert; |
|
37 |
} else { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
38 |
if ( fullscreen && fullscreen.settings.visible ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
39 |
title = $('#wp-fullscreen-title').val(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
40 |
content = $("#wp_mce_fullscreen").val(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
41 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
42 |
title = $('#post #title').val(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
43 |
content = $('#post #content').val(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
44 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
45 |
|
136 | 46 |
if ( ( title || content ) && title + content != autosaveLast ) |
47 |
return autosaveL10n.saveAlert; |
|
48 |
} |
|
49 |
}; |
|
50 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
51 |
$(window).unload( function(e) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
52 |
if ( ! autosaveLockRelease ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
53 |
return; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
54 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
55 |
// unload fires (twice) on removing the Thickbox iframe. Make sure we process only the main document unload. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
if ( e.target && e.target.nodeName != '#document' ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
57 |
return; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
58 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
$.ajax({ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
60 |
type: 'POST', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
61 |
url: ajaxurl, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
62 |
async: false, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
63 |
data: { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
64 |
action: 'wp-remove-post-lock', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
65 |
_wpnonce: $('#_wpnonce').val(), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
66 |
post_ID: $('#post_ID').val(), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
67 |
active_post_lock: $('#active_post_lock').val() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
68 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
69 |
}); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
70 |
} ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
71 |
|
136 | 72 |
// preview |
73 |
$('#post-preview').click(function(){ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
74 |
if ( $('#auto_draft').val() == '1' && notSaved ) { |
136 | 75 |
autosaveDelayPreview = true; |
76 |
autosave(); |
|
77 |
return false; |
|
78 |
} |
|
79 |
doPreview(); |
|
80 |
return false; |
|
81 |
}); |
|
82 |
||
83 |
doPreview = function() { |
|
84 |
$('input#wp-preview').val('dopreview'); |
|
85 |
$('form#post').attr('target', 'wp-preview').submit().attr('target', ''); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
86 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
87 |
/* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
88 |
* Workaround for WebKit bug preventing a form submitting twice to the same action. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
89 |
* https://bugs.webkit.org/show_bug.cgi?id=28633 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
90 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
91 |
if ( $.browser.safari ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
92 |
$('form#post').attr('action', function(index, value) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
93 |
return value + '?t=' + new Date().getTime(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
94 |
}); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
95 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
96 |
|
136 | 97 |
$('input#wp-preview').val(''); |
98 |
} |
|
99 |
||
100 |
// This code is meant to allow tabbing from Title to Post if tinyMCE is defined. |
|
101 |
if ( typeof tinyMCE != 'undefined' ) { |
|
102 |
$('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) { |
|
103 |
if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
104 |
if ( ($('#auto_draft').val() == '1') && ($("#title").val().length > 0) ) { autosave(); } |
136 | 105 |
if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) { |
106 |
e.preventDefault(); |
|
107 |
dotabkey = false; |
|
108 |
tinyMCE.activeEditor.focus(); |
|
109 |
return false; |
|
110 |
} |
|
111 |
} |
|
112 |
}); |
|
113 |
} |
|
114 |
||
115 |
// autosave new posts after a title is typed but not if Publish or Save Draft is clicked |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
116 |
if ( '1' == $('#auto_draft').val() ) { |
136 | 117 |
$('#title').blur( function() { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
118 |
if ( !this.value || $('#auto_draft').val() != '1' ) |
136 | 119 |
return; |
120 |
delayed_autosave(); |
|
121 |
}); |
|
122 |
} |
|
123 |
}); |
|
124 |
||
125 |
function autosave_parse_response(response) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
126 |
var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup; |
136 | 127 |
|
128 |
if ( res && res.responses && res.responses.length ) { |
|
129 |
message = res.responses[0].data; // The saved message or error. |
|
130 |
// someone else is editing: disable autosave, set errors |
|
131 |
if ( res.responses[0].supplemental ) { |
|
132 |
sup = res.responses[0].supplemental; |
|
133 |
if ( 'disable' == sup['disable_autosave'] ) { |
|
134 |
autosave = function() {}; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
135 |
autosaveLockRelease = false; |
136 | 136 |
res = { errors: true }; |
137 |
} |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
138 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
139 |
if ( sup['active-post-lock'] ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
140 |
jQuery('#active_post_lock').val( sup['active-post-lock'] ); |
136 | 141 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
142 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
143 |
if ( sup['alert'] ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
144 |
jQuery('#autosave-alert').remove(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
145 |
jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
146 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
147 |
|
136 | 148 |
jQuery.each(sup, function(selector, value) { |
149 |
if ( selector.match(/^replace-/) ) { |
|
150 |
jQuery('#'+selector.replace('replace-', '')).val(value); |
|
151 |
} |
|
152 |
}); |
|
153 |
} |
|
154 |
||
155 |
// if no errors: add slug UI |
|
156 |
if ( !res.errors ) { |
|
157 |
postID = parseInt( res.responses[0].id, 10 ); |
|
158 |
if ( !isNaN(postID) && postID > 0 ) { |
|
159 |
autosave_update_slug(postID); |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
163 |
if ( message ) { // update autosave message |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
164 |
jQuery('.autosave-message').html(message); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
165 |
} else if ( autosaveOldMessage && res ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
166 |
jQuery('.autosave-message').html( autosaveOldMessage ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
167 |
} |
136 | 168 |
return res; |
169 |
} |
|
170 |
||
171 |
// called when autosaving pre-existing post |
|
172 |
function autosave_saved(response) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
173 |
blockSave = false; |
136 | 174 |
autosave_parse_response(response); // parse the ajax response |
175 |
autosave_enable_buttons(); // re-enable disabled form buttons |
|
176 |
} |
|
177 |
||
178 |
// called when autosaving new post |
|
179 |
function autosave_saved_new(response) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
180 |
blockSave = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
181 |
var res = autosave_parse_response(response), postID; |
136 | 182 |
if ( res && res.responses.length && !res.errors ) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
183 |
// An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves |
136 | 184 |
postID = parseInt( res.responses[0].id, 10 ); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
185 |
if ( !isNaN(postID) && postID > 0 ) { |
136 | 186 |
notSaved = false; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
187 |
jQuery('#auto_draft').val('0'); // No longer an auto-draft |
136 | 188 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
189 |
autosave_enable_buttons(); |
136 | 190 |
if ( autosaveDelayPreview ) { |
191 |
autosaveDelayPreview = false; |
|
192 |
doPreview(); |
|
193 |
} |
|
194 |
} else { |
|
195 |
autosave_enable_buttons(); // re-enable disabled form buttons |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
function autosave_update_slug(post_id) { |
|
200 |
// create slug area only if not already there |
|
201 |
if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
202 |
jQuery.post( ajaxurl, { |
136 | 203 |
action: 'sample-permalink', |
204 |
post_id: post_id, |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
205 |
new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(), |
136 | 206 |
samplepermalinknonce: jQuery('#samplepermalinknonce').val() |
207 |
}, |
|
208 |
function(data) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
209 |
if ( data !== '-1' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
210 |
jQuery('#edit-slug-box').html(data); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
211 |
makeSlugeditClickable(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
212 |
} |
136 | 213 |
} |
214 |
); |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
function autosave_loading() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
219 |
jQuery('.autosave-message').html(autosaveL10n.savingText); |
136 | 220 |
} |
221 |
||
222 |
function autosave_enable_buttons() { |
|
223 |
// delay that a bit to avoid some rare collisions while the DOM is being updated. |
|
224 |
setTimeout(function(){ |
|
225 |
jQuery(':button, :submit', '#submitpost').removeAttr('disabled'); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
226 |
jQuery('.ajax-loading').css('visibility', 'hidden'); |
136 | 227 |
}, 500); |
228 |
} |
|
229 |
||
230 |
function autosave_disable_buttons() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
231 |
jQuery(':button, :submit', '#submitpost').prop('disabled', true); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
232 |
// Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. |
136 | 233 |
setTimeout(autosave_enable_buttons, 5000); |
234 |
} |
|
235 |
||
236 |
function delayed_autosave() { |
|
237 |
setTimeout(function(){ |
|
238 |
if ( blockSave ) |
|
239 |
return; |
|
240 |
autosave(); |
|
241 |
}, 200); |
|
242 |
} |
|
243 |
||
244 |
autosave = function() { |
|
245 |
// (bool) is rich editor enabled and active |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
246 |
blockSave = true; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
247 |
var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
248 |
post_data, doAutoSave, ed, origStatus, successCallback; |
136 | 249 |
|
250 |
autosave_disable_buttons(); |
|
251 |
||
252 |
post_data = { |
|
253 |
action: "autosave", |
|
254 |
post_ID: jQuery("#post_ID").val() || 0, |
|
255 |
autosavenonce: jQuery('#autosavenonce').val(), |
|
256 |
post_type: jQuery('#post_type').val() || "", |
|
257 |
autosave: 1 |
|
258 |
}; |
|
259 |
||
260 |
jQuery('.tags-input').each( function() { |
|
261 |
post_data[this.name] = this.value; |
|
262 |
} ); |
|
263 |
||
264 |
// We always send the ajax request in order to keep the post lock fresh. |
|
265 |
// This (bool) tells whether or not to write the post to the DB during the ajax request. |
|
266 |
doAutoSave = true; |
|
267 |
||
268 |
// No autosave while thickbox is open (media buttons) |
|
269 |
if ( jQuery("#TB_window").css('display') == 'block' ) |
|
270 |
doAutoSave = false; |
|
271 |
||
272 |
/* Gotta do this up here so we can check the length when tinyMCE is in use */ |
|
273 |
if ( rich && doAutoSave ) { |
|
274 |
ed = tinyMCE.activeEditor; |
|
275 |
// Don't run while the TinyMCE spellcheck is on. It resets all found words. |
|
276 |
if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) { |
|
277 |
doAutoSave = false; |
|
278 |
} else { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
279 |
if ( 'mce_fullscreen' == ed.id || 'wp_mce_fullscreen' == ed.id ) |
136 | 280 |
tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'}); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
281 |
tinyMCE.triggerSave(); |
136 | 282 |
} |
283 |
} |
|
284 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
285 |
if ( fullscreen && fullscreen.settings.visible ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
286 |
post_data["post_title"] = jQuery('#wp-fullscreen-title').val() || ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
287 |
post_data["content"] = jQuery("#wp_mce_fullscreen").val() || ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
288 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
289 |
post_data["post_title"] = jQuery("#title").val() || ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
290 |
post_data["content"] = jQuery("#content").val() || ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
291 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
292 |
|
136 | 293 |
if ( jQuery('#post_name').val() ) |
294 |
post_data["post_name"] = jQuery('#post_name').val(); |
|
295 |
||
296 |
// Nothing to save or no change. |
|
297 |
if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) { |
|
298 |
doAutoSave = false; |
|
299 |
} |
|
300 |
||
301 |
origStatus = jQuery('#original_post_status').val(); |
|
302 |
||
303 |
goodcats = ([]); |
|
304 |
jQuery("[name='post_category[]']:checked").each( function(i) { |
|
305 |
goodcats.push(this.value); |
|
306 |
} ); |
|
307 |
post_data["catslist"] = goodcats.join(","); |
|
308 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
309 |
if ( jQuery("#comment_status").prop("checked") ) |
136 | 310 |
post_data["comment_status"] = 'open'; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
311 |
if ( jQuery("#ping_status").prop("checked") ) |
136 | 312 |
post_data["ping_status"] = 'open'; |
313 |
if ( jQuery("#excerpt").size() ) |
|
314 |
post_data["excerpt"] = jQuery("#excerpt").val(); |
|
315 |
if ( jQuery("#post_author").size() ) |
|
316 |
post_data["post_author"] = jQuery("#post_author").val(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
317 |
if ( jQuery("#parent_id").val() ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
318 |
post_data["parent_id"] = jQuery("#parent_id").val(); |
136 | 319 |
post_data["user_ID"] = jQuery("#user-id").val(); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
320 |
if ( jQuery('#auto_draft').val() == '1' ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
321 |
post_data["auto_draft"] = '1'; |
136 | 322 |
|
323 |
if ( doAutoSave ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
324 |
autosaveLast = post_data["post_title"] + post_data["content"]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
325 |
jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]); |
136 | 326 |
} else { |
327 |
post_data['autosave'] = 0; |
|
328 |
} |
|
329 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
330 |
if ( post_data["auto_draft"] == '1' ) { |
136 | 331 |
successCallback = autosave_saved_new; // new post |
332 |
} else { |
|
333 |
successCallback = autosave_saved; // pre-existing post |
|
334 |
} |
|
335 |
||
336 |
autosaveOldMessage = jQuery('#autosave').html(); |
|
337 |
jQuery.ajax({ |
|
338 |
data: post_data, |
|
339 |
beforeSend: doAutoSave ? autosave_loading : null, |
|
340 |
type: "POST", |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
341 |
url: ajaxurl, |
136 | 342 |
success: successCallback |
343 |
}); |
|
344 |
} |