web/wp-includes/js/autosave.dev.js
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
     1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true;
     1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, interimLogin = false;
     2 
     2 
     3 jQuery(document).ready( function($) {
     3 jQuery(document).ready( function($) {
     4 	var dotabkey = true;
     4 	var dotabkey = true;
     5 	
     5 	
     6 	autosaveLast = $('#post #title').val() + $('#post #content').val();
     6 	autosaveLast = $('#post #title').val() + $('#post #content').val();
     7 	autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
     7 	autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
     8 
     8 
     9 	//Disable autosave after the form has been submitted
     9 	//Disable autosave after the form has been submitted
    10 	$("#post").submit(function() { $.cancel(autosavePeriodical); });
    10 	$("#post").submit(function() {
    11 
    11 		$.cancel(autosavePeriodical);
    12 	$('.submitbox input[type="submit"], .submitbox a.submitdelete').click(function(){window.onbeforeunload = null;});
    12 	});
       
    13 
       
    14 	$('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
       
    15 		blockSave = true;
       
    16 		window.onbeforeunload = null;
       
    17 		$(':button, :submit', '#submitpost').each(function(){
       
    18 			var t = $(this);
       
    19 			if ( t.hasClass('button-primary') )
       
    20 				t.addClass('button-primary-disabled');
       
    21 			else
       
    22 				t.addClass('button-disabled');
       
    23 		});
       
    24 		$('#ajax-loading').css('visibility', 'visible');
       
    25 	});
    13 
    26 
    14 	window.onbeforeunload = function(){
    27 	window.onbeforeunload = function(){
    15 		var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content;
    28 		var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content;
    16 
    29 
    17 		if ( mce && !mce.isHidden() ) {
    30 		if ( mce && !mce.isHidden() ) {
    53 					return false;
    66 					return false;
    54 				}
    67 				}
    55 			}
    68 			}
    56 		});
    69 		});
    57 	}
    70 	}
       
    71 
       
    72 	// autosave new posts after a title is typed but not if Publish or Save Draft is clicked
       
    73 	if ( 0 > $('#post_ID').val() ) {
       
    74 		$('#title').blur( function() {
       
    75 			if ( !this.value || 0 < $('#post_ID').val() )
       
    76 				return;
       
    77 
       
    78 			delayed_autosave();
       
    79 		});
       
    80 	}
    58 });
    81 });
    59 
    82 
    60 function autosave_parse_response(response) {
    83 function autosave_parse_response(response) {
    61 	var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID;
    84 	var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup, url;
    62 
    85 
    63 	if ( res && res.responses && res.responses.length ) {
    86 	if ( res && res.responses && res.responses.length ) {
    64 		message = res.responses[0].data; // The saved message or error.
    87 		message = res.responses[0].data; // The saved message or error.
    65 		// someone else is editing: disable autosave, set errors
    88 		// someone else is editing: disable autosave, set errors
    66 		if ( res.responses[0].supplemental ) {
    89 		if ( res.responses[0].supplemental ) {
    67 			if ( 'disable' == res.responses[0].supplemental['disable_autosave'] ) {
    90 			sup = res.responses[0].supplemental;
       
    91 			if ( 'disable' == sup['disable_autosave'] ) {
    68 				autosave = function() {};
    92 				autosave = function() {};
    69 				res = { errors: true };
    93 				res = { errors: true };
    70 			}
    94 			}
    71 			jQuery.each(res.responses[0].supplemental, function(selector, value) {
    95 			if ( sup['session_expired'] && (url = sup['session_expired']) ) {
       
    96 				if ( !interimLogin || interimLogin.closed ) {
       
    97 					interimLogin = window.open(url, 'login', 'width=600,height=450,resizable=yes,scrollbars=yes,status=yes');
       
    98 					interimLogin.focus();
       
    99 				}
       
   100 				delete sup['session_expired'];
       
   101 			}
       
   102 			jQuery.each(sup, function(selector, value) {
    72 				if ( selector.match(/^replace-/) ) {
   103 				if ( selector.match(/^replace-/) ) {
    73 					jQuery('#'+selector.replace('replace-', '')).val(value);
   104 					jQuery('#'+selector.replace('replace-', '')).val(value);
    74 				}
   105 				}
    75 			});
   106 			});
    76 		}
   107 		}
   127 			action: "autosave-generate-nonces",
   158 			action: "autosave-generate-nonces",
   128 			post_ID: postID,
   159 			post_ID: postID,
   129 			autosavenonce: jQuery('#autosavenonce').val(),
   160 			autosavenonce: jQuery('#autosavenonce').val(),
   130 			post_type: jQuery('#post_type').val()
   161 			post_type: jQuery('#post_type').val()
   131 		}, function(html) {
   162 		}, function(html) {
   132 			jQuery('#_wpnonce').val(html);
   163 			jQuery('#_wpnonce').val(html.updateNonce);
       
   164 			jQuery('#delete-action a.submitdelete').attr('href', html.deleteURL);
   133 			autosave_enable_buttons(); // re-enable disabled form buttons
   165 			autosave_enable_buttons(); // re-enable disabled form buttons
   134 		});
   166 			jQuery('#delete-action a.submitdelete').fadeIn();
       
   167 		},
       
   168 		'json');
   135 		jQuery('#hiddenaction').val('editpost');
   169 		jQuery('#hiddenaction').val('editpost');
   136 	}
   170 	}
   137 }
   171 }
   138 
   172 
   139 function autosave_update_slug(post_id) {
   173 function autosave_update_slug(post_id) {
   140 	// create slug area only if not already there
   174 	// create slug area only if not already there
   141 	if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) {
   175 	if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
   142 		jQuery.post(
   176 		jQuery.post(
   143 			slugL10n.requestFile,
   177 			ajaxurl,
   144 			{
   178 			{
   145 				action: 'sample-permalink',
   179 				action: 'sample-permalink',
   146 				post_id: post_id,
   180 				post_id: post_id,
   147 				new_title: jQuery('#title').val(),
   181 				new_title: jQuery('#title').val(),
   148 				samplepermalinknonce: jQuery('#samplepermalinknonce').val()
   182 				samplepermalinknonce: jQuery('#samplepermalinknonce').val()
   149 			},
   183 			},
   150 			function(data) {
   184 			function(data) {
   151 				jQuery('#edit-slug-box').html(data);
   185 				jQuery('#edit-slug-box').html(data);
   152 				make_slugedit_clickable();
   186 				makeSlugeditClickable();
   153 			}
   187 			}
   154 		);
   188 		);
   155 	}
   189 	}
   156 }
   190 }
   157 
   191 
   158 function autosave_loading() {
   192 function autosave_loading() {
   159 	jQuery('#autosave').html(autosaveL10n.savingText);
   193 	jQuery('#autosave').html(autosaveL10n.savingText);
   160 }
   194 }
   161 
   195 
   162 function autosave_enable_buttons() {
   196 function autosave_enable_buttons() {
   163 	jQuery(".submitbox :button:disabled, .submitbox :submit:disabled").attr('disabled', '');
   197 	// delay that a bit to avoid some rare collisions while the DOM is being updated.
       
   198 	setTimeout(function(){
       
   199 		jQuery(':button, :submit', '#submitpost').removeAttr('disabled');
       
   200 		jQuery('#ajax-loading').css('visibility', 'hidden');
       
   201 	}, 500);
   164 }
   202 }
   165 
   203 
   166 function autosave_disable_buttons() {
   204 function autosave_disable_buttons() {
   167 	jQuery(".submitbox :button:enabled, .submitbox :submit:enabled").attr('disabled', 'disabled');
   205 	jQuery(':button, :submit', '#submitpost').attr('disabled', 'disabled');
   168 	// Re-enable 5 sec later.  Just gives autosave a head start to avoid collisions.
   206 	// Re-enable 5 sec later.  Just gives autosave a head start to avoid collisions.
   169 	setTimeout(autosave_enable_buttons, 5000);
   207 	setTimeout(autosave_enable_buttons, 5000);
       
   208 }
       
   209 
       
   210 function delayed_autosave() {
       
   211 	setTimeout(function(){
       
   212 		if ( blockSave )
       
   213 			return;
       
   214 		autosave();
       
   215 	}, 200);
   170 }
   216 }
   171 
   217 
   172 autosave = function() {
   218 autosave = function() {
   173 	// (bool) is rich editor enabled and active
   219 	// (bool) is rich editor enabled and active
   174 	var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback;
   220 	var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback;
       
   221 
       
   222 	autosave_disable_buttons();
   175 
   223 
   176 	post_data = {
   224 	post_data = {
   177 		action: "autosave",
   225 		action: "autosave",
   178 		post_ID:  jQuery("#post_ID").val() || 0,
   226 		post_ID:  jQuery("#post_ID").val() || 0,
   179 		post_title: jQuery("#title").val() || "",
   227 		post_title: jQuery("#title").val() || "",
   180 		autosavenonce: jQuery('#autosavenonce').val(),
   228 		autosavenonce: jQuery('#autosavenonce').val(),
   181 		//tags_input: jQuery("#tags-input").val() || "",
       
   182 		post_type: jQuery('#post_type').val() || "",
   229 		post_type: jQuery('#post_type').val() || "",
   183 		autosave: 1
   230 		autosave: 1
   184 	};
   231 	};
   185 
   232 
   186 	jQuery('.tags-input').each( function() {
   233 	jQuery('.tags-input').each( function() {
   194 	// No autosave while thickbox is open (media buttons)
   241 	// No autosave while thickbox is open (media buttons)
   195 	if ( jQuery("#TB_window").css('display') == 'block' )
   242 	if ( jQuery("#TB_window").css('display') == 'block' )
   196 		doAutoSave = false;
   243 		doAutoSave = false;
   197 
   244 
   198 	/* Gotta do this up here so we can check the length when tinyMCE is in use */
   245 	/* Gotta do this up here so we can check the length when tinyMCE is in use */
   199 	if ( rich ) {
   246 	if ( rich && doAutoSave ) {
   200 		ed = tinyMCE.activeEditor;
   247 		ed = tinyMCE.activeEditor;
   201 		if ( 'mce_fullscreen' == ed.id )
   248 		// Don't run while the TinyMCE spellcheck is on. It resets all found words.
   202 			tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
   249 		if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
   203 		tinyMCE.get('content').save();
   250 			doAutoSave = false;
       
   251 		} else {
       
   252 			if ( 'mce_fullscreen' == ed.id )
       
   253 				tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
       
   254 			tinyMCE.get('content').save();
       
   255 		}
   204 	}
   256 	}
   205 
   257 
   206 	post_data["content"] = jQuery("#content").val();
   258 	post_data["content"] = jQuery("#content").val();
   207 	if ( jQuery('#post_name').val() )
   259 	if ( jQuery('#post_name').val() )
   208 		post_data["post_name"] = jQuery('#post_name').val();
   260 		post_data["post_name"] = jQuery('#post_name').val();
   209 
   261 
   210 	// Nothing to save or no change.
   262 	// 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) {
   263 	if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) {
   212 		doAutoSave = false;
   264 		doAutoSave = false;
   213 	}
   265 	}
   214 
   266 
   215 	autosave_disable_buttons();
       
   216 
       
   217 	origStatus = jQuery('#original_post_status').val();
   267 	origStatus = jQuery('#original_post_status').val();
   218 
   268 
   219 	autosaveLast = jQuery("#title").val()+jQuery("#content").val();
       
   220 	goodcats = ([]);
   269 	goodcats = ([]);
   221 	jQuery("[name='post_category[]']:checked").each( function(i) {
   270 	jQuery("[name='post_category[]']:checked").each( function(i) {
   222 		goodcats.push(this.value);
   271 		goodcats.push(this.value);
   223 	} );
   272 	} );
   224 	post_data["catslist"] = goodcats.join(",");
   273 	post_data["catslist"] = goodcats.join(",");
   231 		post_data["excerpt"] = jQuery("#excerpt").val();
   280 		post_data["excerpt"] = jQuery("#excerpt").val();
   232 	if ( jQuery("#post_author").size() )
   281 	if ( jQuery("#post_author").size() )
   233 		post_data["post_author"] = jQuery("#post_author").val();
   282 		post_data["post_author"] = jQuery("#post_author").val();
   234 	post_data["user_ID"] = jQuery("#user-id").val();
   283 	post_data["user_ID"] = jQuery("#user-id").val();
   235 
   284 
   236 	// Don't run while the TinyMCE spellcheck is on. It resets all found words.
   285 	if ( doAutoSave ) {
   237 	if ( rich && tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active ) {
   286 		autosaveLast = jQuery("#title").val()+jQuery("#content").val();
   238 		doAutoSave = false;
   287 	} else {
       
   288 		post_data['autosave'] = 0;
   239 	}
   289 	}
   240 
   290 
   241 	if ( parseInt(post_data["post_ID"], 10) < 1 ) {
   291 	if ( parseInt(post_data["post_ID"], 10) < 1 ) {
   242 		post_data["temp_ID"] = post_data["post_ID"];
   292 		post_data["temp_ID"] = post_data["post_ID"];
   243 		successCallback = autosave_saved_new; // new post
   293 		successCallback = autosave_saved_new; // new post
   244 	} else {
   294 	} else {
   245 		successCallback = autosave_saved; // pre-existing post
   295 		successCallback = autosave_saved; // pre-existing post
   246 	}
       
   247 
       
   248 	if ( !doAutoSave ) {
       
   249 		post_data['autosave'] = 0;
       
   250 	}
   296 	}
   251 
   297 
   252 	autosaveOldMessage = jQuery('#autosave').html();
   298 	autosaveOldMessage = jQuery('#autosave').html();
   253 
   299 
   254 	jQuery.ajax({
   300 	jQuery.ajax({