Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 19 | // 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 Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 22 | import './table.scss'; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 23 | import * as _ from 'lodash'; |
| 24 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 25 | enum EXosTableColType { |
| 26 | 'boolean', |
| 27 | 'array', |
| 28 | 'object', |
| 29 | 'custom', |
| 30 | 'date' , |
| 31 | 'icon' |
| 32 | } |
| 33 | |
| 34 | export 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 Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 40 | hover?(item: any): string; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 43 | interface IXosTableCgfOrder { |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 44 | reverse?: boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 45 | field: string; |
| 46 | } |
| 47 | |
| 48 | export interface IXosTableCfg { |
| 49 | columns: any[]; |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 50 | pagination?: { |
| 51 | pageSize: number; |
| 52 | }; |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 53 | order?: IXosTableCgfOrder; |
| 54 | filter?: string; |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 55 | selectedRow?: number; |
| 56 | filteredData?: any[]; |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 57 | actions?: any[]; // TODO create interface |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | class TableCtrl { |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 61 | $inject = ['$onInit', '$scope']; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 62 | |
| 63 | public columns: any[]; |
| 64 | public orderBy: string; |
| 65 | public reverse: boolean; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 66 | public classes: string; |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 67 | public data: any; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 68 | private config: IXosTableCfg; |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 69 | private currentPage: number; |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 70 | private loader: boolean = true; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 71 | |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 72 | constructor( |
| 73 | private $scope: ng.IScope |
| 74 | ) { |
| 75 | |
| 76 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 77 | |
| 78 | $onInit() { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 79 | |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 80 | this.$scope.$watch(() => this.data, data => { |
| 81 | if (angular.isDefined(data)) { |
| 82 | this.loader = false; |
| 83 | } |
| 84 | }); |
| 85 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 86 | this.classes = 'table table-striped'; // table-bordered |
| 87 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 88 | if (!this.config) { |
| 89 | throw new Error('[xosTable] Please provide a configuration via the "config" attribute'); |
| 90 | } |
| 91 | |
| 92 | if (!this.config.columns) { |
| 93 | throw new Error('[xosTable] Please provide a columns list in the configuration'); |
| 94 | } |
| 95 | |
| 96 | // handle default ordering |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 97 | if (this.config.order && angular.isObject(this.config.order)) { |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 98 | this.reverse = this.config.order.reverse || false; |
| 99 | this.orderBy = this.config.order.field || 'id'; |
| 100 | } |
| 101 | |
| 102 | // if columns with type 'custom' are provided |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 103 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 104 | let customCols = _.filter(this.config.columns, {type: 'custom'}); |
| 105 | if (angular.isArray(customCols) && customCols.length > 0) { |
| 106 | _.forEach(customCols, (col) => { |
| 107 | if (!col.formatter || !angular.isFunction(col.formatter)) { |
| 108 | throw new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.'); |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | // if columns with type 'icon' are provided |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 114 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 115 | let iconCols = _.filter(this.config.columns, {type: 'icon'}); |
| 116 | if (angular.isArray(iconCols) && iconCols.length > 0) { |
| 117 | _.forEach(iconCols, (col) => { |
| 118 | if (!col.formatter || !angular.isFunction(col.formatter)) { |
| 119 | throw new Error('[xosTable] You have provided an icon field type, a formatter function should provided too.'); |
| 120 | } |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | // if a link property is passed, |
| 125 | // it should be a function |
| 126 | let linkedColumns = _.filter(this.config.columns, col => angular.isDefined(col.link)); |
| 127 | if (angular.isArray(linkedColumns) && linkedColumns.length > 0) { |
| 128 | _.forEach(linkedColumns, (col) => { |
| 129 | if (!angular.isFunction(col.link)) { |
| 130 | throw new Error('[xosTable] The link property should be a function.'); |
| 131 | } |
| 132 | }); |
| 133 | } |
| 134 | |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 135 | // if an hover property is passed, |
| 136 | // it should be a function |
| 137 | let hoverColumns = _.filter(this.config.columns, col => angular.isDefined(col.hover)); |
| 138 | if (angular.isArray(hoverColumns) && hoverColumns.length > 0) { |
| 139 | _.forEach(hoverColumns, (col) => { |
| 140 | if (!angular.isFunction(col.hover)) { |
| 141 | throw new Error('[xosTable] The hover property should be a function.'); |
| 142 | } |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | if (this.config.pagination) { |
| 147 | this.currentPage = 0; |
| 148 | } |
| 149 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 150 | } |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 151 | |
| 152 | public goToPage = (n) => { |
| 153 | this.currentPage = n; |
| 154 | }; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | export const xosTable: angular.IComponentOptions = { |
| 158 | template: require('./table.html'), |
| 159 | controllerAs: 'vm', |
| 160 | controller: TableCtrl, |
| 161 | bindings: { |
Matteo Scandolo | 04a34a1 | 2017-09-12 10:37:48 -0700 | [diff] [blame^] | 162 | data: '<', |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 163 | config: '=' |
| 164 | } |
| 165 | }; |