summaryrefslogtreecommitdiff
path: root/Source/WebInspectorUI/UserInterface/HierarchicalPathComponent.js
blob: 33286840a61e4e877317221e32031cc7b1a76b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

WebInspector.HierarchicalPathComponent = function(displayName, styleClassNames, representedObject, textOnly, showSelectorArrows)
{
    WebInspector.Object.call(this);

    console.assert(displayName);
    console.assert(styleClassNames);

    this._representedObject = representedObject || null;

    this._element = document.createElement("div");
    this._element.className = WebInspector.HierarchicalPathComponent.StyleClassName;

    if (!(styleClassNames instanceof Array))
        styleClassNames = [styleClassNames];

    for (var i = 0; i < styleClassNames.length; ++i) {
        if (!styleClassNames[i])
            continue;
        this._element.classList.add(styleClassNames[i]);
    }

    if (!textOnly) {
        this._iconElement = document.createElement("img");
        this._iconElement.className = WebInspector.HierarchicalPathComponent.IconElementStyleClassName;
        this._element.appendChild(this._iconElement);
    } else
        this._element.classList.add(WebInspector.HierarchicalPathComponent.TextOnlyStyleClassName);

    this._titleElement = document.createElement("div");
    this._titleElement.className = WebInspector.HierarchicalPathComponent.TitleElementStyleClassName;
    this._element.appendChild(this._titleElement);

    this._titleContentElement = document.createElement("div");
    this._titleContentElement.className = WebInspector.HierarchicalPathComponent.TitleContentElementStyleClassName;
    this._titleElement.appendChild(this._titleContentElement);

    this._separatorElement = document.createElement("div");
    this._separatorElement.className = WebInspector.HierarchicalPathComponent.SeparatorElementStyleClassName;
    this._element.appendChild(this._separatorElement);

    this._selectElement = document.createElement("select");
    this._selectElement.addEventListener("mouseover", this._selectElementMouseOver.bind(this));
    this._selectElement.addEventListener("mouseout", this._selectElementMouseOut.bind(this));
    this._selectElement.addEventListener("mousedown", this._selectElementMouseDown.bind(this));
    this._selectElement.addEventListener("mouseup", this._selectElementMouseUp.bind(this));
    this._selectElement.addEventListener("change", this._selectElementSelectionChanged.bind(this));
    this._element.appendChild(this._selectElement);

    this._previousSibling = null;
    this._nextSibling = null;

    this._truncatedDisplayNameLength = 0;

    this.selectorArrows = showSelectorArrows;
    this.displayName = displayName;
};

WebInspector.HierarchicalPathComponent.StyleClassName = "hierarchical-path-component";
WebInspector.HierarchicalPathComponent.HiddenStyleClassName = "hidden";
WebInspector.HierarchicalPathComponent.CollapsedStyleClassName = "collapsed";
WebInspector.HierarchicalPathComponent.IconElementStyleClassName = "icon";
WebInspector.HierarchicalPathComponent.TextOnlyStyleClassName = "text-only";
WebInspector.HierarchicalPathComponent.ShowSelectorArrowsStyleClassName = "show-selector-arrows";
WebInspector.HierarchicalPathComponent.TitleElementStyleClassName = "title";
WebInspector.HierarchicalPathComponent.TitleContentElementStyleClassName = "content";
WebInspector.HierarchicalPathComponent.SelectorArrowsElementStyleClassName = "selector-arrows";
WebInspector.HierarchicalPathComponent.SeparatorElementStyleClassName = "separator";

WebInspector.HierarchicalPathComponent.MinimumWidth = 32;
WebInspector.HierarchicalPathComponent.MinimumWidthCollapsed = 24;
WebInspector.HierarchicalPathComponent.MinimumWidthForOneCharacterTruncatedTitle = 54;
WebInspector.HierarchicalPathComponent.SelectorArrowsWidth = 12;

WebInspector.HierarchicalPathComponent.Event = {
    SiblingWasSelected: "hierarchical-path-component-sibling-was-selected",
    Clicked: "hierarchical-path-component-clicked"
};

