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