demarches-normaliennes/app/javascript/controllers/replace_attachment_controller.ts

31 lines
1,017 B
TypeScript
Raw Normal View History

import { ApplicationController } from './application_controller';
import { show } from '@utils';
export class ReplaceAttachmentController extends ApplicationController {
static targets = ['input'];
declare readonly inputTarget: HTMLInputElement;
open(event: Event) {
show(this.inputTarget);
this.inputTarget.click(); // opens input prompt
const target = event.currentTarget as HTMLButtonElement;
if (target.dataset.autoAttachUrl) {
// set the auto attach url specific to this button to replace the related attachment
this.inputTarget.dataset.originalAutoAttachUrl =
this.inputTarget.dataset.autoAttachUrl;
this.inputTarget.dataset.autoAttachUrl = target.dataset.autoAttachUrl;
// reset autoAttachUrl which would add an attachment
// when replace is not finalized
this.inputTarget.addEventListener('cancel', () => {
this.inputTarget.dataset.autoAttachUrl =
this.inputTarget.dataset.originalAutoAttachUrl;
});
}
}
}