browser/components/pocket/Pocket.jsm
author Justin Dolske <dolske@mozilla.com>
Sun, 10 May 2015 17:19:17 -0700
changeset 259106 83c0c74947a35b7f4914e6922194b629f970d182
parent 259101 18bf7b4baaac02122ae85e56d1ad52ea2d801513
child 259111 32b69592b334671ba391c23c18750777eb74ff41
permissions -rw-r--r--
Bug 1163349 - "View Pocket List" menuitem not working. r=gavin, a=dolske

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;

this.EXPORTED_SYMBOLS = ["Pocket"];

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
  "resource:///modules/CustomizableUI.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ReaderMode",
  "resource://gre/modules/ReaderMode.jsm");

let Pocket = {
  get site() Services.prefs.getCharPref("browser.pocket.site"),
  get listURL() { return "https://" + Pocket.site; },

  /**
   * Functions related to the Pocket panel UI.
   */
  onPanelViewShowing(event) {
    let window = event.target.ownerDocument.defaultView;
    window.addEventListener("popupshowing", Pocket.onPocketPanelShowing, true);
    window.addEventListener("popupshown", Pocket.onPocketPanelShown, true);
  },

  onPocketPanelShowing(event) {
    let window = event.target.ownerDocument.defaultView;
    window.removeEventListener("popupshowing", Pocket.onPocketPanelShowing, true);
    window.pktUI.pocketButtonOnCommand(event);
  },

  onPocketPanelShown(event) {
    let window = event.target.ownerDocument.defaultView;
    window.removeEventListener("popupshown", Pocket.onPocketPanelShown, true);
    window.pktUI.pocketPanelDidShow(event);
  },

  onPanelViewHiding(event) {
    let window = event.target.ownerDocument.defaultView;
    window.pktUI.pocketPanelDidHide(event);
  },

  // Called on tab/urlbar/location changes and after customization. Update
  // anything that is tab specific.
  onLocationChange(browser, locationURI) {
    if (!locationURI) {
      return;
    }
    let widget = CustomizableUI.getWidget("pocket-button");
    for (let instance of widget.instances) {
      let node = instance.node;
      if (!node ||
          node.ownerDocument != browser.ownerDocument) {
        continue;
      }
      if (node) {
        let win = browser.ownerDocument.defaultView;
        node.disabled = win.pktApi.isUserLoggedIn() &&
                        !locationURI.schemeIs("http") &&
                        !locationURI.schemeIs("https") &&
                        !(locationURI.schemeIs("about") &&
                          locationURI.spec.toLowerCase().startsWith("about:reader?url="));
      }
    }
  },
};