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 /** 4059e18f0cSGreg Roach * Look for non-latin characters in a string. 4106fe1eb5SGreg Roach * @param {string} str 4206fe1eb5SGreg Roach * @returns {string} 4359e18f0cSGreg Roach */ 440d2905f7SGreg Roach webtrees.detectScript = function (str) { 450d2905f7SGreg Roach for (const script in scriptRegexes) { 460d2905f7SGreg Roach if (str.match(scriptRegexes[script])) { 470d2905f7SGreg Roach return script; 480d2905f7SGreg Roach } 4959e18f0cSGreg Roach } 5059e18f0cSGreg Roach 51efd89170SGreg Roach return 'Latn'; 520d2905f7SGreg Roach }; 5359e18f0cSGreg Roach 5459e18f0cSGreg Roach /** 5559e18f0cSGreg Roach * In some languages, the SURN uses a male/default form, but NAME uses a gender-inflected form. 5606fe1eb5SGreg Roach * @param {string} surname 5706fe1eb5SGreg Roach * @param {string} sex 5806fe1eb5SGreg Roach * @returns {string} 5959e18f0cSGreg Roach */ 6059e18f0cSGreg Roach function inflectSurname (surname, sex) { 61efd89170SGreg Roach if (lang === 'pl' && sex === 'F') { 6259e18f0cSGreg Roach return surname 63efd89170SGreg Roach .replace(/ski$/, 'ska') 64efd89170SGreg Roach .replace(/cki$/, 'cka') 65efd89170SGreg Roach .replace(/dzki$/, 'dzka') 66efd89170SGreg Roach .replace(/żki$/, 'żka'); 6759e18f0cSGreg Roach } 6859e18f0cSGreg Roach 6959e18f0cSGreg Roach return surname; 7059e18f0cSGreg Roach } 7159e18f0cSGreg Roach 7259e18f0cSGreg Roach /** 7359e18f0cSGreg Roach * Build a NAME from a NPFX, GIVN, SPFX, SURN and NSFX parts. 7459e18f0cSGreg Roach * Assumes the language of the document is the same as the language of the name. 7506fe1eb5SGreg Roach * @param {string} npfx 7606fe1eb5SGreg Roach * @param {string} givn 7706fe1eb5SGreg Roach * @param {string} spfx 7806fe1eb5SGreg Roach * @param {string} surn 7906fe1eb5SGreg Roach * @param {string} nsfx 8006fe1eb5SGreg Roach * @param {string} sex 8106fe1eb5SGreg Roach * @returns {string} 8259e18f0cSGreg Roach */ 830d2905f7SGreg Roach webtrees.buildNameFromParts = function (npfx, givn, spfx, surn, nsfx, sex) { 84efd89170SGreg Roach const usesCJK = webtrees.detectScript(npfx + givn + spfx + givn + surn + nsfx) === 'Han'; 85efd89170SGreg Roach const separator = usesCJK ? '' : ' '; 8659e18f0cSGreg Roach const surnameFirst = usesCJK || ['hu', 'jp', 'ko', 'vi', 'zh-Hans', 'zh-Hant'].indexOf(lang) !== -1; 8759e18f0cSGreg Roach const patronym = ['is'].indexOf(lang) !== -1; 88efd89170SGreg Roach const slash = patronym ? '' : '/'; 8959e18f0cSGreg Roach 9059e18f0cSGreg Roach // GIVN and SURN may be a comma-separated lists. 9159e18f0cSGreg Roach npfx = trim(npfx); 92063107dbSGreg Roach givn = trim(givn.replace(/,/g, separator)); 9359e18f0cSGreg Roach spfx = trim(spfx); 94063107dbSGreg Roach surn = inflectSurname(trim(surn.replace(/,/g, separator)), sex); 9559e18f0cSGreg Roach nsfx = trim(nsfx); 9659e18f0cSGreg Roach 979026ef5bSGreg Roach const surname_separator = spfx.endsWith('\'') || spfx.endsWith('‘') ? '' : ' '; 989026ef5bSGreg Roach 999026ef5bSGreg Roach const surname = trim(spfx + surname_separator + surn); 10059e18f0cSGreg Roach 10159e18f0cSGreg Roach const name = surnameFirst ? slash + surname + slash + separator + givn : givn + separator + slash + surname + slash; 10259e18f0cSGreg Roach 10359e18f0cSGreg Roach return trim(npfx + separator + name + separator + nsfx); 1040d2905f7SGreg Roach }; 1050d2905f7SGreg Roach 1060d2905f7SGreg Roach // Insert text at the current cursor position in a text field. 1070d2905f7SGreg Roach webtrees.pasteAtCursor = function (element, text) { 1080d2905f7SGreg Roach if (element !== null) { 1090d2905f7SGreg Roach const caret_pos = element.selectionStart + text.length; 1100d2905f7SGreg Roach const textBefore = element.value.substring(0, element.selectionStart); 1110d2905f7SGreg Roach const textAfter = element.value.substring(element.selectionEnd); 1120d2905f7SGreg Roach element.value = textBefore + text + textAfter; 1130d2905f7SGreg Roach element.setSelectionRange(caret_pos, caret_pos); 1140d2905f7SGreg Roach element.focus(); 11559e18f0cSGreg Roach } 116efd89170SGreg Roach }; 11771239cb6SGreg Roach 11806fe1eb5SGreg Roach /** 1197fb78f8aSGreg Roach * @param {Element} datefield 12006fe1eb5SGreg Roach * @param {string} dmy 12106fe1eb5SGreg Roach */ 122a7a3d6dbSGreg Roach webtrees.reformatDate = function (datefield, dmy) { 123a7a3d6dbSGreg Roach const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']; 124a7a3d6dbSGreg Roach const hijri_months = ['MUHAR', 'SAFAR', 'RABIA', 'RABIT', 'JUMAA', 'JUMAT', 'RAJAB', 'SHAAB', 'RAMAD', 'SHAWW', 'DHUAQ', 'DHUAH']; 125a7a3d6dbSGreg Roach const hebrew_months = ['TSH', 'CSH', 'KSL', 'TVT', 'SHV', 'ADR', 'ADS', 'NSN', 'IYR', 'SVN', 'TMZ', 'AAV', 'ELL']; 126a7a3d6dbSGreg Roach const french_months = ['VEND', 'BRUM', 'FRIM', 'NIVO', 'PLUV', 'VENT', 'GERM', 'FLOR', 'PRAI', 'MESS', 'THER', 'FRUC', 'COMP']; 127a7a3d6dbSGreg Roach const jalali_months = ['FARVA', 'ORDIB', 'KHORD', 'TIR', 'MORDA', 'SHAHR', 'MEHR', 'ABAN', 'AZAR', 'DEY', 'BAHMA', 'ESFAN']; 12871239cb6SGreg Roach 129a7a3d6dbSGreg Roach let datestr = datefield.value; 13071239cb6SGreg Roach // if a date has a date phrase marked by () this has to be excluded from altering 131a7a3d6dbSGreg Roach let datearr = datestr.split('('); 132a7a3d6dbSGreg Roach let datephrase = ''; 13371239cb6SGreg Roach if (datearr.length > 1) { 13471239cb6SGreg Roach datestr = datearr[0]; 13571239cb6SGreg Roach datephrase = datearr[1]; 13671239cb6SGreg Roach } 13771239cb6SGreg Roach 13871239cb6SGreg Roach // Gedcom dates are upper case 13971239cb6SGreg Roach datestr = datestr.toUpperCase(); 14071239cb6SGreg Roach // Gedcom dates have no leading/trailing/repeated whitespace 14180d699d6SGreg Roach datestr = datestr.replace(/\s+/g, ' '); 14271239cb6SGreg Roach datestr = datestr.replace(/(^\s)|(\s$)/, ''); 14371239cb6SGreg Roach // Gedcom dates have spaces between letters and digits, e.g. "01JAN2000" => "01 JAN 2000" 14480d699d6SGreg Roach datestr = datestr.replace(/(\d)([A-Z])/g, '$1 $2'); 14580d699d6SGreg Roach datestr = datestr.replace(/([A-Z])(\d)/g, '$1 $2'); 14671239cb6SGreg Roach 14745a1224aSGreg Roach // Shortcut for quarter format, "Q1 1900" => "BET JAN 1900 AND MAR 1900". 14871239cb6SGreg Roach if (datestr.match(/^Q ([1-4]) (\d\d\d\d)$/)) { 14971239cb6SGreg Roach datestr = 'BET ' + months[RegExp.$1 * 3 - 3] + ' ' + RegExp.$2 + ' AND ' + months[RegExp.$1 * 3 - 1] + ' ' + RegExp.$2; 15071239cb6SGreg Roach } 15171239cb6SGreg Roach 15271239cb6SGreg Roach // Shortcut for @#Dxxxxx@ 01 01 1400, etc. 15371239cb6SGreg Roach if (datestr.match(/^(@#DHIJRI@|HIJRI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 15471239cb6SGreg Roach datestr = '@#DHIJRI@' + RegExp.$2 + hijri_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 15571239cb6SGreg Roach } 15671239cb6SGreg Roach if (datestr.match(/^(@#DJALALI@|JALALI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 15771239cb6SGreg Roach datestr = '@#DJALALI@' + RegExp.$2 + jalali_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 15871239cb6SGreg Roach } 15971239cb6SGreg Roach if (datestr.match(/^(@#DHEBREW@|HEBREW)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 16071239cb6SGreg Roach datestr = '@#DHEBREW@' + RegExp.$2 + hebrew_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 16171239cb6SGreg Roach } 16271239cb6SGreg Roach if (datestr.match(/^(@#DFRENCH R@|FRENCH)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 16371239cb6SGreg Roach datestr = '@#DFRENCH R@' + RegExp.$2 + french_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 16471239cb6SGreg Roach } 16571239cb6SGreg Roach 1667fb78f8aSGreg Roach // All digit dates 1674ad647d5SGreg Roach datestr = datestr.replace(/(\d\d)(\d\d)(\d\d)(\d\d)/g, function () { 1687fb78f8aSGreg Roach if (RegExp.$1 > '12' && RegExp.$3 <= '12' && RegExp.$4 <= '31') { 1697fb78f8aSGreg Roach return RegExp.$4 + ' ' + months[RegExp.$3 - 1] + ' ' + RegExp.$1 + RegExp.$2; 1707fb78f8aSGreg Roach } 1717fb78f8aSGreg Roach if (RegExp.$1 <= '31' && RegExp.$2 <= '12' && RegExp.$3 > '12') { 1727fb78f8aSGreg Roach return RegExp.$1 + ' ' + months[RegExp.$2 - 1] + ' ' + RegExp.$3 + RegExp.$4; 1737fb78f8aSGreg Roach } 1747fb78f8aSGreg Roach return RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4; 1757fb78f8aSGreg Roach }); 1767fb78f8aSGreg Roach 17745a1224aSGreg Roach // e.g. 17.11.1860, 2 4 1987, 3/4/2005, 1999-12-31. Use locale settings since DMY order is ambiguous. 17845a1224aSGreg Roach datestr = datestr.replace(/(\d+)([ ./-])(\d+)(\2)(\d+)/g, function () { 1792cf1b3d7SGreg Roach let f1 = parseInt(RegExp.$1, 10); 1802cf1b3d7SGreg Roach let f2 = parseInt(RegExp.$3, 10); 1812cf1b3d7SGreg Roach let f3 = parseInt(RegExp.$5, 10); 1822cf1b3d7SGreg Roach let yyyy = new Date().getFullYear(); 1832cf1b3d7SGreg Roach let yy = yyyy % 100; 1842cf1b3d7SGreg Roach let cc = yyyy - yy; 185dee63285SGreg Roach if ((dmy === 'DMY' || f1 > 13 && f3 > 31) && f1 <= 31 && f2 <= 12) { 1867fb78f8aSGreg Roach return f1 + ' ' + months[f2 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 1877fb78f8aSGreg Roach } 188dee63285SGreg Roach if ((dmy === 'MDY' || f2 > 13 && f3 > 31) && f1 <= 12 && f2 <= 31) { 1897fb78f8aSGreg Roach return f2 + ' ' + months[f1 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 1907fb78f8aSGreg Roach } 191dee63285SGreg Roach if ((dmy === 'YMD' || f1 > 31) && f2 <= 12 && f3 <= 31) { 1927fb78f8aSGreg Roach return f3 + ' ' + months[f2 - 1] + ' ' + (f1 >= 100 ? f1 : (f1 <= yy ? f1 + cc : f1 + cc - 100)); 19371239cb6SGreg Roach } 1947fb78f8aSGreg Roach return RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4 + RegExp.$5; 1957fb78f8aSGreg Roach }); 19671239cb6SGreg Roach 197a7a3d6dbSGreg Roach datestr = datestr 19871239cb6SGreg Roach // Shortcuts for date ranges 199a7a3d6dbSGreg Roach .replace(/^[>]([\w ]+)$/, 'AFT $1') 200a7a3d6dbSGreg Roach .replace(/^[<]([\w ]+)$/, 'BEF $1') 201a7a3d6dbSGreg Roach .replace(/^([\w ]+)[-]$/, 'FROM $1') 202a7a3d6dbSGreg Roach .replace(/^[-]([\w ]+)$/, 'TO $1') 203a7a3d6dbSGreg Roach .replace(/^[~]([\w ]+)$/, 'ABT $1') 204a7a3d6dbSGreg Roach .replace(/^[*]([\w ]+)$/, 'EST $1') 205a7a3d6dbSGreg Roach .replace(/^[#]([\w ]+)$/, 'CAL $1') 206a7a3d6dbSGreg Roach .replace(/^([\w ]+) ?- ?([\w ]+)$/, 'BET $1 AND $2') 207a7a3d6dbSGreg Roach .replace(/^([\w ]+) ?~ ?([\w ]+)$/, 'FROM $1 TO $2') 20871239cb6SGreg Roach // Convert full months to short months 2094ad647d5SGreg Roach .replace(/JANUARY/g, 'JAN') 2104ad647d5SGreg Roach .replace(/FEBRUARY/g, 'FEB') 2114ad647d5SGreg Roach .replace(/MARCH/g, 'MAR') 2124ad647d5SGreg Roach .replace(/APRIL/g, 'APR') 2134ad647d5SGreg Roach .replace(/JUNE/g, 'JUN') 2144ad647d5SGreg Roach .replace(/JULY/g, 'JUL') 2154ad647d5SGreg Roach .replace(/AUGUST/g, 'AUG') 2164ad647d5SGreg Roach .replace(/SEPTEMBER/g, 'SEP') 2174ad647d5SGreg Roach .replace(/OCTOBER/, 'OCT') 2184ad647d5SGreg Roach .replace(/NOVEMBER/g, 'NOV') 2194ad647d5SGreg Roach .replace(/DECEMBER/g, 'DEC') 220a7a3d6dbSGreg Roach // Americans enter dates as SEP 20, 1999 2214ad647d5SGreg Roach .replace(/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\.? (\d\d?)[, ]+(\d\d\d\d)/g, '$2 $1 $3') 22271239cb6SGreg Roach // Apply leading zero to day numbers 2234ad647d5SGreg Roach .replace(/(^| )(\d [A-Z]{3,5} \d{4})/g, '$10$2'); 22471239cb6SGreg Roach 22571239cb6SGreg Roach if (datephrase) { 22671239cb6SGreg Roach datestr = datestr + ' (' + datephrase; 22771239cb6SGreg Roach } 228a7a3d6dbSGreg Roach 22971239cb6SGreg Roach // Only update it if is has been corrected - otherwise input focus 23071239cb6SGreg Roach // moves to the end of the field unnecessarily 23171239cb6SGreg Roach if (datefield.value !== datestr) { 23271239cb6SGreg Roach datefield.value = datestr; 23371239cb6SGreg Roach } 2342cf1b3d7SGreg Roach }; 23571239cb6SGreg Roach 2362cf1b3d7SGreg Roach let monthLabels = []; 23771239cb6SGreg Roach monthLabels[1] = 'January'; 23871239cb6SGreg Roach monthLabels[2] = 'February'; 23971239cb6SGreg Roach monthLabels[3] = 'March'; 24071239cb6SGreg Roach monthLabels[4] = 'April'; 24171239cb6SGreg Roach monthLabels[5] = 'May'; 24271239cb6SGreg Roach monthLabels[6] = 'June'; 24371239cb6SGreg Roach monthLabels[7] = 'July'; 24471239cb6SGreg Roach monthLabels[8] = 'August'; 24571239cb6SGreg Roach monthLabels[9] = 'September'; 24671239cb6SGreg Roach monthLabels[10] = 'October'; 24771239cb6SGreg Roach monthLabels[11] = 'November'; 24871239cb6SGreg Roach monthLabels[12] = 'December'; 24971239cb6SGreg Roach 2502cf1b3d7SGreg Roach let monthShort = []; 25171239cb6SGreg Roach monthShort[1] = 'JAN'; 25271239cb6SGreg Roach monthShort[2] = 'FEB'; 25371239cb6SGreg Roach monthShort[3] = 'MAR'; 25471239cb6SGreg Roach monthShort[4] = 'APR'; 25571239cb6SGreg Roach monthShort[5] = 'MAY'; 25671239cb6SGreg Roach monthShort[6] = 'JUN'; 25771239cb6SGreg Roach monthShort[7] = 'JUL'; 25871239cb6SGreg Roach monthShort[8] = 'AUG'; 25971239cb6SGreg Roach monthShort[9] = 'SEP'; 26071239cb6SGreg Roach monthShort[10] = 'OCT'; 26171239cb6SGreg Roach monthShort[11] = 'NOV'; 26271239cb6SGreg Roach monthShort[12] = 'DEC'; 26371239cb6SGreg Roach 2642cf1b3d7SGreg Roach let daysOfWeek = []; 26571239cb6SGreg Roach daysOfWeek[0] = 'S'; 26671239cb6SGreg Roach daysOfWeek[1] = 'M'; 26771239cb6SGreg Roach daysOfWeek[2] = 'T'; 26871239cb6SGreg Roach daysOfWeek[3] = 'W'; 26971239cb6SGreg Roach daysOfWeek[4] = 'T'; 27071239cb6SGreg Roach daysOfWeek[5] = 'F'; 27171239cb6SGreg Roach daysOfWeek[6] = 'S'; 27271239cb6SGreg Roach 2732cf1b3d7SGreg Roach let weekStart = 0; 27471239cb6SGreg Roach 27506fe1eb5SGreg Roach /** 27606fe1eb5SGreg Roach * @param {string} jan 27706fe1eb5SGreg Roach * @param {string} feb 27806fe1eb5SGreg Roach * @param {string} mar 27906fe1eb5SGreg Roach * @param {string} apr 28006fe1eb5SGreg Roach * @param {string} may 28106fe1eb5SGreg Roach * @param {string} jun 28206fe1eb5SGreg Roach * @param {string} jul 28306fe1eb5SGreg Roach * @param {string} aug 28406fe1eb5SGreg Roach * @param {string} sep 28506fe1eb5SGreg Roach * @param {string} oct 28606fe1eb5SGreg Roach * @param {string} nov 28706fe1eb5SGreg Roach * @param {string} dec 2882cf1b3d7SGreg Roach * @param {string} sun 2892cf1b3d7SGreg Roach * @param {string} mon 2902cf1b3d7SGreg Roach * @param {string} tue 2912cf1b3d7SGreg Roach * @param {string} wed 2922cf1b3d7SGreg Roach * @param {string} thu 2932cf1b3d7SGreg Roach * @param {string} fri 2942cf1b3d7SGreg Roach * @param {string} sat 2952cf1b3d7SGreg Roach * @param {number} day 29606fe1eb5SGreg Roach */ 2972cf1b3d7SGreg Roach webtrees.calLocalize = function (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec, sun, mon, tue, wed, thu, fri, sat, day) { 29871239cb6SGreg Roach monthLabels[1] = jan; 29971239cb6SGreg Roach monthLabels[2] = feb; 30071239cb6SGreg Roach monthLabels[3] = mar; 30171239cb6SGreg Roach monthLabels[4] = apr; 30271239cb6SGreg Roach monthLabels[5] = may; 30371239cb6SGreg Roach monthLabels[6] = jun; 30471239cb6SGreg Roach monthLabels[7] = jul; 30571239cb6SGreg Roach monthLabels[8] = aug; 30671239cb6SGreg Roach monthLabels[9] = sep; 30771239cb6SGreg Roach monthLabels[10] = oct; 30871239cb6SGreg Roach monthLabels[11] = nov; 30971239cb6SGreg Roach monthLabels[12] = dec; 31071239cb6SGreg Roach daysOfWeek[0] = sun; 31171239cb6SGreg Roach daysOfWeek[1] = mon; 31271239cb6SGreg Roach daysOfWeek[2] = tue; 31371239cb6SGreg Roach daysOfWeek[3] = wed; 31471239cb6SGreg Roach daysOfWeek[4] = thu; 31571239cb6SGreg Roach daysOfWeek[5] = fri; 31671239cb6SGreg Roach daysOfWeek[6] = sat; 31771239cb6SGreg Roach 31871239cb6SGreg Roach if (day >= 0 && day < 7) { 31971239cb6SGreg Roach weekStart = day; 32071239cb6SGreg Roach } 3212cf1b3d7SGreg Roach }; 32271239cb6SGreg Roach 32306fe1eb5SGreg Roach /** 32406fe1eb5SGreg Roach * @param {string} dateDivId 32506fe1eb5SGreg Roach * @param {string} dateFieldId 32606fe1eb5SGreg Roach * @returns {boolean} 32706fe1eb5SGreg Roach */ 3282cf1b3d7SGreg Roach webtrees.calendarWidget = function (dateDivId, dateFieldId) { 3292cf1b3d7SGreg Roach let dateDiv = document.getElementById(dateDivId); 3302cf1b3d7SGreg Roach let dateField = document.getElementById(dateFieldId); 33171239cb6SGreg Roach 33271239cb6SGreg Roach if (dateDiv.style.visibility === 'visible') { 33371239cb6SGreg Roach dateDiv.style.visibility = 'hidden'; 33471239cb6SGreg Roach return false; 33571239cb6SGreg Roach } 33671239cb6SGreg Roach if (dateDiv.style.visibility === 'show') { 33771239cb6SGreg Roach dateDiv.style.visibility = 'hide'; 33871239cb6SGreg Roach return false; 33971239cb6SGreg Roach } 34071239cb6SGreg Roach 34171239cb6SGreg Roach /* Javascript calendar functions only work with precise gregorian dates "D M Y" or "Y" */ 342f4ac98a5SGreg Roach let greg_regex = /(?:(\d*) ?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) )?(\d+)/i; 3432cf1b3d7SGreg Roach let date; 34471239cb6SGreg Roach if (greg_regex.exec(dateField.value)) { 345f4ac98a5SGreg Roach let day = RegExp.$1 || '1'; 346f4ac98a5SGreg Roach let month = RegExp.$2 || 'JAN' 347f4ac98a5SGreg Roach let year = RegExp.$3; 348f4ac98a5SGreg Roach date = new Date(day + ' ' + month + ' ' + year); 34971239cb6SGreg Roach } else { 35071239cb6SGreg Roach date = new Date(); 35171239cb6SGreg Roach } 35271239cb6SGreg Roach 3532cf1b3d7SGreg Roach dateDiv.innerHTML = calGenerateSelectorContent(dateFieldId, dateDivId, date); 35471239cb6SGreg Roach if (dateDiv.style.visibility === 'hidden') { 35571239cb6SGreg Roach dateDiv.style.visibility = 'visible'; 35671239cb6SGreg Roach return false; 35771239cb6SGreg Roach } 35871239cb6SGreg Roach if (dateDiv.style.visibility === 'hide') { 35971239cb6SGreg Roach dateDiv.style.visibility = 'show'; 36071239cb6SGreg Roach return false; 36171239cb6SGreg Roach } 36271239cb6SGreg Roach 36371239cb6SGreg Roach return false; 3642cf1b3d7SGreg Roach }; 36571239cb6SGreg Roach 36606fe1eb5SGreg Roach /** 36706fe1eb5SGreg Roach * @param {string} dateFieldId 36806fe1eb5SGreg Roach * @param {string} dateDivId 36906fe1eb5SGreg Roach * @param {Date} date 37006fe1eb5SGreg Roach * @returns {string} 37106fe1eb5SGreg Roach */ 3722cf1b3d7SGreg Roach function calGenerateSelectorContent (dateFieldId, dateDivId, date) { 3732cf1b3d7SGreg Roach let i, j; 3742cf1b3d7SGreg Roach let content = '<table border="1"><tr>'; 3752cf1b3d7SGreg Roach content += '<td><select class="form-control" id="' + dateFieldId + '_daySelect" onchange="return webtrees.calUpdateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 37671239cb6SGreg Roach for (i = 1; i < 32; i++) { 37771239cb6SGreg Roach content += '<option value="' + i + '"'; 37871239cb6SGreg Roach if (date.getDate() === i) { 37971239cb6SGreg Roach content += ' selected="selected"'; 38071239cb6SGreg Roach } 38171239cb6SGreg Roach content += '>' + i + '</option>'; 38271239cb6SGreg Roach } 38371239cb6SGreg Roach content += '</select></td>'; 3842cf1b3d7SGreg Roach content += '<td><select class="form-control" id="' + dateFieldId + '_monSelect" onchange="return webtrees.calUpdateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 38571239cb6SGreg Roach for (i = 1; i < 13; i++) { 38671239cb6SGreg Roach content += '<option value="' + i + '"'; 38771239cb6SGreg Roach if (date.getMonth() + 1 === i) { 38871239cb6SGreg Roach content += ' selected="selected"'; 38971239cb6SGreg Roach } 39071239cb6SGreg Roach content += '>' + monthLabels[i] + '</option>'; 39171239cb6SGreg Roach } 39271239cb6SGreg Roach content += '</select></td>'; 3932cf1b3d7SGreg 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>'; 39471239cb6SGreg Roach content += '<tr><td colspan="3">'; 39571239cb6SGreg Roach content += '<table width="100%">'; 39671239cb6SGreg Roach content += '<tr>'; 39771239cb6SGreg Roach j = weekStart; 39871239cb6SGreg Roach for (i = 0; i < 7; i++) { 39971239cb6SGreg Roach content += '<td '; 40071239cb6SGreg Roach content += 'class="descriptionbox"'; 40171239cb6SGreg Roach content += '>'; 40271239cb6SGreg Roach content += daysOfWeek[j]; 40371239cb6SGreg Roach content += '</td>'; 40471239cb6SGreg Roach j++; 40571239cb6SGreg Roach if (j > 6) { 40671239cb6SGreg Roach j = 0; 40771239cb6SGreg Roach } 40871239cb6SGreg Roach } 40971239cb6SGreg Roach content += '</tr>'; 41071239cb6SGreg Roach 4112cf1b3d7SGreg Roach let tdate = new Date(date.getFullYear(), date.getMonth(), 1); 4122cf1b3d7SGreg Roach let day = tdate.getDay(); 41371239cb6SGreg Roach day = day - weekStart; 4142cf1b3d7SGreg Roach let daymilli = 1000 * 60 * 60 * 24; 41571239cb6SGreg Roach tdate = tdate.getTime() - (day * daymilli) + (daymilli / 2); 41671239cb6SGreg Roach tdate = new Date(tdate); 41771239cb6SGreg Roach 41871239cb6SGreg Roach for (j = 0; j < 6; j++) { 41971239cb6SGreg Roach content += '<tr>'; 42071239cb6SGreg Roach for (i = 0; i < 7; i++) { 42171239cb6SGreg Roach content += '<td '; 42271239cb6SGreg Roach if (tdate.getMonth() === date.getMonth()) { 42371239cb6SGreg Roach if (tdate.getDate() === date.getDate()) { 42471239cb6SGreg Roach content += 'class="descriptionbox"'; 42571239cb6SGreg Roach } else { 42671239cb6SGreg Roach content += 'class="optionbox"'; 42771239cb6SGreg Roach } 42871239cb6SGreg Roach } else { 42971239cb6SGreg Roach content += 'style="background-color:#EAEAEA; border: solid #AAAAAA 1px;"'; 43071239cb6SGreg Roach } 4312cf1b3d7SGreg Roach content += '><a href="#" onclick="return webtrees.calDateClicked(\'' + dateFieldId + '\', \'' + dateDivId + '\', ' + tdate.getFullYear() + ', ' + tdate.getMonth() + ', ' + tdate.getDate() + ');">'; 43271239cb6SGreg Roach content += tdate.getDate(); 43371239cb6SGreg Roach content += '</a></td>'; 4342cf1b3d7SGreg Roach let datemilli = tdate.getTime() + daymilli; 43571239cb6SGreg Roach tdate = new Date(datemilli); 43671239cb6SGreg Roach } 43771239cb6SGreg Roach content += '</tr>'; 43871239cb6SGreg Roach } 43971239cb6SGreg Roach content += '</table>'; 44071239cb6SGreg Roach content += '</td></tr>'; 44171239cb6SGreg Roach content += '</table>'; 44271239cb6SGreg Roach 44371239cb6SGreg Roach return content; 44471239cb6SGreg Roach } 44571239cb6SGreg Roach 44606fe1eb5SGreg Roach /** 44706fe1eb5SGreg Roach * @param {string} dateFieldId 44806fe1eb5SGreg Roach * @param {number} year 44906fe1eb5SGreg Roach * @param {number} month 45006fe1eb5SGreg Roach * @param {number} day 45106fe1eb5SGreg Roach * @returns {boolean} 45206fe1eb5SGreg Roach */ 4532cf1b3d7SGreg Roach function calSetDateField (dateFieldId, year, month, day) { 4542cf1b3d7SGreg Roach let dateField = document.getElementById(dateFieldId); 4552cf1b3d7SGreg Roach dateField.value = (day < 10 ? '0' : '') + day + ' ' + monthShort[month + 1] + ' ' + year; 45671239cb6SGreg Roach return false; 45771239cb6SGreg Roach } 45871239cb6SGreg Roach 45906fe1eb5SGreg Roach /** 46006fe1eb5SGreg Roach * @param {string} dateFieldId 46106fe1eb5SGreg Roach * @param {string} dateDivId 46206fe1eb5SGreg Roach * @returns {boolean} 46306fe1eb5SGreg Roach */ 4642cf1b3d7SGreg Roach webtrees.calUpdateCalendar = function (dateFieldId, dateDivId) { 4652cf1b3d7SGreg Roach let dateSel = document.getElementById(dateFieldId + '_daySelect'); 46671239cb6SGreg Roach if (!dateSel) { 46771239cb6SGreg Roach return false; 46871239cb6SGreg Roach } 4692cf1b3d7SGreg Roach let monthSel = document.getElementById(dateFieldId + '_monSelect'); 47071239cb6SGreg Roach if (!monthSel) { 47171239cb6SGreg Roach return false; 47271239cb6SGreg Roach } 4732cf1b3d7SGreg Roach let yearInput = document.getElementById(dateFieldId + '_yearInput'); 47471239cb6SGreg Roach if (!yearInput) { 47571239cb6SGreg Roach return false; 47671239cb6SGreg Roach } 47771239cb6SGreg Roach 4782cf1b3d7SGreg Roach let month = parseInt(monthSel.options[monthSel.selectedIndex].value, 10); 47971239cb6SGreg Roach month = month - 1; 48071239cb6SGreg Roach 4812cf1b3d7SGreg Roach let date = new Date(yearInput.value, month, dateSel.options[dateSel.selectedIndex].value); 4822cf1b3d7SGreg Roach calSetDateField(dateFieldId, date.getFullYear(), date.getMonth(), date.getDate()); 48371239cb6SGreg Roach 4842cf1b3d7SGreg Roach let dateDiv = document.getElementById(dateDivId); 48571239cb6SGreg Roach if (!dateDiv) { 48671239cb6SGreg Roach alert('no dateDiv ' + dateDivId); 48771239cb6SGreg Roach return false; 48871239cb6SGreg Roach } 4892cf1b3d7SGreg Roach dateDiv.innerHTML = calGenerateSelectorContent(dateFieldId, dateDivId, date); 49071239cb6SGreg Roach 49171239cb6SGreg Roach return false; 4922cf1b3d7SGreg Roach }; 49371239cb6SGreg Roach 49406fe1eb5SGreg Roach /** 49506fe1eb5SGreg Roach * @param {string} dateFieldId 49606fe1eb5SGreg Roach * @param {string} dateDivId 49706fe1eb5SGreg Roach * @param {number} year 49806fe1eb5SGreg Roach * @param {number} month 49906fe1eb5SGreg Roach * @param {number} day 50006fe1eb5SGreg Roach * @returns {boolean} 50106fe1eb5SGreg Roach */ 5022cf1b3d7SGreg Roach webtrees.calDateClicked = function (dateFieldId, dateDivId, year, month, day) { 5032cf1b3d7SGreg Roach calSetDateField(dateFieldId, year, month, day); 5042cf1b3d7SGreg Roach webtrees.calendarWidget(dateDivId, dateFieldId); 50571239cb6SGreg Roach return false; 5062cf1b3d7SGreg Roach }; 50771239cb6SGreg Roach 50806fe1eb5SGreg Roach /** 5092cf1b3d7SGreg Roach * Persistent checkbox options to hide/show extra data. 5102d8276baSGreg Roach * @param {HTMLInputElement} element 51171239cb6SGreg Roach */ 5122d8276baSGreg Roach webtrees.persistentToggle = function (element) { 5132d8276baSGreg Roach if (element instanceof HTMLInputElement && element.type === 'checkbox') { 5142d8276baSGreg Roach const key = 'state-of-' + element.dataset.wtPersist; 515efd89170SGreg Roach const state = localStorage.getItem(key); 51671239cb6SGreg Roach 5172d8276baSGreg Roach // Previously selected? Select again now. 51864490ee2SGreg Roach if (state === 'true') { 5192cf1b3d7SGreg Roach element.click(); 52071239cb6SGreg Roach } 52171239cb6SGreg Roach 52264490ee2SGreg Roach // Remember state for the next page load. 5232cf1b3d7SGreg Roach element.addEventListener('change', function () { 5242d8276baSGreg Roach localStorage.setItem(key, element.checked.toString()); 5252cf1b3d7SGreg Roach }); 526adbf37b7SGreg Roach } 5272cf1b3d7SGreg Roach }; 52871239cb6SGreg Roach 52906fe1eb5SGreg Roach /** 5302cf1b3d7SGreg Roach * @param {Element} field 53106fe1eb5SGreg Roach * @param {string} pos 53206fe1eb5SGreg Roach * @param {string} neg 53306fe1eb5SGreg Roach */ 5342cf1b3d7SGreg Roach function reformatLatLong (field, pos, neg) { 53571239cb6SGreg Roach // valid LATI or LONG according to Gedcom standard 53671239cb6SGreg Roach // pos (+) : N or E 53771239cb6SGreg Roach // neg (-) : S or W 5382cf1b3d7SGreg Roach let txt = field.value.toUpperCase(); 53971239cb6SGreg Roach txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim 54071239cb6SGreg Roach txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34 54171239cb6SGreg Roach txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234 54271239cb6SGreg Roach txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698 54371239cb6SGreg Roach txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698 54471239cb6SGreg Roach // 0°34'11 ==> 0:34:11 54571239cb6SGreg Roach txt = txt.replace(/\u00b0/g, ':'); // ° 54671239cb6SGreg Roach txt = txt.replace(/\u0027/g, ':'); // ' 54771239cb6SGreg Roach // 0:34:11.2W ==> W0.5698 54871239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) { 5492cf1b3d7SGreg Roach let n = parseFloat($1); 55071239cb6SGreg Roach n += ($2 / 60); 55171239cb6SGreg Roach n += ($3 / 3600); 55271239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 55371239cb6SGreg Roach return $4 + n; 55471239cb6SGreg Roach }); 55571239cb6SGreg Roach // 0:34W ==> W0.5667 55671239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) { 5572cf1b3d7SGreg Roach let n = parseFloat($1); 55871239cb6SGreg Roach n += ($2 / 60); 55971239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 56071239cb6SGreg Roach return $3 + n; 56171239cb6SGreg Roach }); 56271239cb6SGreg Roach // 0.5698W ==> W0.5698 5632cf1b3d7SGreg Roach txt = txt.replace(/(.*)(NSEW])$/g, '$2$1'); 56471239cb6SGreg Roach // 17.1234 ==> N17.1234 56571239cb6SGreg Roach if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) { 56671239cb6SGreg Roach txt = pos + txt; 56771239cb6SGreg Roach } 56871239cb6SGreg Roach field.value = txt; 56971239cb6SGreg Roach } 57071239cb6SGreg Roach 57106fe1eb5SGreg Roach /** 5722cf1b3d7SGreg Roach * @param {Element} field 5732cf1b3d7SGreg Roach */ 5742cf1b3d7SGreg Roach webtrees.reformatLatitude = function (field) { 5752cf1b3d7SGreg Roach return reformatLatLong(field, 'N', 'S'); 5762cf1b3d7SGreg Roach }; 5772cf1b3d7SGreg Roach 5782cf1b3d7SGreg Roach /** 5792cf1b3d7SGreg Roach * @param {Element} field 5802cf1b3d7SGreg Roach */ 5812cf1b3d7SGreg Roach webtrees.reformatLongitude = function (field) { 5822cf1b3d7SGreg Roach return reformatLatLong(field, 'E', 'W'); 5832cf1b3d7SGreg Roach }; 5842cf1b3d7SGreg Roach 5852cf1b3d7SGreg Roach /** 58606fe1eb5SGreg Roach * Initialize autocomplete elements. 58706fe1eb5SGreg Roach * @param {string} selector 58806fe1eb5SGreg Roach */ 5892cf1b3d7SGreg Roach webtrees.autocomplete = function (selector) { 59071239cb6SGreg Roach // Use typeahead/bloodhound for autocomplete 59171239cb6SGreg Roach $(selector).each(function () { 592efd89170SGreg Roach const that = this; 59371239cb6SGreg Roach $(this).typeahead(null, { 59471239cb6SGreg Roach display: 'value', 59563763244SGreg Roach limit: 10, 59663763244SGreg Roach minLength: 2, 59771239cb6SGreg Roach source: new Bloodhound({ 59871239cb6SGreg Roach datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 59971239cb6SGreg Roach queryTokenizer: Bloodhound.tokenizers.whitespace, 60071239cb6SGreg Roach remote: { 601d4786c66SGreg Roach url: this.dataset.wtAutocompleteUrl, 602f4abaf12SGreg Roach replace: function (url, uriEncodedQuery) { 603f9b64f46SGreg Roach const symbol = (url.indexOf("?") > 0) ? '&' : '?'; 604d4786c66SGreg Roach if (that.dataset.wtAutocompleteExtra === 'SOUR') { 605d99ef095SGreg Roach let row_group = that.closest('.form-group').previousElementSibling; 606d99ef095SGreg Roach while (row_group.querySelector('select') === null) { 607d99ef095SGreg Roach row_group = row_group.previousElementSibling; 608d99ef095SGreg Roach } 609d99ef095SGreg Roach const element = row_group.querySelector('select'); 61082dda228SJonathan Jaubart const extra = element.options[element.selectedIndex].value.replace(/@/g, ''); 611f9b64f46SGreg Roach return url + symbol + "query=" + uriEncodedQuery + '&extra=' + encodeURIComponent(extra); 612f4abaf12SGreg Roach } 613f9b64f46SGreg Roach return url + symbol + "query=" + uriEncodedQuery 614f9b64f46SGreg Roach } 61571239cb6SGreg Roach } 61671239cb6SGreg Roach }) 61771239cb6SGreg Roach }); 61871239cb6SGreg Roach }); 6192cf1b3d7SGreg Roach }; 620c9c6f2ecSGreg Roach 621c9c6f2ecSGreg Roach /** 622c9c6f2ecSGreg Roach * Create a LeafletJS map from a list of providers/layers. 623c9c6f2ecSGreg Roach * @param {string} id 624c9c6f2ecSGreg Roach * @param {object} config 625b7b71725SGreg Roach * @param {function} resetCallback 626c9c6f2ecSGreg Roach * @returns Map 627c9c6f2ecSGreg Roach */ 628b7b71725SGreg Roach webtrees.buildLeafletJsMap = function (id, config, resetCallback) { 629c9c6f2ecSGreg Roach const zoomControl = new L.control.zoom({ 630c9c6f2ecSGreg Roach zoomInTitle: config.i18n.zoomIn, 631c9c6f2ecSGreg Roach zoomoutTitle: config.i18n.zoomOut, 632c9c6f2ecSGreg Roach }); 633c9c6f2ecSGreg Roach 634f352d954SDavid Drury const resetControl = L.Control.extend({ 635f352d954SDavid Drury options: { 636f352d954SDavid Drury position: 'topleft', 637f352d954SDavid Drury }, 638f352d954SDavid Drury onAdd: function (map) { 639f352d954SDavid Drury let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom'); 640b7b71725SGreg Roach container.onclick = resetCallback; 641f352d954SDavid Drury let reset = config.i18n.reset; 642f352d954SDavid Drury let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container); 643f352d954SDavid Drury anchor.setAttribute('aria-label', reset); 644f352d954SDavid Drury anchor.href = '#'; 645f352d954SDavid Drury anchor.title = reset; 646f352d954SDavid Drury anchor.role = 'button'; 6478f878797SDavid Drury L.DomEvent.addListener(anchor, 'click', L.DomEvent.preventDefault); 648f352d954SDavid Drury let image = L.DomUtil.create('i', 'fas fa-redo', anchor); 649f352d954SDavid Drury image.alt = reset; 650f352d954SDavid Drury 651f352d954SDavid Drury return container; 652f352d954SDavid Drury }, 653f352d954SDavid Drury }); 654f352d954SDavid Drury 655c9c6f2ecSGreg Roach let defaultLayer = null; 656c9c6f2ecSGreg Roach 657c9c6f2ecSGreg Roach for (let [, provider] of Object.entries(config.mapProviders)) { 658c9c6f2ecSGreg Roach for (let [, child] of Object.entries(provider.children)) { 659c9c6f2ecSGreg Roach if ('bingMapsKey' in child) { 660c9c6f2ecSGreg Roach child.layer = L.tileLayer.bing(child); 661c9c6f2ecSGreg Roach } else { 662c9c6f2ecSGreg Roach child.layer = L.tileLayer(child.url, child); 663c9c6f2ecSGreg Roach } 664c9c6f2ecSGreg Roach if (provider.default && child.default) { 665c9c6f2ecSGreg Roach defaultLayer = child.layer; 666c9c6f2ecSGreg Roach } 667c9c6f2ecSGreg Roach } 668c9c6f2ecSGreg Roach } 669c9c6f2ecSGreg Roach 670c9c6f2ecSGreg Roach if (defaultLayer === null) { 671c9c6f2ecSGreg Roach console.log('No default map layer defined - using the first one.'); 672c9c6f2ecSGreg Roach let defaultLayer = config.mapProviders[0].children[0].layer; 673c9c6f2ecSGreg Roach } 674c9c6f2ecSGreg Roach 675c9c6f2ecSGreg Roach 676c9c6f2ecSGreg Roach // Create the map with all controls and layers 677c9c6f2ecSGreg Roach return L.map(id, { 678c9c6f2ecSGreg Roach zoomControl: false, 679c9c6f2ecSGreg Roach }) 680c9c6f2ecSGreg Roach .addControl(zoomControl) 681f352d954SDavid Drury .addControl(new resetControl()) 682c9c6f2ecSGreg Roach .addLayer(defaultLayer) 683c9c6f2ecSGreg Roach .addControl(L.control.layers.tree(config.mapProviders, null, { 684c9c6f2ecSGreg Roach closedSymbol: config.icons.expand, 685c9c6f2ecSGreg Roach openedSymbol: config.icons.collapse, 686c9c6f2ecSGreg Roach })); 687c9c6f2ecSGreg Roach 688c9c6f2ecSGreg Roach }; 689c8d78f19SGreg Roach 690c8d78f19SGreg Roach /** 691c8d78f19SGreg Roach * Initialize a tom-select input 692c8d78f19SGreg Roach * @param {Element} element 693c8d78f19SGreg Roach * @returns {TomSelect} 694c8d78f19SGreg Roach */ 695c8d78f19SGreg Roach webtrees.initializeTomSelect = function (element) { 696c8d78f19SGreg Roach if (element.tomselect) { 697c8d78f19SGreg Roach return element.tomselect; 698c8d78f19SGreg Roach } 699c8d78f19SGreg Roach 700c8d78f19SGreg Roach let options = {}; 701c8d78f19SGreg Roach 702c8d78f19SGreg Roach if (element.dataset.url) { 703e1530f04SGreg Roach let plugins = ['dropdown_input', 'virtual_scroll']; 704e1530f04SGreg Roach 705e1530f04SGreg Roach if (element.multiple) { 706e1530f04SGreg Roach plugins.push('remove_button'); 707e1530f04SGreg Roach } else if (!element.required) { 708e1530f04SGreg Roach plugins.push('clear_button'); 709e1530f04SGreg Roach } 710e1530f04SGreg Roach 711c8d78f19SGreg Roach options = { 712e1530f04SGreg Roach plugins: plugins, 71333eb849bSGreg Roach maxOptions: false, 714c8d78f19SGreg Roach render: { 715c8d78f19SGreg Roach item: (data, escape) => '<div>' + data.text + '</div>', 716c8d78f19SGreg Roach option: (data, escape) => '<div>' + data.text + '</div>', 717c8d78f19SGreg Roach }, 718c8d78f19SGreg Roach firstUrl: query => element.dataset.url + '&query=' + encodeURIComponent(query), 719c8d78f19SGreg Roach load: function (query, callback) { 720c8d78f19SGreg Roach fetch(this.getUrl(query)) 721c8d78f19SGreg Roach .then(response => response.json()) 722c8d78f19SGreg Roach .then(json => { 723640069c3SRichard Cissée if (json.nextUrl !== null) { 724c8d78f19SGreg Roach this.setNextUrl(query, json.nextUrl + '&query=' + encodeURIComponent(query)); 725640069c3SRichard Cissée } 726c8d78f19SGreg Roach callback(json.data); 727c8d78f19SGreg Roach }) 728c8d78f19SGreg Roach .catch(callback); 729c8d78f19SGreg Roach }, 730c8d78f19SGreg Roach }; 731c8d78f19SGreg Roach } 732c8d78f19SGreg Roach 733c8d78f19SGreg Roach return new TomSelect(element, options); 734c8d78f19SGreg Roach } 735c8d78f19SGreg Roach 736c8d78f19SGreg Roach /** 737c8d78f19SGreg Roach * Reset a tom-select input to have a single selected option 738c8d78f19SGreg Roach * @param {TomSelect} tomSelect 739c8d78f19SGreg Roach * @param {string} value 740c8d78f19SGreg Roach * @param {string} text 741c8d78f19SGreg Roach */ 742c8d78f19SGreg Roach webtrees.resetTomSelect = function (tomSelect, value, text) { 743c8d78f19SGreg Roach tomSelect.clear(true); 744c8d78f19SGreg Roach tomSelect.clearOptions(); 745c8d78f19SGreg Roach tomSelect.addOption({ value: value, text: text }); 746c8d78f19SGreg Roach tomSelect.refreshOptions(); 747c8d78f19SGreg Roach tomSelect.addItem(value, true); 748c8d78f19SGreg Roach tomSelect.refreshItems(); 749c8d78f19SGreg Roach }; 750c8d78f19SGreg Roach 751c8d78f19SGreg Roach /** 752c8d78f19SGreg Roach * Toggle the visibility/status of INDI/FAM/SOUR/REPO/OBJE selectors 753c8d78f19SGreg Roach * 754c8d78f19SGreg Roach * @param {Element} select 755c8d78f19SGreg Roach * @param {Element} container 756c8d78f19SGreg Roach */ 757c8d78f19SGreg Roach webtrees.initializeIFSRO = function(select, container) { 758c8d78f19SGreg Roach select.addEventListener('change', function () { 759c8d78f19SGreg Roach // Show only the selected selector. 760c8d78f19SGreg Roach console.log(select.value); 761c8d78f19SGreg Roach container.querySelectorAll('.select-record').forEach(element => element.classList.add('d-none')); 762c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value).forEach(element => element.classList.remove('d-none')); 763c8d78f19SGreg Roach // Enable only the selected selector (so that disabled ones do not get submitted). 764c8d78f19SGreg Roach container.querySelectorAll('.select-record select').forEach(element => { 765c8d78f19SGreg Roach element.disabled = true; 766c8d78f19SGreg Roach element.tomselect.disable(); 767c8d78f19SGreg Roach }); 768c8d78f19SGreg Roach container.querySelectorAll('.select-' + select.value + ' select').forEach(element => { 769c8d78f19SGreg Roach element.disabled = false; 770c8d78f19SGreg Roach element.tomselect.enable(); 771c8d78f19SGreg Roach }); 772c8d78f19SGreg Roach }); 773c8d78f19SGreg Roach } 7742cf1b3d7SGreg Roach}(window.webtrees = window.webtrees || {})); 77571239cb6SGreg Roach 77671239cb6SGreg Roach// Send the CSRF token on all AJAX requests 77771239cb6SGreg Roach$.ajaxSetup({ 77871239cb6SGreg Roach headers: { 77971239cb6SGreg Roach 'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content') 78071239cb6SGreg Roach } 78171239cb6SGreg Roach}); 78271239cb6SGreg Roach 7834ed7dff1SGreg Roach/** 7844ed7dff1SGreg Roach * Initialisation 7854ed7dff1SGreg Roach */ 7864ed7dff1SGreg Roach$(function () { 7872cf1b3d7SGreg Roach // Page elements that load automatically via AJAX. 78871239cb6SGreg Roach // This prevents bad robots from crawling resource-intensive pages. 789d4786c66SGreg Roach $('[data-wt-ajax-url]').each(function () { 790d4786c66SGreg Roach $(this).load(this.dataset.wtAjaxUrl); 79171239cb6SGreg Roach }); 79271239cb6SGreg Roach 79371239cb6SGreg Roach // Autocomplete 794d4786c66SGreg Roach webtrees.autocomplete('input[data-wt-autocomplete-url]'); 79571239cb6SGreg Roach 796c8d78f19SGreg Roach document.querySelectorAll('.tom-select').forEach(element => webtrees.initializeTomSelect(element)); 797896a5721SGreg Roach 798896a5721SGreg Roach // If we clear the select (using the "X" button), we need an empty value 799896a5721SGreg Roach // (rather than no value at all) for (non-multiple) selects with name="array[]" 800c8d78f19SGreg Roach document.querySelectorAll('select.tom-select:not([multiple])') 801c8d78f19SGreg Roach .forEach(function (element) { 802c8d78f19SGreg Roach element.addEventListener('clear', function () { 803c8d78f19SGreg Roach webtrees.resetTomSelect(element.tomselect, '', ''); 804c8d78f19SGreg Roach }); 805bdbdb10cSGreg Roach }); 80671239cb6SGreg Roach 80771239cb6SGreg Roach // Datatables - locale aware sorting 80871239cb6SGreg Roach $.fn.dataTableExt.oSort['text-asc'] = function (x, y) { 809efd89170SGreg Roach return x.localeCompare(y, document.documentElement.lang, { sensitivity: 'base' }); 81071239cb6SGreg Roach }; 81171239cb6SGreg Roach $.fn.dataTableExt.oSort['text-desc'] = function (x, y) { 812efd89170SGreg Roach return y.localeCompare(x, document.documentElement.lang, { sensitivity: 'base' }); 81371239cb6SGreg Roach }; 81471239cb6SGreg Roach 81571239cb6SGreg Roach // DataTables - start hidden to prevent FOUC. 81671239cb6SGreg Roach $('table.datatables').each(function () { 8174843b94fSGreg Roach $(this).DataTable(); 8184843b94fSGreg Roach $(this).removeClass('d-none'); 8194843b94fSGreg Roach }); 8204843b94fSGreg Roach 8212d8276baSGreg Roach // Save button/checkbox state between pages 8222d8276baSGreg Roach document.querySelectorAll('[data-wt-persist]') 8232d8276baSGreg Roach .forEach((element) => webtrees.persistentToggle(element)); 82471239cb6SGreg Roach 82571239cb6SGreg Roach // Activate the on-screen keyboard 8262cf1b3d7SGreg Roach let osk_focus_element; 82771239cb6SGreg Roach $('.wt-osk-trigger').click(function () { 82871239cb6SGreg Roach // When a user clicks the icon, set focus to the corresponding input 829d4786c66SGreg Roach osk_focus_element = document.getElementById(this.dataset.wtId); 83071239cb6SGreg Roach osk_focus_element.focus(); 83171239cb6SGreg Roach $('.wt-osk').show(); 83271239cb6SGreg Roach }); 83371239cb6SGreg Roach $('.wt-osk-script-button').change(function () { 83471239cb6SGreg Roach $('.wt-osk-script').prop('hidden', true); 835d4786c66SGreg Roach $('.wt-osk-script-' + this.dataset.wtOskScript).prop('hidden', false); 83671239cb6SGreg Roach }); 83771239cb6SGreg Roach $('.wt-osk-shift-button').click(function () { 83871239cb6SGreg Roach document.querySelector('.wt-osk-keys').classList.toggle('shifted'); 83971239cb6SGreg Roach }); 84071239cb6SGreg Roach $('.wt-osk-keys').on('click', '.wt-osk-key', function () { 8412cf1b3d7SGreg Roach let key = $(this).contents().get(0).nodeValue; 8422cf1b3d7SGreg Roach let shift_state = $('.wt-osk-shift-button').hasClass('active'); 8432cf1b3d7SGreg Roach let shift_key = $('sup', this)[0]; 84471239cb6SGreg Roach if (shift_state && shift_key !== undefined) { 84571239cb6SGreg Roach key = shift_key.innerText; 84671239cb6SGreg Roach } 8470d2905f7SGreg Roach webtrees.pasteAtCursor(osk_focus_element, key); 84871239cb6SGreg Roach if ($('.wt-osk-pin-button').hasClass('active') === false) { 84971239cb6SGreg Roach $('.wt-osk').hide(); 85071239cb6SGreg Roach } 851ee51991cSGreg Roach osk_focus_element.dispatchEvent(new Event('input')); 85271239cb6SGreg Roach }); 85371239cb6SGreg Roach 85471239cb6SGreg Roach $('.wt-osk-close').on('click', function () { 85571239cb6SGreg Roach $('.wt-osk').hide(); 85671239cb6SGreg Roach }); 857b52a415dSGreg Roach 858b52a415dSGreg Roach // Hide/Show password fields 859b52a415dSGreg Roach $('input[type=password]').each(function () { 860b52a415dSGreg Roach $(this).hideShowPassword('infer', true, { 861b52a415dSGreg Roach states: { 862b52a415dSGreg Roach shown: { 863b52a415dSGreg Roach toggle: { 864d4786c66SGreg Roach content: this.dataset.wtHidePasswordText, 865b52a415dSGreg Roach attr: { 866d4786c66SGreg Roach title: this.dataset.wtHidePasswordTitle, 867d4786c66SGreg Roach 'aria-label': this.dataset.wtHidePasswordTitle, 868b52a415dSGreg Roach } 869b52a415dSGreg Roach } 870b52a415dSGreg Roach }, 871b52a415dSGreg Roach hidden: { 872b52a415dSGreg Roach toggle: { 873d4786c66SGreg Roach content: this.dataset.wtShowPasswordText, 874b52a415dSGreg Roach attr: { 875d4786c66SGreg Roach title: this.dataset.wtShowPasswordTitle, 876d4786c66SGreg Roach 'aria-label': this.dataset.wtShowPasswordTitle, 877b52a415dSGreg Roach } 878b52a415dSGreg Roach } 879b52a415dSGreg Roach } 880b52a415dSGreg Roach } 881b52a415dSGreg Roach }); 882b52a415dSGreg Roach }); 88371239cb6SGreg Roach}); 8847adfb8e5SGreg Roach 885d6edd2ebSGreg Roach// Prevent form re-submission via accidental double-click. 886d6edd2ebSGreg Roachdocument.addEventListener('submit', function (event) { 887d6edd2ebSGreg Roach const form = event.target; 888d6edd2ebSGreg Roach 889d6edd2ebSGreg Roach if (form.reportValidity()) { 890d6edd2ebSGreg Roach form.addEventListener('submit', (event) => { 891d6edd2ebSGreg Roach if (form.classList.contains('form-is-submitting')) { 892d6edd2ebSGreg Roach event.preventDefault(); 893d6edd2ebSGreg Roach } 894d6edd2ebSGreg Roach 895d6edd2ebSGreg Roach form.classList.add('form-is-submitting'); 896d6edd2ebSGreg Roach }); 897d6edd2ebSGreg Roach } 898d6edd2ebSGreg Roach}); 899d6edd2ebSGreg Roach 900d4786c66SGreg Roach// Convert data-wt-confirm and data-wt-post-url/data-wt-reload-url attributes into useful behavior. 901efd89170SGreg Roachdocument.addEventListener('click', (event) => { 902a7a3d6dbSGreg Roach const target = event.target.closest('a,button'); 9037adfb8e5SGreg Roach 9047adfb8e5SGreg Roach if (target === null) { 9057adfb8e5SGreg Roach return; 9067adfb8e5SGreg Roach } 9077adfb8e5SGreg Roach 908d4786c66SGreg Roach if ('wtConfirm' in target.dataset && !confirm(target.dataset.wtConfirm)) { 9097adfb8e5SGreg Roach event.preventDefault(); 910*c433703eSGreg Roach return; 9117adfb8e5SGreg Roach } 9127adfb8e5SGreg Roach 913d4786c66SGreg Roach if ('wtPostUrl' in target.dataset) { 914efd89170SGreg Roach const token = document.querySelector('meta[name=csrf]').content; 915ea101122SGreg Roach 916d4786c66SGreg Roach fetch(target.dataset.wtPostUrl, { 917ea101122SGreg Roach method: 'POST', 918ea101122SGreg Roach headers: { 919ea101122SGreg Roach 'X-CSRF-TOKEN': token, 920ea101122SGreg Roach 'X-Requested-with': 'XMLHttpRequest', 921ea101122SGreg Roach }, 9222cf1b3d7SGreg Roach }).then(() => { 923d4786c66SGreg Roach if ('wtReloadUrl' in target.dataset) { 924d4786c66SGreg Roach // Go somewhere else. e.g. the home page after logout. 925d4786c66SGreg Roach document.location = target.dataset.wtReloadUrl; 926ea101122SGreg Roach } else { 927ea101122SGreg Roach // Reload the current page. e.g. change language. 9287adfb8e5SGreg Roach document.location.reload(); 9297adfb8e5SGreg Roach } 930cb8f307bSGreg Roach }).catch((error) => { 931ea101122SGreg Roach alert(error); 932ea101122SGreg Roach }); 9337adfb8e5SGreg Roach } 9347adfb8e5SGreg Roach}); 935