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

create_edit.js « js - github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 82459e9841452a1e03ce98f7418f3905fc5e184d (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
var g_chosen_datetimes = [];
var g_chosen_texts = [];
var g_chosen_groups = [];
var g_chosen_users = [];
var chosen_type = 'event';
var access_type = '';
var isAnonymous;
var hideNames;

function selectItem(cell) {
	cell.removeClass('icon-close');
	cell.addClass('icon-checkmark');
	cell.removeClass('date-text-not-selected');
	cell.addClass('date-text-selected');
	if (cell.attr('class').indexOf('is-text') > -1) {
		var id = cell.attr('id');
		g_chosen_texts.push(id.substring(id.indexOf('_') + 1));
	} else {
		var dateId = cell.parent().attr('id'); //timestamp of date
		var timeId = cell.attr('id');
		g_chosen_datetimes.push(parseInt(dateId) + parseInt(timeId));
	}
}

function deselectItem(cell) {
	var id;
	var index;
	var dateId;
	var timeId;
	cell.removeClass('icon-checkmark');
	cell.addClass('icon-close');
	cell.removeClass('date-text-selected');
	cell.addClass('date-text-not-selected');
	if (cell.attr('class').indexOf('is-text') > -1) {
		id = cell.attr('id');
		index = g_chosen_texts.indexOf(id.substring(id.indexOf('_') + 1));
		if (index > -1) {
			g_chosen_texts.splice(index, 1);
		}
	} else {
		dateId = cell.parent().attr('id'); //timestamp of date
		timeId = cell.attr('id');
		index = g_chosen_datetimes.indexOf(parseInt(dateId) + parseInt(timeId));
		if (index > -1) {
			g_chosen_datetimes.splice(index, 1);
		}
	}
}

function insertText(text, set) {
	if (typeof set === 'undefined') {
		set = false;
	}
	var table = document.getElementById('selected-texts-table');
	var tr = table.insertRow(-1);
	var td = tr.insertCell(-1);
	td.innerHTML = text;
	td.className = 'text-row';
	td = tr.insertCell(-1);
	if (set) {
		td.className = 'icon-checkmark is-text date-text-selected';
	} else {
		td.className = 'icon-close is-text date-text-not-selected';
	}
	td.id = 'text_' + text;
}

function addRowToList(ts, text, timeTs) {
	if (typeof timeTs === 'undefined') {
		timeTs = -1;
	}
	var table = document.getElementById('selected-dates-table');
	var rows = table.rows;
	var td, tr, tdId;
	var i, j;
	var curr;
	if (rows.length === 0) {
		tr = table.insertRow(-1); //start new header
		tr.insertCell(-1);
		tr = table.insertRow(-1); //append new row
		tr.id = ts;
		tr.className = 'toggleable-row';
		td = tr.insertCell(-1);
		td.className = 'date-row';
		td.innerHTML = text;
		return;
	}
	for ( i=1; i<rows.length; i++) {
		curr = rows[i];
		if (curr.id === ts) {
			for (j=1; j<curr.cells.length; j++) {
				td = curr.cells[j];
				 tdId = curr.cells[j].id;
				if ( timeTs === tdId) {
					td.className = 'icon-checkmark date-text-selected';
				}
			}
			return; //already in table, cancel
		} else if (curr.id > ts) {
			tr = table.insertRow(i); //insert row at current index
			tr.id = ts;
			tr.className = 'toggleable-row';
			td = tr.insertCell(-1);
			td.className = 'date-row';
			td.innerHTML = text;
			for (j=1; j<rows[0].cells.length; j++) {
				tdId = rows[0].cells[j].id;
				td = tr.insertCell(-1);
				if (timeTs === tdId) {
					td.className = 'icon-checkmark date-text-selected';
				}  else { 
					td.className = 'icon-close date-text-not-selected';
				}
				td.id = tdId;
				td.innerHTML = '';
			}
			return;
		}
	}
	tr = table.insertRow(-1); //highest value, append new row
	tr.id = ts;
	tr.className = 'toggleable-row';
	td = tr.insertCell(-1);
	td.className = 'date-row';
	td.innerHTML = text;
	for (j=1; j<rows[0].cells.length; j++) {
		tdId = rows[0].cells[j].id;
		td = tr.insertCell(-1);
		if (timeTs === tdId) {
			td.className = 'icon-checkmark date-text-selected';
		} else {
			td.className = 'icon-close date-text-not-selected';
		}
		td.id = tdId;
		td.innerHTML = '';
	}
}

function addColToList(ts, text, dateTs) {
	if (typeof dateTs === 'undefined') {
		dateTs = -1;
	}
	var table = document.getElementById('selected-dates-table');
	var rows = table.rows;
	var tr, row, td, cells, tmpRow;
	var i, curr;
	var index = -1;
	if (rows.length === 0) {
		tr = table.insertRow(-1);
		tr.insertCell(-1);
	}
	rows = table.rows;
	tmpRow = rows[0];
	for (i=0; i<tmpRow.cells.length; i++) {
		curr = tmpRow.cells[i];
		if (curr.id === ts) {
			return; //already in table, cancel
		} else if (curr.id > ts) {
			index = i;
			break;
		}
	}

	for (i=0; i<rows.length; i++) {
		row = rows[i];
		cells = row.cells;
		td = row.insertCell(index);
		//only display time in header row
		if (i===0) {
			td.innerHTML = text;
			td.className = 'date-col';
		} else {
			td.innerHTML = '';
			if (row.id === dateTs) {
				td.className = 'icon-checkmark date-text-selected';
			} else { 
				td.className = 'icon-close date-text-not-selected';
			}
		}
		td.id = ts;
	}
}

function debounce(f, wait, immediate) {
	var timeout;
	return function() {
		var context = this;
		var args = arguments;
		var later = function() {
			timeout = null;
			if (!immediate) {
				f.apply(context, args);
			}
		};
		var callNow = immediate && !timeout;
		clearTimeout(timeout);
		timeout = setTimeout(later, wait);
		if (callNow) {
			f.apply(context, args);
		}
	};
}

$(document).ready(function () {
	// enable / disable date picker
	var i;
	$('#id_expire_set').click(function(){
		$('#id_expire_date').prop("disabled", !this.checked);
		if (this.checked) {
		   $("#id_expire_date").focus();
		}
	});

	var anonOptions = document.getElementById('anonOptions');
	$('#hideNames').click(function() {
		hideNames = this.checked;
	});

	$('#isAnonymous').click(function() {
		isAnonymous = this.checked;
		if (isAnonymous) {
			anonOptions.style.display = 'inline';
		} else {
			anonOptions.style.display = 'none';
		}
	});

	var privateRadio = document.getElementById('private');
	var hiddenRadio = document.getElementById('hidden');
	var publicRadio = document.getElementById('public');
	var selectRadio = document.getElementById('select');
	if (privateRadio.checked) {
		access_type = 'registered';
	} else if (hiddenRadio.checked) {
		access_type = 'hidden';
	} else if (publicRadio.checked) {
		access_type = 'public';
	} else if (selectRadio.checked) {
		access_type = 'select';
	}

	isAnonymous = document.getElementById('isAnonymous').checked;
	hideNames = anonOptions.checked;

	var accessValues = document.getElementById('accessValues');
	if (accessValues.value.length > 0) {
		var list = document.getElementById('selected-search-list-id');
		var accessValueArr = accessValues.value.split(';');
		for (i=0; i<accessValueArr.length; i++) {
			var val = accessValueArr[i];
			if (val === '') {
				continue;
			}
			var li = document.createElement('li');
			li.id = val;
			li.className = 'cl_item cl_access_item selected';
			var index = val.indexOf('group_');
			if (index === 0) {
				g_chosen_groups.push(val);
				li.className += ' is-group';
				li.appendChild(document.createTextNode(val.substring(6) + " (group)"));
				list.appendChild(li);
			} else {
				index = val.indexOf('user_');
				if (index === 0) {
					g_chosen_users.push(val);
					li.className = 'cl_item cl_access_item selected';
					var username = val.substring(5);
					$.post(OC.generateUrl('/apps/polls/get/displayname'), {username: username}, function(data) {
						li.appendChild(document.createTextNode(username + " (" + data + ")"));
						list.appendChild(li);
					});
				}
			}
		}
	}

	var chosenDates = document.getElementById('chosenDates').value;
	var chosen = '';
	if (chosenDates.length > 0) {
		chosen = JSON.parse(chosenDates);
	}
	var text = document.getElementById('text');
	var event = document.getElementById('event');
	if (event.checked) {
		chosen_type = event.value;
		if (chosenDates.length > 0) {
			g_chosen_datetimes = chosen;
		}
		for (i=0; i<chosen.length; i++) {
			var date = new Date(chosen[i]*1000);
			var year = date.getFullYear();
			var month = date.getMonth();
			var day = date.getDate();
			var newDate = new Date(year, month, day).getTime(); //save timestamp without time of day
			month = '0' + (month+1); //month is 0-11, so +1
			day = '0' + day;
			var dateStr = day.substr(-2) + '.' + month.substr(-2) + '.' + year;
			var hours = date.getHours();
			var minutes = date.getMinutes();
			var ms = (hours * 60 * 60 * 1000) + (minutes * 60 * 1000); //time of day in milliseconds
			hours = '0' + hours;
			minutes = '0' + minutes;
			var timeStr = hours.substr(-2) + ':' + minutes.substr(-2);
			addRowToList(newDate/1000, dateStr, ms/1000);
			addColToList(ms/1000, timeStr, newDate/1000);
		}
	} else {
		chosen_type = text.value;
		if (chosenDates.length > 0) {
			g_chosen_texts = chosen;
		}
		for (i=0; i<chosen.length; i++) {
			insertText(chosen[i], true);
		}
	}

	var expirepicker = jQuery('#id_expire_date').datetimepicker({
		inline: false,
		onSelectDate: function(date) {
			var year = date.getFullYear();
			var month = date.getMonth();
			var day = date.getDate();
			var newDate = new Date(year, month, day).getTime()/1000;
			document.getElementById('expireTs').value = newDate;
		},
		timepicker: false,
		format: 'd.m.Y'
	});

	var datepicker = jQuery('#datetimepicker').datetimepicker({
		inline:true,
		step: 15,
		todayButton: true,
		onSelectDate: function(date) {
			var year = date.getFullYear();
			var month = date.getMonth();
			var day = date.getDate();
			var newDate = new Date(year, month, day).getTime(); //save timestamp without time of day
			month = '0' + (month+1); //month is 0-11, so +1
			day = '0' + day;
			var dateStr = day.substr(-2) + '.' + month.substr(-2) + '.' + year;
			addRowToList(newDate/1000, dateStr);
		},
		onSelectTime: function(date) {
			var hours = date.getHours();
			var minutes = date.getMinutes();
			var ms = (hours * 60 * 60 * 1000) + (minutes * 60 * 1000); //time of day in milliseconds
			hours = '0' + hours;
			minutes = '0' + minutes;
			var timeStr = hours.substr(-2) + ':' + minutes.substr(-2);
			addColToList(ms/1000, timeStr);
		}
	});

	$(document).on('click', '.date-row', function() {
		var tr = $(this).parent();
		var dateId = parseInt(tr.attr('id'));
		var index = tr.index();
		var cells = tr[0].cells; //convert jQuery object to DOM
		for (var i=1; i<cells.length; i++) {
			var cell = cells[i];
			var delIndex = g_chosen_datetimes.indexOf(dateId + parseInt(cell.id));
			if (delIndex > -1) {
				g_chosen_datetimes.splice(delIndex, 1);
			}
		}
		var table = document.getElementById('selected-dates-table');
		table.deleteRow(index);
	});

	$(document).on('click', '.date-col', function() {
		var cellIndex = $(this).index();
		var timeId = parseInt($(this).attr('id'));
		var table = document.getElementById('selected-dates-table');
		var rows = table.rows;
		rows[0].deleteCell(cellIndex);
		for (var i=1; i<rows.length; i++) {
			var row = rows[i];
			var delIndex = g_chosen_datetimes.indexOf(parseInt(row.id) + timeId);
			if (delIndex > -1) {
				g_chosen_datetimes.splice(delIndex, 1);
			}
			row.deleteCell(cellIndex);
		}
	});

	$(document).on('click', '.text-row', function() {
		var tr = $(this).parent();
		var rowIndex = tr.index();
		var name = $(this).html();
		var delIndex = g_chosen_texts.indexOf(name);
		if (delIndex > -1) {
			g_chosen_texts.splice(delIndex, 1);
		}
		var table = document.getElementById('selected-texts-table');
		table.deleteRow(rowIndex);
	});

	$(document).on('click', '.icon-close', function() {
		selectItem($(this));
	});

	$(document).on('click', '.icon-checkmark', function() {
		deselectItem($(this));
	});

	$(document).on('click', '#text-submit', function() {
		var text = document.getElementById('text-title');
		if (text.value.length === 0) {
			alert('Please enter a text!');
			return false;
		}
		insertText(text.value);
		text.value = '';
	});

	$(document).on('click', '.cl_item', function() {
		var index;
		var list = document.getElementById('selected-search-list-id');
		var isGroup = $(this).hasClass('is-group');
		if ($(this).hasClass('selected')) {
			if (isGroup) {
				index = g_chosen_groups.indexOf(this.id);
			}
			else { 
				index = g_chosen_users.indexOf(this.id);
			}
			if (index > -1) {
				if (isGroup) {
					g_chosen_groups.splice(index, 1);
				}
				else {
					g_chosen_users.splice(index, 1);
				}
				$(this).remove();
			}
		} else {
			if (!isGroup) {
				var text = this.id.replace('user_', '');
				g_chosen_users.push(this.id);
			} else {
				g_chosen_groups.push(this.id);
			}
			document.getElementById('user-group-search-box').value = '';
			var li = document.createElement('li');
			li.id = this.id;
			li.className = 'cl_item cl_access_item selected' + (isGroup ? ' is-group' : '');
			if (!isGroup) {
				$.post(OC.generateUrl('/apps/polls/get/displayname'), {username: text}, function(data) {
					li.appendChild(document.createTextNode(text + " (" + data + ")"));
					list.appendChild(li);
				});
			} else {
				li.appendChild(document.createTextNode($(this).html()));
				list.appendChild(li);
			}
			$(this).remove();
		}
	});

	$('.toggleable-row').hover(
		function() {
			var td = this.insertCell(-1);
			td.className = 'toggle-all selected-all';
		}, function() {
			$(this).find('td:last-child').remove();
		}
	);

	$(document).on('click', '.toggle-all', function() {
		var children;
		var i;
		if ($(this).attr('class').indexOf('selected-all') > -1) {
			children = $(this).parent().children('.icon-checkmark');
			for (i=0; i<children.length; i++) {
				deselectItem($(children[i]));
			}
			$(this).removeClass('selected-all');
			$(this).addClass('selected-none');
		} else {
			children = $(this).parent().children('.icon-close');
			for (i=0; i<children.length; i++) {
				selectItem($(children[i]));
			}
			$(this).removeClass('selected-none');
			$(this).addClass('selected-all');
		}
	});

	$('input[type=radio][name=pollType]').change(function() {
		if (this.value === 'event') {
			chosen_type = 'event';
			document.getElementById('text-select-container').style.display = 'none';
			document.getElementById('date-select-container').style.display = 'inline';
		} else {
			chosen_type = 'text';
			document.getElementById('text-select-container').style.display = 'inline';
			document.getElementById('date-select-container').style.display = 'none';
		}
	});

	$('input[type=radio][name=accessType]').click(function() {
		access_type = this.value;
		if (access_type === 'select') {
			$("#access_rights").show();
			$("#selected_access").show();
		} else {
			$("#access_rights").hide();
			$("#selected_access").hide();
		}
	});

	$('input[type=checkbox][name=check_expire]').change(function() {
		if (!$(this).is(':checked')) {
			document.getElementById('expireTs').value = '';
		}
	});

	$('#user-group-search-box').on('input', debounce(function() {
		var ul = document.getElementById('live-search-list-id');
		while(ul.firstChild) {
			ul.removeChild(ul.firstChild);
		}
		var val = $(this).val();
		if (val.length < 3) {
			return;
		}
		var formData = {
			searchTerm: val,
			groups: JSON.stringify(g_chosen_groups),
			users: JSON.stringify(g_chosen_users)
		};
		$.post(OC.generateUrl('/apps/polls/search'), formData, function(data) {
			for (var i=0; i<data.length; i++) {
				var ug = data[i];
				var li = document.createElement('li');
				li.className = 'cl_item cl_access_item';
				if (ug.isGroup) {
					li.id = 'group_' + ug.gid;
					li.className += ' is-group';
					li.appendChild(document.createTextNode(ug.gid + " (group)"));
					ul.appendChild(li);
				} else {
					li.id = 'user_' + ug.uid;
					li.appendChild(document.createTextNode(ug.uid + " (" + ug.displayName + ")"));
					var span = document.createElement('span');
					span.id = 'sec_name';
					span.appendChild(document.createTextNode(ug.uid));
					li.appendChild(span);
					ul.appendChild(li);
				}
			}
		});
	}, 250));

	$('.live-search-list-user li').each(function(){
	$(this).attr('data-search-term', $(this).text().toLowerCase());
	});

	$('.live-search-box-user').on('keyup', function(){
	var searchTerm = $(this).val().toLowerCase();
		$('.live-search-list-user li').each(function(){
			if (   $(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0  
			    || searchTerm.length < 1
			) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});
	});

	$('.live-search-list-group li').each(function(){
		$(this).attr('data-search-term', $(this).text().toLowerCase());
	});
 
	$('.live-search-box-group').on('keyup', function(){
	var searchTerm = $(this).val().toLowerCase();
		$('.live-search-list-group li').each(function(){
			if (   $(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0 
			    || searchTerm.length < 1
			) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});
	}); 

	var form = document.finish_poll;
	var submit_finish_poll = document.getElementById('submit_finish_poll');
	if (submit_finish_poll !== null) {
		submit_finish_poll.onclick = function() {
			if (   g_chosen_datetimes.length === 0 
			    && g_chosen_texts.length === 0
			) {
				alert(t('polls', 'Nothing selected!\nClick on cells to turn them green.'));
				return false;
			}
			if (chosen_type === 'event') {
				form.elements.chosenDates.value = JSON.stringify(g_chosen_datetimes);
			}
			else {
				form.elements.chosenDates.value = JSON.stringify(g_chosen_texts);
			}
			var title = document.getElementById('pollTitle');
			if (   title === null 
			    || title.value.length === 0
			) {
				alert(t('polls', 'You must enter at least a title for the new poll.'));
				return false;
			}

			if (access_type === 'select') {
				if (   g_chosen_groups.length === 0 
				    && g_chosen_users === 0
				) {
					alert(t('polls', 'Please select at least one user or group!'));
					return false;
				}
				form.elements.accessValues.value = JSON.stringify({
					groups: g_chosen_groups,
					users: g_chosen_users
				});
			}
			form.elements.isAnonymous.value = isAnonymous;
			form.elements.hideNames.value = hideNames;
			form.submit();
		};
	}
});