blob: 5fb884c92cbb4493a1551d605aedeefb865503b8 [file] [log] [blame]
Matteo Scandolo9f87f302016-12-13 18:11:10 -08001// TODO fininsh to import all methods from https://github.com/opencord/ng-xos-lib/blob/master/src/ui_components/dumbComponents/table/table.component.js
2// TODO import tests
3
Matteo Scandolo035c5932016-12-14 09:55:15 -08004import './table.scss';
Matteo Scandolo9f87f302016-12-13 18:11:10 -08005import * as _ from 'lodash';
6
Matteo Scandolod58d5042016-12-16 16:59:21 -08007enum EXosTableColType {
8 'boolean',
9 'array',
10 'object',
11 'custom',
12 'date' ,
13 'icon'
14}
15
16export interface IXosTableColumn {
17 label: string;
18 prop: string;
19 type?: string; // understand why enum does not work
20 formatter?(item: any): string;
21 link?(item: any): string;
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080022 hover?(item: any): string;
Matteo Scandolod58d5042016-12-16 16:59:21 -080023}
24
Matteo Scandolo9f87f302016-12-13 18:11:10 -080025interface IXosTableCgfOrder {
Matteo Scandolod62ea792016-12-22 14:02:28 -080026 reverse?: boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080027 field: string;
28}
29
30export interface IXosTableCfg {
31 columns: any[];
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080032 pagination?: {
33 pageSize: number;
34 };
Matteo Scandolod62ea792016-12-22 14:02:28 -080035 order?: IXosTableCgfOrder;
36 filter?: string;
37 actions?: any[]; // TODO create interface
Matteo Scandolo9f87f302016-12-13 18:11:10 -080038}
39
40class TableCtrl {
Matteo Scandolo710dc152017-04-11 13:54:23 -070041 $inject = ['$onInit', '$scope'];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080042
43 public columns: any[];
44 public orderBy: string;
45 public reverse: boolean;
Matteo Scandolo266907e2016-12-20 13:41:42 -080046 public classes: string;
Matteo Scandolo710dc152017-04-11 13:54:23 -070047 public data: any;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080048 private config: IXosTableCfg;
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080049 private currentPage: number;
Matteo Scandolo710dc152017-04-11 13:54:23 -070050 private loader: boolean = true;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080051
Matteo Scandolo710dc152017-04-11 13:54:23 -070052 constructor(
53 private $scope: ng.IScope
54 ) {
55
56 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080057
58 $onInit() {
Matteo Scandolo266907e2016-12-20 13:41:42 -080059
Matteo Scandolo710dc152017-04-11 13:54:23 -070060 this.$scope.$watch(() => this.data, data => {
61 if (angular.isDefined(data)) {
62 this.loader = false;
63 }
64 });
65
Matteo Scandolo266907e2016-12-20 13:41:42 -080066 this.classes = 'table table-striped'; // table-bordered
67
Matteo Scandolo9f87f302016-12-13 18:11:10 -080068 if (!this.config) {
69 throw new Error('[xosTable] Please provide a configuration via the "config" attribute');
70 }
71
72 if (!this.config.columns) {
73 throw new Error('[xosTable] Please provide a columns list in the configuration');
74 }
75
76 // handle default ordering
Matteo Scandolo035c5932016-12-14 09:55:15 -080077 if (this.config.order && angular.isObject(this.config.order)) {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080078 this.reverse = this.config.order.reverse || false;
79 this.orderBy = this.config.order.field || 'id';
80 }
81
82 // if columns with type 'custom' are provided
Matteo Scandolo035c5932016-12-14 09:55:15 -080083 // check that a custom formatter is provided too
Matteo Scandolo9f87f302016-12-13 18:11:10 -080084 let customCols = _.filter(this.config.columns, {type: 'custom'});
85 if (angular.isArray(customCols) && customCols.length > 0) {
86 _.forEach(customCols, (col) => {
87 if (!col.formatter || !angular.isFunction(col.formatter)) {
88 throw new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.');
89 }
90 });
91 }
92
93 // if columns with type 'icon' are provided
Matteo Scandolo035c5932016-12-14 09:55:15 -080094 // check that a custom formatter is provided too
Matteo Scandolo9f87f302016-12-13 18:11:10 -080095 let iconCols = _.filter(this.config.columns, {type: 'icon'});
96 if (angular.isArray(iconCols) && iconCols.length > 0) {
97 _.forEach(iconCols, (col) => {
98 if (!col.formatter || !angular.isFunction(col.formatter)) {
99 throw new Error('[xosTable] You have provided an icon field type, a formatter function should provided too.');
100 }
101 });
102 }
103
104 // if a link property is passed,
105 // it should be a function
106 let linkedColumns = _.filter(this.config.columns, col => angular.isDefined(col.link));
107 if (angular.isArray(linkedColumns) && linkedColumns.length > 0) {
108 _.forEach(linkedColumns, (col) => {
109 if (!angular.isFunction(col.link)) {
110 throw new Error('[xosTable] The link property should be a function.');
111 }
112 });
113 }
114
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800115 // if an hover property is passed,
116 // it should be a function
117 let hoverColumns = _.filter(this.config.columns, col => angular.isDefined(col.hover));
118 if (angular.isArray(hoverColumns) && hoverColumns.length > 0) {
119 _.forEach(hoverColumns, (col) => {
120 if (!angular.isFunction(col.hover)) {
121 throw new Error('[xosTable] The hover property should be a function.');
122 }
123 });
124 }
125
126 if (this.config.pagination) {
127 this.currentPage = 0;
128 }
129
Matteo Scandolo9f87f302016-12-13 18:11:10 -0800130 this.columns = this.config.columns;
131
132 }
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800133
134 public goToPage = (n) => {
135 this.currentPage = n;
136 };
Matteo Scandolo9f87f302016-12-13 18:11:10 -0800137}
138
139export const xosTable: angular.IComponentOptions = {
140 template: require('./table.html'),
141 controllerAs: 'vm',
142 controller: TableCtrl,
143 bindings: {
144 data: '=',
145 config: '='
146 }
147};