Skip to content

Commit e24b4ab

Browse files
committed
Add Minify class
Fix js events duplicates Add internal hasLoader Add optional for field validation Add toggle in DataElement
1 parent 28f925e commit e24b4ab

File tree

14 files changed

+63
-37
lines changed

14 files changed

+63
-37
lines changed

Ajax/common/Widget.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ public function setDefaultValueFunction($defaultValueFunction){
358358
$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
359359
return $this;
360360
}
361+
362+
/**
363+
* @return callable
364+
*/
365+
public function getDefaultValueFunction(){
366+
return $this->_instanceViewer->getDefaultValueFunction();
367+
}
361368

362369
/**
363370
* @param string|boolean $disable

Ajax/common/html/traits/BaseHtmlEventsTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefaul
4040
public function _addEvent($event, $jsCode) {
4141
if (array_key_exists($event, $this->_events)) {
4242
if (\is_array($this->_events[$event])) {
43-
$this->_events[$event][]=$jsCode;
43+
if(array_search($jsCode, $this->_events[$event])===false){
44+
$this->_events[$event][]=$jsCode;
45+
}
4446
} else {
4547
$this->_events[$event]=array ($this->_events[$event],$jsCode );
4648
}

Ajax/common/traits/JsUtilsAjaxTrait.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ protected function _ajax($method,$url,$responseElement="",$parameters=[]) {
3838
$retour.="var self=this;\n";
3939
if($hasLoader===true && JString::isNotNull($responseElement)){
4040
$this->addLoading($retour, $responseElement,$ajaxLoader);
41+
}elseif($hasLoader==="internal"){
42+
$retour.="\n$(this).addClass('loading');";
4143
}
4244
$ajaxParameters=["url"=>"url","method"=>"'".\strtoupper($method)."'"];
4345

@@ -51,7 +53,7 @@ protected function _ajax($method,$url,$responseElement="",$parameters=[]) {
5153
}
5254
$this->createAjaxParameters($ajaxParameters, $parameters);
5355
$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data, textStatus, jqXHR ) {\n";
54-
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback)."});\n";
56+
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback,$hasLoader)."});\n";
5557
$retour=$this->_addJsCondition($jsCondition,$retour);
5658
if ($immediatly)
5759
$this->jquery_code_for_compile[]=$retour;
@@ -100,7 +102,7 @@ protected function _getAjaxUrl($url,$attr){
100102
return $retour;
101103
}
102104

103-
protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback){
105+
protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback,$hasLoader=false){
104106
$retour="";$call=null;
105107
if (JString::isNotNull($responseElement)) {
106108
if(isset($ajaxTransition)){
@@ -113,6 +115,9 @@ protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$
113115
else
114116
$retour="\t{$responseElement}.{$jqueryDone}( data );\n";
115117
}
118+
if($hasLoader==="internal"){
119+
$retour.="\n$(self).removeClass('loading');";
120+
}
116121
$retour.="\t".$jsCallback."\n";
117122
return $retour;
118123
}
@@ -480,14 +485,16 @@ private function _postForm($url, $form, $responseElement, $parameters=[]) {
480485
$retour.="var self=this;\n";
481486
if($hasLoader===true){
482487
$this->addLoading($retour, $responseElement,$ajaxLoader);
488+
}elseif($hasLoader==="internal"){
489+
$retour.="\n$(this).addClass('loading');";
483490
}
484491
$ajaxParameters=["url"=>"url","method"=>"'POST'","data"=>"params","async"=>$async];
485492
if(isset($headers)){
486493
$ajaxParameters["headers"]=$headers;
487494
}
488495
$this->createAjaxParameters($ajaxParameters, $parameters);
489496
$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data ) {\n";
490-
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback)."});\n";
497+
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback,$hasLoader)."});\n";
491498

492499
if ($validation) {
493500
$retour="$('#".$form."').validate({submitHandler: function(form) {

Ajax/common/traits/JsUtilsInternalTrait.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Ajax\common\traits;
33
use Ajax\common\BaseGui;
4+
use Ubiquity\js\Minify;
45

56
trait JsUtilsInternalTrait{
67

@@ -37,27 +38,7 @@ protected function ready($script){
3738

3839
protected function minify($input) {
3940
if(trim($input) === "") return $input;
40-
return preg_replace(
41-
array(
42-
// Remove comment(s)
43-
'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#',
44-
// Remove white-space(s) outside the string and regex
45-
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s',
46-
// Remove the last semicolon
47-
'#;+\}#',
48-
// Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}`
49-
'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
50-
// --ibid. From `foo['bar']` to `foo.bar`
51-
'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
52-
),
53-
array(
54-
'$1',
55-
'$1$2',
56-
'}',
57-
'$1$3',
58-
'$1.$3'
59-
),
60-
$input);
41+
return Minify::minifyJavascript($input);
6142
}
6243

6344
/**

Ajax/semantic/components/jsTemplates/tplCheckedList.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
.checkbox({
1010
fireOnInit : %fireOnInit%,
1111
onChange : function() {
12-
var
13-
$listGroup = $(this).closest(".list"),
12+
var $listGroup = $(this).closest(".list"),
1413
$parentCheckbox = $listGroup.closest(".item").children(".checkbox"),
1514
$checkbox = $listGroup.find(".checkbox"),
1615
allChecked = true,
17-
allUnchecked = true
18-
;
16+
allUnchecked = true;
1917
$checkbox.each(function() {
2018
if( $(this).checkbox("is checked") ) {
2119
allUnchecked = false;

Ajax/semantic/components/validation/FieldValidation.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ public function addRule($type,$prompt=NULL,$value=NULL){
6363
}
6464

6565
public function jsonSerialize(){
66-
return ["identifier"=>$this->identifier,"rules"=>$this->rules];
66+
$result=["identifier"=>$this->identifier,"rules"=>$this->rules];
67+
if($this->optional){
68+
$result["optional"]=true;
69+
}
70+
if(isset($this->depends)){
71+
$result["depends"]=$this->depends;
72+
}
73+
return $result;
6774
}
6875

6976
public function setDepends($depends) {

Ajax/semantic/components/validation/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static function custom($name,$jsFunction){
169169
public static function ajax(JsUtils $js,$name,$url,$params,$jsCallback,$method="post",$parameters=[]){
170170
$parameters=\array_merge(["async"=>false,"url"=>$url,"params"=>$params,"hasLoader"=>false,"jsCallback"=>$jsCallback,"dataType"=>"json","stopPropagation"=>false,"preventDefault"=>false,"responseElement"=>null],$parameters);
171171
$ajax=new AjaxCall($method, $parameters);
172-
return self::custom($name, "function(value){var result=true;".$ajax->compile($js)."return result;}");
172+
return self::custom($name, "function(value,ruleValue){var result=true;".$ajax->compile($js)."return result;}");
173173
}
174174

175175
}

Ajax/semantic/html/base/HtmlSemDoubleElement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ public function jsShowDimmer($show=true) {
149149
* @see BaseHtml::compile()
150150
*/
151151
public function compile(JsUtils $js=NULL, &$view=NULL) {
152-
if (isset($this->_popup))
152+
if (isset($this->_popup)){
153153
$this->_popup->compile($js);
154+
}
154155
return parent::compile($js, $view);
155156
}
156157

