tnt-form
Version:
93 lines (65 loc) • 3.5 kB
JavaScript
;
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; }; }();
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 Component = require('../component'),
ComponentList = require('../component-list');
var Stack = function (_Component) {
_inherits(Stack, _Component);
function Stack(id, components) {
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, Stack);
var _this = _possibleConstructorReturn(this, (Stack.__proto__ || Object.getPrototypeOf(Stack)).call(this, id, opts));
var _opts$label = opts.label;
_this.label = _opts$label === undefined ? '' : _opts$label;
var _opts$direction = opts.direction;
_this.direction = _opts$direction === undefined ? 'horizontal' : _opts$direction;
_this.rawComponents = components;
return _this;
}
_createClass(Stack, [{
key: 'build',
value: function build() {
var _this2 = this;
this.components = new ComponentList(this.form);
this.rawComponents.forEach(function (c) {
return _this2.components.add(c);
});
this.el = document.createElement('div');
this.el.classList.add('stack');
this.el.classList.add(this.direction);
var componentContainer = document.createElement('div');
if (this.label) {
var label = document.createElement('div');
label.classList.add('stack-title');
label.textContent = this.label;
componentContainer.appendChild(label);
this.el.classList.add('has-title');
}
componentContainer.classList.add('stack-wrap');
this.el.appendChild(componentContainer);
componentContainer.appendChild(this.components.build());
return this.el;
}
}, {
key: 'validate',
value: function validate(data) {
var _this3 = this;
this.errors = [];
this.components.validate(data);
this.components.forEach(function (c) {
if (c.errors.length) {
_this3.errors.push({ id: 'stackContainsError', default: 'One or more fields in the stack has an error' });
}
});
}
}, {
key: 'save',
value: function save(data) {
this.components.save(data);
}
}]);
return Stack;
}(Component);
module.exports = Stack;