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 | |
| 7 | interface IXosTableCgfOrder { |
| 8 | reverse: boolean; |
| 9 | field: string; |
| 10 | } |
| 11 | |
| 12 | export interface IXosTableCfg { |
| 13 | columns: any[]; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 14 | order?: IXosTableCgfOrder; // | boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | class TableCtrl { |
| 18 | $inject = ['$onInit']; |
| 19 | |
| 20 | public columns: any[]; |
| 21 | public orderBy: string; |
| 22 | public reverse: boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 23 | private config: IXosTableCfg; |
| 24 | |
| 25 | |
| 26 | $onInit() { |
| 27 | if (!this.config) { |
| 28 | throw new Error('[xosTable] Please provide a configuration via the "config" attribute'); |
| 29 | } |
| 30 | |
| 31 | if (!this.config.columns) { |
| 32 | throw new Error('[xosTable] Please provide a columns list in the configuration'); |
| 33 | } |
| 34 | |
| 35 | // handle default ordering |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 36 | if (this.config.order && angular.isObject(this.config.order)) { |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 37 | this.reverse = this.config.order.reverse || false; |
| 38 | this.orderBy = this.config.order.field || 'id'; |
| 39 | } |
| 40 | |
| 41 | // if columns with type 'custom' are provided |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 42 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 43 | let customCols = _.filter(this.config.columns, {type: 'custom'}); |
| 44 | if (angular.isArray(customCols) && customCols.length > 0) { |
| 45 | _.forEach(customCols, (col) => { |
| 46 | if (!col.formatter || !angular.isFunction(col.formatter)) { |
| 47 | throw new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.'); |
| 48 | } |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | // if columns with type 'icon' are provided |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 53 | // check that a custom formatter is provided too |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 54 | let iconCols = _.filter(this.config.columns, {type: 'icon'}); |
| 55 | if (angular.isArray(iconCols) && iconCols.length > 0) { |
| 56 | _.forEach(iconCols, (col) => { |
| 57 | if (!col.formatter || !angular.isFunction(col.formatter)) { |
| 58 | throw new Error('[xosTable] You have provided an icon field type, a formatter function should provided too.'); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | // if a link property is passed, |
| 64 | // it should be a function |
| 65 | let linkedColumns = _.filter(this.config.columns, col => angular.isDefined(col.link)); |
| 66 | if (angular.isArray(linkedColumns) && linkedColumns.length > 0) { |
| 67 | _.forEach(linkedColumns, (col) => { |
| 68 | if (!angular.isFunction(col.link)) { |
| 69 | throw new Error('[xosTable] The link property should be a function.'); |
| 70 | } |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | this.columns = this.config.columns; |
| 75 | |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | export const xosTable: angular.IComponentOptions = { |
| 80 | template: require('./table.html'), |
| 81 | controllerAs: 'vm', |
| 82 | controller: TableCtrl, |
| 83 | bindings: { |
| 84 | data: '=', |
| 85 | config: '=' |
| 86 | } |
| 87 | }; |