171239cb6SGreg Roach/** 271239cb6SGreg Roach * webtrees: online genealogy 35bfc6897SGreg Roach * Copyright (C) 2022 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>'; 4182cf1b3d7SGreg Roach content += '<td><select class="form-control" 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>'; 4272cf1b3d7SGreg Roach content += '<td><select class="form-control" 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 /** 5522cf1b3d7SGreg Roach * Persistent checkbox options to hide/show extra data. 5532d8276baSGreg Roach * @param {HTMLInputElement} element 55471239cb6SGreg Roach */ 5552d8276baSGreg Roach webtrees.persistentToggle = function (element) { 5562d8276baSGreg Roach if (element instanceof HTMLInputElement && element.type === 'checkbox') { 5572d8276baSGreg Roach const key = 'state-of-' + element.dataset.wtPersist; 558efd89170SGreg Roach const state = localStorage.getItem(key); 55971239cb6SGreg Roach 5602d8276baSGreg Roach // Previously selected? Select again now. 56164490ee2SGreg Roach if (state === 'true') { 5622cf1b3d7SGreg Roach element.click(); 56371239cb6SGreg Roach } 56471239cb6SGreg Roach 56564490ee2SGreg Roach // Remember state for the next page load. 5662cf1b3d7SGreg Roach element.addEventListener('change', function () { 5672d8276baSGreg Roach localStorage.setItem(key, element.checked.toString()); 5682cf1b3d7SGreg Roach }); 569adbf37b7SGreg Roach } 5702cf1b3d7SGreg Roach }; 57171239cb6SGreg Roach 57206fe1eb5SGreg Roach /** 5732cf1b3d7SGreg Roach * @param {Element} field 57406fe1eb5SGreg Roach * @param {string} pos 57506fe1eb5SGreg Roach * @param {string} neg 57606fe1eb5SGreg Roach */ 5772cf1b3d7SGreg Roach function reformatLatLong (field, pos, neg) { 57871239cb6SGreg Roach // valid LATI or LONG according to Gedcom standard 57971239cb6SGreg Roach // pos (+) : N or E 58071239cb6SGreg Roach // neg (-) : S or W 5812cf1b3d7SGreg Roach let txt = field.value.toUpperCase(); 58271239cb6SGreg Roach txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim 58371239cb6SGreg Roach txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34 58471239cb6SGreg Roach txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234 58571239cb6SGreg Roach txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698 58671239cb6SGreg Roach txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698 58771239cb6SGreg Roach // 0°34'11 ==> 0:34:11 58871239cb6SGreg Roach txt = txt.replace(/\u00b0/g, ':'); // ° 58971239cb6SGreg Roach txt = txt.replace(/\u0027/g, ':'); // ' 59071239cb6SGreg Roach // 0:34:11.2W ==> W0.5698 59171239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) { 5922cf1b3d7SGreg Roach let n = parseFloat($1); 59371239cb6SGreg Roach n += ($2 / 60); 59471239cb6SGreg Roach n += ($3 / 3600); 59571239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 59671239cb6SGreg Roach return $4 + n; 59771239cb6SGreg Roach }); 59871239cb6SGreg Roach // 0:34W ==> W0.5667 59971239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) { 6002cf1b3d7SGreg Roach let n = parseFloat($1); 60171239cb6SGreg Roach n += ($2 / 60); 60271239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 60371239cb6SGreg Roach return $3 + n; 60471239cb6SGreg Roach }); 60571239cb6SGreg Roach // 0.5698W ==> W0.5698 6062cf1b3d7SGreg Roach txt = txt.replace(/(.*)(NSEW])$/g, '$2$1'); 60771239cb6SGreg Roach // 17.1234 ==> N17.1234 60871239cb6SGreg Roach if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) { 60971239cb6SGreg Roach txt = pos + txt; 61071239cb6SGreg Roach } 61171239cb6SGreg Roach field.value = txt; 61271239cb6SGreg Roach } 61371239cb6SGreg Roach 61406fe1eb5SGreg Roach /** 6152cf1b3d7SGreg Roach * @param {Element} field 6162cf1b3d7SGreg Roach */ 6172cf1b3d7SGreg Roach webtrees.reformatLatitude = function (field) { 6182cf1b3d7SGreg Roach return reformatLatLong(field, 'N', 'S'); 6192cf1b3d7SGreg Roach }; 6202cf1b3d7SGreg Roach 6212cf1b3d7SGreg Roach /** 6222cf1b3d7SGreg Roach * @param {Element} field 6232cf1b3d7SGreg Roach */ 6242cf1b3d7SGreg Roach webtrees.reformatLongitude = function (field) { 6252cf1b3d7SGreg Roach return reformatLatLong(field, 'E', 'W'); 6262cf1b3d7SGreg Roach }; 6272cf1b3d7SGreg Roach 6282cf1b3d7SGreg Roach /** 62906fe1eb5SGreg Roach * Initialize autocomplete elements. 63006fe1eb5SGreg Roach * @param {string} selector 63106fe1eb5SGreg Roach */ 6322cf1b3d7SGreg Roach webtrees.autocomplete = function (selector) { 63371239cb6SGreg Roach // Use typeahead/bloodhound for autocomplete 63471239cb6SGreg Roach $(selector).each(function () { 635efd89170SGreg Roach const that = this; 63671239cb6SGreg Roach $(this).typeahead(null, { 63771239cb6SGreg Roach display: 'value', 63863763244SGreg Roach limit: 10, 63963763244SGreg Roach minLength: 2, 64071239cb6SGreg Roach source: new Bloodhound({ 64171239cb6SGreg Roach datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 64271239cb6SGreg Roach queryTokenizer: Bloodhound.tokenizers.whitespace, 64371239cb6SGreg Roach remote: { 644d4786c66SGreg Roach url: this.dataset.wtAutocompleteUrl, 645f4abaf12SGreg Roach replace: function (url, uriEncodedQuery) { 646f9b64f46SGreg Roach const symbol = (url.indexOf("?") > 0) ? '&' : '?'; 647d4786c66SGreg Roach if (that.dataset.wtAutocompleteExtra === 'SOUR') { 6484de8df7eSGreg Roach let row_group = that.closest('.form-group').parentElement.previousElementSibling; 649d99ef095SGreg Roach while (row_group.querySelector('select') === null) { 650d99ef095SGreg Roach row_group = row_group.previousElementSibling; 651d99ef095SGreg Roach } 652d99ef095SGreg Roach const element = row_group.querySelector('select'); 65382dda228SJonathan Jaubart const extra = element.options[element.selectedIndex].value.replace(/@/g, ''); 654f9b64f46SGreg Roach return url + symbol + "query=" + uriEncodedQuery + '&extra=' + encodeURIComponent(extra); 655f4abaf12SGreg Roach } 656f9b64f46SGreg Roach return url + symbol + "query=" + uriEncodedQuery 657f9b64f46SGreg Roach } 65871239cb6SGreg Roach } 65971239cb6SGreg Roach }) 66071239cb6SGreg Roach }); 66171239cb6SGreg Roach }); 6622cf1b3d7SGreg Roach }; 663c9c6f2ecSGreg Roach 664c9c6f2ecSGreg Roach /** 665c9c6f2ecSGreg Roach * Create a LeafletJS map from a list of providers/layers. 666c9c6f2ecSGreg Roach * @param {string} id 667c9c6f2ecSGreg Roach * @param {object} config 668b7b71725SGreg Roach * @param {function} resetCallback 669c9c6f2ecSGreg Roach * @returns Map 670c9c6f2ecSGreg Roach */ 671b7b71725SGreg Roach webtrees.buildLeafletJsMap = function (id, config, resetCallback) { 672c9c6f2ecSGreg Roach const zoomControl = new L.control.zoom({ 673c9c6f2ecSGreg Roach zoomInTitle: config.i18n.zoomIn, 674c9c6f2ecSGreg Roach zoomoutTitle: config.i18n.zoomOut, 675c9c6f2ecSGreg Roach }); 676c9c6f2ecSGreg Roach 677f352d954SDavid Drury const resetControl = L.Control.extend({ 678f352d954SDavid Drury options: { 679f352d954SDavid Drury position: 'topleft', 680f352d954SDavid Drury }, 681f352d954SDavid Drury onAdd: function (map) { 682f352d954SDavid Drury let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom'); 683b7b71725SGreg Roach container.onclick = resetCallback; 684f352d954SDavid Drury let reset = config.i18n.reset; 685f352d954SDavid Drury let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container); 686f352d954SDavid Drury anchor.setAttribute('aria-label', reset); 687f352d954SDavid Drury anchor.href = '#'; 688f352d954SDavid Drury anchor.title = reset; 689f352d954SDavid Drury anchor.role = 'button'; 6908f878797SDavid Drury L.DomEvent.addListener(anchor, 'click', L.DomEvent.preventDefault); 691f352d954SDavid Drury let image = L.DomUtil.create('i', 'fas fa-redo', anchor); 692f352d954SDavid Drury image.alt = reset; 693f352d954SDavid Drury 694f352d954SDavid Drury return container; 695f352d954SDavid Drury }, 696f352d954SDavid Drury }); 697f352d954SDavid Drury 698c9c6f2ecSGreg Roach let defaultLayer = null; 699c9c6f2ecSGreg Roach 700c9c6f2ecSGreg Roach for (let [, provider] of Object.entries(config.mapProviders)) { 701c9c6f2ecSGreg Roach for (let [, child] of Object.entries(provider.children)) { 702c9c6f2ecSGreg Roach if ('bingMapsKey' in child) { 703c9c6f2ecSGreg Roach child.layer = L.tileLayer.bing(child); 704c9c6f2ecSGreg Roach } else { 705c9c6f2ecSGreg Roach child.layer = L.tileLayer(child.url, child); 706c9c6f2ecSGreg Roach } 707c9c6f2ecSGreg Roach if (provider.default && child.default) { 708c9c6f2ecSGreg Roach defaultLayer = child.layer; 709c9c6f2ecSGreg Roach } 710c9c6f2ecSGreg Roach } 711c9c6f2ecSGreg Roach } 712c9c6f2ecSGreg Roach 713c9c6f2ecSGreg Roach if (defaultLayer === null) { 714c9c6f2ecSGreg Roach console.log('No default map layer defined - using the first one.'); 715c9c6f2ecSGreg Roach let defaultLayer = config.mapProviders[0].children[0].layer; 716c9c6f2ecSGreg Roach } 717c9c6f2ecSGreg Roach 718c9c6f2ecSGreg Roach 719c9c6f2ecSGreg Roach // Create the map with all controls and layers 720c9c6f2ecSGreg Roach return L.map(id, { 721c9c6f2ecSGreg Roach zoomControl: false, 722c9c6f2ecSGreg Roach }) 723c9c6f2ecSGreg Roach .addControl(zoomControl) 724f352d954SDavid Drury .addControl(new resetControl()) 725c9c6f2ecSGreg Roach .addLayer(defaultLayer) 726c9c6f2ecSGreg Roach .addControl(L.control.layers.tree(config.mapProviders, null, { 727c9c6f2ecSGreg Roach closedSymbol: config.icons.expand, 728c9c6f2ecSGreg Roach openedSymbol: config.icons.collapse, 729c9c6f2ecSGreg Roach })); 730c9c6f2ecSGreg Roach 731c9c6f2ecSGreg Roach }; 732c8d78f19SGreg Roach 733c8d78f19SGreg Roach /** 734c8d78f19SGreg Roach * Initialize a tom-select input 735c8d78f19SGreg Roach * @param {Element} element 736c8d78f19SGreg Roach * @returns {TomSelect} 737c8d78f19SGreg Roach */ 738c8d78f19SGreg Roach webtrees.initializeTomSelect = function (element) { 739c8d78f19SGreg Roach if (element.tomselect) { 740c8d78f19SGreg Roach return element.tomselect; 741c8d78f19SGreg Roach } 742c8d78f19SGreg Roach 743c8d78f19SGreg Roach if (element.dataset.url) { 7445cf3b11fSGreg Roach let options = { 7455cf3b11fSGreg Roach plugins: ['dropdown_input', 'virtual_scroll'], 74633eb849bSGreg Roach maxOptions: false, 747c8d78f19SGreg Roach render: { 748c8d78f19SGreg Roach item: (data, escape) => '<div>' + data.text + '</div>', 749c8d78f19SGreg Roach option: (data, escape) => '<div>' + data.text + '</div>', 750c8d78f19SGreg Roach }, 751c8d78f19SGreg Roach firstUrl: query => element.dataset.url + '&query=' + encodeURIComponent(query), 752c8d78f19SGreg Roach load: function (query, callback) { 753313cf418SGreg Roach webtrees.httpGet(this.getUrl(query)) 754c8d78f19SGreg Roach .then(response => response.json()) 755c8d78f19SGreg Roach .then(json => { 756640069c3SRichard Cissée if (json.nextUrl !== null) { 757c8d78f19SGreg Roach this.setNextUrl(query, json.nextUrl + '&query=' + encodeURIComponent(query)); 758640069c3SRichard Cissée } 759c8d78f19SGreg Roach callback(json.data); 760c8d78f19SGreg Roach }) 761c8d78f19SGreg Roach .catch(callback); 762c8d78f19SGreg Roach }, 763c8d78f19SGreg Roach }; 7645cf3b11fSGreg Roach 7655cf3b11fSGreg Roach if (!element.required) { 7665cf3b11fSGreg Roach options.plugins.push('clear_button'); 767c8d78f19SGreg Roach } 768c8d78f19SGreg Roach 769c8d78f19SGreg Roach return new TomSelect(element, options); 770c8d78f19SGreg Roach } 771c8d78f19SGreg Roach 7725cf3b11fSGreg Roach if (element.multiple) { 7735cf3b11fSGreg Roach return new TomSelect(element, { plugins: ['caret_position', 'remove_button'] }); 7745cf3b11fSGreg Roach } 7755cf3b11fSGreg Roach 7765cf3b11fSGreg Roach if (!element.required) { 7775cf3b11fSGreg Roach return new TomSelect(element, { plugins: ['clear_button'] }); 7785cf3b11fSGreg Roach } 7795cf3b11fSGreg Roach 7805cf3b11fSGreg Roach return new TomSelect(element, { }); 7815cf3b11fSGreg Roach } 7825cf3b11fSGreg Roach 783c8d78f19SGreg Roach /** 784c8d78f19SGreg Roach * Reset a tom-select input to have a single selected option 785c8d78f19SGreg Roach * @param {TomSelect} tomSelect 786c8d78f19SGreg Roach * @param {string} value 787c8d78f19SGreg Roach * @param {string} text 788c8d78f19SGreg Roach */ 789c8d78f19SGreg Roach webtrees.resetTomSelect = function (tomSelect, value, text) { 790c8d78f19SGreg Roach tomSelect.clear(true); 791c8d78f19SGreg Roach tomSelect.clearOptions(); 792c8d78f19SGreg Roach tomSelect.addOption({ value: value, text: text }); 793c8d78f19SGreg Roach tomSelect.refreshOptions(); 794c8d78f19SGreg Roach tomSelect.addItem(value, true); 795c8d78f19SGreg Roach tomSelect.refreshItems(); 796c8d78f19SGreg Roach }; 797c8d78f19SGreg Roach 798c8d78f19SGreg Roach /** 799c8d78f19SGreg Roach * Toggle the visibility/status of INDI/FAM/SOUR/REPO/OBJE selectors 800c8d78f19SGreg Roach * 801c8d78f19SGreg Roach * @param {Element} select 802c8d78f19SGreg Roach * @param {Element} container 803c8d78f19SGreg Roach */ 804c8d78f19SGreg Roach webtrees.initializeIFSRO = function(select, container) { 805c8d78f19SGreg Roach select.addEventListener('change', function () { 806c8d78f19SGreg Roach // Show only the selected selector. 807c8d78f19SGreg Roach container.querySelectorAll('.select-record').forEach(element => element.classList.add('d-none')); 808c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value).forEach(element => element.classList.remove('d-none')); 809a5045593SGreg Roach 810c8d78f19SGreg Roach // Enable only the selected selector (so that disabled ones do not get submitted). 811c8d78f19SGreg Roach container.querySelectorAll('.select-record select').forEach(element => { 812c8d78f19SGreg Roach element.disabled = true; 813a5045593SGreg Roach if (element.matches('.tom-select')) { 814c8d78f19SGreg Roach element.tomselect.disable(); 815a5045593SGreg Roach } 816c8d78f19SGreg Roach }); 817c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value + ' select').forEach(element => { 818c8d78f19SGreg Roach element.disabled = false; 819a5045593SGreg Roach if (element.matches('.tom-select')) { 820c8d78f19SGreg Roach element.tomselect.enable(); 821a5045593SGreg Roach } 822c8d78f19SGreg Roach }); 823c8d78f19SGreg Roach }); 824*45ed5d1dSGreg Roach }; 825*45ed5d1dSGreg Roach 826*45ed5d1dSGreg Roach /** 827*45ed5d1dSGreg Roach * Save a form using ajax, for use in modals 828*45ed5d1dSGreg Roach * 829*45ed5d1dSGreg Roach * @param {Event} event 830*45ed5d1dSGreg Roach */ 831*45ed5d1dSGreg Roach webtrees.createRecordModalSubmit = function (event) { 832*45ed5d1dSGreg Roach event.preventDefault(); 833*45ed5d1dSGreg Roach const form = event.target; 834*45ed5d1dSGreg Roach const modal = document.getElementById('wt-ajax-modal') 835*45ed5d1dSGreg Roach const modal_content = modal.querySelector('.modal-content'); 836*45ed5d1dSGreg Roach const select = document.getElementById(modal_content.dataset.wtSelectId); 837*45ed5d1dSGreg Roach 838*45ed5d1dSGreg Roach webtrees.httpPost(form.action, new FormData(form)) 839*45ed5d1dSGreg Roach .then(response => response.json()) 840*45ed5d1dSGreg Roach .then(json => { 841*45ed5d1dSGreg Roach if (select) { 842*45ed5d1dSGreg Roach // This modal was activated by the "create new" button in a select edit control. 843*45ed5d1dSGreg Roach webtrees.resetTomSelect(select.tomselect, json.value, json.text); 844*45ed5d1dSGreg Roach 845*45ed5d1dSGreg Roach bootstrap.Modal.getInstance(modal).hide(); 846*45ed5d1dSGreg Roach } else { 847*45ed5d1dSGreg Roach // Show the success message in the existing modal. 848*45ed5d1dSGreg Roach modal_content.innerHTML = json.html; 849c8d78f19SGreg Roach } 850*45ed5d1dSGreg Roach }) 851*45ed5d1dSGreg Roach .catch(error => { 852*45ed5d1dSGreg Roach modal_content.innerHTML = error; 853*45ed5d1dSGreg Roach }); 854*45ed5d1dSGreg Roach }; 8552cf1b3d7SGreg Roach}(window.webtrees = window.webtrees || {})); 85671239cb6SGreg Roach 85771239cb6SGreg Roach// Send the CSRF token on all AJAX requests 85871239cb6SGreg Roach$.ajaxSetup({ 85971239cb6SGreg Roach headers: { 86071239cb6SGreg Roach 'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content') 86171239cb6SGreg Roach } 86271239cb6SGreg Roach}); 86371239cb6SGreg Roach 8644ed7dff1SGreg Roach/** 8654ed7dff1SGreg Roach * Initialisation 8664ed7dff1SGreg Roach */ 8674ed7dff1SGreg Roach$(function () { 8682cf1b3d7SGreg Roach // Page elements that load automatically via AJAX. 86971239cb6SGreg Roach // This prevents bad robots from crawling resource-intensive pages. 870d4786c66SGreg Roach $('[data-wt-ajax-url]').each(function () { 871d4786c66SGreg Roach $(this).load(this.dataset.wtAjaxUrl); 87271239cb6SGreg Roach }); 87371239cb6SGreg Roach 87471239cb6SGreg Roach // Autocomplete 875d4786c66SGreg Roach webtrees.autocomplete('input[data-wt-autocomplete-url]'); 87671239cb6SGreg Roach 877c8d78f19SGreg Roach document.querySelectorAll('.tom-select').forEach(element => webtrees.initializeTomSelect(element)); 878896a5721SGreg Roach 879896a5721SGreg Roach // If we clear the select (using the "X" button), we need an empty value 880896a5721SGreg Roach // (rather than no value at all) for (non-multiple) selects with name="array[]" 881c8d78f19SGreg Roach document.querySelectorAll('select.tom-select:not([multiple])') 882c8d78f19SGreg Roach .forEach(function (element) { 883c8d78f19SGreg Roach element.addEventListener('clear', function () { 884c8d78f19SGreg Roach webtrees.resetTomSelect(element.tomselect, '', ''); 885c8d78f19SGreg Roach }); 886bdbdb10cSGreg Roach }); 88771239cb6SGreg Roach 88871239cb6SGreg Roach // Datatables - locale aware sorting 88971239cb6SGreg Roach $.fn.dataTableExt.oSort['text-asc'] = function (x, y) { 890efd89170SGreg Roach return x.localeCompare(y, document.documentElement.lang, { sensitivity: 'base' }); 89171239cb6SGreg Roach }; 89271239cb6SGreg Roach $.fn.dataTableExt.oSort['text-desc'] = function (x, y) { 893efd89170SGreg Roach return y.localeCompare(x, document.documentElement.lang, { sensitivity: 'base' }); 89471239cb6SGreg Roach }; 89571239cb6SGreg Roach 89671239cb6SGreg Roach // DataTables - start hidden to prevent FOUC. 89771239cb6SGreg Roach $('table.datatables').each(function () { 8984843b94fSGreg Roach $(this).DataTable(); 8994843b94fSGreg Roach $(this).removeClass('d-none'); 9004843b94fSGreg Roach }); 9014843b94fSGreg Roach 9022d8276baSGreg Roach // Save button/checkbox state between pages 9032d8276baSGreg Roach document.querySelectorAll('[data-wt-persist]') 9042d8276baSGreg Roach .forEach((element) => webtrees.persistentToggle(element)); 90571239cb6SGreg Roach 90671239cb6SGreg Roach // Activate the on-screen keyboard 9072cf1b3d7SGreg Roach let osk_focus_element; 90871239cb6SGreg Roach $('.wt-osk-trigger').click(function () { 90971239cb6SGreg Roach // When a user clicks the icon, set focus to the corresponding input 910d4786c66SGreg Roach osk_focus_element = document.getElementById(this.dataset.wtId); 91171239cb6SGreg Roach osk_focus_element.focus(); 91271239cb6SGreg Roach $('.wt-osk').show(); 91371239cb6SGreg Roach }); 91471239cb6SGreg Roach $('.wt-osk-script-button').change(function () { 91571239cb6SGreg Roach $('.wt-osk-script').prop('hidden', true); 916d4786c66SGreg Roach $('.wt-osk-script-' + this.dataset.wtOskScript).prop('hidden', false); 91771239cb6SGreg Roach }); 91871239cb6SGreg Roach $('.wt-osk-shift-button').click(function () { 91971239cb6SGreg Roach document.querySelector('.wt-osk-keys').classList.toggle('shifted'); 92071239cb6SGreg Roach }); 92171239cb6SGreg Roach $('.wt-osk-keys').on('click', '.wt-osk-key', function () { 9222cf1b3d7SGreg Roach let key = $(this).contents().get(0).nodeValue; 9232cf1b3d7SGreg Roach let shift_state = $('.wt-osk-shift-button').hasClass('active'); 9242cf1b3d7SGreg Roach let shift_key = $('sup', this)[0]; 92571239cb6SGreg Roach if (shift_state && shift_key !== undefined) { 92671239cb6SGreg Roach key = shift_key.innerText; 92771239cb6SGreg Roach } 9280d2905f7SGreg Roach webtrees.pasteAtCursor(osk_focus_element, key); 92971239cb6SGreg Roach if ($('.wt-osk-pin-button').hasClass('active') === false) { 93071239cb6SGreg Roach $('.wt-osk').hide(); 93171239cb6SGreg Roach } 932ee51991cSGreg Roach osk_focus_element.dispatchEvent(new Event('input')); 93371239cb6SGreg Roach }); 93471239cb6SGreg Roach 93571239cb6SGreg Roach $('.wt-osk-close').on('click', function () { 93671239cb6SGreg Roach $('.wt-osk').hide(); 93771239cb6SGreg Roach }); 938b52a415dSGreg Roach 939b52a415dSGreg Roach // Hide/Show password fields 940b52a415dSGreg Roach $('input[type=password]').each(function () { 941b52a415dSGreg Roach $(this).hideShowPassword('infer', true, { 942b52a415dSGreg Roach states: { 943b52a415dSGreg Roach shown: { 944b52a415dSGreg Roach toggle: { 945d4786c66SGreg Roach content: this.dataset.wtHidePasswordText, 946b52a415dSGreg Roach attr: { 947d4786c66SGreg Roach title: this.dataset.wtHidePasswordTitle, 948d4786c66SGreg Roach 'aria-label': this.dataset.wtHidePasswordTitle, 949b52a415dSGreg Roach } 950b52a415dSGreg Roach } 951b52a415dSGreg Roach }, 952b52a415dSGreg Roach hidden: { 953b52a415dSGreg Roach toggle: { 954d4786c66SGreg Roach content: this.dataset.wtShowPasswordText, 955b52a415dSGreg Roach attr: { 956d4786c66SGreg Roach title: this.dataset.wtShowPasswordTitle, 957d4786c66SGreg Roach 'aria-label': this.dataset.wtShowPasswordTitle, 958b52a415dSGreg Roach } 959b52a415dSGreg Roach } 960b52a415dSGreg Roach } 961b52a415dSGreg Roach } 962b52a415dSGreg Roach }); 963b52a415dSGreg Roach }); 96471239cb6SGreg Roach}); 9657adfb8e5SGreg Roach 966d6edd2ebSGreg Roach// Prevent form re-submission via accidental double-click. 967d6edd2ebSGreg Roachdocument.addEventListener('submit', function (event) { 968d6edd2ebSGreg Roach const form = event.target; 969d6edd2ebSGreg Roach 970d6edd2ebSGreg Roach if (form.reportValidity()) { 971d6edd2ebSGreg Roach form.addEventListener('submit', (event) => { 972d6edd2ebSGreg Roach if (form.classList.contains('form-is-submitting')) { 973d6edd2ebSGreg Roach event.preventDefault(); 974d6edd2ebSGreg Roach } 975d6edd2ebSGreg Roach 976d6edd2ebSGreg Roach form.classList.add('form-is-submitting'); 977d6edd2ebSGreg Roach }); 978d6edd2ebSGreg Roach } 979d6edd2ebSGreg Roach}); 980d6edd2ebSGreg Roach 981d4786c66SGreg Roach// Convert data-wt-confirm and data-wt-post-url/data-wt-reload-url attributes into useful behavior. 982efd89170SGreg Roachdocument.addEventListener('click', (event) => { 983a7a3d6dbSGreg Roach const target = event.target.closest('a,button'); 9847adfb8e5SGreg Roach 9857adfb8e5SGreg Roach if (target === null) { 9867adfb8e5SGreg Roach return; 9877adfb8e5SGreg Roach } 9887adfb8e5SGreg Roach 989d4786c66SGreg Roach if ('wtConfirm' in target.dataset && !confirm(target.dataset.wtConfirm)) { 9907adfb8e5SGreg Roach event.preventDefault(); 991c433703eSGreg Roach return; 9927adfb8e5SGreg Roach } 9937adfb8e5SGreg Roach 994d4786c66SGreg Roach if ('wtPostUrl' in target.dataset) { 995313cf418SGreg Roach webtrees.httpPost(target.dataset.wtPostUrl).then(() => { 996d4786c66SGreg Roach if ('wtReloadUrl' in target.dataset) { 997d4786c66SGreg Roach // Go somewhere else. e.g. the home page after logout. 998d4786c66SGreg Roach document.location = target.dataset.wtReloadUrl; 999ea101122SGreg Roach } else { 1000ea101122SGreg Roach // Reload the current page. e.g. change language. 10017adfb8e5SGreg Roach document.location.reload(); 10027adfb8e5SGreg Roach } 1003cb8f307bSGreg Roach }).catch((error) => { 1004ea101122SGreg Roach alert(error); 1005ea101122SGreg Roach }); 10067adfb8e5SGreg Roach } 10077adfb8e5SGreg Roach}); 1008