Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FolderBlockButton {
- constructor($self) {
- var _this = this;
- this.$block = $self;
- this.items = null,
- this.$block = $self;
- //this.items = this.getFolderFetch();
- this.itemsFolderId = this.$block.find('.items-folder-id');
- this.queryFolder = this.$block.find(`.lpc-query-folder`); // Logic query
- this.selectFolder = this.queryFolder.find('.lpc-query-folder__select');
- this.arrayItemsFolder = null;
- if(_this.itemsFolderId.text() && _this.itemsFolderId.text() != 'not_found'){
- this.arrayItemsFolder = this.itemsFolderId.text().split(',');
- }
- this.helperFunction = {
- selectOpen(){
- _this.queryFolder
- .find('.lpc-query-folder__input')
- .on('click', function (e) {
- _this.selectFolder.toggleClass("open");
- });
- },
- in_array(needle, haystack, strict) {
- var found = false, key, strict = !!strict;
- for (key in haystack) {
- if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
- found = true;
- break;
- }
- }
- return found;
- }
- }
- this.eventHandler = {
- selected(e){
- },
- }
- this.topLevelEventHendler = function(){
- };
- this.createSelectOption = async function(){
- _this.items = await _this.getFolderFetch(() => { // get ITEMS and callback
- _this.queryFolder.addClass('lpc-shop-not-found');
- });
- let dropDownList = _this.dropDownListHtml(_this.items);
- _this.queryFolder
- .removeClass('load')
- .addClass('loaded')
- .find('.lpc-query-folder__body')
- .append(dropDownList);
- let allUl = $(dropDownList).find('ul');
- let counterItem = function(elem){
- $(elem).each( function(i, el){
- let $this = $(this);
- let $li = $this.children('li');
- let $div = $this.prev();
- $div.append( `<i>(${$li.length})</i>` );
- });
- }
- counterItem(dropDownList);
- counterItem(allUl);
- return true;
- }
- }
- createButton(){
- let $list = this.$block.find('.lpc-menu-horizontal__list');
- let lpcMenuView = $list.attr('data-lpc-menu-view');
- let lpcButtons = $list.attr('data-lpc-buttons');
- let itemsFolder = this.itemsFolderId.text().split(',');
- let lpcRowButtonClone = $list.find('.lpc-row-button').removeClass('show').clone();
- $list.find('.lpc-row-button').remove()
- let templatesContainer = '';
- this.items.forEach(item => {
- for(let i = 0; i < itemsFolder.length; i++){
- if(itemsFolder[i] == item.folder_id){
- let link = '/' + item.alias;
- //var image = item.folder_image ? '/d/' + item.folder_image.filename : null;
- let name = item.folder_name;
- let template = `<li class="lpc-menu-horizontal__item ${lpcMenuView}" data-lp-selector=".lpc-menu-horizontal__item" data-elem-type="container">
- <a class="lpc-menu-horizontal__link lp-button ${lpcButtons}" href="${link}" data-lp-selector=".lpc-menu-horizontal__link" data-elem-type="text">
- ${name}
- <span class="lpc-menu-horizontal__arrow" data-elem-type="container" data-lp-selector=".lpc-menu-horizontal__arrow-line">
- <span class="lpc-menu-horizontal__arrow-line"></span>
- <span class="lpc-menu-horizontal__arrow-line"></span>
- </span>
- </a>
- </li>`;
- //let tempDiv = document.createElement( "div" );
- //tempDiv.innerHTML = template;
- //$list.prepend( tempDiv.childNodes[0] );
- templatesContainer += template;
- }
- }
- });
- //$list.find('.items-folder-remove').remove();
- $list.html(templatesContainer);
- $list.append(lpcRowButtonClone);
- if(!this.$block.hasClass('lpc-simple-menu')){
- this.$block.addClass('lpc-row-menu');
- lpc_template.queue.rowMenu($('body'));
- }
- }
- dropDownListHtml(data) {
- var rootUL = document.createElement('ul');
- var levelObject = {};
- var parentLi = null;
- data.shift();
- data.forEach(item => {
- var li = document.createElement('li');
- var div = document.createElement('div');
- div.className = 'lpc-query-folder__option lpc-query__option';
- div.setAttribute('data-folder_id', item.folder_id);
- var span = document.createElement('span');
- function decodeHtmlEntities(input) {
- var doc = new DOMParser().parseFromString(input, "text/html");
- return doc.documentElement.textContent;
- }
- span.textContent = decodeHtmlEntities( item.folder_name );
- div.title = decodeHtmlEntities( item.folder_name );
- //var i = document.createElement('i');
- //i.textContent = `(${item.items})`;
- div.appendChild(span);
- //div.appendChild(i);
- li.appendChild(div);
- if(this.arrayItemsFolder){
- if(this.helperFunction.in_array(item.folder_id, this.arrayItemsFolder)){
- div.classList.add('active');
- }
- }
- let level = Number(item._level);
- if (level == 1) {
- li.classList.add('first-level')
- rootUL.appendChild(li);
- parentLi = li;
- levelObject = {};
- } else {
- if (!levelObject[level]) {
- levelObject[level] = document.createElement('ul');
- parentLi.appendChild(levelObject[level]);
- parentLi.classList.add('hasChild')
- }
- levelObject[level].appendChild(li);
- parentLi = li;
- }
- });
- return rootUL;
- }
- async getFolderFetch(notFolder) {
- try {
- // Создаем параметры запроса
- let params = new URLSearchParams();
- params.append('param[type]', 'shop2');
- params.append('param[depth]', 99);
- // Формируем URL для GET-запроса
- let url = '/my/s3/xapi/public/?method=folder/getTree&' + params.toString();
- // Отправляем GET-запрос с использованием fetch
- let response = await fetch(url, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- if (!response.ok) {
- throw new Error('Ошибка при выполнении запроса');
- }
- let data = await response.json();
- if (data.result.success) {
- let items = data.result.items;
- // Нет категории магазина
- if (!items.length) {
- notFolder();
- return false;
- }
- return items;
- }
- } catch (error) {
- console.error('Ошибка при выполнении запроса:', error);
- return false;
- }
- }
- initSelect(){
- let _this = this;
- (async function () {
- _this.helperFunction.selectOpen();
- await _this.createSelectOption();
- let queryFolderOption = _this.queryFolder.find('.lpc-query-folder__option'); /* option select */
- queryFolderOption.on('click', function (e) {
- queryFolderOption.removeClass('active');
- let $this = $(this);
- let $li = $this.closest('li');
- let $ul = $li.children(' ul');
- var $firstLevel = _this.queryFolder.find('.first-level')
- let $option = $ul.children('li').children('.lpc-query-folder__option');
- if($this.hasClass('all-folders')){
- $option = $firstLevel.find(' > .lpc-query-folder__option');
- }
- $option.addClass('active');
- let srtBlock = '';
- let len = $option.length - 1;
- $option.each(function(index){
- let last = ',';
- if(index == len){
- last = '';
- }
- srtBlock += $(this).attr('data-folder_id') + last;
- });
- //console.log( srtBlock )
- _this.itemsFolderId
- .text(srtBlock)
- .addClass('modified modified-text');
- _this.createButton();
- // if (!flagLoadFolder) {
- // queryFolder.addClass('folder-selected')
- // }
- });
- if(_this.itemsFolderId.text() && _this.itemsFolderId.text() != 'not_found'){
- _this.createButton();
- }
- })();
- }
- init(){
- this.initSelect();
- }
- };
- lpc_template.queue.folderBlock = function ($self) {
- let $blocks = $self.hasClass('lpc-folder-block') ? $self : $self.find('.lpc-folder-block');
- if ($blocks.length) {
- $blocks.each(function () {
- let $block = $(this);
- if (s3LP.is_cms) {
- var folderBlock = new FolderBlockButton($block);
- folderBlock.init();
- }
- })
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement