Passed
Push — master ( 1d27da...a57f3d )
by Jean-Christophe
02:11
created

HtmlFormInput::asPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 4
rs 10
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
	public function __construct($identifier, $label = NULL, $type = "text", $value = NULL, $placeholder = NULL) {
11
		if (! isset($placeholder) && $type === "text")
12
			$placeholder = $label;
13
		parent::__construct("field-" . $identifier, new HtmlInput($identifier, $type, $value, $placeholder), $label);
14
		$this->_identifier = $identifier;
15
	}
16
17
	public function getDataField() {
18
		$field = $this->getField();
19
		if ($field instanceof HtmlInput)
20
			$field = $field->getDataField();
21
		return $field;
22
	}
23
24
	/**
25
	 * Changes the input type to password and adds an icon
26
	 *
27
	 * @param string $keyIcon
28
	 */
29
	public function asPassword($keyIcon = 'key') {
30
		$this->setInputType('password');
31
		if ($keyIcon != '') {
32
			$this->addIcon($keyIcon);
33
		}
34
	}
35
36
	/**
37
	 * Adds an action to show/hide the password
38
	 *
39
	 * @param string $buttonIcon
40
	 * @param string $keyIcon
41
	 * @param string $slashIcon
42
	 * @return mixed|\Ajax\semantic\html\elements\HtmlButton
43
	 */
44
	public function addTogglePasswordAction($buttonIcon = 'eye', $keyIcon = 'key', $slashIcon = 'slash') {
45
		$this->asPassword($keyIcon);
46
		$action = $this->addAction('see');
47
		$action->asIcon($buttonIcon);
48
		$action->onClick('$(this).find(".icon").toggleClass("' . $slashIcon . '");$(this).closest(".field").find("input").attr("type",(_,attr)=>(attr=="text")?"password":"text")');
49
		return $action;
50
	}
51
}
52