|
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 'use strict'; |
|
6 |
|
7 let FlyoutPanelsUI = { |
|
8 _isInitialized: false, |
|
9 |
|
10 init: function() { |
|
11 if (this._isInitialized) { |
|
12 Cu.reportError("Attempted to initialize FlyoutPanelsUI more than once"); |
|
13 return; |
|
14 } |
|
15 |
|
16 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
17 Cu.import("resource://gre/modules/Services.jsm"); |
|
18 |
|
19 this._isInitialized = true; |
|
20 let scriptContexts = {}; |
|
21 let scripts = |
|
22 [ |
|
23 ['AboutFlyoutPanel', 'chrome://browser/content/flyoutpanels/AboutFlyoutPanel.js'], |
|
24 ['PrefsFlyoutPanel', 'chrome://browser/content/flyoutpanels/PrefsFlyoutPanel.js'], |
|
25 ['SearchFlyoutPanel', 'chrome://browser/content/flyoutpanels/SearchFlyoutPanel.js'], |
|
26 #ifdef MOZ_SERVICES_SYNC |
|
27 ['SyncFlyoutPanel', 'chrome://browser/content/flyoutpanels/SyncFlyoutPanel.js'], |
|
28 #endif |
|
29 ]; |
|
30 |
|
31 scripts.forEach(function (aScript) { |
|
32 let [name, script] = aScript; |
|
33 XPCOMUtils.defineLazyGetter(FlyoutPanelsUI, name, function() { |
|
34 let sandbox = {}; |
|
35 Services.scriptloader.loadSubScript(script, sandbox); |
|
36 sandbox[name].init(); |
|
37 return sandbox[name]; |
|
38 }); |
|
39 }); |
|
40 |
|
41 Services.obs.addObserver(this, "metro_viewstate_changed", false); |
|
42 window.addEventListener("TabOpen", this, false); |
|
43 }, |
|
44 |
|
45 uninit: function () { |
|
46 Services.obs.removeObserver(this, "metro_viewstate_changed"); |
|
47 window.removeEventListener("TabOpen", this, false); |
|
48 }, |
|
49 |
|
50 show: function(aToShow) { |
|
51 if (!this[aToShow]) { |
|
52 throw("FlyoutPanelsUI asked to show '" + aToShow + "' which does not exist"); |
|
53 } |
|
54 |
|
55 if (this._currentFlyout) { |
|
56 if (this._currentFlyout == this[aToShow]) { |
|
57 return; |
|
58 } else { |
|
59 this.hide(); |
|
60 } |
|
61 } |
|
62 |
|
63 this._currentFlyout = this[aToShow]; |
|
64 if (this._currentFlyout._show) { |
|
65 this._currentFlyout._show(); |
|
66 } else { |
|
67 this._currentFlyout._topmostElement.show(); |
|
68 } |
|
69 DialogUI.pushPopup(this, this._currentFlyout._topmostElement); |
|
70 }, |
|
71 |
|
72 onBackButton: function() { |
|
73 if (this._currentFlyout._onBackButton) { |
|
74 this._currentFlyout._onBackButton(); |
|
75 } else { |
|
76 this.hide(); |
|
77 Services.metro.showSettingsFlyout(); |
|
78 } |
|
79 }, |
|
80 |
|
81 get isVisible() { |
|
82 return this._currentFlyout ? true : false; |
|
83 }, |
|
84 |
|
85 handleEvent: function (aEvent) { |
|
86 switch (aEvent.type) { |
|
87 case "TabOpen": |
|
88 this.hide() |
|
89 break; |
|
90 } |
|
91 }, |
|
92 |
|
93 observe: function (aSubject, aTopic, aData) { |
|
94 switch (aTopic) { |
|
95 case "metro_viewstate_changed": |
|
96 if (aData == "snapped") { |
|
97 this.hide(); |
|
98 } |
|
99 break; |
|
100 } |
|
101 }, |
|
102 |
|
103 dispatchEvent: function(aEvent) { |
|
104 if (this._currentFlyout) { |
|
105 this._currentFlyout._topmostElement.dispatchEvent(aEvent); |
|
106 } |
|
107 }, |
|
108 |
|
109 hide: function() { |
|
110 if (this._currentFlyout) { |
|
111 if (this._currentFlyout._hide) { |
|
112 this._currentFlyout._hide(); |
|
113 } else { |
|
114 this._currentFlyout._topmostElement.hide(); |
|
115 } |
|
116 DialogUI.popPopup(this); |
|
117 delete this._currentFlyout; |
|
118 } |
|
119 } |
|
120 }; |