Subversion Repositories cheapmusic

Rev

Rev 143 | Rev 149 | 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
 
143 - 23
	dataTableSetup();
24
 
116 - 25
	tooltipSetup();
141 - 26
 
143 - 27
	inputClearerSetup();
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();
143 - 43
					dataTableSetup();
107 - 44
				}
45
			}
46
		);
47
	});
15 - 48
}
49
 
66 - 50
function detailTableFilterButtons() {
107 - 51
	$("#detailFilterForm").on('submit', function(e) {
52
		e.preventDefault();
53
		return false;
54
	});
55
 
56
	$("#detailTab").click(function() {
116 - 57
		x = document.getElementById("detailFilter");
58
		if (x.style.display == "block") {
59
			document.getElementById("detailTabArrow").innerHTML = '<i class="material-icons material-text">expand_more</i>';
60
			x.style.display = "none";
61
		} else {
62
			document.getElementById("detailTabArrow").innerHTML = '<i class="material-icons material-text">expand_less</i>';
63
			x.style.display = "block";
64
		}
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();
143 - 84
                	dataTableSetup();
107 - 85
				}
86
			}
87
		);
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() {
134 - 120
                    searchTitle = "Searching for:<br><br><strong>" + document.getElementById("barcodeSearchTerm").value + "</strong>";
121
                    if (window.google_tag_manager && window.ga && ga.create) {
122
                        window.dataLayer.push({ "event" : "search", "search_term" : document.getElementById("barcodeSearchTerm").value, "eventCallback": function () {progressBar(searchTitle);}});
123
                    } else {
124
                        progressBar(searchTitle);
125
                    }
121 - 126
                });
127
    	    }
128
    	}).observe(document.getElementById("barcodeResult"), { childList: true, subtree: true });
113 - 129
	});
66 - 130
}
116 - 131
 
64 - 132
function alertAutoClose() {
107 - 133
	window.setTimeout(function() {
134
		$(".alert").fadeTo(1000, 0).slideUp(1000, function() {
135
			$(this).remove();
136
		});
137
	}, 3000);
64 - 138
}
139
 
15 - 140
function initProgressBarModal(title) {
107 - 141
	var elem = document.getElementById("progressBar");
143 - 142
	elem.style.width = '0';
107 - 143
	elem.innerHTML = '0%';
15 - 144
 
107 - 145
	elem = document.getElementById("progressBarHeader");
146
	elem.innerHTML = title;
15 - 147
 
107 - 148
	elem = document.getElementById("progressBarMessage");
149
	elem.innerHTML = "";
84 - 150
 
107 - 151
	$("#progressBarDiv").modal({
152
		backdrop: "static"
153
	});
15 - 154
}
155
 
156
function updateProgressBar(width, message) {
107 - 157
	var elem = document.getElementById("progressBar");
158
	if (elem && width) {
159
		elem.style.width = width + '%';
160
		elem.innerHTML = width.toFixed(0) + '%';
161
		// bugbug document.getElementById("progressBarMessage").innerHTML = message;
162
	}
15 - 163
}
164
 
165
function endProgressBarModal() {
107 - 166
	$("#progressBarDiv").modal("hide");
15 - 167
}
168
 
169
// Refresh the progress bar.
170
function refreshProgress() {
107 - 171
	$.ajax({
122 - 172
		url: "pbChecker.php?file=" + document.getElementById("sessionId").value + "_" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value,
107 - 173
		success: function(data) {
174
			updateProgressBar(data.percent, data.message);
175
			if (data.percent >= 100) {
120 - 176
				window.clearInterval(myTimer);
177
				myTimer = window.setInterval(function() { completeProgress(); }, 1000);
107 - 178
			}
179
		}
180
	});
15 - 181
}
182
 
183
// End the progress bar.
184
function completeProgress() {
107 - 185
	updateProgressBar(100, "Completed");
120 - 186
	window.clearInterval(myTimer);
107 - 187
	endProgressBarModal();
15 - 188
}
189
 
190
// Start the progress bar.
191
function progressBar(title) {
107 - 192
	initProgressBarModal(title);
121 - 193
	myTimer = window.setInterval(function() { refreshProgress(); }, 500);
28 - 194
}
195
 
