-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPasswordRevealInput.php
More file actions
113 lines (93 loc) · 2.85 KB
/
Copy pathPasswordRevealInput.php
File metadata and controls
113 lines (93 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace ADT\Forms\Controls;
use Nette\Forms\Container;
use Nette\Forms\Form;
use Nette\Forms\Controls\TextInput;
use Nette\Utils\Html;
class PasswordRevealInput extends TextInput
{
public const string INPUT_CLASS = 'toggle-password-input';
public const string TOGGLE_CLASS = 'toggle-password-reveal';
public function __construct($label = null, ?int $maxLength = null)
{
parent::__construct($label, $maxLength);
$this->setHtmlType('password');
$this->getControlPrototype()->appendAttribute('class', self::INPUT_CLASS);
}
public function getControl(): Html
{
$input = parent::getControl();
$input->value = $this->getRenderedValue();
$button = Html::el('button')
->setAttribute('type', 'button')
->setAttribute('tabindex', '-1')
->appendAttribute('class', 'btn btn-outline-secondary ' . self::TOGGLE_CLASS)
->addHtml(Html::el('i')->setAttribute('class', 'fas fa-eye'));
$group = Html::el('div')
->setAttribute('class', 'input-group')
->addHtml($input)
->addHtml($button)
->addHtml(self::getScript());
return $group;
}
public static function addPasswordReveal(Container $container, string $name, $label = null): self
{
$component = new self($label);
$container->addComponent($component, $name);
return $component;
}
public static function register(): void
{
Form::extensionMethod('addPasswordReveal', [self::class, 'addPasswordReveal']);
Container::extensionMethod('addPasswordReveal', [self::class, 'addPasswordReveal']);
}
private static function getScript(): Html
{
$js = <<<JS
(function () {
if (window.__adtPasswordRevealInitialized) {
return;
}
window.__adtPasswordRevealInitialized = true;
var inputSelector = 'input.{INPUT_CLASS}';
function mask(input) {
if (input.dataset.passwordRevealMasked === '1') {
return;
}
input.dataset.passwordRevealMasked = '1';
input.type = 'password';
}
function maskAll() {
document.querySelectorAll(inputSelector).forEach(mask);
}
document.addEventListener('click', function (e) {
var button = e.target.closest('.{TOGGLE_CLASS}');
if (!button) {
return;
}
e.preventDefault();
var group = button.closest('.input-group');
var input = group ? group.querySelector(inputSelector) : null;
if (!input) {
return;
}
var masked = input.type === 'password';
input.type = masked ? 'text' : 'password';
var icon = button.querySelector('i');
if (icon) {
icon.classList.toggle('fa-eye', !masked);
icon.classList.toggle('fa-eye-slash', masked);
}
});
new MutationObserver(maskAll).observe(document.documentElement, { childList: true, subtree: true });
maskAll();
})();
JS;
$js = strtr($js, [
'{INPUT_CLASS}' => self::INPUT_CLASS,
'{TOGGLE_CLASS}' => self::TOGGLE_CLASS,
]);
return Html::el('script')->setHtml($js);
}
}