Skip to content

Commit

Permalink
topic modify plugin more extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
live627 committed Dec 17, 2024
1 parent 97af823 commit b28e58a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 87 deletions.
25 changes: 13 additions & 12 deletions Themes/default/MessageIndex.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,11 @@ function template_main()
<img src="', $topic['first_post']['icon_url'], '" alt="">
', $topic['is_posted_in'] ? '<span class="main_icons profile_sm"></span>' : '', '
</div>
<div class="topic_info', !empty(Utils::$context['can_quick_mod']) ? '' : ' info_qmod', '">
<div ', (!empty($topic['quick_mod']['modify']) ? 'id="topic_' . $topic['first_post']['id'] . '" ondblclick="oQuickModifyTopic.modify_topic(\'' . $topic['id'] . '\', \'' . $topic['first_post']['id'] . '\');"' : ''), '>';
<div', !empty($topic['quick_mod']['modify']) ? ' data-msg-id="' . $topic['first_post']['id'] . '"' : '', '>';

// Now we handle the icons
echo '
<div class="icons floatright">';
<div id="icons', $topic['first_post']['id'], '" class="icons floatright">';

if ($topic['is_watched'])
echo '
Expand Down Expand Up @@ -236,7 +235,7 @@ function template_main()
<div class="message_index_title">
', $topic['new'] && User::$me->is_logged ? '<a href="' . $topic['new_href'] . '" id="newicon' . $topic['first_post']['id'] . '" class="new_posts">' . Lang::$txt['new'] . '</a>' : '', '
<span class="preview', $topic['is_sticky'] ? ' bold_text' : '', '" title="', $topic[(empty(Config::$modSettings['message_index_preview_first']) ? 'last_post' : 'first_post')]['preview'], '">
<span id="msg_', $topic['first_post']['id'], '">', $topic['first_post']['link'], (!$topic['approved'] ? '&nbsp;<em>(' . Lang::$txt['awaiting_approval'] . ')</em>' : ''), '</span>
<span id="msg', $topic['first_post']['id'], '">', $topic['first_post']['link'], (!$topic['approved'] ? '&nbsp;<em>(' . Lang::$txt['awaiting_approval'] . ')</em>' : ''), '</span>
</span>
</div>
<p class="floatleft">
Expand Down Expand Up @@ -348,10 +347,12 @@ function template_main()
// Show breadcrumbs at the bottom too.
theme_linktree();

echo '
<script>
window.addEventListener("DOMContentLoaded", function() {';

if (!empty(Utils::$context['can_quick_mod']) && Theme::$current->options['display_quick_mod'] == 1 && !empty(Utils::$context['topics']) && Utils::$context['can_move'])
echo '
<script>
if (typeof(window.XMLHttpRequest) != "undefined")
aJumpTo[aJumpTo.length] = new JumpTo({
sContainerId: "quick_mod_jump_to",
sClassName: "qaction",
Expand All @@ -366,15 +367,15 @@ function template_main()
bNoRedirect: true,
bDisabled: true,
sCustomName: "move_to"
});
</script>';
});';

// Javascript for inline editing.
echo '
<script>
var oQuickModifyTopic = new QuickModifyTopic({
aHidePrefixes: Array("lockicon", "stickyicon", "pages", "newicon"),
bMouseOnDiv: false,
var oQuickModifyTopic = new QuickModifyTopic({
aHidePrefixes: ["icons", "msg", "pages", "newicon"],
bMouseOnDiv: false,
sTopicContainer: "topic_container",
});
});
</script>';

Expand Down
138 changes: 63 additions & 75 deletions Themes/default/scripts/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,61 @@ function QuickModifyTopic(oOptions)
this.oCurSubjectDiv = null;
this.oTopicModHandle = document;
this.bInEditMode = false;
this.bMouseOnDiv = false;
this.init();
}
this.aTextFields = ['subject'];
this.oSourceElments = {};

// Used to initialise the object event handlers
QuickModifyTopic.prototype.init = function ()
{
// Detect and act on keypress
this.oTopicModHandle.onkeydown = this.modify_topic_keypress.bind(this);
const oElement = document.getElementById(oOptions.sTopicContainer);
for (const el of oElement.children)
{
if (el.children[1].dataset.msgId)
el.children[1].addEventListener(
'dblclick',
this.modify_topic.bind(this, el.children[1].dataset.msgId)
);
}

// Used to detect when we've stopped editing.
this.oTopicModHandle.onclick = this.modify_topic_click.bind(this);
};
this.oTopicModHandle.addEventListener('click', function (oEvent)
{
if (this.bInEditMode && oEvent.target.tagName != 'INPUT')
this.modify_topic_save(smf_session_id, smf_session_var);
}.bind(this));
}

