From f0496b2decd309b2e9c62dee8b1217c7d2ac15e1 Mon Sep 17 00:00:00 2001 From: msnetkov Date: Fri, 22 Feb 2019 09:40:00 +0700 Subject: [PATCH 1/5] Bug wbraganca/yii2-dynamicform#245: Init widget on page after active form --- src/DynamicFormWidget.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/DynamicFormWidget.php b/src/DynamicFormWidget.php index 4fe09fd..4ea9683 100644 --- a/src/DynamicFormWidget.php +++ b/src/DynamicFormWidget.php @@ -216,8 +216,11 @@ public function registerAssets($view) $js .= "});\n"; $view->registerJs($js, $view::POS_READY); - $js = 'jQuery("#' . $this->formId . '").yiiDynamicForm(' . $this->_hashVar .');' . "\n"; - $view->registerJs($js, $view::POS_LOAD); + // init dynamic widget after active form initiazed + $js = 'jQuery("#' . $this->formId . '").on("afterInit", function(e) {' . "\n"; + $js .= ' $(this).yiiDynamicForm(' . $this->_hashVar . ');' . "\n"; + $js .= '});' . "\n"; + $view->registerJs($js, $view::POS_READY); } /** From 3e5873d30da09cf16c3fe88f00dbcb95f9ba9c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=A1=D0=BD=D0=B5?= =?UTF-8?q?=D1=82=D0=BA=D0=BE=D0=B2?= Date: Mon, 25 Feb 2019 09:39:52 +0700 Subject: [PATCH 2/5] Fix headers in README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0cf876e..25bfd69 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Usage ###Hypothetical Scenario ![Database](http://wbraganca.com/img/yii2-dynamicform/hypothetical-scenario.jpg) -###The View +### The View ```php ``` -###Javascript Events +### Javascript Events ```javascript @@ -172,7 +172,7 @@ $(".dynamicform_wrapper").on("limitReached", function(e, item) { ``` -###The Controller (sample code) +### The Controller (sample code) ```php Date: Mon, 25 Feb 2019 09:40:25 +0700 Subject: [PATCH 3/5] Fix header in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 25bfd69..159137f 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Demos Usage ----- -###Hypothetical Scenario +### Hypothetical Scenario ![Database](http://wbraganca.com/img/yii2-dynamicform/hypothetical-scenario.jpg) ### The View From 8040a646b64249c29b06b5835d01f39476383d73 Mon Sep 17 00:00:00 2001 From: msnetkov Date: Tue, 26 Feb 2019 14:50:00 +0700 Subject: [PATCH 4/5] Fix restore DepDrop if widget depends outside input --- src/assets/yii2-dynamic-form.js | 2 +- src/assets/yii2-dynamic-form.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/assets/yii2-dynamic-form.js b/src/assets/yii2-dynamic-form.js index 1fbdec1..49b8362 100644 --- a/src/assets/yii2-dynamic-form.js +++ b/src/assets/yii2-dynamic-form.js @@ -326,7 +326,7 @@ if (matchID && matchID.length === 4) { for (index = 0; index < configDepdrop.depends.length; ++index) { var match = configDepdrop.depends[index].match(regexID); - if (match && match.length === 4) { + if (match && match.length === 4 && match[2] !== '-') { configDepdrop.depends[index] = match[1] + matchID[2] + match[3]; } } diff --git a/src/assets/yii2-dynamic-form.min.js b/src/assets/yii2-dynamic-form.min.js index f2ff758..a989fe2 100644 --- a/src/assets/yii2-dynamic-form.min.js +++ b/src/assets/yii2-dynamic-form.min.js @@ -5,4 +5,4 @@ * * @author Wanderson Bragança */ -!function($){var pluginName="yiiDynamicForm",regexID=/^(.+?)([-\d-]{1,})(.+)$/i,regexName=/(^.+?)([\[\d{1,}\]]{1,})(\[.+\]$)/i;$.fn.yiiDynamicForm=function(e){return methods[e]?methods[e].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof e&&e?($.error("Method "+e+" does not exist on jQuery.yiiDynamicForm"),!1):methods.init.apply(this,arguments)};var events={beforeInsert:"beforeInsert",afterInsert:"afterInsert",beforeDelete:"beforeDelete",afterDelete:"afterDelete",limitReached:"limitReached"},methods={init:function(e){return this.each(function(){e.template=_parseTemplate(e)})},addItem:function(e,t,i){_addItem(e,t,i)},deleteItem:function(e,t,i){_deleteItem(e,t,i)},updateContainer:function(){var widgetOptions=eval($(this).attr("data-dynamicform"));_updateAttributes(widgetOptions),_restoreSpecialJs(widgetOptions),_fixFormValidaton(widgetOptions)}},_parseTemplate=function(widgetOptions){var $template=$(widgetOptions.template);$template.find("div[data-dynamicform]").each(function(){var widgetOptions=eval($(this).attr("data-dynamicform"));if($(widgetOptions.widgetItem).length>1){var item=$(this).find(widgetOptions.widgetItem).first()[0].outerHTML;$(this).find(widgetOptions.widgetBody).html(item)}}),$template.find("input, textarea, select").each(function(){if($(this).is(":checkbox")||$(this).is(":radio")){var e=$(this).is(":checkbox")?"checkbox":"radio",t=$(this).attr("name"),i=$template.find('input[type="hidden"][name="'+t+'"]').first(),a=$template.find('input[type="'+e+'"][name="'+t+'"]').length;i&&1===a&&($(this).val(1),i.val(0)),$(this).prop("checked",!1)}else $(this).is("select")?$(this).find("option:selected").removeAttr("selected"):$(this).val("")});var yiiActiveFormData=$("#"+widgetOptions.formId).yiiActiveForm("data");return $template.find("."+yiiActiveFormData.settings.errorCssClass).removeClass(yiiActiveFormData.settings.errorCssClass),$template.find("."+yiiActiveFormData.settings.successCssClass).removeClass(yiiActiveFormData.settings.successCssClass),$template},_getWidgetOptionsRoot=function(widgetOptions){return eval($(widgetOptions.widgetBody).parents("div[data-dynamicform]").last().attr("data-dynamicform"))},_getLevel=function(e){var t=e.parents("div[data-dynamicform]").length;return t=0>t?0:t},_count=function(e,t){return e.closest("."+t.widgetContainer).find(t.widgetItem).length},_createIdentifiers=function(e){return new Array(e+2).join("0").split("")},_addItem=function(e,t,i){var a=_count(i,e);a1){$elem.find("div[data-dynamicform]").each(function(){for(var currentWidgetOptions=eval($(this).attr("data-dynamicform")),level=_getLevel($(this)),identifiers=_createIdentifiers(level),numItems=$(this).find(currentWidgetOptions.widgetItem).length,i=1;numItems-1>=i;i++){var aux=identifiers;aux[level]=i,currentWidgetOptions.fields.forEach(function(e){var t=e.id.replace("{}",aux.join("-"));"undefined"!==$("#"+currentWidgetOptions.formId).yiiActiveForm("find",t)&&$("#"+currentWidgetOptions.formId).yiiActiveForm("remove",t)})}});var level=_getLevel($elem.closest("."+widgetOptions.widgetContainer)),widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions),identifiers=_createIdentifiers(level);identifiers[0]=$(widgetOptionsRoot.widgetItem).length-1,identifiers[level]=count-1,widgetOptions.fields.forEach(function(e){var t=e.id.replace("{}",identifiers.join("-"));"undefined"!==$("#"+widgetOptions.formId).yiiActiveForm("find",t)&&$("#"+widgetOptions.formId).yiiActiveForm("remove",t)})}},_deleteItem=function(e,t,i){var a=_count(i,e);if(a>e.min){$todelete=i.closest(e.widgetItem);var n=$("."+e.widgetContainer).triggerHandler(events.beforeDelete,$todelete);n!==!1&&(_removeValidations($todelete,e,a),$todelete.remove(),_updateAttributes(e),_restoreSpecialJs(e),_fixFormValidaton(e),$("."+e.widgetContainer).triggerHandler(events.afterDelete))}},_updateAttrID=function($elem,index){var widgetOptions=eval($elem.closest("div[data-dynamicform]").attr("data-dynamicform")),id=$elem.attr("id"),newID=id;if(void 0!==id){var matches=id.match(regexID);if(matches&&4===matches.length){matches[2]=matches[2].substring(1,matches[2].length-1);var identifiers=matches[2].split("-");if(identifiers[0]=index,identifiers.length>1){var widgetsOptions=[];$elem.parents("div[data-dynamicform]").each(function(i){widgetsOptions[i]=eval($(this).attr("data-dynamicform"))}),widgetsOptions=widgetsOptions.reverse();for(var i=identifiers.length-1;i>=1;i--)identifiers[i]=$elem.closest(widgetsOptions[i].widgetItem).index()}newID=matches[1]+"-"+identifiers.join("-")+"-"+matches[3],$elem.attr("id",newID)}else newID=id+index,$elem.attr("id",newID)}return id!==newID&&($elem.closest(widgetOptions.widgetItem).find(".field-"+id).each(function(){$(this).removeClass("field-"+id).addClass("field-"+newID)}),$elem.closest(widgetOptions.widgetItem).find("label[for='"+id+"']").attr("for",newID)),newID},_updateAttrName=function($elem,index){var name=$elem.attr("name");if(void 0!==name){var matches=name.match(regexName);if(matches&&4===matches.length){matches[2]=matches[2].replace(/\]\[/g,"-").replace(/\]|\[/g,"");var identifiers=matches[2].split("-");if(identifiers[0]=index,identifiers.length>1){var widgetsOptions=[];$elem.parents("div[data-dynamicform]").each(function(i){widgetsOptions[i]=eval($(this).attr("data-dynamicform"))}),widgetsOptions=widgetsOptions.reverse();for(var i=identifiers.length-1;i>=1;i--)identifiers[i]=$elem.closest(widgetsOptions[i].widgetItem).index()}name=matches[1]+"["+identifiers.join("][")+"]"+matches[3],$elem.attr("name",name)}}return name},_updateAttributes=function(e){var t=_getWidgetOptionsRoot(e);$(t.widgetItem).each(function(e){$(this);$(this).find("*").each(function(){_updateAttrID($(this),e),_updateAttrName($(this),e)})})},_fixFormValidatonInput=function(e,t,i,a){void 0!==t&&(t=$.extend(!0,{},t),t.id=i,t.container=".field-"+i,t.input="#"+i,t.name=a,t.value=$("#"+i).val(),t.status=0,"undefined"!==$("#"+e.formId).yiiActiveForm("find",i)&&$("#"+e.formId).yiiActiveForm("remove",i),$("#"+e.formId).yiiActiveForm("add",t))},_fixFormValidaton=function(widgetOptions){var widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions);$(widgetOptionsRoot.widgetBody).find("input, textarea, select").each(function(){var id=$(this).attr("id"),name=$(this).attr("name");if(void 0!==id&&void 0!==name){currentWidgetOptions=eval($(this).closest("div[data-dynamicform]").attr("data-dynamicform"));var matches=id.match(regexID);if(matches&&4===matches.length){matches[2]=matches[2].substring(1,matches[2].length-1);var level=_getLevel($(this)),identifiers=_createIdentifiers(level-1),baseID=matches[1]+"-"+identifiers.join("-")+"-"+matches[3],attribute=$("#"+currentWidgetOptions.formId).yiiActiveForm("find",baseID);_fixFormValidatonInput(currentWidgetOptions,attribute,id,name)}}})},_restoreKrajeeDepdrop=function($elem){var configDepdrop=$.extend(!0,{},eval($elem.attr("data-krajee-depdrop"))),inputID=$elem.attr("id"),matchID=inputID.match(regexID);if(matchID&&4===matchID.length)for(index=0;index0&&$hasInputmask.each(function(){$(this).inputmask("remove"),$(this).inputmask(eval($(this).attr("data-plugin-inputmask")))});var $hasDatepicker=$(widgetOptionsRoot.widgetItem).find("[data-krajee-datepicker]");$hasDatepicker.length>0&&$hasDatepicker.each(function(){$(this).parent().removeData().datepicker("remove"),$(this).parent().datepicker(eval($(this).attr("data-krajee-datepicker")))});var $hasTimepicker=$(widgetOptionsRoot.widgetItem).find("[data-krajee-timepicker]");$hasTimepicker.length>0&&$hasTimepicker.each(function(){$(this).removeData().off(),$(this).parent().find(".bootstrap-timepicker-widget").remove(),$(this).unbind(),$(this).timepicker(eval($(this).attr("data-krajee-timepicker")))});var $hasMaskmoney=$(widgetOptionsRoot.widgetItem).find("[data-krajee-maskMoney]");$hasMaskmoney.length>0&&$hasMaskmoney.each(function(){$(this).parent().find("input").removeData().off();var id="#"+$(this).attr("id"),displayID=id+"-disp";$(displayID).maskMoney("destroy"),$(displayID).maskMoney(eval($(this).attr("data-krajee-maskMoney"))),$(displayID).maskMoney("mask",parseFloat($(id).val())),$(displayID).on("change",function(){var e=$(displayID).maskMoney("unmasked")[0];$(id).val(e),$(id).trigger("change")})});var $hasFileinput=$(widgetOptionsRoot.widgetItem).find("[data-krajee-fileinput]");$hasFileinput.length>0&&$hasFileinput.each(function(){$(this).fileinput(eval($(this).attr("data-krajee-fileinput")))});var $hasTouchSpin=$(widgetOptionsRoot.widgetItem).find("[data-krajee-TouchSpin]");$hasTouchSpin.length>0&&$hasTouchSpin.each(function(){$(this).TouchSpin("destroy"),$(this).TouchSpin(eval($(this).attr("data-krajee-TouchSpin")))});var $hasSpectrum=$(widgetOptionsRoot.widgetItem).find("[data-krajee-spectrum]");$hasSpectrum.length>0&&$hasSpectrum.each(function(){var id="#"+$(this).attr("id"),sourceID=id+"-source";$(sourceID).spectrum("destroy"),$(sourceID).unbind(),$(id).unbind();var configSpectrum=eval($(this).attr("data-krajee-spectrum"));configSpectrum.change=function(e){jQuery(id).val(e.toString())},$(sourceID).attr("name",$(sourceID).attr("id")),$(sourceID).spectrum(configSpectrum),$(sourceID).spectrum("set",jQuery(id).val()),$(id).on("change",function(){$(sourceID).spectrum("set",jQuery(id).val())})});var $hasDepdrop=$(widgetOptionsRoot.widgetItem).find("[data-krajee-depdrop]");$hasDepdrop.length>0&&$hasDepdrop.each(function(){void 0===$(this).data("select2")&&($(this).removeData().off(),$(this).unbind(),_restoreKrajeeDepdrop($(this)))});var $hasSelect2=$(widgetOptionsRoot.widgetItem).find("[data-krajee-select2]");$hasSelect2.length>0&&$hasSelect2.each(function(){var id=$(this).attr("id"),configSelect2=eval($(this).attr("data-krajee-select2"));$(this).data("select2")&&$(this).select2("destroy");var configDepdrop=$(this).data("depdrop");configDepdrop&&(configDepdrop=$.extend(!0,{},configDepdrop),$(this).removeData().off(),$(this).unbind(),_restoreKrajeeDepdrop($(this))),$.when($("#"+id).select2(configSelect2)).done(initSelect2Loading(id,".select2-container--krajee"));var kvClose="kv_close_"+id.replace(/\-/g,"_");if($("#"+id).on("select2:opening",function(e){initSelect2DropStyle(id,kvClose,e)}),$("#"+id).on("select2:unselect",function(){window[kvClose]=!0}),configDepdrop){var loadingText=configDepdrop.loadingText?configDepdrop.loadingText:"Loading ...";initDepdropS2(id,loadingText)}})}}(window.jQuery); +!function($){var pluginName="yiiDynamicForm",regexID=/^(.+?)([-\d-]{1,})(.+)$/i,regexName=/(^.+?)([\[\d{1,}\]]{1,})(\[.+\]$)/i;$.fn.yiiDynamicForm=function(e){return methods[e]?methods[e].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof e&&e?($.error("Method "+e+" does not exist on jQuery.yiiDynamicForm"),!1):methods.init.apply(this,arguments)};var events={beforeInsert:"beforeInsert",afterInsert:"afterInsert",beforeDelete:"beforeDelete",afterDelete:"afterDelete",limitReached:"limitReached"},methods={init:function(e){return this.each(function(){e.template=_parseTemplate(e)})},addItem:function(e,t,i){_addItem(e,t,i)},deleteItem:function(e,t,i){_deleteItem(e,t,i)},updateContainer:function(){var widgetOptions=eval($(this).attr("data-dynamicform"));_updateAttributes(widgetOptions),_restoreSpecialJs(widgetOptions),_fixFormValidaton(widgetOptions)}},_parseTemplate=function(widgetOptions){var $template=$(widgetOptions.template);$template.find("div[data-dynamicform]").each(function(){var widgetOptions=eval($(this).attr("data-dynamicform"));if($(widgetOptions.widgetItem).length>1){var item=$(this).find(widgetOptions.widgetItem).first()[0].outerHTML;$(this).find(widgetOptions.widgetBody).html(item)}}),$template.find("input, textarea, select").each(function(){if($(this).is(":checkbox")||$(this).is(":radio")){var e=$(this).is(":checkbox")?"checkbox":"radio",t=$(this).attr("name"),i=$template.find('input[type="hidden"][name="'+t+'"]').first(),a=$template.find('input[type="'+e+'"][name="'+t+'"]').length;i&&1===a&&($(this).val(1),i.val(0)),$(this).prop("checked",!1)}else $(this).is("select")?$(this).find("option:selected").removeAttr("selected"):$(this).val("")});var yiiActiveFormData=$("#"+widgetOptions.formId).yiiActiveForm("data");return $template.find("."+yiiActiveFormData.settings.errorCssClass).removeClass(yiiActiveFormData.settings.errorCssClass),$template.find("."+yiiActiveFormData.settings.successCssClass).removeClass(yiiActiveFormData.settings.successCssClass),$template},_getWidgetOptionsRoot=function(widgetOptions){return eval($(widgetOptions.widgetBody).parents("div[data-dynamicform]").last().attr("data-dynamicform"))},_getLevel=function(e){var t=e.parents("div[data-dynamicform]").length;return t=0>t?0:t},_count=function(e,t){return e.closest("."+t.widgetContainer).find(t.widgetItem).length},_createIdentifiers=function(e){return new Array(e+2).join("0").split("")},_addItem=function(e,t,i){var a=_count(i,e);a1){$elem.find("div[data-dynamicform]").each(function(){for(var currentWidgetOptions=eval($(this).attr("data-dynamicform")),level=_getLevel($(this)),identifiers=_createIdentifiers(level),numItems=$(this).find(currentWidgetOptions.widgetItem).length,i=1;numItems-1>=i;i++){var aux=identifiers;aux[level]=i,currentWidgetOptions.fields.forEach(function(e){var t=e.id.replace("{}",aux.join("-"));"undefined"!==$("#"+currentWidgetOptions.formId).yiiActiveForm("find",t)&&$("#"+currentWidgetOptions.formId).yiiActiveForm("remove",t)})}});var level=_getLevel($elem.closest("."+widgetOptions.widgetContainer)),widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions),identifiers=_createIdentifiers(level);identifiers[0]=$(widgetOptionsRoot.widgetItem).length-1,identifiers[level]=count-1,widgetOptions.fields.forEach(function(e){var t=e.id.replace("{}",identifiers.join("-"));"undefined"!==$("#"+widgetOptions.formId).yiiActiveForm("find",t)&&$("#"+widgetOptions.formId).yiiActiveForm("remove",t)})}},_deleteItem=function(e,t,i){var a=_count(i,e);if(a>e.min){$todelete=i.closest(e.widgetItem);var n=$("."+e.widgetContainer).triggerHandler(events.beforeDelete,$todelete);n!==!1&&(_removeValidations($todelete,e,a),$todelete.remove(),_updateAttributes(e),_restoreSpecialJs(e),_fixFormValidaton(e),$("."+e.widgetContainer).triggerHandler(events.afterDelete))}},_updateAttrID=function($elem,index){var widgetOptions=eval($elem.closest("div[data-dynamicform]").attr("data-dynamicform")),id=$elem.attr("id"),newID=id;if(void 0!==id){var matches=id.match(regexID);if(matches&&4===matches.length){matches[2]=matches[2].substring(1,matches[2].length-1);var identifiers=matches[2].split("-");if(identifiers[0]=index,identifiers.length>1){var widgetsOptions=[];$elem.parents("div[data-dynamicform]").each(function(i){widgetsOptions[i]=eval($(this).attr("data-dynamicform"))}),widgetsOptions=widgetsOptions.reverse();for(var i=identifiers.length-1;i>=1;i--)identifiers[i]=$elem.closest(widgetsOptions[i].widgetItem).index()}newID=matches[1]+"-"+identifiers.join("-")+"-"+matches[3],$elem.attr("id",newID)}else newID=id+index,$elem.attr("id",newID)}return id!==newID&&($elem.closest(widgetOptions.widgetItem).find(".field-"+id).each(function(){$(this).removeClass("field-"+id).addClass("field-"+newID)}),$elem.closest(widgetOptions.widgetItem).find("label[for='"+id+"']").attr("for",newID)),newID},_updateAttrName=function($elem,index){var name=$elem.attr("name");if(void 0!==name){var matches=name.match(regexName);if(matches&&4===matches.length){matches[2]=matches[2].replace(/\]\[/g,"-").replace(/\]|\[/g,"");var identifiers=matches[2].split("-");if(identifiers[0]=index,identifiers.length>1){var widgetsOptions=[];$elem.parents("div[data-dynamicform]").each(function(i){widgetsOptions[i]=eval($(this).attr("data-dynamicform"))}),widgetsOptions=widgetsOptions.reverse();for(var i=identifiers.length-1;i>=1;i--)identifiers[i]=$elem.closest(widgetsOptions[i].widgetItem).index()}name=matches[1]+"["+identifiers.join("][")+"]"+matches[3],$elem.attr("name",name)}}return name},_updateAttributes=function(e){var t=_getWidgetOptionsRoot(e);$(t.widgetItem).each(function(e){$(this);$(this).find("*").each(function(){_updateAttrID($(this),e),_updateAttrName($(this),e)})})},_fixFormValidatonInput=function(e,t,i,a){void 0!==t&&(t=$.extend(!0,{},t),t.id=i,t.container=".field-"+i,t.input="#"+i,t.name=a,t.value=$("#"+i).val(),t.status=0,"undefined"!==$("#"+e.formId).yiiActiveForm("find",i)&&$("#"+e.formId).yiiActiveForm("remove",i),$("#"+e.formId).yiiActiveForm("add",t))},_fixFormValidaton=function(widgetOptions){var widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions);$(widgetOptionsRoot.widgetBody).find("input, textarea, select").each(function(){var id=$(this).attr("id"),name=$(this).attr("name");if(void 0!==id&&void 0!==name){currentWidgetOptions=eval($(this).closest("div[data-dynamicform]").attr("data-dynamicform"));var matches=id.match(regexID);if(matches&&4===matches.length){matches[2]=matches[2].substring(1,matches[2].length-1);var level=_getLevel($(this)),identifiers=_createIdentifiers(level-1),baseID=matches[1]+"-"+identifiers.join("-")+"-"+matches[3],attribute=$("#"+currentWidgetOptions.formId).yiiActiveForm("find",baseID);_fixFormValidatonInput(currentWidgetOptions,attribute,id,name)}}})},_restoreKrajeeDepdrop=function($elem){var configDepdrop=$.extend(!0,{},eval($elem.attr("data-krajee-depdrop"))),inputID=$elem.attr("id"),matchID=inputID.match(regexID);if(matchID&&4===matchID.length)for(index=0;index0&&$hasInputmask.each(function(){$(this).inputmask("remove"),$(this).inputmask(eval($(this).attr("data-plugin-inputmask")))});var $hasDatepicker=$(widgetOptionsRoot.widgetItem).find("[data-krajee-datepicker]");$hasDatepicker.length>0&&$hasDatepicker.each(function(){$(this).parent().removeData().datepicker("remove"),$(this).parent().datepicker(eval($(this).attr("data-krajee-datepicker")))});var $hasTimepicker=$(widgetOptionsRoot.widgetItem).find("[data-krajee-timepicker]");$hasTimepicker.length>0&&$hasTimepicker.each(function(){$(this).removeData().off(),$(this).parent().find(".bootstrap-timepicker-widget").remove(),$(this).unbind(),$(this).timepicker(eval($(this).attr("data-krajee-timepicker")))});var $hasMaskmoney=$(widgetOptionsRoot.widgetItem).find("[data-krajee-maskMoney]");$hasMaskmoney.length>0&&$hasMaskmoney.each(function(){$(this).parent().find("input").removeData().off();var id="#"+$(this).attr("id"),displayID=id+"-disp";$(displayID).maskMoney("destroy"),$(displayID).maskMoney(eval($(this).attr("data-krajee-maskMoney"))),$(displayID).maskMoney("mask",parseFloat($(id).val())),$(displayID).on("change",function(){var e=$(displayID).maskMoney("unmasked")[0];$(id).val(e),$(id).trigger("change")})});var $hasFileinput=$(widgetOptionsRoot.widgetItem).find("[data-krajee-fileinput]");$hasFileinput.length>0&&$hasFileinput.each(function(){$(this).fileinput(eval($(this).attr("data-krajee-fileinput")))});var $hasTouchSpin=$(widgetOptionsRoot.widgetItem).find("[data-krajee-TouchSpin]");$hasTouchSpin.length>0&&$hasTouchSpin.each(function(){$(this).TouchSpin("destroy"),$(this).TouchSpin(eval($(this).attr("data-krajee-TouchSpin")))});var $hasSpectrum=$(widgetOptionsRoot.widgetItem).find("[data-krajee-spectrum]");$hasSpectrum.length>0&&$hasSpectrum.each(function(){var id="#"+$(this).attr("id"),sourceID=id+"-source";$(sourceID).spectrum("destroy"),$(sourceID).unbind(),$(id).unbind();var configSpectrum=eval($(this).attr("data-krajee-spectrum"));configSpectrum.change=function(e){jQuery(id).val(e.toString())},$(sourceID).attr("name",$(sourceID).attr("id")),$(sourceID).spectrum(configSpectrum),$(sourceID).spectrum("set",jQuery(id).val()),$(id).on("change",function(){$(sourceID).spectrum("set",jQuery(id).val())})});var $hasDepdrop=$(widgetOptionsRoot.widgetItem).find("[data-krajee-depdrop]");$hasDepdrop.length>0&&$hasDepdrop.each(function(){void 0===$(this).data("select2")&&($(this).removeData().off(),$(this).unbind(),_restoreKrajeeDepdrop($(this)))});var $hasSelect2=$(widgetOptionsRoot.widgetItem).find("[data-krajee-select2]");$hasSelect2.length>0&&$hasSelect2.each(function(){var id=$(this).attr("id"),configSelect2=eval($(this).attr("data-krajee-select2"));$(this).data("select2")&&$(this).select2("destroy");var configDepdrop=$(this).data("depdrop");configDepdrop&&(configDepdrop=$.extend(!0,{},configDepdrop),$(this).removeData().off(),$(this).unbind(),_restoreKrajeeDepdrop($(this))),$.when($("#"+id).select2(configSelect2)).done(initSelect2Loading(id,".select2-container--krajee"));var kvClose="kv_close_"+id.replace(/\-/g,"_");if($("#"+id).on("select2:opening",function(e){initSelect2DropStyle(id,kvClose,e)}),$("#"+id).on("select2:unselect",function(){window[kvClose]=!0}),configDepdrop){var loadingText=configDepdrop.loadingText?configDepdrop.loadingText:"Loading ...";initDepdropS2(id,loadingText)}})}}(window.jQuery); From 11b9506089efc798bae48c834919abab456154a3 Mon Sep 17 00:00:00 2001 From: msnetkov Date: Tue, 9 Apr 2019 12:01:19 +0700 Subject: [PATCH 5/5] Reset template with custom values using "data-empty-value" attribute --- src/assets/yii2-dynamic-form.js | 12 ++++++++---- src/assets/yii2-dynamic-form.min.js | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/assets/yii2-dynamic-form.js b/src/assets/yii2-dynamic-form.js index 49b8362..b7682ab 100644 --- a/src/assets/yii2-dynamic-form.js +++ b/src/assets/yii2-dynamic-form.js @@ -54,8 +54,12 @@ } }; - var _parseTemplate = function(widgetOptions) { + // Allow reset template to custom empty value. + var _emptyVal = function($el, value) { + $el.val($el.data("empty-value") || value); + } + var _parseTemplate = function(widgetOptions) { var $template = $(widgetOptions.template); $template.find('div[data-dynamicform]').each(function(){ var widgetOptions = eval($(this).attr('data-dynamicform')); @@ -73,15 +77,15 @@ var count = $template.find('input[type="' + type +'"][name="' + inputName + '"]').length; if ($inputHidden && count === 1) { - $(this).val(1); - $inputHidden.val(0); + _emptyVal($(this), 1); + _emptyVal($inputHidden, 0); } $(this).prop('checked', false); } else if($(this).is('select')) { $(this).find('option:selected').removeAttr("selected"); } else { - $(this).val(''); + _emptyVal($(this), ''); } }); diff --git a/src/assets/yii2-dynamic-form.min.js b/src/assets/yii2-dynamic-form.min.js index a989fe2..de2650b 100644 --- a/src/assets/yii2-dynamic-form.min.js +++ b/src/assets/yii2-dynamic-form.min.js @@ -5,4 +5,4 @@ * * @author Wanderson Bragança */ -!function($){var pluginName="yiiDynamicForm",regexID=/^(.+?)([-\d-]{1,})(.+)$/i,regexName=/(^.+?)([\[\d{1,}\]]{1,})(\[.+\]$)/i;$.fn.yiiDynamicForm=function(e){return methods[e]?methods[e].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof e&&e?($.error("Method "+e+" does not exist on jQuery.yiiDynamicForm"),!1):methods.init.apply(this,arguments)};var events={beforeInsert:"beforeInsert",afterInsert:"afterInsert",beforeDelete:"beforeDelete",afterDelete:"afterDelete",limitReached:"limitReached"},methods={init:function(e){return this.each(function(){e.template=_parseTemplate(e)})},addItem:function(e,t,i){_addItem(e,t,i)},deleteItem:function(e,t,i){_deleteItem(e,t,i)},updateContainer:function(){var widgetOptions=eval($(this).attr("data-dynamicform"));_updateAttributes(widgetOptions),_restoreSpecialJs(widgetOptions),_fixFormValidaton(widgetOptions)}},_parseTemplate=function(widgetOptions){var $template=$(widgetOptions.template);$template.find("div[data-dynamicform]").each(function(){var widgetOptions=eval($(this).attr("data-dynamicform"));if($(widgetOptions.widgetItem).length>1){var item=$(this).find(widgetOptions.widgetItem).first()[0].outerHTML;$(this).find(widgetOptions.widgetBody).html(item)}}),$template.find("input, textarea, select").each(function(){if($(this).is(":checkbox")||$(this).is(":radio")){var e=$(this).is(":checkbox")?"checkbox":"radio",t=$(this).attr("name"),i=$template.find('input[type="hidden"][name="'+t+'"]').first(),a=$template.find('input[type="'+e+'"][name="'+t+'"]').length;i&&1===a&&($(this).val(1),i.val(0)),$(this).prop("checked",!1)}else $(this).is("select")?$(this).find("option:selected").removeAttr("selected"):$(this).val("")});var yiiActiveFormData=$("#"+widgetOptions.formId).yiiActiveForm("data");return $template.find("."+yiiActiveFormData.settings.errorCssClass).removeClass(yiiActiveFormData.settings.errorCssClass),$template.find("."+yiiActiveFormData.settings.successCssClass).removeClass(yiiActiveFormData.settings.successCssClass),$template},_getWidgetOptionsRoot=function(widgetOptions){return eval($(widgetOptions.widgetBody).parents("div[data-dynamicform]").last().attr("data-dynamicform"))},_getLevel=function(e){var t=e.parents("div[data-dynamicform]").length;return t=0>t?0:t},_count=function(e,t){return e.closest("."+t.widgetContainer).find(t.widgetItem).length},_createIdentifiers=function(e){return new Array(e+2).join("0").split("")},_addItem=function(e,t,i){var a=_count(i,e);a1){$elem.find("div[data-dynamicform]").each(function(){for(var currentWidgetOptions=eval($(this).attr("data-dynamicform")),level=_getLevel($(this)),identifiers=_createIdentifiers(level),numItems=$(this).find(currentWidgetOptions.widgetItem).length,i=1;numItems-1>=i;i++){var aux=identifiers;aux[level]=i,currentWidgetOptions.fields.forEach(function(e){var t=e.id.replace("{}",aux.join("-"));"undefined"!==$("#"+currentWidgetOptions.formId).yiiActiveForm("find",t)&&$("#"+currentWidgetOptions.formId).yiiActiveForm("remove",t)})}});var level=_getLevel($elem.closest("."+widgetOptions.widgetContainer)),widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions),identifiers=_createIdentifiers(level);identifiers[0]=$(widgetOptionsRoot.widgetItem).length-1,identifiers[level]=count-1,widgetOptions.fields.forEach(function(e){var t=e.id.replace("{}",identifiers.join("-"));"undefined"!==$("#"+widgetOptions.formId).yiiActiveForm("find",t)&&$("#"+widgetOptions.formId).yiiActiveForm("remove",t)})}},_deleteItem=function(e,t,i){var a=_count(i,e);if(a>e.min){$todelete=i.closest(e.widgetItem);var n=$("."+e.widgetContainer).triggerHandler(events.beforeDelete,$todelete);n!==!1&&(_removeValidations($todelete,e,a),$todelete.remove(),_updateAttributes(e),_restoreSpecialJs(e),_fixFormValidaton(e),$("."+e.widgetContainer).triggerHandler(events.afterDelete))}},_updateAttrID=function($elem,index){var widgetOptions=eval($elem.closest("div[data-dynamicform]").attr("data-dynamicform")),id=$elem.attr("id"),newID=id;if(void 0!==id){var matches=id.match(regexID);if(matches&&4===matches.length){matches[2]=matches[2].substring(1,matches[2].length-1);var identifiers=matches[2].split("-");if(identifiers[0]=index,identifiers.length>1){var widgetsOptions=[];$elem.parents("div[data-dynamicform]").each(function(i){widgetsOptions[i]=eval($(this).attr("data-dynamicform"))}),widgetsOptions=widgetsOptions.reverse();for(var i=identifiers.length-1;i>=1;i--)identifiers[i]=$elem.closest(widgetsOptions[i].widgetItem).index()}newID=matches[1]+"-"+identifiers.join("-")+"-"+matches[3],$elem.attr("id",newID)}else newID=id+index,$elem.attr("id",newID)}return id!==newID&&($elem.closest(widgetOptions.widgetItem).find(".field-"+id).each(function(){$(this).removeClass("field-"+id).addClass("field-"+newID)}),$elem.closest(widgetOptions.widgetItem).find("label[for='"+id+"']").attr("for",newID)),newID},_updateAttrName=function($elem,index){var name=$elem.attr("name");if(void 0!==name){var matches=name.match(regexName);if(matches&&4===matches.length){matches[2]=matches[2].replace(/\]\[/g,"-").replace(/\]|\[/g,"");var identifiers=matches[2].split("-");if(identifiers[0]=index,identifiers.length>1){var widgetsOptions=[];$elem.parents("div[data-dynamicform]").each(function(i){widgetsOptions[i]=eval($(this).attr("data-dynamicform"))}),widgetsOptions=widgetsOptions.reverse();for(var i=identifiers.length-1;i>=1;i--)identifiers[i]=$elem.closest(widgetsOptions[i].widgetItem).index()}name=matches[1]+"["+identifiers.join("][")+"]"+matches[3],$elem.attr("name",name)}}return name},_updateAttributes=function(e){var t=_getWidgetOptionsRoot(e);$(t.widgetItem).each(function(e){$(this);$(this).find("*").each(function(){_updateAttrID($(this),e),_updateAttrName($(this),e)})})},_fixFormValidatonInput=function(e,t,i,a){void 0!==t&&(t=$.extend(!0,{},t),t.id=i,t.container=".field-"+i,t.input="#"+i,t.name=a,t.value=$("#"+i).val(),t.status=0,"undefined"!==$("#"+e.formId).yiiActiveForm("find",i)&&$("#"+e.formId).yiiActiveForm("remove",i),$("#"+e.formId).yiiActiveForm("add",t))},_fixFormValidaton=function(widgetOptions){var widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions);$(widgetOptionsRoot.widgetBody).find("input, textarea, select").each(function(){var id=$(this).attr("id"),name=$(this).attr("name");if(void 0!==id&&void 0!==name){currentWidgetOptions=eval($(this).closest("div[data-dynamicform]").attr("data-dynamicform"));var matches=id.match(regexID);if(matches&&4===matches.length){matches[2]=matches[2].substring(1,matches[2].length-1);var level=_getLevel($(this)),identifiers=_createIdentifiers(level-1),baseID=matches[1]+"-"+identifiers.join("-")+"-"+matches[3],attribute=$("#"+currentWidgetOptions.formId).yiiActiveForm("find",baseID);_fixFormValidatonInput(currentWidgetOptions,attribute,id,name)}}})},_restoreKrajeeDepdrop=function($elem){var configDepdrop=$.extend(!0,{},eval($elem.attr("data-krajee-depdrop"))),inputID=$elem.attr("id"),matchID=inputID.match(regexID);if(matchID&&4===matchID.length)for(index=0;index0&&$hasInputmask.each(function(){$(this).inputmask("remove"),$(this).inputmask(eval($(this).attr("data-plugin-inputmask")))});var $hasDatepicker=$(widgetOptionsRoot.widgetItem).find("[data-krajee-datepicker]");$hasDatepicker.length>0&&$hasDatepicker.each(function(){$(this).parent().removeData().datepicker("remove"),$(this).parent().datepicker(eval($(this).attr("data-krajee-datepicker")))});var $hasTimepicker=$(widgetOptionsRoot.widgetItem).find("[data-krajee-timepicker]");$hasTimepicker.length>0&&$hasTimepicker.each(function(){$(this).removeData().off(),$(this).parent().find(".bootstrap-timepicker-widget").remove(),$(this).unbind(),$(this).timepicker(eval($(this).attr("data-krajee-timepicker")))});var $hasMaskmoney=$(widgetOptionsRoot.widgetItem).find("[data-krajee-maskMoney]");$hasMaskmoney.length>0&&$hasMaskmoney.each(function(){$(this).parent().find("input").removeData().off();var id="#"+$(this).attr("id"),displayID=id+"-disp";$(displayID).maskMoney("destroy"),$(displayID).maskMoney(eval($(this).attr("data-krajee-maskMoney"))),$(displayID).maskMoney("mask",parseFloat($(id).val())),$(displayID).on("change",function(){var e=$(displayID).maskMoney("unmasked")[0];$(id).val(e),$(id).trigger("change")})});var $hasFileinput=$(widgetOptionsRoot.widgetItem).find("[data-krajee-fileinput]");$hasFileinput.length>0&&$hasFileinput.each(function(){$(this).fileinput(eval($(this).attr("data-krajee-fileinput")))});var $hasTouchSpin=$(widgetOptionsRoot.widgetItem).find("[data-krajee-TouchSpin]");$hasTouchSpin.length>0&&$hasTouchSpin.each(function(){$(this).TouchSpin("destroy"),$(this).TouchSpin(eval($(this).attr("data-krajee-TouchSpin")))});var $hasSpectrum=$(widgetOptionsRoot.widgetItem).find("[data-krajee-spectrum]");$hasSpectrum.length>0&&$hasSpectrum.each(function(){var id="#"+$(this).attr("id"),sourceID=id+"-source";$(sourceID).spectrum("destroy"),$(sourceID).unbind(),$(id).unbind();var configSpectrum=eval($(this).attr("data-krajee-spectrum"));configSpectrum.change=function(e){jQuery(id).val(e.toString())},$(sourceID).attr("name",$(sourceID).attr("id")),$(sourceID).spectrum(configSpectrum),$(sourceID).spectrum("set",jQuery(id).val()),$(id).on("change",function(){$(sourceID).spectrum("set",jQuery(id).val())})});var $hasDepdrop=$(widgetOptionsRoot.widgetItem).find("[data-krajee-depdrop]");$hasDepdrop.length>0&&$hasDepdrop.each(function(){void 0===$(this).data("select2")&&($(this).removeData().off(),$(this).unbind(),_restoreKrajeeDepdrop($(this)))});var $hasSelect2=$(widgetOptionsRoot.widgetItem).find("[data-krajee-select2]");$hasSelect2.length>0&&$hasSelect2.each(function(){var id=$(this).attr("id"),configSelect2=eval($(this).attr("data-krajee-select2"));$(this).data("select2")&&$(this).select2("destroy");var configDepdrop=$(this).data("depdrop");configDepdrop&&(configDepdrop=$.extend(!0,{},configDepdrop),$(this).removeData().off(),$(this).unbind(),_restoreKrajeeDepdrop($(this))),$.when($("#"+id).select2(configSelect2)).done(initSelect2Loading(id,".select2-container--krajee"));var kvClose="kv_close_"+id.replace(/\-/g,"_");if($("#"+id).on("select2:opening",function(e){initSelect2DropStyle(id,kvClose,e)}),$("#"+id).on("select2:unselect",function(){window[kvClose]=!0}),configDepdrop){var loadingText=configDepdrop.loadingText?configDepdrop.loadingText:"Loading ...";initDepdropS2(id,loadingText)}})}}(window.jQuery); +!function($){var pluginName="yiiDynamicForm",regexID=/^(.+?)([-\d-]{1,})(.+)$/i,regexName=/(^.+?)([\[\d{1,}\]]{1,})(\[.+\]$)/i;$.fn.yiiDynamicForm=function(e){return methods[e]?methods[e].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof e&&e?($.error("Method "+e+" does not exist on jQuery.yiiDynamicForm"),!1):methods.init.apply(this,arguments)};var events={beforeInsert:"beforeInsert",afterInsert:"afterInsert",beforeDelete:"beforeDelete",afterDelete:"afterDelete",limitReached:"limitReached"},methods={init:function(e){return this.each(function(){e.template=_parseTemplate(e)})},addItem:function(e,t,i){_addItem(e,t,i)},deleteItem:function(e,t,i){_deleteItem(e,t,i)},updateContainer:function(){var widgetOptions=eval($(this).attr("data-dynamicform"));_updateAttributes(widgetOptions),_restoreSpecialJs(widgetOptions),_fixFormValidaton(widgetOptions)}},_emptyVal=function(e,t){e.val(e.data("empty-value")||t)},_parseTemplate=function(widgetOptions){var $template=$(widgetOptions.template);$template.find("div[data-dynamicform]").each(function(){var widgetOptions=eval($(this).attr("data-dynamicform"));if(1<$(widgetOptions.widgetItem).length){var item=$(this).find(widgetOptions.widgetItem).first()[0].outerHTML;$(this).find(widgetOptions.widgetBody).html(item)}}),$template.find("input, textarea, select").each(function(){if($(this).is(":checkbox")||$(this).is(":radio")){var e=$(this).is(":checkbox")?"checkbox":"radio",t=$(this).attr("name"),i=$template.find('input[type="hidden"][name="'+t+'"]').first(),a=$template.find('input[type="'+e+'"][name="'+t+'"]').length;i&&1===a&&(_emptyVal($(this),1),_emptyVal(i,0)),$(this).prop("checked",!1)}else $(this).is("select")?$(this).find("option:selected").removeAttr("selected"):_emptyVal($(this),"")});var yiiActiveFormData=$("#"+widgetOptions.formId).yiiActiveForm("data");return $template.find("."+yiiActiveFormData.settings.errorCssClass).removeClass(yiiActiveFormData.settings.errorCssClass),$template.find("."+yiiActiveFormData.settings.successCssClass).removeClass(yiiActiveFormData.settings.successCssClass),$template},_getWidgetOptionsRoot=function(widgetOptions){return eval($(widgetOptions.widgetBody).parents("div[data-dynamicform]").last().attr("data-dynamicform"))},_getLevel=function(e){var t=e.parents("div[data-dynamicform]").length;return t=t<0?0:t},_count=function(e,t){return e.closest("."+t.widgetContainer).find(t.widgetItem).length},_createIdentifiers=function(e){return new Array(e+2).join("0").split("")},_addItem=function(e,t,i){_count(i,e)e.min&&($todelete=i.closest(e.widgetItem),!1!==$("."+e.widgetContainer).triggerHandler(events.beforeDelete,$todelete)&&(_removeValidations($todelete,e,a),$todelete.remove(),_updateAttributes(e),_restoreSpecialJs(e),_fixFormValidaton(e),$("."+e.widgetContainer).triggerHandler(events.afterDelete)))},_updateAttrID=function($elem,index){var widgetOptions=eval($elem.closest("div[data-dynamicform]").attr("data-dynamicform")),id=$elem.attr("id"),newID=id;if(void 0!==id){var matches=id.match(regexID);if(matches&&4===matches.length){matches[2]=matches[2].substring(1,matches[2].length-1);var identifiers=matches[2].split("-");if(identifiers[0]=index,1