blob: 34b3f462344e291e59e2cf4ea99c602ad284faf5 [file] [log] [blame]
Yoshie Muranakabb688792019-08-12 09:31:52 -05001window.angular && (function(angular) {
2 'use strict';
3
4 /**
5 *
6 * tableActions Component
7 *
8 * To use:
9 * The <table-actions> component expects an 'actions' attribute
10 * that should be an array of action objects.
11 * Each action object should have 'type', 'enabled', and 'file'
12 * properties.
13 *
14 * actions: [
15 * {type: 'Edit', enabled: true, file: 'icon-edit.svg'},
16 * {type: 'Delete', enabled: false, file: 'icon-trashcan.svg'}
17 * ]
18 *
19 * The 'type' property is a string value that will be emitted to the
20 * parent component when clicked.
21 *
22 * The 'enabled' property is a boolean that will enable/disable
23 * the button.
24 *
25 * The 'file' property is a string value of the filename of the svg icon
26 * to provide <icon> directive.
27 *
28 */
29
30 const controller = function() {
31 /**
32 * Set defaults if properties undefined
33 * @param {[]} actions
34 */
35 const setActions = (actions = []) => {
36 return actions
37 .map((action) => {
38 if (action.type === undefined) {
39 return;
40 }
Yoshie Muranakabb688792019-08-12 09:31:52 -050041 if (action.enabled === undefined) {
42 action.enabled = true;
43 }
44 return action;
45 })
46 .filter((action) => action);
47 };
48
49 /**
50 * Callback when button action clicked
51 * Emits the action type to the parent component
52 */
53 this.onClick = (action) => {
54 this.emitAction({action});
55 };
56
57 /**
Yoshie Muranaka49001e22019-09-16 10:33:16 -070058 * onChanges Component lifecycle hook
Yoshie Muranakabb688792019-08-12 09:31:52 -050059 */
Yoshie Muranaka49001e22019-09-16 10:33:16 -070060 this.$onChanges = () => {
Yoshie Muranakabb688792019-08-12 09:31:52 -050061 this.actions = setActions(this.actions);
62 };
63 };
64
65 /**
66 * Component template
67 */
68 const template = `
69 <button
70 class="btn btn-tertiary"
71 type="button"
72 aria-label="{{action.type}}"
Yoshie Muranaka49001e22019-09-16 10:33:16 -070073 ng-repeat="action in $ctrl.actions track by $index"
Yoshie Muranakabb688792019-08-12 09:31:52 -050074 ng-disabled="!action.enabled"
75 ng-click="$ctrl.onClick(action.type)">
Yoshie Muranaka432f02c2019-11-11 10:27:06 -080076 <icon ng-if="action.file" ng-file="{{action.file}}"></icon>
77 <span ng-if="!action.file">{{action.type}}</span>
Yoshie Muranakabb688792019-08-12 09:31:52 -050078 </button>`
79
80 /**
81 * Register tableActions component
82 */
83 angular.module('app.common.components').component('tableActions', {
84 controller,
85 template,
86 bindings: {actions: '<', emitAction: '&'}
87 })
88})(window.angular);