summaryrefslogtreecommitdiffstats
path: root/public_html/data/js/multipaste.js
blob: 21434ab0d7b00c13de00da4e00b88ea392abf7f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(function () {
'use strict';
define(['underscore', 'util', 'jquery', 'jquery-ui'], function (_, Util, $) {
	var ui = {
		itemsContainer: ".multipasteQueue .items",
		queueDeleteButton: ".multipaste_queue_delete",
		submitButton: ".multipasteQueue button[type=submit]",
		itemImages: ".multipasteQueue .items img",
		form: ".multipasteQueue form",
		csrfToken: "form input[name=csrf_test_name]",
		ajaxFeedback: "form .ajaxFeedback",
	};

	var timer = 0;

	var PrivateFunctions = {
		setupQueueDeleteButtons: function() {
			$(ui.queueDeleteButton).on('click', function(event) {
				event.stopImmediatePropagation();
				var id = $(event.target).data('id');
				$(event.target).parent().remove();
				PrivateFunctions.saveQueue();
			});
		},
		setupTooltips: function() {
			$(ui.itemImages).popover({
				trigger: 'hover',
				placement: 'auto bottom',
				html: true
			});
		},
		setupButtons: function() {
			this.setupQueueDeleteButtons();
		},
		setupSortable: function() {
			$(ui.itemsContainer).sortable({
				revert: 100,
				placeholder: "ui-state-highlight",
				tolerance: "pointer",
				stop: function(e, u) {
					u.item.find("img").first().popover("show");
				},
				start: function(e, u) {
					u.item.find("img").first().popover("show");
				},
				update: function(e, u) {
					PrivateFunctions.saveQueue();
				},
			});

			$(ui.itemsContainer).disableSelection();
		},
		saveQueue: function() {
			var queue = $(ui.itemsContainer).sortable("toArray", {attribute: "data-id"});
			console.log("queue changed ", queue);
			clearTimeout(timer);
			timer = setTimeout(function() {
				var url = $(ui.form).data("ajax_url");
				var csrf_token = $(ui.csrfToken).attr("value");
				$(ui.ajaxFeedback).show();
				$.ajax({
					method: "POST",
					url: url,
					data: {
						csrf_test_name: csrf_token,
						ids: queue
					},
					complete: function() {
						$(ui.ajaxFeedback).hide();
					},
				});
			}, 2000);
		},
	};

	var Multipaste = {
		initialize: function () {
			PrivateFunctions.setupButtons();
			PrivateFunctions.setupSortable();
			PrivateFunctions.setupTooltips();
		},
	};

	return Multipaste;
});
})();