Browse Source

! update and convert modify topic functions to an object (QuickModifyTopic)

Signed-off-by: Spuds <[email protected]>
Spuds 11 years ago
parent
commit
ffdf012828
2 changed files with 163 additions and 92 deletions
  1. 5 9
      Themes/default/MessageIndex.template.php
  2. 158 83
      Themes/default/scripts/topic.js

+ 5 - 9
Themes/default/MessageIndex.template.php

@@ -252,7 +252,7 @@ function template_main()
 						</div>
 					</td>
 					<td class="', $alternate_class, ' subject">
-						<div ', (!empty($topic['quick_mod']['modify']) ? 'id="topic_' . $topic['first_post']['id'] . '" onmouseout="mouse_on_div = 0;" onmouseover="mouse_on_div = 1;" ondblclick="modify_topic(\'' . $topic['id'] . '\', \'' . $topic['first_post']['id'] . '\');"' : ''), '>';
+						<div ', (!empty($topic['quick_mod']['modify']) ? 'id="topic_' . $topic['first_post']['id'] . '"  ondblclick="oQuickModifyTopic.modify_topic(\'' . $topic['id'] . '\', \'' . $topic['first_post']['id'] . '\');"' : ''), '>';
 
 			// [WIP] Methinks the orange icons look better if they aren't all over the page.
 			// Is this topic new? (assuming they are logged in!)
@@ -425,14 +425,10 @@ function template_main()
 	echo '
 <script type="text/javascript" src="', $settings['default_theme_url'], '/scripts/topic.js"></script>
 <script type="text/javascript"><!-- // --><![CDATA[
-
-	// Hide certain bits during topic edit.
-	hide_prefixes.push("lockicon", "stickyicon", "pages", "newicon");
-
-	// Use it to detect when we\'ve stopped editing.
-	document.onclick = modify_topic_click;
-
-	var mouse_on_div;
+	var oQuickModifyTopic = new QuickModifyTopic({
+		aHidePrefixes: Array("lockicon", "stickyicon", "pages", "newicon"),
+		bMouseOnDiv: false,
+	});
 // ]]></script>';
 }
 

+ 158 - 83
Themes/default/scripts/topic.js

@@ -1,66 +1,134 @@
-var cur_topic_id, cur_msg_id, buff_subject, cur_subject_div, in_edit_mode = 0;
-var hide_prefixes = Array();
+// *** QuickModifyTopic object.
+function QuickModifyTopic(oOptions)
+{
+	this.opt = oOptions;
+	this.aHidePrefixes = this.opt.aHidePrefixes;
+	this.iCurTopicId = 0;
+	this.sCurMessageId = '';
+	this.sBuffSubject = '';
+	this.oCurSubjectDiv = null;
+	this.oTopicModHandle = document;
+	this.bInEditMode = false;
+	this.bMouseOnDiv = false;
+	this.bXmlHttpCapable = this.isXmlHttpCapable();
+	this.init();
+}
 
