-
Notifications
You must be signed in to change notification settings - Fork 0
/
vineyard-edit.js
287 lines (263 loc) · 8.58 KB
/
vineyard-edit.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
274
275
276
277
278
279
280
281
282
283
284
285
286
"use strict";
var g_VineyardEdit = (function() {
function Background() {
return document.getElementById("vineyard_edit_background");
}
function Container() {
return document.getElementById("vineyard_edit_container");
}
function Button_close() {
return document.getElementById("vineyard_edit_button_close");
}
function Button_edit() {
return document.getElementById("vineyard_edit_button_edit");
}
function Title() {
return document.getElementById("vineyard_edit_title");
}
function Name() {
return document.getElementById("vineyard_edit_name");
}
function Country() {
return document.getElementById("vineyard_edit_country");
}
function Region() {
return document.getElementById("vineyard_edit_region");
}
function Address() {
return document.getElementById("vineyard_edit_address");
}
function Maps() {
return document.getElementById("vineyard_edit_maps").firstChild;
}
function Website() {
return document.getElementById("vineyard_edit_website");
}
function Comment() {
return document.getElementById("vineyard_edit_comment");
}
function TotalCount() {
return document.getElementById("vineyard_edit_total_count");
}
function TotalPrice() {
return document.getElementById("vineyard_edit_total_price");
}
function set_text(node, text) {
node.replaceChild(document.createTextNode(text), node.firstChild);
}
function set_button_edit() {
var button = Button_edit();
set_text(button, "Bearbeiten");
button.onclick = Edit;
}
function set_button_save() {
var button = Button_edit();
set_text(button, "Speichern");
button.onclick = Save;
}
function Hide(event) {
event.stopPropagation();
Background().style.display = "none";
}
function CountryListCallback() {
var country_list = document.getElementById("country_list");
PopulateDataList(country_list, GetResponse(this));
}
function PopulateRegions() {
var country = document.getElementById("vineyard_edit_countryinput").value;
if (country === "") return;
SendGet("regions", PopulateRegions_Callback, {country});
}
function PopulateRegions_Callback() {
var region_list = document.getElementById("region_list");
PopulateDataList(region_list, GetResponse(this));
}
function Edit(event) {
event.stopPropagation();
set_button_save();
ReplaceTextWithInput(Name());
var country_input = ReplaceTextWithInput(Country());
country_input.id = "vineyard_edit_countryinput";
country_input.setAttribute("list", "country_list");
country_input.onchange = PopulateRegions;
var region_input = ReplaceTextWithInput(Region());
region_input.setAttribute("list", "region_list")
ReplaceTextWithInput(Website());
ReplaceTextWithInput(Address());
var comment_td = Comment();
var comment_content = comment_td.innerHTML.replace("<br>", "\n");
comment_td.innerHTML = "<textarea>" + comment_content + "</textarea>";
SendGet("countries", CountryListCallback);
PopulateRegions();
}
function PrependHttp(website) {
if (website.startsWith("http")) return website;
return "http://" + website;
}
function Save(event) {
event.stopPropagation();
set_button_edit();
var vineyard_id = Background().vineyard_id;
var name = ReplaceInputWithText(Name());
set_text(Title(), "Weingut " + name);
var country = ReplaceInputWithText(Country());
var region = ReplaceInputWithText(Region());
var td_website = Website();
var input_website = td_website.firstChild;
var website = input_website.value;
var link = document.createElement("a");
link.appendChild(document.createTextNode(website));
link.href = PrependHttp(website);
td_website.replaceChild(link, input_website);
var address = ReplaceInputWithText(Address());
var comment_td = Comment();
var comment = comment_td.firstChild.innerHTML.replace("\n", "<br>");
comment_td.innerHTML = comment;
SendPost("set_vineyard", UpdateVineyard,
{vineyard_id, name, country, region, address, website, comment})
}
return {
Init: function() {
Background().onclick = Hide;
Button_close().onclick = Hide;
Container().onclick = function(event) {
event.stopPropagation();
}
},
Populate: function(data) {
var box = Background();
box.style.display = "block";
box.vineyard_id = data.id;
set_button_edit();
set_text(Title(), "Weingut " + data.vineyard);
set_text(Name(), data.vineyard);
set_text(Country(), data.country);
set_text(Region(), data.region);
set_text(Address(), data.address);
var link = Website();
link.href = PrependHttp(data.website);
set_text(link, data.website);
Comment().innerHTML = data.comment.replace("\n", "<br>");
var mapslink = Maps();
mapslink.href = "https://www.google.de/maps/search/Weingut+" +
encodeURIComponent(data.vineyard);
set_text(TotalCount(), data.total_count);
set_text(TotalPrice(), FormatPrice(data.total_price));
},
};
})();
var g_WineEdit = (function() {
var sender_ = null;
function Background() {
return document.getElementById("wine_edit_background");
}
function Container() {
return document.getElementById("wine_edit_container");
}
function Button_close() {
return document.getElementById("wine_edit_button_close");
}
function Button_edit() {
return document.getElementById("wine_edit_button_edit");
}
function Title() {
return document.getElementById("wine_edit_title");
}
function Name() {
return document.getElementById("wine_edit_name");
}
function Vineyard() {
return document.getElementById("wine_edit_vineyard");
}
function Grape() {
return document.getElementById("wine_edit_grape");
}
function Comment() {
return document.getElementById("wine_edit_comment");
}
function set_text(node, text) {
node.replaceChild(document.createTextNode(text), node.firstChild);
}
function set_button_edit() {
var button = Button_edit();
set_text(button, "Bearbeiten");
button.onclick = Edit;
}
function set_button_save() {
var button = Button_edit();
set_text(button, "Speichern");
button.onclick = Save;
}
function Hide(event) {
event.stopPropagation();
Background().style.display = "none";
}
function GrapeListCallback() {
var grape_list = document.getElementById("grape_list");
PopulateDataList(grape_list, GetResponse(this));
}
function Edit(event) {
event.stopPropagation();
set_button_save();
ReplaceTextWithInput(Name());
var grape_input = ReplaceTextWithInput(Grape());
grape_input.id = "wine_edit_countryinput";
grape_input.setAttribute("list", "grape_list");
var comment_td = Comment();
var textarea = document.createElement("textarea");
textarea.value = comment_td.innerHTML.replace("<br>", "\n");
DropAllChildren(comment_td);
comment_td.appendChild(textarea);
SendGet("grapes", GrapeListCallback);
}
function Save(event) {
event.stopPropagation();
set_button_edit();
var wine_id = Background().wine_id;
var name = ReplaceInputWithText(Name());
var grape = ReplaceInputWithText(Grape());
var comment_td = Comment();
var comment = comment_td.firstChild.value.replace("\n", "<br>");
comment_td.innerHTML = comment;
SendPost("set_wine", UpdateWine, {wine_id, name, grape, comment});
sender_.replaceChild(document.createTextNode(name), sender_.firstChild);
}
return {
Init: function() {
Background().onclick = Hide;
Button_close().onclick = Hide;
Container().onclick = function(event) {
event.stopPropagation();
}
},
Populate: function(data) {
var box = Background();
box.style.display = "block";
box.wine_id = data.id;
set_button_edit();
set_text(Title(), data.wine);
set_text(Name(), data.wine);
set_text(Vineyard(), data.vineyard);
set_text(Grape(), data.grape);
Comment().innerHTML = data.comment.replace("\n", "<br>");
},
SetSender: function(sender) { sender_ = sender; },
};
})();
function ShowVineyardEdit(event) {
var vineyard = event.target.vineyard_id;
SendGet("vineyard_data", ShowVineyardEdit_Callback, {vineyard})
}
function ShowVineyardEdit_Callback() {
var data = GetResponse(this);
g_VineyardEdit.Populate(data);
}
function ShowWineEdit(event) {
var wine = event.target.wine_id;
SendGet("wine_data", ShowWineEdit_Callback, {wine});
g_WineEdit.SetSender(event.target);
}
function ShowWineEdit_Callback() {
var data = GetResponse(this);
g_WineEdit.Populate(data);
}