From 71eaa53dae5174a1c454d0b55df583339e6d9eb0 Mon Sep 17 00:00:00 2001 From: Daniel Herrero Date: Thu, 8 Feb 2018 12:04:17 +0100 Subject: [PATCH] generic class to list models into view with mg-crud --- client/client/src/credit-list/credit-list.js | 4 +- client/client/src/filterClientList.js | 40 ++------------------ client/client/src/greuge-list/greuge-list.js | 4 +- client/core/src/lib/filterList.js | 40 ++++++++++++++++++++ client/core/src/lib/index.js | 1 + client/item/src/filterItemList.js | 8 ++++ 6 files changed, 55 insertions(+), 42 deletions(-) create mode 100644 client/core/src/lib/filterList.js create mode 100644 client/item/src/filterItemList.js diff --git a/client/client/src/credit-list/credit-list.js b/client/client/src/credit-list/credit-list.js index c1d283e33..613a6eb30 100644 --- a/client/client/src/credit-list/credit-list.js +++ b/client/client/src/credit-list/credit-list.js @@ -1,9 +1,7 @@ import ngModule from '../module'; import FilterClientList from '../filterClientList'; -class ClientCreditList extends FilterClientList {} - ngModule.component('vnClientCreditList', { template: require('./credit-list.html'), - controller: ClientCreditList + controller: FilterClientList }); diff --git a/client/client/src/filterClientList.js b/client/client/src/filterClientList.js index 6b6f48bb9..e4946354a 100644 --- a/client/client/src/filterClientList.js +++ b/client/client/src/filterClientList.js @@ -1,40 +1,8 @@ -// Generic object to list models, related to the client, with mgCrud -export default class FilterClientList { +import FilterList from '../../core/src/lib/filterList'; +export default class FilterClientList extends FilterList { constructor($scope, $timeout, $state) { - this.$ = $scope; - this.$timeout = $timeout; - this.$state = $state; - - this.waitingMgCrud = 0; - this.clientFk = $state.params.id; - } - onOrder(field, order) { - this.filter(`${field} ${order}`); - } - filter(order) { - if (this.$.index && this.clientFk) { - this.waitingMgCrud = 0; - this.$.index.filter = { - page: 1, - size: 10, - clientFk: this.clientFk - }; - - if (order) { - this.$.index.filter.order = order; - } - - this.$.index.accept(); - } else if (!this.clientFk) { - throw new Error('Error: ClientFk not found'); - } else if (this.waitingMgCrud > 3) { - throw new Error('Error: Magic Crud is not loaded'); - } else { - this.waitingMgCrud++; - this.$timeout(() => { - this.filter(order); - }, 250); - } + super($scope, $timeout, $state); + this.modelName = 'clientFk'; } } FilterClientList.$inject = ['$scope', '$timeout', '$state']; diff --git a/client/client/src/greuge-list/greuge-list.js b/client/client/src/greuge-list/greuge-list.js index a2e5bd33a..0e91db0c7 100644 --- a/client/client/src/greuge-list/greuge-list.js +++ b/client/client/src/greuge-list/greuge-list.js @@ -1,9 +1,7 @@ import ngModule from '../module'; import FilterClientList from '../filterClientList'; -class ClientGreugeList extends FilterClientList {} - ngModule.component('vnClientGreugeList', { template: require('./greuge-list.html'), - controller: ClientGreugeList + controller: FilterClientList }); diff --git a/client/core/src/lib/filterList.js b/client/core/src/lib/filterList.js new file mode 100644 index 000000000..34789a41d --- /dev/null +++ b/client/core/src/lib/filterList.js @@ -0,0 +1,40 @@ +// Generic object to list models +export default class FilterList { + constructor($scope, $timeout, $state) { + this.$ = $scope; + this.$timeout = $timeout; + this.$state = $state; + + this.waitingMgCrud = 0; + this.modelId = $state.params.id; + } + onOrder(field, order) { + this.filter(`${field} ${order}`); + } + filter(order) { + if (this.$.index && this.modelId && this.modelName) { + this.waitingMgCrud = 0; + this.$.index.filter = { + page: 1, + size: 10 + }; + + this.$.index.filter[this.modelName] = this.modelId; + + if (order) { + this.$.index.filter.order = order; + } + + this.$.index.accept(); + } else if (!this.modelId || !this.modelName) { + throw new Error('Error: model not found'); + } else if (this.waitingMgCrud > 3) { + throw new Error('Error: Magic Crud is not loaded'); + } else { + this.waitingMgCrud++; + this.$timeout(() => { + this.filter(order); + }, 250); + } + } +} diff --git a/client/core/src/lib/index.js b/client/core/src/lib/index.js index 46c4e4882..0c8d89c9c 100644 --- a/client/core/src/lib/index.js +++ b/client/core/src/lib/index.js @@ -6,6 +6,7 @@ import './app'; import './interceptor'; import './aclService'; import './storageServices'; +import './filterList'; export * from './util'; export {default as splitingRegister} from './splitingRegister'; diff --git a/client/item/src/filterItemList.js b/client/item/src/filterItemList.js new file mode 100644 index 000000000..6c084944d --- /dev/null +++ b/client/item/src/filterItemList.js @@ -0,0 +1,8 @@ +import FilterList from '../../core/src/lib/filterList'; +export default class FilterItemList extends FilterList { + constructor($scope, $timeout, $state) { + super($scope, $timeout, $state); + this.modelName = 'itemFk'; + } +} +FilterItemList.$inject = ['$scope', '$timeout', '$state'];