Get rid of some global variables - Part 7

* Replace the following global variables with named imports:
  - GrEventHelper
  - GrPluginRestApi
  - GrRepoApi
* Update gr-app-global-var-init.js

Change-Id: If1ac4eb7e6acd4191486012a37da0ff90a143e80
This commit is contained in:
Dmitrii Filippov
2020-04-06 16:40:13 +02:00
parent c1afe41564
commit 67d206b776
10 changed files with 251 additions and 265 deletions

View File

@@ -166,10 +166,7 @@ module.exports = {
// Instead export variables from modules // Instead export variables from modules
// TODO(dmfilippov): Remove global variables from polygerrit // TODO(dmfilippov): Remove global variables from polygerrit
"Gerrit": "readonly", "Gerrit": "readonly",
"GrEventHelper": "readonly",
"GrPluginActionContext": "readonly", "GrPluginActionContext": "readonly",
"GrPluginRestApi": "readonly",
"GrRepoApi": "readonly",
"GrReporting": "readonly", "GrReporting": "readonly",
"GrSettingsApi": "readonly", "GrSettingsApi": "readonly",
"GrStylesApi": "readonly", "GrStylesApi": "readonly",

View File

@@ -58,6 +58,9 @@ import {GrAnnotationActionsInterface} from './shared/gr-js-api-interface/gr-anno
import {GrChangeMetadataApi} from './plugins/gr-change-metadata-api/gr-change-metadata-api.js'; import {GrChangeMetadataApi} from './plugins/gr-change-metadata-api/gr-change-metadata-api.js';
import {GrEmailSuggestionsProvider} from '../scripts/gr-email-suggestions-provider/gr-email-suggestions-provider.js'; import {GrEmailSuggestionsProvider} from '../scripts/gr-email-suggestions-provider/gr-email-suggestions-provider.js';
import {GrGroupSuggestionsProvider} from '../scripts/gr-group-suggestions-provider/gr-group-suggestions-provider.js'; import {GrGroupSuggestionsProvider} from '../scripts/gr-group-suggestions-provider/gr-group-suggestions-provider.js';
import {GrEventHelper} from './plugins/gr-event-helper/gr-event-helper.js';
import {GrPluginRestApi} from './shared/gr-js-api-interface/gr-plugin-rest-api.js';
import {GrRepoApi} from './plugins/gr-repo-api/gr-repo-api.js';
export function initGlobalVariables() { export function initGlobalVariables() {
window.GrDisplayNameUtils = GrDisplayNameUtils; window.GrDisplayNameUtils = GrDisplayNameUtils;
@@ -99,4 +102,7 @@ export function initGlobalVariables() {
window.GrChangeMetadataApi = GrChangeMetadataApi; window.GrChangeMetadataApi = GrChangeMetadataApi;
window.GrEmailSuggestionsProvider = GrEmailSuggestionsProvider; window.GrEmailSuggestionsProvider = GrEmailSuggestionsProvider;
window.GrGroupSuggestionsProvider = GrGroupSuggestionsProvider; window.GrGroupSuggestionsProvider = GrGroupSuggestionsProvider;
window.GrEventHelper = GrEventHelper;
window.GrPluginRestApi = GrPluginRestApi;
window.GrRepoApi = GrRepoApi;
} }

View File

@@ -24,16 +24,13 @@ $_documentContainer.innerHTML = `<dom-module id="gr-event-helper">
document.head.appendChild($_documentContainer.content); document.head.appendChild($_documentContainer.content);
(function(window) { /** @constructor */
'use strict'; export function GrEventHelper(element) {
/** @constructor */
function GrEventHelper(element) {
this.element = element; this.element = element;
this._unsubscribers = []; this._unsubscribers = [];
} }
/** /**
* Add a callback to arbitrary event. * Add a callback to arbitrary event.
* The callback may return false to prevent event bubbling. * The callback may return false to prevent event bubbling.
* *
@@ -41,40 +38,40 @@ document.head.appendChild($_documentContainer.content);
* @param {function(Event):boolean} callback * @param {function(Event):boolean} callback
* @return {function()} Unsubscribe function. * @return {function()} Unsubscribe function.
*/ */
GrEventHelper.prototype.on = function(event, callback) { GrEventHelper.prototype.on = function(event, callback) {
return this._listen(this.element, callback, {event}); return this._listen(this.element, callback, {event});
}; };
/** /**
* Alias of onClick * Alias of onClick
* *
* @see onClick * @see onClick
*/ */
GrEventHelper.prototype.onTap = function(callback) { GrEventHelper.prototype.onTap = function(callback) {
return this._listen(this.element, callback); return this._listen(this.element, callback);
}; };
/** /**
* Add a callback to element click or touch. * Add a callback to element click or touch.
* The callback may return false to prevent event bubbling. * The callback may return false to prevent event bubbling.
* *
* @param {function(Event):boolean} callback * @param {function(Event):boolean} callback
* @return {function()} Unsubscribe function. * @return {function()} Unsubscribe function.
*/ */
GrEventHelper.prototype.onClick = function(callback) { GrEventHelper.prototype.onClick = function(callback) {
return this._listen(this.element, callback); return this._listen(this.element, callback);
}; };
/** /**
* Alias of captureClick * Alias of captureClick
* *
* @see captureClick * @see captureClick
*/ */
GrEventHelper.prototype.captureTap = function(callback) { GrEventHelper.prototype.captureTap = function(callback) {
return this._listen(this.element.parentElement, callback, {capture: true}); return this._listen(this.element.parentElement, callback, {capture: true});
}; };
/** /**
* Add a callback to element click or touch ahead of normal flow. * Add a callback to element click or touch ahead of normal flow.
* Callback is installed on parent during capture phase. * Callback is installed on parent during capture phase.
* https://www.w3.org/TR/DOM-Level-3-Events/#event-flow * https://www.w3.org/TR/DOM-Level-3-Events/#event-flow
@@ -83,11 +80,11 @@ document.head.appendChild($_documentContainer.content);
* @param {function(Event):boolean} callback * @param {function(Event):boolean} callback
* @return {function()} Unsubscribe function. * @return {function()} Unsubscribe function.
*/ */
GrEventHelper.prototype.captureClick = function(callback) { GrEventHelper.prototype.captureClick = function(callback) {
return this._listen(this.element.parentElement, callback, {capture: true}); return this._listen(this.element.parentElement, callback, {capture: true});
}; };
GrEventHelper.prototype._listen = function(container, callback, opt_options) { GrEventHelper.prototype._listen = function(container, callback, opt_options) {
const capture = opt_options && opt_options.capture; const capture = opt_options && opt_options.capture;
const event = opt_options && opt_options.event || 'click'; const event = opt_options && opt_options.event || 'click';
const handler = e => { const handler = e => {
@@ -110,7 +107,5 @@ document.head.appendChild($_documentContainer.content);
container.removeEventListener(event, handler, capture); container.removeEventListener(event, handler, capture);
this._unsubscribers.push(unsubscribe); this._unsubscribers.push(unsubscribe);
return unsubscribe; return unsubscribe;
}; };
window.GrEventHelper = GrEventHelper;
})(window);

View File

@@ -27,7 +27,6 @@ limitations under the License.
<dom-element id="some-element"> <dom-element id="some-element">
<script type="module"> <script type="module">
import '../../../test/common-test-setup.js'; import '../../../test/common-test-setup.js';
import './gr-event-helper.js';
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js'; import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
Polymer({ Polymer({
is: 'some-element', is: 'some-element',
@@ -51,8 +50,9 @@ Polymer({
<script type="module"> <script type="module">
import '../../../test/common-test-setup.js'; import '../../../test/common-test-setup.js';
import './gr-event-helper.js';
import {addListener} from '@polymer/polymer/lib/utils/gestures.js'; import {addListener} from '@polymer/polymer/lib/utils/gestures.js';
import {GrEventHelper} from './gr-event-helper.js';
suite('gr-event-helper tests', () => { suite('gr-event-helper tests', () => {
let element; let element;
let instance; let instance;

View File

@@ -25,28 +25,22 @@ $_documentContainer.innerHTML = `<dom-module id="gr-repo-api">
document.head.appendChild($_documentContainer.content); document.head.appendChild($_documentContainer.content);
(function(window) { /** @constructor */
'use strict'; export function GrRepoApi(plugin) {
// Prevent redefinition.
if (window.GrRepoApi) { return; }
/** @constructor */
function GrRepoApi(plugin) {
this._hook = null; this._hook = null;
this.plugin = plugin; this.plugin = plugin;
} }
GrRepoApi.prototype._createHook = function(title) { GrRepoApi.prototype._createHook = function(title) {
this._hook = this.plugin.hook('repo-command').onAttached(element => { this._hook = this.plugin.hook('repo-command').onAttached(element => {
const pluginCommand = const pluginCommand =
document.createElement('gr-plugin-repo-command'); document.createElement('gr-plugin-repo-command');
pluginCommand.title = title; pluginCommand.title = title;
element.appendChild(pluginCommand); element.appendChild(pluginCommand);
}); });
}; };
GrRepoApi.prototype.createCommand = function(title, callback) { GrRepoApi.prototype.createCommand = function(title, callback) {
if (this._hook) { if (this._hook) {
console.warn('Already set up.'); console.warn('Already set up.');
return this._hook; return this._hook;
@@ -58,9 +52,9 @@ document.head.appendChild($_documentContainer.content);
} }
}); });
return this; return this;
}; };
GrRepoApi.prototype.onTap = function(callback) { GrRepoApi.prototype.onTap = function(callback) {
if (!this._hook) { if (!this._hook) {
console.warn('Call createCommand first.'); console.warn('Call createCommand first.');
return this; return this;
@@ -69,7 +63,4 @@ document.head.appendChild($_documentContainer.content);
this.plugin.eventHelper(element).on('command-tap', callback); this.plugin.eventHelper(element).on('command-tap', callback);
}); });
return this; return this;
}; };
window.GrRepoApi = GrRepoApi;
})(window);

