extra_sweetalert.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Sweet Alert component
  4. *
  5. * Demo JS code for extra_sweetalert.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var SweetAlert = function () {
  11. //
  12. // Setup module components
  13. //
  14. // Sweet Alerts
  15. var _componentSweetAlert = function() {
  16. if (typeof swal == 'undefined') {
  17. console.warn('Warning - sweet_alert.min.js is not loaded.');
  18. return;
  19. }
  20. // Defaults
  21. var setCustomDefaults = function() {
  22. swal.setDefaults({
  23. buttonsStyling: false,
  24. confirmButtonClass: 'btn btn-primary',
  25. cancelButtonClass: 'btn btn-light'
  26. });
  27. }
  28. setCustomDefaults();
  29. //
  30. // Basic options
  31. //
  32. // Basic
  33. $('#sweet_basic').on('click', function() {
  34. swal({
  35. title: 'Here is a message!'
  36. });
  37. });
  38. // With title
  39. $('#sweet_title_text').on('click', function() {
  40. swal({
  41. title: 'Here\'s a message!',
  42. text: 'It\'s pretty, isn\'t it?'
  43. });
  44. });
  45. // Close button
  46. $('#sweet_close').on('click', function() {
  47. swal({
  48. title: 'Good job!',
  49. text: 'You clicked the button!',
  50. type: 'success',
  51. showCloseButton: true
  52. }).catch(swal.noop);
  53. });
  54. // Custom padding
  55. $('#sweet_padding').on('click', function() {
  56. swal({
  57. title: 'Oops...',
  58. text: 'Something went wrong!',
  59. type: 'error',
  60. padding: 30
  61. });
  62. });
  63. // Custom width
  64. $('#sweet_width').on('click', function() {
  65. swal({
  66. title: 'Got question?',
  67. text: 'If you have any questions, don\t hesitate to let us know!',
  68. type: 'question',
  69. width: '35%'
  70. });
  71. });
  72. // Auto closing
  73. $('#sweet_auto_closer').on('click', function() {
  74. swal({
  75. title: 'Auto close alert!',
  76. text: 'I will close in 2 seconds.',
  77. timer: 2000
  78. });
  79. });
  80. // Dynamic queue
  81. $('#sweet_queue').on('click', function() {
  82. swal.queue([{
  83. title: 'Your public IP',
  84. confirmButtonText: 'Show my public IP',
  85. text: 'Your public IP will be received ' + 'via AJAX request',
  86. showLoaderOnConfirm: true,
  87. preConfirm: function () {
  88. return new Promise(function (resolve) {
  89. $.get('https://api.ipify.org?format=json')
  90. .done(function (data) {
  91. swal.insertQueueStep(data.ip);
  92. resolve();
  93. });
  94. });
  95. }
  96. }]);
  97. });
  98. // AJAX requests
  99. $('#sweet_ajax').on('click', function() {
  100. swal({
  101. title: 'Submit email to run ajax request',
  102. input: 'email',
  103. inputPlaceholder: 'Enter your email',
  104. inputClass: 'form-control',
  105. showCancelButton: true,
  106. confirmButtonText: 'Submit',
  107. showLoaderOnConfirm: true,
  108. preConfirm: function (email) {
  109. return new Promise(function (resolve) {
  110. setTimeout(function () {
  111. if (email === 'taken@example.com') {
  112. swal.showValidationError('This email is already taken.')
  113. }
  114. resolve();
  115. }, 2000);
  116. });
  117. },
  118. allowOutsideClick: false
  119. }).then(function (result) {
  120. if (result.value) {
  121. swal({
  122. type: 'success',
  123. title: 'Ajax request finished!',
  124. html: 'Submitted email: <span class="font-weight-semibold">' + result.value + '</span>'
  125. });
  126. }
  127. });
  128. });
  129. // HTML message
  130. $('#sweet_html').on('click', function() {
  131. swal({
  132. title: '<i>HTML</i> <u>example</u>',
  133. html: 'You can use <strong>bold text</strong>, ' + '<a href="http://demo.interface.club">links</a> ' + 'and other <span class="text-warning">HTML tags</span>'
  134. });
  135. });
  136. // Image
  137. $('#sweet_image').on('click', function() {
  138. swal({
  139. title: 'Sweet!',
  140. text: 'Bootstrap is awesome.',
  141. imageUrl: 'https://getbootstrap.com/assets/img/bootstrap-stack.png',
  142. imageWidth: 260
  143. });
  144. });
  145. // Background image
  146. $('#sweet_bg').on('click', function() {
  147. swal({
  148. title: 'Sweet!',
  149. type: 'success',
  150. text: 'Custom backgrounds are awesome.',
  151. background: '#fff url(../../../../global_assets/images/backgrounds/seamless.png) repeat'
  152. });
  153. });
  154. // Chaining notifications
  155. $('#sweet_chain').on('click', function() {
  156. // Defaults
  157. swal.setDefaults({
  158. input: 'text',
  159. confirmButtonText: 'Next <i class="icon-arrow-right14 ml-2"></i>',
  160. showCancelButton: false,
  161. inputClass: 'form-control',
  162. animation: false,
  163. buttonsStyling: false,
  164. confirmButtonClass: 'btn btn-primary',
  165. progressSteps: ['1', '2', '3']
  166. });
  167. // Setup steps
  168. var steps = [
  169. {
  170. title: 'Question 1',
  171. text: 'Step #1 - ask your first question',
  172. inputPlaceholder: 'Enter your first question'
  173. },
  174. {
  175. title: 'Question 2',
  176. text: 'Step #2 - ask your second question',
  177. inputPlaceholder: 'Enter your second question'
  178. },
  179. {
  180. title: 'Question 3',
  181. text: 'Step #3 - ask your third question',
  182. inputPlaceholder: 'Enter your third question'
  183. }
  184. ];
  185. // Initialize
  186. swal.queue(steps).then(function (result) {
  187. swal.resetDefaults();
  188. setCustomDefaults();
  189. swal({
  190. title: 'All done!',
  191. html: 'Your answers: <pre class="mt-3"><code>' + JSON.stringify(result) + '</code></pre>',
  192. confirmButtonText: 'Lovely!',
  193. buttonsStyling: false,
  194. confirmButtonClass: 'btn btn-primary'
  195. });
  196. }, function () {
  197. swal.resetDefaults();
  198. setCustomDefaults();
  199. });
  200. });
  201. // Reversed buttons
  202. $('#sweet_reverse_buttons').on('click', function() {
  203. swal({
  204. title: 'What is your name?',
  205. input: 'text',
  206. inputPlaceholder: 'Your name or nickname',
  207. showCancelButton: true,
  208. inputClass: 'form-control',
  209. confirmButtonText: 'Submit',
  210. reverseButtons: true
  211. });
  212. });
  213. // Fullscreen
  214. $('#sweet_fullscreen').on('click', function() {
  215. swal({
  216. title: 'Here\'s a message!',
  217. text: 'It\'s pretty, isn\'t it?',
  218. type: 'success',
  219. showConfirmButton: false,
  220. showCloseButton: true,
  221. grow: 'fullscreen'
  222. });
  223. });
  224. // Column grow
  225. $('#sweet_column').on('click', function() {
  226. swal({
  227. title: 'Here\'s a message!',
  228. text: 'It\'s pretty, isn\'t it?',
  229. type: 'success',
  230. showConfirmButton: false,
  231. showCloseButton: true,
  232. grow: 'column'
  233. });
  234. });
  235. // Row grow
  236. $('#sweet_row').on('click', function() {
  237. swal({
  238. title: 'Here\'s a message!',
  239. text: 'It\'s pretty, isn\'t it?',
  240. type: 'success',
  241. grow: 'row'
  242. });
  243. });
  244. // Disabled keyboard interactions
  245. $('#sweet_disabled_keyboard').on('click', function() {
  246. swal({
  247. title: 'Oops...',
  248. text: 'Something went wrong!',
  249. type: 'error',
  250. allowEscapeKey: false,
  251. allowEnterKey: false
  252. });
  253. });
  254. // Disabled animation
  255. $('#sweet_disabled_animation').on('click', function() {
  256. swal({
  257. title: 'For your information',
  258. text: 'This is some sort of a custom alert',
  259. type: 'info',
  260. animation: false
  261. });
  262. });
  263. // Disabled backdrop
  264. $('#sweet_disabled_backdrop').on('click', function() {
  265. swal({
  266. title: 'Got question?',
  267. text: 'You will get the answer soon!',
  268. type: 'question',
  269. backdrop: false
  270. });
  271. });
  272. // Disabled backdrop
  273. $('#sweet_disabled_outside_click').on('click', function() {
  274. swal({
  275. title: 'Oops...',
  276. text: 'Something went wrong!',
  277. type: 'error',
  278. allowOutsideClick: false
  279. });
  280. });
  281. //
  282. // Input types
  283. //
  284. // Text type
  285. $('#sweet_text').on('click', function() {
  286. swal({
  287. title: 'What is your name?',
  288. input: 'text',
  289. inputPlaceholder: 'Your name or nickname',
  290. showCancelButton: true,
  291. inputClass: 'form-control',
  292. inputValidator: function (value) {
  293. return !value && 'You need to write something!'
  294. }
  295. }).then(function (result) {
  296. if (result.value) {
  297. swal({
  298. type: 'success',
  299. html: 'Hi, ' + result.value
  300. });
  301. }
  302. });
  303. });
  304. // Email type
  305. $('#sweet_email').on('click', function() {
  306. swal({
  307. title: 'Input email address',
  308. input: 'email',
  309. inputClass: 'form-control',
  310. inputPlaceholder: 'Enter your email',
  311. }).then(function (result) {
  312. if (result.value) {
  313. swal({
  314. type: 'success',
  315. html: 'Entered email: ' + result.value
  316. });
  317. }
  318. });
  319. });
  320. // URL type
  321. $('#sweet_url').on('click', function() {
  322. swal({
  323. title: 'Input URL',
  324. input: 'url',
  325. inputClass: 'form-control',
  326. inputPlaceholder: 'Enter URL',
  327. }).then(function (result) {
  328. if (result.value) {
  329. swal({
  330. type: 'success',
  331. html: 'Entered URL: ' + result.value
  332. });
  333. }
  334. });
  335. });
  336. // Password type
  337. $('#sweet_password').on('click', function() {
  338. swal({
  339. title: 'Enter your password',
  340. input: 'password',
  341. inputClass: 'form-control',
  342. inputPlaceholder: 'Enter your password',
  343. inputAttributes: {
  344. 'maxlength': 10,
  345. 'autocapitalize': 'off',
  346. 'autocorrect': 'off'
  347. }
  348. }).then(function (result) {
  349. if (result.value) {
  350. swal({
  351. type: 'success',
  352. html: 'Entered password: ' + result.value
  353. });
  354. }
  355. });
  356. });
  357. // Textarea type
  358. $('#sweet_textarea').on('click', function() {
  359. swal({
  360. title: 'Enter your comment',
  361. input: 'textarea',
  362. inputPlaceholder: 'Type your message here',
  363. showCancelButton: true,
  364. inputClass: 'form-control'
  365. }).then(function (result) {
  366. if (result.value) {
  367. swal({
  368. title: 'Your comment:',
  369. text: result.value
  370. });
  371. }
  372. });
  373. });
  374. // Select type
  375. $('#sweet_select').on('click', function() {
  376. swal({
  377. title: 'Select Netherlands',
  378. input: 'select',
  379. inputOptions: {
  380. 'DE': 'Germany',
  381. 'UA': 'Ukraine',
  382. 'HR': 'Croatia',
  383. 'NL': 'Netherlands'
  384. },
  385. inputPlaceholder: 'Select country',
  386. inputClass: 'form-control',
  387. showCancelButton: true,
  388. inputValidator: function (value) {
  389. return new Promise(function (resolve) {
  390. if (value === 'NL') {
  391. resolve();
  392. } else {
  393. resolve('You need to select Netherlands :)');
  394. }
  395. });
  396. }
  397. }).then(function (result) {
  398. if (result.value) {
  399. swal({
  400. type: 'success',
  401. html: 'You selected: ' + result.value
  402. });
  403. }
  404. });
  405. });
  406. // Radio type
  407. $('#sweet_radio').on('click', function() {
  408. // inputOptions can be an object or Promise
  409. var inputOptions = new Promise(function (resolve) {
  410. setTimeout(function () {
  411. resolve({
  412. '#ff0000': 'Red',
  413. '#00ff00': 'Green',
  414. '#0000ff': 'Blue'
  415. });
  416. }, 2000)
  417. });
  418. // Initialize
  419. swal({
  420. title: 'Select color',
  421. input: 'radio',
  422. inputOptions: inputOptions,
  423. inputValidator: function (value) {
  424. return !value && 'You need to choose something!'
  425. }
  426. }).then(function (result) {
  427. if (result.value) {
  428. swal({
  429. type: 'success',
  430. html: 'You selected: ' + result.value
  431. });
  432. }
  433. });
  434. });
  435. // Checkbox type
  436. $('#sweet_checkbox').on('click', function() {
  437. swal({
  438. title: 'Terms and conditions',
  439. input: 'checkbox',
  440. inputValue: 1,
  441. inputPlaceholder: 'I agree with the terms and conditions',
  442. confirmButtonText: 'Continue <i class="icon-arrow-right14 ml-2></i>',
  443. inputValidator: function (value) {
  444. return !value && 'You need to agree with T&C'
  445. }
  446. }).then(function (result) {
  447. if (result.value) {
  448. swal({
  449. type: 'success',
  450. text: 'You agreed with T&C :)'
  451. });
  452. }
  453. });
  454. });
  455. // Range type
  456. $('#sweet_range').on('click', function() {
  457. swal({
  458. title: 'How old are you?',
  459. type: 'question',
  460. input: 'range',
  461. inputAttributes: {
  462. min: 8,
  463. max: 120,
  464. step: 1
  465. },
  466. inputValue: 25
  467. }).then(function (result) {
  468. if (result.value) {
  469. swal({
  470. type: 'success',
  471. html: 'Your age is ' + result.value
  472. });
  473. }
  474. });
  475. });
  476. //
  477. // Contextual alerts
  478. //
  479. // Success alert
  480. $('#sweet_success').on('click', function() {
  481. swal({
  482. title: 'Good job!',
  483. text: 'You clicked the button!',
  484. type: 'success'
  485. });
  486. });
  487. // Error alert
  488. $('#sweet_error').on('click', function() {
  489. swal({
  490. title: 'Oops...',
  491. text: 'Something went wrong!',
  492. type: 'error'
  493. });
  494. });
  495. // Warning alert
  496. $('#sweet_warning').on('click', function() {
  497. swal({
  498. title: 'Are you sure?',
  499. text: 'You will not be able to recover this imaginary file!',
  500. type: 'warning',
  501. showCancelButton: true,
  502. confirmButtonText: 'Yes, delete it!'
  503. });
  504. });
  505. // Info alert
  506. $('#sweet_info').on('click', function() {
  507. swal({
  508. title: 'For your information',
  509. text: 'This is some sort of a custom alert',
  510. type: 'info'
  511. });
  512. });
  513. // Question
  514. $('#sweet_question').on('click', function() {
  515. swal({
  516. title: 'Got question?',
  517. text: 'You will get the answer soon!',
  518. type: 'question'
  519. });
  520. });
  521. // Alert combination
  522. $('#sweet_combine').on('click', function() {
  523. swal({
  524. title: 'Are you sure?',
  525. text: "You won't be able to revert this!",
  526. type: 'warning',
  527. showCancelButton: true,
  528. confirmButtonText: 'Yes, delete it!',
  529. cancelButtonText: 'No, cancel!',
  530. confirmButtonClass: 'btn btn-success',
  531. cancelButtonClass: 'btn btn-danger',
  532. buttonsStyling: false
  533. }).then(function () {
  534. swal(
  535. 'Deleted!',
  536. 'Your file has been deleted.',
  537. 'success'
  538. );
  539. }, function (dismiss) {
  540. // Dismiss can be 'cancel', 'overlay', 'close', and 'timer'
  541. if (dismiss === 'cancel') {
  542. swal(
  543. 'Cancelled',
  544. 'Your imaginary file is safe :)',
  545. 'error'
  546. );
  547. }
  548. });
  549. });
  550. //
  551. // Positions
  552. //
  553. // Top
  554. $('#sweet_top').on('click', function() {
  555. swal({
  556. title: 'Good job!',
  557. text: 'You clicked the button!',
  558. type: 'success',
  559. position: 'top'
  560. });
  561. });
  562. // Top left
  563. $('#sweet_top_left').on('click', function() {
  564. swal({
  565. title: 'Good job!',
  566. text: 'You clicked the button!',
  567. type: 'success',
  568. position: 'top-left'
  569. });
  570. });
  571. // Top right
  572. $('#sweet_top_right').on('click', function() {
  573. swal({
  574. title: 'Good job!',
  575. text: 'You clicked the button!',
  576. type: 'success',
  577. position: 'top-right'
  578. });
  579. });
  580. // Center left
  581. $('#sweet_center_left').on('click', function() {
  582. swal({
  583. title: 'Good job!',
  584. text: 'You clicked the button!',
  585. type: 'success',
  586. position: 'center-left'
  587. });
  588. });
  589. // Center right
  590. $('#sweet_center_right').on('click', function() {
  591. swal({
  592. title: 'Good job!',
  593. text: 'You clicked the button!',
  594. type: 'success',
  595. position: 'center-right'
  596. });
  597. });
  598. // Bottom
  599. $('#sweet_bottom').on('click', function() {
  600. swal({
  601. title: 'Good job!',
  602. text: 'You clicked the button!',
  603. type: 'success',
  604. position: 'bottom'
  605. });
  606. });
  607. // Bottom left
  608. $('#sweet_bottom_left').on('click', function() {
  609. swal({
  610. title: 'Good job!',
  611. text: 'You clicked the button!',
  612. type: 'success',
  613. position: 'bottom-left'
  614. });
  615. });
  616. // Bottom right
  617. $('#sweet_bottom_right').on('click', function() {
  618. swal({
  619. title: 'Good job!',
  620. text: 'You clicked the button!',
  621. type: 'success',
  622. position: 'bottom-right'
  623. });
  624. });
  625. //
  626. // Events
  627. //
  628. // onOpen event
  629. $('#sweet_onopen').on('click', function() {
  630. swal({
  631. title: 'Here\'s a message!',
  632. text: 'It\'s pretty, isn\'t it?',
  633. onOpen: function () {
  634. alert('Notification is opened.');
  635. }
  636. });
  637. });
  638. // onClose event
  639. $('#sweet_onclose').on('click', function() {
  640. swal({
  641. title: 'Here\'s a message!',
  642. text: 'It\'s pretty, isn\'t it?',
  643. onClose: function () {
  644. alert('Notification is closed.');
  645. }
  646. });
  647. });
  648. };
  649. // SweetAlert with Select2 selects
  650. var _componentSelect2 = function() {
  651. if (!$().select2) {
  652. console.warn('Warning - select2.min.js is not loaded.');
  653. return;
  654. }
  655. // Select2 single
  656. $('#sweet_select2_single').on('click', function() {
  657. swal({
  658. title: 'Select Netherlands',
  659. input: 'select',
  660. inputOptions: {
  661. '': '',
  662. 'DE': 'Germany',
  663. 'UA': 'Ukraine',
  664. 'HR': 'Croatia',
  665. 'NL': 'Netherlands'
  666. },
  667. inputClass: 'form-control select-single',
  668. showCancelButton: true,
  669. inputValidator: function (value) {
  670. return new Promise(function (resolve) {
  671. if (value === 'NL') {
  672. resolve();
  673. } else {
  674. resolve('You need to select Netherlands :)');
  675. }
  676. });
  677. },
  678. inputAttributes: {
  679. 'data-placeholder': 'Select Netherlands'
  680. },
  681. onOpen: function() {
  682. // Initialize Select2
  683. $('.swal2-select.select-single').select2({
  684. minimumResultsForSearch: Infinity
  685. });
  686. }
  687. }).then(function (result) {
  688. if (result.value) {
  689. swal({
  690. type: 'success',
  691. html: 'You selected: ' + result.value
  692. });
  693. }
  694. });
  695. });
  696. // Select2 multiple
  697. $('#sweet_select2_multiple').on('click', function() {
  698. swal({
  699. title: 'Select country',
  700. input: 'select',
  701. inputOptions: {
  702. 'DE': 'Germany',
  703. 'UA': 'Ukraine',
  704. 'HR': 'Croatia',
  705. 'NL': 'Netherlands'
  706. },
  707. inputValue: 'NL',
  708. inputClass: 'form-control select-multiple',
  709. showCancelButton: true,
  710. inputValidator: function (value) {
  711. return !value && 'You need to select something!'
  712. },
  713. inputAttributes: {
  714. 'data-placeholder': 'Select country',
  715. 'multiple': 'multiple'
  716. },
  717. onOpen: function() {
  718. // Initialize Select2
  719. $('.swal2-select.select-multiple').select2();
  720. }
  721. }).then(function (result) {
  722. if (result.value) {
  723. swal({
  724. type: 'success',
  725. html: 'You selected: ' + JSON.stringify($('.swal2-select.select-multiple').val())
  726. });
  727. }
  728. });
  729. });
  730. };
  731. // SweetAlert with Bootstrap Multiselect
  732. var _componentMultiselect = function() {
  733. if (!$().multiselect) {
  734. console.warn('Warning - bootstrap-multiselect.js is not loaded.');
  735. return;
  736. }
  737. // SweetAlert with Multiselect
  738. $('#sweet_multiselect').on('click', function() {
  739. swal({
  740. title: 'Select countries',
  741. input: 'select',
  742. inputOptions: {
  743. 'DE': 'Germany',
  744. 'UA': 'Ukraine',
  745. 'HR': 'Croatia',
  746. 'NL': 'Netherlands'
  747. },
  748. inputClass: 'form-control select-multiselect',
  749. showCancelButton: true,
  750. inputValidator: function (value) {
  751. return !$('.swal2-select.select-multiselect').val().length && 'You need to select something!'
  752. },
  753. inputAttributes: {
  754. 'multiple': 'multiple'
  755. },
  756. onOpen: function() {
  757. // Initialize Multiselect when dialog is opened
  758. $('.swal2-select.select-multiselect').multiselect();
  759. // Initialize Uniform for custom checkboxes
  760. $('.swal2-popup input[type=checkbox]').uniform();
  761. }
  762. }).then(function (result) {
  763. // Display selected values
  764. swal({
  765. type: 'success',
  766. html: 'You selected: ' + JSON.stringify($('.swal2-select.select-multiselect').val())
  767. });
  768. // Show another modal if Cancel button is clicked
  769. if (result.dismiss === 'cancel') {
  770. swal({
  771. title: 'Cancelled',
  772. text: 'You are safe now :)',
  773. type: 'error'
  774. });
  775. }
  776. });
  777. });
  778. };
  779. // SweetAlert with custom checkboxes, radios and file input
  780. var _componentControlsCustom = function() {
  781. if (!$().uniform) {
  782. console.warn('Warning - uniform.min.js is not loaded.');
  783. return;
  784. }
  785. // Modal with custom checkboxes
  786. $('#sweet_checkbox_custom').on('click', function() {
  787. swal({
  788. title: 'Terms and conditions',
  789. input: 'checkbox',
  790. inputValue: 1,
  791. inputClass: 'form-check-styled',
  792. inputPlaceholder: 'I agree with the terms and conditions',
  793. confirmButtonText: 'Continue <i class="icon-arrow-right14 ml-2></i>',
  794. inputValidator: function (value) {
  795. return !value && 'You need to agree with T&C'
  796. },
  797. onOpen: function() {
  798. // Initialize Uniform for custom checkboxes
  799. $('.swal2-checkbox.form-check-styled input[type=checkbox]').uniform();
  800. }
  801. }).then(function (result) {
  802. if (result.value) {
  803. swal({
  804. type: 'success',
  805. text: 'You agreed with T&C :)'
  806. });
  807. }
  808. });
  809. });
  810. // Modal with custom radios
  811. $('#sweet_radio_custom').on('click', function() {
  812. // inputOptions can be an object or Promise
  813. var inputOptions = new Promise(function (resolve) {
  814. resolve({
  815. '#ff0000': 'Red',
  816. '#00ff00': 'Green',
  817. '#0000ff': 'Blue'
  818. });
  819. });
  820. // Initialize
  821. swal({
  822. title: 'Select color',
  823. input: 'radio',
  824. inputOptions: inputOptions,
  825. inputClass: 'form-check-styled',
  826. inputValidator: function (value) {
  827. return !value && 'You need to choose something!'
  828. },
  829. onOpen: function() {
  830. // Initialize Uniform for custom radios
  831. $('.swal2-radio.form-check-styled input[type=radio]').uniform();
  832. }
  833. }).then(function (result) {
  834. if (result.value) {
  835. swal({
  836. type: 'success',
  837. html: 'You selected: ' + result.value
  838. });
  839. }
  840. });
  841. });
  842. // File type
  843. $('#sweet_file').on('click', function() {
  844. swal({
  845. title: 'Select image',
  846. input: 'file',
  847. inputClass: 'form-control',
  848. inputAttributes: {
  849. accept: 'image/*'
  850. },
  851. onOpen: function() {
  852. // Initialize Uniform for custom radios
  853. $('.swal2-file').uniform();
  854. }
  855. }).then(function (result) {
  856. if (result.value) {
  857. var reader = new FileReader();
  858. reader.onload = function (e) {
  859. swal({
  860. title: 'Your uploaded picture',
  861. imageUrl: e.target.result,
  862. imageAlt: 'The uploaded picture'
  863. });
  864. }
  865. reader.readAsDataURL(result.value);
  866. }
  867. });
  868. });
  869. };
  870. // SweetAlert with custom checkboxes, radios and file input
  871. var _componentControlSwitchery = function() {
  872. if (typeof Switchery == 'undefined') {
  873. console.warn('Warning - switchery.min.js is not loaded.');
  874. return;
  875. }
  876. // Modal with custom checkboxes
  877. $('#sweet_switchery').on('click', function() {
  878. swal({
  879. title: 'Terms and conditions',
  880. input: 'checkbox',
  881. inputValue: 0,
  882. inputClass: 'form-check-styled',
  883. inputPlaceholder: 'I agree with the terms and conditions',
  884. confirmButtonText: 'Continue <i class="icon-arrow-right14 ml-2></i>',
  885. inputValidator: function (value) {
  886. return !value && 'You need to agree with T&C'
  887. },
  888. onOpen: function() {
  889. // Initialize Switchery
  890. var elem = document.querySelector('.swal2-checkbox.form-check-styled input[type=checkbox]');
  891. var init = new Switchery(elem);
  892. }
  893. }).then(function (result) {
  894. if (result.value) {
  895. swal({
  896. type: 'success',
  897. text: 'You agreed with T&C :)'
  898. });
  899. }
  900. });
  901. });
  902. };
  903. //
  904. // Return objects assigned to module
  905. //
  906. return {
  907. initComponents: function() {
  908. _componentSweetAlert();
  909. _componentSelect2();
  910. _componentMultiselect();
  911. _componentControlsCustom();
  912. _componentControlSwitchery();
  913. }
  914. }
  915. }();
  916. // Initialize module
  917. // ------------------------------
  918. document.addEventListener('DOMContentLoaded', function() {
  919. SweetAlert.initComponents();
  920. });