Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Description of this file. |
| 3 | */ |
| 4 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 5 | // Use of this source code is governed by a BSD-style license that can be |
| 6 | // found in the LICENSE file. |
| 7 | |
| 8 | import {LitElement, html} from 'lit-element'; |
| 9 | import {store, connectStore} from 'reducers/base.js'; |
| 10 | import qs from 'qs'; |
| 11 | import * as sitewide from 'reducers/sitewide.js'; |
| 12 | import 'elements/framework/mr-issue-list/mr-show-columns-dropdown.js'; |
| 13 | import {parseColSpec} from 'shared/issue-fields.js'; |
| 14 | import {equalsIgnoreCase} from 'shared/helpers.js'; |
| 15 | import {DEFAULT_ISSUE_FIELD_LIST} from 'shared/issue-fields.js'; |
| 16 | |
| 17 | /** |
| 18 | * `<ezt-show-columns-connector>` |
| 19 | * |
| 20 | * Glue component to make "Show columns" dropdown work on EZT. |
| 21 | * |
| 22 | */ |
| 23 | export class EztShowColumnsConnector extends connectStore(LitElement) { |
| 24 | /** @override */ |
| 25 | render() { |
| 26 | return html` |
| 27 | <mr-show-columns-dropdown |
| 28 | .defaultFields=${DEFAULT_ISSUE_FIELD_LIST} |
| 29 | .columns=${this.columns} |
| 30 | .phaseNames=${this.phaseNames} |
| 31 | .onHideColumn=${(name) => this.onHideColumn(name)} |
| 32 | .onShowColumn=${(name) => this.onShowColumn(name)} |
| 33 | ></mr-show-columns-dropdown> |
| 34 | `; |
| 35 | } |
| 36 | |
| 37 | /** @override */ |
| 38 | static get properties() { |
| 39 | return { |
| 40 | initialColumns: {type: Array}, |
| 41 | hiddenColumns: {type: Object}, |
| 42 | queryParams: {type: Object}, |
| 43 | colspec: {type: String}, |
| 44 | phasespec: {type: String}, |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | /** @override */ |
| 49 | constructor() { |
| 50 | super(); |
| 51 | this.hiddenColumns = new Set(); |
| 52 | this.queryParams = {}; |
| 53 | } |
| 54 | |
| 55 | /** @override */ |
| 56 | stateChanged(state) { |
| 57 | this.queryParams = sitewide.queryParams(state); |
| 58 | } |
| 59 | |
| 60 | get columns() { |
| 61 | return this.initialColumns.filter((_, i) => |
| 62 | !this.hiddenColumns.has(i)); |
| 63 | } |
| 64 | |
| 65 | get initialColumns() { |
| 66 | // EZT will always pass in a colspec. |
| 67 | return parseColSpec(this.colspec); |
| 68 | } |
| 69 | |
| 70 | get phaseNames() { |
| 71 | return parseColSpec(this.phasespec); |
| 72 | } |
| 73 | |
| 74 | onHideColumn(colName) { |
| 75 | // Custom column hiding logic to avoid reloading the |
| 76 | // EZT list page when a user hides a column. |
| 77 | const colIndex = this.initialColumns.findIndex( |
| 78 | (col) => equalsIgnoreCase(col, colName)); |
| 79 | |
| 80 | // Legacy code integration. |
| 81 | TKR_toggleColumn('hide_col_' + colIndex); |
| 82 | |
| 83 | this.hiddenColumns.add(colIndex); |
| 84 | |
| 85 | this.reflectColumnsToQueryParams(); |
| 86 | this.requestUpdate(); |
| 87 | |
| 88 | // Don't continue navigation. |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | onShowColumn(colName) { |
| 93 | const colIndex = this.initialColumns.findIndex( |
| 94 | (col) => equalsIgnoreCase(col, colName)); |
| 95 | if (colIndex >= 0) { |
| 96 | this.hiddenColumns.delete(colIndex); |
| 97 | TKR_toggleColumn('hide_col_' + colIndex); |
| 98 | |
| 99 | this.reflectColumnsToQueryParams(); |
| 100 | this.requestUpdate(); |
| 101 | return false; |
| 102 | } |
| 103 | // Reload the page if this column is not part of the initial |
| 104 | // table render. |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | reflectColumnsToQueryParams() { |
| 109 | this.queryParams.colspec = this.columns.join(' '); |
| 110 | |
| 111 | // Make sure the column changes in the URL. |
| 112 | window.history.replaceState({}, '', '?' + qs.stringify(this.queryParams)); |
| 113 | |
| 114 | store.dispatch(sitewide.setQueryParams(this.queryParams)); |
| 115 | } |
| 116 | } |
| 117 | customElements.define('ezt-show-columns-connector', EztShowColumnsConnector); |