derby
Version:
MVC framework making it easy to write realtime, collaborative applications that run in both Node.js and browsers.
61 lines (60 loc) • 2.48 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.markup = void 0;
var events_1 = require("events");
var createPathExpression_1 = require("./createPathExpression");
var templates_1 = require("../templates");
var MarkupParser = /** @class */ (function (_super) {
__extends(MarkupParser, _super);
function MarkupParser() {
return _super !== null && _super.apply(this, arguments) || this;
}
return MarkupParser;
}(events_1.EventEmitter));
// TODO: Should be its own module
exports.markup = new MarkupParser();
exports.markup.on('element:a', function (template) {
if (hasListenerFor(template, 'click')) {
var attributes = template.attributes || (template.attributes = {});
if (!attributes.href) {
attributes.href = new templates_1.templates.Attribute('#');
addListener(template, 'click', '$preventDefault($event)');
}
}
});
exports.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_1.templates.ElementOn && hook.name === eventName) {
return true;
}
}
return false;
}
function addListener(template, eventName, source) {
var hooks = template.hooks || (template.hooks = []);
var expression = (0, createPathExpression_1.createPathExpression)(source);
hooks.push(new templates_1.templates.ElementOn(eventName, expression));
}