Subversion Repositories cheapmusic

Rev

Rev 138 | Rev 143 | 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();
141 - 24
 
120 - 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');
141 - 203
	$(field).children('i').first().html('library_add_check');
107 - 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;
141 - 288
					parent.setAttribute("data-artist", artist);
107 - 289
					parent.getElementsByTagName("td")[2].innerHTML = title;
141 - 290
					parent.setAttribute("data-title", title);
107 - 291
					parent.getElementsByTagName("td")[3].innerHTML = barcode;
292
					parent.getElementsByTagName("td")[4].innerHTML = barcode; // bugbug Format
141 - 293
					parent.setAttribute("data-barcode", barcode);
107 - 294
					parent.getElementsByTagName("td")[5].innerHTML = cond;
295
					parent.getElementsByTagName("td")[6].innerHTML = format;
296
					parent.getElementsByTagName("td")[7].innerHTML = price;
297
					parent.getElementsByTagName("td")[8].innerHTML = '$' + Number(price).toFixed(2); // bugbug Format
298
					$("#editWishlistModal").modal('hide');
299
					break;
300
				case 1:
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>" + json.msg + "</div>";
302
					editWishlist(id, null);
303
					break;
304
				default:
305
					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>";
306
					break;
307
			}
308
		}
309
	};
310
	xhttp.open("POST", "wishlistDB.php", true);
311
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 312
	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 - 313
}
314
 
116 - 315
// Flex Data List
316
function flexdatalistSetup() {
118 - 317
	if ($.fn.flexdatalist && $.fn.clearer) {
116 - 318
		$('.flexdatalist').flexdatalist();
319
		$(function() {
320
			$("input[id='searchTerm-flexdatalist']").clearer();
321
		});
322
		$('.flexdatalist').flexdatalist({
138 - 323
            valueProperty: "value",
116 - 324
			minLength: 0,
325
			searchContain: true,
138 - 326
                        searchByWord: true,
116 - 327
			noResultsText: ""
328
		}); /* reset after running clearer */
138 - 329
		$("input[id='searchTerm-flexdatalist']").val($("input[id='searchTerm']").val());
141 - 330
        $("input.flexdatalist").on("select:flexdatalist", function (event) { handleFlexdataSelect(); });
116 - 331
	} else {
120 - 332
		setTimeout(function () { flexdatalistSetup(); }, 100);
116 - 333
	}
334
}
335
 
141 - 336
function handleFlexdataSelect() {
337
    var i, v, t, f;
338
 
339
    v = $("input.flexdatalist").val();
340
    if (v.substr(0, 5) != "*ADV*") {
341
        return;
342
    }
343
 
344
    t = v.substr(5).split("*|*");
345
    for (i = 0; i < t.length; i++) {
346
        f = t[i].split("*:*");
347
        if (f[0] == "a") {
348
            $("#advArtist").val(f[1]);
349
        } else if (f[0] == "t") {
350
            $("#advTitle").val(f[1]);
351
        } else if (f[0] == "tr") {
352
            $("#advTrack").val(f[1]);
353
        } else if (f[0] == "b") {
354
            $("#advBarcode").val(f[1]);
355
        } else if (f[0] == "c") {
356
            $("#advCatno").val(f[1]);
357
        } else if (f[0] == "l") {
358
            $("#advLabel").val(f[1]);
359
        } else if (f[0] == "co") {
360
            $("#advCountry").val(f[1]);
361
        } else if (f[0] == "y") {
362
            $("#advYear").val(f[1]);
363
        }
364
    }
365
 
366
    $("#simpleSearchDiv").addClass("d-none");
367
    $("#advSearchDiv").removeClass("d-none");
368
}
369
 
43 - 370
// Pagination
65 - 371
function paginationSetup() {
120 - 372
	if ($.fn.paginate && $.fn.modal && $.fn.tooltip && typeof Popper !== "undefined") {
116 - 373
		if (document.getElementById("discogsDeck") !== null) {
374
			$("[id^=masterModal]").modal("hide");
375
			$('#discogsDeck').paginate({
376
				paginateElement: '.card',
377
				elementsPerPage: paginationPerPage(),
378
				effect: 'default',
379
				firstButtonText: '<i class="material-icons material-text">first_page</i>',
380
				lastButtonText: '<i class="material-icons material-text">last_page</i>',
381
				prevButtonText: '<i class="material-icons material-text">navigate_before</i>',
382
				nextButtonText: '<i class="material-icons material-text">navigate_next</i>',
124 - 383
				extraButtonClasses: 'btn bg-info px-2 shadow-sm',
120 - 384
				functionOnNav: function () { tooltipSetup(); if (typeof addDiscogsEvents === "function") { addDiscogsEvents();}  }
116 - 385
			});
386
            tooltipSetup();
120 - 387
            if (typeof addDiscogsEvents === "function") {
388
                addDiscogsEvents();
389
            }
116 - 390
		}
391
	} else {
120 - 392
        setTimeout(function () { paginationSetup(); }, 100);
116 - 393
	}
394
}
395
 
