Subversion Repositories cheapmusic

Rev

Rev 128 | Rev 134 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
120 - 1
var myTimer;
2
 
15 - 3
$(document).ready(function() {
107 - 4
	$(window).on('beforeunload', function() {
5
		$(":submit").attr('disabled', 'disabled');
6
		document.body.style.cursor = "progress";
7
	});
52 - 8
 
120 - 9
	$(window).bind('resize orientationchange', function() { paginationSetup(); });
116 - 10
	paginationSetup();
81 - 11
 
120 - 12
	if (document.getElementById("detailFilterForm")) {
107 - 13
		tableFilterButtons();
14
		detailTableFilterButtons();
15
	}
16
 
120 - 17
	if (document.getElementById("barcodeForm")) {
113 - 18
		barcodeFormButtons();
19
	}
20
 
116 - 21
	flexdatalistSetup();
22
 
23
	tooltipSetup();
120 - 24
 
25
	if (typeof addDiscogsEvents === "function") {
26
	    addDiscogsEvents();
27
	}
6 - 28
});
11 - 29
 
107 - 30
 
15 - 31
function tableFilterButtons() {
124 - 32
	$(".filterButton").click(function() {
107 - 33
		$.post("filterTable.php", {
34
				submit: $(this).attr("value"),
120 - 35
				sessionTab: document.getElementById("sessionTab").value,
36
				nonce: document.getElementById("nonce").value
107 - 37
			},
38
			function(data, status) {
39
				if (status == "success") {
40
					document.getElementById("productTable").innerHTML = data;
41
					tableFilterButtons();
42
					detailTableFilterButtons();
43
				}
44
			}
45
		);
46
	});
15 - 47
}
48
 
66 - 49
function detailTableFilterButtons() {
107 - 50
	$("#detailFilterForm").on('submit', function(e) {
51
		e.preventDefault();
52
		return false;
53
	});
54
 
55
	$("#detailTab").click(function() {
116 - 56
		x = document.getElementById("detailFilter");
57
		if (x.style.display == "block") {
58
			document.getElementById("detailTabArrow").innerHTML = '<i class="material-icons material-text">expand_more</i>';
59
			x.style.display = "none";
60
		} else {
61
			document.getElementById("detailTabArrow").innerHTML = '<i class="material-icons material-text">expand_less</i>';
62
			x.style.display = "block";
63
		}
64
		return false;
107 - 65
	});
116 - 66
 
107 - 67
	$(".detailFilterButton").click(function() {
68
		var formData = $("#detailFilterForm").serializeArray();
69
		formData.push({
70
			name: this.name,
121 - 71
			value: this.value,
107 - 72
		});
121 - 73
		formData.push({
74
    		name: "nonce",
75
			value: document.getElementById("nonce").value
76
		});
107 - 77
		$.post("filterTable.php",
78
			formData,
79
			function(data, status) {
80
				if (status == "success") {
81
					document.getElementById("productTable").innerHTML = data;
82
					detailTableFilterButtons();
83
					tableFilterButtons();
84
				}
85
			}
86
		);
87
		return false;
88
	});
66 - 89
}
90
 
113 - 91
function barcodeFormButtons() {
92
	$("#barcodeForm").on('submit', function(e) {
107 - 93
		e.preventDefault();
113 - 94
		return false;
107 - 95
	});
96
 
113 - 97
	$(".barcodeButton").click(function() {
98
		var formData = $("#barcodeForm").serializeArray();
99
		formData.push({
100
			name: this.name,
101
			value: this.value
102
		});
121 - 103
		formData.push({
104
    		name: "nonce",
105
			value: document.getElementById("nonce").value
106
		});
113 - 107
		$.post("barcode.php",
108
			formData,
109
			function(data, status) {
110
				if (status == "success") {
111
					document.getElementById("barcodeResult").innerHTML = data;
112
				}
113
			}
114
		);
121 - 115
 
116
        new MutationObserver(function() {
117
        	$("#barcodeSearchForm").off("submit");
118
      	    if (document.getElementById("barcodeSearchTerm") && document.getElementById("barcodeSearchTerm").value !== "") {
119
            	$("#barcodeSearchForm").submit(function() {
120
                    progressBar("Searching for:<br><br><strong>" + document.getElementById("barcodeSearchTerm").value + "</strong>");
121
                });
122
    	    }
123
    	}).observe(document.getElementById("barcodeResult"), { childList: true, subtree: true });
124
 
113 - 125
		return false;
126
	});
66 - 127
}
116 - 128
 
