Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
tao_helpers_form_elements_xhtml_Viewablehiddenbox | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
render | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
19 | * (under the project TAO-TRANSFER); |
20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
22 | * |
23 | */ |
24 | |
25 | use oat\tao\helpers\form\elements\xhtml\XhtmlRenderingTrait; |
26 | |
27 | /** |
28 | * Short description of class tao_helpers_form_elements_xhtml_ViewableHiddenbox |
29 | * |
30 | * @access public |
31 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
32 | * @package tao |
33 | */ |
34 | class tao_helpers_form_elements_xhtml_Viewablehiddenbox extends tao_helpers_form_elements_Viewablehiddenbox |
35 | { |
36 | use XhtmlRenderingTrait; |
37 | |
38 | /** |
39 | * Short description of method render |
40 | * |
41 | * @access public |
42 | * @author Christophe Noël <christophe@taotesting.com> |
43 | * @return string |
44 | */ |
45 | public function render() |
46 | { |
47 | $uid = helpers_Random::generateString(24); |
48 | |
49 | $this->addClass('viewable-hiddenbox-input'); |
50 | $this->addAttribute('data-identifier', $uid); |
51 | |
52 | $value = _dh($this->value); |
53 | |
54 | $html = <<<HTML |
55 | <span class="viewable-hiddenbox"> |
56 | {$this->renderLabel()} |
57 | <input type='password' name='{$this->name}' id='{$this->name}' {$this->renderAttributes()} value='{$value}'/> |
58 | <span class="viewable-hiddenbox-toggle" data-identifier="{$uid}"></span> |
59 | </span> |
60 | HTML; |
61 | |
62 | $script = <<<SCRIPT |
63 | <script type="text/javascript"> |
64 | (function() { |
65 | var input = document.querySelector('input[data-identifier="$uid"]'), |
66 | toggle = document.querySelector('.viewable-hiddenbox-toggle[data-identifier="$uid"]'), |
67 | |
68 | iconView = document.createElement('span'), |
69 | iconHide = document.createElement('span'); |
70 | |
71 | var show = function() { |
72 | if (iconView.parentElement) { |
73 | toggle.removeChild(iconView); |
74 | } |
75 | if (!iconHide.parentElement) { |
76 | toggle.appendChild(iconHide); |
77 | } |
78 | input.type = 'text'; |
79 | input.autocomplete = 'off'; |
80 | window.addEventListener('mousedown', autoHide); // make sure always submit the form with an password input |
81 | input.focus(); |
82 | }; |
83 | |
84 | var hide = function() { |
85 | if (!iconView.parentElement) { |
86 | toggle.appendChild(iconView); |
87 | } |
88 | if (iconHide.parentElement) { |
89 | toggle.removeChild(iconHide); |
90 | } |
91 | input.type = 'password'; |
92 | input.autocomplete = 'on'; |
93 | window.removeEventListener('mousedown', autoHide); |
94 | }; |
95 | |
96 | var autoHide = function(event) { |
97 | if ( |
98 | !event.target.isSameNode(input) |
99 | && !event.target.isSameNode(iconHide) |
100 | && !event.target.isSameNode(toggle) |
101 | ) { |
102 | hide(); |
103 | } |
104 | }; |
105 | |
106 | iconView.classList.add('icon-preview'); |
107 | iconHide.classList.add('icon-eye-slash'); |
108 | hide(); |
109 | |
110 | toggle.addEventListener('click', function() { |
111 | if (input.type === 'password') { |
112 | show(); |
113 | } else { |
114 | hide(); |
115 | } |
116 | }); |
117 | })(); |
118 | </script> |
119 | SCRIPT; |
120 | |
121 | return (string) ($html . $script); |
122 | } |
123 | } |