/** * 2007-2016 PrestaShop * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@prestashop.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author SeoSA <885588@bk.ru> * @copyright 2012-2021 SeoSA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ function TreeCustom(tree_selector, tree_header_selector) { this.ts = $(tree_selector); this.th = $(tree_header_selector); this.updateCounter = function () { this.ts.find('ul').each(function() { var all_child_selected = $(this).find('span.tree_selected').length; $(this).closest('.tree_item').find('> .tree_label .tree_counter').text(all_child_selected ? '('+all_child_selected+')' : ''); }); }; this.afterChange = function() {}; this.serialize = function() { return this.ts.find(':input').serialize(); }; this.getListSelectedCategories = function() { var categories = []; this.ts.find('.tree_input:checked').each(function () { categories.push({ id: $(this).val(), name: $(this).data('name') }); }); return categories; }; this.collapseAll = function () { this.ts.find('ul').each(function () { var tree_item = $(this).closest('.tree_item'); tree_item.find('> ul').removeClass('tree_open').addClass('tree_close'); if (!tree_item.find('> .tree_label i.tree-dot').length) tree_item.find('> .tree_label i').attr('class', 'icon-folder-close'); }); this.th.find('.collapse_all').hide(); this.th.find('.expand_all').show(); }; this.expandAll = function () { this.ts.find('ul').each(function () { var tree_item = $(this).closest('.tree_item'); tree_item.find('> ul').removeClass('tree_close').addClass('tree_open'); if (!tree_item.find('> .tree_label i.tree-dot').length) tree_item.find('> .tree_label i').attr('class', 'icon-folder-open'); }); this.th.find('.expand_all').hide(); this.th.find('.collapse_all').show(); }; this.uncheckAllAssociatedCategories = function () { this.ts.find('.tree_label.tree_selected').each(function () { $(this).removeClass('tree_selected').find('input[type=checkbox]').removeAttr('checked'); }); this.updateCounter(); this.afterChange(); }; this.checkAllAssociatedCategories = function () { this.ts.find('.tree_label').not('.tree_selected').each(function () { $(this).addClass('tree_selected').find('input[type=checkbox]').attr('checked', 'checked'); }); this.expandAll(); this.updateCounter(); this.afterChange(); }; this.checkAssociatedCategory = function (id) { if (!id) return false; var input = this.ts.find('.tree_input[value='+id+']'); if (input.is(':checked')) return false; input.attr('checked', 'checked'); input.closest('.tree_label').addClass('.tree_selected'); input.closest('.tree_item').find('> ul').removeClass('tree_close').addClass('tree_open'); input.trigger('change'); return true; }; this.search = function (string) { var elements = this.ts.find('.tree_input[data-search*="'+string.replace(/[^a-zA-Zа-яА-Я0-9 ]+/, '')+'"]').not(':checked'); if (!elements.length) return ''; var html_response = ''; elements.each(function () { html_response += ''+$(this).data('name')+''; }); return '