396
// Tooltip Setup
397
function tooltipSetup() {
398
	if ($.fn.tooltip && typeof Popper != 'undefined') {
107 - 399
		$('[data-toggle="tooltip"]').tooltip({
400
			trigger: 'hover'
401
		});
116 - 402
 
107 - 403
		$('[data-toggle2="tooltip"]').tooltip({
404
			trigger: 'hover'
405
		});
116 - 406
	} else {
120 - 407
        setTimeout(function () { tooltipSetup(); }, 100);
107 - 408
	}
43 - 409
}
410
 
52 - 411
function filterWishlist() {
134 - 412
	var input, filter, table, tr, td, i, j, flag;
107 - 413
	input = document.getElementById("tableFilter");
414
	filter = input.value.toUpperCase();
415
	table = document.getElementById("wishlistTable");
416
	tr = table.getElementsByTagName("tr");
417
	document.getElementById("tableFilterButton").style.display = (filter.length > 0 ? "" : "none");
52 - 418
 
107 - 419
	for (i = 1; i < tr.length; i++) {
420
		td = tr[i].getElementsByTagName("td");
134 - 421
		flag = "";
52 - 422
 
134 - 423
        words = filter.trim().split(" ");
424
        for (j = 0; j < words.length; j++) {
425
            if (filterWishlistWord(td, words[j]) === false) {
426
        		flag = "none";
427
        		break;
428
            }
429
        }
52 - 430
 
107 - 431
		tr[i].style.display = flag;
432
	}
52 - 433
}
434
 
134 - 435
function filterWishlistWord(td, word) {
436
    var i, txtvalue;
437
	for (i = 1; i <= 8; i++) {
438
		if (td[i]) {
439
			txtValue = td[i].textContent || td[i].innerText;
440
			if (txtValue.toUpperCase().indexOf(word) > -1) {
441
				return true;
442
			}
443
		}
444
	}
445
    return false;
446
}
447
 
52 - 448
function mysqliHtmlDecode(str) {
107 - 449
	str = str.replace(/&amp;/g, '&');
450
	str = str.replace(/&lt;/g, '<');
451
	str = str.replace(/&gt;/g, '>');
452
	return str;
52 - 453
}
454
 
46 - 455
// number of discogs entries per page according to screen size
43 - 456
function paginationPerPage() {
107 - 457
	var width = $(window).width();
458
	var num = Math.floor(width / 210);
459
	if (num > 9) {
460
		num = 9;
461
	} else if (width < 576) {
462
		num = 1;
463
	}
464
	return num;
43 - 465
}
46 - 466
 
467
// sort table by column
468
function sortTable(table, col, colType) {
107 - 469
	var rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
470
	table = document.getElementById(table);
471
	switching = true;
472
	// Set the sorting direction to ascending:
473
	dir = "asc";
474
	/* Make a loop that will continue until no switching has been done: */
475
	while (switching) {
476
		// Start by saying: no switching is done:
477
		switching = false;
478
		rows = table.rows;
479
		/* Loop through all table rows (except the first, which contains table headers): */
480
		for (i = 1; i < (rows.length - 1); i++) {
481
			// Start by saying there should be no switching:
482
			shouldSwitch = false;
483
			/* Get the two elements you want to compare, one from current row and one from the next: */
484
			x = rows[i].getElementsByTagName("TD")[col];
485
			y = rows[i + 1].getElementsByTagName("TD")[col];
486
			/* Check if the two rows should switch place, based on the direction, asc or desc: */
487
			if (dir == "asc") {
488
				if (colType == 'numeric') {
120 - 489
					if (Number(x.innerHTML) > Number(y.innerHTML)) {
107 - 490
						// If so, mark as a switch and break the loop:
491
						shouldSwitch = true;
492
						break;
493
					}
494
				} else /* if (colType == 'text') */ {
495
					if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
496
						// If so, mark as a switch and break the loop:
497
						shouldSwitch = true;
498
						break;
499
					}
500
				}
501
			} else if (dir == "desc") {
502
				if (colType == 'numeric') {
503
					if (Number(x.innerHTML) < Number(y.innerHTML)) {
504
						// If so, mark as a switch and break the loop:
505
						shouldSwitch = true;
506
						break;
507
					}
508
				} else /* if (colType == 'text') */ {
509
					if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
510
						// If so, mark as a switch and break the loop:
511
						shouldSwitch = true;
512
						break;
513
					}
514
				}
515
			}
516
		}
517
		if (shouldSwitch) {
518
			/* If a switch has been marked, make the switch and mark that a switch has been done: */
519
			rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
520
			switching = true;
521
			// Each time a switch is done, increase this count by 1:
522
			switchcount++;
523
		} else {
120 - 524
			/* If no switching has been done AND the direction is "asc", set the direction to "desc" and run the whole loop again. */
107 - 525
			if (switchcount === 0 && dir == "asc") {
526
				dir = "desc";
527
				switching = true;
528
			}
529
		}
530
	}
66 - 531
}
81 - 532
 
533
function topFunction() {
107 - 534
	document.body.scrollTop = document.documentElement.scrollTop = 0;
124 - 535
}
137 - 536
 
138 - 537
String.prototype.toProperCase = function () {
538
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
539
};