Use imported ChangeTableBehavior

This change removes usage of global Gerrit.ChangeTableBehavior and
replace it with direct import. Also, the ChangeTableMixin is removed,
because it was introduced as a workaround for polylint tests and is no
longer needed.

Change-Id: I831d7ba8ea680fe7d4f076e5bed47addd3406946
This commit is contained in:
Dmitrii Filippov
2020-04-03 16:36:05 +02:00
parent 2d24df4486
commit f8038490eb
6 changed files with 99 additions and 127 deletions

View File

@@ -14,124 +14,100 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function(window) {
'use strict';
window.Gerrit = window.Gerrit || {};
/** @polymerBehavior Gerrit.ChangeTableBehavior */
Gerrit.ChangeTableBehavior = {
properties: {
columnNames: {
type: Array,
value: [
'Subject',
'Status',
'Owner',
'Assignee',
'Reviewers',
'Comments',
'Repo',
'Branch',
'Updated',
'Size',
],
readOnly: true,
},
/** @polymerBehavior Gerrit.ChangeTableBehavior */
export const ChangeTableBehavior = {
properties: {
columnNames: {
type: Array,
value: [
'Subject',
'Status',
'Owner',
'Assignee',
'Reviewers',
'Comments',
'Repo',
'Branch',
'Updated',
'Size',
],
readOnly: true,
},
},
/**
* Returns the complement to the given column array
*
* @param {Array} columns
* @return {!Array}
*/
getComplementColumns(columns) {
return this.columnNames.filter(column => !columns.includes(column));
},
/**
* Returns the complement to the given column array
*
* @param {Array} columns
* @return {!Array}
*/
getComplementColumns(columns) {
return this.columnNames.filter(column => !columns.includes(column));
},
/**
* @param {string} columnToCheck
* @param {!Array} columnsToDisplay
* @return {boolean}
*/
isColumnHidden(columnToCheck, columnsToDisplay) {
if ([columnsToDisplay, columnToCheck].some(arg => arg === undefined)) {
return false;
}
return !columnsToDisplay.includes(columnToCheck);
},
/**
* @param {string} columnToCheck
* @param {!Array} columnsToDisplay
* @return {boolean}
*/
isColumnHidden(columnToCheck, columnsToDisplay) {
if ([columnsToDisplay, columnToCheck].some(arg => arg === undefined)) {
return false;
}
return !columnsToDisplay.includes(columnToCheck);
},
/**
* Is the column disabled by a server config or experiment? For example the
* assignee feature might be disabled and thus the corresponding column is
* also disabled.
*
* @param {string} column
* @param {Object} config
* @param {!Array<string>} experiments
* @return {boolean}
*/
isColumnEnabled(column, config, experiments) {
if (!config || !config.change) return true;
if (column === 'Assignee') return !!config.change.enable_assignee;
if (column === 'Comments') return experiments.includes('comments-column');
if (column === 'Reviewers') return !!config.change.enable_attention_set;
return true;
},
/**
* Is the column disabled by a server config or experiment? For example the
* assignee feature might be disabled and thus the corresponding column is
* also disabled.
*
* @param {string} column
* @param {Object} config
* @param {!Array<string>} experiments
* @return {boolean}
*/
isColumnEnabled(column, config, experiments) {
if (!config || !config.change) return true;
if (column === 'Assignee') return !!config.change.enable_assignee;
if (column === 'Comments') return experiments.includes('comments-column');
if (column === 'Reviewers') return !!config.change.enable_attention_set;
return true;
},
/**
* @param {!Array<string>} columns
* @param {Object} config
* @param {!Array<string>} experiments
* @return {!Array<string>} enabled columns, see isColumnEnabled().
*/
getEnabledColumns(columns, config, experiments) {
return columns.filter(
col => this.isColumnEnabled(col, config, experiments));
},
/**
* @param {!Array<string>} columns
* @param {Object} config
* @param {!Array<string>} experiments
* @return {!Array<string>} enabled columns, see isColumnEnabled().
*/
getEnabledColumns(columns, config, experiments) {
return columns.filter(
col => this.isColumnEnabled(col, config, experiments));
},
/**
* The Project column was renamed to Repo, but some users may have
* preferences that use its old name. If that column is found, rename it
* before use.
*
* @param {!Array<string>} columns
* @return {!Array<string>} If the column was renamed, returns a new array
* with the corrected name. Otherwise, it returns the original param.
*/
getVisibleColumns(columns) {
const projectIndex = columns.indexOf('Project');
if (projectIndex === -1) { return columns; }
const newColumns = columns.slice(0);
newColumns[projectIndex] = 'Repo';
return newColumns;
},
};
/**
* The Project column was renamed to Repo, but some users may have
* preferences that use its old name. If that column is found, rename it
* before use.
*
* @param {!Array<string>} columns
* @return {!Array<string>} If the column was renamed, returns a new array
* with the corrected name. Otherwise, it returns the original param.
*/
getVisibleColumns(columns) {
const projectIndex = columns.indexOf('Project');
if (projectIndex === -1) { return columns; }
const newColumns = columns.slice(0);
newColumns[projectIndex] = 'Repo';
return newColumns;
},
};
// eslint-disable-next-line no-unused-vars
function defineEmptyMixin() {
// This is a temporary function.
// Polymer linter doesn't process correctly the following code:
// class MyElement extends Polymer.mixinBehaviors([legacyBehaviors], ...) {...}
// To workaround this issue, the mock mixin is declared in this method.
// In the following changes, legacy behaviors will be converted to mixins.
/**
* @polymer
* @mixinFunction
*/
Gerrit.ChangeTableMixin = base =>
class extends base {
static get properties() {
return {
columnNames: {
type: Array,
},
};
}
isColumnHidden(columnToCheck, columnsToDisplay) {}
};
}
})(window);
// TODO(dmfilippov) Remove the following lines with assignments
// Plugins can use the behavior because it was accessible with
// the global Gerrit... variable. To avoid breaking changes in plugins
// temporary assign global variables.
window.Gerrit = window.Gerrit || {};
window.Gerrit.ChangeTableBehavior = ChangeTableBehavior;

View File

@@ -39,8 +39,8 @@ limitations under the License.
<script type="module">
import '../../test/common-test-setup.js';
import './gr-change-table-behavior.js';
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
import {ChangeTableBehavior} from './gr-change-table-behavior.js';
suite('gr-change-table-behavior tests', () => {
let element;
// eslint-disable-next-line no-unused-vars
@@ -50,7 +50,7 @@ suite('gr-change-table-behavior tests', () => {
// Define a Polymer element that uses this behavior.
Polymer({
is: 'test-element',
behaviors: [Gerrit.ChangeTableBehavior],
behaviors: [ChangeTableBehavior],
});
});

View File

@@ -15,7 +15,6 @@
* limitations under the License.
*/
import '../../../behaviors/gr-change-table-behavior/gr-change-table-behavior.js';
import '../../../behaviors/gr-path-list-behavior/gr-path-list-behavior.js';
import '../../../behaviors/gr-url-encoding-behavior/gr-url-encoding-behavior.js';
import '../../../behaviors/rest-client-behavior/rest-client-behavior.js';
@@ -38,6 +37,7 @@ import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mix
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-change-list-item_html.js';
import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-behavior.js';
import {ChangeTableBehavior} from '../../../behaviors/gr-change-table-behavior/gr-change-table-behavior.js';
const CHANGE_SIZE = {
XS: 10,
@@ -47,7 +47,6 @@ const CHANGE_SIZE = {
};
/**
* @appliesMixin Gerrit.ChangeTableMixin
* @appliesMixin Gerrit.PathListMixin
* @appliesMixin Gerrit.RESTClientMixin
* @appliesMixin Gerrit.URLEncodingMixin
@@ -55,7 +54,7 @@ const CHANGE_SIZE = {
*/
class GrChangeListItem extends mixinBehaviors( [
BaseUrlBehavior,
Gerrit.ChangeTableBehavior,
ChangeTableBehavior,
Gerrit.PathListBehavior,
Gerrit.RESTClientBehavior,
Gerrit.URLEncodingBehavior,

View File

@@ -15,7 +15,6 @@
* limitations under the License.
*/
import '../../../behaviors/gr-change-table-behavior/gr-change-table-behavior.js';
import '../../../behaviors/gr-url-encoding-behavior/gr-url-encoding-behavior.js';
import '../../../behaviors/keyboard-shortcut-behavior/keyboard-shortcut-behavior.js';
import '../../../behaviors/rest-client-behavior/rest-client-behavior.js';
@@ -36,6 +35,7 @@ import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-change-list_html.js';
import {flags} from '../../../services/flags';
import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-behavior.js';
import {ChangeTableBehavior} from '../../../behaviors/gr-change-table-behavior/gr-change-table-behavior.js';
const NUMBER_FIXED_COLUMNS = 3;
const CLOSED_STATUS = ['MERGED', 'ABANDONED'];
@@ -43,7 +43,6 @@ const LABEL_PREFIX_INVALID_PROLOG = 'Invalid-Prolog-Rules-Label-Name--';
const MAX_SHORTCUT_CHARS = 5;
/**
* @appliesMixin Gerrit.ChangeTableMixin
* @appliesMixin Gerrit.KeyboardShortcutMixin
* @appliesMixin Gerrit.RESTClientMixin
* @appliesMixin Gerrit.URLEncodingMixin
@@ -51,7 +50,7 @@ const MAX_SHORTCUT_CHARS = 5;
*/
class GrChangeList extends mixinBehaviors( [
BaseUrlBehavior,
Gerrit.ChangeTableBehavior,
ChangeTableBehavior,
Gerrit.KeyboardShortcutBehavior,
Gerrit.RESTClientBehavior,
Gerrit.URLEncodingBehavior,

View File

@@ -14,8 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import '../../../behaviors/gr-change-table-behavior/gr-change-table-behavior.js';
import '../../../scripts/bundled-polymer.js';
import '../../shared/gr-button/gr-button.js';
import '../../shared/gr-date-formatter/gr-date-formatter.js';
@@ -28,13 +26,13 @@ import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-l
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-change-table-editor_html.js';
import {ChangeTableBehavior} from '../../../behaviors/gr-change-table-behavior/gr-change-table-behavior.js';
/**
* @appliesMixin Gerrit.ChangeTableMixin
* @extends Polymer.Element
*/
class GrChangeTableEditor extends mixinBehaviors( [
Gerrit.ChangeTableBehavior,
ChangeTableBehavior,
], GestureEventListeners(
LegacyElementMixin(
PolymerElement))) {

View File

@@ -48,6 +48,7 @@ import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mix
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-settings-view_html.js';
import {DocsUrlBehavior} from '../../../behaviors/docs-url-behavior/docs-url-behavior.js';
import {ChangeTableBehavior} from '../../../behaviors/gr-change-table-behavior/gr-change-table-behavior.js';
const PREFS_SECTION_FIELDS = [
'changes_per_page',
@@ -78,12 +79,11 @@ const HTTP_AUTH = [
];
/**
* @appliesMixin Gerrit.ChangeTableMixin
* @extends Polymer.Element
*/
class GrSettingsView extends mixinBehaviors( [
DocsUrlBehavior,
Gerrit.ChangeTableBehavior,
ChangeTableBehavior,
], GestureEventListeners(
LegacyElementMixin(
PolymerElement))) {