topic.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. var cur_topic_id, cur_msg_id, buff_subject, cur_subject_div, in_edit_mode = 0;
  2. var hide_prefixes = Array();
  3. function modify_topic(topic_id, first_msg_id)
  4. {
  5. if (!('XMLHttpRequest' in window))
  6. return;
  7. if ('opera' in window)
  8. {
  9. var oTest = new XMLHttpRequest();
  10. if (!('setRequestHeader' in oTest))
  11. return;
  12. }
  13. // Add backwards compatibility with old themes.
  14. if (typeof(cur_session_var) == 'undefined')
  15. cur_session_var = 'sesc';
  16. if (in_edit_mode == 1)
  17. {
  18. if (cur_topic_id == topic_id)
  19. return;
  20. else
  21. modify_topic_cancel();
  22. }
  23. in_edit_mode = 1;
  24. mouse_on_div = 1;
  25. cur_topic_id = topic_id;
  26. if (typeof window.ajax_indicator == "function")
  27. ajax_indicator(true);
  28. getXMLDocument(smf_prepareScriptUrl(smf_scripturl) + "action=quotefast;quote=" + first_msg_id + ";modify;xml", onDocReceived_modify_topic);
  29. }
  30. function onDocReceived_modify_topic(XMLDoc)
  31. {
  32. cur_msg_id = XMLDoc.getElementsByTagName("message")[0].getAttribute("id");
  33. cur_subject_div = document.getElementById('msg_' + cur_msg_id.substr(4));
  34. buff_subject = getInnerHTML(cur_subject_div);
  35. // Here we hide any other things they want hiding on edit.
  36. set_hidden_topic_areas('none');
  37. modify_topic_show_edit(XMLDoc.getElementsByTagName("subject")[0].childNodes[0].nodeValue);
  38. if (typeof window.ajax_indicator == "function")
  39. ajax_indicator(false);
  40. }
  41. function modify_topic_cancel()
  42. {
  43. setInnerHTML(cur_subject_div, buff_subject);
  44. set_hidden_topic_areas('');
  45. in_edit_mode = 0;
  46. return false;
  47. }
  48. function modify_topic_save(cur_session_id, cur_session_var)
  49. {
  50. if (!in_edit_mode)
  51. return true;
  52. // Add backwards compatibility with old themes.
  53. if (typeof(cur_session_var) == 'undefined')
  54. cur_session_var = 'sesc';
  55. var i, x = new Array();
  56. x[x.length] = 'subject=' + document.forms.quickModForm['subject'].value.replace(/&#/g, "&#").php_to8bit().php_urlencode();
  57. x[x.length] = 'topic=' + parseInt(document.forms.quickModForm.elements['topic'].value);
  58. x[x.length] = 'msg=' + parseInt(document.forms.quickModForm.elements['msg'].value);
  59. if (typeof window.ajax_indicator == "function")
  60. ajax_indicator(true);
  61. 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);
  62. return false;
  63. }
  64. function modify_topic_done(XMLDoc)
  65. {
  66. if (!XMLDoc)
  67. {
  68. modify_topic_cancel();
  69. return true;
  70. }
  71. var message = XMLDoc.getElementsByTagName("smf")[0].getElementsByTagName("message")[0];
  72. var subject = message.getElementsByTagName("subject")[0];
  73. var error = message.getElementsByTagName("error")[0];
  74. if (typeof window.ajax_indicator == "function")
  75. ajax_indicator(false);
  76. if (!subject || error)
  77. return false;
  78. subjectText = subject.childNodes[0].nodeValue;
  79. modify_topic_hide_edit(subjectText);
  80. set_hidden_topic_areas('');
  81. in_edit_mode = 0;
  82. return false;
  83. }
  84. // Simply restore any hidden bits during topic editing.
  85. function set_hidden_topic_areas(set_style)
  86. {
  87. for (var i = 0; i < hide_prefixes.length; i++)
  88. {
  89. if (document.getElementById(hide_prefixes[i] + cur_msg_id.substr(4)) != null)
  90. document.getElementById(hide_prefixes[i] + cur_msg_id.substr(4)).style.display = set_style;
  91. }
  92. }
  93. // *** QuickReply object.
  94. function QuickReply(oOptions)
  95. {
  96. this.opt = oOptions;
  97. this.bCollapsed = this.opt.bDefaultCollapsed;
  98. }
  99. // When a user presses quote, put it in the quick reply box (if expanded).
  100. QuickReply.prototype.quote = function (iMessageId, xDeprecated)
  101. {
  102. // Compatibility with older templates.
  103. if (typeof(xDeprecated) != 'undefined')
  104. return true;
  105. if (this.bCollapsed)
  106. {
  107. window.location.href = smf_prepareScriptUrl(this.opt.sScriptUrl) + 'action=post;quote=' + iMessageId + ';topic=' + this.opt.iTopicId + '.' + this.opt.iStart;
  108. return false;
  109. }
  110. else
  111. {
  112. // Doing it the XMLhttp way?
  113. if (window.XMLHttpRequest)
  114. {
  115. ajax_indicator(true);
  116. getXMLDocument(smf_prepareScriptUrl(this.opt.sScriptUrl) + 'action=quotefast;quote=' + iMessageId + ';xml', this.onQuoteReceived);
  117. }
  118. // Or with a smart popup!
  119. else
  120. reqWin(smf_prepareScriptUrl(this.opt.sScriptUrl) + 'action=quotefast;quote=' + iMessageId, 240, 90);
  121. // Move the view to the quick reply box.
  122. if (navigator.appName == 'Microsoft Internet Explorer')
  123. window.location.hash = this.opt.sJumpAnchor;
  124. else
  125. window.location.hash = '#' + this.opt.sJumpAnchor;
  126. return false;
  127. }
  128. }
  129. // This is the callback function used after the XMLhttp request.
  130. QuickReply.prototype.onQuoteReceived = function (oXMLDoc)
  131. {
  132. var sQuoteText = '';
  133. for (var i = 0; i < oXMLDoc.getElementsByTagName('quote')[0].childNodes.length; i++)
  134. sQuoteText += oXMLDoc.getElementsByTagName('quote')[0].childNodes[i].nodeValue;
  135. replaceText(sQuoteText, document.forms.postmodify.message);
  136. ajax_indicator(false);
  137. }
  138. // The function handling the swapping of the quick reply.
  139. QuickReply.prototype.swap = function ()
  140. {
  141. document.getElementById(this.opt.sImageId).src = this.opt.sImagesUrl + "/" + (this.bCollapsed ? this.opt.sImageCollapsed : this.opt.sImageExpanded);
  142. document.getElementById(this.opt.sContainerId).style.display = this.bCollapsed ? '' : 'none';
  143. this.bCollapsed = !this.bCollapsed;
  144. }
  145. // *** QuickModify object.
  146. function QuickModify(oOptions)
  147. {
  148. this.opt = oOptions;
  149. this.bInEditMode = false;
  150. this.sCurMessageId = '';
  151. this.oCurMessageDiv = null;
  152. this.oCurSubjectDiv = null;
  153. this.sMessageBuffer = '';
  154. this.sSubjectBuffer = '';
  155. this.bXmlHttpCapable = this.isXmlHttpCapable();
  156. // Show the edit buttons
  157. if (this.bXmlHttpCapable)
  158. {
  159. for (var i = document.images.length - 1; i >= 0; i--)
  160. if (document.images[i].id.substr(0, 14) == 'modify_button_')
  161. document.images[i].style.display = '';
  162. }
  163. }
  164. // Determine whether the quick modify can actually be used.
  165. QuickModify.prototype.isXmlHttpCapable = function ()
  166. {
  167. if (typeof(window.XMLHttpRequest) == 'undefined')
  168. return false;
  169. // Opera didn't always support POST requests. So test it first.
  170. if ('opera' in window)
  171. {
  172. var oTest = new XMLHttpRequest();
  173. if (!('setRequestHeader' in oTest))
  174. return false;
  175. }
  176. return true;
  177. }
  178. // Function called when a user presses the edit button.
  179. QuickModify.prototype.modifyMsg = function (iMessageId)
  180. {
  181. if (!this.bXmlHttpCapable)
  182. return;
  183. // Add backwards compatibility with old themes.
  184. if (typeof(sSessionVar) == 'undefined')
  185. sSessionVar = 'sesc';
  186. // First cancel if there's another message still being edited.
  187. if (this.bInEditMode)
  188. this.modifyCancel();
  189. // At least NOW we're in edit mode
  190. this.bInEditMode = true;
  191. // Send out the XMLhttp request to get more info
  192. ajax_indicator(true);
  193. // For IE 5.0 support, 'call' is not yet used.
  194. this.tmpMethod = getXMLDocument;
  195. this.tmpMethod(smf_prepareScriptUrl(this.opt.sScriptUrl) + 'action=quotefast;quote=' + iMessageId + ';modify;xml', this.onMessageReceived);
  196. delete this.tmpMethod;
  197. }
  198. // The callback function used for the XMLhttp request retrieving the message.
  199. QuickModify.prototype.onMessageReceived = function (XMLDoc)
  200. {
  201. var sBodyText = '', sSubjectText = '';
  202. // No longer show the 'loading...' sign.
  203. ajax_indicator(false);
  204. // Grab the message ID.
  205. this.sCurMessageId = XMLDoc.getElementsByTagName('message')[0].getAttribute('id');
  206. // If this is not valid then simply give up.
  207. if (!document.getElementById(this.sCurMessageId))
  208. return this.modifyCancel();
  209. // Replace the body part.
  210. for (var i = 0; i < XMLDoc.getElementsByTagName("message")[0].childNodes.length; i++)
  211. sBodyText += XMLDoc.getElementsByTagName("message")[0].childNodes[i].nodeValue;
  212. this.oCurMessageDiv = document.getElementById(this.sCurMessageId);
  213. this.sMessageBuffer = getInnerHTML(this.oCurMessageDiv);
  214. // We have to force the body to lose its dollar signs thanks to IE.
  215. sBodyText = sBodyText.replace(/\$/g, '{&dollarfix;$}');
  216. // Actually create the content, with a bodge for disappearing dollar signs.
  217. setInnerHTML(this.oCurMessageDiv, this.opt.sTemplateBodyEdit.replace(/%msg_id%/g, this.sCurMessageId.substr(4)).replace(/%body%/, sBodyText).replace(/\{&dollarfix;\$\}/g, '$'));
  218. // Replace the subject part.
  219. this.oCurSubjectDiv = document.getElementById('subject_' + this.sCurMessageId.substr(4));
  220. this.sSubjectBuffer = getInnerHTML(this.oCurSubjectDiv);
  221. sSubjectText = XMLDoc.getElementsByTagName('subject')[0].childNodes[0].nodeValue.replace(/\$/g, '{&dollarfix;$}');
  222. setInnerHTML(this.oCurSubjectDiv, this.opt.sTemplateSubjectEdit.replace(/%subject%/, sSubjectText).replace(/\{&dollarfix;\$\}/g, '$'));
  223. return true;
  224. }
  225. // Function in case the user presses cancel (or other circumstances cause it).
  226. QuickModify.prototype.modifyCancel = function ()
  227. {
  228. // Roll back the HTML to its original state.
  229. if (this.oCurMessageDiv)
  230. {
  231. setInnerHTML(this.oCurMessageDiv, this.sMessageBuffer);
  232. setInnerHTML(this.oCurSubjectDiv, this.sSubjectBuffer);
  233. }
  234. // No longer in edit mode, that's right.
  235. this.bInEditMode = false;
  236. return false;
  237. }
  238. // The function called after a user wants to save his precious message.
  239. QuickModify.prototype.modifySave = function (sSessionId, sSessionVar)
  240. {
  241. // We cannot save if we weren't in edit mode.
  242. if (!this.bInEditMode)
  243. return true;
  244. // Add backwards compatibility with old themes.
  245. if (typeof(sSessionVar) == 'undefined')
  246. sSessionVar = 'sesc';
  247. var i, x = new Array();
  248. x[x.length] = 'subject=' + escape(document.forms.quickModForm['subject'].value.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B");
  249. x[x.length] = 'message=' + escape(document.forms.quickModForm['message'].value.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B");
  250. x[x.length] = 'topic=' + parseInt(document.forms.quickModForm.elements['topic'].value);
  251. x[x.length] = 'msg=' + parseInt(document.forms.quickModForm.elements['msg'].value);
  252. // Send in the XMLhttp request and let's hope for the best.
  253. ajax_indicator(true);
  254. sendXMLDocument.call(this, smf_prepareScriptUrl(this.opt.sScriptUrl) + "action=jsmodify;topic=" + this.opt.iTopicId + ";" + sSessionVar + "=" + sSessionId + ";xml", x.join("&"), this.onModifyDone);
  255. return false;
  256. }
  257. // Callback function of the XMLhttp request sending the modified message.
  258. QuickModify.prototype.onModifyDone = function (XMLDoc)
  259. {
  260. // We've finished the loading stuff.
  261. ajax_indicator(false);
  262. // If we didn't get a valid document, just cancel.
  263. if (!XMLDoc || !XMLDoc.getElementsByTagName('smf')[0])
  264. {
  265. // Mozilla will nicely tell us what's wrong.
  266. if (XMLDoc.childNodes.length > 0 && XMLDoc.firstChild.nodeName == 'parsererror')
  267. setInnerHTML(document.getElementById('error_box'), XMLDoc.firstChild.textContent);
  268. else
  269. this.modifyCancel();
  270. return;
  271. }
  272. var message = XMLDoc.getElementsByTagName('smf')[0].getElementsByTagName('message')[0];
  273. var body = message.getElementsByTagName('body')[0];
  274. var error = message.getElementsByTagName('error')[0];
  275. if (body)
  276. {
  277. // Show new body.
  278. var bodyText = '';
  279. for (var i = 0; i < body.childNodes.length; i++)
  280. bodyText += body.childNodes[i].nodeValue;
  281. this.sMessageBuffer = this.opt.sTemplateBodyNormal.replace(/%body%/, bodyText.replace(/\$/g, '{&dollarfix;$}')).replace(/\{&dollarfix;\$\}/g,'$');
  282. setInnerHTML(this.oCurMessageDiv, this.sMessageBuffer);
  283. // Show new subject.
  284. var oSubject = message.getElementsByTagName('subject')[0];
  285. var sSubjectText = oSubject.childNodes[0].nodeValue.replace(/\$/g, '{&dollarfix;$}');
  286. this.sSubjectBuffer = this.opt.sTemplateSubjectNormal.replace(/%msg_id%/g, this.sCurMessageId.substr(4)).replace(/%subject%/, sSubjectText).replace(/\{&dollarfix;\$\}/g,'$');
  287. setInnerHTML(this.oCurSubjectDiv, this.sSubjectBuffer);
  288. // If this is the first message, also update the topic subject.
  289. if (oSubject.getAttribute('is_first') == '1')
  290. setInnerHTML(document.getElementById('top_subject'), this.opt.sTemplateTopSubject.replace(/%subject%/, sSubjectText).replace(/\{&dollarfix;\$\}/g, '$'));
  291. // Show this message as 'modified on x by y'.
  292. if (this.opt.bShowModify)
  293. setInnerHTML(document.getElementById('modified_' + this.sCurMessageId.substr(4)), message.getElementsByTagName('modified')[0].childNodes[0].nodeValue);
  294. }
  295. else if (error)
  296. {
  297. setInnerHTML(document.getElementById('error_box'), error.childNodes[0].nodeValue);
  298. document.forms.quickModForm.message.style.border = error.getAttribute('in_body') == '1' ? this.opt.sErrorBorderStyle : '';
  299. document.forms.quickModForm.subject.style.border = error.getAttribute('in_subject') == '1' ? this.opt.sErrorBorderStyle : '';
  300. }
  301. }
  302. function InTopicModeration(oOptions)
  303. {
  304. this.opt = oOptions;
  305. this.bButtonsShown = false;
  306. this.iNumSelected = 0;
  307. // Add backwards compatibility with old themes.
  308. if (typeof(this.opt.sSessionVar) == 'undefined')
  309. this.opt.sSessionVar = 'sesc';
  310. this.init();
  311. }
  312. InTopicModeration.prototype.init = function()
  313. {
  314. // Add checkboxes to all the messages.
  315. for (var i = 0, n = this.opt.aMessageIds.length; i < n; i++)
  316. {
  317. // Create the checkbox.
  318. var oCheckbox = document.createElement('input');
  319. oCheckbox.type = 'checkbox';
  320. oCheckbox.className = 'input_check';
  321. oCheckbox.name = 'msgs[]';
  322. oCheckbox.value = this.opt.aMessageIds[i];
  323. oCheckbox.instanceRef = this;
  324. oCheckbox.onclick = function () {
  325. this.instanceRef.handleClick(this);
  326. }
  327. // Append it to the container
  328. var oCheckboxContainer = document.getElementById(this.opt.sCheckboxContainerMask + this.opt.aMessageIds[i]);
  329. oCheckboxContainer.appendChild(oCheckbox);
  330. oCheckboxContainer.style.display = '';
  331. }
  332. }
  333. InTopicModeration.prototype.handleClick = function(oCheckbox)
  334. {
  335. if (!this.bButtonsShown && this.opt.sButtonStripDisplay)
  336. {
  337. var oButtonStrip = document.getElementById(this.opt.sButtonStrip);
  338. var oButtonStripDisplay = document.getElementById(this.opt.sButtonStripDisplay);
  339. // Make sure it can go somewhere.
  340. if (typeof(oButtonStripDisplay) == 'object' && oButtonStripDisplay != null)
  341. oButtonStripDisplay.style.display = "";
  342. else
  343. {
  344. var oNewDiv = document.createElement('div');
  345. var oNewList = document.createElement('ul');
  346. oNewDiv.id = this.opt.sButtonStripDisplay;
  347. oNewDiv.className = this.opt.sButtonStripClass ? this.opt.sButtonStripClass : 'buttonlist floatbottom';
  348. oNewDiv.appendChild(oNewList);
  349. oButtonStrip.appendChild(oNewDiv);
  350. }
  351. // Add the 'remove selected items' button.
  352. if (this.opt.bCanRemove)
  353. smf_addButton(this.opt.sButtonStrip, this.opt.bUseImageButton, {
  354. sId: this.opt.sSelf + '_remove_button',
  355. sText: this.opt.sRemoveButtonLabel,
  356. sImage: this.opt.sRemoveButtonImage,
  357. sUrl: '#',
  358. sCustom: ' onclick="return ' + this.opt.sSelf + '.handleSubmit(\'remove\')"'
  359. });
  360. // Add the 'restore selected items' button.
  361. if (this.opt.bCanRestore)
  362. smf_addButton(this.opt.sButtonStrip, this.opt.bUseImageButton, {
  363. sId: this.opt.sSelf + '_restore_button',
  364. sText: this.opt.sRestoreButtonLabel,
  365. sImage: this.opt.sRestoreButtonImage,
  366. sUrl: '#',
  367. sCustom: ' onclick="return ' + this.opt.sSelf + '.handleSubmit(\'restore\')"'
  368. });
  369. // Adding these buttons once should be enough.
  370. this.bButtonsShown = true;
  371. }
  372. // Keep stats on how many items were selected.
  373. this.iNumSelected += oCheckbox.checked ? 1 : -1;
  374. // Show the number of messages selected in the button.
  375. if (this.opt.bCanRemove && !this.opt.bUseImageButton)
  376. {
  377. setInnerHTML(document.getElementById(this.opt.sSelf + '_remove_button'), this.opt.sRemoveButtonLabel + ' [' + this.iNumSelected + ']');
  378. document.getElementById(this.opt.sSelf + '_remove_button').style.display = this.iNumSelected < 1 ? "none" : "";
  379. }
  380. if (this.opt.bCanRestore && !this.opt.bUseImageButton)
  381. {
  382. setInnerHTML(document.getElementById(this.opt.sSelf + '_restore_button'), this.opt.sRestoreButtonLabel + ' [' + this.iNumSelected + ']');
  383. document.getElementById(this.opt.sSelf + '_restore_button').style.display = this.iNumSelected < 1 ? "none" : "";
  384. }
  385. // Try to restore the correct position.
  386. var aItems = document.getElementById(this.opt.sButtonStrip).getElementsByTagName('span');
  387. if (aItems.length > 3)
  388. {
  389. if (this.iNumSelected < 1)
  390. {
  391. aItems[aItems.length - 3].className = aItems[aItems.length - 3].className.replace(/\s*position_holder/, 'last');
  392. aItems[aItems.length - 2].className = aItems[aItems.length - 2].className.replace(/\s*position_holder/, 'last');
  393. }
  394. else
  395. {
  396. aItems[aItems.length - 2].className = aItems[aItems.length - 2].className.replace(/\s*last/, 'position_holder');
  397. aItems[aItems.length - 3].className = aItems[aItems.length - 3].className.replace(/\s*last/, 'position_holder');
  398. }
  399. }
  400. }
  401. InTopicModeration.prototype.handleSubmit = function (sSubmitType)
  402. {
  403. var oForm = document.getElementById(this.opt.sFormId);
  404. // Make sure this form isn't submitted in another way than this function.
  405. var oInput = document.createElement('input');
  406. oInput.type = 'hidden';
  407. oInput.name = this.opt.sSessionVar;
  408. oInput.value = this.opt.sSessionId;
  409. oForm.appendChild(oInput);
  410. switch (sSubmitType)
  411. {
  412. case 'remove':
  413. if (!confirm(this.opt.sRemoveButtonConfirm))
  414. return false;
  415. oForm.action = oForm.action.replace(/;restore_selected=1/, '');
  416. break;
  417. case 'restore':
  418. if (!confirm(this.opt.sRestoreButtonConfirm))
  419. return false;
  420. oForm.action = oForm.action + ';restore_selected=1';
  421. break;
  422. default:
  423. return false;
  424. break;
  425. }
  426. oForm.submit();
  427. return true;
  428. }
  429. // *** Other functions...
  430. function expandThumb(thumbID)
  431. {
  432. var img = document.getElementById('thumb_' + thumbID);
  433. var link = document.getElementById('link_' + thumbID);
  434. var tmp = img.src;
  435. img.src = link.href;
  436. link.href = tmp;
  437. img.style.width = '';
  438. img.style.height = '';
  439. return false;
  440. }