|
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
|
|
|
protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
|
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
|
|
|
/** |
|
72
|
|
|
* Inline |
|
73
|
|
|
* |
|
74
|
|
|
* Outputs a <script> tag |
|
75
|
|
|
* |
|
76
|
|
|
* @access public |
|
77
|
|
|
* @param string $script |
|
78
|
|
|
* @param boolean $cdata a CDATA section should be added |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
public function inline($script, $cdata=TRUE) { |
|
|
|
|
|
|
82
|
|
|
$str=$this->_open_script(); |
|
83
|
|
|
$str.=($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n"; |
|
84
|
|
|
$str.=$this->_close_script(); |
|
85
|
|
|
|
|
86
|
|
|
return $str; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// -------------------------------------------------------------------- |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Open Script |
|
93
|
|
|
* |
|
94
|
|
|
* Outputs an opening <script> |
|
95
|
|
|
* |
|
96
|
|
|
* @access private |
|
97
|
|
|
* @param string $src |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
private function _open_script($src='') { |
|
101
|
|
|
$str='<script type="text/javascript" '; |
|
102
|
|
|
$str.=($src=='') ? '>' : ' src="'.$src.'">'; |
|
103
|
|
|
return $str; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// -------------------------------------------------------------------- |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Close Script |
|
110
|
|
|
* |
|
111
|
|
|
* Outputs an closing </script> |
|
112
|
|
|
* |
|
113
|
|
|
* @param string |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
private function _close_script($extra="\n") { |
|
117
|
|
|
return "</script>{$extra}"; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getLibraryScript() { |
|
121
|
|
|
$assets=$this->_di->get('assets'); |
|
122
|
|
|
$assets->addJs($this->libraryFile); |
|
123
|
|
|
return $assets->outputJs(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function setLibraryFile($name) { |
|
127
|
|
|
$this->libraryFile=$name; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function _setAjaxLoader($loader) { |
|
131
|
|
|
$this->ajaxLoader=$loader; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
// -------------------------------------------------------------------- |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Outputs script directly |
|
140
|
|
|
* |
|
141
|
|
|
* @param string The element to attach the event to |
|
142
|
|
|
* @param string The code to execute |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function _output($array_js='') { |
|
146
|
|
|
if (!is_array($array_js)) { |
|
147
|
|
|
$array_js=array ( |
|
148
|
|
|
$array_js |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
foreach ( $array_js as $js ) { |
|
153
|
|
|
$this->jquery_code_for_compile[]="\t$js\n"; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// -------------------------------------------------------------------- |
|
158
|
|
|
// Effects |
|
159
|
|
|
// -------------------------------------------------------------------- |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Execute a generic jQuery call with a value. |
|
166
|
|
|
* @param string $jQueryCall |
|
167
|
|
|
* @param string $element |
|
168
|
|
|
* @param string $param |
|
169
|
|
|
* @param boolean $immediatly delayed if false |
|
170
|
|
|
*/ |
|
171
|
|
View Code Duplication |
public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) { |
|
|
|
|
|
|
172
|
|
|
$element=$this->_prep_element($element); |
|
173
|
|
|
if (isset($param)) { |
|
174
|
|
|
$param=$this->_prep_value($param); |
|
175
|
|
|
$str="$({$element}).{$jQueryCall}({$param});"; |
|
176
|
|
|
} else |
|
177
|
|
|
$str="$({$element}).{$jQueryCall}();"; |
|
178
|
|
|
if ($immediatly) |
|
179
|
|
|
$this->jquery_code_for_compile[]=$str; |
|
180
|
|
|
return $str; |
|
181
|
|
|
} |
|
182
|
|
|
/** |
|
183
|
|
|
* Execute a generic jQuery call with 2 elements. |
|
184
|
|
|
* @param string $jQueryCall |
|
185
|
|
|
* @param string $to |
|
186
|
|
|
* @param string $element |
|
187
|
|
|
* @param boolean $immediatly delayed if false |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) { |
|
191
|
|
|
$to=$this->_prep_element($to); |
|
192
|
|
|
$element=$this->_prep_element($element); |
|
193
|
|
|
$str="$({$to}).{$jQueryCall}({$element});"; |
|
194
|
|
|
if ($immediatly) |
|
195
|
|
|
$this->jquery_code_for_compile[]=$str; |
|
196
|
|
|
return $str; |
|
197
|
|
|
} |
|
198
|
|
|
// -------------------------------------------------------------------- |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
// -------------------------------------------------------------------- |
|
202
|
|
|
// Plugins |
|
203
|
|
|
// -------------------------------------------------------------------- |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Creates a jQuery sortable |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $element |
|
209
|
|
|
* @param array $options |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
public function sortable($element, $options=array()) { |
|
213
|
|
|
if (count($options)>0) { |
|
214
|
|
|
$sort_options=array (); |
|
215
|
|
|
foreach ( $options as $k => $v ) { |
|
216
|
|
|
$sort_options[]="\n\t\t".$k.': '.$v.""; |
|
217
|
|
|
} |
|
218
|
|
|
$sort_options=implode(",", $sort_options); |
|
219
|
|
|
} else { |
|
220
|
|
|
$sort_options=''; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return "$(".$this->_prep_element($element).").sortable({".$sort_options."\n\t});"; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// -------------------------------------------------------------------- |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Table Sorter Plugin |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $table table name |
|
232
|
|
|
* @param string $options plugin location |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
|
|
public function tablesorter($table='', $options='') { |
|
236
|
|
|
$this->jquery_code_for_compile[]="\t$(".$this->_prep_element($table).").tablesorter($options);\n"; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
// -------------------------------------------------------------------- |
|
240
|
|
|
// Class functions |
|
241
|
|
|
// -------------------------------------------------------------------- |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Constructs the syntax for an event, and adds to into the array for compilation |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $element The element to attach the event to |
|
247
|
|
|
* @param string $js The code to execute |
|
248
|
|
|
* @param string $event The event to pass |
|
249
|
|
|
* @param boolean $preventDefault If set to true, the default action of the event will not be triggered. |
|
250
|
|
|
* @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. |
|
251
|
|
|
* @return string |
|
252
|
|
|
*/ |
|
253
|
|
|
public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) { |
|
254
|
|
|
if (is_array($js)) { |
|
255
|
|
|
$js=implode("\n\t\t", $js); |
|
256
|
|
|
} |
|
257
|
|
|
if ($preventDefault===true) { |
|
258
|
|
|
$js="event.preventDefault();\n".$js; |
|
259
|
|
|
} |
|
260
|
|
|
if ($stopPropagation===true) { |
|
261
|
|
|
$js="event.stopPropagation();\n".$js; |
|
262
|
|
|
} |
|
263
|
|
|
if (array_search($event, $this->jquery_events)===false) |
|
264
|
|
|
$event="\n\t$(".$this->_prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n"; |
|
265
|
|
|
else |
|
266
|
|
|
$event="\n\t$(".$this->_prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n"; |
|
267
|
|
|
if($immediatly) |
|
268
|
|
|
$this->jquery_code_for_compile[]=$event; |
|
269
|
|
|
return $event; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
// -------------------------------------------------------------------- |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* As events are specified, they are stored in an array |
|
276
|
|
|
* This function compiles them all for output on a page |
|
277
|
|
|
* @param view $view |
|
278
|
|
|
* @param string $view_var |
|
279
|
|
|
* @param boolean $script_tags |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
public function _compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) { |
|
283
|
|
|
// Components UI |
|
284
|
|
|
$ui=$this->ui(); |
|
285
|
|
|
if ($this->ui()!=NULL) { |
|
286
|
|
|
if ($ui->isAutoCompile()) { |
|
287
|
|
|
$ui->compile(true); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
// Components BS |
|
292
|
|
|
$bootstrap=$this->bootstrap(); |
|
293
|
|
|
if ($this->bootstrap()!=NULL) { |
|
294
|
|
|
if ($bootstrap->isAutoCompile()) { |
|
295
|
|
|
$bootstrap->compile(true); |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
// Components Semantic |
|
300
|
|
|
$semantic=$this->semantic(); |
|
301
|
|
|
if ($semantic!=NULL) { |
|
302
|
|
|
if ($semantic->isAutoCompile()) { |
|
303
|
|
|
$semantic->compile(true); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
// External references |
|
308
|
|
|
$external_scripts=implode('', $this->jquery_code_for_load); |
|
309
|
|
|
extract(array ( |
|
310
|
|
|
'library_src' => $external_scripts |
|
311
|
|
|
)); |
|
312
|
|
|
|
|
313
|
|
|
if (count($this->jquery_code_for_compile)==0) { |
|
314
|
|
|
// no inline references, let's just return |
|
315
|
|
|
return; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
// Inline references |
|
319
|
|
|
$script='$(document).ready(function() {'."\n"; |
|
320
|
|
|
$script.=implode('', $this->jquery_code_for_compile); |
|
321
|
|
|
$script.='});'; |
|
322
|
|
|
|
|
323
|
|
|
$this->jquery_code_for_compile=array(); |
|
324
|
|
|
if($this->params["debug"]==false){ |
|
325
|
|
|
$script=$this->minify($script); |
|
326
|
|
|
} |
|
327
|
|
|
$output=($script_tags===FALSE) ? $script : $this->inline($script); |
|
328
|
|
|
|
|
329
|
|
|
if ($view!=NULL) |
|
330
|
|
|
$view->setVar($view_var, $output); |
|
331
|
|
|
return $output; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
public function _addToCompile($jsScript) { |
|
335
|
|
|
$this->jquery_code_for_compile[]=$jsScript; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
// -------------------------------------------------------------------- |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Clears the array of script events collected for output |
|
342
|
|
|
* |
|
343
|
|
|
* @return void |
|
344
|
|
|
*/ |
|
345
|
|
|
public function _clear_compile() { |
|
346
|
|
|
$this->jquery_code_for_compile=array (); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
// -------------------------------------------------------------------- |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* A wrapper for writing document.ready() |
|
353
|
|
|
* |
|
354
|
|
|
* @return string |
|
355
|
|
|
*/ |
|
356
|
|
|
public function _document_ready($js) { |
|
357
|
|
|
if (!is_array($js)) { |
|
358
|
|
|
$js=array ( |
|
359
|
|
|
$js |
|
360
|
|
|
); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
foreach ( $js as $script ) { |
|
364
|
|
|
$this->jquery_code_for_compile[]=$script; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
// -------------------------------------------------------------------- |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Puts HTML element in quotes for use in jQuery code |
|
372
|
|
|
* unless the supplied element is the Javascript 'this' |
|
373
|
|
|
* object, in which case no quotes are added |
|
374
|
|
|
* |
|
375
|
|
|
* @param string $element |
|
376
|
|
|
* @return string |
|
377
|
|
|
*/ |
|
378
|
|
|
public function _prep_element($element) { |
|
379
|
|
View Code Duplication |
if (strrpos($element, 'this')===false&&strrpos($element, 'event')===false&&strrpos($element, 'self')===false) { |
|
|
|
|
|
|
380
|
|
|
$element='"'.addslashes($element).'"'; |
|
381
|
|
|
} |
|
382
|
|
|
return $element; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Puts HTML values in quotes for use in jQuery code |
|
387
|
|
|
* unless the supplied value contains the Javascript 'this' or 'event' |
|
388
|
|
|
* object, in which case no quotes are added |
|
389
|
|
|
* |
|
390
|
|
|
* @param string $value |
|
391
|
|
|
* @return string |
|
392
|
|
|
*/ |
|
393
|
|
|
public function _prep_value($value) { |
|
394
|
|
|
if (is_array($value)) { |
|
395
|
|
|
$value=implode(",", $value); |
|
396
|
|
|
} |
|
397
|
|
View Code Duplication |
if (strrpos($value, 'this')===false&&strrpos($value, 'event')===false&&strrpos($value, 'self')===false) { |
|
|
|
|
|
|
398
|
|
|
$value='"'.$value.'"'; |
|
399
|
|
|
} |
|
400
|
|
|
return $value; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
// -------------------------------------------------------------------- |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Ensures the speed parameter is valid for jQuery |
|
407
|
|
|
* |
|
408
|
|
|
* @param string|int $speed |
|
409
|
|
|
* @return string |
|
410
|
|
|
*/ |
|
411
|
|
|
private function _validate_speed($speed) { |
|
|
|
|
|
|
412
|
|
|
if (in_array($speed, array ( |
|
413
|
|
|
'slow','normal','fast' |
|
414
|
|
|
))) { |
|
415
|
|
|
$speed='"'.$speed.'"'; |
|
416
|
|
|
} elseif (preg_match("/[^0-9]/", $speed)) { |
|
417
|
|
|
$speed=''; |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
return $speed; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
private function minify($input) { |
|
424
|
|
|
if(trim($input) === "") return $input; |
|
425
|
|
|
return preg_replace( |
|
426
|
|
|
array( |
|
427
|
|
|
// Remove comment(s) |
|
428
|
|
|
'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#', |
|
429
|
|
|
// Remove white-space(s) outside the string and regex |
|
430
|
|
|
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s', |
|
431
|
|
|
// Remove the last semicolon |
|
432
|
|
|
'#;+\}#', |
|
433
|
|
|
// Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}` |
|
434
|
|
|
'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i', |
|
435
|
|
|
// --ibid. From `foo['bar']` to `foo.bar` |
|
436
|
|
|
'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i' |
|
437
|
|
|
), |
|
438
|
|
|
array( |
|
439
|
|
|
'$1', |
|
440
|
|
|
'$1$2', |
|
441
|
|
|
'}', |
|
442
|
|
|
'$1$3', |
|
443
|
|
|
'$1.$3' |
|
444
|
|
|
), |
|
445
|
|
|
$input); |
|
446
|
|
|
} |
|
447
|
|
|
} |
|
448
|
|
|
/* End of file Jquery.php */ |
|
449
|
|
|
|
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: