/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * 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/. */ "use strict"; const Cu = Components.utils; Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource:///modules/inspector.jsm"); Cu.import("resource:///modules/devtools/LayoutHelpers.jsm"); Cu.import("resource:///modules/devtools/CssLogic.jsm"); var EXPORTED_SYMBOLS = ["LayoutView"]; function LayoutView(aOptions) { this.chromeDoc = aOptions.document; this.inspector = aOptions.inspector; this.browser = this.inspector.chromeWindow.gBrowser; this.init(); } LayoutView.prototype = { init: function LV_init() { this.cssLogic = new CssLogic(); this.update = this.update.bind(this); this.onMessage = this.onMessage.bind(this); this.isOpen = false; this.documentReady = false; // Is the layout view was open before? if (!("_layoutViewIsOpen" in this.inspector)) { this.inspector._layoutViewIsOpen = Services.prefs.getBoolPref("devtools.layoutview.open"); } // We update the values when: // a node is locked // we get the MozAfterPaint event and the node is locked function onSelect() { if (this.inspector.locked) { this.cssLogic.highlight(this.inspector.selection); this.undim(); this.update(); // We make sure we never add 2 listeners. if (!this.trackingPaint) { this.browser.addEventListener("MozAfterPaint", this.update, true); this.trackingPaint = true; } } } function onUnlock() { this.browser.removeEventListener("MozAfterPaint", this.update, true); this.trackingPaint = false; this.dim(); } this.onSelect= onSelect.bind(this); this.onUnlock = onUnlock.bind(this); this.inspector.on("select", this.onSelect); this.inspector.on("unlocked", this.onUnlock); // Build the layout view in the sidebar. this.buildView(); this.bound_handleKeypress = this.handleKeypress.bind(this); this.iframe.addEventListener("keypress", this.bound_handleKeypress, true); // Get messages from the iframe. this.inspector.chromeWindow.addEventListener("message", this.onMessage, true); // Store for the different dimensions of the node. // 'selector' refers to the element that holds the value in view.xhtml; // 'property' is what we are measuring; // 'value' is the computed dimension, computed in update(). this.map = { marginTop: {selector: ".margin.top > span", property: "margin-top", value: undefined}, marginBottom: {selector: ".margin.bottom > span", property: "margin-bottom", value: undefined}, marginLeft: {selector: ".margin.left > span", property: "margin-left", value: undefined}, marginRight: {selector: ".margin.right > span", property: "margin-right", value: undefined}, paddingTop: {selector: ".padding.top > span", property: "padding-top", value: undefined}, paddingBottom: {selector: ".padding.bottom > span", property: "padding-bottom", value: undefined}, paddingLeft: {selector: ".padding.left > span", property: "padding-left", value: undefined}, paddingRight: {selector: ".padding.right > span", property: "padding-right", value: undefined}, borderTop: {selector: ".border.top > span", property: "border-top-width", value: undefined}, borderBottom: {selector: ".border.bottom > span", property: "border-bottom-width", value: undefined}, borderLeft: {selector: ".border.left > span", property: "border-left-width", value: undefined}, borderRight: {selector: ".border.right > span", property: "border-right-width", value: undefined}, }; }, /** * Destroy the nodes. Remove listeners. */ destroy: function LV_destroy() { this.inspector.off("select", this.onSelect); this.inspector.off("unlocked", this.onUnlock); this.browser.removeEventListener("MozAfterPaint", this.update, true); this.iframe.removeEventListener("keypress", this.bound_handleKeypress, true); this.inspector.chromeWindow.removeEventListener("message", this.onMessage, true); this.close(); this.iframe = null; this.view.parentNode.removeChild(this.view); }, /** * Build the Layout container: * * *