angular-extension-registry
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.
Usage
Include the minified script in your html file. If you want to use the pre-compiled default
templates, include the additional template script.
<script src="/path/to/angular-extension-registry/dist/angular-extension-registry.min.js"></script>
<script 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:
angular.module('myapp', [
'extension-registry'
]);
View Output
Output points must be defined in the views like this:
<div
extension-point
extension-name="register1 register2"
extension-types="text link select html"
extension-args="a_relevant_object_for_context"
extension-limit="2"></div>
Data Input
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:
Built in types
Currently, there are 4 built-in extension types. Some quick vanilla examples:
{
type: 'text',
className: 'my-text',
text: 'This is some text.'
}
{
type: 'link',
className: 'my-class',
linkText: 'google link',
href: 'http://google.com',
target: '_blank'
},
{
type: 'link',
className: 'my-class'
linkText: 'google alert',
onClick: function() {
alert('google!');
}
},
{
type: 'select',
className: 'i am a select box test',
nameText: 'select-name',
options: [
{
label: 'bar 1 - 1',
value: 'bar'
},{
label: 'bar 1 - 2',
value: 'thing'
},{
label: 'bar 1 - 3',
value: 'other'
}
],
onChange: function(item) {
console.log('selected', item);
}
},
{
type: 'dom',
node: '<div>Hello World</div>'
},
{
type: 'dom',
node: $('<div>')
.addClass('outline-red')
.append('<span>')
.text('Hello world')
},
{
type: 'dom',
url: 'http://www.google.com',
onClick: function() {
$window.open(this.url, '_blank');
},
node: [
'<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('')
}
Adding custom types
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:
extensionRegistry.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.
Other return types
A registered callback function can return any of the following:
- inapplicable, undefined
- simple, object
- simple, array
- complex, promise
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.
Data registration
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.
extensionRegistry.add('endpoint1', function(args) {
return $q.when([
]);
});
A typical registration example:
angular.module('myapp')
.run([
'$q',
'$timeout',
'extensionRegistry',
function($q, $timeout, extensionRegistry) {
extensionRegistry.add('register1', function(args) {
return $q.when([
{
type: 'link',
href: args.href,
displayName: args.name + ' link',
target: '_blank'
}
]);
});
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:
extensionRegistry.add('sidebar-left');
extensionRegistry.add('main');
extensionRegistry.add('footer');
extensionRegistry.add('foo');
extensionRegistry.add('bar');
extensionRegistry.add('shizzle');
extensionRegistry.add('foo', function(args) {
}).
It is fine to register multiple callbacks to an endpoint:
extensionRegistry.add('endpoint1', function() { return [ ] });
extensionRegistry.add('endpoint1', function() { return [ ] });
extensionRegistry.add('endpoint1', function() { return [ ] });
extensionRegistry.add('endpoint1', function() { return [ ] });
Deregistering data
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.
var reg = extensionRegistry.add('endpoint1', function() { return [ ] });
reg.remove();
Template usage / overrides
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.
View the demos
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.