Built files from Bizgaze WebServer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

validate.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. 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; };
  2. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  3. var Validate = function Validate(table) {
  4. this.table = table;
  5. };
  6. //validate
  7. Validate.prototype.initializeColumn = function (column) {
  8. var self = this,
  9. config = [],
  10. validator;
  11. if (column.definition.validator) {
  12. if (Array.isArray(column.definition.validator)) {
  13. column.definition.validator.forEach(function (item) {
  14. validator = self._extractValidator(item);
  15. if (validator) {
  16. config.push(validator);
  17. }
  18. });
  19. } else {
  20. validator = this._extractValidator(column.definition.validator);
  21. if (validator) {
  22. config.push(validator);
  23. }
  24. }
  25. column.modules.validate = config.length ? config : false;
  26. }
  27. };
  28. Validate.prototype._extractValidator = function (value) {
  29. var type, params, pos;
  30. switch (typeof value === "undefined" ? "undefined" : _typeof(value)) {
  31. case "string":
  32. pos = value.indexOf(':');
  33. if (pos > -1) {
  34. type = value.substring(0, pos);
  35. params = value.substring(pos + 1);
  36. } else {
  37. type = value;
  38. }
  39. return this._buildValidator(type, params);
  40. break;
  41. case "function":
  42. return this._buildValidator(value);
  43. break;
  44. case "object":
  45. return this._buildValidator(value.type, value.parameters);
  46. break;
  47. }
  48. };
  49. Validate.prototype._buildValidator = function (type, params) {
  50. var func = typeof type == "function" ? type : this.validators[type];
  51. if (!func) {
  52. console.warn("Validator Setup Error - No matching validator found:", type);
  53. return false;
  54. } else {
  55. return {
  56. type: typeof type == "function" ? "function" : type,
  57. func: func,
  58. params: params
  59. };
  60. }
  61. };
  62. Validate.prototype.validate = function (validators, cell, value) {
  63. var self = this,
  64. valid = [];
  65. if (validators) {
  66. validators.forEach(function (item) {
  67. if (!item.func.call(self, cell, value, item.params)) {
  68. valid.push({
  69. type: item.type,
  70. parameters: item.params
  71. });
  72. }
  73. });
  74. }
  75. return valid.length ? valid : true;
  76. };
  77. Validate.prototype.validators = {
  78. //is integer
  79. integer: function integer(cell, value, parameters) {
  80. if (value === "" || value === null || typeof value === "undefined") {
  81. return true;
  82. }
  83. value = Number(value);
  84. return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
  85. },
  86. //is float
  87. float: function float(cell, value, parameters) {
  88. if (value === "" || value === null || typeof value === "undefined") {
  89. return true;
  90. }
  91. value = Number(value);
  92. return typeof value === 'number' && isFinite(value) && value % 1 !== 0;
  93. },
  94. //must be a number
  95. numeric: function numeric(cell, value, parameters) {
  96. if (value === "" || value === null || typeof value === "undefined") {
  97. return true;
  98. }
  99. return !isNaN(value);
  100. },
  101. //must be a string
  102. string: function string(cell, value, parameters) {
  103. if (value === "" || value === null || typeof value === "undefined") {
  104. return true;
  105. }
  106. return isNaN(value);
  107. },
  108. //maximum value
  109. max: function max(cell, value, parameters) {
  110. if (value === "" || value === null || typeof value === "undefined") {
  111. return true;
  112. }
  113. return parseFloat(value) <= parameters;
  114. },
  115. //minimum value
  116. min: function min(cell, value, parameters) {
  117. if (value === "" || value === null || typeof value === "undefined") {
  118. return true;
  119. }
  120. return parseFloat(value) >= parameters;
  121. },
  122. //minimum string length
  123. minLength: function minLength(cell, value, parameters) {
  124. if (value === "" || value === null || typeof value === "undefined") {
  125. return true;
  126. }
  127. return String(value).length >= parameters;
  128. },
  129. //maximum string length
  130. maxLength: function maxLength(cell, value, parameters) {
  131. if (value === "" || value === null || typeof value === "undefined") {
  132. return true;
  133. }
  134. return String(value).length <= parameters;
  135. },
  136. //in provided value list
  137. in: function _in(cell, value, parameters) {
  138. if (value === "" || value === null || typeof value === "undefined") {
  139. return true;
  140. }
  141. if (typeof parameters == "string") {
  142. parameters = parameters.split("|");
  143. }
  144. return value === "" || parameters.indexOf(value) > -1;
  145. },
  146. //must match provided regex
  147. regex: function regex(cell, value, parameters) {
  148. if (value === "" || value === null || typeof value === "undefined") {
  149. return true;
  150. }
  151. var reg = new RegExp(parameters);
  152. return reg.test(value);
  153. },
  154. //value must be unique in this column
  155. unique: function unique(cell, value, parameters) {
  156. if (value === "" || value === null || typeof value === "undefined") {
  157. return true;
  158. }
  159. var unique = true;
  160. var cellData = cell.getData();
  161. var column = cell.getColumn()._getSelf();
  162. this.table.rowManager.rows.forEach(function (row) {
  163. var data = row.getData();
  164. if (data !== cellData) {
  165. if (value == column.getFieldValue(data)) {
  166. unique = false;
  167. }
  168. }
  169. });
  170. return unique;
  171. },
  172. //must have a value
  173. required: function required(cell, value, parameters) {
  174. return value !== "" && value !== null && typeof value !== "undefined";
  175. }
  176. };
  177. Tabulator.prototype.registerModule("validate", Validate);