phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlFormInput.php
Go to the documentation of this file.
1 <?php
3 
6 
7 class HtmlFormInput extends HtmlFormField {
8  use TextFieldsTrait;
9 
10  const TOGGLE_CLICK = 0;
11 
12  const TOGGLE_MOUSEDOWN = 1;
13 
14  const TOGGLE_INTERVAL = 2;
15 
16  public function __construct($identifier, $label = NULL, $type = "text", $value = NULL, $placeholder = NULL) {
17  if (! isset($placeholder) && $type === "text")
18  $placeholder = $label;
19  parent::__construct("field-" . $identifier, new HtmlInput($identifier, $type, $value, $placeholder), $label);
20  $this->_identifier = $identifier;
21  }
22 
23  public function getDataField() {
24  $field = $this->getField();
25  if ($field instanceof HtmlInput)
26  $field = $field->getDataField();
27  return $field;
28  }
29 
35  public function asPassword($keyIcon = 'key') {
36  $this->setInputType('password');
37  if ($keyIcon != '') {
38  $this->addIcon($keyIcon);
39  }
40  }
41 
52  public function addTogglePasswordAction($buttonIcon = 'eye', $keyIcon = 'key', $slashIcon = 'slash', $type = 0) {
53  $this->asPassword($keyIcon);
54  $action = $this->addAction('see');
55  $action->asIcon($buttonIcon);
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  }
70  return $action;
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  }
76 }
addTogglePasswordAction($buttonIcon='eye', $keyIcon='key', $slashIcon='slash', $type=0)
Adds an action to show/hide the password.
getJsToggle($slashIcon, $type, $actionClass)
__construct($identifier, $label=NULL, $type="text", $value=NULL, $placeholder=NULL)
addAction($action, $direction=Direction::RIGHT, $icon=NULL, $labeled=false)
Definition: FieldTrait.php:79
asPassword($keyIcon='key')
Changes the input type to password and adds an icon.
addIcon($icon, $direction=Direction::LEFT)