-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (90 loc) · 2.21 KB
/
index.js
File metadata and controls
109 lines (90 loc) · 2.21 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
/**
* Super Class
*/
class ServerLeaseComponent extends HTMLElement {
baseUrl = 'http://13.209.218.50:8000';
connectedCallback() {
this.setTemplate();
this.initialize();
}
attributeChangedCallback() {
this.initialize();
}
async initialize() {
this.setMeta();
this.setData(await this.fetchData());
}
render() {
this.innerHTML = this.applyTemplate(this.data);
}
setMeta() {
Object.keys(this.dataset).forEach((key) => {
this[key] = this.dataset[key];
});
}
setData(data) {
this.data = data;
this.render();
}
setTemplate() {
this.template = this.innerHTML;
}
fetchData() {}
applyTemplate() {}
}
/**
* Board
*/
class ServerLeaseBoard extends ServerLeaseComponent {
static get observedAttributes() {
return ['data-board-id', 'data-board-page'];
}
applyTemplate(data) {
return data.items.reduce(
(acc, item) =>
acc +
Object.keys(item).reduce(
(acc, key) => acc.replaceAll(`{{ %% BOARD_POST_${key.toUpperCase()} %% }}`, item[key]),
this.template,
),
'',
);
}
fetchData() {
return fetch(`${this.baseUrl}/${this.boardId}?page=${this.boardPage}`) //
.then((res) => res.json());
}
}
/**
* View
*/
class ServerLeaseView extends ServerLeaseComponent {
static get observedAttributes() {
return ['data-board-id', 'data-view-id'];
}
applyTemplate(data) {
return Object.keys(data).reduce(
(acc, key) => acc.replaceAll(`{{ %% VIEW_POST_${key.toUpperCase()} %% }}`, data[key]),
this.template,
);
}
fetchData() {
return fetch(`${this.baseUrl}/${this.boardId}/${this.viewId}`) //
.then((res) => res.json());
}
}
const getUrlParam = (param) => {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
return urlParams.get(param);
};
const viewElem = document.getElementsByTagName('server-lease-view')[0];
if (viewElem) viewElem.dataset.viewId = getUrlParam('id');
const boardElem = document.getElementsByTagName('server-lease-board')[0];
if (boardElem) {
const page = getUrlParam('page');
if (page) boardElem.dataset.boardPage = page;
else boardElem.dataset.boardPage = 1;
}
customElements.define('server-lease-view', ServerLeaseView);
customElements.define('server-lease-board', ServerLeaseBoard);