| 1 |
"use strict"; |
| 2 |
|
| 3 |
var attributes = require('./attributes'); |
| 4 |
var isApiWritable = require("./config").isApiWritable; |
| 5 |
|
| 6 |
module.exports = function(spec, defaultConstructor, tagList, tagNameToImpl) { |
| 7 |
var c = spec.ctor; |
| 8 |
if (c) { |
| 9 |
var props = spec.props || {}; |
| 10 |
|
| 11 |
if (spec.attributes) { |
| 12 |
for (var n in spec.attributes) { |
| 13 |
var attr = spec.attributes[n]; |
| 14 |
if (typeof attr !== 'object' || Array.isArray(attr)) attr = {type: attr}; |
| 15 |
if (!attr.name) attr.name = n.toLowerCase(); |
| 16 |
props[n] = attributes.property(attr); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
props.constructor = { value : c, writable: isApiWritable }; |
| 21 |
c.prototype = Object.create((spec.superclass || defaultConstructor).prototype, props); |
| 22 |
if (spec.events) { |
| 23 |
addEventHandlers(c, spec.events); |
| 24 |
} |
| 25 |
tagList[spec.name] = c; |
| 26 |
} |
| 27 |
else { |
| 28 |
c = defaultConstructor; |
| 29 |
} |
| 30 |
|
| 31 |
(spec.tags || spec.tag && [spec.tag] || []).forEach(function(tag) { |
| 32 |
tagNameToImpl[tag] = c; |
| 33 |
}); |
| 34 |
|
| 35 |
return c; |
| 36 |
}; |
| 37 |
|
| 38 |
function EventHandlerBuilder(body, document, form, element) { |
| 39 |
this.body = body; |
| 40 |
this.document = document; |
| 41 |
this.form = form; |
| 42 |
this.element = element; |
| 43 |
} |
| 44 |
|
| 45 |
EventHandlerBuilder.prototype.build = function () { |
| 46 |
return () => {}; |
| 47 |
}; |
| 48 |
|
| 49 |
function EventHandlerChangeHandler(elt, name, oldval, newval) { |
| 50 |
var doc = elt.ownerDocument || Object.create(null); |
| 51 |
var form = elt.form || Object.create(null); |
| 52 |
elt[name] = new EventHandlerBuilder(newval, doc, form, elt).build(); |
| 53 |
} |
| 54 |
|
| 55 |
function addEventHandlers(c, eventHandlerTypes) { |
| 56 |
var p = c.prototype; |
| 57 |
eventHandlerTypes.forEach(function(type) { |
| 58 |
|
| 59 |
Object.defineProperty(p, "on" + type, { |
| 60 |
get: function() { |
| 61 |
return this._getEventHandler(type); |
| 62 |
}, |
| 63 |
set: function(v) { |
| 64 |
this._setEventHandler(type, v); |
| 65 |
}, |
| 66 |
}); |
| 67 |
|
| 68 |
|
| 69 |
attributes.registerChangeHandler(c, "on" + type, EventHandlerChangeHandler); |
| 70 |
}); |
| 71 |
} |