1 | (function() {
|
2 | var DefaultBuilder, EVENTS, EventEmitter, Template, aliases, doctype, ff, pp, schema, self_closing, _ref;
|
3 | var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.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; }, __slice = Array.prototype.slice;
|
4 |
|
5 | EventEmitter = require('events').EventEmitter;
|
6 |
|
7 | DefaultBuilder = require('asyncxml').Builder;
|
8 |
|
9 | _ref = require('./schema'), schema = _ref.schema, self_closing = _ref.self_closing;
|
10 |
|
11 | doctype = require('./doctype').doctype;
|
12 |
|
13 | aliases = require('./alias').aliases;
|
14 |
|
15 | EVENTS = ['new', 'add', 'show', 'hide', 'attr', 'text', 'raw', 'remove', 'replace', 'data', 'close', 'end'];
|
16 |
|
17 | pp = function(proto, name) {
|
18 | proto[name] = function() {
|
19 | var _ref2;
|
20 | return this.tag.apply(this, (_ref2 = [name]).concat.apply(_ref2, arguments));
|
21 | };
|
22 | return proto["$" + name] = function() {
|
23 | var _ref2;
|
24 | return this.$tag.apply(this, (_ref2 = [name]).concat.apply(_ref2, arguments));
|
25 | };
|
26 | };
|
27 |
|
28 | ff = function(proto, tags) {
|
29 | var tagname, _i, _len;
|
30 | for (_i = 0, _len = tags.length; _i < _len; _i++) {
|
31 | tagname = tags[_i];
|
32 | if (tagname) pp(proto, tagname);
|
33 | }
|
34 | };
|
35 |
|
36 | Template = (function() {
|
37 |
|
38 | __extends(Template, EventEmitter);
|
39 |
|
40 | function Template(opts, template) {
|
41 | var Builder, ExtendedBuilder, ExtendedTag, Tag, old_query, s, schema_input, _ref2, _ref3, _ref4, _ref5, _ref6;
|
42 | var _this = this;
|
43 | if (opts == null) opts = {};
|
44 | this.ready = __bind(this.ready, this);
|
45 | this.end = __bind(this.end, this);
|
46 | this.register = __bind(this.register, this);
|
47 | if (typeof opts === 'function') {
|
48 | _ref2 = [opts, {}], template = _ref2[0], opts = _ref2[1];
|
49 | }
|
50 | if ((_ref3 = opts.encoding) == null) opts.encoding = 'utf-8';
|
51 | if ((_ref4 = opts.doctype) == null) opts.doctype = false;
|
52 | if ((_ref5 = opts.end) == null) opts.end = true;
|
53 | schema_input = opts.schema;
|
54 | s = aliases[schema_input] || schema_input || 'xml';
|
55 | opts.self_closing = typeof self_closing[s] === "function" ? self_closing[s](opts) : void 0;
|
56 | opts.schema = typeof schema[s] === "function" ? schema[s](opts).split(' ') : void 0;
|
57 | Builder = (_ref6 = opts.Builder) != null ? _ref6 : DefaultBuilder;
|
58 | ExtendedBuilder = (function() {
|
59 |
|
60 | __extends(ExtendedBuilder, Builder);
|
61 |
|
62 | function ExtendedBuilder() {
|
63 | ExtendedBuilder.__super__.constructor.apply(this, arguments);
|
64 | }
|
65 |
|
66 | return ExtendedBuilder;
|
67 |
|
68 | })();
|
69 | ff(ExtendedBuilder.prototype, opts.schema);
|
70 | this.xml = new ExtendedBuilder(opts);
|
71 | this.xml.template = this;
|
72 | old_query = this.xml.query;
|
73 | this.xml.query = function(type, tag, key) {
|
74 | var _ref7;
|
75 | if (type === 'tag') {
|
76 | return (_ref7 = key.xml) != null ? _ref7 : key;
|
77 | } else {
|
78 | return old_query.call(this, type, tag, key);
|
79 | }
|
80 | };
|
81 | Tag = this.xml.Tag;
|
82 | ExtendedTag = (function() {
|
83 |
|
84 | __extends(ExtendedTag, Tag);
|
85 |
|
86 | function ExtendedTag() {
|
87 | ExtendedTag.__super__.constructor.apply(this, arguments);
|
88 | }
|
89 |
|
90 | return ExtendedTag;
|
91 |
|
92 | })();
|
93 | ff(ExtendedTag.prototype, opts.schema);
|
94 | this.xml.Tag = this.xml.opts.Tag = ExtendedTag;
|
95 | this.xml.register('end', function(tag, next) {
|
96 | if (!(opts.self_closing === true || opts.self_closing.match(tag.name))) {
|
97 | tag.isempty = false;
|
98 | }
|
99 | return next(tag);
|
100 | });
|
101 | EVENTS.forEach(function(event) {
|
102 | return _this.xml.on(event, function() {
|
103 | var args;
|
104 | args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
105 | return _this.emit.apply(_this, [event].concat(__slice.call(args)));
|
106 | });
|
107 | });
|
108 | process.nextTick(function() {
|
109 | var d, dt, _ref7, _ref8;
|
110 | if (opts.doctype === true) opts.doctype = 'html';
|
111 | d = aliases[opts.doctype] || opts.doctype;
|
112 | if (opts.doctype && (dt = typeof doctype[d] === "function" ? doctype[d](opts) : void 0)) {
|
113 | if (opts.pretty) dt += "\n";
|
114 | _this.xml.emit('data', _this.xml, dt);
|
115 | }
|
116 | if (typeof template === 'function') {
|
117 | if ((_ref7 = _this.xml) != null ? _ref7._debug : void 0) {
|
118 | console.error("RUN", _this.xml._view.cid);
|
119 | }
|
120 | template.call(_this.xml);
|
121 | if (opts.end) return _this.end();
|
122 | } else {
|
123 | if ((_ref8 = _this.xml) != null ? _ref8._debug : void 0) {
|
124 | console.error("PASS", _this.xml._view.cid);
|
125 | }
|
126 | return _this.end(template);
|
127 | }
|
128 | });
|
129 | }
|
130 |
|
131 | Template.prototype.toString = function() {
|
132 | return "[object Template]";
|
133 | };
|
134 |
|
135 | Template.prototype.register = function() {
|
136 | var _ref2;
|
137 | return (_ref2 = this.xml).register.apply(_ref2, arguments);
|
138 | };
|
139 |
|
140 | Template.prototype.end = function() {
|
141 | var _ref2;
|
142 | return (_ref2 = this.xml).end.apply(_ref2, arguments);
|
143 | };
|
144 |
|
145 | Template.prototype.ready = function() {
|
146 | var _ref2;
|
147 | return (_ref2 = this.xml).ready.apply(_ref2, arguments);
|
148 | };
|
149 |
|
150 | return Template;
|
151 |
|
152 | })();
|
153 |
|
154 | Template.schema = schema;
|
155 |
|
156 | Template.doctype = doctype;
|
157 |
|
158 | Template.self_closing = self_closing;
|
159 |
|
160 | Template.aliases = aliases;
|
161 |
|
162 | module.exports = Template;
|
163 |
|
164 | }).call(this);
|