45 private function _callSetter($setter, $key, $value, &$array) {
47 if (method_exists($this, $setter) && substr($setter, 0, 1) !==
"_") {
49 $this->$setter($value);
52 }
catch (\Exception $e) {
63 protected function ctrl($name, $value, $typeCtrl) {
64 if (\is_array($typeCtrl)) {
65 if (array_search($value, $typeCtrl) ===
false) {
66 throw new \Exception(
"La valeur passée `" . $value .
"` à la propriété `" . $name .
"` ne fait pas partie des valeurs possibles : {" . implode(
",", $typeCtrl) .
"}");
69 if (! $typeCtrl($value)) {
70 throw new \Exception(
"La fonction " . $typeCtrl .
" a retourné faux pour l'affectation de la propriété " . $name);
77 $this->
ctrl($name, $value, $typeCtrl);
83 if (\is_array($typeCtrl)) {
85 $name .= $separator . $value;
91 $this->
ctrl($name, $value, $typeCtrl);
92 if (\is_array($typeCtrl)) {
95 $name .= $separator . $value;
99 protected function addToMember(&$name, $value, $separator =
' ') {
100 $name = \str_ireplace($value,
'', $name??
'') . $separator . $value;
105 $oldValue = \str_ireplace($allValues,
'', $oldValue??
'');
106 $oldValue = \trim($oldValue);
110 if (\is_array($elements)) {
111 $elements = \array_values($elements);
114 while (! $flag && $index <
sizeof($elements)) {
115 if ($elements[$index] instanceof
BaseHtml)
116 $flag = ($callback($elements[$index]));
120 return $elements[$index - 1];
121 } elseif ($elements instanceof
BaseHtml) {
122 if ($callback($elements))
129 $this->_wrapBefore = $wrapBefore;
134 $this->_wrapAfter = $wrapAfter;
148 foreach ($this as $key => $value) {
149 if (array_key_exists($key, $array) ===
true)
150 $this->
_callSetter(
"set" . ucfirst($key), $key, $array[$key], $array);
152 foreach ($array as $key => $value) {
153 if ($this->
_callSetter($key, $key, $value, $array) ===
false) {
154 $this->
_callSetter(
"set" . ucfirst($key), $key, $value, $array);
161 if (isset($objects)) {
162 foreach ($objects as $object) {
171 public function wrap($before, $after =
"") {
172 if (isset($before)) {
173 array_unshift($this->_wrapBefore, $before);
175 $this->_wrapAfter[] = $after;
190 $this->_bsComponent = $bsComponent;
195 if (! $this->_compiled) {
197 $beforeCompile = $js->getParam(
"beforeCompileHtml");
198 if (\is_callable($beforeCompile)) {
199 $beforeCompile($this, $js, $view);
203 unset($this->properties[
"jsCallback"]);
204 $this->_compiled =
true;
211 foreach ($this as $key => $value) {
212 if (\strstr($result,
"%{$key}%") !==
false) {
213 if (\is_array($value)) {
215 } elseif ($value instanceof \stdClass) {
216 $v = \print_r($value,
true);
217 } elseif ($value instanceof
BaseHtml) {
218 $v = $value->compile($js, $view);
222 $result = \str_replace(
"%{$key}%", $v ??
'', $result);
225 if (isset($js) ===
true) {
227 if (isset($view) ===
true) {
228 $js->addViewElement($this->
getLibraryId(), $result, $view);
232 if (\is_callable($this->_postCompile)) {
250 public function setDraggable($attr =
"id", $dropZone = null, $parameters = []) {
253 if (isset($dropZone) && $dropZone instanceof
BaseHtml) {
254 $jqueryDone =
"append";
256 extract($parameters);
257 $dropZone->asDropZone($jsCallback, $jqueryDone, $parameters);
270 public function asDropZone($jsCallback =
"", $jqueryDone =
"append", $parameters = []) {
271 $stopPropagation =
false;
272 $this->
addEvent(
"dragover",
'', $stopPropagation,
true);
273 extract($parameters);
288 public function asFileDropZone($responseElement = null, $url = null, $progress = null, $jsCallback =
"", $parameters = []) {
289 $stopPropagation =
false;
290 $defaultAjaxAttributes = [
291 'contentType' =>
'false',
292 'processData' =>
'false' 294 $this->
addEvent(
"dragover",
'', $stopPropagation,
true);
295 extract($parameters);
297 if (isset($ajaxAttributes)) {
298 $ajaxAttributes += $defaultAjaxAttributes;
300 $ajaxAttributes = $defaultAjaxAttributes;
302 if (isset($progress)) {
303 $progress =
new HtmlProgress($this->_identifier .
'-pg', 0, $progress);
304 $progress->setTotal(100);
305 $this->
wrap(
'', $progress);
306 $ajaxAttributes[
'upload'] =
"$('#" . $this->_identifier .
"-pg').progress('set percent', Math.ceil(event.loaded/event.total)*100);";
309 $this->
postOn(
'upload', $url,
'event.target.upload', $responseElement, $ajaxAttributes);
311 return $progress ?? $this;
319 $this->_postCompile = $callback;
323 $this->_preCompile = $this->
addCallback($this->_preCompile, $callback);
327 if (isset($originalValue)) {
328 if (! is_array($originalValue)) {
333 $result[] = $callback;
340 if (\is_callable($callable)) {
341 return $callable($this);
343 if (is_array($callable)) {
344 foreach ($callable as $call) {
addCallback($originalValue, $callback)
BaseHtml for HTML components.
addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ")
fromDatabaseObjects($objects, $function)
getTemplate(JsUtils $js=NULL, $view=null)
setProperty($name, $value)
setDraggable($attr="id", $dropZone=null, $parameters=[])
Sets the element draggable, and eventualy defines the dropzone (HTML5 drag and drop) ...
postOn($event, $url, $params="{}", $responseElement="", $parameters=array())
Performs a post to $url on the event $event on $element and display it in $responseElement.
compile(JsUtils $js=NULL, &$view=NULL)
static dropZone($jqueryDone, $jsCallback="")
static draggable($attr="id")
trait BaseHtmlEventsTrait
_callSetter($setter, $key, $value, &$array)
static wrap($input, $js=NULL, $view=null, $separator=' ', $valueQuote='"')
asFileDropZone($responseElement=null, $url=null, $progress=null, $jsCallback="", $parameters=[])
Declares the element as a drop zone for file uploading (HTML5 drag and drop)
asDropZone($jsCallback="", $jqueryDone="append", $parameters=[])
Declares the element as a drop zone (HTML5 drag and drop)
addToMember(&$name, $value, $separator=' ')
setMemberCtrl(&$name, $value, $typeCtrl)
addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false)
trait BaseHtmlPropertiesTrait
compile_once(JsUtils $js=NULL, &$view=NULL)
removeOldValues(&$oldValue, $allValues)
setWrapBefore($wrapBefore)
_getElementBy($callback, $elements)
ctrl($name, $value, $typeCtrl)
fromDatabaseObject($object, $function)
addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ")
getElementById($identifier, $elements)
static fileDropZone($jsCallback="")
setBsComponent($bsComponent)