Subversion Repositories cheapmusic

Rev

Rev 134 | Rev 137 | 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", {
136 - 34
				submitBtn: $(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
		}
107 - 64
	});
116 - 65
 
107 - 66
	$(".detailFilterButton").click(function() {
67
		var formData = $("#detailFilterForm").serializeArray();
68
		formData.push({
69
			name: this.name,
121 - 70
			value: this.value,
107 - 71
		});
121 - 72
		formData.push({
73
    		name: "nonce",
74
			value: document.getElementById("nonce").value
75
		});
107 - 76
		$.post("filterTable.php",
77
			formData,
78
			function(data, status) {
79
				if (status == "success") {
80
					document.getElementById("productTable").innerHTML = data;
81
					detailTableFilterButtons();
82
					tableFilterButtons();
83
				}
84
			}
85
		);
86
	});
66 - 87
}
88
 
113 - 89
function barcodeFormButtons() {
90
	$("#barcodeForm").on('submit', function(e) {
107 - 91
		e.preventDefault();
113 - 92
		return false;
107 - 93
	});
94
 
113 - 95
	$(".barcodeButton").click(function() {
96
		var formData = $("#barcodeForm").serializeArray();
97
		formData.push({
98
			name: this.name,
99
			value: this.value
100
		});
121 - 101
		formData.push({
102
    		name: "nonce",
103
			value: document.getElementById("nonce").value
104
		});
113 - 105
		$.post("barcode.php",
106
			formData,
107
			function(data, status) {
108
				if (status == "success") {
109
					document.getElementById("barcodeResult").innerHTML = data;
110
				}
111
			}
112
		);
121 - 113
 
114
        new MutationObserver(function() {
115
        	$("#barcodeSearchForm").off("submit");
116
      	    if (document.getElementById("barcodeSearchTerm") && document.getElementById("barcodeSearchTerm").value !== "") {
117
            	$("#barcodeSearchForm").submit(function() {
134 - 118
                    searchTitle = "Searching for:<br><br><strong>" + document.getElementById("barcodeSearchTerm").value + "</strong>";
119
                    if (window.google_tag_manager && window.ga && ga.create) {
120
                        window.dataLayer.push({ "event" : "search", "search_term" : document.getElementById("barcodeSearchTerm").value, "eventCallback": function () {progressBar(searchTitle);}});
121
                    } else {
122
                        progressBar(searchTitle);
123
                    }
121 - 124
                });
125
    	    }
126
    	}).observe(document.getElementById("barcodeResult"), { childList: true, subtree: true });
113 - 127
	});
66 - 128
}
116 - 129
 
64 - 130
function alertAutoClose() {
107 - 131
	window.setTimeout(function() {
132
		$(".alert").fadeTo(1000, 0).slideUp(1000, function() {
133
			$(this).remove();
134
		});
135
	}, 3000);
64 - 136
}
137
 
15 - 138
function initProgressBarModal(title) {
107 - 139
	var elem = document.getElementById("progressBar");
140
	elem.style.width = '0%';
141
	elem.innerHTML = '0%';
15 - 142
 
107 - 143
	elem = document.getElementById("progressBarHeader");
144
	elem.innerHTML = title;
15 - 145
 
107 - 146
	elem = document.getElementById("progressBarMessage");
147
	elem.innerHTML = "";
84 - 148
 
107 - 149
	$("#progressBarDiv").modal({
150
		backdrop: "static"
151
	});
15 - 152
}
153
 
154
function updateProgressBar(width, message) {
107 - 155
	var elem = document.getElementById("progressBar");
156
	if (elem && width) {
157
		elem.style.width = width + '%';
158
		elem.innerHTML = width.toFixed(0) + '%';
159
		// bugbug document.getElementById("progressBarMessage").innerHTML = message;
160
	}
15 - 161
}
162
 
163
function endProgressBarModal() {
107 - 164
	$("#progressBarDiv").modal("hide");
15 - 165
}
166
 
167
// Refresh the progress bar.
168
function refreshProgress() {
107 - 169
	$.ajax({
122 - 170
		url: "pbChecker.php?file=" + document.getElementById("sessionId").value + "_" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value,
107 - 171
		success: function(data) {
172
			updateProgressBar(data.percent, data.message);
173
			if (data.percent >= 100) {
120 - 174
				window.clearInterval(myTimer);
175
				myTimer = window.setInterval(function() { completeProgress(); }, 1000);
107 - 176
			}
177
		}
178
	});
15 - 179
}
180
 
181
// End the progress bar.
182
function completeProgress() {
107 - 183
	updateProgressBar(100, "Completed");
120 - 184
	window.clearInterval(myTimer);
107 - 185
	endProgressBarModal();
15 - 186
}
187
 
188
// Start the progress bar.
189
function progressBar(title) {
107 - 190
	initProgressBarModal(title);
121 - 191
	myTimer = window.setInterval(function() { refreshProgress(); }, 500);
28 - 192
}
193
 
