summaryrefslogtreecommitdiffstats
path: root/app/common/components/table/table-actions.js
blob: d76d5e5201981abdd2599d44fcece20a4e18d72c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
window.angular && (function(angular) {
  'use strict';

  /**
   *
   * tableActions Component
   *
   * To use:
   * The <table-actions> component expects an 'actions' attribute
   * that should be an array of action objects.
   * Each action object should have 'type', 'enabled', and 'file'
   * properties.
   *
   * actions: [
   *  {type: 'Edit', enabled: true, file: 'icon-edit.svg'},
   *  {type: 'Delete', enabled: false, file: 'icon-trashcan.svg'}
   * ]
   *
   * The 'type' property is a string value that will be emitted to the
   * parent component when clicked.
   *
   * The 'enabled' property is a boolean that will enable/disable
   * the button.
   *
   * The 'file' property is a string value of the filename of the svg icon
   * to provide <icon> directive.
   *
   */

  const controller = function() {
    /**
     * Set defaults if properties undefined
     * @param {[]} actions
     */
    const setActions = (actions = []) => {
      return actions
          .map((action) => {
            if (action.type === undefined) {
              return;
            }
            if (action.file === undefined) {
              action.file = null;
            }
            if (action.enabled === undefined) {
              action.enabled = true;
            }
            return action;
          })
          .filter((action) => action);
    };

    /**
     * Callback when button action clicked
     * Emits the action type to the parent component
     */
    this.onClick = (action) => {
      this.emitAction({action});
    };

    /**
     * onChanges Component lifecycle hook
     */
    this.$onChanges = () => {
      this.actions = setActions(this.actions);
    };
  };

  /**
   * Component template
   */
  const template = `
    <button
      class="btn  btn-tertiary"
      type="button"
      aria-label="{{action.type}}"
      ng-repeat="action in $ctrl.actions track by $index"
      ng-disabled="!action.enabled"
      ng-click="$ctrl.onClick(action.type)">
      <icon ng-if="action.file !== null" ng-file="{{action.file}}"></icon>
      <span ng-if="action.file === null">{{action.type}}</span>
    </button>`

  /**
   * Register tableActions component
   */
  angular.module('app.common.components').component('tableActions', {
    controller,
    template,
    bindings: {actions: '<', emitAction: '&'}
  })
})(window.angular);
OpenPOWER on IntegriCloud