From 2f500ab034d57f5ec08777a7f51ffb89bfd11d0f Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Thu, 5 Mar 2020 16:37:16 +0800 Subject: [PATCH] Auto-detect correct postback handler for datatable widget. (#4967) This change removes the default postback handler for the datatable widget (onSave) and instead relies on the closest form's request handler. While `onSave` was a safe default, it did not take into account the relation widget using other handlers. The table widget will use an explicitly specified handler first, then it will try and use the form's request handler, before using `onSave` as a fallback, although this should really never occur. Fixes #4776. --- modules/backend/widgets/Table.php | 14 ++++++++++++-- .../backend/widgets/table/assets/js/build-min.js | 7 ++++--- modules/backend/widgets/table/assets/js/table.js | 9 +++++++-- modules/backend/widgets/table/partials/_table.htm | 2 ++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/modules/backend/widgets/Table.php b/modules/backend/widgets/Table.php index 18ff4dbcf..198f93295 100644 --- a/modules/backend/widgets/Table.php +++ b/modules/backend/widgets/Table.php @@ -1,5 +1,7 @@ vars['recordsKeyFrom'] = $this->recordsKeyFrom; $this->vars['recordsPerPage'] = $this->getConfig('recordsPerPage', false) ?: 'false'; - $this->vars['postbackHandlerName'] = $this->getConfig('postbackHandlerName', 'onSave'); + $this->vars['postbackHandlerName'] = $this->getConfig('postbackHandlerName'); $this->vars['searching'] = $this->getConfig('searching', false); $this->vars['adding'] = $this->getConfig('adding', true); $this->vars['deleting'] = $this->getConfig('deleting', true); @@ -153,7 +155,15 @@ class Table extends WidgetBase protected function loadAssets() { $this->addCss('css/table.css', 'core'); - $this->addJs('js/build-min.js', 'core'); + + if (Config::get('develop.decompileBackendAssets', false)) { + $scripts = Backend::decompileAsset($this->getAssetPath('js/build.js')); + foreach ($scripts as $script) { + $this->addJs($script, 'core'); + } + } else { + $this->addJs('js/build-min.js', 'core'); + } } /** diff --git a/modules/backend/widgets/table/assets/js/build-min.js b/modules/backend/widgets/table/assets/js/build-min.js index 8c8a8637b..45068e70a 100644 --- a/modules/backend/widgets/table/assets/js/build-min.js +++ b/modules/backend/widgets/table/assets/js/build-min.js @@ -21,8 +21,9 @@ this.clickHandler=this.onClick.bind(this) this.keydownHandler=this.onKeydown.bind(this) this.documentClickHandler=this.onDocumentClick.bind(this) this.toolbarClickHandler=this.onToolbarClick.bind(this) -if(this.options.postback&&this.options.clientDataSourceClass=='client') -this.formSubmitHandler=this.onFormSubmit.bind(this) +if(this.options.postback&&this.options.clientDataSourceClass=='client'){if(!this.options.postbackHandlerName){var formHandler=this.$el.closest('form').data('request') +this.options.postbackHandlerName=formHandler||'onSave'} +this.formSubmitHandler=this.onFormSubmit.bind(this)} this.navigation=null this.search=null this.recordsAddedOrDeleted=0 @@ -383,7 +384,7 @@ if(dataContainer.value!=value){dataContainer.value=value this.markCellRowDirty(cellElement) this.notifyRowProcessorsOnChange(cellElement) if(suppressEvents===undefined||!suppressEvents){this.$el.trigger('oc.tableCellChanged',[this.getCellColumnName(cellElement),value,this.getCellRowIndex(cellElement)])}}} -Table.DEFAULTS={clientDataSourceClass:'client',keyColumn:'id',recordsPerPage:false,data:null,postback:true,postbackHandlerName:'onSave',adding:true,deleting:true,toolbar:true,searching:false,rowSorting:false,height:false,dynamicHeight:false} +Table.DEFAULTS={clientDataSourceClass:'client',keyColumn:'id',recordsPerPage:false,data:null,postback:true,postbackHandlerName:null,adding:true,deleting:true,toolbar:true,searching:false,rowSorting:false,height:false,dynamicHeight:false} var old=$.fn.table $.fn.table=function(option){var args=Array.prototype.slice.call(arguments,1),result=undefined this.each(function(){var $this=$(this) diff --git a/modules/backend/widgets/table/assets/js/table.js b/modules/backend/widgets/table/assets/js/table.js index fb83fb515..f20bd08e6 100644 --- a/modules/backend/widgets/table/assets/js/table.js +++ b/modules/backend/widgets/table/assets/js/table.js @@ -69,8 +69,13 @@ this.documentClickHandler = this.onDocumentClick.bind(this) this.toolbarClickHandler = this.onToolbarClick.bind(this) - if (this.options.postback && this.options.clientDataSourceClass == 'client') + if (this.options.postback && this.options.clientDataSourceClass == 'client') { + if (!this.options.postbackHandlerName) { + var formHandler = this.$el.closest('form').data('request') + this.options.postbackHandlerName = formHandler || 'onSave' + } this.formSubmitHandler = this.onFormSubmit.bind(this) + } // Navigation helper this.navigation = null @@ -1112,7 +1117,7 @@ recordsPerPage: false, data: null, postback: true, - postbackHandlerName: 'onSave', + postbackHandlerName: null, adding: true, deleting: true, toolbar: true, diff --git a/modules/backend/widgets/table/partials/_table.htm b/modules/backend/widgets/table/partials/_table.htm index d7eedacce..54fb0205b 100644 --- a/modules/backend/widgets/table/partials/_table.htm +++ b/modules/backend/widgets/table/partials/_table.htm @@ -6,7 +6,9 @@ data-data="" data-alias="alias) ?>" data-field-name="fieldName) ?>" + data-postback-handler-name="" + data-adding="" data-searching="" data-deleting=""