|
7 | 7 | class HtmlFormInput extends HtmlFormField { |
8 | 8 | use TextFieldsTrait; |
9 | 9 |
|
| 10 | + const TOGGLE_CLICK = 0; |
| 11 | + |
| 12 | + const TOGGLE_MOUSEDOWN = 1; |
| 13 | + |
| 14 | + const TOGGLE_INTERVAL = 2; |
| 15 | + |
10 | 16 | public function __construct($identifier, $label = NULL, $type = "text", $value = NULL, $placeholder = NULL) { |
11 | 17 | if (! isset($placeholder) && $type === "text") |
12 | 18 | $placeholder = $label; |
@@ -34,18 +40,37 @@ public function asPassword($keyIcon = 'key') { |
34 | 40 | } |
35 | 41 |
|
36 | 42 | /** |
37 | | - * Adds an action to show/hide the password |
| 43 | + * Adds an action to show/hide the password. |
38 | 44 | * |
39 | 45 | * @param string $buttonIcon |
40 | 46 | * @param string $keyIcon |
41 | 47 | * @param string $slashIcon |
| 48 | + * @param string $type |
| 49 | + * one of TOGGLE_CLICK, TOGGLE_MOUSEDOWN, TOGGLE_INTERVAL |
42 | 50 | * @return mixed|\Ajax\semantic\html\elements\HtmlButton |
43 | 51 | */ |
44 | | - public function addTogglePasswordAction($buttonIcon = 'eye', $keyIcon = 'key', $slashIcon = 'slash') { |
| 52 | + public function addTogglePasswordAction($buttonIcon = 'eye', $keyIcon = 'key', $slashIcon = 'slash', $type = 0) { |
45 | 53 | $this->asPassword($keyIcon); |
46 | 54 | $action = $this->addAction('see'); |
47 | 55 | $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 | + } |
49 | 70 | return $action; |
50 | 71 | } |
| 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 | + } |
51 | 76 | } |
0 commit comments