<commit_before>/**
 * @name      Elkarte Forum
 * @copyright Elkarte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.0 Alpha
 *
 * This file contains javascript associated with the drafts auto function.
 */

function smf_DraftAutoSave(oOptions)
{
	this.opt = oOptions;
	this.bInDraftMode = false;
	this.sCurDraftId = '';
	this.oCurDraftDiv = null;
	this.interval_id = null;
	this.oDraftHandle = window;
	this.sLastSaved = '';
	this.bPM = this.opt.bPM ? true : false;
	this.sCheckDraft = '';

	// slight delay on autosave init to allow sceditor to create the iframe
	setTimeout('addLoadEvent(' + this.opt.sSelf + '.init())', 3000);
}

// Start our self calling routine
smf_DraftAutoSave.prototype.init = function ()
{
	if (this.opt.iFreq > 0)
	{
		// start the autosave timer
		this.interval_id = this.oDraftHandle.setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);

		// Set up the textarea focus and blur events
		this.oDraftHandle.instanceRef = this;
		if (typeof(this.instanceRef) !== 'undefined')
		{
			this.oDraftHandle.onblur = function (oEvent) {return this.instanceRef.draftBlur(oEvent, true);};
			this.oDraftHandle.onfocus = function (oEvent) {return this.instanceRef.draftFocus(oEvent, true);};
		}

		// find the editors wysiwyg iframe and get its window to set focus/blur events for it as well
		var oIframe = document.getElementsByTagName('iframe')[0],
			oIframeWindow = typeof(oIframe) !== 'undefined' ? oIframe.contentWindow || oIframe.contentDocument : null;

		if (oIframeWindow !== null && oIframeWindow.document)
		{
			var oIframeDoc = oIframeWindow.document;
			// @todo oDraftAutoSave should be this.opt.sSelf name not hard code
			oIframeDoc.body.onblur = function (oEvent) {return parent.oDraftAutoSave.draftBlur(oEvent, false);};
			oIframeDoc.body.onfocus = function (oEvent) {return parent.oDraftAutoSave.draftFocus(oEvent, false);};
		}
	}
}

// Moved away from the page, where did you go? ... till you return we pause autosaving
smf_DraftAutoSave.prototype.draftBlur = function(oEvent, source)
{
	if (this.opt.sType === 'quick' || $('#' + this.opt.sSceditorID).data("sceditor").inSourceMode() === source)
	{
		// save what we have and turn of the autosave
		if (this.bPM)
			this.draftPMSave();
		else
			this.draftSave();

		if (this.interval_id !== "")
			window.clearInterval(this.interval_id);

		this.interval_id = "";
	}
	return;
}

// Since your back we resume the autosave timer
smf_DraftAutoSave.prototype.draftFocus = function(oEvent, source)
{
	if (this.opt.sType === 'quick' || $('#' + this.opt.sSceditorID).data("sceditor").inSourceMode() === source)
	{
		if (this.interval_id === "")
			this.interval_id = window.setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);
	}
	return;
}

