1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\common\traits; |
4
|
|
|
|
5
|
|
|
use Ajax\service\Javascript; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author jc |
9
|
|
|
*/ |
10
|
|
|
trait JsUtilsEventsTrait { |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
protected $jquery_events=array ( |
14
|
|
|
"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" |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructs the syntax for an event, and adds to into the array for compilation |
19
|
|
|
* |
20
|
|
|
* @param string $element The element to attach the event to |
21
|
|
|
* @param string $js The code to execute |
22
|
|
|
* @param string $event The event to pass |
23
|
|
|
* @param boolean $preventDefault If set to true, the default action of the event will not be triggered. |
24
|
|
|
* @param boolean $stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true) { |
28
|
|
|
if (\is_array($js)) { |
29
|
|
|
$js=implode("\n\t\t", $js); |
30
|
|
|
} |
31
|
|
|
if ($preventDefault===true) { |
32
|
|
|
$js=Javascript::$preventDefault.$js; |
33
|
|
|
} |
34
|
|
|
if ($stopPropagation===true) { |
35
|
|
|
$js=Javascript::$stopPropagation.$js; |
36
|
|
|
} |
37
|
|
|
if (array_search($event, $this->jquery_events)===false) |
38
|
|
|
$event="\n\t$(".Javascript::prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n"; |
39
|
|
|
else |
40
|
|
|
$event="\n\t$(".Javascript::prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n"; |
41
|
|
|
if($immediatly) |
42
|
|
|
$this->jquery_code_for_compile[]=$event; |
|
|
|
|
43
|
|
|
return $event; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Outputs a javascript library blur event |
48
|
|
|
* |
49
|
|
|
* @param string $element element to attach the event to |
50
|
|
|
* @param string $js code to execute |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function blur($element='this', $js='') { |
54
|
|
|
return $this->_add_event($element, $js, 'blur'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Outputs a javascript library change event |
59
|
|
|
* |
60
|
|
|
* @param string $element element to attach the event to |
61
|
|
|
* @param string $js code to execute |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function change($element='this', $js='') { |
65
|
|
|
return $this->_add_event($element, $js, 'change'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Outputs a javascript library click event |
70
|
|
|
* |
71
|
|
|
* @param string $element element to attach the event to |
72
|
|
|
* @param string $js code to execute |
73
|
|
|
* @param boolean $ret_false or not to return false |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function click($element='this', $js='', $ret_false=TRUE) { |
77
|
|
|
if (!is_array($js)) { |
78
|
|
|
$js=array ( |
79
|
|
|
$js |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($ret_false) { |
84
|
|
|
$js[]="return false;"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->_add_event($element, $js, 'click'); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Outputs a javascript library contextmenu event |
92
|
|
|
* |
93
|
|
|
* @param string $element element to attach the event to |
94
|
|
|
* @param string $js code to execute |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function contextmenu($element='this', $js='') { |
98
|
|
|
return $this->_add_event($element, $js, 'contextmenu'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Outputs a javascript library dblclick event |
104
|
|
|
* |
105
|
|
|
* @param string $element element to attach the event to |
106
|
|
|
* @param string $js code to execute |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function dblclick($element='this', $js='') { |
110
|
|
|
return $this->_add_event($element, $js, 'dblclick'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Outputs a javascript library error event |
115
|
|
|
* |
116
|
|
|
* @param string $element element to attach the event to |
117
|
|
|
* @param string $js code to execute |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function error($element='this', $js='') { |
121
|
|
|
return $this->_add_event($element, $js, 'error'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Outputs a javascript library focus event |
126
|
|
|
* |
127
|
|
|
* @param string $element element to attach the event to |
128
|
|
|
* @param string $js code to execute |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function focus($element='this', $js='') { |
132
|
|
|
return $this->_add_event($element, $js, 'focus'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Outputs a javascript library hover event |
137
|
|
|
* |
138
|
|
|
* @param string $element |
139
|
|
|
* @param string $over code for mouse over |
140
|
|
|
* @param string $out code for mouse out |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function hover($element='this', $over, $out) { |
144
|
|
|
$event="\n\t$(".Javascript::prep_element($element).").hover(\n\t\tfunction()\n\t\t{\n\t\t\t{$over}\n\t\t}, \n\t\tfunction()\n\t\t{\n\t\t\t{$out}\n\t\t});\n"; |
145
|
|
|
$this->jquery_code_for_compile[]=$event; |
146
|
|
|
return $event; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Outputs a javascript library keydown event |
151
|
|
|
* |
152
|
|
|
* @param string $element element to attach the event to |
153
|
|
|
* @param string $js code to execute |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function keydown($element='this', $js='') { |
157
|
|
|
return $this->_add_event($element, $js, 'keydown'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Outputs a javascript library keypress event |
162
|
|
|
* |
163
|
|
|
* @param string $element element to attach the event to |
164
|
|
|
* @param string $js code to execute |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function keypress($element='this', $js='') { |
168
|
|
|
return $this->_add_event($element, $js, 'keypress'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Outputs a javascript library keydown event |
173
|
|
|
* |
174
|
|
|
* @param string $element element to attach the event to |
175
|
|
|
* @param string $js code to execute |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function keyup($element='this', $js='') { |
179
|
|
|
return $this->_add_event($element, $js, 'keyup'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Outputs a javascript library load event |
184
|
|
|
* |
185
|
|
|
* @param string $element element to attach the event to |
186
|
|
|
* @param string $js code to execute |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function load($element='this', $js='') { |
190
|
|
|
return $this->_add_event($element, $js, 'load'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Outputs a javascript library mousedown event |
195
|
|
|
* |
196
|
|
|
* @param string $element element to attach the event to |
197
|
|
|
* @param string $js code to execute |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function mousedown($element='this', $js='') { |
201
|
|
|
return $this->_add_event($element, $js, 'mousedown'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Outputs a javascript library mouseout event |
206
|
|
|
* |
207
|
|
|
* @param string $element element to attach the event to |
208
|
|
|
* @param string $js code to execute |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function mouseout($element='this', $js='') { |
212
|
|
|
return $this->_add_event($element, $js, 'mouseout'); |
213
|
|
|
} |
214
|
|
|
/** |
215
|
|
|
* Outputs a javascript library mouseover event |
216
|
|
|
* |
217
|
|
|
* @param string $element element to attach the event to |
218
|
|
|
* @param string $js code to execute |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function mouseover($element='this', $js='') { |
222
|
|
|
return $this->_add_event($element, $js, 'mouseover'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Outputs a javascript library mouseup event |
227
|
|
|
* |
228
|
|
|
* @param string $element element to attach the event to |
229
|
|
|
* @param string $js code to execute |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function mouseup($element='this', $js='') { |
233
|
|
|
return $this->_add_event($element, $js, 'mouseup'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Outputs a javascript library unload event |
239
|
|
|
* |
240
|
|
|
* @param string $element element to attach the event to |
241
|
|
|
* @param string $js code to execute |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function unload($element='this', $js='') { |
245
|
|
|
return $this->_add_event($element, $js, 'unload'); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// -------------------------------------------------------------------- |
249
|
|
|
/** |
250
|
|
|
* Outputs a javascript library resize event |
251
|
|
|
* |
252
|
|
|
* @param string $element element to attach the event to |
253
|
|
|
* @param string $js code to execute |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
|
public function resize($element='this', $js='') { |
257
|
|
|
return $this->_add_event($element, $js, 'resize'); |
258
|
|
|
} |
259
|
|
|
// -------------------------------------------------------------------- |
260
|
|
|
/** |
261
|
|
|
* Outputs a javascript library scroll event |
262
|
|
|
* |
263
|
|
|
* @param string $element element to attach the event to |
264
|
|
|
* @param string $js code to execute |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
public function scroll($element='this', $js='') { |
268
|
|
|
return $this->_add_event($element, $js, 'scroll'); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
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: