/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; // This is loaded into chrome windows with the subscript loader. If you need to // define globals, wrap in a block to prevent leaking onto `window`. { MozElements.NotificationBox = class NotificationBox { /** * Creates a new class to handle a notification box, but does not add any * elements to the DOM until a notification has to be displayed. * * @param insertElementFn Called with the "notification-stack" element as an * argument when the first notification has to be displayed. * @param {Number} securityDelayMS - Delay in milliseconds until buttons are enabled to * protect against click- and tapjacking. */ constructor(insertElementFn, securityDelayMS = 0) { this._insertElementFn = insertElementFn; this._securityDelayMS = securityDelayMS; this._animating = false; this.currentNotification = null; } get stack() { if (!this._stack) { let stack = document.createXULElement("vbox"); stack._notificationBox = this; stack.className = "notificationbox-stack"; stack.addEventListener("transitionend", event => { if ( event.target.localName == "notification-message" && event.propertyName == "margin-top" ) { this._finishAnimation(); } }); this._stack = stack; this._insertElementFn(stack); } return this._stack; } get _allowAnimation() { return window.matchMedia("(prefers-reduced-motion: no-preference)") .matches; } get allNotifications() { // Don't create any DOM if no new notification has been added yet. if (!this._stack) { return []; } var closedNotification = this._closedNotification; var notifications = [ ...this.stack.getElementsByTagName("notification-message"), ]; return notifications.filter(n => n != closedNotification); } getNotificationWithValue(aValue) { var notifications = this.allNotifications; for (var n = notifications.length - 1; n >= 0; n--) { if (aValue == notifications[n].getAttribute("value")) { return notifications[n]; } } return null; } /** * Creates a element and shows it. The calling code can modify * the element asynchronously to add features to the notification. * * aType * String identifier that can uniquely identify the type of the notification. * aNotification * Object that contains any of the following properties, where only the * priority must be specified: * priority * One of the PRIORITY_ constants. These determine the appearance of * the notification based on severity (using the "type" attribute), and * only the notification with the highest priority is displayed. * label * The main message text (as string), or object (with l10n-id, l10n-args), * or a DocumentFragment containing elements to * add as children of the notification's main element. * eventCallback * This may be called with the "removed", "dismissed" or "disconnected" * parameter: * removed - notification has been removed * dismissed - user dismissed notification * disconnected - notification removed in any way * aButtons * Array of objects defining action buttons: * { * label: * Label of the