/*global htmlEncode, VuFind */ /*exported initFacetTree */ function buildFacetNodes(data, currentPath, allowExclude, excludeTitle, counts) { var json = []; $(data).each(function facetNodesEach() { var html = ''; if (!this.isApplied && counts) { html = '' + this.count.toString().replace(/\B(?=(\d{3})+\b)/g, VuFind.translate('number_thousands_separator')); if (allowExclude) { var excludeURL = currentPath + this.exclude; excludeURL.replace("'", "\\'"); // Just to be safe html += ' '; } html += ''; } var url = currentPath + this.href; // Just to be safe url.replace("'", "\\'"); html += ''; if (this.operator === 'OR') { if (this.isApplied) { html += ''; } else { html += ''; } } else if (this.isApplied) { html += ''; } html += ' ' + this.displayText; html += ''; var children = null; if (typeof this.children !== 'undefined' && this.children.length > 0) { children = buildFacetNodes(this.children, currentPath, allowExclude, excludeTitle, counts); } json.push({ 'text': html, 'children': children, 'applied': this.isApplied, 'state': { 'opened': this.hasAppliedChildren }, 'li_attr': this.isApplied ? { 'class': 'active' } : {} }); }); return json; } function initFacetTree(treeNode, inSidebar) { var loaded = treeNode.data('loaded'); if (loaded) { return; } treeNode.data('loaded', true); var facet = treeNode.data('facet'); var operator = treeNode.data('operator'); var currentPath = treeNode.data('path'); var allowExclude = treeNode.data('exclude'); var excludeTitle = treeNode.data('exclude-title'); var sort = treeNode.data('sort'); var query = window.location.href.split('?')[1]; if (inSidebar) { treeNode.prepend('
  • '); } else { treeNode.prepend('
    '); } $.getJSON(VuFind.path + '/AJAX/JSON?' + query, { method: "getFacetData", facetName: facet, facetSort: sort, facetOperator: operator }, function getFacetData(response/*, textStatus*/) { if (response.status === "OK") { var results = buildFacetNodes(response.data, currentPath, allowExclude, excludeTitle, inSidebar); treeNode.find('.fa-spinner').parent().remove(); if (inSidebar) { treeNode.on('loaded.jstree open_node.jstree', function treeNodeOpen(/*e, data*/) { treeNode.find('ul.jstree-container-ul > li.jstree-node').addClass('list-group-item'); }); } treeNode.jstree({ 'core': { 'data': results } }); } } ); } /* --- Lightbox Facets --- */ VuFind.register('lightbox_facets', function LightboxFacets() { function lightboxFacetSorting() { var sortButtons = $('.js-facet-sort'); function sortAjax(button) { var sort = $(button).data('sort'); var list = $('#facet-list-' + sort); if (list.find('.js-facet-item').length === 0) { list.find('.js-facet-next-page').text(VuFind.translate('loading') + '...'); $.ajax(button.href + '&layout=lightbox') .done(function facetSortTitleDone(data) { list.prepend($('' + data + '').find('.js-facet-item')); list.find('.js-facet-next-page').text(VuFind.translate('more') + ' ...'); }); } $('.full-facet-list').addClass('hidden'); list.removeClass('hidden'); sortButtons.removeClass('active'); } sortButtons.click(function facetSortButton() { sortAjax(this); $(this).addClass('active'); return false; }); } function setup() { lightboxFacetSorting(); $('.js-facet-next-page').click(function facetLightboxMore() { var button = $(this); var page = parseInt(button.attr('data-page'), 10); if (button.attr('disabled')) { return false; } button.attr('disabled', 1); button.text(VuFind.translate('loading') + '...'); $.ajax(this.href + '&layout=lightbox') .done(function facetLightboxMoreDone(data) { var htmlDiv = $('
    ' + data + '
    '); var list = htmlDiv.find('.js-facet-item'); button.before(list); if (list.length && htmlDiv.find('.js-facet-next-page').length) { button.attr('data-page', page + 1); button.attr('href', button.attr('href').replace(/facetpage=\d+/, 'facetpage=' + (page + 1))); button.text(VuFind.translate('more') + ' ...'); button.removeAttr('disabled'); } else { button.remove(); } }); return false; }); var margin = 230; $('#modal').on('show.bs.modal', function facetListHeight() { $('#modal .lightbox-scroll').css('max-height', window.innerHeight - margin); }); $(window).resize(function facetListResize() { $('#modal .lightbox-scroll').css('max-height', window.innerHeight - margin); }); } return { setup: setup }; });