123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
-
- /* Tabulator v4.6.3 (c) Oliver Folkerd */
-
- var Validate = function Validate(table) {
- this.table = table;
- };
-
- //validate
- Validate.prototype.initializeColumn = function (column) {
- var self = this,
- config = [],
- validator;
-
- if (column.definition.validator) {
-
- if (Array.isArray(column.definition.validator)) {
- column.definition.validator.forEach(function (item) {
- validator = self._extractValidator(item);
-
- if (validator) {
- config.push(validator);
- }
- });
- } else {
- validator = this._extractValidator(column.definition.validator);
-
- if (validator) {
- config.push(validator);
- }
- }
-
- column.modules.validate = config.length ? config : false;
- }
- };
-
- Validate.prototype._extractValidator = function (value) {
- var type, params, pos;
-
- switch (typeof value === "undefined" ? "undefined" : _typeof(value)) {
- case "string":
- pos = value.indexOf(':');
-
- if (pos > -1) {
- type = value.substring(0, pos);
- params = value.substring(pos + 1);
- } else {
- type = value;
- }
-
- return this._buildValidator(type, params);
- break;
-
- case "function":
- return this._buildValidator(value);
- break;
-
- case "object":
- return this._buildValidator(value.type, value.parameters);
- break;
- }
- };
-
- Validate.prototype._buildValidator = function (type, params) {
-
- var func = typeof type == "function" ? type : this.validators[type];
-
- if (!func) {
- console.warn("Validator Setup Error - No matching validator found:", type);
- return false;
- } else {
- return {
- type: typeof type == "function" ? "function" : type,
- func: func,
- params: params
- };
- }
- };
-
- Validate.prototype.validate = function (validators, cell, value) {
- var self = this,
- valid = [];
-
- if (validators) {
- validators.forEach(function (item) {
- if (!item.func.call(self, cell, value, item.params)) {
- valid.push({
- type: item.type,
- parameters: item.params
- });
- }
- });
- }
-
- return valid.length ? valid : true;
- };
-
- Validate.prototype.validators = {
-
- //is integer
- integer: function integer(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- value = Number(value);
- return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
- },
-
- //is float
- float: function float(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- value = Number(value);
- return typeof value === 'number' && isFinite(value) && value % 1 !== 0;
- },
-
- //must be a number
- numeric: function numeric(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- return !isNaN(value);
- },
-
- //must be a string
- string: function string(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- return isNaN(value);
- },
-
- //maximum value
- max: function max(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- return parseFloat(value) <= parameters;
- },
-
- //minimum value
- min: function min(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- return parseFloat(value) >= parameters;
- },
-
- //minimum string length
- minLength: function minLength(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- return String(value).length >= parameters;
- },
-
- //maximum string length
- maxLength: function maxLength(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- return String(value).length <= parameters;
- },
-
- //in provided value list
- in: function _in(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- if (typeof parameters == "string") {
- parameters = parameters.split("|");
- }
-
- return value === "" || parameters.indexOf(value) > -1;
- },
-
- //must match provided regex
- regex: function regex(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- var reg = new RegExp(parameters);
-
- return reg.test(value);
- },
-
- //value must be unique in this column
- unique: function unique(cell, value, parameters) {
- if (value === "" || value === null || typeof value === "undefined") {
- return true;
- }
- var unique = true;
-
- var cellData = cell.getData();
- var column = cell.getColumn()._getSelf();
-
- this.table.rowManager.rows.forEach(function (row) {
- var data = row.getData();
-
- if (data !== cellData) {
- if (value == column.getFieldValue(data)) {
- unique = false;
- }
- }
- });
-
- return unique;
- },
-
- //must have a value
- required: function required(cell, value, parameters) {
- return value !== "" && value !== null && typeof value !== "undefined";
- }
- };
-
- Tabulator.prototype.registerModule("validate", Validate);
|