2016-10-06 07:17:00 +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";
|
|
|
|
|
|
|
|
const DEBUG = false;
|
|
|
|
function debug(aStr) {
|
|
|
|
if (DEBUG) {
|
2018-06-10 00:46:35 +03:00
|
|
|
dump("-*- DateTimePickerParent: " + aStr + "\n");
|
2016-10-06 07:17:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 13:14:41 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["DateTimePickerParent"];
|
2016-10-06 07:17:00 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2018-12-22 00:51:57 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"DateTimePickerPanel",
|
|
|
|
"resource://gre/modules/DateTimePickerPanel.jsm"
|
|
|
|
);
|
2016-10-06 07:17:00 +03:00
|
|
|
|
|
|
|
/*
|
2018-06-10 00:46:35 +03:00
|
|
|
* DateTimePickerParent receives message from content side (input box) and
|
|
|
|
* is reposible for opening, closing and updating the picker. Similarly,
|
|
|
|
* DateTimePickerParent listens for picker's events and notifies the content
|
2016-10-06 07:17:00 +03:00
|
|
|
* side (input box) about them.
|
|
|
|
*/
|
2019-07-04 13:14:41 +03:00
|
|
|
class DateTimePickerParent extends JSWindowActorParent {
|
2016-12-30 02:34:54 +03:00
|
|
|
receiveMessage(aMessage) {
|
2016-10-06 07:17:00 +03:00
|
|
|
debug("receiveMessage: " + aMessage.name);
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "FormDateTime:OpenPicker": {
|
2019-07-04 13:14:41 +03:00
|
|
|
let topBrowsingContext = this.manager.browsingContext.top;
|
|
|
|
let browser = topBrowsingContext.embedderElement;
|
|
|
|
this.showPicker(browser, aMessage.data);
|
2016-10-06 07:17:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "FormDateTime:ClosePicker": {
|
2019-07-04 13:14:41 +03:00
|
|
|
if (!this._picker) {
|
2016-10-06 07:17:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-07-04 13:14:41 +03:00
|
|
|
this._picker.closePicker();
|
2017-10-19 04:14:56 +03:00
|
|
|
this.close();
|
2016-10-06 07:17:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "FormDateTime:UpdatePicker": {
|
2019-07-04 13:14:41 +03:00
|
|
|
if (!this._picker) {
|
2017-08-02 12:05:38 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-07-04 13:14:41 +03:00
|
|
|
this._picker.setPopupValue(aMessage.data);
|
2016-10-06 07:17:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-07-04 13:14:41 +03:00
|
|
|
}
|
2016-10-06 07:17:00 +03:00
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
handleEvent(aEvent) {
|
2016-10-06 07:17:00 +03:00
|
|
|
debug("handleEvent: " + aEvent.type);
|
|
|
|
switch (aEvent.type) {
|
|
|
|
case "DateTimePickerValueChanged": {
|
2019-07-04 13:14:41 +03:00
|
|
|
this.sendAsyncMessage("FormDateTime:PickerValueChanged", aEvent.detail);
|
2016-10-06 07:17:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "popuphidden": {
|
2019-07-04 13:14:41 +03:00
|
|
|
this.sendAsyncMessage("FormDateTime:PickerClosed", {});
|
|
|
|
this._picker.closePicker();
|
2016-10-06 07:17:00 +03:00
|
|
|
this.close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-07-04 13:14:41 +03:00
|
|
|
}
|
2016-10-06 07:17:00 +03:00
|
|
|
|
|
|
|
// Get picker from browser and show it anchored to the input box.
|
2018-12-22 00:51:57 +03:00
|
|
|
showPicker(aBrowser, aData) {
|
2016-10-06 07:17:00 +03:00
|
|
|
let rect = aData.rect;
|
|
|
|
let type = aData.type;
|
|
|
|
let detail = aData.detail;
|
2016-09-06 08:01:40 +03:00
|
|
|
|
2016-10-06 07:17:00 +03:00
|
|
|
debug("Opening picker with details: " + JSON.stringify(detail));
|
|
|
|
|
|
|
|
let window = aBrowser.ownerGlobal;
|
|
|
|
let tabbrowser = window.gBrowser;
|
|
|
|
if (
|
|
|
|
Services.focus.activeWindow != window ||
|
2019-06-26 21:15:28 +03:00
|
|
|
(tabbrowser && tabbrowser.selectedBrowser != aBrowser)
|
|
|
|
) {
|
2016-10-06 07:17:00 +03:00
|
|
|
// We were sent a message from a window or tab that went into the
|
|
|
|
// background, so we'll ignore it for now.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:44:03 +03:00
|
|
|
let panel;
|
|
|
|
if (tabbrowser) {
|
|
|
|
panel = tabbrowser._getAndMaybeCreateDateTimePickerPanel();
|
|
|
|
} else {
|
|
|
|
panel = aBrowser.dateTimePicker;
|
|
|
|
}
|
|
|
|
if (!panel) {
|
2016-10-06 07:17:00 +03:00
|
|
|
debug("aBrowser.dateTimePicker not found, exiting now.");
|
|
|
|
return;
|
|
|
|
}
|
2020-06-03 18:44:03 +03:00
|
|
|
this._picker = new DateTimePickerPanel(panel);
|
2020-08-10 02:51:33 +03:00
|
|
|
this._picker.openPicker(type, rect, detail);
|
2016-09-06 08:01:40 +03:00
|
|
|
|
2016-10-06 07:17:00 +03:00
|
|
|
this.addPickerListeners();
|
2019-07-04 13:14:41 +03:00
|
|
|
}
|
2016-10-06 07:17:00 +03:00
|
|
|
|
|
|
|
// Picker is closed, do some cleanup.
|
2016-12-30 02:34:54 +03:00
|
|
|
close() {
|
2016-10-06 07:17:00 +03:00
|
|
|
this.removePickerListeners();
|
2019-07-04 13:14:41 +03:00
|
|
|
this._picker = null;
|
|
|
|
}
|
2016-10-06 07:17:00 +03:00
|
|
|
|
|
|
|
// Listen to picker's event.
|
2016-12-30 02:34:54 +03:00
|
|
|
addPickerListeners() {
|
2019-07-04 13:14:41 +03:00
|
|
|
if (!this._picker) {
|
2016-10-06 07:17:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-07-04 13:14:41 +03:00
|
|
|
this._picker.element.addEventListener("popuphidden", this);
|
|
|
|
this._picker.element.addEventListener("DateTimePickerValueChanged", this);
|
|
|
|
}
|
2016-10-06 07:17:00 +03:00
|
|
|
|
|
|
|
// Stop listening to picker's event.
|
2016-12-30 02:34:54 +03:00
|
|
|
removePickerListeners() {
|
2019-07-04 13:14:41 +03:00
|
|
|
if (!this._picker) {
|
2016-10-06 07:17:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-07-04 13:14:41 +03:00
|
|
|
this._picker.element.removeEventListener("popuphidden", this);
|
|
|
|
this._picker.element.removeEventListener(
|
|
|
|
"DateTimePickerValueChanged",
|
|
|
|
this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|