|
1 #ifdef 0 |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 #endif |
|
6 |
|
7 let gSearch = { |
|
8 |
|
9 currentEngineName: null, |
|
10 |
|
11 init: function () { |
|
12 for (let idSuffix of this._nodeIDSuffixes) { |
|
13 this._nodes[idSuffix] = |
|
14 document.getElementById("newtab-search-" + idSuffix); |
|
15 } |
|
16 |
|
17 window.addEventListener("ContentSearchService", this); |
|
18 this.setUpInitialState(); |
|
19 }, |
|
20 |
|
21 setUpInitialState: function () { |
|
22 this._send("GetState"); |
|
23 }, |
|
24 |
|
25 showPanel: function () { |
|
26 let panel = this._nodes.panel; |
|
27 let logo = this._nodes.logo; |
|
28 panel.openPopup(logo); |
|
29 logo.setAttribute("active", "true"); |
|
30 panel.addEventListener("popuphidden", function onHidden() { |
|
31 panel.removeEventListener("popuphidden", onHidden); |
|
32 logo.removeAttribute("active"); |
|
33 }); |
|
34 }, |
|
35 |
|
36 search: function (event) { |
|
37 event.preventDefault(); |
|
38 let searchStr = this._nodes.text.value; |
|
39 if (this.currentEngineName && searchStr.length) { |
|
40 this._send("Search", { |
|
41 engineName: this.currentEngineName, |
|
42 searchString: searchStr, |
|
43 whence: "newtab", |
|
44 }); |
|
45 } |
|
46 }, |
|
47 |
|
48 manageEngines: function () { |
|
49 this._nodes.panel.hidePopup(); |
|
50 this._send("ManageEngines"); |
|
51 }, |
|
52 |
|
53 setWidth: function (width) { |
|
54 this._nodes.form.style.width = width + "px"; |
|
55 this._nodes.form.style.maxWidth = width + "px"; |
|
56 }, |
|
57 |
|
58 handleEvent: function (event) { |
|
59 this["on" + event.detail.type](event.detail.data); |
|
60 }, |
|
61 |
|
62 onState: function (data) { |
|
63 this._makePanel(data.engines); |
|
64 this._setCurrentEngine(data.currentEngine); |
|
65 this._initWhenInitalStateReceived(); |
|
66 }, |
|
67 |
|
68 onCurrentEngine: function (engineName) { |
|
69 this._setCurrentEngine(engineName); |
|
70 }, |
|
71 |
|
72 _nodeIDSuffixes: [ |
|
73 "form", |
|
74 "logo", |
|
75 "manage", |
|
76 "panel", |
|
77 "text", |
|
78 ], |
|
79 |
|
80 _nodes: {}, |
|
81 |
|
82 _initWhenInitalStateReceived: function () { |
|
83 this._nodes.form.addEventListener("submit", e => this.search(e)); |
|
84 this._nodes.logo.addEventListener("click", e => this.showPanel()); |
|
85 this._nodes.manage.addEventListener("click", e => this.manageEngines()); |
|
86 this._initWhenInitalStateReceived = function () {}; |
|
87 }, |
|
88 |
|
89 _send: function (type, data=null) { |
|
90 window.dispatchEvent(new CustomEvent("ContentSearchClient", { |
|
91 detail: { |
|
92 type: type, |
|
93 data: data, |
|
94 }, |
|
95 })); |
|
96 }, |
|
97 |
|
98 _makePanel: function (engines) { |
|
99 let panel = this._nodes.panel; |
|
100 |
|
101 // Empty the panel except for the Manage Engines row. |
|
102 let i = 0; |
|
103 while (i < panel.childNodes.length) { |
|
104 let node = panel.childNodes[i]; |
|
105 if (node != this._nodes.manage) { |
|
106 panel.removeChild(node); |
|
107 } |
|
108 else { |
|
109 i++; |
|
110 } |
|
111 } |
|
112 |
|
113 // Add all the engines. |
|
114 for (let engine of engines) { |
|
115 panel.insertBefore(this._makePanelEngine(panel, engine), |
|
116 this._nodes.manage); |
|
117 } |
|
118 }, |
|
119 |
|
120 _makePanelEngine: function (panel, engine) { |
|
121 let box = document.createElementNS(XUL_NAMESPACE, "hbox"); |
|
122 box.className = "newtab-search-panel-engine"; |
|
123 box.setAttribute("engine", engine.name); |
|
124 |
|
125 box.addEventListener("click", () => { |
|
126 this._send("SetCurrentEngine", engine.name); |
|
127 panel.hidePopup(); |
|
128 this._nodes.text.focus(); |
|
129 }); |
|
130 |
|
131 let image = document.createElementNS(XUL_NAMESPACE, "image"); |
|
132 if (engine.iconURI) { |
|
133 image.setAttribute("src", engine.iconURI); |
|
134 } |
|
135 box.appendChild(image); |
|
136 |
|
137 let label = document.createElementNS(XUL_NAMESPACE, "label"); |
|
138 label.setAttribute("value", engine.name); |
|
139 box.appendChild(label); |
|
140 |
|
141 return box; |
|
142 }, |
|
143 |
|
144 _setCurrentEngine: function (engine) { |
|
145 this.currentEngineName = engine.name; |
|
146 |
|
147 // Set the logo. |
|
148 let logoURI = window.devicePixelRatio == 2 ? engine.logo2xURI : |
|
149 engine.logoURI; |
|
150 if (logoURI) { |
|
151 this._nodes.logo.hidden = false; |
|
152 this._nodes.logo.style.backgroundImage = "url(" + logoURI + ")"; |
|
153 this._nodes.text.placeholder = ""; |
|
154 } |
|
155 else { |
|
156 this._nodes.logo.hidden = true; |
|
157 this._nodes.text.placeholder = engine.name; |
|
158 } |
|
159 |
|
160 // Set the selected state of all the engines in the panel. |
|
161 for (let box of this._nodes.panel.childNodes) { |
|
162 if (box.getAttribute("engine") == engine.name) { |
|
163 box.setAttribute("selected", "true"); |
|
164 } |
|
165 else { |
|
166 box.removeAttribute("selected"); |
|
167 } |
|
168 } |
|
169 }, |
|
170 }; |