Skip to content

Commit 4973762

Browse files
committed
add 3 modes for password show/hide
1 parent a57f3da commit 4973762

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

Ajax/semantic/html/collections/form/HtmlFormInput.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
class HtmlFormInput extends HtmlFormField {
88
use TextFieldsTrait;
99

10+
const TOGGLE_CLICK = 0;
11+
12+
const TOGGLE_MOUSEDOWN = 1;
13+
14+
const TOGGLE_INTERVAL = 2;
15+
1016
public function __construct($identifier, $label = NULL, $type = "text", $value = NULL, $placeholder = NULL) {
1117
if (! isset($placeholder) && $type === "text")
1218
$placeholder = $label;
@@ -34,18 +40,37 @@ public function asPassword($keyIcon = 'key') {
3440
}
3541

3642
/**
37-
* Adds an action to show/hide the password
43+
* Adds an action to show/hide the password.
3844
*
3945
* @param string $buttonIcon
4046
* @param string $keyIcon
4147
* @param string $slashIcon
48+
* @param string $type
49+
* one of TOGGLE_CLICK, TOGGLE_MOUSEDOWN, TOGGLE_INTERVAL
4250
* @return mixed|\Ajax\semantic\html\elements\HtmlButton
4351
*/
44-
public function addTogglePasswordAction($buttonIcon = 'eye', $keyIcon = 'key', $slashIcon = 'slash') {
52+
public function addTogglePasswordAction($buttonIcon = 'eye', $keyIcon = 'key', $slashIcon = 'slash', $type = 0) {
4553
$this->asPassword($keyIcon);
4654
$action = $this->addAction('see');
4755
$action->asIcon($buttonIcon);
48-
$action->onClick('$(this).find(".icon").toggleClass("' . $slashIcon . '");$(this).closest(".field").find("input").attr("type",(_,attr)=>(attr=="text")?"password":"text")');
56+
switch ($type) {
57+
case self::TOGGLE_CLICK:
58+
$action->onClick('let th=$(this);' . $this->getJsToggle($slashIcon, '(_,attr)=>(attr=="text")?"password":"text"', 'toggle'));
59+
break;
60+
case self::TOGGLE_MOUSEDOWN:
61+
$action->onClick('');
62+
$action->addEvent('mousedown', 'let th=$(this);' . $this->getJsToggle($slashIcon, '"text"', 'add'));
63+
$action->addEvent('mouseup', 'let th=$(this);' . $this->getJsToggle($slashIcon, '"password"', 'remove'));
64+
$action->addEvent('mouseout', 'let th=$(this);' . $this->getJsToggle($slashIcon, '"password"', 'remove'));
65+
break;
66+
case self::TOGGLE_INTERVAL:
67+
$action->onClick('let th=$(this);' . $this->getJsToggle($slashIcon, '"text"', 'add') . 'setTimeout(function(){ ' . $this->getJsToggle($slashIcon, '"password"', 'remove') . ' }, 5000);');
68+
break;
69+
}
4970
return $action;
5071
}
72+
73+
private function getJsToggle($slashIcon, $type, $actionClass) {
74+
return 'th.find(".icon").' . $actionClass . 'Class("' . $slashIcon . '");th.closest(".field").find("input").attr("type",' . $type . ');';
75+
}
5176
}

0 commit comments

Comments
 (0)