64 - 129
function alertAutoClose() {
107 - 130
	window.setTimeout(function() {
131
		$(".alert").fadeTo(1000, 0).slideUp(1000, function() {
132
			$(this).remove();
133
		});
134
	}, 3000);
64 - 135
}
136
 
15 - 137
function initProgressBarModal(title) {
107 - 138
	var elem = document.getElementById("progressBar");
139
	elem.style.width = '0%';
140
	elem.innerHTML = '0%';
15 - 141
 
107 - 142
	elem = document.getElementById("progressBarHeader");
143
	elem.innerHTML = title;
15 - 144
 
107 - 145
	elem = document.getElementById("progressBarMessage");
146
	elem.innerHTML = "";
84 - 147
 
107 - 148
	$("#progressBarDiv").modal({
149
		backdrop: "static"
150
	});
15 - 151
}
152
 
153
function updateProgressBar(width, message) {
107 - 154
	var elem = document.getElementById("progressBar");
155
	if (elem && width) {
156
		elem.style.width = width + '%';
157
		elem.innerHTML = width.toFixed(0) + '%';
158
		// bugbug document.getElementById("progressBarMessage").innerHTML = message;
159
	}
15 - 160
}
161
 
162
function endProgressBarModal() {
107 - 163
	$("#progressBarDiv").modal("hide");
15 - 164
}
165
 
166
// Refresh the progress bar.
167
function refreshProgress() {
107 - 168
	$.ajax({
122 - 169
		url: "pbChecker.php?file=" + document.getElementById("sessionId").value + "_" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value,
107 - 170
		success: function(data) {
171
			updateProgressBar(data.percent, data.message);
172
			if (data.percent >= 100) {
120 - 173
				window.clearInterval(myTimer);
174
				myTimer = window.setInterval(function() { completeProgress(); }, 1000);
107 - 175
			}
176
		}
177
	});
15 - 178
}
179
 
180
// End the progress bar.
181
function completeProgress() {
107 - 182
	updateProgressBar(100, "Completed");
120 - 183
	window.clearInterval(myTimer);
107 - 184
	endProgressBarModal();
15 - 185
}
186
 
187
// Start the progress bar.
188
function progressBar(title) {
107 - 189
	initProgressBarModal(title);
121 - 190
	myTimer = window.setInterval(function() { refreshProgress(); }, 500);
28 - 191
}
192
 
