Source code

Revision control

Other Tools

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/. */
4
5
/*
6
* Chrome side handling of form validation popup.
7
*/
8
9
"use strict";
10
11
var EXPORTED_SYMBOLS = ["FormValidationParent"];
12
13
class FormValidationParent extends JSWindowActorParent {
14
constructor() {
15
super();
16
17
this._panel = null;
18
this._anchor = null;
19
}
20
21
/*
22
* Public apis
23
*/
24
25
uninit() {
26
this._panel = null;
27
this._anchor = null;
28
}
29
30
hidePopup() {
31
this._hidePopup();
32
}
33
34
/*
35
* Events
36
*/
37
38
receiveMessage(aMessage) {
39
switch (aMessage.name) {
40
case "FormValidation:ShowPopup":
41
let browser = this.browsingContext.top.embedderElement;
42
let window = browser.ownerGlobal;
43
let data = aMessage.data;
44
let tabBrowser = window.gBrowser;
45
46
// target is the <browser>, make sure we're receiving a message
47
// from the foreground tab.
48
if (tabBrowser && browser != tabBrowser.selectedBrowser) {
49
return;
50
}
51
52
this._showPopup(window, data);
53
break;
54
case "FormValidation:HidePopup":
55
this._hidePopup();
56
break;
57
}
58
}
59
60
observe(aSubject, aTopic, aData) {
61
this._hidePopup();
62
}
63
64
handleEvent(aEvent) {
65
switch (aEvent.type) {
66
case "FullZoomChange":
67
case "TextZoomChange":
68
case "ZoomChangeUsingMouseWheel":
69
case "scroll":
70
this._hidePopup();
71
break;
72
case "popuphiding":
73
this._onPopupHiding(aEvent);
74
break;
75
}
76
}
77
78
/*
79
* Internal
80
*/
81
82
_onPopupHiding(aEvent) {
83
aEvent.originalTarget.removeEventListener("popuphiding", this, true);
84
let tabBrowser = aEvent.originalTarget.ownerGlobal.gBrowser;
85
tabBrowser.selectedBrowser.removeEventListener("scroll", this, true);
86
tabBrowser.selectedBrowser.removeEventListener("FullZoomChange", this);
87
tabBrowser.selectedBrowser.removeEventListener("TextZoomChange", this);
88
tabBrowser.selectedBrowser.removeEventListener(
89
"ZoomChangeUsingMouseWheel",
90
this
91
);
92
93
this._panel.hidden = true;
94
this._panel = null;
95
this._anchor.hidden = true;
96
this._anchor = null;
97
}
98
99
/*
100
* Shows the form validation popup at a specified position or updates the
101
* messaging and position if the popup is already displayed.
102
*
103
* @aWindow - the chrome window
104
* @aPanelData - Object that contains popup information
105
* aPanelData stucture detail:
106
* contentRect - the bounding client rect of the target element. If
107
* content is remote, this is relative to the browser, otherwise its
108
* relative to the window.
109
* position - popup positional string constants.
110
* message - the form element validation message text.
111
*/
112
_showPopup(aWindow, aPanelData) {
113
let previouslyShown = !!this._panel;
114
this._panel = aWindow.document.getElementById("invalid-form-popup");
115
this._panel.firstChild.textContent = aPanelData.message;
116
this._panel.hidden = false;
117
118
let tabBrowser = aWindow.gBrowser;
119
this._anchor = tabBrowser.selectedBrowser.popupAnchor;
120
this._anchor.left = aPanelData.contentRect.left;
121
this._anchor.top = aPanelData.contentRect.top;
122
this._anchor.width = aPanelData.contentRect.width;
123
this._anchor.height = aPanelData.contentRect.height;
124
this._anchor.hidden = false;
125
126
// Display the panel if it isn't already visible.
127
if (!previouslyShown) {
128
// Cleanup after the popup is hidden
129
this._panel.addEventListener("popuphiding", this, true);
130
131
// Hide if the user scrolls the page
132
tabBrowser.selectedBrowser.addEventListener("scroll", this, true);
133
tabBrowser.selectedBrowser.addEventListener("FullZoomChange", this);
134
tabBrowser.selectedBrowser.addEventListener("TextZoomChange", this);
135
tabBrowser.selectedBrowser.addEventListener(
136
"ZoomChangeUsingMouseWheel",
137
this
138
);
139
140
// Open the popup
141
this._panel.openPopup(this._anchor, aPanelData.position, 0, 0, false);
142
}
143
}
144
145
/*
146
* Hide the popup if currently displayed. Will fire an event to onPopupHiding
147
* above if visible.
148
*/
149
_hidePopup() {
150
if (this._panel) {
151
this._panel.hidePopup();
152
}
153
}
154
}