Update plugin host and avatar to use BaseUrlBehavior

Change-Id: Ibfeb7b2752c9e393c23d4b3a71cceaf53039a665
This commit is contained in:
Thomas Shafer
2017-06-26 16:33:48 -07:00
committed by Viktar Donich
parent aee681d5bb
commit 8e1fde02b2
5 changed files with 87 additions and 15 deletions

View File

@@ -15,6 +15,7 @@ limitations under the License.
-->
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../behaviors/base-url-behavior/base-url-behavior.html">
<link rel="import" href="../../shared/gr-js-api-interface/gr-js-api-interface.html">
<dom-module id="gr-plugin-host">

View File

@@ -24,6 +24,10 @@
},
},
behaviors: [
Gerrit.BaseUrlBehavior,
],
_configChanged(config) {
const jsPlugins = config.js_resource_paths || [];
const htmlPlugins = config.html_resource_paths || [];
@@ -33,21 +37,16 @@
},
_importHtmlPlugins(plugins) {
for (let url of plugins) {
if (!url.startsWith('http')) {
url = '/' + url;
}
for (const url of plugins) {
this.importHref(
url, Gerrit._pluginInstalled, Gerrit._pluginInstalled, true);
this._urlFor(url), Gerrit._pluginInstalled, Gerrit._pluginInstalled,
true);
}
},
_loadJsPlugins(plugins) {
for (let url of plugins) {
if (!url.startsWith('http')) {
url = '/' + url;
}
this._createScriptTag(url);
for (const url of plugins) {
this._createScriptTag(this._urlFor(url));
}
},
@@ -58,5 +57,12 @@
el.onerror = Gerrit._pluginInstalled;
return document.body.appendChild(el);
},
_urlFor(pathOrUrl) {
if (pathOrUrl.startsWith('http')) {
return pathOrUrl;
}
return this.getBaseUrl() + '/' + pathOrUrl;
},
});
})();

View File

@@ -54,7 +54,7 @@ limitations under the License.
assert.isTrue(Gerrit._setPluginsCount.calledWith(3));
});
test('imports html plugins from config', () => {
test('imports relative html plugins from config', () => {
element.config = {
html_resource_paths: ['foo/bar', 'baz'],
};
@@ -64,13 +64,72 @@ limitations under the License.
'/baz', Gerrit._pluginInstalled, Gerrit._pluginInstalled, true));
});
test('imports js plugins from config', () => {
test('imports relative html plugins from config with a base url', () => {
sandbox.stub(element, 'getBaseUrl').returns('/the-base');
element.config = {
html_resource_paths: ['foo/bar', 'baz'],
};
assert.isTrue(element.importHref.calledWith(
'/the-base/foo/bar', Gerrit._pluginInstalled, Gerrit._pluginInstalled,
true));
assert.isTrue(element.importHref.calledWith(
'/the-base/baz', Gerrit._pluginInstalled, Gerrit._pluginInstalled,
true));
});
test('imports absolute html plugins from config', () => {
element.config = {
html_resource_paths: [
'http://example.com/foo/bar',
'https://example.com/baz',
],
};
assert.isTrue(element.importHref.calledWith(
'http://example.com/foo/bar', Gerrit._pluginInstalled,
Gerrit._pluginInstalled, true));
assert.isTrue(element.importHref.calledWith(
'https://example.com/baz', Gerrit._pluginInstalled,
Gerrit._pluginInstalled, true));
});
test('adds js plugins from config to the body', () => {
element.config = {
js_resource_paths: ['foo/bar', 'baz'],
};
assert.isTrue(document.body.appendChild.calledTwice);
});
test('imports relative js plugins from config', () => {
sandbox.stub(element, '_createScriptTag');
element.config = {
js_resource_paths: ['foo/bar', 'http://baz'],
js_resource_paths: ['foo/bar', 'baz'],
};
assert.isTrue(element._createScriptTag.calledWith('/foo/bar'));
assert.isTrue(element._createScriptTag.calledWith('http://baz'));
assert.isTrue(element._createScriptTag.calledWith('/baz'));
});
test('imports relative html plugins from config with a base url', () => {
sandbox.stub(element, '_createScriptTag');
sandbox.stub(element, 'getBaseUrl').returns('/the-base');
element.config = {
js_resource_paths: ['foo/bar', 'baz'],
};
assert.isTrue(element._createScriptTag.calledWith('/the-base/foo/bar'));
assert.isTrue(element._createScriptTag.calledWith('/the-base/baz'));
});
test('imports absolute html plugins from config', () => {
sandbox.stub(element, '_createScriptTag');
element.config = {
js_resource_paths: [
'http://example.com/foo/bar',
'https://example.com/baz',
],
};
assert.isTrue(element._createScriptTag.calledWith(
'http://example.com/foo/bar'));
assert.isTrue(element._createScriptTag.calledWith(
'https://example.com/baz'));
});
});
</script>

View File

@@ -16,6 +16,7 @@ limitations under the License.
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../gr-rest-api-interface/gr-rest-api-interface.html">
<link rel="import" href="../../../behaviors/base-url-behavior/base-url-behavior.html">
<link rel="import" href="../../../styles/shared-styles.html">
<dom-module id="gr-avatar">

View File

@@ -28,6 +28,10 @@
},
},
behaviors: [
Gerrit.BaseUrlBehavior,
],
created() {
this.hidden = true;
},
@@ -64,7 +68,8 @@
return avatars[i].url;
}
}
return '/accounts/' + account._account_id + '/avatar?s=' + this.imageSize;
return this.getBaseUrl() + '/accounts/' +
account._account_id + '/avatar?s=' + this.imageSize;
},
});
})();