Use custom events to trigger successful dialog actions
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 16 Dec 2009 20:04:54 +0000 (13:04 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 18 Dec 2009 20:04:39 +0000 (13:04 -0700)
imp/compose.php
imp/js/DimpCore.js
imp/js/compose.js
imp/js/dialog.js
imp/lib/IMP.php
imp/lib/Mime/Viewer/Pgp.php
imp/lib/Mime/Viewer/Smime.php

index 9b0f9f5..7e07d7b 100644 (file)
@@ -688,16 +688,16 @@ if ($get_sig && isset($msg) && !empty($sig)) {
 if ($pgp_passphrase_dialog || $pgp_symmetric_passphrase_dialog) {
     if ($pgp_passphrase_dialog) {
         Horde::addInlineScript(array(
-           IMP::passphraseDialogJS('PGPPersonal', 'ImpCompose.uniqSubmit(\'send_message\')')
+           IMP::passphraseDialogJS('PGPPersonal')
        ), 'dom');
     } else {
         Horde::addInlineScript(array(
-            IMP::passphraseDialogJS('PGPSymmetric', 'ImpCompose.uniqSubmit(\'send_message\')', array('symmetricid' => 'imp_compose_' . $composeCacheID))
+            IMP::passphraseDialogJS('PGPSymmetric', array('symmetricid' => 'imp_compose_' . $composeCacheID))
        ), 'dom');
     }
 } elseif ($smime_passphrase_dialog) {
     Horde::addInlineScript(array(
-        IMP::passphraseDialogJS('SMIMEPersonal', 'ImpCompose.uniqSubmit(\'send_message\')')
+        IMP::passphraseDialogJS('SMIMEPersonal')
     ), 'dom');
 }
 
index 5a8a3bf..1973c93 100644 (file)
@@ -504,6 +504,18 @@ var DimpCore = {
         /* Add click handler. */
         document.observe('click', DimpCore.clickHandler.bindAsEventListener(DimpCore));
 
+        /* Catch dialog actions. */
+        document.observe('IMPDialog:success', function(e) {
+            switch (e.memo) {
+            case 'PGPPersonal':
+            case 'PGPSymmetric':
+            case 'SMIMEPersonal':
+                IMPDialog.noreload = true;
+                this.reloadMessage({});
+                break;
+            }
+        }.bindAsEventListener(this));
+
         /* Determine base window. Need a try/catch block here since, if the
          * page was loaded by an opener out of this current domain, this will
          * throw an exception. */
index 3b47835..312c25e 100644 (file)
@@ -447,3 +447,15 @@ document.observe('dom:loaded', ImpCompose.onDomLoad.bind(ImpCompose));
 
 /* Warn before closing the window. */
 Event.observe(window, 'beforeunload', ImpCompose.onBeforeUnload.bind(ImpCompose));
+
+/* Catch dialog actions. */
+document.observe('IMPDialog:success', function(e) {
+    switch (e.memo) {
+    case 'PGPPersonal':
+    case 'PGPSymmetric':
+    case 'SMIMEPersonal':
+        IMPDialog.noreload = true;
+        ImpCompose.uniqSubmit('send_message');
+        break;
+    }
+});
index bbf5c80..5b9ebbc 100644 (file)
@@ -13,6 +13,8 @@ var IMPDialog = {
 
     display: function(data)
     {
+        this.noreload = false;
+
         if (Object.isString(data)) {
             data = decodeURIComponent(data).evalJSON(true);
         }
@@ -30,8 +32,8 @@ var IMPDialog = {
 
     _display: function(data)
     {
-        this.action = data.action;
         this.params = data.params;
+        this.type = data.type;
         this.uri = data.uri;
 
         var n = new Element('FORM', { action: '#', id: 'RB_confirm' }).insert(
@@ -80,13 +82,8 @@ var IMPDialog = {
 
         if (r.response.success) {
             this._close();
-            if (this.action) {
-                if (Object.isFunction(this.action)) {
-                    this.action();
-                } else {
-                    eval(this.action)();
-                }
-            } else {
+            document.fire('IMPDialog:success', this.type);
+            if (!this.noreload) {
                 location.reload();
             }
         } else if (r.response.error) {
index edbb19e..dc84478 100644 (file)
@@ -1111,15 +1111,12 @@ class IMP
      * Generate the JS code necessary to open a passphrase dialog. Adds the
      * necessary JS files to open the dialog.
      *
-     * @param string $type    The dialog type.
-     * @param string $action  The JS code to run after success. Defaults to
-     *                        reloading the current window.
-     * @param array $params   Any additional parameters to pass.
+     * @param string $type   The dialog type.
+     * @param array $params  Any additional parameters to pass.
      *
      * @return string  The generated JS code.
      */
-    static public function passphraseDialogJS($type, $action = null,
-                                              $params = array())
+    static public function passphraseDialogJS($type, $params = array())
     {
         Horde::addScriptFile('effects.js', 'horde');
         Horde::addScriptFile('redbox.js', 'horde');
@@ -1145,13 +1142,13 @@ class IMP
         }
 
         $js_params = array(
-            'action' => $action,
-            'uri' => Horde::applicationUrl('ajax.php', true, -1) . '/' . $type,
+            'cancel_text' => _("Cancel"),
+            'ok_text' => _("OK"),
             'params' => $params,
-            'text' => $text,
             'password' => true,
-            'ok_text' => _("OK"),
-            'cancel_text' => _("Cancel")
+            'text' => $text,
+            'type' => $type,
+            'uri' => Horde::applicationUrl('ajax.php', true, -1) . '/' . $type
         );
 
         return 'IMPDialog.display(' . Horde::escapeJson($js_params, array('urlencode' => true)) . ')';
index c5b8581..58c7c81 100644 (file)
@@ -209,20 +209,12 @@ class IMP_Horde_Mime_Viewer_Pgp extends Horde_Mime_Viewer_Driver
                 $symmetric_pass = $this->_imppgp->getPassphrase('symmetric', $symmetric_id);
 
                 if (is_null($symmetric_pass)) {
-                    $js_action = '';
                     $status[] = _("The data in this part has been encrypted via PGP.");
 
-                    switch ($_SESSION['imp']['view']) {
-                    case 'dimp':
-                        $js_action = 'DimpCore.reloadMessage({});';
-                        // Fall through
+                    /* Ask for the correct passphrase if this is encrypted
+                     * symmetrically. */
+                    $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('PGPSymmetric', array('symmetricid' => $symmetric_id)) . ';return false;') . _("You must enter the passphrase used to encrypt this message to view it.") . '</a>';
 
-                    case 'imp':
-                        /* Ask for the correct passphrase if this is encrypted
-                         * symmetrically. */
-                        $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('PGPSymmetric', $js_action, array('symmetricid' => $symmetric_id)) . ';return false;') . _("You must enter the passphrase used to encrypt this message to view it.") . '</a>';
-                        break;
-                    }
                     return null;
                 }
             }
@@ -249,19 +241,9 @@ class IMP_Horde_Mime_Viewer_Pgp extends Horde_Mime_Viewer_Driver
                 if ($this->_imppgp->getPersonalPrivateKey()) {
                     $personal_pass = $this->_imppgp->getPassphrase('personal');
                     if (is_null($personal_pass)) {
-                        $js_action = '';
-
-                        switch ($_SESSION['imp']['view']) {
-                        case 'dimp':
-                            $js_action = 'DimpCore.reloadMessage({});';
-                            // Fall through
-
-                        case 'imp':
-                            /* Ask for the private key's passphrase if this is
-                             * encrypted asymmetrically. */
-                            $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('PGPPersonal', $js_action) . ';return false;') . _("You must enter the passphrase for your PGP private key to view this message.") . '</a>';
-                            break;
-                        }
+                        /* Ask for the private key's passphrase if this is
+                         * encrypted asymmetrically. */
+                        $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('PGPPersonal') . ';return false;') . _("You must enter the passphrase for your PGP private key to view this message.") . '</a>';
                         return null;
                     }
                 } else {
index 21ef7c2..771233f 100644 (file)
@@ -182,17 +182,7 @@ class IMP_Horde_Mime_Viewer_Smime extends Horde_Mime_Viewer_Driver
         /* Make sure we have a passphrase. */
         $passphrase = $this->_impsmime->getPassphrase();
         if (is_null($passphrase)) {
-            $js_action = '';
-
-            switch ($_SESSION['imp']['view']) {
-            case 'dimp':
-                $js_action = 'DimpCore.reloadMessage({});';
-                // Fall through
-
-            case 'imp':
-                $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('SMIMEPersonal', $js_action) . ';return false;') . _("You must enter the passphrase for your S/MIME private key to view this data.") . '</a>';
-                break;
-            }
+            $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('SMIMEPersonal') . ';return false;') . _("You must enter the passphrase for your S/MIME private key to view this data.") . '</a>';
             return null;
         }