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

View File

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

View File

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

View File

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