1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 dump("-*- DateTimePickerParent: " + aStr + "\n");
14 var EXPORTED_SYMBOLS = ["DateTimePickerParent"];
16 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
17 ChromeUtils.defineModuleGetter(
19 "DateTimePickerPanel",
20 "resource://gre/modules/DateTimePickerPanel.jsm"
24 * DateTimePickerParent receives message from content side (input box) and
25 * is reposible for opening, closing and updating the picker. Similarly,
26 * DateTimePickerParent listens for picker's events and notifies the content
27 * side (input box) about them.
29 class DateTimePickerParent extends JSWindowActorParent {
30 receiveMessage(aMessage) {
31 debug("receiveMessage: " + aMessage.name);
32 switch (aMessage.name) {
33 case "FormDateTime:OpenPicker": {
34 let topBrowsingContext = this.manager.browsingContext.top;
35 let browser = topBrowsingContext.embedderElement;
36 this.showPicker(browser, aMessage.data);
39 case "FormDateTime:ClosePicker": {
43 this._picker.closePicker();
47 case "FormDateTime:UpdatePicker": {
51 this._picker.setPopupValue(aMessage.data);
60 debug("handleEvent: " + aEvent.type);
61 switch (aEvent.type) {
62 case "DateTimePickerValueChanged": {
63 this.sendAsyncMessage("FormDateTime:PickerValueChanged", aEvent.detail);
67 this.sendAsyncMessage("FormDateTime:PickerClosed", {});
68 this._picker.closePicker();
77 // Get picker from browser and show it anchored to the input box.
78 showPicker(aBrowser, aData) {
79 let rect = aData.rect;
80 let type = aData.type;
81 let detail = aData.detail;
83 this._anchor = aBrowser.popupAnchor;
86 "No popup anchor for this browser, cannot show date picker"
90 this._anchor.style.left = rect.left + "px";
91 this._anchor.style.top = rect.top + "px";
92 this._anchor.style.width = rect.width + "px";
93 this._anchor.style.height = rect.height + "px";
94 this._anchor.hidden = false;
96 debug("Opening picker with details: " + JSON.stringify(detail));
98 let window = aBrowser.ownerGlobal;
99 let tabbrowser = window.gBrowser;
101 Services.focus.activeWindow != window ||
102 (tabbrowser && tabbrowser.selectedBrowser != aBrowser)
104 // We were sent a message from a window or tab that went into the
105 // background, so we'll ignore it for now.
109 if (!aBrowser.dateTimePicker) {
110 debug("aBrowser.dateTimePicker not found, exiting now.");
113 this._picker = new DateTimePickerPanel(aBrowser.dateTimePicker);
114 // The arrow panel needs an anchor to work. The popupAnchor (this._anchor)
115 // is a transparent div that the arrow can point to.
116 this._picker.openPicker(type, this._anchor, detail);
118 this.addPickerListeners();
121 // Picker is closed, do some cleanup.
123 this.removePickerListeners();
125 this._anchor.hidden = true;
128 // Listen to picker's event.
129 addPickerListeners() {
133 this._picker.element.addEventListener("popuphidden", this);
134 this._picker.element.addEventListener("DateTimePickerValueChanged", this);
137 // Stop listening to picker's event.
138 removePickerListeners() {
142 this._picker.element.removeEventListener("popuphidden", this);
143 this._picker.element.removeEventListener(
144 "DateTimePickerValueChanged",