UNPKG

ciril

Version:
112 lines (85 loc) 4.31 kB
'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMixin = createMixin; exports.default = createClass; var _flownode = require('../core/flownode'); var _flownode2 = _interopRequireDefault(_flownode); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var assign = require('object-assign'); function conservativeMerge(target, source) { Object.getOwnPropertyNames(source).forEach(function (name) { if (name !== "constructor" && !target[name]) Object.defineProperty(target, name, Object.getOwnPropertyDescriptor(source, name)); }); } /** * Helper method to mix source class into target class. Existing * properties are NOT overriden. * @param target {function} * @param source {function} */ function mixin(target) { var proto = target.prototype; for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { sources[_key - 1] = arguments[_key]; } sources.forEach(function (source) { source = source.prototype; conservativeMerge(proto, source); }); return target; } /** * Create a FlowNode mixin class from a constructor. * @param Constructor {function} * @return * A FlowNode mixin class */ function createMixin(Constructor) { if (typeof Constructor !== 'function') throw new Error('createMixin(...): argument ' + ('should be function, got ' + (typeof Constructor === 'undefined' ? 'undefined' : _typeof(Constructor)))); var getInitialState = Constructor.prototype.getInitialState; var initialState = typeof getInitialState === 'function' ? getInitialState.apply(this) : null; var register = Constructor.prototype.registerOnCreate || true; var Mixin = function (_Constructor) { _inherits(Mixin, _Constructor); function Mixin() { _classCallCheck(this, Mixin); return _possibleConstructorReturn(this, Object.getPrototypeOf(Mixin).apply(this, arguments)); } return Mixin; }(Constructor); ; mixin(Mixin, _flownode2.default); var proto = Mixin.prototype; // override constructor Mixin = function Mixin() { _flownode.NodeConstructor.call(this, initialState, register); Constructor.apply(this, arguments); }; // copy back prototype Mixin.prototype = proto; return Mixin; } /** * Create a FlowNode class from a specification. * @param spec {object} */ function createClass(spec) { if ((typeof spec === 'undefined' ? 'undefined' : _typeof(spec)) !== 'object') throw new Error('createClass(...): class ' + 'specification must be an object, ' + ('got ' + (typeof spec === 'undefined' ? 'undefined' : _typeof(spec)))); var NewClass = function (_FlowNode) { _inherits(NewClass, _FlowNode); function NewClass() { _classCallCheck(this, NewClass); return _possibleConstructorReturn(this, Object.getPrototypeOf(NewClass).apply(this, arguments)); } return NewClass; }(_flownode2.default); ; assign(NewClass.prototype, spec); return NewClass; }