52 - 193
function saveTransfer(url) {
107 - 194
	var xhttp = new XMLHttpRequest();
195
	xhttp.open("POST", "savetransfer.php", true);
196
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 197
	xhttp.send("target=" + btoa(url) + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
43 - 198
}
199
 
52 - 200
function addWishlist(id, field, cnt, wl) {
107 - 201
	$(field).tooltip('hide');
202
	$(field).remove();
203
	var xhttp = new XMLHttpRequest();
204
	xhttp.onreadystatechange = function() {
205
		if (this.readyState == 4) {
206
			json = JSON.parse(this.responseText);
207
			switch (json.retval) {
208
				case 0:
209
					msg = "Added to wishlist.";
210
					alertTxt = "alert-success";
211
					break;
212
				case 1:
213
					msg = "Already on the wishlist.";
214
					alertTxt = "alert-warning";
215
					break;
216
				default:
217
					msg = "ERROR! Could not add to the wishlist.";
218
					alertTxt = "alert-danger";
219
					break;
220
			}
221
			document.getElementById("wishlistAdd" + cnt).innerHTML = "<div class=\"alert " + alertTxt + " alert-dismissible mt-2 mb-0\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" + msg + "</div>";
222
			alertAutoClose();
223
		}
224
	};
225
	xhttp.open("POST", "wishlistDB.php", true);
226
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 227
	xhttp.send("id=" + id + "&function=add&wl=" + wl + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
52 - 228
}
229
 
230
function deleteWishlist(id, field, title, artist) {
107 - 231
	$(field).tooltip('hide');
232
	if (confirm('Remove ' + title + ' by ' + artist + ' from wishlist?')) {
233
		var xhttp = new XMLHttpRequest();
234
		xhttp.open("POST", "wishlistDB.php", true);
235
		xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 236
		xhttp.send("function=delete&id=" + id + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
130 - 237
		field.closest("tr").remove();
107 - 238
	}
52 - 239
}
240
 
241
function editWishlist(id, field) {
107 - 242
	if (field !== null) {
243
		$(field).tooltip('hide');
130 - 244
		var parent = field.closest("tr");
52 - 245
 
107 - 246
		var artist = mysqliHtmlDecode(parent.getElementsByTagName("td")[1].innerHTML);
247
		var title = mysqliHtmlDecode(parent.getElementsByTagName("td")[2].innerHTML);
248
		var barcode = parent.getElementsByTagName("td")[3].innerHTML.replace(/-/g, '');
249
		var cond = parent.getElementsByTagName("td")[5].innerHTML;
250
		var format = parent.getElementsByTagName("td")[6].innerHTML;
251
		var price = parent.getElementsByTagName("td")[7].innerHTML;
52 - 252
 
107 - 253
		document.getElementById("wlMsg").innerHTML = "";
254
		document.getElementById("wlId").value = id;
255
		document.getElementById("wlArtist").value = artist;
256
		document.getElementById("wlTitle").value = title;
257
		document.getElementById("wlBarcode").value = barcode;
258
		document.getElementById("wlCond").value = cond;
259
		document.getElementById("wlFormat").value = format;
260
		document.getElementById("wlPrice").value = price;
261
	}
52 - 262
 
116 - 263
	$("#editWishlistModal").modal({
264
		backdrop: 'static',
265
		keyboard: false
266
	});
52 - 267
}
268
 
269
function saveEditedWishlist() {
107 - 270
	var id = document.getElementById("wlId").value;
271
	var field = document.getElementById("wlIdRow" + id);
130 - 272
	var parent = field.closest("tr");
107 - 273
	var artist = document.getElementById("wlArtist").value;
274
	var title = document.getElementById("wlTitle").value;
275
	var barcode = document.getElementById("wlBarcode").value;
276
	var cond = document.getElementById("wlCond").value;
277
	var format = document.getElementById("wlFormat").value;
278
	var price = document.getElementById("wlPrice").value;
52 - 279
 
107 - 280
	var xhttp = new XMLHttpRequest();
281
	xhttp.onreadystatechange = function() {
282
		if (this.readyState == 4) {
283
			json = JSON.parse(this.responseText);
284
			switch (json.retval) {
285
				case 0:
286
					parent.getElementsByTagName("td")[1].innerHTML = artist;
287
					parent.getElementsByTagName("td")[2].innerHTML = title;
288
					parent.getElementsByTagName("td")[3].innerHTML = barcode;
289
					parent.getElementsByTagName("td")[4].innerHTML = barcode; // bugbug Format
290
					parent.getElementsByTagName("td")[5].innerHTML = cond;
291
					parent.getElementsByTagName("td")[6].innerHTML = format;
292
					parent.getElementsByTagName("td")[7].innerHTML = price;
293
					parent.getElementsByTagName("td")[8].innerHTML = '$' + Number(price).toFixed(2); // bugbug Format
294
					$("#editWishlistModal").modal('hide');
295
					break;
296
				case 1:
297
					document.getElementById("wlMsg").innerHTML = "<div class=\"alert alert-danger alert-dismissible mt-0 mb-0\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" + json.msg + "</div>";
298
					editWishlist(id, null);
299
					break;
300
				default:
301
					document.getElementById("wlMsg").innerHTML = "<div class=\"alert alert-danger alert-dismissible mt-0 mb-0\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>Unknown Error. Please reload page.</div>";
302
					break;
303
			}
304
		}
305
	};
306
	xhttp.open("POST", "wishlistDB.php", true);
307
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 308
	xhttp.send("function=update&id=" + id + "&artist=" + encodeURIComponent(artist) + "&title=" + encodeURIComponent(title) + "&barcode=" + encodeURIComponent(barcode) + "&cond=" + encodeURIComponent(cond) + "&format=" + encodeURIComponent(format) + "&price=" + encodeURIComponent(price) + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
52 - 309
}
310
 
116 - 311
// Flex Data List
312
function flexdatalistSetup() {
118 - 313
	if ($.fn.flexdatalist && $.fn.clearer) {
116 - 314
		$('.flexdatalist').flexdatalist();
315
		$(function() {
316
			$("input[id='searchTerm-flexdatalist']").clearer();
317
		});
318
		$('.flexdatalist').flexdatalist({
319
			minLength: 0,
320
			searchContain: true,
321
			noResultsText: ""
322
		}); /* reset after running clearer */
323
	} else {
120 - 324
		setTimeout(function () { flexdatalistSetup(); }, 100);
116 - 325
	}
326
}
327
 
43 - 328
// Pagination
65 - 329
function paginationSetup() {
120 - 330
	if ($.fn.paginate && $.fn.modal && $.fn.tooltip && typeof Popper !== "undefined") {
116 - 331
		if (document.getElementById("discogsDeck") !== null) {
332
			$("[id^=masterModal]").modal("hide");
333
			$('#discogsDeck').paginate({
334
				paginateElement: '.card',
335
				elementsPerPage: paginationPerPage(),
336
				effect: 'default',
337
				firstButtonText: '<i class="material-icons material-text">first_page</i>',
338
				lastButtonText: '<i class="material-icons material-text">last_page</i>',
339
				prevButtonText: '<i class="material-icons material-text">navigate_before</i>',
340
				nextButtonText: '<i class="material-icons material-text">navigate_next</i>',
124 - 341
				extraButtonClasses: 'btn bg-info px-2 shadow-sm',
120 - 342
				functionOnNav: function () { tooltipSetup(); if (typeof addDiscogsEvents === "function") { addDiscogsEvents();}  }
116 - 343
			});
344
            tooltipSetup();
120 - 345
            if (typeof addDiscogsEvents === "function") {
346
                addDiscogsEvents();
347
            }
116 - 348
		}
349
	} else {
120 - 350
        setTimeout(function () { paginationSetup(); }, 100);
116 - 351
	}
352
}
353
 
354
// Tooltip Setup
355
function tooltipSetup() {
356
	if ($.fn.tooltip && typeof Popper != 'undefined') {
107 - 357
		$('[data-toggle="tooltip"]').tooltip({
358
			trigger: 'hover'
359
		});
116 - 360
 
107 - 361
		$('[data-toggle2="tooltip"]').tooltip({
362
			trigger: 'hover'
363
		});
116 - 364
	} else {
120 - 365
        setTimeout(function () { tooltipSetup(); }, 100);
107 - 366
	}
43 - 367
}
368
 
52 - 369
function filterWishlist() {
107 - 370
	var input, filter, table, tr, td, i, j, txtValue, flag;
371
	input = document.getElementById("tableFilter");
372
	filter = input.value.toUpperCase();
373
	table = document.getElementById("wishlistTable");
374
	tr = table.getElementsByTagName("tr");
375
	document.getElementById("tableFilterButton").style.display = (filter.length > 0 ? "" : "none");
52 - 376
 
107 - 377
	// Loop through all table rows, excluding the header, and hide those who don't match the search query
378
	for (i = 1; i < tr.length; i++) {
379
		td = tr[i].getElementsByTagName("td");
380
		flag = "none";
52 - 381
 
107 - 382
		for (j = 0; j < td.length; j++) {
383
			if (td[j]) {
384
				txtValue = td[j].textContent || td[j].innerText;
385
				if (txtValue.toUpperCase().indexOf(filter) > -1) {
386
					flag = "";
387
					break;
388
				}
389
			}
390
		}
52 - 391
 
107 - 392
		tr[i].style.display = flag;
393
	}
52 - 394
}
395
 
396
function mysqliHtmlDecode(str) {
107 - 397
	str = str.replace(/&amp;/g, '&');
398
	str = str.replace(/&lt;/g, '<');
399
	str = str.replace(/&gt;/g, '>');
400
	return str;
52 - 401
}
402
 
46 - 403
// number of discogs entries per page according to screen size
43 - 404
function paginationPerPage() {
107 - 405
	var width = $(window).width();
406
	var num = Math.floor(width / 210);
407
	if (num > 9) {
408
		num = 9;
409
	} else if (width < 576) {
410
		num = 1;
411
	}
412
	return num;
43 - 413
}
46 - 414
 
415
// sort table by column
416
function sortTable(table, col, colType) {
107 - 417
	var rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
418
	table = document.getElementById(table);
419
	switching = true;
420
	// Set the sorting direction to ascending:
421
	dir = "asc";
422
	/* Make a loop that will continue until no switching has been done: */
423
	while (switching) {
424
		// Start by saying: no switching is done:
425
		switching = false;
426
		rows = table.rows;
427
		/* Loop through all table rows (except the first, which contains table headers): */
428
		for (i = 1; i < (rows.length - 1); i++) {
429
			// Start by saying there should be no switching:
430
			shouldSwitch = false;
431
			/* Get the two elements you want to compare, one from current row and one from the next: */
432
			x = rows[i].getElementsByTagName("TD")[col];
433
			y = rows[i + 1].getElementsByTagName("TD")[col];
434
			/* Check if the two rows should switch place, based on the direction, asc or desc: */
435
			if (dir == "asc") {
436
				if (colType == 'numeric') {
120 - 437
					if (Number(x.innerHTML) > Number(y.innerHTML)) {
107 - 438
						// If so, mark as a switch and break the loop:
439
						shouldSwitch = true;
440
						break;
441
					}
442
				} else /* if (colType == 'text') */ {
443
					if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
444
						// If so, mark as a switch and break the loop:
445
						shouldSwitch = true;
446
						break;
447
					}
448
				}
449
			} else if (dir == "desc") {
450
				if (colType == 'numeric') {
451
					if (Number(x.innerHTML) < Number(y.innerHTML)) {
452
						// If so, mark as a switch and break the loop:
453
						shouldSwitch = true;
454
						break;
455
					}
456
				} else /* if (colType == 'text') */ {
457
					if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
458
						// If so, mark as a switch and break the loop:
459
						shouldSwitch = true;
460
						break;
461
					}
462
				}
463
			}
464
		}
465
		if (shouldSwitch) {
466
			/* If a switch has been marked, make the switch and mark that a switch has been done: */
467
			rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
468
			switching = true;
469
			// Each time a switch is done, increase this count by 1:
470
			switchcount++;
471
		} else {
120 - 472
			/* If no switching has been done AND the direction is "asc", set the direction to "desc" and run the whole loop again. */
107 - 473
			if (switchcount === 0 && dir == "asc") {
474
				dir = "desc";
475
				switching = true;
476
			}
477
		}
478
	}
66 - 479
}
81 - 480
 
481
function topFunction() {
107 - 482
	document.body.scrollTop = document.documentElement.scrollTop = 0;
124 - 483
}