From 772bec6789ed4c6ee460577fd567717fac1250d4 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Mon, 23 Dec 2013 06:47:03 -0500
Subject: [PATCH] Make addEventListener() chainable for better performance and compact code Replace deperecated row_init usage with addEventListener('initrow')

---
 program/js/list.js   |    2 
 program/js/common.js |    3 
 program/js/app.js    |  113 ++++++++++++++++++++-----------------
 skins/larry/ui.js    |   38 ++++++------
 4 files changed, 84 insertions(+), 72 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index 4c9b11a..2438ff9 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -198,22 +198,23 @@
             multiselect:true, multiexpand:true, draggable:true, keyboard:true,
             column_movable:this.env.col_movable, dblclick_time:this.dblclick_time
             });
-          this.message_list.row_init = function(o){ p.init_message_row(o); };
-          this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
-          this.message_list.addEventListener('click', function(o){ p.msglist_click(o); });
-          this.message_list.addEventListener('keypress', function(o){ p.msglist_keypress(o); });
-          this.message_list.addEventListener('select', function(o){ p.msglist_select(o); });
-          this.message_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
-          this.message_list.addEventListener('dragmove', function(e){ p.drag_move(e); });
-          this.message_list.addEventListener('dragend', function(e){ p.drag_end(e); });
-          this.message_list.addEventListener('expandcollapse', function(e){ p.msglist_expand(e); });
-          this.message_list.addEventListener('column_replace', function(e){ p.msglist_set_coltypes(e); });
-          this.message_list.addEventListener('listupdate', function(e){ p.triggerEvent('listupdate', e); });
+          this.message_list
+            .addEventListener('initrow', function(o) { p.init_message_row(o); })
+            .addEventListener('dblclick', function(o) { p.msglist_dbl_click(o); })
+            .addEventListener('click', function(o) { p.msglist_click(o); })
+            .addEventListener('keypress', function(o) { p.msglist_keypress(o); })
+            .addEventListener('select', function(o) { p.msglist_select(o); })
+            .addEventListener('dragstart', function(o) { p.drag_start(o); })
+            .addEventListener('dragmove', function(e) { p.drag_move(e); })
+            .addEventListener('dragend', function(e) { p.drag_end(e); })
+            .addEventListener('expandcollapse', function(o) { p.msglist_expand(o); })
+            .addEventListener('column_replace', function(o) { p.msglist_set_coltypes(o); })
+            .addEventListener('listupdate', function(o) { p.triggerEvent('listupdate', o); })
+            .init();
 
           document.onmouseup = function(e){ return p.doc_mouse_up(e); };
           this.gui_objects.messagelist.parentNode.onmousedown = function(e){ return p.click_on_list(e); };
 
-          this.message_list.init();
           this.enable_command('toggle_status', 'toggle_flag', 'sort', true);
 
           // load messages
@@ -313,10 +314,11 @@
         if (this.gui_objects.contactslist) {
           this.contact_list = new rcube_list_widget(this.gui_objects.contactslist,
             { multiselect:true, draggable:false, keyboard:false });
-          this.contact_list.row_init = function(row){ p.triggerEvent('insertrow', { cid:row.uid, row:row }); };
-          this.contact_list.addEventListener('select', function(o){ ref.compose_recipient_select(o); });
-          this.contact_list.addEventListener('dblclick', function(o){ ref.compose_add_recipient('to'); });
-          this.contact_list.init();
+          this.contact_list
+            .addEventListener('initrow', function(o) { p.triggerEvent('insertrow', { cid:o.uid, row:o }); })
+            .addEventListener('select', function(o) { ref.compose_recipient_select(o); })
+            .addEventListener('dblclick', function(o) { ref.compose_add_recipient('to'); })
+            .init();
         }
 
         if (this.gui_objects.addressbookslist) {
@@ -353,13 +355,14 @@
         if (this.gui_objects.contactslist) {
           this.contact_list = new rcube_list_widget(this.gui_objects.contactslist,
             {multiselect:true, draggable:this.gui_objects.folderlist?true:false, keyboard:true});
-          this.contact_list.row_init = function(row){ p.triggerEvent('insertrow', { cid:row.uid, row:row }); };
-          this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
-          this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
-          this.contact_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
-          this.contact_list.addEventListener('dragmove', function(e){ p.drag_move(e); });
-          this.contact_list.addEventListener('dragend', function(e){ p.drag_end(e); });
-          this.contact_list.init();
+          this.contact_list
+            .addEventListener('initrow', function(o) { p.triggerEvent('insertrow', { cid:o.uid, row:o }); })
+            .addEventListener('keypress', function(o) { p.contactlist_keypress(o); })
+            .addEventListener('select', function(o) { p.contactlist_select(o); })
+            .addEventListener('dragstart', function(o) { p.drag_start(o); })
+            .addEventListener('dragmove', function(e) { p.drag_move(e); })
+            .addEventListener('dragend', function(e) { p.drag_end(e); })
+            .init();
 
           if (this.env.cid)
             this.contact_list.highlight_row(this.env.cid);
@@ -416,35 +419,39 @@
         }
 
         if (this.gui_objects.identitieslist) {
-          this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
-          this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
-          this.identity_list.init();
-          this.identity_list.focus();
+          this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist,
+            {multiselect:false, draggable:false, keyboard:false});
+          this.identity_list
+            .addEventListener('select', function(o) { p.identity_select(o); })
+            .init()
+            .focus();
 
           if (this.env.iid)
             this.identity_list.highlight_row(this.env.iid);
         }
         else if (this.gui_objects.sectionslist) {
           this.sections_list = new rcube_list_widget(this.gui_objects.sectionslist, {multiselect:false, draggable:false, keyboard:false});
-          this.sections_list.addEventListener('select', function(o){ p.section_select(o); });
-          this.sections_list.init();
-          this.sections_list.focus();
+          this.sections_list
+            .addEventListener('select', function(o) { p.section_select(o); })
+            .init()
+            .focus();
         }
         else if (this.gui_objects.subscriptionlist) {
           this.init_subscription_list();
         }
         else if (this.gui_objects.responseslist) {
           this.responses_list = new rcube_list_widget(this.gui_objects.responseslist, {multiselect:false, draggable:false, keyboard:false});
-          this.responses_list.addEventListener('select', function(list) {
-            var win, id = list.get_single_selection();
-            p.enable_command('delete', !!id && $.inArray(id, p.env.readonly_responses) < 0);
-            if (id && (win = p.get_frame_window(p.env.contentframe))) {
-              p.set_busy(true);
-              p.location_href({ _action:'edit-response', _key:id, _framed:1 }, win);
-            }
-          });
-          this.responses_list.init();
-          this.responses_list.focus();
+          this.responses_list
+            .addEventListener('select', function(list) {
+              var win, id = list.get_single_selection();
+              p.enable_command('delete', !!id && $.inArray(id, p.env.readonly_responses) < 0);
+              if (id && (win = p.get_frame_window(p.env.contentframe))) {
+                p.set_busy(true);
+                p.location_href({ _action:'edit-response', _key:id, _framed:1 }, win);
+              }
+            })
+            .init()
+            .focus();
         }
 
         break;
@@ -510,11 +517,12 @@
           id_prefix: 'rcmli',
           id_encode: this.html_identifier_encode,
           id_decode: this.html_identifier_decode,
-          check_droptarget: function(node){ return !node.virtual && ref.check_droptarget(node.id) }
+          check_droptarget: function(node) { return !node.virtual && ref.check_droptarget(node.id) }
         });
-        this.treelist.addEventListener('collapse', function(node){ ref.folder_collapsed(node) });
-        this.treelist.addEventListener('expand', function(node){ ref.folder_collapsed(node) });
-        this.treelist.addEventListener('select', function(node){ ref.triggerEvent('selectfolder', { folder:node.id, prefix:'rcmli' }) });
+        this.treelist
+          .addEventListener('collapse', function(node) { ref.folder_collapsed(node) })
+          .addEventListener('expand', function(node) { ref.folder_collapsed(node) })
+          .addEventListener('select', function(node) { ref.triggerEvent('selectfolder', { folder:node.id, prefix:'rcmli' }) });
       }
     }
 
@@ -5615,14 +5623,15 @@
 
     this.subscription_list = new rcube_list_widget(this.gui_objects.subscriptionlist,
       {multiselect:false, draggable:true, keyboard:false, toggleselect:true});
-    this.subscription_list.addEventListener('select', function(o){ p.subscription_select(o); });
-    this.subscription_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
-    this.subscription_list.addEventListener('dragend', function(o){ p.subscription_move_folder(o); });
-    this.subscription_list.row_init = function (row) {
-      row.obj.onmouseover = function() { p.focus_subscription(row.id); };
-      row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
-    };
-    this.subscription_list.init();
+    this.subscription_list
+      .addEventListener('select', function(o){ p.subscription_select(o); })
+      .addEventListener('dragstart', function(o){ p.drag_active = true; })
+      .addEventListener('dragend', function(o){ p.subscription_move_folder(o); })
+      .addEventListener('initrow', function (row) {
+        row.obj.onmouseover = function() { p.focus_subscription(row.id); };
+        row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
+      })
+      .init();
 
     $('#mailboxroot')
       .mouseover(function(){ p.focus_subscription(this.id); })
diff --git a/program/js/common.js b/program/js/common.js
index 1409146..9c0bb9d 100644
--- a/program/js/common.js
+++ b/program/js/common.js
@@ -285,7 +285,6 @@
  *
  * @param {String}   Event name
  * @param {Function} Handler function
