Source: components/filter/filter-result/filter-result.js

import jQ from 'jquery';

import BaseComponent from "../../base-component";
import PageInfoSearch from './filter-result-element/page-info-search';
import PageInfoCollection from './filter-result-element/page-info-collection';
import RobotsMeta from "./filter-result-element/robots-meta";
import ProductList from "./product/product-list";
import ProductPagination from "./filter-result-element/pagination/product-pagination";
import ProductPaginationDefault from "./filter-result-element/pagination/product-pagination-default";
import ProductPaginationLoadMore from "./filter-result-element/pagination/product-pagination-load-more";
import ProductPaginationInfinite from "./filter-result-element/pagination/product-pagination-infinite";
import ProductPaginationLoadPrevious from "./filter-result-element/pagination/product-pagination-load-previous";
import SearchDisplayPagination from "./filter-result-element/pagination/search-display-pagination";
import ProductSorting from "./filter-result-element/product-sorting";
import ProductLimit from "./filter-result-element/product-limit";
import ProductDisplayType from "./filter-result-element/product-display-type";
import ProductListPlaceholder from "./product/product-list-placeholder";
import Selector from "../../../helpers/selector";
import Settings from "../../../helpers/settings";
import Utils from "../../../helpers/utils";
import SearchResultPanels from './filter-result-element/search-result-panels';
import CollectionList from './collection/collection-list';
import PageList from './page/page-list';
import SearchResultPanelItem from './filter-result-element/search-result-panel-item';
import SearchResultTotal from './filter-result-element/search-result-total';


/**
 * Filter result
 */
class FilterResult extends BaseComponent {
	/**
	 * @constructs
	 */
	constructor() {
		super();
		this.data = null;
		this.productList = null;
		this.totalProduct = 0;
		this.pagination = '';
		this.sorting = '';
		this.breadcrumbs = '';
		this.eventType = '';
		this.displayType = '';
		/**
		 * The Filter result element (product list)
		 * @type {Object}
		 */
		this.$element = jQ(Selector.products);
		this.settngs = {
			paginationType: Settings.getSettingValue('general.paginationType')
		};
	};

	/**
	 * 
	 */
	init() {
		// Add Product list placeholder
		this.productPlaceholder = new ProductListPlaceholder();
		this.addComponent(this.productPlaceholder);

		// Add Product list component
		this.productList = new ProductList();
		this.addComponent(this.productList);

		// Add Pagination component
		var ProductPaginationClass = this._getProductPaginationClass();
		this.pagination = new ProductPaginationClass();
		this.addComponent(this.pagination);

		// Add Load previous page component
		if (Utils.isLoadPreviousPagePaginationType()) {
			this.loadPrevious = new ProductPaginationLoadPrevious();
			this.addComponent(this.loadPrevious);
		}

		// Add Sorting component
		this.sorting = new ProductSorting();
		this.addComponent(this.sorting);

		// Add product limit component
		this.limit = new ProductLimit();
		this.addComponent(this.limit);

		// Add display type component
		this.displayType = new ProductDisplayType();
		this.addComponent(this.displayType);

		// Add page info component
		var PageInfoClass = this._getPageInfoClass();
		this.pageInfo = new PageInfoClass();
		this.addComponent(this.pageInfo);

		// Add rebots meta tag
		this.rebotsMeta = new RobotsMeta();
		this.addComponent(this.rebotsMeta);

		if (Utils.isSearchPage()) {
			// Search result panels in Search page
			this.searchResultPanels = new SearchResultPanels();
			this.addComponent(this.searchResultPanels);
			// Add Collection list component
			this.collectionList = new CollectionList();
			this.addComponent(this.collectionList);
			// Add collection list pagination
			this.collectionListPagination = new SearchDisplayPagination(Selector.searchCollectionPagination, SearchResultPanelItem.Enum.COLLECTION);
			this.addComponent(this.collectionListPagination);
			// Add Page list component
			this.pageList = new PageList();
			this.addComponent(this.pageList);
			// Add page list pagination
			this.pageListPagination = new SearchDisplayPagination(Selector.searchPagePagination, SearchResultPanelItem.Enum.PAGE);
			this.addComponent(this.pageListPagination);
			// Add search total result
			this.searchResultTotal = new SearchResultTotal();
			this.addComponent(this.searchResultTotal);
		}
	}

	/**
	 * Returns whether or not the filter result is rendered
	 */
	isRender() {
		return this.parent.isFetchedProductData;
	}

	/**
	 * Set up date for the filter result
	 * @param {Object} data - The filter result data 
	 */
	setData(data) {
		this.data = data;
		//this.children = [];
		this.totalProduct = data.total_product;
		this.eventType = data.event_type;

		// FIXME: Don't need to check productTotal > 0
		// Set data for product list
		this.productList.setData(data.products);
		// Set data for pagination
		this.pagination.setData(data);
		// Set data for load previous button
		if (this.loadPrevious) {
			this.loadPrevious.setData(data);
		}

		if (Utils.isSearchPage()) {
			// Set data for search result panels
			this.searchResultPanels.setData(data, this.eventType);
			// Set data for Collection list
			this.collectionList.setData(data, this.eventType);
			// Set data for Page list
			this.pageList.setData(data, this.eventType);
			if (data.hasOwnProperty('total_product')) {
				this.searchResultTotal.setData(data.total_product);
			}
		}

		// Set data for placeholder
		this.productPlaceholder.setData(data);
		// Build Page information
		this.pageInfo.setData(data);
	}

	/**
	 * Get the pagination class for default, load more or infinite loading
	 */
	_getProductPaginationClass() {
		switch (Settings.getSettingValue('general.paginationType')) {
			case ProductPagination.Type.DEFAULT:
				return ProductPaginationDefault;
			case ProductPagination.Type.LOAD_MORE:
				return ProductPaginationLoadMore;
			default:
				return ProductPaginationInfinite;
		}
	}

	/**
	 * Get page info for search page / collection page
	 */
	_getPageInfoClass() {
		return Utils.isSearchPage() ? PageInfoSearch : PageInfoCollection
	}
}

export default FilterResult;