From e5ec11c0c052f9fb5af7f14ce20ad9c5c83b89e5 Mon Sep 17 00:00:00 2001 From: Viktar Donich Date: Wed, 13 Sep 2017 13:56:50 -0700 Subject: [PATCH] Plugin endpoint parameters Enable plugin extension endpoints to accept and pass parameters to plugin-provided web components. Declare a named parameter: ``` html ``` Register a component and get the parameter in the plugin: ``` js Gerrit.install(plugin => { plugin.registerCustomComponent( 'some-endpoint', 'my-plugin-element'); }); ``` ``` html ``` Change-Id: Ie6b2d92107f332c74532e4462898ae0d876c4603 --- .../gr-endpoint-decorator.js | 29 ++++++++++++++++--- .../gr-endpoint-decorator_test.html | 13 ++++++++- .../gr-endpoint-param/gr-endpoint-param.html | 21 ++++++++++++++ .../gr-endpoint-param/gr-endpoint-param.js | 24 +++++++++++++++ polygerrit-ui/app/test/index.html | 1 + 5 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.html create mode 100644 polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.js diff --git a/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator.js b/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator.js index bcd2378c85..a2a1c0b4f4 100644 --- a/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator.js +++ b/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator.js @@ -40,20 +40,41 @@ _initDecoration(name, plugin) { const el = document.createElement(name); - el.plugin = plugin; - el.content = this.getContentChildren()[0]; + this._initProperties(el, plugin, this.getContentChildren().find( + el => el.nodeName !== 'GR-ENDPOINT-PARAM')); this._appendChild(el); return el; }, _initReplacement(name, plugin) { - this.getContentChildren().forEach(node => node.remove()); + this.getContentChildNodes().forEach(node => node.remove()); const el = document.createElement(name); - el.plugin = plugin; + this._initProperties(el, plugin); this._appendChild(el); return el; }, + _getEndpointParams() { + return Polymer.dom(this).querySelectorAll('gr-endpoint-param').map(el => { + return {name: el.getAttribute('name'), value: el.value}; + }); + }, + + /** + * @param {!Element} el + * @param {!Object} plugin + * @param {!Element=} opt_content + */ + _initProperties(el, plugin, opt_content) { + el.plugin = plugin; + if (opt_content) { + el.content = opt_content; + } + for (const {name, value} of this._getEndpointParams()) { + el[name] = value; + } + }, + _appendChild(el) { Polymer.dom(this.root).appendChild(el); }, diff --git a/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html b/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html index e7d1930473..578989e7b9 100644 --- a/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html +++ b/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html @@ -22,10 +22,13 @@ limitations under the License. + @@ -44,6 +47,7 @@ limitations under the License. domHookStub = { handleInstanceAttached: sandbox.stub(), handleInstanceDetached: sandbox.stub(), + getPublicAPI: () => domHookStub, }; sandbox.stub( GrDomHooksManager.prototype, 'getDomHook').returns(domHookStub); @@ -53,6 +57,7 @@ limitations under the License. plugin = p; plugin.registerCustomComponent('foo', 'some-module'); plugin.registerCustomComponent('foo', 'other-module', {replace: true}); + plugin.registerCustomComponent('bar', 'some-module'); }, '0.1', 'http://some/plugin/url.html'); sandbox.stub(Gerrit, '_arePluginsLoaded').returns(true); @@ -110,5 +115,11 @@ limitations under the License. done(); }); }); + + test('params', () => { + const instance = document.createElement('foo'); + element._initProperties(instance, plugin); + assert.equal(instance.someparam, 'barbar'); + }); }); diff --git a/polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.html b/polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.html new file mode 100644 index 0000000000..1aa9e7c8ab --- /dev/null +++ b/polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.html @@ -0,0 +1,21 @@ + + + + + + + diff --git a/polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.js b/polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.js new file mode 100644 index 0000000000..5a2ab591d9 --- /dev/null +++ b/polygerrit-ui/app/elements/plugins/gr-endpoint-param/gr-endpoint-param.js @@ -0,0 +1,24 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +(function() { + 'use strict'; + + Polymer({ + is: 'gr-endpoint-param', + properties: { + name: String, + value: Object, + }, + }); +})(); diff --git a/polygerrit-ui/app/test/index.html b/polygerrit-ui/app/test/index.html index 7c8b6ffd71..c748a9b789 100644 --- a/polygerrit-ui/app/test/index.html +++ b/polygerrit-ui/app/test/index.html @@ -102,6 +102,7 @@ limitations under the License. 'diff/gr-syntax-layer/gr-syntax-layer_test.html', 'diff/gr-syntax-lib-loader/gr-syntax-lib-loader_test.html', 'plugins/gr-attribute-helper/gr-attribute-helper_test.html', + 'plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html', 'plugins/gr-event-helper/gr-event-helper_test.html', 'plugins/gr-external-style/gr-external-style_test.html', 'plugins/gr-plugin-host/gr-plugin-host_test.html',