Bug 1578148 - Fix various problems in tag add/edit. r=aceman DONTBUILD BETA_BASE_20190902
authorJorg K <jorgk@jorgk.com>
Mon, 02 Sep 2019 20:47:49 +0200
changeset 35725 86e0e08f619fc722527b1ec8140ab65dc15671e0
parent 35724 10403dd296bdc6230e419a1f2cc42344d4f8df55
child 35726 4eb5a53e294be47e6051f54ef2ed90fe03b57fd7
push id2486
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:24:43 +0000
treeherdercomm-beta@1b30a9a6e7f6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaceman
bugs1578148
Bug 1578148 - Fix various problems in tag add/edit. r=aceman DONTBUILD
mail/base/content/newTagDialog.js
mail/components/preferences/general.js
--- a/mail/base/content/newTagDialog.js
+++ b/mail/base/content/newTagDialog.js
@@ -4,18 +4,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 var { MailServices } = ChromeUtils.import(
   "resource:///modules/MailServices.jsm"
 );
 
-document.addEventListener("dialogaccept", onOKNewTag);
-
 var dialog;
 
 /**
  * Pass in keyToEdit as a window argument to turn this dialog into an edit
  * tag dialog.
  */
 function onLoad() {
   let windowArgs = window.arguments[0];
@@ -25,67 +23,67 @@ function onLoad() {
   dialog.OKButton = document.documentElement.getButton("accept");
   dialog.nameField = document.getElementById("name");
   dialog.nameField.focus();
 
   // call this when OK is pressed
   dialog.okCallback = windowArgs.okCallback;
   if (windowArgs.keyToEdit) {
     initializeForEditing(windowArgs.keyToEdit);
+    document.addEventListener("dialogaccept", event => onOKEditTag(event));
+  } else {
+    document.addEventListener("dialogaccept", event => onOKNewTag(event));
   }
 
   doEnabling();
 }
 
 /**
  * Turn the new tag dialog into an edit existing tag dialog
  */
 function initializeForEditing(aTagKey) {
   dialog.editTagKey = aTagKey;
 
   // Change the title of the dialog
   var messengerBundle = document.getElementById("bundle_messenger");
   document.title = messengerBundle.getString("editTagTitle");
 
-  // override the OK button
-  document.documentElement.setAttribute(
-    "ondialogaccept",
-    "return onOKEditTag();"
-  );
-
   // extract the color and name for the current tag
   document.getElementById(
     "tagColorPicker"
   ).value = MailServices.tags.getColorForKey(aTagKey);
   dialog.nameField.value = MailServices.tags.getTagForKey(aTagKey);
 }
 
 /**
  * on OK handler for editing a new tag.
  */
-function onOKEditTag() {
+function onOKEditTag(event) {
   // get the tag name of the current key we are editing
   let existingTagName = MailServices.tags.getTagForKey(dialog.editTagKey);
 
   // it's ok if the name didn't change
   if (existingTagName != dialog.nameField.value) {
     // don't let the user edit a tag to the name of another existing tag
     if (MailServices.tags.getKeyForTag(dialog.nameField.value)) {
       alertForExistingTag();
-      return false; // abort the OK
+      event.preventDefault();
+      return;
     }
 
     MailServices.tags.setTagForKey(dialog.editTagKey, dialog.nameField.value);
   }
 
   MailServices.tags.setColorForKey(
     dialog.editTagKey,
     document.getElementById("tagColorPicker").value
   );
-  return dialog.okCallback();
+  if (!dialog.okCallback()) {
+    event.preventDefault();
+  }
 }
 
 /**
  * on OK handler for creating a new tag. Alerts the user if a tag with
  * the name already exists.
  */
 function onOKNewTag(event) {
   var name = dialog.nameField.value;
--- a/mail/components/preferences/general.js
+++ b/mail/components/preferences/general.js
@@ -2718,36 +2718,40 @@ function addTagCallback(aName, aColor) {
   let key = MailServices.tags.getKeyForTag(aName);
   TagUtils.addTagToAllDocumentSheets(key, aColor);
 
   var item = gGeneralPane.appendTagItem(aName, key, aColor);
   var tagListBox = document.getElementById("tagList");
   tagListBox.ensureElementIsVisible(item);
   tagListBox.selectItem(item);
   tagListBox.focus();
+  return true;
 }
 
 function editTagCallback() {
   // update the values of the selected item
   let tagListEl = document.getElementById("tagList");
   let index = tagListEl.selectedIndex;
   if (index < 0) {
-    return;
+    return false;
   }
 
   let tagElToEdit = tagListEl.getItemAtIndex(index);
   let key = tagElToEdit.getAttribute("value");
   let color = MailServices.tags.getColorForKey(key);
   // update the color and label elements
-  tagElToEdit.setAttribute("label", MailServices.tags.getTagForKey(key));
+  tagElToEdit
+    .querySelector("label")
+    .setAttribute("value", MailServices.tags.getTagForKey(key));
   tagElToEdit.style.color = color;
 
   // Add to style sheet. We simply add the new color, the rule is added at the
   // end and will overrule the previous rule.
   TagUtils.addTagToAllDocumentSheets(key, color);
+  return true;
 }
 
 Preferences.get("mailnews.start_page.enabled").on(
   "change",
   gGeneralPane.updateStartPage
 );
 Preferences.get("font.language.group").on("change", gGeneralPane._rebuildFonts);
 Preferences.get("mailnews.mark_message_read.auto").on(