blob: d9fa9ccc284e09516be37c375a7f14a4175b811e [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 Scandolodc249eb2016-04-26 11:44:36 -070031 <pre>{{vm.responseErr}}</pre>
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070032 <xos-table config="vm.tableConfig" data="vm.data"></xos-table>
Matteo Scandolodc249eb2016-04-26 11:44:36 -070033 <div class="panel panel-default" ng-show="vm.detailedItem">
34 <div class="panel-heading">
Matteo Scandolof6445352016-04-26 12:15:23 -070035 <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 Scandolodc249eb2016-04-26 11:44:36 -070045 </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 Scandolo8b55d9f2016-04-25 17:50:28 -070052 `,
53 bindToController: true,
54 controllerAs: 'vm',
55 controller: function($injector, LabelFormatter, _){
56
Matteo Scandolodc249eb2016-04-26 11:44:36 -070057 this.responseMsg = false;
58 this.responseErr = false;
59
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070060 this.tableConfig = {
61 columns: [
62 ],
Matteo Scandolodc249eb2016-04-26 11:44:36 -070063 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 Scandoloba1d35c2016-04-26 10:10:54 -070087 classes: 'table table-striped table-bordered table-responsive',
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070088 filter: 'field',
89 order: true,
90 pagination: {
91 pageSize: 10
92 }
93 };
94
Matteo Scandolodc249eb2016-04-26 11:44:36 -070095 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 Scandolof6445352016-04-26 12:15:23 -0700114 };
115
116 this.cleanForm = () => {
117 delete this.detailedItem;
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700118 }
119
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700120 this.Resource = $injector.get(this.config.resource);
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700121
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700122 this.Resource.query().$promise
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700123 .then((res) => {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700124
125 if(!res[0]){
126 return;
127 }
128
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700129 let props = Object.keys(res[0]);
130
131 _.remove(props, p => {
132 return p == 'id' || p == 'password' || p == 'validators'
133 });
134
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700135 // TODO move out cb
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700136 if(angular.isArray(this.config.hiddenFields)){
137 props = _.difference(props, this.config.hiddenFields)
138 }
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700139
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700140 let labels = props.map(p => LabelFormatter.format(p));
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700141
142 props.forEach((p, i) => {
143 this.tableConfig.columns.push({
144 label: labels[i],
145 prop: p
146 });
147 });
148
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700149 this.data = res;
Matteo Scandolof6445352016-04-26 12:15:23 -0700150 });
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700151 }
152 };
153 });
154})();