View File

@@ -34,7 +34,7 @@ limitations under the License.
<script type="module"> <script type="module">
import '../../../test/common-test-setup.js'; import '../../../test/common-test-setup.js';
import '../gr-endpoint-decorator/gr-endpoint-decorator.js'; import '../gr-endpoint-decorator/gr-endpoint-decorator.js';
import './gr-repo-api.js';
suite('gr-repo-api tests', () => { suite('gr-repo-api tests', () => {
let sandbox; let sandbox;
let repoApi; let repoApi;

View File

@@ -16,15 +16,12 @@
*/ */
import '../../../scripts/bundled-polymer.js'; import '../../../scripts/bundled-polymer.js';
import '../../core/gr-reporting/gr-reporting.js'; import '../../core/gr-reporting/gr-reporting.js';
import '../../plugins/gr-event-helper/gr-event-helper.js';
import '../../plugins/gr-repo-api/gr-repo-api.js';
import '../../plugins/gr-settings-api/gr-settings-api.js'; import '../../plugins/gr-settings-api/gr-settings-api.js';
import '../../plugins/gr-styles-api/gr-styles-api.js'; import '../../plugins/gr-styles-api/gr-styles-api.js';
import '../gr-rest-api-interface/gr-rest-api-interface.js'; import '../gr-rest-api-interface/gr-rest-api-interface.js';
import './gr-api-utils.js'; import './gr-api-utils.js';
import './gr-js-api-interface-element.js'; import './gr-js-api-interface-element.js';
import './gr-plugin-action-context.js'; import './gr-plugin-action-context.js';
import './gr-plugin-rest-api.js';
import './gr-public-js-api.js'; import './gr-public-js-api.js';
import './gr-plugin-loader.js'; import './gr-plugin-loader.js';
import './gr-gerrit.js'; import './gr-gerrit.js';

View File

@@ -14,52 +14,50 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
(function(window) {
'use strict';
let restApi; let restApi;
function getRestApi() { function getRestApi() {
if (!restApi) { if (!restApi) {
restApi = document.createElement('gr-rest-api-interface'); restApi = document.createElement('gr-rest-api-interface');
} }
return restApi; return restApi;
} }
function GrPluginRestApi(opt_prefix) { export function GrPluginRestApi(opt_prefix) {
this.opt_prefix = opt_prefix || ''; this.opt_prefix = opt_prefix || '';
} }
GrPluginRestApi.prototype.getLoggedIn = function() { GrPluginRestApi.prototype.getLoggedIn = function() {
return getRestApi().getLoggedIn(); return getRestApi().getLoggedIn();
}; };
GrPluginRestApi.prototype.getVersion = function() { GrPluginRestApi.prototype.getVersion = function() {
return getRestApi().getVersion(); return getRestApi().getVersion();
}; };
GrPluginRestApi.prototype.getConfig = function() { GrPluginRestApi.prototype.getConfig = function() {
return getRestApi().getConfig(); return getRestApi().getConfig();
}; };
GrPluginRestApi.prototype.invalidateReposCache = function() { GrPluginRestApi.prototype.invalidateReposCache = function() {
getRestApi().invalidateReposCache(); getRestApi().invalidateReposCache();
}; };
GrPluginRestApi.prototype.getAccount = function() { GrPluginRestApi.prototype.getAccount = function() {
return getRestApi().getAccount(); return getRestApi().getAccount();
}; };
GrPluginRestApi.prototype.getAccountCapabilities = function(capabilities) { GrPluginRestApi.prototype.getAccountCapabilities = function(capabilities) {
return getRestApi().getAccountCapabilities(capabilities); return getRestApi().getAccountCapabilities(capabilities);
}; };
GrPluginRestApi.prototype.getRepos = GrPluginRestApi.prototype.getRepos =
function(filter, reposPerPage, opt_offset) { function(filter, reposPerPage, opt_offset) {
return getRestApi().getRepos(filter, reposPerPage, opt_offset); return getRestApi().getRepos(filter, reposPerPage, opt_offset);
}; };
/** /**
* Fetch and return native browser REST API Response. * Fetch and return native browser REST API Response.
* *
* @param {string} method HTTP Method (GET, POST, etc) * @param {string} method HTTP Method (GET, POST, etc)
@@ -69,13 +67,13 @@
* passed as null sometimes. * passed as null sometimes.
* @return {!Promise} * @return {!Promise}
*/ */
GrPluginRestApi.prototype.fetch = function(method, url, opt_payload, GrPluginRestApi.prototype.fetch = function(method, url, opt_payload,
opt_errFn, opt_contentType) { opt_errFn, opt_contentType) {
return getRestApi().send(method, this.opt_prefix + url, opt_payload, return getRestApi().send(method, this.opt_prefix + url, opt_payload,
opt_errFn, opt_contentType); opt_errFn, opt_contentType);
}; };
/** /**
* Fetch and parse REST API response, if request succeeds. * Fetch and parse REST API response, if request succeeds.
* *
* @param {string} method HTTP Method (GET, POST, etc) * @param {string} method HTTP Method (GET, POST, etc)
@@ -85,7 +83,7 @@
* passed as null sometimes. * passed as null sometimes.
* @return {!Promise} resolves on success, rejects on error. * @return {!Promise} resolves on success, rejects on error.
*/ */
GrPluginRestApi.prototype.send = function(method, url, opt_payload, GrPluginRestApi.prototype.send = function(method, url, opt_payload,
opt_errFn, opt_contentType) { opt_errFn, opt_contentType) {
return this.fetch(method, url, opt_payload, opt_errFn, opt_contentType) return this.fetch(method, url, opt_payload, opt_errFn, opt_contentType)
.then(response => { .then(response => {
@@ -101,39 +99,39 @@
return getRestApi().getResponseObject(response); return getRestApi().getResponseObject(response);
} }
}); });
}; };
/** /**
* @param {string} url URL without base path or plugin prefix * @param {string} url URL without base path or plugin prefix
* @return {!Promise} resolves on success, rejects on error. * @return {!Promise} resolves on success, rejects on error.
*/ */
GrPluginRestApi.prototype.get = function(url) { GrPluginRestApi.prototype.get = function(url) {
return this.send('GET', url); return this.send('GET', url);
}; };
/** /**
* @param {string} url URL without base path or plugin prefix * @param {string} url URL without base path or plugin prefix
* @return {!Promise} resolves on success, rejects on error. * @return {!Promise} resolves on success, rejects on error.
*/ */
GrPluginRestApi.prototype.post = function(url, opt_payload, opt_errFn, GrPluginRestApi.prototype.post = function(url, opt_payload, opt_errFn,
opt_contentType) { opt_contentType) {
return this.send('POST', url, opt_payload, opt_errFn, opt_contentType); return this.send('POST', url, opt_payload, opt_errFn, opt_contentType);
}; };
/** /**
* @param {string} url URL without base path or plugin prefix * @param {string} url URL without base path or plugin prefix
* @return {!Promise} resolves on success, rejects on error. * @return {!Promise} resolves on success, rejects on error.
*/ */
GrPluginRestApi.prototype.put = function(url, opt_payload, opt_errFn, GrPluginRestApi.prototype.put = function(url, opt_payload, opt_errFn,
opt_contentType) { opt_contentType) {
return this.send('PUT', url, opt_payload, opt_errFn, opt_contentType); return this.send('PUT', url, opt_payload, opt_errFn, opt_contentType);
}; };
/** /**
* @param {string} url URL without base path or plugin prefix * @param {string} url URL without base path or plugin prefix
* @return {!Promise} resolves on 204, rejects on error. * @return {!Promise} resolves on 204, rejects on error.
*/ */
GrPluginRestApi.prototype.delete = function(url) { GrPluginRestApi.prototype.delete = function(url) {
return this.fetch('DELETE', url).then(response => { return this.fetch('DELETE', url).then(response => {
if (response.status !== 204) { if (response.status !== 204) {
return response.text().then(text => { return response.text().then(text => {
@@ -146,7 +144,4 @@
} }
return response; return response;
}); });
}; };
window.GrPluginRestApi = GrPluginRestApi;
})(window);

View File

@@ -26,6 +26,8 @@ limitations under the License.
<script type="module"> <script type="module">
import '../../../test/common-test-setup.js'; import '../../../test/common-test-setup.js';
import './gr-js-api-interface.js'; import './gr-js-api-interface.js';
import {GrPluginRestApi} from './gr-plugin-rest-api.js';
suite('gr-plugin-rest-api tests', () => { suite('gr-plugin-rest-api tests', () => {
let instance; let instance;
let sandbox; let sandbox;

View File

@@ -25,6 +25,9 @@ import {GrPopupInterface} from '../../plugins/gr-popup-interface/gr-popup-interf
import {GrAdminApi} from '../../plugins/gr-admin-api/gr-admin-api.js'; import {GrAdminApi} from '../../plugins/gr-admin-api/gr-admin-api.js';
import {GrAnnotationActionsInterface} from './gr-annotation-actions-js-api.js'; import {GrAnnotationActionsInterface} from './gr-annotation-actions-js-api.js';
import {GrChangeMetadataApi} from '../../plugins/gr-change-metadata-api/gr-change-metadata-api.js'; import {GrChangeMetadataApi} from '../../plugins/gr-change-metadata-api/gr-change-metadata-api.js';
import {GrEventHelper} from '../../plugins/gr-event-helper/gr-event-helper.js';
import {GrPluginRestApi} from './gr-plugin-rest-api.js';
import {GrRepoApi} from '../../plugins/gr-repo-api/gr-repo-api.js';
(function(window) { (function(window) {
'use strict'; 'use strict';