UNPKG

coveo-search-ui-extensions

Version:

Small generic components to extend the functionality of Coveo's Search UI framework.

84 lines 3.01 kB
import { Component, ComponentOptions, $$ } from 'coveo-search-ui'; /** * The base class for all ResultAction components. * Its main responsibility is handling the visual elements of the Result Action. */ export class ResultAction extends Component { /** * Construct a ResultAction component. * @param element The HTML element bound to this component. * @param options The options that can be provided to this component. * @param bindings The bindings, or environment within which this component exists. * @param queryResult The result of the query in which this resultAction exists. */ constructor(element, options, bindings, queryResult) { super(element, ResultAction.ID, bindings); this.element = element; this.options = options; this.bindings = bindings; this.queryResult = queryResult; this.isInitialized = false; this.options = ComponentOptions.initComponentOptions(element, ResultAction, options); this.queryResult = this.queryResult || this.resolveResult(); // Hide until initialized. $$(this.element).addClass('coveo-hidden'); this.bind.on(this.element, 'click', () => this.doAction()); } /** * Initializes the component if it is not already initialized. */ init() { if (!this.isInitialized) { this.show(); this.isInitialized = true; } else { this.logger.debug('Attempted to initialize ResultAction that was already initialized.'); } } /** * Deactivate the component if it is initialized. * @param e The reason for the deactivation. */ deactivate(e) { $$(this.element).remove(); this.logger.warn(e); this.isInitialized = false; } /** * Make the result action button visible. */ show() { $$(this.element).removeClass('coveo-hidden'); if (this.options.icon) { const icon = document.createElement('span'); icon.innerHTML = this.options.icon; icon.className = 'coveo-icon'; this.element.appendChild(icon); } if (this.options.tooltip) { const tooltip = document.createElement('span'); tooltip.innerText = this.options.tooltip; tooltip.className = 'coveo-caption-for-icon'; this.element.appendChild(tooltip); } } } ResultAction.ID = 'ResultAction'; /** * The possible options for _ResultAction_. * @componentOptions */ ResultAction.options = { /** * See {@link IResultActionOptions.icon} * Optional. You may instead provide the icon by appending it as a child element. */ icon: ComponentOptions.buildStringOption(), /** * See {@link IResultActionOptions.tooltip} * Optional. If no tooltip is provided, the tooltip popup will not appear. */ tooltip: ComponentOptions.buildStringOption(), }; //# sourceMappingURL=ResultAction.js.map