UNPKG

tnt-form

Version:
117 lines (94 loc) 2.63 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"); } } var ComponentList = function () { function ComponentList(form) { _classCallCheck(this, ComponentList); this.form = form; this.components = []; } _createClass(ComponentList, [{ key: "add", value: function add(component) { component.setForm(this.form); this.components.push(component); } }, { key: "remove", value: function remove(id) { var index = this.getIndex(id); if (index !== false) { this.components[index].destroy(); this.components.splice(index, 1); } } }, { key: "get", value: function get(id) { var index = this.getIndex(id); if (index !== false) { return this.components[index]; } for (var i = 0; i < this.components.length; i++) { if (this.components[i].components) { var component = this.components[i].components.get(id); if (component) { return component; } } } return false; } }, { key: "getIndex", value: function getIndex(id) { for (var i = 0; i < this.components.length; i++) { if (this.components[i].id == id) { return i; } } return false; } }, { key: "build", value: function build() { var el = document.createElement('div'); this.components.forEach(function (c) { el.appendChild(c.build()); }); return el; } }, { key: "validate", value: function validate(data) { this.forEach(function (c) { c.validate(data); }); } }, { key: "save", value: function save(data) { this.forEach(function (c) { return c.save(data); }); } }, { key: "forEach", value: function forEach(cb) { this.components.forEach(function (c) { if (!c.isDisabled) { cb(c); } }); } }, { key: "forEachAll", value: function forEachAll(cb) { this.components.forEach(function (c) { cb(c); }); } }]); return ComponentList; }(); module.exports = ComponentList;