ES6ify /plugins/*

Bug: Issue 6179
Change-Id: I2ddd0d9e522b06041c08ec2e7902806b4a6924f7
This commit is contained in:
Kasper Nilsson
2017-05-15 17:05:43 -07:00
parent 9278bacdaa
commit d2873235af
4 changed files with 44 additions and 45 deletions

View File

@@ -21,39 +21,39 @@
name: String,
},
_import: function(url) {
return new Promise(function(resolve, reject) {
_import(url) {
return new Promise((resolve, reject) => {
this.importHref(url, resolve, reject);
}.bind(this));
});
},
_applyStyle: function(name) {
var s = document.createElement('style', 'custom-style');
_applyStyle(name) {
const s = document.createElement('style', 'custom-style');
s.setAttribute('include', name);
Polymer.dom(this.root).appendChild(s);
},
ready: function() {
Gerrit.awaitPluginsLoaded().then(function() {
var sharedStyles = Gerrit._styleModules[this.name];
ready() {
Gerrit.awaitPluginsLoaded().then(() => {
const sharedStyles = Gerrit._styleModules[this.name];
if (sharedStyles) {
var pluginUrls = [];
var moduleNames = [];
sharedStyles.reduce(function(result, item) {
const pluginUrls = [];
const moduleNames = [];
sharedStyles.reduce((result, item) => {
if (!result.pluginUrls.includes(item.pluginUrl)) {
result.pluginUrls.push(item.pluginUrl);
}
result.moduleNames.push(item.moduleName);
return result;
}, {pluginUrls: pluginUrls, moduleNames: moduleNames});
}, {pluginUrls, moduleNames});
Promise.all(pluginUrls.map(this._import.bind(this)))
.then(function() {
moduleNames.forEach(function(name) {
this._applyStyle(name);
}.bind(this));
}.bind(this));
.then(() => {
for (const name of moduleNames) {
this._applyStyle(name);
}
});
}
}.bind(this));
});
},
});
})();

View File

@@ -31,30 +31,30 @@ limitations under the License.
</test-fixture>
<script>
suite('gr-change-metadata integration tests', function() {
var sandbox;
var element;
suite('gr-change-metadata integration tests', () => {
let sandbox;
let element;
setup(function(done) {
setup(done => {
sandbox = sinon.sandbox.create();
sandbox.stub(Gerrit, 'awaitPluginsLoaded').returns(Promise.resolve());
Gerrit._styleModules = {'foo': [{pluginUrl: 'bar', moduleName: 'baz',}]};
Gerrit._styleModules = {foo: [{pluginUrl: 'bar', moduleName: 'baz'}]};
element = fixture('basic');
sandbox.stub(element, '_applyStyle');
sandbox.stub(element, 'importHref', function(url, resolve) { resolve() });
sandbox.stub(element, 'importHref', (url, resolve) => { resolve(); });
flush(done);
});
teardown(function() {
teardown(() => {
sandbox.restore();
});
test('imports plugin-provided module', function() {
test('imports plugin-provided module', () => {
assert.isTrue(element.importHref.calledWith('bar'));
});
test('applies plugin-provided styles', function() {
test('applies plugin-provided styles', () => {
assert.isTrue(element._applyStyle.calledWith('baz'));
});
});

View File

@@ -24,28 +24,27 @@
},
},
_configChanged: function(config) {
var jsPlugins = config.js_resource_paths || [];
var htmlPlugins = config.html_resource_paths || [];
_configChanged(config) {
const jsPlugins = config.js_resource_paths || [];
const htmlPlugins = config.html_resource_paths || [];
Gerrit._setPluginsCount(jsPlugins.length + htmlPlugins.length);
this._loadJsPlugins(jsPlugins);
this._importHtmlPlugins(htmlPlugins);
},
_importHtmlPlugins: function(plugins) {
plugins.forEach(function(url) {
if (url.indexOf('http') !== 0) {
_importHtmlPlugins(plugins) {
for (let url of plugins) {
if (!url.startsWith('http')) {
url = '/' + url;
}
this.importHref(
url, Gerrit._pluginInstalled, Gerrit._pluginInstalled, true);
}.bind(this));
}
},
_loadJsPlugins: function(plugins) {
for (var i = 0; i < plugins.length; i++) {
var url = plugins[i];
var scriptEl = document.createElement('script');
_loadJsPlugins(plugins) {
for (let i = 0; i < plugins.length; i++) {
const scriptEl = document.createElement('script');
scriptEl.defer = true;
scriptEl.src = '/' + plugins[i];
scriptEl.onerror = Gerrit._pluginInstalled;

View File

@@ -30,22 +30,22 @@ limitations under the License.
</test-fixture>
<script>
suite('gr-diff tests', function() {
var element;
var sandbox;
suite('gr-diff tests', () => {
let element;
let sandbox;
setup(function() {
setup(() => {
element = fixture('basic');
sandbox = sinon.sandbox.create();
sandbox.stub(document.body, 'appendChild');
sandbox.stub(element, 'importHref');
});
teardown(function() {
teardown(() => {
sandbox.restore();
});
test('counts plugins', function() {
test('counts plugins', () => {
sandbox.stub(Gerrit, '_setPluginsCount');
element.config = {
html_resource_paths: ['foo/bar', 'baz'],
@@ -54,7 +54,7 @@ limitations under the License.
assert.isTrue(Gerrit._setPluginsCount.calledWith(3));
});
test('imports html plugins from config', function() {
test('imports html plugins from config', () => {
element.config = {
html_resource_paths: ['foo/bar', 'baz'],
};