1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @extends {WebInspector.Object}
9 WebInspector.SharedSidebarModel = function()
11 WebInspector.Object.call(this);
12 this._node = WebInspector.context.flavor(WebInspector.DOMNode);
13 WebInspector.context.addFlavorChangeListener(WebInspector.DOMNode, this._onNodeChanged, this);
17 * @param {?WebInspector.DOMNode} node
18 * @return {?WebInspector.DOMNode}
20 WebInspector.SharedSidebarModel.elementNode = function(node)
22 if (node && node.nodeType() === Node.TEXT_NODE && node.parentNode)
23 node = node.parentNode;
25 if (node && node.nodeType() !== Node.ELEMENT_NODE)
30 WebInspector.SharedSidebarModel.Events = {
31 ComputedStyleChanged: "ComputedStyleChanged"
34 WebInspector.SharedSidebarModel.prototype = {
36 * @return {?WebInspector.DOMNode}
44 * @return {?WebInspector.CSSStyleModel}
48 return this._cssModel;
52 * @param {!WebInspector.Event} event
54 _onNodeChanged: function(event)
56 this._node = /** @type {?WebInspector.DOMNode} */(event.data);
57 this._updateTarget(this._node ? this._node.target() : null);
58 this._onComputedStyleChanged();
62 * @param {?WebInspector.Target} target
64 _updateTarget: function(target)
66 if (this._target === target)
69 this._cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this._onComputedStyleChanged, this);
70 this._cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetRemoved, this._onComputedStyleChanged, this);
71 this._cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetChanged, this._onComputedStyleChanged, this);
72 this._cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.MediaQueryResultChanged, this._onComputedStyleChanged, this);
73 this._cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.PseudoStateForced, this._onComputedStyleChanged, this);
74 this._cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.ModelWasEnabled, this._onComputedStyleChanged, this);
75 this._domModel.removeEventListener(WebInspector.DOMModel.Events.DOMMutated, this._onComputedStyleChanged, this);
77 this._target = target;
79 this._cssModel = WebInspector.CSSStyleModel.fromTarget(target);
80 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this._onComputedStyleChanged, this);
81 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetRemoved, this._onComputedStyleChanged, this);
82 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetChanged, this._onComputedStyleChanged, this);
83 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.MediaQueryResultChanged, this._onComputedStyleChanged, this);
84 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.PseudoStateForced, this._onComputedStyleChanged, this);
85 this._cssModel.addEventListener(WebInspector.CSSStyleModel.Events.ModelWasEnabled, this._onComputedStyleChanged, this);
86 this._domModel = WebInspector.DOMModel.fromTarget(target);
87 this._domModel.addEventListener(WebInspector.DOMModel.Events.DOMMutated, this._onComputedStyleChanged, this);
92 * @return {?WebInspector.DOMNode}
94 _elementNode: function()
96 return WebInspector.SharedSidebarModel.elementNode(this.node());
100 * @return {!Promise.<?WebInspector.SharedSidebarModel.ComputedStyle>}
102 fetchComputedStyle: function()
104 var elementNode = this._elementNode();
105 var cssModel = this.cssModel();
106 if (!elementNode || !cssModel)
107 return Promise.resolve(/** @type {?WebInspector.SharedSidebarModel.ComputedStyle} */(null));
109 if (!this._computedStylePromise)
110 this._computedStylePromise = cssModel.computedStylePromise(elementNode.id).then(verifyOutdated.bind(this, elementNode));
112 return this._computedStylePromise;
115 * @param {!WebInspector.DOMNode} elementNode
116 * @param {?Map.<string, string>} style
117 * @return {?WebInspector.SharedSidebarModel.ComputedStyle}
118 * @this {WebInspector.SharedSidebarModel}
120 function verifyOutdated(elementNode, style)
122 return elementNode === this._elementNode() && style ? new WebInspector.SharedSidebarModel.ComputedStyle(elementNode, style) : /** @type {?WebInspector.SharedSidebarModel.ComputedStyle} */(null);
126 _onComputedStyleChanged: function()
128 delete this._computedStylePromise;
129 this.dispatchEventToListeners(WebInspector.SharedSidebarModel.Events.ComputedStyleChanged);
132 __proto__: WebInspector.Object.prototype
137 * @param {!WebInspector.DOMNode} node
138 * @param {!Map.<string, string>} computedStyle
140 WebInspector.SharedSidebarModel.ComputedStyle = function(node, computedStyle)
143 this.computedStyle = computedStyle;