|
1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
8 |
|
9 const Ci = Components.interfaces; |
|
10 const Cc = Components.classes; |
|
11 |
|
12 var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"] |
|
13 .getService(Ci.nsIWindowMediator); |
|
14 |
|
15 // Copied from nsILookAndFeel.h, see comments on eMetric_AlertNotificationOrigin |
|
16 const NS_ALERT_HORIZONTAL = 1; |
|
17 const NS_ALERT_LEFT = 2; |
|
18 const NS_ALERT_TOP = 4; |
|
19 |
|
20 const WINDOW_MARGIN = 10; |
|
21 |
|
22 var gOrigin = 0; // Default value: alert from bottom right. |
|
23 var gReplacedWindow = null; |
|
24 var gAlertListener = null; |
|
25 var gAlertTextClickable = false; |
|
26 var gAlertCookie = ""; |
|
27 var gIsReplaced = false; |
|
28 |
|
29 function prefillAlertInfo() { |
|
30 // unwrap all the args.... |
|
31 // arguments[0] --> the image src url |
|
32 // arguments[1] --> the alert title |
|
33 // arguments[2] --> the alert text |
|
34 // arguments[3] --> is the text clickable? |
|
35 // arguments[4] --> the alert cookie to be passed back to the listener |
|
36 // arguments[5] --> the alert origin reported by the look and feel |
|
37 // arguments[6] --> bidi |
|
38 // arguments[7] --> lang |
|
39 // arguments[8] --> replaced alert window (nsIDOMWindow) |
|
40 // arguments[9] --> an optional callback listener (nsIObserver) |
|
41 |
|
42 switch (window.arguments.length) { |
|
43 default: |
|
44 case 10: |
|
45 gAlertListener = window.arguments[9]; |
|
46 case 9: |
|
47 gReplacedWindow = window.arguments[8]; |
|
48 case 6: |
|
49 gOrigin = window.arguments[5]; |
|
50 case 5: |
|
51 gAlertCookie = window.arguments[4]; |
|
52 case 4: |
|
53 gAlertTextClickable = window.arguments[3]; |
|
54 if (gAlertTextClickable) { |
|
55 document.getElementById('alertNotification').setAttribute('clickable', true); |
|
56 document.getElementById('alertTextLabel').setAttribute('clickable', true); |
|
57 } |
|
58 case 3: |
|
59 document.getElementById('alertTextLabel').textContent = window.arguments[2]; |
|
60 case 2: |
|
61 document.getElementById('alertTitleLabel').setAttribute('value', window.arguments[1]); |
|
62 case 1: |
|
63 if (window.arguments[0]) { |
|
64 document.getElementById('alertImage').setAttribute('src', window.arguments[0]); |
|
65 } |
|
66 case 0: |
|
67 break; |
|
68 } |
|
69 } |
|
70 |
|
71 function onAlertLoad() { |
|
72 const ALERT_DURATION_IMMEDIATE = 4000; |
|
73 let alertTextBox = document.getElementById("alertTextBox"); |
|
74 let alertImageBox = document.getElementById("alertImageBox"); |
|
75 alertImageBox.style.minHeight = alertTextBox.scrollHeight + "px"; |
|
76 |
|
77 sizeToContent(); |
|
78 |
|
79 if (gReplacedWindow && !gReplacedWindow.closed) { |
|
80 moveWindowToReplace(gReplacedWindow); |
|
81 gReplacedWindow.gIsReplaced = true; |
|
82 gReplacedWindow.close(); |
|
83 } else { |
|
84 moveWindowToEnd(); |
|
85 } |
|
86 |
|
87 window.addEventListener("XULAlertClose", function() { window.close(); }); |
|
88 |
|
89 if (Services.prefs.getBoolPref("alerts.disableSlidingEffect")) { |
|
90 setTimeout(function() { window.close(); }, ALERT_DURATION_IMMEDIATE); |
|
91 return; |
|
92 } |
|
93 |
|
94 let alertBox = document.getElementById("alertBox"); |
|
95 alertBox.addEventListener("animationend", function hideAlert(event) { |
|
96 if (event.animationName == "alert-animation") { |
|
97 alertBox.removeEventListener("animationend", hideAlert, false); |
|
98 window.close(); |
|
99 } |
|
100 }, false); |
|
101 alertBox.setAttribute("animate", true); |
|
102 |
|
103 if (gAlertListener) { |
|
104 gAlertListener.observe(null, "alertshow", gAlertCookie); |
|
105 } |
|
106 } |
|
107 |
|
108 function moveWindowToReplace(aReplacedAlert) { |
|
109 let heightDelta = window.outerHeight - aReplacedAlert.outerHeight; |
|
110 |
|
111 // Move windows that come after the replaced alert if the height is different. |
|
112 if (heightDelta != 0) { |
|
113 let windows = windowMediator.getEnumerator('alert:alert'); |
|
114 while (windows.hasMoreElements()) { |
|
115 let alertWindow = windows.getNext(); |
|
116 // boolean to determine if the alert window is after the replaced alert. |
|
117 let alertIsAfter = gOrigin & NS_ALERT_TOP ? |
|
118 alertWindow.screenY > aReplacedAlert.screenY : |
|
119 aReplacedAlert.screenY > alertWindow.screenY; |
|
120 if (alertIsAfter) { |
|
121 // The new Y position of the window. |
|
122 let adjustedY = gOrigin & NS_ALERT_TOP ? |
|
123 alertWindow.screenY + heightDelta : |
|
124 alertWindow.screenY - heightDelta; |
|
125 alertWindow.moveTo(alertWindow.screenX, adjustedY); |
|
126 } |
|
127 } |
|
128 } |
|
129 |
|
130 let adjustedY = gOrigin & NS_ALERT_TOP ? aReplacedAlert.screenY : |
|
131 aReplacedAlert.screenY - heightDelta; |
|
132 window.moveTo(aReplacedAlert.screenX, adjustedY); |
|
133 } |
|
134 |
|
135 function moveWindowToEnd() { |
|
136 // Determine position |
|
137 let x = gOrigin & NS_ALERT_LEFT ? screen.availLeft : |
|
138 screen.availLeft + screen.availWidth - window.outerWidth; |
|
139 let y = gOrigin & NS_ALERT_TOP ? screen.availTop : |
|
140 screen.availTop + screen.availHeight - window.outerHeight; |
|
141 |
|
142 // Position the window at the end of all alerts. |
|
143 let windows = windowMediator.getEnumerator('alert:alert'); |
|
144 while (windows.hasMoreElements()) { |
|
145 let alertWindow = windows.getNext(); |
|
146 if (alertWindow != window) { |
|
147 if (gOrigin & NS_ALERT_TOP) { |
|
148 y = Math.max(y, alertWindow.screenY + alertWindow.outerHeight); |
|
149 } else { |
|
150 y = Math.min(y, alertWindow.screenY - window.outerHeight); |
|
151 } |
|
152 } |
|
153 } |
|
154 |
|
155 // Offset the alert by WINDOW_MARGIN pixels from the edge of the screen |
|
156 y += gOrigin & NS_ALERT_TOP ? WINDOW_MARGIN : -WINDOW_MARGIN; |
|
157 x += gOrigin & NS_ALERT_LEFT ? WINDOW_MARGIN : -WINDOW_MARGIN; |
|
158 |
|
159 window.moveTo(x, y); |
|
160 } |
|
161 |
|
162 function onAlertBeforeUnload() { |
|
163 if (!gIsReplaced) { |
|
164 // Move other alert windows to fill the gap left by closing alert. |
|
165 let heightDelta = window.outerHeight + WINDOW_MARGIN; |
|
166 let windows = windowMediator.getEnumerator('alert:alert'); |
|
167 while (windows.hasMoreElements()) { |
|
168 let alertWindow = windows.getNext(); |
|
169 if (alertWindow != window) { |
|
170 if (gOrigin & NS_ALERT_TOP) { |
|
171 if (alertWindow.screenY > window.screenY) { |
|
172 alertWindow.moveTo(alertWindow.screenX, alertWindow.screenY - heightDelta); |
|
173 } |
|
174 } else { |
|
175 if (window.screenY > alertWindow.screenY) { |
|
176 alertWindow.moveTo(alertWindow.screenX, alertWindow.screenY + heightDelta); |
|
177 } |
|
178 } |
|
179 } |
|
180 } |
|
181 } |
|
182 |
|
183 if (gAlertListener) { |
|
184 gAlertListener.observe(null, "alertfinished", gAlertCookie); |
|
185 } |
|
186 } |
|
187 |
|
188 function onAlertClick() { |
|
189 if (gAlertListener && gAlertTextClickable) { |
|
190 gAlertListener.observe(null, "alertclickcallback", gAlertCookie); |
|
191 } |
|
192 |
|
193 window.close(); |
|
194 } |