-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathfunctions.js
286 lines (241 loc) · 8.49 KB
/
functions.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
//Tables
function addTemplate(id, name, parent, element = false, parentElement = "table", container = "tbody", callback) {
// id = attribute-template
// name = product_attribute
// parent = attribute
let template;
let tbody;
if (element) {
element = element.closest(parentElement);
} else {
element = document;
}
template = document.querySelector(id).cloneNode(true);
template.querySelectorAll('[type="date"]', template).forEach(e => e.setAttribute("value", date()));
template.querySelectorAll('[type="datetime-local"]').forEach(e => e.setAttribute("value", datetime()));
template.querySelectorAll("input,select").forEach(e => e.removeAttribute("disabled"));
template =template.outerHTML;
let newId = Math.floor(Math.random() * 10000);
template = template.replaceAll(name + '[0]', name + '[' + newId + ']').
replaceAll(name + '[#]', name + '[' + newId + ']').
replaceAll(name + '#', name + newId ).
replaceAll("[" + name +"][#]", "[" + name +"][" + newId + "]").
replace('d-none', '').
replace('id="' + id + '"', '');
let row = generateElements(template)[0];
element.querySelector(parent + " " + container).append(row);
if (callback) {
callback(row);
}
return row;
}
function removeRow(element, elementName = false) {
let row = element.closest("tr");
if (row && elementName) {
let form = element.closest("form");
form.append(generateElements('<input type="hidden" name="delete[' + elementName + '][]" value="' + row.dataset.id + '">')[0]);
}
return row.remove();
}
function slugify(str) {
if (str) {
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '') // Remove accents
.replace(/([^\w]+|\s+)/g, '-') // Replace space and other characters by hyphen
.replace(/\-\-+/g, '-') // Replaces multiple hyphens by one hyphen
.replace(/(^-+|-+$)/g, '') // Remove extra hyphens from beginning or end of the string
.toLowerCase();
}
return str;
}
function addTab(element, callback) {
let nav = element.closest(".nav");
let content = element.closest(".row").querySelector(".tab-content");
let navTemplate = nav.querySelector(".tab-nav-template");
let contentTemplate = content.querySelector(".tab-content-template");
let newId = Math.floor(Math.random() * 10000);
navTemplate = navTemplate.cloneNode(true);
navTemplate.classList.remove("tab-nav-template", "d-none");
contentTemplate = contentTemplate.cloneNode(true);
contentTemplate.classList.remove("tab-content-template", "d-none");
navTemplate.querySelector("a").setAttribute("href", "#tab-" + newId);
contentTemplate.setAttribute("id", "tab-" + newId);
contentTemplate = contentTemplate.outerHTML;
contentTemplate = contentTemplate.replaceAll('[0]', '[' + newId + ']').replaceAll('-0-', '-' + newId + '-').replaceAll('disabled', '');
nav.append(navTemplate);
let contentElement = generateElements(contentTemplate)[0];
content.append(contentElement);
if (callback) {
callback(navTemplate, contentElement);
}
document.querySelectorAll("#tab-" + newId +" .product_option_id").forEach(e => {
e.dispatchEvent(new Event("change"));
});
navTemplate.querySelector("a").click();
}
function removeTab(element, elementName) {
let link = element.closest("a");
let nav = element.closest(".nav");
if (elementName) {
let form = element.closest("form");
form.append(generateElements('<input type="hidden" name="delete[' + elementName + '][]" value="' + link.dataset.id + '">')[0]);
}
const bsTab = bootstrap.Tab.getOrCreateInstance(nav.querySelector(".nav-item:not(.d-none) a"));
bsTab.show();
document.querySelector(link.getAttribute("href")).remove();
return link.parentNode.remove();
return false;
}
function datetime() {
let now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
now.setMilliseconds(null)
now.setSeconds(null)
return now.toISOString().slice(0, 19).replace('T', ' ');
}
function date() {
let now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
now.setMilliseconds(null)
now.setSeconds(null)
return now.toISOString().slice(0, 10);
}
function addRow(element) {
// id = attribute-template
// name = product_attribute
// parent = attribute
let table = element.closest("table");
let template = table.querySelector("tr.template").cloneNode(true);
template.classList.remove("template", "d-none");
let newId = Math.floor(Math.random() * 10000);
template.querySelectorAll("input,select", template).forEach(e => e.removeAttribute("disabled"));
template = template.outerHTML;
template = template.replaceAll('[0]', '[' + newId + ']').
replaceAll('[#]', '[' + newId + ']');
table.querySelector("tbody").append(generateElements(template)[0]);
return element;
}
let optionValues = [];
function optionTypeChange(element) {
let tab = element.closest(".tab-pane");
let optionValueId = element.value;
let option = productOption[element.value];
let optionType = option['type'];
document.querySelectorAll("a[href='#" + tab.getAttribute("id") + "'] span").forEach(e => e.textContent = option['name']);
tab.setAttribute("data-type", optionType);
if (optionType == 'radio' || optionType == 'checkbox' || optionType == 'select') {
tab.querySelectorAll(".default").forEach(e => e.classList.add("d-none"));
tab.querySelectorAll(".values").forEach(e => e.classList.remove("d-none"));
} else {
tab.querySelectorAll(".default").forEach(e => e.classList.remove("d-none"));
tab.querySelectorAll(".values").forEach(e => e.classList.add("d-none"));
}
function setValues(values){
let options = "";
for (id in values) {
options += "<option value='"+ id +"'>" + values[id] + "</option>";
}
tab.querySelector(".values select.option_value", tab).innerHTML = options;
}
if (!optionValues[optionValueId]) {
fetch(window.location.pathname + '?module=product/product&action=optionValuesAutocomplete&option_id=' + optionValueId)
.then((response) => {
if (!response.ok) { throw new Error(response) }
return response.json()
})
.then((values) => {
optionValues[optionValueId] = values;
setValues(values);
})
.catch(error => {
console.log(error.statusText);
displayToast("bg-danger", "Error", "Error saving!");
});
} else {
setValues(optionValues[optionValueId]);
}
}
// Date
let datepicker = function (e) {
let element = e.target.closest('input.date, input[type="date"]');
if (element && !element.daterangepicker) {
element.daterangepicker =
new DateRangePicker(element, {
singleDatePicker: true,
autoApply: true,
autoUpdateInput: false,
locale: {
format: 'YYYY-MM-DD'
}
}, function (start, end) {
this.element.value = start.format('YYYY-MM-DD');
});
}
}
document.addEventListener('focusin', datepicker);
// Date range
let daterangepicker = function (e) {
let element = e.target.closest('input.daterange');
if (element && !element.daterangepicker) {
element.daterangepicker =
new DateRangePicker(element, {
singleDatePicker: false,
autoApply: true,
//autoUpdateInput: false,
locale: {
format: 'YYYY-MM-DD'
}
}/*, function (start, end) {
this.element.value = start.format('YYYY-MM-DD') + " - " + end.format('YYYY-MM-DD');
}*/);
}
}
document.addEventListener('focusin', daterangepicker);
// Time
let timepicker = function (e) {
let element = e.target.closest('input.time');
if (element && !element.daterangepicker) {
element.daterangepicker =
new DateRangePicker(element, {
singleDatePicker: true,
datePicker: false,
autoApply: true,
autoUpdateInput: false,
timePicker: true,
timePicker24Hour: true,
locale: {
format: 'HH:mm'
}
}, function (start, end) {
this.element.value = start.format('HH:mm');
});
element.addEventListener('show.daterangepicker', function (ev, picker) {
picker.container.querySelectorAll('.calendar-table').forEach(e => e.style.display = "none");
});
}
}
document.addEventListener('focusin', timepicker);
// Date Time
let datetimepicker = function (e) {
let element = e.target.closest('input.datetime');
if (element && !element.daterangepicker) {
element.daterangepicker =
new DateRangePicker(element, {
singleDatePicker: true,
autoApply: true,
autoUpdateInput: false,
timePicker: true,
timePicker24Hour: true,
locale: {
format: 'YYYY-MM-DD HH:mm'
}
}, function (start, end) {
this.element.value = start.format('YYYY-MM-DD HH:mm');
});
}
}
document.addEventListener('focusin', datetimepicker);
function generateElements(html) {
const template = document.createElement('template');
template.innerHTML = html.trim();
return template.content.children;
}