addon-sdk/source/lib/sdk/windows/fennec.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/windows/fennec.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +const { Class } = require('../core/heritage');
    1.10 +const { BrowserWindow } = require('../window/browser');
    1.11 +const { WindowTracker } = require('../deprecated/window-utils');
    1.12 +const { isBrowser, getMostRecentBrowserWindow } = require('../window/utils');
    1.13 +const { windowNS } = require('../window/namespace');
    1.14 +const { on, off, once, emit } = require('../event/core');
    1.15 +const { method } = require('../lang/functional');
    1.16 +const { EventTarget } = require('../event/target');
    1.17 +const { List, addListItem } = require('../util/list');
    1.18 +
    1.19 +const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec, consider using require("sdk/tabs") instead';
    1.20 +
    1.21 +// NOTE: On Fennec there is only one window.
    1.22 +
    1.23 +let BrowserWindows = Class({
    1.24 +  implements: [ List ],
    1.25 +  extends: EventTarget,
    1.26 +  initialize: function() {
    1.27 +    List.prototype.initialize.apply(this);
    1.28 +  },
    1.29 +  get activeWindow() {
    1.30 +    let window = getMostRecentBrowserWindow();
    1.31 +    return window ? getBrowserWindow({window: window}) : null;
    1.32 +  },
    1.33 +  open: function open(options) {
    1.34 +    throw new Error(ERR_FENNEC_MSG);
    1.35 +    return null;
    1.36 +  }
    1.37 +});
    1.38 +const browserWindows = exports.browserWindows = BrowserWindows();
    1.39 +
    1.40 +
    1.41 +/**
    1.42 + * Gets a `BrowserWindow` for the given `chromeWindow` if previously
    1.43 + * registered, `null` otherwise.
    1.44 + */
    1.45 +function getRegisteredWindow(chromeWindow) {
    1.46 +  for each (let window in browserWindows) {
    1.47 +    if (chromeWindow === windowNS(window).window)
    1.48 +      return window;
    1.49 +  }
    1.50 +
    1.51 +  return null;
    1.52 +}
    1.53 +
    1.54 +/**
    1.55 + * Gets a `BrowserWindow` for the provided window options obj
    1.56 + * @params {Object} options
    1.57 + *    Options that are passed to the the `BrowserWindowTrait`
    1.58 + * @returns {BrowserWindow}
    1.59 + */
    1.60 +function getBrowserWindow(options) {
    1.61 +  let window = null;
    1.62 +
    1.63 +  // if we have a BrowserWindow already then use it
    1.64 +  if ('window' in options)
    1.65 +    window = getRegisteredWindow(options.window);
    1.66 +  if (window)
    1.67 +    return window;
    1.68 +
    1.69 +  // we don't have a BrowserWindow yet, so create one
    1.70 +  var window = BrowserWindow(options);
    1.71 +  addListItem(browserWindows, window);
    1.72 +  return window;
    1.73 +}
    1.74 +
    1.75 +WindowTracker({
    1.76 +  onTrack: function onTrack(chromeWindow) {
    1.77 +    if (!isBrowser(chromeWindow)) return;
    1.78 +    let window = getBrowserWindow({ window: chromeWindow });
    1.79 +    emit(browserWindows, 'open', window);
    1.80 +  },
    1.81 +  onUntrack: function onUntrack(chromeWindow) {
    1.82 +    if (!isBrowser(chromeWindow)) return;
    1.83 +    let window = getBrowserWindow({ window: chromeWindow });
    1.84 +    emit(browserWindows, 'close', window);
    1.85 +  }
    1.86 +});

mercurial