import tpl from 'vj/utils/tpl'; import i18n from 'vj/utils/i18n'; import DomDialog from './DomDialog'; export default class Dialog { constructor(options = {}) { this.options = { classes: '', $body: null, $action: null, ...options, }; 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')); } } const buttonOk = tpl``; const buttonCancel = tpl``; const buttonYes = tpl``; const buttonNo = tpl``; export class InfoDialog extends Dialog { constructor(options = {}) { super({ $action: buttonOk, cancelByClickingBack: true, cancelByEsc: true, ...options, }); } } export class ActionDialog extends Dialog { constructor(options = {}) { super({ $action: [buttonCancel, buttonOk].join('\n'), cancelByClickingBack: true, cancelByEsc: true, ...options, }); } } export class ConfirmDialog extends Dialog { constructor(options = {}) { 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;