UNPKG

tnt-form

Version:
116 lines (85 loc) 3.79 kB
"use strict"; 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 ComponentList = require('./component-list'), Eventable = require('./eventable'); var Form = function (_Eventable) { _inherits(Form, _Eventable); function Form(id) { var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, Form); var _this = _possibleConstructorReturn(this, (Form.__proto__ || Object.getPrototypeOf(Form)).call(this)); _this.id = id; _this.errorMessages = {}; _this.components = new ComponentList(_this); var _opts$submitButtonTex = opts.submitButtonText; _this.submitButtonText = _opts$submitButtonTex === undefined ? 'Send' : _opts$submitButtonTex; var _opts$inlineErrorMess = opts.inlineErrorMessages; _this.inlineErrorMessages = _opts$inlineErrorMess === undefined ? false : _opts$inlineErrorMess; return _this; } _createClass(Form, [{ key: 'addField', value: function addField(component) { this.components.add(component); } }, { key: 'removeField', value: function removeField(id) { this.components.remove(id); } }, { key: 'getField', value: function getField(id) { return this.components.get(id); } }, { key: 'build', value: function build() { var _this2 = this; var container = document.createElement('form'); container.setAttribute('enctype', 'multipart/form-data'); var saveBtn = document.createElement('button'); saveBtn.setAttribute('type', 'submit'); saveBtn.textContent = this.submitButtonText; container.appendChild(this.components.build()); container.appendChild(saveBtn); container.addEventListener('submit', function (e) { if (_this2.validate()) { _this2.save(); } e.preventDefault(); }); return container; } }, { key: 'setErrorMessages', value: function setErrorMessages(messages) { this.errorMessages = messages; } }, { key: 'validate', value: function validate(data) { this.components.validate(data); var valid = true; this.components.forEach(function (c) { if (c.errors.length) { valid = false; } c.postValidate(); }); return valid; } }, { key: 'save', value: function save() { var data = {}; this.components.save(data); this.trigger('submit', { data: data }); } }]); return Form; }(Eventable); module.exports = Form;