Built files from Bizgaze WebServer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validate.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.8.2 (c) Oliver Folkerd */
  3. var Validate = function Validate(table) {
  4. this.table = table;
  5. this.invalidCells = [];
  6. };
  7. //validate
  8. Validate.prototype.initializeColumn = function (column) {
  9. var self = this,
  10. config = [],
  11. validator;
  12. if (column.definition.validator) {
  13. if (Array.isArray(column.definition.validator)) {
  14. column.definition.validator.forEach(function (item) {
  15. validator = self._extractValidator(item);
  16. if (validator) {
  17. config.push(validator);
  18. }
  19. });
  20. } else {
  21. validator = this._extractValidator(column.definition.validator);
  22. if (validator) {
  23. config.push(validator);
  24. }
  25. }
  26. column.modules.validate = config.length ? config : false;
  27. }
  28. };
  29. Validate.prototype._extractValidator = function (value) {
  30. var type, params, pos;
  31. switch (typeof value === "undefined" ? "undefined" : _typeof(value)) {
  32. case "string":
  33. pos = value.indexOf(':');
  34. if (pos > -1) {
  35. type = value.substring(0, pos);
  36. params = value.substring(pos + 1);
  37. } else {
  38. type = value;
  39. }
  40. return this._buildValidator(type, params);
  41. break;
  42. case "function":
  43. return this._buildValidator(value);
  44. break;
  45. case "object":
  46. return this._buildValidator(value.type, value.parameters);
  47. break;
  48. }
  49. };
  50. Validate.prototype._buildValidator = function (type, params) {
  51. var func = typeof type == "function" ? type : this.validators[type];
  52. if (!func) {
  53. console.warn("Validator Setup Error - No matching validator found:", type);
  54. return false;
  55. } else {
  56. return {
  57. type: typeof type == "function" ? "function" : type,
  58. func: func,
  59. params: params
  60. };
  61. }
  62. };
  63. Validate.prototype.validate = function (validators, cell, value) {
  64. var self = this,
  65. valid = [],
  66. invalidIndex = this.invalidCells.indexOf(cell);
  67. if (validators) {
  68. validators.forEach(function (item) {
  69. if (!item.func.call(self, cell.getComponent(), value, item.params)) {
  70. valid.push({
  71. type: item.type,
  72. parameters: item.params
  73. });
  74. }
  75. });
  76. }
  77. valid = valid.length ? valid : true;
  78. if (!cell.modules.validate) {
  79. cell.modules.validate = {};
  80. }
  81. if (valid === true) {
  82. cell.modules.validate.invalid = false;
  83. cell.getElement().classList.remove("tabulator-validation-fail");
  84. if (invalidIndex > -1) {
  85. this.invalidCells.splice(invalidIndex, 1);
  86. }
  87. } else {
  88. cell.modules.validate.invalid = true;
  89. if (this.table.options.validationMode !== "manual") {
  90. cell.getElement().classList.add("tabulator-validation-fail");
  91. }
  92. if (invalidIndex == -1) {
  93. this.invalidCells.push(cell);
  94. }
  95. }
  96. return valid;
  97. };
  98. Validate.prototype.getInvalidCells = function () {
  99. var output = [];
  100. this.invalidCells.forEach(function (cell) {
  101. output.push(cell.getComponent());
  102. });
  103. return output;
  104. };
  105. Validate.prototype.clearValidation = function (cell) {
  106. var invalidIndex;
  107. if (cell.modules.validate && cell.modules.validate.invalid) {
  108. cell.getElement().classList.remove("tabulator-validation-fail");
  109. cell.modules.validate.invalid = false;
  110. invalidIndex = this.invalidCells.indexOf(cell);
  111. if (invalidIndex > -1) {
  112. this.invalidCells.splice(invalidIndex, 1);
  113. }
  114. }
  115. };
  116. Validate.prototype.validators = {
  117. //is integer
  118. integer: function integer(cell, value, parameters) {
  119. if (value === "" || value === null || typeof value === "undefined") {
  120. return true;
  121. }
  122. value = Number(value);
  123. return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
  124. },
  125. //is float
  126. float: function float(cell, value, parameters) {
  127. if (value === "" || value === null || typeof value === "undefined") {
  128. return true;
  129. }
  130. value = Number(value);
  131. return typeof value === 'number' && isFinite(value) && value % 1 !== 0;
  132. },
  133. //must be a number
  134. numeric: function numeric(cell, value, parameters) {
  135. if (value === "" || value === null || typeof value === "undefined") {
  136. return true;
  137. }
  138. return !isNaN(value);
  139. },
  140. //must be a string
  141. string: function string(cell, value, parameters) {
  142. if (value === "" || value === null || typeof value === "undefined") {
  143. return true;
  144. }
  145. return isNaN(value);
  146. },
  147. //maximum value
  148. max: function max(cell, value, parameters) {
  149. if (value === "" || value === null || typeof value === "undefined") {
  150. return true;
  151. }
  152. return parseFloat(value) <= parameters;
  153. },
  154. //minimum value
  155. min: function min(cell, value, parameters) {
  156. if (value === "" || value === null || typeof value === "undefined") {
  157. return true;
  158. }
  159. return parseFloat(value) >= parameters;
  160. },
  161. //starts with value
  162. starts: function starts(cell, value, parameters) {
  163. if (value === "" || value === null || typeof value === "undefined") {
  164. return true;
  165. }
  166. return String(value).toLowerCase().startsWith(String(parameters).toLowerCase());
  167. },
  168. //ends with value
  169. ends: function ends(cell, value, parameters) {
  170. if (value === "" || value === null || typeof value === "undefined") {
  171. return true;
  172. }
  173. return String(value).toLowerCase().endsWith(String(parameters).toLowerCase());
  174. },
  175. //minimum string length
  176. minLength: function minLength(cell, value, parameters) {
  177. if (value === "" || value === null || typeof value === "undefined") {
  178. return true;
  179. }
  180. return String(value).length >= parameters;
  181. },
  182. //maximum string length
  183. maxLength: function maxLength(cell, value, parameters) {
  184. if (value === "" || value === null || typeof value === "undefined") {
  185. return true;
  186. }
  187. return String(value).length <= parameters;
  188. },
  189. //in provided value list
  190. in: function _in(cell, value, parameters) {
  191. if (value === "" || value === null || typeof value === "undefined") {
  192. return true;
  193. }
  194. if (typeof parameters == "string") {
  195. parameters = parameters.split("|");
  196. }
  197. return value === "" || parameters.indexOf(value) > -1;
  198. },
  199. //must match provided regex
  200. regex: function regex(cell, value, parameters) {
  201. if (value === "" || value === null || typeof value === "undefined") {
  202. return true;
  203. }
  204. var reg = new RegExp(parameters);
  205. return reg.test(value);
  206. },
  207. //value must be unique in this column
  208. unique: function unique(cell, value, parameters) {
  209. if (value === "" || value === null || typeof value === "undefined") {
  210. return true;
  211. }
  212. var unique = true;
  213. var cellData = cell.getData();
  214. var column = cell.getColumn()._getSelf();
  215. this.table.rowManager.rows.forEach(function (row) {
  216. var data = row.getData();
  217. if (data !== cellData) {
  218. if (value == column.getFieldValue(data)) {
  219. unique = false;
  220. }
  221. }
  222. });
  223. return unique;
  224. },
  225. //must have a value
  226. required: function required(cell, value, parameters) {
  227. return value !== "" && value !== null && typeof value !== "undefined";
  228. }
  229. };
  230. Tabulator.prototype.registerModule("validate", Validate);