Subversion Repositories cheapmusic

Rev

Rev 137 | Rev 141 | 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({
138 - 320
            valueProperty: "value",
116 - 321
			minLength: 0,
322
			searchContain: true,
138 - 323
                        searchByWord: true,
116 - 324
			noResultsText: ""
325
		}); /* reset after running clearer */
138 - 326
		$("input[id='searchTerm-flexdatalist']").val($("input[id='searchTerm']").val());
116 - 327
	} else {
120 - 328
		setTimeout(function () { flexdatalistSetup(); }, 100);
116 - 329
	}
330
}
331
 
43 - 332
// Pagination
65 - 333
function paginationSetup() {
120 - 334
	if ($.fn.paginate && $.fn.modal && $.fn.tooltip && typeof Popper !== "undefined") {
116 - 335
		if (document.getElementById("discogsDeck") !== null) {
336
			$("[id^=masterModal]").modal("hide");
337
			$('#discogsDeck').paginate({
338
				paginateElement: '.card',
339
				elementsPerPage: paginationPerPage(),
340
				effect: 'default',
341
				firstButtonText: '<i class="material-icons material-text">first_page</i>',
342
				lastButtonText: '<i class="material-icons material-text">last_page</i>',
343
				prevButtonText: '<i class="material-icons material-text">navigate_before</i>',
344
				nextButtonText: '<i class="material-icons material-text">navigate_next</i>',
124 - 345
				extraButtonClasses: 'btn bg-info px-2 shadow-sm',
120 - 346
				functionOnNav: function () { tooltipSetup(); if (typeof addDiscogsEvents === "function") { addDiscogsEvents();}  }
116 - 347
			});
348
            tooltipSetup();
120 - 349
            if (typeof addDiscogsEvents === "function") {
350
                addDiscogsEvents();
351
            }
116 - 352
		}
353
	} else {
120 - 354
        setTimeout(function () { paginationSetup(); }, 100);
116 - 355
	}
356
}
357
 
358
// Tooltip Setup
359
function tooltipSetup() {
360
	if ($.fn.tooltip && typeof Popper != 'undefined') {
107 - 361
		$('[data-toggle="tooltip"]').tooltip({
362
			trigger: 'hover'
363
		});
116 - 364
 
107 - 365
		$('[data-toggle2="tooltip"]').tooltip({
366
			trigger: 'hover'
367
		});
116 - 368
	} else {
120 - 369
        setTimeout(function () { tooltipSetup(); }, 100);
107 - 370
	}
43 - 371
}
372
 
52 - 373
function filterWishlist() {
134 - 374
	var input, filter, table, tr, td, i, j, flag;
107 - 375
	input = document.getElementById("tableFilter");
376
	filter = input.value.toUpperCase();
377
	table = document.getElementById("wishlistTable");
378
	tr = table.getElementsByTagName("tr");
379
	document.getElementById("tableFilterButton").style.display = (filter.length > 0 ? "" : "none");
52 - 380
 
107 - 381
	for (i = 1; i < tr.length; i++) {
382
		td = tr[i].getElementsByTagName("td");
134 - 383
		flag = "";
52 - 384
 
134 - 385
        words = filter.trim().split(" ");
386
        for (j = 0; j < words.length; j++) {
387
            if (filterWishlistWord(td, words[j]) === false) {
388
        		flag = "none";
389
        		break;
390
            }
391
        }
52 - 392
 
107 - 393
		tr[i].style.display = flag;
394
	}
52 - 395
}
396
 
134 - 397
function filterWishlistWord(td, word) {
398
    var i, txtvalue;
399
	for (i = 1; i <= 8; i++) {
400
		if (td[i]) {
401
			txtValue = td[i].textContent || td[i].innerText;
402
			if (txtValue.toUpperCase().indexOf(word) > -1) {
403
				return true;
404
			}
405
		}
406
	}
407
    return false;
408
}
409
 
52 - 410
function mysqliHtmlDecode(str) {
107 - 411
	str = str.replace(/&amp;/g, '&');
412
	str = str.replace(/&lt;/g, '<');
413
	str = str.replace(/&gt;/g, '>');
414
	return str;
52 - 415
}
416
 
46 - 417
// number of discogs entries per page according to screen size
43 - 418
function paginationPerPage() {
107 - 419
	var width = $(window).width();
420
	var num = Math.floor(width / 210);
421
	if (num > 9) {
422
		num = 9;
423
	} else if (width < 576) {
424
		num = 1;
425
	}
426
	return num;
43 - 427
}
46 - 428
 
429
// sort table by column
430
function sortTable(table, col, colType) {
107 - 431
	var rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
432
	table = document.getElementById(table);
433
	switching = true;
434
	// Set the sorting direction to ascending:
435
	dir = "asc";
436
	/* Make a loop that will continue until no switching has been done: */
437
	while (switching) {
438
		// Start by saying: no switching is done:
439
		switching = false;
440
		rows = table.rows;
441
		/* Loop through all table rows (except the first, which contains table headers): */
442
		for (i = 1; i < (rows.length - 1); i++) {
443
			// Start by saying there should be no switching:
444
			shouldSwitch = false;
445
			/* Get the two elements you want to compare, one from current row and one from the next: */
446
			x = rows[i].getElementsByTagName("TD")[col];
447
			y = rows[i + 1].getElementsByTagName("TD")[col];
448
			/* Check if the two rows should switch place, based on the direction, asc or desc: */
449
			if (dir == "asc") {
450
				if (colType == 'numeric') {
120 - 451
					if (Number(x.innerHTML) > Number(y.innerHTML)) {
107 - 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
			} else if (dir == "desc") {
464
				if (colType == 'numeric') {
465
					if (Number(x.innerHTML) < Number(y.innerHTML)) {
466
						// If so, mark as a switch and break the loop:
467
						shouldSwitch = true;
468
						break;
469
					}
470
				} else /* if (colType == 'text') */ {
471
					if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
472
						// If so, mark as a switch and break the loop:
473
						shouldSwitch = true;
474
						break;
475
					}
476
				}
477
			}
478
		}
479
		if (shouldSwitch) {
480
			/* If a switch has been marked, make the switch and mark that a switch has been done: */
481
			rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
482
			switching = true;
483
			// Each time a switch is done, increase this count by 1:
484
			switchcount++;
485
		} else {
120 - 486
			/* If no switching has been done AND the direction is "asc", set the direction to "desc" and run the whole loop again. */
107 - 487
			if (switchcount === 0 && dir == "asc") {
488
				dir = "desc";
489
				switching = true;
490
			}
491
		}
492
	}
66 - 493
}
81 - 494
 
495
function topFunction() {
107 - 496
	document.body.scrollTop = document.documentElement.scrollTop = 0;
124 - 497
}
137 - 498
 
138 - 499
String.prototype.toProperCase = function () {
500
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
501
};