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/. */
6 * Chrome side handling of form validation popup.
11 let Cc = Components.classes;
12 let Ci = Components.interfaces;
13 let Cu = Components.utils;
15 this.EXPORTED_SYMBOLS = [ "FormValidationHandler" ];
17 Cu.import("resource://gre/modules/Services.jsm");
19 let FormValidationHandler =
29 let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
30 mm.addMessageListener("FormValidation:ShowPopup", this);
31 mm.addMessageListener("FormValidation:HidePopup", this);
35 let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
36 mm.removeMessageListener("FormValidation:ShowPopup", this);
37 mm.removeMessageListener("FormValidation:HidePopup", this);
42 hidePopup: function () {
50 receiveMessage: function (aMessage) {
51 let window = aMessage.target.ownerDocument.defaultView;
52 let json = aMessage.json;
53 let tabBrowser = window.gBrowser;
54 switch (aMessage.name) {
55 case "FormValidation:ShowPopup":
56 // target is the <browser>, make sure we're receiving a message
57 // from the foreground tab.
58 if (tabBrowser && aMessage.target != tabBrowser.selectedBrowser) {
61 this._showPopup(window, json);
63 case "FormValidation:HidePopup":
69 observe: function (aSubject, aTopic, aData) {
73 handleEvent: function (aEvent) {
74 switch (aEvent.type) {
75 case "FullZoomChange":
76 case "TextZoomChange":
77 case "ZoomChangeUsingMouseWheel":
82 this._onPopupHiding(aEvent);
91 _onPopupHiding: function (aEvent) {
92 aEvent.originalTarget.removeEventListener("popuphiding", this, true);
93 let tabBrowser = aEvent.originalTarget.ownerDocument.getElementById("content");
94 tabBrowser.selectedBrowser.removeEventListener("scroll", this, true);
95 tabBrowser.selectedBrowser.removeEventListener("FullZoomChange", this, false);
96 tabBrowser.selectedBrowser.removeEventListener("TextZoomChange", this, false);
97 tabBrowser.selectedBrowser.removeEventListener("ZoomChangeUsingMouseWheel", this, false);
99 this._panel.hidden = true;
101 this._anchor.hidden = true;
106 * Shows the form validation popup at a specified position or updates the
107 * messaging and position if the popup is already displayed.
109 * @aWindow - the chrome window
110 * @aPanelData - Object that contains popup information
111 * aPanelData stucture detail:
112 * contentRect - the bounding client rect of the target element. If
113 * content is remote, this is relative to the browser, otherwise its
114 * relative to the window.
115 * position - popup positional string constants.
116 * message - the form element validation message text.
118 _showPopup: function (aWindow, aPanelData) {
119 let previouslyShown = !!this._panel;
120 this._panel = aWindow.document.getElementById("invalid-form-popup");
121 this._panel.firstChild.textContent = aPanelData.message;
122 this._panel.hidden = false;
124 let tabBrowser = aWindow.gBrowser;
125 this._anchor = tabBrowser.formValidationAnchor;
126 this._anchor.left = aPanelData.contentRect.left;
127 this._anchor.top = aPanelData.contentRect.top;
128 this._anchor.width = aPanelData.contentRect.width;
129 this._anchor.height = aPanelData.contentRect.height;
130 this._anchor.hidden = false;
132 // Display the panel if it isn't already visible.
133 if (!previouslyShown) {
134 // Cleanup after the popup is hidden
135 this._panel.addEventListener("popuphiding", this, true);
137 // Hide if the user scrolls the page
138 tabBrowser.selectedBrowser.addEventListener("scroll", this, true);
139 tabBrowser.selectedBrowser.addEventListener("FullZoomChange", this, false);
140 tabBrowser.selectedBrowser.addEventListener("TextZoomChange", this, false);
141 tabBrowser.selectedBrowser.addEventListener("ZoomChangeUsingMouseWheel", this, false);
144 this._panel.openPopup(this._anchor, aPanelData.position, 0, 0, false);
149 * Hide the popup if currently displayed. Will fire an event to onPopupHiding
152 _hidePopup: function () {
154 this._panel.hidePopup();