2016-01-15 10:50:10 +00:00
|
|
|
(function(Modules) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2016-01-19 09:55:13 +00:00
|
|
|
if (!document.queryCommandSupported('copy')) return;
|
|
|
|
|
|
2016-01-15 10:50:10 +00:00
|
|
|
Modules.ApiKey = function() {
|
|
|
|
|
|
|
|
|
|
const states = {
|
2016-04-18 11:10:47 +01:00
|
|
|
'keyVisible': (key, thing) => `
|
2016-01-15 10:50:10 +00:00
|
|
|
<span class="api-key-key">${key}</span>
|
2016-04-18 11:10:47 +01:00
|
|
|
<input type='button' class='api-key-button-copy' value='Copy ${thing} to clipboard' />
|
2016-01-15 10:50:10 +00:00
|
|
|
`,
|
2016-04-18 11:10:47 +01:00
|
|
|
'keyCopied': thing => `
|
2016-01-15 10:50:10 +00:00
|
|
|
<span class="api-key-key">Copied to clipboard</span>
|
2016-04-18 11:10:47 +01:00
|
|
|
<input type='button' class='api-key-button-show' value='Show ${thing}' />
|
2016-01-15 10:50:10 +00:00
|
|
|
`
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
2016-01-19 09:55:13 +00:00
|
|
|
const $component = $(component),
|
2016-04-18 11:10:47 +01:00
|
|
|
key = $component.data('key'),
|
|
|
|
|
thing = $component.data('thing');
|
2016-01-15 10:50:10 +00:00
|
|
|
|
|
|
|
|
$component
|
2018-10-30 13:03:44 +00:00
|
|
|
.addClass('api-key')
|
|
|
|
|
.css('min-height', $component.height())
|
2016-04-18 11:10:47 +01:00
|
|
|
.html(states.keyVisible(key, thing))
|
2016-01-19 09:55:13 +00:00
|
|
|
.attr('aria-live', 'polite')
|
2016-01-15 10:50:10 +00:00
|
|
|
.on(
|
|
|
|
|
'click', '.api-key-button-copy', () =>
|
|
|
|
|
this.copyKey(
|
|
|
|
|
$('.api-key-key', component)[0], () =>
|
2016-04-18 11:10:47 +01:00
|
|
|
$component.html(states.keyCopied(thing))
|
2016-01-15 10:50:10 +00:00
|
|
|
)
|
2016-01-19 09:55:13 +00:00
|
|
|
)
|
|
|
|
|
.on(
|
|
|
|
|
'click', '.api-key-button-show', () =>
|
2016-04-18 11:10:47 +01:00
|
|
|
$component.html(states.keyVisible(key, thing))
|
2016-01-15 10:50:10 +00:00
|
|
|
);
|
|
|
|
|
|
2019-02-01 15:45:38 +00:00
|
|
|
if ('stickAtBottomWhenScrolling' in GOVUK) {
|
|
|
|
|
GOVUK.stickAtBottomWhenScrolling.recalculate();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 10:50:10 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(window.GOVUK.Modules);
|