summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/edit/gr-edit-file-controls/gr-edit-file-controls.ts
blob: e343a0172aa6ef12e98d6165240a4a798a0a2383 (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
92
93
94
95
96
97
98
99
100
101
102
/**
 * @license
 * Copyright 2017 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import '../../shared/gr-dropdown/gr-dropdown';
import {GrEditConstants} from '../gr-edit-constants';
import {sharedStyles} from '../../../styles/shared-styles';
import {FileActionTapEvent} from '../../../types/events';
import {LitElement, css, html} from 'lit';
import {customElement, property} from 'lit/decorators.js';
import {fire} from '../../../utils/event-util';
import {DropdownLink} from '../../../types/common';
import {SpecialFilePath} from '../../../constants/constants';

interface EditAction {
  label: string;
  id: string;
}

@customElement('gr-edit-file-controls')
export class GrEditFileControls extends LitElement {
  @property({type: String})
  filePath?: string;

  @property({type: Array})
  _allFileActions = Object.values(GrEditConstants.Actions);

  static override get styles() {
    return [
      sharedStyles,
      css`
        :host {
          align-items: center;
          display: flex;
          justify-content: flex-end;
        }
        gr-dropdown {
          --gr-dropdown-item-color: var(--link-color);
          --gr-button-padding: var(--spacing-xs) var(--spacing-s);
          --gr-dropdown-item-background-color: transparent;
          --gr-dropdown-item-border: none;
          --gr-dropdown-item-text-transform: uppercase;
        }
        #actions {
          margin-right: var(--spacing-l);
        }
      `,
    ];
  }

  override render() {
    const fileActions = this._computeFileActions(this._allFileActions);
    return html` <gr-dropdown
      id="actions"
      .items=${fileActions}
      down-arrow=""
      vertical-offset="20"
      @tap-item=${this._handleActionTap}
      link=""
      >Actions</gr-dropdown
    >`;
  }

  _handleActionTap(e: CustomEvent<DropdownLink>) {
    e.preventDefault();
    e.stopPropagation();
    const actionId = e.detail.id;
    if (!actionId) return;
    if (!this.filePath) return;
    this._dispatchFileAction(actionId, this.filePath);
  }

  _dispatchFileAction(action: string, path: string) {
    fire(this, 'file-action-tap', {action, path});
  }

  _computeFileActions(actions: EditAction[]): DropdownLink[] {
    // TODO(kaspern): conditionally disable some actions based on file status.
    return actions
      .filter(
        action =>
          this.filePath !== SpecialFilePath.COMMIT_MESSAGE ||
          action.label === GrEditConstants.Actions.OPEN.label
      )
      .map(action => {
        return {
          name: action.label,
          id: action.id,
        };
      });
  }
}

declare global {
  interface HTMLElementTagNameMap {
    'gr-edit-file-controls': GrEditFileControls;
  }
  interface HTMLElementEventMap {
    'file-action-tap': FileActionTapEvent;
  }
}