@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
420 lines (337 loc) • 11.7 kB
JavaScript
import * as TheLogManager from 'aurelia-logging';
import { Container } from 'aurelia-dependency-injection';
import { Loader } from 'aurelia-loader';
import { BindingLanguage, ViewSlot, ViewResources, TemplatingEngine, CompositionTransaction, ViewEngine } from 'aurelia-templating';
import { DOM, PLATFORM } from 'aurelia-pal';
import { relativeToFile, join } from 'aurelia-path';
function preventActionlessFormSubmit() {
DOM.addEventListener('submit', evt => {
const target = evt.target;
const action = target.action;
if (target.tagName.toLowerCase() === 'form' && !action) {
evt.preventDefault();
}
});
}
export let Aurelia = class Aurelia {
constructor(loader, container, resources) {
this.loader = loader || new PLATFORM.Loader();
this.container = container || new Container().makeGlobal();
this.resources = resources || new ViewResources();
this.use = new FrameworkConfiguration(this);
this.logger = TheLogManager.getLogger('aurelia');
this.hostConfigured = false;
this.host = null;
this.use.instance(Aurelia, this);
this.use.instance(Loader, this.loader);
this.use.instance(ViewResources, this.resources);
}
start() {
if (this.started) {
return Promise.resolve(this);
}
this.started = true;
this.logger.info('Aurelia Starting');
return this.use.apply().then(() => {
preventActionlessFormSubmit();
if (!this.container.hasResolver(BindingLanguage)) {
let message = 'You must configure Aurelia with a BindingLanguage implementation.';
this.logger.error(message);
throw new Error(message);
}
this.logger.info('Aurelia Started');
let evt = DOM.createCustomEvent('aurelia-started', { bubbles: true, cancelable: true });
DOM.dispatchEvent(evt);
return this;
});
}
enhance(bindingContext = {}, applicationHost = null) {
this._configureHost(applicationHost || DOM.querySelectorAll('body')[0]);
return new Promise(resolve => {
let engine = this.container.get(TemplatingEngine);
this.root = engine.enhance({ container: this.container, element: this.host, resources: this.resources, bindingContext: bindingContext });
this.root.attached();
this._onAureliaComposed();
resolve(this);
});
}
setRoot(root = null, applicationHost = null) {
let instruction = {};
if (this.root && this.root.viewModel && this.root.viewModel.router) {
this.root.viewModel.router.deactivate();
this.root.viewModel.router.reset();
}
this._configureHost(applicationHost);
let engine = this.container.get(TemplatingEngine);
let transaction = this.container.get(CompositionTransaction);
delete transaction.initialComposition;
if (!root) {
if (this.configModuleId) {
root = relativeToFile('./app', this.configModuleId);
} else {
root = 'app';
}
}
instruction.viewModel = root;
instruction.container = instruction.childContainer = this.container;
instruction.viewSlot = this.hostSlot;
instruction.host = this.host;
return engine.compose(instruction).then(r => {
this.root = r;
instruction.viewSlot.attached();
this._onAureliaComposed();
return this;
});
}
_configureHost(applicationHost) {
if (this.hostConfigured) {
return;
}
applicationHost = applicationHost || this.host;
if (!applicationHost || typeof applicationHost === 'string') {
this.host = DOM.getElementById(applicationHost || 'applicationHost');
} else {
this.host = applicationHost;
}
if (!this.host) {
throw new Error('No applicationHost was specified.');
}
this.hostConfigured = true;
this.host.aurelia = this;
this.hostSlot = new ViewSlot(this.host, true);
this.hostSlot.transformChildNodesIntoView();
this.container.registerInstance(DOM.boundary, this.host);
}
_onAureliaComposed() {
let evt = DOM.createCustomEvent('aurelia-composed', { bubbles: true, cancelable: true });
setTimeout(() => DOM.dispatchEvent(evt), 1);
}
};
const logger = TheLogManager.getLogger('aurelia');
const extPattern = /\.[^/.]+$/;
function runTasks(config, tasks) {
let current;
let next = () => {
current = tasks.shift();
if (current) {
return Promise.resolve(current(config)).then(next);
}
return Promise.resolve();
};
return next();
}
function loadPlugin(config, loader, info) {
logger.debug(`Loading plugin ${ info.moduleId }.`);
config.resourcesRelativeTo = info.resourcesRelativeTo;
let id = info.moduleId;
if (info.resourcesRelativeTo.length > 1) {
return loader.normalize(info.moduleId, info.resourcesRelativeTo[1]).then(normalizedId => _loadPlugin(normalizedId));
}
return _loadPlugin(id);
function _loadPlugin(moduleId) {
return loader.loadModule(moduleId).then(m => {
if ('configure' in m) {
return Promise.resolve(m.configure(config, info.config || {})).then(() => {
config.resourcesRelativeTo = null;
logger.debug(`Configured plugin ${ info.moduleId }.`);
});
}
config.resourcesRelativeTo = null;
logger.debug(`Loaded plugin ${ info.moduleId }.`);
});
}
}
function loadResources(aurelia, resourcesToLoad, appResources) {
let viewEngine = aurelia.container.get(ViewEngine);
return Promise.all(Object.keys(resourcesToLoad).map(n => _normalize(resourcesToLoad[n]))).then(loads => {
let names = [];
let importIds = [];
loads.forEach(l => {
names.push(undefined);
importIds.push(l.importId);
});
return viewEngine.importViewResources(importIds, names, appResources);
});
function _normalize(load) {
let moduleId = load.moduleId;
let ext = getExt(moduleId);
if (isOtherResource(moduleId)) {
moduleId = removeExt(moduleId);
}
return aurelia.loader.normalize(moduleId, load.relativeTo).then(normalized => {
return {
name: load.moduleId,
importId: isOtherResource(load.moduleId) ? addOriginalExt(normalized, ext) : normalized
};
});
}
function isOtherResource(name) {
let ext = getExt(name);
if (!ext) return false;
if (ext === '') return false;
if (ext === '.js' || ext === '.ts') return false;
return true;
}
function removeExt(name) {
return name.replace(extPattern, '');
}
function addOriginalExt(normalized, ext) {
return removeExt(normalized) + '.' + ext;
}
}
function getExt(name) {
let match = name.match(extPattern);
if (match && match.length > 0) {
return match[0].split('.')[1];
}
}
function assertProcessed(plugins) {
if (plugins.processed) {
throw new Error('This config instance has already been applied. To load more plugins or global resources, create a new FrameworkConfiguration instance.');
}
}
export let FrameworkConfiguration = class FrameworkConfiguration {
constructor(aurelia) {
this.aurelia = aurelia;
this.container = aurelia.container;
this.info = [];
this.processed = false;
this.preTasks = [];
this.postTasks = [];
this.resourcesToLoad = {};
this.preTask(() => aurelia.loader.normalize('aurelia-bootstrapper').then(name => this.bootstrapperName = name));
this.postTask(() => loadResources(aurelia, this.resourcesToLoad, aurelia.resources));
}
instance(type, instance) {
this.container.registerInstance(type, instance);
return this;
}
singleton(type, implementation) {
this.container.registerSingleton(type, implementation);
return this;
}
transient(type, implementation) {
this.container.registerTransient(type, implementation);
return this;
}
preTask(task) {
assertProcessed(this);
this.preTasks.push(task);
return this;
}
postTask(task) {
assertProcessed(this);
this.postTasks.push(task);
return this;
}
feature(plugin, config) {
if (getExt(plugin)) {
return this.plugin({ moduleId: plugin, resourcesRelativeTo: [plugin, ''], config: config || {} });
}
return this.plugin({ moduleId: plugin + '/index', resourcesRelativeTo: [plugin, ''], config: config || {} });
}
globalResources(resources) {
assertProcessed(this);
let toAdd = Array.isArray(resources) ? resources : arguments;
let resource;
let resourcesRelativeTo = this.resourcesRelativeTo || ['', ''];
for (let i = 0, ii = toAdd.length; i < ii; ++i) {
resource = toAdd[i];
if (typeof resource !== 'string') {
throw new Error(`Invalid resource path [${ resource }]. Resources must be specified as relative module IDs.`);
}
let parent = resourcesRelativeTo[0];
let grandParent = resourcesRelativeTo[1];
let name = resource;
if ((resource.startsWith('./') || resource.startsWith('../')) && parent !== '') {
name = join(parent, resource);
}
this.resourcesToLoad[name] = { moduleId: name, relativeTo: grandParent };
}
return this;
}
globalName(resourcePath, newName) {
assertProcessed(this);
this.resourcesToLoad[resourcePath] = { moduleId: newName, relativeTo: '' };
return this;
}
plugin(plugin, config) {
assertProcessed(this);
if (typeof plugin === 'string') {
return this.plugin({ moduleId: plugin, resourcesRelativeTo: [plugin, ''], config: config || {} });
}
this.info.push(plugin);
return this;
}
_addNormalizedPlugin(name, config) {
let plugin = { moduleId: name, resourcesRelativeTo: [name, ''], config: config || {} };
this.plugin(plugin);
this.preTask(() => {
let relativeTo = [name, this.bootstrapperName];
plugin.moduleId = name;
plugin.resourcesRelativeTo = relativeTo;
return Promise.resolve();
});
return this;
}
defaultBindingLanguage() {
return this._addNormalizedPlugin('aurelia-templating-binding');
}
router() {
return this._addNormalizedPlugin('aurelia-templating-router');
}
history() {
return this._addNormalizedPlugin('aurelia-history-browser');
}
defaultResources() {
return this._addNormalizedPlugin('aurelia-templating-resources');
}
eventAggregator() {
return this._addNormalizedPlugin('aurelia-event-aggregator');
}
basicConfiguration() {
return this.defaultBindingLanguage().defaultResources().eventAggregator();
}
standardConfiguration() {
return this.basicConfiguration().history().router();
}
developmentLogging() {
this.preTask(() => {
return this.aurelia.loader.normalize('aurelia-logging-console', this.bootstrapperName).then(name => {
return this.aurelia.loader.loadModule(name).then(m => {
TheLogManager.addAppender(new m.ConsoleAppender());
TheLogManager.setLevel(TheLogManager.logLevel.debug);
});
});
});
return this;
}
apply() {
if (this.processed) {
return Promise.resolve();
}
return runTasks(this, this.preTasks).then(() => {
let loader = this.aurelia.loader;
let info = this.info;
let current;
let next = () => {
current = info.shift();
if (current) {
return loadPlugin(this, loader, current).then(next);
}
this.processed = true;
return Promise.resolve();
};
return next().then(() => runTasks(this, this.postTasks));
});
}
};
export * from 'aurelia-dependency-injection';
export * from 'aurelia-binding';
export * from 'aurelia-metadata';
export * from 'aurelia-templating';
export * from 'aurelia-loader';
export * from 'aurelia-task-queue';
export * from 'aurelia-path';
export * from 'aurelia-pal';
export const LogManager = TheLogManager;