Bug 1371131 - Part 2. Refactor ProfileAutoCompleteResult to using class syntax. r=lchang, steveck draft
authorRay Lin <ralin@mozilla.com>
Mon, 24 Jul 2017 11:41:33 +0800
changeset 614119 d5bec265d7e395316db3e5c856c7224abdbcddb2
parent 614118 1b1861332d4c2e4915dd3cb8df8de48c2dbe2723
child 614120 926b9185dbacbdc6a166b9a6c8460438e8d97b97
child 614257 66c9a014ed61cdf00c81365491962f15049c87e6
child 614934 51f404fbb834adcb012b468b6cfc6b8cc2963cc3
push id69925
push userbmo:ralin@mozilla.com
push dateMon, 24 Jul 2017 06:34:51 +0000
reviewerslchang, steveck
bugs1371131
milestone56.0a1
Bug 1371131 - Part 2. Refactor ProfileAutoCompleteResult to using class syntax. r=lchang, steveck MozReview-Commit-ID: GDsLZrhcSBx
browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
--- a/browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
+++ b/browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
@@ -9,87 +9,71 @@ this.EXPORTED_SYMBOLS = ["ProfileAutoCom
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://formautofill/FormAutofillUtils.jsm");
 
 this.log = null;
 FormAutofillUtils.defineLazyLogGetter(this, this.EXPORTED_SYMBOLS[0]);
 
+class ProfileAutoCompleteResult {
+  constructor(searchString, focusedFieldName, allFieldNames, matchingProfiles, {resultCode = null}) {
+    log.debug("Constructing new ProfileAutoCompleteResult:", [...arguments]);
 
-this.ProfileAutoCompleteResult = function(searchString,
-                                          focusedFieldName,
-                                          allFieldNames,
-                                          matchingProfiles,
-                                          {resultCode = null}) {
-  log.debug("Constructing new ProfileAutoCompleteResult:", [...arguments]);
-  this.searchString = searchString;
-  this._focusedFieldName = focusedFieldName;
-  this._allFieldNames = allFieldNames;
-  this._matchingProfiles = matchingProfiles;
+    // nsISupports
+    this.QueryInterface = XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult]);
 
-  if (resultCode) {
-    this.searchResult = resultCode;
-  } else if (matchingProfiles.length > 0) {
-    this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
-  } else {
-    this.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
-  }
+    // The user's query string
+    this.searchString = searchString;
+    // The field name of the focused input.
+    this._focusedFieldName = focusedFieldName;
+    // All field names in the form which contains the focused input.
+    this._allFieldNames = allFieldNames;
+    // The matching profiles contains the information for filling forms.
+    this._matchingProfiles = matchingProfiles;
+    // The default item that should be entered if none is selected
+    this.defaultIndex = 0;
+    // The reason the search failed
+    this.errorDescription = "";
 
-  this._popupLabels = this._generateLabels(this._focusedFieldName,
-                                           this._allFieldNames,
-                                           this._matchingProfiles);
-  // Add an empty result entry for footer. Its content will come from
-  // the footer binding, so don't assign any value to it.
-  this._popupLabels.push({
-    primary: "",
-    secondary: "",
-    categories: FormAutofillUtils.getCategoriesFromFieldNames(allFieldNames),
-    focusedCategory: FormAutofillUtils.getCategoryFromFieldName(focusedFieldName),
-  });
-};
-
-ProfileAutoCompleteResult.prototype = {
-
-  // The user's query string
-  searchString: "",
+    // The result code of this result object.
+    if (resultCode) {
+      this.searchResult = resultCode;
+    } else if (matchingProfiles.length > 0) {
+      this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
+    } else {
+      this.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
+    }
 
-  // The default item that should be entered if none is selected
-  defaultIndex: 0,
-
-  // The reason the search failed
-  errorDescription: "",
-
-  // The result code of this result object.
-  searchResult: null,
-
-  // The field name of the focused input.
-  _focusedFieldName: "",
-
-  // All field names in the form which contains the focused input.
-  _allFieldNames: null,
-
-  // The matching profiles contains the information for filling forms.
-  _matchingProfiles: null,
-
-  // An array of primary and secondary labels for each profiles.
-  _popupLabels: null,
+    // An array of primary and secondary labels for each profile.
+    this._popupLabels = this._generateLabels(this._focusedFieldName,
+                                             this._allFieldNames,
+                                             this._matchingProfiles);
+    // Add an empty result entry for footer. Its content will come from
+    // the footer binding, so don't assign any value to it.
+    this._popupLabels.push({
+      primary: "",
+      secondary: "",
+      categories: FormAutofillUtils.getCategoriesFromFieldNames(allFieldNames),
+      focusedCategory: FormAutofillUtils.getCategoryFromFieldName(focusedFieldName),
+    });
+  }
 
   /**
    * @returns {number} The number of results
    */
   get matchCount() {
     return this._popupLabels.length;
-  },
+  }
 
   _checkIndexBounds(index) {
     if (index < 0 || index >= this._popupLabels.length) {
       throw Components.Exception("Index out of range.", Cr.NS_ERROR_ILLEGAL_VALUE);
     }
-  },
+  }
 
   /**
    * Get the secondary label based on the focused field name and related field names
    * in the same form.
    * @param   {string} focusedFieldName The field name of the focused input
    * @param   {Array<Object>} allFieldNames The field names in the same section
    * @param   {object} profile The profile providing the labels to show.
    * @returns {string} The secondary label
@@ -149,17 +133,17 @@ ProfileAutoCompleteResult.prototype = {
             profile["-moz-street-address-one-line"]) {
           return profile["-moz-street-address-one-line"];
         }
         return profile[currentFieldName];
       }
     }
 
     return ""; // Nothing matched.
-  },
+  }
 
   _generateLabels(focusedFieldName, allFieldNames, profiles) {
     // Skip results without a primary label.
     return profiles.filter(profile => {
       return !!profile[focusedFieldName];
     }).map(profile => {
       let primaryLabel = profile[focusedFieldName];
       if (focusedFieldName == "street-address" &&
@@ -168,80 +152,78 @@ ProfileAutoCompleteResult.prototype = {
       }
       return {
         primary: primaryLabel,
         secondary: this._getSecondaryLabel(focusedFieldName,
                                            allFieldNames,
                                            profile),
       };
     });
-  },
+  }
+
 
   /**
    * Retrieves a result
    * @param   {number} index The index of the result requested
    * @returns {string} The result at the specified index
    */
   getValueAt(index) {
     this._checkIndexBounds(index);
     return this._popupLabels[index].primary;
-  },
+  }
 
   getLabelAt(index) {
     this._checkIndexBounds(index);
     return JSON.stringify(this._popupLabels[index]);
-  },
+  }
 
   /**
    * Retrieves a comment (metadata instance)
    * @param   {number} index The index of the comment requested
    * @returns {string} The comment at the specified index
    */
   getCommentAt(index) {
     this._checkIndexBounds(index);
     return JSON.stringify(this._matchingProfiles[index]);
-  },
+  }
 
   /**
    * Retrieves a style hint specific to a particular index.
    * @param   {number} index The index of the style hint requested
    * @returns {string} The style hint at the specified index
    */
   getStyleAt(index) {
     this._checkIndexBounds(index);
     if (index == this.matchCount - 1) {
       return "autofill-footer";
     }
     return "autofill-profile";
-  },
+  }
 
   /**
    * Retrieves an image url.
    * @param   {number} index The index of the image url requested
    * @returns {string} The image url at the specified index
    */
   getImageAt(index) {
     this._checkIndexBounds(index);
     return "";
-  },
+  }
 
   /**
    * Retrieves a result
    * @param   {number} index The index of the result requested
    * @returns {string} The result at the specified index
    */
   getFinalCompleteValueAt(index) {
     return this.getValueAt(index);
-  },
+  }
 
   /**
    * Removes a result from the resultset
    * @param {number} index The index of the result to remove
    * @param {boolean} removeFromDatabase TRUE for removing data from DataBase
    *                                     as well.
    */
   removeValueAt(index, removeFromDatabase) {
     // There is no plan to support removing profiles via autocomplete.
-  },
-
-  // nsISupports
-  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult]),
-};
+  }
+}