dynamictemplate
Version:
Δt - async & dynamic templating engine
174 lines (139 loc) • 5.1 kB
JavaScript
(function() {
var EVENTS, EventEmitter, Template, aliases, clear, doctype, lookup, schema, self_closing, _ref, _ref1,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
EventEmitter = require('events').EventEmitter;
_ref = require('./schema'), schema = _ref.schema, self_closing = _ref.self_closing;
doctype = require('./doctype').doctype;
aliases = require('./alias').aliases;
_ref1 = require('./cache'), lookup = _ref1.lookup, clear = _ref1.clear;
EVENTS = ['new', 'add', 'show', 'hide', 'attr', 'text', 'raw', 'remove', 'replace', 'data', 'close', 'end'];
Template = (function(_super) {
__extends(Template, _super);
function Template(opts, template) {
var old_query, s, _ref2, _ref3, _ref4, _ref5,
_this = this;
if (opts == null) {
opts = {};
}
this.hide = __bind(this.hide, this);
this.show = __bind(this.show, this);
this.ready = __bind(this.ready, this);
this.end = __bind(this.end, this);
this.remove = __bind(this.remove, this);
this.register = __bind(this.register, this);
if (typeof opts === 'function') {
_ref2 = [opts, {}], template = _ref2[0], opts = _ref2[1];
}
if (opts.partial) {
opts.run = false;
}
if ((_ref3 = opts.encoding) == null) {
opts.encoding = 'utf-8';
}
if ((_ref4 = opts.doctype) == null) {
opts.doctype = false;
}
if ((_ref5 = opts.end) == null) {
opts.end = true;
}
opts._schema = opts.schema;
s = aliases[opts._schema] || opts._schema || 'xml';
opts.self_closing = typeof self_closing[s] === "function" ? self_closing[s](opts) : void 0;
opts.schema = typeof schema[s] === "function" ? schema[s](opts).split(' ') : void 0;
this.xml = lookup(opts);
this.xml.template = this;
old_query = this.xml.query;
this.xml.query = function(type, tag, key) {
var _ref6;
if (type === 'tag') {
return (_ref6 = key.xml) != null ? _ref6 : key;
} else {
return old_query.call(this, type, tag, key);
}
};
if (opts.self_closing !== false) {
this.xml.register('end', function(tag, next) {
if (!tag.isselfclosing) {
return next(tag);
}
if (opts.self_closing === true || opts.self_closing.match(tag.name)) {
tag.isempty = true;
}
return next(tag);
});
}
EVENTS.forEach(function(event) {
return _this.xml.on(event, _this.emit.bind(_this, event));
});
this.run = this.run.bind(this, template, opts);
if (opts.run === false) {
return;
}
process.nextTick(this.run);
}
Template.prototype.run = function(template, opts, _arg) {
var d, dt, restart;
restart = (_arg != null ? _arg : {}).restart;
if (this.started && !restart) {
return;
}
this.started = true;
if (opts.doctype === true) {
opts.doctype = opts._schema || 'html';
}
d = aliases[opts.doctype] || opts.doctype;
if (opts.doctype && (dt = typeof doctype[d] === "function" ? doctype[d](opts) : void 0)) {
if (opts.pretty) {
dt += "\n";
}
this.xml.emit('data', this.xml, dt);
}
if (typeof template === 'function') {
template.call(this.xml);
if (opts.end) {
return this.end();
}
} else if (opts.end) {
return this.end(template);
}
};
Template.prototype.toString = function() {
return "[object Template]";
};
Template.prototype.register = function() {
var _ref2;
return (_ref2 = this.xml).register.apply(_ref2, arguments);
};
Template.prototype.remove = function() {
var _ref2;
(_ref2 = this.xml).remove.apply(_ref2, arguments);
this.xml.template = null;
return this.xml = null;
};
Template.prototype.end = function() {
var _ref2;
return (_ref2 = this.xml).end.apply(_ref2, arguments);
};
Template.prototype.ready = function() {
var _ref2;
return (_ref2 = this.xml).ready.apply(_ref2, arguments);
};
Template.prototype.show = function() {
var _ref2;
return (_ref2 = this.xml).show.apply(_ref2, arguments);
};
Template.prototype.hide = function() {
var _ref2;
return (_ref2 = this.xml).hide.apply(_ref2, arguments);
};
return Template;
})(EventEmitter);
Template.schema = schema;
Template.doctype = doctype;
Template.self_closing = self_closing;
Template.aliases = aliases;
Template.clearCache = clear;
module.exports = Template;
}).call(this);