Completed
Push — master ( da7bd7...a4feba )
by Jean-Christophe
03:22
created

SimpleComponent   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 96
rs 10
c 0
b 0
f 0

12 Methods

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