Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {LitElement, html, css} from 'lit-element'; |
| 6 | import page from 'page'; |
| 7 | import qs from 'qs'; |
| 8 | import 'elements/chops/chops-button/chops-button.js'; |
| 9 | import 'elements/chops/chops-dialog/chops-dialog.js'; |
| 10 | import {SHARED_STYLES} from 'shared/shared-styles.js'; |
| 11 | import {parseColSpec} from 'shared/issue-fields.js'; |
| 12 | |
| 13 | /** |
| 14 | * `<mr-change-columns>` |
| 15 | * |
| 16 | * Dialog where the user can change columns on the list view. |
| 17 | * |
| 18 | */ |
| 19 | export class MrChangeColumns extends LitElement { |
| 20 | /** @override */ |
| 21 | static get styles() { |
| 22 | return [ |
| 23 | SHARED_STYLES, |
| 24 | css` |
| 25 | .edit-actions { |
| 26 | margin: 0.5em 0; |
| 27 | text-align: right; |
| 28 | } |
| 29 | .input-grid { |
| 30 | align-items: center; |
| 31 | width: 800px; |
| 32 | max-width: 100%; |
| 33 | } |
| 34 | input { |
| 35 | box-sizing: border-box; |
| 36 | padding: 0.25em 4px; |
| 37 | } |
| 38 | `, |
| 39 | ]; |
| 40 | } |
| 41 | |
| 42 | /** @override */ |
| 43 | render() { |
| 44 | return html` |
| 45 | <chops-dialog closeOnOutsideClick> |
| 46 | <h3 class="medium-heading">Change list columns</h3> |
| 47 | <form id="changeColumns" @submit=${this._save}> |
| 48 | <div class="input-grid"> |
| 49 | <label for="columnsInput">Columns: </label> |
| 50 | <input |
| 51 | id="columnsInput" |
| 52 | placeholder="Edit columns..." |
| 53 | value=${this.columns.join(' ')} |
| 54 | /> |
| 55 | </div> |
| 56 | <div class="edit-actions"> |
| 57 | <chops-button |
| 58 | @click=${this.close} |
| 59 | class="de-emphasized discard-button" |
| 60 | > |
| 61 | Discard |
| 62 | </chops-button> |
| 63 | <chops-button |
| 64 | @click=${this._save} |
| 65 | class="emphasized" |
| 66 | > |
| 67 | Update columns |
| 68 | </chops-button> |
| 69 | </div> |
| 70 | </form> |
| 71 | </chops-dialog> |
| 72 | `; |
| 73 | } |
| 74 | |
| 75 | /** @override */ |
| 76 | static get properties() { |
| 77 | return { |
| 78 | /** |
| 79 | * Array of the currently configured issue columns, used to set |
| 80 | * the default value. |
| 81 | */ |
| 82 | columns: {type: Array}, |
| 83 | /** |
| 84 | * Parsed query params for the current page, to be used in |
| 85 | * navigation. |
| 86 | */ |
| 87 | queryParams: {type: Object}, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | /** @override */ |
| 92 | constructor() { |
| 93 | super(); |
| 94 | |
| 95 | this.columns = []; |
| 96 | this.queryParams = {}; |
| 97 | |
| 98 | this._page = page; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Abstract out the computation of the current page. Useful for testing. |
| 103 | */ |
| 104 | get _currentPage() { |
| 105 | return window.location.pathname; |
| 106 | } |
| 107 | |
| 108 | /** Updates the URL query params with the new columns. */ |
| 109 | save() { |
| 110 | const input = this.shadowRoot.querySelector('#columnsInput'); |
| 111 | const newColumns = parseColSpec(input.value); |
| 112 | |
| 113 | const params = {...this.queryParams}; |
| 114 | params.colspec = newColumns.join('+'); |
| 115 | |
| 116 | // TODO(zhangtiff): Create a shared function to change only |
| 117 | // query params in a URL. |
| 118 | this._page(`${this._currentPage}?${qs.stringify(params)}`); |
| 119 | |
| 120 | this.close(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Handles form submit events. |
| 125 | * @param {Event} e A click or submit event. |
| 126 | */ |
| 127 | _save(e) { |
| 128 | e.preventDefault(); |
| 129 | this.save(); |
| 130 | } |
| 131 | |
| 132 | /** Opens and resets this dialog. */ |
| 133 | open() { |
| 134 | this.reset(); |
| 135 | const dialog = this.shadowRoot.querySelector('chops-dialog'); |
| 136 | dialog.open(); |
| 137 | } |
| 138 | |
| 139 | /** Closes this dialog. */ |
| 140 | close() { |
| 141 | const dialog = this.shadowRoot.querySelector('chops-dialog'); |
| 142 | dialog.close(); |
| 143 | } |
| 144 | |
| 145 | /** Resets the form in this dialog. */ |
| 146 | reset() { |
| 147 | this.shadowRoot.querySelector('form').reset(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | customElements.define('mr-change-columns', MrChangeColumns); |