1/** 2 * webtrees: online genealogy 3 * Copyright (C) 2019 webtrees development team 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * You should have received a copy of the GNU General Public License 13 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 */ 15 16"use strict"; 17 18(function(webtrees) { 19 const lang = document.documentElement.lang; 20 21 // Identify the script used by some text. 22 const scriptRegexes = { 23 Han: /[\u3400-\u9FCC]/, 24 Grek: /[\u0370-\u03FF]/, 25 Cyrl: /[\u0400-\u04FF]/, 26 Hebr: /[\u0590-\u05FF]/, 27 Arab: /[\u0600-\u06FF]/, 28 }; 29 30 /** 31 * Tidy the whitespace in a string. 32 */ 33 function trim(str) { 34 return str.replace(/\s+/g, " ").trim(); 35 36 } 37 38 /** 39 * Look for non-latin characters in a string. 40 */ 41 webtrees.detectScript = function (str) { 42 for(const script in scriptRegexes) { 43 if (str.match(scriptRegexes[script])) { 44 return script; 45 } 46 } 47 48 return "Latn"; 49 }; 50 51 /** 52 * In some languages, the SURN uses a male/default form, but NAME uses a gender-inflected form. 53 */ 54 function inflectSurname(surname, sex) { 55 if (lang === "pl" && sex === "F") { 56 return surname 57 .replace(/ski$/, "ska") 58 .replace(/cki$/, "cka") 59 .replace(/dzki$/, "dzka") 60 .replace(/żki$/, "żka"); 61 } 62 63 return surname; 64 } 65 66 /** 67 * Build a NAME from a NPFX, GIVN, SPFX, SURN and NSFX parts. 68 * 69 * Assumes the language of the document is the same as the language of the name. 70 */ 71 webtrees.buildNameFromParts = function (npfx, givn, spfx, surn, nsfx, sex) { 72 const usesCJK = webtrees.detectScript(npfx + givn + spfx + givn + surn + nsfx) === "Han"; 73 const separator = usesCJK ? "" : " "; 74 const surnameFirst = usesCJK || ['hu', 'jp', 'ko', 'vi', 'zh-Hans', 'zh-Hant'].indexOf(lang) !== -1; 75 const patronym = ['is'].indexOf(lang) !== -1; 76 const slash = patronym ? "" : "/"; 77 78 // GIVN and SURN may be a comma-separated lists. 79 npfx = trim(npfx); 80 givn = trim(givn.replace(",", separator)); 81 spfx = trim(spfx); 82 surn = inflectSurname(trim(surn.replace(",", separator)), sex); 83 nsfx = trim(nsfx); 84 85 const surname = trim(spfx + separator + surn); 86 87 const name = surnameFirst ? slash + surname + slash + separator + givn : givn + separator + slash + surname + slash; 88 89 return trim(npfx + separator + name + separator + nsfx); 90 }; 91 92 // Insert text at the current cursor position in a text field. 93 webtrees.pasteAtCursor = function (element, text) { 94 if (element !== null) { 95 const caret_pos = element.selectionStart + text.length; 96 const textBefore = element.value.substring(0, element.selectionStart); 97 const textAfter = element.value.substring(element.selectionEnd); 98 element.value = textBefore + text + textAfter; 99 element.setSelectionRange(caret_pos, caret_pos); 100 element.focus(); 101 } 102 103 } 104}(window.webtrees = window.webtrees || {})); 105 106function expand_layer(sid) 107{ 108 $('#' + sid + '_img').toggleClass('icon-plus icon-minus'); 109 $('#' + sid).slideToggle('fast'); 110 $('#' + sid + '-alt').toggle(); // hide something when we show the layer - and vice-versa 111 return false; 112} 113 114var pastefield; 115 116function valid_date(datefield, dmy) 117{ 118 var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']; 119 var hijri_months = ['MUHAR', 'SAFAR', 'RABIA', 'RABIT', 'JUMAA', 'JUMAT', 'RAJAB', 'SHAAB', 'RAMAD', 'SHAWW', 'DHUAQ', 'DHUAH']; 120 var hebrew_months = ['TSH', 'CSH', 'KSL', 'TVT', 'SHV', 'ADR', 'ADS', 'NSN', 'IYR', 'SVN', 'TMZ', 'AAV', 'ELL']; 121 var french_months = ['VEND', 'BRUM', 'FRIM', 'NIVO', 'PLUV', 'VENT', 'GERM', 'FLOR', 'PRAI', 'MESS', 'THER', 'FRUC', 'COMP']; 122 var jalali_months = ['FARVA', 'ORDIB', 'KHORD', 'TIR', 'MORDA', 'SHAHR', 'MEHR', 'ABAN', 'AZAR', 'DEY', 'BAHMA', 'ESFAN']; 123 124 var datestr = datefield.value; 125 // if a date has a date phrase marked by () this has to be excluded from altering 126 var datearr = datestr.split('('); 127 var datephrase = ''; 128 if (datearr.length > 1) { 129 datestr = datearr[0]; 130 datephrase = datearr[1]; 131 } 132 133 // Gedcom dates are upper case 134 datestr = datestr.toUpperCase(); 135 // Gedcom dates have no leading/trailing/repeated whitespace 136 datestr = datestr.replace(/\s+/, ' '); 137 datestr = datestr.replace(/(^\s)|(\s$)/, ''); 138 // Gedcom dates have spaces between letters and digits, e.g. "01JAN2000" => "01 JAN 2000" 139 datestr = datestr.replace(/(\d)([A-Z])/, '$1 $2'); 140 datestr = datestr.replace(/([A-Z])(\d)/, '$1 $2'); 141 142 // Shortcut for quarter format, "Q1 1900" => "BET JAN 1900 AND MAR 1900". See [ 1509083 ] 143 if (datestr.match(/^Q ([1-4]) (\d\d\d\d)$/)) { 144 datestr = 'BET ' + months[RegExp.$1 * 3 - 3] + ' ' + RegExp.$2 + ' AND ' + months[RegExp.$1 * 3 - 1] + ' ' + RegExp.$2; 145 } 146 147 // Shortcut for @#Dxxxxx@ 01 01 1400, etc. 148 if (datestr.match(/^(@#DHIJRI@|HIJRI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 149 datestr = '@#DHIJRI@' + RegExp.$2 + hijri_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 150 } 151 if (datestr.match(/^(@#DJALALI@|JALALI)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 152 datestr = '@#DJALALI@' + RegExp.$2 + jalali_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 153 } 154 if (datestr.match(/^(@#DHEBREW@|HEBREW)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 155 datestr = '@#DHEBREW@' + RegExp.$2 + hebrew_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 156 } 157 if (datestr.match(/^(@#DFRENCH R@|FRENCH)( \d?\d )(\d?\d)( \d?\d?\d?\d)$/)) { 158 datestr = '@#DFRENCH R@' + RegExp.$2 + french_months[parseInt(RegExp.$3, 10) - 1] + RegExp.$4; 159 } 160 161 // e.g. 17.11.1860, 03/04/2005 or 1999-12-31. Use locale settings where DMY order is ambiguous. 162 var qsearch = /^([^\d]*)(\d+)[^\d](\d+)[^\d](\d+)$/i; 163 if (qsearch.exec(datestr)) { 164 var f0 = RegExp.$1; 165 var f1 = parseInt(RegExp.$2, 10); 166 var f2 = parseInt(RegExp.$3, 10); 167 var f3 = parseInt(RegExp.$4, 10); 168 var yyyy = new Date().getFullYear(); 169 var yy = yyyy % 100; 170 var cc = yyyy - yy; 171 if (dmy === 'DMY' && f1 <= 31 && f2 <= 12 || f1 > 13 && f1 <= 31 && f2 <= 12 && f3 > 31) { 172 datestr = f0 + f1 + ' ' + months[f2 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 173 } else { 174 if (dmy === 'MDY' && f1 <= 12 && f2 <= 31 || f2 > 13 && f2 <= 31 && f1 <= 12 && f3 > 31) { 175 datestr = f0 + f2 + ' ' + months[f1 - 1] + ' ' + (f3 >= 100 ? f3 : (f3 <= yy ? f3 + cc : f3 + cc - 100)); 176 } else { 177 if (dmy === 'YMD' && f2 <= 12 && f3 <= 31 || f3 > 13 && f3 <= 31 && f2 <= 12 && f1 > 31) { 178 datestr = f0 + f3 + ' ' + months[f2 - 1] + ' ' + (f1 >= 100 ? f1 : (f1 <= yy ? f1 + cc : f1 + cc - 100)); 179 } 180 } 181 } 182 } 183 184 // Shortcuts for date ranges 185 datestr = datestr.replace(/^[>]([\w ]+)$/, 'AFT $1'); 186 datestr = datestr.replace(/^[<]([\w ]+)$/, 'BEF $1'); 187 datestr = datestr.replace(/^([\w ]+)[-]$/, 'FROM $1'); 188 datestr = datestr.replace(/^[-]([\w ]+)$/, 'TO $1'); 189 datestr = datestr.replace(/^[~]([\w ]+)$/, 'ABT $1'); 190 datestr = datestr.replace(/^[*]([\w ]+)$/, 'EST $1'); 191 datestr = datestr.replace(/^[#]([\w ]+)$/, 'CAL $1'); 192 datestr = datestr.replace(/^([\w ]+) ?- ?([\w ]+)$/, 'BET $1 AND $2'); 193 datestr = datestr.replace(/^([\w ]+) ?~ ?([\w ]+)$/, 'FROM $1 TO $2'); 194 195 // Convert full months to short months 196 datestr = datestr.replace(/(JANUARY)/, 'JAN'); 197 datestr = datestr.replace(/(FEBRUARY)/, 'FEB'); 198 datestr = datestr.replace(/(MARCH)/, 'MAR'); 199 datestr = datestr.replace(/(APRIL)/, 'APR'); 200 datestr = datestr.replace(/(MAY)/, 'MAY'); 201 datestr = datestr.replace(/(JUNE)/, 'JUN'); 202 datestr = datestr.replace(/(JULY)/, 'JUL'); 203 datestr = datestr.replace(/(AUGUST)/, 'AUG'); 204 datestr = datestr.replace(/(SEPTEMBER)/, 'SEP'); 205 datestr = datestr.replace(/(OCTOBER)/, 'OCT'); 206 datestr = datestr.replace(/(NOVEMBER)/, 'NOV'); 207 datestr = datestr.replace(/(DECEMBER)/, 'DEC'); 208 209 // Americans frequently enter dates as SEP 20, 1999 210 // No need to internationalise this, as this is an english-language issue 211 datestr = datestr.replace(/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\.? (\d\d?)[, ]+(\d\d\d\d)/, '$2 $1 $3'); 212 213 // Apply leading zero to day numbers 214 datestr = datestr.replace(/(^| )(\d [A-Z]{3,5} \d{4})/, '$10$2'); 215 216 if (datephrase) { 217 datestr = datestr + ' (' + datephrase; 218 } 219 // Only update it if is has been corrected - otherwise input focus 220 // moves to the end of the field unnecessarily 221 if (datefield.value !== datestr) { 222 datefield.value = datestr; 223 } 224} 225 226var monthLabels = []; 227monthLabels[1] = 'January'; 228monthLabels[2] = 'February'; 229monthLabels[3] = 'March'; 230monthLabels[4] = 'April'; 231monthLabels[5] = 'May'; 232monthLabels[6] = 'June'; 233monthLabels[7] = 'July'; 234monthLabels[8] = 'August'; 235monthLabels[9] = 'September'; 236monthLabels[10] = 'October'; 237monthLabels[11] = 'November'; 238monthLabels[12] = 'December'; 239 240var monthShort = []; 241monthShort[1] = 'JAN'; 242monthShort[2] = 'FEB'; 243monthShort[3] = 'MAR'; 244monthShort[4] = 'APR'; 245monthShort[5] = 'MAY'; 246monthShort[6] = 'JUN'; 247monthShort[7] = 'JUL'; 248monthShort[8] = 'AUG'; 249monthShort[9] = 'SEP'; 250monthShort[10] = 'OCT'; 251monthShort[11] = 'NOV'; 252monthShort[12] = 'DEC'; 253 254var daysOfWeek = []; 255daysOfWeek[0] = 'S'; 256daysOfWeek[1] = 'M'; 257daysOfWeek[2] = 'T'; 258daysOfWeek[3] = 'W'; 259daysOfWeek[4] = 'T'; 260daysOfWeek[5] = 'F'; 261daysOfWeek[6] = 'S'; 262 263var weekStart = 0; 264 265function cal_setMonthNames(jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec) 266{ 267 monthLabels[1] = jan; 268 monthLabels[2] = feb; 269 monthLabels[3] = mar; 270 monthLabels[4] = apr; 271 monthLabels[5] = may; 272 monthLabels[6] = jun; 273 monthLabels[7] = jul; 274 monthLabels[8] = aug; 275 monthLabels[9] = sep; 276 monthLabels[10] = oct; 277 monthLabels[11] = nov; 278 monthLabels[12] = dec; 279} 280 281function cal_setDayHeaders(sun, mon, tue, wed, thu, fri, sat) 282{ 283 daysOfWeek[0] = sun; 284 daysOfWeek[1] = mon; 285 daysOfWeek[2] = tue; 286 daysOfWeek[3] = wed; 287 daysOfWeek[4] = thu; 288 daysOfWeek[5] = fri; 289 daysOfWeek[6] = sat; 290} 291 292function cal_setWeekStart(day) 293{ 294 if (day >= 0 && day < 7) { 295 weekStart = day; 296 } 297} 298 299function calendarWidget(dateDivId, dateFieldId) 300{ 301 var dateDiv = document.getElementById(dateDivId); 302 var dateField = document.getElementById(dateFieldId); 303 304 if (dateDiv.style.visibility === 'visible') { 305 dateDiv.style.visibility = 'hidden'; 306 return false; 307 } 308 if (dateDiv.style.visibility === 'show') { 309 dateDiv.style.visibility = 'hide'; 310 return false; 311 } 312 313 /* Javascript calendar functions only work with precise gregorian dates "D M Y" or "Y" */ 314 var greg_regex = /((\d+ (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) )?\d+)/i; 315 var date; 316 if (greg_regex.exec(dateField.value)) { 317 date = new Date(RegExp.$1); 318 } else { 319 date = new Date(); 320 } 321 322 dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date); 323 if (dateDiv.style.visibility === 'hidden') { 324 dateDiv.style.visibility = 'visible'; 325 return false; 326 } 327 if (dateDiv.style.visibility === 'hide') { 328 dateDiv.style.visibility = 'show'; 329 return false; 330 } 331 332 return false; 333} 334 335function cal_generateSelectorContent(dateFieldId, dateDivId, date) 336{ 337 var i, j; 338 var content = '<table border="1"><tr>'; 339 content += '<td><select class="form-control" id="' + dateFieldId + '_daySelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 340 for (i = 1; i < 32; i++) { 341 content += '<option value="' + i + '"'; 342 if (date.getDate() === i) { 343 content += ' selected="selected"'; 344 } 345 content += '>' + i + '</option>'; 346 } 347 content += '</select></td>'; 348 content += '<td><select class="form-control" id="' + dateFieldId + '_monSelect" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');">'; 349 for (i = 1; i < 13; i++) { 350 content += '<option value="' + i + '"'; 351 if (date.getMonth() + 1 === i) { 352 content += ' selected="selected"'; 353 } 354 content += '>' + monthLabels[i] + '</option>'; 355 } 356 content += '</select></td>'; 357 content += '<td><input class="form-control" type="text" id="' + dateFieldId + '_yearInput" size="5" value="' + date.getFullYear() + '" onchange="return cal_updateCalendar(\'' + dateFieldId + '\', \'' + dateDivId + '\');" /></td></tr>'; 358 content += '<tr><td colspan="3">'; 359 content += '<table width="100%">'; 360 content += '<tr>'; 361 j = weekStart; 362 for (i = 0; i < 7; i++) { 363 content += '<td '; 364 content += 'class="descriptionbox"'; 365 content += '>'; 366 content += daysOfWeek[j]; 367 content += '</td>'; 368 j++; 369 if (j > 6) { 370 j = 0; 371 } 372 } 373 content += '</tr>'; 374 375 var tdate = new Date(date.getFullYear(), date.getMonth(), 1); 376 var day = tdate.getDay(); 377 day = day - weekStart; 378 var daymilli = 1000 * 60 * 60 * 24; 379 tdate = tdate.getTime() - (day * daymilli) + (daymilli / 2); 380 tdate = new Date(tdate); 381 382 for (j = 0; j < 6; j++) { 383 content += '<tr>'; 384 for (i = 0; i < 7; i++) { 385 content += '<td '; 386 if (tdate.getMonth() === date.getMonth()) { 387 if (tdate.getDate() === date.getDate()) { 388 content += 'class="descriptionbox"'; 389 } else { 390 content += 'class="optionbox"'; 391 } 392 } else { 393 content += 'style="background-color:#EAEAEA; border: solid #AAAAAA 1px;"'; 394 } 395 content += '><a href="#" onclick="return cal_dateClicked(\'' + dateFieldId + '\', \'' + dateDivId + '\', ' + tdate.getFullYear() + ', ' + tdate.getMonth() + ', ' + tdate.getDate() + ');">'; 396 content += tdate.getDate(); 397 content += '</a></td>'; 398 var datemilli = tdate.getTime() + daymilli; 399 tdate = new Date(datemilli); 400 } 401 content += '</tr>'; 402 } 403 content += '</table>'; 404 content += '</td></tr>'; 405 content += '</table>'; 406 407 return content; 408} 409 410function cal_setDateField(dateFieldId, year, month, day) 411{ 412 var dateField = document.getElementById(dateFieldId); 413 if (!dateField) { 414 return false; 415 } 416 if (day < 10) { 417 day = '0' + day; 418 } 419 dateField.value = day + ' ' + monthShort[month + 1] + ' ' + year; 420 return false; 421} 422 423function cal_updateCalendar(dateFieldId, dateDivId) 424{ 425 var dateSel = document.getElementById(dateFieldId + '_daySelect'); 426 if (!dateSel) { 427 return false; 428 } 429 var monthSel = document.getElementById(dateFieldId + '_monSelect'); 430 if (!monthSel) { 431 return false; 432 } 433 var yearInput = document.getElementById(dateFieldId + '_yearInput'); 434 if (!yearInput) { 435 return false; 436 } 437 438 var month = parseInt(monthSel.options[monthSel.selectedIndex].value, 10); 439 month = month - 1; 440 441 var date = new Date(yearInput.value, month, dateSel.options[dateSel.selectedIndex].value); 442 cal_setDateField(dateFieldId, date.getFullYear(), date.getMonth(), date.getDate()); 443 444 var dateDiv = document.getElementById(dateDivId); 445 if (!dateDiv) { 446 alert('no dateDiv ' + dateDivId); 447 return false; 448 } 449 dateDiv.innerHTML = cal_generateSelectorContent(dateFieldId, dateDivId, date); 450 451 return false; 452} 453 454function cal_dateClicked(dateFieldId, dateDivId, year, month, day) 455{ 456 cal_setDateField(dateFieldId, year, month, day); 457 calendarWidget(dateDivId, dateFieldId); 458 return false; 459} 460 461function openerpasteid(id) 462{ 463 if (window.opener.paste_id) { 464 window.opener.paste_id(id); 465 } 466 window.close(); 467} 468 469function paste_id(value) 470{ 471 pastefield.value = value; 472} 473 474function pastename(name) 475{ 476 if (nameElement) { 477 nameElement.innerHTML = name; 478 } 479 if (remElement) { 480 remElement.style.display = 'block'; 481 } 482} 483 484function paste_char(value) 485{ 486 if (document.selection) { 487 // IE 488 pastefield.focus(); 489 document.selection.createRange().text = value; 490 } else if (pastefield.selectionStart || pastefield.selectionStart === 0) { 491 // Mozilla/Chrome/Safari 492 pastefield.value = 493 pastefield.value.substring(0, pastefield.selectionStart) + 494 value + 495 pastefield.value.substring(pastefield.selectionEnd, pastefield.value.length); 496 pastefield.selectionStart = pastefield.selectionEnd = pastefield.selectionStart + value.length; 497 } else { 498 // Fallback? - just append 499 pastefield.value += value; 500 } 501 502 if (pastefield.id === 'NPFX' || pastefield.id === 'GIVN' || pastefield.id === 'SPFX' || pastefield.id === 'SURN' || pastefield.id === 'NSFX') { 503 updatewholename(); 504 } 505} 506 507/** 508 * Persistant checkbox options to hide/show extra data. 509 510 * @param element_id 511 */ 512function persistent_toggle(element_id) 513{ 514 let element = document.getElementById(element_id); 515 let key = 'state-of-' + element_id; 516 let state = localStorage.getItem(key); 517 518 // Previously selected? 519 if (state === 'true') { 520 $(element).click(); 521 } 522 523 // Remember state for the next page load. 524 $(element).on('change', function() { localStorage.setItem(key, element.checked); }); 525} 526 527function valid_lati_long(field, pos, neg) 528{ 529 // valid LATI or LONG according to Gedcom standard 530 // pos (+) : N or E 531 // neg (-) : S or W 532 var txt = field.value.toUpperCase(); 533 txt = txt.replace(/(^\s*)|(\s*$)/g, ''); // trim 534 txt = txt.replace(/ /g, ':'); // N12 34 ==> N12.34 535 txt = txt.replace(/\+/g, ''); // +17.1234 ==> 17.1234 536 txt = txt.replace(/-/g, neg); // -0.5698 ==> W0.5698 537 txt = txt.replace(/,/g, '.'); // 0,5698 ==> 0.5698 538 // 0°34'11 ==> 0:34:11 539 txt = txt.replace(/\u00b0/g, ':'); // ° 540 txt = txt.replace(/\u0027/g, ':'); // ' 541 // 0:34:11.2W ==> W0.5698 542 txt = txt.replace(/^([0-9]+):([0-9]+):([0-9.]+)(.*)/g, function ($0, $1, $2, $3, $4) { 543 var n = parseFloat($1); 544 n += ($2 / 60); 545 n += ($3 / 3600); 546 n = Math.round(n * 1E4) / 1E4; 547 return $4 + n; 548 }); 549 // 0:34W ==> W0.5667 550 txt = txt.replace(/^([0-9]+):([0-9]+)(.*)/g, function ($0, $1, $2, $3) { 551 var n = parseFloat($1); 552 n += ($2 / 60); 553 n = Math.round(n * 1E4) / 1E4; 554 return $3 + n; 555 }); 556 // 0.5698W ==> W0.5698 557 txt = txt.replace(/(.*)([N|S|E|W]+)$/g, '$2$1'); 558 // 17.1234 ==> N17.1234 559 if (txt && txt.charAt(0) !== neg && txt.charAt(0) !== pos) { 560 txt = pos + txt; 561 } 562 field.value = txt; 563} 564 565// Initialize autocomplete elements. 566function autocomplete(selector) 567{ 568 // Use typeahead/bloodhound for autocomplete 569 $(selector).each(function () { 570 let that = this; 571 $(this).typeahead(null, { 572 display: 'value', 573 source: new Bloodhound({ 574 datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 575 queryTokenizer: Bloodhound.tokenizers.whitespace, 576 remote: { 577 url: this.dataset.autocompleteUrl, 578 replace: function(url, uriEncodedQuery) { 579 if (that.dataset.autocompleteExtra) { 580 let extra = $(document.querySelector(that.dataset.autocompleteExtra)).val(); 581 return url.replace("QUERY",uriEncodedQuery) + '&extra=' + encodeURIComponent(extra) 582 } 583 return url.replace("QUERY",uriEncodedQuery); 584 }, 585 wildcard: 'QUERY', 586 587 } 588 }) 589 }); 590 }); 591} 592 593/** 594 * Insert text at the current cursor position in an input field. 595 * 596 * @param e The input element. 597 * @param t The text to insert. 598 */ 599function insertTextAtCursor(e, t) 600{ 601 var scrollTop = e.scrollTop; 602 var selectionStart = e.selectionStart; 603 var prefix = e.value.substring(0, selectionStart); 604 var suffix = e.value.substring(e.selectionEnd, e.value.length); 605 e.value = prefix + t + suffix; 606 e.selectionStart = selectionStart + t.length; 607 e.selectionEnd = e.selectionStart; 608 e.focus(); 609 e.scrollTop = scrollTop; 610} 611 612// Send the CSRF token on all AJAX requests 613$.ajaxSetup({ 614 headers: { 615 'X-CSRF-TOKEN': $('meta[name=csrf]').attr('content') 616 } 617}); 618 619// Initialisation 620$(function () { 621 // Page elements that load automaticaly via AJAX. 622 // This prevents bad robots from crawling resource-intensive pages. 623 $("[data-ajax-url]").each(function () { 624 $(this).load($(this).data('ajaxUrl')); 625 }); 626 627 // Select2 - format entries in the select list 628 function templateOptionForSelect2(data) 629 { 630 if (data.loading) { 631 // If we're waiting for the server, this will be a "waiting..." message 632 return data.text; 633 } else { 634 // The response from the server is already in HTML, so no need to format it here. 635 return data.text; 636 } 637 } 638 639 // Autocomplete 640 autocomplete('input[data-autocomplete-url]'); 641 642 // Select2 - activate autocomplete fields 643 const lang = document.documentElement.lang; 644 const select2_languages = { 645 'zh-Hans': 'zh-CN', 646 'zh-Hant': 'zh-TW', 647 }; 648 $("select.select2").select2({ 649 language: select2_languages[lang] || lang, 650 // Needed for elements that are initially hidden. 651 width: "100%", 652 // Do not escape - we do it on the server. 653 escapeMarkup: function (x) { 654 return x; 655 }, 656 }); 657 658 // If we clear the select (using the "X" button), we need an empty value 659 // (rather than no value at all) for (non-multiple) selects with name="array[]" 660 $("select.select2:not([multiple])") 661 .on("select2:unselect", function (evt) { 662 $(evt.delegateTarget).html("<option value=\"\" selected></option>"); 663 }); 664 665 // Datatables - locale aware sorting 666 $.fn.dataTableExt.oSort['text-asc'] = function (x, y) { 667 return x.localeCompare(y, document.documentElement.lang, {'sensitivity': 'base'}); 668 }; 669 $.fn.dataTableExt.oSort['text-desc'] = function (x, y) { 670 return y.localeCompare(x, document.documentElement.lang, {'sensitivity': 'base'}); 671 }; 672 673 // DataTables - start hidden to prevent FOUC. 674 $('table.datatables').each(function () { 675 $(this).DataTable(); $(this).removeClass('d-none'); }); 676 677 // Activate the on-screen keyboard 678 var osk_focus_element; 679 $('.wt-osk-trigger').click(function () { 680 // When a user clicks the icon, set focus to the corresponding input 681 osk_focus_element = document.getElementById($(this).data('id')); 682 osk_focus_element.focus(); 683 $('.wt-osk').show(); 684 }); 685 $('.wt-osk-script-button').change(function () { 686 $('.wt-osk-script').prop('hidden', true); 687 $('.wt-osk-script-' + $(this).data('script')).prop('hidden', false); 688 }); 689 $('.wt-osk-shift-button').click(function () { 690 document.querySelector('.wt-osk-keys').classList.toggle('shifted'); 691 }); 692 $('.wt-osk-keys').on('click', '.wt-osk-key', function () { 693 var key = $(this).contents().get(0).nodeValue; 694 var shift_state = $('.wt-osk-shift-button').hasClass('active'); 695 var shift_key = $('sup', this)[0]; 696 if (shift_state && shift_key !== undefined) { 697 key = shift_key.innerText; 698 } 699 webtrees.pasteAtCursor(osk_focus_element, key); 700 if ($('.wt-osk-pin-button').hasClass('active') === false) { 701 $('.wt-osk').hide(); 702 } 703 }); 704 705 $('.wt-osk-close').on('click', function () { 706 $('.wt-osk').hide(); 707 }); 708}); 709 710// Convert data-confirm and data-post-url attributes into useful behavior. 711document.addEventListener("click", (event) => { 712 const target = event.target.closest("a"); 713 714 if (target === null) { 715 return; 716 } 717 718 if ("confirm" in target.dataset && !confirm(target.dataset.confirm)) { 719 event.preventDefault(); 720 return; 721 } 722 723 if ("postUrl" in target.dataset) { 724 let request = new XMLHttpRequest(); 725 let token = document.querySelector("meta[name=csrf]").content; 726 request.open("POST", target.dataset.postUrl, true); 727 request.setRequestHeader("X-CSRF-TOKEN", token); 728 request.onreadystatechange = () => { 729 if (request.readyState === request.DONE) { 730 document.location.reload(); 731 } 732 }; 733 request.send(); 734 event.preventDefault(); 735 } 736}); 737