/* ------------------------------------------------------------------------------ * * # Sweet Alert component * * Demo JS code for extra_sweetalert.html page * * ---------------------------------------------------------------------------- */ // Setup module // ------------------------------ var SweetAlert = function () { // // Setup module components // // Sweet Alerts var _componentSweetAlert = function() { if (typeof swal == 'undefined') { console.warn('Warning - sweet_alert.min.js is not loaded.'); return; } // Defaults var swalInit = swal.mixin({ buttonsStyling: false, confirmButtonClass: 'btn btn-primary', cancelButtonClass: 'btn btn-light' }); // // Basic options // // Basic $('#sweet_basic').on('click', function() { swalInit({ title: 'Here is a message!' }); }); // With title $('#sweet_title_text').on('click', function() { swalInit({ title: 'Here\'s a message!', text: 'It\'s pretty, isn\'t it?' }); }); // Close button $('#sweet_close').on('click', function() { swalInit({ title: 'Good job!', text: 'You clicked the button!', type: 'success', showCloseButton: true }); }); // Custom padding $('#sweet_padding').on('click', function() { swalInit({ title: 'Oops...', text: 'Something went wrong!', type: 'error', padding: 40 }); }); // Custom width $('#sweet_width').on('click', function() { swalInit({ title: 'Got question?', text: 'If you have any questions, don\t hesitate to let us know!', type: 'question', width: '35%' }); }); // Auto closing $('#sweet_auto_closer').on('click', function() { swalInit({ title: 'Auto close alert!', text: 'I will close in 2 seconds.', timer: 2000 }); }); // Dynamic queue $('#sweet_queue').on('click', function() { swalInit.queue([{ title: 'Your public IP', confirmButtonText: 'Show my public IP', text: 'Your public IP will be received ' + 'via AJAX request', showLoaderOnConfirm: true, preConfirm: function() { return fetch('https://api.ipify.org?format=json') .then(function(response) { return response.json(); }) .then(function(data) { return swal.insertQueueStep(data.ip); }) .catch(function() { swalInit.insertQueueStep({ type: 'error', title: 'Unable to get your public IP' }); }); } }]); }); // AJAX requests $('#sweet_ajax').on('click', function() { swalInit({ title: 'Submit your Github username', input: 'text', inputAttributes: { autocapitalize: 'off' }, inputPlaceholder: 'Enter Github username', inputClass: 'form-control', showCancelButton: true, confirmButtonText: 'Look up', showLoaderOnConfirm: true, preConfirm: function(login) { return fetch('https://api.github.com/users/' + login) .then(function(response) { if(!response.ok) { throw new Error(response.statusText) } return response.json(); }) .catch(function(error) { swalInit.showValidationMessage( 'Request failed: ' + error ); }); }, allowOutsideClick: false }).then(function(result) { if(result.value) { swalInit({ title: result.value.login + '\'s avatar', imageUrl: result.value.avatar_url }); } }); }); // HTML message $('#sweet_html').on('click', function() { swalInit({ title: 'HTML example', type: 'info', html: 'You can use bold text, ' + 'links ' + 'and other HTML tags', showCloseButton: true, showCancelButton: true, focusConfirm: false, confirmButtonText: ' Great!', confirmButtonAriaLabel: 'Thumbs up, great!', cancelButtonText: '', cancelButtonAriaLabel: 'Thumbs down' }); }); // Image $('#sweet_image').on('click', function() { swalInit({ title: 'Sweet!', text: 'Bootstrap is awesome.', imageUrl: 'https://getbootstrap.com/docs/4.1/assets/img/bootstrap-stack.png', imageWidth: 260 }); }); // Background image $('#sweet_bg').on('click', function() { swalInit({ title: 'Sweet!', type: 'success', text: 'Custom backgrounds are awesome.', background: '#fff url(../../../../global_assets/images/backgrounds/seamless.png) repeat' }); }); // Chaining notifications $('#sweet_chain').on('click', function() { swal.mixin({ input: 'text', confirmButtonText: 'Next ', showCancelButton: true, inputClass: 'form-control', animation: false, buttonsStyling: false, confirmButtonClass: 'btn btn-primary', cancelButtonClass: 'btn btn-light', progressSteps: ['1', '2', '3'] }).queue([ { title: 'Question 1', text: 'Step #1 - ask your first question', inputPlaceholder: 'Enter your first question' }, { title: 'Question 2', text: 'Step #2 - ask your second question', inputPlaceholder: 'Enter your second question' }, { title: 'Question 3', text: 'Step #3 - ask your third question', inputPlaceholder: 'Enter your third question' } ]).then(function(result) { if(result.value) { swalInit({ title: 'All done!', html: 'Your answers:
' +
JSON.stringify(result.value) +
'',
confirmButtonText: 'Lovely!'
});
}
});
});
// Reversed buttons
$('#sweet_reverse_buttons').on('click', function() {
swalInit({
title: 'What is your name?',
input: 'text',
inputPlaceholder: 'Your name or nickname',
showCancelButton: true,
inputClass: 'form-control',
confirmButtonText: 'Submit',
reverseButtons: true
});
});
// Fullscreen
$('#sweet_fullscreen').on('click', function() {
swalInit({
title: 'Here\'s a message!',
text: 'It\'s pretty, isn\'t it?',
type: 'success',
showConfirmButton: false,
showCloseButton: true,
grow: 'fullscreen'
});
});
// Column grow
$('#sweet_column').on('click', function() {
swalInit({
title: 'Here\'s a message!',
text: 'It\'s pretty, isn\'t it?',
type: 'success',
showConfirmButton: false,
showCloseButton: true,
grow: 'column'
});
});
// Row grow
$('#sweet_row').on('click', function() {
swalInit({
title: 'Here\'s a message!',
text: 'It\'s pretty, isn\'t it?',
type: 'success',
grow: 'row'
});
});
// Disabled keyboard interactions
$('#sweet_disabled_keyboard').on('click', function() {
swalInit({
title: 'Oops...',
text: 'Something went wrong!',
type: 'error',
allowEscapeKey: false,
allowEnterKey: false
});
});
// Disabled animation
$('#sweet_disabled_animation').on('click', function() {
swalInit({
title: 'For your information',
text: 'This is some sort of a custom alert',
type: 'info',
animation: false
});
});
// Disabled backdrop
$('#sweet_disabled_backdrop').on('click', function() {
swalInit({
title: 'Got question?',
text: 'You will get the answer soon!',
type: 'question',
allowOutsideClick: false,
backdrop: false
});
});
// Disabled backdrop
$('#sweet_disabled_outside_click').on('click', function() {
swalInit({
title: 'Oops...',
text: 'Something went wrong!',
type: 'error',
allowOutsideClick: false
});
});
//
// Input types
//
// Text type
$('#sweet_text').on('click', function() {
swalInit({
title: 'What is your name?',
input: 'text',
inputPlaceholder: 'Your name or nickname',
showCancelButton: true,
inputClass: 'form-control',
inputValidator: function(value) {
return !value && 'You need to write something!'
}
}).then(function(result) {
if(result.value) {
swalInit({
type: 'success',
html: 'Hi, ' + result.value
});
}
});
});
// Email type
$('#sweet_email').on('click', function() {
swalInit({
title: 'Input email address',
input: 'email',
inputClass: 'form-control',
inputPlaceholder: 'Enter your email',
}).then(function(result) {
if(result.value) {
swalInit({
type: 'success',
html: 'Entered email: ' + result.value
});
}
});
});
// URL type
$('#sweet_url').on('click', function() {
swalInit({
title: 'Input URL',
input: 'url',
inputClass: 'form-control',
inputPlaceholder: 'Enter URL',
}).then(function(result) {
if(result.value) {
swalInit({
type: 'success',
html: 'Entered URL: ' + result.value
});
}
});
});
// Password type
$('#sweet_password').on('click', function() {
swalInit({
title: 'Enter your password',
input: 'password',
inputClass: 'form-control',
inputPlaceholder: 'Enter your password',
inputAttributes: {
'maxlength': 10,
'autocapitalize': 'off',
'autocorrect': 'off'
}
}).then(function(result) {
if(result.value) {
swalInit({
type: 'success',
html: 'Entered password: ' + result.value
});
}
});
});
// Textarea type
$('#sweet_textarea').on('click', function() {
swalInit({
title: 'Enter your comment',
input: 'textarea',
inputPlaceholder: 'Type your message here',
showCancelButton: true,
inputClass: 'form-control'
}).then(function(result) {
if(result.value) {
swalInit({
title: 'Your comment:',
text: result.value
});
}
});
});
// Select type
$('#sweet_select').on('click', function() {
swalInit({
title: 'Select Netherlands',
input: 'select',
inputOptions: {
'DE': 'Germany',
'UA': 'Ukraine',
'HR': 'Croatia',
'NL': 'Netherlands'
},
inputPlaceholder: 'Select country',
inputClass: 'form-control',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if(value === 'NL') {
resolve();
}
else {
resolve('You need to select Netherlands :)');
}
});
}
}).then(function(result) {
if(result.value) {
swalInit({
type: 'success',
html: 'You selected: ' + result.value
});
}
});
});
// Radio type
$('#sweet_radio').on('click', function() {
// inputOptions can be an object or Promise
var inputOptions = new Promise(function(resolve) {
setTimeout(function() {
resolve({
'#ff0000': 'Red',
'#00ff00': 'Green',
'#0000ff': 'Blue'
});
}, 2000)
});
// Initialize
swalInit({
title: 'Select color',
input: 'radio',
inputOptions: inputOptions,
inputValidator: function(value) {
return !value && 'You need to choose something!'
}
}).then(function(result) {
if(result.value) {
swalInit({
type: 'success',
html: 'You selected: ' + result.value
});
}
});
});
// Checkbox type
$('#sweet_checkbox').on('click', function() {
swalInit({
title: 'Terms and conditions',
input: 'checkbox',
inputValue: 1,
inputPlaceholder: 'I agree with the terms and conditions',
confirmButtonText: 'Continue ',
inputValidator: function(value) {
return !value && 'You need to agree with T&C'
},
onOpen: function() {
// Initialize Switchery
var elem = document.querySelector('.swal2-checkbox.form-check-styled input[type=checkbox]');
var init = new Switchery(elem);
}
}).then(function(result) {
if(result.value) {
swalInit({
type: 'success',
text: 'You agreed with T&C :)'
});
}
});
});
};
//
// Return objects assigned to module
//
return {
initComponents: function() {
_componentSweetAlert();
_componentSelect2();
_componentMultiselect();
_componentControlsCustom();
_componentControlSwitchery();
}
}
}();
// Initialize module
// ------------------------------
document.addEventListener('DOMContentLoaded', function() {
SweetAlert.initComponents();
});