/* 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. */ constructor(insertElementFn) { this._insertElementFn = insertElementFn; 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" || 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"), ...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 synchronously 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 * notificationIs * Defines a Custom Element name to use as the "is" value on creation. * This allows subclassing the created element. * telemetry * Specifies the telemetry key to use that triggers when the notification * is shown, dismissed and an action taken. This telemetry is a keyed scalar with keys for: * 'shown', 'dismissed' and 'action'. If a button specifies a separate key, * then 'action' is replaced by values specific to each button. The value telemetryFilter * can be used to filter out each type. * telemetryFilter * If assigned, then an array of the telemetry types to send telemetry for. If not set, * then all telemetry is sent. * aButtons * Array of objects defining action buttons: * { * label: * Label of the