- * @return Listener ID (used to remove this handler later on)
  */
 addEventListener: function(evt, func, obj)
 {
@@ -295,6 +294,8 @@
     this._events[evt] = [];
 
   this._events[evt].push({func:func, obj:obj ? obj : window});
+
+  return this; // chainable
 },
 
 /**
diff --git a/program/js/list.js b/program/js/list.js
index 54daa98..2ea4d21 100644
--- a/program/js/list.js
+++ b/program/js/list.js
@@ -97,6 +97,8 @@
     if (this.keyboard)
       rcube_event.add_listener({event:'keydown', object:this, method:'key_press'});
   }
+
+  return this;
 },
 
 
diff --git a/skins/larry/ui.js b/skins/larry/ui.js
index 3c4740d..add6910 100644
--- a/skins/larry/ui.js
+++ b/skins/larry/ui.js
@@ -136,9 +136,9 @@
 
     /***  mail task  ***/
     if (rcmail.env.task == 'mail') {
-      rcmail.addEventListener('menu-open', menu_open);
-      rcmail.addEventListener('menu-save', menu_save);
-      rcmail.addEventListener('responseafterlist', function(e){ switch_view_mode(rcmail.env.threading ? 'thread' : 'list') });
+      rcmail.addEventListener('menu-open', menu_open)
+        .addEventListener('menu-save', menu_save)
+        .addEventListener('responseafterlist', function(e){ switch_view_mode(rcmail.env.threading ? 'thread' : 'list') });
 
       var dragmenu = $('#dragmessagemenu');
       if (dragmenu.length) {
@@ -147,9 +147,9 @@
       }
 
       if (rcmail.env.action == 'show' || rcmail.env.action == 'preview') {
-        rcmail.addEventListener('enable-command', enable_command);
-        rcmail.addEventListener('aftershow-headers', function() { layout_messageview(); });
-        rcmail.addEventListener('afterhide-headers', function() { layout_messageview(); });
+        rcmail.addEventListener('enable-command', enable_command)
+          .addEventListener('aftershow-headers', function() { layout_messageview(); })
+          .addEventListener('afterhide-headers', function() { layout_messageview(); });
         $('#previewheaderstoggle').click(function(e){ toggle_preview_headers(); return false });
 
         // add menu link for each attachment
@@ -162,13 +162,13 @@
         }
       }
       else if (rcmail.env.action == 'compose') {
-        rcmail.addEventListener('aftertoggle-editor', function(e){
-          window.setTimeout(function(){ layout_composeview() }, 200);
-          if (e && e.mode)
-            $("select[name='editorSelector']").val(e.mode);
-        });
-        rcmail.addEventListener('aftersend-attachment', show_uploadform);
-        rcmail.addEventListener('add-recipient', function(p){ show_header_row(p.field, true); });
+        rcmail.addEventListener('aftersend-attachment', show_uploadform)
+          .addEventListener('add-recipient', function(p){ show_header_row(p.field, true); })
+          .addEventListener('aftertoggle-editor', function(e){
+            window.setTimeout(function(){ layout_composeview() }, 200);
+            if (e && e.mode)
+              $("select[name='editorSelector']").val(e.mode);
+          });
 
         // Show input elements with non-empty value
         var f, v, field, fields = ['cc', 'bcc', 'replyto', 'followupto'];
@@ -218,9 +218,9 @@
 
         new rcube_scroller('#folderlist-content', '#folderlist-header', '#folderlist-footer');
 
-        rcmail.addEventListener('setquota', update_quota);
-        rcmail.addEventListener('enable-command', enable_command);
-        rcmail.addEventListener('afterimport-messages', show_uploadform);
+        rcmail.addEventListener('setquota', update_quota)
+          .addEventListener('enable-command', enable_command)
+          .addEventListener('afterimport-messages', show_uploadform);
       }
       else if (rcmail.env.action == 'get') {
         new rcube_splitter({ id:'mailpartsplitterv', p1:'#messagepartheader', p2:'#messagepartcontainer',
@@ -275,9 +275,9 @@
     }
     /***  addressbook task  ***/
     else if (rcmail.env.task == 'addressbook') {
-      rcmail.addEventListener('afterupload-photo', show_uploadform);
-      rcmail.addEventListener('beforepushgroup', push_contactgroup);
-      rcmail.addEventListener('beforepopgroup', pop_contactgroup);
+      rcmail.addEventListener('afterupload-photo', show_uploadform)
+        .addEventListener('beforepushgroup', push_contactgroup)
+        .addEventListener('beforepopgroup', pop_contactgroup);
 
       if (rcmail.env.action == '') {
         new rcube_splitter({ id:'addressviewsplitterd', p1:'#addressview-left', p2:'#addressview-right',

--
Gitblit v1.9.1