armo-breadboard
Version:
Edit a live React component's source in real time.
117 lines (91 loc) • 5.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _class, _temp;
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactControllers = require('react-controllers');
var _armoUtil = require('armo-util');
var _BreadboardBuilder = require('./BreadboardBuilder');
var _BreadboardBuilder2 = _interopRequireDefault(_BreadboardBuilder);
var _ConsoleController = require('./ConsoleController');
var _ConsoleController2 = _interopRequireDefault(_ConsoleController);
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 BreadboardController = (_temp = _class = function (_Controller) {
_inherits(BreadboardController, _Controller);
function BreadboardController(props) {
_classCallCheck(this, BreadboardController);
var _this = _possibleConstructorReturn(this, (BreadboardController.__proto__ || Object.getPrototypeOf(BreadboardController)).call(this, props));
var transformedSources = _this.setTransformsAndBuild(props.transforms, props.sources.model.value);
_this.state = {
transformedSources: transformedSources,
sourcesChangedSinceLastBuild: false,
firstTransformError: findFirstError(transformedSources),
consoleController: (0, _reactControllers.instantiateController)(_ConsoleController2.default)
};
return _this;
}
_createClass(BreadboardController, [{
key: 'controllerWillReceiveProps',
value: function controllerWillReceiveProps(nextProps) {
var nextModes = nextProps.modes;
var nextModeRequiresBuild = nextModes.browser || nextModes.console || nextModes.transformedSource;
var sourcesDidChange = nextProps.sources !== this.props.sources;
var rebuild = false;
if (nextProps.transforms !== this.props.transforms) {
var transformedSources = this.setTransformsAndBuild(nextProps.transforms, nextProps.sources.model.value);
this.setState({
transformedSources: transformedSources,
firstTransformError: findFirstError(transformedSources)
});
} else if (nextModeRequiresBuild && sourcesDidChange || this.state.sourcesChangedSinceLastBuild) {
var _transformedSources = this.builder.buildAll(nextProps.sources.model.value);
this.setState({
transformedSources: _transformedSources,
sourcesChangedSinceLastBuild: false,
firstTransformError: findFirstError(_transformedSources)
});
} else if (sourcesDidChange && !this.state.sourcesChangedSinceLastBuild) {
this.setState({
sourcesChangedSinceLastBuild: true
});
}
}
}, {
key: 'setTransformsAndBuild',
value: function setTransformsAndBuild(transforms, sources) {
this.builder = new _BreadboardBuilder2.default(transforms);
return this.builder.buildAll(sources);
}
}]);
return BreadboardController;
}(_reactControllers.Controller), _class.propTypes = {
modes: _propTypes2.default.shape({
transformedSource: _propTypes2.default.bool,
browser: _propTypes2.default.bool,
console: _propTypes2.default.bool
}).isRequired,
sources: _propTypes2.default.shape({
bus: _propTypes2.default.object.isRequired,
model: _propTypes2.default.shape({
value: _propTypes2.default.objectOf(_propTypes2.default.string).isRequired,
onChange: _propTypes2.default.func.isRequired
})
}).isRequired,
transforms: _propTypes2.default.arrayOf(_propTypes2.default.array).isRequired,
// TODO: if we have transformedSource but not browser/console, only
// transform appropriate source
selected: _propTypes2.default.object
}, _class.actions = {}, _temp);
exports.default = BreadboardController;
function findFirstError(obj) {
return Object.values(obj).find(function (x) {
return x instanceof Error;
});
}