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; |
| 55 | actions?: any[]; // TODO create interface |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | class TableCtrl { |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 59 | $inject = ['$onInit', '$scope']; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 60 | |
| 61 | public columns: any[]; |
| 62 | public orderBy: string; |
| 63 | public reverse: boolean; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 64 | public classes: string; |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 65 | public data: any; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 66 | private config: IXosTableCfg; |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 67 | private currentPage: number; |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 68 | private loader: boolean = true; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 69 | |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 70 | constructor( |
| 71 | private $scope: ng.IScope |
| 72 | ) { |
| 73 | |
| 74 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 75 | |
| 76 | $onInit() { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 77 | |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 78 | this.$scope.$watch(() => this.data, data => { |
| 79 | if (angular.isDefined(data)) { |
| 80 | this.loader = false; |
| 81 | } |
| 82 | }); |
| 83 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 84 | this.classes = 'table table-striped'; // table-bordered |
| 85 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 86 | 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 Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 95 | if (this.config.order && angular.isObject(this.config.order)) { |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 96 | 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 Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 101 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 102 | 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 Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 112 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 113 | 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 Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 133 | // 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 Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 148 | // this.columns = this.config.columns; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 149 | |
| 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: { |
| 162 | data: '=', |
| 163 | config: '=' |
| 164 | } |
| 165 | }; |