import clipboardaction from './clipboard-action'; import emitter from 'tiny-emitter'; import listen from 'good-listener'; /** * base class which takes one or more elements, adds event listeners to them, * and instantiates a new `clipboardaction` on each click. */ class clipboard extends emitter { /** * @param {string|htmlelement|htmlcollection|nodelist} trigger * @param {object} options */ constructor(trigger, options) { super(); this.resolveoptions(options); this.listenclick(trigger); } /** * defines if attributes would be resolved using internal setter functions * or custom functions that were passed in the constructor. * @param {object} options */ resolveoptions(options = {}) { this.action = (typeof options.action === 'function') ? options.action : this.defaultaction; this.target = (typeof options.target === 'function') ? options.target : this.defaulttarget; this.text = (typeof options.text === 'function') ? options.text : this.defaulttext; this.container = (typeof options.container === 'object') ? options.container : document.body; } /** * adds a click event listener to the passed trigger. * @param {string|htmlelement|htmlcollection|nodelist} trigger */ listenclick(trigger) { this.listener = listen(trigger, 'click', (e) => this.onclick(e)); } /** * defines a new `clipboardaction` on each click event. * @param {event} e */ onclick(e) { const trigger = e.delegatetarget || e.currenttarget; if (this.clipboardaction) { this.clipboardaction = null; } this.clipboardaction = new clipboardaction({ action : this.action(trigger), target : this.target(trigger), text : this.text(trigger), container : this.container, trigger : trigger, emitter : this }); } /** * default `action` lookup function. * @param {element} trigger */ defaultaction(trigger) { return getattributevalue('action', trigger); } /** * default `target` lookup function. * @param {element} trigger */ defaulttarget(trigger) { const selector = getattributevalue('target', trigger); if (selector) { return document.queryselector(selector); } } /** * returns the support of the given action, or all actions if no action is * given. * @param {string} [action] */ static issupported(action = ['copy', 'cut']) { const actions = (typeof action === 'string') ? [action] : action; let support = !!document.querycommandsupported; actions.foreach((action) => { support = support && !!document.querycommandsupported(action); }); return support; } /** * default `text` lookup function. * @param {element} trigger */ defaulttext(trigger) { return getattributevalue('text', trigger); } /** * destroy lifecycle. */ destroy() { this.listener.destroy(); if (this.clipboardaction) { this.clipboardaction.destroy(); this.clipboardaction = null; } } } /** * helper function to retrieve attribute value. * @param {string} suffix * @param {element} element */ function getattributevalue(suffix, element) { const attribute = `data-clipboard-${suffix}`; if (!element.hasattribute(attribute)) { return; } return element.getattribute(attribute); } export default clipboard;