Ajax/semantic/html/collections/form/HtmlFormField.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ public function addRule($type,$prompt=NULL,$value=NULL){
133133
}
134134
return $this;
135135
}
136+
137+
public function setOptional($optional=true){
138+
$field=$this->getDataField();
139+
if(isset($field)){
140+
if(!isset($this->_validation)){
141+
$this->_validation=new FieldValidation($field->getIdentifier());
142+
}
143+
$this->_validation->setOptional($optional);
144+
}
145+
}
136146

137147
public function addRules(array $rules){
138148
foreach ($rules as $rule){

Ajax/semantic/html/collections/form/traits/FormTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,10 @@ public function addExtraFieldRule($fieldname,$type,$prompt=NULL,$value=NULL){
175175
$fv=$form->getExtraFieldValidation($fieldname);
176176
$fv->addRule($type,$prompt,$value);
177177
}
178+
179+
public function setOptional($fieldname,$optional=true){
180+
$form=$this->getForm();
181+
$fv=$form->getExtraFieldValidation($fieldname);
182+
$fv->setOptional($optional);
183+
}
178184
}

Ajax/semantic/html/content/InternalPopup.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public function onShow($jsCode){
3737

3838
public function compile(JsUtils $js=NULL){
3939
if(JString::isNotNull($this->title)){
40-
$this->semElement->addToProperty("data-title", $this->title);
40+
$this->semElement->setProperty("data-title", $this->title);
4141
}
4242
if(JString::isNotNull($this->content)){
43-
$this->semElement->addToProperty("data-content", $this->content);
43+
$this->semElement->setProperty("data-content", $this->content);
4444
}
4545
$this->_compileHtml($js);
4646
if(JString::isNotNull($this->variation)){
47-
$this->semElement->addToProperty("data-variation", $this->variation);
47+
$this->semElement->setProperty("data-variation", $this->variation);
4848
}
4949
}
5050

Ajax/semantic/html/modules/checkbox/HtmlCheckbox.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Ajax\semantic\html\modules\checkbox;
44

5-
use Ajax\semantic\html\modules\checkbox\AbstractCheckbox;
65
use Ajax\semantic\html\base\constants\CheckboxType;
76

87
class HtmlCheckbox extends AbstractCheckbox {

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,10 @@ public function getVisibleProperties() {
345345
return $this->visibleProperties;
346346
}
347347

348+
/**
349+
* @return callable
350+
*/
351+
public function getDefaultValueFunction() {
352+
return $this->defaultValueFunction;
353+
}
348354
}

Ajax/semantic/widgets/dataelement/DataElement.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Ajax\service\JArray;
99
use Ajax\semantic\html\collections\table\HtmlTable;
1010
use Ajax\semantic\html\base\traits\BaseTrait;
11+
use Ajax\service\JString;
1112

1213
/**
1314
* DataElement widget for displaying an instance of model
@@ -122,7 +123,8 @@ public function setColWidths($widths){
122123
}
123124

124125
public function run(JsUtils $js){
125-
$js->execOn("click", ".ui.toggle", 'var active=$(this).hasClass("active");$(this).children("i").toggleClass("up",active).toggleClass("down",!active);$(this).closest("td").next("td").children().toggle(active);');
126+
if(JString::isNotNull($this->_identifier))
127+
$js->execOn("click", "#".$this->_identifier." .ui.toggle", 'var active=$(this).hasClass("active");$(this).children("i").toggleClass("up",active).toggleClass("down",!active);var nextTd=$(this).closest("td").next("td");nextTd.children(":not(.toggle-caption)").toggle(active);nextTd.children(".toggle-caption").toggle(!active);$(this).trigger({type:"toggled",active: active,caption: nextTd.children(".toggle-caption")});');
126128
parent::run($js);
127129
}
128130
}

0 commit comments

Comments
 (0)