Passed
Push — master ( a57f3d...497376 )
by Jean-Christophe
02:17
created

HtmlFormInput   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 34
c 3
b 0
f 2
dl 0
loc 68
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A addTogglePasswordAction() 0 19 4
A asPassword() 0 4 2
A getJsToggle() 0 2 1
A getDataField() 0 5 2
1
<?php
2
namespace Ajax\semantic\html\collections\form;
3
4
use Ajax\semantic\html\collections\form\traits\TextFieldsTrait;
5
use Ajax\semantic\html\elements\HtmlInput;
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
30
	/**
31
	 * Changes the input type to password and adds an icon
32
	 *
33
	 * @param string $keyIcon
34
	 */
35
	public function asPassword($keyIcon = 'key') {
36
		$this->setInputType('password');
37
		if ($keyIcon != '') {
38
			$this->addIcon($keyIcon);
39
		}
40
	}
41
42
	/**
43
	 * Adds an action to show/hide the password.
44
	 *
45
	 * @param string $buttonIcon
46
	 * @param string $keyIcon
47
	 * @param string $slashIcon
48
	 * @param string $type
49
	 *        	one of TOGGLE_CLICK, TOGGLE_MOUSEDOWN, TOGGLE_INTERVAL
50
	 * @return mixed|\Ajax\semantic\html\elements\HtmlButton
51
	 */
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
}
77