Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 1 | window.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 Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 41 | 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 Muranaka | 49001e2 | 2019-09-16 10:33:16 -0700 | [diff] [blame] | 58 | * onChanges Component lifecycle hook |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 59 | */ |
Yoshie Muranaka | 49001e2 | 2019-09-16 10:33:16 -0700 | [diff] [blame] | 60 | this.$onChanges = () => { |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 61 | 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 Muranaka | 49001e2 | 2019-09-16 10:33:16 -0700 | [diff] [blame] | 73 | ng-repeat="action in $ctrl.actions track by $index" |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 74 | ng-disabled="!action.enabled" |
| 75 | ng-click="$ctrl.onClick(action.type)"> |
Yoshie Muranaka | 432f02c | 2019-11-11 10:27:06 -0800 | [diff] [blame] | 76 | <icon ng-if="action.file" ng-file="{{action.file}}"></icon> |
| 77 | <span ng-if="!action.file">{{action.type}}</span> |
Yoshie Muranaka | bb68879 | 2019-08-12 09:31:52 -0500 | [diff] [blame] | 78 | </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); |