171239cb6SGreg Roach/** 271239cb6SGreg Roach * webtrees: online genealogy 3242a7862SGreg Roach * Copyright (C) 2019 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 1671239cb6SGreg Roach'use strict'; 1771239cb6SGreg Roach 1859e18f0cSGreg Roachlet webtrees = function () { 1959e18f0cSGreg Roach const lang = document.documentElement.lang; 2059e18f0cSGreg Roach 2159e18f0cSGreg Roach /** 2259e18f0cSGreg Roach * Tidy the whitespace in a string. 2359e18f0cSGreg Roach */ 2459e18f0cSGreg Roach function trim(str) { 2559e18f0cSGreg Roach return str.replace(/\s+/g, " ").trim(); 2659e18f0cSGreg Roach 2759e18f0cSGreg Roach } 2859e18f0cSGreg Roach 2959e18f0cSGreg Roach /** 3059e18f0cSGreg Roach * Look for non-latin characters in a string. 3159e18f0cSGreg Roach */ 3259e18f0cSGreg Roach function detectScript(str) { 3359e18f0cSGreg Roach if (str.match(/[\u3400-\u9FCC]/)) { 3459e18f0cSGreg Roach return "cjk"; 3559e18f0cSGreg Roach } else if (str.match(/[\u0370-\u03FF]/)) { 3659e18f0cSGreg Roach return "greek"; 3759e18f0cSGreg Roach } else if (str.match(/[\u0400-\u04FF]/)) { 3859e18f0cSGreg Roach return "cyrillic"; 3959e18f0cSGreg Roach } else if (str.match(/[\u0590-\u05FF]/)) { 4059e18f0cSGreg Roach return "hebrew"; 4159e18f0cSGreg Roach } else if (str.match(/[\u0600-\u06FF]/)) { 4259e18f0cSGreg Roach return "arabic"; 4359e18f0cSGreg Roach } 4459e18f0cSGreg Roach 4559e18f0cSGreg Roach return "latin"; 4659e18f0cSGreg Roach } 4759e18f0cSGreg Roach 4859e18f0cSGreg Roach /** 4959e18f0cSGreg Roach * In some languages, the SURN uses a male/default form, but NAME uses a gender-inflected form. 5059e18f0cSGreg Roach */ 5159e18f0cSGreg Roach function inflectSurname(surname, sex) { 5259e18f0cSGreg Roach if (lang === "pl" && sex === "F") { 5359e18f0cSGreg Roach return surname 5459e18f0cSGreg Roach .replace(/ski$/, "ska") 5559e18f0cSGreg Roach .replace(/cki$/, "cka") 5659e18f0cSGreg Roach .replace(/dzki$/, "dzka") 5759e18f0cSGreg Roach .replace(/żki$/, "żka"); 5859e18f0cSGreg Roach } 5959e18f0cSGreg Roach 6059e18f0cSGreg Roach return surname; 6159e18f0cSGreg Roach } 6259e18f0cSGreg Roach 6359e18f0cSGreg Roach /** 6459e18f0cSGreg Roach * Build a NAME from a NPFX, GIVN, SPFX, SURN and NSFX parts. 6559e18f0cSGreg Roach * 6659e18f0cSGreg Roach * Assumes the language of the document is the same as the language of the name. 6759e18f0cSGreg Roach */ 6859e18f0cSGreg Roach function buildNameFromParts(npfx, givn, spfx, surn, nsfx, sex) { 6959e18f0cSGreg Roach const usesCJK = detectScript(npfx + givn + spfx + givn + surn + nsfx) === "cjk"; 7059e18f0cSGreg Roach const separator = usesCJK ? "" : " "; 7159e18f0cSGreg Roach const surnameFirst = usesCJK || ['hu', 'jp', 'ko', 'vi', 'zh-Hans', 'zh-Hant'].indexOf(lang) !== -1; 7259e18f0cSGreg Roach const patronym = ['is'].indexOf(lang) !== -1; 7359e18f0cSGreg Roach const slash = patronym ? "" : "/"; 7459e18f0cSGreg Roach 7559e18f0cSGreg Roach // GIVN and SURN may be a comma-separated lists. 7659e18f0cSGreg Roach npfx = trim(npfx); 7759e18f0cSGreg Roach givn = trim(givn.replace(",", separator)); 7859e18f0cSGreg Roach spfx = trim(spfx); 7959e18f0cSGreg Roach surn = inflectSurname(trim(surn.replace(",", separator)), sex); 8059e18f0cSGreg Roach nsfx = trim(nsfx); 8159e18f0cSGreg Roach 8259e18f0cSGreg Roach const surname = trim(spfx + separator + surn); 8359e18f0cSGreg Roach 8459e18f0cSGreg Roach const name = surnameFirst ? slash + surname + slash + separator + givn : givn + separator + slash + surname + slash; 8559e18f0cSGreg Roach 8659e18f0cSGreg Roach return trim(npfx + separator + name + separator + nsfx); 8759e18f0cSGreg Roach } 8859e18f0cSGreg Roach 8959e18f0cSGreg Roach // Public methods 9059e18f0cSGreg Roach return { 9159e18f0cSGreg Roach buildNameFromParts: buildNameFromParts, 9259e18f0cSGreg Roach detectScript: detectScript, 9359e18f0cSGreg Roach }; 9459e18f0cSGreg Roach}(); 9559e18f0cSGreg Roach 9671239cb6SGreg Roachfunction expand_layer(sid) 9771239cb6SGreg Roach{ 9871239cb6SGreg Roach $('#' + sid + '_img').toggleClass('icon-plus icon-minus'); 9971239cb6SGreg Roach $('#' + sid).slideToggle('fast'); 10071239cb6SGreg Roach $('#' + sid + '-alt').toggle(); // hide something when we show the layer - and vice-versa 10171239cb6SGreg Roach return false; 10271239cb6SGreg Roach} 10371239cb6SGreg Roach 10471239cb6SGreg Roach// Accept the changes to a record - and reload the page 10571239cb6SGreg Roachfunction accept_changes(xref, ged) 10671239cb6SGreg Roach{ 10771239cb6SGreg Roach $.post( 1081bd3adbdSGreg Roach 'index.php?route=accept-changes', 10971239cb6SGreg Roach { 11071239cb6SGreg Roach xref: xref, 11171239cb6SGreg Roach ged: ged, 11271239cb6SGreg Roach }, 11371239cb6SGreg Roach function () { 114070932ceSGreg Roach document.location.reload(); 11571239cb6SGreg Roach } 11671239cb6SGreg Roach ); 11771239cb6SGreg Roach return false; 11871239cb6SGreg Roach} 11971239cb6SGreg Roach 12071239cb6SGreg Roach// Reject the changes to a record - and reload the page 12171239cb6SGreg Roachfunction reject_changes(xref, ged) 12271239cb6SGreg Roach{ 12371239cb6SGreg Roach $.post( 1241bd3adbdSGreg Roach 'index.php?route=reject-changes', 12571239cb6SGreg Roach { 12671239cb6SGreg Roach xref: xref, 12771239cb6SGreg Roach ged: ged, 12871239cb6SGreg Roach }, 12971239cb6SGreg Roach function () { 130070932ceSGreg Roach document.location.reload(); 13171239cb6SGreg Roach } 13271239cb6SGreg Roach ); 13371239cb6SGreg Roach return false; 13471239cb6SGreg Roach} 13571239cb6SGreg Roach 13671239cb6SGreg Roach// Delete a record - and reload the page 13771239cb6SGreg Roachfunction delete_record(xref, gedcom) 13871239cb6SGreg Roach{ 13971239cb6SGreg Roach $.post( 1401bd3adbdSGreg Roach 'index.php?route=delete-record', 14171239cb6SGreg Roach { 14271239cb6SGreg Roach xref: xref, 14371239cb6SGreg Roach ged: gedcom, 14471239cb6SGreg Roach }, 14571239cb6SGreg Roach function () { 146070932ceSGreg Roach document.location.reload(); 14771239cb6SGreg Roach } 14871239cb6SGreg Roach ); 14971239cb6SGreg Roach 15071239cb6SGreg Roach return false; 15171239cb6SGreg Roach} 15271239cb6SGreg Roach 15371239cb6SGreg Roach// Delete a fact - and reload the page 15471239cb6SGreg Roachfunction delete_fact(message, ged, xref, fact_id) 15571239cb6SGreg Roach{ 15671239cb6SGreg Roach if (confirm(message)) { 15771239cb6SGreg Roach $.post( 1581bd3adbdSGreg Roach 'index.php?route=delete-fact', 15971239cb6SGreg Roach { 16071239cb6SGreg Roach xref: xref, 16171239cb6SGreg Roach fact_id: fact_id, 16271239cb6SGreg Roach ged: ged 16371239cb6SGreg Roach }, 16471239cb6SGreg Roach function () { 165070932ceSGreg Roach document.location.reload(); 16671239cb6SGreg Roach } 16771239cb6SGreg Roach ); 16871239cb6SGreg Roach } 16971239cb6SGreg Roach return false; 17071239cb6SGreg Roach} 17171239cb6SGreg Roach 17271239cb6SGreg Roach// Copy a fact to the clipboard 17371239cb6SGreg Roachfunction copy_fact(ged, xref, fact_id) 17471239cb6SGreg Roach{ 17571239cb6SGreg Roach $.post( 176b774d484SGreg Roach 'index.php?route=copy-fact', 17771239cb6SGreg Roach { 17871239cb6SGreg Roach xref: xref, 17971239cb6SGreg Roach fact_id: fact_id, 18071239cb6SGreg Roach ged: ged, 18171239cb6SGreg Roach }, 18271239cb6SGreg Roach function () { 183070932ceSGreg Roach document.location.reload(); 18471239cb6SGreg Roach } 18571239cb6SGreg Roach ); 18671239cb6SGreg Roach return false; 18771239cb6SGreg Roach} 18871239cb6SGreg Roach 18971239cb6SGreg Roach// Paste a fact from the clipboard 19071239cb6SGreg Roachfunction paste_fact(ged, xref, element) 19171239cb6SGreg Roach{ 19271239cb6SGreg Roach $.post( 193a82694daSGreg Roach 'index.php?route=paste-fact', 19471239cb6SGreg Roach { 19571239cb6SGreg Roach xref: xref, 19671239cb6SGreg Roach fact_id: $(element).val(), // element is the <select> containing the option 19771239cb6SGreg Roach ged: ged, 19871239cb6SGreg Roach }, 19971239cb6SGreg Roach function () { 200070932ceSGreg Roach document.location.reload(); 20171239cb6SGreg Roach } 20271239cb6SGreg Roach ); 20371239cb6SGreg Roach return false; 20471239cb6SGreg Roach} 20571239cb6SGreg Roach 20671239cb6SGreg Roach// Delete a user - and reload the page 20771239cb6SGreg Roachfunction delete_user(message, user_id) 20871239cb6SGreg Roach{ 20971239cb6SGreg Roach if (confirm(message)) { 21071239cb6SGreg Roach $.post( 2111bd3adbdSGreg Roach 'index.php?route=delete-user', 21271239cb6SGreg Roach { 21371239cb6SGreg Roach user_id: user_id, 21471239cb6SGreg Roach }, 21571239cb6SGreg Roach function () { 216070932ceSGreg Roach document.location.reload(); 21771239cb6SGreg Roach } 21871239cb6SGreg Roach ); 21971239cb6SGreg Roach } 22071239cb6SGreg Roach return false; 22171239cb6SGreg Roach} 22271239cb6SGreg Roach 22371239cb6SGreg Roach// Masquerade as another user - and reload the page. 22471239cb6SGreg Roachfunction masquerade(user_id) 22571239cb6SGreg Roach{ 22671239cb6SGreg Roach $.post( 2271bd3adbdSGreg Roach 'index.php?route=masquerade', 22871239cb6SGreg Roach { 22971239cb6SGreg Roach user_id: user_id, 23071239cb6SGreg Roach }, 23171239cb6SGreg Roach function () { 232070932ceSGreg Roach document.location.reload(); 23371239cb6SGreg Roach } 23471239cb6SGreg Roach ); 23571239cb6SGreg Roach return false; 23671239cb6SGreg Roach} 23771239cb6SGreg Roach 23871239cb6SGreg Roachvar pastefield; 23971239cb6SGreg Roachfunction addmedia_links(field, iid, iname) 24071239cb6SGreg Roach{ 24171239cb6SGreg Roach pastefield = field; 24271239cb6SGreg Roach insertRowToTable(iid, iname); 24371239cb6SGreg Roach return false; 24471239cb6SGreg Roach} 24571239cb6SGreg Roach 24671239cb6SGreg Roachfunction valid_date(datefield, dmy) 24771239cb6SGreg Roach{ 24871239cb6SGreg Roach var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']; 24971239cb6SGreg Roach var hijri_months = ['MUHAR', 'SAFAR', 'RABIA', 'RABIT', 'JUMAA', 'JUMAT', 'RAJAB', 'SHAAB', 'RAMAD', 'SHAWW', 'DHUAQ', 'DHUAH']; 25071239cb6SGreg Roach var hebrew_months = ['TSH', 'CSH', 'KSL', 'TVT', 'SHV', 'ADR', 'ADS', 'NSN', 'IYR', 'SVN', 'TMZ', 'AAV', 'ELL']; 25171239cb6SGreg Roach var french_months = ['VEND', 'BRUM', 'FRIM', 'NIVO', 'PLUV', 'VENT', 'GERM', 'FLOR', 'PRAI', 'MESS', 'THER', 'FRUC', 'COMP']; 25271239cb6SGreg Roach var jalali_months = ['FARVA', 'ORDIB', 'KHORD', 'TIR', 'MORDA', 'SHAHR', 'MEHR', 'ABAN', 'AZAR', 'DEY', 'BAHMA', 'ESFAN']; 25371239cb6SGreg Roach 25471239cb6SGreg Roach var datestr = datefield.value; 25571239cb6SGreg Roach // if a date has a date phrase marked by () this has to be excluded from altering 25671239cb6SGreg Roach var datearr = datestr.split('('); 25771239cb6SGreg Roach var datephrase = ''; 25871239cb6SGreg Roach if (datearr.length > 1) { 25971239cb6SGreg Roach datestr = datearr[0]; 26071239cb6SGreg Roach datephrase = datearr[1]; 26171239cb6SGreg Roach } 26271239cb6SGreg Roach 26371239cb6SGreg Roach // Gedcom dates are upper case 26471239cb6SGreg Roach datestr = datestr.toUpperCase(); 26571239cb6SGreg Roach // Gedcom dates have no leading/trailing/repeated whitespace 26671239cb6SGreg Roach datestr = datestr.replace(/\s+/, ' '); 26771239cb6SGreg Roach datestr = datestr.replace(/(^\s)|(\s$)/, ''); 26871239cb6SGreg Roach // Gedcom dates have spaces between letters and digits, e.g. "01JAN2000" => "01 JAN 2000" 26971239cb6SGreg Roach datestr = datestr.replace(/(\d)([A-Z])/, '$1 $2'); 27071239cb6SGreg Roach datestr = datestr.replace(/([A-Z])(\d)/, '$1 $2'); 27171239cb6SGreg Roach 27271239cb6SGreg Roach // Shortcut for quarter format, "Q1 1900" => "BET JAN 1900 AND MAR 1900". See [ 1509083 ] 27371239cb6SGreg Roach if (datestr.match(/^Q ([1-4]) (\d\d\d\d)$/)) { 27471239cb6SGreg Roach datestr = 'BET ' + months[RegExp.$1 * 3 - 3] + ' ' + RegExp.$2 + ' AND ' + months[RegExp.$1 * 3 - 1] + ' ' + RegExp.$2; 27571239cb6SGreg Roach } 27671239cb6SGreg Roach 27771239cb6SGreg Roach // Shortcut for @#Dxxxxx@ 01 01 1400, etc. 27871239cb6SGreg Roach if (datestr.match(/^(@#DHIJRI@|HIJRI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 27971239cb6SGreg Roach datestr = '@#DHIJRI@' + RegExp.$2 + hijri_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 28071239cb6SGreg Roach } 28171239cb6SGreg Roach if (datestr.match(/^(@#DJALALI@|JALALI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 28271239cb6SGreg Roach datestr = '@#DJALALI@' + RegExp.$2 + jalali_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 28371239cb6SGreg Roach } 28471239cb6SGreg Roach if (datestr.match(/^(@#DHEBREW@|HEBREW)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 28571239cb6SGreg Roach datestr = '@#DHEBREW@' + RegExp.$2 + hebrew_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 28671239cb6SGreg Roach } 28771239cb6SGreg Roach if (datestr.match(/^(@#DFRENCH R@|FRENCH)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 28871239cb6SGreg Roach datestr = '@#DFRENCH R@' + RegExp.$2 + french_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 28971239cb6SGreg Roach } 29071239cb6SGreg Roach 29171239cb6SGreg Roach // e.g. 17.11.1860, 03/04/2005 or 1999-12-31. Use locale settings where DMY order is ambiguous. 29271239cb6SGreg Roach var qsearch = /^([^\d]*)(\d+)[^\d](\d+)[^\d](\d+)$/i; 29371239cb6SGreg Roach if (qsearch.exec(datestr)) { 29471239cb6SGreg Roach var f0 = RegExp.$1; 29571239cb6SGreg Roach var f1 = parseInt(RegExp.$2, 10); 29671239cb6SGreg Roach var f2 = parseInt(RegExp.$3, 10); 29771239cb6SGreg Roach var f3 = parseInt(RegExp.$4, 10); 29871239cb6SGreg Roach var yyyy = new Date().getFullYear(); 29971239cb6SGreg Roach var yy = yyyy % 100; 30071239cb6SGreg Roach var cc = yyyy - yy; 30171239cb6SGreg Roach if (dmy === 'DMY' && f1 <= 31 && f2 <= 12 || f1 > 13 && f1 <= 31 && f2 <= 12 && f3 > 31) { 30271239cb6SGreg Roach datestr = f0 + f1 + ' ' + months[f2 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 30371239cb6SGreg Roach } else { 30471239cb6SGreg Roach if (dmy === 'MDY' && f1 <= 12 && f2 <= 31 || f2 > 13 && f2 <= 31 && f1 <= 12 && f3 > 31) { 30571239cb6SGreg Roach datestr = f0 + f2 + ' ' + months[f1 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 30671239cb6SGreg Roach } else { 30771239cb6SGreg Roach if (dmy === 'YMD' && f2 <= 12 && f3 <= 31 || f3 > 13 && f3 <= 31 && f2 <= 12 && f1 > 31) { 30871239cb6SGreg Roach datestr = f0 + f3 + ' ' + months[f2 - 1] + ' ' + (f1 >= 100 ? f1 : (f1 <= yy ? f1 + cc : f1 + cc - 100)); 30971239cb6SGreg Roach } 31071239cb6SGreg Roach } 31171239cb6SGreg Roach } 31271239cb6SGreg Roach } 31371239cb6SGreg Roach 31471239cb6SGreg Roach // Shortcuts for date ranges 31571239cb6SGreg Roach datestr = datestr.replace(/^[>]([\w ]+)$/, 'AFT $1'); 31671239cb6SGreg Roach datestr = datestr.replace(/^[<]([\w ]+)$/, 'BEF $1'); 31771239cb6SGreg Roach datestr = datestr.replace(/^([\w ]+)[-]$/, 'FROM $1'); 31871239cb6SGreg Roach datestr = datestr.replace(/^[-]([\w ]+)$/, 'TO $1'); 31971239cb6SGreg Roach datestr = datestr.replace(/^[~]([\w ]+)$/, 'ABT $1'); 32071239cb6SGreg Roach datestr = datestr.replace(/^[*]([\w ]+)$/, 'EST $1'); 32171239cb6SGreg Roach datestr = datestr.replace(/^[#]([\w ]+)$/, 'CAL $1'); 32271239cb6SGreg Roach datestr = datestr.replace(/^([\w ]+) ?- ?([\w ]+)$/, 'BET $1 AND $2'); 32371239cb6SGreg Roach datestr = datestr.replace(/^([\w ]+) ?~ ?([\w ]+)$/, 'FROM $1 TO $2'); 32471239cb6SGreg Roach 32571239cb6SGreg Roach // Convert full months to short months 32671239cb6SGreg Roach datestr = datestr.replace(/(JANUARY)/, 'JAN'); 32771239cb6SGreg Roach datestr = datestr.replace(/(FEBRUARY)/, 'FEB'); 32871239cb6SGreg Roach datestr = datestr.replace(/(MARCH)/, 'MAR'); 32971239cb6SGreg Roach datestr = datestr.replace(/(APRIL)/, 'APR'); 33071239cb6SGreg Roach datestr = datestr.replace(/(MAY)/, 'MAY'); 33171239cb6SGreg Roach datestr = datestr.replace(/(JUNE)/, 'JUN'); 33271239cb6SGreg Roach datestr = datestr.replace(/(JULY)/, 'JUL'); 33371239cb6SGreg Roach datestr = datestr.replace(/(AUGUST)/, 'AUG'); 33471239cb6SGreg Roach datestr = datestr.replace(/(SEPTEMBER)/, 'SEP'); 33571239cb6SGreg Roach datestr = datestr.replace(/(OCTOBER)/, 'OCT'); 33671239cb6SGreg Roach datestr = datestr.replace(/(NOVEMBER)/, 'NOV'); 33771239cb6SGreg Roach datestr = datestr.replace(/(DECEMBER)/, 'DEC'); 33871239cb6SGreg Roach 33971239cb6SGreg Roach // Americans frequently enter dates as SEP 20, 1999 34071239cb6SGreg Roach // No need to internationalise this, as this is an english-language issue 34171239cb6SGreg 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'); 34271239cb6SGreg Roach 34371239cb6SGreg Roach // Apply leading zero to day numbers 34471239cb6SGreg Roach datestr = datestr.replace(/(^| )(\d [A-Z]{3,5} \d{4})/, '$10$2'); 34571239cb6SGreg Roach 34671239cb6SGreg Roach if (datephrase) { 34771239cb6SGreg Roach datestr = datestr + ' (' + datephrase; 34871239cb6SGreg Roach } 34971239cb6SGreg Roach // Only update it if is has been corrected - otherwise input focus 35071239cb6SGreg Roach // moves to the end of the field unnecessarily 35171239cb6SGreg Roach if (datefield.value !== datestr) { 35271239cb6SGreg Roach datefield.value = datestr; 35371239cb6SGreg Roach } 35471239cb6SGreg Roach} 35571239cb6SGreg Roach 35671239cb6SGreg Roachvar monthLabels = []; 35771239cb6SGreg RoachmonthLabels[1] = 'January'; 35871239cb6SGreg RoachmonthLabels[2] = 'February'; 35971239cb6SGreg RoachmonthLabels[3] = 'March'; 36071239cb6SGreg RoachmonthLabels[4] = 'April'; 36171239cb6SGreg RoachmonthLabels[5] = 'May'; 36271239cb6SGreg RoachmonthLabels[6] = 'June'; 36371239cb6SGreg RoachmonthLabels[7] = 'July'; 36471239cb6SGreg RoachmonthLabels[8] = 'August'; 36571239cb6SGreg RoachmonthLabels[9] = 'September'; 36671239cb6SGreg RoachmonthLabels[10] = 'October'; 36771239cb6SGreg RoachmonthLabels[11] = 'November'; 36871239cb6SGreg RoachmonthLabels[12] = 'December'; 36971239cb6SGreg Roach 37071239cb6SGreg Roachvar monthShort = []; 37171239cb6SGreg RoachmonthShort[1] = 'JAN'; 37271239cb6SGreg RoachmonthShort[2] = 'FEB'; 37371239cb6SGreg RoachmonthShort[3] = 'MAR'; 37471239cb6SGreg RoachmonthShort[4] = 'APR'; 37571239cb6SGreg RoachmonthShort[5] = 'MAY'; 37671239cb6SGreg RoachmonthShort[6] = 'JUN'; 37771239cb6SGreg RoachmonthShort[7] = 'JUL'; 37871239cb6SGreg RoachmonthShort[8] = 'AUG'; 37971239cb6SGreg RoachmonthShort[9] = 'SEP'; 38071239cb6SGreg RoachmonthShort[10] = 'OCT'; 38171239cb6SGreg RoachmonthShort[11] = 'NOV'; 38271239cb6SGreg RoachmonthShort[12] = 'DEC'; 38371239cb6SGreg Roach 38471239cb6SGreg Roachvar daysOfWeek = []; 38571239cb6SGreg RoachdaysOfWeek[0] = 'S'; 38671239cb6SGreg RoachdaysOfWeek[1] = 'M'; 38771239cb6SGreg RoachdaysOfWeek[2] = 'T'; 38871239cb6SGreg RoachdaysOfWeek[3] = 'W'; 38971239cb6SGreg RoachdaysOfWeek[4] = 'T'; 39071239cb6SGreg RoachdaysOfWeek[5] = 'F'; 39171239cb6SGreg RoachdaysOfWeek[6] = 'S'; 39271239cb6SGreg Roach 39371239cb6SGreg Roachvar weekStart = 0; 39471239cb6SGreg Roach 39571239cb6SGreg Roachfunction cal_setMonthNames(jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec) 39671239cb6SGreg Roach{ 39771239cb6SGreg Roach monthLabels[1] = jan; 39871239cb6SGreg Roach monthLabels[2] = feb; 39971239cb6SGreg Roach monthLabels[3] = mar; 40071239cb6SGreg Roach monthLabels[4] = apr; 40171239cb6SGreg Roach monthLabels[5] = may; 40271239cb6SGreg Roach monthLabels[6] = jun; 40371239cb6SGreg Roach monthLabels[7] = jul; 40471239cb6SGreg Roach monthLabels[8] = aug; 40571239cb6SGreg Roach monthLabels[9] = sep; 40671239cb6SGreg Roach monthLabels[10] = oct; 40771239cb6SGreg Roach monthLabels[11] = nov; 40871239cb6SGreg Roach monthLabels[12] = dec; 40971239cb6SGreg Roach} 41071239cb6SGreg Roach 41171239cb6SGreg Roachfunction cal_setDayHeaders(sun, mon, tue, wed, thu, fri, sat) 41271239cb6SGreg Roach{ 41371239cb6SGreg Roach daysOfWeek[0] = sun; 41471239cb6SGreg Roach daysOfWeek[1] = mon; 41571239cb6SGreg Roach daysOfWeek[2] = tue; 41671239cb6SGreg Roach daysOfWeek[3] = wed; 41771239cb6SGreg Roach daysOfWeek[4] = thu; 41871239cb6SGreg Roach daysOfWeek[5] = fri; 41971239cb6SGreg Roach daysOfWeek[6] = sat; 42071239cb6SGreg Roach} 42171239cb6SGreg Roach 42271239cb6SGreg Roachfunction cal_setWeekStart(day) 42371239cb6SGreg Roach{ 42471239cb6SGreg Roach if (day >= 0 && day < 7) { 42571239cb6SGreg Roach weekStart = day; 42671239cb6SGreg Roach } 42771239cb6SGreg Roach} 42871239cb6SGreg Roach 42971239cb6SGreg Roachfunction calendarWidget(dateDivId, dateFieldId) 43071239cb6SGreg Roach{ 43171239cb6SGreg Roach var dateDiv = document.getElementById(dateDivId); 43271239cb6SGreg Roach var dateField = document.getElementById(dateFieldId); 43371239cb6SGreg Roach 43471239cb6SGreg Roach if (dateDiv.style.visibility === 'visible') { 43571239cb6SGreg Roach dateDiv.style.visibility = 'hidden'; 43671239cb6SGreg Roach return false; 43771239cb6SGreg Roach } 43871239cb6SGreg Roach if (dateDiv.style.visibility === 'show') { 43971239cb6SGreg Roach dateDiv.style.visibility = 'hide'; 44071239cb6SGreg Roach return false; 44171239cb6SGreg Roach } 44271239cb6SGreg Roach 44371239cb6SGreg Roach /* Javascript calendar functions only work with precise gregorian dates "D M Y" or "Y" */ 44471239cb6SGreg Roach var greg_regex = /((\d+ (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) )?\d+)/i; 44571239cb6SGreg Roach var date; 44671239cb6SGreg Roach if (greg_regex.exec(dateField.value)) { 44771239cb6SGreg Roach date = new Date(RegExp.$1); 44871239cb6SGreg Roach } else { 44971239cb6SGreg Roach date = new Date(); 45071239cb6SGreg Roach } 45171239cb6SGreg Roach 45271239cb6SGreg Roach dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date); 45371239cb6SGreg Roach if (dateDiv.style.visibility === 'hidden') { 45471239cb6SGreg Roach dateDiv.style.visibility = 'visible'; 45571239cb6SGreg Roach return false; 45671239cb6SGreg Roach } 45771239cb6SGreg Roach if (dateDiv.style.visibility === 'hide') { 45871239cb6SGreg Roach dateDiv.style.visibility = 'show'; 45971239cb6SGreg Roach return false; 46071239cb6SGreg Roach } 46171239cb6SGreg Roach 46271239cb6SGreg Roach return false; 46371239cb6SGreg Roach} 46471239cb6SGreg Roach 46571239cb6SGreg Roachfunction cal_generateSelectorContent(dateFieldId, dateDivId, date) 46671239cb6SGreg Roach{ 46771239cb6SGreg Roach var i, j; 46871239cb6SGreg Roach var content = '<table border="1"><tr>'; 46971239cb6SGreg Roach content += '<td><select class="form-control" id="' + dateFieldId + '_daySelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 47071239cb6SGreg Roach for (i = 1; i < 32; i++) { 47171239cb6SGreg Roach content += '<option value="' + i + '"'; 47271239cb6SGreg Roach if (date.getDate() === i) { 47371239cb6SGreg Roach content += ' selected="selected"'; 47471239cb6SGreg Roach } 47571239cb6SGreg Roach content += '>' + i + '</option>'; 47671239cb6SGreg Roach } 47771239cb6SGreg Roach content += '</select></td>'; 47871239cb6SGreg Roach content += '<td><select class="form-control" id="' + dateFieldId + '_monSelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 47971239cb6SGreg Roach for (i = 1; i < 13; i++) { 48071239cb6SGreg Roach content += '<option value="' + i + '"'; 48171239cb6SGreg Roach if (date.getMonth() + 1 === i) { 48271239cb6SGreg Roach content += ' selected="selected"'; 48371239cb6SGreg Roach } 48471239cb6SGreg Roach content += '>' + monthLabels[i] + '</option>'; 48571239cb6SGreg Roach } 48671239cb6SGreg Roach content += '</select></td>'; 48771239cb6SGreg 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>'; 48871239cb6SGreg Roach content += '<tr><td colspan="3">'; 48971239cb6SGreg Roach content += '<table width="100%">'; 49071239cb6SGreg Roach content += '<tr>'; 49171239cb6SGreg Roach j = weekStart; 49271239cb6SGreg Roach for (i = 0; i < 7; i++) { 49371239cb6SGreg Roach content += '<td '; 49471239cb6SGreg Roach content += 'class="descriptionbox"'; 49571239cb6SGreg Roach content += '>'; 49671239cb6SGreg Roach content += daysOfWeek[j]; 49771239cb6SGreg Roach content += '</td>'; 49871239cb6SGreg Roach j++; 49971239cb6SGreg Roach if (j > 6) { 50071239cb6SGreg Roach j = 0; 50171239cb6SGreg Roach } 50271239cb6SGreg Roach } 50371239cb6SGreg Roach content += '</tr>'; 50471239cb6SGreg Roach 50571239cb6SGreg Roach var tdate = new Date(date.getFullYear(), date.getMonth(), 1); 50671239cb6SGreg Roach var day = tdate.getDay(); 50771239cb6SGreg Roach day = day - weekStart; 50871239cb6SGreg Roach var daymilli = 1000 * 60 * 60 * 24; 50971239cb6SGreg Roach tdate = tdate.getTime() - (day * daymilli) + (daymilli / 2); 51071239cb6SGreg Roach tdate = new Date(tdate); 51171239cb6SGreg Roach 51271239cb6SGreg Roach for (j = 0; j < 6; j++) { 51371239cb6SGreg Roach content += '<tr>'; 51471239cb6SGreg Roach for (i = 0; i < 7; i++) { 51571239cb6SGreg Roach content += '<td '; 51671239cb6SGreg Roach if (tdate.getMonth() === date.getMonth()) { 51771239cb6SGreg Roach if (tdate.getDate() === date.getDate()) { 51871239cb6SGreg Roach content += 'class="descriptionbox"'; 51971239cb6SGreg Roach } else { 52071239cb6SGreg Roach content += 'class="optionbox"'; 52171239cb6SGreg Roach } 52271239cb6SGreg Roach } else { 52371239cb6SGreg Roach content += 'style="background-color:#EAEAEA; border: solid #AAAAAA 1px;"'; 52471239cb6SGreg Roach } 52571239cb6SGreg Roach content += '><a href="#" onclick="return cal_dateClicked(\'' + dateFieldId + '\', \'' + dateDivId + '\', ' + tdate.getFullYear() + ', ' + tdate.getMonth() + ', ' + tdate.getDate() + ');">'; 52671239cb6SGreg Roach content += tdate.getDate(); 52771239cb6SGreg Roach content += '</a></td>'; 52871239cb6SGreg Roach var datemilli = tdate.getTime() + daymilli; 52971239cb6SGreg Roach tdate = new Date(datemilli); 53071239cb6SGreg Roach } 53171239cb6SGreg Roach content += '</tr>'; 53271239cb6SGreg Roach } 53371239cb6SGreg Roach content += '</table>'; 53471239cb6SGreg Roach content += '</td></tr>'; 53571239cb6SGreg Roach content += '</table>'; 53671239cb6SGreg Roach 53771239cb6SGreg Roach return content; 53871239cb6SGreg Roach} 53971239cb6SGreg Roach 54071239cb6SGreg Roachfunction cal_setDateField(dateFieldId, year, month, day) 54171239cb6SGreg Roach{ 54271239cb6SGreg Roach var dateField = document.getElementById(dateFieldId); 54371239cb6SGreg Roach if (!dateField) { 54471239cb6SGreg Roach return false; 54571239cb6SGreg Roach } 54671239cb6SGreg Roach if (day < 10) { 54771239cb6SGreg Roach day = '0' + day; 54871239cb6SGreg Roach } 54971239cb6SGreg Roach dateField.value = day + ' ' + monthShort[month + 1] + ' ' + year; 55071239cb6SGreg Roach return false; 55171239cb6SGreg Roach} 55271239cb6SGreg Roach 55371239cb6SGreg Roachfunction cal_updateCalendar(dateFieldId, dateDivId) 55471239cb6SGreg Roach{ 55571239cb6SGreg Roach var dateSel = document.getElementById(dateFieldId + '_daySelect'); 55671239cb6SGreg Roach if (!dateSel) { 55771239cb6SGreg Roach return false; 55871239cb6SGreg Roach } 55971239cb6SGreg Roach var monthSel = document.getElementById(dateFieldId + '_monSelect'); 56071239cb6SGreg Roach if (!monthSel) { 56171239cb6SGreg Roach return false; 56271239cb6SGreg Roach } 56371239cb6SGreg Roach var yearInput = document.getElementById(dateFieldId + '_yearInput'); 56471239cb6SGreg Roach if (!yearInput) { 56571239cb6SGreg Roach return false; 56671239cb6SGreg Roach } 56771239cb6SGreg Roach 56871239cb6SGreg Roach var month = parseInt(monthSel.options[monthSel.selectedIndex].value, 10); 56971239cb6SGreg Roach month = month - 1; 57071239cb6SGreg Roach 57171239cb6SGreg Roach var date = new Date(yearInput.value, month, dateSel.options[dateSel.selectedIndex].value); 57271239cb6SGreg Roach cal_setDateField(dateFieldId, date.getFullYear(), date.getMonth(), date.getDate()); 57371239cb6SGreg Roach 57471239cb6SGreg Roach var dateDiv = document.getElementById(dateDivId); 57571239cb6SGreg Roach if (!dateDiv) { 57671239cb6SGreg Roach alert('no dateDiv ' + dateDivId); 57771239cb6SGreg Roach return false; 57871239cb6SGreg Roach } 57971239cb6SGreg Roach dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date); 58071239cb6SGreg Roach 58171239cb6SGreg Roach return false; 58271239cb6SGreg Roach} 58371239cb6SGreg Roach 58471239cb6SGreg Roachfunction cal_dateClicked(dateFieldId, dateDivId, year, month, day) 58571239cb6SGreg Roach{ 58671239cb6SGreg Roach cal_setDateField(dateFieldId, year, month, day); 58771239cb6SGreg Roach calendarWidget(dateDivId, dateFieldId); 58871239cb6SGreg Roach return false; 58971239cb6SGreg Roach} 59071239cb6SGreg Roach 59171239cb6SGreg Roachfunction openerpasteid(id) 59271239cb6SGreg Roach{ 59371239cb6SGreg Roach if (window.opener.paste_id) { 59471239cb6SGreg Roach window.opener.paste_id(id); 59571239cb6SGreg Roach } 59671239cb6SGreg Roach window.close(); 59771239cb6SGreg Roach} 59871239cb6SGreg Roach 59971239cb6SGreg Roachfunction paste_id(value) 60071239cb6SGreg Roach{ 60171239cb6SGreg Roach pastefield.value = value; 60271239cb6SGreg Roach} 60371239cb6SGreg Roach 60471239cb6SGreg Roachfunction pastename(name) 60571239cb6SGreg Roach{ 60671239cb6SGreg Roach if (nameElement) { 60771239cb6SGreg Roach nameElement.innerHTML = name; 60871239cb6SGreg Roach } 60971239cb6SGreg Roach if (remElement) { 61071239cb6SGreg Roach remElement.style.display = 'block'; 61171239cb6SGreg Roach } 61271239cb6SGreg Roach} 61371239cb6SGreg Roach 61471239cb6SGreg Roachfunction paste_char(value) 61571239cb6SGreg Roach{ 61671239cb6SGreg Roach if (document.selection) { 61771239cb6SGreg Roach // IE 61871239cb6SGreg Roach pastefield.focus(); 61971239cb6SGreg Roach document.selection.createRange().text = value; 62071239cb6SGreg Roach } else if (pastefield.selectionStart || pastefield.selectionStart === 0) { 62171239cb6SGreg Roach // Mozilla/Chrome/Safari 62271239cb6SGreg Roach pastefield.value = 62371239cb6SGreg Roach pastefield.value.substring(0, pastefield.selectionStart) + 62471239cb6SGreg Roach value + 62571239cb6SGreg Roach pastefield.value.substring(pastefield.selectionEnd, pastefield.value.length); 62671239cb6SGreg Roach pastefield.selectionStart = pastefield.selectionEnd = pastefield.selectionStart + value.length; 62771239cb6SGreg Roach } else { 62871239cb6SGreg Roach // Fallback? - just append 62971239cb6SGreg Roach pastefield.value += value; 63071239cb6SGreg Roach } 63171239cb6SGreg Roach 63271239cb6SGreg Roach if (pastefield.id === 'NPFX' || pastefield.id === 'GIVN' || pastefield.id === 'SPFX' || pastefield.id === 'SURN' || pastefield.id === 'NSFX') { 63371239cb6SGreg Roach updatewholename(); 63471239cb6SGreg Roach } 63571239cb6SGreg Roach} 63671239cb6SGreg Roach 63771239cb6SGreg Roach/** 63871239cb6SGreg Roach * Persistant checkbox options to hide/show extra data. 63971239cb6SGreg Roach 64064490ee2SGreg Roach * @param element_id 64171239cb6SGreg Roach */ 64264490ee2SGreg Roachfunction persistent_toggle(element_id) 64371239cb6SGreg Roach{ 64464490ee2SGreg Roach let element = document.getElementById(element_id); 64564490ee2SGreg Roach let key = 'state-of-' + element_id; 64664490ee2SGreg Roach let state = localStorage.getItem(key); 64771239cb6SGreg Roach 64864490ee2SGreg Roach // Previously selected? 64964490ee2SGreg Roach if (state === 'true') { 65064490ee2SGreg Roach $(element).click(); 65171239cb6SGreg Roach } 65271239cb6SGreg Roach 65364490ee2SGreg Roach // Remember state for the next page load. 65464490ee2SGreg Roach $(element).on('change', function() { localStorage.setItem(key, element.checked); }); 65571239cb6SGreg Roach} 65671239cb6SGreg Roach 65771239cb6SGreg Roachfunction valid_lati_long(field, pos, neg) 65871239cb6SGreg Roach{ 65971239cb6SGreg Roach // valid LATI or LONG according to Gedcom standard 66071239cb6SGreg Roach // pos (+) : N or E 66171239cb6SGreg Roach // neg (-) : S or W 66271239cb6SGreg Roach var txt = field.value.toUpperCase(); 66371239cb6SGreg Roach txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim 66471239cb6SGreg Roach txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34 66571239cb6SGreg Roach txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234 66671239cb6SGreg Roach txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698 66771239cb6SGreg Roach txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698 66871239cb6SGreg Roach // 0°34'11 ==> 0:34:11 66971239cb6SGreg Roach txt = txt.replace(/\u00b0/g, ':'); // ° 67071239cb6SGreg Roach txt = txt.replace(/\u0027/g, ':'); // ' 67171239cb6SGreg Roach // 0:34:11.2W ==> W0.5698 67271239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) { 67371239cb6SGreg Roach var n = parseFloat($1); 67471239cb6SGreg Roach n += ($2 / 60); 67571239cb6SGreg Roach n += ($3 / 3600); 67671239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 67771239cb6SGreg Roach return $4 + n; 67871239cb6SGreg Roach }); 67971239cb6SGreg Roach // 0:34W ==> W0.5667 68071239cb6SGreg Roach txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) { 68171239cb6SGreg Roach var n = parseFloat($1); 68271239cb6SGreg Roach n += ($2 / 60); 68371239cb6SGreg Roach n = Math.round(n * 1E4) / 1E4; 68471239cb6SGreg Roach return $3 + n; 68571239cb6SGreg Roach }); 68671239cb6SGreg Roach // 0.5698W ==> W0.5698 68771239cb6SGreg Roach txt = txt.replace(/(.*)([N|S|E|W]+)$/g, '$2$1'); 68871239cb6SGreg Roach // 17.1234 ==> N17.1234 68971239cb6SGreg Roach if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) { 69071239cb6SGreg Roach txt = pos + txt; 69171239cb6SGreg Roach } 69271239cb6SGreg Roach field.value = txt; 69371239cb6SGreg Roach} 69471239cb6SGreg Roach 69571239cb6SGreg Roach// This is the default way for webtrees to show image galleries. 69671239cb6SGreg Roach// Custom themes may use a different viewer. 69771239cb6SGreg Roachfunction activate_colorbox(config) 69871239cb6SGreg Roach{ 69971239cb6SGreg Roach $.extend($.colorbox.settings, { 70071239cb6SGreg Roach // Don't scroll window with document 70171239cb6SGreg Roach fixed: true, 70271239cb6SGreg Roach current: '', 70371239cb6SGreg Roach previous: '\uf048', 70471239cb6SGreg Roach next: '\uf051', 70571239cb6SGreg Roach slideshowStart: '\uf04b', 70671239cb6SGreg Roach slideshowStop: '\uf04c', 70771239cb6SGreg Roach close: '\uf00d' 70871239cb6SGreg Roach }); 70971239cb6SGreg Roach if (config) { 71071239cb6SGreg Roach $.extend($.colorbox.settings, config); 71171239cb6SGreg Roach } 71271239cb6SGreg Roach 71371239cb6SGreg Roach // Trigger an event when we click on an (any) image 71471239cb6SGreg Roach $('body').on('click', 'a.gallery', function () { 71571239cb6SGreg Roach // Enable colorbox for images 71671239cb6SGreg Roach $('a[type^=image].gallery').colorbox({ 71771239cb6SGreg Roach photo: true, 71871239cb6SGreg Roach maxWidth: '95%', 71971239cb6SGreg Roach maxHeight: '95%', 72071239cb6SGreg Roach rel: 'gallery', // Turn all images on the page into a slideshow 72171239cb6SGreg Roach slideshow: true, 72271239cb6SGreg Roach slideshowAuto: false, 72371239cb6SGreg Roach // Add wheelzoom to the displayed image 72471239cb6SGreg Roach onComplete: function () { 72571239cb6SGreg Roach // Disable click on image triggering next image 72671239cb6SGreg Roach // https://github.com/jackmoore/colorbox/issues/668 72771239cb6SGreg Roach $('.cboxPhoto').unbind('click'); 72871239cb6SGreg Roach 72971239cb6SGreg Roach wheelzoom(document.querySelectorAll('.cboxPhoto')); 73071239cb6SGreg Roach } 73171239cb6SGreg Roach }); 73271239cb6SGreg Roach 73371239cb6SGreg Roach // Enable colorbox for audio using <audio></audio>, where supported 73471239cb6SGreg Roach // $('html.video a[type^=video].gallery').colorbox({ 73571239cb6SGreg Roach // rel: 'nofollow' // Slideshows are just for images 73671239cb6SGreg Roach // }); 73771239cb6SGreg Roach 73871239cb6SGreg Roach // Enable colorbox for video using <video></video>, where supported 73971239cb6SGreg Roach // $('html.audio a[type^=audio].gallery').colorbox({ 74071239cb6SGreg Roach // rel: 'nofollow', // Slideshows are just for images 74171239cb6SGreg Roach // }); 74271239cb6SGreg Roach 74371239cb6SGreg Roach // Allow all other media types remain as download links 74471239cb6SGreg Roach }); 74571239cb6SGreg Roach} 74671239cb6SGreg Roach 74771239cb6SGreg Roach// Initialize autocomplete elements. 74871239cb6SGreg Roachfunction autocomplete(selector) 74971239cb6SGreg Roach{ 75071239cb6SGreg Roach // Use typeahead/bloodhound for autocomplete 75171239cb6SGreg Roach $(selector).each(function () { 752f4abaf12SGreg Roach let that = this; 75371239cb6SGreg Roach $(this).typeahead(null, { 75471239cb6SGreg Roach display: 'value', 75571239cb6SGreg Roach source: new Bloodhound({ 75671239cb6SGreg Roach datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 75771239cb6SGreg Roach queryTokenizer: Bloodhound.tokenizers.whitespace, 75871239cb6SGreg Roach remote: { 75971239cb6SGreg Roach url: this.dataset.autocompleteUrl, 760f4abaf12SGreg Roach replace: function(url, uriEncodedQuery) { 761f4abaf12SGreg Roach if (that.dataset.autocompleteExtra) { 762f4abaf12SGreg Roach let extra = $(document.querySelector(that.dataset.autocompleteExtra)).val(); 763f4abaf12SGreg Roach return url.replace("QUERY",uriEncodedQuery) + '&extra=' + encodeURIComponent(extra) 764f4abaf12SGreg Roach } 765f4abaf12SGreg Roach return url.replace("QUERY",uriEncodedQuery); 766f4abaf12SGreg Roach }, 767f4abaf12SGreg Roach wildcard: 'QUERY', 768f4abaf12SGreg Roach 76971239cb6SGreg Roach } 77071239cb6SGreg Roach }) 77171239cb6SGreg Roach }); 77271239cb6SGreg Roach }); 77371239cb6SGreg Roach} 77471239cb6SGreg Roach 77571239cb6SGreg Roach/** 77671239cb6SGreg Roach * Insert text at the current cursor position in an input field. 77771239cb6SGreg Roach * 77871239cb6SGreg Roach * @param e The input element. 77971239cb6SGreg Roach * @param t The text to insert. 78071239cb6SGreg Roach */ 78171239cb6SGreg Roachfunction insertTextAtCursor(e, t) 78271239cb6SGreg Roach{ 78371239cb6SGreg Roach var scrollTop = e.scrollTop; 78471239cb6SGreg Roach var selectionStart = e.selectionStart; 78571239cb6SGreg Roach var prefix = e.value.substring(0, selectionStart); 78671239cb6SGreg Roach var suffix = e.value.substring(e.selectionEnd, e.value.length); 78771239cb6SGreg Roach e.value = prefix + t + suffix; 78871239cb6SGreg Roach e.selectionStart = selectionStart + t.length; 78971239cb6SGreg Roach e.selectionEnd = e.selectionStart; 79071239cb6SGreg Roach e.focus(); 79171239cb6SGreg Roach e.scrollTop = scrollTop; 79271239cb6SGreg Roach} 79371239cb6SGreg Roach 79488de55fdSRico Sonntag 79588de55fdSRico Sonntag/** 79688de55fdSRico Sonntag * Draws a google pie chart. 79788de55fdSRico Sonntag * 79888de55fdSRico Sonntag * @param {String} elementId The element id of the HTML element the chart is rendered too 79988de55fdSRico Sonntag * @param {Array} data The chart data array 80088de55fdSRico Sonntag * @param {Array} colors The chart color array 80188de55fdSRico Sonntag * @param {String} title The chart title 80288de55fdSRico Sonntag * @param {String} labeledValueText The type of how to display the slice text 80388de55fdSRico Sonntag */ 80488de55fdSRico Sonntagfunction drawPieChart(elementId, data, colors, title, labeledValueText) 80588de55fdSRico Sonntag{ 80688de55fdSRico Sonntag var data = google.visualization.arrayToDataTable(data); 80788de55fdSRico Sonntag var options = { 80888de55fdSRico Sonntag title: title, 80988de55fdSRico Sonntag height: '100%', 81088de55fdSRico Sonntag width: '100%', 81188de55fdSRico Sonntag pieStartAngle: 0, 81288de55fdSRico Sonntag pieSliceText: 'none', 81388de55fdSRico Sonntag pieSliceTextStyle: { 81488de55fdSRico Sonntag color: '#777' 81588de55fdSRico Sonntag }, 81688de55fdSRico Sonntag pieHole: 0.4, // Donut 81788de55fdSRico Sonntag //is3D: true, // 3D (not together with pieHole) 81888de55fdSRico Sonntag legend: { 81988de55fdSRico Sonntag alignment: 'center', 82088de55fdSRico Sonntag // Flickers on mouseover :( 82188de55fdSRico Sonntag labeledValueText: labeledValueText || 'value', 82288de55fdSRico Sonntag position: 'labeled' 82388de55fdSRico Sonntag }, 82488de55fdSRico Sonntag chartArea: { 82588de55fdSRico Sonntag left: 0, 82688de55fdSRico Sonntag top: '5%', 82788de55fdSRico Sonntag height: '90%', 82888de55fdSRico Sonntag width: '100%' 82988de55fdSRico Sonntag }, 83088de55fdSRico Sonntag tooltip: { 83188de55fdSRico Sonntag trigger: 'none', 83288de55fdSRico Sonntag text: 'both' 83388de55fdSRico Sonntag }, 83488de55fdSRico Sonntag backgroundColor: 'transparent', 83588de55fdSRico Sonntag colors: colors 83688de55fdSRico Sonntag }; 83788de55fdSRico Sonntag 83888de55fdSRico Sonntag var chart = new google.visualization.PieChart(document.getElementById(elementId)); 83988de55fdSRico Sonntag 84088de55fdSRico Sonntag chart.draw(data, options); 84188de55fdSRico Sonntag} 84288de55fdSRico Sonntag 84388de55fdSRico Sonntag/** 84488de55fdSRico Sonntag * Draws a google column chart. 84588de55fdSRico Sonntag * 84688de55fdSRico Sonntag * @param {String} elementId The element id of the HTML element the chart is rendered too 84788de55fdSRico Sonntag * @param {Array} data The chart data array 84888de55fdSRico Sonntag * @param {Object} options The chart specific options to overwrite the default ones 84988de55fdSRico Sonntag */ 85088de55fdSRico Sonntagfunction drawColumnChart(elementId, data, options) 85188de55fdSRico Sonntag{ 85288de55fdSRico Sonntag var defaults = { 85388de55fdSRico Sonntag title: '', 85488de55fdSRico Sonntag subtitle: '', 855a81e5019SRico Sonntag titleTextStyle: { 856a81e5019SRico Sonntag color: '#757575', 857a81e5019SRico Sonntag fontName: 'Roboto', 858a81e5019SRico Sonntag fontSize: '16px', 859a81e5019SRico Sonntag bold: false, 860a81e5019SRico Sonntag italic: false 861a81e5019SRico Sonntag }, 86288de55fdSRico Sonntag height: '100%', 86388de55fdSRico Sonntag width: '100%', 86488de55fdSRico Sonntag vAxis: { 86588de55fdSRico Sonntag title: '' 86688de55fdSRico Sonntag }, 86788de55fdSRico Sonntag hAxis: { 86888de55fdSRico Sonntag title: '' 86988de55fdSRico Sonntag }, 87088de55fdSRico Sonntag legend: { 87188de55fdSRico Sonntag position: 'none' 87288de55fdSRico Sonntag }, 87388de55fdSRico Sonntag backgroundColor: 'transparent' 87488de55fdSRico Sonntag }; 87588de55fdSRico Sonntag 87688de55fdSRico Sonntag options = Object.assign(defaults, options); 87788de55fdSRico Sonntag 87888de55fdSRico Sonntag var chart = new google.visualization.ColumnChart(document.getElementById(elementId)); 87988de55fdSRico Sonntag var data = google.visualization.arrayToDataTable(data); 88088de55fdSRico Sonntag 88188de55fdSRico Sonntag chart.draw(data, options); 88288de55fdSRico Sonntag} 88388de55fdSRico Sonntag 88488de55fdSRico Sonntag/** 88588de55fdSRico Sonntag * Draws a google combo chart. 88688de55fdSRico Sonntag * 88788de55fdSRico Sonntag * @param {String} elementId The element id of the HTML element the chart is rendered too 88888de55fdSRico Sonntag * @param {Array} data The chart data array 88988de55fdSRico Sonntag * @param {Object} options The chart specific options to overwrite the default ones 89088de55fdSRico Sonntag */ 89188de55fdSRico Sonntagfunction drawComboChart(elementId, data, options) 89288de55fdSRico Sonntag{ 89388de55fdSRico Sonntag var defaults = { 89488de55fdSRico Sonntag title: '', 89588de55fdSRico Sonntag subtitle: '', 89688de55fdSRico Sonntag titleTextStyle: { 89788de55fdSRico Sonntag color: '#757575', 89888de55fdSRico Sonntag fontName: 'Roboto', 89988de55fdSRico Sonntag fontSize: '16px', 90088de55fdSRico Sonntag bold: false, 90188de55fdSRico Sonntag italic: false 90288de55fdSRico Sonntag }, 90388de55fdSRico Sonntag height: '100%', 90488de55fdSRico Sonntag width: '100%', 90588de55fdSRico Sonntag vAxis: { 90688de55fdSRico Sonntag title: '' 90788de55fdSRico Sonntag }, 90888de55fdSRico Sonntag hAxis: { 90988de55fdSRico Sonntag title: '' 91088de55fdSRico Sonntag }, 91188de55fdSRico Sonntag legend: { 91288de55fdSRico Sonntag position: 'none' 91388de55fdSRico Sonntag }, 91488de55fdSRico Sonntag seriesType: 'bars', 91588de55fdSRico Sonntag series: { 91688de55fdSRico Sonntag 2: { 91788de55fdSRico Sonntag type: 'line' 91888de55fdSRico Sonntag } 91988de55fdSRico Sonntag }, 92088de55fdSRico Sonntag colors: [], 92188de55fdSRico Sonntag backgroundColor: 'transparent' 92288de55fdSRico Sonntag }; 92388de55fdSRico Sonntag 92488de55fdSRico Sonntag options = Object.assign(defaults, options); 92588de55fdSRico Sonntag 92688de55fdSRico Sonntag var chart = new google.visualization.ComboChart(document.getElementById(elementId)); 92788de55fdSRico Sonntag var data = google.visualization.arrayToDataTable(data); 92888de55fdSRico Sonntag 92988de55fdSRico Sonntag chart.draw(data, options); 93088de55fdSRico Sonntag} 93188de55fdSRico Sonntag 932a81e5019SRico Sonntag/** 933a81e5019SRico Sonntag * Draws a google geo chart. 934a81e5019SRico Sonntag * 935a81e5019SRico Sonntag * @param {String} elementId The element id of the HTML element the chart is rendered too 936a81e5019SRico Sonntag * @param {Array} data The chart data array 937a81e5019SRico Sonntag * @param {Object} options The chart specific options to overwrite the default ones 938a81e5019SRico Sonntag */ 939a81e5019SRico Sonntagfunction drawGeoChart(elementId, data, options) 940a81e5019SRico Sonntag{ 941a81e5019SRico Sonntag var defaults = { 942a81e5019SRico Sonntag title: '', 943a81e5019SRico Sonntag subtitle: '', 944a81e5019SRico Sonntag height: '100%', 945a81e5019SRico Sonntag width: '100%' 946a81e5019SRico Sonntag }; 947a81e5019SRico Sonntag 948a81e5019SRico Sonntag options = Object.assign(defaults, options); 949a81e5019SRico Sonntag 950a81e5019SRico Sonntag var chart = new google.visualization.GeoChart(document.getElementById(elementId)); 951a81e5019SRico Sonntag var data = google.visualization.arrayToDataTable(data); 952a81e5019SRico Sonntag 953a81e5019SRico Sonntag chart.draw(data, options); 954a81e5019SRico Sonntag} 95588de55fdSRico Sonntag 95671239cb6SGreg Roach// Send the CSRF token on all AJAX requests 95771239cb6SGreg Roach$.ajaxSetup({ 95871239cb6SGreg Roach headers: { 95971239cb6SGreg Roach 'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content') 96071239cb6SGreg Roach } 96171239cb6SGreg Roach}); 96271239cb6SGreg Roach 96371239cb6SGreg Roach// Initialisation 96471239cb6SGreg Roach$(function () { 96571239cb6SGreg Roach // Page elements that load automaticaly via AJAX. 96671239cb6SGreg Roach // This prevents bad robots from crawling resource-intensive pages. 96771239cb6SGreg Roach $("[data-ajax-url]").each(function () { 96871239cb6SGreg Roach $(this).load($(this).data('ajaxUrl')); 96971239cb6SGreg Roach }); 97071239cb6SGreg Roach 97171239cb6SGreg Roach // Select2 - format entries in the select list 97271239cb6SGreg Roach function templateOptionForSelect2(data) 97371239cb6SGreg Roach { 97471239cb6SGreg Roach if (data.loading) { 97571239cb6SGreg Roach // If we're waiting for the server, this will be a "waiting..." message 97671239cb6SGreg Roach return data.text; 97771239cb6SGreg Roach } else { 97871239cb6SGreg Roach // The response from the server is already in HTML, so no need to format it here. 97971239cb6SGreg Roach return data.text; 98071239cb6SGreg Roach } 98171239cb6SGreg Roach } 98271239cb6SGreg Roach 98371239cb6SGreg Roach // Autocomplete 98471239cb6SGreg Roach autocomplete('input[data-autocomplete-url]'); 98571239cb6SGreg Roach 98671239cb6SGreg Roach // Select2 - activate autocomplete fields 987*bdbdb10cSGreg Roach const lang = document.documentElement.lang; 988*bdbdb10cSGreg Roach const select2_languages = { 989*bdbdb10cSGreg Roach 'zh-Hans': 'zh-CN', 990*bdbdb10cSGreg Roach 'zh-Hant': 'zh-TW', 991*bdbdb10cSGreg Roach }; 9928ec20abdSGreg Roach $("select.select2").select2({ 993*bdbdb10cSGreg Roach language: select2_languages[lang] || lang, 9948ec20abdSGreg Roach width: "100%", 99571239cb6SGreg Roach // Do not escape. 99671239cb6SGreg Roach escapeMarkup: function (x) { 9978ec20abdSGreg Roach return x; 9988ec20abdSGreg Roach }, 99971239cb6SGreg Roach // Same formatting for both selections and rsult 100071239cb6SGreg Roach //templateResult: templateOptionForSelect2, 100171239cb6SGreg Roach //templateSelection: templateOptionForSelect2 1002*bdbdb10cSGreg Roach }); 100371239cb6SGreg Roach 100471239cb6SGreg Roach // Datatables - locale aware sorting 100571239cb6SGreg Roach $.fn.dataTableExt.oSort['text-asc'] = function (x, y) { 100671239cb6SGreg Roach return x.localeCompare(y, document.documentElement.lang, {'sensitivity': 'base'}); 100771239cb6SGreg Roach }; 100871239cb6SGreg Roach $.fn.dataTableExt.oSort['text-desc'] = function (x, y) { 100971239cb6SGreg Roach return y.localeCompare(x, document.documentElement.lang, {'sensitivity': 'base'}); 101071239cb6SGreg Roach }; 101171239cb6SGreg Roach 101271239cb6SGreg Roach // DataTables - start hidden to prevent FOUC. 101371239cb6SGreg Roach $('table.datatables').each(function () { 101471239cb6SGreg Roach $(this).DataTable(); $(this).removeClass('d-none'); }); 101571239cb6SGreg Roach 101671239cb6SGreg Roach // Create a new record while editing an existing one. 101771239cb6SGreg Roach // Paste the XREF and description into the Select2 element. 101871239cb6SGreg Roach $('.wt-modal-create-record').on('show.bs.modal', function (event) { 101971239cb6SGreg Roach // Find the element ID that needs to be updated with the new value. 102071239cb6SGreg Roach $('form', $(this)).data('element-id', $(event.relatedTarget).data('element-id')); 102171239cb6SGreg Roach $('form .form-group input:first', $(this)).focus(); 102271239cb6SGreg Roach }); 102371239cb6SGreg Roach 102471239cb6SGreg Roach // Submit the modal form using AJAX, and paste the returned record ID/NAME into the parent form. 102571239cb6SGreg Roach $('.wt-modal-create-record form').on('submit', function (event) { 102671239cb6SGreg Roach event.preventDefault(); 102771239cb6SGreg Roach var elementId = $(this).data('element-id'); 102871239cb6SGreg Roach $.ajax({ 102971239cb6SGreg Roach url: 'index.php', 103071239cb6SGreg Roach type: 'POST', 103171239cb6SGreg Roach data: new FormData(this), 103271239cb6SGreg Roach async: false, 103371239cb6SGreg Roach cache: false, 103471239cb6SGreg Roach contentType: false, 103571239cb6SGreg Roach processData: false, 103671239cb6SGreg Roach success: function (data) { 103771239cb6SGreg Roach $('#' + elementId).select2().empty().append(new Option(data.text, data.id)).val(data.id).trigger('change'); 103871239cb6SGreg Roach }, 103971239cb6SGreg Roach failure: function (data) { 104071239cb6SGreg Roach alert(data.error_message); 104171239cb6SGreg Roach } 104271239cb6SGreg Roach }); 104371239cb6SGreg Roach // Clear the form 104471239cb6SGreg Roach this.reset(); 104571239cb6SGreg Roach // Close the modal 104671239cb6SGreg Roach $(this).closest('.wt-modal-create-record').modal('hide'); 104771239cb6SGreg Roach }); 104871239cb6SGreg Roach 104971239cb6SGreg Roach // Activate the langauge selection menu. 105071239cb6SGreg Roach $('.menu-language').on('click', '[data-language]', function () { 10511bd3adbdSGreg Roach $.post('index.php?route=language', { 105271239cb6SGreg Roach language: $(this).data('language') 105371239cb6SGreg Roach }, function () { 1054070932ceSGreg Roach document.location.reload(); 105571239cb6SGreg Roach }); 105671239cb6SGreg Roach 105771239cb6SGreg Roach return false; 105871239cb6SGreg Roach }); 105971239cb6SGreg Roach 106071239cb6SGreg Roach // Activate the theme selection menu. 106171239cb6SGreg Roach $('.menu-theme').on('click', '[data-theme]', function () { 10621bd3adbdSGreg Roach $.post('index.php?route=theme', { 106371239cb6SGreg Roach theme: $(this).data('theme') 106471239cb6SGreg Roach }, function () { 1065070932ceSGreg Roach document.location.reload(); 106671239cb6SGreg Roach }); 106771239cb6SGreg Roach 106871239cb6SGreg Roach return false; 106971239cb6SGreg Roach }); 107071239cb6SGreg Roach 107171239cb6SGreg Roach // Activate the on-screen keyboard 107271239cb6SGreg Roach var osk_focus_element; 107371239cb6SGreg Roach $('.wt-osk-trigger').click(function () { 107471239cb6SGreg Roach // When a user clicks the icon, set focus to the corresponding input 107571239cb6SGreg Roach osk_focus_element = document.getElementById($(this).data('id')); 107671239cb6SGreg Roach osk_focus_element.focus(); 107771239cb6SGreg Roach $('.wt-osk').show(); 107871239cb6SGreg Roach 107971239cb6SGreg Roach }); 108071239cb6SGreg Roach 108171239cb6SGreg Roach $('.wt-osk-script-button').change(function () { 108271239cb6SGreg Roach $('.wt-osk-script').prop('hidden', true); 108371239cb6SGreg Roach $('.wt-osk-script-' + $(this).data('script')).prop('hidden', false); 108471239cb6SGreg Roach }); 108571239cb6SGreg Roach $('.wt-osk-shift-button').click(function () { 108671239cb6SGreg Roach document.querySelector('.wt-osk-keys').classList.toggle('shifted'); 108771239cb6SGreg Roach }); 108871239cb6SGreg Roach $('.wt-osk-keys').on('click', '.wt-osk-key', function () { 108971239cb6SGreg Roach var key = $(this).contents().get(0).nodeValue; 109071239cb6SGreg Roach var shift_state = $('.wt-osk-shift-button').hasClass('active'); 109171239cb6SGreg Roach var shift_key = $('sup', this)[0]; 109271239cb6SGreg Roach if (shift_state && shift_key !== undefined) { 109371239cb6SGreg Roach key = shift_key.innerText; 109471239cb6SGreg Roach } 109571239cb6SGreg Roach if (osk_focus_element !== null) { 109671239cb6SGreg Roach var cursorPos = osk_focus_element.selectionStart; 109771239cb6SGreg Roach var v = osk_focus_element.value; 109871239cb6SGreg Roach var textBefore = v.substring(0, cursorPos); 109971239cb6SGreg Roach var textAfter = v.substring(cursorPos, v.length); 110071239cb6SGreg Roach osk_focus_element.value = textBefore + key + textAfter; 110171239cb6SGreg Roach if ($('.wt-osk-pin-button').hasClass('active') === false) { 110271239cb6SGreg Roach $('.wt-osk').hide(); 110371239cb6SGreg Roach } 110471239cb6SGreg Roach } 110571239cb6SGreg Roach }); 110671239cb6SGreg Roach 110771239cb6SGreg Roach $('.wt-osk-close').on('click', function () { 110871239cb6SGreg Roach $('.wt-osk').hide(); 110971239cb6SGreg Roach }); 111071239cb6SGreg Roach}); 1111