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>'; 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 /** 552*f0ecc9a9SGreg Roach * Make bootstrap "collapse" elements persistent. 553*f0ecc9a9SGreg Roach * 554*f0ecc9a9SGreg Roach * @param {HTMLElement} element 55571239cb6SGreg Roach */ 5562d8276baSGreg Roach webtrees.persistentToggle = function (element) { 5572d8276baSGreg Roach const key = 'state-of-' + element.dataset.wtPersist; 558*f0ecc9a9SGreg Roach const previous_state = localStorage.getItem(key); 559*f0ecc9a9SGreg Roach 560*f0ecc9a9SGreg Roach // Accordion buttons have aria-expanded. Checkboxes are checked/unchecked 561*f0ecc9a9SGreg Roach const current_state = element.getAttribute('aria-expanded') ?? element.checked.toString(); 56271239cb6SGreg Roach 5632d8276baSGreg Roach // Previously selected? Select again now. 564*f0ecc9a9SGreg Roach if (previous_state !== current_state) { 5652cf1b3d7SGreg Roach element.click(); 56671239cb6SGreg Roach } 56771239cb6SGreg Roach 56864490ee2SGreg Roach // Remember state for the next page load. 569*f0ecc9a9SGreg Roach element.addEventListener('click', function () { 570*f0ecc9a9SGreg Roach if (element.type === 'checkbox') { 5712d8276baSGreg Roach localStorage.setItem(key, element.checked.toString()); 572adbf37b7SGreg Roach } 573*f0ecc9a9SGreg Roach if (element.type === 'button') { 574*f0ecc9a9SGreg Roach localStorage.setItem(key, element.getAttribute('aria-expanded')); 575*f0ecc9a9SGreg Roach } 576*f0ecc9a9SGreg 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 }, 688f352d954SDavid Drury onAdd: function (map) { 689f352d954SDavid Drury let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom'); 690b7b71725SGreg Roach container.onclick = resetCallback; 691f352d954SDavid Drury let reset = config.i18n.reset; 692f352d954SDavid Drury let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container); 693f352d954SDavid Drury anchor.setAttribute('aria-label', reset); 694f352d954SDavid Drury anchor.href = '#'; 695f352d954SDavid Drury anchor.title = reset; 696f352d954SDavid Drury anchor.role = 'button'; 6978f878797SDavid Drury L.DomEvent.addListener(anchor, 'click', L.DomEvent.preventDefault); 698f352d954SDavid Drury let image = L.DomUtil.create('i', 'fas fa-redo', anchor); 699f352d954SDavid Drury image.alt = reset; 700f352d954SDavid Drury 701f352d954SDavid Drury return container; 702f352d954SDavid Drury }, 703f352d954SDavid Drury }); 704f352d954SDavid Drury 705c9c6f2ecSGreg Roach let defaultLayer = null; 706c9c6f2ecSGreg Roach 707c9c6f2ecSGreg Roach for (let [, provider] of Object.entries(config.mapProviders)) { 708c9c6f2ecSGreg Roach for (let [, child] of Object.entries(provider.children)) { 709c9c6f2ecSGreg Roach if ('bingMapsKey' in child) { 710c9c6f2ecSGreg Roach child.layer = L.tileLayer.bing(child); 711c9c6f2ecSGreg Roach } else { 712c9c6f2ecSGreg Roach child.layer = L.tileLayer(child.url, child); 713c9c6f2ecSGreg Roach } 714c9c6f2ecSGreg Roach if (provider.default && child.default) { 715c9c6f2ecSGreg Roach defaultLayer = child.layer; 716c9c6f2ecSGreg Roach } 717c9c6f2ecSGreg Roach } 718c9c6f2ecSGreg Roach } 719c9c6f2ecSGreg Roach 720c9c6f2ecSGreg Roach if (defaultLayer === null) { 721c9c6f2ecSGreg Roach console.log('No default map layer defined - using the first one.'); 722c9c6f2ecSGreg Roach let defaultLayer = config.mapProviders[0].children[0].layer; 723c9c6f2ecSGreg Roach } 724c9c6f2ecSGreg Roach 725c9c6f2ecSGreg Roach 726c9c6f2ecSGreg Roach // Create the map with all controls and layers 727c9c6f2ecSGreg Roach return L.map(id, { 728c9c6f2ecSGreg Roach zoomControl: false, 729c9c6f2ecSGreg Roach }) 730c9c6f2ecSGreg Roach .addControl(zoomControl) 731f352d954SDavid Drury .addControl(new resetControl()) 732c9c6f2ecSGreg Roach .addLayer(defaultLayer) 733c9c6f2ecSGreg Roach .addControl(L.control.layers.tree(config.mapProviders, null, { 734c9c6f2ecSGreg Roach closedSymbol: config.icons.expand, 735c9c6f2ecSGreg Roach openedSymbol: config.icons.collapse, 736c9c6f2ecSGreg Roach })); 737c9c6f2ecSGreg Roach 738c9c6f2ecSGreg Roach }; 739c8d78f19SGreg Roach 740c8d78f19SGreg Roach /** 741c8d78f19SGreg Roach * Initialize a tom-select input 742c8d78f19SGreg Roach * @param {Element} element 743c8d78f19SGreg Roach * @returns {TomSelect} 744c8d78f19SGreg Roach */ 745c8d78f19SGreg Roach webtrees.initializeTomSelect = function (element) { 746c8d78f19SGreg Roach if (element.tomselect) { 747c8d78f19SGreg Roach return element.tomselect; 748c8d78f19SGreg Roach } 749c8d78f19SGreg Roach 750c8d78f19SGreg Roach if (element.dataset.url) { 7515cf3b11fSGreg Roach let options = { 7525cf3b11fSGreg Roach plugins: ['dropdown_input', 'virtual_scroll'], 75333eb849bSGreg Roach maxOptions: false, 7549a8145f8SGreg Roach searchField: [], // We filter on the server, so don't filter on the client. 755c8d78f19SGreg Roach render: { 756c8d78f19SGreg Roach item: (data, escape) => '<div>' + data.text + '</div>', 757c8d78f19SGreg Roach option: (data, escape) => '<div>' + data.text + '</div>', 758c8d78f19SGreg Roach }, 759c8d78f19SGreg Roach firstUrl: query => element.dataset.url + '&query=' + encodeURIComponent(query), 760c8d78f19SGreg Roach load: function (query, callback) { 761313cf418SGreg Roach webtrees.httpGet(this.getUrl(query)) 762c8d78f19SGreg Roach .then(response => response.json()) 763c8d78f19SGreg Roach .then(json => { 764640069c3SRichard Cissée if (json.nextUrl !== null) { 765c8d78f19SGreg Roach this.setNextUrl(query, json.nextUrl + '&query=' + encodeURIComponent(query)); 766640069c3SRichard Cissée } 767c8d78f19SGreg Roach callback(json.data); 768c8d78f19SGreg Roach }) 769c8d78f19SGreg Roach .catch(callback); 770c8d78f19SGreg Roach }, 771c8d78f19SGreg Roach }; 7725cf3b11fSGreg Roach 7735cf3b11fSGreg Roach if (!element.required) { 7745cf3b11fSGreg Roach options.plugins.push('clear_button'); 775c8d78f19SGreg Roach } 776c8d78f19SGreg Roach 777c8d78f19SGreg Roach return new TomSelect(element, options); 778c8d78f19SGreg Roach } 779c8d78f19SGreg Roach 7805cf3b11fSGreg Roach if (element.multiple) { 7815cf3b11fSGreg Roach return new TomSelect(element, { plugins: ['caret_position', 'remove_button'] }); 7825cf3b11fSGreg Roach } 7835cf3b11fSGreg Roach 7845cf3b11fSGreg Roach if (!element.required) { 7855cf3b11fSGreg Roach return new TomSelect(element, { plugins: ['clear_button'] }); 7865cf3b11fSGreg Roach } 7875cf3b11fSGreg Roach 7885cf3b11fSGreg Roach return new TomSelect(element, { }); 7895cf3b11fSGreg Roach } 7905cf3b11fSGreg Roach 791c8d78f19SGreg Roach /** 792c8d78f19SGreg Roach * Reset a tom-select input to have a single selected option 793c8d78f19SGreg Roach * @param {TomSelect} tomSelect 794c8d78f19SGreg Roach * @param {string} value 795c8d78f19SGreg Roach * @param {string} text 796c8d78f19SGreg Roach */ 797c8d78f19SGreg Roach webtrees.resetTomSelect = function (tomSelect, value, text) { 798c8d78f19SGreg Roach tomSelect.clear(true); 799c8d78f19SGreg Roach tomSelect.clearOptions(); 800c8d78f19SGreg Roach tomSelect.addOption({ value: value, text: text }); 801c8d78f19SGreg Roach tomSelect.refreshOptions(); 802c8d78f19SGreg Roach tomSelect.addItem(value, true); 803c8d78f19SGreg Roach tomSelect.refreshItems(); 804c8d78f19SGreg Roach }; 805c8d78f19SGreg Roach 806c8d78f19SGreg Roach /** 807c8d78f19SGreg Roach * Toggle the visibility/status of INDI/FAM/SOUR/REPO/OBJE selectors 808c8d78f19SGreg Roach * 809c8d78f19SGreg Roach * @param {Element} select 810c8d78f19SGreg Roach * @param {Element} container 811c8d78f19SGreg Roach */ 812c8d78f19SGreg Roach webtrees.initializeIFSRO = function(select, container) { 813c8d78f19SGreg Roach select.addEventListener('change', function () { 814c8d78f19SGreg Roach // Show only the selected selector. 815c8d78f19SGreg Roach container.querySelectorAll('.select-record').forEach(element => element.classList.add('d-none')); 816c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value).forEach(element => element.classList.remove('d-none')); 817a5045593SGreg Roach 818c8d78f19SGreg Roach // Enable only the selected selector (so that disabled ones do not get submitted). 819c8d78f19SGreg Roach container.querySelectorAll('.select-record select').forEach(element => { 820c8d78f19SGreg Roach element.disabled = true; 821a5045593SGreg Roach if (element.matches('.tom-select')) { 822c8d78f19SGreg Roach element.tomselect.disable(); 823a5045593SGreg Roach } 824c8d78f19SGreg Roach }); 825c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value + ' select').forEach(element => { 826c8d78f19SGreg Roach element.disabled = false; 827a5045593SGreg Roach if (element.matches('.tom-select')) { 828c8d78f19SGreg Roach element.tomselect.enable(); 829a5045593SGreg Roach } 830c8d78f19SGreg Roach }); 831c8d78f19SGreg Roach }); 83245ed5d1dSGreg Roach }; 83345ed5d1dSGreg Roach 83445ed5d1dSGreg Roach /** 83545ed5d1dSGreg Roach * Save a form using ajax, for use in modals 83645ed5d1dSGreg Roach * 83745ed5d1dSGreg Roach * @param {Event} event 83845ed5d1dSGreg Roach */ 83945ed5d1dSGreg Roach webtrees.createRecordModalSubmit = function (event) { 84045ed5d1dSGreg Roach event.preventDefault(); 84145ed5d1dSGreg Roach const form = event.target; 84245ed5d1dSGreg Roach const modal = document.getElementById('wt-ajax-modal') 84345ed5d1dSGreg Roach const modal_content = modal.querySelector('.modal-content'); 84445ed5d1dSGreg Roach const select = document.getElementById(modal_content.dataset.wtSelectId); 84545ed5d1dSGreg Roach 84645ed5d1dSGreg Roach webtrees.httpPost(form.action, new FormData(form)) 84745ed5d1dSGreg Roach .then(response => response.json()) 84845ed5d1dSGreg Roach .then(json => { 84945ed5d1dSGreg Roach if (select) { 85045ed5d1dSGreg Roach // This modal was activated by the "create new" button in a select edit control. 85145ed5d1dSGreg Roach webtrees.resetTomSelect(select.tomselect, json.value, json.text); 85245ed5d1dSGreg Roach 85345ed5d1dSGreg Roach bootstrap.Modal.getInstance(modal).hide(); 85445ed5d1dSGreg Roach } else { 85545ed5d1dSGreg Roach // Show the success message in the existing modal. 85645ed5d1dSGreg Roach modal_content.innerHTML = json.html; 857c8d78f19SGreg Roach } 85845ed5d1dSGreg Roach }) 85945ed5d1dSGreg Roach .catch(error => { 86045ed5d1dSGreg Roach modal_content.innerHTML = error; 86145ed5d1dSGreg Roach }); 86245ed5d1dSGreg Roach }; 86348c46458SGreg Roach 86448c46458SGreg Roach /** 86548c46458SGreg Roach * Text areas don't support the pattern attribute, so apply it manually via data-wt-pattern. 86648c46458SGreg Roach * 86748c46458SGreg Roach * @param {HTMLFormElement} form 86848c46458SGreg Roach */ 86948c46458SGreg Roach webtrees.textareaPatterns = function (form) { 87048c46458SGreg Roach form.addEventListener('submit', function (event) { 87148c46458SGreg Roach event.target.querySelectorAll('textarea[data-wt-pattern]').forEach(function (element) { 87248c46458SGreg Roach const pattern = new RegExp('^' + element.dataset.wtPattern + '$'); 87348c46458SGreg Roach 87448c46458SGreg Roach if (!element.readOnly && element.value !== '' && !pattern.test(element.value)) { 87548c46458SGreg Roach event.preventDefault(); 87648c46458SGreg Roach event.stopPropagation(); 87748c46458SGreg Roach element.classList.add('is-invalid'); 87848c46458SGreg Roach element.scrollIntoView(); 87948c46458SGreg Roach } else { 88048c46458SGreg Roach element.classList.remove('is-invalid'); 88148c46458SGreg Roach } 88248c46458SGreg Roach }); 88348c46458SGreg Roach }); 88448c46458SGreg Roach }; 8852cf1b3d7SGreg Roach}(window.webtrees = window.webtrees || {})); 88671239cb6SGreg Roach 88771239cb6SGreg Roach// Send the CSRF token on all AJAX requests 88871239cb6SGreg Roach$.ajaxSetup({ 88971239cb6SGreg Roach headers: { 89071239cb6SGreg Roach 'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content') 89171239cb6SGreg Roach } 89271239cb6SGreg Roach}); 89371239cb6SGreg Roach 8944ed7dff1SGreg Roach/** 8954ed7dff1SGreg Roach * Initialisation 8964ed7dff1SGreg Roach */ 8974ed7dff1SGreg Roach$(function () { 8982cf1b3d7SGreg Roach // Page elements that load automatically via AJAX. 89971239cb6SGreg Roach // This prevents bad robots from crawling resource-intensive pages. 900d4786c66SGreg Roach $('[data-wt-ajax-url]').each(function () { 901d4786c66SGreg Roach $(this).load(this.dataset.wtAjaxUrl); 90271239cb6SGreg Roach }); 90371239cb6SGreg Roach 90471239cb6SGreg Roach // Autocomplete 905d4786c66SGreg Roach webtrees.autocomplete('input[data-wt-autocomplete-url]'); 90671239cb6SGreg Roach 907c8d78f19SGreg Roach document.querySelectorAll('.tom-select').forEach(element => webtrees.initializeTomSelect(element)); 908896a5721SGreg Roach 909896a5721SGreg Roach // If we clear the select (using the "X" button), we need an empty value 910896a5721SGreg Roach // (rather than no value at all) for (non-multiple) selects with name="array[]" 911c8d78f19SGreg Roach document.querySelectorAll('select.tom-select:not([multiple])') 912c8d78f19SGreg Roach .forEach(function (element) { 913c8d78f19SGreg Roach element.addEventListener('clear', function () { 914c8d78f19SGreg Roach webtrees.resetTomSelect(element.tomselect, '', ''); 915c8d78f19SGreg Roach }); 916bdbdb10cSGreg Roach }); 91771239cb6SGreg Roach 91871239cb6SGreg Roach // Datatables - locale aware sorting 91971239cb6SGreg Roach $.fn.dataTableExt.oSort['text-asc'] = function (x, y) { 920efd89170SGreg Roach return x.localeCompare(y, document.documentElement.lang, { sensitivity: 'base' }); 92171239cb6SGreg Roach }; 92271239cb6SGreg Roach $.fn.dataTableExt.oSort['text-desc'] = function (x, y) { 923efd89170SGreg Roach return y.localeCompare(x, document.documentElement.lang, { sensitivity: 'base' }); 92471239cb6SGreg Roach }; 92571239cb6SGreg Roach 92671239cb6SGreg Roach // DataTables - start hidden to prevent FOUC. 92771239cb6SGreg Roach $('table.datatables').each(function () { 9284843b94fSGreg Roach $(this).DataTable(); 9294843b94fSGreg Roach $(this).removeClass('d-none'); 9304843b94fSGreg Roach }); 9314843b94fSGreg Roach 9322d8276baSGreg Roach // Save button/checkbox state between pages 9332d8276baSGreg Roach document.querySelectorAll('[data-wt-persist]') 9342d8276baSGreg Roach .forEach((element) => webtrees.persistentToggle(element)); 93571239cb6SGreg Roach 93671239cb6SGreg Roach // Activate the on-screen keyboard 9372cf1b3d7SGreg Roach let osk_focus_element; 93871239cb6SGreg Roach $('.wt-osk-trigger').click(function () { 93971239cb6SGreg Roach // When a user clicks the icon, set focus to the corresponding input 940d4786c66SGreg Roach osk_focus_element = document.getElementById(this.dataset.wtId); 94171239cb6SGreg Roach osk_focus_element.focus(); 94271239cb6SGreg Roach $('.wt-osk').show(); 94371239cb6SGreg Roach }); 94471239cb6SGreg Roach $('.wt-osk-script-button').change(function () { 94571239cb6SGreg Roach $('.wt-osk-script').prop('hidden', true); 946d4786c66SGreg Roach $('.wt-osk-script-' + this.dataset.wtOskScript).prop('hidden', false); 94771239cb6SGreg Roach }); 94871239cb6SGreg Roach $('.wt-osk-shift-button').click(function () { 94971239cb6SGreg Roach document.querySelector('.wt-osk-keys').classList.toggle('shifted'); 95071239cb6SGreg Roach }); 95171239cb6SGreg Roach $('.wt-osk-keys').on('click', '.wt-osk-key', function () { 9522cf1b3d7SGreg Roach let key = $(this).contents().get(0).nodeValue; 9532cf1b3d7SGreg Roach let shift_state = $('.wt-osk-shift-button').hasClass('active'); 9542cf1b3d7SGreg Roach let shift_key = $('sup', this)[0]; 95571239cb6SGreg Roach if (shift_state && shift_key !== undefined) { 95671239cb6SGreg Roach key = shift_key.innerText; 95771239cb6SGreg Roach } 9580d2905f7SGreg Roach webtrees.pasteAtCursor(osk_focus_element, key); 95971239cb6SGreg Roach if ($('.wt-osk-pin-button').hasClass('active') === false) { 96071239cb6SGreg Roach $('.wt-osk').hide(); 96171239cb6SGreg Roach } 962ee51991cSGreg Roach osk_focus_element.dispatchEvent(new Event('input')); 96371239cb6SGreg Roach }); 96471239cb6SGreg Roach 96571239cb6SGreg Roach $('.wt-osk-close').on('click', function () { 96671239cb6SGreg Roach $('.wt-osk').hide(); 96771239cb6SGreg Roach }); 968b52a415dSGreg Roach 969b52a415dSGreg Roach // Hide/Show password fields 970b52a415dSGreg Roach $('input[type=password]').each(function () { 971b52a415dSGreg Roach $(this).hideShowPassword('infer', true, { 972b52a415dSGreg Roach states: { 973b52a415dSGreg Roach shown: { 974b52a415dSGreg Roach toggle: { 975d4786c66SGreg Roach content: this.dataset.wtHidePasswordText, 976b52a415dSGreg Roach attr: { 977d4786c66SGreg Roach title: this.dataset.wtHidePasswordTitle, 978d4786c66SGreg Roach 'aria-label': this.dataset.wtHidePasswordTitle, 979b52a415dSGreg Roach } 980b52a415dSGreg Roach } 981b52a415dSGreg Roach }, 982b52a415dSGreg Roach hidden: { 983b52a415dSGreg Roach toggle: { 984d4786c66SGreg Roach content: this.dataset.wtShowPasswordText, 985b52a415dSGreg Roach attr: { 986d4786c66SGreg Roach title: this.dataset.wtShowPasswordTitle, 987d4786c66SGreg Roach 'aria-label': this.dataset.wtShowPasswordTitle, 988b52a415dSGreg Roach } 989b52a415dSGreg Roach } 990b52a415dSGreg Roach } 991b52a415dSGreg Roach } 992b52a415dSGreg Roach }); 993b52a415dSGreg Roach }); 99471239cb6SGreg Roach}); 9957adfb8e5SGreg Roach 996d6edd2ebSGreg Roach// Prevent form re-submission via accidental double-click. 997d6edd2ebSGreg Roachdocument.addEventListener('submit', function (event) { 998d6edd2ebSGreg Roach const form = event.target; 999d6edd2ebSGreg Roach 1000d6edd2ebSGreg Roach if (form.reportValidity()) { 1001d6edd2ebSGreg Roach form.addEventListener('submit', (event) => { 1002d6edd2ebSGreg Roach if (form.classList.contains('form-is-submitting')) { 1003d6edd2ebSGreg Roach event.preventDefault(); 1004d6edd2ebSGreg Roach } 1005d6edd2ebSGreg Roach 1006d6edd2ebSGreg Roach form.classList.add('form-is-submitting'); 1007d6edd2ebSGreg Roach }); 1008d6edd2ebSGreg Roach } 1009d6edd2ebSGreg Roach}); 1010d6edd2ebSGreg Roach 1011d4786c66SGreg Roach// Convert data-wt-confirm and data-wt-post-url/data-wt-reload-url attributes into useful behavior. 1012efd89170SGreg Roachdocument.addEventListener('click', (event) => { 1013a7a3d6dbSGreg Roach const target = event.target.closest('a,button'); 10147adfb8e5SGreg Roach 10157adfb8e5SGreg Roach if (target === null) { 10167adfb8e5SGreg Roach return; 10177adfb8e5SGreg Roach } 10187adfb8e5SGreg Roach 1019d4786c66SGreg Roach if ('wtConfirm' in target.dataset && !confirm(target.dataset.wtConfirm)) { 10207adfb8e5SGreg Roach event.preventDefault(); 1021c433703eSGreg Roach return; 10227adfb8e5SGreg Roach } 10237adfb8e5SGreg Roach 1024d4786c66SGreg Roach if ('wtPostUrl' in target.dataset) { 1025313cf418SGreg Roach webtrees.httpPost(target.dataset.wtPostUrl).then(() => { 1026d4786c66SGreg Roach if ('wtReloadUrl' in target.dataset) { 1027d4786c66SGreg Roach // Go somewhere else. e.g. the home page after logout. 1028d4786c66SGreg Roach document.location = target.dataset.wtReloadUrl; 1029ea101122SGreg Roach } else { 1030ea101122SGreg Roach // Reload the current page. e.g. change language. 10317adfb8e5SGreg Roach document.location.reload(); 10327adfb8e5SGreg Roach } 1033cb8f307bSGreg Roach }).catch((error) => { 1034ea101122SGreg Roach alert(error); 1035ea101122SGreg Roach }); 10367adfb8e5SGreg Roach } 10377adfb8e5SGreg Roach}); 1038