-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathComponent.js
More file actions
148 lines (128 loc) · 3.88 KB
/
Component.js
File metadata and controls
148 lines (128 loc) · 3.88 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : Component.js
* Author : Black Logic
* Note : Base Components for rendering objects
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 11. 18
* Change Date :
*/
//============================================================================
// [CLASS] Component
//============================================================================
define([
'../com_util',
'../com_String'
], function(com_util, com_String) {
'use strict';
//========================================================================
// Declare class
//========================================================================
/**
* Component
*/
class Component {
constructor($target, state, prop={}) {
// get uuid
this._uuid = com_util.getUUID();
// target, pageDom query objects
this.$target = $target;
this.$pageDom = '';
// save state
this.state = state;
// save propagation from parent
this.prop = prop;
this._init();
this.load();
this.render();
}
wrapSelector(selector='') {
var sbSelector = new com_String();
var cnt = arguments.length;
if (cnt < 2) {
// if there's no more arguments
sbSelector.appendFormat(".{0} {1}", this.uuid, selector);
} else {
// if there's more arguments
sbSelector.appendFormat(".{0}", this.uuid);
for (var idx = 0; idx < cnt; idx++) {
sbSelector.appendFormat(" {0}", arguments[idx]);
}
}
return sbSelector.toString();
}
_init() {
/** Implementation needed */
}
_unbindEvent() {
/** Implementation needed */
}
_bindEvent() {
/** Implementation needed */
}
/**
* Load data
*/
load() {
/** Implementation needed */
}
/**
* Generate template using states and return
* @returns template DOM
*/
template() {
/** Implementation needed */
return '';
}
/**
* Render component under $target
* @param {*} inplace overwrite under $target
*/
render(inplace=false) {
this.$pageDom = $(this.template());
this.$pageDom.addClass(this.uuid);
let $page = this.$target.find('.' + this.uuid);
if ($page.length > 0) {
// if exists, replace it
$page.replaceWith(this.$pageDom);
} else {
// if not exists...
if (inplace) {
// replace under $target
this.$target.html(this.$pageDom);
} else {
// append under $target
this.$target.append(this.$pageDom);
}
}
this._bindEvent();
}
equals(component) {
if (!component) {
return false;
}
return component.uuid === this.uuid;
}
get uuid() {
return this._uuid;
}
getTag() {
return $(this.wrapSelector());
}
setState(stateObj) {
this.state = {
...this.state,
...stateObj
}
}
getState(stateKey=undefined) {
if (stateKey == undefined) {
return this.state;
}
return this.state[stateKey];
}
}
return Component;
});
/* End of file */