Bug 1603673 - Signal that we support web manifest processing in Fenix r=snorp,agi...
[gecko.git] / toolkit / actors / DateTimePickerParent.jsm
bloba6c47f24cea825286563c6fe290a21f0755dd9dc
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/. */
5 "use strict";
7 const DEBUG = false;
8 function debug(aStr) {
9   if (DEBUG) {
10     dump("-*- DateTimePickerParent: " + aStr + "\n");
11   }
14 var EXPORTED_SYMBOLS = ["DateTimePickerParent"];
16 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
17 ChromeUtils.defineModuleGetter(
18   this,
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.
28  */
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);
37         break;
38       }
39       case "FormDateTime:ClosePicker": {
40         if (!this._picker) {
41           return;
42         }
43         this._picker.closePicker();
44         this.close();
45         break;
46       }
47       case "FormDateTime:UpdatePicker": {
48         if (!this._picker) {
49           return;
50         }
51         this._picker.setPopupValue(aMessage.data);
52         break;
53       }
54       default:
55         break;
56     }
57   }
59   handleEvent(aEvent) {
60     debug("handleEvent: " + aEvent.type);
61     switch (aEvent.type) {
62       case "DateTimePickerValueChanged": {
63         this.sendAsyncMessage("FormDateTime:PickerValueChanged", aEvent.detail);
64         break;
65       }
66       case "popuphidden": {
67         this.sendAsyncMessage("FormDateTime:PickerClosed", {});
68         this._picker.closePicker();
69         this.close();
70         break;
71       }
72       default:
73         break;
74     }
75   }
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;
84     if (!this._anchor) {
85       throw new Error(
86         "No popup anchor for this browser, cannot show date picker"
87       );
88     }
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;
100     if (
101       Services.focus.activeWindow != window ||
102       (tabbrowser && tabbrowser.selectedBrowser != aBrowser)
103     ) {
104       // We were sent a message from a window or tab that went into the
105       // background, so we'll ignore it for now.
106       return;
107     }
109     if (!aBrowser.dateTimePicker) {
110       debug("aBrowser.dateTimePicker not found, exiting now.");
111       return;
112     }
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();
119   }
121   // Picker is closed, do some cleanup.
122   close() {
123     this.removePickerListeners();
124     this._picker = null;
125     this._anchor.hidden = true;
126   }
128   // Listen to picker's event.
129   addPickerListeners() {
130     if (!this._picker) {
131       return;
132     }
133     this._picker.element.addEventListener("popuphidden", this);
134     this._picker.element.addEventListener("DateTimePickerValueChanged", this);
135   }
137   // Stop listening to picker's event.
138   removePickerListeners() {
139     if (!this._picker) {
140       return;
141     }
142     this._picker.element.removeEventListener("popuphidden", this);
143     this._picker.element.removeEventListener(
144       "DateTimePickerValueChanged",
145       this
146     );
147   }