(function(Modules) { "use strict"; Modules.ApiKey = function() { const states = { 'initial': ` `, 'keyVisibleBasic': key => ` ${key} `, 'keyVisible': key => ` ${key} `, 'keyCopied': ` Copied to clipboard ` }; this.copyKey = function(keyElement, callback) { var selection = window.getSelection ? window.getSelection() : document.selection, range = document.createRange(); selection.removeAllRanges(); range.selectNodeContents(keyElement); selection.addRange(range); document.execCommand('copy'); selection.removeAllRanges(); callback(); }; this.start = function(component) { const $component = $(component).html(states.initial).attr('aria-live', 'polite'), key = $component.data('key'); $component .on( 'click', '.api-key-button-show', () => $component.html( document.queryCommandSupported('copy') ? states.keyVisible(key) : states.keyVisibleBasic(key) ) ) .on( 'click', '.api-key-button-copy', () => this.copyKey( $('.api-key-key', component)[0], () => $component.html(states.keyCopied) ) ); }; }; })(window.GOVUK.Modules);