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