1window.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 } 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 /** 58 * onChanges Component lifecycle hook 59 */ 60 this.$onChanges = () => { 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}}" 73 ng-repeat="action in $ctrl.actions track by $index" 74 ng-disabled="!action.enabled" 75 ng-click="$ctrl.onClick(action.type)"> 76 <icon ng-if="action.file" ng-file="{{action.file}}"></icon> 77 <span ng-if="!action.file">{{action.type}}</span> 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);