-
Notifications
You must be signed in to change notification settings - Fork 7
/
Day3-CompositePattern-06.html
217 lines (168 loc) · 5.71 KB
/
Day3-CompositePattern-06.html
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<html><head></head><body>
<section id="todo"></section>
<script>
const Element = class {
constructor(title) {
this._title = title;
this._observers = new Set();
}
getValue() { return this._getValue(); }
_getValue() { throw '_title() should be overriden'}
addObserver(observer) { this._observers.add(observer); }
removeObserver(observer) { this._observers.delete(observer); }
notifyAll() { this._observers.forEach(o => o.update()); }
}
const TaskFolder = class extends Element {
constructor(title) {
super(title);
this._children = [];
}
add(element) {
this._children.push(element);
}
_getValue() {
const currentEl = Dr.el('section', '@margin-left', '20px');
[
Dr.el('h1', 'innerHTML', '\uD83D\uDCC2 '+ this._title),
Dr.el('input', 'type', 'text', 'placeholder', '폴더나 작업 이름 입력'),
Dr.el('button', 'innerHTML', 'Add Folder', 'addEventListener', ['click', e => console.log('Add Folder')]),
Dr.el('button', 'innerHTML', 'Add Item', 'addEventListener', ['click', e => console.log('Add Item')]),
Dr.el('button', 'innerHTML', 'Add Remove', 'addEventListener', ['click', e => console.log('Remove')]),
].forEach(v => currentEl.appendChild(v));
const c = this._children;
for (let i = 0, len = c.length; i < len; i++) {
currentEl.appendChild(c[i].getValue());
}
return currentEl;
}
}
const TaskItem = class extends Element {
constructor(title) {
super(title);
this._isDone = false;
}
done() {
this._isDone = true;
this.notifyAll();
}
undone() {
this._isDone = false;
this.notifyAll();
}
_getValue() {
const currentEl = Dr.el('section', '@margin-left', '20px');
[
Dr.el('h3', '@display', 'inline', 'innerHTML', '\uD83D\uDCCC ' + this._title + (this._isDone ? ' \u2705' : '')),
Dr.el('button', 'innerHTML', 'Done',
'addEventListener', ['click', e => this.done()]),
Dr.el('button', 'innerHTML', 'Undone',
'addEventListener', ['click', e => this.undone()]),
Dr.el('button', 'innerHTML', 'Remove', 'addEventListener', ['click', e => console.log('Remove')]),
].forEach(v => currentEl.appendChild(v));
return currentEl;
}
}
const Dr = class {
static el(tag, ...attr) {
const el = document.createElement(tag);
for (let i = 0, len = attr.length; i < len;) {
const k = attr[i++], v = attr[i++];
if (typeof el[k] === 'function') el[k].apply(el, Array.isArray(v) ? v : [v]);
else if (k[0] === '@') el.style[k.substr(1)] = v;
else el[k] = v;
}
return el;
}
};
const Observer = class {
update() { this._update(); }
_update() { throw '_update() should be overriden'; }
};
const Renderer = class extends Observer {
constructor(root, rootElId) {
super();
this._root = root;
this._rootEl = document.getElementById(rootElId);
}
render() {
this._rootEl.innerHTML = '';
this._rootEl.appendChild(root.getValue());
}
_update() {
this.render();
}
};
// 6 - Observer Pattern 적용 - TaskItem의 Done, Undone 가능
const root = new TaskFolder('root');
const item1 = new TaskItem('item1');
root.add(item1);
const folder1 = new TaskFolder('folder1');
root.add(folder1);
const item11 = new TaskItem('item11');
folder1.add(item11);
const folder11 = new TaskFolder('folder11');
folder1.add(folder11);
const folder2 = new TaskFolder('folder2');
root.add(folder2);
const renderer = new Renderer(root, 'todo');
// observer 등록
root.addObserver(renderer);
item1.addObserver(renderer);
folder1.addObserver(renderer);
item11.addObserver(renderer);
folder11.addObserver(renderer);
folder2.addObserver(renderer);
renderer.render();
// 5 - Item Done, Undone 핸들러 구현 시 문제 발견(TaskItem이 Renderer를 알고 있어야 한다는..)
// const root = new TaskFolder('root');
// const item1 = new TaskItem('item1');
// root.add(item1);
// const folder1 = new TaskFolder('folder1');
// root.add(folder1);
// const item11 = new TaskItem('item11');
// folder1.add(item11);
// const folder11 = new TaskFolder('folder11');
// folder1.add(folder11);
// const folder2 = new TaskFolder('folder2');
// root.add(folder2);
// const renderer = new Renderer(root, 'todo');
// renderer.render();
// 4 - TaskFolder, TaskItem의 Hierarchy에 따라 들여쓰기 적용
// const root = new TaskFolder('root');
// const item1 = new TaskItem('item1');
// root.add(item1);
// const folder1 = new TaskFolder('folder1');
// root.add(folder1);
// const item11 = new TaskItem('item11');
// folder1.add(item11);
// const folder11 = new TaskFolder('folder11');
// folder1.add(folder11);
// const folder2 = new TaskFolder('folder2');
// root.add(folder2);
// document.getElementById('todo').appendChild(root.getValue());
// 3 - DOM Renderer로 TaskFolder, TaskItem 렌더링
// const rootEl = document.getElementById('todo');
// const folder1 = new TaskFolder('folder1');
// rootEl.appendChild(folder1.getValue());
// const item1 = new TaskItem('folder1');
// rootEl.appendChild(item1.getValue());
// 2 - DOM Renderer 추가 및 테스트
// const el1 = Dr.el('h1', 'innerHTML', 'ABCDE', '@display', 'inline');
// const el2 = Dr.el('h1', 'innerHTML', 'ZXD', '@display', 'inline');
// const button1 = Dr.el('button', 'innerHTML', 'Show Folder', 'addEventListener', ['click', e => console.log('Folder')]);
// document.getElementById('todo').appendChild(el1);
// document.getElementById('todo').appendChild(el2);
// document.getElementById('todo').appendChild(button1);
// 1 - Task, TaskFolder, TaskItem에 Composite 패턴 적용
// const root = new TaskFolder('root');
// const item1 = new TaskItem('item1');
// root.add(item1);
// const folder1 = new TaskFolder('folder1');
// root.add(folder1);
// const item11 = new TaskItem('item11');
// folder1.add(item11);
// const folder11 = new TaskFolder('folder11');
// folder1.add(folder11);
// console.log(root.getValue());
</script>
</body></html>