tissue/resources/assets/js/tissue.ts
2020-08-07 00:27:15 +09:00

78 lines
2.6 KiB
TypeScript

import { fetchGet } from './fetch';
function suicide<T>(e: T) {
return function (): never {
throw e;
};
}
export function linkCard(el: Element) {
const url = el.querySelector('a')?.href;
if (!url) {
return;
}
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();
if (data.title === '') {
title.style.display = 'none';
} else {
title.textContent = data.title;
}
if (data.description === '') {
desc.style.display = 'none';
} else {
desc.textContent = data.description;
}
if (data.image === '') {
imageColumn.style.display = 'none';
metaColumn.classList.remove('col-md-6');
} else {
image.src = data.image;
}
if (data.title !== '' || data.description !== '' || data.image !== '') {
el.classList.remove('d-none');
}
});
}
export function pageSelector(el: Element) {
if (!(el instanceof HTMLSelectElement)) return;
el.addEventListener('change', function () {
location.href = this.options[this.selectedIndex].dataset.href as string;
});
}
(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')
.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);