Get rid of global GrAttributeHelper
* Replace the global GrAttributeHelper variable with named imports.
* Update gr-app-global-var-init.js
Change-Id: I02500a5fc3b8157e107850e1c83b332b8c089522
diff --git a/polygerrit-ui/app/.eslintrc.js b/polygerrit-ui/app/.eslintrc.js
index b8e381b..a540dcc 100644
--- a/polygerrit-ui/app/.eslintrc.js
+++ b/polygerrit-ui/app/.eslintrc.js
@@ -172,7 +172,6 @@
"GrAdminApi": "readonly",
"GrAnnotationActionsContext": "readonly",
"GrAnnotationActionsInterface": "readonly",
- "GrAttributeHelper": "readonly",
"GrChangeActionsInterface": "readonly",
"GrChangeMetadataApi": "readonly",
"GrChangeReplyInterface": "readonly",
diff --git a/polygerrit-ui/app/elements/gr-app-global-var-init.js b/polygerrit-ui/app/elements/gr-app-global-var-init.js
index 1f33fd5..3225788 100644
--- a/polygerrit-ui/app/elements/gr-app-global-var-init.js
+++ b/polygerrit-ui/app/elements/gr-app-global-var-init.js
@@ -24,8 +24,10 @@
import {GrDisplayNameUtils} from '../scripts/gr-display-name-utils/gr-display-name-utils.js';
import {GrAnnotation} from './diff/gr-diff-highlight/gr-annotation.js';
+import {GrAttributeHelper} from './plugins/gr-attribute-helper/gr-attribute-helper.js';
export function initGlobalVariables() {
window.GrDisplayNameUtils = GrDisplayNameUtils;
window.GrAnnotation = GrAnnotation;
+ window.GrAttributeHelper = GrAttributeHelper;
}
diff --git a/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper.js b/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper.js
index 09620ef..d5ebb65 100644
--- a/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper.js
+++ b/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper.js
@@ -24,91 +24,85 @@
document.head.appendChild($_documentContainer.content);
-(function(window) {
- 'use strict';
+/** @constructor */
+export function GrAttributeHelper(element) {
+ this.element = element;
+ this._promises = {};
+}
- /** @constructor */
- function GrAttributeHelper(element) {
- this.element = element;
- this._promises = {};
+GrAttributeHelper.prototype._getChangedEventName = function(name) {
+ return name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() + '-changed';
+};
+
+/**
+ * Returns true if the property is defined on wrapped element.
+ *
+ * @param {string} name
+ * @return {boolean}
+ */
+GrAttributeHelper.prototype._elementHasProperty = function(name) {
+ return this.element[name] !== undefined;
+};
+
+GrAttributeHelper.prototype._reportValue = function(callback, value) {
+ try {
+ callback(value);
+ } catch (e) {
+ console.info(e);
}
+};
- GrAttributeHelper.prototype._getChangedEventName = function(name) {
- return name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() + '-changed';
- };
+/**
+ * Binds callback to property updates.
+ *
+ * @param {string} name Property name.
+ * @param {function(?)} callback
+ * @return {function()} Unbind function.
+ */
+GrAttributeHelper.prototype.bind = function(name, callback) {
+ const attributeChangedEventName = this._getChangedEventName(name);
+ const changedHandler = e => this._reportValue(callback, e.detail.value);
+ const unbind = () => this.element.removeEventListener(
+ attributeChangedEventName, changedHandler);
+ this.element.addEventListener(
+ attributeChangedEventName, changedHandler);
+ if (this._elementHasProperty(name)) {
+ this._reportValue(callback, this.element[name]);
+ }
+ return unbind;
+};
- /**
- * Returns true if the property is defined on wrapped element.
- *
- * @param {string} name
- * @return {boolean}
- */
- GrAttributeHelper.prototype._elementHasProperty = function(name) {
- return this.element[name] !== undefined;
- };
+/**
+ * Get value of the property from wrapped object. Waits for the property
+ * to be initialized if it isn't defined.
+ *
+ * @param {string} name Property name.
+ * @return {!Promise<?>}
+ */
+GrAttributeHelper.prototype.get = function(name) {
+ if (this._elementHasProperty(name)) {
+ return Promise.resolve(this.element[name]);
+ }
+ if (!this._promises[name]) {
+ let resolve;
+ const promise = new Promise(r => resolve = r);
+ const unbind = this.bind(name, value => {
+ resolve(value);
+ unbind();
+ });
+ this._promises[name] = promise;
+ }
+ return this._promises[name];
+};
- GrAttributeHelper.prototype._reportValue = function(callback, value) {
- try {
- callback(value);
- } catch (e) {
- console.info(e);
- }
- };
-
- /**
- * Binds callback to property updates.
- *
- * @param {string} name Property name.
- * @param {function(?)} callback
- * @return {function()} Unbind function.
- */
- GrAttributeHelper.prototype.bind = function(name, callback) {
- const attributeChangedEventName = this._getChangedEventName(name);
- const changedHandler = e => this._reportValue(callback, e.detail.value);
- const unbind = () => this.element.removeEventListener(
- attributeChangedEventName, changedHandler);
- this.element.addEventListener(
- attributeChangedEventName, changedHandler);
- if (this._elementHasProperty(name)) {
- this._reportValue(callback, this.element[name]);
- }
- return unbind;
- };
-
- /**
- * Get value of the property from wrapped object. Waits for the property
- * to be initialized if it isn't defined.
- *
- * @param {string} name Property name.
- * @return {!Promise<?>}
- */
- GrAttributeHelper.prototype.get = function(name) {
- if (this._elementHasProperty(name)) {
- return Promise.resolve(this.element[name]);
- }
- if (!this._promises[name]) {
- let resolve;
- const promise = new Promise(r => resolve = r);
- const unbind = this.bind(name, value => {
- resolve(value);
- unbind();
- });
- this._promises[name] = promise;
- }
- return this._promises[name];
- };
-
- /**
- * Sets value and dispatches event to force notify.
- *
- * @param {string} name Property name.
- * @param {?} value
- */
- GrAttributeHelper.prototype.set = function(name, value) {
- this.element[name] = value;
- this.element.dispatchEvent(
- new CustomEvent(this._getChangedEventName(name), {detail: {value}}));
- };
-
- window.GrAttributeHelper = GrAttributeHelper;
-})(window);
+/**
+ * Sets value and dispatches event to force notify.
+ *
+ * @param {string} name Property name.
+ * @param {?} value
+ */
+GrAttributeHelper.prototype.set = function(name, value) {
+ this.element[name] = value;
+ this.element.dispatchEvent(
+ new CustomEvent(this._getChangedEventName(name), {detail: {value}}));
+};
diff --git a/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper_test.html b/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper_test.html
index fd2fd5b..50f9002 100644
--- a/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper_test.html
+++ b/polygerrit-ui/app/elements/plugins/gr-attribute-helper/gr-attribute-helper_test.html
@@ -27,7 +27,6 @@
<dom-element id="some-element">
<script type="module">
import '../../../test/common-test-setup.js';
-import './gr-attribute-helper.js';
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
Polymer({
is: 'some-element',
@@ -49,8 +48,8 @@
</test-fixture>
<script type="module">
-import '../../../test/common-test-setup.js';
-import './gr-attribute-helper.js';
+import {GrAttributeHelper} from './gr-attribute-helper.js';
+
suite('gr-attribute-helper tests', () => {
let element;
let instance;
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js
index f5ffad8..06c47e5 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js
@@ -17,7 +17,6 @@
import '../../../scripts/bundled-polymer.js';
import '../../core/gr-reporting/gr-reporting.js';
import '../../plugins/gr-admin-api/gr-admin-api.js';
-import '../../plugins/gr-attribute-helper/gr-attribute-helper.js';
import '../../plugins/gr-change-metadata-api/gr-change-metadata-api.js';
import '../../plugins/gr-dom-hooks/gr-dom-hooks.js';
import '../../plugins/gr-event-helper/gr-event-helper.js';
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
index 3cf38e7..df598d2 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
@@ -16,6 +16,7 @@
*/
import {BaseUrlBehavior} from '../../../behaviors/base-url-behavior/base-url-behavior.js';
+import {GrAttributeHelper} from '../../plugins/gr-attribute-helper/gr-attribute-helper.js';
(function(window) {
'use strict';