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 |
Matteo Scandolo | 71378f9 | 2016-04-28 14:16:45 -0700 | [diff] [blame] | 17 | * @link xos.uiComponents.directive:xosTable xosTable |
| 18 | * @link xos.uiComponents.directive:xosForm xosForm |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 19 | * @restrict E |
| 20 | * @description The xos-table directive |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 21 | * @param {Object} config The configuration for the component, |
| 22 | * it is composed by the name of an angular [$resource](https://docs.angularjs.org/api/ngResource/service/$resource) |
| 23 | * and an array of fields that shouldn't be printed. |
| 24 | * ``` |
| 25 | * { |
| 26 | resource: 'Users', |
| 27 | hiddenFields: [] |
| 28 | } |
| 29 | * ``` |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 30 | * @scope |
| 31 | * @example |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 32 | |
| 33 | <example module="sampleSmartTable"> |
| 34 | <file name="index.html"> |
| 35 | <div ng-controller="SampleCtrl as vm"> |
| 36 | <xos-smart-table config="vm.config"></xos-smart-table> |
| 37 | </div> |
| 38 | </file> |
| 39 | <file name="script.js"> |
| 40 | angular.module('sampleSmartTable', ['xos.uiComponents', 'ngResource', 'ngMockE2E']) |
| 41 | // This is only for documentation purpose |
| 42 | .run(function($httpBackend, _){ |
| 43 | let datas = [{id: 1, name: 'Jhon', surname: 'Doe'}]; |
| 44 | let count = 1; |
| 45 | |
| 46 | let paramsUrl = new RegExp(/\/test\/(.+)/); |
| 47 | |
| 48 | $httpBackend.whenDELETE(paramsUrl, undefined, ['id']).respond((method, url, data, headers, params) => { |
| 49 | data = angular.fromJson(data); |
| 50 | let id = url.match(paramsUrl)[1]; |
| 51 | _.remove(datas, (d) => { |
| 52 | return d.id === parseInt(id); |
| 53 | }) |
| 54 | return [204]; |
| 55 | }); |
| 56 | |
| 57 | $httpBackend.whenGET('/test').respond(200, datas) |
| 58 | $httpBackend.whenPOST('/test').respond((method, url, data) => { |
| 59 | data = angular.fromJson(data); |
| 60 | data.id = ++count; |
| 61 | datas.push(data); |
| 62 | return [201, data, {}]; |
| 63 | }); |
| 64 | }) |
| 65 | .factory('_', function($window){ |
| 66 | return $window._; |
| 67 | }) |
| 68 | .service('SampleResource', function($resource){ |
| 69 | return $resource('/test/:id', {id: '@id'}); |
| 70 | }) |
| 71 | // End of documentation purpose, example start |
| 72 | .controller('SampleCtrl', function(){ |
| 73 | this.config = { |
| 74 | resource: 'SampleResource' |
| 75 | }; |
| 76 | }); |
| 77 | </file> |
| 78 | </example> |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 79 | */ |
| 80 | |
| 81 | .directive('xosSmartTable', function(){ |
| 82 | return { |
| 83 | restrict: 'E', |
| 84 | scope: { |
| 85 | config: '=' |
| 86 | }, |
| 87 | template: ` |
Matteo Scandolo | c3c697a | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 88 | <div class="row" ng-show="vm.data.length > 0"> |
| 89 | <div class="col-xs-12 text-right"> |
| 90 | <a href="" class="btn btn-success" ng-click="vm.createItem()"> |
| 91 | Add |
| 92 | </a> |
| 93 | </div> |
| 94 | </div> |
Matteo Scandolo | e2ee2d9 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 95 | <div class="row"> |
| 96 | <div class="col-xs-12 table-responsive"> |
| 97 | <xos-table config="vm.tableConfig" data="vm.data"></xos-table> |
| 98 | </div> |
| 99 | </div> |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 100 | <div class="panel panel-default" ng-show="vm.detailedItem"> |
| 101 | <div class="panel-heading"> |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 102 | <div class="row"> |
| 103 | <div class="col-xs-11"> |
Matteo Scandolo | e2ee2d9 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 104 | <h3 class="panel-title" ng-show="vm.detailedItem.id">Update {{vm.config.resource}} {{vm.detailedItem.id}}</h3> |
| 105 | <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] | 106 | </div> |
| 107 | <div class="col-xs-1"> |
| 108 | <a href="" ng-click="vm.cleanForm()"> |
| 109 | <i class="glyphicon glyphicon-remove pull-right"></i> |
| 110 | </a> |
| 111 | </div> |
| 112 | </div> |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 113 | </div> |
| 114 | <div class="panel-body"> |
| 115 | <xos-form config="vm.formConfig" ng-model="vm.detailedItem"></xos-form> |
| 116 | </div> |
| 117 | </div> |
| 118 | <xos-alert config="{type: 'success', closeBtn: true}" show="vm.responseMsg">{{vm.responseMsg}}</xos-alert> |
| 119 | <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] | 120 | `, |
| 121 | bindToController: true, |
| 122 | controllerAs: 'vm', |
Matteo Scandolo | e2ee2d9 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 123 | controller: function($injector, LabelFormatter, _, XosFormHelpers){ |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 124 | |
Matteo Scandolo | 67e79cb | 2016-04-29 09:14:58 -0700 | [diff] [blame] | 125 | // TODO |
| 126 | // - Validate the config (what if resource does not exist?) |
| 127 | |
Matteo Scandolo | c3c697a | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 128 | // NOTE |
| 129 | // Corner case |
| 130 | // - if response is empty, how can we generate a form ? |
| 131 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 132 | this.responseMsg = false; |
| 133 | this.responseErr = false; |
| 134 | |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 135 | this.tableConfig = { |
| 136 | columns: [ |
| 137 | ], |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 138 | actions: [ |
| 139 | { |
| 140 | label: 'delete', |
| 141 | icon: 'remove', |
| 142 | cb: (item) => { |
| 143 | this.Resource.delete({id: item.id}).$promise |
| 144 | .then(() => { |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 145 | _.remove(this.data, (d) => d.id === item.id); |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 146 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully deleted`; |
| 147 | }) |
| 148 | .catch(err => { |
| 149 | this.responseErr = err.data.detail || `Error while deleting ${this.config.resource} with id ${item.id}`; |
| 150 | }); |
| 151 | }, |
| 152 | color: 'red' |
| 153 | }, |
| 154 | { |
| 155 | label: 'details', |
| 156 | icon: 'search', |
| 157 | cb: (item) => { |
| 158 | this.detailedItem = item; |
| 159 | } |
| 160 | } |
| 161 | ], |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 162 | classes: 'table table-striped table-bordered table-responsive', |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 163 | filter: 'field', |
| 164 | order: true, |
| 165 | pagination: { |
| 166 | pageSize: 10 |
| 167 | } |
| 168 | }; |
| 169 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 170 | this.formConfig = { |
| 171 | exclude: this.config.hiddenFields, |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 172 | fields: {}, |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 173 | formName: `${this.config.resource}Form`, |
| 174 | actions: [ |
| 175 | { |
| 176 | label: 'Save', |
| 177 | icon: 'ok', |
| 178 | cb: (item) => { |
Matteo Scandolo | 67e79cb | 2016-04-29 09:14:58 -0700 | [diff] [blame] | 179 | let p; |
| 180 | let isNew = true; |
| 181 | |
| 182 | if(item.id){ |
| 183 | p = item.$update(); |
| 184 | isNew = false; |
| 185 | } |
| 186 | else { |
| 187 | p = item.$save(); |
| 188 | } |
| 189 | |
| 190 | p.then((res) => { |
| 191 | if(isNew){ |
| 192 | this.data.push(angular.copy(res)); |
| 193 | } |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 194 | delete this.detailedItem; |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 195 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully saved`; |
| 196 | }) |
| 197 | .catch((err) => { |
| 198 | this.responseErr = err.data.detail || `Error while saving ${this.config.resource} with id ${item.id}`; |
| 199 | }) |
| 200 | }, |
| 201 | class: 'success' |
| 202 | } |
| 203 | ] |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | this.cleanForm = () => { |
| 207 | delete this.detailedItem; |
Matteo Scandolo | c3c697a | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | this.createItem = () => { |
| 211 | this.detailedItem = new this.Resource(); |
Matteo Scandolo | c3c697a | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 212 | }; |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 213 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 214 | this.Resource = $injector.get(this.config.resource); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 215 | |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 216 | const getData = () => { |
| 217 | this.Resource.query().$promise |
| 218 | .then((res) => { |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 219 | |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 220 | if(!res[0]){ |
| 221 | return; |
| 222 | } |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 223 | |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 224 | let item = res[0]; |
| 225 | let props = Object.keys(item); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 226 | |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 227 | _.remove(props, p => { |
Matteo Scandolo | 67e79cb | 2016-04-29 09:14:58 -0700 | [diff] [blame] | 228 | return p == 'id' || p == 'validators' |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 229 | }); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 230 | |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 231 | // TODO move out cb |
| 232 | if(angular.isArray(this.config.hiddenFields)){ |
| 233 | props = _.difference(props, this.config.hiddenFields) |
| 234 | } |
| 235 | |
| 236 | let labels = props.map(p => LabelFormatter.format(p)); |
| 237 | |
| 238 | props.forEach((p, i) => { |
| 239 | this.tableConfig.columns.push({ |
| 240 | label: labels[i], |
| 241 | prop: p |
| 242 | }); |
Matteo Scandolo | e2ee2d9 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 243 | }); |
Matteo Scandolo | 6ba1287 | 2016-04-27 16:29:33 -0700 | [diff] [blame] | 244 | |
Matteo Scandolo | b7dac50 | 2016-04-28 13:14:08 -0700 | [diff] [blame] | 245 | // build form structure |
| 246 | props.forEach((p, i) => { |
| 247 | this.formConfig.fields[p] = { |
| 248 | label: LabelFormatter.format(labels[i]).replace(':', ''), |
| 249 | type: XosFormHelpers._getFieldFormat(item[p]) |
| 250 | }; |
| 251 | }); |
| 252 | |
| 253 | this.data = res; |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | getData(); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 258 | } |
| 259 | }; |
| 260 | }); |
| 261 | })(); |