Use MD5 hashes to determine when to auto-save
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 4 Feb 2010 00:38:21 +0000 (17:38 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 4 Feb 2010 00:39:10 +0000 (17:39 -0700)
imp/compose-dimp.php
imp/compose.php
imp/js/compose-dimp.js
imp/js/compose.js
imp/templates/javascript_defs_dimp.php

index 105e756..4618855 100644 (file)
@@ -197,7 +197,8 @@ $compose_result['js_onload'][] = 'DimpCompose.fillForm(' . Horde_Serialize::seri
 Horde::addInlineScript($compose_result['js_onload'], 'load');
 
 $scripts = array(
-    array('compose-dimp.js', 'imp')
+    array('compose-dimp.js', 'imp'),
+    array('md5.js', 'horde')
 );
 
 IMP_Dimp::header(_("Message Composition"), $scripts);
index 73db777..78a2e53 100644 (file)
@@ -1104,6 +1104,7 @@ if ($showmenu) {
     IMP::prepareMenu();
 }
 Horde::addScriptFile('compose.js', 'imp');
+Horde::addScriptFile('md5.js', 'horde');
 require IMP_TEMPLATES . '/common-header.inc';
 Horde::addInlineScript($js_code);
 if ($showmenu) {
index 592214a..c7b2765 100644 (file)
@@ -10,9 +10,9 @@
 var DimpCompose = {
     // Variables defaulting to empty/false:
     //   auto_save_interval, compose_cursor, disabled, drafts_mbox, editor_on,
-    //   is_popup, knl_p, knl_sm, mp_padding, resizebcc, resizecc, resizeto,
-    //   row_height, rte, skip_spellcheck, spellcheck, sc_submit, uploading
-    last_msg: '',
+    //   is_popup, knl_p, knl_sm, last_msg, mp_padding, resizebcc, resizecc,
+    //   resizeto, row_height, rte, skip_spellcheck, spellcheck, sc_submit,
+    //   uploading
 
     confirmCancel: function()
     {
@@ -472,34 +472,25 @@ var DimpCompose = {
             identity = this.getIdentity($F('last_identity')),
             msgval = $('composeMessage');
 
-        if (!this.last_msg.empty() &&
-            this.last_msg != $F(msgval).replace(/\r/g, '') &&
-            !window.confirm(DIMP.text_compose.fillform)) {
-            return;
-        }
-
         // Set auto-save-drafts now if not already active.
         if (DIMP.conf_compose.auto_save_interval_val &&
             !this.auto_save_interval) {
             this.auto_save_interval = new PeriodicalExecuter(function() {
-                var cur_msg = this.editor_on
-                    ? this.rte.getData()
-                    : $F(msgval);
-                cur_msg = cur_msg.replace(/\r/g, '');
-                if (!cur_msg.empty() && this.last_msg != cur_msg) {
+                var curr_hash = MD5.hash($('to', 'cc', 'bcc', 'subject').invoke('getValue').join('\0') + (this.editor_on ? this.rte.getData() : $F(msgval)));
+                if (this.last_msg && curr_hash != this.last_msg) {
                     this.uniqueSubmit('AutoSaveDraft');
-                    this.last_msg = cur_msg;
                 }
+                this.last_msg = curr_hash;
             }.bind(this), DIMP.conf_compose.auto_save_interval_val * 60);
+            /* Immediately execute to get MD5 hash of empty message. */
+            this.auto_save_interval.execute();
         }
 
         if (this.editor_on) {
             this.rte.setData(msg);
-            this.last_msg = this.rte.getData().replace(/\r/g, '');
         } else {
             msgval.setValue(msg);
             this.setCursorPosition(msgval);
-            this.last_msg = $F(msgval).replace(/\r/g, '');
         }
 
         $('to').setValue(header.to);
index 47a8020..e8c081a 100644 (file)
@@ -202,14 +202,13 @@ var ImpCompose = {
                 CKEDITOR.instances.composeMessage.updateElement();
             }
 
-            cur_msg = $F('composeMessage').replace(/\r/g, '');
-            if (!cur_msg.empty() && this.last_msg != cur_msg) {
+            cur_msg = MD5.hash($('to', 'cc', 'bcc', 'subject').compact().invoke('getValue').join('\0') + $F('composeMessage'));
+            if (this.last_msg && curr_hash != this.last_msg) {
                 // Use an AJAX submit here so that the page doesn't reload.
                 $('actionID').setValue(actionID);
                 $('compose').request({ onComplete: this._autoSaveDraft.bind(this) });
-
-                this.last_msg = cur_msg;
             }
+            this.last_msg = cur_msg;
             return;
 
         case 'toggle_editor':
@@ -385,7 +384,8 @@ var ImpCompose = {
         document.observe('SpellChecker:noerror', this._onNoErrorSpellCheck.bind(this));
 
         if (this.auto_save) {
-            new PeriodicalExecuter(this.uniqSubmit.bind(this, 'auto_save_draft'), this.auto_save * 60);
+            /* Immediately execute to get MD5 hash of empty message. */
+            new PeriodicalExecuter(this.uniqSubmit.bind(this, 'auto_save_draft'), this.auto_save * 60).execute();
         }
 
         this.resize.bind(this).delay(0.25);
index 970c793..ae9e6aa 100644 (file)
@@ -169,7 +169,6 @@ if ($compose_mode) {
     $code['text_compose'] = array(
         'atc_limit' => _("The attachment limit has been reached."),
         'cancel' => _("Cancelling this message will permanently discard its contents and will delete auto-saved drafts.\nAre you sure you want to do this?"),
-        'fillform' => _("You have already changed the message body, are you sure you want to drop the changes?"),
         'nosubject' => _("The message does not have a Subject entered.") . "\n" . _("Send message without a Subject?"),
         'remove' => _("Remove"),
         'spell_noerror' => _("No spelling errors found."),