Name: angular-extension-registry
Owner: OpenShift
Description: An angular module allowing arbitrary data to be injected & rendered in UI
Created: 2015-07-11 01:42:02.0
Updated: 2018-03-13 17:14:07.0
Pushed: 2017-08-22 13:33:16.0
Homepage: null
Size: 114
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
An angular module that provides a plugin registry system for arbitrarily injecting additional UI components into views. A primary use case would be allowing developers to add components to an application without compiling the extension point files into the actual application.
Include the minified script in your html file. If you want to use the pre-compiled default templates, include the additional template script.
ipt src="/path/to/angular-extension-registry/dist/angular-extension-registry.min.js"></script>
ipt src="/path/to/angular-extension-registry/dist/compiled-templates.js"></script>
Then require the module in your app. This is done in the typical Angular fashion:
equire 'extension-registry'
lar.module('myapp', [
xtension-registry'
Output points must be defined in the views like this:
extension-point is the main directive
configure it with extension-name="space delimited endpoint names"
configure it with extension-types to filter out types of objects it will
render. objects that do not match this filter will not be rendered.
configure runtime contextual data via extension-args. This object will be
passed to each registered callback function to generate unique output
in certain cases the # of items may need to be limited. use extension-limit
tension-point
tension-name="register1 register2"
tension-types="text link select html"
tension-args="a_relevant_object_for_context"
tension-limit="2"></div>
Then the service can be used to register data objects for render. Two-way data binding will apply, output will re-render whenever the UI changes:
Currently, there are 4 built-in extension types. Some quick vanilla examples:
ype: text
pe: 'text',
assName: 'my-text',
xt: 'This is some text.'
ype: link
pe: 'link',
assName: 'my-class',
nkText: 'google link',
ef: 'http://google.com',
rget: '_blank'
ink: with an onclick function
pe: 'link',
assName: 'my-class'
nkText: 'google alert',
Click: function() {
alert('google!');
ype: select box
pe: 'select',
assName: 'i am a select box test',
meText: 'select-name',
tions: [
{
label: 'bar 1 - 1',
value: 'bar'
},{
label: 'bar 1 - 2',
value: 'thing'
},{
label: 'bar 1 - 3',
value: 'other'
}
Change: function(item) {
console.log('selected', item);
ype: dom
OTE: node: '' can be a jQuery object, or a plain string
pe: 'dom',
de: '<div>Hello World</div>'
r
pe: 'dom',
de: $('<div>')
.addClass('outline-red')
.append('<span>')
.text('Hello world')
nd can be as complex as desired:
pe: 'dom',
arbitrary keys can be added to the object, these will be
accessible via node once rendered
l: 'http://www.google.com',
Click: function() {
$window.open(this.url, '_blank');
de: [
'<div row ',
'ng-show="item.url" ',
'class="foo" ',
'title="A link title">',
'<div>',
'<i class="fa fa-share" aria-hidden="true"></i>',
'</div>',
'<div>',
'<a ng-click="item.onClick($event)" ',
'ng-href="item.url">',
'Open some link',
'</a>',
'</div>',
'</div>'
join('')
To add a new type, you must name the type & provide a template for rendering.
Templates are given a model object called item
.
Example of adding a new type:
nsionRegistry.addType('li', '<li>{{item.text}}</li>');
This will register the template with angular's $templateCache
for use whenever the extension point is needed. Templates are registered as __extension-<type-name>.html
.
A registered callback function can return any of the following:
A function that returns nothing may be used to instead manipulate the data. It is encouraged to be a good citizen, of course. Changing data that will be used by other registered extensions could have undesirable consequences.
Registering the data objects to a specific endpoint happens via a registration function. The function will receive contextual arguments and can return a promise, data, etc.
rgs is an object set via the directive in the view.
ikely it is some object on a controller scope that gives
eaning to the endpoint.
nsionRegistry.add('endpoint1', function(args) {
turn $q.when([
// my objects
;
A typical registration example:
lar.module('myapp')
un([
'$q',
'$timeout',
'extensionRegistry',
function($q, $timeout, extensionRegistry) {
// args is provided via the directive attrib extension-args="some_object"
// and can be used to customize the data objects that will be rendered
extensionRegistry.add('register1', function(args) {
// simulate async (service calls, etc)
return $q.when([
// add a single link, assuming the args to the directive will provide
// a name & href for the object
{
type: 'link',
href: args.href,
displayName: args.name + ' link',
target: '_blank'
}
]);
});
// multiple items registered
extensionRegistry.add('register1', function(args) {
return $q.when([
{
type: 'link',
href: args.href,
displayName: args.name + ' link',
target: '_blank'
},
{
type: 'link',
displayName: args.name + 'alert',
onClick: function() {
alert('clicked!');
}
}
]);
});
}
;
It is perfectly fine to register endpoints ahead of time, then later register additional callbacks. Example:
nsionRegistry.add('sidebar-left');
nsionRegistry.add('main');
nsionRegistry.add('footer');
nsionRegistry.add('foo');
nsionRegistry.add('bar');
nsionRegistry.add('shizzle');
hen elsewhere:
nsionRegistry.add('foo', function(args) {
do stuff...
It is fine to register multiple callbacks to an endpoint:
nsionRegistry.add('endpoint1', function() { return [ /* stuff */ ] });
nsionRegistry.add('endpoint1', function() { return [ /* stuff2 */ ] });
nsionRegistry.add('endpoint1', function() { return [ /* stuff3 */ ] });
nsionRegistry.add('endpoint1', function() { return [ /* stuff4 */ ] });
Each time you register data to a registry, the .add()
function will return an object that
has a .remove()
function bound to that particular data set, allowing you to unregister that
block of data. Calling .remove()
does not clear an entire registry, ONLY the data that was
registered in that data set.
reg = extensionRegistry.add('endpoint1', function() { return [ /* stuff */ ] });
ah, actually we don't want this anymore.
remove();
The /src/views/
directory houses the source html files used to generate templates. These
are compiled into /dist/compiled-templates.js
. The script can be included to use the default
templates, or you can create your own overrides by making templates that match the template
path name. Example: __extension-link.html
.
These templates will need to be registered via Angular's templateCache. The best way to do this
is with a build tool, such as gulp's gulp-angular-templatecache
plugin.
Clone the project, then run the following from the root directory:
npm install
bower install
gulp serve
This will load a file in your browser with links to the /demos
directory. Feel free to experiment
with these examples.