blob: acd9457e6b73d1f73a92be1a7490f57633b4a150 [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
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 Filippovdaf0ec92020-03-17 11:27:28 +010017import '../../../scripts/bundled-polymer.js';
Wyatt Allenffae7242018-02-14 15:58:17 -080018
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010019import '@polymer/iron-icon/iron-icon.js';
20import '../../../styles/shared-styles.js';
21import '../../shared/gr-button/gr-button.js';
22import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js';
23import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
24import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
25import {PolymerElement} from '@polymer/polymer/polymer-element.js';
26import {htmlTemplate} from './gr-diff-mode-selector_html.js';
Wyatt Allenffae7242018-02-14 15:58:17 -080027
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010028/** @extends Polymer.Element */
29class 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 Filippov3fd2b102019-11-15 16:16:46 +010059 },
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010060 },
61 };
Dmitrii Filippov3fd2b102019-11-15 16:16:46 +010062 }
63
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010064 /**
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
87customElements.define(GrDiffModeSelector.is, GrDiffModeSelector);