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\php\ubiquity;
3 
10 
11 class JsUtils extends \Ajax\JsUtils {
12 
19  protected function _open_script($src = '') {
20  $str = '<script ';
21  if (($this->params['csp'] ?? false) === 'nonce' && ContentSecurityManager::isStarted()) {
22  $nonce = ContentSecurityManager::getNonce('jsUtils');
23  $str .= ' nonce="' . $nonce . '" ';
24  }
25  $str .= ($src == '') ? '>' : ' src="' . $src . '">';
26  return $str;
27  }
28 
29  public function inline($script, $cdata = true) {
30  if (($this->params['csp'] ?? false) === 'hash' && ContentSecurityManager::isStarted()) {
31  $script = ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
32  ContentSecurityManager::getHash('jsUtils', $script);
33  }
34  return $this->_open_script() . $script . $this->_close_script();
35  }
36 
37  public function getUrl($url) {
38  return URequest::getUrl($url);
39  }
40 
41  public function addViewElement($identifier, $content, &$view) {
42  $controls = $view->getVar("q");
43  if (isset($controls) === false) {
44  $controls = array();
45  }
46  $controls[$identifier] = $content;
47  $view->setVar("q", $controls);
48  }
49 
50  public function createScriptVariable(&$view, $view_var, $output) {
51  $view->setVar($view_var, $output);
52  }
53 
54  public function forward($initialController, $controller, $action, $params = array()) {
55  return $initialController->forward($controller, $action, $params, true, true, true);
56  }
57 
58  public function renderContent($initialControllerInstance, $viewName, $params = NULL) {
59  return $initialControllerInstance->loadView($viewName, $params, true);
60  }
61 
62  public function fromDispatcher($dispatcher) {
63  return Startup::$urlParts;
64  }
65 
76  public function renderView($viewName, $parameters = [], $asString = false) {
77  if (isset($this->injected)) {
78  $view = $this->injected->getView();
79  $this->compile($view);
80  return $this->injected->loadView($viewName, $parameters, $asString);
81  }
82  throw new \Exception(get_class() . " instance is not properly instancied : you omitted the second parameter \$controller!");
83  }
84 
93  public function renderComponent(BaseHtml $component, bool $asString = false) {
94  $component->setLibraryId('_compo_');
95  return $this->renderView('@framework/main/component.html',[],$asString);
96  }
97 
107  public function renderDefaultView($parameters = [], $asString = false) {
108  return $this->renderView($this->injected->getDefaultViewName(), $parameters, $asString);
109  }
110 
120  public function execJSFromFile($jsFile, $parameters = [], $immediatly = true) {
121  if (isset($this->injected)) {
122  $view = $this->injected->getView();
123  if (isset($parameters))
124  $view->setVars($parameters);
125  $js = $view->render($jsFile . '.js', true);
126  return $this->exec($js, $immediatly);
127  }
128  throw new \Exception(get_class() . " instance is not properly instancied : you omitted the second parameter \$controller!");
129  }
130 
138  public static function diSemantic($controller, $options = [
139  'defer' => true,
140  'gc' => true
141  ]) {
142  $jquery = new JsUtils($options, $controller);
143  $jquery->semantic(new \Ajax\Semantic());
144  $jquery->setAjaxLoader("<div class=\"ui active centered inline text loader\">Loading</div>");
145  return $jquery;
146  }
147 
155  public static function diBootstrap($controller, $options = [
156  'defer' => true,
157  'gc' => true
158  ]) {
159  $jquery = new JsUtils($options, $controller);
160  $jquery->bootstrap(new \Ajax\Bootstrap());
161  $jquery->setAjaxLoader("<div class=\"d-flex justify-content-center\"><div class=\"spinner-border\" role=\"status\"><span class=\"sr-only\">Loading...</span></div></div>");
162  return $jquery;
163  }
164 
174  public function ajaxValidationRule(string $ruleName,string $checkUrl,string $params='{_value:value}',string $method='post'): void {
175  $this->exec(Rule::ajax($this, $ruleName, $checkUrl, $params, 'result=data.result;', $method), true);
176  }
177 
183  public function checkValidationRule($callback): void {
184  if (URequest::isPost()) {
185  $result = [];
186  UResponse::asJSON();
187  $value = $_POST['_value'];
188  $result['result'] = $callback($value);
189  echo \json_encode($result);
190  }
191  }
192 
193  public function applyInverted(){
194  $this->setParam('beforeCompileHtml', function ($elm) {
195  if (\method_exists($elm, 'setInverted')) {
196  $elm->setInverted(false);
197  }
198  });
199  }
200 }
fromDispatcher($dispatcher)
Definition: JsUtils.php:62
BaseHtml for HTML components.
Definition: BaseHtml.php:17
createScriptVariable(&$view, $view_var, $output)
Definition: JsUtils.php:50
execJSFromFile($jsFile, $parameters=[], $immediatly=true)
Loads and eventually executes a jsFile with php parameters, using the default template engine...
Definition: JsUtils.php:120
renderComponent(BaseHtml $component, bool $asString=false)
Compile and render a component.
Definition: JsUtils.php:93
static diSemantic($controller, $options=[ 'defer'=> true, 'gc'=> true])
Returns an instance of JsUtils initialized with Semantic (for di injection)
Definition: JsUtils.php:138
static ajax(JsUtils $js, $name, $url, $params, $jsCallback, $method="post", $parameters=[])
Definition: Rule.php:170
renderView($viewName, $parameters=[], $asString=false)
Performs jQuery compilation and displays a view.
Definition: JsUtils.php:76
static diBootstrap($controller, $options=[ 'defer'=> true, 'gc'=> true])
Returns an instance of JsUtils initialized with Bootstrap (for di injection)
Definition: JsUtils.php:155
addViewElement($identifier, $content, &$view)
Definition: JsUtils.php:41
forward($initialController, $controller, $action, $params=array())
Definition: JsUtils.php:54
setParam(string $param, $value)
Definition: JsUtils.php:526
_close_script($extra="\)
Outputs an closing </script>
exec($js, $immediatly=false)
Executes the code $js.
checkValidationRule($callback)
Checks a validation rule declared with .
Definition: JsUtils.php:183
compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE)
gather together all script needing to be output
Definition: JsUtils.php:319
ajaxValidationRule(string $ruleName, string $checkUrl, string $params='{_value:value}', string $method='post')
Adds a new semantic validation ajax rule.
Definition: JsUtils.php:174
renderDefaultView($parameters=[], $asString=false)
Performs jQuery compilation and displays the default view.
Definition: JsUtils.php:107
JQuery PHP library.
Definition: JsUtils.php:23
_open_script($src='')
Outputs an opening <script>
Definition: JsUtils.php:19
renderContent($initialControllerInstance, $viewName, $params=NULL)
Definition: JsUtils.php:58