blob: b339f8957304adb70589d4d2e1ef1790b5b7f614 [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 Scandolo8b55d9f2016-04-25 17:50:28 -070038 <xos-table config="vm.tableConfig" data="vm.data"></xos-table>
Matteo Scandolodc249eb2016-04-26 11:44:36 -070039 <div class="panel panel-default" ng-show="vm.detailedItem">
40 <div class="panel-heading">
Matteo Scandolof6445352016-04-26 12:15:23 -070041 <div class="row">
42 <div class="col-xs-11">
43 <h3 class="panel-title">Update {{vm.config.resource}} {{vm.detailedItem.id}}</h3>
44 </div>
45 <div class="col-xs-1">
46 <a href="" ng-click="vm.cleanForm()">
47 <i class="glyphicon glyphicon-remove pull-right"></i>
48 </a>
49 </div>
50 </div>
Matteo Scandolodc249eb2016-04-26 11:44:36 -070051 </div>
52 <div class="panel-body">
53 <xos-form config="vm.formConfig" ng-model="vm.detailedItem"></xos-form>
54 </div>
55 </div>
56 <xos-alert config="{type: 'success', closeBtn: true}" show="vm.responseMsg">{{vm.responseMsg}}</xos-alert>
57 <xos-alert config="{type: 'danger', closeBtn: true}" show="vm.responseErr">{{vm.responseErr}}</xos-alert>
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070058 `,
59 bindToController: true,
60 controllerAs: 'vm',
61 controller: function($injector, LabelFormatter, _){
62
Matteo Scandoloc3c697a2016-04-26 15:56:05 -070063 // NOTE
64 // Corner case
65 // - if response is empty, how can we generate a form ?
66
Matteo Scandolodc249eb2016-04-26 11:44:36 -070067 this.responseMsg = false;
68 this.responseErr = false;
69
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070070 this.tableConfig = {
71 columns: [
72 ],
Matteo Scandolodc249eb2016-04-26 11:44:36 -070073 actions: [
74 {
75 label: 'delete',
76 icon: 'remove',
77 cb: (item) => {
78 this.Resource.delete({id: item.id}).$promise
79 .then(() => {
Matteo Scandolodc249eb2016-04-26 11:44:36 -070080 this.responseMsg = `${this.config.resource} with id ${item.id} successfully deleted`;
81 })
82 .catch(err => {
83 this.responseErr = err.data.detail || `Error while deleting ${this.config.resource} with id ${item.id}`;
84 });
85 },
86 color: 'red'
87 },
88 {
89 label: 'details',
90 icon: 'search',
91 cb: (item) => {
92 this.detailedItem = item;
93 }
94 }
95 ],
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070096 classes: 'table table-striped table-bordered table-responsive',
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070097 filter: 'field',
98 order: true,
99 pagination: {
100 pageSize: 10
101 }
102 };
103
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700104 this.formConfig = {
105 exclude: this.config.hiddenFields,
106 formName: `${this.config.resource}Form`,
107 actions: [
108 {
109 label: 'Save',
110 icon: 'ok',
111 cb: (item) => {
112 item.$save()
113 .then(() => {
114 this.responseMsg = `${this.config.resource} with id ${item.id} successfully saved`;
115 })
116 .catch((err) => {
117 this.responseErr = err.data.detail || `Error while saving ${this.config.resource} with id ${item.id}`;
118 })
119 },
120 class: 'success'
121 }
122 ]
Matteo Scandolof6445352016-04-26 12:15:23 -0700123 };
124
125 this.cleanForm = () => {
126 delete this.detailedItem;
Matteo Scandoloc3c697a2016-04-26 15:56:05 -0700127 };
128
129 this.createItem = () => {
130 this.detailedItem = new this.Resource();
131 console.log(this.detailedItem);
132 };
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700133
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700134 this.Resource = $injector.get(this.config.resource);
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700135
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700136 this.Resource.query().$promise
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700137 .then((res) => {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700138
139 if(!res[0]){
140 return;
141 }
142
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700143 let props = Object.keys(res[0]);
144
145 _.remove(props, p => {
146 return p == 'id' || p == 'password' || p == 'validators'
147 });
148
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700149 // TODO move out cb
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700150 if(angular.isArray(this.config.hiddenFields)){
151 props = _.difference(props, this.config.hiddenFields)
152 }
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700153
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700154 let labels = props.map(p => LabelFormatter.format(p));
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700155
156 props.forEach((p, i) => {
157 this.tableConfig.columns.push({
158 label: labels[i],
159 prop: p
160 });
161 });
162
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700163 this.data = res;
Matteo Scandolof6445352016-04-26 12:15:23 -0700164 });
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700165 }
166 };
167 });
168})();