Skip to content

Commit f4e97dc

Browse files
committed
Jquery class refactoring
1 parent d27f990 commit f4e97dc

File tree

20 files changed

+283
-269
lines changed

20 files changed

+283
-269
lines changed

Ajax/Jquery.php

Lines changed: 23 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,21 @@
55
use Ajax\common\traits\JqueryEventsTrait;
66
use Ajax\common\traits\JqueryAjaxTrait;
77
use Ajax\common\traits\JqueryActionsTrait;
8+
use Ajax\service\Javascript;
89

910
/**
10-
* JQuery Phalcon library
11+
* jQuery Class
1112
*
1213
* @author jcheron
1314
* @version 1.002
1415
* @license Apache 2 http://www.apache.org/licenses/
15-
*/
16-
/**
17-
* jQuery Class
18-
*/
16+
**/
1917
class Jquery {
2018
use JqueryEventsTrait,JqueryAjaxTrait,JqueryActionsTrait;
2119
protected $_ui;
2220
protected $_bootstrap;
2321
protected $_semantic;
24-
protected $libraryFile;
25-
protected $_javascript_folder='js';
26-
protected $jquery_code_for_load=array ();
2722
protected $jquery_code_for_compile=array ();
28-
protected $jquery_corner_active=FALSE;
29-
protected $jquery_table_sorter_active=FALSE;
30-
protected $jquery_table_sorter_pager_active=FALSE;
3123
protected $jsUtils;
3224
protected $params;
3325

@@ -109,10 +101,6 @@ private function _close_script($extra="\n") {
109101
return "</script>{$extra}";
110102
}
111103

112-
public function setLibraryFile($name) {
113-
$this->libraryFile=$name;
114-
}
115-
116104
public function _setAjaxLoader($loader) {
117105
$this->ajaxLoader=$loader;
118106
}
@@ -144,9 +132,9 @@ public function _output($array_js='') {
144132
* @param boolean $immediatly delayed if false
145133
*/
146134
public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) {
147-
$element=$this->_prep_element($element);
135+
$element=Javascript::prep_element($element);
148136
if (isset($param)) {
149-
$param=$this->_prep_value($param);
137+
$param=Javascript::prep_value($param);
150138
$str="$({$element}).{$jQueryCall}({$param});";
151139
} else
152140
$str="$({$element}).{$jQueryCall}();";
@@ -163,8 +151,8 @@ public function _genericCallValue($jQueryCall,$element='this', $param="", $immed
163151
* @return string
164152
*/
165153
public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) {
166-
$to=$this->_prep_element($to);
167-
$element=$this->_prep_element($element);
154+
$to=Javascript::prep_element($to);
155+
$element=Javascript::prep_element($element);
168156
$str="$({$to}).{$jQueryCall}({$element});";
169157
if ($immediatly)
170158
$this->jquery_code_for_compile[]=$str;
@@ -189,7 +177,7 @@ public function sortable($element, $options=array()) {
189177
$sort_options='';
190178
}
191179

192-
return "$(".$this->_prep_element($element).").sortable({".$sort_options."\n\t});";
180+
return "$(".Javascript::prep_element($element).").sortable({".$sort_options."\n\t});";
193181
}
194182

195183
/**
@@ -200,7 +188,7 @@ public function sortable($element, $options=array()) {
200188
* @return string
201189
*/
202190
public function tablesorter($table='', $options='') {
203-
$this->jquery_code_for_compile[]="\t$(".$this->_prep_element($table).").tablesorter($options);\n";
191+
$this->jquery_code_for_compile[]="\t$(".Javascript::prep_element($table).").tablesorter($options);\n";
204192
}
205193

206194
/**
@@ -224,9 +212,9 @@ public function _add_event($element, $js, $event, $preventDefault=false, $stopPr
224212
$js="event.stopPropagation();\n".$js;
225213
}
226214
if (array_search($event, $this->jquery_events)===false)
227-
$event="\n\t$(".$this->_prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
215+
$event="\n\t$(".Javascript::prep_element($element).").bind('{$event}',function(event){\n\t\t{$js}\n\t});\n";
228216
else
229-
$event="\n\t$(".$this->_prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n";
217+
$event="\n\t$(".Javascript::prep_element($element).").{$event}(function(event){\n\t\t{$js}\n\t});\n";
230218
if($immediatly)
231219
$this->jquery_code_for_compile[]=$event;
232220
return $event;
@@ -242,36 +230,13 @@ public function _add_event($element, $js, $event, $preventDefault=false, $stopPr
242230
*/
243231
public function _compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE) {
244232
// Components UI
245-
$ui=$this->ui();
246-
if ($this->ui()!=NULL) {
247-
if ($ui->isAutoCompile()) {
248-
$ui->compile(true);
249-
}
250-
}
251-
233+
$this->_compileLibrary($this->ui());
252234
// Components BS
253-
$bootstrap=$this->bootstrap();
254-
if ($this->bootstrap()!=NULL) {
255-
if ($bootstrap->isAutoCompile()) {
256-
$bootstrap->compile(true);
257-
}
258-
}
259-
235+
$this->_compileLibrary($this->bootstrap());
260236
// Components Semantic
261-
$semantic=$this->semantic();
262-
if ($semantic!=NULL) {
263-
if ($semantic->isAutoCompile()) {
264-
$semantic->compile(true);
265-
}
266-
}
237+
$this->_compileLibrary($this->semantic());
267238

268-
// External references
269-
$external_scripts=implode('', $this->jquery_code_for_load);
270-
extract(array (
271-
'library_src' => $external_scripts
272-
));
273-
274-
if (count($this->jquery_code_for_compile)==0) {
239+
if (\sizeof($this->jquery_code_for_compile)==0) {
275240
// no inline references, let's just return
276241
return;
277242
}
@@ -293,6 +258,14 @@ public function _compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE
293258
return $output;
294259
}
295260

261+
private function _compileLibrary($library){
262+
if ($library!=NULL) {
263+
if ($library->isAutoCompile()) {
264+
$library->compile(true);
265+
}
266+
}
267+
}
268+
296269
public function _addToCompile($jsScript) {
297270
$this->jquery_code_for_compile[]=$jsScript;
298271
}
@@ -322,39 +295,6 @@ public function _document_ready($js) {
322295
}
323296
}
324297

325-
/**
326-
* Puts HTML element in quotes for use in jQuery code
327-
* unless the supplied element is the Javascript 'this'
328-
* object, in which case no quotes are added
329-
*
330-
* @param string $element
331-
* @return string
332-
*/
333-
public function _prep_element($element) {
334-
if (strrpos($element, 'this')===false&&strrpos($element, 'event')===false&&strrpos($element, 'self')===false) {
335-
$element='"'.addslashes($element).'"';
336-
}
337-
return $element;
338-
}
339-
340-
/**
341-
* Puts HTML values in quotes for use in jQuery code
342-
* unless the supplied value contains the Javascript 'this' or 'event'
343-
* object, in which case no quotes are added
344-
*
345-
* @param string $value
346-
* @return string
347-
*/
348-
public function _prep_value($value) {
349-
if (is_array($value)) {
350-
$value=implode(",", $value);
351-
}
352-
if (strrpos($value, 'this')===false&&strrpos($value, 'event')===false&&strrpos($value, 'self')===false) {
353-
$value='"'.$value.'"';
354-
}
355-
return $value;
356-
}
357-
358298
private function minify($input) {
359299
if(trim($input) === "") return $input;
360300
return preg_replace(

Ajax/common/Widget.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Ajax\semantic\widgets\base\FieldAsTrait;
1010
use Ajax\semantic\html\elements\HtmlButtonGroups;
1111
use Ajax\semantic\widgets\base\InstanceViewer;
12+
use Ajax\semantic\html\modules\HtmlDropdown;
13+
use Ajax\service\JArray;
1214

1315
abstract class Widget extends HtmlDoubleElement {
1416
use FieldAsTrait;
@@ -139,10 +141,32 @@ public function addInToolbar($element,$callback=NULL){
139141

140142
public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
141143
$result=$this->addInToolbar($caption,$callback);
142-
$result->addIcon($icon);
144+
if(isset($icon))
145+
$result->addIcon($icon);
143146
return $result;
144147
}
145148

149+
public function addItemsInToolbar(array $items,$callback=NULL){
150+
if(JArray::isAssociative($items)){
151+
foreach ($items as $icon=>$item){
152+
$this->addItemInToolbar($item,$icon,$callback);
153+
}
154+
}else{
155+
foreach ($items as $item){
156+
$this->addItemInToolbar($item,null,$callback);
157+
}
158+
}
159+
return $this;
160+
}
161+
162+
public function addDropdownInToolbar($value,$items=NULL,$callback=NULL){
163+
$dd=$value;
164+
if (\is_string($value)) {
165+
$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
166+
}
167+
return $this->addInToolbar($dd,$callback);
168+
}
169+
146170
public function addButtonInToolbar($caption,$callback=NULL){
147171
$bt=new HtmlButton("",$caption);
148172
return $this->addInToolbar($bt,$callback);

0 commit comments

Comments
 (0)