Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vote.js « js - github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bdab63b1066b6dbc2d5070229c13d37d5d7e4306 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var newUserDates = [];
var newUserTypes = [];

var maxVotes = 0;
var valuesChanged = false;

var tzOffset = new Date().getTimezoneOffset();

$.fn.switchClass = function (a, b) {
	this.removeClass(a);
	this.addClass(b);
	return this;
};

function updateCommentsCount() {
	$('#comment-counter').removeClass('no-comments');
	$('#comment-counter').text(parseInt($('#comment-counter').text()) +1);

}

function updateBest() {
	maxVotes = 0;
	$('.counter').each(function () {
		var yes = parseInt($(this).find('.yes').text());
		var no = parseInt($(this).find('.no').text());
		if(yes - no > maxVotes) {
			maxVotes = yes - no;
		}
	});
	var i = 0;
	$('.vote').each(function () {
		var yes = parseInt($(this).find('.yes').text());
		var no = parseInt($(this).find('.no').text());
		$(this).toggleClass('winner', yes - no === maxVotes);
	});
}

function updateCounters() {
	$('.result-cell.yes').each(function () {
		$(this).text($('#voteid_'+ $(this).attr('data-voteId') + '.poll-cell.yes').length);
	});
	$('.result-cell.no').each(function () {
		$(this).text($('#voteid_'+ $(this).attr('data-voteId') + '.poll-cell.no').length);
	});
	updateBest();
}

function switchSidebar() {
	if ($('#app-content').hasClass('with-app-sidebar')) {
		OC.Apps.hideAppSidebar();
	} else {
		OC.Apps.showAppSidebar();
	}
}

$(document).ready(function () {
	// count how many times in each date
	new Clipboard('.copy-link');
	updateBest();
	$('.delete-poll').click(function () {
		deletePoll(this);
	});

	$('#switchDetails').click(function () {
		switchSidebar();
	});

	$('#closeDetails').click(function () {
		OC.Apps.hideAppSidebar();
	});

	$('.avatar').each(function (i, obj) {
		$(obj).avatar(obj.title, 32);
	});

	$('.vote.time').each(function () {
		var extendedDate = new Date($(this).attr("data-value-utc").replace(/ /g,"T")+"Z"); //Fix display in Safari and IE

		$(this).find('.month').text(extendedDate.toLocaleString(window.navigator.language, {month: 'short'}));
		$(this).find('.day').text(extendedDate.toLocaleString(window.navigator.language, {day: 'numeric'}));
		$(this).find('.dayow').text(extendedDate.toLocaleString(window.navigator.language, {weekday: 'short'}));
		$(this).find('.time').text(extendedDate.toLocaleTimeString(window.navigator.language, {hour: 'numeric', minute:'2-digit', timeZoneName:'short'}));

	});

	$('#submit_finish_vote').click(function () {
		var form = document.finish_vote;
		var ac = document.getElementById('user_name');
		if (ac !== null) {
			if(ac.value.length >= 3){
				form.elements.userId.value = ac.value;
			} else {
				alert(t('polls', 'You are not registered.\nPlease enter your name to vote\n(at least 3 characters).'));
				return;
			}
		}
		var check_notif = document.getElementById('check_notif');
		var newUserDates = [], newUserTypes = [];
		$(".poll-cell.active").each(function () {
			if($(this).hasClass('no')) {
				newUserTypes.push(0);
			} else if ($(this).hasClass('yes')) {
				newUserTypes.push(1);
			} else if($(this).hasClass('maybe')) {
				newUserTypes.push(2);
			} else {
				newUserTypes.push(-1);
			}
			if (isNaN($(this).attr('data-value'))) {
				newUserDates.push($(this).attr('data-value'));
			} else {
				newUserDates.push(parseInt($(this).attr('data-value')));
			}
		});
		form.elements.dates.value = JSON.stringify(newUserDates);
		form.elements.types.value = JSON.stringify(newUserTypes);
		form.elements.receiveNotifications.value = (check_notif && check_notif.checked) ? 'true' : 'false';
		form.elements.changed.value = valuesChanged ? 'true' : 'false';
		form.submit();
	});

	$('#submit_send_comment').click(function (e) {
		e.preventDefault();
		var form = document.send_comment;
		var ac = document.getElementById('user_name_comm');
		if (ac !== null) {
			if(ac.value.length >= 3) {
				form.elements.userId.value = ac.value;
			} else {
				alert(t('polls', 'You are not registered.\nPlease enter your name to vote\n(at least 3 characters).'));
				return;
			}
		}
		var comment = document.getElementById('commentBox');
		if(comment.textContent.trim().length <= 0) {
			alert(t('polls', 'Please add some text to your comment before submitting it.'));
			return;
		}
		var data = {
			pollId: form.elements.pollId.value,
			userId: form.elements.userId.value,
			commentBox: comment.textContent.trim()
		};
		$('.new-comment .icon-loading-small').show();
		$.post(form.action, data, function(data) {
		var newCommentElement = '<li class="comment column"> ' +
								'<div class="authorRow user-cell row"> ' +
								'<div class="avatar missing" title="' + data.userName + '"></div> ' +
								'<div class="author">' + data.userName + '</div>' +
								'<div class="date has-tooltip live-relative-timestamp datespan" data-timestamp="' + Date.now() + '" title="' + data.date + '">' + t('now') + '</div>' +
								'</div>' +
								'<div class="message wordwrap comment-content">' + data.comment + '</div>' +
								'</li>';


			$('#no-comments').after(newCommentElement);

			if (!$('#no-comments').hasClass('hidden')) {
				$('#no-comments').addClass('hidden');
			}

			$('.new-comment .message').text('').focus();
			$('.new-comment .icon-loading-small').hide();

			$('.avatar.missing').each(function (i, obj) {
				$(obj).avatar(obj.title, 32);
				$(obj).removeClass('missing');
			});

			updateCommentsCount();
		}).error(function () {
			alert(t('polls', 'An error occurred, your comment was not posted.'));
			$('.new-comment .icon-loading-small').hide();
		});
	});

	$(".share input").click(function () {
		$(this).select();
	});

	$('.has-tooltip').tooltip();
	updateCounters();

});

$('#commentBox').keyup(function() {
	var $message = $('#commentBox');
	if(!$message.text().trim().length) {
		$message.empty();
	}
});

$(document).on('click', '.toggle-cell, .poll-cell.active', function () {
	valuesChanged = true;
	var $nextClass = "";
	var $toggleAllClasses = "";

	if($(this).hasClass('yes')) {
		$nextClass = "no";
		$toggleAllClasses= "yes";
	} else if($(this).hasClass('no')) {
		$nextClass = "maybe";
		$toggleAllClasses= "no";
	} else if($(this).hasClass('maybe')) {
		$nextClass = "yes";
		$toggleAllClasses= "maybe";
	} else {
		$nextClass = "yes";
		$toggleAllClasses= "maybe";
	}

	$(this).removeClass('yes no maybe unvoted');
	$(this).addClass($nextClass);

	if($(this).hasClass('toggle-cell')) {
		$(".poll-cell.active").removeClass('yes no maybe unvoted');
		$(".poll-cell.active").addClass($toggleAllClasses);
	}
	updateCounters();
});