Debian Codegraph @

vendor/ipl/web/src/Widget/CopyToClipboard.php

raw
1 <?php
2
3 namespace ipl\Web\Widget;
4
5 use ipl\Html\Attributes;
6 use ipl\Html\BaseHtmlElement;
7 use ipl\Html\HtmlElement;
8 use ipl\I18n\Translation;
9
10 /**
11 * Copy to clipboard button
12 */
13 class CopyToClipboard extends BaseHtmlElement
14 {
15 use Translation;
16
17 protected $tag = 'button';
18
19 protected $defaultAttributes = ['type' => 'button'];
20
21 /**
22 * Create a copy to clipboard button
23 *
24 * Creates a copy to clipboard button, which when clicked copies the text from the html element identified as
25 * clipboard source that the clipboard button attaches itself to.
26 */
27 private function __construct()
28 {
29 $this->addAttributes(
30 [
31 'class' => 'copy-to-clipboard',
32 'data-icinga-clipboard' => true,
33 'tabindex' => -1,
34 'data-copied-label' => $this->translate('Copied'),
35 'title' => $this->translate('Copy to clipboard'),
36 ]
37 );
38 }
39
40 /**
41 * Attach the copy to clipboard button to the given Html source element
42 *
43 * @param BaseHtmlElement $source
44 *
45 * @return void
46 */
47 public static function attachTo(BaseHtmlElement $source): void
48 {
49 $clipboardWrapper = new HtmlElement(
50 'div',
51 Attributes::create(['class' => 'clipboard-wrapper'])
52 );
53
54 $clipboardWrapper->addHtml(new static());
55
56 $source->addAttributes(['data-clipboard-source' => true]);
57 $source->prependWrapper($clipboardWrapper);
58 }
59
60 public function assemble(): void
61 {
62 $this->setHtmlContent(new Icon('clone'));
63 }
64 }