Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extended-ui-sweetalert2.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * Sweet Alerts
  3. */
  4. 'use strict';
  5. (function () {
  6. const basicAlert = document.querySelector('#basic-alert'),
  7. withTitle = document.querySelector('#with-title'),
  8. footerAlert = document.querySelector('#footer-alert'),
  9. htmlAlert = document.querySelector('#html-alert'),
  10. positionTopStart = document.querySelector('#position-top-start'),
  11. positionTopEnd = document.querySelector('#position-top-end'),
  12. positionBottomStart = document.querySelector('#position-bottom-start'),
  13. positionBottomEnd = document.querySelector('#position-bottom-end'),
  14. bounceInAnimation = document.querySelector('#bounce-in-animation'),
  15. fadeInAnimation = document.querySelector('#fade-in-animation'),
  16. flipXAnimation = document.querySelector('#flip-x-animation'),
  17. tadaAnimation = document.querySelector('#tada-animation'),
  18. shakeAnimation = document.querySelector('#shake-animation'),
  19. iconSuccess = document.querySelector('#type-success'),
  20. iconInfo = document.querySelector('#type-info'),
  21. iconWarning = document.querySelector('#type-warning'),
  22. iconError = document.querySelector('#type-error'),
  23. iconQuestion = document.querySelector('#type-question'),
  24. customImage = document.querySelector('#custom-image'),
  25. autoClose = document.querySelector('#auto-close'),
  26. outsideClick = document.querySelector('#outside-click'),
  27. progressSteps = document.querySelector('#progress-steps'),
  28. ajaxRequest = document.querySelector('#ajax-request'),
  29. confirmText = document.querySelector('#confirm-text'),
  30. confirmColor = document.querySelector('#confirm-color');
  31. // Basic Alerts
  32. // --------------------------------------------------------------------
  33. // Default Alert
  34. if (basicAlert) {
  35. basicAlert.onclick = function () {
  36. Swal.fire({
  37. title: 'Any fool can use a computer',
  38. customClass: {
  39. confirmButton: 'btn btn-primary'
  40. },
  41. buttonsStyling: false
  42. });
  43. };
  44. }
  45. // Alert With Title
  46. if (withTitle) {
  47. withTitle.onclick = function () {
  48. Swal.fire({
  49. title: 'The Internet?,',
  50. text: 'That thing is still around?',
  51. customClass: {
  52. confirmButton: 'btn btn-primary'
  53. },
  54. buttonsStyling: false
  55. });
  56. };
  57. }
  58. // Alert With Footer
  59. if (footerAlert) {
  60. footerAlert.onclick = function () {
  61. Swal.fire({
  62. icon: 'error',
  63. title: 'Oops...',
  64. text: 'Something went wrong!',
  65. footer: '<a href>Why do I have this issue?</a>',
  66. customClass: {
  67. confirmButton: 'btn btn-primary'
  68. },
  69. buttonsStyling: false
  70. });
  71. };
  72. }
  73. // Html Alert
  74. if (htmlAlert) {
  75. htmlAlert.onclick = function () {
  76. Swal.fire({
  77. title: '<strong>HTML <u>example</u></strong>',
  78. icon: 'info',
  79. html:
  80. 'You can use <b>bold text</b>, ' +
  81. '<a href="https://pixinvent.com/" target="_blank">links</a> ' +
  82. 'and other HTML tags',
  83. showCloseButton: true,
  84. showCancelButton: true,
  85. focusConfirm: false,
  86. confirmButtonText: '<i class="icon-base bx bxs-like"></i> Great!',
  87. confirmButtonAriaLabel: 'Thumbs up, great!',
  88. cancelButtonText: '<i class="icon-base bx bxs-dislike"></i>',
  89. cancelButtonAriaLabel: 'Thumbs down',
  90. customClass: {
  91. confirmButton: 'btn btn-primary',
  92. cancelButton: 'btn btn-label-secondary'
  93. },
  94. buttonsStyling: false
  95. });
  96. };
  97. }
  98. // Alerts Positions
  99. // --------------------------------------------------------------------
  100. // Top Start Alert
  101. if (positionTopStart) {
  102. positionTopStart.onclick = function () {
  103. Swal.fire({
  104. position: 'top-start',
  105. icon: 'success',
  106. title: 'Your work has been saved',
  107. showConfirmButton: false,
  108. timer: 1500,
  109. customClass: {
  110. confirmButton: 'btn btn-primary'
  111. },
  112. buttonsStyling: false
  113. });
  114. };
  115. }
  116. // Top End Alert
  117. if (positionTopEnd) {
  118. positionTopEnd.onclick = function () {
  119. Swal.fire({
  120. position: 'top-end',
  121. icon: 'success',
  122. title: 'Your work has been saved',
  123. showConfirmButton: false,
  124. timer: 1500,
  125. customClass: {
  126. confirmButton: 'btn btn-primary'
  127. },
  128. buttonsStyling: false
  129. });
  130. };
  131. }
  132. // Bottom Start Alert
  133. if (positionBottomStart) {
  134. positionBottomStart.onclick = function () {
  135. Swal.fire({
  136. position: 'bottom-start',
  137. icon: 'success',
  138. title: 'Your work has been saved',
  139. showConfirmButton: false,
  140. timer: 1500,
  141. customClass: {
  142. confirmButton: 'btn btn-primary'
  143. },
  144. buttonsStyling: false
  145. });
  146. };
  147. }
  148. // Bottom End Alert
  149. if (positionBottomEnd) {
  150. positionBottomEnd.onclick = function () {
  151. Swal.fire({
  152. position: 'bottom-end',
  153. icon: 'success',
  154. title: 'Your work has been saved',
  155. showConfirmButton: false,
  156. timer: 1500,
  157. customClass: {
  158. confirmButton: 'btn btn-primary'
  159. },
  160. buttonsStyling: false
  161. });
  162. };
  163. }
  164. // Alerts With Animations
  165. // --------------------------------------------------------------------
  166. // Bounce In Animation
  167. if (bounceInAnimation) {
  168. bounceInAnimation.onclick = function () {
  169. Swal.fire({
  170. title: 'Bounce In Animation',
  171. showClass: {
  172. popup: 'animate__animated animate__bounceIn'
  173. },
  174. customClass: {
  175. confirmButton: 'btn btn-primary'
  176. },
  177. buttonsStyling: false
  178. });
  179. };
  180. }
  181. // Fade In Animation
  182. if (fadeInAnimation) {
  183. fadeInAnimation.onclick = function () {
  184. Swal.fire({
  185. title: 'Fade In Animation',
  186. showClass: {
  187. popup: 'animate__animated animate__fadeIn'
  188. },
  189. customClass: {
  190. confirmButton: 'btn btn-primary'
  191. },
  192. buttonsStyling: false
  193. });
  194. };
  195. }
  196. // Flip X Animation
  197. if (flipXAnimation) {
  198. flipXAnimation.onclick = function () {
  199. Swal.fire({
  200. title: 'Flip In Animation',
  201. showClass: {
  202. popup: 'animate__animated animate__flipInX'
  203. },
  204. customClass: {
  205. confirmButton: 'btn btn-primary'
  206. },
  207. buttonsStyling: false
  208. });
  209. };
  210. }
  211. // Tada Animation
  212. if (tadaAnimation) {
  213. tadaAnimation.onclick = function () {
  214. Swal.fire({
  215. title: 'Tada Animation',
  216. showClass: {
  217. popup: 'animate__animated animate__tada'
  218. },
  219. customClass: {
  220. confirmButton: 'btn btn-primary'
  221. },
  222. buttonsStyling: false
  223. });
  224. };
  225. }
  226. // Shake Animation
  227. if (shakeAnimation) {
  228. shakeAnimation.onclick = function () {
  229. Swal.fire({
  230. title: 'Shake Animation',
  231. showClass: {
  232. popup: 'animate__animated animate__shakeX'
  233. },
  234. customClass: {
  235. confirmButton: 'btn btn-primary'
  236. },
  237. buttonsStyling: false
  238. });
  239. };
  240. }
  241. // Alert Types
  242. // --------------------------------------------------------------------
  243. // Success Alert
  244. if (iconSuccess) {
  245. iconSuccess.onclick = function () {
  246. Swal.fire({
  247. title: 'Good job!',
  248. text: 'You clicked the button!',
  249. icon: 'success',
  250. customClass: {
  251. confirmButton: 'btn btn-primary'
  252. },
  253. buttonsStyling: false
  254. });
  255. };
  256. }
  257. // Info Alert
  258. if (iconInfo) {
  259. iconInfo.onclick = function () {
  260. Swal.fire({
  261. title: 'Info!',
  262. text: 'You clicked the button!',
  263. icon: 'info',
  264. customClass: {
  265. confirmButton: 'btn btn-primary'
  266. },
  267. buttonsStyling: false
  268. });
  269. };
  270. }
  271. // Warning Alert
  272. if (iconWarning) {
  273. iconWarning.onclick = function () {
  274. Swal.fire({
  275. title: 'Warning!',
  276. text: ' You clicked the button!',
  277. icon: 'warning',
  278. customClass: {
  279. confirmButton: 'btn btn-primary'
  280. },
  281. buttonsStyling: false
  282. });
  283. };
  284. }
  285. // Error Alert
  286. if (iconError) {
  287. iconError.onclick = function () {
  288. Swal.fire({
  289. title: 'Error!',
  290. text: ' You clicked the button!',
  291. icon: 'error',
  292. customClass: {
  293. confirmButton: 'btn btn-primary'
  294. },
  295. buttonsStyling: false
  296. });
  297. };
  298. }
  299. // Question Alert
  300. if (iconQuestion) {
  301. iconQuestion.onclick = function () {
  302. Swal.fire({
  303. title: 'Question!',
  304. text: ' You clicked the button!',
  305. icon: 'question',
  306. customClass: {
  307. confirmButton: 'btn btn-primary'
  308. },
  309. buttonsStyling: false
  310. });
  311. };
  312. }
  313. // Advanced Options
  314. // --------------------------------------------------------------------
  315. //Alert With Custom Icon
  316. if (customImage) {
  317. customImage.onclick = function () {
  318. Swal.fire({
  319. title: 'Sweet!',
  320. text: 'Modal with a custom image.',
  321. imageUrl: assetsPath + 'img/backgrounds/15.jpg',
  322. imageWidth: 400,
  323. imageAlt: 'Custom image',
  324. customClass: {
  325. confirmButton: 'btn btn-primary'
  326. },
  327. buttonsStyling: false
  328. });
  329. };
  330. }
  331. // Auto Closing Alert
  332. if (autoClose) {
  333. autoClose.onclick = function () {
  334. var timerInterval;
  335. Swal.fire({
  336. title: 'Auto close alert!',
  337. html: 'I will close in <strong></strong> seconds.',
  338. timer: 2000,
  339. customClass: {
  340. confirmButton: 'btn btn-primary'
  341. },
  342. buttonsStyling: false,
  343. willOpen: function () {
  344. Swal.showLoading();
  345. timerInterval = setInterval(function () {
  346. Swal.getHtmlContainer().querySelector('strong').textContent = Swal.getTimerLeft();
  347. }, 100);
  348. },
  349. willClose: function () {
  350. clearInterval(timerInterval);
  351. }
  352. }).then(function (result) {
  353. if (
  354. // Read more about handling dismissals
  355. result.dismiss === Swal.DismissReason.timer
  356. ) {
  357. console.log('I was closed by the timer');
  358. }
  359. });
  360. };
  361. }
  362. // Close Alert On Backdrop Click
  363. if (outsideClick) {
  364. outsideClick.onclick = function () {
  365. Swal.fire({
  366. title: 'Click outside to close!',
  367. text: 'This is a cool message!',
  368. backdrop: true,
  369. allowOutsideClick: true,
  370. customClass: {
  371. confirmButton: 'btn btn-primary'
  372. },
  373. buttonsStyling: false
  374. });
  375. };
  376. }
  377. // Alert With Steps
  378. if (progressSteps) {
  379. progressSteps.onclick = function () {
  380. const steps = ['1', '2', '3'];
  381. const swalQueueStep = Swal.mixin({
  382. confirmButtonText: 'Forward',
  383. cancelButtonText: 'Back',
  384. progressSteps: steps,
  385. input: 'text',
  386. inputAttributes: {
  387. required: true
  388. },
  389. validationMessage: 'This field is required'
  390. });
  391. async function backAndForward() {
  392. const values = [];
  393. let currentStep;
  394. for (currentStep = 0; currentStep < steps.length; ) {
  395. const result = await new swalQueueStep({
  396. title: 'Question ' + steps[currentStep],
  397. showCancelButton: currentStep > 0,
  398. currentProgressStep: currentStep
  399. });
  400. if (result.value) {
  401. values[currentStep] = result.value;
  402. currentStep++;
  403. } else if (result.dismiss === 'cancel') {
  404. currentStep--;
  405. }
  406. }
  407. Swal.fire(JSON.stringify(values));
  408. }
  409. backAndForward();
  410. };
  411. }
  412. // Alert With Ajax Request
  413. if (ajaxRequest) {
  414. ajaxRequest.onclick = function () {
  415. Swal.fire({
  416. title: 'Submit your Github username',
  417. input: 'text',
  418. inputAttributes: {
  419. autocapitalize: 'off'
  420. },
  421. showCancelButton: true,
  422. confirmButtonText: 'Look up',
  423. showLoaderOnConfirm: true,
  424. customClass: {
  425. confirmButton: 'btn btn-primary',
  426. cancelButton: 'btn btn-label-danger'
  427. },
  428. preConfirm: login => {
  429. return fetch('//api.github.com/users/' + login)
  430. .then(response => {
  431. if (!response.ok) {
  432. throw new Error(response.statusText);
  433. }
  434. return response.json();
  435. })
  436. .catch(error => {
  437. Swal.showValidationMessage('Request failed:' + error);
  438. });
  439. },
  440. backdrop: true,
  441. allowOutsideClick: () => !Swal.isLoading()
  442. }).then(result => {
  443. if (result.isConfirmed) {
  444. Swal.fire({
  445. title: result.value.login + "'s avatar",
  446. imageUrl: result.value.avatar_url,
  447. customClass: {
  448. confirmButtonText: 'Close me!',
  449. confirmButton: 'btn btn-primary'
  450. }
  451. });
  452. }
  453. });
  454. };
  455. }
  456. // Alert With Functional Confirm Button
  457. if (confirmText) {
  458. confirmText.onclick = function () {
  459. Swal.fire({
  460. title: 'Are you sure?',
  461. text: "You won't be able to revert this!",
  462. icon: 'warning',
  463. showCancelButton: true,
  464. confirmButtonText: 'Yes, delete it!',
  465. customClass: {
  466. confirmButton: 'btn btn-primary',
  467. cancelButton: 'btn btn-label-secondary'
  468. },
  469. buttonsStyling: false
  470. }).then(function (result) {
  471. if (result.value) {
  472. Swal.fire({
  473. icon: 'success',
  474. title: 'Deleted!',
  475. text: 'Your file has been deleted.',
  476. customClass: {
  477. confirmButton: 'btn btn-success'
  478. }
  479. });
  480. }
  481. });
  482. };
  483. }
  484. // Alert With Functional Confirm Cancel Button
  485. if (confirmColor) {
  486. confirmColor.onclick = function () {
  487. Swal.fire({
  488. title: 'Are you sure?',
  489. text: "You won't be able to revert this!",
  490. icon: 'warning',
  491. showCancelButton: true,
  492. confirmButtonText: 'Yes, delete it!',
  493. customClass: {
  494. confirmButton: 'btn btn-primary',
  495. cancelButton: 'btn btn-label-secondary'
  496. },
  497. buttonsStyling: false
  498. }).then(function (result) {
  499. if (result.value) {
  500. Swal.fire({
  501. icon: 'success',
  502. title: 'Deleted!',
  503. text: 'Your file has been deleted.',
  504. customClass: {
  505. confirmButton: 'btn btn-success'
  506. }
  507. });
  508. } else if (result.dismiss === Swal.DismissReason.cancel) {
  509. Swal.fire({
  510. title: 'Cancelled',
  511. text: 'Your imaginary file is safe :)',
  512. icon: 'error',
  513. customClass: {
  514. confirmButton: 'btn btn-success'
  515. }
  516. });
  517. }
  518. });
  519. };
  520. }
  521. })();