summaryrefslogtreecommitdiffstatshomepage
path: root/wp-includes/js/autosave.js
blob: bdea35cca9552457903a4fed58c138e290132a78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var autosaveLast = '';
var autosavePeriodical;

function autosave_start_timer() {
	autosaveLast = jQuery('#post #title').val()+jQuery('#post #content').val();
	// Keep autosave_interval in sync with edit_post().
	autosavePeriodical = jQuery.schedule({time: autosaveL10n.autosaveInterval * 1000, func: autosave, repeat: true, protect: true});

	//Disable autosave after the form has been submitted
	jQuery("#post #submit").submit(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #save").click(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #submit").click(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #publish").click(function() { jQuery.cancel(autosavePeriodical); });
	jQuery("#post #deletepost").click(function() { jQuery.cancel(autosavePeriodical); });
}
addLoadEvent(autosave_start_timer)

function autosave_cur_time() {
	var now = new Date();
	return "" + ((now.getHours() >12) ? now.getHours() -12 : now.getHours()) + 
	((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes() +
	((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
}

function autosave_update_post_ID(response) {
	var res = parseInt(response);
	var message;

	if(isNaN(res)) {
		message = autosaveL10n.errorText.replace(/%response%/g, response);
	} else {
		message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time());
		jQuery('#post_ID').attr({name: "post_ID"});
		jQuery('#post_ID').val(res);
		// We need new nonces
		jQuery.post(autosaveL10n.requestFile, {
			action: "autosave-generate-nonces",
			post_ID: res,
			cookie: document.cookie,
			post_type: jQuery('#post_type').val()
		}, function(html) {
			jQuery('#_wpnonce').val(html);
		});
		jQuery('#hiddenaction').val('editpost');
	}
	jQuery('#autosave').html(message);
	autosave_enable_buttons();
}

function autosave_loading() {
	jQuery('#autosave').html(autosaveL10n.savingText);
}

function autosave_saved(response) {
	var res = parseInt(response);
	var message;

	if(isNaN(res)) {
		message = autosaveL10n.errorText.replace(/%response%/g, response);
	} else {
		message = autosaveL10n.saveText.replace(/%time%/g, autosave_cur_time());
	}
	jQuery('#autosave').html(message);
	autosave_enable_buttons();
}

function autosave_disable_buttons() {
	jQuery("#post #save:enabled").attr('disabled', 'disabled');
	jQuery("#post #submit:enabled").attr('disabled', 'disabled');
	jQuery("#post #publish:enabled").attr('disabled', 'disabled');
	jQuery("#post #deletepost:enabled").attr('disabled', 'disabled');
	setTimeout('autosave_enable_buttons();', 1000); // Re-enable 1 sec later.  Just gives autosave a head start to avoid collisions.
}

function autosave_enable_buttons() {
	jQuery("#post #save:disabled").attr('disabled', '');
	jQuery("#post #submit:disabled").attr('disabled', '');
	jQuery("#post #publish:disabled").attr('disabled', '');
	jQuery("#post #deletepost:disabled").attr('disabled', '');
}

function autosave() {
	var rich = ((typeof tinyMCE != "undefined") && tinyMCE.getInstanceById('content')) ? true : false;
	var post_data = {
			action: "autosave",
			post_ID:  jQuery("#post_ID").val() || 0,
			post_title: jQuery("#title").val() || "",
			cookie: document.cookie,
			tags_input: jQuery("#tags-input").val() || "",
			post_type: jQuery('#post_type').val() || ""
		};

	/* Gotta do this up here so we can check the length when tinyMCE is in use */
	if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 || rich == false ) {
		post_data["content"] = jQuery("#content").val();
	} else {
		// Don't run while the TinyMCE spellcheck is on.
		if(tinyMCE.selectedInstance.spellcheckerOn) return;
		tinyMCE.wpTriggerSave();
		post_data["content"] = jQuery("#content").val();
	}

	if(post_data["post_title"].length==0 || post_data["content"].length==0 || post_data["post_title"] + post_data["content"] == autosaveLast) {
		return;
	}

	autosave_disable_buttons();

	autosaveLast = jQuery("#title").val()+jQuery("#content").val();
	goodcats = ([]);
	jQuery("[@name='post_category[]']:checked").each( function(i) {
		goodcats.push(this.value);
	} );
	post_data["catslist"] = goodcats.join(",");

	if ( jQuery("#comment_status").attr("checked") )
		post_data["comment_status"] = 'open';
	if ( jQuery("#ping_status").attr("checked") )
		post_data["ping_status"] = 'open';
	if( jQuery("#excerpt"))
		post_data["excerpt"] = jQuery("#excerpt").val();

	if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 || rich == false ) {
		post_data["content"] = jQuery("#content").val();
	} else {
		tinyMCE.wpTriggerSave();
		post_data["content"] = jQuery("#content").val();
	}

	if(parseInt(post_data["post_ID"]) < 1) {
		post_data["temp_ID"] = post_data["post_ID"];
		jQuery.ajaxSetup({
			success: function(html) { autosave_update_post_ID(html); }
		});
	} else {
		jQuery.ajaxSetup({
			success: function(html) { autosave_saved(html); }
		});
	}
	jQuery.ajax({
		data: post_data,
		beforeSend: function() { autosave_loading() },
		type: "POST",
		url: autosaveL10n.requestFile
	});
}