General JS cleanup
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 13 Feb 2009 08:00:32 +0000 (01:00 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 18 Feb 2009 06:07:52 +0000 (23:07 -0700)
imp/js/src/ContextSensitive.js
imp/js/src/DimpBase.js
imp/js/src/DimpCore.js
imp/js/src/compose-dimp.js
imp/js/src/fullmessage-dimp.js

index b587b72..b50498a 100644 (file)
 
 var ContextSensitive = Class.create({
 
-    initialize: function()
+    initialize: function(opts)
     {
-        this.lasttarget = this.target = null;
+        this.basectx = this.lasttarget = this.target = null;
         this.elements = $H();
         this.submenus = $H();
         this.current = [];
-        this.basectx = this.onShow = null;
+        this.opts = opts || {};
 
         document.observe('contextmenu', this._rightClickHandler.bindAsEventListener(this));
         document.observe('click', this._leftClickHandler.bindAsEventListener(this));
@@ -44,14 +44,6 @@ var ContextSensitive = Class.create({
     },
 
     /**
-     * Set an onshow function.
-     */
-    setOnShow: function(func)
-    {
-        this.onShow = func;
-    },
-
-    /**
      * Elements are of type ContextSensitive.Element.
      */
     addElement: function(id, target, opts)
@@ -234,8 +226,8 @@ var ContextSensitive = Class.create({
             x = v.width - size.width - 10;
         }
 
-        if (this.onShow) {
-            this.onShow(id, this.basectx);
+        if (this.opts.onShow) {
+            this.opts.onShow(id, this.basectx);
         }
 
         Effect.Appear(elt.setStyle({ left: x + 'px', top: y + 'px' }), { duration: 0.2, queue: { position: 'end', scope: 'cm_' + id, limit: 2 } });
index 424e3c7..4b7a64f 100644 (file)
@@ -602,12 +602,12 @@ var DimpBase = {
 
     _onMenuShow: function(ctx_id, ctx)
     {
-        var elts, folder, ob, sel;
+        var elts, folder, ob, sel, tmp;
 
         switch (ctx_id) {
         case 'ctx_folder':
             elts = $('ctx_folder_create', 'ctx_folder_rename', 'ctx_folder_delete');
-            folder = DimpCore.DMenu.element();
+            folder = $(ctx.ctx);
             if (folder.readAttribute('mbox') == 'INBOX') {
                 elts.invoke('hide');
             } else if (DIMP.conf.fixed_folders.indexOf(folder.readAttribute('mbox')) != -1) {
@@ -617,19 +617,9 @@ var DimpBase = {
                 elts.invoke('show');
             }
 
-            if (folder.hasAttribute('u')) {
-                $('ctx_folder_poll').hide();
-                $('ctx_folder_nopoll').show();
-            } else {
-                $('ctx_folder_poll').show();
-                $('ctx_folder_nopoll').hide();
-            }
-            break;
-
-        case 'ctx_message':
-            DimpCore.DMenu.addSubMenu('ctx_message_reply', 'ctx_reply');
-            DimpCore.DMenu.addSubMenu('ctx_message_forward', 'ctx_forward');
-            DimpCore.DMenu.addSubMenu('ctx_message_setflag', 'ctx_flag');
+            tmp = folder.hasAttribute('u');
+            [ $('ctx_folder_poll') ].invoke(tmp ? 'hide' : 'show');
+            [ $('ctx_folder_nopoll') ].invoke(tmp? 'show' : 'hide');
             break;
 
         case 'ctx_reply':
@@ -642,17 +632,11 @@ var DimpBase = {
 
         case 'ctx_otheractions':
             $('oa_setflag', 'oa_sep1', 'oa_blacklist', 'oa_whitelist', 'oa_sep2', 'oa_undeleted').compact().invoke(this.viewport.getSelected().size() ? 'show' : 'hide');
-            DimpCore.DMenu.addSubMenu('oa_setflag', 'ctx_flag');
-            break;
-
-        case 'ctx_draft':
-            DimpCore.DMenu.addSubMenu('ctx_draft_setflag', 'ctx_flag');
             break;
         }
-        return true;
     },
 
-    _onResize: function(noupdate, nowait)
+    onResize: function(noupdate, nowait)
     {
         if (this.viewport) {
             this.viewport.onResize(noupdate, nowait);
@@ -663,6 +647,7 @@ var DimpBase = {
     updateTitle: function()
     {
         var elt, label, unseen;
+
         if (this.viewport.isFiltering()) {
             label = DIMP.text.search + ' :: ' + (this.viewport.getMetaData('total_rows') || 0) + ' ' + DIMP.text.resfound;
         } else {
@@ -689,6 +674,7 @@ var DimpBase = {
 
         var s, sortby,
             elt = e.element();
+
         if (!elt.hasAttribute('sortby')) {
             elt = elt.up('[sortby]');
             if (!elt) {
@@ -1004,9 +990,10 @@ var DimpBase = {
     /* Folder list updates. */
     pollFolders: function()
     {
+        var args = {};
+
         // Reset poll folder counter.
         this.setPollFolders();
-        var args = {};
 
         // Check for label info - it is possible that the mailbox may be
         // loading but not complete yet and sending this request will cause
@@ -1022,13 +1009,11 @@ var DimpBase = {
 
     _pollFoldersCallback: function(r)
     {
-        var that;
         r = r.response;
         if (r.poll) {
-            that = this;
             $H(r.poll).each(function(u) {
-                that.updateUnseenStatus(u.key, u.value);
-            });
+                this.updateUnseenStatus(u.key, u.value);
+            }, this);
         }
         if (r.quota) {
             this._displayQuota(r.quota);
@@ -1038,7 +1023,7 @@ var DimpBase = {
 
     _displayQuota: function(r)
     {
-        q = $('quota').cleanWhitespace();
+        var q = $('quota').cleanWhitespace();
         q.setText(r.m);
         q.down('SPAN.used IMG').writeAttribute({ width: 99 - r.p });
     },
@@ -1093,7 +1078,7 @@ var DimpBase = {
             this._setSearchfilterParams(this.viewport.getMetaData('special') ? 'to' : 'from', 'msg');
             this._setSearchfilterParams('current', 'folder');
             $(document.documentElement).setStyle({ overflowY: 'hidden' });
-            Effect.SlideDown(q, { duration: 0.5, afterFinish: function() { this._onResize(false, true); $(document.documentElement).setStyle({ overflowY: 'auto' }); }.bind(this) });
+            Effect.SlideDown(q, { duration: 0.5, afterFinish: function() { this.onResize(false, true); $(document.documentElement).setStyle({ overflowY: 'auto' }); }.bind(this) });
         }
     },
 
@@ -1117,7 +1102,7 @@ var DimpBase = {
             this.searchobserve = null;
         }
         this._setFilterText(true);
-        Effect.SlideUp($('qoptions').up(), { duration: 0.5, afterFinish: this._onResize.bind(this, reset) });
+        Effect.SlideUp($('qoptions').up(), { duration: 0.5, afterFinish: this.onResize.bind(this, reset) });
         this.filtertoggle = 2;
         this.resetSelected();
         this.viewport.stopFilter(reset);
@@ -1224,7 +1209,7 @@ var DimpBase = {
     },
 
     /* Keydown event handler */
-    _keydownHandler: function(e)
+    keydownHandler: function(e)
     {
         var co, form, ps, r, row, rowoff, sel,
             elt = e.element(),
@@ -1345,7 +1330,7 @@ var DimpBase = {
         }
     },
 
-    _keyupHandler: function(e)
+    keyupHandler: function(e)
     {
         if (e.element().readAttribute('id') == 'msgList_filter') {
             if (this.searchobserve) {
@@ -1357,7 +1342,7 @@ var DimpBase = {
         }
     },
 
-    _clickHandler: function(e, dblclick)
+    clickHandler: function(e, dblclick)
     {
         if (e.isRightClick()) {
             return;
@@ -1626,7 +1611,7 @@ var DimpBase = {
         }
     },
 
-    _mouseHandler: function(e, type)
+    mouseHandler: function(e, type)
     {
         var elt = e.element();
 
@@ -2265,7 +2250,9 @@ var DimpBase = {
     /* Onload function. */
     onDomLoad: function()
     {
-        var DC = DimpCore;
+        var DC = DimpCore, DM = DimpCore.DMenu;
+
+        DC.init({ DMenu_onShow: this._onMenuShow.bind(this) });
 
         $('dimpLoading').hide();
         $('dimpPage').show();
@@ -2296,12 +2283,16 @@ var DimpBase = {
         this._setFilterText(true);
 
         /* Add popdown menus. */
-        DC.DMenu.setOnShow(this._onMenuShow.bind(this));
         this._addMouseEvents({ id: 'button_reply', type: 'reply' }, true);
-        DC.DMenu.disable('button_reply_img', true, true);
+        DM.disable('button_reply_img', true, true);
         this._addMouseEvents({ id: 'button_forward', type: 'forward' }, true);
-        DC.DMenu.disable('button_forward_img', true, true);
+        DM.disable('button_forward_img', true, true);
         this._addMouseEvents({ id: 'button_other', type: 'otheractions' }, true);
+        DM.addSubMenu('ctx_message_reply', 'ctx_reply');
+        DM.addSubMenu('ctx_message_forward', 'ctx_forward');
+        DM.addSubMenu('ctx_message_setflag', 'ctx_flag');
+        DM.addSubMenu('oa_setflag', 'ctx_flag');
+        DM.addSubMenu('ctx_draft_setflag', 'ctx_flag');
 
         new Drop('dropbase', this._folderDropConfig);
 
@@ -2319,18 +2310,6 @@ var DimpBase = {
         /* Check for new mail. */
         this.setPollFolders();
 
-        /* Bind key shortcuts. */
-        document.observe('keydown', this._keydownHandler.bindAsEventListener(this));
-        document.observe('keyup', this._keyupHandler.bindAsEventListener(this));
-
-        /* Bind mouse clicks. */
-        document.observe('click', this._clickHandler.bindAsEventListener(this));
-        document.observe('dblclick', this._clickHandler.bindAsEventListener(this, true));
-        document.observe('mouseover', this._mouseHandler.bindAsEventListener(this, 'over'));
-
-        /* Resize elements on window size change. */
-        Event.observe(window, 'resize', this._onResize.bind(this));
-
         if (DIMP.conf.is_ie6) {
             /* Disable text selection in preview pane for IE 6. */
             document.observe('selectstart', Event.stop);
@@ -2366,6 +2345,7 @@ var DimpBase = {
             iframe.setStyle({ width: $('dimpmain').getStyle('width'), height: (document.viewport.getHeight() - 20) + 'px' });
         }
     }
+
 };
 
 /* Need to add after DimpBase is defined. */
@@ -2451,6 +2431,11 @@ DimpCore.onDoActionComplete = function(r) {
     }
 };
 
-/* Stuff to do immediately when page is ready. */
+/* Initialize global event handlers. */
 document.observe('dom:loaded', DimpBase.onDomLoad.bind(DimpBase));
-
+document.observe('keydown', DimpBase.keydownHandler.bindAsEventListener(DimpBase));
+document.observe('keyup', DimpBase.keyupHandler.bindAsEventListener(DimpBase));
+document.observe('click', DimpBase.clickHandler.bindAsEventListener(DimpBase));
+document.observe('dblclick', DimpBase.clickHandler.bindAsEventListener(DimpBase, true));
+document.observe('mouseover', DimpBase.mouseHandler.bindAsEventListener(DimpBase, 'over'));
+Event.observe(window, 'resize', DimpBase.onResize.bind(DimpBase));
index 863cdad..bd6fe5d 100644 (file)
@@ -14,8 +14,7 @@ var frames = { horde_main: true },
 /* DimpCore object. */
 DimpCore = {
     // Vars used and defaulting to null/false:
-    //   DMenu, alertrequest, inAjaxCallback, is_logout, onDoActionComplete,
-    //   window_load
+    //   DMenu, alertrequest, inAjaxCallback, is_logout, onDoActionComplete
     server_error: 0,
 
     buttons: [
@@ -147,7 +146,6 @@ DimpCore = {
     doActionComplete: function(request, callback)
     {
         this.inAjaxCallback = true;
-        var r;
 
         if (!request.responseJSON) {
             if (++this.server_error == 3) {
@@ -157,19 +155,17 @@ DimpCore = {
             return;
         }
 
-        r = request.responseJSON;
+        var r = request.responseJSON;
 
         if (!r.msgs) {
             r.msgs = [];
         }
 
         if (r.response && Object.isFunction(callback)) {
-            if (DIMP.conf.debug) {
+            try {
                 callback(r);
-            } else {
-                try {
-                    callback(r);
-                } catch (e) {}
+            } catch (e) {
+                this.debug('doActionComplete', e);
             }
         }
 
@@ -502,42 +498,41 @@ DimpCore = {
 
             elt = elt.up();
         }
-    }
-};
+    },
 
-// Initialize DMenu now.  Need to init here because IE doesn't load dom:loaded
-// in a predictable order.
-if (typeof ContextSensitive != 'undefined') {
-    DimpCore.DMenu = new ContextSensitive();
-}
-
-document.observe('dom:loaded', function() {
-    /* Don't do additional onload stuff if we are in a popup. We 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. */
-    try {
-        if (parent.opener &&
-            parent.opener.location.host == window.location.host &&
-            parent.opener.DimpCore) {
-            DIMP.baseWindow = parent.opener.DIMP.baseWindow || parent.opener;
+    /* Dimp initialization function. */
+    init: function(opts)
+    {
+        opts = opts || {};
+
+        if (typeof ContextSensitive != 'undefined') {
+            this.DMenu = new ContextSensitive({ onShow: opts.DMenu_onShow });
         }
-    } catch (e) {}
 
-    /* Remove unneeded buttons. */
-    if (!DIMP.conf.spam_reporting) {
-        DimpCore.buttons = DimpCore.buttons.without('button_spam');
-    }
-    if (!DIMP.conf.ham_reporting) {
-        DimpCore.buttons = DimpCore.buttons.without('button_ham');
+        /* Don't do additional onload stuff if we are in a popup. We 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. */
+        try {
+            if (parent.opener &&
+                parent.opener.location.host == window.location.host &&
+                parent.opener.DimpCore) {
+                DIMP.baseWindow = parent.opener.DIMP.baseWindow || parent.opener;
+            }
+        } catch (e) {}
+
+        /* Remove unneeded buttons. */
+        if (!DIMP.conf.spam_reporting) {
+            this.buttons = this.buttons.without('button_spam');
+        }
+        if (!DIMP.conf.ham_reporting) {
+            this.buttons = this.buttons.without('button_ham');
+        }
     }
 
-    /* Add click handler. */
-    document.observe('click', DimpCore._clickHandler.bindAsEventListener(DimpCore));
-});
+};
 
-Event.observe(window, 'load', function() {
-    DimpCore.window_load = true;
-});
+/* Add click handler. */
+document.observe('click', DimpCore._clickHandler.bindAsEventListener(DimpCore));
 
 /* Helper methods for setting/getting element text without mucking
  * around with multiple TextNodes. */
index 866190d..0f280de 100644 (file)
@@ -256,7 +256,6 @@ var DimpCompose = {
                     elt = new Element('DIV', [ DIMP.text_compose.attachment_limit ]);
                 } else {
                     elt = new Element('INPUT', { type: 'file', name: 'file_1' });
-                    elt.observe('change', this.uploadAttachment.bind(this));
                 }
                 $('upload_wait').replace(elt.writeAttribute('id', 'upload'));
                 this.resizeMsgArea();
@@ -524,7 +523,7 @@ var DimpCompose = {
             de = document.documentElement,
             msg = $('message');
 
-        if (!DimpCore.window_load) {
+        if (!document.loaded) {
             this.resizeMsgArea.bind(this).defer();
             return;
         }
@@ -567,16 +566,14 @@ var DimpCompose = {
     uploadAttachment: function()
     {
         var u = $('upload');
-        $('submit_frame').observe('load', this.attachmentComplete.bind(this));
         this.uniqueSubmit('add_attachment');
-        u.stopObserving('change').replace(new Element('DIV', { id: 'upload_wait' }).insert(DIMP.text_compose.uploading + ' ' + $F(u)));
+        u.replace(new Element('DIV', { id: 'upload_wait' }).insert(DIMP.text_compose.uploading + ' ' + $F(u)));
     },
 
     attachmentComplete: function()
     {
         var sf = $('submit_frame'),
             doc = sf.contentDocument || sf.contentWindow.document;
-        sf.stopObserving('load');
         DimpCore.doActionComplete({ responseJSON: doc.body.innerHTML.evalJSON(true) }, this.uniqueSubmitCallback.bind(this));
     },
 
@@ -631,8 +628,7 @@ var DimpCompose = {
     },
 
     /* Click observe handler. */
-
-    _clickHandler: function(e)
+    clickHandler: function(e)
     {
         if (e.isRightClick()) {
             return;
@@ -683,7 +679,53 @@ var DimpCompose = {
 
             elt = elt.up();
         }
+    },
+
+    changeHandler: function(e)
+    {
+        var elt = e.element(),
+            id = elt.readAttribute('id');
+
+        switch (id) {
+        case 'identity':
+            this.change_identity();
+            break;
+
+        case 'upload':
+            this.uploadAttachment();
+            break;
+        }
+    },
+
+    onDomLoad: function()
+    {
+        var boundResize = this.resizeMsgArea.bind(this);
+
+        DimpCore.init();
+
+        this.resizeMsgArea();
+        this.initializeSpellChecker();
+
+        // Automatically resize address fields.
+        this.resizeto = new ResizeTextArea('to', boundResize);
+        this.resizecc = new ResizeTextArea('cc', boundResize);
+        this.resizebcc = new ResizeTextArea('bcc', boundResize);
+
+        // Safari requires a submit target iframe to be at least 1x1 size or
+        // else it will open content in a new window.  See:
+        //   http://blog.caboo.se/articles/2007/4/2/ajax-file-upload
+        if (Prototype.Browser.WebKit) {
+            $('submit_frame').writeAttribute({ position: 'absolute', width: '1px', height: '1px' }).setStyle({ left: '-999px' }).show();
+        }
+
+        /* Add addressbook link formatting. */
+        if (DIMP.conf_compose.abook_url) {
+            $('sendto', 'sendcc', 'sendbcc').each(function(a) {
+                a.down('TD.label SPAN').addClassName('composeAddrbook');
+            });
+        }
     }
+
 },
 
 ResizeTextArea = Class.create({
@@ -725,44 +767,13 @@ ResizeTextArea = Class.create({
             }
         }
     }
-});
-
-document.observe('dom:loaded', function() {
-    var tmp,
-        DC = DimpCompose,
-        boundResize = DC.resizeMsgArea.bind(DC);
-
-    DC.resizeMsgArea();
-    DC.initializeSpellChecker();
-    $('upload').observe('change', DC.uploadAttachment.bind(DC));
-
-    // Automatically resize address fields.
-    DC.resizeto = new ResizeTextArea('to', boundResize);
-    DC.resizecc = new ResizeTextArea('cc', boundResize);
-    DC.resizebcc = new ResizeTextArea('bcc', boundResize);
-
-    // Safari requires a submit target iframe to be at least 1x1 size or else
-    // it will open content in a new window.  See:
-    //   http://blog.caboo.se/articles/2007/4/2/ajax-file-upload
-    if (Prototype.Browser.WebKit) {
-        $('submit_frame').writeAttribute({ position: 'absolute', width: '1px', height: '1px' }).setStyle({ left: '-999px' }).show();
-    }
-
-    /* Add addressbook link formatting. */
-    if (DIMP.conf_compose.abook_url) {
-        $('sendto', 'sendcc', 'sendbcc').each(function(a) {
-            a.down('TD.label SPAN').addClassName('composeAddrbook');
-        });
-    }
 
-    /* Mouse click handler. */
-    document.observe('click', DC._clickHandler.bindAsEventListener(DC));
-
-    /* Only allow submit through send button. */
-    $('compose').observe('submit', Event.stop);
-
-    /* Attach other handlers. */
-    $('identity').observe('change', DC.change_identity.bind(DC));
-
-    Event.observe(window, 'resize', boundResize);
 });
+
+/* Attach event handlers. */
+document.observe('dom:loaded', DimpCompose.onDomLoad.bind(DimpCompose));
+document.observe('change', DimpCompose.changeHandler.bindAsEventListener(DimpCompose));
+document.observe('click', DimpCompose.clickHandler.bindAsEventListener(DimpCompose));
+Event.observe(window, 'resize', DimpCompose.resizeMsgArea.bind(DimpCompose));
+$('compose').observe('submit', Event.stop);
+$('submit_frame').observe('load', DimpCompose.attachmentComplete.bind(DimpCompose));
index 2441ea2..b8b45c4 100644 (file)
@@ -67,7 +67,7 @@ var DimpFullmessage = {
     },
 
     /* Mouse click handler. */
-    _clickHandler: function(e)
+    clickHandler: function(e)
     {
         if (e.isRightClick()) {
             return;
@@ -122,31 +122,30 @@ var DimpFullmessage = {
     },
 
     /* Add a popdown menu to a dimpactions button. */
-    addPopdown: function(bid, ctx)
+    _addPopdown: function(bid, ctx)
     {
         var bidelt = $(bid);
         bidelt.insert({ after: $($('popdown_img').cloneNode(false)).writeAttribute('id', bid + '_img').show() });
         DimpCore.DMenu.addElement(bid + '_img', 'ctx_' + ctx, { offset: bidelt.up(), left: true });
-    }
-
-};
+    },
 
-document.observe('dom:loaded', function() {
-    var FM = DimpFullmessage;
+    onDomLoad: function()
+    {
+        DimpCore.init();
 
-    window.focus();
+        this.addPopdown('reply_link', 'replypopdown');
+        this.addPopdown('forward_link', 'fwdpopdown');
 
-    FM.addPopdown('reply_link', 'replypopdown');
-    FM.addPopdown('forward_link', 'fwdpopdown');
+        /* Set up address linking. */
+        [ 'from', 'to', 'cc', 'bcc', 'replyTo' ].each(function(a) {
+            if (this[a]) {
+                var elt = $('msgHeader' + a.charAt(0).toUpperCase() + a.substring(1)).down('TD', 1);
+                elt.replace(DimpCore.buildAddressLinks(this[a], elt.cloneNode(false)));
+            }
+        }, this);
+    }
 
-    /* Set up address linking. */
-    [ 'from', 'to', 'cc', 'bcc', 'replyTo' ].each(function(a) {
-        if (FM[a]) {
-            var elt = $('msgHeader' + a.charAt(0).toUpperCase() + a.substring(1)).down('TD', 1);
-            elt.replace(DimpCore.buildAddressLinks(FM[a], elt.cloneNode(false)));
-        }
-    });
+};
 
-    /* Set up click handlers. */
-    document.observe('click', FM._clickHandler.bindAsEventListener(FM));
-});
+document.observe('dom:loaded', DimpFullmessage.onDomLoad.bind(DimpFullmessage));
+document.observe('click', DimpFullmessage.clickHandler.bindAsEventListener(DimpFullmessage));