Files
justice/www/html/js/jd-swal.js
T

99 lines
3.3 KiB
JavaScript

/**
* JD SweetAlert2 helpers — ใช้แทน alert / confirm / prompt ของเบราว์เซอร์
* ต้องโหลด sweetalert2 ก่อนไฟล์นี้
*/
(function (g) {
'use strict';
var baseClass = {
popup: 'jd-swal-popup',
title: 'jd-swal-title',
htmlContainer: 'jd-swal-text',
confirmButton: 'jd-swal-btn-confirm',
cancelButton: 'jd-swal-btn-cancel',
input: 'jd-swal-input',
};
function hasSwal() {
return !!(g.Swal && typeof g.Swal.fire === 'function');
}
function mergeOpts(opts) {
opts = opts || {};
var customClass = Object.assign({}, baseClass, opts.customClass || {});
return Object.assign({
confirmButtonColor: '#2563eb',
cancelButtonColor: '#64748b',
buttonsStyling: true,
customClass: customClass,
}, opts, { customClass: customClass });
}
g.jdAlert = function jdAlert(message, opts) {
opts = mergeOpts(opts);
var text = String(message == null ? '' : message);
if (!hasSwal()) {
g.alert(text);
return Promise.resolve();
}
return g.Swal.fire(Object.assign({
icon: opts.icon || 'info',
title: opts.title || 'แจ้งเตือน',
text: text,
confirmButtonText: opts.confirmButtonText || 'ตกลง',
}, opts));
};
g.jdConfirm = function jdConfirm(message, opts) {
opts = mergeOpts(opts);
var text = String(message == null ? '' : message);
if (!hasSwal()) {
return Promise.resolve(!!g.confirm(text));
}
return g.Swal.fire(Object.assign({
icon: opts.icon || 'warning',
title: opts.title || 'ยืนยัน',
text: text,
showCancelButton: true,
confirmButtonText: opts.confirmButtonText || 'ตกลง',
cancelButtonText: opts.cancelButtonText || 'ยกเลิก',
reverseButtons: true,
focusCancel: true,
}, opts)).then(function (r) { return !!(r && r.isConfirmed); });
};
g.jdPrompt = function jdPrompt(message, defaultValue, opts) {
if (defaultValue != null && typeof defaultValue === 'object') {
opts = defaultValue;
defaultValue = '';
}
opts = mergeOpts(opts || {});
var text = String(message == null ? '' : message);
var inputValue = defaultValue == null ? '' : String(defaultValue);
if (!hasSwal()) {
var v = g.prompt(text, inputValue);
return Promise.resolve(v == null ? null : String(v));
}
return g.Swal.fire(Object.assign({
icon: opts.icon || 'question',
title: opts.title || 'กรอกข้อมูล',
text: text,
input: opts.input || 'text',
inputValue: inputValue,
inputPlaceholder: opts.inputPlaceholder || '',
showCancelButton: true,
confirmButtonText: opts.confirmButtonText || 'ตกลง',
cancelButtonText: opts.cancelButtonText || 'ยกเลิก',
reverseButtons: true,
}, opts)).then(function (r) {
if (!r || !r.isConfirmed) return null;
return r.value == null ? '' : String(r.value);
});
};
if (hasSwal()) {
g.__jdNativeAlert = g.alert;
g.alert = function (msg) { jdAlert(msg, { icon: 'info' }); };
}
})(typeof window !== 'undefined' ? window : globalThis);