Completed
Pull Request — release-2.1 (#4823)
by
unknown
08:48
created

Themes/default/scripts/drafts.js (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
// The draft save object
2
function smf_DraftAutoSave(oOptions)
3
{
4
	this.opt = oOptions;
5
	this.bInDraftMode = false;
6
	this.sCurDraftId = '';
7
	this.oCurDraftDiv = null;
8
	this.interval_id = null;
9
	this.oDraftHandle = window;
10
	this.sLastSaved = '';
11
	this.bPM = this.opt.bPM ? true : false;
12
	this.sCheckDraft = '';
13
14
	// slight delay on autosave init to allow sceditor to create the iframe
15
	setTimeout('addLoadEvent(' + this.opt.sSelf + '.init())', 4000);
16
}
17
18
// Start our self calling routine
19
smf_DraftAutoSave.prototype.init = function ()
20
{
21
	if (this.opt.iFreq > 0)
22
	{
23
		// find the editors wysiwyg iframe and gets its window
24
		var oIframe = document.getElementsByTagName('iframe')[0];
25
		var oIframeWindow = oIframe.contentWindow || oIframe.contentDocument;
26
		// start the autosave timer
27
		this.interval_id = window.setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);
28
29
		// Set up window focus and blur events
30
		var instanceRef = this;
31
		this.oDraftHandle.onblur = function (oEvent) {return instanceRef.draftBlur(oEvent, true);};
32
		this.oDraftHandle.onfocus = function (oEvent) {return instanceRef.draftFocus(oEvent, true);};
33
34
		// If we found the iframe window, set body focus/blur events for it
35
		if (oIframeWindow.document)
36
		{
37
			var oIframeDoc = oIframeWindow.document;
38
			// @todo oDraftAutoSave should use the this.opt.sSelf name not hardcoded
39
			oIframeDoc.body.onblur = function (oEvent) {return parent.oDraftAutoSave.draftBlur(oEvent, false);};
40
			oIframeDoc.body.onfocus = function (oEvent) {return parent.oDraftAutoSave.draftFocus(oEvent, false);};
41
		};
42
	}
43
}
44
45
// Moved away from the page, where did you go? ... till you return we pause autosaving
46
smf_DraftAutoSave.prototype.draftBlur = function(oEvent, source)
47
{
48
	var e = $('#' + this.opt.sSceditorID).get(0);
49
	if (sceditor.instance(e).inSourceMode() == source)
50
	{
51
		// save what we have and turn of the autosave
52
		if (this.bPM)
53
			this.draftPMSave();
54
		else
55
			this.draftSave();
56
57
		if (this.interval_id != "")
58
			window.clearInterval(this.interval_id);
59
		this.interval_id = "";
60
	}
61
	return;
62
}
63
64
// Since you're back we resume the autosave timer
65
smf_DraftAutoSave.prototype.draftFocus = function(oEvent, source)
66
{
67
	var e = $('#' + this.opt.sSceditorID).get(0);
68
	if (sceditor.instance(e).inSourceMode() == source)
69
	{
70
		if (this.interval_id == "")
71
			this.interval_id = window.setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);
72
	}
73
	return;
74
}
75
76
// Make the call to save this draft in the background
77
smf_DraftAutoSave.prototype.draftSave = function ()
78
{
79
	var e = $('#' + this.opt.sSceditorID).get(0);
80
	var sPostdata = sceditor.instance(e).getText(true);
81
	var sPosticon = (typeof document.forms.postmodify['icon'] === 'undefined' ? 'xx' : document.forms.postmodify['icon'].value);
82
	var sPostsubj = (typeof document.forms.postmodify['subject'] === 'undefined' ? '' : document.forms.postmodify['subject'].value);
83
84
	// nothing to save or already posting or nothing changed?
85
	if (isEmptyText(sPostdata) || smf_formSubmitted || this.sCheckDraft == sPostdata)
86
		return false;
87
88
	// Still saving the last one or other?
89
	if (this.bInDraftMode)
90
		this.draftCancel();
91
92
	// Flag that we are saving a draft
93
	document.getElementById('throbber').style.display = '';
94
	this.bInDraftMode = true;
95
96
	// Get the form elements that we want to save
97
	var aSections = [
98
		'topic=' + parseInt(document.forms.postmodify.elements['topic'].value),
99
		'id_draft=' + (('id_draft' in document.forms.postmodify.elements) ? parseInt(document.forms.postmodify.elements['id_draft'].value) : 0),
100
		'subject=' + escape(sPostsubj.php_to8bit()).replace(/\+/g, "%2B"),
101
		'message=' + escape(sPostdata.php_to8bit()).replace(/\+/g, "%2B"),
102
		'icon=' + escape(sPosticon.php_to8bit()).replace(/\+/g, "%2B"),
103
		'save_draft=true',
104
		smf_session_var + '=' + smf_session_id,
105
	];
106
107
	// Get the locked an/or sticky values if they have been selected or set that is
108
	if (this.opt.sType == 'post')
109
	{
110
		if (document.getElementById('check_lock') && document.getElementById('check_lock').checked)
111
			aSections[aSections.length] = 'lock=1';
112
		if (document.getElementById('check_sticky') && document.getElementById('check_sticky').checked)
113
			aSections[aSections.length] = 'sticky=1';
114
	}
115
116
	// keep track of source or wysiwyg
117
	var e = $('#' + this.opt.sSceditorID).get(0);
118
	aSections[aSections.length] = 'message_mode=' + sceditor.instance(e).inSourceMode();
119
120
	// Send in document for saving and hope for the best
121
	sendXMLDocument.call(this, smf_prepareScriptUrl(smf_scripturl) + "action=post2;board=" + this.opt.iBoard + ";xml", aSections.join("&"), this.onDraftDone);
122
123
	// Save the latest for compare
124
	this.sCheckDraft = sPostdata;
125
}
126
127
// Make the call to save this PM draft in the background
128
smf_DraftAutoSave.prototype.draftPMSave = function ()
129
{
130
	var e = $('#' + this.opt.sSceditorID).get(0);
131
	var sPostdata = sceditor.instance(e).getText();
132
133
	// nothing to save or already posting or nothing changed?
134
	if (isEmptyText(sPostdata) || smf_formSubmitted || this.sCheckDraft == sPostdata)
135
		return false;
136
137
	// Still saving the last one or some other?
138
	if (this.bInDraftMode)
139
		this.draftCancel();
140
141
	// Flag that we are saving
142
	document.getElementById('throbber').style.display = '';
143
	this.bInDraftMode = true;
144
145
	// Get the to and bcc values
146
	var aTo = this.draftGetRecipient('recipient_to[]');
147
	var aBcc = this.draftGetRecipient('recipient_bcc[]');
148
149
	// Get the rest of the form elements that we want to save, and load them up
150
	var aSections = [
151
		'replied_to=' + parseInt(document.forms.postmodify.elements['replied_to'].value),
152
		'id_pm_draft=' + (('id_pm_draft' in document.forms.postmodify.elements) ? parseInt(document.forms.postmodify.elements['id_pm_draft'].value) : 0),
153
		'subject=' + escape(document.forms.postmodify['subject'].value.php_to8bit()).replace(/\+/g, "%2B"),
154
		'message=' + escape(sPostdata.php_to8bit()).replace(/\+/g, "%2B"),
155
		'recipient_to=' + aTo,
156
		'recipient_bcc=' + aBcc,
157
		'save_draft=true',
158
		smf_session_var + '=' + smf_session_id,
159
	];
160
161
	// account for wysiwyg
162
	if (this.opt.sType == 'post')
163
		aSections[aSections.length] = 'message_mode=' + parseInt(document.forms.postmodify.elements['message_mode'].value);
164
165
	// Send in (post) the document for saving
166
	sendXMLDocument.call(this, smf_prepareScriptUrl(smf_scripturl) + "action=pm;sa=send2;xml", aSections.join("&"), this.onDraftDone);
167
168
	// Save the latest for compare
169
	this.sCheckDraft = sPostdata;
170
}
171
172
// Callback function of the XMLhttp request for saving the draft message
173
smf_DraftAutoSave.prototype.onDraftDone = function (XMLDoc)
174
{
175
	// If it is not valid then clean up
176
	if (!XMLDoc || !XMLDoc.getElementsByTagName('draft'))
177
		return this.draftCancel();
178
179
	// Grab the returned draft id and saved time from the response
180
	this.sCurDraftId = XMLDoc.getElementsByTagName('draft')[0].getAttribute('id');
181
	this.sLastSaved = XMLDoc.getElementsByTagName('draft')[0].childNodes[0].nodeValue;
182
183
	// Update the form to show we finished, if the id is not set, then set it
184
	document.getElementById(this.opt.sLastID).value = this.sCurDraftId;
185
	oCurDraftDiv = document.getElementById(this.opt.sLastNote);
0 ignored issues
show
The variable oCurDraftDiv seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.oCurDraftDiv.
Loading history...
186
	setInnerHTML(oCurDraftDiv, this.sLastSaved);
187
188
	// hide the saved draft infobox in the event they pressed the save draft button at some point
189
	if (this.opt.sType == 'post')
190
		document.getElementById('draft_section').style.display = 'none';
191
192
	// thank you sir, may I have another
193
	this.bInDraftMode = false;
194
	document.getElementById('throbber').style.display = 'none';
195
}
196
197
// function to retrieve the to and bcc values from the pseudo arrays
198
smf_DraftAutoSave.prototype.draftGetRecipient = function (sField)
199
{
200
	var oRecipient = document.forms.postmodify.elements[sField];
201
	var aRecipient = []
202
203
	if (typeof(oRecipient) != 'undefined')
204
	{
205
		// just one recipient
206
		if ('value' in oRecipient)
207
			aRecipient.push(parseInt(oRecipient.value));
208
		else
209
		{
210
			// or many !
211
			for (var i = 0, n = oRecipient.length; i < n; i++)
212
				aRecipient.push(parseInt(oRecipient[i].value));
213
		}
214
	}
215
	return aRecipient;
216
}
217
218
// If another auto save came in with one still pending
219
smf_DraftAutoSave.prototype.draftCancel = function ()
220
{
221
	// can we do anything at all ... do we want to (e.g. sequence our async events?)
222
	// @todo if not remove this function
223
	this.bInDraftMode = false;
224
	document.getElementById('throbber').style.display = 'none';
225
}
226