phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
JsUtils.php
Go to the documentation of this file.
1<?php
2namespace Ajax;
3
12
24
25 protected $params;
26
27 protected $injected;
28
33 protected $_ui;
34
39 protected $_bootstrap;
40
45 protected $_semantic;
46
51 protected $config;
52
60 abstract public function getUrl($url);
61
70 abstract public function addViewElement($identifier, $content, &$view);
71
81 abstract public function createScriptVariable(&$view, $view_var, $output);
82
94 abstract public function forward($initialController, $controller, $action, $params);
95
105 abstract public function renderContent($initialControllerInstance, $viewName, $params = NULL);
106
114 abstract public function fromDispatcher($dispatcher);
115
122 public function ui(JqueryUI $ui = NULL) {
123 if ($ui !== NULL) {
124 $this->_ui = $ui;
125 $ui->setJs($this);
126 $bs = $this->bootstrap();
127 if (isset($bs)) {
128 $this->conflict();
129 }
130 }
131 return $this->_ui;
132 }
133
140 public function bootstrap(Bootstrap $bootstrap = NULL) {
141 if ($bootstrap !== NULL) {
142 $this->_bootstrap = $bootstrap;
143 $bootstrap->setJs($this);
144 $ui = $this->ui();
145 if (isset($ui)) {
146 $this->conflict();
147 }
148 }
149 return $this->_bootstrap;
150 }
151
158 public function semantic(Semantic $semantic = NULL) {
159 if ($semantic !== NULL) {
160 $this->_semantic = $semantic;
161 $semantic->setJs($this);
162 $ui = $this->ui();
163 if (isset($ui)) {
164 $this->conflict();
165 }
166 }
167 return $this->_semantic;
168 }
169
175 public function config($config = NULL) {
176 if ($config === NULL) {
177 if ($this->config === NULL) {
178 $this->config = new DefaultConfig();
179 }
180 } elseif (\is_array($config)) {
181 $this->config = new Config($config);
182 } elseif ($config instanceof Config) {
183 $this->config = $config;
184 }
185 return $this->config;
186 }
187
195 public function __construct($params = array(), $injected = NULL) {
196 $ajaxDefault = [
197 'ajaxTransition' => null,
198 'attr' => '',
199 'historize' => false,
200 'jsCallback' => null,
201 'hasLoader' => true,
202 'jqueryDone' => 'html',
203 'async' => true,
204 'params' => null,
205 'headers' => null,
206 'jsCondition' => null,
207 'ajaxLoader' => null,
208 'csrf' => false
209 ];
210 $defaults = [
211 'debug' => true,
212 'defer' => false,
213 'ajax' => $ajaxDefault,
214 'historize' => true,
215 'autoActiveLinks' => true
216 ];
217 foreach ($defaults as $key => $val) {
218 if (isset($params[$key]) === false || $params[$key] === "") {
219 $params[$key] = $defaults[$key];
220 }
221 }
222 foreach ($ajaxDefault as $key => $val) {
223 if (isset($params["ajax"][$key]) === false || $params["ajax"][$key] === "") {
224 $params["ajax"][$key] = $ajaxDefault[$key];
225 }
226 }
227
228 if (\array_key_exists("semantic", $params)) {
229 $this->semantic(new Semantic());
230 }
231 if (\array_key_exists("bootstrap", $params)) {
232 $this->bootstrap(new Bootstrap());
233 }
234 if (isset($params["ajax"])) {
235 if (isset($params["ajax"]["ajaxTransition"])) {
236 $this->ajaxTransition = $this->setAjaxDataCall($params["ajax"]["ajaxTransition"]);
237 }
238 if ($params["ajax"]["historize"]) {
239 $params["historize"] = true;
240 }
241 if ($params["ajax"]["csrf"]) {
242 $this->exec($this->addCsrf(), true);
243 }
244 }
245 if ($params["historize"]) {
246 $this->execAtLast($this->onPopstate());
247 }
248 if ($params["autoActiveLinks"]) {
249 $this->exec($this->autoActiveLinks(), true);
250 }
251
252 $this->params = $params;
253 $this->injected = $injected;
254 if (isset($params['gc'])) {
255 \gc_disable();
256 }
257 $this->initialize();
258 }
259
263 protected function initialize() {}
264
265 public function __set($property, $value) {
266 switch ($property) {
267 case "bootstrap":
268 $this->bootstrap($value);
269 break;
270 case "semantic":
271 $this->semantic(value);
272 break;
273 case "ui":
274 $this->ui($value);
275 break;
276 default:
277 throw new \Exception('Unknown property !');
278 }
279 }
280
285 public function getParam($key) {
286 if (isset($this->params[$key]))
287 return $this->params[$key];
288 }
289
297 public function output($array_js) {
298 if (! is_array($array_js)) {
299 $array_js = array(
300 $array_js
301 );
302 }
303
304 foreach ($array_js as $js) {
305 $this->jquery_code_for_compile[] = "\t$js\n";
306 }
307 }
308
318 public function compile(&$view = NULL, $view_var = 'script_foot', $script_tags = TRUE) {
319 if (isset($this->_ui)) {
320 $this->_compileLibrary($this->_ui, $view);
321 }
322 if (isset($this->_bootstrap)) {
323 $this->_compileLibrary($this->_bootstrap, $view);
324 }
325 if (isset($this->_semantic)) {
326 $this->_compileLibrary($this->_semantic, $view);
327 }
328
329 $this->jquery_code_for_compile = \array_merge($this->jquery_code_for_compile, $this->jquery_code_for_compile_at_last);
330
331 if (\count($this->jquery_code_for_compile) == 0) {
332 return;
333 }
334
335 // Inline references
336 $script = $this->ready(implode('', $this->jquery_code_for_compile));
337 if ($this->params["defer"]) {
338 $script = $this->defer($script);
339 }
340 $script .= ";";
341
342 $this->jquery_code_for_compile = $this->jquery_code_for_compile_at_last = [];
343 if ($this->params["debug"] === false) {
344 $script = $this->minify($script);
345 }
346 $output = ($script_tags === FALSE) ? $script : $this->inline($script);
347
348 if ($view !== NULL) {
349 $this->createScriptVariable($view, $view_var, $output);
350 }
351
352 return $output;
353 }
354
360 public function clear_compile() {
361 $this->jquery_code_for_compile = $this->jquery_code_for_compile_at_last = [];
362 }
363
364 public function getScript($offset = 0) {
365 $code = \array_merge($this->jquery_code_for_compile, $this->jquery_code_for_compile_at_last);
366 if ($offset > 0) {
367 $code = \array_slice($code, $offset);
368 }
369 return \implode('', $code);
370 }
371
372 public function scriptCount() {
373 return \count($this->jquery_code_for_compile);
374 }
375
384 public function inline($script, $cdata = TRUE) {
385 $str = $this->_open_script();
386 $str .= ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
387 $str .= $this->_close_script();
388 return $str;
389 }
390
400 public function generate_json($result = NULL, $match_array_type = FALSE) {
401 // JSON data can optionally be passed to this function
402 // either as a database result object or an array, or a user supplied array
403 if (! is_null($result)) {
404 if (is_object($result)) {
405 $json_result = $result->result_array();
406 } elseif (\is_array($result)) {
407 $json_result = $result;
408 } else {
409 return $this->_prep_args($result);
410 }
411 } else {
412 return 'null';
413 }
414 return $this->_create_json($json_result, $match_array_type);
415 }
416
417 private function _create_json($json_result, $match_array_type) {
418 $json = array();
419 $_is_assoc = TRUE;
420 if (! is_array($json_result) && empty($json_result)) {
421 show_error("Generate JSON Failed - Illegal key, value pair.");
422 } elseif ($match_array_type) {
423 $_is_assoc = JArray::isAssociative($json_result);
424 }
425 foreach ($json_result as $k => $v) {
426 if ($_is_assoc) {
427 $json[] = $this->_prep_args($k, TRUE) . ':' . $this->generate_json($v, $match_array_type);
428 } else {
429 $json[] = $this->generate_json($v, $match_array_type);
430 }
431 }
432 $json = implode(',', $json);
433 return $_is_assoc ? "{" . $json . "}" : "[" . $json . "]";
434 }
435
443 public function _prep_args($result, $is_key = FALSE) {
444 if (is_null($result)) {
445 return 'null';
446 } elseif (is_bool($result)) {
447 return ($result === TRUE) ? 'true' : 'false';
448 } elseif (is_string($result) || $is_key) {
449 return '"' . str_replace(array(
450 '\\',
451 "\t",
452 "\n",
453 "\r",
454 '"',
455 '/'
456 ), array(
457 '\\\\',
458 '\\t',
459 '\\n',
460 "\\r",
461 '\"',
462 '\/'
463 ), $result) . '"';
464 } elseif (is_scalar($result)) {
465 return $result;
466 }
467 }
468
488 public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn = false) {
489 if (\is_array($js)) {
490 $js = implode("\n\t\t", $js);
491 }
492 if ($preventDefault === true) {
493 $js = Javascript::$preventDefault . $js;
494 }
495 if ($stopPropagation === true) {
496 $js = Javascript::$stopPropagation . $js;
497 }
498 if (\is_string($listenerOn)) {
499 $event = "\n\t$(" . Javascript::prep_element($listenerOn) . ").on('{$event}'," . Javascript::prep_element($element) . ",function(event){\n\t\t{$js}\n\t});\n";
500 } else {
501 if (\array_search($event, $this->jquery_events) === false) {
502 $event = "\n\t$(" . Javascript::prep_element($element) . ").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
503 } else {
504 $event = "\n\t$(" . Javascript::prep_element($element) . ").{$event}(function(event){\n\t\t{$js}\n\t});\n";
505 }
506 }
507 if ($immediatly)
508 $this->jquery_code_for_compile[] = $event;
509 return $event;
510 }
511
512 public function getInjected() {
513 return $this->injected;
514 }
515
516 public function setParam(string $param,$value){
517 $this->params[$param]=$value;
518 }
519}
JQuery UI Phalcon library.
Definition JqueryUI.php:29
JQuery PHP library.
Definition JsUtils.php:23
forward($initialController, $controller, $action, $params)
Forwards to.
config($config=NULL)
Definition JsUtils.php:175
_prep_args($result, $is_key=FALSE)
Ensures a standard json value and escapes values.
Definition JsUtils.php:443
setParam(string $param, $value)
Definition JsUtils.php:516
output($array_js)
Outputs the called javascript to the screen.
Definition JsUtils.php:297
fromDispatcher($dispatcher)
Collects url parts from the request dispatcher : controllerName, actionName, parameters Used internal...
getUrl($url)
Generates an URL Used internally by phpMv.
_create_json($json_result, $match_array_type)
Definition JsUtils.php:417
clear_compile()
Clears the array of script events collected for output.
Definition JsUtils.php:360
renderContent($initialControllerInstance, $viewName, $params=NULL)
render the content of an existing view : $viewName and set the response to the modal content Used int...
compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE)
gather together all script needing to be output
Definition JsUtils.php:318
getScript($offset=0)
Definition JsUtils.php:364
ui(JqueryUI $ui=NULL)
getter or setter of the jQuery-UI variable
Definition JsUtils.php:122
_add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false, $immediatly=true, $listenerOn=false)
Constructs the syntax for an event, and adds to into the array for compilation.
Definition JsUtils.php:488
initialize()
To override for special initialization of the component.
Definition JsUtils.php:263
bootstrap(Bootstrap $bootstrap=NULL)
getter or setter of the Twitter Bootstrap variable
Definition JsUtils.php:140
__set($property, $value)
Definition JsUtils.php:265
createScriptVariable(&$view, $view_var, $output)
Creates the script variable script_foot Used internally by phpMv.
getParam($key)
Definition JsUtils.php:285
generate_json($result=NULL, $match_array_type=FALSE)
Can be passed a database result or associative array and returns a JSON formatted string.
Definition JsUtils.php:400
__construct($params=array(), $injected=NULL)
Definition JsUtils.php:195
addViewElement($identifier, $content, &$view)
Adds the array of controls q in the $view element Used internally by phpMv.
semantic(Semantic $semantic=NULL)
getter or setter of the Semantic-UI variable
Definition JsUtils.php:158