WebInspector.HierarchicalPathComponent.prototype = {
    constructor: WebInspector.HierarchicalPathComponent,

    // Public

    get element()
    {
        return this._element;
    },

    get representedObject()
    {
        return this._representedObject;
    },

    get displayName()
    {
        return this._displayName;
    },

    set displayName(newDisplayName)
    {
        console.assert(newDisplayName);
        if (newDisplayName === this._displayName)
            return;

        this._displayName = newDisplayName;

        this._updateElementTitleAndText();
    },

    get truncatedDisplayNameLength()
    {
        return this._truncatedDisplayNameLength;
    },

    set truncatedDisplayNameLength(truncatedDisplayNameLength)
    {
        truncatedDisplayNameLength = truncatedDisplayNameLength || 0;

        if (truncatedDisplayNameLength === this._truncatedDisplayNameLength)
            return;

        this._truncatedDisplayNameLength = truncatedDisplayNameLength;

        this._updateElementTitleAndText();
    },

    get minimumWidth()
    {
        if (this.collapsed)
            return WebInspector.HierarchicalPathComponent.MinimumWidthCollapsed;
        if (this.selectorArrows)
            return WebInspector.HierarchicalPathComponent.MinimumWidth + WebInspector.HierarchicalPathComponent.SelectorArrowsWidth;
        return WebInspector.HierarchicalPathComponent.MinimumWidth;
    },

    get forcedWidth()
    {
        var maxWidth = this._element.style.getProperty("width");
        if (typeof maxWidth === "string")
            return parseInt(maxWidth);
        return null;
    },

    set forcedWidth(width)
    {
        if (typeof width === "number") {
            var minimumWidthForOneCharacterTruncatedTitle = WebInspector.HierarchicalPathComponent.MinimumWidthForOneCharacterTruncatedTitle;
            if (this.selectorArrows)
                minimumWidthForOneCharacterTruncatedTitle += WebInspector.HierarchicalPathComponent.SelectorArrowsWidth;

            // If the width is less than the minimum width required to show a single character and ellipsis, then
            // just collapse down to the bare minimum to show only the icon.
            if (width < minimumWidthForOneCharacterTruncatedTitle)
                width = 0;

            // Ensure the width does not go less than 1px. If the width is 0 the layout gets funky. There is a min-width
            // in the CSS too, so as long the width is less than min-width we get the desired effect of only showing the icon.
            this._element.style.setProperty("width", Math.max(1, width) + "px");
        } else
            this._element.style.removeProperty("width");
    },

    get hidden()
    {
        return this._element.classList.contains(WebInspector.HierarchicalPathComponent.HiddenStyleClassName);
    },

    set hidden(flag)
    {
        if (flag)
            this._element.classList.add(WebInspector.HierarchicalPathComponent.HiddenStyleClassName);
        else
            this._element.classList.remove(WebInspector.HierarchicalPathComponent.HiddenStyleClassName);
    },

    get collapsed()
    {
        return this._element.classList.contains(WebInspector.HierarchicalPathComponent.CollapsedStyleClassName);
    },

    set collapsed(flag)
    {
        if (flag)
            this._element.classList.add(WebInspector.HierarchicalPathComponent.CollapsedStyleClassName);
        else
            this._element.classList.remove(WebInspector.HierarchicalPathComponent.CollapsedStyleClassName);
    },

    get selectorArrows()
    {
        return this._element.classList.contains(WebInspector.HierarchicalPathComponent.ShowSelectorArrowsStyleClassName);
    },

    set selectorArrows(flag)
    {
        if (flag) {
            this._selectorArrowsElement = document.createElement("img");
            this._selectorArrowsElement.className = WebInspector.HierarchicalPathComponent.SelectorArrowsElementStyleClassName;
            this._element.insertBefore(this._selectorArrowsElement, this._separatorElement);

            this._element.classList.add(WebInspector.HierarchicalPathComponent.ShowSelectorArrowsStyleClassName);
        } else {
            if (this._selectorArrowsElement) {
                this._selectorArrowsElement.remove();
                delete this._selectorArrowsElement;
            }

            this._element.classList.remove(WebInspector.HierarchicalPathComponent.ShowSelectorArrowsStyleClassName);
        }
    },

    get previousSibling()
    {
        return this._previousSibling;
    },

    set previousSibling(newSlibling)
    {
        this._previousSibling = newSlibling || null;
    },

    get nextSibling()
    {
        return this._nextSibling;
    },

    set nextSibling(newSlibling)
    {
        this._nextSibling = newSlibling || null;
    },

    // Private

    _updateElementTitleAndText: function()
    {
        var truncatedDisplayName = this._displayName;
        if (this._truncatedDisplayNameLength && truncatedDisplayName.length > this._truncatedDisplayNameLength)
            truncatedDisplayName = truncatedDisplayName.substring(0, this._truncatedDisplayNameLength) + "\u2026";

        this._element.title = this._displayName;
        this._titleContentElement.textContent = truncatedDisplayName;
    },

    _updateSelectElement: function()
    {
        this._selectElement.removeChildren();

        function createOption(component)
        {
            var optionElement = document.createElement("option");
            const maxPopupMenuLength = 130; // <rdar://problem/13445374> <select> with very long option has clipped text and popup menu is still very wide
            optionElement.textContent = component.displayName.length <= maxPopupMenuLength ? component.displayName : component.displayName.substring(0, maxPopupMenuLength) + "\u2026";
            optionElement._pathComponent = component;
            return optionElement;
        }

        var previousSiblingCount = 0;
        var sibling = this.previousSibling;
        while (sibling) {
            this._selectElement.insertBefore(createOption(sibling), this._selectElement.firstChild);
            sibling = sibling.previousSibling;
            ++previousSiblingCount;
        }

        this._selectElement.appendChild(createOption(this));

        sibling = this.nextSibling;
        while (sibling) {
            this._selectElement.appendChild(createOption(sibling));
            sibling = sibling.nextSibling;
        }

        // Since the change event only fires when the selection actually changes we are
        // stuck with either not showing the current selection in the menu or accepting that
        // the user can't select what is already selected again. Selecting the same item
        // again can be desired (for selecting the main resource while looking at an image).
        // So if there is only one option, don't make it be selected by default. This lets
        // you select the top level item which usually has no siblings to go back.
        // FIXME: Make this work when there are multiple options with a selectedIndex.
        if (this._selectElement.options.length === 1)
            this._selectElement.selectedIndex = -1;
        else
            this._selectElement.selectedIndex = previousSiblingCount;
    },

    _selectElementMouseOver: function(event)
    {
        if (typeof this.mouseOver === "function")
            this.mouseOver();
    },

    _selectElementMouseOut: function(event)
    {
        if (typeof this.mouseOut === "function")
            this.mouseOut();
    },

    _selectElementMouseDown: function(event)
    {
        this._updateSelectElement();
    },

    _selectElementMouseUp: function(event)
    {
        this.dispatchEventToListeners(WebInspector.HierarchicalPathComponent.Event.Clicked);
    },

    _selectElementSelectionChanged: function(event)
    {
        this.dispatchEventToListeners(WebInspector.HierarchicalPathComponent.Event.SiblingWasSelected, {pathComponent: this._selectElement[this._selectElement.selectedIndex]._pathComponent});
    }
};

WebInspector.HierarchicalPathComponent.prototype.__proto__ = WebInspector.Object.prototype;