xref: /webtrees/resources/js/webtrees.js (revision 94026f200c17dcfccf296678dc90ea88b14f6246)
1/**
2 * webtrees: online genealogy
3 * Copyright (C) 2018 webtrees development team
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
16'use strict';
17
18function expand_layer(sid)
19{
20    $('#' + sid + '_img').toggleClass('icon-plus icon-minus');
21    $('#' + sid).slideToggle('fast');
22    $('#' + sid + '-alt').toggle(); // hide something when we show the layer - and vice-versa
23    return false;
24}
25
26// Accept the changes to a record - and reload the page
27function accept_changes(xref, ged)
28{
29    $.post(
30        'index.php',
31        {
32            route: 'accept-changes',
33            xref: xref,
34            ged: ged,
35        },
36        function () {
37            location.reload();
38        }
39    );
40    return false;
41}
42
43// Reject the changes to a record - and reload the page
44function reject_changes(xref, ged)
45{
46    $.post(
47        'index.php',
48        {
49            route: 'reject-changes',
50            xref: xref,
51            ged: ged,
52        },
53        function () {
54            location.reload();
55        }
56    );
57    return false;
58}
59
60// Delete a record - and reload the page
61function delete_record(xref, gedcom)
62{
63    $.post(
64        'index.php',
65        {
66            route: 'delete-record',
67            xref: xref,
68            ged: gedcom,
69        },
70        function () {
71            location.reload();
72        }
73    );
74
75    return false;
76}
77
78// Delete a fact - and reload the page
79function delete_fact(message, ged, xref, fact_id)
80{
81    if (confirm(message)) {
82        $.post(
83            'index.php',
84            {
85                route: 'delete-fact',
86                xref: xref,
87                fact_id: fact_id,
88                ged: ged
89            },
90            function () {
91                location.reload();
92            }
93        );
94    }
95    return false;
96}
97
98// Copy a fact to the clipboard
99function copy_fact(ged, xref, fact_id)
100{
101    $.post(
102        'index.php',
103        {
104            route: 'copy-fact',
105            xref: xref,
106            fact_id: fact_id,
107            ged: ged,
108        },
109        function () {
110            location.reload();
111        }
112    );
113    return false;
114}
115
116// Paste a fact from the clipboard
117function paste_fact(ged, xref, element)
118{
119    $.post(
120        'index.php',
121        {
122            route: 'paste-fact',
123            xref: xref,
124            fact_id: $(element).val(), // element is the <select> containing the option
125            ged: ged,
126        },
127        function () {
128            location.reload();
129        }
130    );
131    return false;
132}
133
134// Delete a user - and reload the page
135function delete_user(message, user_id)
136{
137    if (confirm(message)) {
138        $.post(
139            'index.php',
140            {
141                route: 'delete-user',
142                user_id: user_id,
143            },
144            function () {
145                location.reload();
146            }
147        );
148    }
149    return false;
150}
151
152// Masquerade as another user - and reload the page.
153function masquerade(user_id)
154{
155    $.post(
156        'index.php',
157        {
158            route: 'masquerade',
159            user_id: user_id,
160        },
161        function () {
162            location.reload();
163        }
164    );
165    return false;
166}
167
168var pastefield;
169function addmedia_links(field, iid, iname)
170{
171    pastefield = field;
172    insertRowToTable(iid, iname);
173    return false;
174}
175
176function valid_date(datefield, dmy)
177{
178    var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
179    var hijri_months = ['MUHAR', 'SAFAR', 'RABIA', 'RABIT', 'JUMAA', 'JUMAT', 'RAJAB', 'SHAAB', 'RAMAD', 'SHAWW', 'DHUAQ', 'DHUAH'];
180    var hebrew_months = ['TSH', 'CSH', 'KSL', 'TVT', 'SHV', 'ADR', 'ADS', 'NSN', 'IYR', 'SVN', 'TMZ', 'AAV', 'ELL'];
181    var french_months = ['VEND', 'BRUM', 'FRIM', 'NIVO', 'PLUV', 'VENT', 'GERM', 'FLOR', 'PRAI', 'MESS', 'THER', 'FRUC', 'COMP'];
182    var jalali_months = ['FARVA', 'ORDIB', 'KHORD', 'TIR', 'MORDA', 'SHAHR', 'MEHR', 'ABAN', 'AZAR', 'DEY', 'BAHMA', 'ESFAN'];
183
184    var datestr = datefield.value;
185  // if a date has a date phrase marked by () this has to be excluded from altering
186    var datearr = datestr.split('(');
187    var datephrase = '';
188    if (datearr.length > 1) {
189        datestr = datearr[0];
190        datephrase = datearr[1];
191    }
192
193  // Gedcom dates are upper case
194    datestr = datestr.toUpperCase();
195  // Gedcom dates have no leading/trailing/repeated whitespace
196    datestr = datestr.replace(/\s+/, ' ');
197    datestr = datestr.replace(/(^\s)|(\s$)/, '');
198  // Gedcom dates have spaces between letters and digits, e.g. "01JAN2000" => "01 JAN 2000"
199    datestr = datestr.replace(/(\d)([A-Z])/, '$1 $2');
200    datestr = datestr.replace(/([A-Z])(\d)/, '$1 $2');
201
202  // Shortcut for quarter format, "Q1 1900" => "BET JAN 1900 AND MAR 1900". See [ 1509083 ]
203    if (datestr.match(/^Q ([1-4]) (\d\d\d\d)$/)) {
204        datestr = 'BET ' + months[RegExp.$1 * 3 - 3] + ' ' + RegExp.$2 + ' AND ' + months[RegExp.$1 * 3 - 1] + ' ' + RegExp.$2;
205    }
206
207  // Shortcut for @#Dxxxxx@ 01 01 1400, etc.
208    if (datestr.match(/^(@#DHIJRI@|HIJRI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
209        datestr = '@#DHIJRI@' + RegExp.$2 + hijri_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
210    }
211    if (datestr.match(/^(@#DJALALI@|JALALI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
212        datestr = '@#DJALALI@' + RegExp.$2 + jalali_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
213    }
214    if (datestr.match(/^(@#DHEBREW@|HEBREW)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
215        datestr = '@#DHEBREW@' + RegExp.$2 + hebrew_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
216    }
217    if (datestr.match(/^(@#DFRENCH R@|FRENCH)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
218        datestr = '@#DFRENCH R@' + RegExp.$2 + french_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
219    }
220
221  // e.g. 17.11.1860, 03/04/2005 or 1999-12-31. Use locale settings where DMY order is ambiguous.
222    var qsearch = /^([^\d]*)(\d+)[^\d](\d+)[^\d](\d+)$/i;
223    if (qsearch.exec(datestr)) {
224        var f0 = RegExp.$1;
225        var f1 = parseInt(RegExp.$2, 10);
226        var f2 = parseInt(RegExp.$3, 10);
227        var f3 = parseInt(RegExp.$4, 10);
228        var yyyy = new Date().getFullYear();
229        var yy = yyyy % 100;
230        var cc = yyyy - yy;
231        if (dmy === 'DMY' && f1 <= 31 && f2 <= 12 || f1 > 13 && f1 <= 31 && f2 <= 12 && f3 > 31) {
232            datestr = f0 + f1 + ' ' + months[f2 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100));
233        } else {
234            if (dmy === 'MDY' && f1 <= 12 && f2 <= 31 || f2 > 13 && f2 <= 31 && f1 <= 12 && f3 > 31) {
235                datestr = f0 + f2 + ' ' + months[f1 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100));
236            } else {
237                if (dmy === 'YMD' && f2 <= 12 && f3 <= 31 || f3 > 13 && f3 <= 31 && f2 <= 12 && f1 > 31) {
238                    datestr = f0 + f3 + ' ' + months[f2 - 1] + ' ' + (f1 >= 100 ? f1 : (f1 <= yy ? f1 + cc : f1 + cc - 100));
239                }
240            }
241        }
242    }
243
244  // Shortcuts for date ranges
245    datestr = datestr.replace(/^[>]([\w ]+)$/, 'AFT $1');
246    datestr = datestr.replace(/^[<]([\w ]+)$/, 'BEF $1');
247    datestr = datestr.replace(/^([\w ]+)[-]$/, 'FROM $1');
248    datestr = datestr.replace(/^[-]([\w ]+)$/, 'TO $1');
249    datestr = datestr.replace(/^[~]([\w ]+)$/, 'ABT $1');
250    datestr = datestr.replace(/^[*]([\w ]+)$/, 'EST $1');
251    datestr = datestr.replace(/^[#]([\w ]+)$/, 'CAL $1');
252    datestr = datestr.replace(/^([\w ]+) ?- ?([\w ]+)$/, 'BET $1 AND $2');
253    datestr = datestr.replace(/^([\w ]+) ?~ ?([\w ]+)$/, 'FROM $1 TO $2');
254
255  // Convert full months to short months
256    datestr = datestr.replace(/(JANUARY)/, 'JAN');
257    datestr = datestr.replace(/(FEBRUARY)/, 'FEB');
258    datestr = datestr.replace(/(MARCH)/, 'MAR');
259    datestr = datestr.replace(/(APRIL)/, 'APR');
260    datestr = datestr.replace(/(MAY)/, 'MAY');
261    datestr = datestr.replace(/(JUNE)/, 'JUN');
262    datestr = datestr.replace(/(JULY)/, 'JUL');
263    datestr = datestr.replace(/(AUGUST)/, 'AUG');
264    datestr = datestr.replace(/(SEPTEMBER)/, 'SEP');
265    datestr = datestr.replace(/(OCTOBER)/, 'OCT');
266    datestr = datestr.replace(/(NOVEMBER)/, 'NOV');
267    datestr = datestr.replace(/(DECEMBER)/, 'DEC');
268
269  // Americans frequently enter dates as SEP 20, 1999
270  // No need to internationalise this, as this is an english-language issue
271    datestr = datestr.replace(/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\.? (\d\d?)[, ]+(\d\d\d\d)/, '$2 $1 $3');
272
273  // Apply leading zero to day numbers
274    datestr = datestr.replace(/(^| )(\d [A-Z]{3,5} \d{4})/, '$10$2');
275
276    if (datephrase) {
277        datestr = datestr + ' (' + datephrase;
278    }
279  // Only update it if is has been corrected - otherwise input focus
280  // moves to the end of the field unnecessarily
281    if (datefield.value !== datestr) {
282        datefield.value = datestr;
283    }
284}
285
286var menutimeouts = [];
287
288function show_submenu(elementid, parentid)
289{
290    var pagewidth = document.body.scrollWidth + document.documentElement.scrollLeft;
291    var element = document.getElementById(elementid);
292
293    if (element && element.style) {
294        if (document.all) {
295            pagewidth = document.body.offsetWidth;
296        } else {
297            pagewidth = document.body.scrollWidth + document.documentElement.scrollLeft - 55;
298            if (document.documentElement.dir === 'rtl') {
299                boxright = element.offsetLeft + element.offsetWidth + 10;
300            }
301        }
302
303      // -- make sure the submenu is the size of the largest child
304        var maxwidth = 0;
305        var count = element.childNodes.length;
306        for (var i = 0; i < count; i++) {
307            var child = element.childNodes[i];
308            if (child.offsetWidth > maxwidth + 5) {
309                maxwidth = child.offsetWidth;
310            }
311        }
312        if (element.offsetWidth < maxwidth) {
313            element.style.width = maxwidth + 'px';
314        }
315        var pelement, boxright;
316        pelement = document.getElementById(parentid);
317        if (pelement) {
318            element.style.left = pelement.style.left;
319            boxright = element.offsetLeft + element.offsetWidth + 10;
320            if (boxright > pagewidth) {
321                var menuleft = pagewidth - element.offsetWidth;
322                element.style.left = menuleft + 'px';
323            }
324        }
325
326        if (element.offsetLeft < 0) {
327            element.style.left = '0px';
328        }
329
330      // -- put scrollbars on really long menus
331        if (element.offsetHeight > 500) {
332            element.style.height = '400px';
333            element.style.overflow = 'auto';
334        }
335
336        element.style.visibility = 'visible';
337    }
338    clearTimeout(menutimeouts[elementid]);
339    menutimeouts[elementid] = null;
340}
341
342function hide_submenu(elementid)
343{
344    if (typeof menutimeouts[elementid] !== 'number') {
345        return;
346    }
347    var element = document.getElementById(elementid);
348    if (element && element.style) {
349        element.style.visibility = 'hidden';
350    }
351    clearTimeout(menutimeouts[elementid]);
352    menutimeouts[elementid] = null;
353}
354
355function timeout_submenu(elementid)
356{
357    if (typeof menutimeouts[elementid] !== 'number') {
358        menutimeouts[elementid] = setTimeout("hide_submenu('" + elementid + "')", 100);
359    }
360}
361
362var monthLabels = [];
363monthLabels[1] = 'January';
364monthLabels[2] = 'February';
365monthLabels[3] = 'March';
366monthLabels[4] = 'April';
367monthLabels[5] = 'May';
368monthLabels[6] = 'June';
369monthLabels[7] = 'July';
370monthLabels[8] = 'August';
371monthLabels[9] = 'September';
372monthLabels[10] = 'October';
373monthLabels[11] = 'November';
374monthLabels[12] = 'December';
375
376var monthShort = [];
377monthShort[1] = 'JAN';
378monthShort[2] = 'FEB';
379monthShort[3] = 'MAR';
380monthShort[4] = 'APR';
381monthShort[5] = 'MAY';
382monthShort[6] = 'JUN';
383monthShort[7] = 'JUL';
384monthShort[8] = 'AUG';
385monthShort[9] = 'SEP';
386monthShort[10] = 'OCT';
387monthShort[11] = 'NOV';
388monthShort[12] = 'DEC';
389
390var daysOfWeek = [];
391daysOfWeek[0] = 'S';
392daysOfWeek[1] = 'M';
393daysOfWeek[2] = 'T';
394daysOfWeek[3] = 'W';
395daysOfWeek[4] = 'T';
396daysOfWeek[5] = 'F';
397daysOfWeek[6] = 'S';
398
399var weekStart = 0;
400
401function cal_setMonthNames(jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
402{
403    monthLabels[1] = jan;
404    monthLabels[2] = feb;
405    monthLabels[3] = mar;
406    monthLabels[4] = apr;
407    monthLabels[5] = may;
408    monthLabels[6] = jun;
409    monthLabels[7] = jul;
410    monthLabels[8] = aug;
411    monthLabels[9] = sep;
412    monthLabels[10] = oct;
413    monthLabels[11] = nov;
414    monthLabels[12] = dec;
415}
416
417function cal_setDayHeaders(sun, mon, tue, wed, thu, fri, sat)
418{
419    daysOfWeek[0] = sun;
420    daysOfWeek[1] = mon;
421    daysOfWeek[2] = tue;
422    daysOfWeek[3] = wed;
423    daysOfWeek[4] = thu;
424    daysOfWeek[5] = fri;
425    daysOfWeek[6] = sat;
426}
427
428function cal_setWeekStart(day)
429{
430    if (day >= 0 && day < 7) {
431        weekStart = day;
432    }
433}
434
435function calendarWidget(dateDivId, dateFieldId)
436{
437    var dateDiv = document.getElementById(dateDivId);
438    var dateField = document.getElementById(dateFieldId);
439
440    if (dateDiv.style.visibility === 'visible') {
441        dateDiv.style.visibility = 'hidden';
442        return false;
443    }
444    if (dateDiv.style.visibility === 'show') {
445        dateDiv.style.visibility = 'hide';
446        return false;
447    }
448
449  /* Javascript calendar functions only work with precise gregorian dates "D M Y" or "Y" */
450    var greg_regex = /((\d+ (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) )?\d+)/i;
451    var date;
452    if (greg_regex.exec(dateField.value)) {
453        date = new Date(RegExp.$1);
454    } else {
455        date = new Date();
456    }
457
458    dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date);
459    if (dateDiv.style.visibility === 'hidden') {
460        dateDiv.style.visibility = 'visible';
461        return false;
462    }
463    if (dateDiv.style.visibility === 'hide') {
464        dateDiv.style.visibility = 'show';
465        return false;
466    }
467
468    return false;
469}
470
471function cal_generateSelectorContent(dateFieldId, dateDivId, date)
472{
473    var i, j;
474    var content = '<table border="1"><tr>';
475    content += '<td><select class="form-control" id="' + dateFieldId + '_daySelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">';
476    for (i = 1; i < 32; i++) {
477        content += '<option value="' + i + '"';
478        if (date.getDate() === i) {
479            content += ' selected="selected"';
480        }
481        content += '>' + i + '</option>';
482    }
483    content += '</select></td>';
484    content += '<td><select class="form-control" id="' + dateFieldId + '_monSelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">';
485    for (i = 1; i < 13; i++) {
486        content += '<option value="' + i + '"';
487        if (date.getMonth() + 1 === i) {
488            content += ' selected="selected"';
489        }
490        content += '>' + monthLabels[i] + '</option>';
491    }
492    content += '</select></td>';
493    content += '<td><input class="form-control" type="text" id="' + dateFieldId + '_yearInput" size="5" value="' + date.getFullYear() + '" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');" /></td></tr>';
494    content += '<tr><td colspan="3">';
495    content += '<table width="100%">';
496    content += '<tr>';
497    j = weekStart;
498    for (i = 0; i < 7; i++) {
499        content += '<td ';
500        content += 'class="descriptionbox"';
501        content += '>';
502        content += daysOfWeek[j];
503        content += '</td>';
504        j++;
505        if (j > 6) {
506            j = 0;
507        }
508    }
509    content += '</tr>';
510
511    var tdate = new Date(date.getFullYear(), date.getMonth(), 1);
512    var day = tdate.getDay();
513    day = day - weekStart;
514    var daymilli = 1000 * 60 * 60 * 24;
515    tdate = tdate.getTime() - (day * daymilli) + (daymilli / 2);
516    tdate = new Date(tdate);
517
518    for (j = 0; j < 6; j++) {
519        content += '<tr>';
520        for (i = 0; i < 7; i++) {
521            content += '<td ';
522            if (tdate.getMonth() === date.getMonth()) {
523                if (tdate.getDate() === date.getDate()) {
524                    content += 'class="descriptionbox"';
525                } else {
526                    content += 'class="optionbox"';
527                }
528            } else {
529                content += 'style="background-color:#EAEAEA; border: solid #AAAAAA 1px;"';
530            }
531            content += '><a href="#" onclick="return cal_dateClicked(\'' + dateFieldId + '\', \'' + dateDivId + '\', ' + tdate.getFullYear() + ', ' + tdate.getMonth() + ', ' + tdate.getDate() + ');">';
532            content += tdate.getDate();
533            content += '</a></td>';
534            var datemilli = tdate.getTime() + daymilli;
535            tdate = new Date(datemilli);
536        }
537        content += '</tr>';
538    }
539    content += '</table>';
540    content += '</td></tr>';
541    content += '</table>';
542
543    return content;
544}
545
546function cal_setDateField(dateFieldId, year, month, day)
547{
548    var dateField = document.getElementById(dateFieldId);
549    if (!dateField) {
550        return false;
551    }
552    if (day < 10) {
553        day = '0' + day;
554    }
555    dateField.value = day + ' ' + monthShort[month + 1] + ' ' + year;
556    return false;
557}
558
559function cal_updateCalendar(dateFieldId, dateDivId)
560{
561    var dateSel = document.getElementById(dateFieldId + '_daySelect');
562    if (!dateSel) {
563        return false;
564    }
565    var monthSel = document.getElementById(dateFieldId + '_monSelect');
566    if (!monthSel) {
567        return false;
568    }
569    var yearInput = document.getElementById(dateFieldId + '_yearInput');
570    if (!yearInput) {
571        return false;
572    }
573
574    var month = parseInt(monthSel.options[monthSel.selectedIndex].value, 10);
575    month = month - 1;
576
577    var date = new Date(yearInput.value, month, dateSel.options[dateSel.selectedIndex].value);
578    cal_setDateField(dateFieldId, date.getFullYear(), date.getMonth(), date.getDate());
579
580    var dateDiv = document.getElementById(dateDivId);
581    if (!dateDiv) {
582        alert('no dateDiv ' + dateDivId);
583        return false;
584    }
585    dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date);
586
587    return false;
588}
589
590function cal_dateClicked(dateFieldId, dateDivId, year, month, day)
591{
592    cal_setDateField(dateFieldId, year, month, day);
593    calendarWidget(dateDivId, dateFieldId);
594    return false;
595}
596
597function openerpasteid(id)
598{
599    if (window.opener.paste_id) {
600        window.opener.paste_id(id);
601    }
602    window.close();
603}
604
605function paste_id(value)
606{
607    pastefield.value = value;
608}
609
610function pastename(name)
611{
612    if (nameElement) {
613        nameElement.innerHTML = name;
614    }
615    if (remElement) {
616        remElement.style.display = 'block';
617    }
618}
619
620function paste_char(value)
621{
622    if (document.selection) {
623      // IE
624        pastefield.focus();
625        document.selection.createRange().text = value;
626    } else if (pastefield.selectionStart || pastefield.selectionStart === 0) {
627      // Mozilla/Chrome/Safari
628        pastefield.value =
629        pastefield.value.substring(0, pastefield.selectionStart) +
630        value +
631        pastefield.value.substring(pastefield.selectionEnd, pastefield.value.length);
632        pastefield.selectionStart = pastefield.selectionEnd = pastefield.selectionStart + value.length;
633    } else {
634      // Fallback? - just append
635        pastefield.value += value;
636    }
637
638    if (pastefield.id === 'NPFX' || pastefield.id === 'GIVN' || pastefield.id === 'SPFX' || pastefield.id === 'SURN' || pastefield.id === 'NSFX') {
639        updatewholename();
640    }
641}
642
643/**
644 * Persistant checkbox options to hide/show extra data.
645
646 * @param element_id
647 */
648function persistent_toggle(element_id)
649{
650    let element = document.getElementById(element_id);
651    let key     = 'state-of-' + element_id;
652    let state   = localStorage.getItem(key);
653
654    // Previously selected?
655    if (state === 'true') {
656        $(element).click();
657    }
658
659    // Remember state for the next page load.
660    $(element).on('change', function() { localStorage.setItem(key, element.checked); });
661}
662
663function valid_lati_long(field, pos, neg)
664{
665  // valid LATI or LONG according to Gedcom standard
666  // pos (+) : N or E
667  // neg (-) : S or W
668    var txt = field.value.toUpperCase();
669    txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim
670    txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34
671    txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234
672    txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698
673    txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698
674  // 0°34'11 ==> 0:34:11
675    txt = txt.replace(/\u00b0/g, ':'); // °
676    txt = txt.replace(/\u0027/g, ':'); // '
677  // 0:34:11.2W ==> W0.5698
678    txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) {
679        var n = parseFloat($1);
680        n += ($2 / 60);
681        n += ($3 / 3600);
682        n = Math.round(n * 1E4) / 1E4;
683        return $4 + n;
684    });
685  // 0:34W ==> W0.5667
686    txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) {
687        var n = parseFloat($1);
688        n += ($2 / 60);
689        n = Math.round(n * 1E4) / 1E4;
690        return $3 + n;
691    });
692  // 0.5698W ==> W0.5698
693    txt = txt.replace(/(.*)([N|S|E|W]+)$/g, '$2$1');
694  // 17.1234 ==> N17.1234
695    if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) {
696        txt = pos + txt;
697    }
698    field.value = txt;
699}
700
701// This is the default way for webtrees to show image galleries.
702// Custom themes may use a different viewer.
703function activate_colorbox(config)
704{
705    $.extend($.colorbox.settings, {
706      // Don't scroll window with document
707        fixed: true,
708        current: '',
709        previous: '\uf048',
710        next: '\uf051',
711        slideshowStart: '\uf04b',
712        slideshowStop: '\uf04c',
713        close: '\uf00d'
714    });
715    if (config) {
716        $.extend($.colorbox.settings, config);
717    }
718
719  // Trigger an event when we click on an (any) image
720    $('body').on('click', 'a.gallery', function () {
721      // Enable colorbox for images
722        $('a[type^=image].gallery').colorbox({
723            photo: true,
724            maxWidth: '95%',
725            maxHeight: '95%',
726            rel: 'gallery', // Turn all images on the page into a slideshow
727            slideshow: true,
728            slideshowAuto: false,
729          // Add wheelzoom to the displayed image
730            onComplete: function () {
731              // Disable click on image triggering next image
732              // https://github.com/jackmoore/colorbox/issues/668
733                $('.cboxPhoto').unbind('click');
734
735                wheelzoom(document.querySelectorAll('.cboxPhoto'));
736            }
737        });
738
739      // Enable colorbox for audio using <audio></audio>, where supported
740      // $('html.video a[type^=video].gallery').colorbox({
741      //  rel:         'nofollow' // Slideshows are just for images
742      // });
743
744      // Enable colorbox for video using <video></video>, where supported
745      // $('html.audio a[type^=audio].gallery').colorbox({
746      //  rel:         'nofollow', // Slideshows are just for images
747      // });
748
749      // Allow all other media types remain as download links
750    });
751}
752
753// Initialize autocomplete elements.
754function autocomplete(selector)
755{
756  // Use typeahead/bloodhound for autocomplete
757    $(selector).each(function () {
758        $(this).typeahead(null, {
759            display: 'value',
760            source: new Bloodhound({
761                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
762                queryTokenizer: Bloodhound.tokenizers.whitespace,
763                remote: {
764                    url: this.dataset.autocompleteUrl,
765                    wildcard: 'QUERY'
766                }
767            })
768        });
769    });
770}
771
772/* Show / Hide event data for boxes used on charts and elsewhere */
773$('body').on('click', '.iconz', function (e) {
774    'use strict';
775    e.stopPropagation();
776
777    var wrapper = $(this).closest('.person_box_template'),
778    inout = wrapper.find('.inout'),
779    inout2 = wrapper.find('.inout2'),
780    namedef = wrapper.find('.namedef'),
781    basestyle = wrapper.attr('class').match(/(box-style[0-2])/)[1];
782
783    function showDetails()
784    {
785        wrapper.parent().css('z-index', 100);
786        toggleExpanded();
787        namedef.addClass('nameZoom');
788        inout2.hide(0, function () {
789            inout.slideDown();
790        });
791    }
792
793    function hideDetails()
794    {
795        inout.slideUp(function () {
796            inout2.show(0);
797            namedef.removeClass('nameZoom');
798            toggleExpanded();
799            wrapper.parent().css('z-index', '');
800        });
801    }
802
803    function toggleExpanded()
804    {
805        wrapper.toggleClass(function () {
806            return basestyle + ' ' + basestyle + '-expanded';
807        });
808    }
809
810    if (!inout.text().length) {
811        wrapper.css('cursor', 'progress');
812        inout.load('index.php', {route: 'expand-chart-box', xref: wrapper.data('xref'), ged: wrapper.data('tree')}, function () {
813            wrapper.css('cursor', '');
814            showDetails();
815        });
816    } else {
817        if (wrapper.hasClass(basestyle)) {
818            showDetails();
819        } else {
820            hideDetails();
821        }
822    }
823    wrapper.find('.iconz').toggleClass('icon-zoomin icon-zoomout');
824});
825
826/**
827 * Insert text at the current cursor position in an input field.
828 *
829 * @param e The input element.
830 * @param t The text to insert.
831 */
832function insertTextAtCursor(e, t)
833{
834    var scrollTop = e.scrollTop;
835    var selectionStart = e.selectionStart;
836    var prefix = e.value.substring(0, selectionStart);
837    var suffix = e.value.substring(e.selectionEnd, e.value.length);
838    e.value = prefix + t + suffix;
839    e.selectionStart = selectionStart + t.length;
840    e.selectionEnd = e.selectionStart;
841    e.focus();
842    e.scrollTop = scrollTop;
843}
844
845// Send the CSRF token on all AJAX requests
846$.ajaxSetup({
847    headers: {
848        'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content')
849    }
850});
851
852// Initialisation
853$(function () {
854  // Page elements that load automaticaly via AJAX.
855  // This prevents bad robots from crawling resource-intensive pages.
856    $("[data-ajax-url]").each(function () {
857        $(this).load($(this).data('ajaxUrl'));
858    });
859
860  // Select2 - format entries in the select list
861    function templateOptionForSelect2(data)
862    {
863        if (data.loading) {
864          // If we're waiting for the server, this will be a "waiting..." message
865            return data.text;
866        } else {
867          // The response from the server is already in HTML, so no need to format it here.
868            return data.text;
869        }
870    }
871
872  // Autocomplete
873    autocomplete('input[data-autocomplete-url]');
874
875  // Select2 - activate autocomplete fields
876    $('select.select2').select2({
877      // Do not escape.
878        escapeMarkup: function (x) {
879            return x }
880      // Same formatting for both selections and rsult
881      //templateResult: templateOptionForSelect2,
882      //templateSelection: templateOptionForSelect2
883    })
884  // If we clear the select (using the "X" button), we need an empty
885  // value (rather than no value at all) for inputs with name="array[]"
886    .on('select2:unselect', function (evt) {
887        $(evt.delegateTarget).append('<option value="" selected="selected"></option>');
888    })
889
890  // Datatables - locale aware sorting
891    $.fn.dataTableExt.oSort['text-asc'] = function (x, y) {
892        return x.localeCompare(y, document.documentElement.lang, {'sensitivity': 'base'});
893    };
894    $.fn.dataTableExt.oSort['text-desc'] = function (x, y) {
895        return y.localeCompare(x, document.documentElement.lang, {'sensitivity': 'base'});
896    };
897
898  // DataTables - start hidden to prevent FOUC.
899    $('table.datatables').each(function () {
900        $(this).DataTable(); $(this).removeClass('d-none'); });
901
902  // Create a new record while editing an existing one.
903  // Paste the XREF and description into the Select2 element.
904    $('.wt-modal-create-record').on('show.bs.modal', function (event) {
905      // Find the element ID that needs to be updated with the new value.
906        $('form', $(this)).data('element-id', $(event.relatedTarget).data('element-id'));
907        $('form .form-group input:first', $(this)).focus();
908    });
909
910  // Submit the modal form using AJAX, and paste the returned record ID/NAME into the parent form.
911    $('.wt-modal-create-record form').on('submit', function (event) {
912        event.preventDefault();
913        var elementId = $(this).data('element-id');
914        $.ajax({
915            url: 'index.php',
916            type: 'POST',
917            data: new FormData(this),
918            async: false,
919            cache: false,
920            contentType: false,
921            processData: false,
922            success: function (data) {
923                $('#' + elementId).select2().empty().append(new Option(data.text, data.id)).val(data.id).trigger('change');
924            },
925            failure: function (data) {
926                alert(data.error_message);
927            }
928        });
929      // Clear the form
930        this.reset();
931      // Close the modal
932        $(this).closest('.wt-modal-create-record').modal('hide');
933    });
934
935  // Activate the langauge selection menu.
936    $('.menu-language').on('click', '[data-language]', function () {
937        $.post('index.php', {
938            route: 'language',
939            language: $(this).data('language')
940        }, function () {
941            window.location.reload();
942        });
943
944        return false;
945    });
946
947  // Activate the theme selection menu.
948    $('.menu-theme').on('click', '[data-theme]', function () {
949        $.post('index.php', {
950            route: 'theme',
951            theme: $(this).data('theme')
952        }, function () {
953            window.location.reload();
954        });
955
956        return false;
957    });
958
959  // Activate the on-screen keyboard
960    var osk_focus_element;
961    $('.wt-osk-trigger').click(function () {
962      // When a user clicks the icon, set focus to the corresponding input
963        osk_focus_element = document.getElementById($(this).data('id'));
964        osk_focus_element.focus();
965        $('.wt-osk').show();
966
967    });
968
969    $('.wt-osk-script-button').change(function () {
970        $('.wt-osk-script').prop('hidden', true);
971        $('.wt-osk-script-' + $(this).data('script')).prop('hidden', false);
972    });
973    $('.wt-osk-shift-button').click(function () {
974        document.querySelector('.wt-osk-keys').classList.toggle('shifted');
975    });
976    $('.wt-osk-keys').on('click', '.wt-osk-key', function () {
977        var key = $(this).contents().get(0).nodeValue;
978        var shift_state = $('.wt-osk-shift-button').hasClass('active');
979        var shift_key = $('sup', this)[0];
980        if (shift_state && shift_key !== undefined) {
981            key = shift_key.innerText;
982        }
983        if (osk_focus_element !== null) {
984            var cursorPos = osk_focus_element.selectionStart;
985            var v = osk_focus_element.value;
986            var textBefore = v.substring(0,  cursorPos);
987            var textAfter  = v.substring(cursorPos, v.length);
988            osk_focus_element.value = textBefore + key + textAfter;
989            if ($('.wt-osk-pin-button').hasClass('active') === false) {
990                $('.wt-osk').hide();
991            }
992        }
993    });
994
995    $('.wt-osk-close').on('click', function () {
996        $('.wt-osk').hide();
997    });
998});
999