Matteo Scandolo | 8b55d9f | 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 | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 31 | <pre>{{vm.responseErr}}</pre> |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 32 | <xos-table config="vm.tableConfig" data="vm.data"></xos-table> |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 33 | <div class="panel panel-default" ng-show="vm.detailedItem"> |
| 34 | <div class="panel-heading"> |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame^] | 35 | <div class="row"> |
| 36 | <div class="col-xs-11"> |
| 37 | <h3 class="panel-title">Update {{vm.config.resource}} {{vm.detailedItem.id}}</h3> |
| 38 | </div> |
| 39 | <div class="col-xs-1"> |
| 40 | <a href="" ng-click="vm.cleanForm()"> |
| 41 | <i class="glyphicon glyphicon-remove pull-right"></i> |
| 42 | </a> |
| 43 | </div> |
| 44 | </div> |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 45 | </div> |
| 46 | <div class="panel-body"> |
| 47 | <xos-form config="vm.formConfig" ng-model="vm.detailedItem"></xos-form> |
| 48 | </div> |
| 49 | </div> |
| 50 | <xos-alert config="{type: 'success', closeBtn: true}" show="vm.responseMsg">{{vm.responseMsg}}</xos-alert> |
| 51 | <xos-alert config="{type: 'danger', closeBtn: true}" show="vm.responseErr">{{vm.responseErr}}</xos-alert> |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 52 | `, |
| 53 | bindToController: true, |
| 54 | controllerAs: 'vm', |
| 55 | controller: function($injector, LabelFormatter, _){ |
| 56 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 57 | this.responseMsg = false; |
| 58 | this.responseErr = false; |
| 59 | |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 60 | this.tableConfig = { |
| 61 | columns: [ |
| 62 | ], |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 63 | actions: [ |
| 64 | { |
| 65 | label: 'delete', |
| 66 | icon: 'remove', |
| 67 | cb: (item) => { |
| 68 | this.Resource.delete({id: item.id}).$promise |
| 69 | .then(() => { |
| 70 | console.log(this.config.resource); |
| 71 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully deleted`; |
| 72 | }) |
| 73 | .catch(err => { |
| 74 | this.responseErr = err.data.detail || `Error while deleting ${this.config.resource} with id ${item.id}`; |
| 75 | }); |
| 76 | }, |
| 77 | color: 'red' |
| 78 | }, |
| 79 | { |
| 80 | label: 'details', |
| 81 | icon: 'search', |
| 82 | cb: (item) => { |
| 83 | this.detailedItem = item; |
| 84 | } |
| 85 | } |
| 86 | ], |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 87 | classes: 'table table-striped table-bordered table-responsive', |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 88 | filter: 'field', |
| 89 | order: true, |
| 90 | pagination: { |
| 91 | pageSize: 10 |
| 92 | } |
| 93 | }; |
| 94 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 95 | this.formConfig = { |
| 96 | exclude: this.config.hiddenFields, |
| 97 | formName: `${this.config.resource}Form`, |
| 98 | actions: [ |
| 99 | { |
| 100 | label: 'Save', |
| 101 | icon: 'ok', |
| 102 | cb: (item) => { |
| 103 | item.$save() |
| 104 | .then(() => { |
| 105 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully saved`; |
| 106 | }) |
| 107 | .catch((err) => { |
| 108 | this.responseErr = err.data.detail || `Error while saving ${this.config.resource} with id ${item.id}`; |
| 109 | }) |
| 110 | }, |
| 111 | class: 'success' |
| 112 | } |
| 113 | ] |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame^] | 114 | }; |
| 115 | |
| 116 | this.cleanForm = () => { |
| 117 | delete this.detailedItem; |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 120 | this.Resource = $injector.get(this.config.resource); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 121 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 122 | this.Resource.query().$promise |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 123 | .then((res) => { |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 124 | |
| 125 | if(!res[0]){ |
| 126 | return; |
| 127 | } |
| 128 | |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 129 | let props = Object.keys(res[0]); |
| 130 | |
| 131 | _.remove(props, p => { |
| 132 | return p == 'id' || p == 'password' || p == 'validators' |
| 133 | }); |
| 134 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 135 | // TODO move out cb |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 136 | if(angular.isArray(this.config.hiddenFields)){ |
| 137 | props = _.difference(props, this.config.hiddenFields) |
| 138 | } |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 139 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 140 | let labels = props.map(p => LabelFormatter.format(p)); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 141 | |
| 142 | props.forEach((p, i) => { |
| 143 | this.tableConfig.columns.push({ |
| 144 | label: labels[i], |
| 145 | prop: p |
| 146 | }); |
| 147 | }); |
| 148 | |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 149 | this.data = res; |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame^] | 150 | }); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 151 | } |
| 152 | }; |
| 153 | }); |
| 154 | })(); |