44 private function _callSetter($setter, $key, $value, &$array) {
46 if (method_exists($this, $setter) && substr($setter, 0, 1) !==
"_") {
48 $this->$setter($value);
51 }
catch (\Exception $e) {
62 protected function ctrl($name, $value, $typeCtrl) {
63 if (\is_array($typeCtrl)) {
64 if (array_search($value, $typeCtrl) ===
false) {
65 throw new \Exception(
"La valeur passée `" . $value .
"` à la propriété `" . $name .
"` ne fait pas partie des valeurs possibles : {" . implode(
",", $typeCtrl) .
"}");
68 if (! $typeCtrl($value)) {
69 throw new \Exception(
"La fonction " . $typeCtrl .
" a retourné faux pour l'affectation de la propriété " . $name);
76 $this->
ctrl($name, $value, $typeCtrl);
82 if (\is_array($typeCtrl)) {
84 $name .= $separator . $value;
90 $this->
ctrl($name, $value, $typeCtrl);
91 if (\is_array($typeCtrl)) {
94 $name .= $separator . $value;
98 protected function addToMember(&$name, $value, $separator =
' ') {
99 $name = \str_ireplace($value,
'', $name??
'') . $separator . $value;
104 $oldValue = \str_ireplace($allValues,
'', $oldValue??
'');
105 $oldValue = \trim($oldValue);
109 if (\is_array($elements)) {
110 $elements = \array_values($elements);
113 while (! $flag && $index <
sizeof($elements)) {
114 if ($elements[$index] instanceof
BaseHtml)
115 $flag = ($callback($elements[$index]));
119 return $elements[$index - 1];
120 } elseif ($elements instanceof
BaseHtml) {
121 if ($callback($elements))
128 $this->_wrapBefore = $wrapBefore;
133 $this->_wrapAfter = $wrapAfter;
147 foreach ($this as $key => $value) {
148 if (array_key_exists($key, $array) ===
true)
149 $this->
_callSetter(
"set" . ucfirst($key), $key, $array[$key], $array);
151 foreach ($array as $key => $value) {
152 if ($this->
_callSetter($key, $key, $value, $array) ===
false) {
153 $this->
_callSetter(
"set" . ucfirst($key), $key, $value, $array);
160 if (isset($objects)) {
161 foreach ($objects as $object) {
170 public function wrap($before, $after =
"") {
171 if (isset($before)) {
172 array_unshift($this->_wrapBefore, $before);
174 $this->_wrapAfter[] = $after;
189 $this->_bsComponent = $bsComponent;
194 if (! $this->_compiled) {
196 $beforeCompile = $js->getParam(
"beforeCompileHtml");
197 if (\is_callable($beforeCompile)) {
198 $beforeCompile($this, $js, $view);
202 unset($this->properties[
"jsCallback"]);
203 $this->_compiled =
true;
210 foreach ($this as $key => $value) {
211 if (\strstr($result,
"%{$key}%") !==
false) {
212 if (\is_array($value)) {
214 } elseif ($value instanceof \stdClass) {
215 $v = \print_r($value,
true);
216 } elseif ($value instanceof
BaseHtml) {
217 $v = $value->compile($js, $view);
221 $result = \str_replace(
"%{$key}%", $v ??
'', $result);
224 if (isset($js) ===
true) {
226 if (isset($view) ===
true) {
227 $js->addViewElement($this->
getLibraryId(), $result, $view);
231 if (\is_callable($this->_postCompile)) {
249 public function setDraggable($attr =
"id", $dropZone =
null, $parameters = []) {
252 if (isset($dropZone) && $dropZone instanceof
BaseHtml) {
253 $jqueryDone =
"append";
255 extract($parameters);
256 $dropZone->asDropZone($jsCallback, $jqueryDone, $parameters);
269 public function asDropZone($jsCallback =
"", $jqueryDone =
"append", $parameters = []) {
270 $stopPropagation =
false;
271 $this->
addEvent(
"dragover",
'', $stopPropagation,
true);
272 extract($parameters);
287 public function asFileDropZone($responseElement =
null, $url =
null, $progress =
null, $jsCallback =
"", $parameters = []) {
288 $stopPropagation =
false;
289 $defaultAjaxAttributes = [
290 'contentType' =>
'false',
291 'processData' =>
'false'
293 $this->
addEvent(
"dragover",
'', $stopPropagation,
true);
294 extract($parameters);
296 if (isset($ajaxAttributes)) {
297 $ajaxAttributes += $defaultAjaxAttributes;
299 $ajaxAttributes = $defaultAjaxAttributes;
301 if (isset($progress)) {
302 $progress =
new HtmlProgress($this->_identifier .
'-pg', 0, $progress);
303 $progress->setTotal(100);
304 $this->
wrap(
'', $progress);
305 $ajaxAttributes[
'upload'] =
"$('#" . $this->_identifier .
"-pg').progress('set percent', Math.ceil(event.loaded/event.total)*100);";
308 $this->
postOn(
'upload', $url,
'event.target.upload', $responseElement, $ajaxAttributes);
310 return $progress ?? $this;
318 $this->_postCompile = $callback;
322 $this->_preCompile = $this->
addCallback($this->_preCompile, $callback);
326 if (isset($originalValue)) {
327 if (! is_array($originalValue)) {
332 $result[] = $callback;
339 if (\is_callable($callable)) {
340 return $callable($this);
342 if (is_array($callable)) {
343 foreach ($callable as $call) {
BaseHtml for HTML components.
removeOldValues(&$oldValue, $allValues)
fromDatabaseObjects($objects, $function)
_getElementBy($callback, $elements)
asFileDropZone($responseElement=null, $url=null, $progress=null, $jsCallback="", $parameters=[])
Declares the element as a drop zone for file uploading (HTML5 drag and drop)
_callSetter($setter, $key, $value, &$array)
addToMember(&$name, $value, $separator=' ')
setMemberCtrl(&$name, $value, $typeCtrl)
setBsComponent($bsComponent)
setDraggable($attr="id", $dropZone=null, $parameters=[])
Sets the element draggable, and eventualy defines the dropzone (HTML5 drag and drop)
setWrapBefore($wrapBefore)
addCallback($originalValue, $callback)
getElementById($identifier, $elements)
asDropZone($jsCallback="", $jqueryDone="append", $parameters=[])
Declares the element as a drop zone (HTML5 drag and drop)
compile(JsUtils $js=NULL, &$view=NULL)
getTemplate(JsUtils $js=NULL, $view=null)
addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ")
compile_once(JsUtils $js=NULL, &$view=NULL)
ctrl($name, $value, $typeCtrl)
addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ")
fromDatabaseObject($object, $function)
static wrap($input, $js=NULL, $view=null, $separator=' ', $valueQuote='"')
postOn($event, $url, $params="{}", $responseElement="", $parameters=array())
Performs a post to $url on the event $event on $element and display it in $responseElement.
addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false)
setProperty($name, $value)
static draggable($attr="id")
static dropZone($jqueryDone, $jsCallback="")
static fileDropZone($jsCallback="")