Source: components/filter/filter-result/filter-result-element/page-info-search.js

import jQ from "jquery";
import BaseComponent from "../../../base-component";
import Settings from "../../../../helpers/settings";
import Labels from "../../../../helpers/labels";
import Globals from "../../../../helpers/globals";
import Utils from "../../../../helpers/utils";
import Class from "../../../../helpers/class";

/**
 * Page collection info
 * @extends BaseComponent
 */
class PageInfoSearch extends BaseComponent {
	/**
	 * @constructs
	 */
	constructor() {
		super();
		this.data = null;
		Globals.currentTerm = Utils.getSearchTerm();
		this.$searchResultHeader = jQ('.' + Class.searchResultHeader);
		this.$searchResultNumber = jQ('.' + Class.searchResultNumber);
	}

	init() {
		if (Globals.currentTerm) {
			this.searchTerm = Utils.escape(Globals.currentTerm.trim());
		}
	}

	/**
	 * Replace the brackets in raw html template with proper values for SEO title
	 * @returns {String} HTML string
	 */
	compileSEOTitleTemplate() {
		var seoTitle = this.data.total_product <= 1 ? Labels.search.seoTitleOne : Labels.search.seoTitleOther;
		if (seoTitle) {
			seoTitle = seoTitle.replace(/{{ count }}/g, this.data.total_product).replace(/{{ terms }}/g, this.searchTerm);
		}
		return seoTitle;
	}

	/**
	 * Replace the brackets in raw html template with proper values for search result heading
	 * @returns {String} HTML string
	 */
	compileSearchResultHeader() {
		if (this.searchTerm) {
			var content = this.data.total_product > 0 ? Labels.search.resultHeader : Labels.search.resultEmpty;
		} else {
			var content = Labels.search.generalTitle;
		}
		return content.replace(/{{ terms }}/g, this.searchTerm);
	}

	/**
	 * Replace the brackets in raw html template with proper values for search result number
	 * @returns {String} HTML string
	 */
	compileSearchResultNumber() {
		var result = '';
		if (this.searchTerm) {
			result = Labels.search.resultNumber;
		}
		return result
			.replace(/{{ count }}/g, '<strong>' + this.data.total_product + '</strong>')
			.replace(/{{ terms }}/g, '<strong>' + this.searchTerm + '</strong>');
	}

	/**
	 * Return whether or not the search page info is rendered
	 */
	isRender() {
		return !!this.data;
	}

	/**
	 * Render the search page info
	 */
	render() {
		// Build Search result for SEO title
		this._buildSEOTitle();
		// Build header of Search result
		this._buildSearchResultHeader();
		// Build number of Search result
		this._buildSearchResultNumber();
	}

	/**
	 * Set data for search page info
	 * @param {Object} data - The filter result data
	 */
	setData(data) {
		if (data) {
			this.data = data;
		}
	}

	/**
	 * Build Search result for SEO title
	 */
	_buildSEOTitle() {
		var enableFixHeadTitle = Settings.getSettingValue('search.enableFixHeadTitle');
		if (enableFixHeadTitle) {
			var seoTitle = this.compileSEOTitleTemplate();
			if (seoTitle && this.searchTerm && this.searchTerm !== '') {
				// Set SEL title for title tag
				document.title = seoTitle.replace(/&quot;/g,'"');
			}
		}
	}

	/**
	 * Build header of Search result
	 */
	_buildSearchResultHeader() {
		var headerLabel = this.compileSearchResultHeader();
		this.$searchResultHeader.html(headerLabel);
	}

	/**
	 * Build number of Search result
	 */
	_buildSearchResultNumber() {
		var resultHTML = this.compileSearchResultNumber();
		this.$searchResultNumber.html(resultHTML);
	}
}

export default PageInfoSearch;