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