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