philomena/assets/js/sources.ts

33 lines
867 B
TypeScript
Raw Normal View History

import { $ } from './utils/dom';
import { inputDuplicatorCreator } from './input-duplicator';
2024-06-10 23:20:44 +02:00
export interface TagSourceEvent extends CustomEvent<Response> {
target: HTMLElement,
}
function setupInputs() {
inputDuplicatorCreator({
addButtonSelector: '.js-image-add-source',
fieldSelector: '.js-image-source',
maxInputCountSelector: '.js-max-source-count',
removeButtonSelector: '.js-source-remove',
});
}
function imageSourcesCreator() {
setupInputs();
2024-06-10 23:20:44 +02:00
document.addEventListener('fetchcomplete', (({ target, detail }: TagSourceEvent) => {
const sourceSauce = $<HTMLElement>('.js-sourcesauce');
if (sourceSauce && target && target.matches('#source-form')) {
detail.text().then(text => {
sourceSauce.outerHTML = text;
setupInputs();
});
}
}) as EventListener);
}
export { imageSourcesCreator };