Advertisement
Th3Ward3n

Global Window Alert System

May 28th, 2025
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.64 KB | None | 0 0
  1. <div id="alert-container">
  2.     <!-- ERROR -->
  3.     <dialog id="alert-error" class="a-error">
  4.         <div class="spc-btw gap-15p">
  5.             <p>== Text Content ==</p>
  6.             <button autofocus onclick="dismissAlert('error');">Close</button>
  7.         </div>
  8.     </dialog>
  9.     <!-- WARNING -->
  10.     <dialog id="alert-warning" class="a-warning">
  11.         <div class="spc-btw gap-15p">
  12.             <p>== Text Content ==</p>
  13.             <button autofocus onclick="dismissAlert('warning');">Close</button>
  14.         </div>
  15.     </dialog>
  16.     <!-- INFO -->
  17.     <dialog id="alert-info" class="a-info">
  18.         <div class="spc-btw gap-15p">
  19.             <p>== Text Content ==</p>
  20.             <button autofocus onclick="dismissAlert('info');">Close</button>
  21.         </div>
  22.     </dialog>
  23.     <!-- SUCCESS -->
  24.     <dialog id="alert-success" class="a-success">
  25.         <div class="spc-btw gap-15p">
  26.             <p>== Text Content ==</p>
  27.             <button autofocus onclick="dismissAlert('success');">Close</button>
  28.         </div>
  29.     </dialog>
  30. </div>
  31. <script>
  32.     /** @typedef {"error" | "warning" | "info" | "success"} ValidAlertTypes */
  33.     const validAlertTypeList = ["error", "warning", "info", "success"];
  34.     /**
  35.      * @param alertType {ValidAlertTypes}
  36.      * @returns {HTMLDialogElement}
  37.      */
  38.     const findDialog = (alertType) => {
  39.         const diaEle = document.querySelector(`dialog#alert-${alertType}`);
  40.         if (!diaEle) throw new Error(`[WARNING] Failed to find Dialog with id alert-${alertType}!`);
  41.         return diaEle;
  42.     };
  43.  
  44.     class AlertManager {
  45.         parentDialog;
  46.         textContainer;
  47.         /** @param alertType {ValidAlertTypes} */
  48.         constructor(alertType) {
  49.             this.parentDialog = findDialog(alertType);
  50.             this.textContainer = Object.values(
  51.                 Object.values(this.parentDialog.childNodes)
  52.                 .find((child) => child instanceof HTMLDivElement).childNodes
  53.             ).find((child) => child instanceof HTMLParagraphElement);
  54.         }
  55.  
  56.         get dlog() { return this.parentDialog; }
  57.         get isOpen() { return this.dlog.hasAttribute('open'); }
  58.         /** @param txt {string} */
  59.         set txtDisplay(txt) { this.textContainer.textContent = txt; }
  60.  
  61.         // TODO: Calculate position of prexisting dialog boxes currently `open`
  62.         // Apply position shift to prevent overlap, accounting for translation distance to take up `closed` boxes
  63.         show() { this.dlog.show(); }
  64.         close() { this.dlog.close(); }
  65.     }
  66.  
  67.     const dialogContainer = {
  68.         error: new AlertManager('error'),
  69.         warning: new AlertManager('warning'),
  70.         info: new AlertManager('info'),
  71.         success: new AlertManager('success')
  72.     };
  73.  
  74.     /** @param alertType {ValidAlertTypes} */
  75.     function dismissAlert(alertType) {
  76.         console.log(alertType);
  77.         if (dialogContainer[alertType].isOpen) dialogContainer[alertType].close();
  78.     }
  79.  
  80.    
  81.     window.addEventListener("message", (event) => {
  82.         /**
  83.          * @typedef {
  84.             {
  85.                message: string,
  86.                type: ValidAlertTypes
  87.             }
  88.          } ValidMsgPayload
  89.          */
  90.         if (event.origin !== "http://127.0.0.1:4450") return;
  91.         /** @type {ValidMsgPayload} */
  92.         const msgPayload = event.data;
  93.         if (typeof msgPayload === 'string' || !validAlertTypeList.includes(msgPayload.type)) return;
  94.        
  95.         dialogContainer[msgPayload.type].txtDisplay = msgPayload.message;
  96.         dialogContainer[msgPayload.type].show();
  97.  
  98.         setTimeout(() => { dialogContainer[msgPayload.type].close(); }, 4400);
  99.     });
  100. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement