UNPKG

coveo-search-ui-extensions

Version:

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

86 lines 3.98 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { ResultAction } from '../ResultAction/ResultAction'; import { ComponentOptions, l, StringUtils, Initialization } from 'coveo-search-ui'; import { copy } from '../../utils/icons'; import './Strings'; export class CopyToClipboard extends ResultAction { /** * 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 result The result of the query in which this resultAction exists. */ constructor(element, options, bindings, result) { super(element, ComponentOptions.initComponentOptions(element, CopyToClipboard, options), bindings, result); this.element = element; this.options = options; this.bindings = bindings; this.result = result; super.init(); } doAction() { this.usageAnalytics.logClickEvent({ name: 'copyToClipboard', type: 'resultAction' }, {}, this.result, this.element); this.copyToClipboard(StringUtils.buildStringTemplateFromResult(this.options.template, this.result)); } copyToClipboard(text) { return __awaiter(this, void 0, void 0, function* () { if (navigator && navigator.clipboard && navigator.clipboard.writeText) { try { yield navigator.clipboard.writeText(text); } catch (err) { this.logger.error('Copy to clipboard failed.', text, err); this.copyToClipboardFallback(text); } } else { this.copyToClipboardFallback(text); } this.setToolipText(this.options.successTooltip); this.refreshTooltip(); }); } refreshTooltip() { setTimeout(() => this.setToolipText(this.options.tooltip), 500); } setToolipText(text) { const tooltipElement = this.element.querySelector('.coveo-caption-for-icon'); if (tooltipElement && text) { tooltipElement.innerText = text; } } /** * Sadly that's the only way of doing in in IE11 and in lockerservice. */ copyToClipboardFallback(text) { const el = document.createElement('textarea'); el.value = text; document.body.appendChild(el); el.select(); document.execCommand('copy'); document.body.removeChild(el); } } CopyToClipboard.ID = 'CopyToClipboard'; /** * The possible options for _ResultAction_. * @componentOptions */ CopyToClipboard.options = { icon: ComponentOptions.buildStringOption({ defaultValue: copy }), tooltip: ComponentOptions.buildCustomOption((tooltip) => tooltip, { defaultFunction: () => l('CopyToClipboard_copy') }), successTooltip: ComponentOptions.buildCustomOption((tooltip) => tooltip, { defaultFunction: () => l('CopyToClipboard_copied') }), template: ComponentOptions.buildStringOption({ defaultValue: '${title}\n${clickUri}' }), }; Initialization.registerComponentFields(CopyToClipboard.ID, ['title', 'clickUri']); Initialization.registerAutoCreateComponent(CopyToClipboard); //# sourceMappingURL=CopyToClipboard.js.map