Source: components/instant-search/instant-search-block/instant-search-result-block-empty.js

import jQ from "jquery";

import Class from "../../../helpers/class";
import Labels from "../../../helpers/labels";
import Utils from "../../../helpers/utils";
import Globals from "../../../helpers/globals";
import InstantSearchEnum from "../../../enum/instant-search-enum";
import BaseComponent from "../../base-component";

/**
 * Instant search result - Empty block
 * @extends BaseComponent
 */
class InstantSearchResultBlockEmpty extends BaseComponent {
	/**
	 * @constructs
	 */
	constructor() {
		super();
		/**
		 * This is jQuery object and contains the DYM-DOM as well
		 * @type {Object}
		 */
		this.$element = null;
	}

	/**
	 * Get the raw html template of Empty result block
	 * @returns {String} The raw HTML template
	 */
	getTemplate() {
		return `
			<li class="{{class.searchSuggestion}}-no-result {{class.searchSuggestionItem}}" data-label="No Results: {{searchTerm}}" data-value="{{searchTerm}}" aria-label="No Results">
				<span>{{noResultLabel}}</span>
			</li>
		`;
	}

	/**
	 * Replace the brackets in raw html template with proper values
	 * @returns {String} HTML string (include result data)
	 */
	compileTemplate() {
		var searchTerm = Utils.escape(Globals.currentTerm);
		var noResultLabel = Labels.error.noSuggestionResult.replace(/{{ terms }}/g, '<strong>' + searchTerm + '</strong>');
		return this.getTemplate()
			.replace(/{{class.searchSuggestion}}/g, Class.searchSuggestion)
			.replace(/{{class.searchSuggestionItem}}/g, Class.searchSuggestionItem)
			.replace(/{{searchTerm}}/g, searchTerm)
			.replace(/{{noResultLabel}}/g, noResultLabel);
	}

	/**
	 * Render the empty block
	 * @returns {Object} jQuery object
	 */
	render() {
		if (!this.hasRedirect) {
			this.$element = jQ(this.compileTemplate());
		} else {
			this.$element = null;
		}
	}

	/**
	 * Set data for the empty component
	 * @param {Object} data the suggestion result data
	 */
	setData(data) {
		if (data) {
			this.data = data;
			this.hasRedirect = Utils.getValueInObjectArray(InstantSearchEnum.ResultType.REDIRECT, this.data);
		} else {
			this.data = null;
			this.hasRedirect = false;
		}
	}
}

export default InstantSearchResultBlockEmpty;