browser/base/content/newtab/search.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/newtab/search.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +#ifdef 0
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +#endif
     1.9 +
    1.10 +let gSearch = {
    1.11 +
    1.12 +  currentEngineName: null,
    1.13 +
    1.14 +  init: function () {
    1.15 +    for (let idSuffix of this._nodeIDSuffixes) {
    1.16 +      this._nodes[idSuffix] =
    1.17 +        document.getElementById("newtab-search-" + idSuffix);
    1.18 +    }
    1.19 +
    1.20 +    window.addEventListener("ContentSearchService", this);
    1.21 +    this.setUpInitialState();
    1.22 +  },
    1.23 +
    1.24 +  setUpInitialState: function () {
    1.25 +    this._send("GetState");
    1.26 +  },
    1.27 +
    1.28 +  showPanel: function () {
    1.29 +    let panel = this._nodes.panel;
    1.30 +    let logo = this._nodes.logo;
    1.31 +    panel.openPopup(logo);
    1.32 +    logo.setAttribute("active", "true");
    1.33 +    panel.addEventListener("popuphidden", function onHidden() {
    1.34 +      panel.removeEventListener("popuphidden", onHidden);
    1.35 +      logo.removeAttribute("active");
    1.36 +    });
    1.37 +  },
    1.38 +
    1.39 +  search: function (event) {
    1.40 +    event.preventDefault();
    1.41 +    let searchStr = this._nodes.text.value;
    1.42 +    if (this.currentEngineName && searchStr.length) {
    1.43 +      this._send("Search", {
    1.44 +        engineName: this.currentEngineName,
    1.45 +        searchString: searchStr,
    1.46 +        whence: "newtab",
    1.47 +      });
    1.48 +    }
    1.49 +  },
    1.50 +
    1.51 +  manageEngines: function () {
    1.52 +    this._nodes.panel.hidePopup();
    1.53 +    this._send("ManageEngines");
    1.54 +  },
    1.55 +
    1.56 +  setWidth: function (width) {
    1.57 +    this._nodes.form.style.width = width + "px";
    1.58 +    this._nodes.form.style.maxWidth = width + "px";
    1.59 +  },
    1.60 +
    1.61 +  handleEvent: function (event) {
    1.62 +    this["on" + event.detail.type](event.detail.data);
    1.63 +  },
    1.64 +
    1.65 +  onState: function (data) {
    1.66 +    this._makePanel(data.engines);
    1.67 +    this._setCurrentEngine(data.currentEngine);
    1.68 +    this._initWhenInitalStateReceived();
    1.69 +  },
    1.70 +
    1.71 +  onCurrentEngine: function (engineName) {
    1.72 +    this._setCurrentEngine(engineName);
    1.73 +  },
    1.74 +
    1.75 +  _nodeIDSuffixes: [
    1.76 +    "form",
    1.77 +    "logo",
    1.78 +    "manage",
    1.79 +    "panel",
    1.80 +    "text",
    1.81 +  ],
    1.82 +
    1.83 +  _nodes: {},
    1.84 +
    1.85 +  _initWhenInitalStateReceived: function () {
    1.86 +    this._nodes.form.addEventListener("submit", e => this.search(e));
    1.87 +    this._nodes.logo.addEventListener("click", e => this.showPanel());
    1.88 +    this._nodes.manage.addEventListener("click", e => this.manageEngines());
    1.89 +    this._initWhenInitalStateReceived = function () {};
    1.90 +  },
    1.91 +
    1.92 +  _send: function (type, data=null) {
    1.93 +    window.dispatchEvent(new CustomEvent("ContentSearchClient", {
    1.94 +      detail: {
    1.95 +        type: type,
    1.96 +        data: data,
    1.97 +      },
    1.98 +    }));
    1.99 +  },
   1.100 +
   1.101 +  _makePanel: function (engines) {
   1.102 +    let panel = this._nodes.panel;
   1.103 +
   1.104 +    // Empty the panel except for the Manage Engines row.
   1.105 +    let i = 0;
   1.106 +    while (i < panel.childNodes.length) {
   1.107 +      let node = panel.childNodes[i];
   1.108 +      if (node != this._nodes.manage) {
   1.109 +        panel.removeChild(node);
   1.110 +      }
   1.111 +      else {
   1.112 +        i++;
   1.113 +      }
   1.114 +    }
   1.115 +
   1.116 +    // Add all the engines.
   1.117 +    for (let engine of engines) {
   1.118 +      panel.insertBefore(this._makePanelEngine(panel, engine),
   1.119 +                         this._nodes.manage);
   1.120 +    }
   1.121 +  },
   1.122 +
   1.123 +  _makePanelEngine: function (panel, engine) {
   1.124 +    let box = document.createElementNS(XUL_NAMESPACE, "hbox");
   1.125 +    box.className = "newtab-search-panel-engine";
   1.126 +    box.setAttribute("engine", engine.name);
   1.127 +
   1.128 +    box.addEventListener("click", () => {
   1.129 +      this._send("SetCurrentEngine", engine.name);
   1.130 +      panel.hidePopup();
   1.131 +      this._nodes.text.focus();
   1.132 +    });
   1.133 +
   1.134 +    let image = document.createElementNS(XUL_NAMESPACE, "image");
   1.135 +    if (engine.iconURI) {
   1.136 +      image.setAttribute("src", engine.iconURI);
   1.137 +    }
   1.138 +    box.appendChild(image);
   1.139 +
   1.140 +    let label = document.createElementNS(XUL_NAMESPACE, "label");
   1.141 +    label.setAttribute("value", engine.name);
   1.142 +    box.appendChild(label);
   1.143 +
   1.144 +    return box;
   1.145 +  },
   1.146 +
   1.147 +  _setCurrentEngine: function (engine) {
   1.148 +    this.currentEngineName = engine.name;
   1.149 +
   1.150 +    // Set the logo.
   1.151 +    let logoURI = window.devicePixelRatio == 2 ? engine.logo2xURI :
   1.152 +                  engine.logoURI;
   1.153 +    if (logoURI) {
   1.154 +      this._nodes.logo.hidden = false;
   1.155 +      this._nodes.logo.style.backgroundImage = "url(" + logoURI + ")";
   1.156 +      this._nodes.text.placeholder = "";
   1.157 +    }
   1.158 +    else {
   1.159 +      this._nodes.logo.hidden = true;
   1.160 +      this._nodes.text.placeholder = engine.name;
   1.161 +    }
   1.162 +
   1.163 +    // Set the selected state of all the engines in the panel.
   1.164 +    for (let box of this._nodes.panel.childNodes) {
   1.165 +      if (box.getAttribute("engine") == engine.name) {
   1.166 +        box.setAttribute("selected", "true");
   1.167 +      }
   1.168 +      else {
   1.169 +        box.removeAttribute("selected");
   1.170 +      }
   1.171 +    }
   1.172 +  },
   1.173 +};

mercurial