/* 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`. { const { Services } = ChromeUtils.import( "resource://gre/modules/Services.jsm" ); 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("legacy-stack"); stack._notificationBox = this; stack.className = "notificationbox-stack"; stack.appendChild(document.createXULElement("spacer")); stack.addEventListener("transitionend", event => { if ( event.target.localName == "notification" && event.propertyName == "margin-top" ) { this._finishAnimation(); } }); this._insertElementFn(stack); this._stack = 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"); return Array.prototype.filter.call( notifications, 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. * * @param aLabel * The main message text, or a DocumentFragment containing elements to * add as children of the notification's main element. * @param aValue * String identifier of the notification. * @param aImage * URL of the icon image to display. If not specified, a default icon * based on the priority will be shown. * @param aPriority * 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. * @param aButtons * Array of objects defining action buttons: * { * label: * Label of the