xref: /webtrees/resources/js/webtrees.js (revision ac1ce4fbf33682bc66a297b31426f011b76ac44f)
171239cb6SGreg Roach/**
271239cb6SGreg Roach * webtrees: online genealogy
3d11be702SGreg Roach * Copyright (C) 2023 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
16efd89170SGreg Roach'use strict';
1771239cb6SGreg Roach
180d2905f7SGreg Roach(function (webtrees) {
1959e18f0cSGreg Roach  const lang = document.documentElement.lang;
2059e18f0cSGreg Roach
210d2905f7SGreg Roach  // Identify the script used by some text.
220d2905f7SGreg Roach  const scriptRegexes = {
230d2905f7SGreg Roach    Han: /[\u3400-\u9FCC]/,
240d2905f7SGreg Roach    Grek: /[\u0370-\u03FF]/,
250d2905f7SGreg Roach    Cyrl: /[\u0400-\u04FF]/,
260d2905f7SGreg Roach    Hebr: /[\u0590-\u05FF]/,
27efd89170SGreg Roach    Arab: /[\u0600-\u06FF]/
280d2905f7SGreg Roach  };
290d2905f7SGreg Roach
3059e18f0cSGreg Roach  /**
3159e18f0cSGreg Roach   * Tidy the whitespace in a string.
3206fe1eb5SGreg Roach   * @param {string} str
3306fe1eb5SGreg Roach   * @returns {string}
3459e18f0cSGreg Roach   */
3559e18f0cSGreg Roach  function trim (str) {
36efd89170SGreg Roach    return str.replace(/\s+/g, ' ').trim();
3759e18f0cSGreg Roach  }
3859e18f0cSGreg Roach
3959e18f0cSGreg Roach  /**
40313cf418SGreg Roach   * Simple wrapper around fetch() with our preferred headers
41313cf418SGreg Roach   *
42313cf418SGreg Roach   * @param {string} url
43313cf418SGreg Roach   * @returns {Promise}
44313cf418SGreg Roach   */
45313cf418SGreg Roach  webtrees.httpGet = function (url) {
46313cf418SGreg Roach    const options = {
47313cf418SGreg Roach      method: 'GET',
48313cf418SGreg Roach      credentials: 'same-origin',
49313cf418SGreg Roach      referrerPolicy: 'same-origin',
50313cf418SGreg Roach      headers: new Headers({
51313cf418SGreg Roach        'x-requested-with': 'XMLHttpRequest',
52313cf418SGreg Roach      })
53313cf418SGreg Roach    };
54313cf418SGreg Roach
55313cf418SGreg Roach    return fetch(url, options);
56313cf418SGreg Roach  }
57313cf418SGreg Roach
58313cf418SGreg Roach  /**
59313cf418SGreg Roach   * Simple wrapper around fetch() with our preferred headers
60313cf418SGreg Roach   *
61313cf418SGreg Roach   * @param {string} url
62313cf418SGreg Roach   * @param {string|FormData} body
63313cf418SGreg Roach   * @returns {Promise}
64313cf418SGreg Roach   */
65313cf418SGreg Roach  webtrees.httpPost= function (url, body = '') {
66313cf418SGreg Roach    const csrfToken = document.head.querySelector('meta[name=csrf]').getAttribute('content');
67313cf418SGreg Roach
68313cf418SGreg Roach    const options = {
69313cf418SGreg Roach      body: body,
70313cf418SGreg Roach      method: 'POST',
71313cf418SGreg Roach      credentials: 'same-origin',
72313cf418SGreg Roach      referrerPolicy:  'same-origin',
73313cf418SGreg Roach      headers: new Headers({
74313cf418SGreg Roach        'X-CSRF-TOKEN': csrfToken,
75313cf418SGreg Roach        'x-requested-with': 'XMLHttpRequest',
76313cf418SGreg Roach      })
77313cf418SGreg Roach    };
78313cf418SGreg Roach
79313cf418SGreg Roach    return fetch(url, options, body);
80313cf418SGreg Roach  }
81313cf418SGreg Roach
82313cf418SGreg Roach  /**
8359e18f0cSGreg Roach   * Look for non-latin characters in a string.
8406fe1eb5SGreg Roach   * @param {string} str
8506fe1eb5SGreg Roach   * @returns {string}
8659e18f0cSGreg Roach   */
870d2905f7SGreg Roach  webtrees.detectScript = function (str) {
880d2905f7SGreg Roach    for (const script in scriptRegexes) {
890d2905f7SGreg Roach      if (str.match(scriptRegexes[script])) {
900d2905f7SGreg Roach        return script;
910d2905f7SGreg Roach      }
9259e18f0cSGreg Roach    }
9359e18f0cSGreg Roach
94efd89170SGreg Roach    return 'Latn';
950d2905f7SGreg Roach  };
9659e18f0cSGreg Roach
9759e18f0cSGreg Roach  /**
9859e18f0cSGreg Roach   * In some languages, the SURN uses a male/default form, but NAME uses a gender-inflected form.
9906fe1eb5SGreg Roach   * @param {string} surname
10006fe1eb5SGreg Roach   * @param {string} sex
10106fe1eb5SGreg Roach   * @returns {string}
10259e18f0cSGreg Roach   */
10359e18f0cSGreg Roach  function inflectSurname (surname, sex) {
104efd89170SGreg Roach    if (lang === 'pl' && sex === 'F') {
10559e18f0cSGreg Roach      return surname
106efd89170SGreg Roach        .replace(/ski$/, 'ska')
107efd89170SGreg Roach        .replace(/cki$/, 'cka')
108efd89170SGreg Roach        .replace(/dzki$/, 'dzka')
109efd89170SGreg Roach        .replace(/żki$/, 'żka');
11059e18f0cSGreg Roach    }
11159e18f0cSGreg Roach
11259e18f0cSGreg Roach    return surname;
11359e18f0cSGreg Roach  }
11459e18f0cSGreg Roach
11559e18f0cSGreg Roach  /**
11659e18f0cSGreg Roach   * Build a NAME from a NPFX, GIVN, SPFX, SURN and NSFX parts.
11759e18f0cSGreg Roach   * Assumes the language of the document is the same as the language of the name.
11806fe1eb5SGreg Roach   * @param {string} npfx
11906fe1eb5SGreg Roach   * @param {string} givn
12006fe1eb5SGreg Roach   * @param {string} spfx
12106fe1eb5SGreg Roach   * @param {string} surn
12206fe1eb5SGreg Roach   * @param {string} nsfx
12306fe1eb5SGreg Roach   * @param {string} sex
12406fe1eb5SGreg Roach   * @returns {string}
12559e18f0cSGreg Roach   */
1260d2905f7SGreg Roach  webtrees.buildNameFromParts = function (npfx, givn, spfx, surn, nsfx, sex) {
127efd89170SGreg Roach    const usesCJK = webtrees.detectScript(npfx + givn + spfx + givn + surn + nsfx) === 'Han';
128efd89170SGreg Roach    const separator = usesCJK ? '' : ' ';
12959e18f0cSGreg Roach    const surnameFirst = usesCJK || ['hu', 'jp', 'ko', 'vi', 'zh-Hans', 'zh-Hant'].indexOf(lang) !== -1;
13059e18f0cSGreg Roach    const patronym = ['is'].indexOf(lang) !== -1;
131efd89170SGreg Roach    const slash = patronym ? '' : '/';
13259e18f0cSGreg Roach
13359e18f0cSGreg Roach    // GIVN and SURN may be a comma-separated lists.
13459e18f0cSGreg Roach    npfx = trim(npfx);
135063107dbSGreg Roach    givn = trim(givn.replace(/,/g, separator));
13659e18f0cSGreg Roach    spfx = trim(spfx);
137063107dbSGreg Roach    surn = inflectSurname(trim(surn.replace(/,/g, separator)), sex);
13859e18f0cSGreg Roach    nsfx = trim(nsfx);
13959e18f0cSGreg Roach
1409026ef5bSGreg Roach    const surname_separator = spfx.endsWith('\'') || spfx.endsWith('‘') ? '' : ' ';
1419026ef5bSGreg Roach
1429026ef5bSGreg Roach    const surname = trim(spfx + surname_separator + surn);
14359e18f0cSGreg Roach
14459e18f0cSGreg Roach    const name = surnameFirst ? slash + surname + slash + separator + givn : givn + separator + slash + surname + slash;
14559e18f0cSGreg Roach
14659e18f0cSGreg Roach    return trim(npfx + separator + name + separator + nsfx);
1470d2905f7SGreg Roach  };
1480d2905f7SGreg Roach
1490d2905f7SGreg Roach  // Insert text at the current cursor position in a text field.
1500d2905f7SGreg Roach  webtrees.pasteAtCursor = function (element, text) {
1510d2905f7SGreg Roach    if (element !== null) {
1520d2905f7SGreg Roach      const caret_pos = element.selectionStart + text.length;
1530d2905f7SGreg Roach      const textBefore = element.value.substring(0, element.selectionStart);
1540d2905f7SGreg Roach      const textAfter = element.value.substring(element.selectionEnd);
1550d2905f7SGreg Roach      element.value = textBefore + text + textAfter;
1560d2905f7SGreg Roach      element.setSelectionRange(caret_pos, caret_pos);
1570d2905f7SGreg Roach      element.focus();
15859e18f0cSGreg Roach    }
159efd89170SGreg Roach  };
16071239cb6SGreg Roach
16106fe1eb5SGreg Roach  /**
1627fb78f8aSGreg Roach   * @param {Element} datefield
16306fe1eb5SGreg Roach   * @param {string} dmy
16406fe1eb5SGreg Roach   */
165a7a3d6dbSGreg Roach  webtrees.reformatDate = function (datefield, dmy) {
166a7a3d6dbSGreg Roach    const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
167a7a3d6dbSGreg Roach    const hijri_months = ['MUHAR', 'SAFAR', 'RABIA', 'RABIT', 'JUMAA', 'JUMAT', 'RAJAB', 'SHAAB', 'RAMAD', 'SHAWW', 'DHUAQ', 'DHUAH'];
168a7a3d6dbSGreg Roach    const hebrew_months = ['TSH', 'CSH', 'KSL', 'TVT', 'SHV', 'ADR', 'ADS', 'NSN', 'IYR', 'SVN', 'TMZ', 'AAV', 'ELL'];
169a7a3d6dbSGreg Roach    const french_months = ['VEND', 'BRUM', 'FRIM', 'NIVO', 'PLUV', 'VENT', 'GERM', 'FLOR', 'PRAI', 'MESS', 'THER', 'FRUC', 'COMP'];
170a7a3d6dbSGreg Roach    const jalali_months = ['FARVA', 'ORDIB', 'KHORD', 'TIR', 'MORDA', 'SHAHR', 'MEHR', 'ABAN', 'AZAR', 'DEY', 'BAHMA', 'ESFAN'];
17171239cb6SGreg Roach
172a7a3d6dbSGreg Roach    let datestr = datefield.value;
17371239cb6SGreg Roach    // if a date has a date phrase marked by () this has to be excluded from altering
174a7a3d6dbSGreg Roach    let datearr = datestr.split('(');
175a7a3d6dbSGreg Roach    let datephrase = '';
17671239cb6SGreg Roach    if (datearr.length > 1) {
17771239cb6SGreg Roach      datestr = datearr[0];
17871239cb6SGreg Roach      datephrase = datearr[1];
17971239cb6SGreg Roach    }
18071239cb6SGreg Roach
18171239cb6SGreg Roach    // Gedcom dates are upper case
18271239cb6SGreg Roach    datestr = datestr.toUpperCase();
18371239cb6SGreg Roach    // Gedcom dates have no leading/trailing/repeated whitespace
18480d699d6SGreg Roach    datestr = datestr.replace(/\s+/g, ' ');
18571239cb6SGreg Roach    datestr = datestr.replace(/(^\s)|(\s$)/, '');
18671239cb6SGreg Roach    // Gedcom dates have spaces between letters and digits, e.g. "01JAN2000" => "01 JAN 2000"
18780d699d6SGreg Roach    datestr = datestr.replace(/(\d)([A-Z])/g, '$1 $2');
18880d699d6SGreg Roach    datestr = datestr.replace(/([A-Z])(\d)/g, '$1 $2');
18971239cb6SGreg Roach
19045a1224aSGreg Roach    // Shortcut for quarter format, "Q1 1900" => "BET JAN 1900 AND MAR 1900".
19171239cb6SGreg Roach    if (datestr.match(/^Q ([1-4]) (\d\d\d\d)$/)) {
19271239cb6SGreg Roach      datestr = 'BET ' + months[RegExp.$1 * 3 - 3] + ' ' + RegExp.$2 + ' AND ' + months[RegExp.$1 * 3 - 1] + ' ' + RegExp.$2;
19371239cb6SGreg Roach    }
19471239cb6SGreg Roach
19571239cb6SGreg Roach    // Shortcut for @#Dxxxxx@ 01 01 1400, etc.
19671239cb6SGreg Roach    if (datestr.match(/^(@#DHIJRI@|HIJRI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
19771239cb6SGreg Roach      datestr = '@#DHIJRI@' + RegExp.$2 + hijri_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
19871239cb6SGreg Roach    }
19971239cb6SGreg Roach    if (datestr.match(/^(@#DJALALI@|JALALI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
20071239cb6SGreg Roach      datestr = '@#DJALALI@' + RegExp.$2 + jalali_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
20171239cb6SGreg Roach    }
20271239cb6SGreg Roach    if (datestr.match(/^(@#DHEBREW@|HEBREW)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
20371239cb6SGreg Roach      datestr = '@#DHEBREW@' + RegExp.$2 + hebrew_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
20471239cb6SGreg Roach    }
20571239cb6SGreg Roach    if (datestr.match(/^(@#DFRENCH R@|FRENCH)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) {
20671239cb6SGreg Roach      datestr = '@#DFRENCH R@' + RegExp.$2 + french_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4;
20771239cb6SGreg Roach    }
20871239cb6SGreg Roach
2097fb78f8aSGreg Roach    // All digit dates
2104ad647d5SGreg Roach    datestr = datestr.replace(/(\d\d)(\d\d)(\d\d)(\d\d)/g, function () {
2117fb78f8aSGreg Roach      if (RegExp.$1 > '12' && RegExp.$3 <= '12' && RegExp.$4 <= '31') {
2127fb78f8aSGreg Roach        return RegExp.$4 + ' ' + months[RegExp.$3 - 1] + ' ' + RegExp.$1 + RegExp.$2;
2137fb78f8aSGreg Roach      }
2147fb78f8aSGreg Roach      if (RegExp.$1 <= '31' && RegExp.$2 <= '12' && RegExp.$3 > '12') {
2157fb78f8aSGreg Roach        return RegExp.$1 + ' ' + months[RegExp.$2 - 1] + ' ' + RegExp.$3 + RegExp.$4;
2167fb78f8aSGreg Roach      }
2177fb78f8aSGreg Roach      return RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4;
2187fb78f8aSGreg Roach    });
2197fb78f8aSGreg Roach
22045a1224aSGreg Roach    // e.g. 17.11.1860, 2 4 1987, 3/4/2005, 1999-12-31. Use locale settings since DMY order is ambiguous.
22145a1224aSGreg Roach    datestr = datestr.replace(/(\d+)([ ./-])(\d+)(\2)(\d+)/g, function () {
2222cf1b3d7SGreg Roach      let f1 = parseInt(RegExp.$1, 10);
2232cf1b3d7SGreg Roach      let f2 = parseInt(RegExp.$3, 10);
2242cf1b3d7SGreg Roach      let f3 = parseInt(RegExp.$5, 10);
2252cf1b3d7SGreg Roach      let yyyy = new Date().getFullYear();
2262cf1b3d7SGreg Roach      let yy = yyyy % 100;
2272cf1b3d7SGreg Roach      let cc = yyyy - yy;
228dee63285SGreg Roach      if ((dmy === 'DMY' || f1 > 13 && f3 > 31) && f1 <= 31 && f2 <= 12) {
2297fb78f8aSGreg Roach        return f1 + ' ' + months[f2 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100));
2307fb78f8aSGreg Roach      }
231dee63285SGreg Roach      if ((dmy === 'MDY' || f2 > 13 && f3 > 31) && f1 <= 12 && f2 <= 31) {
2327fb78f8aSGreg Roach        return f2 + ' ' + months[f1 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100));
2337fb78f8aSGreg Roach      }
234dee63285SGreg Roach      if ((dmy === 'YMD' || f1 > 31) && f2 <= 12 && f3 <= 31) {
2357fb78f8aSGreg Roach        return f3 + ' ' + months[f2 - 1] + ' ' + (f1 >= 100 ? f1 : (f1 <= yy ? f1 + cc : f1 + cc - 100));
23671239cb6SGreg Roach      }
2377fb78f8aSGreg Roach      return RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4 + RegExp.$5;
2387fb78f8aSGreg Roach    });
23971239cb6SGreg Roach
240a7a3d6dbSGreg Roach    datestr = datestr
24171239cb6SGreg Roach      // Shortcuts for date ranges
242a7a3d6dbSGreg Roach      .replace(/^[>]([\w ]+)$/, 'AFT $1')
243a7a3d6dbSGreg Roach      .replace(/^[<]([\w ]+)$/, 'BEF $1')
244a7a3d6dbSGreg Roach      .replace(/^([\w ]+)[-]$/, 'FROM $1')
245a7a3d6dbSGreg Roach      .replace(/^[-]([\w ]+)$/, 'TO $1')
246a7a3d6dbSGreg Roach      .replace(/^[~]([\w ]+)$/, 'ABT $1')
247a7a3d6dbSGreg Roach      .replace(/^[*]([\w ]+)$/, 'EST $1')
248a7a3d6dbSGreg Roach      .replace(/^[#]([\w ]+)$/, 'CAL $1')
249a7a3d6dbSGreg Roach      .replace(/^([\w ]+) ?- ?([\w ]+)$/, 'BET $1 AND $2')
250a7a3d6dbSGreg Roach      .replace(/^([\w ]+) ?~ ?([\w ]+)$/, 'FROM $1 TO $2')
25171239cb6SGreg Roach      // Convert full months to short months
2524ad647d5SGreg Roach      .replace(/JANUARY/g, 'JAN')
2534ad647d5SGreg Roach      .replace(/FEBRUARY/g, 'FEB')
2544ad647d5SGreg Roach      .replace(/MARCH/g, 'MAR')
2554ad647d5SGreg Roach      .replace(/APRIL/g, 'APR')
2564ad647d5SGreg Roach      .replace(/JUNE/g, 'JUN')
2574ad647d5SGreg Roach      .replace(/JULY/g, 'JUL')
2584ad647d5SGreg Roach      .replace(/AUGUST/g, 'AUG')
2594ad647d5SGreg Roach      .replace(/SEPTEMBER/g, 'SEP')
2604ad647d5SGreg Roach      .replace(/OCTOBER/, 'OCT')
2614ad647d5SGreg Roach      .replace(/NOVEMBER/g, 'NOV')
2624ad647d5SGreg Roach      .replace(/DECEMBER/g, 'DEC')
263a7a3d6dbSGreg Roach      // Americans enter dates as SEP 20, 1999
2644ad647d5SGreg Roach      .replace(/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\.? (\d\d?)[, ]+(\d\d\d\d)/g, '$2 $1 $3')
26571239cb6SGreg Roach      // Apply leading zero to day numbers
2664ad647d5SGreg Roach      .replace(/(^| )(\d [A-Z]{3,5} \d{4})/g, '$10$2');
26771239cb6SGreg Roach
26871239cb6SGreg Roach    if (datephrase) {
26971239cb6SGreg Roach      datestr = datestr + ' (' + datephrase;
27071239cb6SGreg Roach    }
271a7a3d6dbSGreg Roach
27271239cb6SGreg Roach    // Only update it if is has been corrected - otherwise input focus
27371239cb6SGreg Roach    // moves to the end of the field unnecessarily
27471239cb6SGreg Roach    if (datefield.value !== datestr) {
27571239cb6SGreg Roach      datefield.value = datestr;
27671239cb6SGreg Roach    }
2772cf1b3d7SGreg Roach  };
27871239cb6SGreg Roach
2792cf1b3d7SGreg Roach  let monthLabels = [];
28071239cb6SGreg Roach  monthLabels[1] = 'January';
28171239cb6SGreg Roach  monthLabels[2] = 'February';
28271239cb6SGreg Roach  monthLabels[3] = 'March';
28371239cb6SGreg Roach  monthLabels[4] = 'April';
28471239cb6SGreg Roach  monthLabels[5] = 'May';
28571239cb6SGreg Roach  monthLabels[6] = 'June';
28671239cb6SGreg Roach  monthLabels[7] = 'July';
28771239cb6SGreg Roach  monthLabels[8] = 'August';
28871239cb6SGreg Roach  monthLabels[9] = 'September';
28971239cb6SGreg Roach  monthLabels[10] = 'October';
29071239cb6SGreg Roach  monthLabels[11] = 'November';
29171239cb6SGreg Roach  monthLabels[12] = 'December';
29271239cb6SGreg Roach
2932cf1b3d7SGreg Roach  let monthShort = [];
29471239cb6SGreg Roach  monthShort[1] = 'JAN';
29571239cb6SGreg Roach  monthShort[2] = 'FEB';
29671239cb6SGreg Roach  monthShort[3] = 'MAR';
29771239cb6SGreg Roach  monthShort[4] = 'APR';
29871239cb6SGreg Roach  monthShort[5] = 'MAY';
29971239cb6SGreg Roach  monthShort[6] = 'JUN';
30071239cb6SGreg Roach  monthShort[7] = 'JUL';
30171239cb6SGreg Roach  monthShort[8] = 'AUG';
30271239cb6SGreg Roach  monthShort[9] = 'SEP';
30371239cb6SGreg Roach  monthShort[10] = 'OCT';
30471239cb6SGreg Roach  monthShort[11] = 'NOV';
30571239cb6SGreg Roach  monthShort[12] = 'DEC';
30671239cb6SGreg Roach
3072cf1b3d7SGreg Roach  let daysOfWeek = [];
30871239cb6SGreg Roach  daysOfWeek[0] = 'S';
30971239cb6SGreg Roach  daysOfWeek[1] = 'M';
31071239cb6SGreg Roach  daysOfWeek[2] = 'T';
31171239cb6SGreg Roach  daysOfWeek[3] = 'W';
31271239cb6SGreg Roach  daysOfWeek[4] = 'T';
31371239cb6SGreg Roach  daysOfWeek[5] = 'F';
31471239cb6SGreg Roach  daysOfWeek[6] = 'S';
31571239cb6SGreg Roach
3162cf1b3d7SGreg Roach  let weekStart = 0;
31771239cb6SGreg Roach
31806fe1eb5SGreg Roach  /**
31906fe1eb5SGreg Roach   * @param {string} jan
32006fe1eb5SGreg Roach   * @param {string} feb
32106fe1eb5SGreg Roach   * @param {string} mar
32206fe1eb5SGreg Roach   * @param {string} apr
32306fe1eb5SGreg Roach   * @param {string} may
32406fe1eb5SGreg Roach   * @param {string} jun
32506fe1eb5SGreg Roach   * @param {string} jul
32606fe1eb5SGreg Roach   * @param {string} aug
32706fe1eb5SGreg Roach   * @param {string} sep
32806fe1eb5SGreg Roach   * @param {string} oct
32906fe1eb5SGreg Roach   * @param {string} nov
33006fe1eb5SGreg Roach   * @param {string} dec
3312cf1b3d7SGreg Roach   * @param {string} sun
3322cf1b3d7SGreg Roach   * @param {string} mon
3332cf1b3d7SGreg Roach   * @param {string} tue
3342cf1b3d7SGreg Roach   * @param {string} wed
3352cf1b3d7SGreg Roach   * @param {string} thu
3362cf1b3d7SGreg Roach   * @param {string} fri
3372cf1b3d7SGreg Roach   * @param {string} sat
3382cf1b3d7SGreg Roach   * @param {number} day
33906fe1eb5SGreg Roach   */
3402cf1b3d7SGreg Roach  webtrees.calLocalize = function (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec, sun, mon, tue, wed, thu, fri, sat, day) {
34171239cb6SGreg Roach    monthLabels[1] = jan;
34271239cb6SGreg Roach    monthLabels[2] = feb;
34371239cb6SGreg Roach    monthLabels[3] = mar;
34471239cb6SGreg Roach    monthLabels[4] = apr;
34571239cb6SGreg Roach    monthLabels[5] = may;
34671239cb6SGreg Roach    monthLabels[6] = jun;
34771239cb6SGreg Roach    monthLabels[7] = jul;
34871239cb6SGreg Roach    monthLabels[8] = aug;
34971239cb6SGreg Roach    monthLabels[9] = sep;
35071239cb6SGreg Roach    monthLabels[10] = oct;
35171239cb6SGreg Roach    monthLabels[11] = nov;
35271239cb6SGreg Roach    monthLabels[12] = dec;
35371239cb6SGreg Roach    daysOfWeek[0] = sun;
35471239cb6SGreg Roach    daysOfWeek[1] = mon;
35571239cb6SGreg Roach    daysOfWeek[2] = tue;
35671239cb6SGreg Roach    daysOfWeek[3] = wed;
35771239cb6SGreg Roach    daysOfWeek[4] = thu;
35871239cb6SGreg Roach    daysOfWeek[5] = fri;
35971239cb6SGreg Roach    daysOfWeek[6] = sat;
36071239cb6SGreg Roach
36171239cb6SGreg Roach    if (day >= 0 && day < 7) {
36271239cb6SGreg Roach      weekStart = day;
36371239cb6SGreg Roach    }
3642cf1b3d7SGreg Roach  };
36571239cb6SGreg Roach
36606fe1eb5SGreg Roach  /**
36706fe1eb5SGreg Roach   * @param {string} dateDivId
36806fe1eb5SGreg Roach   * @param {string} dateFieldId
36906fe1eb5SGreg Roach   * @returns {boolean}
37006fe1eb5SGreg Roach   */
3712cf1b3d7SGreg Roach  webtrees.calendarWidget = function (dateDivId, dateFieldId) {
3722cf1b3d7SGreg Roach    let dateDiv = document.getElementById(dateDivId);
3732cf1b3d7SGreg Roach    let dateField = document.getElementById(dateFieldId);
37471239cb6SGreg Roach
37571239cb6SGreg Roach    if (dateDiv.style.visibility === 'visible') {
37671239cb6SGreg Roach      dateDiv.style.visibility = 'hidden';
37771239cb6SGreg Roach      return false;
37871239cb6SGreg Roach    }
37971239cb6SGreg Roach    if (dateDiv.style.visibility === 'show') {
38071239cb6SGreg Roach      dateDiv.style.visibility = 'hide';
38171239cb6SGreg Roach      return false;
38271239cb6SGreg Roach    }
38371239cb6SGreg Roach
38471239cb6SGreg Roach    /* Javascript calendar functions only work with precise gregorian dates "D M Y" or "Y" */
385f4ac98a5SGreg Roach    let greg_regex = /(?:(\d*) ?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) )?(\d+)/i;
3862cf1b3d7SGreg Roach    let date;
38771239cb6SGreg Roach    if (greg_regex.exec(dateField.value)) {
388f4ac98a5SGreg Roach      let day   = RegExp.$1 || '1';
389f4ac98a5SGreg Roach      let month = RegExp.$2 || 'JAN'
390f4ac98a5SGreg Roach      let year  = RegExp.$3;
391f4ac98a5SGreg Roach      date = new Date(day + ' ' + month + ' ' + year);
39271239cb6SGreg Roach    } else {
39371239cb6SGreg Roach      date = new Date();
39471239cb6SGreg Roach    }
39571239cb6SGreg Roach
3962cf1b3d7SGreg Roach    dateDiv.innerHTML = calGenerateSelectorContent(dateFieldId, dateDivId, date);
39771239cb6SGreg Roach    if (dateDiv.style.visibility === 'hidden') {
39871239cb6SGreg Roach      dateDiv.style.visibility = 'visible';
39971239cb6SGreg Roach      return false;
40071239cb6SGreg Roach    }
40171239cb6SGreg Roach    if (dateDiv.style.visibility === 'hide') {
40271239cb6SGreg Roach      dateDiv.style.visibility = 'show';
40371239cb6SGreg Roach      return false;
40471239cb6SGreg Roach    }
40571239cb6SGreg Roach
40671239cb6SGreg Roach    return false;
4072cf1b3d7SGreg Roach  };
40871239cb6SGreg Roach
40906fe1eb5SGreg Roach  /**
41006fe1eb5SGreg Roach   * @param {string} dateFieldId
41106fe1eb5SGreg Roach   * @param {string} dateDivId
41206fe1eb5SGreg Roach   * @param {Date} date
41306fe1eb5SGreg Roach   * @returns {string}
41406fe1eb5SGreg Roach   */
4152cf1b3d7SGreg Roach  function calGenerateSelectorContent (dateFieldId, dateDivId, date) {
4162cf1b3d7SGreg Roach    let i, j;
4172cf1b3d7SGreg Roach    let content = '<table border="1"><tr>';
4184b9213b3SGreg Roach    content += '<td><select class="form-select" id="' + dateFieldId + '_daySelect" onchange="return webtrees.calUpdateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">';
41971239cb6SGreg Roach    for (i = 1; i < 32; i++) {
42071239cb6SGreg Roach      content += '<option value="' + i + '"';
42171239cb6SGreg Roach      if (date.getDate() === i) {
42271239cb6SGreg Roach        content += ' selected="selected"';
42371239cb6SGreg Roach      }
42471239cb6SGreg Roach      content += '>' + i + '</option>';
42571239cb6SGreg Roach    }
42671239cb6SGreg Roach    content += '</select></td>';
4274b9213b3SGreg Roach    content += '<td><select class="form-select" id="' + dateFieldId + '_monSelect" onchange="return webtrees.calUpdateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">';
42871239cb6SGreg Roach    for (i = 1; i < 13; i++) {
42971239cb6SGreg Roach      content += '<option value="' + i + '"';
43071239cb6SGreg Roach      if (date.getMonth() + 1 === i) {
43171239cb6SGreg Roach        content += ' selected="selected"';
43271239cb6SGreg Roach      }
43371239cb6SGreg Roach      content += '>' + monthLabels[i] + '</option>';
43471239cb6SGreg Roach    }
43571239cb6SGreg Roach    content += '</select></td>';
4362cf1b3d7SGreg Roach    content += '<td><input class="form-control" type="text" id="' + dateFieldId + '_yearInput" size="5" value="' + date.getFullYear() + '" onchange="return webtrees.calUpdateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');" /></td></tr>';
43771239cb6SGreg Roach    content += '<tr><td colspan="3">';
43871239cb6SGreg Roach    content += '<table width="100%">';
43971239cb6SGreg Roach    content += '<tr>';
44071239cb6SGreg Roach    j = weekStart;
44171239cb6SGreg Roach    for (i = 0; i < 7; i++) {
44271239cb6SGreg Roach      content += '<td ';
44371239cb6SGreg Roach      content += 'class="descriptionbox"';
44471239cb6SGreg Roach      content += '>';
44571239cb6SGreg Roach      content += daysOfWeek[j];
44671239cb6SGreg Roach      content += '</td>';
44771239cb6SGreg Roach      j++;
44871239cb6SGreg Roach      if (j > 6) {
44971239cb6SGreg Roach        j = 0;
45071239cb6SGreg Roach      }
45171239cb6SGreg Roach    }
45271239cb6SGreg Roach    content += '</tr>';
45371239cb6SGreg Roach
4542cf1b3d7SGreg Roach    let tdate = new Date(date.getFullYear(), date.getMonth(), 1);
4552cf1b3d7SGreg Roach    let day = tdate.getDay();
45671239cb6SGreg Roach    day = day - weekStart;
4572cf1b3d7SGreg Roach    let daymilli = 1000 * 60 * 60 * 24;
45871239cb6SGreg Roach    tdate = tdate.getTime() - (day * daymilli) + (daymilli / 2);
45971239cb6SGreg Roach    tdate = new Date(tdate);
46071239cb6SGreg Roach
46171239cb6SGreg Roach    for (j = 0; j < 6; j++) {
46271239cb6SGreg Roach      content += '<tr>';
46371239cb6SGreg Roach      for (i = 0; i < 7; i++) {
46471239cb6SGreg Roach        content += '<td ';
46571239cb6SGreg Roach        if (tdate.getMonth() === date.getMonth()) {
46671239cb6SGreg Roach          if (tdate.getDate() === date.getDate()) {
46771239cb6SGreg Roach            content += 'class="descriptionbox"';
46871239cb6SGreg Roach          } else {
46971239cb6SGreg Roach            content += 'class="optionbox"';
47071239cb6SGreg Roach          }
47171239cb6SGreg Roach        } else {
47271239cb6SGreg Roach          content += 'style="background-color:#EAEAEA; border: solid #AAAAAA 1px;"';
47371239cb6SGreg Roach        }
4742cf1b3d7SGreg Roach        content += '><a href="#" onclick="return webtrees.calDateClicked(\'' + dateFieldId + '\', \'' + dateDivId + '\', ' + tdate.getFullYear() + ', ' + tdate.getMonth() + ', ' + tdate.getDate() + ');">';
47571239cb6SGreg Roach        content += tdate.getDate();
47671239cb6SGreg Roach        content += '</a></td>';
4772cf1b3d7SGreg Roach        let datemilli = tdate.getTime() + daymilli;
47871239cb6SGreg Roach        tdate = new Date(datemilli);
47971239cb6SGreg Roach      }
48071239cb6SGreg Roach      content += '</tr>';
48171239cb6SGreg Roach    }
48271239cb6SGreg Roach    content += '</table>';
48371239cb6SGreg Roach    content += '</td></tr>';
48471239cb6SGreg Roach    content += '</table>';
48571239cb6SGreg Roach
48671239cb6SGreg Roach    return content;
48771239cb6SGreg Roach  }
48871239cb6SGreg Roach
48906fe1eb5SGreg Roach  /**
49006fe1eb5SGreg Roach   * @param {string} dateFieldId
49106fe1eb5SGreg Roach   * @param {number} year
49206fe1eb5SGreg Roach   * @param {number} month
49306fe1eb5SGreg Roach   * @param {number} day
49406fe1eb5SGreg Roach   * @returns {boolean}
49506fe1eb5SGreg Roach   */
4962cf1b3d7SGreg Roach  function calSetDateField (dateFieldId, year, month, day) {
4972cf1b3d7SGreg Roach    let dateField = document.getElementById(dateFieldId);
4982cf1b3d7SGreg Roach    dateField.value = (day < 10 ? '0' : '') + day + ' ' + monthShort[month + 1] + ' ' + year;
49971239cb6SGreg Roach    return false;
50071239cb6SGreg Roach  }
50171239cb6SGreg Roach
50206fe1eb5SGreg Roach  /**
50306fe1eb5SGreg Roach   * @param {string} dateFieldId
50406fe1eb5SGreg Roach   * @param {string} dateDivId
50506fe1eb5SGreg Roach   * @returns {boolean}
50606fe1eb5SGreg Roach   */
5072cf1b3d7SGreg Roach  webtrees.calUpdateCalendar = function (dateFieldId, dateDivId) {
5082cf1b3d7SGreg Roach    let dateSel = document.getElementById(dateFieldId + '_daySelect');
50971239cb6SGreg Roach    if (!dateSel) {
51071239cb6SGreg Roach      return false;
51171239cb6SGreg Roach    }
5122cf1b3d7SGreg Roach    let monthSel = document.getElementById(dateFieldId + '_monSelect');
51371239cb6SGreg Roach    if (!monthSel) {
51471239cb6SGreg Roach      return false;
51571239cb6SGreg Roach    }
5162cf1b3d7SGreg Roach    let yearInput = document.getElementById(dateFieldId + '_yearInput');
51771239cb6SGreg Roach    if (!yearInput) {
51871239cb6SGreg Roach      return false;
51971239cb6SGreg Roach    }
52071239cb6SGreg Roach
5212cf1b3d7SGreg Roach    let month = parseInt(monthSel.options[monthSel.selectedIndex].value, 10);
52271239cb6SGreg Roach    month = month - 1;
52371239cb6SGreg Roach
5242cf1b3d7SGreg Roach    let date = new Date(yearInput.value, month, dateSel.options[dateSel.selectedIndex].value);
5252cf1b3d7SGreg Roach    calSetDateField(dateFieldId, date.getFullYear(), date.getMonth(), date.getDate());
52671239cb6SGreg Roach
5272cf1b3d7SGreg Roach    let dateDiv = document.getElementById(dateDivId);
52871239cb6SGreg Roach    if (!dateDiv) {
52971239cb6SGreg Roach      alert('no dateDiv ' + dateDivId);
53071239cb6SGreg Roach      return false;
53171239cb6SGreg Roach    }
5322cf1b3d7SGreg Roach    dateDiv.innerHTML = calGenerateSelectorContent(dateFieldId, dateDivId, date);
53371239cb6SGreg Roach
53471239cb6SGreg Roach    return false;
5352cf1b3d7SGreg Roach  };
53671239cb6SGreg Roach
53706fe1eb5SGreg Roach  /**
53806fe1eb5SGreg Roach   * @param {string} dateFieldId
53906fe1eb5SGreg Roach   * @param {string} dateDivId
54006fe1eb5SGreg Roach   * @param {number} year
54106fe1eb5SGreg Roach   * @param {number} month
54206fe1eb5SGreg Roach   * @param {number} day
54306fe1eb5SGreg Roach   * @returns {boolean}
54406fe1eb5SGreg Roach   */
5452cf1b3d7SGreg Roach  webtrees.calDateClicked = function (dateFieldId, dateDivId, year, month, day) {
5462cf1b3d7SGreg Roach    calSetDateField(dateFieldId, year, month, day);
5472cf1b3d7SGreg Roach    webtrees.calendarWidget(dateDivId, dateFieldId);
54871239cb6SGreg Roach    return false;
5492cf1b3d7SGreg Roach  };
55071239cb6SGreg Roach
55106fe1eb5SGreg Roach  /**
552f0ecc9a9SGreg Roach   * Make bootstrap "collapse" elements persistent.
553f0ecc9a9SGreg Roach   *
554f0ecc9a9SGreg Roach   * @param {HTMLElement} element
55571239cb6SGreg Roach   */
5562d8276baSGreg Roach  webtrees.persistentToggle = function (element) {
5572d8276baSGreg Roach    const key = 'state-of-' + element.dataset.wtPersist;
558f0ecc9a9SGreg Roach    const previous_state = localStorage.getItem(key);
559f0ecc9a9SGreg Roach
560f0ecc9a9SGreg Roach    // Accordion buttons have aria-expanded.  Checkboxes are checked/unchecked
561f0ecc9a9SGreg Roach    const current_state = element.getAttribute('aria-expanded') ?? element.checked.toString();
56271239cb6SGreg Roach
5632d8276baSGreg Roach    // Previously selected? Select again now.
564e107bf98SGreg Roach    if (previous_state !== null && previous_state !== current_state) {
5652cf1b3d7SGreg Roach      element.click();
56671239cb6SGreg Roach    }
56771239cb6SGreg Roach
56864490ee2SGreg Roach    // Remember state for the next page load.
569f0ecc9a9SGreg Roach    element.addEventListener('click', function () {
570f0ecc9a9SGreg Roach      if (element.type === 'checkbox') {
5712d8276baSGreg Roach        localStorage.setItem(key, element.checked.toString());
572adbf37b7SGreg Roach      }
573f0ecc9a9SGreg Roach      if (element.type === 'button') {
574f0ecc9a9SGreg Roach        localStorage.setItem(key, element.getAttribute('aria-expanded'));
575f0ecc9a9SGreg Roach      }
576f0ecc9a9SGreg Roach    });
5772cf1b3d7SGreg Roach  };
57871239cb6SGreg Roach
57906fe1eb5SGreg Roach  /**
5802cf1b3d7SGreg Roach   * @param {Element} field
58106fe1eb5SGreg Roach   * @param {string} pos
58206fe1eb5SGreg Roach   * @param {string} neg
58306fe1eb5SGreg Roach   */
5842cf1b3d7SGreg Roach  function reformatLatLong (field, pos, neg) {
58571239cb6SGreg Roach    // valid LATI or LONG according to Gedcom standard
58671239cb6SGreg Roach    // pos (+) : N or E
58771239cb6SGreg Roach    // neg (-) : S or W
5882cf1b3d7SGreg Roach    let txt = field.value.toUpperCase();
58971239cb6SGreg Roach    txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim
59071239cb6SGreg Roach    txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34
59171239cb6SGreg Roach    txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234
59271239cb6SGreg Roach    txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698
59371239cb6SGreg Roach    txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698
59471239cb6SGreg Roach    // 0°34'11 ==> 0:34:11
59571239cb6SGreg Roach    txt = txt.replace(/\u00b0/g, ':'); // °
59671239cb6SGreg Roach    txt = txt.replace(/\u0027/g, ':'); // '
59771239cb6SGreg Roach    // 0:34:11.2W ==> W0.5698
59871239cb6SGreg Roach    txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) {
5992cf1b3d7SGreg Roach      let n = parseFloat($1);
60071239cb6SGreg Roach      n += ($2 / 60);
60171239cb6SGreg Roach      n += ($3 / 3600);
60271239cb6SGreg Roach      n = Math.round(n * 1E4) / 1E4;
60371239cb6SGreg Roach      return $4 + n;
60471239cb6SGreg Roach    });
60571239cb6SGreg Roach    // 0:34W ==> W0.5667
60671239cb6SGreg Roach    txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) {
6072cf1b3d7SGreg Roach      let n = parseFloat($1);
60871239cb6SGreg Roach      n += ($2 / 60);
60971239cb6SGreg Roach      n = Math.round(n * 1E4) / 1E4;
61071239cb6SGreg Roach      return $3 + n;
61171239cb6SGreg Roach    });
61271239cb6SGreg Roach    // 0.5698W ==> W0.5698
6132cf1b3d7SGreg Roach    txt = txt.replace(/(.*)(NSEW])$/g, '$2$1');
61471239cb6SGreg Roach    // 17.1234 ==> N17.1234
61571239cb6SGreg Roach    if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) {
61671239cb6SGreg Roach      txt = pos + txt;
61771239cb6SGreg Roach    }
61871239cb6SGreg Roach    field.value = txt;
61971239cb6SGreg Roach  }
62071239cb6SGreg Roach
62106fe1eb5SGreg Roach  /**
6222cf1b3d7SGreg Roach   * @param {Element} field
6232cf1b3d7SGreg Roach   */
6242cf1b3d7SGreg Roach  webtrees.reformatLatitude = function (field) {
6252cf1b3d7SGreg Roach    return reformatLatLong(field, 'N', 'S');
6262cf1b3d7SGreg Roach  };
6272cf1b3d7SGreg Roach
6282cf1b3d7SGreg Roach  /**
6292cf1b3d7SGreg Roach   * @param {Element} field
6302cf1b3d7SGreg Roach   */
6312cf1b3d7SGreg Roach  webtrees.reformatLongitude = function (field) {
6322cf1b3d7SGreg Roach    return reformatLatLong(field, 'E', 'W');
6332cf1b3d7SGreg Roach  };
6342cf1b3d7SGreg Roach
6352cf1b3d7SGreg Roach  /**
63606fe1eb5SGreg Roach   * Initialize autocomplete elements.
63706fe1eb5SGreg Roach   * @param {string} selector
63806fe1eb5SGreg Roach   */
6392cf1b3d7SGreg Roach  webtrees.autocomplete = function (selector) {
64071239cb6SGreg Roach    // Use typeahead/bloodhound for autocomplete
64171239cb6SGreg Roach    $(selector).each(function () {
642efd89170SGreg Roach      const that = this;
64371239cb6SGreg Roach      $(this).typeahead(null, {
64471239cb6SGreg Roach        display: 'value',
64563763244SGreg Roach        limit: 10,
64663763244SGreg Roach        minLength: 2,
64771239cb6SGreg Roach        source: new Bloodhound({
64871239cb6SGreg Roach          datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
64971239cb6SGreg Roach          queryTokenizer: Bloodhound.tokenizers.whitespace,
65071239cb6SGreg Roach          remote: {
651d4786c66SGreg Roach            url: this.dataset.wtAutocompleteUrl,
652f4abaf12SGreg Roach            replace: function (url, uriEncodedQuery) {
653f9b64f46SGreg Roach              const symbol = (url.indexOf("?") > 0) ? '&' : '?';
654d4786c66SGreg Roach              if (that.dataset.wtAutocompleteExtra === 'SOUR') {
655561ec906SGreg Roach                let row_group = that.closest('.wt-nested-edit-fields').previousElementSibling;
656d99ef095SGreg Roach                while (row_group.querySelector('select') === null) {
657d99ef095SGreg Roach                  row_group = row_group.previousElementSibling;
658d99ef095SGreg Roach                }
659d99ef095SGreg Roach                const element = row_group.querySelector('select');
66082dda228SJonathan Jaubart                const extra   = element.options[element.selectedIndex].value.replace(/@/g, '');
661f9b64f46SGreg Roach                return url + symbol + "query=" + uriEncodedQuery + '&extra=' + encodeURIComponent(extra);
662f4abaf12SGreg Roach              }
663f9b64f46SGreg Roach              return url + symbol + "query=" + uriEncodedQuery
664f9b64f46SGreg Roach            }
66571239cb6SGreg Roach          }
66671239cb6SGreg Roach        })
66771239cb6SGreg Roach      });
66871239cb6SGreg Roach    });
6692cf1b3d7SGreg Roach  };
670c9c6f2ecSGreg Roach
671c9c6f2ecSGreg Roach  /**
672c9c6f2ecSGreg Roach   * Create a LeafletJS map from a list of providers/layers.
673c9c6f2ecSGreg Roach   * @param {string} id
674c9c6f2ecSGreg Roach   * @param {object} config
675b7b71725SGreg Roach   * @param {function} resetCallback
676c9c6f2ecSGreg Roach   * @returns Map
677c9c6f2ecSGreg Roach   */
678b7b71725SGreg Roach  webtrees.buildLeafletJsMap = function (id, config, resetCallback) {
679c9c6f2ecSGreg Roach    const zoomControl = new L.control.zoom({
680c9c6f2ecSGreg Roach      zoomInTitle: config.i18n.zoomIn,
681c9c6f2ecSGreg Roach      zoomoutTitle: config.i18n.zoomOut,
682c9c6f2ecSGreg Roach    });
683c9c6f2ecSGreg Roach
684f352d954SDavid Drury    const resetControl = L.Control.extend({
685f352d954SDavid Drury      options: {
686f352d954SDavid Drury        position: 'topleft',
687f352d954SDavid Drury      },
688b2e5f20eSGreg Roach      onAdd: () => {
689053cb27dSDavid Drury        const container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
690053cb27dSDavid Drury        const anchor = L.DomUtil.create('a', 'leaflet-control-reset', container);
691053cb27dSDavid Drury
692b2e5f20eSGreg Roach        anchor.href = '#';
693b2e5f20eSGreg Roach        anchor.setAttribute('aria-label', config.i18n.reset); /* Firefox doesn't yet support element.ariaLabel */
694b2e5f20eSGreg Roach        anchor.title = config.i18n.reset;
695*ac1ce4fbSDavid Drury        anchor.setAttribute('role', 'button');
696053cb27dSDavid Drury        anchor.innerHTML = config.icons.reset;
697945239ddSDavid Drury        anchor.onclick = resetCallback;
698f352d954SDavid Drury
699f352d954SDavid Drury        return container;
700f352d954SDavid Drury      },
701f352d954SDavid Drury    });
702f352d954SDavid Drury
70355ea0389SGreg Roach    const fullscreenControl = L.Control.extend({
704e95f22dbSDavid Drury      options: {
705e95f22dbSDavid Drury        position: 'topleft',
706e95f22dbSDavid Drury      },
707e95f22dbSDavid Drury      onAdd: (map) => {
708e95f22dbSDavid Drury        const container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
709e95f22dbSDavid Drury        const anchor = L.DomUtil.create('a', 'leaflet-control-fullscreen', container);
710e95f22dbSDavid Drury
711*ac1ce4fbSDavid Drury        anchor.href = '#';
712e95f22dbSDavid Drury        anchor.setAttribute('role', 'button');
71355ea0389SGreg Roach        anchor.dataset.wtFullscreen = '.wt-fullscreen-container';
71455ea0389SGreg Roach        anchor.innerHTML = config.icons.fullScreen;
715e95f22dbSDavid Drury
716e95f22dbSDavid Drury        return container;
717e95f22dbSDavid Drury      },
718e95f22dbSDavid Drury    });
719e95f22dbSDavid Drury
720c9c6f2ecSGreg Roach    let defaultLayer = null;
721c9c6f2ecSGreg Roach
722c9c6f2ecSGreg Roach    for (let [, provider] of Object.entries(config.mapProviders)) {
723c9c6f2ecSGreg Roach      for (let [, child] of Object.entries(provider.children)) {
724c9c6f2ecSGreg Roach        if ('bingMapsKey' in child) {
725c9c6f2ecSGreg Roach          child.layer = L.tileLayer.bing(child);
726c9c6f2ecSGreg Roach        } else {
727c9c6f2ecSGreg Roach          child.layer = L.tileLayer(child.url, child);
728c9c6f2ecSGreg Roach        }
729c9c6f2ecSGreg Roach        if (provider.default && child.default) {
730c9c6f2ecSGreg Roach          defaultLayer = child.layer;
731c9c6f2ecSGreg Roach        }
732c9c6f2ecSGreg Roach      }
733c9c6f2ecSGreg Roach    }
734c9c6f2ecSGreg Roach
735c9c6f2ecSGreg Roach    if (defaultLayer === null) {
736c9c6f2ecSGreg Roach      console.log('No default map layer defined - using the first one.');
73701f69943SAndrew Gregory      defaultLayer = config.mapProviders[0].children[0].layer;
738c9c6f2ecSGreg Roach    }
739c9c6f2ecSGreg Roach
740c9c6f2ecSGreg Roach
741c9c6f2ecSGreg Roach    // Create the map with all controls and layers
742c9c6f2ecSGreg Roach    return L.map(id, {
743c9c6f2ecSGreg Roach      zoomControl: false,
744c9c6f2ecSGreg Roach    })
745c9c6f2ecSGreg Roach      .addControl(zoomControl)
746*ac1ce4fbSDavid Drury      .addControl(new fullscreenControl())
747f352d954SDavid Drury      .addControl(new resetControl())
748c9c6f2ecSGreg Roach      .addLayer(defaultLayer)
749c9c6f2ecSGreg Roach      .addControl(L.control.layers.tree(config.mapProviders, null, {
750c9c6f2ecSGreg Roach        closedSymbol: config.icons.expand,
751c9c6f2ecSGreg Roach        openedSymbol: config.icons.collapse,
752c9c6f2ecSGreg Roach      }));
753c9c6f2ecSGreg Roach
754c9c6f2ecSGreg Roach  };
755c8d78f19SGreg Roach
756c8d78f19SGreg Roach  /**
757c8d78f19SGreg Roach   * Initialize a tom-select input
758c8d78f19SGreg Roach   * @param {Element} element
759c8d78f19SGreg Roach   * @returns {TomSelect}
760c8d78f19SGreg Roach   */
761c8d78f19SGreg Roach  webtrees.initializeTomSelect = function (element) {
762c8d78f19SGreg Roach    if (element.tomselect) {
763c8d78f19SGreg Roach      return element.tomselect;
764c8d78f19SGreg Roach    }
765c8d78f19SGreg Roach
766c8d78f19SGreg Roach    if (element.dataset.url) {
7675cf3b11fSGreg Roach      let options = {
7685cf3b11fSGreg Roach        plugins: ['dropdown_input', 'virtual_scroll'],
76933eb849bSGreg Roach        maxOptions: false,
7709a8145f8SGreg Roach        searchField: [], // We filter on the server, so don't filter on the client.
771c8d78f19SGreg Roach        render: {
772c8d78f19SGreg Roach          item: (data, escape) => '<div>' + data.text + '</div>',
773c8d78f19SGreg Roach          option: (data, escape) => '<div>' + data.text + '</div>',
774c8d78f19SGreg Roach        },
775c8d78f19SGreg Roach        firstUrl: query => element.dataset.url + '&query=' + encodeURIComponent(query),
776c8d78f19SGreg Roach        load: function (query, callback) {
777313cf418SGreg Roach          webtrees.httpGet(this.getUrl(query))
778c8d78f19SGreg Roach            .then(response => response.json())
779c8d78f19SGreg Roach            .then(json => {
780640069c3SRichard Cissée              if (json.nextUrl !== null) {
781c8d78f19SGreg Roach                this.setNextUrl(query, json.nextUrl + '&query=' + encodeURIComponent(query));
782640069c3SRichard Cissée              }
783c8d78f19SGreg Roach              callback(json.data);
784c8d78f19SGreg Roach            })
785c8d78f19SGreg Roach            .catch(callback);
786c8d78f19SGreg Roach        },
787c8d78f19SGreg Roach      };
7885cf3b11fSGreg Roach
7895cf3b11fSGreg Roach      if (!element.required) {
7905cf3b11fSGreg Roach        options.plugins.push('clear_button');
791c8d78f19SGreg Roach      }
792c8d78f19SGreg Roach
793c8d78f19SGreg Roach      return new TomSelect(element, options);
794c8d78f19SGreg Roach    }
795c8d78f19SGreg Roach
7965cf3b11fSGreg Roach    if (element.multiple) {
7975cf3b11fSGreg Roach      return new TomSelect(element, { plugins: ['caret_position', 'remove_button'] });
7985cf3b11fSGreg Roach    }
7995cf3b11fSGreg Roach
8005cf3b11fSGreg Roach    if (!element.required) {
8015cf3b11fSGreg Roach      return new TomSelect(element, { plugins: ['clear_button'] });
8025cf3b11fSGreg Roach    }
8035cf3b11fSGreg Roach
8045cf3b11fSGreg Roach    return new TomSelect(element, { });
8055cf3b11fSGreg Roach  }
8065cf3b11fSGreg Roach
807c8d78f19SGreg Roach  /**
808c8d78f19SGreg Roach   * Reset a tom-select input to have a single selected option
809c8d78f19SGreg Roach   * @param {TomSelect} tomSelect
810c8d78f19SGreg Roach   * @param {string} value
811c8d78f19SGreg Roach   * @param {string} text
812c8d78f19SGreg Roach   */
813c8d78f19SGreg Roach  webtrees.resetTomSelect = function (tomSelect, value, text) {
814c8d78f19SGreg Roach    tomSelect.clear(true);
815c8d78f19SGreg Roach    tomSelect.clearOptions();
816c8d78f19SGreg Roach    tomSelect.addOption({ value: value, text: text });
817c8d78f19SGreg Roach    tomSelect.refreshOptions();
818c8d78f19SGreg Roach    tomSelect.addItem(value, true);
819c8d78f19SGreg Roach    tomSelect.refreshItems();
820c8d78f19SGreg Roach  };
821c8d78f19SGreg Roach
822c8d78f19SGreg Roach  /**
823c8d78f19SGreg Roach   * Toggle the visibility/status of INDI/FAM/SOUR/REPO/OBJE selectors
824c8d78f19SGreg Roach   *
825c8d78f19SGreg Roach   * @param {Element} select
826c8d78f19SGreg Roach   * @param {Element} container
827c8d78f19SGreg Roach   */
828c8d78f19SGreg Roach  webtrees.initializeIFSRO = function(select, container) {
829c8d78f19SGreg Roach    select.addEventListener('change', function () {
830c8d78f19SGreg Roach      // Show only the selected selector.
831c8d78f19SGreg Roach      container.querySelectorAll('.select-record').forEach(element => element.classList.add('d-none'));
832c8d78f19SGreg Roach      container.querySelectorAll('.select-' + select.value).forEach(element => element.classList.remove('d-none'));
833a5045593SGreg Roach
834c8d78f19SGreg Roach      // Enable only the selected selector (so that disabled ones do not get submitted).
835c8d78f19SGreg Roach      container.querySelectorAll('.select-record select').forEach(element => {
836c8d78f19SGreg Roach        element.disabled = true;
837a5045593SGreg Roach        if (element.matches('.tom-select')) {
838c8d78f19SGreg Roach          element.tomselect.disable();
839a5045593SGreg Roach        }
840c8d78f19SGreg Roach      });
841c8d78f19SGreg Roach      container.querySelectorAll('.select-' + select.value + ' select').forEach(element => {
842c8d78f19SGreg Roach        element.disabled = false;
843a5045593SGreg Roach        if (element.matches('.tom-select')) {
844c8d78f19SGreg Roach          element.tomselect.enable();
845a5045593SGreg Roach        }
846c8d78f19SGreg Roach      });
847c8d78f19SGreg Roach    });
84845ed5d1dSGreg Roach  };
84945ed5d1dSGreg Roach
85045ed5d1dSGreg Roach  /**
85145ed5d1dSGreg Roach   * Save a form using ajax, for use in modals
85245ed5d1dSGreg Roach   *
85345ed5d1dSGreg Roach   * @param {Event} event
85445ed5d1dSGreg Roach   */
85545ed5d1dSGreg Roach  webtrees.createRecordModalSubmit = function (event) {
85645ed5d1dSGreg Roach    event.preventDefault();
85745ed5d1dSGreg Roach    const form = event.target;
85845ed5d1dSGreg Roach    const modal = document.getElementById('wt-ajax-modal')
85945ed5d1dSGreg Roach    const modal_content = modal.querySelector('.modal-content');
86045ed5d1dSGreg Roach    const select = document.getElementById(modal_content.dataset.wtSelectId);
86145ed5d1dSGreg Roach
86245ed5d1dSGreg Roach    webtrees.httpPost(form.action, new FormData(form))
86345ed5d1dSGreg Roach      .then(response => response.json())
86445ed5d1dSGreg Roach      .then(json => {
86545ed5d1dSGreg Roach        if (select) {
86645ed5d1dSGreg Roach          // This modal was activated by the "create new" button in a select edit control.
86745ed5d1dSGreg Roach          webtrees.resetTomSelect(select.tomselect, json.value, json.text);
86845ed5d1dSGreg Roach
86945ed5d1dSGreg Roach          bootstrap.Modal.getInstance(modal).hide();
87045ed5d1dSGreg Roach        } else {
87145ed5d1dSGreg Roach          // Show the success message in the existing modal.
87245ed5d1dSGreg Roach          modal_content.innerHTML = json.html;
873c8d78f19SGreg Roach        }
87445ed5d1dSGreg Roach      })
87545ed5d1dSGreg Roach      .catch(error => {
87645ed5d1dSGreg Roach        modal_content.innerHTML = error;
87745ed5d1dSGreg Roach      });
87845ed5d1dSGreg Roach  };
87948c46458SGreg Roach
88048c46458SGreg Roach  /**
88148c46458SGreg Roach   * Text areas don't support the pattern attribute, so apply it manually via data-wt-pattern.
88248c46458SGreg Roach   *
88348c46458SGreg Roach   * @param {HTMLFormElement} form
88448c46458SGreg Roach   */
88548c46458SGreg Roach  webtrees.textareaPatterns = function (form) {
88648c46458SGreg Roach    form.addEventListener('submit', function (event) {
88748c46458SGreg Roach      event.target.querySelectorAll('textarea[data-wt-pattern]').forEach(function (element) {
88848c46458SGreg Roach        const pattern = new RegExp('^' + element.dataset.wtPattern + '$');
88948c46458SGreg Roach
89048c46458SGreg Roach        if (!element.readOnly && element.value !== '' && !pattern.test(element.value)) {
89148c46458SGreg Roach          event.preventDefault();
89248c46458SGreg Roach          event.stopPropagation();
89348c46458SGreg Roach          element.classList.add('is-invalid');
89448c46458SGreg Roach          element.scrollIntoView();
89548c46458SGreg Roach        } else {
89648c46458SGreg Roach          element.classList.remove('is-invalid');
89748c46458SGreg Roach        }
89848c46458SGreg Roach      });
89948c46458SGreg Roach    });
90048c46458SGreg Roach  };
9012cf1b3d7SGreg Roach}(window.webtrees = window.webtrees || {}));
90271239cb6SGreg Roach
90371239cb6SGreg Roach// Send the CSRF token on all AJAX requests
90471239cb6SGreg Roach$.ajaxSetup({
90571239cb6SGreg Roach  headers: {
90671239cb6SGreg Roach    'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content')
90771239cb6SGreg Roach  }
90871239cb6SGreg Roach});
90971239cb6SGreg Roach
9104ed7dff1SGreg Roach/**
9114ed7dff1SGreg Roach * Initialisation
9124ed7dff1SGreg Roach */
9134ed7dff1SGreg Roach$(function () {
9142cf1b3d7SGreg Roach  // Page elements that load automatically via AJAX.
91571239cb6SGreg Roach  // This prevents bad robots from crawling resource-intensive pages.
916d4786c66SGreg Roach  $('[data-wt-ajax-url]').each(function () {
917d4786c66SGreg Roach    $(this).load(this.dataset.wtAjaxUrl);
91871239cb6SGreg Roach  });
91971239cb6SGreg Roach
92071239cb6SGreg Roach  // Autocomplete
921d4786c66SGreg Roach  webtrees.autocomplete('input[data-wt-autocomplete-url]');
92271239cb6SGreg Roach
923c8d78f19SGreg Roach  document.querySelectorAll('.tom-select').forEach(element => webtrees.initializeTomSelect(element));
924896a5721SGreg Roach
925896a5721SGreg Roach  // If we clear the select (using the "X" button), we need an empty value
926896a5721SGreg Roach  // (rather than no value at all) for (non-multiple) selects with name="array[]"
927c8d78f19SGreg Roach  document.querySelectorAll('select.tom-select:not([multiple])')
928c8d78f19SGreg Roach    .forEach(function (element) {
929c8d78f19SGreg Roach      element.addEventListener('clear', function () {
930c8d78f19SGreg Roach        webtrees.resetTomSelect(element.tomselect, '', '');
931c8d78f19SGreg Roach      });
932bdbdb10cSGreg Roach    });
93371239cb6SGreg Roach
93471239cb6SGreg Roach  // Datatables - locale aware sorting
93571239cb6SGreg Roach  $.fn.dataTableExt.oSort['text-asc'] = function (x, y) {
936efd89170SGreg Roach    return x.localeCompare(y, document.documentElement.lang, { sensitivity: 'base' });
93771239cb6SGreg Roach  };
93871239cb6SGreg Roach  $.fn.dataTableExt.oSort['text-desc'] = function (x, y) {
939efd89170SGreg Roach    return y.localeCompare(x, document.documentElement.lang, { sensitivity: 'base' });
94071239cb6SGreg Roach  };
94171239cb6SGreg Roach
94271239cb6SGreg Roach  // DataTables - start hidden to prevent FOUC.
94371239cb6SGreg Roach  $('table.datatables').each(function () {
9444843b94fSGreg Roach    $(this).DataTable();
9454843b94fSGreg Roach    $(this).removeClass('d-none');
9464843b94fSGreg Roach  });
9474843b94fSGreg Roach
9482d8276baSGreg Roach  // Save button/checkbox state between pages
9492d8276baSGreg Roach  document.querySelectorAll('[data-wt-persist]')
9502d8276baSGreg Roach    .forEach((element) => webtrees.persistentToggle(element));
95171239cb6SGreg Roach
95271239cb6SGreg Roach  // Activate the on-screen keyboard
9532cf1b3d7SGreg Roach  let osk_focus_element;
95471239cb6SGreg Roach  $('.wt-osk-trigger').click(function () {
95571239cb6SGreg Roach    // When a user clicks the icon, set focus to the corresponding input
956d4786c66SGreg Roach    osk_focus_element = document.getElementById(this.dataset.wtId);
95771239cb6SGreg Roach    osk_focus_element.focus();
95871239cb6SGreg Roach    $('.wt-osk').show();
95971239cb6SGreg Roach  });
96071239cb6SGreg Roach  $('.wt-osk-script-button').change(function () {
96171239cb6SGreg Roach    $('.wt-osk-script').prop('hidden', true);
962d4786c66SGreg Roach    $('.wt-osk-script-' + this.dataset.wtOskScript).prop('hidden', false);
96371239cb6SGreg Roach  });
96471239cb6SGreg Roach  $('.wt-osk-shift-button').click(function () {
96571239cb6SGreg Roach    document.querySelector('.wt-osk-keys').classList.toggle('shifted');
96671239cb6SGreg Roach  });
96771239cb6SGreg Roach  $('.wt-osk-keys').on('click', '.wt-osk-key', function () {
9682cf1b3d7SGreg Roach    let key = $(this).contents().get(0).nodeValue;
9692cf1b3d7SGreg Roach    let shift_state = $('.wt-osk-shift-button').hasClass('active');
9702cf1b3d7SGreg Roach    let shift_key = $('sup', this)[0];
97171239cb6SGreg Roach    if (shift_state && shift_key !== undefined) {
97271239cb6SGreg Roach      key = shift_key.innerText;
97371239cb6SGreg Roach    }
9740d2905f7SGreg Roach    webtrees.pasteAtCursor(osk_focus_element, key);
97571239cb6SGreg Roach    if ($('.wt-osk-pin-button').hasClass('active') === false) {
97671239cb6SGreg Roach      $('.wt-osk').hide();
97771239cb6SGreg Roach    }
978ee51991cSGreg Roach    osk_focus_element.dispatchEvent(new Event('input'));
97971239cb6SGreg Roach  });
98071239cb6SGreg Roach
98171239cb6SGreg Roach  $('.wt-osk-close').on('click', function () {
98271239cb6SGreg Roach    $('.wt-osk').hide();
98371239cb6SGreg Roach  });
984b52a415dSGreg Roach
985b52a415dSGreg Roach  // Hide/Show password fields
986b52a415dSGreg Roach  $('input[type=password]').each(function () {
987b52a415dSGreg Roach    $(this).hideShowPassword('infer', true, {
988b52a415dSGreg Roach      states: {
989b52a415dSGreg Roach        shown: {
990b52a415dSGreg Roach          toggle: {
991d4786c66SGreg Roach            content: this.dataset.wtHidePasswordText,
992b52a415dSGreg Roach            attr: {
993d4786c66SGreg Roach              title: this.dataset.wtHidePasswordTitle,
994d4786c66SGreg Roach              'aria-label': this.dataset.wtHidePasswordTitle,
995b52a415dSGreg Roach            }
996b52a415dSGreg Roach          }
997b52a415dSGreg Roach        },
998b52a415dSGreg Roach        hidden: {
999b52a415dSGreg Roach          toggle: {
1000d4786c66SGreg Roach            content: this.dataset.wtShowPasswordText,
1001b52a415dSGreg Roach            attr: {
1002d4786c66SGreg Roach              title: this.dataset.wtShowPasswordTitle,
1003d4786c66SGreg Roach              'aria-label': this.dataset.wtShowPasswordTitle,
1004b52a415dSGreg Roach            }
1005b52a415dSGreg Roach          }
1006b52a415dSGreg Roach        }
1007b52a415dSGreg Roach      }
1008b52a415dSGreg Roach    });
1009b52a415dSGreg Roach  });
101071239cb6SGreg Roach});
10117adfb8e5SGreg Roach
1012d6edd2ebSGreg Roach// Prevent form re-submission via accidental double-click.
1013d6edd2ebSGreg Roachdocument.addEventListener('submit', function (event) {
1014d6edd2ebSGreg Roach  const form = event.target;
1015d6edd2ebSGreg Roach
1016d6edd2ebSGreg Roach  if (form.reportValidity()) {
1017d6edd2ebSGreg Roach    form.addEventListener('submit', (event) => {
1018d6edd2ebSGreg Roach      if (form.classList.contains('form-is-submitting')) {
1019d6edd2ebSGreg Roach        event.preventDefault();
1020d6edd2ebSGreg Roach      }
1021d6edd2ebSGreg Roach
1022d6edd2ebSGreg Roach      form.classList.add('form-is-submitting');
1023d6edd2ebSGreg Roach    });
1024d6edd2ebSGreg Roach  }
1025d6edd2ebSGreg Roach});
1026d6edd2ebSGreg Roach
102755ea0389SGreg Roach// Convert data-wt-* attributes into useful behavior.
1028efd89170SGreg Roachdocument.addEventListener('click', (event) => {
1029a7a3d6dbSGreg Roach  const target = event.target.closest('a,button');
10307adfb8e5SGreg Roach
10317adfb8e5SGreg Roach  if (target === null) {
10327adfb8e5SGreg Roach    return;
10337adfb8e5SGreg Roach  }
10347adfb8e5SGreg Roach
1035d4786c66SGreg Roach  if ('wtConfirm' in target.dataset && !confirm(target.dataset.wtConfirm)) {
10367adfb8e5SGreg Roach    event.preventDefault();
1037c433703eSGreg Roach    return;
10387adfb8e5SGreg Roach  }
10397adfb8e5SGreg Roach
1040d4786c66SGreg Roach  if ('wtPostUrl' in target.dataset) {
1041313cf418SGreg Roach    webtrees.httpPost(target.dataset.wtPostUrl).then(() => {
1042d4786c66SGreg Roach      if ('wtReloadUrl' in target.dataset) {
1043d4786c66SGreg Roach        // Go somewhere else. e.g. the home page after logout.
1044d4786c66SGreg Roach        document.location = target.dataset.wtReloadUrl;
1045ea101122SGreg Roach      } else {
1046ea101122SGreg Roach        // Reload the current page. e.g. change language.
10477adfb8e5SGreg Roach        document.location.reload();
10487adfb8e5SGreg Roach      }
1049cb8f307bSGreg Roach    }).catch((error) => {
1050ea101122SGreg Roach      alert(error);
1051ea101122SGreg Roach    });
10527adfb8e5SGreg Roach  }
105355ea0389SGreg Roach
105455ea0389SGreg Roach  if (('wtFullscreen' in target.dataset)) {
105555ea0389SGreg Roach    event.stopPropagation();
105655ea0389SGreg Roach
1057c01f908aSGreg Roach    const element = target.closest(target.dataset.wtFullscreen);
105855ea0389SGreg Roach
105955ea0389SGreg Roach    if (document.fullscreenElement === element) {
106055ea0389SGreg Roach      document.exitFullscreen()
106155ea0389SGreg Roach        .catch((error) => alert(error));
106255ea0389SGreg Roach    } else {
106355ea0389SGreg Roach      element.requestFullscreen()
106455ea0389SGreg Roach        .catch((error) => alert(error));
106555ea0389SGreg Roach    }
106655ea0389SGreg Roach  }
10677adfb8e5SGreg Roach});
1068