Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Visit http://guide.xosproject.org/devguide/addview/ for more information |
| 5 | * |
| 6 | * Created by teone on 3/24/16. |
| 7 | */ |
| 8 | |
| 9 | (function () { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular.module('xos.uiComponents') |
| 13 | |
| 14 | /** |
| 15 | * @ngdoc directive |
| 16 | * @name xos.uiComponents.directive:xosSmartTable |
| 17 | * @restrict E |
| 18 | * @description The xos-table directive |
| 19 | * @param {Object} config The configuration for the component. |
| 20 | * @scope |
| 21 | * @example |
| 22 | */ |
| 23 | |
| 24 | .directive('xosSmartTable', function(){ |
| 25 | return { |
| 26 | restrict: 'E', |
| 27 | scope: { |
| 28 | config: '=' |
| 29 | }, |
| 30 | template: ` |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame^] | 31 | <pre>{{vm.responseErr}}</pre> |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 32 | <xos-table config="vm.tableConfig" data="vm.data"></xos-table> |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame^] | 33 | <div class="panel panel-default" ng-show="vm.detailedItem"> |
| 34 | <div class="panel-heading"> |
| 35 | <h3 class="panel-title">Update {{vm.config.resource}} {{vm.detailedItem.id}}</h3> |
| 36 | </div> |
| 37 | <div class="panel-body"> |
| 38 | <xos-form config="vm.formConfig" ng-model="vm.detailedItem"></xos-form> |
| 39 | </div> |
| 40 | </div> |
| 41 | <xos-alert config="{type: 'success', closeBtn: true}" show="vm.responseMsg">{{vm.responseMsg}}</xos-alert> |
| 42 | <xos-alert config="{type: 'danger', closeBtn: true}" show="vm.responseErr">{{vm.responseErr}}</xos-alert> |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 43 | `, |
| 44 | bindToController: true, |
| 45 | controllerAs: 'vm', |
| 46 | controller: function($injector, LabelFormatter, _){ |
| 47 | |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame^] | 48 | this.responseMsg = false; |
| 49 | this.responseErr = false; |
| 50 | |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 51 | this.tableConfig = { |
| 52 | columns: [ |
| 53 | ], |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame^] | 54 | actions: [ |
| 55 | { |
| 56 | label: 'delete', |
| 57 | icon: 'remove', |
| 58 | cb: (item) => { |
| 59 | this.Resource.delete({id: item.id}).$promise |
| 60 | .then(() => { |
| 61 | console.log(this.config.resource); |
| 62 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully deleted`; |
| 63 | }) |
| 64 | .catch(err => { |
| 65 | this.responseErr = err.data.detail || `Error while deleting ${this.config.resource} with id ${item.id}`; |
| 66 | }); |
| 67 | }, |
| 68 | color: 'red' |
| 69 | }, |
| 70 | { |
| 71 | label: 'details', |
| 72 | icon: 'search', |
| 73 | cb: (item) => { |
| 74 | this.detailedItem = item; |
| 75 | } |
| 76 | } |
| 77 | ], |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 78 | classes: 'table table-striped table-bordered table-responsive', |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 79 | filter: 'field', |
| 80 | order: true, |
| 81 | pagination: { |
| 82 | pageSize: 10 |
| 83 | } |
| 84 | }; |
| 85 | |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame^] | 86 | this.formConfig = { |
| 87 | exclude: this.config.hiddenFields, |
| 88 | formName: `${this.config.resource}Form`, |
| 89 | actions: [ |
| 90 | { |
| 91 | label: 'Save', |
| 92 | icon: 'ok', |
| 93 | cb: (item) => { |
| 94 | item.$save() |
| 95 | .then(() => { |
| 96 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully saved`; |
| 97 | }) |
| 98 | .catch((err) => { |
| 99 | this.responseErr = err.data.detail || `Error while saving ${this.config.resource} with id ${item.id}`; |
| 100 | }) |
| 101 | }, |
| 102 | class: 'success' |
| 103 | } |
| 104 | ] |
| 105 | } |
| 106 | |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 107 | this.Resource = $injector.get(this.config.resource); |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 108 | |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 109 | this.Resource.query().$promise |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 110 | .then((res) => { |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 111 | |
| 112 | if(!res[0]){ |
| 113 | return; |
| 114 | } |
| 115 | |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 116 | let props = Object.keys(res[0]); |
| 117 | |
| 118 | _.remove(props, p => { |
| 119 | return p == 'id' || p == 'password' || p == 'validators' |
| 120 | }); |
| 121 | |
Matteo Scandolo | 3fa9871 | 2016-04-26 11:44:36 -0700 | [diff] [blame^] | 122 | // TODO move out cb |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 123 | if(angular.isArray(this.config.hiddenFields)){ |
| 124 | props = _.difference(props, this.config.hiddenFields) |
| 125 | } |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 126 | |
Matteo Scandolo | 3a55ad6 | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 127 | let labels = props.map(p => LabelFormatter.format(p)); |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 128 | |
| 129 | props.forEach((p, i) => { |
| 130 | this.tableConfig.columns.push({ |
| 131 | label: labels[i], |
| 132 | prop: p |
| 133 | }); |
| 134 | }); |
| 135 | |
Matteo Scandolo | ead6fa5 | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 136 | this.data = res; |
| 137 | }) |
| 138 | } |
| 139 | }; |
| 140 | }); |
| 141 | })(); |