Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/js/functions.js
blob: 6df60e2543fd4d8b062bd5ef9563186f90dda20c (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
// Set up the task list onclick handler
addEvent(window,'load',setUpTasklistTable);
function Disable(formid)
{
   document.formid.buSubmit.disabled = true;
   document.formid.submit();
}

function showstuff(boxid, type){
   if (!type) type = 'block';
   $(boxid).style.display= type;
   $(boxid).style.visibility='visible';
}

function hidestuff(boxid){
   $(boxid).style.display='none';
}

function hidestuff_e(e, boxid){
   e = e || window.event;
   if (Event.element(e).getAttribute('id') !== 'lastsearchlink' ||
	(Event.element(e).getAttribute('id') === 'lastsearchlink' && $('lastsearchlink').className == 'inactive')) {
	if (!Position.within($(boxid), Event.pointerX(e), Event.pointerY(e))) {
	   //Event.stop(e);
	   if (boxid === 'mysearches') {
		activelink('lastsearchlink');
	   }
	   $(boxid).style.visibility='hidden';
	   $(boxid).style.display='none';
	   document.onmouseup = null;
   	}
   }
}

function showhidestuff(boxid) {
   if (boxid === 'mysearches') {
	activelink('lastsearchlink');
   }
   switch ($(boxid).style.visibility) {
      case '':
	$(boxid).style.visibility='visible';
	break;
      case 'hidden':
	$(boxid).style.visibility='visible';
	break;
      case 'visible':
	$(boxid).style.visibility='hidden';
	break;
   }
   switch ($(boxid).style.display) {
      case '':
	$(boxid).style.display='block';
	document.onmouseup = function(e) { hidestuff_e(e, boxid); };
	break;
      case 'none':
	$(boxid).style.display='block';
	document.onmouseup = function(e) { hidestuff_e(e, boxid); };
	break;
      case 'block':
	$(boxid).style.display='none';
	document.onmouseup = null;
	break;
      case 'inline':
	$(boxid).style.display='none';
	document.onmouseup = null;
	break;
   }
}
function setUpTasklistTable() {
  if (!$('tasklist_table')) {
    // No tasklist on the page
    return;
  }
  var table = $('tasklist_table');
  // deactivated 201508: when users click on cells with property, IMHO it should go to property or filter list by property, not open task details view.
  //addEvent(table,'click',tasklistTableClick);
}
function tasklistTableClick(e) {
  var src = eventGetSrc(e);
  if (src.nodeName != 'TD') {
    return;
  }
  if (src.hasChildNodes()) {
    var checkBoxes = src.getElementsByTagName('input');
    if (checkBoxes.length > 0) {
      // User clicked the cell where the task select checkbox is
      if (checkBoxes[0].checked) {
        checkBoxes[0].checked = false;
      } else {
        checkBoxes[0].checked = true;
      }
      return;
    }
  }
  var row = src.parentNode;
  var aElements = row.getElementsByTagName('A');
  if (aElements.length > 0) {
    window.location = aElements[0].href;
  } else {
    // If both the task id and the task summary columns are non-visible
    // just use the good old way to get to the task
    window.location = '?do=details&task_id=' + row.id.substr(4);
  }
}

function eventGetSrc(e) {
  if (e.target) {
    return e.target;
  } else if (window.event) {
    return window.event.srcElement;
  } else {
    return;
  }
}

function ToggleSelected(id) {
  var inputs = $(id).getElementsByTagName('input');
  for (var i = 0; i < inputs.length; i++) {
    if(inputs[i].type == 'checkbox'){
      inputs[i].checked = !(inputs[i].checked);
    }
  }
}

function addUploadFields(id) {
  if (!id) {
    id = 'uploadfilebox';
  }
  var el = $(id);
  var span = el.getElementsByTagName('span')[0];
  if ('none' == span.style.display) {
    // Show the file upload box
    span.style.display = 'inline';
    // Switch the buttns
    $(id + '_attachafile').style.display = 'none';
    $(id + '_attachanotherfile').style.display = 'inline';

  } else {
    // Copy the first file upload box and clear it's value
    var newBox = span.cloneNode(true);
    newBox.getElementsByTagName('input')[0].value = '';
    el.appendChild(newBox);
  }
}

function addLinkField(id) {
     if(!id) {
	 id = 'addlinkbox';
     }
     var el = $(id);
     var span = el.getElementsByTagName('span')[0];
     if('none' == span.style.display) {

         span.style.display = 'inline';

         $(id + '_addalink').style.display = 'none';
	 $(id + '_addanotherlink').style.display = 'inline';
      } else {

        var newBox = span.cloneNode(true);
        newBox.getElementsByTagName('input')[0].value = '';
        el.appendChild(newBox);
      }
}

function checkok(url, message, form) {

    var myAjax = new Ajax.Request(url, {method: 'get', onComplete:function(originalRequest)
	{
        if(originalRequest.responseText == 'ok' || confirm(message)) {
            $(form).submit();
        }
	}});
    return false;
}

function removeUploadField(element, id) {
  if (!id) {
    id = 'uploadfilebox';
  }
  var el = $(id);
  var span = el.getElementsByTagName('span');
  if (1 == span.length) {
    // Clear and hide the box
    span[0].style.display='none';
    span[0].getElementsByTagName('input')[0].value = '';
    // Switch the buttons
    $(id + '_attachafile').style.display = 'inline';
    $(id + '_attachanotherfile').style.display = 'none';
  } else {
    el.removeChild(element.parentNode);
  }
}

function removeLinkField(element, id) {
    if(!id) {
        id = 'addlinkbox';
    }
    var el = $(id);
    var span = el.getElementsByTagName('span');
    if (1 == span.length) {
       span[0].style.display='none';
       span[0].getElementsByTagName('input')[0].value = '';

       $(id + '_addalink').style.display = 'inline';
       $(id + '_addanotherlink').style.display = 'none';
    } else {
       el.removeChild(element.parentNode);
    }
}

function updateDualSelectValue(id)
{
    var rt  = $('r'+id);
    var val = $('v'+id);
    val.value = '';

    var i;
    for (i=0; i < rt.options.length; i++) {
        val.value += (i > 0 ? ' ' : '') + rt.options[i].value;
    }
}
function dualSelect(from, to, id) {
    if (typeof(from) == 'string') {
	from = $(from+id);
    }
    if (typeof(to) == 'string') {
        var to_el = $(to+id);
	// if (!to_el) alert("no element with id '" + (to+id) + "'");
	to = to_el;
    }

    var i;
    var len = from.options.length;
    for(i=0;i<len;++i) {
	if (!from.options[i].selected) continue;
	if (to && to.options)
	    to.appendChild(from.options[i]);
	else
	    from.removeChild(from.options[i]);
	// make the option that is slid down selected (if any)
	if (len > 1)
	    from.options[i == len - 1 ? len - 2 : i].selected = true;
	break;
    }

    updateDualSelectValue(id);
}

function selectMove(id, step) {
    var sel = $('r'+id);

    var i = 0;

    while (i < sel.options.length) {
        if (sel.options[i].selected) {
            if (i+step < 0 || i+step >= sel.options.length) {
                return;
            }
	    if (i + step == sel.options.length - 1)
		sel.appendChild(sel.options[i]);
	    else if (step < 0)
		sel.insertBefore(sel.options[i], sel.options[i+step]);
	    else
		sel.insertBefore(sel.options[i], sel.options[i+step+1]);
            updateDualSelectValue(id);
            return;
        }
        i++;
    }
}
var Cookie = {
  getVar: function(name) {
    var cookie = document.cookie;
    if (cookie.length > 0) {
      cookie += ';';
    }
    re = new RegExp(name + '\=(.*?);' );
    if (cookie.match(re)) {
      return RegExp.$1;
    } else {
      return '';
    }
  },
  setVar: function(name,value,expire,path) {
    document.cookie = name + '=' + value;
  },
  removeVar: function(name) {
    var date = new Date(12);
    document.cookie = name + '=;expires=' + date.toUTCString();
  }
};

function deletesearch(id, url) {
    $('rs' + id).getElementsByTagName('i')[0].className='fa fa-spinner fa-spin fa-lg';
    url = url + 'js/callbacks/deletesearches.php';
    var myAjax = new Ajax.Request(url, {
    		method: 'post',
    		parameters: { 'id': id, 'csrftoken': document.getElementById('deletesearchtoken').value },
    		onSuccess:function()
                {
                        var oNodeToRemove = $('rs' + id);
                        oNodeToRemove.parentNode.removeChild(oNodeToRemove);
                        var table = $('mysearchestable');
                        if(table.rows.length > 0) {
                            table.getElementsByTagName('tr')[table.rows.length-1].style.borderBottom = '0';
                        } else {
                            showstuff('nosearches');
                        }
                }
        });
}
function savesearch(query, baseurl, savetext, csrftoken) {
    url = baseurl + 'js/callbacks/savesearches.php';
    if($('save_search').value != '') {
        var old_text = $('lblsaveas').firstChild.nodeValue;
        $('lblsaveas').firstChild.nodeValue = savetext;
        var myAjax = new Ajax.Request(url, {
        	method: 'post',
        	parameters: query + '&search_name=' + encodeURIComponent($('save_search').value) + '&csrftoken=' + csrftoken,
                onComplete:function()
                {
                        $('lblsaveas').firstChild.nodeValue=old_text;
                        var myAjax2 = new Ajax.Updater('mysearches', baseurl + 'js/callbacks/getsearches.php', { method: 'get'});
                     }
                });
    }
}
function activelink(id) {
    if($(id).className == 'active') {
        $(id).className = 'inactive';
    } else {
        $(id).className = 'active';
    }
}
var useAltForKeyboardNavigation = false;  // Set this to true if you don't want to kill
                                         // Firefox's find as you type

function emptyElement(el) {
    while(el.firstChild) {
        emptyElement(el.firstChild);
        var oNodeToRemove = el.firstChild;
        oNodeToRemove.parentNode.removeChild(oNodeToRemove);
    }
}
function showPreview(textfield, baseurl, field)
{
    var preview = $(field);
    emptyElement(preview);

    var img = document.createElement('i');
    //img.src = baseurl + 'themes/CleanFS/ajax_load.gif';
    img.className='fa fa-spinner fa-spin fa-lg'; // fontawesome animated fonticon
    img.id = 'temp_img';
    img.alt = 'Loading...';
    preview.appendChild(img);

    var text = $(textfield).value;
    text = encodeURIComponent(text);
    var url = baseurl + 'js/callbacks/getpreview.php';
    var myAjax = new Ajax.Updater(field, url, {parameters:'text=' + text, method: 'post'});

    if (text == '') {
        hidestuff(field);
    } else {
        showstuff(field);
    }
}
function checkname(value){
    var re=/^[A-Za-z0-9_\.\-]*$/;

    if (re.test(value)==false)
    {
        $('username').style.color ='red';
        $('buSubmit').style.visibility = 'hidden';
	// TODO an available translation array 
	// maybe provided by something like js/translations.php?lang=de
        $('errormessage').innerHTML = 'invalid username';
    }
    // Otherwise check if username already exists
    else
    {
      new Ajax.Request('js/callbacks/searchnames.php?name='+value, {onSuccess: function(t){ allow(t.responseText); } });
    }
}
function allow(booler){
    if(booler.indexOf('false') > -1) {
        $('username').style.color ='red';
        $('buSubmit').style.visibility = 'hidden';
	// text after 'false|'
        $('errormessage').innerHTML = booler.substring(6,booler.length);
    }
    else {
        $('username').style.color ='green';
        $('buSubmit').style.visibility = 'visible';
        $('errormessage').innerHTML = '';
    }
}
function getHistory(task_id, baseurl, field, details)
{
    var url = baseurl + 'js/callbacks/gethistory.php?task_id=' + task_id;
    if (details) {
        url += '&details=' + details;
    }
    var myAjax = new Ajax.Updater(field, url, { method: 'get'});
}

/*********  Permissions popup  ***********/

function createClosure(obj, method) {
    return (function() { obj[method](); });
}

function Perms(id) {
    this.div = $(id);
}

Perms.prototype.timeout = null;
Perms.prototype.div     = null;

Perms.prototype.clearTimeout = function() {
    if (this.timeout) {
        clearTimeout(this.timeout);
        this.timeout = null;
    }
}

Perms.prototype.do_later = function(action) {
    this.clearTimeout();
    closure = createClosure(this, action);
    this.timeout = setTimeout(closure, 400);
}

Perms.prototype.show = function() {
    this.clearTimeout();
    this.div.style.display = 'block';
    this.div.style.visibility = 'visible';
}

Perms.prototype.hide = function() {
    this.clearTimeout();
    this.div.style.display = 'none';
}

// Replaces the currently selected text with the passed text.
function replaceText(text, textarea)
{
	textarea = document.getElementById( textarea );
	// Attempt to create a text range (IE).
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	}
    else if (document.selection) {
        textarea.focus();
        sel=document.selection.createRange();
        sel.text=text;
    }
	// Just put it on the end.
	else
	{
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}


// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
	textarea = document.getElementById( textarea );
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		caretPos.select();
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
    else if(document.selection) {
        textarea.focus();
        var sampleText = 'TEXT';
        var currentRange = document.selection.createRange();
        var selection = currentRange.text;
        var replaced = true;
        if(!selection) {
               replaced=false;
               selection = sampleText;
        }
        if(selection.charAt(selection.length-1)==" "){
               selection=selection.substring(0,selection.length-1);
               currentRange.text = text1 + selection + text2 + " ";
        }
        else
        {
               currentRange.text = text1 + selection + text2;
        }
        if(!replaced){
               // If putting in sample text (i.e. insert) adjust range start and end
               currentRange.moveStart('character',-text.length-text2.length);
               currentRange.moveEnd('character',-text2.length);
        }
        currentRange.select();
    }
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

function stopBubble(e) {
	if (!e) { var e = window.event; }
	e.cancelBubble = true;
	if (e.stopPropagation) { e.stopPropagation(); }
}

//login box toggling
login_box_status = false;
var toggleLoginBox = function(el){
    //this would turn the box on
    if(login_box_status == false){
        el.addClassName('active');
        showstuff('loginbox');
    //this would turn the box off
    } else {
        el.removeClassName('active');
        hidestuff('loginbox');
    }
    //toggle functionality
    if(login_box_status == true) login_box_status = false;
    else login_box_status = true;
    //return false to stop event bubbling
    return false;
}