Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SweetAlert2
  2. // github.com/limonte/sweetalert2
  3.  
  4. ;(function(window, document) {
  5.   'use strict';
  6.  
  7.   window.swalClasses = {
  8.     modal: 'sweet-alert',
  9.     overlay: 'sweet-overlay',
  10.     confirm: 'sweet-confirm',
  11.     cancel: 'sweet-cancel'
  12.   };
  13.  
  14.   var mediaqueryId = 'sweet-alert-mediaquery';
  15.   var alertTypes   = ['error', 'warning', 'info', 'success'];
  16.   var defaultParams = {
  17.     title: '',
  18.     text: '',
  19.     html: '',
  20.     type: null,
  21.     animation: true,
  22.     allowOutsideClick: true,
  23.     allowEscapeKey: true,
  24.     showConfirmButton: true,
  25.     showCancelButton: false,
  26.     closeOnConfirm: true,
  27.     closeOnCancel: true,
  28.     confirmButtonText: 'OK',
  29.     confirmButtonColor: '#3085d6',
  30.     confirmButtonClass: 'btn btn-info btn-fill', // param overwritten by Creative Tim Staff for design consistency purpose
  31.     cancelButtonText: 'Cancel',
  32.     cancelButtonColor: '#aaa',
  33.     cancelButtonClass: 'btn btn-danger btn-fill', // param overwritten by Creative Tim Staff for design consistency purpose
  34.     buttonsStyling: false,    // param overwritten by Creative Tim Staff for design consistency purpose
  35.     reverseButtons: false,
  36.     imageUrl: null,
  37.     imageWidth: null,
  38.     imageHeight: null,
  39.     imageClass: null,
  40.     timer: null,
  41.     width: 500,
  42.     padding: 20,
  43.     background: '#fff'
  44.   };
  45.  
  46.   /*
  47.    * Manipulate DOM
  48.    */
  49.   var getModal = function() {
  50.     return document.querySelector('.' + window.swalClasses.modal);
  51.   };
  52.  
  53.   var getOverlay = function() {
  54.     return document.querySelector('.' + window.swalClasses.overlay);
  55.   };
  56.  
  57.   var hasClass = function(elem, className) {
  58.     return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
  59.   };
  60.  
  61.   var addClass = function(elem, className) {
  62.     if (className && !hasClass(elem, className)) {
  63.       elem.className += ' ' + className;
  64.     }
  65.   };
  66.  
  67.   var removeClass = function(elem, className) {
  68.     var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
  69.     if (hasClass(elem, className)) {
  70.       while (newClass.indexOf(' ' + className + ' ') >= 0) {
  71.         newClass = newClass.replace(' ' + className + ' ', ' ');
  72.       }
  73.       elem.className = newClass.replace(/^\s+|\s+$/g, '');
  74.     }
  75.   };
  76.  
  77.   var escapeHtml = function(str) {
  78.     var div = document.createElement('div');
  79.     div.appendChild(document.createTextNode(str));
  80.     return div.innerHTML;
  81.   };
  82.  
  83.   var _show = function(elem) {
  84.     elem.style.opacity = '';
  85.     elem.style.display = 'block';
  86.   };
  87.  
  88.   var show = function(elems) {
  89.     if (elems && !elems.length) {
  90.       return _show(elems);
  91.     }
  92.     for (var i = 0; i < elems.length; ++i) {
  93.       _show(elems[i]);
  94.     }
  95.   };
  96.  
  97.   var _hide = function(elem) {
  98.     elem.style.opacity = '';
  99.     elem.style.display = 'none';
  100.   };
  101.  
  102.   var hide = function(elems) {
  103.     if (elems && !elems.length) {
  104.       return _hide(elems);
  105.     }
  106.     for (var i = 0; i < elems.length; ++i) {
  107.       _hide(elems[i]);
  108.     }
  109.   };
  110.  
  111.   var removeStyleProperty = function(elem, property) {
  112.     if (elem.style.removeProperty) {
  113.       elem.style.removeProperty(property);
  114.     } else {
  115.       elem.style.removeAttribute(property);
  116.     }
  117.   }
  118.  
  119.   var isDescendant = function(parent, child) {
  120.     var node = child.parentNode;
  121.     while (node !== null) {
  122.       if (node === parent) {
  123.         return true;
  124.       }
  125.       node = node.parentNode;
  126.     }
  127.     return false;
  128.   };
  129.  
  130.   var getTopMargin = function(elem) {
  131.     elem.style.left = '-9999px';
  132.     elem.style.display = 'block';
  133.  
  134.     var height = elem.clientHeight;
  135.     var padding = parseInt(getComputedStyle(elem).getPropertyValue('padding'), 10);
  136.  
  137.     elem.style.left = '';
  138.     elem.style.display = 'none';
  139.     return ('-' + parseInt(height / 2 + padding) + 'px');
  140.   };
  141.  
  142.   var fadeIn = function(elem, interval) {
  143.     if (+elem.style.opacity < 1) {
  144.       interval = interval || 16;
  145.       elem.style.opacity = 0;
  146.       elem.style.display = 'block';
  147.       var last = +new Date();
  148.       var tick = function() {
  149.         elem.style.opacity = +elem.style.opacity + (new Date() - last) / 100;
  150.         last = +new Date();
  151.  
  152.         if (+elem.style.opacity < 1) {
  153.           setTimeout(tick, interval);
  154.         }
  155.       };
  156.       tick();
  157.     }
  158.     elem.style.display = 'block'; //fallback IE8
  159.   };
  160.  
  161.   var fireClick = function(node) {
  162.     // Taken from http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/
  163.     // Then fixed for today's Chrome browser.
  164.     if (typeof MouseEvent === 'function') {
  165.       // Up-to-date approach
  166.       var mevt = new MouseEvent('click', {
  167.         view: window,
  168.         bubbles: false,
  169.         cancelable: true
  170.       });
  171.       node.dispatchEvent(mevt);
  172.     } else if (document.createEvent) {
  173.       // Fallback
  174.       var evt = document.createEvent('MouseEvents');
  175.       evt.initEvent('click', false, false);
  176.       node.dispatchEvent(evt);
  177.     } else if (document.createEventObject) {
  178.       node.fireEvent('onclick') ;
  179.     } else if (typeof node.onclick === 'function') {
  180.       node.onclick();
  181.     }
  182.   };
  183.  
  184.   var stopEventPropagation = function(e) {
  185.     // In particular, make sure the space bar doesn't scroll the main window.
  186.     if (typeof e.stopPropagation === 'function') {
  187.       e.stopPropagation();
  188.       e.preventDefault();
  189.     } else if (window.event && window.event.hasOwnProperty('cancelBubble')) {
  190.       window.event.cancelBubble = true;
  191.     }
  192.   };
  193.  
  194.   // Remember state in cases where opening and handling a modal will fiddle with it.
  195.   var previousActiveElement;
  196.   var previousDocumentClick;
  197.   var previousWindowKeyDown;
  198.   var lastFocusedButton;
  199.  
  200.   /*
  201.    * Global sweetAlert function
  202.    */
  203.   window.sweetAlert = window.swal = function() {
  204.     // Copy arguments to the local args variable
  205.     var args = arguments;
  206.     var modal = getModal();
  207.     if (modal !== null) {
  208.         if (hasClass(modal, 'visible')) {
  209.           resetPrevState();
  210.         }
  211.         // If getModal returns values then continue
  212.         modalDependant.apply(this, args);
  213.     } else {
  214.         // If getModal returns null i.e. no matches, then set up a interval event to check the return value until it is not null
  215.         var modalCheckInterval = setInterval(function() {
  216.           if (getModal() !== null) {
  217.             clearInterval(modalCheckInterval);
  218.             modalDependant.apply(this, args);
  219.           }
  220.       }, 100);
  221.     }
  222.   };
  223.  
  224.   /*
  225.    * Global function to close sweetAlert
  226.    */
  227.   window.sweetAlert.closeModal = window.swal.closeModal = function() {
  228.     closeModal();
  229.   };
  230.  
  231.   /*
  232.    * Global function to click 'Confirm' button
  233.    */
  234.   window.sweetAlert.clickConfirm = window.swal.clickConfirm = function() {
  235.     var modal = getModal();
  236.     var $confirmButton = modal.querySelector('button.' + window.swalClasses.confirm);
  237.     $confirmButton.click();
  238.   };
  239.  
  240.   /*
  241.    * Global function to click 'Cancel' button
  242.    */
  243.   window.sweetAlert.clickCancel = window.swal.clickCancel = function() {
  244.     var modal = getModal();
  245.     var $cancelButton = modal.querySelector('button.' + window.swalClasses.cancel);
  246.     $cancelButton.click();
  247.   };
  248.  
  249.   function modalDependant() {
  250.  
  251.     if (arguments[0] === undefined) {
  252.       window.console.error('sweetAlert expects at least 1 attribute!');
  253.       return false;
  254.     }
  255.  
  256.     var params = extend({}, defaultParams);
  257.  
  258.     switch (typeof arguments[0]) {
  259.  
  260.       case 'string':
  261.         params.title = arguments[0];
  262.         params.text  = arguments[1] || '';
  263.         params.type  = arguments[2] || '';
  264.  
  265.         break;
  266.  
  267.       case 'object':
  268.         params.title              = arguments[0].title || defaultParams.title;
  269.         params.text               = arguments[0].text || defaultParams.text;
  270.         params.html               = arguments[0].html || defaultParams.html;
  271.         params.type               = arguments[0].type || defaultParams.type;
  272.         params.animation          = arguments[0].animation !== undefined ? arguments[0].animation : defaultParams.animation;
  273.         params.customClass        = arguments[0].customClass || params.customClass;
  274.         params.allowOutsideClick  = arguments[0].allowOutsideClick !== undefined ? arguments[0].allowOutsideClick : defaultParams.allowOutsideClick;
  275.         params.allowEscapeKey     = arguments[0].allowEscapeKey !== undefined ? arguments[0].allowEscapeKey : defaultParams.allowEscapeKey;
  276.         params.showConfirmButton  = arguments[0].showConfirmButton !== undefined ? arguments[0].showConfirmButton : defaultParams.showConfirmButton;
  277.         params.showCancelButton   = arguments[0].showCancelButton !== undefined ? arguments[0].showCancelButton : defaultParams.showCancelButton;
  278.         params.closeOnConfirm     = arguments[0].closeOnConfirm !== undefined ? arguments[0].closeOnConfirm : defaultParams.closeOnConfirm;
  279.         params.closeOnCancel      = arguments[0].closeOnCancel !== undefined ? arguments[0].closeOnCancel : defaultParams.closeOnCancel;
  280.         params.timer              = parseInt(arguments[0].timer) || defaultParams.timer;
  281.         params.width              = parseInt(arguments[0].width) || defaultParams.width;
  282.         params.padding            = parseInt(arguments[0].padding) || defaultParams.padding;
  283.         params.background         = arguments[0].background !== undefined ? arguments[0].background : defaultParams.background;
  284.  
  285.         params.confirmButtonText  = arguments[0].confirmButtonText || defaultParams.confirmButtonText;
  286.         params.confirmButtonColor = arguments[0].confirmButtonColor || defaultParams.confirmButtonColor;
  287.         params.confirmButtonClass = arguments[0].confirmButtonClass || params.confirmButtonClass;
  288.         params.cancelButtonText   = arguments[0].cancelButtonText || defaultParams.cancelButtonText;
  289.         params.cancelButtonColor  = arguments[0].cancelButtonColor || defaultParams.cancelButtonColor;
  290.         params.cancelButtonClass  = arguments[0].cancelButtonClass || params.cancelButtonClass;
  291.         params.buttonsStyling     = arguments[0].buttonsStyling !== undefined ? arguments[0].buttonsStyling : defaultParams.buttonsStyling;
  292.         params.reverseButtons     = arguments[0].reverseButtons !== undefined ? arguments[0].reverseButtons : defaultParams.reverseButtons;
  293.         params.imageUrl           = arguments[0].imageUrl || defaultParams.imageUrl;
  294.         params.imageWidth         = arguments[0].imageWidth || defaultParams.imageWidth;
  295.         params.imageHeight        = arguments[0].imageHeight || defaultParams.imageHeight;
  296.         params.imageClass         = arguments[0].imageClass || defaultParams.imageClass;
  297.         params.callback           = arguments[1] || null;
  298.  
  299.          /*
  300.          * Global function to call sweetAlert callback
  301.          */
  302.         window.sweetAlert.callback = window.swal.callback = function(isConfirm) {
  303.           if (typeof params.callback === 'function') {
  304.             params.callback(isConfirm);
  305.           }
  306.         };
  307.  
  308.         break;
  309.  
  310.       default:
  311.         window.console.error('Unexpected type of argument! Expected "string" or "object", got ' + typeof arguments[0]);
  312.         return false;
  313.  
  314.     }
  315.  
  316.     setParameters(params);
  317.     fixVerticalPosition();
  318.     openModal();
  319.  
  320.     // Modal interactions
  321.     var modal = getModal();
  322.  
  323.     // Close on timer
  324.     if (params.timer) {
  325.       modal.timeout = setTimeout(function() {
  326.         closeModal();
  327.         if (typeof params.callback === 'function') {
  328.           params.callback();
  329.         }
  330.       }, params.timer);
  331.     }
  332.  
  333.     // Mouse interactions
  334.     var onButtonEvent = function(event) {
  335.       var e = event || window.event;
  336.       var target = e.target || e.srcElement;
  337.       var targetedConfirm = hasClass(target, window.swalClasses.confirm);
  338.       var targetedCancel  = hasClass(target, window.swalClasses.cancel);
  339.       var modalIsVisible  = hasClass(modal, 'visible');
  340.  
  341.       switch (e.type) {
  342.         case 'mouseover':
  343.         case 'mouseup':
  344.         case 'focus':
  345.           if (params.buttonsStyling) {
  346.             if (targetedConfirm) {
  347.               target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.1);
  348.             } else if (targetedCancel) {
  349.               target.style.backgroundColor = colorLuminance(params.cancelButtonColor, -0.1);
  350.             }
  351.           }
  352.           break;
  353.         case 'mouseout':
  354.         case 'blur':
  355.           if (params.buttonsStyling) {
  356.             if (targetedConfirm) {
  357.               target.style.backgroundColor = params.confirmButtonColor;
  358.             } else if (targetedCancel) {
  359.               target.style.backgroundColor = params.cancelButtonColor;
  360.             }
  361.           }
  362.           break;
  363.         case 'mousedown':
  364.           if (params.buttonsStyling) {
  365.             if (targetedConfirm) {
  366.               target.style.backgroundColor = colorLuminance(params.confirmButtonColor, -0.2);
  367.             } else if (targetedCancel) {
  368.               target.style.backgroundColor = colorLuminance(params.cancelButtonColor, -0.2);
  369.             }
  370.           }
  371.           break;
  372.         case 'click':
  373.           // Clicked 'confirm'
  374.           if (targetedConfirm && params.callback && modalIsVisible) {
  375.             params.callback(true);
  376.  
  377.             if (params.closeOnConfirm) {
  378.               closeModal();
  379.             }
  380.  
  381.           // Clicked 'cancel'
  382.           } else if (params.callback && modalIsVisible) {
  383.  
  384.             // Check if callback function expects a parameter (to track cancel actions)
  385.             if (params.callback.length > 0) {
  386.               params.callback(false);
  387.             }
  388.  
  389.             if (params.closeOnCancel) {
  390.               closeModal();
  391.             }
  392.           } else {
  393.             closeModal();
  394.           }
  395.  
  396.           break;
  397.       }
  398.     };
  399.  
  400.     var $buttons = modal.querySelectorAll('button');
  401.     var i;
  402.     for (i = 0; i < $buttons.length; i++) {
  403.       $buttons[i].onclick     = onButtonEvent;
  404.       $buttons[i].onmouseover = onButtonEvent;
  405.       $buttons[i].onmouseout  = onButtonEvent;
  406.       $buttons[i].onmousedown = onButtonEvent;
  407.     }
  408.  
  409.     // Remember the current document.onclick event.
  410.     previousDocumentClick = document.onclick;
  411.     document.onclick = function(event) {
  412.       var e = event || window.event;
  413.       var target = e.target || e.srcElement;
  414.  
  415.       if (target === getOverlay() && params.allowOutsideClick) {
  416.         closeModal();
  417.         if (typeof params.callback === 'function') {
  418.           params.callback();
  419.         }
  420.       }
  421.     };
  422.  
  423.     // Keyboard interactions
  424.     var $confirmButton = modal.querySelector('button.' + window.swalClasses.confirm);
  425.     var $cancelButton = modal.querySelector('button.' + window.swalClasses.cancel);
  426.     var $modalElements = modal.querySelectorAll('button, input:not([type=hidden]), textarea, select');
  427.     for (i = 0; i < $modalElements.length; i++) {
  428.       $modalElements[i].onfocus = onButtonEvent;
  429.       $modalElements[i].onblur = onButtonEvent;
  430.     }
  431.  
  432.     // Reverse buttons if needed
  433.     if (params.reverseButtons) {
  434.       $confirmButton.parentNode.insertBefore($cancelButton, $confirmButton);
  435.     }
  436.  
  437.     // Focus the first element (input or button)
  438.     setFocus(-1, 1);
  439.  
  440.     function setFocus(index, increment) {
  441.       // search for visible elements and select the next possible match
  442.       for (var i = 0; i < $modalElements.length; i++) {
  443.         index = index + increment;
  444.  
  445.         // rollover to first item
  446.         if (index === $modalElements.length) {
  447.           index = 0;
  448.  
  449.         // go to last item
  450.         } else if (index === -1) {
  451.           index = $modalElements.length - 1;
  452.         }
  453.  
  454.         // determine if element is visible, the following is borrowed from jqeury $(elem).is(':visible') implementation
  455.         if (
  456.           $modalElements[index].offsetWidth ||
  457.           $modalElements[index].offsetHeight ||
  458.           $modalElements[index].getClientRects().length
  459.         ) {
  460.           $modalElements[index].focus();
  461.           return;
  462.         }
  463.       }
  464.     }
  465.  
  466.     function handleKeyDown(event) {
  467.       var e = event || window.event;
  468.       var keyCode = e.keyCode || e.which;
  469.       var modalIsVisible = hasClass(modal, 'visible');
  470.  
  471.       if ([9,13,32,27].indexOf(keyCode) === -1) {
  472.         // Don't do work on keys we don't care about.
  473.         return;
  474.       }
  475.  
  476.       var $targetElement = e.target || e.srcElement;
  477.  
  478.       var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
  479.       for (var i = 0; i < $modalElements.length; i++) {
  480.         if ($targetElement === $modalElements[i]) {
  481.           btnIndex = i;
  482.           break;
  483.         }
  484.       }
  485.  
  486.       if (keyCode === 9) {
  487.         // TAB
  488.  
  489.         // Should only happen if modal is visible
  490.         if (!modalIsVisible) {
  491.           return;
  492.         }
  493.  
  494.         if (!e.shiftKey) {
  495.           // Cycle to the next button
  496.           setFocus(btnIndex, 1);
  497.         } else {
  498.           // Cycle to the prev button
  499.           setFocus(btnIndex, -1);
  500.         }
  501.  
  502.         stopEventPropagation(e);
  503.  
  504.       } else {
  505.         if (keyCode === 13 || keyCode === 32) {
  506.           if (btnIndex === -1) {
  507.             // ENTER/SPACE clicked outside of a button.
  508.             fireClick($confirmButton, e);
  509.           }
  510.         } else if (keyCode === 27 && params.allowEscapeKey === true) {
  511.           fireClick($cancelButton, e);
  512.         }
  513.       }
  514.     }
  515.  
  516.     previousWindowKeyDown = window.onkeydown;
  517.     window.onkeydown = handleKeyDown;
  518.  
  519.     // Loading state
  520.     if (params.buttonsStyling) {
  521.       $confirmButton.style.borderLeftColor = params.confirmButtonColor;
  522.       $confirmButton.style.borderRightColor = params.confirmButtonColor;
  523.     }
  524.  
  525.     window.swal.toggleLoading = function() {
  526.       $confirmButton.disabled = !$confirmButton.disabled;
  527.       $cancelButton.disabled = !$cancelButton.disabled;
  528.     };
  529.  
  530.     window.swal.enableButtons = function() {
  531.       $confirmButton.disabled = false;
  532.       $cancelButton.disabled = false;
  533.     };
  534.  
  535.     window.swal.disableButtons = function() {
  536.       $confirmButton.disabled = true;
  537.       $cancelButton.disabled = true;
  538.     };
  539.  
  540.     swal.enableButtons();
  541.  
  542.     window.onfocus = function() {
  543.       // When the user has focused away and focused back from the whole window.
  544.       window.setTimeout(function() {
  545.         // Put in a timeout to jump out of the event sequence. Calling focus() in the event
  546.         // sequence confuses things.
  547.         if (lastFocusedButton !== undefined) {
  548.           lastFocusedButton.focus();
  549.           lastFocusedButton = undefined;
  550.         }
  551.       }, 0);
  552.     };
  553.   }
  554.  
  555.   /*
  556.    * Add modal + overlay to DOM
  557.    */
  558.   window.swal.init = function() {
  559.     var sweetHTML = '<div class="' + window.swalClasses.overlay + '" tabIndex="-1"></div><div class="' + window.swalClasses.modal + '" style="display: none" tabIndex="-1"><div class="icon error"><span class="x-mark"><span class="line left"></span><span class="line right"></span></span></div><div class="icon warning"> <span class="body"></span> <span class="dot"></span> </div> <div class="icon info"></div> <div class="icon success"> <span class="line tip"></span> <span class="line long"></span> <div class="placeholder"></div> <div class="fix"></div> </div> <img class="sweet-image"> <h2>Title</h2><div class="sweet-content">Text</div><hr class="sweet-spacer"><button class="' + window.swalClasses.confirm + '">OK</button><button class="' + window.swalClasses.cancel + '">Cancel</button></div>';
  560.     var sweetWrap = document.createElement('div');
  561.     sweetWrap.className = 'sweet-container';
  562.  
  563.     sweetWrap.innerHTML = sweetHTML;
  564.  
  565.     document.body.appendChild(sweetWrap);
  566.   };
  567.  
  568.   /**
  569.    * Set default params for each popup
  570.    * @param {Object} userParams
  571.    */
  572.   window.swal.setDefaults = function(userParams) {
  573.     if (!userParams) {
  574.       throw new Error('userParams is required');
  575.     }
  576.     if (typeof userParams !== 'object') {
  577.       throw new Error('userParams has to be a object');
  578.     }
  579.  
  580.     extend(defaultParams, userParams);
  581.   };
  582.  
  583.   /*
  584.    * Set type, text and actions on modal
  585.    */
  586.   function setParameters(params) {
  587.     var modal = getModal();
  588.  
  589.     // set modal width, padding and margin-left
  590.     modal.style.width = params.width + 'px';
  591.     modal.style.padding = params.padding + 'px';
  592.     modal.style.marginLeft = -params.width / 2 + 'px';
  593.     modal.style.background = params.background;
  594.  
  595.     // add dynamic media query css
  596.     var head = document.getElementsByTagName('head')[0];
  597.     var cssNode = document.createElement('style');
  598.     cssNode.type = 'text/css';
  599.     cssNode.id = mediaqueryId;
  600.     cssNode.innerHTML =
  601.       '@media screen and (max-width: ' + params.width + 'px) {' +
  602.         '.' + window.swalClasses.modal + ' {' +
  603.           'max-width: 100%;' +
  604.           'left: 0 !important;' +
  605.           'margin-left: 0 !important;' +
  606.         '}' +
  607.       '}';
  608.     head.appendChild(cssNode);
  609.  
  610.     var $title = modal.querySelector('h2');
  611.     var $content = modal.querySelector('div.sweet-content');
  612.     var $confirmBtn = modal.querySelector('button.' + window.swalClasses.confirm);
  613.     var $cancelBtn = modal.querySelector('button.' + window.swalClasses.cancel);
  614.     var $btnSpacer = modal.querySelector('hr.sweet-spacer');
  615.  
  616.     // Title
  617.     $title.innerHTML = escapeHtml(params.title).split('\n').join('<br>');
  618.  
  619.     // Content
  620.     if (window.jQuery) {
  621.       $content = $($content).html(params.html || ('<p>' + escapeHtml(params.text.split('\n').join('<br>'))) + '</p>');
  622.     } else {
  623.       $content.innerHTML = params.html || ('<p>' + escapeHtml(params.text.split('\n').join('<br>')) + '</p>');
  624.       if ($content.innerHTML) {
  625.         show($content);
  626.       }
  627.     }
  628.  
  629.     // Custom Class
  630.     modal.className = window.swalClasses.modal;
  631.     if (params.customClass) {
  632.       addClass(modal, params.customClass);
  633.     }
  634.  
  635.     // Icon
  636.     hide(modal.querySelectorAll('.icon'));
  637.     if (params.type) {
  638.       var validType = false;
  639.       for (var i = 0; i < alertTypes.length; i++) {
  640.         if (params.type === alertTypes[i]) {
  641.           validType = true;
  642.           break;
  643.         }
  644.       }
  645.       if (!validType) {
  646.         window.console.error('Unknown alert type: ' + params.type);
  647.         return false;
  648.       }
  649.       var $icon = modal.querySelector('.icon.' + params.type);
  650.       show($icon);
  651.  
  652.       // Animate icon
  653.       switch (params.type) {
  654.         case 'success':
  655.           addClass($icon, 'animate');
  656.           addClass($icon.querySelector('.tip'), 'animate-success-tip');
  657.           addClass($icon.querySelector('.long'), 'animate-success-long');
  658.           break;
  659.         case 'error':
  660.           addClass($icon, 'animate-error-icon');
  661.           addClass($icon.querySelector('.x-mark'), 'animate-x-mark');
  662.           break;
  663.         case 'warning':
  664.           addClass($icon, 'pulse-warning');
  665.           addClass($icon.querySelector('.body'), 'pulse-warning-ins');
  666.           addClass($icon.querySelector('.dot'), 'pulse-warning-ins');
  667.           break;
  668.       }
  669.  
  670.     }
  671.  
  672.     // Custom image
  673.     var $customImage = modal.querySelector('.sweet-image');
  674.     if (params.imageUrl) {
  675.       $customImage.setAttribute('src', params.imageUrl);
  676.       show($customImage);
  677.  
  678.       if (params.imageWidth) {
  679.         $customImage.setAttribute('width', params.imageWidth);
  680.       }
  681.  
  682.       if (params.imageHeight) {
  683.         $customImage.setAttribute('height', params.imageHeight);
  684.       }
  685.  
  686.       if (params.imageClass) {
  687.         addClass($customImage, params.imageClass);
  688.       }
  689.     } else {
  690.       hide($customImage);
  691.     }
  692.  
  693.     // Cancel button
  694.     if (params.showCancelButton) {
  695.       $cancelBtn.style.display = 'inline-block';
  696.     } else {
  697.       hide($cancelBtn);
  698.     }
  699.  
  700.     // Confirm button
  701.     if (params.showConfirmButton) {
  702.       removeStyleProperty($confirmBtn, 'display');
  703.     } else {
  704.       hide($confirmBtn);
  705.     }
  706.  
  707.     // Buttons spacer
  708.     if (!params.showConfirmButton && !params.showCancelButton) {
  709.       hide($btnSpacer);
  710.     } else {
  711.       show($btnSpacer);
  712.     }
  713.  
  714.     // Edit text on cancel and confirm buttons
  715.     $confirmBtn.innerHTML = escapeHtml(params.confirmButtonText);
  716.     $cancelBtn.innerHTML = escapeHtml(params.cancelButtonText);
  717.  
  718.     // Set buttons to selected background colors
  719.     if (params.buttonsStyling) {
  720.       $confirmBtn.style.backgroundColor = params.confirmButtonColor;
  721.       $cancelBtn.style.backgroundColor = params.cancelButtonColor;
  722.     }
  723.  
  724.     // Add buttons custom classes
  725.     $confirmBtn.className = window.swalClasses.confirm;
  726.     addClass($confirmBtn, params.confirmButtonClass);
  727.     $cancelBtn.className = window.swalClasses.cancel;
  728.     addClass($cancelBtn, params.cancelButtonClass);
  729.  
  730.     // Buttons styling
  731.     if (params.buttonsStyling) {
  732.       addClass($confirmBtn, 'styled');
  733.       addClass($cancelBtn, 'styled');
  734.     } else {
  735.       removeClass($confirmBtn, 'styled');
  736.       removeClass($cancelBtn, 'styled');
  737.  
  738.       $confirmBtn.style.backgroundColor = $confirmBtn.style.borderLeftColor = $confirmBtn.style.borderRightColor = '';
  739.       $cancelBtn.style.backgroundColor = $cancelBtn.style.borderLeftColor = $cancelBtn.style.borderRightColor = '';
  740.     }
  741.  
  742.     // CSS animation
  743.     if (params.animation === true) {
  744.       removeClass(modal, 'no-animation');
  745.     } else {
  746.       addClass(modal, 'no-animation');
  747.     }
  748.   }
  749.  
  750.   /*
  751.    * Set hover, active and focus-states for buttons (source: http://www.sitepoint.com/javascript-generate-lighter-darker-color)
  752.    */
  753.   function colorLuminance(hex, lum) {
  754.     // Validate hex string
  755.     hex = String(hex).replace(/[^0-9a-f]/gi, '');
  756.     if (hex.length < 6) {
  757.       hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  758.     }
  759.     lum = lum || 0;
  760.  
  761.     // Convert to decimal and change luminosity
  762.     var rgb = '#';
  763.     for (var i = 0; i < 3; i++) {
  764.       var c = parseInt(hex.substr(i * 2,2), 16);
  765.       c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
  766.       rgb += ('00' + c).substr(c.length);
  767.     }
  768.  
  769.     return rgb;
  770.   }
  771.  
  772.   function extend(a, b) {
  773.     for (var key in b) {
  774.       if (b.hasOwnProperty(key)) {
  775.         a[key] = b[key];
  776.       }
  777.     }
  778.  
  779.     return a;
  780.   }
  781.  
  782.   /*
  783.    * Animations
  784.    */
  785.   function openModal() {
  786.     var modal = getModal();
  787.     fadeIn(getOverlay(), 10);
  788.     show(modal);
  789.     addClass(modal, 'show-sweet-alert');
  790.     removeClass(modal, 'hide-sweet-alert');
  791.  
  792.     previousActiveElement = document.activeElement;
  793.  
  794.     setTimeout(function() {
  795.       addClass(modal, 'visible');
  796.     }, 500);
  797.   }
  798.  
  799.   function closeModal() {
  800.     var modal = getModal();
  801.     _hide(getOverlay());
  802.     _hide(modal);
  803.     removeClass(modal, 'showSweetAlert');
  804.     addClass(modal, 'hideSweetAlert');
  805.     removeClass(modal, 'visible');
  806.  
  807.     // Reset icon animations
  808.  
  809.     var $successIcon = modal.querySelector('.icon.success');
  810.     removeClass($successIcon, 'animate');
  811.     removeClass($successIcon.querySelector('.tip'), 'animate-success-tip');
  812.     removeClass($successIcon.querySelector('.long'), 'animate-success-long');
  813.  
  814.     var $errorIcon = modal.querySelector('.icon.error');
  815.     removeClass($errorIcon, 'animate-error-icon');
  816.     removeClass($errorIcon.querySelector('.x-mark'), 'animate-x-mark');
  817.  
  818.     var $warningIcon = modal.querySelector('.icon.warning');
  819.     removeClass($warningIcon, 'pulse-warning');
  820.     removeClass($warningIcon.querySelector('.body'), 'pulse-warning-ins');
  821.     removeClass($warningIcon.querySelector('.dot'), 'pulse-warning-ins');
  822.  
  823.     resetPrevState();
  824.   }
  825.  
  826.   // Reset the page to its previous state
  827.   function resetPrevState() {
  828.     var modal = getModal();
  829.     window.onkeydown = previousWindowKeyDown;
  830.     document.onclick = previousDocumentClick;
  831.     if (previousActiveElement) {
  832.       previousActiveElement.focus();
  833.     }
  834.     lastFocusedButton = undefined;
  835.     clearTimeout(modal.timeout);
  836.  
  837.     // Remove dynamically created media query
  838.     var head = document.getElementsByTagName('head')[0];
  839.     var mediaquery = document.getElementById(mediaqueryId);
  840.     if (mediaquery) {
  841.       head.removeChild(mediaquery);
  842.     }
  843.   }
  844.  
  845.   /*
  846.    * Set 'margin-top'-property on modal based on its computed height
  847.    */
  848.   function fixVerticalPosition() {
  849.     var modal = getModal();
  850.  
  851.     modal.style.marginTop = getTopMargin(getModal());
  852.   }
  853.  
  854.   /*
  855.    * If library is injected after page has loaded
  856.    */
  857.   (function() {
  858.     if (document.readyState === 'complete' || document.readyState === 'interactive' && document.body) {
  859.       swal.init();
  860.     } else {
  861.       if (document.addEventListener) {
  862.         document.addEventListener('DOMContentLoaded', function onDomContentLoaded() {
  863.           document.removeEventListener('DOMContentLoaded', onDomContentLoaded, false);
  864.           swal.init();
  865.         }, false);
  866.       } else if (document.attachEvent) {
  867.         document.attachEvent('onreadystatechange', function onReadyStateChange() {
  868.           if (document.readyState === 'complete') {
  869.             document.detachEvent('onreadystatechange', onReadyStateChange);
  870.             swal.init();
  871.           }
  872.         });
  873.       }
  874.     }
  875.   })();
  876.  
  877. })(window, document);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement