blob: 1d400927014ac4ae9b7474c9f466c2ddf7d6e3f9 [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
7interface IXosTableCgfOrder {
8 reverse: boolean;
9 field: string;
10}
11
12export interface IXosTableCfg {
13 columns: any[];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080014 order?: IXosTableCgfOrder; // | boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080015}
16
17class TableCtrl {
18 $inject = ['$onInit'];
19
20 public columns: any[];
21 public orderBy: string;
22 public reverse: boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080023 private config: IXosTableCfg;
24
25
26 $onInit() {
27 if (!this.config) {
28 throw new Error('[xosTable] Please provide a configuration via the "config" attribute');
29 }
30
31 if (!this.config.columns) {
32 throw new Error('[xosTable] Please provide a columns list in the configuration');
33 }
34
35 // handle default ordering
Matteo Scandolo035c5932016-12-14 09:55:15 -080036 if (this.config.order && angular.isObject(this.config.order)) {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080037 this.reverse = this.config.order.reverse || false;
38 this.orderBy = this.config.order.field || 'id';
39 }
40
41 // if columns with type 'custom' are provided
Matteo Scandolo035c5932016-12-14 09:55:15 -080042 // check that a custom formatter is provided too
Matteo Scandolo9f87f302016-12-13 18:11:10 -080043 let customCols = _.filter(this.config.columns, {type: 'custom'});
44 if (angular.isArray(customCols) && customCols.length > 0) {
45 _.forEach(customCols, (col) => {
46 if (!col.formatter || !angular.isFunction(col.formatter)) {
47 throw new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.');
48 }
49 });
50 }
51
52 // if columns with type 'icon' are provided
Matteo Scandolo035c5932016-12-14 09:55:15 -080053 // check that a custom formatter is provided too
Matteo Scandolo9f87f302016-12-13 18:11:10 -080054 let iconCols = _.filter(this.config.columns, {type: 'icon'});
55 if (angular.isArray(iconCols) && iconCols.length > 0) {
56 _.forEach(iconCols, (col) => {
57 if (!col.formatter || !angular.isFunction(col.formatter)) {
58 throw new Error('[xosTable] You have provided an icon field type, a formatter function should provided too.');
59 }
60 });
61 }
62
63 // if a link property is passed,
64 // it should be a function
65 let linkedColumns = _.filter(this.config.columns, col => angular.isDefined(col.link));
66 if (angular.isArray(linkedColumns) && linkedColumns.length > 0) {
67 _.forEach(linkedColumns, (col) => {
68 if (!angular.isFunction(col.link)) {
69 throw new Error('[xosTable] The link property should be a function.');
70 }
71 });
72 }
73
74 this.columns = this.config.columns;
75
76 }
77}
78
79export const xosTable: angular.IComponentOptions = {
80 template: require('./table.html'),
81 controllerAs: 'vm',
82 controller: TableCtrl,
83 bindings: {
84 data: '=',
85 config: '='
86 }
87};