/* * Project Name : Visual Python * Description : GUI-based Python code generator * File Name : Modal.js * Author : Black Logic * Note : Modal * License : GNU GPLv3 with Visual Python special exception * Date : 2021. 11. 18 * Change Date : */ //============================================================================ // [CLASS] Modal //============================================================================ define([ 'css!vp_base/css/component/modal.css', 'vp_base/js/com/com_String', 'vp_base/js/com/component/Component', 'vp_base/js/com/com_util' ], function(modalCss, com_String, Component, com_util) { /** * Modal */ class Modal extends Component { /** * * @param {Object} state {title, message, buttons, defaultButtonIdx, finish} * - title : string / modal title * - message : string / modal message * - buttons : list / at least 1 button needed * [optional] * - defaultButtonIdx : int (default: 1) * - finish : callback function when modal button clicked (result:button idx 0~n) * - buttonClass : list / same length as buttons, define classes for buttons */ constructor(state) { super($('body'), state); } _init() { super._init(); this.state = { title: '', message: '', buttons: [], defaultButtonIdx: 1, finish: function(modalIdx) { /* Implementation needed */ }, buttonClass: [], ...this.state } /** Write codes executed before rendering */ if (this.state.buttons.length == 0) { this.state.buttons.push("OK"); } } _bindEvent() { /** Implement binding events */ let that = this; // button click event $(this.wrapSelector('.vp-modal-button')).on('click', function () { if (typeof that.state.finish == "function") { let modalIdx = parseInt($(this).data('idx')); that.state.finish(modalIdx); } that.close(); }); // esc key to close, enter key to select $(document).bind(com_util.formatString('keydown.{0}', this.uuid), function (event) { that.handleEscToExit(event); that.handleEnterToClickButton(event); }); } template() { /** Implement generating template */ let { title, message, buttons, defaultButtonIdx, buttonClass } = this.state; var sbTagString = new com_String(); sbTagString.appendLine("