-function modify_topic(topic_id, first_msg_id)
+// @todo Determine if this is even needed anymore opera meh
+// Ajax supported?
+QuickModifyTopic.prototype.isXmlHttpCapable = function ()
 {
-	if (!('XMLHttpRequest' in window))
-		return;
+	if (typeof(window.XMLHttpRequest) == 'undefined')
+		return false;
 
+	// Opera didn't always support POST requests. So test it first.
 	if ('opera' in window)
 	{
 		var oTest = new XMLHttpRequest();
 		if (!('setRequestHeader' in oTest))
-			return;
+			return false;
 	}
 
+	return true;
+}
+
+// Used to initialise the object event handlers
+QuickModifyTopic.prototype.init = function ()
+{
+	// Attach some events to it so we can respond to actions
+	this.oTopicModHandle.instanceRef = this;
+
+	// detect and act on keypress
+	this.oTopicModHandle.onkeydown = function (oEvent) {return this.instanceRef.modify_topic_keypress(oEvent);};
+
+	// Used to detect when we've stopped editing.
+	this.oTopicModHandle.onclick = function (oEvent) {return this.instanceRef.modify_topic_click(oEvent);};
+}
+
+// called from the double click in the div
+QuickModifyTopic.prototype.modify_topic = function (topic_id, first_msg_id)
+{
 	// Add backwards compatibility with old themes.
 	if (typeof(cur_session_var) == 'undefined')
 		cur_session_var = 'sesc';
 
-	if (in_edit_mode == 1)
+	// already editing
+	if (this.bInEditMode)
 	{
-		if (cur_topic_id == topic_id)
+		// same message then just return, otherwise drop out of this edit.
+		if (this.iCurTopicId == topic_id)
 			return;
 		else
-			modify_topic_cancel();
+			this.modify_topic_cancel();
 	}
 
-	in_edit_mode = 1;
-	mouse_on_div = 1;
-	cur_topic_id = topic_id;
+	this.bInEditMode = true;
+	this.bMouseOnDiv = true;
+	this.iCurTopicId = topic_id;
 
-	if (typeof window.ajax_indicator == "function")
-		ajax_indicator(true);
-	getXMLDocument(smf_prepareScriptUrl(smf_scripturl) + "action=quotefast;quote=" + first_msg_id + ";modify;xml", onDocReceived_modify_topic);
+	// Get the topics current subject 
+	ajax_indicator(true);
+	sendXMLDocument.call(this, smf_prepareScriptUrl(smf_scripturl) + "action=quotefast;quote=" + first_msg_id + ";modify;xml", '', this.onDocReceived_modify_topic);
 }
 
-function onDocReceived_modify_topic(XMLDoc)
+// callback function from the modify_topic ajax call
+QuickModifyTopic.prototype.onDocReceived_modify_topic = function (XMLDoc)
 {
-	cur_msg_id = XMLDoc.getElementsByTagName("message")[0].getAttribute("id");
+	// If it is not valid then clean up
+	if (!XMLDoc || !XMLDoc.getElementsByTagName('message'))
+	{
+		this.modify_topic_cancel();
+		return true;
+	}
+	
+	this.sCurMessageId = XMLDoc.getElementsByTagName("message")[0].getAttribute("id");
+	this.oCurSubjectDiv = document.getElementById('msg_' + this.sCurMessageId.substr(4));
+	this.sBuffSubject = getInnerHTML(this.oCurSubjectDiv);
 
-	cur_subject_div = document.getElementById('msg_' + cur_msg_id.substr(4));
-	buff_subject = getInnerHTML(cur_subject_div);
+	// Here we hide any other things they want hidden on edit.
+	this.set_hidden_topic_areas('none');
 
-	// Here we hide any other things they want hiding on edit.
-	set_hidden_topic_areas('none');
+	// Show we are in edit mode and allow the edit
+	ajax_indicator(false);
+	this.modify_topic_show_edit(XMLDoc.getElementsByTagName("subject")[0].childNodes[0].nodeValue);
+}
 
-	modify_topic_show_edit(XMLDoc.getElementsByTagName("subject")[0].childNodes[0].nodeValue);
-	if (typeof window.ajax_indicator == "function")
-		ajax_indicator(false);
+// Cancel out of an edit and return things to back to what they were
+QuickModifyTopic.prototype.modify_topic_cancel = function ()
+{
+	setInnerHTML(this.oCurSubjectDiv, this.sBuffSubject);
+	this.set_hidden_topic_areas('');
+	this.bInEditMode = false;
+	
+	return false;
 }
 
-function modify_topic_cancel()
+// Simply restore/show any hidden bits during topic editing.
+QuickModifyTopic.prototype.set_hidden_topic_areas = function (set_style)
 {
-	setInnerHTML(cur_subject_div, buff_subject);
-	set_hidden_topic_areas('');
+	for (var i = 0; i < this.aHidePrefixes.length; i++)
+	{
+		if (document.getElementById(this.aHidePrefixes[i] + this.sCurMessageId.substr(4)) != null)
+			document.getElementById(this.aHidePrefixes[i] + this.sCurMessageId.substr(4)).style.display = set_style;
+	}
+}
 
-	in_edit_mode = 0;
-	return false;
+// For templating, shown that an inline edit is being made.
+QuickModifyTopic.prototype.modify_topic_show_edit = function (subject)
+{
+	// Just template the subject.
+	setInnerHTML(this.oCurSubjectDiv, '<input type="text" name="subject" value="' + subject + '" size="60" style="width: 95%;" maxlength="80" class="input_text" /><input type="hidden" name="topic" value="' + this.iCurTopicId + '" /><input type="hidden" name="msg" value="' + this.sCurMessageId.substr(4) + '" />');
+	
+	// attach mouse over and out events to this new div
+	this.oCurSubjectDiv.instanceRef = this;
+	this.oCurSubjectDiv.onmouseout = function (oEvent) {return this.instanceRef.modify_topic_mouseout(oEvent);};
+	this.oCurSubjectDiv.onmouseover = function (oEvent) {return this.instanceRef.modify_topic_mouseover(oEvent);};
 }
 
-function modify_topic_save(cur_session_id, cur_session_var)
+// Yup thats right, save it
+QuickModifyTopic.prototype.modify_topic_save = function (cur_session_id, cur_session_var)
 {
-	if (!in_edit_mode)
+	if (!this.bInEditMode)
 		return true;
 
 	// Add backwards compatibility with old themes.
@@ -72,18 +140,22 @@ function modify_topic_save(cur_session_id, cur_session_var)
 	x[x.length] = 'topic=' + parseInt(document.forms.quickModForm.elements['topic'].value);
 	x[x.length] = 'msg=' + parseInt(document.forms.quickModForm.elements['msg'].value);
 
-	if (typeof window.ajax_indicator == "function")
-		ajax_indicator(true);
-	sendXMLDocument(smf_prepareScriptUrl(smf_scripturl) + "action=jsmodify;topic=" + parseInt(document.forms.quickModForm.elements['topic'].value) + ";" + cur_session_var + "=" + cur_session_id + ";xml", x.join("&"), modify_topic_done);
+	// send in the call to save the updated topic subject
+	ajax_indicator(true);
+	sendXMLDocument.call(this, smf_prepareScriptUrl(smf_scripturl) + "action=jsmodify;topic=" + parseInt(document.forms.quickModForm.elements['topic'].value) + ";" + cur_session_var + "=" + cur_session_id + ";xml", x.join("&"), this.modify_topic_done);
 
 	return false;
 }
 
-function modify_topic_done(XMLDoc)
+// done with the edit, if all went well show the new topic title
+QuickModifyTopic.prototype.modify_topic_done = function (XMLDoc)
 {
-	if (!XMLDoc)
+	ajax_indicator(false);
+	
+	// If it is not valid then clean up
+	if (!XMLDoc || !XMLDoc.getElementsByTagName('subject'))
 	{
-		modify_topic_cancel();
+		this.modify_topic_cancel();
 		return true;
 	}
 
@@ -91,33 +163,68 @@ function modify_topic_done(XMLDoc)
 	var subject = message.getElementsByTagName("subject")[0];
 	var error = message.getElementsByTagName("error")[0];
 
-	if (typeof window.ajax_indicator == "function")
-		ajax_indicator(false);
+	// No subject or other error>
 
 	if (!subject || error)
 		return false;
 
-	subjectText = subject.childNodes[0].nodeValue;
-
-	modify_topic_hide_edit(subjectText);
-
-	set_hidden_topic_areas('');
-
-	in_edit_mode = 0;
+	this.modify_topic_hide_edit(subject.childNodes[0].nodeValue);
+	this.set_hidden_topic_areas('');
+	this.bInEditMode = false;
 
 	return false;
 }
 
