-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
273 lines (235 loc) · 6.75 KB
/
script.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"use strict";
let greetAdd;
function Greet(index, data) {
this.index = index;
this.data = data ? data : {};
}
Greet.prototype.createRow = function() {
let tag_input_group = Tag('input', {
type: "text",
value: this.data.Group.Name
});
if (this.data.Group.Disambiguation) {
tag_input_group.value += " / " + this.data.Group.Disambiguation;
}
tag_input_group.disabled = true;
let tag_input_note = Tag('input', {
type: "text",
value: this.data.Note
});
tag_input_note.disabled = true;
let tag_button_delete = Tag('button', null, "Delete");
tag_button_delete.onclick = () => {
if (confirm("Are you sure you want to delete this greet?")) { // TODO show greet info
this.delete();
}
};
return Tag('tr', {class: "greet"}, null, [
Tag('td', null, null, [
Text("Group: "), tag_input_group,
Text(" Note: "), tag_input_note,
Text(" "), tag_button_delete
])
]);
};
Greet.prototype.delete = function() {
let xhr = sendRequest("DELETE", "/v1/greets/" + this.data.ID, null, null,
(response, status) => {
console.log(status, response);
loadProd();
},
(response, status) => {
console.log(status, response)
document.getElementById("greets_list").innerHTML = "ERROR:" + response;
}
)
}
function loadProd(args) {
let pid = parseInt(document.getElementById("in_prod_id").value);
if (isNaN(pid)) {
return;
}
let prod_container = document.getElementById("prod_container");
prod_container.style.display = "none";
let prod = document.getElementById("prod");
prod.innerHTML = "";
let greets_list = document.getElementById("greets_list");
greets_list.innerHTML = "";
let xhr = sendRequest("GET", "/v1/prods/" + pid, null, null,
(response) => {
let json = JSON.parse(response);
let html = "<strong>" + json.Name + "</strong> by ";
for (let gi in json.Groups) {
let g = json.Groups[gi];
if (gi > 0) {
html += " and ";
}
html += '<a href="https://www.pouet.net/groups.php?which=' + g.ID + '">' + g.Name + '</a>';
}
html += " <br />";
prod.innerHTML = html;
if (json.Screenshot) {
prod.appendChild(Tag('img', {src: json.Screenshot}));
prod.appendChild(Tag('br'));
}
prod.appendChild(Tag('a', {href: "https://www.pouet.net/prod.php?which=" + json.ID}, '[pouet.net]'));
prod.appendChild(Text(" "));
if (json.Demozoo > 0) {
prod.appendChild(Tag('a', {href: "https://demozoo.org/productions/" + json.Demozoo}, '[demozoo]'));
}
prod.appendChild(Text(" "));
prod.appendChild(Tag('a', {href: "https://www.pouet.net/prod_nfo.php?which=" + json.ID}, '[nfo]'));
if (json.Video) {
prod.appendChild(Text(" "));
prod.appendChild(Tag('a', {href: json.Video}, '[video]'));
}
for (let i in json.Greets) {
let greet = new Greet(i, json.Greets[i]);
greets_list.appendChild(greet.createRow());
}
prod_container.style.display = "block";
if (args && args.clear_greets) {
greetAdd.clear();
}
},
(response) => {
document.getElementById("prod").innerHTML = "ERROR:" + response;
}
)
}
window.onload = function() {
greetAdd = function () {
let self = Object();
let greet_add_group = $('greet_add_group');
let greet_add_note = $('greet_add_note');
let greet_add_button = $('greet_add_button');
let greet_add_group_id = null;
self.clear = () => {
greet_add_group.value = "";
greet_add_note.value = "";
greet_add_button.disabled = true;
greet_add_group_id = null;
}
self.clear();
self.add = () => {
let pid = parseInt($("in_prod_id").value);
if (isNaN(pid)) {
return;
}
if (!greet_add_group_id || isNaN(greet_add_group_id)) {
return;
}
let note = greet_add_note.value;
let body = {
ProdId: pid,
GroupId: greet_add_group_id,
Note: note
};
let xhr = sendRequest("POST", "/v1/greets/", null, body,
(response, status) => {
console.log(status, response);
self.clear();
loadProd();
},
(response, status) => {
console.log(status, response)
alert("ERROR:" + response);
}
)
}
greet_add_button.onclick = self.add;
self.setGroupNameAndId = (name, gid) => {
greet_add_group.value = name;
greet_add_group_id = gid;
greet_add_button.disabled = false;
}
return self;
}();
$("in_prod_id").onkeyup = (e) => {
if (e.keyCode === KEY_ENTER || e.key === "Enter") {
loadProd({clear_greets: true});
}
};
let prod_autocomplete = new Autocomplete({
input: $('in_prod'),
funcSelect: (item) => {
console.log("Selected", item);
$('in_prod').value = item.name;
$('in_prod_id').value = item.id;
loadProd({clear_greets: true});
},
funcFindVariants: (value, found, error) => {
let query = { name: value };
let xhr = sendRequest("GET", "/v1/prods/search", query, null,
(response) => {
let json = response ? JSON.parse(response) : null;
if (!json) {
found([]);
return;
}
let suggestions = [];
json.forEach((obj) => {
let element = Tag('div', {class: "item-inactive"}, null, [
Text(obj.Name + (obj.Disambiguation ? " (" + obj.Disambiguation + ")" : "")),
Text(" by "),
(obj.Groups && obj.Groups.length > 0) ? Tag("span", {class: "group"}, obj.Groups[0].Name) : Text("N/A")]);
suggestions.push({
id: obj.ID,
name: obj.Name,
element: element,
deactivate: () => { element.setAttribute("class", "item-inactive"); },
activate: () => { element.setAttribute("class", "item-active"); },
});
});
found(suggestions);
},
(response, status) => {
error({http_status: status, reponse: response});
}
)
}
});
let group_autocomplete = new Autocomplete({
input: $('greet_add_group'),
funcSelect: (item) => {
greetAdd.setGroupNameAndId(item.name, item.id);
},
funcFindVariants: (value, found, error) => {
let query = { name: value };
let xhr = sendRequest("GET", "/v1/groups/search", query, null,
(response) => {
let json = response ? JSON.parse(response) : null;
if (!json) {
found([]);
return;
}
let suggestions = [];
json.forEach((obj) => {
let desc = [Tag("strong", null, obj.Name)];
let name = obj.Name;
if (obj.Disambiguation) {
name += " / " + obj.Disambiguation;
desc.push(
Tag("span", {class: "group"}, null, [
Text(" / " + obj.Disambiguation)
])
);
}
let element = Tag('div', {class: "item-inactive"}, null, desc);
suggestions.push({
id: obj.ID,
name: name,
element: element,
deactivate: () => { element.setAttribute("class", "item-inactive"); },
activate: () => { element.setAttribute("class", "item-active"); },
});
});
found(suggestions);
},
(response, status) => {
error({http_status: status, reponse: response});
}
)
}
});
}