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