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