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 | */ |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 17 | import '../../../scripts/bundled-polymer.js'; |
Wyatt Allen | ffae724 | 2018-02-14 15:58:17 -0800 | [diff] [blame] | 18 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 19 | import '@polymer/iron-icon/iron-icon.js'; |
| 20 | import '../../../styles/shared-styles.js'; |
| 21 | import '../../shared/gr-button/gr-button.js'; |
| 22 | import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js'; |
| 23 | import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js'; |
| 24 | import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js'; |
| 25 | import {PolymerElement} from '@polymer/polymer/polymer-element.js'; |
| 26 | import {htmlTemplate} from './gr-diff-mode-selector_html.js'; |
Wyatt Allen | ffae724 | 2018-02-14 15:58:17 -0800 | [diff] [blame] | 27 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 28 | /** @extends Polymer.Element */ |
| 29 | class GrDiffModeSelector extends GestureEventListeners( |
| 30 | LegacyElementMixin( |
| 31 | PolymerElement)) { |
| 32 | static get template() { return htmlTemplate; } |
| 33 | |
| 34 | static get is() { return 'gr-diff-mode-selector'; } |
| 35 | |
| 36 | static get properties() { |
| 37 | return { |
| 38 | mode: { |
| 39 | type: String, |
| 40 | notify: true, |
| 41 | }, |
| 42 | |
| 43 | /** |
| 44 | * If set to true, the user's preference will be updated every time a |
| 45 | * button is tapped. Don't set to true if there is no user. |
| 46 | */ |
| 47 | saveOnChange: { |
| 48 | type: Boolean, |
| 49 | value: false, |
| 50 | }, |
| 51 | |
| 52 | /** @type {?} */ |
| 53 | _VIEW_MODES: { |
| 54 | type: Object, |
| 55 | readOnly: true, |
| 56 | value: { |
| 57 | SIDE_BY_SIDE: 'SIDE_BY_SIDE', |
| 58 | UNIFIED: 'UNIFIED_DIFF', |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 59 | }, |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 60 | }, |
| 61 | }; |
Dmitrii Filippov | 3fd2b10 | 2019-11-15 16:16:46 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame^] | 64 | /** |
| 65 | * Set the mode. If save on change is enabled also update the preference. |
| 66 | */ |
| 67 | setMode(newMode) { |
| 68 | if (this.saveOnChange && this.mode && this.mode !== newMode) { |
| 69 | this.$.restAPI.savePreferences({diff_view: newMode}); |
| 70 | } |
| 71 | this.mode = newMode; |
| 72 | } |
| 73 | |
| 74 | _computeSelectedClass(diffViewMode, buttonViewMode) { |
| 75 | return buttonViewMode === diffViewMode ? 'selected' : ''; |
| 76 | } |
| 77 | |
| 78 | _handleSideBySideTap() { |
| 79 | this.setMode(this._VIEW_MODES.SIDE_BY_SIDE); |
| 80 | } |
| 81 | |
| 82 | _handleUnifiedTap() { |
| 83 | this.setMode(this._VIEW_MODES.UNIFIED); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | customElements.define(GrDiffModeSelector.is, GrDiffModeSelector); |