1
|
|
|
<?php |
2
|
|
|
namespace Ajax\common\traits; |
3
|
|
|
|
4
|
|
|
use Ajax\service\Javascript; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* |
8
|
|
|
* @author jc |
9
|
|
|
* @property array $jquery_code_for_compile |
10
|
|
|
* @property array $jquery_code_for_compile_at_last |
11
|
|
|
*/ |
12
|
|
|
trait JsUtilsActionsTrait { |
13
|
|
|
|
14
|
|
|
abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* show or hide with effect |
18
|
|
|
* |
19
|
|
|
* @param string $action |
20
|
|
|
* @param string $element |
21
|
|
|
* element |
22
|
|
|
* @param string $speed |
23
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
24
|
|
|
* @param string $callback |
25
|
|
|
* Javascript callback function |
26
|
|
|
* @param boolean $immediatly |
27
|
|
|
* defers the execution if set to false |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
protected function _showHideWithEffect($action, $element = 'this', $speed = '', $callback = '', $immediatly = false) { |
31
|
|
|
$element = Javascript::prep_element($element); |
32
|
|
|
$speed = $this->_validate_speed($speed); |
33
|
|
|
if ($callback != '') { |
34
|
|
|
$callback = ", function(){\n{$callback}\n}"; |
35
|
|
|
} |
36
|
|
|
$str = "$({$element}).{$action}({$speed}{$callback});"; |
37
|
|
|
if ($immediatly) |
38
|
|
|
$this->jquery_code_for_compile[] = $str; |
39
|
|
|
return $str; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Ensures the speed parameter is valid for jQuery |
44
|
|
|
* |
45
|
|
|
* @param string|int $speed |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
private function _validate_speed($speed) { |
49
|
|
|
if (in_array($speed, array( |
50
|
|
|
'slow', |
51
|
|
|
'normal', |
52
|
|
|
'fast' |
53
|
|
|
))) { |
54
|
|
|
$speed = '"' . $speed . '"'; |
55
|
|
|
} elseif (preg_match("/[^0-9]/", $speed)) { |
56
|
|
|
$speed = ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $speed; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Execute a generic jQuery call with a value. |
64
|
|
|
* |
65
|
|
|
* @param string $jQueryCall |
66
|
|
|
* @param string $element |
67
|
|
|
* @param string $param |
68
|
|
|
* @param boolean $immediatly |
69
|
|
|
* delayed if false |
70
|
|
|
*/ |
71
|
|
|
public function _genericCallValue($jQueryCall, $element = 'this', $param = "", $immediatly = false) { |
72
|
|
|
$element = Javascript::prep_element($element); |
73
|
|
|
if (isset($param)) { |
74
|
|
|
$param = Javascript::prep_value($param); |
75
|
|
|
$str = "$({$element}).{$jQueryCall}({$param});"; |
76
|
|
|
} else |
77
|
|
|
$str = "$({$element}).{$jQueryCall}();"; |
78
|
|
|
if ($immediatly) |
79
|
|
|
$this->jquery_code_for_compile[] = $str; |
80
|
|
|
return $str; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Execute a generic jQuery call with 2 elements. |
85
|
|
|
* |
86
|
|
|
* @param string $jQueryCall |
87
|
|
|
* @param string $to |
88
|
|
|
* @param string $element |
89
|
|
|
* @param boolean $immediatly |
90
|
|
|
* delayed if false |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function _genericCallElement($jQueryCall, $to = 'this', $element, $immediatly = false) { |
94
|
|
|
$to = Javascript::prep_element($to); |
95
|
|
|
$element = Javascript::prep_element($element); |
96
|
|
|
$str = "$({$to}).{$jQueryCall}({$element});"; |
97
|
|
|
if ($immediatly) |
98
|
|
|
$this->jquery_code_for_compile[] = $str; |
99
|
|
|
return $str; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* add class to element |
104
|
|
|
* |
105
|
|
|
* @param string $element |
106
|
|
|
* @param string $class |
107
|
|
|
* to add |
108
|
|
|
* @param boolean $immediatly |
109
|
|
|
* defers the execution if set to false |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function addClass($element = 'this', $class = '', $immediatly = false) { |
113
|
|
|
return $this->_genericCallValue('addClass', $element, $class, $immediatly); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Insert content, specified by the parameter, after each element in the set of matched elements |
118
|
|
|
* |
119
|
|
|
* @param string $to |
120
|
|
|
* @param string $element |
121
|
|
|
* @param boolean $immediatly |
122
|
|
|
* defers the execution if set to false |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function after($to, $element, $immediatly = false) { |
126
|
|
|
return $this->_genericCallElement('after', $to, $element, $immediatly); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Insert content, specified by the parameter, before each element in the set of matched elements |
131
|
|
|
* |
132
|
|
|
* @param string $to |
133
|
|
|
* @param string $element |
134
|
|
|
* @param boolean $immediatly |
135
|
|
|
* defers the execution if set to false |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function before($to, $element, $immediatly = false) { |
139
|
|
|
return $this->_genericCallElement('before', $to, $element, $immediatly); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get or set the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element. |
144
|
|
|
* |
145
|
|
|
* @param string $element |
146
|
|
|
* @param string $attributeName |
147
|
|
|
* @param string $value |
148
|
|
|
* @param boolean $immediatly |
149
|
|
|
* delayed if false |
150
|
|
|
*/ |
151
|
|
|
public function attr($element = 'this', $attributeName, $value = "", $immediatly = false) { |
152
|
|
|
$element = Javascript::prep_element($element); |
153
|
|
|
if (isset($value)) { |
154
|
|
|
$value = Javascript::prep_value($value); |
155
|
|
|
$str = "$({$element}).attr(\"$attributeName\",{$value});"; |
156
|
|
|
} else |
157
|
|
|
$str = "$({$element}).attr(\"$attributeName\");"; |
158
|
|
|
if ($immediatly) |
159
|
|
|
$this->jquery_code_for_compile[] = $str; |
160
|
|
|
return $str; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get or set the value of the first element in the set of matched elements or set one or more attributes for every matched element. |
165
|
|
|
* |
166
|
|
|
* @param string $element |
167
|
|
|
* @param string $value |
168
|
|
|
* @param boolean $immediatly |
169
|
|
|
* defers the execution if set to false |
170
|
|
|
*/ |
171
|
|
|
public function val($element = 'this', $value = '', $immediatly = false) { |
172
|
|
|
return $this->_genericCallValue('val', $element, $value, $immediatly); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get or set the html of an attribute for the first element in the set of matched elements. |
177
|
|
|
* |
178
|
|
|
* @param string $element |
179
|
|
|
* @param string $value |
180
|
|
|
* @param boolean $immediatly |
181
|
|
|
* defers the execution if set to false |
182
|
|
|
*/ |
183
|
|
|
public function html($element = 'this', $value = '', $immediatly = false) { |
184
|
|
|
return $this->_genericCallValue('html', $element, $value, $immediatly); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Outputs a javascript library animate event |
189
|
|
|
* |
190
|
|
|
* @param string $element |
191
|
|
|
* element |
192
|
|
|
* @param array|string $params |
193
|
|
|
* @param string $speed |
194
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
195
|
|
|
* @param string $extra |
196
|
|
|
* @param boolean $immediatly |
197
|
|
|
* defers the execution if set to false |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function animate($element = 'this', $params = array(), $speed = '', $extra = '', $immediatly = false) { |
201
|
|
|
$element = Javascript::prep_element($element); |
202
|
|
|
$speed = $this->_validate_speed($speed); |
203
|
|
|
|
204
|
|
|
$animations = "\t\t\t"; |
205
|
|
|
if (\is_array($params)) { |
206
|
|
|
foreach ($params as $param => $value) { |
207
|
|
|
$animations .= $param . ': \'' . $value . '\', '; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
$animations = substr($animations, 0, - 2); // remove the last ", " |
211
|
|
|
|
212
|
|
|
if ($speed != '') { |
213
|
|
|
$speed = ', ' . $speed; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if ($extra != '') { |
217
|
|
|
$extra = ', ' . $extra; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$str = "$({$element}).animate({\n$animations\n\t\t}" . $speed . $extra . ");"; |
221
|
|
|
|
222
|
|
|
if ($immediatly) |
223
|
|
|
$this->jquery_code_for_compile[] = $str; |
224
|
|
|
return $str; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Insert content, specified by the parameter $element, to the end of each element in the set of matched elements $to. |
229
|
|
|
* |
230
|
|
|
* @param string $to |
231
|
|
|
* @param string $element |
232
|
|
|
* @param boolean $immediatly |
233
|
|
|
* defers the execution if set to false |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function append($to, $element, $immediatly = false) { |
237
|
|
|
return $this->_genericCallElement('append', $to, $element, $immediatly); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Insert content, specified by the parameter $element, to the beginning of each element in the set of matched elements $to. |
242
|
|
|
* |
243
|
|
|
* @param string $to |
244
|
|
|
* @param string $element |
245
|
|
|
* @param boolean $immediatly |
246
|
|
|
* defers the execution if set to false |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function prepend($to, $element, $immediatly = false) { |
250
|
|
|
return $this->_genericCallElement('prepend', $to, $element, $immediatly); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Execute a javascript library hide action |
255
|
|
|
* |
256
|
|
|
* @param string $element |
257
|
|
|
* element |
258
|
|
|
* @param string $speed |
259
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
260
|
|
|
* @param string $callback |
261
|
|
|
* Javascript callback function |
262
|
|
|
* @param boolean $immediatly |
263
|
|
|
* defers the execution if set to false |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public function fadeIn($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
267
|
|
|
return $this->_showHideWithEffect("fadeIn", $element, $speed, $callback, $immediatly); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Execute a javascript library hide action |
272
|
|
|
* |
273
|
|
|
* @param string $element |
274
|
|
|
* element |
275
|
|
|
* @param string $speed |
276
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
277
|
|
|
* @param string $callback |
278
|
|
|
* Javascript callback function |
279
|
|
|
* @param boolean $immediatly |
280
|
|
|
* defers the execution if set to false |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public function fadeOut($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
284
|
|
|
return $this->_showHideWithEffect("fadeOut", $element, $speed, $callback, $immediatly); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Execute a javascript library slideUp action |
289
|
|
|
* |
290
|
|
|
* @param string $element |
291
|
|
|
* element |
292
|
|
|
* @param string $speed |
293
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
294
|
|
|
* @param string $callback |
295
|
|
|
* Javascript callback function |
296
|
|
|
* @param boolean $immediatly |
297
|
|
|
* defers the execution if set to false |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public function slideUp($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
301
|
|
|
return $this->_showHideWithEffect("slideUp", $element, $speed, $callback, $immediatly); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Execute a javascript library removeClass action |
306
|
|
|
* |
307
|
|
|
* @param string $element |
308
|
|
|
* element |
309
|
|
|
* @param string $class |
310
|
|
|
* Class to add |
311
|
|
|
* @param boolean $immediatly |
312
|
|
|
* defers the execution if set to false |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
|
|
public function removeClass($element = 'this', $class = '', $immediatly = false) { |
316
|
|
|
return $this->_genericCallValue('removeClass', $element, $class, $immediatly); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Execute a javascript library slideDown action |
321
|
|
|
* |
322
|
|
|
* @param string $element |
323
|
|
|
* element |
324
|
|
|
* @param string $speed |
325
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
326
|
|
|
* @param string $callback |
327
|
|
|
* Javascript callback function |
328
|
|
|
* @param boolean $immediatly |
329
|
|
|
* defers the execution if set to false |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
public function slideDown($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
333
|
|
|
return $this->_showHideWithEffect("slideDown", $element, $speed, $callback, $immediatly); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Execute a javascript library slideToggle action |
338
|
|
|
* |
339
|
|
|
* @param string $element |
340
|
|
|
* element |
341
|
|
|
* @param string $speed |
342
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
343
|
|
|
* @param string $callback |
344
|
|
|
* Javascript callback function |
345
|
|
|
* @param boolean $immediatly |
346
|
|
|
* defers the execution if set to false |
347
|
|
|
* @return string |
348
|
|
|
*/ |
349
|
|
|
public function slideToggle($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
350
|
|
|
return $this->_showHideWithEffect("slideToggle", $element, $speed, $callback, $immediatly); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Execute a javascript library hide action |
355
|
|
|
* |
356
|
|
|
* @param string $element |
357
|
|
|
* element |
358
|
|
|
* @param string $speed |
359
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
360
|
|
|
* @param string $callback |
361
|
|
|
* Javascript callback function |
362
|
|
|
* @param boolean $immediatly |
363
|
|
|
* defers the execution if set to false |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
|
|
public function hide($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
367
|
|
|
return $this->_showHideWithEffect("hide", $element, $speed, $callback, $immediatly); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Execute a javascript library toggle action |
372
|
|
|
* |
373
|
|
|
* @param string $element |
374
|
|
|
* element |
375
|
|
|
* @param string $speed |
376
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
377
|
|
|
* @param string $callback |
378
|
|
|
* Javascript callback function |
379
|
|
|
* @param boolean $immediatly |
380
|
|
|
* defers the execution if set to false |
381
|
|
|
* @return string |
382
|
|
|
*/ |
383
|
|
|
public function toggle($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
384
|
|
|
return $this->_showHideWithEffect("toggle", $element, $speed, $callback, $immediatly); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Execute a javascript library toggle class action |
389
|
|
|
* |
390
|
|
|
* @param string $element |
391
|
|
|
* element |
392
|
|
|
* @param string $class |
393
|
|
|
* @param boolean $immediatly |
394
|
|
|
* defers the execution if set to false |
395
|
|
|
* @return string |
396
|
|
|
*/ |
397
|
|
|
public function toggleClass($element = 'this', $class = '', $immediatly = false) { |
398
|
|
|
return $this->_genericCallValue('toggleClass', $element, $class, $immediatly); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Execute all handlers and behaviors attached to the matched elements for the given event. |
403
|
|
|
* |
404
|
|
|
* @param string $element |
405
|
|
|
* @param string $event |
406
|
|
|
* @param boolean $immediatly |
407
|
|
|
* defers the execution if set to false |
408
|
|
|
*/ |
409
|
|
|
public function trigger($element = 'this', $event = 'click', $immediatly = false) { |
410
|
|
|
$element = Javascript::prep_element($element); |
411
|
|
|
$str = "$({$element}).trigger(\"$event\");"; |
412
|
|
|
|
413
|
|
|
if ($immediatly) |
414
|
|
|
$this->jquery_code_for_compile[] = $str; |
415
|
|
|
return $str; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Execute a javascript library show action |
420
|
|
|
* |
421
|
|
|
* @param string $element |
422
|
|
|
* element |
423
|
|
|
* @param string $speed |
424
|
|
|
* One of 'slow', 'normal', 'fast', or time in milliseconds |
425
|
|
|
* @param string $callback |
426
|
|
|
* Javascript callback function |
427
|
|
|
* @param boolean $immediatly |
428
|
|
|
* defers the execution if set to false |
429
|
|
|
* @return string |
430
|
|
|
*/ |
431
|
|
|
public function show($element = 'this', $speed = '', $callback = '', $immediatly = false) { |
432
|
|
|
return $this->_showHideWithEffect("show", $element, $speed, $callback, $immediatly); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Creates a jQuery sortable |
437
|
|
|
* |
438
|
|
|
* @param string $element |
439
|
|
|
* @param array $options |
440
|
|
|
* @return string |
441
|
|
|
*/ |
442
|
|
|
public function sortable($element, $options = array()) { |
443
|
|
|
if (count($options) > 0) { |
444
|
|
|
$sort_options = array(); |
445
|
|
|
foreach ($options as $k => $v) { |
446
|
|
|
$sort_options[] = "\n\t\t" . $k . ': ' . $v . ""; |
447
|
|
|
} |
448
|
|
|
$sort_options = implode(",", $sort_options); |
449
|
|
|
} else { |
450
|
|
|
$sort_options = ''; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
return "$(" . Javascript::prep_element($element) . ").sortable({" . $sort_options . "\n\t});"; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Table Sorter Plugin |
458
|
|
|
* |
459
|
|
|
* @param string $table |
460
|
|
|
* table name |
461
|
|
|
* @param string $options |
462
|
|
|
* plugin location |
463
|
|
|
* @return string |
464
|
|
|
*/ |
465
|
|
|
public function tablesorter($table = '', $options = '') { |
466
|
|
|
$this->jquery_code_for_compile[] = "\t$(" . Javascript::prep_element($table) . ").tablesorter($options);\n"; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Allows to attach a condition |
471
|
|
|
* |
472
|
|
|
* @param string $condition |
473
|
|
|
* @param string $jsCodeIfTrue |
474
|
|
|
* @param string $jsCodeIfFalse |
475
|
|
|
* @param boolean $immediatly |
476
|
|
|
* defers the execution if set to false |
477
|
|
|
*/ |
478
|
|
|
public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse = null, $immediatly = false) { |
479
|
|
|
$str = "if(" . $condition . "){" . $jsCodeIfTrue . "}"; |
480
|
|
|
if (isset($jsCodeIfFalse)) { |
481
|
|
|
$str .= "else{" . $jsCodeIfFalse . "}"; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
if ($immediatly) |
485
|
|
|
$this->jquery_code_for_compile[] = $str; |
486
|
|
|
return $str; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Call the JQuery method $jqueryCall on $element with parameters $param |
491
|
|
|
* |
492
|
|
|
* @param string $element |
493
|
|
|
* @param string $jqueryCall |
494
|
|
|
* @param mixed $param |
495
|
|
|
* @param string $jsCallback |
496
|
|
|
* javascript code to execute after the jquery call |
497
|
|
|
* @param boolean $immediatly |
498
|
|
|
* @return string |
499
|
|
|
*/ |
500
|
|
|
private function _doJQuery($element, $jqueryCall, $param = "", $jsCallback = "", $immediatly = false) { |
501
|
|
|
$param = Javascript::prep_value($param); |
502
|
|
|
$callback = ""; |
503
|
|
|
if ($jsCallback != "") |
504
|
|
|
$callback = ", function(event){\n{$jsCallback}\n}"; |
505
|
|
|
$script = "$(" . Javascript::prep_element($element) . ")." . $jqueryCall . "(" . $param . $callback . ");\n"; |
506
|
|
|
if ($immediatly) |
507
|
|
|
$this->jquery_code_for_compile[] = $script; |
508
|
|
|
return $script; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Calls the JQuery callback $someThing on $element with facultative parameter $param |
513
|
|
|
* |
514
|
|
|
* @param string $element |
515
|
|
|
* the element |
516
|
|
|
* @param string $jqueryCall |
517
|
|
|
* the JQuery callback |
518
|
|
|
* @param mixed $param |
519
|
|
|
* array or string parameters |
520
|
|
|
* @param string $jsCallback |
521
|
|
|
* javascript code to execute after the jquery call |
522
|
|
|
* @return mixed |
523
|
|
|
*/ |
524
|
|
|
public function doJQuery($element, $jqueryCall, $param = "", $jsCallback = "") { |
525
|
|
|
return $this->_doJQuery($element, $jqueryCall, $param, $jsCallback, true); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Calls the JQuery callback $someThing on $element with facultative parameter $param |
530
|
|
|
* |
531
|
|
|
* @param string $element |
532
|
|
|
* the element |
533
|
|
|
* @param string $jqueryCall |
534
|
|
|
* the JQuery callback |
535
|
|
|
* @param mixed $param |
536
|
|
|
* array or string parameters |
537
|
|
|
* @param string $jsCallback |
538
|
|
|
* javascript code to execute after the jquery call |
539
|
|
|
* @return mixed |
540
|
|
|
*/ |
541
|
|
|
public function doJQueryDeferred($element, $jqueryCall, $param = "", $jsCallback = "") { |
542
|
|
|
return $this->_doJQuery($element, $jqueryCall, $param, $jsCallback, false); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* |
547
|
|
|
* @param string $event |
548
|
|
|
* @param string $element |
549
|
|
|
* @param string $elementToModify |
550
|
|
|
* @param string $jqueryCall |
551
|
|
|
* @param string|array $param |
552
|
|
|
* @param boolean $preventDefault |
553
|
|
|
* @param boolean $stopPropagation |
554
|
|
|
* @param string $jsCallback |
555
|
|
|
* javascript code to execute after the jquery call |
556
|
|
|
* @param boolean $immediatly |
557
|
|
|
* @return string |
558
|
|
|
*/ |
559
|
|
|
private function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param = "", $preventDefault = false, $stopPropagation = false, $jsCallback = "", $immediatly = true) { |
560
|
|
|
return $this->_add_event($element, $this->_doJQuery($elementToModify, $jqueryCall, $param, $jsCallback), $event, $preventDefault, $stopPropagation, $immediatly); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Calls the JQuery callback $jqueryCall on $element with facultative parameter $param in response to an event $event |
565
|
|
|
* |
566
|
|
|
* @param string $event |
567
|
|
|
* @param string $element |
568
|
|
|
* @param string $elementToModify |
569
|
|
|
* @param string $jqueryCall |
570
|
|
|
* @param string $param |
571
|
|
|
* @param array $parameters |
572
|
|
|
* default : array("preventDefault"=>false,"stopPropagation"=>false,"jsCallback"=>'',"immediatly"=>true) |
573
|
|
|
*/ |
574
|
|
|
public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param = "", $parameters = array()) { |
575
|
|
|
$jsCallback = ""; |
576
|
|
|
$stopPropagation = false; |
577
|
|
|
$preventDefault = false; |
578
|
|
|
$immediatly = true; |
579
|
|
|
extract($parameters); |
580
|
|
|
return $this->_doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param, $preventDefault, $stopPropagation, $jsCallback, $immediatly); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* Executes the code $js |
585
|
|
|
* |
586
|
|
|
* @param string $js |
587
|
|
|
* Code to execute |
588
|
|
|
* @param boolean $immediatly |
589
|
|
|
* delayed if false |
590
|
|
|
* @return String |
591
|
|
|
*/ |
592
|
|
|
public function exec($js, $immediatly = false) { |
593
|
|
|
$script = $js . "\n"; |
594
|
|
|
if ($immediatly) |
595
|
|
|
$this->jquery_code_for_compile[] = $script; |
596
|
|
|
return $script; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* Executes the code $js |
601
|
|
|
* |
602
|
|
|
* @param string $js |
603
|
|
|
* Code to execute |
604
|
|
|
* @param boolean $immediatly |
605
|
|
|
* delayed if false |
606
|
|
|
* @return String |
607
|
|
|
*/ |
608
|
|
|
public function execAtLast($js) { |
609
|
|
|
$script = $js . "\n"; |
610
|
|
|
$this->jquery_code_for_compile_at_last[] = $script; |
611
|
|
|
return $script; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Executes the javascript code $js when $event fires on $element |
616
|
|
|
* |
617
|
|
|
* @param string $event |
618
|
|
|
* @param string $element |
619
|
|
|
* @param string $js |
620
|
|
|
* Code to execute |
621
|
|
|
* @param array $parameters |
622
|
|
|
* default : array("preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true) |
623
|
|
|
* @return String |
624
|
|
|
*/ |
625
|
|
|
public function execOn($event, $element, $js, $parameters = array()) { |
626
|
|
|
$stopPropagation = false; |
627
|
|
|
$preventDefault = false; |
628
|
|
|
$immediatly = true; |
629
|
|
|
extract($parameters); |
630
|
|
|
$script = $this->_add_event($element, $this->exec($js), $event, $preventDefault, $stopPropagation, $immediatly); |
631
|
|
|
return $script; |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
public function setJsonToElement($json, $elementClass = "_element", $immediatly = true) { |
|
|
|
|
635
|
|
|
$retour = "var data={$json};" . "\n\tdata=($.isPlainObject(data))?data:JSON.parse(data);" . "\n\tvar pk=data['pk'];var object=data['object'];" . "\n\tfor(var field in object){" . "\n\tif($('[data-field='+field+']',$('._element[data-ajax='+pk+']')).length){" . "\n\t\t$('[data-field='+field+']',$('._element[data-ajax='+pk+']')).each(function(){" . "\n\t\t\tif($(this).is('[value]')) { $(this).val(object[field]);} else { $(this).html(object[field]); }" . "\n\t});" . "\n}};\n"; |
636
|
|
|
$retour .= "\t$(document).trigger('jsonReady',[data]);\n"; |
637
|
|
|
return $this->exec($retour, $immediatly); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Sets an element draggable (HTML5 drag and drop) |
642
|
|
|
* |
643
|
|
|
* @param string $element |
644
|
|
|
* The element selector |
645
|
|
|
* @param array $parameters |
646
|
|
|
* default : array("attr"=>"id","preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true) |
647
|
|
|
*/ |
648
|
|
|
public function setDraggable($element, $parameters = []) { |
649
|
|
|
$attr = "id"; |
650
|
|
|
extract($parameters); |
651
|
|
|
$script = $this->_add_event($element, Javascript::draggable($attr), "dragstart", $parameters); |
652
|
|
|
return $script; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Declares an element as a drop zone (HTML5 drag and drop) |
657
|
|
|
* |
658
|
|
|
* @param string $element |
659
|
|
|
* The element selector |
660
|
|
|
* @param array $parameters |
661
|
|
|
* default : array("attr"=>"id","stopPropagation"=>false,"immediatly"=>true,"jqueryDone"=>"append") |
662
|
|
|
* @param string $jsCallback |
663
|
|
|
* the js script to call when element is dropped |
664
|
|
|
*/ |
665
|
|
|
public function asDropZone($element, $jsCallback = "", $parameters = []) { |
666
|
|
|
$stopPropagation = false; |
667
|
|
|
$immediatly = true; |
668
|
|
|
$jqueryDone = "append"; |
669
|
|
|
$script = $this->_add_event($element, '', "dragover", true, $stopPropagation, $immediatly); |
670
|
|
|
extract($parameters); |
671
|
|
|
$script .= $this->_add_event($element, Javascript::dropZone($jqueryDone, $jsCallback), "drop", true, $stopPropagation, $immediatly); |
672
|
|
|
return $script; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Calls a function or evaluates an expression at specified intervals (in milliseconds) |
677
|
|
|
* |
678
|
|
|
* @param string $jsCode |
679
|
|
|
* The code to execute |
680
|
|
|
* @param int $time |
681
|
|
|
* The time interval in milliseconds |
682
|
|
|
* @param string $globalName |
683
|
|
|
* The global name of the interval, used to clear it |
684
|
|
|
* @param boolean $immediatly |
685
|
|
|
* delayed if false |
686
|
|
|
* @return string |
687
|
|
|
*/ |
688
|
|
|
public function interval($jsCode, $time, $globalName = null, $immediatly = true) { |
689
|
|
|
if (! Javascript::isFunction($jsCode)) { |
690
|
|
|
$jsCode = "function(){\n" . $jsCode . "\n}"; |
691
|
|
|
} |
692
|
|
|
if (isset($globalName)) { |
693
|
|
|
$script = "if(window.{$globalName}){clearInterval(window.{$globalName});}\nwindow.{$globalName}=setInterval({$jsCode},{$time});"; |
694
|
|
|
} else { |
695
|
|
|
$script = "setInterval({$jsCode},{$time});"; |
696
|
|
|
} |
697
|
|
|
return $this->exec($script, $immediatly); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Clears an existing interval |
702
|
|
|
* |
703
|
|
|
* @param string $globalName |
704
|
|
|
* The interval global name |
705
|
|
|
* @param boolean $immediatly |
706
|
|
|
* delayed if false |
707
|
|
|
* @return string |
708
|
|
|
*/ |
709
|
|
|
public function clearInterval($globalName, $immediatly = true) { |
710
|
|
|
return $this->exec("if(window.{$globalName}){clearInterval(window.{$globalName});}", $immediatly); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Associates a counter to the element designated by $counterSelector |
715
|
|
|
* Triggers the events counter-start and counter-end on finished with the parameters value and limit |
716
|
|
|
* |
717
|
|
|
* @param string $counterSelector |
718
|
|
|
* Selector of the existing element wich display the counter |
719
|
|
|
* @param integer $value |
720
|
|
|
* The initial value of the counter |
721
|
|
|
* @param integer $limit |
722
|
|
|
* The limit of the counter (minimum if countDown is true, maximum if not) |
723
|
|
|
* @param string $globalName |
724
|
|
|
* The global name of the counter, to use with the clearInterval method |
725
|
|
|
* @param boolean $countDown |
726
|
|
|
* count down if true or elapse if false |
727
|
|
|
* @param boolean $immediatly |
728
|
|
|
* delayed if false |
729
|
|
|
* @return string |
730
|
|
|
*/ |
731
|
|
|
public function counter($counterSelector, $value = 0, $limit = 0, $globalName = null, $countDown = true, $immediatly = true) { |
732
|
|
|
$stop = ""; |
733
|
|
|
if ($countDown) { |
734
|
|
|
$stop = "if (--timer < " . $limit . ") {clearInterval(interval);display.trigger({type:'counter-end',value: timer,limit:" . $limit . "});}"; |
735
|
|
|
} else { |
736
|
|
|
if ($limit != 0) { |
737
|
|
|
$stop = "if (++timer > " . $limit . ") {clearInterval(interval);display.trigger({type:'counter-end',value: timer,limit:" . $limit . "});}"; |
738
|
|
|
} |
739
|
|
|
} |
740
|
|
|
$global = ""; |
741
|
|
|
if (isset($globalName)) { |
742
|
|
|
$global = "\nwindow.{$globalName}=interval;"; |
743
|
|
|
} |
744
|
|
|
$timer = "var startTimer=function(duration, display) {var timer = duration, minutes, seconds; |
745
|
|
|
display.trigger('counter-start',timer); |
746
|
|
|
display.show(); |
747
|
|
|
var interval=setInterval(function () { |
748
|
|
|
minutes = parseInt(timer / 60, 10);seconds = parseInt(timer % 60, 10); |
749
|
|
|
minutes = minutes < 10 ? '0' + minutes : minutes; |
750
|
|
|
seconds = seconds < 10 ? '0' + seconds : seconds; |
751
|
|
|
if(display.is('[value]')){display.val(minutes + ':' + seconds);} else {display.html(minutes + ':' + seconds);}; |
752
|
|
|
" . $stop . " |
753
|
|
|
}, 1000); |
754
|
|
|
" . $global . " |
755
|
|
|
}"; |
756
|
|
|
$element = '$("' . $counterSelector . '")'; |
757
|
|
|
return $this->exec($timer . "\nstartTimer(" . $value . "," . $element . ");", $immediatly); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* Associates a counter to the element designated by $counterSelector when $event is triggered on $element |
762
|
|
|
* |
763
|
|
|
* @param string $element |
764
|
|
|
* The triggering element |
765
|
|
|
* @param string $event |
766
|
|
|
* The triggering event |
767
|
|
|
* @param string $counterSelector |
768
|
|
|
* Selector of the existing element wich display the counter |
769
|
|
|
* @param integer $value |
770
|
|
|
* The initial value of the counter |
771
|
|
|
* @param integer $limit |
772
|
|
|
* The limit of the counter (minimum if countDown is true, maximum if not) |
773
|
|
|
* @param string $globalName |
774
|
|
|
* The global name of the counter, to use with the clearInterval method |
775
|
|
|
* @param boolean $countDown |
776
|
|
|
* count down if true or elapse if false |
777
|
|
|
* @return string |
778
|
|
|
*/ |
779
|
|
|
public function counterOn($element, $event, $counterSelector, $value = 0, $limit = 0, $globalName = null, $countDown = true) { |
780
|
|
|
return $this->execOn($event, $element, $this->counter($counterSelector, $value, $limit, $globalName, $countDown, false)); |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
/** |
784
|
|
|
* Activates an element if it is active (add the class active) |
785
|
|
|
* |
786
|
|
|
* @param string $target |
787
|
|
|
* the container element |
788
|
|
|
* @param string $property |
789
|
|
|
* default: href |
790
|
|
|
* @param string $href |
791
|
|
|
* the active href (if null, window.location.href is used) |
792
|
|
|
* @return string |
793
|
|
|
*/ |
794
|
|
|
public function activateLink($target, $property = 'href', $href = null) { |
795
|
|
|
$js = '$(' . $target . ' [' . $property . ']).removeClass("active");'; |
796
|
|
|
if (isset($href)) { |
797
|
|
|
$js .= 'var href="' . $href . '";'; |
798
|
|
|
} else { |
799
|
|
|
$js .= 'var href=window.location.href;'; |
800
|
|
|
} |
801
|
|
|
$js .= '$(' . $target . ' [' . $property . ']).each(function(){if(href.includes($(this).attr("' . $property . '"))) $(this).addClass("active");});'; |
802
|
|
|
return $js; |
803
|
|
|
} |
804
|
|
|
} |
805
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.