aboutsummaryrefslogtreecommitdiff
path: root/p/scripts/category.js
blob: c33e685285dc8cee7b9c341f15c66b63539020b6 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict";

var loading = false,
	dnd_successful = false;

function dragend_process(t) {
	t.style.display = 'none';

	if (loading) {
		window.setTimeout(function() {
			dragend_process(t);
		}, 50);
	}

	if (!dnd_successful) {
		t.style.display = 'block';
		t.style.opacity = 1.0;
	} else {
		var parent = $(t.parentNode);
		$(t).remove();

		if (parent.children().length <= 0) {
			parent.append('<li class="item disabled" dropzone="move">' + i18n['category_empty'] + '</li>');
		}
	}
}

function init_draggable() {
	if (!(window.$ && window.i18n)) {
		if (window.console) {
			console.log('FreshRSS waiting for JS…');
		}
		window.setTimeout(init_draggable, 50);
		return;
	}

	$.event.props.push('dataTransfer');

	var draggable = '[draggable="true"]',
	    dropzone = '[dropzone="move"]';

	$('.drop-section').on('dragstart', draggable, function(e) {
		e.dataTransfer.effectAllowed = 'move';
		e.dataTransfer.setData('text/html', e.target.outerHTML);
		e.dataTransfer.setData('text', e.target.getAttribute('data-feed-id'));
		e.target.style.opacity = 0.3;

		dnd_successful = false;
	});
	$('.drop-section').on('dragend', draggable, function(e) {
		dragend_process(e.target);
	});

	$('.drop-section').on('dragenter', dropzone, function(e) {
		$(this).addClass('drag-hover');

		e.preventDefault();
	});
	$('.drop-section').on('dragleave', dropzone, function(e) {
		var pos_this = $(this).position(),
		    scroll_top = $(document).scrollTop(),
		    top = pos_this.top,
		    left = pos_this.left,
		    right = left + $(this).width(),
		    bottom = top + $(this).height(),
		    mouse_x = e.originalEvent.screenX,
		    mouse_y = e.originalEvent.clientY + scroll_top;

		if (left <= mouse_x && mouse_x <= right &&
			top <= mouse_y && mouse_y <= bottom) {
			// HACK because dragleave is triggered when hovering children!
			return;
		}
		$(this).removeClass('drag-hover');
	});
	$('.drop-section').on('dragover', dropzone, function(e) {
		e.dataTransfer.dropEffect = "move";

		e.preventDefault();
		return false;
	});
	$('.drop-section').on('drop', dropzone, function(e) {
		var feed_id = e.dataTransfer.getData('text'),
		    cat_id = e.target.parentNode.getAttribute('data-cat-id');

		loading = true;

		$.ajax({
			type: 'POST',
			url: './?c=feed&a=move',
			data : {
				f_id: feed_id,
				c_id: cat_id
			}
		}).success(function() {
			$(e.target).after(e.dataTransfer.getData('text/html'));
			if ($(e.target).hasClass('disabled')) {
				$(e.target).remove();
			}
			dnd_successful = true;
		}).complete(function() {
			loading = false;
		});

		$(this).removeClass('drag-hover');

		e.preventDefault();
	});
}


if (document.readyState && document.readyState !== 'loading') {
	init_draggable();
} else if (document.addEventListener) {
	document.addEventListener('DOMContentLoaded', function () {
		init_draggable();
	}, false);
}