tissue/resources/assets/js/tissue.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-08-06 02:26:51 +09:00
import { fetchGet } from './fetch';
2020-08-07 00:25:43 +09:00
function suicide<T>(e: T) {
return function (): never {
throw e;
};
2020-08-06 01:09:25 +09:00
}
2020-08-07 00:25:43 +09:00
export function linkCard(el: Element) {
const url = el.querySelector('a')?.href;
if (!url) {
return;
}
2020-08-07 00:25:43 +09:00
fetchGet('/api/checkin/card', { url })
.then((response) => response.json())
.then((data) => {
const die = suicide('Element not found!');
const metaColumn = el.querySelector('.col-12:last-of-type') || die();
const imageColumn = el.querySelector<HTMLElement>('.col-12:first-of-type') || die();
const title = el.querySelector<HTMLElement>('.card-title') || die();
const desc = el.querySelector<HTMLElement>('.card-text') || die();
const image = imageColumn.querySelector('img') || die();
2020-08-06 02:26:51 +09:00
2020-08-07 00:25:43 +09:00
if (data.title === '') {
title.style.display = 'none';
} else {
title.textContent = data.title;
2020-08-06 02:26:51 +09:00
}
2020-08-07 00:25:43 +09:00
if (data.description === '') {
desc.style.display = 'none';
} else {
desc.textContent = data.description;
}
2020-08-06 02:26:51 +09:00
2020-08-07 00:25:43 +09:00
if (data.image === '') {
imageColumn.style.display = 'none';
metaColumn.classList.remove('col-md-6');
} else {
image.src = data.image;
}
2020-08-06 02:26:51 +09:00
2020-08-07 00:25:43 +09:00
if (data.title !== '' || data.description !== '' || data.image !== '') {
el.classList.remove('d-none');
}
});
2020-08-07 00:25:43 +09:00
}
export function pageSelector(el: Element) {
if (!(el instanceof HTMLSelectElement)) return;
el.addEventListener('change', function () {
location.href = this.options[this.selectedIndex].dataset.href as string;
});
}
2020-08-07 00:25:43 +09:00
(function ($) {
$.fn.deleteCheckinModal = function () {
return this.each(function () {
$(this)
.on('show.bs.modal', function (event) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const target = $(event.relatedTarget!);
const modal = $(this);
modal.find('.modal-body .date-label').text(target.data('date'));
modal.data('id', target.data('id'));
})
.find('.btn-danger')
2020-06-06 18:40:49 +09:00
.on('click', function (_event) {
const modal = $('#deleteCheckinModal');
const form = modal.find('form');
form.attr('action', form.attr('action')?.replace('@', modal.data('id')) || null);
form.submit();
});
});
};
})(jQuery);