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 { |
| 41 | $inject = ['$onInit']; |
| 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 | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 47 | private config: IXosTableCfg; |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame^] | 48 | private currentPage: number; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 49 | |
| 50 | |
| 51 | $onInit() { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 52 | |
| 53 | this.classes = 'table table-striped'; // table-bordered |
| 54 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 55 | if (!this.config) { |
| 56 | throw new Error('[xosTable] Please provide a configuration via the "config" attribute'); |
| 57 | } |
| 58 | |
| 59 | if (!this.config.columns) { |
| 60 | throw new Error('[xosTable] Please provide a columns list in the configuration'); |
| 61 | } |
| 62 | |
| 63 | // handle default ordering |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 64 | if (this.config.order && angular.isObject(this.config.order)) { |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 65 | this.reverse = this.config.order.reverse || false; |
| 66 | this.orderBy = this.config.order.field || 'id'; |
| 67 | } |
| 68 | |
| 69 | // if columns with type 'custom' are provided |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 70 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 71 | let customCols = _.filter(this.config.columns, {type: 'custom'}); |
| 72 | if (angular.isArray(customCols) && customCols.length > 0) { |
| 73 | _.forEach(customCols, (col) => { |
| 74 | if (!col.formatter || !angular.isFunction(col.formatter)) { |
| 75 | throw new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.'); |
| 76 | } |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | // if columns with type 'icon' are provided |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 81 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 82 | let iconCols = _.filter(this.config.columns, {type: 'icon'}); |
| 83 | if (angular.isArray(iconCols) && iconCols.length > 0) { |
| 84 | _.forEach(iconCols, (col) => { |
| 85 | if (!col.formatter || !angular.isFunction(col.formatter)) { |
| 86 | throw new Error('[xosTable] You have provided an icon field type, a formatter function should provided too.'); |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | // if a link property is passed, |
| 92 | // it should be a function |
| 93 | let linkedColumns = _.filter(this.config.columns, col => angular.isDefined(col.link)); |
| 94 | if (angular.isArray(linkedColumns) && linkedColumns.length > 0) { |
| 95 | _.forEach(linkedColumns, (col) => { |
| 96 | if (!angular.isFunction(col.link)) { |
| 97 | throw new Error('[xosTable] The link property should be a function.'); |
| 98 | } |
| 99 | }); |
| 100 | } |
| 101 | |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame^] | 102 | // if an hover property is passed, |
| 103 | // it should be a function |
| 104 | let hoverColumns = _.filter(this.config.columns, col => angular.isDefined(col.hover)); |
| 105 | if (angular.isArray(hoverColumns) && hoverColumns.length > 0) { |
| 106 | _.forEach(hoverColumns, (col) => { |
| 107 | if (!angular.isFunction(col.hover)) { |
| 108 | throw new Error('[xosTable] The hover property should be a function.'); |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | if (this.config.pagination) { |
| 114 | this.currentPage = 0; |
| 115 | } |
| 116 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 117 | this.columns = this.config.columns; |
| 118 | |
| 119 | } |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame^] | 120 | |
| 121 | public goToPage = (n) => { |
| 122 | this.currentPage = n; |
| 123 | }; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | export const xosTable: angular.IComponentOptions = { |
| 127 | template: require('./table.html'), |
| 128 | controllerAs: 'vm', |
| 129 | controller: TableCtrl, |
| 130 | bindings: { |
| 131 | data: '=', |
| 132 | config: '=' |
| 133 | } |
| 134 | }; |