52 - 194
function saveTransfer(url) {
107 - 195
	var xhttp = new XMLHttpRequest();
196
	xhttp.open("POST", "savetransfer.php", true);
197
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 198
	xhttp.send("target=" + btoa(url) + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
43 - 199
}
200
 
52 - 201
function addWishlist(id, field, cnt, wl) {
107 - 202
	$(field).tooltip('hide');
203
	$(field).remove();
204
	var xhttp = new XMLHttpRequest();
205
	xhttp.onreadystatechange = function() {
206
		if (this.readyState == 4) {
207
			json = JSON.parse(this.responseText);
208
			switch (json.retval) {
209
				case 0:
210
					msg = "Added to wishlist.";
211
					alertTxt = "alert-success";
212
					break;
213
				case 1:
214
					msg = "Already on the wishlist.";
215
					alertTxt = "alert-warning";
216
					break;
217
				default:
218
					msg = "ERROR! Could not add to the wishlist.";
219
					alertTxt = "alert-danger";
220
					break;
221
			}
222
			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>";
223
			alertAutoClose();
224
		}
225
	};
226
	xhttp.open("POST", "wishlistDB.php", true);
227
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 228
	xhttp.send("id=" + id + "&function=add&wl=" + wl + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
52 - 229
}
230
 
231
function deleteWishlist(id, field, title, artist) {
107 - 232
	$(field).tooltip('hide');
233
	if (confirm('Remove ' + title + ' by ' + artist + ' from wishlist?')) {
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);
130 - 238
		field.closest("tr").remove();
107 - 239
	}
52 - 240
}
241
 
242
function editWishlist(id, field) {
107 - 243
	if (field !== null) {
244
		$(field).tooltip('hide');
130 - 245
		var parent = field.closest("tr");
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);
130 - 273
	var parent = field.closest("tr");
107 - 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() {
134 - 371
	var input, filter, table, tr, td, i, j, flag;
107 - 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
	for (i = 1; i < tr.length; i++) {
379
		td = tr[i].getElementsByTagName("td");
134 - 380
		flag = "";
52 - 381
 
134 - 382
        words = filter.trim().split(" ");
383
        for (j = 0; j < words.length; j++) {
384
            if (filterWishlistWord(td, words[j]) === false) {
385
        		flag = "none";
386
        		break;
387
            }
388
        }
52 - 389
 
107 - 390
		tr[i].style.display = flag;
391
	}
52 - 392
}
393
 
134 - 394
function filterWishlistWord(td, word) {
395
    var i, txtvalue;
396
	for (i = 1; i <= 8; i++) {
397
		if (td[i]) {
398
			txtValue = td[i].textContent || td[i].innerText;
399
			if (txtValue.toUpperCase().indexOf(word) > -1) {
400
				return true;
401
			}
402
		}
403
	}
404
    return false;
405
}
406
 
52 - 407
function mysqliHtmlDecode(str) {
107 - 408
	str = str.replace(/&amp;/g, '&');
409
	str = str.replace(/&lt;/g, '<');
410
	str = str.replace(/&gt;/g, '>');
411
	return str;
52 - 412
}
413
 
46 - 414
// number of discogs entries per page according to screen size
43 - 415
function paginationPerPage() {
107 - 416
	var width = $(window).width();
417
	var num = Math.floor(width / 210);
418
	if (num > 9) {
419
		num = 9;
420
	} else if (width < 576) {
421
		num = 1;
422
	}
423
	return num;
43 - 424
}
46 - 425
 
426
// sort table by column
427
function sortTable(table, col, colType) {
107 - 428
	var rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
429
	table = document.getElementById(table);
430
	switching = true;
431
	// Set the sorting direction to ascending:
432
	dir = "asc";
433
	/* Make a loop that will continue until no switching has been done: */
434
	while (switching) {
435
		// Start by saying: no switching is done:
436
		switching = false;
437
		rows = table.rows;
438
		/* Loop through all table rows (except the first, which contains table headers): */
439
		for (i = 1; i < (rows.length - 1); i++) {
440
			// Start by saying there should be no switching:
441
			shouldSwitch = false;
442
			/* Get the two elements you want to compare, one from current row and one from the next: */
443
			x = rows[i].getElementsByTagName("TD")[col];
444
			y = rows[i + 1].getElementsByTagName("TD")[col];
445
			/* Check if the two rows should switch place, based on the direction, asc or desc: */
446
			if (dir == "asc") {
447
				if (colType == 'numeric') {
120 - 448
					if (Number(x.innerHTML) > Number(y.innerHTML)) {
107 - 449
						// If so, mark as a switch and break the loop:
450
						shouldSwitch = true;
451
						break;
452
					}
453
				} else /* if (colType == 'text') */ {
454
					if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
455
						// If so, mark as a switch and break the loop:
456
						shouldSwitch = true;
457
						break;
458
					}
459
				}
460
			} else if (dir == "desc") {
461
				if (colType == 'numeric') {
462
					if (Number(x.innerHTML) < Number(y.innerHTML)) {
463
						// If so, mark as a switch and break the loop:
464
						shouldSwitch = true;
465
						break;
466
					}
467
				} else /* if (colType == 'text') */ {
468
					if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
469
						// If so, mark as a switch and break the loop:
470
						shouldSwitch = true;
471
						break;
472
					}
473
				}
474
			}
475
		}
476
		if (shouldSwitch) {
477
			/* If a switch has been marked, make the switch and mark that a switch has been done: */
478
			rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
479
			switching = true;
480
			// Each time a switch is done, increase this count by 1:
481
			switchcount++;
482
		} else {
120 - 483
			/* If no switching has been done AND the direction is "asc", set the direction to "desc" and run the whole loop again. */
107 - 484
			if (switchcount === 0 && dir == "asc") {
485
				dir = "desc";
486
				switching = true;
487
			}
488
		}
489
	}
66 - 490
}
81 - 491
 
492
function topFunction() {
107 - 493
	document.body.scrollTop = document.documentElement.scrollTop = 0;
124 - 494
}