Passed
Push — master ( 9a9b82...74dc3c )
by Jean-Christophe
02:28
created

HtmlDoubleElement::asEditable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 3
dl 0
loc 22
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\common\html;
4
5
6
use Ajax\JsUtils;
7
use Ajax\semantic\html\collections\form\HtmlFormField;
8
use Ajax\semantic\html\collections\form\HtmlForm;
9
class HtmlDoubleElement extends HtmlSingleElement {
10
	/**
11
	 *
12
	 * @var mixed
13
	 */
14
	protected $content;
15
	protected $wrapContentBefore="";
16
	protected $wrapContentAfter="";
17
	protected $_editableContent;
18
19
	public function __construct($identifier, $tagName="p") {
20
		parent::__construct($identifier, $tagName);
21
		$this->_template='<%tagName% id="%identifier%" %properties%>%wrapContentBefore%%content%%wrapContentAfter%</%tagName%>';
22
	}
23
24
	public function setContent($content) {
25
		$this->content=$content;
26
		return $this;
27
	}
28
29
	public function getContent() {
30
		return $this->content;
31
	}
32
33
	public function addContent($content,$before=false) {
34
		if (!\is_array($this->content)) {
35
			if(isset($this->content))
36
				$this->content=array ($this->content);
37
			else
38
				$this->content=array();
39
		}
40
		if($before)
41
			array_unshift($this->content,$content);
42
		else
43
			$this->content []=$content;
44
		return $this;
45
	}
46
47
	/*
48
	 * (non-PHPdoc)
49
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::run()
50
	 */
51
	public function run(JsUtils $js) {
52
		parent::run($js);
53
		if ($this->content instanceof HtmlDoubleElement) {
54
			$this->content->run($js);
55
		} else if (\is_array($this->content)) {
56
			foreach ( $this->content as $itemContent ) {
57
				if ($itemContent instanceof HtmlDoubleElement) {
58
					$itemContent->run($js);
59
				}
60
			}
61
		}
62
	}
63
64
	public function setValue($value) {
65
	}
66
67
	public function wrapContent($before, $after="") {
68
		$this->wrapContentBefore.=$before;
69
		$this->wrapContentAfter=$after.$this->wrapContentAfter;
70
		return $this;
71
	}
72
73
	public function getContentInstances($class){
74
		return $this->_getContentInstances($class,$this->content);
75
	}
76
77
	protected function _getContentInstances($class,$content){
78
		$instances=[];
79
		if($content instanceof $class){
80
			$instances[]=$content;
81
		}elseif($content instanceof HtmlDoubleElement){
82
			$instances=\array_merge($instances,$content->getContentInstances($class));
83
		}elseif (\is_array($content)){
84
			foreach ($content as $element){
85
				$instances=\array_merge($instances,$this->_getContentInstances($class, $element));
86
			}
87
		}
88
		return $instances;
89
	}
90
91
	/**
92
	 * Transforms the element into a link
93
	 * @return HtmlDoubleElement
94
	 */
95
	public function asLink($href=NULL,$target=NULL) {
96
		if (isset($href))
97
			$this->setProperty("href", $href);
98
		if(isset($target))
99
			$this->setProperty("target", $target);
100
		return $this->setTagName("a");
101
	}
102
	
103
	public function getTextContent(){
104
		if(is_array($this->content)){
105
			return strip_tags(implode("", $this->content));
106
		}
107
		return strip_tags($this->content);
108
	}
109
	
110
	public function asEditable(HtmlFormField $field,$asForm=false,$setValueProperty="val()"){
111
		$idF=$field->getIdentifier();
112
		$idE=$idF;
113
		if($asForm){
114
			$frm=new HtmlForm("frm-".$field->getIdentifier());
115
			$frm->setProperty("onsubmit", "return false;");
116
			$fields=$frm->addFields();
117
			$idE=$frm->getIdentifier();
118
			$fields->addItem($field);
119
			$fields->addButtonIcon("bt-okay", "check","green mini","\$('#".$idE."').trigger('validate',{value: $('#'+idF+' input').val()});");
120
			$fields->addButtonIcon("bt-cancel", "close","mini","\$('#".$idE."').trigger('endEdit');");
121
			$this->_editableContent=$frm;
122
			$keypress="";
123
			$focusOut="";
124
		}else{
125
			$focusOut="if(e.relatedTarget==null)elm.trigger('endEdit');";
126
			$this->_editableContent=$field;
127
			$keypress="$('#".$idF."').keyup(function(e){if(e.which == 13) {\$('#".$idE."').trigger('validate',{value: $('#'+idF+' input').val()});}if(e.keyCode===27) {\$('#".$idE."').trigger('endEdit');}});";
128
		}
129
		$this->_editableContent->setProperty("style", "display:none;");
130
		$this->onCreate("let idF='".$idF."';let idE='".$idE."';let elm=$('#'+idE);let self=$('#".$this->getIdentifier()."');".$keypress."elm.on('validate',function(){self.html($('#'+idE+' input').".$setValueProperty.");elm.trigger('endEdit');});elm.on('endEdit',function(){self.show();$(this).hide();});elm.focusout(function(e){".$focusOut."});");
131
		$this->onClick("let self=$(this);self.hide();".$field->setJsContent("self.html()").";$('#".$idF." input').trigger('change');elm.show();$('#'+idE+' input').focus();");
132
	}
133
	/**
134
	 * {@inheritDoc}
135
	 * @see \Ajax\common\html\BaseHtml::compile_once()
136
	 */
137
	protected function compile_once(\Ajax\JsUtils $js = NULL, &$view = NULL) {
138
		if(!$this->_compiled && isset($this->_editableContent)){
139
			$this->wrap("",$this->_editableContent);
140
		}
141
		parent::compile_once($js,$view);
142
		
143
	}
144
145
}
146