12ebb07b4SGreg Roach/** 22ebb07b4SGreg Roach * webtrees: online genealogy 3*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 42ebb07b4SGreg Roach * This program is free software: you can redistribute it and/or modify 52ebb07b4SGreg Roach * it under the terms of the GNU General Public License as published by 62ebb07b4SGreg Roach * the Free Software Foundation, either version 3 of the License, or 72ebb07b4SGreg Roach * (at your option) any later version. 82ebb07b4SGreg Roach * This program is distributed in the hope that it will be useful, 92ebb07b4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 102ebb07b4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 112ebb07b4SGreg Roach * GNU General Public License for more details. 122ebb07b4SGreg Roach * You should have received a copy of the GNU General Public License 132ebb07b4SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 142ebb07b4SGreg Roach */ 152ebb07b4SGreg Roach 162ebb07b4SGreg Roachfunction TreeViewHandler (treeview_instance, ged) { 172ebb07b4SGreg Roach var tv = this; // Store "this" for usage within jQuery functions where "this" is not this ;-) 182ebb07b4SGreg Roach 19efd89170SGreg Roach this.treeview = $('#' + treeview_instance + '_in'); 20efd89170SGreg Roach this.loadingImage = $('#' + treeview_instance + '_loading'); 21efd89170SGreg Roach this.toolbox = $('#tv_tools'); 22efd89170SGreg Roach this.buttons = $('.tv_button:first', this.toolbox); 232ebb07b4SGreg Roach this.zoom = 100; // in percent 242ebb07b4SGreg Roach this.boxWidth = 180; // default family box width 252ebb07b4SGreg Roach this.boxExpandedWidth = 250; // default expanded family box width 262ebb07b4SGreg Roach this.cookieDays = 3; // lifetime of preferences memory, in days 27efd89170SGreg Roach this.ajaxDetails = document.getElementById(treeview_instance + '_out').dataset.urlDetails + '&instance=' + encodeURIComponent(treeview_instance); 28efd89170SGreg Roach this.ajaxPersons = document.getElementById(treeview_instance + '_out').dataset.urlIndividuals + '&instance=' + encodeURIComponent(treeview_instance); 292ebb07b4SGreg Roach 302ebb07b4SGreg Roach this.container = this.treeview.parent(); // Store the container element ("#" + treeview_instance + "_out") 312ebb07b4SGreg Roach this.auto_box_width = false; 322ebb07b4SGreg Roach this.updating = false; 332ebb07b4SGreg Roach 342ebb07b4SGreg Roach // Restore user preferences 35efd89170SGreg Roach if (readCookie('compact') === 'true') { 362ebb07b4SGreg Roach tv.compact(); 372ebb07b4SGreg Roach } 382ebb07b4SGreg Roach 3942584a2bSGreg Roach // Drag handlers for the treeview canvas 4042584a2bSGreg Roach (function () { 4142584a2bSGreg Roach let dragging = false; 4242584a2bSGreg Roach let drag_start_x; 4342584a2bSGreg Roach let drag_start_y; 442ebb07b4SGreg Roach 4542584a2bSGreg Roach tv.treeview.on('mousedown touchstart', function (event) { 4642584a2bSGreg Roach event.preventDefault(); 472ebb07b4SGreg Roach 4842584a2bSGreg Roach let pageX = (event.type === 'touchstart') ? event.touches[0].pageX : event.pageX; 4942584a2bSGreg Roach let pageY = (event.type === 'touchstart') ? event.touches[0].pageY : event.pageY; 502ebb07b4SGreg Roach 5142584a2bSGreg Roach drag_start_x = tv.treeview.offset().left - pageX; 5242584a2bSGreg Roach drag_start_y = tv.treeview.offset().top - pageY; 5342584a2bSGreg Roach dragging = true; 542ebb07b4SGreg Roach }); 5542584a2bSGreg Roach 5642584a2bSGreg Roach $(document).on('mousemove touchmove', function (event) { 5742584a2bSGreg Roach if (dragging) { 5842584a2bSGreg Roach event.preventDefault(); 5942584a2bSGreg Roach 6042584a2bSGreg Roach let pageX = (event.type === 'touchmove') ? event.touches[0].pageX : event.pageX; 6142584a2bSGreg Roach let pageY = (event.type === 'touchmove') ? event.touches[0].pageY : event.pageY; 6242584a2bSGreg Roach 6342584a2bSGreg Roach tv.treeview.offset({ 6442584a2bSGreg Roach left: pageX + drag_start_x, 6542584a2bSGreg Roach top: pageY + drag_start_y, 6642584a2bSGreg Roach }); 6742584a2bSGreg Roach } 6842584a2bSGreg Roach }); 6942584a2bSGreg Roach 7042584a2bSGreg Roach $(document).on('mouseup touchend', function (event) { 7142584a2bSGreg Roach if (dragging) { 7242584a2bSGreg Roach event.preventDefault(); 7342584a2bSGreg Roach dragging = false; 742ebb07b4SGreg Roach tv.updateTree(); 7542584a2bSGreg Roach } 762ebb07b4SGreg Roach }); 7742584a2bSGreg Roach })(); 782ebb07b4SGreg Roach 792ebb07b4SGreg Roach // Add click handlers to buttons 80efd89170SGreg Roach tv.toolbox.find('#tvbCompact').each(function (index, tvCompact) { 812ebb07b4SGreg Roach tvCompact.onclick = function () { 822ebb07b4SGreg Roach tv.compact(); 832ebb07b4SGreg Roach }; 842ebb07b4SGreg Roach }); 852ebb07b4SGreg Roach // If we click the "hide/show all partners" button, toggle the setting before reloading the page 86efd89170SGreg Roach tv.toolbox.find('#tvbAllPartners').each(function (index, tvAllPartners) { 872ebb07b4SGreg Roach tvAllPartners.onclick = function () { 88efd89170SGreg Roach createCookie('allPartners', readCookie('allPartners') === 'true' ? 'false' : 'true', tv.cookieDays); 892ebb07b4SGreg Roach document.location = document.location; 902ebb07b4SGreg Roach }; 912ebb07b4SGreg Roach }); 92efd89170SGreg Roach tv.toolbox.find('#tvbOpen').each(function (index, tvbOpen) { 932ebb07b4SGreg Roach var b = $(tvbOpen, tv.toolbox); 942ebb07b4SGreg Roach tvbOpen.onclick = function () { 95efd89170SGreg Roach b.addClass('tvPressed'); 962ebb07b4SGreg Roach tv.setLoading(); 97efd89170SGreg Roach var e = jQuery.Event('click'); 98efd89170SGreg Roach tv.treeview.find('.tv_box:not(.boxExpanded)').each(function (index, box) { 992ebb07b4SGreg Roach var pos = $(box, tv.treeview).offset(); 1002ebb07b4SGreg Roach if (pos.left >= tv.leftMin && pos.left <= tv.leftMax && pos.top >= tv.topMin && pos.top <= tv.topMax) { 1012ebb07b4SGreg Roach tv.expandBox(box, e); 1022ebb07b4SGreg Roach } 1032ebb07b4SGreg Roach }); 104efd89170SGreg Roach b.removeClass('tvPressed'); 1052ebb07b4SGreg Roach tv.setComplete(); 1062ebb07b4SGreg Roach }; 1072ebb07b4SGreg Roach }); 108efd89170SGreg Roach tv.toolbox.find('#tvbClose').each(function (index, tvbClose) { 1092ebb07b4SGreg Roach var b = $(tvbClose, tv.toolbox); 1102ebb07b4SGreg Roach tvbClose.onclick = function () { 111efd89170SGreg Roach b.addClass('tvPressed'); 1122ebb07b4SGreg Roach tv.setLoading(); 113efd89170SGreg Roach tv.treeview.find('.tv_box.boxExpanded').each(function (index, box) { 114efd89170SGreg Roach $(box).css('display', 'none').removeClass('boxExpanded').parent().find('.tv_box.collapsedContent').css('display', 'block'); 1152ebb07b4SGreg Roach }); 116efd89170SGreg Roach b.removeClass('tvPressed'); 1172ebb07b4SGreg Roach tv.setComplete(); 1182ebb07b4SGreg Roach }; 1192ebb07b4SGreg Roach }); 1202ebb07b4SGreg Roach 1212ebb07b4SGreg Roach tv.centerOnRoot(); // fire ajax update if needed, which call setComplete() when all is loaded 1222ebb07b4SGreg Roach} 1232ebb07b4SGreg Roach/** 1242ebb07b4SGreg Roach * Class TreeView setLoading method 1252ebb07b4SGreg Roach */ 1262ebb07b4SGreg RoachTreeViewHandler.prototype.setLoading = function () { 127efd89170SGreg Roach this.treeview.css('cursor', 'wait'); 128efd89170SGreg Roach this.loadingImage.css('display', 'block'); 1292ebb07b4SGreg Roach}; 1302ebb07b4SGreg Roach/** 1312ebb07b4SGreg Roach * Class TreeView setComplete method 1322ebb07b4SGreg Roach */ 1332ebb07b4SGreg RoachTreeViewHandler.prototype.setComplete = function () { 134efd89170SGreg Roach this.treeview.css('cursor', 'move'); 135efd89170SGreg Roach this.loadingImage.css('display', 'none'); 1362ebb07b4SGreg Roach}; 1372ebb07b4SGreg Roach 1382ebb07b4SGreg Roach/** 1392ebb07b4SGreg Roach * Class TreeView getSize method 1402ebb07b4SGreg Roach * Store the viewport current size 1412ebb07b4SGreg Roach */ 1422ebb07b4SGreg RoachTreeViewHandler.prototype.getSize = function () { 1432ebb07b4SGreg Roach var tv = this; 1442ebb07b4SGreg Roach // retrieve the current container bounding box 1452ebb07b4SGreg Roach var container = tv.container.parent(); 1462ebb07b4SGreg Roach var offset = container.offset(); 1472ebb07b4SGreg Roach tv.leftMin = offset.left; 1482ebb07b4SGreg Roach tv.leftMax = tv.leftMin + container.innerWidth(); 1492ebb07b4SGreg Roach tv.topMin = offset.top; 1502ebb07b4SGreg Roach tv.topMax = tv.topMin + container.innerHeight(); 1512ebb07b4SGreg Roach /* 1522ebb07b4SGreg Roach var frm = $("#tvTreeBorder"); 1532ebb07b4SGreg Roach tv.treeview.css("width", frm.width()); 1542ebb07b4SGreg Roach tv.treeview.css("height", frm.height()); */ 1552ebb07b4SGreg Roach}; 1562ebb07b4SGreg Roach 1572ebb07b4SGreg Roach/** 1582ebb07b4SGreg Roach * Class TreeView updateTree method 1592ebb07b4SGreg Roach * Perform ajax requests to complete the tree after drag 1602ebb07b4SGreg Roach * param boolean @center center on root person when done 1612ebb07b4SGreg Roach */ 1622ebb07b4SGreg RoachTreeViewHandler.prototype.updateTree = function (center, button) { 1632ebb07b4SGreg Roach var tv = this; // Store "this" for usage within jQuery functions where "this" is not this ;-) 1642ebb07b4SGreg Roach var to_load = []; 1652ebb07b4SGreg Roach var elts = []; 1662ebb07b4SGreg Roach this.getSize(); 1672ebb07b4SGreg Roach 1682ebb07b4SGreg Roach // check which td with datafld attribute are within the container bounding box 1692ebb07b4SGreg Roach // and therefore need to be dynamically loaded 170efd89170SGreg Roach tv.treeview.find('td[abbr]').each(function (index, el) { 1712ebb07b4SGreg Roach el = $(el, tv.treeview); 1722ebb07b4SGreg Roach var pos = el.offset(); 1732ebb07b4SGreg Roach if (pos.left >= tv.leftMin && pos.left <= tv.leftMax && pos.top >= tv.topMin && pos.top <= tv.topMax) { 174efd89170SGreg Roach to_load.push(el.attr('abbr')); 1752ebb07b4SGreg Roach elts.push(el); 1762ebb07b4SGreg Roach } 1772ebb07b4SGreg Roach }); 1782ebb07b4SGreg Roach // if some boxes need update, we perform an ajax request 1792ebb07b4SGreg Roach if (to_load.length > 0) { 1802ebb07b4SGreg Roach tv.updating = true; 1812ebb07b4SGreg Roach tv.setLoading(); 1822ebb07b4SGreg Roach jQuery.ajax({ 18306f42609SGreg Roach url: tv.ajaxPersons, 184efd89170SGreg Roach dataType: 'json', 185efd89170SGreg Roach data: 'q=' + to_load.join(';'), 1862ebb07b4SGreg Roach success: function (ret) { 1872ebb07b4SGreg Roach var nb = elts.length; 188efd89170SGreg Roach var root_element = $('.rootPerson', this.treeview); 1892ebb07b4SGreg Roach var l = root_element.offset().left; 1902ebb07b4SGreg Roach for (var i = 0; i < nb; i++) { 191efd89170SGreg Roach elts[i].removeAttr('abbr').html(ret[i]); 1922ebb07b4SGreg Roach } 1932ebb07b4SGreg Roach // we now ajust the draggable treeview size to its content size 1942ebb07b4SGreg Roach tv.getSize(); 1952ebb07b4SGreg Roach }, 1962ebb07b4SGreg Roach complete: function () { 197efd89170SGreg Roach if (tv.treeview.find('td[abbr]').length) { 1982ebb07b4SGreg Roach tv.updateTree(center, button); // recursive call 1992ebb07b4SGreg Roach } 2002ebb07b4SGreg Roach // the added boxes need that in mode compact boxes 2012ebb07b4SGreg Roach if (tv.auto_box_width) { 202efd89170SGreg Roach tv.treeview.find('.tv_box').css('width', 'auto'); 2032ebb07b4SGreg Roach } 2042ebb07b4SGreg Roach tv.updating = true; // avoid an unuseful recursive call when all requested persons are loaded 2052ebb07b4SGreg Roach if (center) { 2062ebb07b4SGreg Roach tv.centerOnRoot(); 2072ebb07b4SGreg Roach } 2082ebb07b4SGreg Roach if (button) { 209efd89170SGreg Roach button.removeClass('tvPressed'); 2102ebb07b4SGreg Roach } 2112ebb07b4SGreg Roach tv.setComplete(); 2122ebb07b4SGreg Roach tv.updating = false; 2132ebb07b4SGreg Roach }, 2142ebb07b4SGreg Roach timeout: function () { 2152ebb07b4SGreg Roach if (button) { 216efd89170SGreg Roach button.removeClass('tvPressed'); 2172ebb07b4SGreg Roach } 2182ebb07b4SGreg Roach tv.updating = false; 2192ebb07b4SGreg Roach tv.setComplete(); 2202ebb07b4SGreg Roach } 2212ebb07b4SGreg Roach }); 2222ebb07b4SGreg Roach } else { 2232ebb07b4SGreg Roach if (button) { 224efd89170SGreg Roach button.removeClass('tvPressed'); 2252ebb07b4SGreg Roach } 2262ebb07b4SGreg Roach tv.setComplete(); 2272ebb07b4SGreg Roach } 2282ebb07b4SGreg Roach return false; 2292ebb07b4SGreg Roach}; 2302ebb07b4SGreg Roach 2312ebb07b4SGreg Roach/** 2322ebb07b4SGreg Roach * Class TreeView compact method 2332ebb07b4SGreg Roach */ 2342ebb07b4SGreg RoachTreeViewHandler.prototype.compact = function () { 2352ebb07b4SGreg Roach var tv = this; 236efd89170SGreg Roach var b = $('#tvbCompact', tv.toolbox); 2372ebb07b4SGreg Roach tv.setLoading(); 2382ebb07b4SGreg Roach if (tv.auto_box_width) { 239efd89170SGreg Roach var w = tv.boxWidth * (tv.zoom / 100) + 'px'; 240efd89170SGreg Roach var ew = tv.boxExpandedWidth * (tv.zoom / 100) + 'px'; 241efd89170SGreg Roach tv.treeview.find('.tv_box:not(boxExpanded)', tv.treeview).css('width', w); 242efd89170SGreg Roach tv.treeview.find('.boxExpanded', tv.treeview).css('width', ew); 2432ebb07b4SGreg Roach tv.auto_box_width = false; 244efd89170SGreg Roach if (readCookie('compact')) { 245efd89170SGreg Roach createCookie('compact', false, tv.cookieDays); 2462ebb07b4SGreg Roach } 247efd89170SGreg Roach b.removeClass('tvPressed'); 2482ebb07b4SGreg Roach } else { 249efd89170SGreg Roach tv.treeview.find('.tv_box').css('width', 'auto'); 2502ebb07b4SGreg Roach tv.auto_box_width = true; 251efd89170SGreg Roach if (!readCookie('compact')) { 252efd89170SGreg Roach createCookie('compact', true, tv.cookieDays); 2532ebb07b4SGreg Roach } 2542ebb07b4SGreg Roach if (!tv.updating) { 2552ebb07b4SGreg Roach tv.updateTree(); 2562ebb07b4SGreg Roach } 257efd89170SGreg Roach b.addClass('tvPressed'); 2582ebb07b4SGreg Roach } 2592ebb07b4SGreg Roach tv.setComplete(); 2602ebb07b4SGreg Roach return false; 2612ebb07b4SGreg Roach}; 2622ebb07b4SGreg Roach 2632ebb07b4SGreg Roach/** 2642ebb07b4SGreg Roach * Class TreeView centerOnRoot method 2652ebb07b4SGreg Roach */ 2662ebb07b4SGreg RoachTreeViewHandler.prototype.centerOnRoot = function () { 267efd89170SGreg Roach this.loadingImage.css('display', 'block'); 2682ebb07b4SGreg Roach var tv = this; 2692ebb07b4SGreg Roach var tvc = this.container; 2702ebb07b4SGreg Roach var tvc_width = tvc.innerWidth() / 2; 27180d699d6SGreg Roach if (Number.isNaN(tvc_width)) { 2722ebb07b4SGreg Roach return false; 2732ebb07b4SGreg Roach } 2742ebb07b4SGreg Roach var tvc_height = tvc.innerHeight() / 2; 275efd89170SGreg Roach var root_person = $('.rootPerson', this.treeview); 2762ebb07b4SGreg Roach 2772ebb07b4SGreg Roach if (!this.updating) { 2782ebb07b4SGreg Roach tv.setComplete(); 2792ebb07b4SGreg Roach } 2802ebb07b4SGreg Roach return false; 2812ebb07b4SGreg Roach}; 2822ebb07b4SGreg Roach 2832ebb07b4SGreg Roach/** 2842ebb07b4SGreg Roach * Class TreeView expandBox method 285220f62c2SGreg Roach * Called ONLY for elements which have NOT the class tv_link to avoid un-useful requests to the server 286220f62c2SGreg Roach * @param {string} box - the person box element 287220f62c2SGreg Roach * @param {string} event - the call event 2882ebb07b4SGreg Roach */ 2892ebb07b4SGreg RoachTreeViewHandler.prototype.expandBox = function (box, event) { 2902ebb07b4SGreg Roach var t = $(event.target); 291efd89170SGreg Roach if (t.hasClass('tv_link')) { 2922ebb07b4SGreg Roach return false; 2932ebb07b4SGreg Roach } 2942ebb07b4SGreg Roach 2952ebb07b4SGreg Roach var box = $(box, this.treeview); 2962ebb07b4SGreg Roach var bc = box.parent(); // bc is Box Container 297efd89170SGreg Roach var pid = box.attr('abbr'); 2982ebb07b4SGreg Roach var tv = this; // Store "this" for usage within jQuery functions where "this" is not this ;-) 2992ebb07b4SGreg Roach var expanded; 3002ebb07b4SGreg Roach var collapsed; 3012ebb07b4SGreg Roach 302efd89170SGreg Roach if (bc.hasClass('detailsLoaded')) { 303efd89170SGreg Roach collapsed = bc.find('.collapsedContent'); 304efd89170SGreg Roach expanded = bc.find('.tv_box:not(.collapsedContent)'); 3052ebb07b4SGreg Roach } else { 3062ebb07b4SGreg Roach // Cache the box content as an hidden person's box in the box's parent element 3072ebb07b4SGreg Roach expanded = box; 3082ebb07b4SGreg Roach collapsed = box.clone(); 309efd89170SGreg Roach bc.append(collapsed.addClass('collapsedContent').css('display', 'none')); 3102ebb07b4SGreg Roach // we add a waiting image at the right side of the box 311efd89170SGreg Roach var loading_image = this.loadingImage.find('img').clone().addClass('tv_box_loading').css('display', 'block'); 3122ebb07b4SGreg Roach box.prepend(loading_image); 3132ebb07b4SGreg Roach tv.updating = true; 3142ebb07b4SGreg Roach tv.setLoading(); 3152ebb07b4SGreg Roach // perform the Ajax request and load the result in the box 316efd89170SGreg Roach box.load(tv.ajaxDetails + '&pid=' + encodeURIComponent(pid), function () { 3172ebb07b4SGreg Roach // If Lightbox module is active, we reinitialize it for the new links 318efd89170SGreg Roach if (typeof CB_Init === 'function') { 3192ebb07b4SGreg Roach CB_Init(); 3202ebb07b4SGreg Roach } 321efd89170SGreg Roach box.css('width', tv.boxExpandedWidth * (tv.zoom / 100) + 'px'); 3222ebb07b4SGreg Roach loading_image.remove(); 323efd89170SGreg Roach bc.addClass('detailsLoaded'); 3242ebb07b4SGreg Roach tv.setComplete(); 3252ebb07b4SGreg Roach tv.updating = false; 3262ebb07b4SGreg Roach }); 3272ebb07b4SGreg Roach } 328efd89170SGreg Roach if (box.hasClass('boxExpanded')) { 329efd89170SGreg Roach expanded.css('display', 'none'); 330efd89170SGreg Roach collapsed.css('display', 'block'); 331efd89170SGreg Roach box.removeClass('boxExpanded'); 3322ebb07b4SGreg Roach } else { 333efd89170SGreg Roach expanded.css('display', 'block'); 334efd89170SGreg Roach collapsed.css('display', 'none'); 335efd89170SGreg Roach expanded.addClass('boxExpanded'); 3362ebb07b4SGreg Roach } 3372ebb07b4SGreg Roach // we must ajust the draggable treeview size to its content size 3382ebb07b4SGreg Roach this.getSize(); 3392ebb07b4SGreg Roach return false; 3402ebb07b4SGreg Roach}; 3412ebb07b4SGreg Roach 342220f62c2SGreg Roach/** 343220f62c2SGreg Roach * @param {string} name 344220f62c2SGreg Roach * @param {string} value 345220f62c2SGreg Roach * @param {number} days 346220f62c2SGreg Roach */ 3472ebb07b4SGreg Roachfunction createCookie (name, value, days) { 3482ebb07b4SGreg Roach if (days) { 3492ebb07b4SGreg Roach var date = new Date(); 3502ebb07b4SGreg Roach date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 351efd89170SGreg Roach document.cookie = name + '=' + value + '; expires=' + date.toGMTString() + '; path=/'; 3522ebb07b4SGreg Roach } else { 353efd89170SGreg Roach document.cookie = name + '=' + value + '; path=/'; 3542ebb07b4SGreg Roach } 3552ebb07b4SGreg Roach} 3562ebb07b4SGreg Roach 357220f62c2SGreg Roach/** 358220f62c2SGreg Roach * @param {string} name 359220f62c2SGreg Roach * @returns {string|null} 360220f62c2SGreg Roach */ 3612ebb07b4SGreg Roachfunction readCookie (name) { 362efd89170SGreg Roach var name_equals = name + '='; 3632ebb07b4SGreg Roach var ca = document.cookie.split(';'); 3642ebb07b4SGreg Roach for (var i = 0; i < ca.length; i++) { 3652ebb07b4SGreg Roach var c = ca[i]; 3662ebb07b4SGreg Roach while (c.charAt(0) === ' ') { 3672ebb07b4SGreg Roach c = c.substring(1, c.length); 3682ebb07b4SGreg Roach } 3692ebb07b4SGreg Roach if (c.indexOf(name_equals) === 0) { 3702ebb07b4SGreg Roach return c.substring(name_equals.length, c.length); 3712ebb07b4SGreg Roach } 3722ebb07b4SGreg Roach } 3732ebb07b4SGreg Roach return null; 3742ebb07b4SGreg Roach} 375