blob: 46a5ca5a1265b118cb844299ffeb3d4c5b92176b [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo9f87f302016-12-13 18:11:10 -080019// TODO fininsh to import all methods from https://github.com/opencord/ng-xos-lib/blob/master/src/ui_components/dumbComponents/table/table.component.js
20// TODO import tests
21
Matteo Scandolo035c5932016-12-14 09:55:15 -080022import './table.scss';
Matteo Scandolo9f87f302016-12-13 18:11:10 -080023import * as _ from 'lodash';
24
Matteo Scandolod58d5042016-12-16 16:59:21 -080025enum EXosTableColType {
26 'boolean',
27 'array',
28 'object',
29 'custom',
30 'date' ,
31 'icon'
32}
33
34export interface IXosTableColumn {
35 label: string;
36 prop: string;
37 type?: string; // understand why enum does not work
38 formatter?(item: any): string;
39 link?(item: any): string;
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080040 hover?(item: any): string;
Matteo Scandolod58d5042016-12-16 16:59:21 -080041}
42
Matteo Scandolo9f87f302016-12-13 18:11:10 -080043interface IXosTableCgfOrder {
Matteo Scandolod62ea792016-12-22 14:02:28 -080044 reverse?: boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080045 field: string;
46}
47
48export interface IXosTableCfg {
49 columns: any[];
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080050 pagination?: {
51 pageSize: number;
52 };
Matteo Scandolod62ea792016-12-22 14:02:28 -080053 order?: IXosTableCgfOrder;
54 filter?: string;
55 actions?: any[]; // TODO create interface
Matteo Scandolo9f87f302016-12-13 18:11:10 -080056}
57
58class TableCtrl {
Matteo Scandolo710dc152017-04-11 13:54:23 -070059 $inject = ['$onInit', '$scope'];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080060
61 public columns: any[];
62 public orderBy: string;
63 public reverse: boolean;
Matteo Scandolo266907e2016-12-20 13:41:42 -080064 public classes: string;
Matteo Scandolo710dc152017-04-11 13:54:23 -070065 public data: any;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080066 private config: IXosTableCfg;
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080067 private currentPage: number;
Matteo Scandolo710dc152017-04-11 13:54:23 -070068 private loader: boolean = true;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080069
Matteo Scandolo710dc152017-04-11 13:54:23 -070070 constructor(
71 private $scope: ng.IScope
72 ) {
73
74 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080075
76 $onInit() {
Matteo Scandolo266907e2016-12-20 13:41:42 -080077
Matteo Scandolo710dc152017-04-11 13:54:23 -070078 this.$scope.$watch(() => this.data, data => {
79 if (angular.isDefined(data)) {
80 this.loader = false;
81 }
82 });
83
Matteo Scandolo266907e2016-12-20 13:41:42 -080084 this.classes = 'table table-striped'; // table-bordered
85
Matteo Scandolo9f87f302016-12-13 18:11:10 -080086 if (!this.config) {
87 throw new Error('[xosTable] Please provide a configuration via the "config" attribute');
88 }
89
90 if (!this.config.columns) {
91 throw new Error('[xosTable] Please provide a columns list in the configuration');
92 }
93
94 // handle default ordering
Matteo Scandolo035c5932016-12-14 09:55:15 -080095 if (this.config.order && angular.isObject(this.config.order)) {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080096 this.reverse = this.config.order.reverse || false;
97 this.orderBy = this.config.order.field || 'id';
98 }
99
100 // if columns with type 'custom' are provided
Matteo Scandolo035c5932016-12-14 09:55:15 -0800101 // check that a custom formatter is provided too
Matteo Scandolo9f87f302016-12-13 18:11:10 -0800102 let customCols = _.filter(this.config.columns, {type: 'custom'});
103 if (angular.isArray(customCols) && customCols.length > 0) {
104 _.forEach(customCols, (col) => {
105 if (!col.formatter || !angular.isFunction(col.formatter)) {
106 throw new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.');
107 }
108 });
109 }
110
111 // if columns with type 'icon' are provided
Matteo Scandolo035c5932016-12-14 09:55:15 -0800112 // check that a custom formatter is provided too
Matteo Scandolo9f87f302016-12-13 18:11:10 -0800113 let iconCols = _.filter(this.config.columns, {type: 'icon'});
114 if (angular.isArray(iconCols) && iconCols.length > 0) {
115 _.forEach(iconCols, (col) => {
116 if (!col.formatter || !angular.isFunction(col.formatter)) {
117 throw new Error('[xosTable] You have provided an icon field type, a formatter function should provided too.');
118 }
119 });
120 }
121
122 // if a link property is passed,
123 // it should be a function
124 let linkedColumns = _.filter(this.config.columns, col => angular.isDefined(col.link));
125 if (angular.isArray(linkedColumns) && linkedColumns.length > 0) {
126 _.forEach(linkedColumns, (col) => {
127 if (!angular.isFunction(col.link)) {
128 throw new Error('[xosTable] The link property should be a function.');
129 }
130 });
131 }
132
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800133 // if an hover property is passed,
134 // it should be a function
135 let hoverColumns = _.filter(this.config.columns, col => angular.isDefined(col.hover));
136 if (angular.isArray(hoverColumns) && hoverColumns.length > 0) {
137 _.forEach(hoverColumns, (col) => {
138 if (!angular.isFunction(col.hover)) {
139 throw new Error('[xosTable] The hover property should be a function.');
140 }
141 });
142 }
143
144 if (this.config.pagination) {
145 this.currentPage = 0;
146 }
147
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700148 // this.columns = this.config.columns;
Matteo Scandolo9f87f302016-12-13 18:11:10 -0800149
150 }
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800151
152 public goToPage = (n) => {
153 this.currentPage = n;
154 };
Matteo Scandolo9f87f302016-12-13 18:11:10 -0800155}
156
157export const xosTable: angular.IComponentOptions = {
158 template: require('./table.html'),
159 controllerAs: 'vm',
160 controller: TableCtrl,
161 bindings: {
162 data: '=',
163 config: '='
164 }
165};