phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
JsUtils.php
Go to the documentation of this file.
1 <?php
2 namespace Ajax;
3 
12 
23 abstract class JsUtils {
25 
26  protected $params;
27 
28  protected $injected;
29 
34  protected $_ui;
35 
40  protected $_bootstrap;
41 
46  protected $_semantic;
47 
52  protected $config;
53 
61  abstract public function getUrl($url);
62 
71  abstract public function addViewElement($identifier, $content, &$view);
72 
82  abstract public function createScriptVariable(&$view, $view_var, $output);
83 
95  abstract public function forward($initialController, $controller, $action, $params);
96 
106  abstract public function renderContent($initialControllerInstance, $viewName, $params = NULL);
107 
115  abstract public function fromDispatcher($dispatcher);
116 
123  public function ui(JqueryUI $ui = NULL) {
124  if ($ui !== NULL) {
125  $this->_ui = $ui;
126  $ui->setJs($this);
127  $bs = $this->bootstrap();
128  if (isset($bs)) {
129  $this->conflict();
130  }
131  }
132  return $this->_ui;
133  }
134 
141  public function bootstrap(Bootstrap $bootstrap = NULL) {
142  if ($bootstrap !== NULL) {
143  $this->_bootstrap = $bootstrap;
144  $bootstrap->setJs($this);
145  $ui = $this->ui();
146  if (isset($ui)) {
147  $this->conflict();
148  }
149  }
150  return $this->_bootstrap;
151  }
152 
159  public function semantic(Semantic $semantic = NULL) {
160  if ($semantic !== NULL) {
161  $this->_semantic = $semantic;
162  $semantic->setJs($this);
163  $ui = $this->ui();
164  if (isset($ui)) {
165  $this->conflict();
166  }
167  }
168  return $this->_semantic;
169  }
170 
176  public function config($config = NULL) {
177  if ($config === NULL) {
178  if ($this->config === NULL) {
179  $this->config = new DefaultConfig();
180  }
181  } elseif (\is_array($config)) {
182  $this->config = new Config($config);
183  } elseif ($config instanceof Config) {
184  $this->config = $config;
185  }
186  return $this->config;
187  }
188 
196  public function __construct($params = array(), $injected = NULL) {
197  $ajaxDefault = [
198  'ajaxTransition' => null,
199  'attr' => '',
200  'historize' => false,
201  'jsCallback' => null,
202  'hasLoader' => true,
203  'jqueryDone' => 'html',
204  'async' => true,
205  'params' => null,
206  'headers' => null,
207  'jsCondition' => null,
208  'ajaxLoader' => null,
209  'csrf' => false
210  ];
211  $defaults = [
212  'debug' => true,
213  'defer' => false,
214  'ajax' => $ajaxDefault,
215  'historize' => true,
216  'autoActiveLinks' => true
217  ];
218  foreach ($defaults as $key => $val) {
219  if (isset($params[$key]) === false || $params[$key] === "") {
220  $params[$key] = $defaults[$key];
221  }
222  }
223  foreach ($ajaxDefault as $key => $val) {
224  if (isset($params["ajax"][$key]) === false || $params["ajax"][$key] === "") {
225  $params["ajax"][$key] = $ajaxDefault[$key];
226  }
227  }
228 
229  if (\array_key_exists("semantic", $params)) {
230  $this->semantic(new Semantic());
231  }
232  if (\array_key_exists("bootstrap", $params)) {
233  $this->bootstrap(new Bootstrap());
234  }
235  if (isset($params["ajax"])) {
236  if (isset($params["ajax"]["ajaxTransition"])) {
237  $this->ajaxTransition = $this->setAjaxDataCall($params["ajax"]["ajaxTransition"]);
238  }
239  if ($params["ajax"]["historize"]) {
240  $params["historize"] = true;
241  }
242  if ($params["ajax"]["csrf"]) {
243  $this->exec($this->addCsrf(), true);
244  }
245  }
246  if ($params["historize"]) {
247  $this->execAtLast($this->onPopstate());
248  }
249  if ($params["autoActiveLinks"]) {
250  $this->exec($this->autoActiveLinks(), true);
251  }
252 
253  $this->params = $params;
254  $this->injected = $injected;
255  if (isset($params['gc'])) {
256  \gc_disable();
257  }
258  $this->initialize();
259  }
260 
264  protected function initialize() {}
265 
266  public function __set($property, $value) {
267  switch ($property) {
268  case "bootstrap":
269  $this->bootstrap($value);
270  break;
271  case "semantic":
272  $this->semantic(value);
273  break;
274  case "ui":
275  $this->ui($value);
276  break;
277  default:
278  throw new \Exception('Unknown property !');
279  }
280  }
281 
286  public function getParam($key) {
287  if (isset($this->params[$key]))
288  return $this->params[$key];
289  }
290 
298  public function output($array_js) {
299  if (! is_array($array_js)) {
300  $array_js = array(
301  $array_js
302  );
303  }
304 
305  foreach ($array_js as $js) {
306  $this->jquery_code_for_compile[] = "\t$js\n";
307  }
308  }
309 
319  public function compile(&$view = NULL, $view_var = 'script_foot', $script_tags = TRUE) {
320  if (isset($this->_ui)) {
321  $this->_compileLibrary($this->_ui, $view);
322  }
323  if (isset($this->_bootstrap)) {
324  $this->_compileLibrary($this->_bootstrap, $view);
325  }
326  if (isset($this->_semantic)) {
327  $this->_compileLibrary($this->_semantic, $view);
328  }
329 
330  $this->jquery_code_for_compile = \array_merge($this->jquery_code_for_compile, $this->jquery_code_for_compile_at_last);
331 
332  if (\count($this->jquery_code_for_compile) == 0) {
333  return;
334  }
335 
336  // Inline references
337  $script = $this->ready(implode('', $this->jquery_code_for_compile));
338  if ($this->params["defer"]) {
339  $script = $this->defer($script);
340  }
341  $script .= ";";
342 
343  $this->jquery_code_for_compile = $this->jquery_code_for_compile_at_last = [];
344  if ($this->params["debug"] === false) {
345  $script = $this->minify($script);
346  }
347  $output = ($script_tags === FALSE) ? $script : $this->inline($script);
348 
349  if ($view !== NULL) {
350  $this->createScriptVariable($view, $view_var, $output);
351  }
352 
353  return $output;
354  }
355 
361  public function clear_compile() {
362  $this->jquery_code_for_compile = $this->jquery_code_for_compile_at_last = [];
363  if($this->_bootstrap!==null){
364  $this->_bootstrap->clearComponents();
365  }
366  if($this->_semantic!==null){
367  $this->_semantic->clearComponents();
368  }
369  if($this->_ui!==null){
370  $this->_ui->clearComponents();
371  }
372  }
373 
374  public function getScript($offset = 0) {
375  $code = \array_merge($this->jquery_code_for_compile, $this->jquery_code_for_compile_at_last);
376  if ($offset > 0) {
377  $code = \array_slice($code, $offset);
378  }
379  return \implode('', $code);
380  }
381 
382  public function scriptCount() {
383  return \count($this->jquery_code_for_compile);
384  }
385 
394  public function inline($script, $cdata = TRUE) {
395  $str = $this->_open_script();
396  $str .= ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
397  $str .= $this->_close_script();
398  return $str;
399  }
400 
410  public function generate_json($result = NULL, $match_array_type = FALSE) {
411  // JSON data can optionally be passed to this function
412  // either as a database result object or an array, or a user supplied array
413  if (! is_null($result)) {
414  if (is_object($result)) {
415  $json_result = $result->result_array();
416  } elseif (\is_array($result)) {
417  $json_result = $result;
418  } else {
419  return $this->_prep_args($result);
420  }
421  } else {
422  return 'null';
423  }
424  return $this->_create_json($json_result, $match_array_type);
425  }
426 
427  private function _create_json($json_result, $match_array_type) {
428  $json = array();
429  $_is_assoc = TRUE;
430  if (! is_array($json_result) && empty($json_result)) {
431  show_error("Generate JSON Failed - Illegal key, value pair.");
432  } elseif ($match_array_type) {
433  $_is_assoc = JArray::isAssociative($json_result);
434  }
435  foreach ($json_result as $k => $v) {
436  if ($_is_assoc) {
437  $json[] = $this->_prep_args($k, TRUE) . ':' . $this->generate_json($v, $match_array_type);
438  } else {
439  $json[] = $this->generate_json($v, $match_array_type);
440  }
441  }
442  $json = implode(',', $json);
443  return $_is_assoc ? "{" . $json . "}" : "[" . $json . "]";
444  }
445 
453  public function _prep_args($result, $is_key = FALSE) {
454  if (is_null($result)) {
455  return 'null';
456  } elseif (is_bool($result)) {
457  return ($result === TRUE) ? 'true' : 'false';
458  } elseif (is_string($result) || $is_key) {
459  return '"' . str_replace(array(
460  '\\',
461  "\t",
462  "\n",
463  "\r",
464  '"',
465  '/'
466  ), array(
467  '\\\\',
468  '\\t',
469  '\\n',
470  "\\r",
471  '\"',
472  '\/'
473  ), $result) . '"';
474  } elseif (is_scalar($result)) {
475  return $result;
476  }
477  }
478 
498  public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn = false) {
499  if (\is_array($js)) {
500  $js = implode("\n\t\t", $js);
501  }
502  if ($preventDefault === true) {
503  $js = Javascript::$preventDefault . $js;
504  }
505  if ($stopPropagation === true) {
506  $js = Javascript::$stopPropagation . $js;
507  }
508  if (\is_string($listenerOn)) {
509  $event = "\n\t$(" . Javascript::prep_element($listenerOn) . ").on('{$event}'," . Javascript::prep_element($element) . ",function(event){\n\t\t{$js}\n\t});\n";
510  } else {
511  if (\array_search($event, $this->jquery_events) === false) {
512  $event = "\n\t$(" . Javascript::prep_element($element) . ").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
513  } else {
514  $event = "\n\t$(" . Javascript::prep_element($element) . ").{$event}(function(event){\n\t\t{$js}\n\t});\n";
515  }
516  }
517  if ($immediatly)
518  $this->jquery_code_for_compile[] = $event;
519  return $event;
520  }
521 
522  public function getInjected() {
523  return $this->injected;
524  }
525 
526  public function setParam(string $param,$value){
527  $this->params[$param]=$value;
528  }
529 }
config($config=NULL)
Definition: JsUtils.php:176
fromDispatcher(JsUtils $js, $dispatcher, $startIndex=0)
getScript($offset=0)
Definition: JsUtils.php:374
_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:498
addCsrf($name='csrf-token')
autoActiveLinks($previousURL="window.location.href")
output($array_js)
Outputs the called javascript to the screen.
Definition: JsUtils.php:298
clear_compile()
Clears the array of script events collected for output.
Definition: JsUtils.php:361
setParam(string $param, $value)
Definition: JsUtils.php:526
__construct($params=array(), $injected=NULL)
Definition: JsUtils.php:196
_prep_args($result, $is_key=FALSE)
Ensures a standard json value and escapes values.
Definition: JsUtils.php:453
_close_script($extra="\)
Outputs an closing </script>
exec($js, $immediatly=false)
Executes the code $js.
getParam($key)
Definition: JsUtils.php:286
compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE)
gather together all script needing to be output
Definition: JsUtils.php:319
semantic(Semantic $semantic=NULL)
getter or setter of the Semantic-UI variable
Definition: JsUtils.php:159
bootstrap(Bootstrap $bootstrap=NULL)
getter or setter of the Twitter Bootstrap variable
Definition: JsUtils.php:141
_open_script($src='')
Outputs an opening <script>
ui(JqueryUI $ui=NULL)
getter or setter of the jQuery-UI variable
Definition: JsUtils.php:123
JQuery UI Phalcon library.
Definition: JqueryUI.php:29
JQuery PHP library.
Definition: JsUtils.php:23
initialize()
To override for special initialization of the component.
Definition: JsUtils.php:264
__set($property, $value)
Definition: JsUtils.php:266
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:410
_compileLibrary(BaseGui $library, &$view=NULL)
_create_json($json_result, $match_array_type)
Definition: JsUtils.php:427
execAtLast($js)
Executes the code $js.