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