1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax; |
4
|
|
|
|
5
|
|
|
use Ajax\common\traits\JqueryEventsTrait; |
6
|
|
|
use Ajax\common\traits\JqueryAjaxTrait; |
7
|
|
|
use Ajax\common\traits\JqueryActionsTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* JQuery Phalcon library |
11
|
|
|
* |
12
|
|
|
* @author jcheron |
13
|
|
|
* @version 1.002 |
14
|
|
|
* @license Apache 2 http://www.apache.org/licenses/ |
15
|
|
|
*/ |
16
|
|
|
/** |
17
|
|
|
* jQuery Class |
18
|
|
|
*/ |
19
|
|
|
class Jquery { |
20
|
|
|
use JqueryEventsTrait,JqueryAjaxTrait,JqueryActionsTrait; |
21
|
|
|
protected $_di; |
22
|
|
|
protected $_ui; |
23
|
|
|
protected $_bootstrap; |
24
|
|
|
protected $_semantic; |
25
|
|
|
protected $libraryFile; |
26
|
|
|
protected $_javascript_folder='js'; |
27
|
|
|
protected $jquery_code_for_load=array (); |
28
|
|
|
protected $jquery_code_for_compile=array (); |
29
|
|
|
protected $jquery_corner_active=FALSE; |
30
|
|
|
protected $jquery_table_sorter_active=FALSE; |
31
|
|
|
protected $jquery_table_sorter_pager_active=FALSE; |
32
|
|
|
|
33
|
|
|
protected $jquery_events=array ( |
34
|
|
|
"bind","blur","change","click","dblclick","delegate","die","error","focus","focusin","focusout","hover","keydown","keypress","keyup","live","load","mousedown","mousseenter","mouseleave","mousemove","mouseout","mouseover","mouseup","off","on","one","ready","resize","scroll","select","submit","toggle","trigger","triggerHandler","undind","undelegate","unload" |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
public function setDi($di) { |
38
|
|
|
$this->_di=$di; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function ui($ui=NULL) { |
42
|
|
|
if ($ui!==NULL) { |
43
|
|
|
$this->_ui=$ui; |
44
|
|
|
} |
45
|
|
|
return $this->_ui; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function bootstrap($bootstrap=NULL) { |
49
|
|
|
if ($bootstrap!==NULL) { |
50
|
|
|
$this->_bootstrap=$bootstrap; |
51
|
|
|
} |
52
|
|
|
return $this->_bootstrap; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function semantic($semantic=NULL) { |
56
|
|
|
if ($semantic!==NULL) { |
57
|
|
|
$this->_semantic=$semantic; |
58
|
|
|
} |
59
|
|
|
return $this->_semantic; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function __construct($params) { |
63
|
|
|
$this->params=array(); |
|
|
|
|
64
|
|
|
foreach ( $params as $key => $val ) { |
65
|
|
|
$this->params[$key]=$params[$key]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Inline |
72
|
|
|
* |
73
|
|
|
* Outputs a <script> tag |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* @param string $script |
77
|
|
|
* @param boolean $cdata a CDATA section should be added |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function inline($script, $cdata=TRUE) { |
|
|
|
|
81
|
|
|
$str=$this->_open_script(); |
82
|
|
|
$str.=($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n"; |
83
|
|
|
$str.=$this->_close_script(); |
84
|
|
|
|
85
|
|
|
return $str; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Open Script |
90
|
|
|
* |
91
|
|
|
* Outputs an opening <script> |
92
|
|
|
* |
93
|
|
|
* @access private |
94
|
|
|
* @param string $src |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
private function _open_script($src='') { |
98
|
|
|
$str='<script type="text/javascript" '; |
99
|
|
|
$str.=($src=='') ? '>' : ' src="'.$src.'">'; |
100
|
|
|
return $str; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Close Script |
105
|
|
|
* |
106
|
|
|
* Outputs an closing </script> |
107
|
|
|
* |
108
|
|
|
* @param string |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
private function _close_script($extra="\n") { |
112
|
|
|
return "</script>{$extra}"; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getLibraryScript() { |
116
|
|
|
$assets=$this->_di->get('assets'); |
117
|
|
|
$assets->addJs($this->libraryFile); |
118
|
|
|
return $assets->outputJs(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function setLibraryFile($name) { |
122
|
|
|
$this->libraryFile=$name; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function _setAjaxLoader($loader) { |
126
|
|
|
$this->ajaxLoader=$loader; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Outputs script directly |
131
|
|
|
* |
132
|
|
|
* @param string The element to attach the event to |
133
|
|
|
* @param string The code to execute |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function _output($array_js='') { |
137
|
|
|
if (!is_array($array_js)) { |
138
|
|
|
$array_js=array ( |
139
|
|
|
$array_js |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
foreach ( $array_js as $js ) { |
144
|
|
|
$this->jquery_code_for_compile[]="\t$js\n"; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Execute a generic jQuery call with a value. |
150
|
|
|
* @param string $jQueryCall |
151
|
|
|
* @param string $element |
152
|
|
|
* @param string $param |
153
|
|
|
* @param boolean $immediatly delayed if false |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) { |
|
|
|
|
156
|
|
|
$element=$this->_prep_element($element); |
157
|
|
|
if (isset($param)) { |
158
|
|
|
$param=$this->_prep_value($param); |
159
|
|
|
$str="$({$element}).{$jQueryCall}({$param});"; |
160
|
|
|
} else |
161
|
|
|
$str="$({$element}).{$jQueryCall}();"; |
162
|
|
|
if ($immediatly) |
163
|
|
|
$this->jquery_code_for_compile[]=$str; |
164
|
|
|
return $str; |
165
|
|
|
} |
166
|
|
|
/** |
167
|
|
|
* Execute a generic jQuery call with 2 elements. |
168
|
|
|
* @param string $jQueryCall |
169
|
|
|
* @param string $to |
170
|
|
|
* @param string $element |
171
|
|
|
* @param boolean $immediatly delayed if false |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) { |
175
|
|
|
$to=$this->_prep_element($to); |
176
|
|
|
$element=$this->_prep_element($element); |
177
|
|
|
$str="$({$to}).{$jQueryCall}({$element});"; |
178
|
|
|
if ($immediatly) |
179
|
|
|
$this->jquery_code_for_compile[]=$str; |
180
|
|
|
return $str; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Creates a jQuery sortable |
185
|
|
|
* |
186
|
|
|
* @param string $element |
187
|
|
|
* @param array $options |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
public function sortable($element, $options=array()) { |
191
|
|
|
if (count($options)>0) { |
192
|
|
|
$sort_options=array (); |
193
|
|
|
foreach ( $options as $k => $v ) { |
194
|
|
|
$sort_options[]="\n\t\t".$k.': '.$v.""; |
195
|
|
|
} |
196
|
|
|
$sort_options=implode(",", $sort_options); |
197
|
|
|
} else { |
198
|
|
|
$sort_options=''; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return "$(".$this->_prep_element($element).").sortable({".$sort_options."\n\t});"; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Table Sorter Plugin |
206
|
|
|
* |
207
|
|
|
* @param string $table table name |
208
|
|
|
* @param string $options plugin location |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function tablesorter($table='', $options='') { |
212
|
|
|
$this->jquery_code_for_compile[]="\t$(".$this->_prep_element($table).").tablesorter($options);\n"; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Constructs the syntax for an event, and adds to into the array for compilation |
217
|
|
|
* |
218
|
|
|
* @param string $element The element to attach the event to |
219
|
|
|
* @param string $js The code to execute |
220
|
|
|
* @param string $event The event to pass |
221
|
|
|
* @param boolean $preventDefault If set to true, the default action of the event will not be triggered. |
222
|
|
|
* @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) { |
226
|
|
|
if (is_array($js)) { |
227
|
|
|
$js=implode("\n\t\t", $js); |
228
|
|
|
} |
229
|
|
|
if ($preventDefault===true) { |
230
|
|
|
$js="event.preventDefault();\n".$js; |
231
|
|
|
} |
232
|
|
|
if ($stopPropagation===true) { |
233
|
|
|
$js="event.stopPropagation();\n".$js; |
234
|
|
|
} |
235
|
|
|
if (array_search($event, $this->jquery_events)===false) |
236
|
|
|
$event="\n\t$(".$this->_prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n"; |
237
|
|
|
else |
238
|
|
|
$event="\n\t$(".$this->_prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n"; |
239
|
|
|
if($immediatly) |
240
|
|
|
$this->jquery_code_for_compile[]=$event; |
241
|
|
|
return $event; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* As events are specified, they are stored in an array |
246
|
|
|
* This function compiles them all for output on a page |
247
|
|
|
* @param view $view |
248
|
|
|
* @param string $view_var |
249
|
|
|
* @param boolean $script_tags |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
public function _compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) { |
253
|
|
|
// Components UI |
254
|
|
|
$ui=$this->ui(); |
255
|
|
|
if ($this->ui()!=NULL) { |
256
|
|
|
if ($ui->isAutoCompile()) { |
257
|
|
|
$ui->compile(true); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// Components BS |
262
|
|
|
$bootstrap=$this->bootstrap(); |
263
|
|
|
if ($this->bootstrap()!=NULL) { |
264
|
|
|
if ($bootstrap->isAutoCompile()) { |
265
|
|
|
$bootstrap->compile(true); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
// Components Semantic |
270
|
|
|
$semantic=$this->semantic(); |
271
|
|
|
if ($semantic!=NULL) { |
272
|
|
|
if ($semantic->isAutoCompile()) { |
273
|
|
|
$semantic->compile(true); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// External references |
278
|
|
|
$external_scripts=implode('', $this->jquery_code_for_load); |
279
|
|
|
extract(array ( |
280
|
|
|
'library_src' => $external_scripts |
281
|
|
|
)); |
282
|
|
|
|
283
|
|
|
if (count($this->jquery_code_for_compile)==0) { |
284
|
|
|
// no inline references, let's just return |
285
|
|
|
return; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// Inline references |
289
|
|
|
$script='$(document).ready(function() {'."\n"; |
290
|
|
|
$script.=implode('', $this->jquery_code_for_compile); |
291
|
|
|
$script.='});'; |
292
|
|
|
|
293
|
|
|
$this->jquery_code_for_compile=array(); |
294
|
|
|
if($this->params["debug"]==false){ |
295
|
|
|
$script=$this->minify($script); |
296
|
|
|
} |
297
|
|
|
$output=($script_tags===FALSE) ? $script : $this->inline($script); |
298
|
|
|
|
299
|
|
|
if ($view!=NULL) |
300
|
|
|
$view->setVar($view_var, $output); |
301
|
|
|
return $output; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
public function _addToCompile($jsScript) { |
305
|
|
|
$this->jquery_code_for_compile[]=$jsScript; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Clears the array of script events collected for output |
310
|
|
|
* |
311
|
|
|
* @return void |
312
|
|
|
*/ |
313
|
|
|
public function _clear_compile() { |
314
|
|
|
$this->jquery_code_for_compile=array (); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* A wrapper for writing document.ready() |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function _document_ready($js) { |
322
|
|
|
if (!is_array($js)) { |
323
|
|
|
$js=array ( |
324
|
|
|
$js |
325
|
|
|
); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
foreach ( $js as $script ) { |
329
|
|
|
$this->jquery_code_for_compile[]=$script; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Puts HTML element in quotes for use in jQuery code |
335
|
|
|
* unless the supplied element is the Javascript 'this' |
336
|
|
|
* object, in which case no quotes are added |
337
|
|
|
* |
338
|
|
|
* @param string $element |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
|
|
public function _prep_element($element) { |
342
|
|
View Code Duplication |
if (strrpos($element, 'this')===false&&strrpos($element, 'event')===false&&strrpos($element, 'self')===false) { |
|
|
|
|
343
|
|
|
$element='"'.addslashes($element).'"'; |
344
|
|
|
} |
345
|
|
|
return $element; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Puts HTML values in quotes for use in jQuery code |
350
|
|
|
* unless the supplied value contains the Javascript 'this' or 'event' |
351
|
|
|
* object, in which case no quotes are added |
352
|
|
|
* |
353
|
|
|
* @param string $value |
354
|
|
|
* @return string |
355
|
|
|
*/ |
356
|
|
|
public function _prep_value($value) { |
357
|
|
|
if (is_array($value)) { |
358
|
|
|
$value=implode(",", $value); |
359
|
|
|
} |
360
|
|
View Code Duplication |
if (strrpos($value, 'this')===false&&strrpos($value, 'event')===false&&strrpos($value, 'self')===false) { |
|
|
|
|
361
|
|
|
$value='"'.$value.'"'; |
362
|
|
|
} |
363
|
|
|
return $value; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
private function minify($input) { |
367
|
|
|
if(trim($input) === "") return $input; |
368
|
|
|
return preg_replace( |
369
|
|
|
array( |
370
|
|
|
// Remove comment(s) |
371
|
|
|
'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#', |
372
|
|
|
// Remove white-space(s) outside the string and regex |
373
|
|
|
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s', |
374
|
|
|
// Remove the last semicolon |
375
|
|
|
'#;+\}#', |
376
|
|
|
// Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}` |
377
|
|
|
'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i', |
378
|
|
|
// --ibid. From `foo['bar']` to `foo.bar` |
379
|
|
|
'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i' |
380
|
|
|
), |
381
|
|
|
array( |
382
|
|
|
'$1', |
383
|
|
|
'$1$2', |
384
|
|
|
'}', |
385
|
|
|
'$1$3', |
386
|
|
|
'$1.$3' |
387
|
|
|
), |
388
|
|
|
$input); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: