Files
notifications-admin/app/assets/javascripts/apiKey.js
T

61 lines
1.8 KiB
JavaScript
Raw Normal View History

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) => `
<span class="api-key__key">${key}</span>
<input type='button' class='govuk-button govuk-button--secondary 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 => `
<span class="api-key__key">Copied to clipboard</span>
<input type='button' class='govuk-button govuk-button--secondary 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
.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', () =>
2016-01-15 10:50:10 +00:00
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
);
if ('stickAtBottomWhenScrolling' in GOVUK) {
GOVUK.stickAtBottomWhenScrolling.recalculate();
}
2016-01-15 10:50:10 +00:00
};
};
})(window.GOVUK.Modules);