import $ from 'jquery'; import i18n from 'vj/utils/i18n'; import tpl from 'vj/utils/tpl'; import DomDialog, { DialogOptions } from './DomDialog'; export class Dialog { options: DialogOptions; $dom: JQuery; domDialogInstance: DomDialog; constructor(options: Partial = {}) { this.options = { classes: '', $body: null, $action: null, ...options, }; let box = ''; if (options.width) box += `width:${options.width};max-width:${options.width};`; if (options.height) box += `height:${options.height};max-height:${options.height};`; this.$dom = $(tpl` `); this.$dom.on('click', '[data-action]', this.handleActionButton.bind(this)); this.$dom.on('vjDomDialogShow', this.beforeShow.bind(this)); this.$dom.on('vjDomDialogHidden', this.afterHide.bind(this)); this.$dom.find('.dialog__body').append(this.options.$body); this.$dom.find('.dialog__action').append(this.options.$action); this.domDialogInstance = new DomDialog(this.$dom, this.options); } beforeShow() { this.$dom.appendTo('body'); } afterHide() { this.$dom.detach(); } open() { return this.domDialogInstance.show(); } close() { return this.domDialogInstance.hide(); } handleActionButton(ev) { this.domDialogInstance.dispatchAction($(ev.currentTarget).attr('data-action')); } } export default Dialog; const buttonOk = tpl``; const buttonCancel = tpl``; const buttonYes = tpl``; const buttonNo = tpl``; export class InfoDialog extends Dialog { constructor(options: Partial = {}) { super({ $action: buttonOk, cancelByClickingBack: true, cancelByEsc: true, ...options, }); } } export class ActionDialog extends Dialog { constructor(options: Partial = {}) { super({ $action: [buttonCancel, buttonOk].join('\n'), cancelByClickingBack: true, cancelByEsc: true, ...options, }); } clear() { this.$dom.find('input').val(''); return this; } } export class ConfirmDialog extends Dialog { constructor(options: Partial = {}) { let buttons = []; if (options.canCancel) { buttons = [buttonCancel, buttonNo, buttonYes]; } else { buttons = [buttonNo, buttonYes]; } super({ $action: buttons.join('\n'), cancelByClickingBack: options.canCancel, cancelByEsc: options.canCancel, ...options, }); } } window.Hydro.components.Dialog = Dialog; window.Hydro.components.InfoDialog = InfoDialog; window.Hydro.components.ActionDialog = ActionDialog; window.Hydro.components.ConfirmDialog = ConfirmDialog;