2015-03-20 18:46:46 +03:00
|
|
|
/* 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";
|
|
|
|
|
2016-03-08 16:59:54 +03:00
|
|
|
const {utils: Cu} = Components;
|
2015-03-20 18:46:46 +03:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["modal"];
|
|
|
|
|
2016-03-08 16:59:54 +03:00
|
|
|
const isFirefox = () => Services.appinfo.name == "Firefox";
|
2015-03-20 18:46:46 +03:00
|
|
|
|
|
|
|
this.modal = {};
|
|
|
|
modal = {
|
|
|
|
COMMON_DIALOG_LOADED: "common-dialog-loaded",
|
|
|
|
TABMODAL_DIALOG_LOADED: "tabmodal-dialog-loaded",
|
|
|
|
handlers: {
|
|
|
|
"common-dialog-loaded": new Set(),
|
|
|
|
"tabmodal-dialog-loaded": new Set()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add handler that will be called when a global- or tab modal dialogue
|
|
|
|
* appears.
|
|
|
|
*
|
|
|
|
* This is achieved by installing observers for common-
|
|
|
|
* and tab modal loaded events.
|
|
|
|
*
|
|
|
|
* This function is a no-op if called on any other product than Firefox.
|
|
|
|
*
|
|
|
|
* @param {function(Object, string)} handler
|
|
|
|
* The handler to be called, which is passed the
|
|
|
|
* subject (e.g. ChromeWindow) and the topic (one of
|
|
|
|
* {@code modal.COMMON_DIALOG_LOADED} or
|
|
|
|
* {@code modal.TABMODAL_DIALOG_LOADED}.
|
|
|
|
*/
|
|
|
|
modal.addHandler = function(handler) {
|
|
|
|
if (!isFirefox()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(this.handlers).map(topic => {
|
|
|
|
this.handlers[topic].add(handler);
|
|
|
|
Services.obs.addObserver(handler, topic, false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove modal dialogue handler by function reference.
|
|
|
|
*
|
|
|
|
* This function is a no-op if called on any other product than Firefox.
|
|
|
|
*
|
|
|
|
* @param {function} toRemove
|
|
|
|
* The handler previously passed to modal.addHandler which will now
|
|
|
|
* be removed.
|
|
|
|
*/
|
|
|
|
modal.removeHandler = function(toRemove) {
|
|
|
|
if (!isFirefox()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let topic of Object.keys(this.handlers)) {
|
|
|
|
let handlers = this.handlers[topic];
|
|
|
|
for (let handler of handlers) {
|
|
|
|
if (handler == toRemove) {
|
|
|
|
Services.obs.removeObserver(handler, topic);
|
|
|
|
handlers.delete(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the current modal dialogue.
|
|
|
|
*
|
2016-05-10 15:29:21 +03:00
|
|
|
* @param {function(): browser.Context} curBrowserFn
|
|
|
|
* Function that returns the current |browser.Context|.
|
2016-03-08 17:10:32 +03:00
|
|
|
* @param {nsIWeakReference=} winRef
|
|
|
|
* A weak reference to the current |ChromeWindow|.
|
2015-03-20 18:46:46 +03:00
|
|
|
*/
|
2016-03-08 17:10:32 +03:00
|
|
|
modal.Dialog = class {
|
|
|
|
constructor(curBrowserFn, winRef = undefined) {
|
|
|
|
this.curBrowserFn_ = curBrowserFn;
|
|
|
|
this.win_ = winRef;
|
|
|
|
}
|
2015-03-20 18:46:46 +03:00
|
|
|
|
2016-03-08 17:10:32 +03:00
|
|
|
get curBrowser_() { return this.curBrowserFn_(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ChromeWindow associated with an open dialog window if
|
|
|
|
* it is currently attached to the DOM.
|
|
|
|
*/
|
|
|
|
get window() {
|
|
|
|
if (this.win_) {
|
2015-03-20 18:46:46 +03:00
|
|
|
let win = this.win_.get();
|
2015-03-23 23:43:18 +03:00
|
|
|
if (win && win.parent) {
|
2015-03-20 18:46:46 +03:00
|
|
|
return win;
|
2015-03-23 23:43:18 +03:00
|
|
|
}
|
2015-03-20 18:46:46 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-08 17:10:32 +03:00
|
|
|
get ui() {
|
2015-03-20 18:46:46 +03:00
|
|
|
let win = this.window;
|
2015-03-23 23:43:18 +03:00
|
|
|
if (win) {
|
2015-03-20 18:46:46 +03:00
|
|
|
return win.Dialog.ui;
|
2015-03-23 23:43:18 +03:00
|
|
|
}
|
2016-03-08 17:10:32 +03:00
|
|
|
return this.curBrowser_.getTabModalUI();
|
2015-03-20 18:46:46 +03:00
|
|
|
}
|
2016-03-08 17:10:32 +03:00
|
|
|
};
|