Long overdue Viewer event listener refactoring: Change listeners
authorGraeme McCutcheon <graememcc_firefox@graeme-online.co.uk>
Wed, 12 Dec 2012 18:49:58 +0000
changeset 1182 84285807f70e67d89052669a19cb0b30fd11a426
parent 1181 c37527b85c24daf0f76f766c9649114d1d39891b
child 1183 29db0c0d6172a9563019d3356e47a55f97f8d32e
push id659
push useremorley@mozilla.com
push dateTue, 18 Jun 2013 13:49:17 +0000
Long overdue Viewer event listener refactoring: Change listeners
mcmerge/js/Viewer.js
--- a/mcmerge/js/Viewer.js
+++ b/mcmerge/js/Viewer.js
@@ -6,16 +6,17 @@ var Viewer = {
       return function (e) {
         fn.call(that, e);
       };
     }
 
     var self = this;
     $('#viewerOutput').click(bindListener(self.clickListener, self));
     $('#viewerOutput').on('input', bindListener(self.inputListener, self));
+    $('#viewerOutput').on('change', bindListener(self.changeListener, self));
   },
 
   clickListener: function viewer_clickListener(e) {
     var listenerDict = {
       'addBug'      : {func: this.onAddButtonClick,    preventDefault: true},
       'changeButton': {func: this.onChangeButtonClick, preventDefault: true},
       'removeButton': {func: this.onRemoveButtonClick, preventDefault: true},
       'commentCheck': {func: this.onCommentCheckClick, preventDefault: false},
@@ -48,35 +49,36 @@ var Viewer = {
     var className = e.target.className.split(' ')[0];
     if (!(className in listenerDict))
       return;
 
     listenerDict[className].call(this, e.target);
   },
 
 
-  addMilestoneChangeListener: function viewer_addMilestoneChangeListener(cset, bug) {
-    $('#' + this.getMilestonesID(cset, bug)).on('change', this.onMilestoneChange);
-  },
+  changeListener: function viewer_changeListener(e) {
+    var listenerDict = {
+      'milestone': this.onMilestoneChange,
+      'testsuite': this.onTestsuiteChange
+    };
 
+    var className = e.target.className.split(' ')[0];
+    if (!(className in listenerDict))
+      return;
 
-  addTestsuiteChangeListener: function viewer_addTestsuiteChangeListener(cset, bug) {
-    $('#' + this.getTestsuiteID(cset, bug)).on('change', this.onTestsuiteChange);
+    listenerDict[className].call(this, e.target);
   },
 
 
   addBug: function viewer_attachBug(index, bugID) {
     var cset = PushData.allPushes[index].cset;
 
     var bugHTML = this.makeBugHTML(index, bugID);
     $('#' + this.getBugDivID(cset)).prepend(bugHTML);
 
-    // Hook up listeners
-    this.addMilestoneChangeListener(cset, bugID);
-    this.addTestsuiteChangeListener(cset, bugID);
     this.updateSubmitButton();
   },
 
 
   // Assumes the bug has already been removed from attachedBugs[cset]
   removeBug: function viewer_removeBug(index, id) {
     var cset = PushData.allPushes[index].cset;
     $('#' + this.getBugCsetID(cset, id)).remove();
@@ -229,38 +231,32 @@ var Viewer = {
         var index = elem.getAttribute('data-index');
         ViewerController.onCommentCheckClick(index, bug, checked);
       }
     }
     elems.attr('checked', checked);
   },
 
 
-  onMilestoneChange: function viewer_onMilestoneChange(e) {
-    e.preventDefault();
-    var target = e.target;
-
+  onMilestoneChange: function viewer_onMilestoneChange(target) {
     if (!target.hasAttribute('data-index') ||
         !target.hasAttribute('data-bug')) {
       UI.showErrorMessage('Milestone changed with no data!');
       return;
     }
 
     var index = target.getAttribute('data-index');
     var bug = target.getAttribute('data-bug');
     // Update all other instances of this bug
     $('.'+bug+'Milestone').val(target.value);
     ViewerController.onMilestoneChange(bug, target.value);
   },
 
 
-  onTestsuiteChange: function viewer_onTestsuiteChange(e) {
-    e.preventDefault();
-    var target = e.target;
-
+  onTestsuiteChange: function viewer_onTestsuiteChange(target) {
     if (!target.hasAttribute('data-index') ||
         !target.hasAttribute('data-bug')) {
       UI.showErrorMessage('Testsuite changed with no data!');
       return;
     }
 
     var index = target.getAttribute('data-index');
     var bug = target.getAttribute('data-bug');
@@ -694,20 +690,16 @@ var Viewer = {
     } else {
       var html = pushes.map(function viewer_ViewChangesetMaker2(i, ind, arr) {
         var h = PushData.allPushes[i].affected.map(function viewer_ViewBackoutMaker(j) {return this.addChangeset(j, false, 'backedout');}, this).join('');
         return h + this.makeBackoutBannerHTML() + this.addChangeset(i, ind == arr.length - 1, 'backout');
       }, this).join('');
       $('#viewerOutput').append(html);
     }
 
-    // Add listeners
-    $('.milestone').on('change', this.onMilestoneChange);
-    $('.testsuite').on('change', this.onTestsuiteChange);
-
     $('#viewerOutput').append(this.makeSubmitHTML());
     $('#viewerOutput').append(this.makeButtonHTML(onPrevious.label, onNext.label));
     if (onPrevious.fn)
       this.onPrevious = onPrevious.fn;
     else
       $('.prevButton').attr('disabled', true);
     if (onNext.fn)
       this.onNext = onNext.fn;