toolkit/components/alerts/resources/content/alert.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial