Subversion Repositories cheapmusic

Rev

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