blob: 734c54bbc0314e3dd130849e381df570a152f033 [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">
35 <h3 class="panel-title">Update {{vm.config.resource}} {{vm.detailedItem.id}}</h3>
36 </div>
37 <div class="panel-body">
38 <xos-form config="vm.formConfig" ng-model="vm.detailedItem"></xos-form>
39 </div>
40 </div>
41 <xos-alert config="{type: 'success', closeBtn: true}" show="vm.responseMsg">{{vm.responseMsg}}</xos-alert>
42 <xos-alert config="{type: 'danger', closeBtn: true}" show="vm.responseErr">{{vm.responseErr}}</xos-alert>
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070043 `,
44 bindToController: true,
45 controllerAs: 'vm',
46 controller: function($injector, LabelFormatter, _){
47
Matteo Scandolodc249eb2016-04-26 11:44:36 -070048 this.responseMsg = false;
49 this.responseErr = false;
50
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070051 this.tableConfig = {
52 columns: [
53 ],
Matteo Scandolodc249eb2016-04-26 11:44:36 -070054 actions: [
55 {
56 label: 'delete',
57 icon: 'remove',
58 cb: (item) => {
59 this.Resource.delete({id: item.id}).$promise
60 .then(() => {
61 console.log(this.config.resource);
62 this.responseMsg = `${this.config.resource} with id ${item.id} successfully deleted`;
63 })
64 .catch(err => {
65 this.responseErr = err.data.detail || `Error while deleting ${this.config.resource} with id ${item.id}`;
66 });
67 },
68 color: 'red'
69 },
70 {
71 label: 'details',
72 icon: 'search',
73 cb: (item) => {
74 this.detailedItem = item;
75 }
76 }
77 ],
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070078 classes: 'table table-striped table-bordered table-responsive',
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070079 filter: 'field',
80 order: true,
81 pagination: {
82 pageSize: 10
83 }
84 };
85
Matteo Scandolodc249eb2016-04-26 11:44:36 -070086 this.formConfig = {
87 exclude: this.config.hiddenFields,
88 formName: `${this.config.resource}Form`,
89 actions: [
90 {
91 label: 'Save',
92 icon: 'ok',
93 cb: (item) => {
94 item.$save()
95 .then(() => {
96 this.responseMsg = `${this.config.resource} with id ${item.id} successfully saved`;
97 })
98 .catch((err) => {
99 this.responseErr = err.data.detail || `Error while saving ${this.config.resource} with id ${item.id}`;
100 })
101 },
102 class: 'success'
103 }
104 ]
105 }
106
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700107 this.Resource = $injector.get(this.config.resource);
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700108
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700109 this.Resource.query().$promise
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700110 .then((res) => {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700111
112 if(!res[0]){
113 return;
114 }
115
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700116 let props = Object.keys(res[0]);
117
118 _.remove(props, p => {
119 return p == 'id' || p == 'password' || p == 'validators'
120 });
121
Matteo Scandolodc249eb2016-04-26 11:44:36 -0700122 // TODO move out cb
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700123 if(angular.isArray(this.config.hiddenFields)){
124 props = _.difference(props, this.config.hiddenFields)
125 }
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700126
Matteo Scandoloba1d35c2016-04-26 10:10:54 -0700127 let labels = props.map(p => LabelFormatter.format(p));
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700128
129 props.forEach((p, i) => {
130 this.tableConfig.columns.push({
131 label: labels[i],
132 prop: p
133 });
134 });
135
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -0700136 this.data = res;
137 })
138 }
139 };
140 });
141})();