Convert gr-confirm-revert-submission-dialog to typescript

The change converts the following files to typescript:

* elements/change/gr-confirm-revert-submission-dialog/gr-confirm-revert-submission-dialog.ts

Change-Id: I3132b807bde64eddfd78e51d903c3a4b98a5b1c1
This commit is contained in:
Milutin Kristofic
2020-09-01 13:24:09 +02:00
parent bcb64f26c0
commit c98b6156be
4 changed files with 80 additions and 54 deletions

View File

@@ -988,8 +988,7 @@ class GrChangeActions extends GestureEventListeners(
this.$.restAPI.getChanges('', query)
.then(changes => {
this.$.confirmRevertSubmissionDialog.
_populateRevertSubmissionMessage(
this.commitMessage, this.change, changes);
_populateRevertSubmissionMessage(this.change, changes);
this._showActionDialog(this.$.confirmRevertSubmissionDialog);
});
}

View File

@@ -14,26 +14,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import '../../shared/gr-dialog/gr-dialog.js';
import '../../../styles/shared-styles.js';
import '../../shared/gr-js-api-interface/gr-js-api-interface.js';
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-confirm-revert-submission-dialog_html.js';
import '../../shared/gr-dialog/gr-dialog';
import '../../../styles/shared-styles';
import '../../shared/gr-js-api-interface/gr-js-api-interface';
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners';
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin';
import {PolymerElement} from '@polymer/polymer/polymer-element';
import {htmlTemplate} from './gr-confirm-revert-submission-dialog_html';
import {customElement, property} from '@polymer/decorators';
import {JsApiService} from '../../shared/gr-js-api-interface/gr-js-api-types';
import {ChangeInfo} from '../../../types/common';
const ERR_COMMIT_NOT_FOUND =
'Unable to find the commit hash of this change.';
const ERR_COMMIT_NOT_FOUND = 'Unable to find the commit hash of this change.';
const CHANGE_SUBJECT_LIMIT = 50;
/**
* @extends PolymerElement
*/
class GrConfirmRevertSubmissionDialog extends GestureEventListeners(
LegacyElementMixin(PolymerElement)) {
static get template() { return htmlTemplate; }
export interface GrConfirmRevertSubmissionDialog {
$: {
jsAPI: JsApiService & Element;
};
}
@customElement('gr-confirm-revert-submission-dialog')
export class GrConfirmRevertSubmissionDialog extends GestureEventListeners(
LegacyElementMixin(PolymerElement)
) {
static get template() {
return htmlTemplate;
}
static get is() { return 'gr-confirm-revert-submission-dialog'; }
/**
* Fired when the confirm button is pressed.
*
@@ -46,69 +53,88 @@ class GrConfirmRevertSubmissionDialog extends GestureEventListeners(
* @event cancel
*/
static get properties() {
return {
message: String,
commitMessage: String,
};
}
@property({type: String})
message?: string;
_getTrimmedChangeSubject(subject) {
@property({type: String})
commitMessage?: string;
_getTrimmedChangeSubject(subject: string) {
if (!subject) return '';
if (subject.length < CHANGE_SUBJECT_LIMIT) return subject;
return subject.substring(0, CHANGE_SUBJECT_LIMIT) + '...';
}
_modifyRevertSubmissionMsg(change) {
return this.$.jsAPI.modifyRevertSubmissionMsg(change,
this.message, this.commitMessage);
_modifyRevertSubmissionMsg(change?: ChangeInfo) {
if (!change || !this.message || !this.commitMessage) {
return this.message;
}
return this.$.jsAPI.modifyRevertSubmissionMsg(
change,
this.message,
this.commitMessage
);
}
_populateRevertSubmissionMessage(message, change, changes) {
_populateRevertSubmissionMessage(
change?: ChangeInfo,
changes?: ChangeInfo[]
) {
if (change === undefined) {
return;
}
// Follow the same convention of the revert
const commitHash = change.current_revision;
if (!commitHash) {
this.dispatchEvent(new CustomEvent('show-alert', {
detail: {message: ERR_COMMIT_NOT_FOUND},
composed: true, bubbles: true,
}));
this.dispatchEvent(
new CustomEvent('show-alert', {
detail: {message: ERR_COMMIT_NOT_FOUND},
composed: true,
bubbles: true,
})
);
return;
}
const submissionId = change.submission_id;
const revertTitle = 'Revert submission ' + submissionId;
this.changes = changes;
this.message = revertTitle + '\n\n' +
'Reason for revert: <INSERT REASONING HERE>\n';
const revertTitle = `Revert submission ${change.submission_id}`;
this.message =
revertTitle + '\n\n' + 'Reason for revert: <INSERT REASONING HERE>\n';
changes = changes || [];
if (changes.length) {
this.message += 'Reverted Changes:\n';
changes.forEach(change => {
this.message += change.change_id.substring(0, 10) + ': ' +
this._getTrimmedChangeSubject(change.subject) + '\n';
this.message +=
`${change.change_id.substring(0, 10)}: ` +
`${this._getTrimmedChangeSubject(change.subject)}\n`;
});
}
this.message = this._modifyRevertSubmissionMsg(change);
}
_handleConfirmTap(e) {
_handleConfirmTap(e: Event) {
e.preventDefault();
e.stopPropagation();
this.dispatchEvent(new CustomEvent('confirm', {
composed: true, bubbles: false,
}));
this.dispatchEvent(
new CustomEvent('confirm', {
composed: true,
bubbles: false,
})
);
}
_handleCancelTap(e) {
_handleCancelTap(e: Event) {
e.preventDefault();
e.stopPropagation();
this.dispatchEvent(new CustomEvent('cancel', {
composed: true, bubbles: false,
}));
this.dispatchEvent(
new CustomEvent('cancel', {
composed: true,
bubbles: false,
})
);
}
}
customElements.define(GrConfirmRevertSubmissionDialog.is,
GrConfirmRevertSubmissionDialog);
declare global {
interface HTMLElementTagNameMap {
'gr-confirm-revert-submission-dialog': GrConfirmRevertSubmissionDialog;
}
}

View File

@@ -40,7 +40,6 @@ suite('gr-confirm-revert-submission-dialog tests', () => {
test('single line', () => {
assert.isNotOk(element.message);
element._populateRevertSubmissionMessage(
'one line commit\n\nChange-Id: abcdefg\n',
{current_revision: 'abcd123', submission_id: '111'});
const expected = 'Revert submission 111\n\n' +
'Reason for revert: <INSERT REASONING HERE>\n';
@@ -50,7 +49,6 @@ suite('gr-confirm-revert-submission-dialog tests', () => {
test('multi line', () => {
assert.isNotOk(element.message);
element._populateRevertSubmissionMessage(
'many lines\ncommit\n\nmessage\n\nChange-Id: abcdefg\n',
{current_revision: 'abcd123', submission_id: '111'});
const expected = 'Revert submission 111\n\n' +
'Reason for revert: <INSERT REASONING HERE>\n';
@@ -60,7 +58,6 @@ suite('gr-confirm-revert-submission-dialog tests', () => {
test('issue above change id', () => {
assert.isNotOk(element.message);
element._populateRevertSubmissionMessage(
'test \nvery\n\ncommit\n\nBug: Issue 42\nChange-Id: abcdefg\n',
{current_revision: 'abcd123', submission_id: '111'});
const expected = 'Revert submission 111\n\n' +
'Reason for revert: <INSERT REASONING HERE>\n';
@@ -70,7 +67,6 @@ suite('gr-confirm-revert-submission-dialog tests', () => {
test('revert a revert', () => {
assert.isNotOk(element.message);
element._populateRevertSubmissionMessage(
'Revert "one line commit"\n\nChange-Id: abcdefg\n',
{current_revision: 'abcd123', submission_id: '111'});
const expected = 'Revert submission 111\n\n' +
'Reason for revert: <INSERT REASONING HERE>\n';

View File

@@ -33,5 +33,10 @@ export type EventCallback = (...args: any[]) => any;
export interface JsApiService {
getElement(key: TargetElement): HTMLElement;
addEventCallback(eventName: EventType, callback: EventCallback): void;
modifyRevertSubmissionMsg(
change: ChangeInfo,
revertSubmissionMsg: string,
origMsg: string
): string;
// TODO(TS): Add more methods when needed for the TS conversion.
}