52 - 196
function saveTransfer(url) {
107 - 197
	var xhttp = new XMLHttpRequest();
198
	xhttp.open("POST", "savetransfer.php", true);
199
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 200
	xhttp.send("target=" + btoa(url) + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
43 - 201
}
202
 
52 - 203
function addWishlist(id, field, cnt, wl) {
107 - 204
	$(field).tooltip('hide');
141 - 205
	$(field).children('i').first().html('library_add_check');
107 - 206
	var xhttp = new XMLHttpRequest();
207
	xhttp.onreadystatechange = function() {
208
		if (this.readyState == 4) {
209
			json = JSON.parse(this.responseText);
210
			switch (json.retval) {
211
				case 0:
212
					msg = "Added to wishlist.";
213
					alertTxt = "alert-success";
214
					break;
215
				case 1:
216
					msg = "Already on the wishlist.";
217
					alertTxt = "alert-warning";
218
					break;
219
				default:
220
					msg = "ERROR! Could not add to the wishlist.";
221
					alertTxt = "alert-danger";
222
					break;
223
			}
224
			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>";
225
			alertAutoClose();
226
		}
227
	};
228
	xhttp.open("POST", "wishlistDB.php", true);
229
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 230
	xhttp.send("id=" + id + "&function=add&wl=" + wl + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
52 - 231
}
232
 
233
function deleteWishlist(id, field, title, artist) {
107 - 234
	$(field).tooltip('hide');
235
	if (confirm('Remove ' + title + ' by ' + artist + ' from wishlist?')) {
236
		var xhttp = new XMLHttpRequest();
237
		xhttp.open("POST", "wishlistDB.php", true);
238
		xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 239
		xhttp.send("function=delete&id=" + id + "&sessionTab=" + document.getElementById("sessionTab").value + "&nonce=" + document.getElementById("nonce").value);
143 - 240
		row = field.closest("tr");
241
		if ($(row).hasClass("child")) {
242
		    row.previousElementSibling.remove();
243
		}
244
		row.remove();
107 - 245
	}
52 - 246
}
247
 
248
function editWishlist(id, field) {
107 - 249
	if (field !== null) {
250
		$(field).tooltip('hide');
143 - 251
		var row = field.closest("tr");
252
		if ($(row).hasClass("child")) {
253
		    row = row.previousElementSibling;
254
		}
52 - 255
 
143 - 256
		var artist = mysqliHtmlDecode(row.getElementsByTagName("td")[1].innerHTML);
257
		var title = mysqliHtmlDecode(row.getElementsByTagName("td")[2].innerHTML);
258
		var barcode = row.getElementsByTagName("td")[3].getAttribute("data-order");
259
		var cond = row.getElementsByTagName("td")[4].innerHTML;
260
		var format = row.getElementsByTagName("td")[5].innerHTML;
261
		var price = row.getElementsByTagName("td")[6].getAttribute("data-order");
52 - 262
 
107 - 263
		document.getElementById("wlMsg").innerHTML = "";
264
		document.getElementById("wlId").value = id;
265
		document.getElementById("wlArtist").value = artist;
266
		document.getElementById("wlTitle").value = title;
267
		document.getElementById("wlBarcode").value = barcode;
268
		document.getElementById("wlCond").value = cond;
269
		document.getElementById("wlFormat").value = format;
270
		document.getElementById("wlPrice").value = price;
271
	}
52 - 272
 
116 - 273
	$("#editWishlistModal").modal({
274
		backdrop: 'static',
275
		keyboard: false
276
	});
52 - 277
}
278
 
279
function saveEditedWishlist() {
107 - 280
	var id = document.getElementById("wlId").value;
143 - 281
	var row = document.getElementById("wlIdRow" + id);
107 - 282
	var artist = document.getElementById("wlArtist").value;
283
	var title = document.getElementById("wlTitle").value;
284
	var barcode = document.getElementById("wlBarcode").value;
285
	var cond = document.getElementById("wlCond").value;
286
	var format = document.getElementById("wlFormat").value;
287
	var price = document.getElementById("wlPrice").value;
143 - 288
	var table = row.closest("table");
52 - 289
 
107 - 290
	var xhttp = new XMLHttpRequest();
291
	xhttp.onreadystatechange = function() {
292
		if (this.readyState == 4) {
293
			json = JSON.parse(this.responseText);
294
			switch (json.retval) {
295
				case 0:
143 - 296
					$(table).DataTable().cell(row.getElementsByTagName("td")[1]).data(artist);
297
					row.setAttribute("data-artist", artist);
298
					$(table).DataTable().cell(row.getElementsByTagName("td")[2]).data(title);
299
					row.setAttribute("data-title", title);
300
					row.getElementsByTagName("td")[3].setAttribute("data-order", barcode);
301
					$(table).DataTable().cell(row.getElementsByTagName("td")[3]).data(barcode); // bugbug Format
302
					row.setAttribute("data-barcode", barcode);
303
					$(table).DataTable().cell(row.getElementsByTagName("td")[4]).data(cond);
304
					$(table).DataTable().cell(row.getElementsByTagName("td")[5]).data(format);
305
					row.getElementsByTagName("td")[6].setAttribute("data-order", price);
306
					$(table).DataTable().cell(row.getElementsByTagName("td")[6]).data('$' + Number(price).toFixed(2));
307
                    $(table).DataTable().draw();
107 - 308
					$("#editWishlistModal").modal('hide');
309
					break;
310
				case 1:
311
					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>";
312
					editWishlist(id, null);
313
					break;
314
				default:
315
					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>";
316
					break;
317
			}
318
		}
319
	};
320
	xhttp.open("POST", "wishlistDB.php", true);
321
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
122 - 322
	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 - 323
}
324
 
116 - 325
// Flex Data List
326
function flexdatalistSetup() {
143 - 327
	if ($.fn.flexdatalist) {
116 - 328
		$('.flexdatalist').flexdatalist({
138 - 329
            valueProperty: "value",
116 - 330
			minLength: 0,
331
			searchContain: true,
143 - 332
            searchByWord: true,
116 - 333
			noResultsText: ""
143 - 334
		});
138 - 335
		$("input[id='searchTerm-flexdatalist']").val($("input[id='searchTerm']").val());
141 - 336
        $("input.flexdatalist").on("select:flexdatalist", function (event) { handleFlexdataSelect(); });
116 - 337
	} else {
120 - 338
		setTimeout(function () { flexdatalistSetup(); }, 100);
116 - 339
	}
340
}
341
 
141 - 342
function handleFlexdataSelect() {
343
    var i, v, t, f;
143 - 344
 
141 - 345
    v = $("input.flexdatalist").val();
346
    if (v.substr(0, 5) != "*ADV*") {
143 - 347
        $('input.clearable').trigger("change");
141 - 348
        return;
349
    }
143 - 350
 
141 - 351
    t = v.substr(5).split("*|*");
352
    for (i = 0; i < t.length; i++) {
353
        f = t[i].split("*:*");
354
        if (f[0] == "a") {
355
            $("#advArtist").val(f[1]);
356
        } else if (f[0] == "t") {
357
            $("#advTitle").val(f[1]);
358
        } else if (f[0] == "tr") {
359
            $("#advTrack").val(f[1]);
360
        } else if (f[0] == "b") {
361
            $("#advBarcode").val(f[1]);
362
        } else if (f[0] == "c") {
363
            $("#advCatno").val(f[1]);
364
        } else if (f[0] == "l") {
365
            $("#advLabel").val(f[1]);
366
        } else if (f[0] == "co") {
367
            $("#advCountry").val(f[1]);
368
        } else if (f[0] == "y") {
369
            $("#advYear").val(f[1]);
370
        }
371
    }
372
 
373
    $("#simpleSearchDiv").addClass("d-none");
374
    $("#advSearchDiv").removeClass("d-none");
143 - 375
    $('input.clearable').trigger("change");
141 - 376
}
377
 
43 - 378
// Pagination
65 - 379
function paginationSetup() {
120 - 380
	if ($.fn.paginate && $.fn.modal && $.fn.tooltip && typeof Popper !== "undefined") {
116 - 381
		if (document.getElementById("discogsDeck") !== null) {
382
			$("[id^=masterModal]").modal("hide");
146 - 383
			$("[id^=videoModal]").modal("hide");
116 - 384
			$('#discogsDeck').paginate({
385
				paginateElement: '.card',
386
				elementsPerPage: paginationPerPage(),
387
				effect: 'default',
388
				firstButtonText: '<i class="material-icons material-text">first_page</i>',
389
				lastButtonText: '<i class="material-icons material-text">last_page</i>',
390
				prevButtonText: '<i class="material-icons material-text">navigate_before</i>',
391
				nextButtonText: '<i class="material-icons material-text">navigate_next</i>',
124 - 392
				extraButtonClasses: 'btn bg-info px-2 shadow-sm',
143 - 393
				functionOnNav: function () { tooltipSetup(); }
116 - 394
			});
395
            tooltipSetup();
396
		}
397
	} else {
120 - 398
        setTimeout(function () { paginationSetup(); }, 100);
116 - 399
	}
400
}
401
 
402
// Tooltip Setup
403
function tooltipSetup() {
404
	if ($.fn.tooltip && typeof Popper != 'undefined') {
107 - 405
		$('[data-toggle="tooltip"]').tooltip({
406
			trigger: 'hover'
407
		});
116 - 408
 
107 - 409
		$('[data-toggle2="tooltip"]').tooltip({
410
			trigger: 'hover'
411
		});
116 - 412
	} else {
120 - 413
        setTimeout(function () { tooltipSetup(); }, 100);
107 - 414
	}
43 - 415
}
416
 
143 - 417
// Tooltip Setup
418
function dataTableSetup() {
419
	if ($.fn.DataTable) {
420
	    $('table.DataTable').DataTable({
421
            "infoCallback": function( settings, start, end, max, total, pre ) {return "Showing " + total +" of "+ max + " entries";}
422
        });
423
        $('div.dataTables_filter input').attr('type', 'text').addClass('clearable');
424
	} else {
425
        setTimeout(function () { dataTableSetup(); }, 100);
107 - 426
	}
52 - 427
}
428
 
429
function mysqliHtmlDecode(str) {
107 - 430
	str = str.replace(/&amp;/g, '&');
431
	str = str.replace(/&lt;/g, '<');
432
	str = str.replace(/&gt;/g, '>');
433
	return str;
52 - 434
}
435
 
46 - 436
// number of discogs entries per page according to screen size
43 - 437
function paginationPerPage() {
107 - 438
	var width = $(window).width();
439
	var num = Math.floor(width / 210);
440
	if (num > 9) {
441
		num = 9;
442
	} else if (width < 576) {
443
		num = 1;
444
	}
445
	return num;
43 - 446
}
46 - 447
 
81 - 448
function topFunction() {
107 - 449
	document.body.scrollTop = document.documentElement.scrollTop = 0;
124 - 450
}
137 - 451
 
138 - 452
String.prototype.toProperCase = function () {
453
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
454
};
143 - 455
 
456
/* clearable input fields, toggle class */
457
function tog(v){return v?'addClass':'removeClass';}
458
 
459
function inputClearerSetup() {
460
    $(document).on('input focus change select', '.clearable', function(){
461
        $(this)[tog(this.value)]('x');
462
    }).on('mousemove', '.x', function( e ){
463
        $(this)[tog(this.offsetWidth-18 < e.clientX-this.getBoundingClientRect().left)]('onX');
464
    }).on('touchstart click', '.onX', function( ev ){
465
        ev.preventDefault();
466
        $(this).removeClass('x onX').val('').change();
146 - 467
        if (ev.target.id == "searchTerm-flexdatalist") {
468
            $('#' + ev.target.id).trigger("focusin");
469
        } else {
470
            $(this).trigger("keyup");
471
        }
143 - 472
    }).on('shown.bs.modal', function(e){
473
        $('#' + e.target.id + ' input.clearable').trigger("input");
474
    });
475
 
476
    $('input.clearable').trigger("change");
477
}