michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: let FlyoutPanelsUI = { michael@0: _isInitialized: false, michael@0: michael@0: init: function() { michael@0: if (this._isInitialized) { michael@0: Cu.reportError("Attempted to initialize FlyoutPanelsUI more than once"); michael@0: return; michael@0: } michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: this._isInitialized = true; michael@0: let scriptContexts = {}; michael@0: let scripts = michael@0: [ michael@0: ['AboutFlyoutPanel', 'chrome://browser/content/flyoutpanels/AboutFlyoutPanel.js'], michael@0: ['PrefsFlyoutPanel', 'chrome://browser/content/flyoutpanels/PrefsFlyoutPanel.js'], michael@0: ['SearchFlyoutPanel', 'chrome://browser/content/flyoutpanels/SearchFlyoutPanel.js'], michael@0: #ifdef MOZ_SERVICES_SYNC michael@0: ['SyncFlyoutPanel', 'chrome://browser/content/flyoutpanels/SyncFlyoutPanel.js'], michael@0: #endif michael@0: ]; michael@0: michael@0: scripts.forEach(function (aScript) { michael@0: let [name, script] = aScript; michael@0: XPCOMUtils.defineLazyGetter(FlyoutPanelsUI, name, function() { michael@0: let sandbox = {}; michael@0: Services.scriptloader.loadSubScript(script, sandbox); michael@0: sandbox[name].init(); michael@0: return sandbox[name]; michael@0: }); michael@0: }); michael@0: michael@0: Services.obs.addObserver(this, "metro_viewstate_changed", false); michael@0: window.addEventListener("TabOpen", this, false); michael@0: }, michael@0: michael@0: uninit: function () { michael@0: Services.obs.removeObserver(this, "metro_viewstate_changed"); michael@0: window.removeEventListener("TabOpen", this, false); michael@0: }, michael@0: michael@0: show: function(aToShow) { michael@0: if (!this[aToShow]) { michael@0: throw("FlyoutPanelsUI asked to show '" + aToShow + "' which does not exist"); michael@0: } michael@0: michael@0: if (this._currentFlyout) { michael@0: if (this._currentFlyout == this[aToShow]) { michael@0: return; michael@0: } else { michael@0: this.hide(); michael@0: } michael@0: } michael@0: michael@0: this._currentFlyout = this[aToShow]; michael@0: if (this._currentFlyout._show) { michael@0: this._currentFlyout._show(); michael@0: } else { michael@0: this._currentFlyout._topmostElement.show(); michael@0: } michael@0: DialogUI.pushPopup(this, this._currentFlyout._topmostElement); michael@0: }, michael@0: michael@0: onBackButton: function() { michael@0: if (this._currentFlyout._onBackButton) { michael@0: this._currentFlyout._onBackButton(); michael@0: } else { michael@0: this.hide(); michael@0: Services.metro.showSettingsFlyout(); michael@0: } michael@0: }, michael@0: michael@0: get isVisible() { michael@0: return this._currentFlyout ? true : false; michael@0: }, michael@0: michael@0: handleEvent: function (aEvent) { michael@0: switch (aEvent.type) { michael@0: case "TabOpen": michael@0: this.hide() michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: observe: function (aSubject, aTopic, aData) { michael@0: switch (aTopic) { michael@0: case "metro_viewstate_changed": michael@0: if (aData == "snapped") { michael@0: this.hide(); michael@0: } michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: dispatchEvent: function(aEvent) { michael@0: if (this._currentFlyout) { michael@0: this._currentFlyout._topmostElement.dispatchEvent(aEvent); michael@0: } michael@0: }, michael@0: michael@0: hide: function() { michael@0: if (this._currentFlyout) { michael@0: if (this._currentFlyout._hide) { michael@0: this._currentFlyout._hide(); michael@0: } else { michael@0: this._currentFlyout._topmostElement.hide(); michael@0: } michael@0: DialogUI.popPopup(this); michael@0: delete this._currentFlyout; michael@0: } michael@0: } michael@0: };