Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2018 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Chris Poucet | 55cbccb | 2021-11-16 03:17:06 +0100 | [diff] [blame] | 17 | import {Subscription} from 'rxjs'; |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 18 | import '@polymer/iron-icon/iron-icon'; |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 19 | import '@polymer/iron-a11y-announcer/iron-a11y-announcer'; |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 20 | import '../../../styles/shared-styles'; |
| 21 | import '../../shared/gr-button/gr-button'; |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 22 | import {DiffViewMode} from '../../../constants/constants'; |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 23 | import {PolymerElement} from '@polymer/polymer/polymer-element'; |
| 24 | import {htmlTemplate} from './gr-diff-mode-selector_html'; |
| 25 | import {customElement, property} from '@polymer/decorators'; |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 26 | import {IronA11yAnnouncer} from '@polymer/iron-a11y-announcer/iron-a11y-announcer'; |
Milutin Kristofic | be0b516 | 2020-09-01 11:22:29 +0200 | [diff] [blame] | 27 | import {FixIronA11yAnnouncer} from '../../../types/types'; |
Chris Poucet | c6e880b | 2021-11-15 19:57:06 +0100 | [diff] [blame] | 28 | import {getAppContext} from '../../../services/app-context'; |
Ben Rohlfs | 62278c8 | 2021-03-12 11:35:56 +0100 | [diff] [blame] | 29 | import {fireIronAnnounce} from '../../../utils/event-util'; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 30 | |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 31 | @customElement('gr-diff-mode-selector') |
Ben Rohlfs | 27c856da | 2021-03-12 14:09:10 +0100 | [diff] [blame] | 32 | export class GrDiffModeSelector extends PolymerElement { |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 33 | static get template() { |
| 34 | return htmlTemplate; |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 37 | @property({type: String, notify: true}) |
Dhruv Srivastava | 80d34f4 | 2021-10-18 16:45:02 +0200 | [diff] [blame] | 38 | mode: DiffViewMode = DiffViewMode.SIDE_BY_SIDE; |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * If set to true, the user's preference will be updated every time a |
| 42 | * button is tapped. Don't set to true if there is no user. |
| 43 | */ |
| 44 | @property({type: Boolean}) |
| 45 | saveOnChange = false; |
| 46 | |
Milutin Kristofic | 3828cc3 | 2021-07-28 14:49:48 +0200 | [diff] [blame] | 47 | @property({type: Boolean}) |
| 48 | showTooltipBelow = false; |
| 49 | |
Chris Poucet | b24bd7f | 2021-11-22 21:07:20 +0100 | [diff] [blame] | 50 | // Private but accessed by tests. |
| 51 | readonly browserModel = getAppContext().browserModel; |
| 52 | |
Chris Poucet | efcdb20 | 2021-11-22 23:54:26 +0100 | [diff] [blame^] | 53 | private readonly userModel = getAppContext().userModel; |
Ben Rohlfs | 43935a4 | 2020-12-01 19:14:09 +0100 | [diff] [blame] | 54 | |
Chris Poucet | 55cbccb | 2021-11-16 03:17:06 +0100 | [diff] [blame] | 55 | private subscriptions: Subscription[] = []; |
Dhruv Srivastava | 80d34f4 | 2021-10-18 16:45:02 +0200 | [diff] [blame] | 56 | |
| 57 | constructor() { |
| 58 | super(); |
| 59 | } |
| 60 | |
Chris Poucet | 59dad57 | 2021-08-20 15:25:36 +0000 | [diff] [blame] | 61 | override connectedCallback() { |
Ben Rohlfs | 5f520da | 2021-03-10 14:58:43 +0100 | [diff] [blame] | 62 | super.connectedCallback(); |
Chris Poucet | caeea1b | 2021-08-19 22:12:56 +0000 | [diff] [blame] | 63 | ( |
| 64 | IronA11yAnnouncer as unknown as FixIronA11yAnnouncer |
| 65 | ).requestAvailability(); |
Chris Poucet | 55cbccb | 2021-11-16 03:17:06 +0100 | [diff] [blame] | 66 | this.subscriptions.push( |
Chris Poucet | b24bd7f | 2021-11-22 21:07:20 +0100 | [diff] [blame] | 67 | this.browserModel.diffViewMode$.subscribe( |
| 68 | diffView => (this.mode = diffView) |
| 69 | ) |
Chris Poucet | 55cbccb | 2021-11-16 03:17:06 +0100 | [diff] [blame] | 70 | ); |
Dhruv Srivastava | 80d34f4 | 2021-10-18 16:45:02 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | override disconnectedCallback() { |
Chris Poucet | 55cbccb | 2021-11-16 03:17:06 +0100 | [diff] [blame] | 74 | for (const s of this.subscriptions) { |
| 75 | s.unsubscribe(); |
| 76 | } |
| 77 | this.subscriptions = []; |
| 78 | super.disconnectedCallback(); |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 81 | /** |
| 82 | * Set the mode. If save on change is enabled also update the preference. |
| 83 | */ |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 84 | setMode(newMode: DiffViewMode) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 85 | if (this.saveOnChange && this.mode && this.mode !== newMode) { |
Chris Poucet | efcdb20 | 2021-11-22 23:54:26 +0100 | [diff] [blame^] | 86 | this.userModel.updatePreferences({diff_view: newMode}); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 87 | } |
| 88 | this.mode = newMode; |
David Ostrovsky | f91f966 | 2021-02-22 19:34:37 +0100 | [diff] [blame] | 89 | let announcement; |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 90 | if (this.isUnifiedSelected(newMode)) { |
David Ostrovsky | f91f966 | 2021-02-22 19:34:37 +0100 | [diff] [blame] | 91 | announcement = 'Changed diff view to unified'; |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 92 | } else if (this.isSideBySideSelected(newMode)) { |
David Ostrovsky | f91f966 | 2021-02-22 19:34:37 +0100 | [diff] [blame] | 93 | announcement = 'Changed diff view to side by side'; |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 94 | } |
David Ostrovsky | f91f966 | 2021-02-22 19:34:37 +0100 | [diff] [blame] | 95 | if (announcement) { |
Ben Rohlfs | 62278c8 | 2021-03-12 11:35:56 +0100 | [diff] [blame] | 96 | fireIronAnnounce(this, announcement); |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 97 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Paladox none | 178838d | 2021-08-17 02:52:53 +0000 | [diff] [blame] | 100 | _computeSideBySideSelected(mode?: DiffViewMode) { |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 101 | return mode === DiffViewMode.SIDE_BY_SIDE ? 'selected' : ''; |
| 102 | } |
| 103 | |
Paladox none | 178838d | 2021-08-17 02:52:53 +0000 | [diff] [blame] | 104 | _computeUnifiedSelected(mode?: DiffViewMode) { |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 105 | return mode === DiffViewMode.UNIFIED ? 'selected' : ''; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Paladox none | 178838d | 2021-08-17 02:52:53 +0000 | [diff] [blame] | 108 | isSideBySideSelected(mode?: DiffViewMode) { |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 109 | return mode === DiffViewMode.SIDE_BY_SIDE; |
| 110 | } |
| 111 | |
Paladox none | 178838d | 2021-08-17 02:52:53 +0000 | [diff] [blame] | 112 | isUnifiedSelected(mode?: DiffViewMode) { |
Milutin Kristofic | 88b0408 | 2020-08-20 17:00:30 +0200 | [diff] [blame] | 113 | return mode === DiffViewMode.UNIFIED; |
| 114 | } |
| 115 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 116 | _handleSideBySideTap() { |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 117 | this.setMode(DiffViewMode.SIDE_BY_SIDE); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | _handleUnifiedTap() { |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 121 | this.setMode(DiffViewMode.UNIFIED); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Ben Rohlfs | 96caad9 | 2020-08-14 21:32:38 +0200 | [diff] [blame] | 125 | declare global { |
| 126 | interface HTMLElementTagNameMap { |
| 127 | 'gr-diff-mode-selector': GrDiffModeSelector; |
| 128 | } |
| 129 | } |