"use strict";
const EXPORTED_SYMBOLS = ["dlgman"];
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
var utils;
const dlgman = {
init: function yDlgMan_init(api) {
if (this._initialized)
return;
this._api = api;
Cu.import(this._api.Package.resolvePath("/native/fx/modules/utils.jsm"));
this._welcome = this._getModule("welcome","/native/fx/modules/welcome.jsm");
this._feeds = this._getModule("feeds","/native/fx/modules/feeds.jsm");
this._bookmarks = this._getModule("bookmarks","/native/fx/modules/bookmarks.jsm");
this.__proto__ = new utils.NotificationSource();
this._initialized = true;
}
,
openAuthDialog: function yDlgMan_openAuthDialog(auth, event, dialogParams, isForcedLogin, onEventCallback, onCloseCallback) {
var windowArguments = {
api: this._api,
auth: auth,
services: this._welcome,
onEvent: onEventCallback,
dialogParams: dialogParams,
isForcedLogin: isForcedLogin};
this._api.Controls.createSlice({
url: this._api.Package.resolvePath("native/fx/dialogs/auth/auth.xul"),
injectedProperties: {
windowArguments: windowArguments},
disposable: true,
system: true}).show(event && event.target,onCloseCallback);
}
,
openFeedsDialog: function yDlgMan_openFeedsDialog() {
var dialog = this._openWindow({
url: this._api.Package.resolvePath("native/fx/dialogs/feeds/add-feeds-dialog.xul"),
name: "FeedsDialog",
parent: this.getTopBrowserWindow(),
features: "chrome,dialog,centerscreen",
arguments: {
services: this._feeds,
api: this._api}},true);
this._subscribeToClose(dialog);
return dialog;
}
,
closeFeedsDialog: function yDlgMan_closeFeedsDialog() {
var dialog = this._WW.getWindowByName("FeedsDialog",utils.getCurrentWindow());
if (dialog && ! dialog.closed)
{
dialog.close();
}

}
,
_onNextTimeout: 1000,
_arrBufferedNotifications: [],
openAlertDialog: function yDlgMan_openAlertDialog(type, api, data) {
var fnDialogPopUp = this._openPanel.bind(this,type,api,data);
this._arrBufferedNotifications.push({
type: type,
openAlertFunction: fnDialogPopUp});
if (this._arrBufferedNotifications.length == 1)
this._onBufferedNotificationNext();
}
,
_openPanel: function yDlgMan__openPanel(type, api, data) {
var hiddenDoc = api.Browser.getHiddenFrame().contentDocument;
var stylesheetId = type + "-notification-stylesheet";
if (! hiddenDoc.getElementById(stylesheetId))
{
let styleElement = hiddenDoc.createElementNS("http://www.w3.org/1999/xhtml","style");
styleElement.setAttribute("id",stylesheetId);
hiddenDoc.documentElement.appendChild(styleElement);
let stylesheet = styleElement.sheet;
stylesheet.insertRule("@import url('" + api.Package.resolvePath("native/fx/dialogs/alerts/alert.css") + "');",stylesheet.cssRules.length);
}

var panel = hiddenDoc.createElement("panel");
panel.setAttribute("noautohide","true");
panel.setAttribute("level","top");
panel.yaWinArguments = {
api: api,
type: type,
data: data};
hiddenDoc.documentElement.appendChild(panel);
panel.clientTop;
return panel;
}
,
closeAlertDialog: function yDlgMan_closeAlertDialog(type) {
var showingNotificationData = this._arrBufferedNotifications[0];
this._arrBufferedNotifications = this._arrBufferedNotifications.filter(function (notificationData) notificationData.type !== type);
if (showingNotificationData && showingNotificationData.type === type)
{

}

}
,
openOpinionsDialog: function yDlgMan_openOpinionsDialog(opinionsModule, strDlgID, strTargetURL, scrollPos, onEventCallback, needToRefresh) {
var dialog = this._openWindow({
url: this._api.Package.resolvePath("native/fx/dialogs/opinions/opinions.xul"),
name: strDlgID,
features: "__popup__",
arguments: {
onEvent: onEventCallback,
api: this._api,
opinionsModule: opinionsModule,
targetURL: strTargetURL,
scrollPos: scrollPos}},needToRefresh);
this._subscribeToClose(dialog);
return dialog;
}
,
closeOpinionsDialog: function yDlgMan_closeOpinionsDialog(strDlgID) {
var dialog = this._WW.getWindowByName(strDlgID,null);
if (dialog)
dialog.close();
}
,
openBookmarksDialog: function yDlgMan_openBookmarksDialog(auth, bookmark, tags, showYaRuWindow, onEventCallback) {
var dialog = this._openWindow({
url: this._api.Package.resolvePath("native/fx/dialogs/bookmarks/bookmarks.xul"),
name: "BookmarksAddDialog",
features: "dialog,centerscreen",
arguments: {
services: this._bookmarks,
onEvent: onEventCallback,
api: this._api,
auth: auth,
bookmark: bookmark,
tags: tags,
showYaRuWindow: showYaRuWindow}});
this._subscribeToClose(dialog);
return dialog;
}
,
closeBookmarksDialog: function yDlgMan_closeOpinionsDialog() {
var dialog = this._WW.getWindowByName("BookmarksAddDialog",null);
if (dialog)
dialog.close();
}
,
getTopBrowserWindow: function yDlgMan_getTopBrowserWindow() {
return this._getTopWindowOfType("navigator:browser");
}
,
EVENTS: {
ON_DIALOG_CLOSE: "dlgman-dialog-close"},
_subscribeToClose: function yDlgMan__subscribeToClose(dialog, callback) {
var me = this;
dialog.addEventListener("load",function loadEventListener(aLoadEvent) {
aLoadEvent.currentTarget.removeEventListener("load",loadEventListener,false);
aLoadEvent.currentTarget.addEventListener("unload",function unloadEventListener(aUnloadEvent) {
aUnloadEvent.currentTarget.removeEventListener("unload",unloadEventListener,false);
if (callback)
callback();
me._notifyListeners(me.EVENTS.ON_DIALOG_CLOSE,dialog.name);
}
,false);
}
,false);
}
,
_onBufferedNotificationNext: function yDlgMan__onBufferedNotificationNext() {
var notificationData = this._arrBufferedNotifications[0];
if (! notificationData)
return;
var that = this;
var timerCallback = {
notify: function yDlgMan__onBufferedNotificationNext_notify() {
var panel = notificationData.openAlertFunction.apply();
panel.addEventListener("popuphidden",function popuphiddenEventListener(aEvent) {
aEvent.currentTarget.removeEventListener("popuphidden",popuphiddenEventListener,false);
that._arrBufferedNotifications.shift();
that._onBufferedNotificationNext();
}
,false);
}
};
this._timer.initWithCallback(timerCallback,this._onNextTimeout,Ci.nsITimer.TYPE_ONE_SHOT);
}
,
_openWindow: function yDlgMan__openWindow(parameters, needToRefresh) {
var window;
if ("name" in parameters && parameters.name)
{
window = this._WW.getWindowByName(parameters.name,utils.getCurrentWindow());
if (window)
{
if (needToRefresh)
{
window.yaWinArguments = parameters.arguments || {
};
if (typeof window.onload == "function")
window.onload();
}

window.focus();
return window;
}

}

var parent;
var features = parameters.features || "";
if (features.indexOf("__popup__") != - 1)
{
let featuresHash = Object.create(null);
features.replace(/(^|,)__popup__($|,)/,"").split(",").forEach(function (aFeatureString) {
if (aFeatureString)
{
let [name, value] = aFeatureString.split("=");
if (name)
featuresHash[name] = value;
}

}
);
let addFeature = function (aFeatureString) {
var [name, value] = aFeatureString.split("=");
if (! (name in featuresHash))
featuresHash[name] = value;
}
;
addFeature("chrome");
addFeature("dependent=yes");
if (this._api.Environment.os.name != "windows")
addFeature("popup=yes");
let featuresMod = [];
for(let [name, value] in Iterator(featuresHash)) featuresMod.push(name + (value ? "=" + value : ""));
features = featuresMod.join(",");
if (! ("parent" in parameters))
parent = this.getTopBrowserWindow();
}

parent = parent || parameters.parent || null;
window = this._WW.openWindow(parent,parameters.url,parameters.name || "_blank",features,null);
window.yaWinArguments = parameters.arguments || {
};
return window;
}
,
_getTopWindowOfType: function yDlgMan_getTopWindowOfType(windowType) {
var mediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
return mediator.getMostRecentWindow(windowType);
}
,
_getModule: function yDlgMan__getModule(moduleName, modulePath) {
var mScope = {
};
Cu.import(this._api.Package.resolvePath(modulePath),mScope);
var module = mScope[moduleName];
if (! module)
throw new Error("No module '" + moduleName + "' in " + modulePath);
if (typeof module.init == "function")
module.init(this._api);
return module;
}
,
_timer: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer),
_WW: Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher)};
