2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-09-28 14:43:02 -04:00
|
|
|
import Component from '@glimmer/component';
|
2020-09-29 11:13:26 -04:00
|
|
|
import ClipboardJS from 'clipboard';
|
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
|
import { generateComponentID } from 'rose/utilities/component-auto-id';
|
2020-10-02 12:31:30 -04:00
|
|
|
import { tracked } from '@glimmer/tracking';
|
2022-01-02 14:07:25 -05:00
|
|
|
import { later } from '@ember/runloop';
|
2020-09-28 14:43:02 -04:00
|
|
|
|
2020-09-29 15:08:21 -04:00
|
|
|
export default class CopyableComponent extends Component {
|
2020-09-29 11:13:26 -04:00
|
|
|
// =attributes
|
|
|
|
|
|
|
|
|
|
id = generateComponentID();
|
2020-09-30 22:15:30 -04:00
|
|
|
copyableButtonId = `copyable-button-${this.id}`;
|
2020-10-02 12:31:30 -04:00
|
|
|
|
2021-09-16 14:03:26 -07:00
|
|
|
actionIcon = 'flight-icons/svg/clipboard-copy-16';
|
|
|
|
|
successIcon = 'flight-icons/svg/clipboard-checked-16';
|
2020-10-02 12:31:30 -04:00
|
|
|
actionText = this.args.buttonText;
|
|
|
|
|
successText = this.args.acknowledgeText;
|
|
|
|
|
|
|
|
|
|
@tracked copied = false;
|
2020-09-29 11:13:26 -04:00
|
|
|
|
2020-10-05 12:35:07 -04:00
|
|
|
clipboard = null;
|
|
|
|
|
|
2022-01-02 14:07:25 -05:00
|
|
|
confirmCopy() {
|
2020-10-02 12:31:30 -04:00
|
|
|
this.copied = true;
|
2022-01-02 14:07:25 -05:00
|
|
|
later(() => (this.copied = false), 1000);
|
|
|
|
|
}
|
2020-09-29 11:13:26 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks for ClipboardJS support.
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
get isClipboardLibrarySupported() {
|
2020-09-29 15:08:21 -04:00
|
|
|
return ClipboardJS.isSupported();
|
2020-09-29 11:13:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =actions
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize ClipboardJS library and register for it's events.
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
register() {
|
2021-03-05 17:27:00 -05:00
|
|
|
this.tearDown();
|
2020-10-05 12:35:07 -04:00
|
|
|
/* istanbul ignore next */
|
|
|
|
|
this.clipboard = new ClipboardJS(`#${this.copyableButtonId}`, {
|
|
|
|
|
text: () => this.args.text,
|
|
|
|
|
});
|
|
|
|
|
/* istanbul ignore next */
|
|
|
|
|
this.clipboard.on('success', (clipboardEvent) => {
|
|
|
|
|
clipboardEvent.clearSelection();
|
2022-01-02 14:07:25 -05:00
|
|
|
this.confirmCopy();
|
2020-10-05 12:35:07 -04:00
|
|
|
});
|
2020-09-29 11:13:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy ClipboardJS library instance and cancel icon timer.
|
|
|
|
|
*/
|
|
|
|
|
@action
|
2021-03-05 17:27:00 -05:00
|
|
|
tearDown() {
|
2020-09-29 11:13:26 -04:00
|
|
|
this.clipboard?.destroy();
|
2020-10-05 12:35:07 -04:00
|
|
|
this.clipboard = null;
|
2020-09-29 11:13:26 -04:00
|
|
|
}
|
2020-09-28 14:43:02 -04:00
|
|
|
}
|