From 97eab95aa52251b7fc56d009462575959e89473d Mon Sep 17 00:00:00 2001 From: Wanderson Braganca Date: Mon, 23 Feb 2015 16:20:21 -0300 Subject: [PATCH] Fixed error for the use of multiple nested widgets with zero initial elements --- CHANGELOG.md | 6 + DynamicFormWidget.php | 34 ++++- assets/yii2-dynamic-form.js | 221 +++++++++++++++----------------- assets/yii2-dynamic-form.min.js | 2 +- 4 files changed, 139 insertions(+), 124 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c9fda9..d8a6418 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ dev-master ---------- +version 2.0.1 +------------- +**Date:** 23-Fev-2015 +- bug: Fixed error for the use of multiple nested widgets with zero initial elements + + version 2.0.0 ------------- **Date:** 22-Fev-2015 diff --git a/DynamicFormWidget.php b/DynamicFormWidget.php index 7b9542c..6788aa7 100644 --- a/DynamicFormWidget.php +++ b/DynamicFormWidget.php @@ -20,6 +20,7 @@ */ class DynamicFormWidget extends \yii\base\Widget { + const HASH_VAR_BASE_NAME = 'dynamicform_'; /** * @var string */ @@ -72,6 +73,10 @@ class DynamicFormWidget extends \yii\base\Widget * @var string */ private $_insertPositions = ['bottom', 'top']; + /** + * @var string the hashed global variable name storing the pluginOptions + */ + private $_hashVar; /** * Initializes the widget @@ -133,6 +138,13 @@ protected function initOptions() ob_implicit_flush(false); } + protected function registerOptions($view) + { + $encOptions = Json::encode($this->_options); + $this->_hashVar = DynamicFormWidget::HASH_VAR_BASE_NAME . hash('crc32', $encOptions); + $view->registerJs("var {$this->_hashVar} = {$encOptions};\n", $view::POS_HEAD); + } + /** * Registers the needed assets */ @@ -141,7 +153,25 @@ public function registerAssets() $view = $this->getView(); DynamicFormAsset::register($view); $options = Json::encode($this->_options); - $view->registerJs('$(".' . $this->widgetContainer . '").yiiDynamicForm(' .$options .');', $view::POS_LOAD); + $this->registerOptions($view); + + $js = 'jQuery("#' . $this->formId . '").yiiDynamicForm(' . $this->_hashVar .');' . "\n"; + $view->registerJs($js, $view::POS_READY); + + // add a click handler for the clone button + $js = 'jQuery("#' . $this->formId . '").on("click", "' . $this->insertButton . '", function(e) {'. "\n"; + $js .= " e.preventDefault();\n"; + $js .= ' jQuery(".' . $this->widgetContainer . '").triggerHandler("beforeInsert", [jQuery(this)]);' . "\n"; + $js .= ' jQuery(".' . $this->widgetContainer . '").yiiDynamicForm("addItem", '. $this->_hashVar . ", e, jQuery(this));\n"; + $js .= "});\n"; + $view->registerJs($js, $view::POS_READY); + + // add a click handler for the remove button + $js = 'jQuery("#' . $this->formId . '").on("click", "' . $this->deleteButton . '", function(e) {'. "\n"; + $js .= " e.preventDefault();\n"; + $js .= ' jQuery(".' . $this->widgetContainer . '").yiiDynamicForm("deleteItem", '. $this->_hashVar . ", e, jQuery(this));\n"; + $js .= "});\n"; + $view->registerJs($js, $view::POS_READY); } public function run() @@ -159,7 +189,7 @@ public function run() } $this->registerAssets(); - echo Html::tag('div', $content, ['class' => $this->widgetContainer, 'data-dynamicform' => $this->widgetContainer]); + echo Html::tag('div', $content, ['class' => $this->widgetContainer, 'data-dynamicform' => $this->_hashVar]); } private function removeItems($content) diff --git a/assets/yii2-dynamic-form.js b/assets/yii2-dynamic-form.js index 1afa790..b5078c3 100644 --- a/assets/yii2-dynamic-form.js +++ b/assets/yii2-dynamic-form.js @@ -32,52 +32,36 @@ }; var methods = { - init: function (settings) { + init: function (widgetOptions) { return this.each(function () { - var $this = $(this); - var widgetData = {}; - - if ($('body').data(pluginName) !== undefined) { - widgetData = $('body').data(pluginName); - } - - if (widgetData[settings.widgetContainer] === undefined) { - widgetData = (widgetData[settings.widgetContainer] = settings, widgetData); - settings.template = _parseTemplate(settings); - - $('body').data(pluginName, widgetData); + widgetOptions.template = _parseTemplate(widgetOptions); + }); + }, - // add a click handler for the clone button - $('#' + settings.formId).on('click', settings.insertButton, function(e) { - e.preventDefault(); - $('.' + settings.widgetContainer).triggerHandler(events.beforeInsert, [$(this)]); - _addItem(settings.widgetContainer, e, $(this)); - }); + addItem: function (widgetOptions, e, $elem) { + _addItem(widgetOptions, e, $elem); + }, - // add a click handler for the remove button - $('#' + settings.formId).on('click', settings.deleteButton, function(e) { - e.preventDefault(); - _deleteItem(settings.widgetContainer, e, $(this)); - }); - } - }); + deleteItem: function (widgetOptions, e, $elem) { + _deleteItem(widgetOptions, e, $elem); }, updateContainer: function () { - var widgetContainer = $(this).attr('data-dynamicform'); - _updateAttributes(widgetContainer); - _restoreSpecialJs(widgetContainer); - _fixFormValidaton(widgetContainer); + var widgetOptions = eval($(this).attr('data-dynamicform')); + _updateAttributes(widgetOptions); + _restoreSpecialJs(widgetOptions); + _fixFormValidaton(widgetOptions); } }; - var _parseTemplate = function(settings) { - var $template = $(settings.template); + var _parseTemplate = function(widgetOptions) { + var $template = $(widgetOptions.template); + $template.find('div[data-dynamicform]').each(function(){ - var widgetData = $('body').data(pluginName)[$(this).attr('data-dynamicform')]; - if ($(widgetData.widgetItem).length > 1) { - var item = $(this).find(widgetData.widgetItem).first()[0].outerHTML; - $(this).find(widgetData.widgetBody).html(item); + 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); } }); @@ -97,10 +81,8 @@ return $template; }; - var _getDataWidgetRoot = function(widgetContainer) { - var widgetData = $('body').data(pluginName)[widgetContainer]; - var widgetContainerRoot = $(widgetData.widgetBody).parents('div[data-dynamicform]').last().attr('data-dynamicform'); - return $('body').data(pluginName)[widgetContainerRoot]; + var _getWidgetOptionsRoot = function(widgetOptions) { + return eval($(widgetOptions.widgetBody).parents('div[data-dynamicform]').last().attr('data-dynamicform')); }; var _getLevel = function($elem) { @@ -109,95 +91,92 @@ return level; }; - var _count = function($elem, widgetContainer) { - var widgetData = $('body').data(pluginName)[widgetContainer]; - return $elem.closest('.' + widgetContainer).find(widgetData.widgetItem).length; + var _count = function($elem, widgetOptions) { + return $elem.closest('.' + widgetOptions.widgetContainer).find(widgetOptions.widgetItem).length; }; var _creatIdentifiers = function(level) { return new Array(level + 2).join('0').split(''); }; - var _addItem = function(widgetContainer, e, $elem) { - var widgetData = $('body').data(pluginName)[widgetContainer]; - var count = _count($elem, widgetContainer); + var _addItem = function(widgetOptions, e, $elem) { + var count = _count($elem, widgetOptions); - if (count < widgetData.limit) { - $toclone = widgetData.template; + if (count < widgetOptions.limit) { + $toclone = widgetOptions.template; $newclone = $toclone.clone(false, false); - if (widgetData.insertPosition === 'top') { - $elem.closest('.' + widgetData.widgetContainer).find(widgetData.widgetBody).prepend($newclone); + if (widgetOptions.insertPosition === 'top') { + $elem.closest('.' + widgetOptions.widgetContainer).find(widgetOptions.widgetBody).prepend($newclone); } else { - $elem.closest('.' + widgetData.widgetContainer).find(widgetData.widgetBody).append($newclone); + $elem.closest('.' + widgetOptions.widgetContainer).find(widgetOptions.widgetBody).append($newclone); } - _updateAttributes(widgetContainer); - _restoreSpecialJs(widgetContainer); - _fixFormValidaton(widgetContainer); - $elem.closest('.' + widgetContainer).triggerHandler(events.afterInsert, $newclone); + _updateAttributes(widgetOptions); + _restoreSpecialJs(widgetOptions); + _fixFormValidaton(widgetOptions); + $elem.closest('.' + widgetOptions.widgetContainer).triggerHandler(events.afterInsert, $newclone); } else { // trigger a custom event for hooking - $elem.closest('.' + widgetContainer).triggerHandler(events.limitReached, widgetData.limit); + $elem.closest('.' + widgetOptions.widgetContainer).triggerHandler(events.limitReached, widgetOptions.limit); } }; - var _removeValidations = function($elem, widgetData, count) { + var _removeValidations = function($elem, widgetOptions, count) { if (count > 1) { $elem.find('div[data-dynamicform]').each(function() { - var currentAttrData = $('body').data(pluginName)[$(this).attr('data-dynamicform')]; + var currentWidgetOptions = eval($(this).attr('data-dynamicform')); var level = _getLevel($(this)); var identifiers = _creatIdentifiers(level); - var numItems = $(this).find(currentAttrData.widgetItem).length; + var numItems = $(this).find(currentWidgetOptions.widgetItem).length; for (var i = 1; i <= numItems -1; i++) { var aux = identifiers; aux[level] = i; - currentAttrData.fields.forEach(function(input) { + currentWidgetOptions.fields.forEach(function(input) { var id = input.id.replace("{}", aux.join('-')); - if ($("#" + currentAttrData.formId).yiiActiveForm("find", id) !== "undefined") { - $("#" + currentAttrData.formId).yiiActiveForm("remove", id); + if ($("#" + currentWidgetOptions.formId).yiiActiveForm("find", id) !== "undefined") { + $("#" + currentWidgetOptions.formId).yiiActiveForm("remove", id); } }); } }); - var level = _getLevel($elem.closest('.' + widgetData.widgetContainer)); - var rootData = _getDataWidgetRoot(widgetData.widgetContainer); + var level = _getLevel($elem.closest('.' + widgetOptions.widgetContainer)); + var widgetOptionsRoot = _getWidgetOptionsRoot(widgetOptions); var identifiers = _creatIdentifiers(level); - identifiers[0] = $(rootData.widgetItem).length - 1; + identifiers[0] = $(widgetOptionsRoot.widgetItem).length - 1; identifiers[level] = count - 1; - widgetData.fields.forEach(function(input) { + widgetOptions.fields.forEach(function(input) { var id = input.id.replace("{}", identifiers.join('-')); - if ($("#" + widgetData.formId).yiiActiveForm("find", id) !== "undefined") { - $("#" + widgetData.formId).yiiActiveForm("remove", id); + if ($("#" + widgetOptions.formId).yiiActiveForm("find", id) !== "undefined") { + $("#" + widgetOptions.formId).yiiActiveForm("remove", id); } }); } }; - var _deleteItem = function(widgetContainer, e, $elem) { - var widgetData = $('body').data(pluginName)[widgetContainer]; - var count = _count($elem, widgetContainer); + var _deleteItem = function(widgetOptions, e, $elem) { + var count = _count($elem, widgetOptions); - if (count > widgetData.min) { - $todelete = $elem.closest(widgetData.widgetItem); + if (count > widgetOptions.min) { + $todelete = $elem.closest(widgetOptions.widgetItem); // trigger a custom event for hooking - var eventResult = $('.' + widgetData.widgetContainer).triggerHandler(events.beforeDelete, $todelete); + var eventResult = $('.' + widgetOptions.widgetContainer).triggerHandler(events.beforeDelete, $todelete); if (eventResult !== false) { - _removeValidations($todelete, widgetData, count); + _removeValidations($todelete, widgetOptions, count); $todelete.remove(); - _updateAttributes(widgetContainer); - _restoreSpecialJs(widgetContainer); - _fixFormValidaton(widgetContainer); - $('.' + widgetData.widgetContainer).triggerHandler(events.afterDelete); + _updateAttributes(widgetOptions); + _restoreSpecialJs(widgetOptions); + _fixFormValidaton(widgetOptions); + $('.' + widgetOptions.widgetContainer).triggerHandler(events.afterDelete); } } }; - var _updateAttrID = function($item, $elem, index, currentAttrData, level) { + var _updateAttrID = function(widgetOptions, $elem, index, level) { var id = $elem.attr('id'); var newID = id; @@ -210,7 +189,7 @@ identifiers[0] = index; if (level > 0 && identifiers[level] !== undefined) { - identifiers[level] = $elem.closest(currentAttrData.widgetItem).index(); + identifiers[level] = $elem.closest(widgetOptions.widgetItem).index(); } newID = matches[1] + '-' + identifiers.join('-') + '-' + matches[3]; @@ -222,17 +201,17 @@ } if (id !== newID) { - $item.find('.field-' + id).each(function() { + $elem.closest(widgetOptions.widgetItem).find('.field-' + id).each(function() { $(this).removeClass('field-' + id).addClass('field-' + newID); }); // update "for" attribute - $elem.closest($item).find("label[for='" + id + "']").attr('for',newID); + $elem.closest(widgetOptions.widgetItem).find("label[for='" + id + "']").attr('for',newID); } return newID; }; - var _updateAttrName = function($item, $elem, index, currentAttrData, level) { + var _updateAttrName = function(widgetOptions, $elem, index, level) { var name = $elem.attr('name'); if (name !== undefined) { @@ -244,7 +223,7 @@ identifiers[0] = index; if (level > 0 && identifiers[level] !== undefined) { - identifiers[level] = $elem.closest(currentAttrData.widgetItem).index(); + identifiers[level] = $elem.closest(widgetOptions.widgetItem).index(); } name = matches[1] + '[' + identifiers.join('][') + ']' + matches[3]; @@ -255,32 +234,31 @@ return name; }; - var _updateAttributes = function(widgetContainer) { - var rootData = _getDataWidgetRoot(widgetContainer); + var _updateAttributes = function(widgetOptions) { + var widgetOptionsRoot = _getWidgetOptionsRoot(widgetOptions); - $(rootData.widgetItem).each(function(index) { + $(widgetOptionsRoot.widgetItem).each(function(index) { var $item = $(this); var level = 0; - var currentAttrData = rootData; + var currentWidgetOptions = widgetOptionsRoot; $(this).find('*').each(function() { if ($(this).attr('data-dynamicform') !== undefined) { - currentAttrData = $('body').data(pluginName)[$(this).attr('data-dynamicform')]; + currentWidgetOptions = eval($(this).attr('data-dynamicform')); level = _getLevel($(this)); } // update "id" attribute - _updateAttrID($item, $(this), index, currentAttrData, level); + _updateAttrID(currentWidgetOptions, $(this), index, level); // update "name" attribute - _updateAttrName($item, $(this), index, currentAttrData, level); + _updateAttrName(currentWidgetOptions, $(this), index, level); }); }); }; - var _fixFormValidatonInput = function(widgetData, input, index, id, name) { + var _fixFormValidatonInput = function(widgetOptions, input, index, id, name) { var attribute = input.baseConfig; - if (attribute !== undefined) { attribute = $.extend(true, {}, attribute); attribute.id = id; @@ -290,57 +268,58 @@ attribute.value = $("#" + id).val(); attribute.status = 0; - if ($("#" + widgetData.formId).yiiActiveForm("find", id) !== "undefined") { - $("#" + widgetData.formId).yiiActiveForm("remove", id); + if ($("#" + widgetOptions.formId).yiiActiveForm("find", id) !== "undefined") { + $("#" + widgetOptions.formId).yiiActiveForm("remove", id); } - $("#" + widgetData.formId).yiiActiveForm("add", attribute); + $("#" + widgetOptions.formId).yiiActiveForm("add", attribute); } }; - var _fixFormValidatonInputs = function(widgetData, level, index, i) { - widgetData.fields.forEach(function(input) { + var _fixFormValidatonInputs = function(widgetOptions, level, index, i) { + widgetOptions.fields.forEach(function(input) { var identifiers = _creatIdentifiers(level); - var yiiActiveFormAttribute = $("#" + widgetData.formId).yiiActiveForm("find", input.id.replace("{}", identifiers.join('-'))); - + var yiiActiveFormAttribute = $("#" + widgetOptions.formId).yiiActiveForm("find", input.id.replace("{}", identifiers.join('-'))); if (yiiActiveFormAttribute !== undefined) { - input.baseConfig = yiiActiveFormAttribute; + input.baseConfig = yiiActiveFormAttribute; identifiers[0] = index; identifiers[level] = i; var id = input.id.replace("{}", identifiers.join('-')); var name = input.name.replace("{}", identifiers.join('][')); - _fixFormValidatonInput(widgetData, input, i, id, name); + + _fixFormValidatonInput(widgetOptions, input, i, id, name); } }); }; - var _fixFormValidaton = function(widgetContainer) { - var rootData = _getDataWidgetRoot(widgetContainer); + var _fixFormValidaton = function(widgetOptions) { + var widgetOptionsRoot = _getWidgetOptionsRoot(widgetOptions); - $(rootData.widgetItem).each(function(index) { + $(widgetOptionsRoot.widgetItem).each(function(index) { var $item = $(this); var level = 0; - var currentAttrData = rootData; - _fixFormValidatonInputs(rootData, level, index, index); + var currentWidgetOptions = widgetOptionsRoot; + _fixFormValidatonInputs(widgetOptionsRoot, level, index, index); $(this).find('div[data-dynamicform]').each(function() { - currentAttrData = $('body').data(pluginName)[$(this).attr('data-dynamicform')]; + currentWidgetOptions = eval($(this).attr('data-dynamicform')); level = _getLevel($(this)); if (level > 0) { - $(this).find(currentAttrData.widgetItem).each(function(i) { - _fixFormValidatonInputs(currentAttrData, level, index, i); + $(this).find(currentWidgetOptions.widgetItem).each(function(i) { + + _fixFormValidatonInputs(currentWidgetOptions, level, index, i); }); } }); }); }; - var _restoreSpecialJs = function(widgetContainer) { - var rootData = _getDataWidgetRoot(widgetContainer); + var _restoreSpecialJs = function(widgetOptions) { + var widgetOptionsRoot = _getWidgetOptionsRoot(widgetOptions); // "kartik-v/yii2-widget-datepicker" - var $hasDatepicker = $(rootData.widgetItem).find('[data-krajee-datepicker]'); + var $hasDatepicker = $(widgetOptionsRoot.widgetItem).find('[data-krajee-datepicker]'); if ($hasDatepicker.length > 0) { $hasDatepicker.each(function() { $(this).parent().removeData().datepicker('remove'); @@ -349,7 +328,7 @@ } // "kartik-v/yii2-widget-timepicker" - var $hasTimepicker = $(rootData.widgetItem).find('[data-krajee-timepicker]'); + var $hasTimepicker = $(widgetOptionsRoot.widgetItem).find('[data-krajee-timepicker]'); if ($hasTimepicker.length > 0) { $hasTimepicker.each(function() { $(this).removeData().off(); @@ -360,7 +339,7 @@ } // "kartik-v/yii2-money" - var $hasMaskmoney = $(rootData.widgetItem).find('[data-krajee-maskMoney]'); + var $hasMaskmoney = $(widgetOptionsRoot.widgetItem).find('[data-krajee-maskMoney]'); if ($hasMaskmoney.length > 0) { $hasMaskmoney.each(function() { $(this).parent().find('input').removeData().off(); @@ -378,7 +357,7 @@ } // "kartik-v/yii2-widget-fileinput" - var $hasFileinput = $(rootData.widgetItem).find('[data-krajee-fileinput]'); + var $hasFileinput = $(widgetOptionsRoot.widgetItem).find('[data-krajee-fileinput]'); if ($hasFileinput.length > 0) { $hasFileinput.each(function() { $(this).fileinput(eval($(this).attr('data-krajee-fileinput'))); @@ -386,7 +365,7 @@ } // "kartik-v/yii2-widget-touchspin" - var $hasTouchSpin = $(rootData.widgetItem).find('[data-krajee-TouchSpin]'); + var $hasTouchSpin = $(widgetOptionsRoot.widgetItem).find('[data-krajee-TouchSpin]'); if ($hasTouchSpin.length > 0) { $hasTouchSpin.each(function() { $(this).TouchSpin('destroy'); @@ -395,7 +374,7 @@ } // "kartik-v/yii2-widget-colorinput" - var $hasSpectrum = $(rootData.widgetItem).find('[data-krajee-spectrum]'); + var $hasSpectrum = $(widgetOptionsRoot.widgetItem).find('[data-krajee-spectrum]'); if ($hasSpectrum.length > 0) { $hasSpectrum.each(function() { var id = '#' + $(this).attr('id'); @@ -417,7 +396,7 @@ } // "kartik-v/yii2-widget-depdrop" - var $hasDepdrop = $(rootData.widgetItem).find('[data-krajee-depdrop]'); + var $hasDepdrop = $(widgetOptionsRoot.widgetItem).find('[data-krajee-depdrop]'); if ($hasDepdrop.length > 0) { $hasDepdrop.each(function() { $(this).removeData().off(); @@ -438,7 +417,7 @@ } // "kartik-v/yii2-widget-select2" - var $hasSelect2 = $(rootData.widgetItem).find('[data-krajee-select2]'); + var $hasSelect2 = $(widgetOptionsRoot.widgetItem).find('[data-krajee-select2]'); if ($hasSelect2.length > 0) { $hasSelect2.each(function() { var id = $(this).attr('id'); diff --git a/assets/yii2-dynamic-form.min.js b/assets/yii2-dynamic-form.min.js index f1622c1..08775da 100644 --- a/assets/yii2-dynamic-form.min.js +++ b/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(){var t=($(this),{});void 0!==$("body").data(pluginName)&&(t=$("body").data(pluginName)),void 0===t[e.widgetContainer]&&(t[e.widgetContainer]=e,t=t,e.template=_parseTemplate(e),$("body").data(pluginName,t),$("#"+e.formId).on("click",e.insertButton,function(t){t.preventDefault(),$("."+e.widgetContainer).triggerHandler(events.beforeInsert,[$(this)]),_addItem(e.widgetContainer,t,$(this))}),$("#"+e.formId).on("click",e.deleteButton,function(t){t.preventDefault(),_deleteItem(e.widgetContainer,t,$(this))}))})},updateContainer:function(){var e=$(this).attr("data-dynamicform");_updateAttributes(e),_restoreSpecialJs(e),_fixFormValidaton(e)}},_parseTemplate=function(e){var t=$(e.template);return t.find("div[data-dynamicform]").each(function(){var e=$("body").data(pluginName)[$(this).attr("data-dynamicform")];if($(e.widgetItem).length>1){var t=$(this).find(e.widgetItem).first()[0].outerHTML;$(this).find(e.widgetBody).html(t)}}),t.find("input, textarea, select").each(function(){$(this).val("")}),t.find('input[type="checkbox"]').each(function(){var e=$(this).attr("name"),i=t.find('input[type="hidden"][name="'+e+'"]').first();i&&($(this).val(1),i.val(0))}),t},_getDataWidgetRoot=function(e){var t=$("body").data(pluginName)[e],i=$(t.widgetBody).parents("div[data-dynamicform]").last().attr("data-dynamicform");return $("body").data(pluginName)[i]},_getLevel=function(e){var t=e.parents("div[data-dynamicform]").length;return t=0>t?0:t},_count=function(e,t){var i=$("body").data(pluginName)[t];return e.closest("."+t).find(i.widgetItem).length},_creatIdentifiers=function(e){return new Array(e+2).join("0").split("")},_addItem=function(e,t,i){var a=$("body").data(pluginName)[e],n=_count(i,e);n1){e.find("div[data-dynamicform]").each(function(){for(var e=$("body").data(pluginName)[$(this).attr("data-dynamicform")],t=_getLevel($(this)),i=_creatIdentifiers(t),a=$(this).find(e.widgetItem).length,n=1;a-1>=n;n++){var r=i;r[t]=n,e.fields.forEach(function(t){var i=t.id.replace("{}",r.join("-"));"undefined"!==$("#"+e.formId).yiiActiveForm("find",i)&&$("#"+e.formId).yiiActiveForm("remove",i)})}});var a=_getLevel(e.closest("."+t.widgetContainer)),n=_getDataWidgetRoot(t.widgetContainer),r=_creatIdentifiers(a);r[0]=$(n.widgetItem).length-1,r[a]=i-1,t.fields.forEach(function(e){var i=e.id.replace("{}",r.join("-"));"undefined"!==$("#"+t.formId).yiiActiveForm("find",i)&&$("#"+t.formId).yiiActiveForm("remove",i)})}},_deleteItem=function(e,t,i){var a=$("body").data(pluginName)[e],n=_count(i,e);if(n>a.min){$todelete=i.closest(a.widgetItem);var r=$("."+a.widgetContainer).triggerHandler(events.beforeDelete,$todelete);r!==!1&&(_removeValidations($todelete,a,n),$todelete.remove(),_updateAttributes(e),_restoreSpecialJs(e),_fixFormValidaton(e),$("."+a.widgetContainer).triggerHandler(events.afterDelete))}},_updateAttrID=function(e,t,i,a,n){var r=t.attr("id"),o=r;if(void 0!==r){var d=r.match(regexID);if(d&&4===d.length){d[2]=d[2].substring(1,d[2].length-1);var c=d[2].split("-");c[0]=i,n>0&&void 0!==c[n]&&(c[n]=t.closest(a.widgetItem).index()),o=d[1]+"-"+c.join("-")+"-"+d[3],t.attr("id",o)}else o=r+i,t.attr("id",o)}return r!==o&&(e.find(".field-"+r).each(function(){$(this).removeClass("field-"+r).addClass("field-"+o)}),t.closest(e).find("label[for='"+r+"']").attr("for",o)),o},_updateAttrName=function(e,t,i,a,n){var r=t.attr("name");if(void 0!==r){var o=r.match(regexName);if(o&&4===o.length){o[2]=o[2].replace(/\]\[/g,"-").replace(/\]|\[/g,"");var d=o[2].split("-");d[0]=i,n>0&&void 0!==d[n]&&(d[n]=t.closest(a.widgetItem).index()),r=o[1]+"["+d.join("][")+"]"+o[3],t.attr("name",r)}}return r},_updateAttributes=function(e){var t=_getDataWidgetRoot(e);$(t.widgetItem).each(function(e){var i=$(this),a=0,n=t;$(this).find("*").each(function(){void 0!==$(this).attr("data-dynamicform")&&(n=$("body").data(pluginName)[$(this).attr("data-dynamicform")],a=_getLevel($(this))),_updateAttrID(i,$(this),e,n,a),_updateAttrName(i,$(this),e,n,a)})})},_fixFormValidatonInput=function(e,t,i,a){var n=t.baseConfig;void 0!==n&&(n=$.extend(!0,{},n),n.id=a,n.container=".field-"+a,n.input="#"+a,n.name=t.name.replace("{}",i),n.value=$("#"+a).val(),n.status=0,"undefined"!==$("#"+e.formId).yiiActiveForm("find",a)&&$("#"+e.formId).yiiActiveForm("remove",a),$("#"+e.formId).yiiActiveForm("add",n))},_fixFormValidatonInputs=function(e,t,i,a){e.fields.forEach(function(n){var r=_creatIdentifiers(t),o=$("#"+e.formId).yiiActiveForm("find",n.id.replace("{}",r.join("-")));if(void 0!==o){n.baseConfig=o,r[0]=i,r[t]=a;var d=n.id.replace("{}",r.join("-")),c=n.name.replace("{}",r.join("]["));_fixFormValidatonInput(e,n,a,d,c)}})},_fixFormValidaton=function(e){var t=_getDataWidgetRoot(e);$(t.widgetItem).each(function(e){var i=($(this),0),a=t;_fixFormValidatonInputs(t,i,e,e),$(this).find("div[data-dynamicform]").each(function(){a=$("body").data(pluginName)[$(this).attr("data-dynamicform")],i=_getLevel($(this)),i>0&&$(this).find(a.widgetItem).each(function(t){_fixFormValidatonInputs(a,i,e,t)})})})},_restoreSpecialJs=function(widgetContainer){var rootData=_getDataWidgetRoot(widgetContainer),$hasDatepicker=$(rootData.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=$(rootData.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=$(rootData.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=$(rootData.widgetItem).find("[data-krajee-fileinput]");$hasFileinput.length>0&&$hasFileinput.each(function(){$(this).fileinput(eval($(this).attr("data-krajee-fileinput")))});var $hasTouchSpin=$(rootData.widgetItem).find("[data-krajee-TouchSpin]");$hasTouchSpin.length>0&&$hasTouchSpin.each(function(){$(this).TouchSpin("destroy"),$(this).TouchSpin(eval($(this).attr("data-krajee-TouchSpin")))});var $hasSpectrum=$(rootData.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=$(rootData.widgetItem).find("[data-krajee-depdrop]");$hasDepdrop.length>0&&$hasDepdrop.each(function(){$(this).removeData().off(),$(this).unbind();var configDepdrop=eval($(this).attr("data-krajee-depdrop")),inputID=$(this).attr("id"),matchID=inputID.match(regex);if(matchID&&4===matchID.length)for(index=0;index0&&$hasSelect2.each(function(){var id=$(this).attr("id"),configSelect2=eval($(this).attr("data-krajee-select2"));$(this).select2("destroy"),$.when($("#"+id).select2(configSelect2)).done(initSelect2Loading(id)),$("#"+id).on("select2-open",function(){initSelect2DropStyle(id)}),$(this).attr("data-krajee-depdrop")&&($(this).on("depdrop.beforeChange",function(e,i,v){var configDepdrop=eval($(this).attr("data-krajee-depdrop")),loadingText=configDepdrop.loadingText?configDepdrop.loadingText:"Loading ...";$("#"+id).select2("data",{text:loadingText})}),$(this).on("depdrop.change",function(){$("#"+id).select2("val",$("#"+id).val())}))})}}(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);return $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(){$(this).val("")}),$template.find('input[type="checkbox"]').each(function(){var e=$(this).attr("name"),t=$template.find('input[type="hidden"][name="'+e+'"]').first();t&&($(this).val(1),t.val(0))}),$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},_creatIdentifiers=function(e){return new Array(e+2).join("0").split("")},_addItem=function(e,t,i){var n=_count(i,e);n1){$elem.find("div[data-dynamicform]").each(function(){for(var currentWidgetOptions=eval($(this).attr("data-dynamicform")),level=_getLevel($(this)),identifiers=_creatIdentifiers(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=_creatIdentifiers(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 n=_count(i,e);if(n>e.min){$todelete=i.closest(e.widgetItem);var a=$("."+e.widgetContainer).triggerHandler(events.beforeDelete,$todelete);a!==!1&&(_removeValidations($todelete,e,n),$todelete.remove(),_updateAttributes(e),_restoreSpecialJs(e),_fixFormValidaton(e),$("."+e.widgetContainer).triggerHandler(events.afterDelete))}},_updateAttrID=function(e,t,i,n){var a=t.attr("id"),o=a;if(void 0!==a){var d=a.match(regexID);if(d&&4===d.length){d[2]=d[2].substring(1,d[2].length-1);var r=d[2].split("-");r[0]=i,n>0&&void 0!==r[n]&&(r[n]=t.closest(e.widgetItem).index()),o=d[1]+"-"+r.join("-")+"-"+d[3],t.attr("id",o)}else o=a+i,t.attr("id",o)}return a!==o&&(t.closest(e.widgetItem).find(".field-"+a).each(function(){$(this).removeClass("field-"+a).addClass("field-"+o)}),t.closest(e.widgetItem).find("label[for='"+a+"']").attr("for",o)),o},_updateAttrName=function(e,t,i,n){var a=t.attr("name");if(void 0!==a){var o=a.match(regexName);if(o&&4===o.length){o[2]=o[2].replace(/\]\[/g,"-").replace(/\]|\[/g,"");var d=o[2].split("-");d[0]=i,n>0&&void 0!==d[n]&&(d[n]=t.closest(e.widgetItem).index()),a=o[1]+"["+d.join("][")+"]"+o[3],t.attr("name",a)}}return a},_updateAttributes=function(widgetOptions){var widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions);$(widgetOptionsRoot.widgetItem).each(function(index){var $item=$(this),level=0,currentWidgetOptions=widgetOptionsRoot;$(this).find("*").each(function(){void 0!==$(this).attr("data-dynamicform")&&(currentWidgetOptions=eval($(this).attr("data-dynamicform")),level=_getLevel($(this))),_updateAttrID(currentWidgetOptions,$(this),index,level),_updateAttrName(currentWidgetOptions,$(this),index,level)})})},_fixFormValidatonInput=function(e,t,i,n){var a=t.baseConfig;void 0!==a&&(a=$.extend(!0,{},a),a.id=n,a.container=".field-"+n,a.input="#"+n,a.name=t.name.replace("{}",i),a.value=$("#"+n).val(),a.status=0,"undefined"!==$("#"+e.formId).yiiActiveForm("find",n)&&$("#"+e.formId).yiiActiveForm("remove",n),$("#"+e.formId).yiiActiveForm("add",a))},_fixFormValidatonInputs=function(e,t,i,n){e.fields.forEach(function(a){var o=_creatIdentifiers(t),d=$("#"+e.formId).yiiActiveForm("find",a.id.replace("{}",o.join("-")));if(void 0!==d){a.baseConfig=d,o[0]=i,o[t]=n;var r=a.id.replace("{}",o.join("-")),s=a.name.replace("{}",o.join("]["));_fixFormValidatonInput(e,a,n,r,s)}})},_fixFormValidaton=function(widgetOptions){var widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions);$(widgetOptionsRoot.widgetItem).each(function(index){var $item=$(this),level=0,currentWidgetOptions=widgetOptionsRoot;_fixFormValidatonInputs(widgetOptionsRoot,level,index,index),$(this).find("div[data-dynamicform]").each(function(){currentWidgetOptions=eval($(this).attr("data-dynamicform")),level=_getLevel($(this)),level>0&&$(this).find(currentWidgetOptions.widgetItem).each(function(e){_fixFormValidatonInputs(currentWidgetOptions,level,index,e)})})})},_restoreSpecialJs=function(widgetOptions){var widgetOptionsRoot=_getWidgetOptionsRoot(widgetOptions),$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(){$(this).removeData().off(),$(this).unbind();var configDepdrop=eval($(this).attr("data-krajee-depdrop")),inputID=$(this).attr("id"),matchID=inputID.match(regex);if(matchID&&4===matchID.length)for(index=0;index0&&$hasSelect2.each(function(){var id=$(this).attr("id"),configSelect2=eval($(this).attr("data-krajee-select2"));$(this).select2("destroy"),$.when($("#"+id).select2(configSelect2)).done(initSelect2Loading(id)),$("#"+id).on("select2-open",function(){initSelect2DropStyle(id)}),$(this).attr("data-krajee-depdrop")&&($(this).on("depdrop.beforeChange",function(e,i,v){var configDepdrop=eval($(this).attr("data-krajee-depdrop")),loadingText=configDepdrop.loadingText?configDepdrop.loadingText:"Loading ...";$("#"+id).select2("data",{text:loadingText})}),$(this).on("depdrop.change",function(){$("#"+id).select2("val",$("#"+id).val())}))})}}(window.jQuery);