diff --git a/modules/backend/formwidgets/Repeater.php b/modules/backend/formwidgets/Repeater.php index f67252842..70205ff15 100644 --- a/modules/backend/formwidgets/Repeater.php +++ b/modules/backend/formwidgets/Repeater.php @@ -160,7 +160,7 @@ class Repeater extends FormWidgetBase public function onRefresh() { - $index = post('_index'); + $index = post('_repeater_index'); $widget = $this->makeItemFormWidget($index); diff --git a/modules/backend/formwidgets/repeater/partials/_repeater.htm b/modules/backend/formwidgets/repeater/partials/_repeater.htm index 78f63aac3..59cba2994 100644 --- a/modules/backend/formwidgets/repeater/partials/_repeater.htm +++ b/modules/backend/formwidgets/repeater/partials/_repeater.htm @@ -15,12 +15,10 @@
+ data-load-indicator>
- diff --git a/modules/backend/formwidgets/repeater/partials/_repeater_item.htm b/modules/backend/formwidgets/repeater/partials/_repeater_item.htm index 11c64e406..063b9f7c9 100644 --- a/modules/backend/formwidgets/repeater/partials/_repeater_item.htm +++ b/modules/backend/formwidgets/repeater/partials/_repeater_item.htm @@ -20,7 +20,7 @@
+ data-refresh-data="'_repeater_index': ''"> getFields() as $field): ?> renderField($field) ?> diff --git a/modules/backend/widgets/form/assets/js/october.form.js b/modules/backend/widgets/form/assets/js/october.form.js index 7b46b581b..e33f848df 100644 --- a/modules/backend/widgets/form/assets/js/october.form.js +++ b/modules/backend/widgets/form/assets/js/october.form.js @@ -11,6 +11,7 @@ var FormWidget = function (element, options) { this.$el = $(element) this.options = options || {} + this.fieldElementCache = null /* * Throttle dependency updating @@ -44,7 +45,9 @@ this.$el.removeData('oc.formwidget') this.$el = null + this.$form = null this.options = null + this.fieldElementCache = null BaseProto.dispose.call(this) } @@ -71,25 +74,45 @@ } + /* + * Get all fields elements that belong to this form, nested form + * fields are removed from this collection. + */ + FormWidget.prototype.getFieldElements = function() { + if (this.fieldElementCache !== null) { + return this.fieldElementCache + } + + var form = this.$el, + nestedFields = form.find('[data-control="formwidget"] [data-field-name]') + + return this.fieldElementCache = form.find('[data-field-name]').not(nestedFields) + } + /* * Bind dependant fields */ FormWidget.prototype.bindDependants = function() { + + if (!$('[data-field-depends]', this.$el).length) { + return; + } + var self = this, - form = this.$el, fieldMap = {}, - nestedDependencies = form.find('[data-control="formwidget"] [data-field-depends]'); + fieldElements = this.getFieldElements() /* * Map master and slave fields */ - form.find('[data-field-depends]').not(nestedDependencies).each(function() { + fieldElements.filter('[data-field-depends]').each(function() { var name = $(this).data('field-name'), depends = $(this).data('field-depends') $.each(depends, function(index, depend){ - if (!fieldMap[depend]) + if (!fieldMap[depend]) { fieldMap[depend] = { fields: [] } + } fieldMap[depend].fields.push(name) }) @@ -99,10 +122,7 @@ * When a master is updated, refresh its slaves */ $.each(fieldMap, function(fieldName, toRefresh){ - form - .find('[data-field-name="'+fieldName+'"]') - // Exclude nested formwidget elements - .not(form.find('[data-control="formwidget"] [data-field-name="'+fieldName+'"]')) + fieldElements.filter('[data-field-name="'+fieldName+'"]') .on('change.oc.formwidget', $.proxy(self.onRefreshDependants, self, fieldName, toRefresh)) }) } @@ -114,24 +134,28 @@ FormWidget.prototype.onRefreshDependants = function(fieldName, toRefresh) { var self = this, form = this.$el, - formEl = this.$form + formEl = this.$form, + fieldElements = this.getFieldElements() if (this.dependantUpdateTimers[fieldName] !== undefined) { window.clearTimeout(this.dependantUpdateTimers[fieldName]) } this.dependantUpdateTimers[fieldName] = window.setTimeout(function() { + var refreshData = $.extend({}, + toRefresh, + paramToObj('data-refresh-data', self.options.refreshData) + ) + formEl.request(self.options.refreshHandler, { - data: $.extend({}, toRefresh, form.data('refresh-data')) + data: refreshData }).success(function() { self.toggleEmptyTabs() }) }, this.dependantUpdateInterval) $.each(toRefresh.fields, function(index, field) { - form.find('[data-field-name="'+field+'"]:visible') - // Exclude nested formwidget elements - .not(form.find('[data-control="formwidget"] [data-field-name="'+field+'"]:visible')) + fieldElements.filter('[data-field-name="'+field+'"]:visible') .addClass('loading-indicator-container size-form-field') .loadIndicator() }) @@ -197,7 +221,8 @@ } FormWidget.DEFAULTS = { - refreshHandler: null + refreshHandler: null, + refreshData: {} } // FORM WIDGET PLUGIN DEFINITION @@ -234,6 +259,18 @@ // FORM WIDGET DATA-API // ============== + function paramToObj(name, value) { + if (value === undefined) value = '' + if (typeof value == 'object') return value + + try { + return JSON.parse(JSON.stringify(eval("({" + value + "})"))) + } + catch (e) { + throw new Error('Error parsing the '+name+' attribute value. '+e) + } + } + $(document).render(function() { $('[data-control="formwidget"]').formWidget(); })