UNPKG

@dzeio/form-manager

Version:

A powerfull Form Manager

75 lines (74 loc) 3.29 kB
"use strict"; 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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var Functions_1 = require("../Functions"); var AttributeAbstract_1 = __importDefault(require("./AttributeAbstract")); var AttributeListeners_1 = __importDefault(require("./AttributeListeners")); var DefaultAttribute = (function (_super) { __extends(DefaultAttribute, _super); function DefaultAttribute() { return _super !== null && _super.apply(this, arguments) || this; } DefaultAttribute.prototype.trigger = function () { this.run(); return true; }; DefaultAttribute.prototype.run = function () { var _this = this; var attrVal = this.input.element.getAttribute("data-default"); var type = this.input.element.getAttribute("type") || ""; if (type === "date" || type === "time" || type === "week") { this.input.setValue(attrVal !== "" && attrVal !== null ? new Date(attrVal) : new Date()); return; } if (!attrVal) { this.input.setValue(attrVal); return; } if (Functions_1.evalF(attrVal, function (str) { return _this.input.setValue(str); })) { return; } if (attrVal && attrVal.startsWith("child:")) { var splitted = attrVal.split(":", 3); if (splitted.length !== 3) throw Error("Error: \"" + this.input.getName() + "\" data-default starts with child: but is not a three part sequence!"); var el = this.input.element.querySelector(splitted[2]); if (!el) throw Error("Error: \"" + this.input.getName() + "\" child not found!"); if (splitted[1] === "text") return this.input.setValue(el.textContent); if (splitted[1] === "value") return this.input.setValue(el.value); if (!el.hasAttribute(splitted[1])) throw Error("Error: \"" + this.input.getName() + "\" element don't have the attribute \"" + splitted[1] + "\""); return this.input.setValue(el.getAttribute(splitted[1])); } this.input.setValue(attrVal); }; DefaultAttribute.listeners = [ AttributeListeners_1.default.POST_CLEAR, AttributeListeners_1.default.FORM_INIT, AttributeListeners_1.default.FORM_FILL ]; DefaultAttribute.identity = { attribute: DefaultAttribute, dataElement: "data-default" }; return DefaultAttribute; }(AttributeAbstract_1.default)); exports.default = DefaultAttribute;