Subversion Repositories cheapmusic

Rev

Rev 124 | Rev 130 | 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 parent = field.parentNode.parentNode.parentNode;
234
		var xhttp = new XMLHttpRequest();
235
		xhttp.open("POST", "wishlistDB.php", true);
236
		xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 237
		xhttp.send("function=delete&id=" + id + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
107 - 238
		parent.parentNode.removeChild(parent);
239
	}
52 - 240
}
241
 
242
function editWishlist(id, field) {
107 - 243
	if (field !== null) {
244
		$(field).tooltip('hide');
245
		var parent = field.parentNode.parentNode.parentNode;
52 - 246
 
107 - 247
		var artist = mysqliHtmlDecode(parent.getElementsByTagName("td")[1].innerHTML);
248
		var title = mysqliHtmlDecode(parent.getElementsByTagName("td")[2].innerHTML);
249
		var barcode = parent.getElementsByTagName("td")[3].innerHTML.replace(/-/g, '');
250
		var cond = parent.getElementsByTagName("td")[5].innerHTML;
251
		var format = parent.getElementsByTagName("td")[6].innerHTML;
252
		var price = parent.getElementsByTagName("td")[7].innerHTML;
52 - 253
 
107 - 254
		document.getElementById("wlMsg").innerHTML = "";
255
		document.getElementById("wlId").value = id;
256
		document.getElementById("wlArtist").value = artist;
257
		document.getElementById("wlTitle").value = title;
258
		document.getElementById("wlBarcode").value = barcode;
259
		document.getElementById("wlCond").value = cond;
260
		document.getElementById("wlFormat").value = format;
261
		document.getElementById("wlPrice").value = price;
262
	}
52 - 263
 
116 - 264
	$("#editWishlistModal").modal({
265
		backdrop: 'static',
266
		keyboard: false
267
	});
52 - 268
}
269
 
270
function saveEditedWishlist() {
107 - 271
	var id = document.getElementById("wlId").value;
272
	var field = document.getElementById("wlIdRow" + id);
273
	var parent = field.parentNode;
274
	var artist = document.getElementById("wlArtist").value;
275
	var title = document.getElementById("wlTitle").value;
276
	var barcode = document.getElementById("wlBarcode").value;
277
	var cond = document.getElementById("wlCond").value;
278
	var format = document.getElementById("wlFormat").value;
279
	var price = document.getElementById("wlPrice").value;
52 - 280
 
107 - 281
	var xhttp = new XMLHttpRequest();
282
	xhttp.onreadystatechange = function() {
283
		if (this.readyState == 4) {
284
			json = JSON.parse(this.responseText);
285
			switch (json.retval) {
286
				case 0:
287
					parent.getElementsByTagName("td")[1].innerHTML = artist;
288
					parent.getElementsByTagName("td")[2].innerHTML = title;
289
					parent.getElementsByTagName("td")[3].innerHTML = barcode;
290
					parent.getElementsByTagName("td")[4].innerHTML = barcode; // bugbug Format
291
					parent.getElementsByTagName("td")[5].innerHTML = cond;
292
					parent.getElementsByTagName("td")[6].innerHTML = format;
293
					parent.getElementsByTagName("td")[7].innerHTML = price;
294
					parent.getElementsByTagName("td")[8].innerHTML = '$' + Number(price).toFixed(2); // bugbug Format
295
					$("#editWishlistModal").modal('hide');
296
					break;
297
				case 1:
298
					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>";
299
					editWishlist(id, null);
300
					break;
301
				default:
302
					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>";
303
					break;
304
			}
305
		}
306
	};
307
	xhttp.open("POST", "wishlistDB.php", true);
308
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 309
	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 - 310
}
311
 
116 - 312
// Flex Data List
313
function flexdatalistSetup() {
118 - 314
	if ($.fn.flexdatalist && $.fn.clearer) {
116 - 315
		$('.flexdatalist').flexdatalist();
316
		$(function() {
317
			$("input[id='searchTerm-flexdatalist']").clearer();
318
		});
319
		$('.flexdatalist').flexdatalist({
320
			minLength: 0,
321
			searchContain: true,
322
			noResultsText: ""
323
		}); /* reset after running clearer */
324
	} else {
120 - 325
		setTimeout(function () { flexdatalistSetup(); }, 100);
116 - 326
	}
327
}
328
 
43 - 329
// Pagination
65 - 330
function paginationSetup() {
120 - 331
	if ($.fn.paginate && $.fn.modal && $.fn.tooltip && typeof Popper !== "undefined") {
116 - 332
		if (document.getElementById("discogsDeck") !== null) {
333
			$("[id^=masterModal]").modal("hide");
334
			$('#discogsDeck').paginate({
335
				paginateElement: '.card',
336
				elementsPerPage: paginationPerPage(),
337
				effect: 'default',
338
				firstButtonText: '<i class="material-icons material-text">first_page</i>',
339
				lastButtonText: '<i class="material-icons material-text">last_page</i>',
340
				prevButtonText: '<i class="material-icons material-text">navigate_before</i>',
341
				nextButtonText: '<i class="material-icons material-text">navigate_next</i>',
124 - 342
				extraButtonClasses: 'btn bg-info px-2 shadow-sm',
120 - 343
				functionOnNav: function () { tooltipSetup(); if (typeof addDiscogsEvents === "function") { addDiscogsEvents();}  }
116 - 344
			});
345
            tooltipSetup();
120 - 346
            if (typeof addDiscogsEvents === "function") {
347
                addDiscogsEvents();
348
            }
116 - 349
		}
350
	} else {
120 - 351
        setTimeout(function () { paginationSetup(); }, 100);
116 - 352
	}
353
}
354
 
