Completed
Push — master ( 3555a5...e61355 )
by Jean-Christophe
03:33
created

SimpleComponent::setParamCtrl()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 3
1
<?php
2
3
namespace Ajax\common\components;
4
5
6
use Ajax\JsUtils;
7
/**
8
 * Base component for JQuery UI visuals components
9
 * @author jc
10
 * @version 1.001
11
 */
12
abstract class SimpleComponent extends BaseComponent {
13
	protected $attachTo;
14
	protected $itemSelector;
15
	protected $uiName;
16
	protected $events;
17
18
	public function __construct(JsUtils $js) {
19
		parent::__construct($js);
20
		$this->events=array ();
21
	}
22
23
	protected function compileEvents() {
24
		foreach ( $this->events as $event => $jsCode ) {
25
			if($event=="execute"){
26
				$this->jquery_code_for_compile []=$jsCode;
27
			}else if($event=="beforeExecute"){
28
				\array_unshift($this->jquery_code_for_compile, $jsCode);
29
			}else{
30
				$selector=$this->attachTo;
31
				if(isset($this->itemSelector)){
32
					$selector.=" ".$this->itemSelector;
33
				}
34
				$this->jquery_code_for_compile []="$( \"".$selector."\" ).on(\"".$event."\" , function( event, data ) {".$jsCode."});";
35
			}
36
		}
37
	}
38
39
	protected function compileJQueryCode() {
40
		$result=implode("\n", $this->jquery_code_for_compile);
41
		$result=str_ireplace("\"%", "", $result);
42
		$result=str_ireplace("%\"", "", $result);
43
		$result=str_replace(array (
44
				"\\n",
45
				"\\r",
46
				"\\t"
47
		), '', $result);
48
		return $result;
49
	}
50
51
	public function getScript() {
52
		$allParams=$this->params;
53
		$this->jquery_code_for_compile=array ();
54
		$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).{$this->uiName}(".$this->getParamsAsJSON($allParams).");";
55
		$this->compileEvents();
56
		return $this->compileJQueryCode();
57
	}
58
59
	/**
60
	 *
61
	 * @param String $identifier identifiant CSS
62
	 */
63
	public function attach($identifier) {
64
		$this->attachTo=$identifier;
65
	}
66
67
	public function addEvent($event, $jsCode) {
68
		return $this->setParam($event, "%function( event, ui ) {".$jsCode."}%");
69
	}
70
71
	public function on($event, $jsCode) {
72
		$this->events [$event]=$jsCode;
73
		return $this;
74
	}
75
76
	protected function setParamCtrl($key, $value, $typeCtrl) {
77
		if (is_array($typeCtrl)) {
78
			if (array_search($value, $typeCtrl)===false)
79
				throw new \Exception("La valeur passée a propriété `".$key."` pour le composant `".$this->uiName."` ne fait pas partie des valeurs possibles : {".implode(",", $typeCtrl)."}");
80
		} else {
81
			if (!$typeCtrl($value)) {
82
				throw new \Exception("La fonction ".$typeCtrl." a retourné faux pour l'affectation de la propriété ".$key." au composant ".$this->uiName);
83
			}
84
		}
85
		return $this->setParam($key, $value);
86
	}
87
88
	public function getAttachTo() {
89
		return $this->attachTo;
90
	}
91
92
	public function getItemSelector() {
93
		return $this->itemSelector;
94
	}
95
96
	public function setItemSelector($itemSelector) {
97
		$this->itemSelector=$itemSelector;
98
		return $this;
99
	}
100
101
}