Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 1 | window.angular && (function(angular) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | /** |
| 5 | * |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 6 | * bmcTable Component |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 7 | * |
| 8 | * To use: |
| 9 | * The <bmc-table> component expects a 'model' attribute |
| 10 | * that will contain all the data needed to render the table. |
| 11 | * |
Yoshie Muranaka | 8c80dbd | 2019-08-08 10:58:04 -0500 | [diff] [blame] | 12 | * The component accepts a 'row-actions-enabled' attribute, |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 13 | * to optionally render table row actions. Defaults to false. |
| 14 | * Pass true to render actions. Row actions are defined in |
| 15 | * model.data.actions. |
| 16 | * |
Yoshie Muranaka | 8c80dbd | 2019-08-08 10:58:04 -0500 | [diff] [blame] | 17 | * The component accepts a 'size' attribute which can be |
| 18 | * set to 'small' which will render a smaller font size in the |
| 19 | * table. |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 20 | * |
Yoshie Muranaka | 8c80dbd | 2019-08-08 10:58:04 -0500 | [diff] [blame] | 21 | * The model object should contain 'header' and 'data' |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 22 | * properties. |
| 23 | * |
| 24 | * model: { |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 25 | * header: <string>[], // Array of header labels |
| 26 | * data: <any>[], // Array of each row object |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 27 | * } |
| 28 | * |
| 29 | * The header property will render each label as a <th> in the table. |
| 30 | * |
| 31 | * The data property will render each item as a <tr> in the table. |
| 32 | * Each row object in the model.data array should also have a 'uiData' |
| 33 | * property that should be an array of the properties that will render |
| 34 | * as each table cell <td>. |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 35 | * Each row object in the model.data array can optionally have an |
| 36 | * 'actions' property that should be an array of actions to provide the |
| 37 | * <bmc-table-actions> component. |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 38 | * |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 39 | * The 'rowActionsEnabled' property will render <bmc-table-actions> if set |
| 40 | * to true. |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 41 | * |
| 42 | */ |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 43 | |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 44 | const TableController = function() { |
| 45 | /** |
| 46 | * Init model data |
| 47 | * @param {any} model : table model object |
| 48 | * @returns : table model object with defaults |
| 49 | */ |
| 50 | const setModel = (model) => { |
| 51 | model.header = model.header === undefined ? [] : model.header; |
| 52 | model.data = model.data === undefined ? [] : model.data; |
| 53 | model.data = model.data.map((row) => { |
| 54 | if (row.uiData === undefined) { |
| 55 | row.uiData = []; |
| 56 | } |
| 57 | return row; |
| 58 | }) |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 59 | return model; |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * Callback when table row action clicked |
| 64 | * Emits user desired action and associated row data to |
| 65 | * parent controller |
| 66 | * @param {string} action : action type |
| 67 | * @param {any} row : user object |
| 68 | */ |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 69 | this.onEmitTableAction = (action, row) => { |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 70 | if (action !== undefined && row !== undefined) { |
| 71 | const value = {action, row}; |
| 72 | this.emitAction({value}); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * onInit Component lifecycle hooked |
| 78 | */ |
| 79 | this.$onInit = () => { |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 80 | if (this.model === undefined) { |
| 81 | console.log('<bmc-table> Component is missing "model" attribute.'); |
| 82 | return; |
| 83 | } |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 84 | this.model = setModel(this.model); |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 85 | this.rowActionsEnabled = |
| 86 | this.rowActionsEnabled === undefined ? false : true; |
| 87 | if (this.rowActionsEnabled) { |
| 88 | // If table actions are enabled push an empty |
| 89 | // string to the header array to account for additional |
| 90 | // table actions cell |
| 91 | this.model.header.push(''); |
| 92 | } |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 93 | }; |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * Register bmcTable component |
| 98 | */ |
| 99 | angular.module('app.common.components').component('bmcTable', { |
| 100 | template: require('./table.html'), |
| 101 | controller: TableController, |
Yoshie Muranaka | 8c80dbd | 2019-08-08 10:58:04 -0500 | [diff] [blame] | 102 | bindings: {model: '<', rowActionsEnabled: '<', size: '<', emitAction: '&'} |
Yoshie Muranaka | fa56273 | 2019-07-17 11:23:15 -0500 | [diff] [blame] | 103 | }) |
| 104 | })(window.angular); |