355
// Tooltip Setup
356
function tooltipSetup() {
357
	if ($.fn.tooltip && typeof Popper != 'undefined') {
107 - 358
		$('[data-toggle="tooltip"]').tooltip({
359
			trigger: 'hover'
360
		});
116 - 361
 
107 - 362
		$('[data-toggle2="tooltip"]').tooltip({
363
			trigger: 'hover'
364
		});
116 - 365
	} else {
120 - 366
        setTimeout(function () { tooltipSetup(); }, 100);
107 - 367
	}
43 - 368
}
369
 
52 - 370
function filterWishlist() {
107 - 371
	var input, filter, table, tr, td, i, j, txtValue, flag;
372
	input = document.getElementById("tableFilter");
373
	filter = input.value.toUpperCase();
374
	table = document.getElementById("wishlistTable");
375
	tr = table.getElementsByTagName("tr");
376
	document.getElementById("tableFilterButton").style.display = (filter.length > 0 ? "" : "none");
52 - 377
 
107 - 378
	// Loop through all table rows, excluding the header, and hide those who don't match the search query
379
	for (i = 1; i < tr.length; i++) {
380
		td = tr[i].getElementsByTagName("td");
381
		flag = "none";
52 - 382
 
107 - 383
		for (j = 0; j < td.length; j++) {
384
			if (td[j]) {
385
				txtValue = td[j].textContent || td[j].innerText;
386
				if (txtValue.toUpperCase().indexOf(filter) > -1) {
387
					flag = "";
388
					break;
389
				}
390
			}
391
		}
52 - 392
 
107 - 393
		tr[i].style.display = flag;
394
	}
52 - 395
}
396
 
397
function mysqliHtmlDecode(str) {
107 - 398
	str = str.replace(/&amp;/g, '&');
399
	str = str.replace(/&lt;/g, '<');
400
	str = str.replace(/&gt;/g, '>');
401
	return str;
52 - 402
}
403
 
46 - 404
// number of discogs entries per page according to screen size
43 - 405
function paginationPerPage() {
107 - 406
	var width = $(window).width();
407
	var num = Math.floor(width / 210);
408
	if (num > 9) {
409
		num = 9;
410
	} else if (width < 576) {
411
		num = 1;
412
	}
413
	return num;
43 - 414
}
46 - 415
 
416
// sort table by column
417
function sortTable(table, col, colType) {
107 - 418
	var rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
419
	table = document.getElementById(table);
420
	switching = true;
421
	// Set the sorting direction to ascending:
422
	dir = "asc";
423
	/* Make a loop that will continue until no switching has been done: */
424
	while (switching) {
425
		// Start by saying: no switching is done:
426
		switching = false;
427
		rows = table.rows;
428
		/* Loop through all table rows (except the first, which contains table headers): */
429
		for (i = 1; i < (rows.length - 1); i++) {
430
			// Start by saying there should be no switching:
431
			shouldSwitch = false;
432
			/* Get the two elements you want to compare, one from current row and one from the next: */
433
			x = rows[i].getElementsByTagName("TD")[col];
434
			y = rows[i + 1].getElementsByTagName("TD")[col];
435
			/* Check if the two rows should switch place, based on the direction, asc or desc: */
436
			if (dir == "asc") {
437
				if (colType == 'numeric') {
120 - 438
					if (Number(x.innerHTML) > Number(y.innerHTML)) {
107 - 439
						// If so, mark as a switch and break the loop:
440
						shouldSwitch = true;
441
						break;
442
					}
443
				} else /* if (colType == 'text') */ {
444
					if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
445
						// If so, mark as a switch and break the loop:
446
						shouldSwitch = true;
447
						break;
448
					}
449
				}
450
			} else if (dir == "desc") {
451
				if (colType == 'numeric') {
452
					if (Number(x.innerHTML) < Number(y.innerHTML)) {
453
						// If so, mark as a switch and break the loop:
454
						shouldSwitch = true;
455
						break;
456
					}
457
				} else /* if (colType == 'text') */ {
458
					if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
459
						// If so, mark as a switch and break the loop:
460
						shouldSwitch = true;
461
						break;
462
					}
463
				}
464
			}
465
		}
466
		if (shouldSwitch) {
467
			/* If a switch has been marked, make the switch and mark that a switch has been done: */
468
			rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
469
			switching = true;
470
			// Each time a switch is done, increase this count by 1:
471
			switchcount++;
472
		} else {
120 - 473
			/* If no switching has been done AND the direction is "asc", set the direction to "desc" and run the whole loop again. */
107 - 474
			if (switchcount === 0 && dir == "asc") {
475
				dir = "desc";
476
				switching = true;
477
			}
478
		}
479
	}
66 - 480
}
81 - 481
 
482
function topFunction() {
107 - 483
	document.body.scrollTop = document.documentElement.scrollTop = 0;
124 - 484
}