Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / elements / PlatformFontsSidebarPane.js
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.ElementsSidebarPane}
34  */
35 WebInspector.PlatformFontsSidebarPane = function()
36 {
37     WebInspector.ElementsSidebarPane.call(this, WebInspector.UIString("Fonts"));
38     this.element.classList.add("platform-fonts");
39
40     this._sectionTitle = createElementWithClass("div", "sidebar-separator");
41     this.element.insertBefore(this._sectionTitle, this.bodyElement);
42     this._sectionTitle.textContent = WebInspector.UIString("Rendered Fonts");
43     this._fontStatsSection = this.bodyElement.createChild("div", "stats-section");
44 }
45
46 WebInspector.PlatformFontsSidebarPane.prototype = {
47     /**
48      * @param {?WebInspector.DOMNode} node
49      */
50     setNode: function(node)
51     {
52         WebInspector.ElementsSidebarPane.prototype.setNode.call(this, node);
53         this._updateTarget(node.target());
54     },
55
56     /**
57      * @param {!WebInspector.Target} target
58      */
59     _updateTarget: function(target)
60     {
61         if (this._target === target)
62             return;
63         if (this._target) {
64             this._target.cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this.update, this);
65             this._target.cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetRemoved, this.update, this);
66             this._target.cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetChanged, this.update, this);
67             this._target.cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.MediaQueryResultChanged, this.update, this);
68             this._target.domModel.removeEventListener(WebInspector.DOMModel.Events.AttrModified, this.update, this);
69             this._target.domModel.removeEventListener(WebInspector.DOMModel.Events.AttrRemoved, this.update, this);
70             this._target.domModel.removeEventListener(WebInspector.DOMModel.Events.CharacterDataModified, this.update, this);
71         }
72         this._target = target;
73         this._target.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this.update, this);
74         this._target.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetRemoved, this.update, this);
75         this._target.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetChanged, this.update, this);
76         this._target.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.MediaQueryResultChanged, this.update, this);
77         this._target.domModel.addEventListener(WebInspector.DOMModel.Events.AttrModified, this.update, this);
78         this._target.domModel.addEventListener(WebInspector.DOMModel.Events.AttrRemoved, this.update, this);
79         this._target.domModel.addEventListener(WebInspector.DOMModel.Events.CharacterDataModified, this.update, this);
80     },
81
82     /**
83      * @param {!WebInspector.Throttler.FinishCallback} finishedCallback
84      * @protected
85      */
86     doUpdate: function(finishedCallback)
87     {
88         if (!this.node())
89             return;
90         this._target.cssModel.getPlatformFontsForNode(this.node().id, this._refreshUI.bind(this, /** @type {!WebInspector.DOMNode} */ (this.node()), finishedCallback));
91     },
92
93     /**
94      * @param {!WebInspector.DOMNode} node
95      * @param {!WebInspector.Throttler.FinishCallback} finishedCallback
96      * @param {?string} cssFamilyName
97      * @param {?Array.<!CSSAgent.PlatformFontUsage>} platformFonts
98      */
99     _refreshUI: function(node, finishedCallback, cssFamilyName, platformFonts)
100     {
101         if (this.node() !== node) {
102             finishedCallback();
103             return;
104         }
105
106         this._fontStatsSection.removeChildren();
107
108         var isEmptySection = !platformFonts || !platformFonts.length;
109         this._sectionTitle.classList.toggle("hidden", isEmptySection);
110         if (isEmptySection) {
111             finishedCallback();
112             return;
113         }
114         platformFonts.sort(function (a, b) {
115             return b.glyphCount - a.glyphCount;
116         });
117         for (var i = 0; i < platformFonts.length; ++i) {
118             var fontStatElement = this._fontStatsSection.createChild("div", "font-stats-item");
119
120             var fontNameElement = fontStatElement.createChild("span", "font-name");
121             fontNameElement.textContent = platformFonts[i].familyName;
122
123             var fontDelimeterElement = fontStatElement.createChild("span", "delimeter");
124             fontDelimeterElement.textContent = "\u2014";
125
126             var fontUsageElement = fontStatElement.createChild("span", "font-usage");
127             var usage = platformFonts[i].glyphCount;
128             fontUsageElement.textContent = usage === 1 ? WebInspector.UIString("%d glyph", usage) : WebInspector.UIString("%d glyphs", usage);
129         }
130         finishedCallback();
131     },
132
133     __proto__: WebInspector.ElementsSidebarPane.prototype
134 }