// Make the call to save this draft in the background
smf_DraftAutoSave.prototype.draftSave = function ()
{
	if (this.opt.sType !== 'quick')
		var sPostdata = $('#' + this.opt.sSceditorID).data("sceditor").getText();
	else
		var sPostdata = document.forms.postmodify["message"].value;

	// nothing to save or already posting or nothing changed?
	if (isEmptyText(sPostdata) || smf_formSubmitted || this.sCheckDraft === sPostdata)
		return false;

	// Still saving the last one or other?
	if (this.bInDraftMode)
		this.draftCancel();

	// Flag that we are saving a draft
	document.getElementById('throbber').style.display = '';
	this.bInDraftMode = true;

	// Get the form elements that we want to save
	var aSections = [
		'topic=' + parseInt(document.forms.postmodify.elements['topic'].value),
		'id_draft=' + parseInt(document.forms.postmodify.elements['id_draft'].value),
		'subject=' + escape(document.forms.postmodify['subject'].value.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B"),
		'message=' + escape(sPostdata.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B"),
		'icon=' + escape(document.forms.postmodify['icon'].value.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B"),
		'save_draft=true',
		smf_session_var + '=' + smf_session_id,
	];

	// Get the locked an/or sticky values if they have been selected or set that is
	if (this.opt.sType === 'post')
	{
		if (document.getElementById('check_lock').checked)
			aSections[aSections.length] = 'lock=1';
		if (document.getElementById('check_sticky').checked)
			aSections[aSections.length] = 'sticky=1';
	}<commit_msg>! Some browsers are just dumb, ie8 is one of them.  Fix draft autosave with ie8<commit_after>/**
 * @name      Elkarte Forum
 * @copyright Elkarte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.0 Alpha
 *
 * This file contains javascript associated with the drafts auto function.
 */

function smf_DraftAutoSave(oOptions)
{
	this.opt = oOptions;
	this.bInDraftMode = false;
	this.sCurDraftId = '';
	this.oCurDraftDiv = null;
	this.interval_id = null;
	this.oDraftHandle = window;
	this.sLastSaved = '';
	this.bPM = this.opt.bPM ? true : false;
	this.sCheckDraft = '';

	// slight delay on autosave init to allow sceditor to create the iframe
	setTimeout('addLoadEvent(' + this.opt.sSelf + '.init())', 3000);
}

// Start our self calling routine
smf_DraftAutoSave.prototype.init = function ()
{
	if (this.opt.iFreq > 0)
	{
		// start the autosave timer
		this.interval_id = this.oDraftHandle.setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);

		// Set up the textarea focus and blur events
		this.oDraftHandle.instanceRef = this;
		if (typeof(this.instanceRef) !== 'undefined')
		{
			this.oDraftHandle.onblur = function (oEvent) {return this.instanceRef.draftBlur(oEvent, true);};
			this.oDraftHandle.onfocus = function (oEvent) {return this.instanceRef.draftFocus(oEvent, true);};
		}

		// find the editors wysiwyg iframe and get its window to set focus/blur events for it as well
		var oIframe = document.getElementsByTagName('iframe')[0],
			oIframeWindow = typeof(oIframe) !== 'undefined' ? oIframe.contentWindow || oIframe.contentDocument : null;

		if (oIframeWindow !== null && oIframeWindow.document)
		{
			var oIframeDoc = oIframeWindow.document;
			// @todo oDraftAutoSave should be this.opt.sSelf name not hard code
			oIframeDoc.body.onblur = function (oEvent) {return parent.oDraftAutoSave.draftBlur(oEvent, false);};
			oIframeDoc.body.onfocus = function (oEvent) {return parent.oDraftAutoSave.draftFocus(oEvent, false);};
		}
	}
}

// Moved away from the page, where did you go? ... till you return we pause autosaving
smf_DraftAutoSave.prototype.draftBlur = function(oEvent, source)
{
	if (this.opt.sType === 'quick' || $('#' + this.opt.sSceditorID).data("sceditor").inSourceMode() === source)
	{
		// save what we have and turn of the autosave
		if (this.bPM)
			this.draftPMSave();
		else
			this.draftSave();

		if (this.interval_id !== "")
			window.clearInterval(this.interval_id);

		this.interval_id = "";
	}
	return;
}

// Since your back we resume the autosave timer
smf_DraftAutoSave.prototype.draftFocus = function(oEvent, source)
{
	if (this.opt.sType === 'quick' || $('#' + this.opt.sSceditorID).data("sceditor").inSourceMode() === source)
	{
		if (this.interval_id === "")
			this.interval_id = window.setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);
	}
	return;
}

// Make the call to save this draft in the background
smf_DraftAutoSave.prototype.draftSave = function ()
{
	if (this.opt.sType !== 'quick')
		var sPostdata = $('#' + this.opt.sSceditorID).data("sceditor").getText();
	else
		var sPostdata = document.forms.postmodify["message"].value;

	// nothing to save or already posting or nothing changed?
	if (isEmptyText(sPostdata) || smf_formSubmitted || this.sCheckDraft === sPostdata)
		return false;

	// Still saving the last one or other?
	if (this.bInDraftMode)
		this.draftCancel();

	// Flag that we are saving a draft
	document.getElementById('throbber').style.display = '';
	this.bInDraftMode = true;

	// Get the form elements that we want to save
	var aSections = [
		'topic=' + parseInt(document.forms.postmodify.elements['topic'].value),
		'id_draft=' + parseInt(document.forms.postmodify.elements['id_draft'].value),
		'subject=' + escape(document.forms.postmodify['subject'].value.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B"),
		'message=' + escape(sPostdata.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B"),
		'icon=' + escape(document.forms.postmodify['icon'].value.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B"),
		'save_draft=true',
		smf_session_var + '=' + smf_session_id,
	];

	// Get the locked an/or sticky values if they have been selected or set that is
	if (this.opt.sType === 'post')
	{
		var oLock = document.getElementById('check_lock');
		var oSticky = document.getElementById('check_sticky');
		if (oLock && oLock.checked)
			aSections[aSections.length] = 'lock=1';
		if (oSticky && oSticky.checked)
			aSections[aSections.length] = 'sticky=1';
	}
{'fraction_non_alphanumeric': 0.1597574421168688, 'fraction_numerical': 0.005292171995589856, 'mean_word_length': 4.273837209302326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2953429', 'n_tokens_mistral': 3331, 'n_tokens_neox': 3154, 'n_words': 705}