| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- /**
- * Sweet Alerts
- */
-
- 'use strict';
-
- (function () {
- const basicAlert = document.querySelector('#basic-alert'),
- withTitle = document.querySelector('#with-title'),
- footerAlert = document.querySelector('#footer-alert'),
- htmlAlert = document.querySelector('#html-alert'),
- positionTopStart = document.querySelector('#position-top-start'),
- positionTopEnd = document.querySelector('#position-top-end'),
- positionBottomStart = document.querySelector('#position-bottom-start'),
- positionBottomEnd = document.querySelector('#position-bottom-end'),
- bounceInAnimation = document.querySelector('#bounce-in-animation'),
- fadeInAnimation = document.querySelector('#fade-in-animation'),
- flipXAnimation = document.querySelector('#flip-x-animation'),
- tadaAnimation = document.querySelector('#tada-animation'),
- shakeAnimation = document.querySelector('#shake-animation'),
- iconSuccess = document.querySelector('#type-success'),
- iconInfo = document.querySelector('#type-info'),
- iconWarning = document.querySelector('#type-warning'),
- iconError = document.querySelector('#type-error'),
- iconQuestion = document.querySelector('#type-question'),
- customImage = document.querySelector('#custom-image'),
- autoClose = document.querySelector('#auto-close'),
- outsideClick = document.querySelector('#outside-click'),
- progressSteps = document.querySelector('#progress-steps'),
- ajaxRequest = document.querySelector('#ajax-request'),
- confirmText = document.querySelector('#confirm-text'),
- confirmColor = document.querySelector('#confirm-color');
-
- // Basic Alerts
- // --------------------------------------------------------------------
-
- // Default Alert
- if (basicAlert) {
- basicAlert.onclick = function () {
- Swal.fire({
- title: 'Any fool can use a computer',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Alert With Title
- if (withTitle) {
- withTitle.onclick = function () {
- Swal.fire({
- title: 'The Internet?,',
- text: 'That thing is still around?',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Alert With Footer
- if (footerAlert) {
- footerAlert.onclick = function () {
- Swal.fire({
- icon: 'error',
- title: 'Oops...',
- text: 'Something went wrong!',
- footer: '<a href>Why do I have this issue?</a>',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Html Alert
- if (htmlAlert) {
- htmlAlert.onclick = function () {
- Swal.fire({
- title: '<strong>HTML <u>example</u></strong>',
- icon: 'info',
- html:
- 'You can use <b>bold text</b>, ' +
- '<a href="https://pixinvent.com/" target="_blank">links</a> ' +
- 'and other HTML tags',
- showCloseButton: true,
- showCancelButton: true,
- focusConfirm: false,
- confirmButtonText: '<i class="icon-base bx bxs-like"></i> Great!',
- confirmButtonAriaLabel: 'Thumbs up, great!',
- cancelButtonText: '<i class="icon-base bx bxs-dislike"></i>',
- cancelButtonAriaLabel: 'Thumbs down',
- customClass: {
- confirmButton: 'btn btn-primary',
- cancelButton: 'btn btn-label-secondary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Alerts Positions
- // --------------------------------------------------------------------
-
- // Top Start Alert
- if (positionTopStart) {
- positionTopStart.onclick = function () {
- Swal.fire({
- position: 'top-start',
- icon: 'success',
- title: 'Your work has been saved',
- showConfirmButton: false,
- timer: 1500,
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Top End Alert
- if (positionTopEnd) {
- positionTopEnd.onclick = function () {
- Swal.fire({
- position: 'top-end',
- icon: 'success',
- title: 'Your work has been saved',
- showConfirmButton: false,
- timer: 1500,
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Bottom Start Alert
- if (positionBottomStart) {
- positionBottomStart.onclick = function () {
- Swal.fire({
- position: 'bottom-start',
- icon: 'success',
- title: 'Your work has been saved',
- showConfirmButton: false,
- timer: 1500,
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Bottom End Alert
- if (positionBottomEnd) {
- positionBottomEnd.onclick = function () {
- Swal.fire({
- position: 'bottom-end',
- icon: 'success',
- title: 'Your work has been saved',
- showConfirmButton: false,
- timer: 1500,
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Alerts With Animations
- // --------------------------------------------------------------------
-
- // Bounce In Animation
- if (bounceInAnimation) {
- bounceInAnimation.onclick = function () {
- Swal.fire({
- title: 'Bounce In Animation',
- showClass: {
- popup: 'animate__animated animate__bounceIn'
- },
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Fade In Animation
- if (fadeInAnimation) {
- fadeInAnimation.onclick = function () {
- Swal.fire({
- title: 'Fade In Animation',
- showClass: {
- popup: 'animate__animated animate__fadeIn'
- },
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Flip X Animation
- if (flipXAnimation) {
- flipXAnimation.onclick = function () {
- Swal.fire({
- title: 'Flip In Animation',
- showClass: {
- popup: 'animate__animated animate__flipInX'
- },
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Tada Animation
- if (tadaAnimation) {
- tadaAnimation.onclick = function () {
- Swal.fire({
- title: 'Tada Animation',
- showClass: {
- popup: 'animate__animated animate__tada'
- },
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Shake Animation
- if (shakeAnimation) {
- shakeAnimation.onclick = function () {
- Swal.fire({
- title: 'Shake Animation',
- showClass: {
- popup: 'animate__animated animate__shakeX'
- },
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Alert Types
- // --------------------------------------------------------------------
-
- // Success Alert
- if (iconSuccess) {
- iconSuccess.onclick = function () {
- Swal.fire({
- title: 'Good job!',
- text: 'You clicked the button!',
- icon: 'success',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Info Alert
- if (iconInfo) {
- iconInfo.onclick = function () {
- Swal.fire({
- title: 'Info!',
- text: 'You clicked the button!',
- icon: 'info',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Warning Alert
- if (iconWarning) {
- iconWarning.onclick = function () {
- Swal.fire({
- title: 'Warning!',
- text: ' You clicked the button!',
- icon: 'warning',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Error Alert
- if (iconError) {
- iconError.onclick = function () {
- Swal.fire({
- title: 'Error!',
- text: ' You clicked the button!',
- icon: 'error',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Question Alert
- if (iconQuestion) {
- iconQuestion.onclick = function () {
- Swal.fire({
- title: 'Question!',
- text: ' You clicked the button!',
- icon: 'question',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Advanced Options
- // --------------------------------------------------------------------
-
- //Alert With Custom Icon
- if (customImage) {
- customImage.onclick = function () {
- Swal.fire({
- title: 'Sweet!',
- text: 'Modal with a custom image.',
- imageUrl: assetsPath + 'img/backgrounds/15.jpg',
- imageWidth: 400,
- imageAlt: 'Custom image',
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Auto Closing Alert
- if (autoClose) {
- autoClose.onclick = function () {
- var timerInterval;
- Swal.fire({
- title: 'Auto close alert!',
- html: 'I will close in <strong></strong> seconds.',
- timer: 2000,
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false,
- willOpen: function () {
- Swal.showLoading();
- timerInterval = setInterval(function () {
- Swal.getHtmlContainer().querySelector('strong').textContent = Swal.getTimerLeft();
- }, 100);
- },
- willClose: function () {
- clearInterval(timerInterval);
- }
- }).then(function (result) {
- if (
- // Read more about handling dismissals
- result.dismiss === Swal.DismissReason.timer
- ) {
- console.log('I was closed by the timer');
- }
- });
- };
- }
-
- // Close Alert On Backdrop Click
- if (outsideClick) {
- outsideClick.onclick = function () {
- Swal.fire({
- title: 'Click outside to close!',
- text: 'This is a cool message!',
- backdrop: true,
- allowOutsideClick: true,
- customClass: {
- confirmButton: 'btn btn-primary'
- },
- buttonsStyling: false
- });
- };
- }
-
- // Alert With Steps
- if (progressSteps) {
- progressSteps.onclick = function () {
- const steps = ['1', '2', '3'];
- const swalQueueStep = Swal.mixin({
- confirmButtonText: 'Forward',
- cancelButtonText: 'Back',
- progressSteps: steps,
- input: 'text',
- inputAttributes: {
- required: true
- },
- validationMessage: 'This field is required'
- });
-
- async function backAndForward() {
- const values = [];
- let currentStep;
-
- for (currentStep = 0; currentStep < steps.length; ) {
- const result = await new swalQueueStep({
- title: 'Question ' + steps[currentStep],
- showCancelButton: currentStep > 0,
- currentProgressStep: currentStep
- });
-
- if (result.value) {
- values[currentStep] = result.value;
- currentStep++;
- } else if (result.dismiss === 'cancel') {
- currentStep--;
- }
- }
-
- Swal.fire(JSON.stringify(values));
- }
-
- backAndForward();
- };
- }
-
- // Alert With Ajax Request
- if (ajaxRequest) {
- ajaxRequest.onclick = function () {
- Swal.fire({
- title: 'Submit your Github username',
- input: 'text',
- inputAttributes: {
- autocapitalize: 'off'
- },
- showCancelButton: true,
- confirmButtonText: 'Look up',
- showLoaderOnConfirm: true,
- customClass: {
- confirmButton: 'btn btn-primary',
- cancelButton: 'btn btn-label-danger'
- },
- preConfirm: login => {
- return fetch('//api.github.com/users/' + login)
- .then(response => {
- if (!response.ok) {
- throw new Error(response.statusText);
- }
- return response.json();
- })
- .catch(error => {
- Swal.showValidationMessage('Request failed:' + error);
- });
- },
- backdrop: true,
- allowOutsideClick: () => !Swal.isLoading()
- }).then(result => {
- if (result.isConfirmed) {
- Swal.fire({
- title: result.value.login + "'s avatar",
- imageUrl: result.value.avatar_url,
- customClass: {
- confirmButtonText: 'Close me!',
- confirmButton: 'btn btn-primary'
- }
- });
- }
- });
- };
- }
-
- // Alert With Functional Confirm Button
- if (confirmText) {
- confirmText.onclick = function () {
- Swal.fire({
- title: 'Are you sure?',
- text: "You won't be able to revert this!",
- icon: 'warning',
- showCancelButton: true,
- confirmButtonText: 'Yes, delete it!',
- customClass: {
- confirmButton: 'btn btn-primary',
- cancelButton: 'btn btn-label-secondary'
- },
- buttonsStyling: false
- }).then(function (result) {
- if (result.value) {
- Swal.fire({
- icon: 'success',
- title: 'Deleted!',
- text: 'Your file has been deleted.',
- customClass: {
- confirmButton: 'btn btn-success'
- }
- });
- }
- });
- };
- }
-
- // Alert With Functional Confirm Cancel Button
- if (confirmColor) {
- confirmColor.onclick = function () {
- Swal.fire({
- title: 'Are you sure?',
- text: "You won't be able to revert this!",
- icon: 'warning',
- showCancelButton: true,
- confirmButtonText: 'Yes, delete it!',
- customClass: {
- confirmButton: 'btn btn-primary',
- cancelButton: 'btn btn-label-secondary'
- },
- buttonsStyling: false
- }).then(function (result) {
- if (result.value) {
- Swal.fire({
- icon: 'success',
- title: 'Deleted!',
- text: 'Your file has been deleted.',
- customClass: {
- confirmButton: 'btn btn-success'
- }
- });
- } else if (result.dismiss === Swal.DismissReason.cancel) {
- Swal.fire({
- title: 'Cancelled',
- text: 'Your imaginary file is safe :)',
- icon: 'error',
- customClass: {
- confirmButton: 'btn btn-success'
- }
- });
- }
- });
- };
- }
- })();
|