Allow IMPDialog to handle simple Yes/No dialogs
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Jul 2010 03:44:37 +0000 (21:44 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 7 Jul 2010 04:11:24 +0000 (22:11 -0600)
Add a bit of IMPDialog documentation

imp/js/dialog.js
imp/js/mailbox.js
imp/mailbox.php

index de588a4..9f1ef79 100644 (file)
@@ -1,6 +1,22 @@
 /**
  * Javascript code used to display a RedBox dialog.
  *
+ * Parameters to display():
+ * 'cancel_text' - [REQUIRED] Cancel text.
+ * 'dialog_load' - TODO
+ * 'form' - TODO
+ * 'form_id' - The ID for the form (default: RB_confirm).
+ * 'noinput' - TODO
+ * 'ok_text' - OK text.
+ * 'password' - TODO
+ * 'text' - [REQUIRED] The text to display at top of dialog box.
+ *
+ * If these are set, an AJAX action (to 'uri') will be initiated if the OK
+ * button is pressed.
+ * 'params' - TODO
+ * 'type' - TODO
+ * 'uri' - TODO
+ *
  * Copyright 2008-2010 The Horde Project (http://www.horde.org/)
  *
  * See the enclosed file COPYING for license information (GPL). If you
@@ -16,6 +32,7 @@ var IMPDialog = {
         if (Object.isString(data)) {
             data = decodeURIComponent(data).evalJSON(true);
         }
+
         if (data.dialog_load) {
             new Ajax.Request(data.dialog_load, { onComplete: this._onComplete.bind(this) });
         } else {
@@ -30,17 +47,19 @@ var IMPDialog = {
 
     _display: function(data)
     {
-        this.params = data.params;
-        this.type = data.type;
-        this.uri = data.uri;
+        if (data.uri) {
+            this.params = data.params;
+            this.type = data.type;
+            this.uri = data.uri;
+        }
 
-        var n = new Element('FORM', { action: '#', id: 'RB_confirm' }).insert(
+        var n = new Element('FORM', { action: '#', id: data.form_id || 'RB_confirm' }).insert(
                     new Element('P').insert(data.text)
                 );
 
         if (data.form) {
             n.insert(data.form);
-        } else {
+        } else if (!data.noinput) {
             n.insert(new Element('INPUT', { name: 'dialog_input', type: data.password ? 'password' : 'text', size: 15 }));
         }
 
@@ -63,15 +82,24 @@ var IMPDialog = {
     {
         var c = RedBox.getWindowContents();
         [ c, c.descendants()].flatten().compact().invoke('stopObserving');
+        c.fire('IMPDialog:close');
         RedBox.close();
     },
 
     _onClick: function(e)
     {
-        var params = $H((!this.params || Object.isArray(this.params)) ? {} : this.params);
-        params.update(e.findElement('form').serialize(true));
+        if (this.uri) {
+            var params = $H((!this.params || Object.isArray(this.params)) ? {} : this.params);
+            params.update(e.findElement('form').serialize(true));
 
-        new Ajax.Request(this.uri, { parameters: params, onSuccess: this._onSuccess.bind(this) });
+            new Ajax.Request(this.uri, {
+                onSuccess: this._onSuccess.bind(this),
+                parameters: params
+            });
+        } else {
+            RedBox.getWindowContents().fire('IMPDialog:onClick', e);
+            this._close();
+        }
     },
 
     _onSuccess: function(r)
@@ -81,7 +109,7 @@ var IMPDialog = {
         if (r.response.success) {
             this._close();
             this.noreload = false;
-            document.fire('IMPDialog:success', this.type);
+            RedBox.getWindowContents().fire('IMPDialog:success', this.type);
             if (!this.noreload) {
                 location.reload();
             }
index 2a533b6..b01ca5f 100644 (file)
@@ -22,13 +22,6 @@ var ImpMailbox = {
         id.down('INPUT.checkbox').setValue(select);
     },
 
-    confirmDialog: function(url, msg)
-    {
-        RedBox.overlay = true;
-        RedBox.showHtml('<div id="RB_confirm"><p>' + msg + '</p><input type="button" class="button" onclick="window.location=\'' + url + '\';" value="' + IMP.text.yes + '" />' +
-                        '<input type="button" class="button" onclick="RedBox.close();" value="' + IMP.text.no + '" /></div>');
-    },
-
     submit: function(actID)
     {
         if (!this.anySelected()) {
@@ -263,12 +256,26 @@ var ImpMailbox = {
                 return;
 
             case 'delete_vfolder':
-                this.confirmDialog(elt.readAttribute('href'), IMP.text.mailbox_delete_vfolder);
+                this.lastclick = elt.readAttribute('href');
+                IMPDialog.display({
+                    cancel_text: IMP.text.no,
+                    form_id: 'RB_ImpMailbox',
+                    noinput: true,
+                    ok_text: IMP.text.yes,
+                    text: IMP.text.mailbox_delete_vfolder
+                });
                 e.stop();
                 return;
 
             case 'empty_mailbox':
-                this.confirmDialog(elt.readAttribute('href'), IMP.text.mailbox_delete_all);
+                this.lastclick = elt.readAttribute('href');
+                IMPDialog.display({
+                    cancel_text: IMP.text.no,
+                    form_id: 'RB_ImpMailbox',
+                    noinput: true,
+                    ok_text: IMP.text.yes,
+                    text: IMP.text.mailbox_delete_all
+                });
                 e.stop();
                 return;
             }
@@ -370,3 +377,9 @@ document.observe('dom:loaded', function() {
         } catch (e) {}
     }
 });
+
+document.observe('IMPDialog:onClick', function(e) {
+    if (e.element().identify() == 'RB_ImpMailbox') {
+        window.location = this.lastclick;
+    }
+}.bindAsEventListener(ImpMailbox));
index 21b91ec..c5e40fc 100644 (file)
@@ -344,6 +344,7 @@ if ($vfolder ||
     $pagetitle = $title = htmlspecialchars($title);
 }
 
+Horde::addScriptFile('dialog.js', 'imp');
 Horde::addScriptFile('effects.js', 'horde');
 Horde::addScriptFile('redbox.js', 'horde');
 Horde::addScriptFile('mailbox.js', 'imp');