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