Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / elements / SharedSidebarModel.js
blobd8606f2d736e375079091eff4f93ecd3dc1a13d6
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.
5 /**
6  * @extends {WebInspector.Object}
7  * @constructor
8  */
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);
16 /**
17  * @param {?WebInspector.DOMNode} node
18  * @return {?WebInspector.DOMNode}
19  */
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)
26         node = null;
27     return node;
30 WebInspector.SharedSidebarModel.Events = {
31     ComputedStyleChanged: "ComputedStyleChanged"
34 WebInspector.SharedSidebarModel.prototype = {
35     /**
36      * @return {?WebInspector.DOMNode}
37      */
38     node: function()
39     {
40         return this._node;
41     },
43     /**
44      * @return {?WebInspector.CSSStyleModel}
45      */
46     cssModel: function()
47     {
48         return this._cssModel;
49     },
51     /**
52      * @param {!WebInspector.Event} event
53      */
54     _onNodeChanged: function(event)
55     {
56         this._node = /** @type {?WebInspector.DOMNode} */(event.data);
57         this._updateTarget(this._node ? this._node.target() : null);
58         this._onComputedStyleChanged();
59     },
61     /**
62      * @param {?WebInspector.Target} target
63      */
64     _updateTarget: function(target)
65     {
66         if (this._target === target)
67             return;
68         if (this._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);
76         }
77         this._target = target;
78         if (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);
88         }
89     },
91     /**
92      * @return {?WebInspector.DOMNode}
93      */
94     _elementNode: function()
95     {
96         return WebInspector.SharedSidebarModel.elementNode(this.node());
97     },
99     /**
100      * @return {!Promise.<?WebInspector.SharedSidebarModel.ComputedStyle>}
101      */
102     fetchComputedStyle: function()
103     {
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;
114         /**
115          * @param {!WebInspector.DOMNode} elementNode
116          * @param {?Map.<string, string>} style
117          * @return {?WebInspector.SharedSidebarModel.ComputedStyle}
118          * @this {WebInspector.SharedSidebarModel}
119          */
120         function verifyOutdated(elementNode, style)
121         {
122             return elementNode === this._elementNode() && style ? new WebInspector.SharedSidebarModel.ComputedStyle(elementNode, style) : /** @type {?WebInspector.SharedSidebarModel.ComputedStyle} */(null);
123         }
124     },
126     _onComputedStyleChanged: function()
127     {
128         delete this._computedStylePromise;
129         this.dispatchEventToListeners(WebInspector.SharedSidebarModel.Events.ComputedStyleChanged);
130     },
132     __proto__: WebInspector.Object.prototype
136  * @constructor
137  * @param {!WebInspector.DOMNode} node
138  * @param {!Map.<string, string>} computedStyle
139  */
140 WebInspector.SharedSidebarModel.ComputedStyle = function(node, computedStyle)
142     this.node = node;
143     this.computedStyle = computedStyle;