-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathModal.js
More file actions
159 lines (147 loc) · 5.64 KB
/
Modal.js
File metadata and controls
159 lines (147 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* 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("<div id='vp_multiButtonModal'>");
sbTagString.appendLine("<div class='vp-multi-button-modal-box'>");
sbTagString.appendLine("<div class='vp-multi-button-modal-message'>");
sbTagString.appendLine("<div class='vp-multi-button-modal-message-inner'>");
sbTagString.appendFormatLine("<span>{0}</span>", title);
sbTagString.appendFormatLine("<p>{0}</p>", message);
sbTagString.appendLine("</div>");
sbTagString.appendLine("</div>");
sbTagString.appendLine("<div class='vp-multi-button-modal-buttons'>");
buttons && buttons.forEach((btn, idx) => {
sbTagString.appendFormatLine("<input class='vp-button vp-modal-button {0} {1}' data-idx={2} type='button' value='{3}' />",
defaultButtonIdx == idx? 'vp-modal-selected-button' : '',
buttonClass[idx],
idx,
btn);
});
sbTagString.appendLine("</div>");
sbTagString.appendLine("</div>");
sbTagString.appendLine("</div>");
sbTagString.appendLine("</div>");
return sbTagString.toString();
}
render() {
/** Implement after rendering */
}
//============================================================================
// Settings
//============================================================================
/**
* Open modal tag
* @param {function} callBackList
*/
open() {
// render on open
super.render();
/** focus on default button */
$(this.wrapSelector('.vp-modal-button[data-idx="' + this.state.defaultButtonIdx + '"]')).focus();
// show
$(this.wrapSelector()).show();
}
close() {
$(document).unbind(com_util.formatString(".{0}", this.uuid));
$(document).unbind(com_util.formatString("keydown.{0}", this.uuid));
$(this.wrapSelector()).remove();
}
/**
* close using esc
* @param {Event} event
*/
handleEscToExit(event) {
var keyCode = event.keyCode ? event.keyCode : event.which;
// esc
if (keyCode == 27) {
this.close();
}
}
/**
* select using enter
* @param {Event} event
*/
handleEnterToClickButton(event) {
var keyCode = event.keyCode ? event.keyCode : event.which;
// enter
if (keyCode == 13) {
$(this.wrapSelector('.vp-modal-button[data-idx="' + this.state.defaultButtonIdx + '"]')).click();
this.close();
}
}
}
return Modal;
});