-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
155 lines (109 loc) · 4.86 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body style="color:black">
<div is="main-content"></div>
<script>
const has_call_backend = {
call_backend(name_method, array_arguments = []) {
return new Promise((resolve, reject) => {
google.script.run
.withSuccessHandler((result) => {
resolve(result)
})
.withFailureHandler((result) => {
reject(result)
})[name_method](...array_arguments);
});
}
}
class SelectScript extends HTMLDivElement {
static get tag() {
return 'select-script';
}
async connectedCallback() {
this.innerHTML = 'loading scripts';
const array_objects_scripts = JSON.parse(await this.call_backend('get_array_objects_scripts'));
const html_options = array_objects_scripts
.map(object_script => `<option value="${object_script.id}">${object_script.name}</option>`)
.join('');
this.innerHTML = `
<label for="${this.id}-select">${this.getAttribute('additional-text')}</label>
<select id="${this.id}-select" disabled>${html_options}</select>
<a href="https://script.google.com/d/${array_objects_scripts[0].id}/edit" target="_blank">open script</a>
`;
this.querySelector('select').addEventListener('change', () => {
this.querySelector('a').href = 'https://script.google.com/d/' + this.querySelector('select').value + '/edit';
});
document.querySelector(`[is="${MainContent.tag}"]`).dispatchEvent(new CustomEvent('ready', {
detail: {
ready: true
}
}));
}
}
Object.assign(SelectScript.prototype, has_call_backend);
customElements.define(SelectScript.tag, SelectScript, {
extends: 'div'
});
class ButtonCompile extends HTMLDivElement {
static get tag() {
return 'button-compile';
}
connectedCallback() {
this.innerHTML = `
<button>compile</button>
<span></span>
`;
this.querySelector('button').addEventListener('click', async () => {
this.setAttribute('disabled', true);
this.querySelector('span').innerHTML = 'compiling...';
if( confirm('are you sure? The process is irreversible') ){
const array_arguments = [
document.querySelector('#origin-select').value,
document.querySelector('#destination-select').value
]
const result = await this.call_backend('compile', array_arguments);
const object_result = JSON.parse(result) ;
const message_result = object_result.error ?
object_result.error.message
:
'successfully compiled'
;
this.querySelector('span').innerHTML = message_result;
}
this.removeAttribute('disabled');
});
}
}
Object.assign(ButtonCompile.prototype, has_call_backend);
customElements.define(ButtonCompile.tag, ButtonCompile, {
extends: 'div'
});
class MainContent extends HTMLDivElement {
static get tag() {
return 'main-content';
}
connectedCallback() {
this.innerHTML = `
<div is="select-script" id="origin" additional-text="Origin project in ES6"></div>
<div is="select-script" id="destination" additional-text="Destination project in ES5"></div>
`;
let counter_ready = 0;
this.addEventListener('ready', () => {
counter_ready++;
if (counter_ready === 2) {
Array.from(document.querySelectorAll(`[is="${SelectScript.tag}"]`)).forEach(element => element.querySelector('select').removeAttribute('disabled'));
this.insertAdjacentHTML('beforeend', `<div is="${ButtonCompile.tag}"></div>`);
}
})
}
}
customElements.define(MainContent.tag, MainContent, {
extends: 'div'
});
</script>
</body>
</html>