blob: 0a4a1b0d3c64046351ff33522941a561df6e38cf [file] [log] [blame]
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -07001/**
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 Scandoloc3c697a2016-04-26 15:56:05 -070031 <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 Scandoloe2ee2d92016-04-27 15:58:16 -070038 <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 Scandolodc249eb2016-04-26 11:44:36 -070043 <div class="panel panel-default" ng-show="vm.detailedItem">
44 <div class="panel-heading">
Matteo Scandolof6445352016-04-26 12:15:23 -070045 <div class="row">
46 <div class="col-xs-11">
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -070047 <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 Scandolof6445352016-04-26 12:15:23 -070049 </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 Scandolodc249eb2016-04-26 11:44:36 -070056 </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 Scandolo8b55d9f2016-04-25 17:50:28 -070063 `,
64 bindToController: true,
65 controllerAs: 'vm',
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -070066 controller: function($injector, LabelFormatter, _, XosFormHelpers){
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070067
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070068 // NOTE
69 // Corner case
70 // - if response is empty, how can we generate a form ?
71
Matteo Scandolodc249eb2016-04-26 11:44:36 -070072 this.responseMsg = false;
73 this.responseErr = false;
74
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070075 this.tableConfig = {
76 columns: [
77 ],
Matteo Scandolodc249eb2016-04-26 11:44:36 -070078 actions: [
79 {
80 label: 'delete',
81 icon: 'remove',
82 cb: (item) => {
83 this.Resource.delete({id: item.id}).$promise
84 .then(() => {
Matteo Scandolodc249eb2016-04-26 11:44:36 -070085 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 Scandoloba1d35c2016-04-26 10:10:54 -0700101 classes: 'table table-striped table-bordered table-responsive',
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700102 filter: 'field',
103 order: true,
104 pagination: {
105 pageSize: 10
106 }
107 };
108
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700109 this.formConfig = {
110 exclude: this.config.hiddenFields,
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700111 fields: [],
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700112 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 Scandolof6445352016-04-26 12:15:23 -0700129 };
130
131 this.cleanForm = () => {
132 delete this.detailedItem;
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700133 };
134
135 this.createItem = () => {
136 this.detailedItem = new this.Resource();
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700137 };
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700138
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700139 this.Resource = $injector.get(this.config.resource);
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700140
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700141 this.Resource.query().$promise
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700142 .then((res) => {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700143
144 if(!res[0]){
145 return;
146 }
147
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700148 let item = res[0];
149 let props = Object.keys(item);
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700150
151 _.remove(props, p => {
152 return p == 'id' || p == 'password' || p == 'validators'
153 });
154
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700155 // TODO move out cb
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700156 if(angular.isArray(this.config.hiddenFields)){
157 props = _.difference(props, this.config.hiddenFields)
158 }
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700159
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700160 let labels = props.map(p => LabelFormatter.format(p));
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700161
162 props.forEach((p, i) => {
163 this.tableConfig.columns.push({
164 label: labels[i],
165 prop: p
166 });
167 });
168
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -0700169 // 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 Scandolo6ba12872016-04-27 16:29:33 -0700176
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700177 this.data = res;
Matteo Scandolof6445352016-04-26 12:15:23 -0700178 });
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700179 }
180 };
181 });
182})();