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 /** 552f0ecc9a9SGreg Roach * Make bootstrap "collapse" elements persistent. 553f0ecc9a9SGreg Roach * 554f0ecc9a9SGreg Roach * @param {HTMLElement} element 55571239cb6SGreg Roach */ 5562d8276baSGreg Roach webtrees.persistentToggle = function (element) { 5572d8276baSGreg Roach const key = 'state-of-' + element.dataset.wtPersist; 558f0ecc9a9SGreg Roach const previous_state = localStorage.getItem(key); 559f0ecc9a9SGreg Roach 560f0ecc9a9SGreg Roach // Accordion buttons have aria-expanded. Checkboxes are checked/unchecked 561f0ecc9a9SGreg Roach const current_state = element.getAttribute('aria-expanded') ?? element.checked.toString(); 56271239cb6SGreg Roach 5632d8276baSGreg Roach // Previously selected? Select again now. 564e107bf98SGreg Roach if (previous_state !== null && previous_state !== current_state) { 5652cf1b3d7SGreg Roach element.click(); 56671239cb6SGreg Roach } 56771239cb6SGreg Roach 56864490ee2SGreg Roach // Remember state for the next page load. 569f0ecc9a9SGreg Roach element.addEventListener('click', function () { 570f0ecc9a9SGreg Roach if (element.type === 'checkbox') { 5712d8276baSGreg Roach localStorage.setItem(key, element.checked.toString()); 572adbf37b7SGreg Roach } 573f0ecc9a9SGreg Roach if (element.type === 'button') { 574f0ecc9a9SGreg Roach localStorage.setItem(key, element.getAttribute('aria-expanded')); 575f0ecc9a9SGreg Roach } 576f0ecc9a9SGreg Roach }); 5772cf1b3d7SGreg Roach }; 57871239cb6SGreg Roach 57906fe1eb5SGreg Roach /** 5802cf1b3d7SGreg Roach * @param {Element} field 58106fe1eb5SGreg Roach * @param {string} pos 58206fe1eb5SGreg Roach * @param {string} neg 58306fe1eb5SGreg Roach */ 5842cf1b3d7SGreg Roach function reformatLatLong (field, pos, neg) { 58571239cb6SGreg Roach // valid LATI or LONG according to Gedcom standard 58671239cb6SGreg Roach // pos (+) : N or E 58771239cb6SGreg Roach // neg (-) : S or W 5882cf1b3d7SGreg Roach let txt = field.value.toUpperCase(); 58971239cb6SGreg Roach txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim 59071239cb6SGreg Roach txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34 59171239cb6SGreg Roach txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234 59271239cb6SGreg Roach txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698 59371239cb6SGreg Roach txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698 59471239cb6SGreg Roach // 0°34'11 ==> 0:34:11 59571239cb6SGreg Roach txt = txt.replace(/\u00b0/g, ':'); // ° 59671239cb6SGreg Roach txt = txt.replace(/\u0027/g, ':'); // ' 59771239cb6SGreg Roach // 0:34:11.2W ==> W0.5698 59871239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) { 5992cf1b3d7SGreg Roach let n = parseFloat($1); 60071239cb6SGreg Roach n += ($2 / 60); 60171239cb6SGreg Roach n += ($3 / 3600); 60271239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 60371239cb6SGreg Roach return $4 + n; 60471239cb6SGreg Roach }); 60571239cb6SGreg Roach // 0:34W ==> W0.5667 60671239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) { 6072cf1b3d7SGreg Roach let n = parseFloat($1); 60871239cb6SGreg Roach n += ($2 / 60); 60971239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 61071239cb6SGreg Roach return $3 + n; 61171239cb6SGreg Roach }); 61271239cb6SGreg Roach // 0.5698W ==> W0.5698 6132cf1b3d7SGreg Roach txt = txt.replace(/(.*)(NSEW])$/g, '$2$1'); 61471239cb6SGreg Roach // 17.1234 ==> N17.1234 61571239cb6SGreg Roach if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) { 61671239cb6SGreg Roach txt = pos + txt; 61771239cb6SGreg Roach } 61871239cb6SGreg Roach field.value = txt; 61971239cb6SGreg Roach } 62071239cb6SGreg Roach 62106fe1eb5SGreg Roach /** 6222cf1b3d7SGreg Roach * @param {Element} field 6232cf1b3d7SGreg Roach */ 6242cf1b3d7SGreg Roach webtrees.reformatLatitude = function (field) { 6252cf1b3d7SGreg Roach return reformatLatLong(field, 'N', 'S'); 6262cf1b3d7SGreg Roach }; 6272cf1b3d7SGreg Roach 6282cf1b3d7SGreg Roach /** 6292cf1b3d7SGreg Roach * @param {Element} field 6302cf1b3d7SGreg Roach */ 6312cf1b3d7SGreg Roach webtrees.reformatLongitude = function (field) { 6322cf1b3d7SGreg Roach return reformatLatLong(field, 'E', 'W'); 6332cf1b3d7SGreg Roach }; 6342cf1b3d7SGreg Roach 6352cf1b3d7SGreg Roach /** 63606fe1eb5SGreg Roach * Initialize autocomplete elements. 63706fe1eb5SGreg Roach * @param {string} selector 63806fe1eb5SGreg Roach */ 6392cf1b3d7SGreg Roach webtrees.autocomplete = function (selector) { 64071239cb6SGreg Roach // Use typeahead/bloodhound for autocomplete 64171239cb6SGreg Roach $(selector).each(function () { 642efd89170SGreg Roach const that = this; 64371239cb6SGreg Roach $(this).typeahead(null, { 64471239cb6SGreg Roach display: 'value', 64563763244SGreg Roach limit: 10, 64663763244SGreg Roach minLength: 2, 64771239cb6SGreg Roach source: new Bloodhound({ 64871239cb6SGreg Roach datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 64971239cb6SGreg Roach queryTokenizer: Bloodhound.tokenizers.whitespace, 65071239cb6SGreg Roach remote: { 651d4786c66SGreg Roach url: this.dataset.wtAutocompleteUrl, 652f4abaf12SGreg Roach replace: function (url, uriEncodedQuery) { 653f9b64f46SGreg Roach const symbol = (url.indexOf("?") > 0) ? '&' : '?'; 654d4786c66SGreg Roach if (that.dataset.wtAutocompleteExtra === 'SOUR') { 655561ec906SGreg Roach let row_group = that.closest('.wt-nested-edit-fields').previousElementSibling; 656d99ef095SGreg Roach while (row_group.querySelector('select') === null) { 657d99ef095SGreg Roach row_group = row_group.previousElementSibling; 658d99ef095SGreg Roach } 659d99ef095SGreg Roach const element = row_group.querySelector('select'); 66082dda228SJonathan Jaubart const extra = element.options[element.selectedIndex].value.replace(/@/g, ''); 661f9b64f46SGreg Roach return url + symbol + "query=" + uriEncodedQuery + '&extra=' + encodeURIComponent(extra); 662f4abaf12SGreg Roach } 663f9b64f46SGreg Roach return url + symbol + "query=" + uriEncodedQuery 664f9b64f46SGreg Roach } 66571239cb6SGreg Roach } 66671239cb6SGreg Roach }) 66771239cb6SGreg Roach }); 66871239cb6SGreg Roach }); 6692cf1b3d7SGreg Roach }; 670c9c6f2ecSGreg Roach 671c9c6f2ecSGreg Roach /** 672c9c6f2ecSGreg Roach * Create a LeafletJS map from a list of providers/layers. 673c9c6f2ecSGreg Roach * @param {string} id 674c9c6f2ecSGreg Roach * @param {object} config 675b7b71725SGreg Roach * @param {function} resetCallback 676c9c6f2ecSGreg Roach * @returns Map 677c9c6f2ecSGreg Roach */ 678b7b71725SGreg Roach webtrees.buildLeafletJsMap = function (id, config, resetCallback) { 679c9c6f2ecSGreg Roach const zoomControl = new L.control.zoom({ 680c9c6f2ecSGreg Roach zoomInTitle: config.i18n.zoomIn, 681c9c6f2ecSGreg Roach zoomoutTitle: config.i18n.zoomOut, 682c9c6f2ecSGreg Roach }); 683c9c6f2ecSGreg Roach 684f352d954SDavid Drury const resetControl = L.Control.extend({ 685f352d954SDavid Drury options: { 686f352d954SDavid Drury position: 'topleft', 687f352d954SDavid Drury }, 688b2e5f20eSGreg Roach onAdd: () => { 689053cb27dSDavid Drury const container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom'); 690053cb27dSDavid Drury const anchor = L.DomUtil.create('a', 'leaflet-control-reset', container); 691053cb27dSDavid Drury 692b2e5f20eSGreg Roach anchor.href = '#'; 693b2e5f20eSGreg Roach anchor.setAttribute('aria-label', config.i18n.reset); /* Firefox doesn't yet support element.ariaLabel */ 694b2e5f20eSGreg Roach anchor.title = config.i18n.reset; 695b2e5f20eSGreg Roach anchor.role = 'button'; 696053cb27dSDavid Drury anchor.innerHTML = config.icons.reset; 697945239ddSDavid Drury anchor.onclick = resetCallback; 698f352d954SDavid Drury 699f352d954SDavid Drury return container; 700f352d954SDavid Drury }, 701f352d954SDavid Drury }); 702f352d954SDavid Drury 703*e95f22dbSDavid Drury const fullScreenControl = L.Control.extend({ 704*e95f22dbSDavid Drury options: { 705*e95f22dbSDavid Drury position: 'topleft', 706*e95f22dbSDavid Drury }, 707*e95f22dbSDavid Drury onAdd: (map) => { 708*e95f22dbSDavid Drury const container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom'); 709*e95f22dbSDavid Drury const anchor = L.DomUtil.create('a', 'leaflet-control-fullscreen', container); 710*e95f22dbSDavid Drury 711*e95f22dbSDavid Drury anchor.setAttribute('aria-label', config.i18n.fullScreen); 712*e95f22dbSDavid Drury anchor.setAttribute('title', config.i18n.fullScreen); 713*e95f22dbSDavid Drury anchor.setAttribute('aria-disabled', 'false'); 714*e95f22dbSDavid Drury anchor.setAttribute('role', 'button'); 715*e95f22dbSDavid Drury anchor.innerHTML = config.icons.fullscreen; 716*e95f22dbSDavid Drury 717*e95f22dbSDavid Drury anchor.onclick = () => { 718*e95f22dbSDavid Drury webtrees.fullScreen('wt-fullscreen-wrapper'); 719*e95f22dbSDavid Drury }; 720*e95f22dbSDavid Drury 721*e95f22dbSDavid Drury return container; 722*e95f22dbSDavid Drury }, 723*e95f22dbSDavid Drury }); 724*e95f22dbSDavid Drury 725c9c6f2ecSGreg Roach let defaultLayer = null; 726c9c6f2ecSGreg Roach 727c9c6f2ecSGreg Roach for (let [, provider] of Object.entries(config.mapProviders)) { 728c9c6f2ecSGreg Roach for (let [, child] of Object.entries(provider.children)) { 729c9c6f2ecSGreg Roach if ('bingMapsKey' in child) { 730c9c6f2ecSGreg Roach child.layer = L.tileLayer.bing(child); 731c9c6f2ecSGreg Roach } else { 732c9c6f2ecSGreg Roach child.layer = L.tileLayer(child.url, child); 733c9c6f2ecSGreg Roach } 734c9c6f2ecSGreg Roach if (provider.default && child.default) { 735c9c6f2ecSGreg Roach defaultLayer = child.layer; 736c9c6f2ecSGreg Roach } 737c9c6f2ecSGreg Roach } 738c9c6f2ecSGreg Roach } 739c9c6f2ecSGreg Roach 740c9c6f2ecSGreg Roach if (defaultLayer === null) { 741c9c6f2ecSGreg Roach console.log('No default map layer defined - using the first one.'); 742c9c6f2ecSGreg Roach let defaultLayer = config.mapProviders[0].children[0].layer; 743c9c6f2ecSGreg Roach } 744c9c6f2ecSGreg Roach 745c9c6f2ecSGreg Roach 746c9c6f2ecSGreg Roach // Create the map with all controls and layers 747c9c6f2ecSGreg Roach return L.map(id, { 748c9c6f2ecSGreg Roach zoomControl: false, 749c9c6f2ecSGreg Roach }) 750c9c6f2ecSGreg Roach .addControl(zoomControl) 751*e95f22dbSDavid Drury .addControl(new fullScreenControl) 752f352d954SDavid Drury .addControl(new resetControl()) 753c9c6f2ecSGreg Roach .addLayer(defaultLayer) 754c9c6f2ecSGreg Roach .addControl(L.control.layers.tree(config.mapProviders, null, { 755c9c6f2ecSGreg Roach closedSymbol: config.icons.expand, 756c9c6f2ecSGreg Roach openedSymbol: config.icons.collapse, 757c9c6f2ecSGreg Roach })); 758c9c6f2ecSGreg Roach 759c9c6f2ecSGreg Roach }; 760c8d78f19SGreg Roach 761c8d78f19SGreg Roach /** 762*e95f22dbSDavid Drury * General purpose fullscreen function 763*e95f22dbSDavid Drury * @param {string} id of the element to be fullscreened 764*e95f22dbSDavid Drury * 765*e95f22dbSDavid Drury */ 766*e95f22dbSDavid Drury webtrees.fullScreen = function (id) { 767*e95f22dbSDavid Drury const element = document.getElementById(id); 768*e95f22dbSDavid Drury if (fscreen.fullscreenEnabled) { 769*e95f22dbSDavid Drury if (!fscreen.fullscreenElement) { 770*e95f22dbSDavid Drury element.requestFullscreen(); 771*e95f22dbSDavid Drury } else if (fscreen.exitFullscreen) { 772*e95f22dbSDavid Drury fscreen.exitFullscreen(); 773*e95f22dbSDavid Drury } 774*e95f22dbSDavid Drury } else { 775*e95f22dbSDavid Drury console.log('Your browser cannot use fullscreen at this time'); 776*e95f22dbSDavid Drury } 777*e95f22dbSDavid Drury } 778*e95f22dbSDavid Drury 779*e95f22dbSDavid Drury /** 780*e95f22dbSDavid Drury * Catch error generated when going to fullscreen 781*e95f22dbSDavid Drury * @param {Event} event 782*e95f22dbSDavid Drury */ 783*e95f22dbSDavid Drury fscreen.onfullscreenerror = (event) => { 784*e95f22dbSDavid Drury console.error(event); 785*e95f22dbSDavid Drury console.log('An error occurred changing into fullscreen'); 786*e95f22dbSDavid Drury } 787*e95f22dbSDavid Drury 788*e95f22dbSDavid Drury /** 789c8d78f19SGreg Roach * Initialize a tom-select input 790c8d78f19SGreg Roach * @param {Element} element 791c8d78f19SGreg Roach * @returns {TomSelect} 792c8d78f19SGreg Roach */ 793c8d78f19SGreg Roach webtrees.initializeTomSelect = function (element) { 794c8d78f19SGreg Roach if (element.tomselect) { 795c8d78f19SGreg Roach return element.tomselect; 796c8d78f19SGreg Roach } 797c8d78f19SGreg Roach 798c8d78f19SGreg Roach if (element.dataset.url) { 7995cf3b11fSGreg Roach let options = { 8005cf3b11fSGreg Roach plugins: ['dropdown_input', 'virtual_scroll'], 80133eb849bSGreg Roach maxOptions: false, 8029a8145f8SGreg Roach searchField: [], // We filter on the server, so don't filter on the client. 803c8d78f19SGreg Roach render: { 804c8d78f19SGreg Roach item: (data, escape) => '<div>' + data.text + '</div>', 805c8d78f19SGreg Roach option: (data, escape) => '<div>' + data.text + '</div>', 806c8d78f19SGreg Roach }, 807c8d78f19SGreg Roach firstUrl: query => element.dataset.url + '&query=' + encodeURIComponent(query), 808c8d78f19SGreg Roach load: function (query, callback) { 809313cf418SGreg Roach webtrees.httpGet(this.getUrl(query)) 810c8d78f19SGreg Roach .then(response => response.json()) 811c8d78f19SGreg Roach .then(json => { 812640069c3SRichard Cissée if (json.nextUrl !== null) { 813c8d78f19SGreg Roach this.setNextUrl(query, json.nextUrl + '&query=' + encodeURIComponent(query)); 814640069c3SRichard Cissée } 815c8d78f19SGreg Roach callback(json.data); 816c8d78f19SGreg Roach }) 817c8d78f19SGreg Roach .catch(callback); 818c8d78f19SGreg Roach }, 819c8d78f19SGreg Roach }; 8205cf3b11fSGreg Roach 8215cf3b11fSGreg Roach if (!element.required) { 8225cf3b11fSGreg Roach options.plugins.push('clear_button'); 823c8d78f19SGreg Roach } 824c8d78f19SGreg Roach 825c8d78f19SGreg Roach return new TomSelect(element, options); 826c8d78f19SGreg Roach } 827c8d78f19SGreg Roach 8285cf3b11fSGreg Roach if (element.multiple) { 8295cf3b11fSGreg Roach return new TomSelect(element, { plugins: ['caret_position', 'remove_button'] }); 8305cf3b11fSGreg Roach } 8315cf3b11fSGreg Roach 8325cf3b11fSGreg Roach if (!element.required) { 8335cf3b11fSGreg Roach return new TomSelect(element, { plugins: ['clear_button'] }); 8345cf3b11fSGreg Roach } 8355cf3b11fSGreg Roach 8365cf3b11fSGreg Roach return new TomSelect(element, { }); 8375cf3b11fSGreg Roach } 8385cf3b11fSGreg Roach 839c8d78f19SGreg Roach /** 840c8d78f19SGreg Roach * Reset a tom-select input to have a single selected option 841c8d78f19SGreg Roach * @param {TomSelect} tomSelect 842c8d78f19SGreg Roach * @param {string} value 843c8d78f19SGreg Roach * @param {string} text 844c8d78f19SGreg Roach */ 845c8d78f19SGreg Roach webtrees.resetTomSelect = function (tomSelect, value, text) { 846c8d78f19SGreg Roach tomSelect.clear(true); 847c8d78f19SGreg Roach tomSelect.clearOptions(); 848c8d78f19SGreg Roach tomSelect.addOption({ value: value, text: text }); 849c8d78f19SGreg Roach tomSelect.refreshOptions(); 850c8d78f19SGreg Roach tomSelect.addItem(value, true); 851c8d78f19SGreg Roach tomSelect.refreshItems(); 852c8d78f19SGreg Roach }; 853c8d78f19SGreg Roach 854c8d78f19SGreg Roach /** 855c8d78f19SGreg Roach * Toggle the visibility/status of INDI/FAM/SOUR/REPO/OBJE selectors 856c8d78f19SGreg Roach * 857c8d78f19SGreg Roach * @param {Element} select 858c8d78f19SGreg Roach * @param {Element} container 859c8d78f19SGreg Roach */ 860c8d78f19SGreg Roach webtrees.initializeIFSRO = function(select, container) { 861c8d78f19SGreg Roach select.addEventListener('change', function () { 862c8d78f19SGreg Roach // Show only the selected selector. 863c8d78f19SGreg Roach container.querySelectorAll('.select-record').forEach(element => element.classList.add('d-none')); 864c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value).forEach(element => element.classList.remove('d-none')); 865a5045593SGreg Roach 866c8d78f19SGreg Roach // Enable only the selected selector (so that disabled ones do not get submitted). 867c8d78f19SGreg Roach container.querySelectorAll('.select-record select').forEach(element => { 868c8d78f19SGreg Roach element.disabled = true; 869a5045593SGreg Roach if (element.matches('.tom-select')) { 870c8d78f19SGreg Roach element.tomselect.disable(); 871a5045593SGreg Roach } 872c8d78f19SGreg Roach }); 873c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value + ' select').forEach(element => { 874c8d78f19SGreg Roach element.disabled = false; 875a5045593SGreg Roach if (element.matches('.tom-select')) { 876c8d78f19SGreg Roach element.tomselect.enable(); 877a5045593SGreg Roach } 878c8d78f19SGreg Roach }); 879c8d78f19SGreg Roach }); 88045ed5d1dSGreg Roach }; 88145ed5d1dSGreg Roach 88245ed5d1dSGreg Roach /** 88345ed5d1dSGreg Roach * Save a form using ajax, for use in modals 88445ed5d1dSGreg Roach * 88545ed5d1dSGreg Roach * @param {Event} event 88645ed5d1dSGreg Roach */ 88745ed5d1dSGreg Roach webtrees.createRecordModalSubmit = function (event) { 88845ed5d1dSGreg Roach event.preventDefault(); 88945ed5d1dSGreg Roach const form = event.target; 89045ed5d1dSGreg Roach const modal = document.getElementById('wt-ajax-modal') 89145ed5d1dSGreg Roach const modal_content = modal.querySelector('.modal-content'); 89245ed5d1dSGreg Roach const select = document.getElementById(modal_content.dataset.wtSelectId); 89345ed5d1dSGreg Roach 89445ed5d1dSGreg Roach webtrees.httpPost(form.action, new FormData(form)) 89545ed5d1dSGreg Roach .then(response => response.json()) 89645ed5d1dSGreg Roach .then(json => { 89745ed5d1dSGreg Roach if (select) { 89845ed5d1dSGreg Roach // This modal was activated by the "create new" button in a select edit control. 89945ed5d1dSGreg Roach webtrees.resetTomSelect(select.tomselect, json.value, json.text); 90045ed5d1dSGreg Roach 90145ed5d1dSGreg Roach bootstrap.Modal.getInstance(modal).hide(); 90245ed5d1dSGreg Roach } else { 90345ed5d1dSGreg Roach // Show the success message in the existing modal. 90445ed5d1dSGreg Roach modal_content.innerHTML = json.html; 905c8d78f19SGreg Roach } 90645ed5d1dSGreg Roach }) 90745ed5d1dSGreg Roach .catch(error => { 90845ed5d1dSGreg Roach modal_content.innerHTML = error; 90945ed5d1dSGreg Roach }); 91045ed5d1dSGreg Roach }; 91148c46458SGreg Roach 91248c46458SGreg Roach /** 91348c46458SGreg Roach * Text areas don't support the pattern attribute, so apply it manually via data-wt-pattern. 91448c46458SGreg Roach * 91548c46458SGreg Roach * @param {HTMLFormElement} form 91648c46458SGreg Roach */ 91748c46458SGreg Roach webtrees.textareaPatterns = function (form) { 91848c46458SGreg Roach form.addEventListener('submit', function (event) { 91948c46458SGreg Roach event.target.querySelectorAll('textarea[data-wt-pattern]').forEach(function (element) { 92048c46458SGreg Roach const pattern = new RegExp('^' + element.dataset.wtPattern + '$'); 92148c46458SGreg Roach 92248c46458SGreg Roach if (!element.readOnly && element.value !== '' && !pattern.test(element.value)) { 92348c46458SGreg Roach event.preventDefault(); 92448c46458SGreg Roach event.stopPropagation(); 92548c46458SGreg Roach element.classList.add('is-invalid'); 92648c46458SGreg Roach element.scrollIntoView(); 92748c46458SGreg Roach } else { 92848c46458SGreg Roach element.classList.remove('is-invalid'); 92948c46458SGreg Roach } 93048c46458SGreg Roach }); 93148c46458SGreg Roach }); 93248c46458SGreg Roach }; 9332cf1b3d7SGreg Roach}(window.webtrees = window.webtrees || {})); 93471239cb6SGreg Roach 93571239cb6SGreg Roach// Send the CSRF token on all AJAX requests 93671239cb6SGreg Roach$.ajaxSetup({ 93771239cb6SGreg Roach headers: { 93871239cb6SGreg Roach 'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content') 93971239cb6SGreg Roach } 94071239cb6SGreg Roach}); 94171239cb6SGreg Roach 9424ed7dff1SGreg Roach/** 9434ed7dff1SGreg Roach * Initialisation 9444ed7dff1SGreg Roach */ 9454ed7dff1SGreg Roach$(function () { 9462cf1b3d7SGreg Roach // Page elements that load automatically via AJAX. 94771239cb6SGreg Roach // This prevents bad robots from crawling resource-intensive pages. 948d4786c66SGreg Roach $('[data-wt-ajax-url]').each(function () { 949d4786c66SGreg Roach $(this).load(this.dataset.wtAjaxUrl); 95071239cb6SGreg Roach }); 95171239cb6SGreg Roach 95271239cb6SGreg Roach // Autocomplete 953d4786c66SGreg Roach webtrees.autocomplete('input[data-wt-autocomplete-url]'); 95471239cb6SGreg Roach 955c8d78f19SGreg Roach document.querySelectorAll('.tom-select').forEach(element => webtrees.initializeTomSelect(element)); 956896a5721SGreg Roach 957896a5721SGreg Roach // If we clear the select (using the "X" button), we need an empty value 958896a5721SGreg Roach // (rather than no value at all) for (non-multiple) selects with name="array[]" 959c8d78f19SGreg Roach document.querySelectorAll('select.tom-select:not([multiple])') 960c8d78f19SGreg Roach .forEach(function (element) { 961c8d78f19SGreg Roach element.addEventListener('clear', function () { 962c8d78f19SGreg Roach webtrees.resetTomSelect(element.tomselect, '', ''); 963c8d78f19SGreg Roach }); 964bdbdb10cSGreg Roach }); 96571239cb6SGreg Roach 96671239cb6SGreg Roach // Datatables - locale aware sorting 96771239cb6SGreg Roach $.fn.dataTableExt.oSort['text-asc'] = function (x, y) { 968efd89170SGreg Roach return x.localeCompare(y, document.documentElement.lang, { sensitivity: 'base' }); 96971239cb6SGreg Roach }; 97071239cb6SGreg Roach $.fn.dataTableExt.oSort['text-desc'] = function (x, y) { 971efd89170SGreg Roach return y.localeCompare(x, document.documentElement.lang, { sensitivity: 'base' }); 97271239cb6SGreg Roach }; 97371239cb6SGreg Roach 97471239cb6SGreg Roach // DataTables - start hidden to prevent FOUC. 97571239cb6SGreg Roach $('table.datatables').each(function () { 9764843b94fSGreg Roach $(this).DataTable(); 9774843b94fSGreg Roach $(this).removeClass('d-none'); 9784843b94fSGreg Roach }); 9794843b94fSGreg Roach 9802d8276baSGreg Roach // Save button/checkbox state between pages 9812d8276baSGreg Roach document.querySelectorAll('[data-wt-persist]') 9822d8276baSGreg Roach .forEach((element) => webtrees.persistentToggle(element)); 98371239cb6SGreg Roach 98471239cb6SGreg Roach // Activate the on-screen keyboard 9852cf1b3d7SGreg Roach let osk_focus_element; 98671239cb6SGreg Roach $('.wt-osk-trigger').click(function () { 98771239cb6SGreg Roach // When a user clicks the icon, set focus to the corresponding input 988d4786c66SGreg Roach osk_focus_element = document.getElementById(this.dataset.wtId); 98971239cb6SGreg Roach osk_focus_element.focus(); 99071239cb6SGreg Roach $('.wt-osk').show(); 99171239cb6SGreg Roach }); 99271239cb6SGreg Roach $('.wt-osk-script-button').change(function () { 99371239cb6SGreg Roach $('.wt-osk-script').prop('hidden', true); 994d4786c66SGreg Roach $('.wt-osk-script-' + this.dataset.wtOskScript).prop('hidden', false); 99571239cb6SGreg Roach }); 99671239cb6SGreg Roach $('.wt-osk-shift-button').click(function () { 99771239cb6SGreg Roach document.querySelector('.wt-osk-keys').classList.toggle('shifted'); 99871239cb6SGreg Roach }); 99971239cb6SGreg Roach $('.wt-osk-keys').on('click', '.wt-osk-key', function () { 10002cf1b3d7SGreg Roach let key = $(this).contents().get(0).nodeValue; 10012cf1b3d7SGreg Roach let shift_state = $('.wt-osk-shift-button').hasClass('active'); 10022cf1b3d7SGreg Roach let shift_key = $('sup', this)[0]; 100371239cb6SGreg Roach if (shift_state && shift_key !== undefined) { 100471239cb6SGreg Roach key = shift_key.innerText; 100571239cb6SGreg Roach } 10060d2905f7SGreg Roach webtrees.pasteAtCursor(osk_focus_element, key); 100771239cb6SGreg Roach if ($('.wt-osk-pin-button').hasClass('active') === false) { 100871239cb6SGreg Roach $('.wt-osk').hide(); 100971239cb6SGreg Roach } 1010ee51991cSGreg Roach osk_focus_element.dispatchEvent(new Event('input')); 101171239cb6SGreg Roach }); 101271239cb6SGreg Roach 101371239cb6SGreg Roach $('.wt-osk-close').on('click', function () { 101471239cb6SGreg Roach $('.wt-osk').hide(); 101571239cb6SGreg Roach }); 1016b52a415dSGreg Roach 1017b52a415dSGreg Roach // Hide/Show password fields 1018b52a415dSGreg Roach $('input[type=password]').each(function () { 1019b52a415dSGreg Roach $(this).hideShowPassword('infer', true, { 1020b52a415dSGreg Roach states: { 1021b52a415dSGreg Roach shown: { 1022b52a415dSGreg Roach toggle: { 1023d4786c66SGreg Roach content: this.dataset.wtHidePasswordText, 1024b52a415dSGreg Roach attr: { 1025d4786c66SGreg Roach title: this.dataset.wtHidePasswordTitle, 1026d4786c66SGreg Roach 'aria-label': this.dataset.wtHidePasswordTitle, 1027b52a415dSGreg Roach } 1028b52a415dSGreg Roach } 1029b52a415dSGreg Roach }, 1030b52a415dSGreg Roach hidden: { 1031b52a415dSGreg Roach toggle: { 1032d4786c66SGreg Roach content: this.dataset.wtShowPasswordText, 1033b52a415dSGreg Roach attr: { 1034d4786c66SGreg Roach title: this.dataset.wtShowPasswordTitle, 1035d4786c66SGreg Roach 'aria-label': this.dataset.wtShowPasswordTitle, 1036b52a415dSGreg Roach } 1037b52a415dSGreg Roach } 1038b52a415dSGreg Roach } 1039b52a415dSGreg Roach } 1040b52a415dSGreg Roach }); 1041b52a415dSGreg Roach }); 104271239cb6SGreg Roach}); 10437adfb8e5SGreg Roach 1044d6edd2ebSGreg Roach// Prevent form re-submission via accidental double-click. 1045d6edd2ebSGreg Roachdocument.addEventListener('submit', function (event) { 1046d6edd2ebSGreg Roach const form = event.target; 1047d6edd2ebSGreg Roach 1048d6edd2ebSGreg Roach if (form.reportValidity()) { 1049d6edd2ebSGreg Roach form.addEventListener('submit', (event) => { 1050d6edd2ebSGreg Roach if (form.classList.contains('form-is-submitting')) { 1051d6edd2ebSGreg Roach event.preventDefault(); 1052d6edd2ebSGreg Roach } 1053d6edd2ebSGreg Roach 1054d6edd2ebSGreg Roach form.classList.add('form-is-submitting'); 1055d6edd2ebSGreg Roach }); 1056d6edd2ebSGreg Roach } 1057d6edd2ebSGreg Roach}); 1058d6edd2ebSGreg Roach 1059d4786c66SGreg Roach// Convert data-wt-confirm and data-wt-post-url/data-wt-reload-url attributes into useful behavior. 1060efd89170SGreg Roachdocument.addEventListener('click', (event) => { 1061a7a3d6dbSGreg Roach const target = event.target.closest('a,button'); 10627adfb8e5SGreg Roach 10637adfb8e5SGreg Roach if (target === null) { 10647adfb8e5SGreg Roach return; 10657adfb8e5SGreg Roach } 10667adfb8e5SGreg Roach 1067d4786c66SGreg Roach if ('wtConfirm' in target.dataset && !confirm(target.dataset.wtConfirm)) { 10687adfb8e5SGreg Roach event.preventDefault(); 1069c433703eSGreg Roach return; 10707adfb8e5SGreg Roach } 10717adfb8e5SGreg Roach 1072d4786c66SGreg Roach if ('wtPostUrl' in target.dataset) { 1073313cf418SGreg Roach webtrees.httpPost(target.dataset.wtPostUrl).then(() => { 1074d4786c66SGreg Roach if ('wtReloadUrl' in target.dataset) { 1075d4786c66SGreg Roach // Go somewhere else. e.g. the home page after logout. 1076d4786c66SGreg Roach document.location = target.dataset.wtReloadUrl; 1077ea101122SGreg Roach } else { 1078ea101122SGreg Roach // Reload the current page. e.g. change language. 10797adfb8e5SGreg Roach document.location.reload(); 10807adfb8e5SGreg Roach } 1081cb8f307bSGreg Roach }).catch((error) => { 1082ea101122SGreg Roach alert(error); 1083ea101122SGreg Roach }); 10847adfb8e5SGreg Roach } 10857adfb8e5SGreg Roach}); 1086