Web Inspector: REGRESSION: no context menu items work when context menu clicking...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Base / SearchUtilities.js
1 /*
2  * Copyright (C) 2018 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.SearchUtilities = class SearchUtilities {
27     static get defaultSettings()
28     {
29         return {
30             caseSensitive: WI.settings.searchCaseSensitive,
31             regularExpression: WI.settings.searchRegularExpression,
32         };
33     }
34
35     static createSettings(namePrefix, options = {})
36     {
37         let settings = {};
38         for (let [key, defaultSetting] of Object.entries(WI.SearchUtilities.defaultSettings)) {
39             let setting = new WI.Setting(namePrefix + "-" + defaultSetting.name, defaultSetting.value);
40             defaultSetting.addEventListener(WI.Setting.Event.Changed, (event) => {
41                 setting.value = defaultSetting.value;
42             });
43             settings[key] = setting;
44
45             if (options.handleChanged)
46                 setting.addEventListener(WI.Setting.Event.Changed, options.handleChanged);
47         }
48         return settings;
49     }
50
51     static regExpForString(query, settings = {})
52     {
53         function checkSetting(setting) {
54             return setting instanceof WI.Setting ? setting.value : !!setting;
55         }
56
57         console.assert((typeof query === "string" && query) || query instanceof RegExp);
58
59         if (!checkSetting(settings.regularExpression))
60             query = simpleGlobStringToRegExp(String(query));
61
62         console.assert((typeof query === "string" && query) || query instanceof RegExp);
63
64         let flags = "g";
65         if (!checkSetting(settings.caseSensitive))
66             flags += "i";
67
68         return new RegExp(query, flags);
69     }
70
71     static createSettingsButton(settings)
72     {
73         console.assert(!isEmptyObject(settings));
74
75         let button = document.createElement("button");
76         button.classList.add("search-settings");
77         button.tabIndex = -1;
78         WI.addMouseDownContextMenuHandlers(button, (contextMenu) => {
79             if (settings.caseSensitive) {
80                 contextMenu.appendCheckboxItem(WI.UIString("Case Sensitive", "Case Sensitive @ Context Menu", "Context menu label for whether searches should be case sensitive."), () => {
81                     settings.caseSensitive.value = !settings.caseSensitive.value;
82                 }, settings.caseSensitive.value);
83             }
84
85             if (settings.regularExpression) {
86                 contextMenu.appendCheckboxItem(WI.UIString("Regular Expression", "Regular Expression @ Context Menu", "Context menu label for whether searches should be treated as regular expressions."), () => {
87                     settings.regularExpression.value = !settings.regularExpression.value;
88                 }, settings.regularExpression.value);
89             }
90         });
91
92         button.appendChild(WI.ImageUtilities.useSVGSymbol("Images/Gear.svg", "glyph"));
93
94         function toggleActive() {
95             button.classList.toggle("active", Object.values(settings).some((setting) => !!setting.value));
96         }
97         settings.caseSensitive.addEventListener(WI.Setting.Event.Changed, toggleActive);
98         settings.regularExpression.addEventListener(WI.Setting.Event.Changed, toggleActive);
99         toggleActive();
100
101         return button;
102     }
103 };