1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\common\components; |
4
|
|
|
|
5
|
|
|
use Ajax\JsUtils; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base component for JQuery UI visuals components |
9
|
|
|
* @author jc |
10
|
|
|
* @version 1.001 |
11
|
|
|
*/ |
12
|
|
View Code Duplication |
abstract class SimpleComponent extends BaseComponent { |
|
|
|
|
13
|
|
|
protected $attachTo; |
14
|
|
|
protected $uiName; |
15
|
|
|
protected $events; |
16
|
|
|
|
17
|
|
|
public function __construct(JsUtils $js) { |
18
|
|
|
parent::__construct($js); |
19
|
|
|
$this->events=array (); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function compileEvents() { |
23
|
|
|
foreach ( $this->events as $event => $jsCode ) { |
24
|
|
|
$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).on(\"".$event."\" , function( event, data ) {".$jsCode."});"; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function compileJQueryCode() { |
29
|
|
|
$result=implode("\n", $this->jquery_code_for_compile); |
30
|
|
|
$result=str_ireplace("\"%", "", $result); |
31
|
|
|
$result=str_ireplace("%\"", "", $result); |
32
|
|
|
$result=str_replace(array ( |
33
|
|
|
"\\n", |
34
|
|
|
"\\r", |
35
|
|
|
"\\t" |
36
|
|
|
), '', $result); |
37
|
|
|
return $result; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getScript() { |
41
|
|
|
$allParams=$this->params; |
42
|
|
|
$this->jquery_code_for_compile=array (); |
43
|
|
|
$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).{$this->uiName}(".$this->getParamsAsJSON($allParams).");"; |
44
|
|
|
$this->compileEvents(); |
45
|
|
|
return $this->compileJQueryCode(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @param String $identifier identifiant CSS |
51
|
|
|
*/ |
52
|
|
|
public function attach($identifier) { |
53
|
|
|
$this->attachTo=$identifier; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function addEvent($event, $jsCode) { |
57
|
|
|
return $this->setParam($event, "%function( event, ui ) {".$jsCode."}%"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function on($event, $jsCode) { |
61
|
|
|
$this->events [$event]=$jsCode; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function setParamCtrl($key, $value, $typeCtrl) { |
66
|
|
|
if (is_array($typeCtrl)) { |
67
|
|
|
if (array_search($value, $typeCtrl)===false) |
68
|
|
|
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)."}"); |
69
|
|
|
} else { |
70
|
|
|
if (!$typeCtrl($value)) { |
71
|
|
|
throw new \Exception("La fonction ".$typeCtrl." a retourné faux pour l'affectation de la propriété ".$key." au composant ".$this->uiName); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return $this->setParam($key, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getAttachTo() { |
78
|
|
|
return $this->attachTo; |
79
|
|
|
} |
80
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.