@aappddeevv/dynamics-client-ui
Version:
## What is it? A library to help you create great dynamics applications.
153 lines (123 loc) • 4.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FormController = exports.CommonRegex = undefined;
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Utilitiy belt for working with forms and data validation/transformation.
*/
var CommonRegex = exports.CommonRegex = {
Integer: /^\d+$/,
FloatingPoint: /^[+-]?\d+(\.\d+)?$/,
Email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
/** Action: ETL */
};
var ETL = function ETL() {
var _this = this;
(0, _classCallCheck3.default)(this, ETL);
this.init = function () {
var xrm = _this.controller.xrm;
_this.sourceAttribute = xrm.getAttribute(_this.source);
if (_this.sourceAttribute) _this.attrValid = true;
if (_this.attrValid) _this.crmControls = _this.sourceAttribute.controls;
_this.targets.forEach(function (t) {
return _this.targetAttributes.push(xrm.getAttribute(t));
});
// attach handlers to the change event
if (_this.attrValid) {
_this.sourceAttribute.addOnChange(_this.onChangeHandler);
}
_this.setter = function (value) {
if (_this.attrValid) {
_this.targetAttributes.forEach(function (t) {
return t.setValue(value);
});
} else {
// do nothing
}
};
};
this.active = function (flag) {
_this.active = flag;
};
this.deactivate = function () {
_this.active(false);
};
this.activate = function () {
_this.active(true);
};
this.onChangeHandler = function (ctx) {
_this.crmControls.forEach(function (c) {
return c.clearNotification(_this.errorId);
});
if (_this.sourceAttribute) {
try {
var get = _this.sourceAttribute.getValue();
var ok = _this.validator({ contetx: ctx, value: get, clientData: _this.clientData });
if (!ok) {
_this.crmControls.forEach(function (c) {
return c.setNotification(_this.message, _this.errorId);
});
} else {
var transformed = _this.transform ? _this.transform({ value: get, clientData: _this.clientData }) : get;
if (_this.setter) _this.setter(transformed);
}
} catch (e) {
console.log("Error in processing ETL for attribute", _this.source, _this);
}
}
};
this.controller = null;
this.source = null;
this.controls = [];
this.validator = null; // func, takes extracted {context, value, clientData} as args
this.transform = null; // takes {value, clientData}
this.setter = null; // thunk that sets the value, takes (value)
this.targets = []; // target attributes
this.active = false;
this.message = null;
this.clientData = {};
this.sourceAttribute = null;
this.crmControls = [];
this.attrValid = false;
this.errorId = this.source + "-errorid";
this.targetAttributes = [];
}
/** Extract, validate, transform, then set. */
;
/** Controls multiple actions per form page. Handles new forms and onSave events properly. */
var FormController = exports.FormController = function FormController() {
var _this2 = this;
var xrm = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
(0, _classCallCheck3.default)(this, FormController);
this.actions = [];
this.addValidation = function (source, validator, message, targets) {
var clientData = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
var etl = new ETL();
_this2.controller = _this2;
etl.source = source;
etl.validator = validator;
etl.targets = targets;
etl.clientData = clientData;
etl.activate();
etl.init();
actions.push(etl);
return etl;
};
this.activate = function () {};
this.deactivate = function () {};
this.xrm = xrm;
if (this.xrm === null || this.xrm === undefined) this.xrm = Xrm || window.parent.Xrm;
}
/** All actions. */
/**
* @param source Name of entity attribute that are sources.
* @param validator Function to validate content when its changed.
* @param message Notification message for the control if validation fails.
* @param targets Target attribute (or array) to set if valid value.
*/
;
//# sourceMappingURL=FormUtilities.js.map