xref: /webtrees/resources/js/webtrees.js (revision 0d2905f717f42d085519a491b9ee6dccb5cdc2a4)
171239cb6SGreg Roach/**
271239cb6SGreg Roach * webtrees: online genealogy
3242a7862SGreg Roach * Copyright (C) 2019 webtrees development team
471239cb6SGreg Roach * This program is free software: you can redistribute it and/or modify
571239cb6SGreg Roach * it under the terms of the GNU General Public License as published by
671239cb6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
771239cb6SGreg Roach * (at your option) any later version.
871239cb6SGreg Roach * This program is distributed in the hope that it will be useful,
971239cb6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1071239cb6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1171239cb6SGreg Roach * GNU General Public License for more details.
1271239cb6SGreg Roach * You should have received a copy of the GNU General Public License
1371239cb6SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1471239cb6SGreg Roach */
1571239cb6SGreg Roach
16*0d2905f7SGreg Roach"use strict";
1771239cb6SGreg Roach
18*0d2905f7SGreg Roach(function(webtrees) {
1959e18f0cSGreg Roach    const lang = document.documentElement.lang;
2059e18f0cSGreg Roach
21*0d2905f7SGreg Roach    // Identify the script used by some text.
22*0d2905f7SGreg Roach    const scriptRegexes = {
23*0d2905f7SGreg Roach        Han:  /[\u3400-\u9FCC]/,
24*0d2905f7SGreg Roach        Grek: /[\u0370-\u03FF]/,
25*0d2905f7SGreg Roach        Cyrl: /[\u0400-\u04FF]/,
26*0d2905f7SGreg Roach        Hebr: /[\u0590-\u05FF]/,
27*0d2905f7SGreg Roach        Arab: /[\u0600-\u06FF]/,
28*0d2905f7SGreg Roach    };
29*0d2905f7SGreg Roach
3059e18f0cSGreg Roach    /**
3159e18f0cSGreg Roach     * Tidy the whitespace in a string.
3259e18f0cSGreg Roach     */
3359e18f0cSGreg Roach    function trim(str) {
3459e18f0cSGreg Roach        return str.replace(/\s+/g, " ").trim();
3559e18f0cSGreg Roach
3659e18f0cSGreg Roach    }
3759e18f0cSGreg Roach
3859e18f0cSGreg Roach    /**
3959e18f0cSGreg Roach     * Look for non-latin characters in a string.
4059e18f0cSGreg Roach     */
41*0d2905f7SGreg Roach    webtrees.detectScript = function (str) {
42*0d2905f7SGreg Roach        for(const script in scriptRegexes) {
43*0d2905f7SGreg Roach            if (str.match(scriptRegexes[script])) {
44*0d2905f7SGreg Roach                return script;
45*0d2905f7SGreg Roach            }
4659e18f0cSGreg Roach        }
4759e18f0cSGreg Roach
48*0d2905f7SGreg Roach        return "Latn";
49*0d2905f7SGreg Roach    };
5059e18f0cSGreg Roach
5159e18f0cSGreg Roach    /**
5259e18f0cSGreg Roach     * In some languages, the SURN uses a male/default form, but NAME uses a gender-inflected form.
5359e18f0cSGreg Roach     */
5459e18f0cSGreg Roach    function inflectSurname(surname, sex) {
5559e18f0cSGreg Roach        if (lang === "pl" && sex === "F") {
5659e18f0cSGreg Roach            return surname
5759e18f0cSGreg Roach                .replace(/ski$/, "ska")
5859e18f0cSGreg Roach                .replace(/cki$/, "cka")
5959e18f0cSGreg Roach                .replace(/dzki$/, "dzka")
6059e18f0cSGreg Roach                .replace(/żki$/, "żka");
6159e18f0cSGreg Roach        }
6259e18f0cSGreg Roach
6359e18f0cSGreg Roach        return surname;
6459e18f0cSGreg Roach    }
6559e18f0cSGreg Roach
6659e18f0cSGreg Roach    /**
6759e18f0cSGreg Roach     * Build a NAME from a NPFX, GIVN, SPFX, SURN and NSFX parts.
6859e18f0cSGreg Roach     *
6959e18f0cSGreg Roach     * Assumes the language of the document is the same as the language of the name.
7059e18f0cSGreg Roach     */
71*0d2905f7SGreg Roach    webtrees.buildNameFromParts = function (npfx, givn, spfx, surn, nsfx, sex) {
72*0d2905f7SGreg Roach        const usesCJK      = webtrees.detectScript(npfx + givn + spfx + givn + surn + nsfx) === "Han";
7359e18f0cSGreg Roach        const separator    = usesCJK ? "" : " ";
7459e18f0cSGreg Roach        const surnameFirst = usesCJK || ['hu', 'jp', 'ko', 'vi', 'zh-Hans', 'zh-Hant'].indexOf(lang) !== -1;
7559e18f0cSGreg Roach        const patronym     = ['is'].indexOf(lang) !== -1;
7659e18f0cSGreg Roach        const slash        = patronym ? "" : "/";
7759e18f0cSGreg Roach
7859e18f0cSGreg Roach        // GIVN and SURN may be a comma-separated lists.
7959e18f0cSGreg Roach        npfx = trim(npfx);
8059e18f0cSGreg Roach        givn = trim(givn.replace(",", separator));
8159e18f0cSGreg Roach        spfx = trim(spfx);
8259e18f0cSGreg Roach        surn = inflectSurname(trim(surn.replace(",", separator)), sex);
8359e18f0cSGreg Roach        nsfx = trim(nsfx);
8459e18f0cSGreg Roach
8559e18f0cSGreg Roach        const surname = trim(spfx + separator + surn);
8659e18f0cSGreg Roach
8759e18f0cSGreg Roach        const name = surnameFirst ? slash + surname + slash + separator + givn : givn + separator + slash + surname + slash;
8859e18f0cSGreg Roach
8959e18f0cSGreg Roach        return trim(npfx + separator + name + separator + nsfx);
90*0d2905f7SGreg Roach    };
91*0d2905f7SGreg Roach
92*0d2905f7SGreg Roach    // Insert text at the current cursor position in a text field.
93*0d2905f7SGreg Roach    webtrees.pasteAtCursor = function (element, text) {
94*0d2905f7SGreg Roach        if (element !== null) {
95*0d2905f7SGreg Roach            const caret_pos  = element.selectionStart + text.length;
96*0d2905f7SGreg Roach            const textBefore = element.value.substring(0, element.selectionStart);
97*0d2905f7SGreg Roach            const textAfter  = element.value.substring(element.selectionEnd);
98*0d2905f7SGreg Roach            element.value    = textBefore + text + textAfter;
99*0d2905f7SGreg Roach            element.setSelectionRange(caret_pos, caret_pos);
100*0d2905f7SGreg Roach            element.focus();
10159e18f0cSGreg Roach        }
10259e18f0cSGreg Roach
103*0d2905f7SGreg Roach    }
104*0d2905f7SGreg Roach}(window.webtrees = window.webtrees || {}));
10559e18f0cSGreg Roach
10671239cb6SGreg Roachfunction expand_layer(sid)
10771239cb6SGreg Roach{
10871239cb6SGreg Roach    $('#' + sid + '_img').toggleClass('icon-plus icon-minus');
10971239cb6SGreg Roach    $('#' + sid).slideToggle('fast');
11071239cb6SGreg Roach    $('#' + sid + '-alt').toggle(); // hide something when we show the layer - and vice-versa
11171239cb6SGreg Roach    return false;
11271239cb6SGreg Roach}
11371239cb6SGreg Roach
11471239cb6SGreg Roachvar pastefield;
11571239cb6SGreg Roach
11671239cb6SGreg Roachfunction valid_date(datefield, dmy)
11771239cb6SGreg Roach{
11871239cb6SGreg Roach    var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
11971239cb6SGreg Roach    var hijri_months = ['MUHAR', 'SAFAR', 'RABIA', 'RABIT', 'JUMAA', 'JUMAT', 'RAJAB', 'SHAAB', 'RAMAD', 'SHAWW', 'DHUAQ', 'DHUAH'];
12071239cb6SGreg Roach    var hebrew_months = ['TSH', 'CSH', 'KSL', 'TVT', 'SHV', 'ADR', 'ADS', 'NSN', 'IYR', 'SVN', 'TMZ', 'AAV', 'ELL'];
12171239cb6SGreg Roach    var french_months = ['VEND', 'BRUM', 'FRIM', 'NIVO', 'PLUV', 'VENT', 'GERM', 'FLOR', 'PRAI', 'MESS', 'THER', 'FRUC', 'COMP'];
12271239cb6SGreg Roach    var jalali_months = ['FARVA', 'ORDIB', 'KHORD', 'TIR', 'MORDA', 'SHAHR', 'MEHR', 'ABAN', 'AZAR', 'DEY', 'BAHMA', 'ESFAN'];
12371239cb6SGreg Roach
12471239cb6SGreg Roach    var datestr = datefield.value;
12571239cb6SGreg Roach  // if a date has a date phrase marked by () this has to be excluded from altering
12671239cb6SGreg Roach    var datearr = datestr.split('(');
12771239cb6SGreg Roach    var datephrase = '';
12871239cb6SGreg Roach    if (datearr.length > 1) {
12971239cb6SGreg Roach        datestr = datearr[0];
13071239cb6SGreg Roach        datephrase = datearr[1];
13171239cb6SGreg Roach    }
13271239cb6SGreg Roach
13371239cb6SGreg Roach  // Gedcom dates are upper case
13471239cb6SGreg Roach    datestr = datestr.toUpperCase();
13571239cb6SGreg Roach  // Gedcom dates have no leading/trailing/repeated whitespace
13671239cb6SGreg Roach    datestr = datestr.replace(/\s+/, ' ');
13771239cb6SGreg Roach    datestr = datestr.replace(/(^\s)|(\s$)/, '');
13871239cb6SGreg Roach  // Gedcom dates have spaces between letters and digits, e.g. "01JAN2000" => "01 JAN 2000"
13971239cb6SGreg Roach    datestr = datestr.replace(/(\d)([A-Z])/, '$1 $2');
14071239cb6SGreg Roach    datestr = datestr.replace(/([A-Z])(\d)/, '$1 $2');
14171239cb6SGreg Roach
14271239cb6SGreg Roach  // Shortcut for quarter format, "Q1 1900" => "BET JAN 1900 AND MAR 1900". See [ 1509083 ]
14371239cb6SGreg Roach    if (datestr.match(/^Q ([1-4]) (\d\d\d\d)$/)) {
14471239cb6SGreg Roach        datestr = 'BET ' + months[RegExp.$1 * 3 - 3] + ' ' + RegExp.$2 + ' AND ' + months[RegExp.$1 * 3 - 1] + ' ' + RegExp.$2;
14571239cb6SGreg Roach    }
14671239cb6SGreg Roach
14771239cb6SGreg Roach  // Shortcut for @#Dxxxxx@ 01 01 1400, etc.
14871239cb6SGreg Roach    if (datestr.match(/^(@#DHIJRI@|HIJRI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
14971239cb6SGreg Roach        datestr = '@#DHIJRI@' + RegExp.$2 + hijri_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
15071239cb6SGreg Roach    }
15171239cb6SGreg Roach    if (datestr.match(/^(@#DJALALI@|JALALI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
15271239cb6SGreg Roach        datestr = '@#DJALALI@' + RegExp.$2 + jalali_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
15371239cb6SGreg Roach    }
15471239cb6SGreg Roach    if (datestr.match(/^(@#DHEBREW@|HEBREW)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
15571239cb6SGreg Roach        datestr = '@#DHEBREW@' + RegExp.$2 + hebrew_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
15671239cb6SGreg Roach    }
15771239cb6SGreg Roach    if (datestr.match(/^(@#DFRENCH R@|FRENCH)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
15871239cb6SGreg Roach        datestr = '@#DFRENCH R@' + RegExp.$2 + french_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
15971239cb6SGreg Roach    }
16071239cb6SGreg Roach
16171239cb6SGreg Roach  // e.g. 17.11.1860, 03/04/2005 or 1999-12-31. Use locale settings where DMY order is ambiguous.
16271239cb6SGreg Roach    var qsearch = /^([^\d]*)(\d+)[^\d](\d+)[^\d](\d+)$/i;
16371239cb6SGreg Roach    if (qsearch.exec(datestr)) {
16471239cb6SGreg Roach        var f0 = RegExp.$1;
16571239cb6SGreg Roach        var f1 = parseInt(RegExp.$2, 10);
16671239cb6SGreg Roach        var f2 = parseInt(RegExp.$3, 10);
16771239cb6SGreg Roach        var f3 = parseInt(RegExp.$4, 10);
16871239cb6SGreg Roach        var yyyy = new Date().getFullYear();
16971239cb6SGreg Roach        var yy = yyyy % 100;
17071239cb6SGreg Roach        var cc = yyyy - yy;
17171239cb6SGreg Roach        if (dmy === 'DMY' && f1 <= 31 && f2 <= 12 || f1 > 13 && f1 <= 31 && f2 <= 12 && f3 > 31) {
17271239cb6SGreg Roach            datestr = f0 + f1 + ' ' + months[f2 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100));
17371239cb6SGreg Roach        } else {
17471239cb6SGreg Roach            if (dmy === 'MDY' && f1 <= 12 && f2 <= 31 || f2 > 13 && f2 <= 31 && f1 <= 12 && f3 > 31) {
17571239cb6SGreg Roach                datestr = f0 + f2 + ' ' + months[f1 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100));
17671239cb6SGreg Roach            } else {
17771239cb6SGreg Roach                if (dmy === 'YMD' && f2 <= 12 && f3 <= 31 || f3 > 13 && f3 <= 31 && f2 <= 12 && f1 > 31) {
17871239cb6SGreg Roach                    datestr = f0 + f3 + ' ' + months[f2 - 1] + ' ' + (f1 >= 100 ? f1 : (f1 <= yy ? f1 + cc : f1 + cc - 100));
17971239cb6SGreg Roach                }
18071239cb6SGreg Roach            }
18171239cb6SGreg Roach        }
18271239cb6SGreg Roach    }
18371239cb6SGreg Roach
18471239cb6SGreg Roach  // Shortcuts for date ranges
18571239cb6SGreg Roach    datestr = datestr.replace(/^[>]([\w ]+)$/, 'AFT $1');
18671239cb6SGreg Roach    datestr = datestr.replace(/^[<]([\w ]+)$/, 'BEF $1');
18771239cb6SGreg Roach    datestr = datestr.replace(/^([\w ]+)[-]$/, 'FROM $1');
18871239cb6SGreg Roach    datestr = datestr.replace(/^[-]([\w ]+)$/, 'TO $1');
18971239cb6SGreg Roach    datestr = datestr.replace(/^[~]([\w ]+)$/, 'ABT $1');
19071239cb6SGreg Roach    datestr = datestr.replace(/^[*]([\w ]+)$/, 'EST $1');
19171239cb6SGreg Roach    datestr = datestr.replace(/^[#]([\w ]+)$/, 'CAL $1');
19271239cb6SGreg Roach    datestr = datestr.replace(/^([\w ]+) ?- ?([\w ]+)$/, 'BET $1 AND $2');
19371239cb6SGreg Roach    datestr = datestr.replace(/^([\w ]+) ?~ ?([\w ]+)$/, 'FROM $1 TO $2');
19471239cb6SGreg Roach
19571239cb6SGreg Roach  // Convert full months to short months
19671239cb6SGreg Roach    datestr = datestr.replace(/(JANUARY)/, 'JAN');
19771239cb6SGreg Roach    datestr = datestr.replace(/(FEBRUARY)/, 'FEB');
19871239cb6SGreg Roach    datestr = datestr.replace(/(MARCH)/, 'MAR');
19971239cb6SGreg Roach    datestr = datestr.replace(/(APRIL)/, 'APR');
20071239cb6SGreg Roach    datestr = datestr.replace(/(MAY)/, 'MAY');
20171239cb6SGreg Roach    datestr = datestr.replace(/(JUNE)/, 'JUN');
20271239cb6SGreg Roach    datestr = datestr.replace(/(JULY)/, 'JUL');
20371239cb6SGreg Roach    datestr = datestr.replace(/(AUGUST)/, 'AUG');
20471239cb6SGreg Roach    datestr = datestr.replace(/(SEPTEMBER)/, 'SEP');
20571239cb6SGreg Roach    datestr = datestr.replace(/(OCTOBER)/, 'OCT');
20671239cb6SGreg Roach    datestr = datestr.replace(/(NOVEMBER)/, 'NOV');
20771239cb6SGreg Roach    datestr = datestr.replace(/(DECEMBER)/, 'DEC');
20871239cb6SGreg Roach
20971239cb6SGreg Roach  // Americans frequently enter dates as SEP 20, 1999
21071239cb6SGreg Roach  // No need to internationalise this, as this is an english-language issue
21171239cb6SGreg Roach    datestr = datestr.replace(/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\.? (\d\d?)[, ]+(\d\d\d\d)/, '$2 $1 $3');
21271239cb6SGreg Roach
21371239cb6SGreg Roach  // Apply leading zero to day numbers
21471239cb6SGreg Roach    datestr = datestr.replace(/(^| )(\d [A-Z]{3,5} \d{4})/, '$10$2');
21571239cb6SGreg Roach
21671239cb6SGreg Roach    if (datephrase) {
21771239cb6SGreg Roach        datestr = datestr + ' (' + datephrase;
21871239cb6SGreg Roach    }
21971239cb6SGreg Roach  // Only update it if is has been corrected - otherwise input focus
22071239cb6SGreg Roach  // moves to the end of the field unnecessarily
22171239cb6SGreg Roach    if (datefield.value !== datestr) {
22271239cb6SGreg Roach        datefield.value = datestr;
22371239cb6SGreg Roach    }
22471239cb6SGreg Roach}
22571239cb6SGreg Roach
22671239cb6SGreg Roachvar monthLabels = [];
22771239cb6SGreg RoachmonthLabels[1] = 'January';
22871239cb6SGreg RoachmonthLabels[2] = 'February';
22971239cb6SGreg RoachmonthLabels[3] = 'March';
23071239cb6SGreg RoachmonthLabels[4] = 'April';
23171239cb6SGreg RoachmonthLabels[5] = 'May';
23271239cb6SGreg RoachmonthLabels[6] = 'June';
23371239cb6SGreg RoachmonthLabels[7] = 'July';
23471239cb6SGreg RoachmonthLabels[8] = 'August';
23571239cb6SGreg RoachmonthLabels[9] = 'September';
23671239cb6SGreg RoachmonthLabels[10] = 'October';
23771239cb6SGreg RoachmonthLabels[11] = 'November';
23871239cb6SGreg RoachmonthLabels[12] = 'December';
23971239cb6SGreg Roach
24071239cb6SGreg Roachvar monthShort = [];
24171239cb6SGreg RoachmonthShort[1] = 'JAN';
24271239cb6SGreg RoachmonthShort[2] = 'FEB';
24371239cb6SGreg RoachmonthShort[3] = 'MAR';
24471239cb6SGreg RoachmonthShort[4] = 'APR';
24571239cb6SGreg RoachmonthShort[5] = 'MAY';
24671239cb6SGreg RoachmonthShort[6] = 'JUN';
24771239cb6SGreg RoachmonthShort[7] = 'JUL';
24871239cb6SGreg RoachmonthShort[8] = 'AUG';
24971239cb6SGreg RoachmonthShort[9] = 'SEP';
25071239cb6SGreg RoachmonthShort[10] = 'OCT';
25171239cb6SGreg RoachmonthShort[11] = 'NOV';
25271239cb6SGreg RoachmonthShort[12] = 'DEC';
25371239cb6SGreg Roach
25471239cb6SGreg Roachvar daysOfWeek = [];
25571239cb6SGreg RoachdaysOfWeek[0] = 'S';
25671239cb6SGreg RoachdaysOfWeek[1] = 'M';
25771239cb6SGreg RoachdaysOfWeek[2] = 'T';
25871239cb6SGreg RoachdaysOfWeek[3] = 'W';
25971239cb6SGreg RoachdaysOfWeek[4] = 'T';
26071239cb6SGreg RoachdaysOfWeek[5] = 'F';
26171239cb6SGreg RoachdaysOfWeek[6] = 'S';
26271239cb6SGreg Roach
26371239cb6SGreg Roachvar weekStart = 0;
26471239cb6SGreg Roach
26571239cb6SGreg Roachfunction cal_setMonthNames(jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
26671239cb6SGreg Roach{
26771239cb6SGreg Roach    monthLabels[1] = jan;
26871239cb6SGreg Roach    monthLabels[2] = feb;
26971239cb6SGreg Roach    monthLabels[3] = mar;
27071239cb6SGreg Roach    monthLabels[4] = apr;
27171239cb6SGreg Roach    monthLabels[5] = may;
27271239cb6SGreg Roach    monthLabels[6] = jun;
27371239cb6SGreg Roach    monthLabels[7] = jul;
27471239cb6SGreg Roach    monthLabels[8] = aug;
27571239cb6SGreg Roach    monthLabels[9] = sep;
27671239cb6SGreg Roach    monthLabels[10] = oct;
27771239cb6SGreg Roach    monthLabels[11] = nov;
27871239cb6SGreg Roach    monthLabels[12] = dec;
27971239cb6SGreg Roach}
28071239cb6SGreg Roach
28171239cb6SGreg Roachfunction cal_setDayHeaders(sun, mon, tue, wed, thu, fri, sat)
28271239cb6SGreg Roach{
28371239cb6SGreg Roach    daysOfWeek[0] = sun;
28471239cb6SGreg Roach    daysOfWeek[1] = mon;
28571239cb6SGreg Roach    daysOfWeek[2] = tue;
28671239cb6SGreg Roach    daysOfWeek[3] = wed;
28771239cb6SGreg Roach    daysOfWeek[4] = thu;
28871239cb6SGreg Roach    daysOfWeek[5] = fri;
28971239cb6SGreg Roach    daysOfWeek[6] = sat;
29071239cb6SGreg Roach}
29171239cb6SGreg Roach
29271239cb6SGreg Roachfunction cal_setWeekStart(day)
29371239cb6SGreg Roach{
29471239cb6SGreg Roach    if (day >= 0 && day < 7) {
29571239cb6SGreg Roach        weekStart = day;
29671239cb6SGreg Roach    }
29771239cb6SGreg Roach}
29871239cb6SGreg Roach
29971239cb6SGreg Roachfunction calendarWidget(dateDivId, dateFieldId)
30071239cb6SGreg Roach{
30171239cb6SGreg Roach    var dateDiv = document.getElementById(dateDivId);
30271239cb6SGreg Roach    var dateField = document.getElementById(dateFieldId);
30371239cb6SGreg Roach
30471239cb6SGreg Roach    if (dateDiv.style.visibility === 'visible') {
30571239cb6SGreg Roach        dateDiv.style.visibility = 'hidden';
30671239cb6SGreg Roach        return false;
30771239cb6SGreg Roach    }
30871239cb6SGreg Roach    if (dateDiv.style.visibility === 'show') {
30971239cb6SGreg Roach        dateDiv.style.visibility = 'hide';
31071239cb6SGreg Roach        return false;
31171239cb6SGreg Roach    }
31271239cb6SGreg Roach
31371239cb6SGreg Roach  /* Javascript calendar functions only work with precise gregorian dates "D M Y" or "Y" */
31471239cb6SGreg Roach    var greg_regex = /((\d+ (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) )?\d+)/i;
31571239cb6SGreg Roach    var date;
31671239cb6SGreg Roach    if (greg_regex.exec(dateField.value)) {
31771239cb6SGreg Roach        date = new Date(RegExp.$1);
31871239cb6SGreg Roach    } else {
31971239cb6SGreg Roach        date = new Date();
32071239cb6SGreg Roach    }
32171239cb6SGreg Roach
32271239cb6SGreg Roach    dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date);
32371239cb6SGreg Roach    if (dateDiv.style.visibility === 'hidden') {
32471239cb6SGreg Roach        dateDiv.style.visibility = 'visible';
32571239cb6SGreg Roach        return false;
32671239cb6SGreg Roach    }
32771239cb6SGreg Roach    if (dateDiv.style.visibility === 'hide') {
32871239cb6SGreg Roach        dateDiv.style.visibility = 'show';
32971239cb6SGreg Roach        return false;
33071239cb6SGreg Roach    }
33171239cb6SGreg Roach
33271239cb6SGreg Roach    return false;
33371239cb6SGreg Roach}
33471239cb6SGreg Roach
33571239cb6SGreg Roachfunction cal_generateSelectorContent(dateFieldId, dateDivId, date)
33671239cb6SGreg Roach{
33771239cb6SGreg Roach    var i, j;
33871239cb6SGreg Roach    var content = '<table border="1"><tr>';
33971239cb6SGreg Roach    content += '<td><select class="form-control" id="' + dateFieldId + '_daySelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">';
34071239cb6SGreg Roach    for (i = 1; i < 32; i++) {
34171239cb6SGreg Roach        content += '<option value="' + i + '"';
34271239cb6SGreg Roach        if (date.getDate() === i) {
34371239cb6SGreg Roach            content += ' selected="selected"';
34471239cb6SGreg Roach        }
34571239cb6SGreg Roach        content += '>' + i + '</option>';
34671239cb6SGreg Roach    }
34771239cb6SGreg Roach    content += '</select></td>';
34871239cb6SGreg Roach    content += '<td><select class="form-control" id="' + dateFieldId + '_monSelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">';
34971239cb6SGreg Roach    for (i = 1; i < 13; i++) {
35071239cb6SGreg Roach        content += '<option value="' + i + '"';
35171239cb6SGreg Roach        if (date.getMonth() + 1 === i) {
35271239cb6SGreg Roach            content += ' selected="selected"';
35371239cb6SGreg Roach        }
35471239cb6SGreg Roach        content += '>' + monthLabels[i] + '</option>';
35571239cb6SGreg Roach    }
35671239cb6SGreg Roach    content += '</select></td>';
35771239cb6SGreg Roach    content += '<td><input class="form-control" type="text" id="' + dateFieldId + '_yearInput" size="5" value="' + date.getFullYear() + '" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');" /></td></tr>';
35871239cb6SGreg Roach    content += '<tr><td colspan="3">';
35971239cb6SGreg Roach    content += '<table width="100%">';
36071239cb6SGreg Roach    content += '<tr>';
36171239cb6SGreg Roach    j = weekStart;
36271239cb6SGreg Roach    for (i = 0; i < 7; i++) {
36371239cb6SGreg Roach        content += '<td ';
36471239cb6SGreg Roach        content += 'class="descriptionbox"';
36571239cb6SGreg Roach        content += '>';
36671239cb6SGreg Roach        content += daysOfWeek[j];
36771239cb6SGreg Roach        content += '</td>';
36871239cb6SGreg Roach        j++;
36971239cb6SGreg Roach        if (j > 6) {
37071239cb6SGreg Roach            j = 0;
37171239cb6SGreg Roach        }
37271239cb6SGreg Roach    }
37371239cb6SGreg Roach    content += '</tr>';
37471239cb6SGreg Roach
37571239cb6SGreg Roach    var tdate = new Date(date.getFullYear(), date.getMonth(), 1);
37671239cb6SGreg Roach    var day = tdate.getDay();
37771239cb6SGreg Roach    day = day - weekStart;
37871239cb6SGreg Roach    var daymilli = 1000 * 60 * 60 * 24;
37971239cb6SGreg Roach    tdate = tdate.getTime() - (day * daymilli) + (daymilli / 2);
38071239cb6SGreg Roach    tdate = new Date(tdate);
38171239cb6SGreg Roach
38271239cb6SGreg Roach    for (j = 0; j < 6; j++) {
38371239cb6SGreg Roach        content += '<tr>';
38471239cb6SGreg Roach        for (i = 0; i < 7; i++) {
38571239cb6SGreg Roach            content += '<td ';
38671239cb6SGreg Roach            if (tdate.getMonth() === date.getMonth()) {
38771239cb6SGreg Roach                if (tdate.getDate() === date.getDate()) {
38871239cb6SGreg Roach                    content += 'class="descriptionbox"';
38971239cb6SGreg Roach                } else {
39071239cb6SGreg Roach                    content += 'class="optionbox"';
39171239cb6SGreg Roach                }
39271239cb6SGreg Roach            } else {
39371239cb6SGreg Roach                content += 'style="background-color:#EAEAEA; border: solid #AAAAAA 1px;"';
39471239cb6SGreg Roach            }
39571239cb6SGreg Roach            content += '><a href="#" onclick="return cal_dateClicked(\'' + dateFieldId + '\', \'' + dateDivId + '\', ' + tdate.getFullYear() + ', ' + tdate.getMonth() + ', ' + tdate.getDate() + ');">';
39671239cb6SGreg Roach            content += tdate.getDate();
39771239cb6SGreg Roach            content += '</a></td>';
39871239cb6SGreg Roach            var datemilli = tdate.getTime() + daymilli;
39971239cb6SGreg Roach            tdate = new Date(datemilli);
40071239cb6SGreg Roach        }
40171239cb6SGreg Roach        content += '</tr>';
40271239cb6SGreg Roach    }
40371239cb6SGreg Roach    content += '</table>';
40471239cb6SGreg Roach    content += '</td></tr>';
40571239cb6SGreg Roach    content += '</table>';
40671239cb6SGreg Roach
40771239cb6SGreg Roach    return content;
40871239cb6SGreg Roach}
40971239cb6SGreg Roach
41071239cb6SGreg Roachfunction cal_setDateField(dateFieldId, year, month, day)
41171239cb6SGreg Roach{
41271239cb6SGreg Roach    var dateField = document.getElementById(dateFieldId);
41371239cb6SGreg Roach    if (!dateField) {
41471239cb6SGreg Roach        return false;
41571239cb6SGreg Roach    }
41671239cb6SGreg Roach    if (day < 10) {
41771239cb6SGreg Roach        day = '0' + day;
41871239cb6SGreg Roach    }
41971239cb6SGreg Roach    dateField.value = day + ' ' + monthShort[month + 1] + ' ' + year;
42071239cb6SGreg Roach    return false;
42171239cb6SGreg Roach}
42271239cb6SGreg Roach
42371239cb6SGreg Roachfunction cal_updateCalendar(dateFieldId, dateDivId)
42471239cb6SGreg Roach{
42571239cb6SGreg Roach    var dateSel = document.getElementById(dateFieldId + '_daySelect');
42671239cb6SGreg Roach    if (!dateSel) {
42771239cb6SGreg Roach        return false;
42871239cb6SGreg Roach    }
42971239cb6SGreg Roach    var monthSel = document.getElementById(dateFieldId + '_monSelect');
43071239cb6SGreg Roach    if (!monthSel) {
43171239cb6SGreg Roach        return false;
43271239cb6SGreg Roach    }
43371239cb6SGreg Roach    var yearInput = document.getElementById(dateFieldId + '_yearInput');
43471239cb6SGreg Roach    if (!yearInput) {
43571239cb6SGreg Roach        return false;
43671239cb6SGreg Roach    }
43771239cb6SGreg Roach
43871239cb6SGreg Roach    var month = parseInt(monthSel.options[monthSel.selectedIndex].value, 10);
43971239cb6SGreg Roach    month = month - 1;
44071239cb6SGreg Roach
44171239cb6SGreg Roach    var date = new Date(yearInput.value, month, dateSel.options[dateSel.selectedIndex].value);
44271239cb6SGreg Roach    cal_setDateField(dateFieldId, date.getFullYear(), date.getMonth(), date.getDate());
44371239cb6SGreg Roach
44471239cb6SGreg Roach    var dateDiv = document.getElementById(dateDivId);
44571239cb6SGreg Roach    if (!dateDiv) {
44671239cb6SGreg Roach        alert('no dateDiv ' + dateDivId);
44771239cb6SGreg Roach        return false;
44871239cb6SGreg Roach    }
44971239cb6SGreg Roach    dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date);
45071239cb6SGreg Roach
45171239cb6SGreg Roach    return false;
45271239cb6SGreg Roach}
45371239cb6SGreg Roach
45471239cb6SGreg Roachfunction cal_dateClicked(dateFieldId, dateDivId, year, month, day)
45571239cb6SGreg Roach{
45671239cb6SGreg Roach    cal_setDateField(dateFieldId, year, month, day);
45771239cb6SGreg Roach    calendarWidget(dateDivId, dateFieldId);
45871239cb6SGreg Roach    return false;
45971239cb6SGreg Roach}
46071239cb6SGreg Roach
46171239cb6SGreg Roachfunction openerpasteid(id)
46271239cb6SGreg Roach{
46371239cb6SGreg Roach    if (window.opener.paste_id) {
46471239cb6SGreg Roach        window.opener.paste_id(id);
46571239cb6SGreg Roach    }
46671239cb6SGreg Roach    window.close();
46771239cb6SGreg Roach}
46871239cb6SGreg Roach
46971239cb6SGreg Roachfunction paste_id(value)
47071239cb6SGreg Roach{
47171239cb6SGreg Roach    pastefield.value = value;
47271239cb6SGreg Roach}
47371239cb6SGreg Roach
47471239cb6SGreg Roachfunction pastename(name)
47571239cb6SGreg Roach{
47671239cb6SGreg Roach    if (nameElement) {
47771239cb6SGreg Roach        nameElement.innerHTML = name;
47871239cb6SGreg Roach    }
47971239cb6SGreg Roach    if (remElement) {
48071239cb6SGreg Roach        remElement.style.display = 'block';
48171239cb6SGreg Roach    }
48271239cb6SGreg Roach}
48371239cb6SGreg Roach
48471239cb6SGreg Roachfunction paste_char(value)
48571239cb6SGreg Roach{
48671239cb6SGreg Roach    if (document.selection) {
48771239cb6SGreg Roach      // IE
48871239cb6SGreg Roach        pastefield.focus();
48971239cb6SGreg Roach        document.selection.createRange().text = value;
49071239cb6SGreg Roach    } else if (pastefield.selectionStart || pastefield.selectionStart === 0) {
49171239cb6SGreg Roach      // Mozilla/Chrome/Safari
49271239cb6SGreg Roach        pastefield.value =
49371239cb6SGreg Roach        pastefield.value.substring(0, pastefield.selectionStart) +
49471239cb6SGreg Roach        value +
49571239cb6SGreg Roach        pastefield.value.substring(pastefield.selectionEnd, pastefield.value.length);
49671239cb6SGreg Roach        pastefield.selectionStart = pastefield.selectionEnd = pastefield.selectionStart + value.length;
49771239cb6SGreg Roach    } else {
49871239cb6SGreg Roach      // Fallback? - just append
49971239cb6SGreg Roach        pastefield.value += value;
50071239cb6SGreg Roach    }
50171239cb6SGreg Roach
50271239cb6SGreg Roach    if (pastefield.id === 'NPFX' || pastefield.id === 'GIVN' || pastefield.id === 'SPFX' || pastefield.id === 'SURN' || pastefield.id === 'NSFX') {
50371239cb6SGreg Roach        updatewholename();
50471239cb6SGreg Roach    }
50571239cb6SGreg Roach}
50671239cb6SGreg Roach
50771239cb6SGreg Roach/**
50871239cb6SGreg Roach * Persistant checkbox options to hide/show extra data.
50971239cb6SGreg Roach
51064490ee2SGreg Roach * @param element_id
51171239cb6SGreg Roach */
51264490ee2SGreg Roachfunction persistent_toggle(element_id)
51371239cb6SGreg Roach{
51464490ee2SGreg Roach    let element = document.getElementById(element_id);
51564490ee2SGreg Roach    let key     = 'state-of-' + element_id;
51664490ee2SGreg Roach    let state   = localStorage.getItem(key);
51771239cb6SGreg Roach
51864490ee2SGreg Roach    // Previously selected?
51964490ee2SGreg Roach    if (state === 'true') {
52064490ee2SGreg Roach        $(element).click();
52171239cb6SGreg Roach    }
52271239cb6SGreg Roach
52364490ee2SGreg Roach    // Remember state for the next page load.
52464490ee2SGreg Roach    $(element).on('change', function() { localStorage.setItem(key, element.checked); });
52571239cb6SGreg Roach}
52671239cb6SGreg Roach
52771239cb6SGreg Roachfunction valid_lati_long(field, pos, neg)
52871239cb6SGreg Roach{
52971239cb6SGreg Roach  // valid LATI or LONG according to Gedcom standard
53071239cb6SGreg Roach  // pos (+) : N or E
53171239cb6SGreg Roach  // neg (-) : S or W
53271239cb6SGreg Roach    var txt = field.value.toUpperCase();
53371239cb6SGreg Roach    txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim
53471239cb6SGreg Roach    txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34
53571239cb6SGreg Roach    txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234
53671239cb6SGreg Roach    txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698
53771239cb6SGreg Roach    txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698
53871239cb6SGreg Roach  // 0°34'11 ==> 0:34:11
53971239cb6SGreg Roach    txt = txt.replace(/\u00b0/g, ':'); // °
54071239cb6SGreg Roach    txt = txt.replace(/\u0027/g, ':'); // '
54171239cb6SGreg Roach  // 0:34:11.2W ==> W0.5698
54271239cb6SGreg Roach    txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) {
54371239cb6SGreg Roach        var n = parseFloat($1);
54471239cb6SGreg Roach        n += ($2 / 60);
54571239cb6SGreg Roach        n += ($3 / 3600);
54671239cb6SGreg Roach        n = Math.round(n * 1E4) / 1E4;
54771239cb6SGreg Roach        return $4 + n;
54871239cb6SGreg Roach    });
54971239cb6SGreg Roach  // 0:34W ==> W0.5667
55071239cb6SGreg Roach    txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) {
55171239cb6SGreg Roach        var n = parseFloat($1);
55271239cb6SGreg Roach        n += ($2 / 60);
55371239cb6SGreg Roach        n = Math.round(n * 1E4) / 1E4;
55471239cb6SGreg Roach        return $3 + n;
55571239cb6SGreg Roach    });
55671239cb6SGreg Roach  // 0.5698W ==> W0.5698
55771239cb6SGreg Roach    txt = txt.replace(/(.*)([N|S|E|W]+)$/g, '$2$1');
55871239cb6SGreg Roach  // 17.1234 ==> N17.1234
55971239cb6SGreg Roach    if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) {
56071239cb6SGreg Roach        txt = pos + txt;
56171239cb6SGreg Roach    }
56271239cb6SGreg Roach    field.value = txt;
56371239cb6SGreg Roach}
56471239cb6SGreg Roach
56571239cb6SGreg Roach// This is the default way for webtrees to show image galleries.
56671239cb6SGreg Roach// Custom themes may use a different viewer.
56771239cb6SGreg Roachfunction activate_colorbox(config)
56871239cb6SGreg Roach{
56971239cb6SGreg Roach    $.extend($.colorbox.settings, {
57071239cb6SGreg Roach      // Don't scroll window with document
57171239cb6SGreg Roach        fixed: true,
57271239cb6SGreg Roach        current: '',
57371239cb6SGreg Roach        previous: '\uf048',
57471239cb6SGreg Roach        next: '\uf051',
57571239cb6SGreg Roach        slideshowStart: '\uf04b',
57671239cb6SGreg Roach        slideshowStop: '\uf04c',
57771239cb6SGreg Roach        close: '\uf00d'
57871239cb6SGreg Roach    });
57971239cb6SGreg Roach    if (config) {
58071239cb6SGreg Roach        $.extend($.colorbox.settings, config);
58171239cb6SGreg Roach    }
58271239cb6SGreg Roach
58371239cb6SGreg Roach  // Trigger an event when we click on an (any) image
58471239cb6SGreg Roach    $('body').on('click', 'a.gallery', function () {
58571239cb6SGreg Roach      // Enable colorbox for images
58671239cb6SGreg Roach        $('a[type^=image].gallery').colorbox({
58771239cb6SGreg Roach            photo: true,
58871239cb6SGreg Roach            maxWidth: '95%',
58971239cb6SGreg Roach            maxHeight: '95%',
59071239cb6SGreg Roach            rel: 'gallery', // Turn all images on the page into a slideshow
59171239cb6SGreg Roach            slideshow: true,
59271239cb6SGreg Roach            slideshowAuto: false,
59371239cb6SGreg Roach          // Add wheelzoom to the displayed image
59471239cb6SGreg Roach            onComplete: function () {
59571239cb6SGreg Roach              // Disable click on image triggering next image
59671239cb6SGreg Roach              // https://github.com/jackmoore/colorbox/issues/668
59771239cb6SGreg Roach                $('.cboxPhoto').unbind('click');
59871239cb6SGreg Roach
59971239cb6SGreg Roach                wheelzoom(document.querySelectorAll('.cboxPhoto'));
60071239cb6SGreg Roach            }
60171239cb6SGreg Roach        });
60271239cb6SGreg Roach
60371239cb6SGreg Roach      // Enable colorbox for audio using <audio></audio>, where supported
60471239cb6SGreg Roach      // $('html.video a[type^=video].gallery').colorbox({
60571239cb6SGreg Roach      //  rel:         'nofollow' // Slideshows are just for images
60671239cb6SGreg Roach      // });
60771239cb6SGreg Roach
60871239cb6SGreg Roach      // Enable colorbox for video using <video></video>, where supported
60971239cb6SGreg Roach      // $('html.audio a[type^=audio].gallery').colorbox({
61071239cb6SGreg Roach      //  rel:         'nofollow', // Slideshows are just for images
61171239cb6SGreg Roach      // });
61271239cb6SGreg Roach
61371239cb6SGreg Roach      // Allow all other media types remain as download links
61471239cb6SGreg Roach    });
61571239cb6SGreg Roach}
61671239cb6SGreg Roach
61771239cb6SGreg Roach// Initialize autocomplete elements.
61871239cb6SGreg Roachfunction autocomplete(selector)
61971239cb6SGreg Roach{
62071239cb6SGreg Roach  // Use typeahead/bloodhound for autocomplete
62171239cb6SGreg Roach    $(selector).each(function () {
622f4abaf12SGreg Roach        let that = this;
62371239cb6SGreg Roach        $(this).typeahead(null, {
62471239cb6SGreg Roach            display: 'value',
62571239cb6SGreg Roach            source: new Bloodhound({
62671239cb6SGreg Roach                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
62771239cb6SGreg Roach                queryTokenizer: Bloodhound.tokenizers.whitespace,
62871239cb6SGreg Roach                remote: {
62971239cb6SGreg Roach                    url: this.dataset.autocompleteUrl,
630f4abaf12SGreg Roach                    replace: function(url, uriEncodedQuery) {
631f4abaf12SGreg Roach                        if (that.dataset.autocompleteExtra) {
632f4abaf12SGreg Roach                            let extra = $(document.querySelector(that.dataset.autocompleteExtra)).val();
633f4abaf12SGreg Roach                            return url.replace("QUERY",uriEncodedQuery) + '&extra=' + encodeURIComponent(extra)
634f4abaf12SGreg Roach                        }
635f4abaf12SGreg Roach                        return url.replace("QUERY",uriEncodedQuery);
636f4abaf12SGreg Roach                    },
637f4abaf12SGreg Roach                    wildcard: 'QUERY',
638f4abaf12SGreg Roach
63971239cb6SGreg Roach                }
64071239cb6SGreg Roach            })
64171239cb6SGreg Roach        });
64271239cb6SGreg Roach    });
64371239cb6SGreg Roach}
64471239cb6SGreg Roach
64571239cb6SGreg Roach/**
64671239cb6SGreg Roach * Insert text at the current cursor position in an input field.
64771239cb6SGreg Roach *
64871239cb6SGreg Roach * @param e The input element.
64971239cb6SGreg Roach * @param t The text to insert.
65071239cb6SGreg Roach */
65171239cb6SGreg Roachfunction insertTextAtCursor(e, t)
65271239cb6SGreg Roach{
65371239cb6SGreg Roach    var scrollTop = e.scrollTop;
65471239cb6SGreg Roach    var selectionStart = e.selectionStart;
65571239cb6SGreg Roach    var prefix = e.value.substring(0, selectionStart);
65671239cb6SGreg Roach    var suffix = e.value.substring(e.selectionEnd, e.value.length);
65771239cb6SGreg Roach    e.value = prefix + t + suffix;
65871239cb6SGreg Roach    e.selectionStart = selectionStart + t.length;
65971239cb6SGreg Roach    e.selectionEnd = e.selectionStart;
66071239cb6SGreg Roach    e.focus();
66171239cb6SGreg Roach    e.scrollTop = scrollTop;
66271239cb6SGreg Roach}
66371239cb6SGreg Roach
66471239cb6SGreg Roach// Send the CSRF token on all AJAX requests
66571239cb6SGreg Roach$.ajaxSetup({
66671239cb6SGreg Roach    headers: {
66771239cb6SGreg Roach        'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content')
66871239cb6SGreg Roach    }
66971239cb6SGreg Roach});
67071239cb6SGreg Roach
67171239cb6SGreg Roach// Initialisation
67271239cb6SGreg Roach$(function () {
67371239cb6SGreg Roach  // Page elements that load automaticaly via AJAX.
67471239cb6SGreg Roach  // This prevents bad robots from crawling resource-intensive pages.
67571239cb6SGreg Roach    $("[data-ajax-url]").each(function () {
67671239cb6SGreg Roach        $(this).load($(this).data('ajaxUrl'));
67771239cb6SGreg Roach    });
67871239cb6SGreg Roach
67971239cb6SGreg Roach  // Select2 - format entries in the select list
68071239cb6SGreg Roach    function templateOptionForSelect2(data)
68171239cb6SGreg Roach    {
68271239cb6SGreg Roach        if (data.loading) {
68371239cb6SGreg Roach          // If we're waiting for the server, this will be a "waiting..." message
68471239cb6SGreg Roach            return data.text;
68571239cb6SGreg Roach        } else {
68671239cb6SGreg Roach          // The response from the server is already in HTML, so no need to format it here.
68771239cb6SGreg Roach            return data.text;
68871239cb6SGreg Roach        }
68971239cb6SGreg Roach    }
69071239cb6SGreg Roach
69171239cb6SGreg Roach    // Autocomplete
69271239cb6SGreg Roach    autocomplete('input[data-autocomplete-url]');
69371239cb6SGreg Roach
69471239cb6SGreg Roach    // Select2 - activate autocomplete fields
695bdbdb10cSGreg Roach    const lang = document.documentElement.lang;
696bdbdb10cSGreg Roach    const select2_languages = {
697bdbdb10cSGreg Roach        'zh-Hans': 'zh-CN',
698bdbdb10cSGreg Roach        'zh-Hant': 'zh-TW',
699bdbdb10cSGreg Roach    };
7008ec20abdSGreg Roach    $("select.select2").select2({
701bdbdb10cSGreg Roach        language: select2_languages[lang] || lang,
702896a5721SGreg Roach        // Do not escape - we do it on the server.
70371239cb6SGreg Roach        escapeMarkup: function (x) {
7048ec20abdSGreg Roach            return x;
7058ec20abdSGreg Roach        },
706896a5721SGreg Roach    });
707896a5721SGreg Roach
708896a5721SGreg Roach    // If we clear the select (using the "X" button), we need an empty value
709896a5721SGreg Roach    // (rather than no value at all) for (non-multiple) selects with name="array[]"
710896a5721SGreg Roach    $("select.select2:not([multiple])")
7115d0d3c69SGreg Roach    .on("select2:unselect", function (evt) {
7125d0d3c69SGreg Roach        $(evt.delegateTarget).html("<option value=\"\" selected></option>");
713bdbdb10cSGreg Roach    });
71471239cb6SGreg Roach
71571239cb6SGreg Roach    // Datatables - locale aware sorting
71671239cb6SGreg Roach    $.fn.dataTableExt.oSort['text-asc'] = function (x, y) {
71771239cb6SGreg Roach        return x.localeCompare(y, document.documentElement.lang, {'sensitivity': 'base'});
71871239cb6SGreg Roach    };
71971239cb6SGreg Roach    $.fn.dataTableExt.oSort['text-desc'] = function (x, y) {
72071239cb6SGreg Roach        return y.localeCompare(x, document.documentElement.lang, {'sensitivity': 'base'});
72171239cb6SGreg Roach    };
72271239cb6SGreg Roach
72371239cb6SGreg Roach  // DataTables - start hidden to prevent FOUC.
72471239cb6SGreg Roach    $('table.datatables').each(function () {
72571239cb6SGreg Roach        $(this).DataTable(); $(this).removeClass('d-none'); });
72671239cb6SGreg Roach
72771239cb6SGreg Roach  // Activate the on-screen keyboard
72871239cb6SGreg Roach    var osk_focus_element;
72971239cb6SGreg Roach    $('.wt-osk-trigger').click(function () {
73071239cb6SGreg Roach      // When a user clicks the icon, set focus to the corresponding input
73171239cb6SGreg Roach        osk_focus_element = document.getElementById($(this).data('id'));
73271239cb6SGreg Roach        osk_focus_element.focus();
73371239cb6SGreg Roach        $('.wt-osk').show();
73471239cb6SGreg Roach    });
73571239cb6SGreg Roach    $('.wt-osk-script-button').change(function () {
73671239cb6SGreg Roach        $('.wt-osk-script').prop('hidden', true);
73771239cb6SGreg Roach        $('.wt-osk-script-' + $(this).data('script')).prop('hidden', false);
73871239cb6SGreg Roach    });
73971239cb6SGreg Roach    $('.wt-osk-shift-button').click(function () {
74071239cb6SGreg Roach        document.querySelector('.wt-osk-keys').classList.toggle('shifted');
74171239cb6SGreg Roach    });
74271239cb6SGreg Roach    $('.wt-osk-keys').on('click', '.wt-osk-key', function () {
74371239cb6SGreg Roach        var key = $(this).contents().get(0).nodeValue;
74471239cb6SGreg Roach        var shift_state = $('.wt-osk-shift-button').hasClass('active');
74571239cb6SGreg Roach        var shift_key = $('sup', this)[0];
74671239cb6SGreg Roach        if (shift_state && shift_key !== undefined) {
74771239cb6SGreg Roach            key = shift_key.innerText;
74871239cb6SGreg Roach        }
749*0d2905f7SGreg Roach        webtrees.pasteAtCursor(osk_focus_element, key);
75071239cb6SGreg Roach        if ($('.wt-osk-pin-button').hasClass('active') === false) {
75171239cb6SGreg Roach            $('.wt-osk').hide();
75271239cb6SGreg Roach        }
75371239cb6SGreg Roach    });
75471239cb6SGreg Roach
75571239cb6SGreg Roach    $('.wt-osk-close').on('click', function () {
75671239cb6SGreg Roach        $('.wt-osk').hide();
75771239cb6SGreg Roach    });
75871239cb6SGreg Roach});
7597adfb8e5SGreg Roach
760b3a775f6SGreg Roach// Convert data-confirm and data-post-url attributes into useful behavior.
7617adfb8e5SGreg Roachdocument.addEventListener("click",  (event) => {
7627adfb8e5SGreg Roach    const target = event.target.closest("a");
7637adfb8e5SGreg Roach
7647adfb8e5SGreg Roach    if (target === null) {
7657adfb8e5SGreg Roach        return;
7667adfb8e5SGreg Roach    }
7677adfb8e5SGreg Roach
7687adfb8e5SGreg Roach    if ("confirm" in target.dataset && !confirm(target.dataset.confirm)) {
7697adfb8e5SGreg Roach        event.preventDefault();
7707adfb8e5SGreg Roach        return;
7717adfb8e5SGreg Roach    }
7727adfb8e5SGreg Roach
7737adfb8e5SGreg Roach    if ("postUrl" in target.dataset) {
7747adfb8e5SGreg Roach        let request = new XMLHttpRequest();
7757adfb8e5SGreg Roach        let token   = document.querySelector("meta[name=csrf]").content;
7767adfb8e5SGreg Roach        request.open("POST", target.dataset.postUrl, true);
7777adfb8e5SGreg Roach        request.setRequestHeader("X-CSRF-TOKEN", token);
7787adfb8e5SGreg Roach        request.onreadystatechange = () => {
7797adfb8e5SGreg Roach            if (request.readyState === request.DONE) {
7807adfb8e5SGreg Roach                document.location.reload();
7817adfb8e5SGreg Roach            }
7827adfb8e5SGreg Roach        };
7837adfb8e5SGreg Roach        request.send();
7847adfb8e5SGreg Roach        event.preventDefault();
7857adfb8e5SGreg Roach    }
7867adfb8e5SGreg Roach});
787