gd-bs
Version:
Bootstrap JavaScript, TypeScript and Web Components library.
234 lines (233 loc) • 8.69 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormControl = exports.Form = void 0;
var base_1 = require("../base");
var control_1 = require("./control");
var group_1 = require("./group");
var row_1 = require("./row");
var templates_1 = require("./templates");
var types_1 = require("./types");
__exportStar(require("./custom"), exports);
__exportStar(require("./types"), exports);
/**
* Form
* @property props - The form properties.
*/
var _Form = /** @class */ (function (_super) {
__extends(_Form, _super);
// Constructor
function _Form(props) {
var _this = _super.call(this, templates_1.HTML, props) || this;
_this._groups = null;
_this._rows = null;
// Configure the form
_this.configure();
// Configure the events
_this.configureEvents();
// Configure the parent
_this.configureParent();
return _this;
}
// Configure the form
_Form.prototype.configure = function () {
// Clear the groups and rows
this._groups = [];
this._rows = [];
// Add the class name
var classNames = (this.props.className || "").split(" ");
for (var i = 0; i < classNames.length; i++) {
var className = classNames[i];
// Append the class name
className ? this.el.classList.add(className) : null;
}
// Set the floating class
this.props.isFloating ? this.el.classList.add("form-floating") : null;
// Append the controls
this.appendControls(this.props.controls);
// Append the rows
this.appendRows(this.props.rows);
};
// Configure the events
_Form.prototype.configureEvents = function () {
var _this = this;
// See if an onrendered event exists
if (this.props.onRendered) {
// Wait before executing the rendered event, otherwise the controls will be null
var intervalId_1 = setInterval(function () {
var isLoaded = true;
// Parse the controls
for (var i = 0; i < _this.controls.length; i++) {
var control = _this.controls[i];
// Set the flag
isLoaded = isLoaded && control && control.isRendered;
}
// See if the form is loaded
if (isLoaded) {
// Clear the interval
clearInterval(intervalId_1);
// Execute the event
_this.props.onRendered(_this.controls);
}
}, 10);
}
};
/**
* Public Interface
*/
// Append controls to the form
_Form.prototype.appendControls = function (controls) {
if (controls === void 0) { controls = []; }
// Parse the controls
for (var i = 0; i < controls.length; i++) {
// Create the group
var group = new group_1.FormGroup(controls[i], this.props);
this._groups.push(group);
this.el.appendChild(group.el);
}
};
// Append rows to the form
_Form.prototype.appendRows = function (rows) {
if (rows === void 0) { rows = []; }
// Parse the rows
for (var i = 0; i < rows.length; i++) {
// Create the row
var row = new row_1.FormRow(rows[i], this.props);
this._rows.push(row);
this.el.appendChild(row.el);
}
};
Object.defineProperty(_Form.prototype, "controls", {
// The forms controls
get: function () {
var controls = [];
// Parse the groups
for (var i = 0; i < this._groups.length; i++) {
// Add the control
controls.push(this._groups[i].control);
}
// Parse the rows
for (var i = 0; i < this._rows.length; i++) {
// Add the controls
controls = controls.concat(this._rows[i].controls);
}
// Return the controls
return controls;
},
enumerable: false,
configurable: true
});
// Gets a form control by its name
_Form.prototype.getControl = function (name) {
// Parse the controls
var controls = this.controls;
for (var i = 0; i < controls.length; i++) {
var control = controls[i];
// See if this is the control we are looking for
if (control && control.props && control.props.name == name) {
// Return the control
return control;
}
}
// Control not found
return null;
};
// Gets the form values
_Form.prototype.getValues = function () {
var values = {};
// Parse the controls
var controls = this.controls;
for (var i = 0; i < controls.length; i++) {
var control = controls[i];
if (control.props.name) {
// Set the value
values[control.props.name] = control.getValue();
}
}
// Return the values
return values;
};
// Inserts a control into the form
_Form.prototype.insertControl = function (idx, control) {
// Create the group
var group = new group_1.FormGroup(control, this.props);
this._groups.push(group);
// Validate the index
if (idx < this.el.childElementCount) {
// Insert the control
this.el.insertBefore(group.el, this.el.childNodes[idx]);
}
else {
// Append the control
this.el.appendChild(group.el);
}
};
// Validates the form
_Form.prototype.isValid = function () {
var isValid = true;
// Parse the controls
var controls = this.controls;
for (var i = 0; i < controls.length; i++) {
// See if this control is valid
if (controls[i].isValid == false) {
// Set the flag
isValid = false;
}
}
// Update the classes
this.el.classList.remove("needs-validation");
this.el.classList.add("was-validated");
// Return the flag
return isValid;
};
return _Form;
}(base_1.Base));
var Form = function (props) { return new _Form(props); };
exports.Form = Form;
/**
* Form Control
*/
var FormControl = function (props) {
// Create a base object
var base = new base_1.Base("", props);
// Create the control
var control = new control_1.FormControl(props, { validationType: types_1.FormValidationTypes.Default });
// Wait for the control to be loaded
control.isLoaded().then(function () {
// Set the element
base.el = control.el;
// Configure the parent
base.configureParent();
});
// Return the control
return control;
};
exports.FormControl = FormControl;