JAVASCRIPT   106

queue.js

Guest on 29th July 2021 03:20:33 PM

  1. // private global state for the queue feature
  2. let currentSteps = []
  3.  
  4. /*
  5.  * Global function for chaining sweetAlert popups
  6.  */
  7. export const queue = function (steps) {
  8.   const swal = this
  9.   currentSteps = steps
  10.   const resetQueue = () => {
  11.     currentSteps = []
  12.     document.body.removeAttribute('data-swal2-queue-step')
  13.   }
  14.   let queueResult = []
  15.   return new Promise((resolve) => {
  16.     (function step (i, callback) {
  17.       if (i < currentSteps.length) {
  18.         document.body.setAttribute('data-swal2-queue-step', i)
  19.  
  20.         swal(currentSteps[i]).then((result) => {
  21.           if (typeof result.value !== 'undefined') {
  22.             queueResult.push(result.value)
  23.             step(i + 1, callback)
  24.           } else {
  25.             resetQueue()
  26.             resolve({ dismiss: result.dismiss })
  27.           }
  28.         })
  29.       } else {
  30.         resetQueue()
  31.         resolve({ value: queueResult })
  32.       }
  33.     })(0)
  34.   })
  35. }
  36.  
  37. /*
  38.  * Global function for getting the index of current popup in queue
  39.  */
  40. export const getQueueStep = () => document.body.getAttribute('data-swal2-queue-step')
  41.  
  42. /*
  43.  * Global function for inserting a popup to the queue
  44.  */
  45. export const insertQueueStep = (step, index) => {
  46.   if (index && index < currentSteps.length) {
  47.     return currentSteps.splice(index, 0, step)
  48.   }
  49.   return currentSteps.push(step)
  50. }
  51.  
  52. /*
  53.  * Global function for deleting a popup from the queue
  54.  */
  55. export const deleteQueueStep = (index) => {
  56.   if (typeof currentSteps[index] !== 'undefined') {
  57.     currentSteps.splice(index, 1)
  58.   }
  59. }

Raw Paste


Login or Register to edit or fork this paste. It's free.