// called from the double click in the div
QuickModifyTopic.prototype.modify_topic = function (topic_id, first_msg_id)
{
// already editing
if (this.bInEditMode)
{
// Same message then just return, otherwise drop out of this edit.
// same message then just return, otherwise drop out of this edit.
if (this.iCurTopicId == topic_id)
return;
else
this.modify_topic_cancel();
}

this.bInEditMode = true;
this.bMouseOnDiv = true;
this.iCurTopicId = topic_id;

// 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);
this.sCurMessageId = 'msg_' + first_msg_id;
this.oCurSubjectDiv = document.getElementById('msg_' + first_msg_id);
var oInput = document.createElement('input');
oInput.type = 'text';
oInput.name = 'subject';
oInput.value = this.oCurSubjectDiv.textContent;
oInput.size = '60';
oInput.style.width = '99%';
oInput.maxlength = '80';
oInput.onkeydown = this.modify_topic_keypress.bind(this);
this.oCurSubjectDiv.after(oInput);
oInput.focus();

if (this.opt.funcOnAfterCreate) {
this.opt.funcOnAfterCreate.call(this);
}

// Here we hide any other things they want hidden on edit.
this.set_hidden_topic_areas('none');
}

// callback function from the modify_topic ajax call
Expand All @@ -55,11 +76,6 @@ QuickModifyTopic.prototype.onDocReceived_modify_topic = function (XMLDoc)
return true;
}

this.sCurMessageId = XMLDoc.getElementsByTagName("message")[0].getAttribute("id");
this.oCurSubjectDiv = document.getElementById('msg_' + this.sCurMessageId.substr(4));
this.sBuffSubject = getInnerHTML(this.oCurSubjectDiv);

// Here we hide any other things they want hidden on edit.
this.set_hidden_topic_areas('none');

// Show we are in edit mode and allow the edit
Expand All @@ -70,7 +86,10 @@ QuickModifyTopic.prototype.onDocReceived_modify_topic = function (XMLDoc)
// 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);
for (var i of this.aTextFields)
if (i in document.forms.quickModForm)
document.forms.quickModForm[i].remove();

this.set_hidden_topic_areas('');
this.bInEditMode = false;

Expand All @@ -87,36 +106,20 @@ QuickModifyTopic.prototype.set_hidden_topic_areas = function (set_style)
}
}

// 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"><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);};
}

// Yup that's right, save it
// Yup thats right, save it
QuickModifyTopic.prototype.modify_topic_save = function (cur_session_id, cur_session_var)
{
if (!this.bInEditMode)
return true;

// Add backwards compatibility with old themes.
if (typeof(cur_session_var) == 'undefined')
cur_session_var = 'sesc';

var i, x = new Array();
x[x.length] = 'subject=' + document.forms.quickModForm['subject'].value.php_to8bit().php_urlencode();
x[x.length] = 'topic=' + parseInt(document.forms.quickModForm.elements['topic'].value);
x[x.length] = 'msg=' + parseInt(document.forms.quickModForm.elements['msg'].value);
let x = [];
for (var i of this.aTextFields)
if (i in document.forms.quickModForm)
x.push(i + '=' + document.forms.quickModForm[i].value.php_to8bit().php_urlencode());

// 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);
sendXMLDocument.call(this, smf_prepareScriptUrl(smf_scripturl) + "action=jsmodify;topic=" + this.iCurTopicId + ";" + cur_session_var + "=" + cur_session_id + ";xml", x.join("&"), this.modify_topic_done);

return false;
}
Expand All @@ -134,31 +137,34 @@ QuickModifyTopic.prototype.modify_topic_done = function (XMLDoc)
}

var message = XMLDoc.getElementsByTagName("smf")[0].getElementsByTagName("message")[0];
var subject = message.getElementsByTagName("subject")[0];
var subject = message.getElementsByTagName("subject")[0].childNodes[0].nodeValue;
var error = message.getElementsByTagName("error")[0];

// No subject or other error?

if (!subject || error)
return false;

this.modify_topic_hide_edit(subject.childNodes[0].nodeValue);
setInnerHTML(this.oCurSubjectDiv, '<a href="' + smf_scripturl + '?topic=' + this.iCurTopicId + '.0">' + subject + '<' +'/a>')
this.set_hidden_topic_areas('');
this.bInEditMode = false;

for (var i of this.aTextFields)
{
if (this.oSourceElments[i])
setInnerHTML(this.oSourceElments[i], message.getElementsByTagName(i)[0].childNodes[0].nodeValue);

if (i in document.forms.quickModForm)
document.forms.quickModForm[i].remove();
}

// redo tips if they are on since we just pulled the rug out on this one
if ($.isFunction($.fn.SMFtooltip))
$('.preview').SMFtooltip().smf_tooltip_off;

return false;
}

// 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)
{
Expand All @@ -183,25 +189,6 @@ QuickModifyTopic.prototype.modify_topic_keypress = function (oEvent)
}
}

// 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)
{
Expand Down Expand Up @@ -517,9 +504,10 @@ function InTopicModeration(oOptions)
this.opt = oOptions;
this.bButtonsShown = false;
this.iNumSelected = 0;
this.oRemoveButton = null;
this.oRestoreButton = null;
this.oSplitButton = null;

// Add backwards compatibility with old themes.
if (typeof(this.opt.sSessionVar) == 'undefined')
this.opt.sSessionVar = 'sesc';

this.init();
}
Expand Down

0 comments on commit b28e58a

Please sign in to comment.