UNPKG

kontainer-js

Version:

A media file format generator/parser that exposes a React-like API.

152 lines (128 loc) 3.87 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); exports.createElement = createElement; var _Component = require('./Component'); var _Component2 = _interopRequireDefault(_Component); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var Element = function () { function Element(type, props) { (0, _classCallCheck3.default)(this, Element); this.type = type; this.props = props; this.instance = null; // Instantiation is deferred until the rendering time. this.rootClass = null; this.format = null; } (0, _createClass3.default)(Element, [{ key: 'querySelector', value: function querySelector(type) { var queue = [this]; var element = void 0; while (element = queue.shift()) { if (type === element.type.COMPACT_NAME) { return element; } var children = element.props.children; if (children) { children.forEach(function (child) { queue.push(child); }); } } return null; } }, { key: 'querySelectorAll', value: function querySelectorAll(type) { var queue = [this]; var elems = []; var element = void 0; while (element = queue.shift()) { if (type === element.type.COMPACT_NAME) { elems.push(element); } var children = element.props.children; if (children) { children.forEach(function (child) { queue.push(child); }); } } return elems; } }, { key: 'getMimeType', value: function getMimeType() { return this.type.getMimeType(this); } }, { key: 'setFormatInfo', value: function setFormatInfo(format) { this.format = format.name; this.rootClass = format.getRootWrapperClass(); } }, { key: 'wrap', value: function wrap(elements) { if (this.rootClass) { var rootElement = createElement(this.rootClass, null, elements); rootElement.format = this.format; return rootElement; } return null; } }]); return Element; }(); function isValidComponentClass(type) { var proto = type.prototype; if (proto instanceof _Component2.default && typeof proto.serialize === 'function' && typeof proto.getSize === 'function' && typeof proto.setSize === 'function') { return true; } return false; } function unfold(list) { var plain = []; list.forEach(function f(item) { if (Array.isArray(item)) { item.forEach(f); } else { plain.push(item); } }); return plain; } function createElement(type, props) { for (var _len = arguments.length, children = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { children[_key - 2] = arguments[_key]; } props = props || {}; children = unfold(children); children = children.filter(function (child) { return child instanceof Element; }); // Validate type if (!isValidComponentClass(type)) { console.error('MediaFormat.createElement: the class (' + type + ') does not implement necessary methods.'); return null; } props.children = children; // Resolve default props var defaultProps = type.defaultProps; if (defaultProps) { (0, _keys2.default)(defaultProps).forEach(function (key) { if (props[key] === void 0) { props[key] = defaultProps[key]; } }); } return new Element(type, props); }