src/lib/README.md
The src/lib/*.js files contain library functions. The main difference to plugins is that:
Plugins can access libraries via the API:
var api;
document.addEventListener( "impress:init", function(event){
api = event.detail.api;
api().lib.<libraryName>.<libaryFunction>();
});
...which is equivalent to:
impress().lib.<libraryName>.<libraryFunction>();
Create a file under src/lib/.
Start with the standard boilerplate documentation, and the (function(document, window){})(); wrapper.
The library should implement a factory function, and make its existence known to impress.js core:
window.impress.addLibraryFactory( { libName : libraryFactory} );
The library function should return a similar API object as core impress() function does:
var libraryFactory = function(rootId) { /* implement library functions ... */
var lib = {
libFunction1: libFunction1,
libFunction2: libFunction2
}
return lib;
};
While rarely used, impress.js actually supports multiple presentation root div elements on a single html page. Each of these have their own API object, identified by the root element id attribute:
impress("other-root-id").init();
(The default rootId obviously is "impress".)
Libraries MUST implement this support for multiple root elements as well.
impress.init(rootId).rootId.impress(rootId).tear(), the same root element might be
initialized more than once, and each of these MUST be treated as a new valid initialization.Putting all of the above together, a skeleton library file will look like:
/**
* Example library libName
*
* Henrik Ingo (c) 2016
* MIT License
*/
(function ( document, window ) {
'use strict';
// Singleton library variables
var roots = [];
var singletonVar = {};
var libraryFactory = function(rootId) {
if (roots["impress-root-" + rootId]) {
return roots["impress-root-" + rootId];
}
// Per root global variables (instance variables?)
var instanceVar = {};
// LIBRARY FUNCTIONS
var libraryFunction1 = function () {
/* ... */
};
var libraryFunction2 = function () {
/* ... */
};
var lib = {
libFunction1: libFunction1,
libFunction2: libFunction2
}
roots["impress-root-" + rootId] = lib;
return lib;
};
// Let impress core know about the existence of this library
window.impress.addLibraryFactory( { libName : libraryFactory } );
})(document, window);