-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmarkup.js
53 lines (45 loc) · 1.45 KB
/
markup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var EventEmitter = require('events').EventEmitter;
var templates = require('derby-templates').templates;
var createPathExpression = require('./createPathExpression');
// TODO: Should be its own module
var markup = module.exports = new MarkupParser();
function MarkupParser() {
EventEmitter.call(this);
}
mergeInto(MarkupParser.prototype, EventEmitter.prototype);
markup.on('element:a', function(template) {
if (hasListenerFor(template, 'click')) {
var attributes = template.attributes || (template.attributes = {});
if (!attributes.href) {
attributes.href = new templates.Attribute('#');
addListener(template, 'click', '$preventDefault($event)');
}
}
});
markup.on('element:form', function(template) {
if (hasListenerFor(template, 'submit')) {
addListener(template, 'submit', '$preventDefault($event)');
}
});
function hasListenerFor(template, eventName) {
var hooks = template.hooks;
if (!hooks) return false;
for (var i = 0, len = hooks.length; i < len; i++) {
var hook = hooks[i];
if (hook instanceof templates.ElementOn && hook.name === eventName) {
return true;
}
}
return false;
}
function addListener(template, eventName, source) {
var hooks = template.hooks || (template.hooks = []);
var expression = createPathExpression(source);
hooks.push(new templates.ElementOn(eventName, expression));
}
function mergeInto(to, from) {
for (var key in from) {
to[key] = from[key];
}
return to;
}