roles-bot/packages/website/public/script.js

78 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-01-02 19:02:40 +01:00
const json = {
2022-04-14 12:41:23 +02:00
roles: [],
type: "1",
2022-01-02 19:02:40 +01:00
};
2022-04-14 12:41:23 +02:00
document.getElementById('json').innerHTML = hljs.highlight(JSON.stringify(json), { language: 'json' }).value;
2021-12-22 18:40:46 +01:00
2022-04-14 12:41:23 +02:00
for (const tag of ['input', 'select']) {
$(tag).change((e) => {
json[e.currentTarget.id] = e.currentTarget.value.replaceAll('\\n', '\n');
document.getElementById('json').innerHTML = hljs.highlight(JSON.stringify(json), { language: 'json' }).value;
});
}
2021-12-22 18:40:46 +01:00
$('button[id=addRole]').click((e) => {
2022-01-02 19:02:40 +01:00
Swal.fire({
title: 'Add Role',
html:
2022-04-14 12:41:23 +02:00
`<input id="swal-input1" class="swal2-input" placeholder="${json.type === "1" ? 'Button' : 'Option'} Label*" required />` +
2021-12-22 18:40:46 +01:00
'<input id="swal-input2" class="swal2-input" placeholder="Role Id*" required />' +
2022-04-08 18:10:35 +02:00
'<input id="swal-input3" class="swal2-input" placeholder="Emoji" />' +
2022-04-14 12:41:23 +02:00
`${json.type === "2" ? '<input id="swal-input4" class="swal2-input" placeholder="Option Placeholder" />' : ''} ` +
`${json.type === "1" ? '<select id="swal-input5" class="swal2-input" style="display: flex;"><option value="" disabled="">Select a style</option><option value="1">Primary</option><option value="2">Secondary</option><option value="3">Success</option><option value="4">Danger</option></select>' : ''}`,
2022-01-02 19:02:40 +01:00
preConfirm: function () {
return new Promise(function (resolve) {
resolve([
$('#swal-input1').val(),
$('#swal-input2').val(),
2022-04-08 18:10:35 +02:00
$('#swal-input3').val(),
2022-04-14 12:41:23 +02:00
$('#swal-input4').val(),
$('#swal-input5').val()
2022-01-02 19:02:40 +01:00
]);
});
}
}).then(function (result) {
if (result.value?.[0] && result.value?.[1]) {
json.roles.push({
id: result.value[1],
label: result.value[0],
2022-04-08 18:10:35 +02:00
emoji: result.value[2] || null,
2022-04-14 12:41:23 +02:00
placeholder: result.value[3] || null,
style: parseInt(result.value[4]) || 2
2022-01-02 19:02:40 +01:00
});
2021-12-22 18:40:46 +01:00
2022-01-02 19:02:40 +01:00
document.getElementById('json').innerHTML = hljs.highlight(JSON.stringify(json), { language: 'json' }).value;
} else Swal.fire('Missing parameters');
}).catch(swal.noop);
});
2021-12-22 18:40:46 +01:00
$('pre[id=jsonPre].copy').click((e) => {
2022-01-02 19:02:40 +01:00
navigator.clipboard.writeText(e?.currentTarget?.textContent || e.textContent);
2021-12-22 18:40:46 +01:00
2022-01-02 19:02:40 +01:00
Swal.fire({
position: 'top-end',
icon: 'success',
title: 'Copied!',
showConfirmButton: false,
timer: 1500
});
});
2021-12-22 18:40:46 +01:00
$('button[id=buttonCopy]').click((e) => {
2022-01-02 19:02:40 +01:00
const element = $('pre[id=jsonPre].copy')[0];
navigator.clipboard.writeText(element?.currentTarget?.textContent || element.textContent);
2021-12-22 18:40:46 +01:00
2022-01-02 19:02:40 +01:00
Swal.fire({
position: 'top-end',
icon: 'success',
title: 'Copied!',
showConfirmButton: false,
timer: 1500
});
});
2021-12-22 18:40:46 +01:00
$(window).on('load', () => {
2022-01-02 19:02:40 +01:00
$('input').toArray().forEach((i) => i.value = '');
document.getElementById('json').innerHTML = hljs.highlight(JSON.stringify(json), { language: 'json' }).value;
});