blob: 4a0ecd93a1644133b7e47ad6d949eb79f856def7 [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: `
31 <xos-table config="vm.tableConfig" data="vm.data"></xos-table>
32 `,
33 bindToController: true,
34 controllerAs: 'vm',
35 controller: function($injector, LabelFormatter, _){
36
37 this.tableConfig = {
38 columns: [
39 ],
40 // actions: [
41 // {
42 // label: 'delete',
43 // icon: 'remove',
44 // cb: (user) => {
45 // console.log(user);
46 // // _.remove(this.users, {id: user.id});
47 // },
48 // color: 'red'
49 // }
50 // ],
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070051 classes: 'table table-striped table-bordered table-responsive',
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070052 filter: 'field',
53 order: true,
54 pagination: {
55 pageSize: 10
56 }
57 };
58
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070059 this.Resource = $injector.get(this.config.resource);
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070060
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070061 this.Resource.query().$promise
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070062 .then((res) => {
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070063
64 if(!res[0]){
65 return;
66 }
67
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070068 let props = Object.keys(res[0]);
69
70 _.remove(props, p => {
71 return p == 'id' || p == 'password' || p == 'validators'
72 });
73
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070074 if(angular.isArray(this.config.hiddenFields)){
75 props = _.difference(props, this.config.hiddenFields)
76 }
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070077
Matteo Scandoloba1d35c2016-04-26 10:10:54 -070078 let labels = props.map(p => LabelFormatter.format(p));
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070079
80 props.forEach((p, i) => {
81 this.tableConfig.columns.push({
82 label: labels[i],
83 prop: p
84 });
85 });
86
Matteo Scandolo8b55d9f2016-04-25 17:50:28 -070087 this.data = res;
88 })
89 }
90 };
91 });
92})();