From 23189b76e40158f18371be30f071e8922a8015e4 Mon Sep 17 00:00:00 2001 From: shibafu Date: Fri, 7 Aug 2020 00:53:36 +0900 Subject: [PATCH] =?UTF-8?q?deleteCheckinModal=E3=82=92Vanilla=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/assets/js/@types/jquery-tissue.d.ts | 5 -- resources/assets/js/app.ts | 16 +++--- resources/assets/js/bootstrap.ts | 3 -- resources/assets/js/tissue.ts | 53 ++++++++++--------- 4 files changed, 37 insertions(+), 40 deletions(-) delete mode 100644 resources/assets/js/@types/jquery-tissue.d.ts diff --git a/resources/assets/js/@types/jquery-tissue.d.ts b/resources/assets/js/@types/jquery-tissue.d.ts deleted file mode 100644 index 0494a7b..0000000 --- a/resources/assets/js/@types/jquery-tissue.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -// tissue.ts で定義されているjQuery Pluginの型定義 - -interface JQuery { - deleteCheckinModal: () => this; -} diff --git a/resources/assets/js/app.ts b/resources/assets/js/app.ts index fda7cbf..10fdc70 100644 --- a/resources/assets/js/app.ts +++ b/resources/assets/js/app.ts @@ -1,6 +1,6 @@ import * as Cookies from 'js-cookie'; import { fetchPostJson, fetchDeleteJson, ResponseError } from './fetch'; -import { linkCard, pageSelector } from './tissue'; +import { linkCard, pageSelector, deleteCheckinModal } from './tissue'; require('./bootstrap'); @@ -24,11 +24,15 @@ $(() => { document.querySelectorAll('.tis-page-selector').forEach(pageSelector); document.querySelectorAll('.link-card').forEach(linkCard); - const $deleteCheckinModal = $('#deleteCheckinModal').deleteCheckinModal(); - $(document).on('click', '[data-target="#deleteCheckinModal"]', function (event) { - event.preventDefault(); - $deleteCheckinModal.modal('show', this); - }); + + const elDeleteCheckinModal = document.getElementById('deleteCheckinModal'); + if (elDeleteCheckinModal) { + const $deleteCheckinModal = deleteCheckinModal(elDeleteCheckinModal); + $(document).on('click', '[data-target="#deleteCheckinModal"]', function (event) { + event.preventDefault(); + $deleteCheckinModal.modal('show', this); + }); + } $(document).on('click', '[data-href]', function (_event) { location.href = $(this).data('href'); diff --git a/resources/assets/js/bootstrap.ts b/resources/assets/js/bootstrap.ts index fbfc914..02b71c6 100644 --- a/resources/assets/js/bootstrap.ts +++ b/resources/assets/js/bootstrap.ts @@ -1,5 +1,2 @@ -// jQuery -import './tissue'; - // Bootstrap import 'bootstrap'; diff --git a/resources/assets/js/tissue.ts b/resources/assets/js/tissue.ts index 0e0b945..ae2ddcb 100644 --- a/resources/assets/js/tissue.ts +++ b/resources/assets/js/tissue.ts @@ -1,6 +1,6 @@ import { fetchGet } from './fetch'; -function suicide(e: T) { +export function suicide(e: T) { return function (): never { throw e; }; @@ -45,33 +45,34 @@ export function linkCard(el: Element) { el.classList.remove('d-none'); } }); + + return el; } export function pageSelector(el: Element) { - if (!(el instanceof HTMLSelectElement)) return; - el.addEventListener('change', function () { - location.href = this.options[this.selectedIndex].dataset.href as string; - }); + if (el instanceof HTMLSelectElement) { + el.addEventListener('change', function () { + location.href = this.options[this.selectedIndex].dataset.href as string; + }); + } + return el; } -(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); +export function deleteCheckinModal(el: Element) { + return $(el) + .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(); + }) + .end(); +}