blob: cf2b7970ccb50754c7393bfbd51f4a16a24e6ac9 [file] [log] [blame]
Yoshie Muranakafa562732019-07-17 11:23:15 -05001window.angular && (function(angular) {
2 'use strict';
3
4 /**
5 *
Yoshie Muranakabb688792019-08-12 09:31:52 -05006 * bmcTable Component
Yoshie Muranakafa562732019-07-17 11:23:15 -05007 *
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 Muranaka8c80dbd2019-08-08 10:58:04 -050012 * The component accepts a 'row-actions-enabled' attribute,
Yoshie Muranakabb688792019-08-12 09:31:52 -050013 * 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 Muranaka8c80dbd2019-08-08 10:58:04 -050017 * 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 Muranakabb688792019-08-12 09:31:52 -050020 *
Yoshie Muranaka8c80dbd2019-08-08 10:58:04 -050021 * The model object should contain 'header' and 'data'
Yoshie Muranakafa562732019-07-17 11:23:15 -050022 * properties.
23 *
24 * model: {
Yoshie Muranakabb688792019-08-12 09:31:52 -050025 * header: <string>[], // Array of header labels
26 * data: <any>[], // Array of each row object
Yoshie Muranakafa562732019-07-17 11:23:15 -050027 * }
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 Muranakabb688792019-08-12 09:31:52 -050035 * 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 Muranakafa562732019-07-17 11:23:15 -050038 *
Yoshie Muranakabb688792019-08-12 09:31:52 -050039 * The 'rowActionsEnabled' property will render <bmc-table-actions> if set
40 * to true.
Yoshie Muranakafa562732019-07-17 11:23:15 -050041 *
42 */
Yoshie Muranakabb688792019-08-12 09:31:52 -050043
Yoshie Muranakafa562732019-07-17 11:23:15 -050044 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 Muranakafa562732019-07-17 11:23:15 -050059 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 Muranakabb688792019-08-12 09:31:52 -050069 this.onEmitTableAction = (action, row) => {
Yoshie Muranakafa562732019-07-17 11:23:15 -050070 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 Muranakabb688792019-08-12 09:31:52 -050080 if (this.model === undefined) {
81 console.log('<bmc-table> Component is missing "model" attribute.');
82 return;
83 }
Yoshie Muranakafa562732019-07-17 11:23:15 -050084 this.model = setModel(this.model);
Yoshie Muranakabb688792019-08-12 09:31:52 -050085 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 Muranakafa562732019-07-17 11:23:15 -050093 };
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 Muranaka8c80dbd2019-08-08 10:58:04 -0500102 bindings: {model: '<', rowActionsEnabled: '<', size: '<', emitAction: '&'}
Yoshie Muranakafa562732019-07-17 11:23:15 -0500103 })
104})(window.angular);