Bug 1456680 - Part 7: Make the grid display markup badge able to toggle on and off the grid highlighter. r=pbro draft
authorGabriel Luong <gabriel.luong@gmail.com>
Sun, 29 Apr 2018 21:51:07 -0400
changeset 789667 6388d7ca18ed2c6200502162bb4588861a76919a
parent 789666 080f13e4edbfa28883d27fc4f0666e4912e8d60d
push id108295
push userbmo:gl@mozilla.com
push dateMon, 30 Apr 2018 01:53:22 +0000
reviewerspbro
bugs1456680
milestone61.0a1
Bug 1456680 - Part 7: Make the grid display markup badge able to toggle on and off the grid highlighter. r=pbro MozReview-Commit-ID: AYoAi16ZM45
devtools/client/inspector/markup/views/element-editor.js
--- a/devtools/client/inspector/markup/views/element-editor.js
+++ b/devtools/client/inspector/markup/views/element-editor.js
@@ -54,44 +54,48 @@ const DISPLAY_TYPES = {
  * @param  {Element} node
  *         The node being edited.
  */
 function ElementEditor(container, node) {
   this.container = container;
   this.node = node;
   this.markup = this.container.markup;
   this.highlighters = this.markup.highlighters;
+  this.inspector = this.markup.inspector;
   this.doc = this.markup.doc;
   this._cssProperties = getCssProperties(this.markup.toolbox);
 
   this.attrElements = new Map();
   this.animationTimers = {};
 
   this.elt = null;
   this.tag = null;
   this.closeTag = null;
   this.attrList = null;
   this.newAttr = null;
   this.closeElt = null;
 
+  this.onDisplayBadgeClick = this.onDisplayBadgeClick.bind(this);
+  this.onTagEditor = this.onTagEdit.bind(this);
+
   // Create the main editor
   this.buildMarkup();
 
   // Make the tag name editable (unless this is a remote node or
   // a document element)
   if (!node.isDocumentElement) {
     // Make the tag optionally tabbable but not by default.
     this.tag.setAttribute("tabindex", "-1");
     editableField({
       element: this.tag,
       multiline: true,
       maxWidth: () => getAutocompleteMaxWidth(this.tag, this.container.elt),
       trigger: "dblclick",
       stopOnReturn: true,
-      done: this.onTagEdit.bind(this),
+      done: this.onTagEdit,
       cssProperties: this._cssProperties
     });
   }
 
   // Make the new attribute space editable.
   this.newAttr.editMode = editableField({
     element: this.newAttr,
     multiline: true,
@@ -175,16 +179,17 @@ ElementEditor.prototype = {
     this.eventBadge.classList.add("markup-badge");
     this.eventBadge.dataset.event = "true";
     this.eventBadge.textContent = "event";
     this.eventBadge.title = INSPECTOR_L10N.getStr("markupView.event.tooltiptext");
     this.elt.appendChild(this.eventBadge);
 
     this.displayBadge = this.doc.createElement("div");
     this.displayBadge.classList.add("markup-badge");
+    this.displayBadge.addEventListener("click", this.onDisplayBadgeClick);
     this.elt.appendChild(this.displayBadge);
   },
 
   set selected(value) {
     if (this.textEditor) {
       this.textEditor.selected = value;
     }
   },
@@ -269,34 +274,39 @@ ElementEditor.prototype = {
           this.flashAttribute(attr.name);
         }
       }
     }
 
     // Update the event markup badge
     this.eventBadge.style.display = this.node.hasEventListeners ? "inline-block" : "none";
 
-    // Update the display markup badge
-    let showDisplayBadge = this.node.displayType in DISPLAY_TYPES;
+    this.updateDisplayBadge();
+    this.updateTextEditor();
+  },
+
+  /**
+   * Update the markup display badge.
+   */
+  updateDisplayBadge: function() {
+    const showDisplayBadge = this.node.displayType in DISPLAY_TYPES;
     this.displayBadge.textContent = this.node.displayType;
     this.displayBadge.dataset.display = this.node.displayType;
     this.displayBadge.style.display = showDisplayBadge ? "inline-block" : "none";
     this.displayBadge.title = showDisplayBadge ?
       DISPLAY_TYPES[this.node.displayType] : "";
 
     if (this.highlighters.gridHighlighterShown === this.node) {
       this.displayBadge.classList.add("active");
 
       // Store the MarkupElementContainer of the highlighted grid.
       this.markup.highlightedGridContainer = this.container;
     } else {
       this.displayBadge.classList.remove("active");
     }
-
-    this.updateTextEditor();
   },
 
   /**
    * Update the inline text editor in case of a single text child node.
    */
   updateTextEditor: function() {
     let node = this.node.inlineTextChild;
 
@@ -631,16 +641,28 @@ ElementEditor.prototype = {
       this.markup.emit("refocusedonedit");
     };
 
     // Start listening for mutations until we find an attributes change
     // that modifies this attribute.
     this.markup.inspector.once("markupmutation", onMutations);
   },
 
+  onDisplayBadgeClick: function(event) {
+    event.stopPropagation();
+
+    const target = event.target;
+
+    if (target.dataset.display !== "grid") {
+      return;
+    }
+
+    this.highlighters.toggleGridHighlighter(this.inspector.selection.nodeFront, "markup");
+  },
+
   /**
    * Called when the tag name editor has is done editing.
    */
   onTagEdit: function(newTagName, isCommit) {
     if (!isCommit ||
         newTagName.toLowerCase() === this.node.tagName.toLowerCase() ||
         !("editTagName" in this.markup.walker)) {
       return;
@@ -655,12 +677,14 @@ ElementEditor.prototype = {
     });
   },
 
   destroy: function() {
     for (let key in this.animationTimers) {
       clearTimeout(this.animationTimers[key]);
     }
     this.animationTimers = null;
+
+    this.displayBadge.removeEventListener("click", this.onDisplayBadgeClick);
   }
 };
 
 module.exports = ElementEditor;