tissue/resources/assets/js/tissue.ts

74 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-08-06 02:26:51 +09:00
import { fetchGet } from './fetch';
2020-08-07 00:53:36 +09:00
export function suicide<T>(e: T) {
2020-08-07 00:25:43 +09:00
return function (): never {
throw e;
};
2020-08-06 01:09:25 +09:00
}
const die = suicide('Element not found!');
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 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:53:36 +09:00
return el;
2020-08-07 00:25:43 +09:00
}
export function pageSelector(el: Element) {
2020-08-07 00:53:36 +09:00
if (el instanceof HTMLSelectElement) {
el.addEventListener('change', function () {
location.href = this.options[this.selectedIndex].dataset.href as string;
});
}
return el;
2020-08-07 00:25:43 +09:00
}
export function deleteCheckinModal(modal: Element) {
let id: any = null;
modal.querySelector('form')?.addEventListener('submit', function () {
this.action = this.action.replace('@', id);
});
return $(modal).on('show.bs.modal', function (event) {
const target = event.relatedTarget || die();
const dateLabel = this.querySelector('.modal-body .date-label') || die();
dateLabel.textContent = target.dataset.date || null;
id = target.dataset.id;
});
2020-08-07 00:53:36 +09:00
}