-// Simply restore any hidden bits during topic editing.
-function set_hidden_topic_areas(set_style)
+// Done with the edit, put in new subject and link.
+QuickModifyTopic.prototype.modify_topic_hide_edit = function (subject)
+{
+	// Re-template the subject!
+	setInnerHTML(this.oCurSubjectDiv, '<a href="' + smf_scripturl + '?topic=' + this.iCurTopicId + '.0">' + subject + '<' +'/a>');
+}
+
+// keypress event ... like enter or escape
+QuickModifyTopic.prototype.modify_topic_keypress = function (oEvent)
 {
-	for (var i = 0; i < hide_prefixes.length; i++)
+	if (typeof(oEvent.keyCode) != "undefined")
 	{
-		if (document.getElementById(hide_prefixes[i] + cur_msg_id.substr(4)) != null)
-			document.getElementById(hide_prefixes[i] + cur_msg_id.substr(4)).style.display = set_style;
+		if (oEvent.keyCode == 27)
+		{
+			this.modify_topic_cancel();
+			if (typeof(oEvent.preventDefault) == "undefined")
+				oEvent.returnValue = false;
+			else
+				oEvent.preventDefault();
+		}
+		else if (oEvent.keyCode == 13)
+		{
+			this.modify_topic_save(smf_session_id, smf_session_var);
+			if (typeof(oEvent.preventDefault) == "undefined")
+				oEvent.returnValue = false;
+			else
+				oEvent.preventDefault();
+		}
 	}
 }
 
+// A click event to signal the finish of the edit 
+QuickModifyTopic.prototype.modify_topic_click = function (oEvent)
+{
+	if (this.bInEditMode && !this.bMouseOnDiv)
+		this.modify_topic_save(smf_session_id, smf_session_var);
+}
+
+// Moved out of the editing div
+QuickModifyTopic.prototype.modify_topic_mouseout = function (oEvent)
+{
+	this.bMouseOnDiv = false;
+}
+
+// Moved back over the editing div
+QuickModifyTopic.prototype.modify_topic_mouseover = function (oEvent)
+{
+	this.bMouseOnDiv = true;
+}
+
 // *** QuickReply object.
 function QuickReply(oOptions)
 {
@@ -628,38 +735,6 @@ function expandThumb(thumbID)
 	return false;
 }
 
-// For templating, shown when an inline edit is made.
-function modify_topic_show_edit(subject)
-{
-	// Just template the subject.
-	setInnerHTML(cur_subject_div, '<input type="text" name="subject" value="' + subject + '" size="60" style="width: 95%;" maxlength="80" onkeypress="modify_topic_keypress(event)" class="input_text" /><input type="hidden" name="topic" value="' + cur_topic_id + '" /><input type="hidden" name="msg" value="' + cur_msg_id.substr(4) + '" />');
-}
-
-function modify_topic_click()
-{
-	if (in_edit_mode == 1 && mouse_on_div == 0)
-		modify_topic_save(smf_sesion_id, smf_session_var);
-}
-
-function modify_topic_keypress(oEvent)
-{
-	if (typeof(oEvent.keyCode) != "undefined" && oEvent.keyCode == 13)
-	{
-		modify_topic_save(smf_sesion_id, smf_session_var);
-		if (typeof(oEvent.preventDefault) == "undefined")
-			oEvent.returnValue = false;
-		else
-			oEvent.preventDefault();
-	}
-}
-
-// And the reverse for hiding it.
-function modify_topic_hide_edit(subject)
-{
-	// Re-template the subject!
-	setInnerHTML(cur_subject_div, '<a href="' + smf_scripturl + '?topic=' + cur_topic_id + '.0">' + subject + '<' +'/a>');
-}
-
 function ignore_toggles(msgids, text)
 {
 	for (i = 0; i < msgids.length; i++)