171239cb6SGreg Roach/** 271239cb6SGreg Roach * webtrees: online genealogy 3063107dbSGreg Roach * Copyright (C) 2020 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 9759e18f0cSGreg Roach const surname = trim(spfx + separator + surn); 9859e18f0cSGreg Roach 9959e18f0cSGreg Roach const name = surnameFirst ? slash + surname + slash + separator + givn : givn + separator + slash + surname + slash; 10059e18f0cSGreg Roach 10159e18f0cSGreg Roach return trim(npfx + separator + name + separator + nsfx); 1020d2905f7SGreg Roach }; 1030d2905f7SGreg Roach 1040d2905f7SGreg Roach // Insert text at the current cursor position in a text field. 1050d2905f7SGreg Roach webtrees.pasteAtCursor = function (element, text) { 1060d2905f7SGreg Roach if (element !== null) { 1070d2905f7SGreg Roach const caret_pos = element.selectionStart + text.length; 1080d2905f7SGreg Roach const textBefore = element.value.substring(0, element.selectionStart); 1090d2905f7SGreg Roach const textAfter = element.value.substring(element.selectionEnd); 1100d2905f7SGreg Roach element.value = textBefore + text + textAfter; 1110d2905f7SGreg Roach element.setSelectionRange(caret_pos, caret_pos); 1120d2905f7SGreg Roach element.focus(); 11359e18f0cSGreg Roach } 114efd89170SGreg Roach }; 1150d2905f7SGreg Roach}(window.webtrees = window.webtrees || {})); 11659e18f0cSGreg Roach 11771239cb6SGreg Roachvar pastefield; 11871239cb6SGreg Roach 11906fe1eb5SGreg Roach/** 120*7fb78f8aSGreg Roach * @param {Element} datefield 12106fe1eb5SGreg Roach * @param {string} dmy 12206fe1eb5SGreg Roach */ 123efd89170SGreg Roachfunction valid_date (datefield, dmy) { 12471239cb6SGreg Roach var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']; 12571239cb6SGreg Roach var hijri_months = ['MUHAR', 'SAFAR', 'RABIA', 'RABIT', 'JUMAA', 'JUMAT', 'RAJAB', 'SHAAB', 'RAMAD', 'SHAWW', 'DHUAQ', 'DHUAH']; 12671239cb6SGreg Roach var hebrew_months = ['TSH', 'CSH', 'KSL', 'TVT', 'SHV', 'ADR', 'ADS', 'NSN', 'IYR', 'SVN', 'TMZ', 'AAV', 'ELL']; 12771239cb6SGreg Roach var french_months = ['VEND', 'BRUM', 'FRIM', 'NIVO', 'PLUV', 'VENT', 'GERM', 'FLOR', 'PRAI', 'MESS', 'THER', 'FRUC', 'COMP']; 12871239cb6SGreg Roach var jalali_months = ['FARVA', 'ORDIB', 'KHORD', 'TIR', 'MORDA', 'SHAHR', 'MEHR', 'ABAN', 'AZAR', 'DEY', 'BAHMA', 'ESFAN']; 12971239cb6SGreg Roach 13071239cb6SGreg Roach var datestr = datefield.value; 13171239cb6SGreg Roach // if a date has a date phrase marked by () this has to be excluded from altering 13271239cb6SGreg Roach var datearr = datestr.split('('); 13371239cb6SGreg Roach var datephrase = ''; 13471239cb6SGreg Roach if (datearr.length > 1) { 13571239cb6SGreg Roach datestr = datearr[0]; 13671239cb6SGreg Roach datephrase = datearr[1]; 13771239cb6SGreg Roach } 13871239cb6SGreg Roach 13971239cb6SGreg Roach // Gedcom dates are upper case 14071239cb6SGreg Roach datestr = datestr.toUpperCase(); 14171239cb6SGreg Roach // Gedcom dates have no leading/trailing/repeated whitespace 14280d699d6SGreg Roach datestr = datestr.replace(/\s+/g, ' '); 14371239cb6SGreg Roach datestr = datestr.replace(/(^\s)|(\s$)/, ''); 14471239cb6SGreg Roach // Gedcom dates have spaces between letters and digits, e.g. "01JAN2000" => "01 JAN 2000" 14580d699d6SGreg Roach datestr = datestr.replace(/(\d)([A-Z])/g, '$1 $2'); 14680d699d6SGreg Roach datestr = datestr.replace(/([A-Z])(\d)/g, '$1 $2'); 14771239cb6SGreg Roach 14871239cb6SGreg Roach // Shortcut for quarter format, "Q1 1900" => "BET JAN 1900 AND MAR 1900". See [ 1509083 ] 14971239cb6SGreg Roach if (datestr.match(/^Q ([1-4]) (\d\d\d\d)$/)) { 15071239cb6SGreg Roach datestr = 'BET ' + months[RegExp.$1 * 3 - 3] + ' ' + RegExp.$2 + ' AND ' + months[RegExp.$1 * 3 - 1] + ' ' + RegExp.$2; 15171239cb6SGreg Roach } 15271239cb6SGreg Roach 15371239cb6SGreg Roach // Shortcut for @#Dxxxxx@ 01 01 1400, etc. 15471239cb6SGreg Roach if (datestr.match(/^(@#DHIJRI@|HIJRI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 15571239cb6SGreg Roach datestr = '@#DHIJRI@' + RegExp.$2 + hijri_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 15671239cb6SGreg Roach } 15771239cb6SGreg Roach if (datestr.match(/^(@#DJALALI@|JALALI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 15871239cb6SGreg Roach datestr = '@#DJALALI@' + RegExp.$2 + jalali_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 15971239cb6SGreg Roach } 16071239cb6SGreg Roach if (datestr.match(/^(@#DHEBREW@|HEBREW)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 16171239cb6SGreg Roach datestr = '@#DHEBREW@' + RegExp.$2 + hebrew_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 16271239cb6SGreg Roach } 16371239cb6SGreg Roach if (datestr.match(/^(@#DFRENCH R@|FRENCH)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 16471239cb6SGreg Roach datestr = '@#DFRENCH R@' + RegExp.$2 + french_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 16571239cb6SGreg Roach } 16671239cb6SGreg Roach 167*7fb78f8aSGreg Roach // All digit dates 168*7fb78f8aSGreg Roach datestr = datestr.replaceAll(/(\d\d)(\d\d)(\d\d)(\d\d)/g, function () { 169*7fb78f8aSGreg Roach if (RegExp.$1 > '12' && RegExp.$3 <= '12' && RegExp.$4 <= '31') { 170*7fb78f8aSGreg Roach return RegExp.$4 + ' ' + months[RegExp.$3 - 1] + ' ' + RegExp.$1 + RegExp.$2; 171*7fb78f8aSGreg Roach } 172*7fb78f8aSGreg Roach if (RegExp.$1 <= '31' && RegExp.$2 <= '12' && RegExp.$3 > '12') { 173*7fb78f8aSGreg Roach return RegExp.$1 + ' ' + months[RegExp.$2 - 1] + ' ' + RegExp.$3 + RegExp.$4; 174*7fb78f8aSGreg Roach } 175*7fb78f8aSGreg Roach return RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4; 176*7fb78f8aSGreg Roach }); 177*7fb78f8aSGreg Roach 178*7fb78f8aSGreg Roach // e.g. 17.11.1860, 3/4/2005 or 1999-12-31. Use locale settings since DMY order is ambiguous. 179*7fb78f8aSGreg Roach datestr = datestr.replaceAll(/(\d+)([./-])(\d+)([./-])(\d+)/g, function () { 180*7fb78f8aSGreg Roach var f1 = parseInt(RegExp.$1, 10); 18171239cb6SGreg Roach var f2 = parseInt(RegExp.$3, 10); 182*7fb78f8aSGreg Roach var f3 = parseInt(RegExp.$5, 10); 18371239cb6SGreg Roach var yyyy = new Date().getFullYear(); 18471239cb6SGreg Roach var yy = yyyy % 100; 18571239cb6SGreg Roach var cc = yyyy - yy; 18671239cb6SGreg Roach if (dmy === 'DMY' && f1 <= 31 && f2 <= 12 || f1 > 13 && f1 <= 31 && f2 <= 12 && f3 > 31) { 187*7fb78f8aSGreg Roach return f1 + ' ' + months[f2 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 188*7fb78f8aSGreg Roach } 18971239cb6SGreg Roach if (dmy === 'MDY' && f1 <= 12 && f2 <= 31 || f2 > 13 && f2 <= 31 && f1 <= 12 && f3 > 31) { 190*7fb78f8aSGreg Roach return f2 + ' ' + months[f1 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 191*7fb78f8aSGreg Roach } 19271239cb6SGreg Roach if (dmy === 'YMD' && f2 <= 12 && f3 <= 31 || f3 > 13 && f3 <= 31 && f2 <= 12 && f1 > 31) { 193*7fb78f8aSGreg Roach return f3 + ' ' + months[f2 - 1] + ' ' + (f1 >= 100 ? f1 : (f1 <= yy ? f1 + cc : f1 + cc - 100)); 19471239cb6SGreg Roach } 195*7fb78f8aSGreg Roach return RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4 + RegExp.$5; 196*7fb78f8aSGreg Roach }); 19771239cb6SGreg Roach 19871239cb6SGreg Roach // Shortcuts for date ranges 19971239cb6SGreg Roach datestr = datestr.replace(/^[>]([\w ]+)$/, 'AFT $1'); 20071239cb6SGreg Roach datestr = datestr.replace(/^[<]([\w ]+)$/, 'BEF $1'); 20171239cb6SGreg Roach datestr = datestr.replace(/^([\w ]+)[-]$/, 'FROM $1'); 20271239cb6SGreg Roach datestr = datestr.replace(/^[-]([\w ]+)$/, 'TO $1'); 20371239cb6SGreg Roach datestr = datestr.replace(/^[~]([\w ]+)$/, 'ABT $1'); 20471239cb6SGreg Roach datestr = datestr.replace(/^[*]([\w ]+)$/, 'EST $1'); 20571239cb6SGreg Roach datestr = datestr.replace(/^[#]([\w ]+)$/, 'CAL $1'); 20671239cb6SGreg Roach datestr = datestr.replace(/^([\w ]+) ?- ?([\w ]+)$/, 'BET $1 AND $2'); 20771239cb6SGreg Roach datestr = datestr.replace(/^([\w ]+) ?~ ?([\w ]+)$/, 'FROM $1 TO $2'); 20871239cb6SGreg Roach 20971239cb6SGreg Roach // Convert full months to short months 21071239cb6SGreg Roach datestr = datestr.replace(/(JANUARY)/, 'JAN'); 21171239cb6SGreg Roach datestr = datestr.replace(/(FEBRUARY)/, 'FEB'); 21271239cb6SGreg Roach datestr = datestr.replace(/(MARCH)/, 'MAR'); 21371239cb6SGreg Roach datestr = datestr.replace(/(APRIL)/, 'APR'); 21471239cb6SGreg Roach datestr = datestr.replace(/(MAY)/, 'MAY'); 21571239cb6SGreg Roach datestr = datestr.replace(/(JUNE)/, 'JUN'); 21671239cb6SGreg Roach datestr = datestr.replace(/(JULY)/, 'JUL'); 21771239cb6SGreg Roach datestr = datestr.replace(/(AUGUST)/, 'AUG'); 21871239cb6SGreg Roach datestr = datestr.replace(/(SEPTEMBER)/, 'SEP'); 21971239cb6SGreg Roach datestr = datestr.replace(/(OCTOBER)/, 'OCT'); 22071239cb6SGreg Roach datestr = datestr.replace(/(NOVEMBER)/, 'NOV'); 22171239cb6SGreg Roach datestr = datestr.replace(/(DECEMBER)/, 'DEC'); 22271239cb6SGreg Roach 22371239cb6SGreg Roach // Americans frequently enter dates as SEP 20, 1999 22471239cb6SGreg Roach // No need to internationalise this, as this is an english-language issue 22571239cb6SGreg Roach datestr = datestr.replace(/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\.? (\d\d?)[, ]+(\d\d\d\d)/, '$2 $1 $3'); 22671239cb6SGreg Roach 22771239cb6SGreg Roach // Apply leading zero to day numbers 22871239cb6SGreg Roach datestr = datestr.replace(/(^| )(\d [A-Z]{3,5} \d{4})/, '$10$2'); 22971239cb6SGreg Roach 23071239cb6SGreg Roach if (datephrase) { 23171239cb6SGreg Roach datestr = datestr + ' (' + datephrase; 23271239cb6SGreg Roach } 23371239cb6SGreg Roach // Only update it if is has been corrected - otherwise input focus 23471239cb6SGreg Roach // moves to the end of the field unnecessarily 23571239cb6SGreg Roach if (datefield.value !== datestr) { 23671239cb6SGreg Roach datefield.value = datestr; 23771239cb6SGreg Roach } 23871239cb6SGreg Roach} 23971239cb6SGreg Roach 24071239cb6SGreg Roachvar monthLabels = []; 24171239cb6SGreg RoachmonthLabels[1] = 'January'; 24271239cb6SGreg RoachmonthLabels[2] = 'February'; 24371239cb6SGreg RoachmonthLabels[3] = 'March'; 24471239cb6SGreg RoachmonthLabels[4] = 'April'; 24571239cb6SGreg RoachmonthLabels[5] = 'May'; 24671239cb6SGreg RoachmonthLabels[6] = 'June'; 24771239cb6SGreg RoachmonthLabels[7] = 'July'; 24871239cb6SGreg RoachmonthLabels[8] = 'August'; 24971239cb6SGreg RoachmonthLabels[9] = 'September'; 25071239cb6SGreg RoachmonthLabels[10] = 'October'; 25171239cb6SGreg RoachmonthLabels[11] = 'November'; 25271239cb6SGreg RoachmonthLabels[12] = 'December'; 25371239cb6SGreg Roach 25471239cb6SGreg Roachvar monthShort = []; 25571239cb6SGreg RoachmonthShort[1] = 'JAN'; 25671239cb6SGreg RoachmonthShort[2] = 'FEB'; 25771239cb6SGreg RoachmonthShort[3] = 'MAR'; 25871239cb6SGreg RoachmonthShort[4] = 'APR'; 25971239cb6SGreg RoachmonthShort[5] = 'MAY'; 26071239cb6SGreg RoachmonthShort[6] = 'JUN'; 26171239cb6SGreg RoachmonthShort[7] = 'JUL'; 26271239cb6SGreg RoachmonthShort[8] = 'AUG'; 26371239cb6SGreg RoachmonthShort[9] = 'SEP'; 26471239cb6SGreg RoachmonthShort[10] = 'OCT'; 26571239cb6SGreg RoachmonthShort[11] = 'NOV'; 26671239cb6SGreg RoachmonthShort[12] = 'DEC'; 26771239cb6SGreg Roach 26871239cb6SGreg Roachvar daysOfWeek = []; 26971239cb6SGreg RoachdaysOfWeek[0] = 'S'; 27071239cb6SGreg RoachdaysOfWeek[1] = 'M'; 27171239cb6SGreg RoachdaysOfWeek[2] = 'T'; 27271239cb6SGreg RoachdaysOfWeek[3] = 'W'; 27371239cb6SGreg RoachdaysOfWeek[4] = 'T'; 27471239cb6SGreg RoachdaysOfWeek[5] = 'F'; 27571239cb6SGreg RoachdaysOfWeek[6] = 'S'; 27671239cb6SGreg Roach 27771239cb6SGreg Roachvar weekStart = 0; 27871239cb6SGreg Roach 27906fe1eb5SGreg Roach/** 28006fe1eb5SGreg Roach * @param {string} jan 28106fe1eb5SGreg Roach * @param {string} feb 28206fe1eb5SGreg Roach * @param {string} mar 28306fe1eb5SGreg Roach * @param {string} apr 28406fe1eb5SGreg Roach * @param {string} may 28506fe1eb5SGreg Roach * @param {string} jun 28606fe1eb5SGreg Roach * @param {string} jul 28706fe1eb5SGreg Roach * @param {string} aug 28806fe1eb5SGreg Roach * @param {string} sep 28906fe1eb5SGreg Roach * @param {string} oct 29006fe1eb5SGreg Roach * @param {string} nov 29106fe1eb5SGreg Roach * @param {string} dec 29206fe1eb5SGreg Roach */ 293efd89170SGreg Roachfunction cal_setMonthNames (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec) { 29471239cb6SGreg Roach monthLabels[1] = jan; 29571239cb6SGreg Roach monthLabels[2] = feb; 29671239cb6SGreg Roach monthLabels[3] = mar; 29771239cb6SGreg Roach monthLabels[4] = apr; 29871239cb6SGreg Roach monthLabels[5] = may; 29971239cb6SGreg Roach monthLabels[6] = jun; 30071239cb6SGreg Roach monthLabels[7] = jul; 30171239cb6SGreg Roach monthLabels[8] = aug; 30271239cb6SGreg Roach monthLabels[9] = sep; 30371239cb6SGreg Roach monthLabels[10] = oct; 30471239cb6SGreg Roach monthLabels[11] = nov; 30571239cb6SGreg Roach monthLabels[12] = dec; 30671239cb6SGreg Roach} 30771239cb6SGreg Roach 30806fe1eb5SGreg Roach/** 30906fe1eb5SGreg Roach * @param {string} sun 31006fe1eb5SGreg Roach * @param {string} mon 31106fe1eb5SGreg Roach * @param {string} tue 31206fe1eb5SGreg Roach * @param {string} wed 31306fe1eb5SGreg Roach * @param {string} thu 31406fe1eb5SGreg Roach * @param {string} fri 31506fe1eb5SGreg Roach * @param {string} sat 31606fe1eb5SGreg Roach */ 317efd89170SGreg Roachfunction cal_setDayHeaders (sun, mon, tue, wed, thu, fri, sat) { 31871239cb6SGreg Roach daysOfWeek[0] = sun; 31971239cb6SGreg Roach daysOfWeek[1] = mon; 32071239cb6SGreg Roach daysOfWeek[2] = tue; 32171239cb6SGreg Roach daysOfWeek[3] = wed; 32271239cb6SGreg Roach daysOfWeek[4] = thu; 32371239cb6SGreg Roach daysOfWeek[5] = fri; 32471239cb6SGreg Roach daysOfWeek[6] = sat; 32571239cb6SGreg Roach} 32671239cb6SGreg Roach 32706fe1eb5SGreg Roach/** 32806fe1eb5SGreg Roach * @param {number} day 32906fe1eb5SGreg Roach */ 330efd89170SGreg Roachfunction cal_setWeekStart (day) { 33171239cb6SGreg Roach if (day >= 0 && day < 7) { 33271239cb6SGreg Roach weekStart = day; 33371239cb6SGreg Roach } 33471239cb6SGreg Roach} 33571239cb6SGreg Roach 33606fe1eb5SGreg Roach/** 33706fe1eb5SGreg Roach * @param {string} dateDivId 33806fe1eb5SGreg Roach * @param {string} dateFieldId 33906fe1eb5SGreg Roach * @returns {boolean} 34006fe1eb5SGreg Roach */ 341efd89170SGreg Roachfunction calendarWidget (dateDivId, dateFieldId) { 34271239cb6SGreg Roach var dateDiv = document.getElementById(dateDivId); 34371239cb6SGreg Roach var dateField = document.getElementById(dateFieldId); 34471239cb6SGreg Roach 34571239cb6SGreg Roach if (dateDiv.style.visibility === 'visible') { 34671239cb6SGreg Roach dateDiv.style.visibility = 'hidden'; 34771239cb6SGreg Roach return false; 34871239cb6SGreg Roach } 34971239cb6SGreg Roach if (dateDiv.style.visibility === 'show') { 35071239cb6SGreg Roach dateDiv.style.visibility = 'hide'; 35171239cb6SGreg Roach return false; 35271239cb6SGreg Roach } 35371239cb6SGreg Roach 35471239cb6SGreg Roach /* Javascript calendar functions only work with precise gregorian dates "D M Y" or "Y" */ 35571239cb6SGreg Roach var greg_regex = /((\d+ (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) )?\d+)/i; 35671239cb6SGreg Roach var date; 35771239cb6SGreg Roach if (greg_regex.exec(dateField.value)) { 35871239cb6SGreg Roach date = new Date(RegExp.$1); 35971239cb6SGreg Roach } else { 36071239cb6SGreg Roach date = new Date(); 36171239cb6SGreg Roach } 36271239cb6SGreg Roach 36371239cb6SGreg Roach dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date); 36471239cb6SGreg Roach if (dateDiv.style.visibility === 'hidden') { 36571239cb6SGreg Roach dateDiv.style.visibility = 'visible'; 36671239cb6SGreg Roach return false; 36771239cb6SGreg Roach } 36871239cb6SGreg Roach if (dateDiv.style.visibility === 'hide') { 36971239cb6SGreg Roach dateDiv.style.visibility = 'show'; 37071239cb6SGreg Roach return false; 37171239cb6SGreg Roach } 37271239cb6SGreg Roach 37371239cb6SGreg Roach return false; 37471239cb6SGreg Roach} 37571239cb6SGreg Roach 37606fe1eb5SGreg Roach/** 37706fe1eb5SGreg Roach * @param {string} dateFieldId 37806fe1eb5SGreg Roach * @param {string} dateDivId 37906fe1eb5SGreg Roach * @param {Date} date 38006fe1eb5SGreg Roach * @returns {string} 38106fe1eb5SGreg Roach */ 382efd89170SGreg Roachfunction cal_generateSelectorContent (dateFieldId, dateDivId, date) { 38371239cb6SGreg Roach var i, j; 38471239cb6SGreg Roach var content = '<table border="1"><tr>'; 38571239cb6SGreg Roach content += '<td><select class="form-control" id="' + dateFieldId + '_daySelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 38671239cb6SGreg Roach for (i = 1; i < 32; i++) { 38771239cb6SGreg Roach content += '<option value="' + i + '"'; 38871239cb6SGreg Roach if (date.getDate() === i) { 38971239cb6SGreg Roach content += ' selected="selected"'; 39071239cb6SGreg Roach } 39171239cb6SGreg Roach content += '>' + i + '</option>'; 39271239cb6SGreg Roach } 39371239cb6SGreg Roach content += '</select></td>'; 39471239cb6SGreg Roach content += '<td><select class="form-control" id="' + dateFieldId + '_monSelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 39571239cb6SGreg Roach for (i = 1; i < 13; i++) { 39671239cb6SGreg Roach content += '<option value="' + i + '"'; 39771239cb6SGreg Roach if (date.getMonth() + 1 === i) { 39871239cb6SGreg Roach content += ' selected="selected"'; 39971239cb6SGreg Roach } 40071239cb6SGreg Roach content += '>' + monthLabels[i] + '</option>'; 40171239cb6SGreg Roach } 40271239cb6SGreg Roach content += '</select></td>'; 40371239cb6SGreg Roach content += '<td><input class="form-control" type="text" id="' + dateFieldId + '_yearInput" size="5" value="' + date.getFullYear() + '" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');" /></td></tr>'; 40471239cb6SGreg Roach content += '<tr><td colspan="3">'; 40571239cb6SGreg Roach content += '<table width="100%">'; 40671239cb6SGreg Roach content += '<tr>'; 40771239cb6SGreg Roach j = weekStart; 40871239cb6SGreg Roach for (i = 0; i < 7; i++) { 40971239cb6SGreg Roach content += '<td '; 41071239cb6SGreg Roach content += 'class="descriptionbox"'; 41171239cb6SGreg Roach content += '>'; 41271239cb6SGreg Roach content += daysOfWeek[j]; 41371239cb6SGreg Roach content += '</td>'; 41471239cb6SGreg Roach j++; 41571239cb6SGreg Roach if (j > 6) { 41671239cb6SGreg Roach j = 0; 41771239cb6SGreg Roach } 41871239cb6SGreg Roach } 41971239cb6SGreg Roach content += '</tr>'; 42071239cb6SGreg Roach 42171239cb6SGreg Roach var tdate = new Date(date.getFullYear(), date.getMonth(), 1); 42271239cb6SGreg Roach var day = tdate.getDay(); 42371239cb6SGreg Roach day = day - weekStart; 42471239cb6SGreg Roach var daymilli = 1000 * 60 * 60 * 24; 42571239cb6SGreg Roach tdate = tdate.getTime() - (day * daymilli) + (daymilli / 2); 42671239cb6SGreg Roach tdate = new Date(tdate); 42771239cb6SGreg Roach 42871239cb6SGreg Roach for (j = 0; j < 6; j++) { 42971239cb6SGreg Roach content += '<tr>'; 43071239cb6SGreg Roach for (i = 0; i < 7; i++) { 43171239cb6SGreg Roach content += '<td '; 43271239cb6SGreg Roach if (tdate.getMonth() === date.getMonth()) { 43371239cb6SGreg Roach if (tdate.getDate() === date.getDate()) { 43471239cb6SGreg Roach content += 'class="descriptionbox"'; 43571239cb6SGreg Roach } else { 43671239cb6SGreg Roach content += 'class="optionbox"'; 43771239cb6SGreg Roach } 43871239cb6SGreg Roach } else { 43971239cb6SGreg Roach content += 'style="background-color:#EAEAEA; border: solid #AAAAAA 1px;"'; 44071239cb6SGreg Roach } 44171239cb6SGreg Roach content += '><a href="#" onclick="return cal_dateClicked(\'' + dateFieldId + '\', \'' + dateDivId + '\', ' + tdate.getFullYear() + ', ' + tdate.getMonth() + ', ' + tdate.getDate() + ');">'; 44271239cb6SGreg Roach content += tdate.getDate(); 44371239cb6SGreg Roach content += '</a></td>'; 44471239cb6SGreg Roach var datemilli = tdate.getTime() + daymilli; 44571239cb6SGreg Roach tdate = new Date(datemilli); 44671239cb6SGreg Roach } 44771239cb6SGreg Roach content += '</tr>'; 44871239cb6SGreg Roach } 44971239cb6SGreg Roach content += '</table>'; 45071239cb6SGreg Roach content += '</td></tr>'; 45171239cb6SGreg Roach content += '</table>'; 45271239cb6SGreg Roach 45371239cb6SGreg Roach return content; 45471239cb6SGreg Roach} 45571239cb6SGreg Roach 45606fe1eb5SGreg Roach/** 45706fe1eb5SGreg Roach * @param {string} dateFieldId 45806fe1eb5SGreg Roach * @param {number} year 45906fe1eb5SGreg Roach * @param {number} month 46006fe1eb5SGreg Roach * @param {number} day 46106fe1eb5SGreg Roach * @returns {boolean} 46206fe1eb5SGreg Roach */ 463efd89170SGreg Roachfunction cal_setDateField (dateFieldId, year, month, day) { 46471239cb6SGreg Roach var dateField = document.getElementById(dateFieldId); 46571239cb6SGreg Roach if (!dateField) { 46671239cb6SGreg Roach return false; 46771239cb6SGreg Roach } 46871239cb6SGreg Roach if (day < 10) { 46971239cb6SGreg Roach day = '0' + day; 47071239cb6SGreg Roach } 47171239cb6SGreg Roach dateField.value = day + ' ' + monthShort[month + 1] + ' ' + year; 47271239cb6SGreg Roach return false; 47371239cb6SGreg Roach} 47471239cb6SGreg Roach 47506fe1eb5SGreg Roach/** 47606fe1eb5SGreg Roach * @param {string} dateFieldId 47706fe1eb5SGreg Roach * @param {string} dateDivId 47806fe1eb5SGreg Roach * @returns {boolean} 47906fe1eb5SGreg Roach */ 480efd89170SGreg Roachfunction cal_updateCalendar (dateFieldId, dateDivId) { 48171239cb6SGreg Roach var dateSel = document.getElementById(dateFieldId + '_daySelect'); 48271239cb6SGreg Roach if (!dateSel) { 48371239cb6SGreg Roach return false; 48471239cb6SGreg Roach } 48571239cb6SGreg Roach var monthSel = document.getElementById(dateFieldId + '_monSelect'); 48671239cb6SGreg Roach if (!monthSel) { 48771239cb6SGreg Roach return false; 48871239cb6SGreg Roach } 48971239cb6SGreg Roach var yearInput = document.getElementById(dateFieldId + '_yearInput'); 49071239cb6SGreg Roach if (!yearInput) { 49171239cb6SGreg Roach return false; 49271239cb6SGreg Roach } 49371239cb6SGreg Roach 49471239cb6SGreg Roach var month = parseInt(monthSel.options[monthSel.selectedIndex].value, 10); 49571239cb6SGreg Roach month = month - 1; 49671239cb6SGreg Roach 49771239cb6SGreg Roach var date = new Date(yearInput.value, month, dateSel.options[dateSel.selectedIndex].value); 49871239cb6SGreg Roach cal_setDateField(dateFieldId, date.getFullYear(), date.getMonth(), date.getDate()); 49971239cb6SGreg Roach 50071239cb6SGreg Roach var dateDiv = document.getElementById(dateDivId); 50171239cb6SGreg Roach if (!dateDiv) { 50271239cb6SGreg Roach alert('no dateDiv ' + dateDivId); 50371239cb6SGreg Roach return false; 50471239cb6SGreg Roach } 50571239cb6SGreg Roach dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date); 50671239cb6SGreg Roach 50771239cb6SGreg Roach return false; 50871239cb6SGreg Roach} 50971239cb6SGreg Roach 51006fe1eb5SGreg Roach/** 51106fe1eb5SGreg Roach * @param {string} dateFieldId 51206fe1eb5SGreg Roach * @param {string} dateDivId 51306fe1eb5SGreg Roach * @param {number} year 51406fe1eb5SGreg Roach * @param {number} month 51506fe1eb5SGreg Roach * @param {number} day 51606fe1eb5SGreg Roach * @returns {boolean} 51706fe1eb5SGreg Roach */ 518efd89170SGreg Roachfunction cal_dateClicked (dateFieldId, dateDivId, year, month, day) { 51971239cb6SGreg Roach cal_setDateField(dateFieldId, year, month, day); 52071239cb6SGreg Roach calendarWidget(dateDivId, dateFieldId); 52171239cb6SGreg Roach return false; 52271239cb6SGreg Roach} 52371239cb6SGreg Roach 52406fe1eb5SGreg Roach/** 52506fe1eb5SGreg Roach * @param {string} id 52606fe1eb5SGreg Roach */ 527efd89170SGreg Roachfunction openerpasteid (id) { 52871239cb6SGreg Roach if (window.opener.paste_id) { 52971239cb6SGreg Roach window.opener.paste_id(id); 53071239cb6SGreg Roach } 53171239cb6SGreg Roach window.close(); 53271239cb6SGreg Roach} 53371239cb6SGreg Roach 53406fe1eb5SGreg Roach/** 53506fe1eb5SGreg Roach * @param {string} value 53606fe1eb5SGreg Roach */ 537efd89170SGreg Roachfunction paste_id (value) { 53871239cb6SGreg Roach pastefield.value = value; 53971239cb6SGreg Roach} 54071239cb6SGreg Roach 54106fe1eb5SGreg Roach/** 54206fe1eb5SGreg Roach * @param {string} name 54306fe1eb5SGreg Roach */ 544efd89170SGreg Roachfunction pastename (name) { 54571239cb6SGreg Roach if (nameElement) { 54671239cb6SGreg Roach nameElement.innerHTML = name; 54771239cb6SGreg Roach } 54871239cb6SGreg Roach if (remElement) { 54971239cb6SGreg Roach remElement.style.display = 'block'; 55071239cb6SGreg Roach } 55171239cb6SGreg Roach} 55271239cb6SGreg Roach 55306fe1eb5SGreg Roach/** 55406fe1eb5SGreg Roach * @param {string} value 55506fe1eb5SGreg Roach */ 556efd89170SGreg Roachfunction paste_char (value) { 55771239cb6SGreg Roach if (document.selection) { 55871239cb6SGreg Roach // IE 55971239cb6SGreg Roach pastefield.focus(); 56071239cb6SGreg Roach document.selection.createRange().text = value; 56171239cb6SGreg Roach } else if (pastefield.selectionStart || pastefield.selectionStart === 0) { 56271239cb6SGreg Roach // Mozilla/Chrome/Safari 56371239cb6SGreg Roach pastefield.value = 56471239cb6SGreg Roach pastefield.value.substring(0, pastefield.selectionStart) + 56571239cb6SGreg Roach value + 56671239cb6SGreg Roach pastefield.value.substring(pastefield.selectionEnd, pastefield.value.length); 56771239cb6SGreg Roach pastefield.selectionStart = pastefield.selectionEnd = pastefield.selectionStart + value.length; 56871239cb6SGreg Roach } else { 56971239cb6SGreg Roach // Fallback? - just append 57071239cb6SGreg Roach pastefield.value += value; 57171239cb6SGreg Roach } 57271239cb6SGreg Roach 57371239cb6SGreg Roach if (pastefield.id === 'NPFX' || pastefield.id === 'GIVN' || pastefield.id === 'SPFX' || pastefield.id === 'SURN' || pastefield.id === 'NSFX') { 57471239cb6SGreg Roach updatewholename(); 57571239cb6SGreg Roach } 57671239cb6SGreg Roach} 57771239cb6SGreg Roach 57871239cb6SGreg Roach/** 57971239cb6SGreg Roach * Persistant checkbox options to hide/show extra data. 58064490ee2SGreg Roach * @param element_id 58171239cb6SGreg Roach */ 582efd89170SGreg Roachfunction persistent_toggle (element_id) { 583efd89170SGreg Roach const element = document.getElementById(element_id); 584efd89170SGreg Roach const key = 'state-of-' + element_id; 585efd89170SGreg Roach const state = localStorage.getItem(key); 58671239cb6SGreg Roach 58764490ee2SGreg Roach // Previously selected? 58864490ee2SGreg Roach if (state === 'true') { 58964490ee2SGreg Roach $(element).click(); 59071239cb6SGreg Roach } 59171239cb6SGreg Roach 59264490ee2SGreg Roach // Remember state for the next page load. 59364490ee2SGreg Roach $(element).on('change', function () { localStorage.setItem(key, element.checked); }); 59471239cb6SGreg Roach} 59571239cb6SGreg Roach 59606fe1eb5SGreg Roach/** 59706fe1eb5SGreg Roach * @param {string} field 59806fe1eb5SGreg Roach * @param {string} pos 59906fe1eb5SGreg Roach * @param {string} neg 60006fe1eb5SGreg Roach */ 601efd89170SGreg Roachfunction valid_lati_long (field, pos, neg) { 60271239cb6SGreg Roach // valid LATI or LONG according to Gedcom standard 60371239cb6SGreg Roach // pos (+) : N or E 60471239cb6SGreg Roach // neg (-) : S or W 60571239cb6SGreg Roach var txt = field.value.toUpperCase(); 60671239cb6SGreg Roach txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim 60771239cb6SGreg Roach txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34 60871239cb6SGreg Roach txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234 60971239cb6SGreg Roach txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698 61071239cb6SGreg Roach txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698 61171239cb6SGreg Roach // 0°34'11 ==> 0:34:11 61271239cb6SGreg Roach txt = txt.replace(/\u00b0/g, ':'); // ° 61371239cb6SGreg Roach txt = txt.replace(/\u0027/g, ':'); // ' 61471239cb6SGreg Roach // 0:34:11.2W ==> W0.5698 61571239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) { 61671239cb6SGreg Roach var n = parseFloat($1); 61771239cb6SGreg Roach n += ($2 / 60); 61871239cb6SGreg Roach n += ($3 / 3600); 61971239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 62071239cb6SGreg Roach return $4 + n; 62171239cb6SGreg Roach }); 62271239cb6SGreg Roach // 0:34W ==> W0.5667 62371239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) { 62471239cb6SGreg Roach var n = parseFloat($1); 62571239cb6SGreg Roach n += ($2 / 60); 62671239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 62771239cb6SGreg Roach return $3 + n; 62871239cb6SGreg Roach }); 62971239cb6SGreg Roach // 0.5698W ==> W0.5698 63071239cb6SGreg Roach txt = txt.replace(/(.*)([N|S|E|W]+)$/g, '$2$1'); 63171239cb6SGreg Roach // 17.1234 ==> N17.1234 63271239cb6SGreg Roach if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) { 63371239cb6SGreg Roach txt = pos + txt; 63471239cb6SGreg Roach } 63571239cb6SGreg Roach field.value = txt; 63671239cb6SGreg Roach} 63771239cb6SGreg Roach 63806fe1eb5SGreg Roach/** 63906fe1eb5SGreg Roach * Initialize autocomplete elements. 64006fe1eb5SGreg Roach * @param {string} selector 64106fe1eb5SGreg Roach */ 642efd89170SGreg Roachfunction autocomplete (selector) { 64371239cb6SGreg Roach // Use typeahead/bloodhound for autocomplete 64471239cb6SGreg Roach $(selector).each(function () { 645efd89170SGreg Roach const that = this; 64671239cb6SGreg Roach $(this).typeahead(null, { 64771239cb6SGreg Roach display: 'value', 6485e6816beSGreg Roach limit: 0, 64971239cb6SGreg Roach source: new Bloodhound({ 65071239cb6SGreg Roach datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 65171239cb6SGreg Roach queryTokenizer: Bloodhound.tokenizers.whitespace, 65271239cb6SGreg Roach remote: { 65371239cb6SGreg Roach url: this.dataset.autocompleteUrl, 654f4abaf12SGreg Roach replace: function (url, uriEncodedQuery) { 655f4abaf12SGreg Roach if (that.dataset.autocompleteExtra) { 656efd89170SGreg Roach const extra = $(document.querySelector(that.dataset.autocompleteExtra)).val(); 657efd89170SGreg Roach return url.replace('QUERY', uriEncodedQuery) + '&extra=' + encodeURIComponent(extra); 658f4abaf12SGreg Roach } 659efd89170SGreg Roach return url.replace('QUERY', uriEncodedQuery); 660f4abaf12SGreg Roach }, 661efd89170SGreg Roach wildcard: 'QUERY' 662f4abaf12SGreg Roach 66371239cb6SGreg Roach } 66471239cb6SGreg Roach }) 66571239cb6SGreg Roach }); 66671239cb6SGreg Roach }); 66771239cb6SGreg Roach} 66871239cb6SGreg Roach 66971239cb6SGreg Roach/** 67071239cb6SGreg Roach * Insert text at the current cursor position in an input field. 67106fe1eb5SGreg Roach * @param {Element} e The input element. 67206fe1eb5SGreg Roach * @param {string} t The text to insert. 67371239cb6SGreg Roach */ 674efd89170SGreg Roachfunction insertTextAtCursor (e, t) { 67571239cb6SGreg Roach var scrollTop = e.scrollTop; 67671239cb6SGreg Roach var selectionStart = e.selectionStart; 67771239cb6SGreg Roach var prefix = e.value.substring(0, selectionStart); 67871239cb6SGreg Roach var suffix = e.value.substring(e.selectionEnd, e.value.length); 67971239cb6SGreg Roach e.value = prefix + t + suffix; 68071239cb6SGreg Roach e.selectionStart = selectionStart + t.length; 68171239cb6SGreg Roach e.selectionEnd = e.selectionStart; 68271239cb6SGreg Roach e.focus(); 68371239cb6SGreg Roach e.scrollTop = scrollTop; 68471239cb6SGreg Roach} 68571239cb6SGreg Roach 68671239cb6SGreg Roach// Send the CSRF token on all AJAX requests 68771239cb6SGreg Roach$.ajaxSetup({ 68871239cb6SGreg Roach headers: { 68971239cb6SGreg Roach 'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content') 69071239cb6SGreg Roach } 69171239cb6SGreg Roach}); 69271239cb6SGreg Roach 69306fe1eb5SGreg Roach/** 69406fe1eb5SGreg Roach * Initialisation 69506fe1eb5SGreg Roach */ 69671239cb6SGreg Roach$(function () { 69771239cb6SGreg Roach // Page elements that load automaticaly via AJAX. 69871239cb6SGreg Roach // This prevents bad robots from crawling resource-intensive pages. 699efd89170SGreg Roach $('[data-ajax-url]').each(function () { 70071239cb6SGreg Roach $(this).load($(this).data('ajaxUrl')); 70171239cb6SGreg Roach }); 70271239cb6SGreg Roach 70306fe1eb5SGreg Roach /** 70406fe1eb5SGreg Roach * Select2 - format entries in the select list 70506fe1eb5SGreg Roach * @param {Object} data 70606fe1eb5SGreg Roach * @returns {string} 70706fe1eb5SGreg Roach */ 708efd89170SGreg Roach function templateOptionForSelect2 (data) { 70980d699d6SGreg Roach // This could be a "waiting..." message (data.loading is true) or a response from the server. 71080d699d6SGreg Roach // Both are already HTML, so no need to reformat it. 71171239cb6SGreg Roach return data.text; 71271239cb6SGreg Roach } 71371239cb6SGreg Roach 71471239cb6SGreg Roach // Autocomplete 71571239cb6SGreg Roach autocomplete('input[data-autocomplete-url]'); 71671239cb6SGreg Roach 71771239cb6SGreg Roach // Select2 - activate autocomplete fields 718bdbdb10cSGreg Roach const lang = document.documentElement.lang; 719bdbdb10cSGreg Roach const select2_languages = { 720bdbdb10cSGreg Roach 'zh-Hans': 'zh-CN', 721efd89170SGreg Roach 'zh-Hant': 'zh-TW' 722bdbdb10cSGreg Roach }; 723efd89170SGreg Roach $('select.select2').select2({ 724bdbdb10cSGreg Roach language: select2_languages[lang] || lang, 725aee7167dSGreg Roach // Needed for elements that are initially hidden. 726efd89170SGreg Roach width: '100%', 727896a5721SGreg Roach // Do not escape - we do it on the server. 72871239cb6SGreg Roach escapeMarkup: function (x) { 7298ec20abdSGreg Roach return x; 730efd89170SGreg Roach } 731896a5721SGreg Roach }); 732896a5721SGreg Roach 733896a5721SGreg Roach // If we clear the select (using the "X" button), we need an empty value 734896a5721SGreg Roach // (rather than no value at all) for (non-multiple) selects with name="array[]" 735efd89170SGreg Roach $('select.select2:not([multiple])') 736efd89170SGreg Roach .on('select2:unselect', function (evt) { 737efd89170SGreg Roach $(evt.delegateTarget).html('<option value="" selected></option>'); 738bdbdb10cSGreg Roach }); 73971239cb6SGreg Roach 74071239cb6SGreg Roach // Datatables - locale aware sorting 74171239cb6SGreg Roach $.fn.dataTableExt.oSort['text-asc'] = function (x, y) { 742efd89170SGreg Roach return x.localeCompare(y, document.documentElement.lang, { sensitivity: 'base' }); 74371239cb6SGreg Roach }; 74471239cb6SGreg Roach $.fn.dataTableExt.oSort['text-desc'] = function (x, y) { 745efd89170SGreg Roach return y.localeCompare(x, document.documentElement.lang, { sensitivity: 'base' }); 74671239cb6SGreg Roach }; 74771239cb6SGreg Roach 74871239cb6SGreg Roach // DataTables - start hidden to prevent FOUC. 74971239cb6SGreg Roach $('table.datatables').each(function () { 7504843b94fSGreg Roach $(this).DataTable(); 7514843b94fSGreg Roach $(this).removeClass('d-none'); 7524843b94fSGreg Roach }); 7534843b94fSGreg Roach 7544843b94fSGreg Roach // Save button state between pages 755efd89170SGreg Roach document.querySelectorAll('[data-toggle=button][data-persist]').forEach((element) => { 7564843b94fSGreg Roach // Previously selected? 757efd89170SGreg Roach if (localStorage.getItem('state-of-' + element.dataset.persist) === 'T') { 7584843b94fSGreg Roach element.click(); 7594843b94fSGreg Roach } 7604843b94fSGreg Roach // Save state on change 761efd89170SGreg Roach element.addEventListener('click', (event) => { 7624843b94fSGreg Roach // Event occurs *before* the state changes, so reverse T/F. 763efd89170SGreg Roach localStorage.setItem('state-of-' + event.target.dataset.persist, event.target.classList.contains('active') ? 'F' : 'T'); 7644843b94fSGreg Roach }); 7654843b94fSGreg Roach }); 76671239cb6SGreg Roach 76771239cb6SGreg Roach // Activate the on-screen keyboard 76871239cb6SGreg Roach var osk_focus_element; 76971239cb6SGreg Roach $('.wt-osk-trigger').click(function () { 77071239cb6SGreg Roach // When a user clicks the icon, set focus to the corresponding input 77171239cb6SGreg Roach osk_focus_element = document.getElementById($(this).data('id')); 77271239cb6SGreg Roach osk_focus_element.focus(); 77371239cb6SGreg Roach $('.wt-osk').show(); 77471239cb6SGreg Roach }); 77571239cb6SGreg Roach $('.wt-osk-script-button').change(function () { 77671239cb6SGreg Roach $('.wt-osk-script').prop('hidden', true); 77771239cb6SGreg Roach $('.wt-osk-script-' + $(this).data('script')).prop('hidden', false); 77871239cb6SGreg Roach }); 77971239cb6SGreg Roach $('.wt-osk-shift-button').click(function () { 78071239cb6SGreg Roach document.querySelector('.wt-osk-keys').classList.toggle('shifted'); 78171239cb6SGreg Roach }); 78271239cb6SGreg Roach $('.wt-osk-keys').on('click', '.wt-osk-key', function () { 78371239cb6SGreg Roach var key = $(this).contents().get(0).nodeValue; 78471239cb6SGreg Roach var shift_state = $('.wt-osk-shift-button').hasClass('active'); 78571239cb6SGreg Roach var shift_key = $('sup', this)[0]; 78671239cb6SGreg Roach if (shift_state && shift_key !== undefined) { 78771239cb6SGreg Roach key = shift_key.innerText; 78871239cb6SGreg Roach } 7890d2905f7SGreg Roach webtrees.pasteAtCursor(osk_focus_element, key); 79071239cb6SGreg Roach if ($('.wt-osk-pin-button').hasClass('active') === false) { 79171239cb6SGreg Roach $('.wt-osk').hide(); 79271239cb6SGreg Roach } 79371239cb6SGreg Roach }); 79471239cb6SGreg Roach 79571239cb6SGreg Roach $('.wt-osk-close').on('click', function () { 79671239cb6SGreg Roach $('.wt-osk').hide(); 79771239cb6SGreg Roach }); 79871239cb6SGreg Roach}); 7997adfb8e5SGreg Roach 800b3a775f6SGreg Roach// Convert data-confirm and data-post-url attributes into useful behavior. 801efd89170SGreg Roachdocument.addEventListener('click', (event) => { 802efd89170SGreg Roach const target = event.target.closest('a'); 8037adfb8e5SGreg Roach 8047adfb8e5SGreg Roach if (target === null) { 8057adfb8e5SGreg Roach return; 8067adfb8e5SGreg Roach } 8077adfb8e5SGreg Roach 808efd89170SGreg Roach if ('confirm' in target.dataset && !confirm(target.dataset.confirm)) { 8097adfb8e5SGreg Roach event.preventDefault(); 8107adfb8e5SGreg Roach return; 8117adfb8e5SGreg Roach } 8127adfb8e5SGreg Roach 813efd89170SGreg Roach if ('postUrl' in target.dataset) { 814efd89170SGreg Roach const token = document.querySelector('meta[name=csrf]').content; 815ea101122SGreg Roach 816ea101122SGreg Roach fetch(target.dataset.postUrl, { 817ea101122SGreg Roach method: 'POST', 818ea101122SGreg Roach headers: { 819ea101122SGreg Roach 'X-CSRF-TOKEN': token, 820ea101122SGreg Roach 'X-Requested-with': 'XMLHttpRequest', 821ea101122SGreg Roach }, 822ea101122SGreg Roach }).then((response) => { 823ea101122SGreg Roach if ('reloadUrl' in target.dataset) { 824ea101122SGreg Roach // Go somewhere else. e.g. home page after logout. 825ea101122SGreg Roach document.location = target.dataset.reloadUrl; 826ea101122SGreg Roach } else { 827ea101122SGreg Roach // Reload the current page. e.g. change language. 8287adfb8e5SGreg Roach document.location.reload(); 8297adfb8e5SGreg Roach } 830cb8f307bSGreg Roach }).catch((error) => { 831ea101122SGreg Roach alert(error); 832ea101122SGreg Roach }); 8337adfb8e5SGreg Roach } 8347adfb8e5SGreg Roach}); 835