drafts.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. addLoadEvent(this.opt.sSelf + '.init();');
  13. }
  14. // Start our self calling routine
  15. smf_DraftAutoSave.prototype.init = function ()
  16. {
  17. if (this.opt.iFreq > 0)
  18. {
  19. // start the autosave timer
  20. this.interval_id = setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);
  21. // Set up window focus and blur events
  22. this.oDraftHandle.instanceRef = this;
  23. this.oDraftHandle.onblur = function (oEvent) {return this.instanceRef.draftBlur(oEvent);};
  24. this.oDraftHandle.onfocus = function (oEvent) {return this.instanceRef.draftFocus(oEvent);};
  25. }
  26. }
  27. // Moved away from the page, where did you go? ... till you return we pause autosaving
  28. smf_DraftAutoSave.prototype.draftBlur = function(oEvent)
  29. {
  30. // save what we have and turn of the autosave
  31. if (this.bPM)
  32. this.draftPMSave();
  33. else
  34. this.draftSave();
  35. clearInterval(this.interval_id);
  36. this.interval_id = 0;
  37. }
  38. // Since your back we resume the autosave timer
  39. smf_DraftAutoSave.prototype.draftFocus = function(oEvent)
  40. {
  41. this.interval_id = setInterval(this.opt.sSelf + '.draft' + (this.bPM ? 'PM' : '') + 'Save();', this.opt.iFreq);
  42. }
  43. // Make the call to save this draft in the background
  44. smf_DraftAutoSave.prototype.draftSave = function ()
  45. {
  46. // nothing to save or already posting?
  47. if (isEmptyText($('#' + this.opt.sSceditorID).data("sceditor").getText()) || smf_formSubmitted)
  48. return false;
  49. // Still saving the last one or other?
  50. if (this.bInDraftMode)
  51. this.draftCancel();
  52. // Flag that we are saving a draft
  53. document.getElementById('throbber').style.display = '';
  54. this.bInDraftMode = true;
  55. // Get the form elements that we want to save
  56. var aSections = [
  57. 'topic=' + parseInt(document.forms.postmodify.elements['topic'].value),
  58. 'id_draft=' + parseInt(document.forms.postmodify.elements['id_draft'].value),
  59. 'subject=' + escape(document.forms.postmodify['subject'].value.replace(/&#/g, "&#").php_to8bit()).replace(/\+/g, "%2B"),
  60. 'message=' + escape($('#' + this.opt.sSceditorID).data("sceditor").getText().replace(/&#/g, "&#").php_to8bit()).replace(/\+/g, "%2B"),
  61. 'icon=' + escape(document.forms.postmodify['icon'].value.replace(/&#/g, "&#").php_to8bit()).replace(/\+/g, "%2B"),
  62. 'save_draft=true',
  63. smf_session_var + '=' + smf_session_id,
  64. ];
  65. // Get the locked an/or sticky values if they have been selected or set that is
  66. if (this.opt.sType == 'post')
  67. {
  68. if (document.getElementById('check_lock').checked)
  69. aSections[aSections.length] = 'lock=1';
  70. if (document.getElementById('check_sticky').checked)
  71. aSections[aSections.length] = 'sticky=1';
  72. }
  73. // keep track of source or wysiwyg
  74. aSections[aSections.length] = 'message_mode=' + $("#message").data("sceditor").inSourceMode();
  75. // Send in document for saving and hope for the best
  76. sendXMLDocument.call(this, smf_prepareScriptUrl(smf_scripturl) + "action=post2;board=" + this.opt.iBoard + ";xml", aSections.join("&"), this.onDraftDone);
  77. }
  78. // Make the call to save this PM draft in the background
  79. smf_DraftAutoSave.prototype.draftPMSave = function ()
  80. {
  81. // nothing to save?
  82. if (isEmptyText(document.forms.postmodify['message']))
  83. return false;
  84. // Still saving the last one or some other?
  85. if (this.bInDraftMode)
  86. this.draftCancel();
  87. // Flag that we are saving
  88. document.getElementById('throbber').style.display = '';
  89. this.bInDraftMode = true;
  90. // Get the to and bcc values
  91. var aTo = this.draftGetRecipient('recipient_to[]');
  92. var aBcc = this.draftGetRecipient('recipient_bcc[]');
  93. // Get the rest of the form elements that we want to save, and load them up
  94. var aSections = [
  95. 'replied_to=' + parseInt(document.forms.postmodify.elements['replied_to'].value),
  96. 'id_pm_draft=' + parseInt(document.forms.postmodify.elements['id_pm_draft'].value),
  97. 'subject=' + escape(document.forms.postmodify['subject'].value.replace(/&#/g, "&#").php_to8bit()).replace(/\+/g, "%2B"),
  98. 'message=' + escape(document.forms.postmodify['message'].value.replace(/&#/g, "&#").php_to8bit()).replace(/\+/g, "%2B"),
  99. 'recipient_to=' + aTo,
  100. 'recipient_bcc=' + aBcc,
  101. 'save_draft=true',
  102. smf_session_var + '=' + smf_session_id,
  103. ];
  104. // Saving a copy in the outbox?
  105. if (document.getElementById('outbox'))
  106. aSections[aSections.length] = 'outbox=' + parseInt(document.getElementById('outbox').value);
  107. // account for wysiwyg
  108. if (this.opt.sType == 'post')
  109. aSections[aSections.length] = 'message_mode=' + parseInt(document.forms.postmodify.elements['message_mode'].value);
  110. // Send in (post) the document for saving
  111. sendXMLDocument.call(this, smf_prepareScriptUrl(smf_scripturl) + "action=pm;sa=send2;xml", aSections.join("&"), this.onDraftDone);
  112. }
  113. // Callback function of the XMLhttp request for saving the draft message
  114. smf_DraftAutoSave.prototype.onDraftDone = function (XMLDoc)
  115. {
  116. // If it is not valid then clean up
  117. if (!XMLDoc || !XMLDoc.getElementsByTagName('draft'))
  118. return this.draftCancel();
  119. // Grab the returned draft id and saved time from the response
  120. this.sCurDraftId = XMLDoc.getElementsByTagName('draft')[0].getAttribute('id');
  121. this.sLastSaved = XMLDoc.getElementsByTagName('draft')[0].childNodes[0].nodeValue;
  122. // Update the form to show we finished, if the id is not set, then set it
  123. document.getElementById(this.opt.sLastID).value = this.sCurDraftId;
  124. oCurDraftDiv = document.getElementById(this.opt.sLastNote);
  125. setInnerHTML(oCurDraftDiv, this.sLastSaved);
  126. // hide the saved draft infobox in the event they pressed the save draft button at some point
  127. if (this.opt.sType == 'post')
  128. document.getElementById('draft_section').style.display = 'none';
  129. // thank you sir, may I have another
  130. this.bInDraftMode = false;
  131. document.getElementById('throbber').style.display = 'none';
  132. }
  133. // function to retrieve the to and bcc values from the pseudo arrays
  134. smf_DraftAutoSave.prototype.draftGetRecipient = function (sField)
  135. {
  136. var oRecipient = document.forms.postmodify.elements[sField];
  137. var aRecipient = []
  138. if (typeof(oRecipient) != 'undefined')
  139. {
  140. // just one recipient
  141. if ('value' in oRecipient)
  142. aRecipient.push(parseInt(oRecipient.value));
  143. else
  144. {
  145. // or many !
  146. for (var i = 0, n = oRecipient.length; i < n; i++)
  147. aRecipient.push(parseInt(oRecipient[i].value));
  148. }
  149. }
  150. return aRecipient;
  151. }
  152. // If another auto save came in with one still pending
  153. smf_DraftAutoSave.prototype.draftCancel = function ()
  154. {
  155. // can we do anything at all ... do we want to (e.g. sequence our async events?)
  156. // @todo if not remove this function
  157. this.bInDraftMode = false;
  158. document.getElementById('throbber').style.display = 'none';
  159. }