UNPKG

armstrong-react

Version:

Rocketmakers Armstrong library of React components

253 lines (252 loc) 11.1 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var _ = require("underscore"); var formValueConverters_1 = require("./formValueConverters"); var EntityItems = (function () { function EntityItems() { } EntityItems.itemsId = function (items) { return function (value) { return _.find(items, function (i) { return i.id.toString() === value; }); }; }; EntityItems.itemsNumericId = function (items) { return function (value) { return _.find(items, function (i) { return i.id.toString() === value; }); }; }; return EntityItems; }()); var FormBinderBase = (function () { function FormBinderBase(dataName, propertySet, valueConverter, propertyGet) { if (propertyGet === void 0) { propertyGet = propertySet; } this.dataName = dataName; this.propertySet = propertySet; this.valueConverter = valueConverter; this.propertyGet = propertyGet; } FormBinderBase.prototype.setElementProperty = function (props, dataBinder) { props["name"] = this.dataName; props[this.propertySet] = this.convert(dataBinder.getValue(this.dataName)); }; FormBinderBase.prototype.onChanged = function (dataBinder, newValue, notifyChanged) { var value = this.convertBack(newValue); if (_.isUndefined(value)) { return; } dataBinder.setValue(this.dataName, value); notifyChanged(); }; ; FormBinderBase.prototype.convert = function (data) { if (this.valueConverter && this.valueConverter.convert) { return this.valueConverter.convert(data); } return data; }; FormBinderBase.prototype.convertBack = function (value) { if (this.valueConverter && this.valueConverter.convertBack) { return this.valueConverter.convertBack(value); } return value; }; return FormBinderBase; }()); exports.FormBinderBase = FormBinderBase; var InputFormBinder = (function (_super) { __extends(InputFormBinder, _super); function InputFormBinder() { _super.apply(this, arguments); } InputFormBinder.prototype.handleValueChanged = function (props, dataBinder, notifyChanged) { var _this = this; props.onChange = function (e) { _this.onChanged(dataBinder, e.currentTarget[_this.propertyGet], notifyChanged); }; }; return InputFormBinder; }(FormBinderBase)); exports.InputFormBinder = InputFormBinder; var RadioFormBinder = (function (_super) { __extends(RadioFormBinder, _super); function RadioFormBinder() { _super.apply(this, arguments); } RadioFormBinder.prototype.setElementProperty = function (props, dataBinder) { props["name"] = this.dataName; props[this.propertySet] = this.convert(dataBinder.getValue(this.dataName)) === props[this.propertyGet]; }; return RadioFormBinder; }(InputFormBinder)); exports.RadioFormBinder = RadioFormBinder; var KeyboardHelper = (function () { function KeyboardHelper() { } KeyboardHelper.getNumericRegEx = function (options) { if (options) { if (options.allowNegative) { if (options.decimals) { return /[\d\.\-]/; } return /[\d\-]/; } if (options.decimals) { return /[\d\.]/; } } return /\d/; }; KeyboardHelper.numericKeyPress = function (e, options) { var regEx = KeyboardHelper.getNumericRegEx(options); var m = e.key.match(regEx); if (!m) { e.preventDefault(); return; } if (options) { var element = e.currentTarget; var selectionStart = element.selectionStart; var selectionEnd = element.selectionEnd; if (options.allowNegative) { var indexOfNegative = element.value.indexOf("-"); var negativeWillRemove = indexOfNegative >= selectionStart && indexOfNegative < selectionEnd; if (m[0] === "-") { if (negativeWillRemove) { return; } if (indexOfNegative > -1 || selectionStart > 0) { e.preventDefault(); return; } } else if (!negativeWillRemove && selectionStart <= indexOfNegative) { e.preventDefault(); return; } } if (options.decimals) { var indexOfDecimal = element.value.indexOf("."); var decimalWillRemove = indexOfDecimal >= selectionStart && indexOfDecimal < selectionEnd; if (m[0] === ".") { if (decimalWillRemove) { return; } if (indexOfDecimal > -1) { e.preventDefault(); return; } } } } }; return KeyboardHelper; }()); var FormBinder = (function () { function FormBinder() { } FormBinder.custom = function (formBinder) { return { __formBinder: formBinder }; }; FormBinder.hidden = function (dataName, valueConverter) { var adaptorInjector = FormBinder.custom(new InputFormBinder(dataName, "value", valueConverter)); adaptorInjector["type"] = "hidden"; return adaptorInjector; }; FormBinder.password = function (dataName, valueConverter) { var adaptorInjector = FormBinder.custom(new InputFormBinder(dataName, "value", valueConverter)); adaptorInjector["type"] = "password"; return adaptorInjector; }; FormBinder.text = function (dataName, valueConverter) { var adaptorInjector = FormBinder.custom(new InputFormBinder(dataName, "value", valueConverter)); adaptorInjector["type"] = "text"; return adaptorInjector; }; FormBinder.textEmail = function (dataName, valueConverter) { var adaptorInjector = FormBinder.custom(new InputFormBinder(dataName, "value", valueConverter)); adaptorInjector["type"] = "email"; return adaptorInjector; }; FormBinder.range = function (dataName, options) { var adaptorInjector = FormBinder.custom(new InputFormBinder(dataName, "value")); if (options) { adaptorInjector["min"] = options.min; adaptorInjector["max"] = options.max; adaptorInjector["step"] = options.step || 1; } return adaptorInjector; }; FormBinder.defaultText = function (dataName, valueConverter) { return FormBinder.custom(new InputFormBinder(dataName, "defaultValue", valueConverter, "value")); }; FormBinder.textNumeric = function (dataName, options) { var converter = new formValueConverters_1.NumericValueConverter(options); var adaptorInjector = FormBinder.custom(new InputFormBinder(dataName, "value", converter)); adaptorInjector["type"] = "text"; adaptorInjector["min"] = options ? options.min : null; adaptorInjector["max"] = options ? options.max : null; adaptorInjector.__formBinder.extender = function (props, ea, nc) { props.onKeyPress = function (e) { return KeyboardHelper.numericKeyPress(e, options); }; props.onBlur = function (e) { e.currentTarget["value"] = converter.convert(ea.getValue(dataName)); }; }; return adaptorInjector; }; FormBinder.selectInject = function (dataName, valueConverter) { return FormBinder.custom(new InputFormBinder(dataName, "value", valueConverter)); }; FormBinder.select = function (dataName) { return FormBinder.selectInject(dataName, formValueConverters_1.DefaultValueConverter.instance); }; FormBinder.selectNumeric = function (dataName) { return FormBinder.selectInject(dataName, new formValueConverters_1.NumericValueConverter()); }; FormBinder.selectId = function (dataName, items) { var convertBack = _.isArray(items) ? EntityItems.itemsId(items) : items; return FormBinder.selectInject(dataName, new formValueConverters_1.IdEntityValueConverter(convertBack)); }; FormBinder.selectNumericId = function (dataName, items) { var convertBack = _.isArray(items) ? EntityItems.itemsNumericId(items) : items; return FormBinder.selectInject(dataName, new formValueConverters_1.NumericIdEntityValueConverter(convertBack)); }; FormBinder.checkboxInject = function (dataName, valueConverter) { var adaptorInjector = FormBinder.custom(new InputFormBinder(dataName, "checked", valueConverter)); adaptorInjector["type"] = "checkbox"; return adaptorInjector; }; FormBinder.checkbox = function (dataName) { return FormBinder.checkboxInject(dataName); }; FormBinder.checkboxConvert = function (dataName, trueValue, falseValue) { return FormBinder.checkboxInject(dataName, new formValueConverters_1.CheckboxValueConverter(trueValue, falseValue)); }; FormBinder.radioInject = function (dataName, value, valueConverter) { var adaptorInjector = FormBinder.custom(new RadioFormBinder(dataName, "checked", valueConverter, "value")); adaptorInjector["type"] = "radio"; adaptorInjector["value"] = value; return adaptorInjector; }; FormBinder.radio = function (dataName, value) { return FormBinder.radioInject(dataName, value, formValueConverters_1.DefaultValueConverter.instance); }; FormBinder.radioNumeric = function (dataName, value) { return FormBinder.radioInject(dataName, value.toString(), new formValueConverters_1.NumericValueConverter()); }; FormBinder.radioId = function (dataName, value) { return FormBinder.radioInject(dataName, value.id, new formValueConverters_1.IdValueConverter(function (v) { return value.id; })); }; FormBinder.radioIdEntity = function (dataName, value) { return FormBinder.radioInject(dataName, value.id, new formValueConverters_1.IdEntityValueConverter(function (v) { return value; })); }; FormBinder.radioNumericIdEntity = function (dataName, value) { return FormBinder.radioInject(dataName, value.id.toString(), new formValueConverters_1.NumericIdEntityValueConverter(function (v) { return value; })); }; FormBinder.radioNumericId = function (dataName, value) { return FormBinder.radioInject(dataName, value.id.toString(), new formValueConverters_1.NumericIdValueConverter(function (v) { return value.id; })); }; return FormBinder; }()); exports.FormBinder = FormBinder;