PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/proengsoft/laravel-jsvalidation/resources/assets/js/helpers.js

https://bitbucket.org/hiolen/dev-deedel
JavaScript | 339 lines | 170 code | 44 blank | 125 comment | 33 complexity | 06d0ee2286325fb9ce3b28648c29e771 MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, BitTorrent-1.0, LGPL-2.0
  1. /*!
  2. * Laravel Javascript Validation
  3. *
  4. * https://github.com/proengsoft/laravel-jsvalidation
  5. *
  6. * Helper functions used by validators
  7. *
  8. * Copyright (c) 2014 Proengsoft
  9. * Released under the MIT license
  10. */
  11. $.extend(true, laravelValidation, {
  12. helpers: {
  13. /**
  14. * Numeric rules
  15. */
  16. numericRules: ['Integer', 'Numeric'],
  17. /**
  18. * Gets the file information from file input
  19. *
  20. * @param fieldObj
  21. * @param index
  22. * @returns {{file: *, extension: string, size: number}}
  23. */
  24. fileinfo: function (fieldObj, index) {
  25. var FileName = fieldObj.value;
  26. index = typeof index !== 'undefined' ? index : 0;
  27. if ( fieldObj.files !== null ) {
  28. if (typeof fieldObj.files[index] !== 'undefined') {
  29. return {
  30. file: FileName,
  31. extension: FileName.substr(FileName.lastIndexOf('.') + 1),
  32. size: fieldObj.files[index].size / 1024,
  33. type: fieldObj.files[index].type
  34. };
  35. }
  36. }
  37. return false;
  38. },
  39. /**
  40. *
  41. * Gets the selectors for th specified field names
  42. *
  43. * @param names
  44. * @returns {string}
  45. */
  46. selector: function (names) {
  47. var selector = [];
  48. if (!$.isArray(names)) {
  49. names = [names];
  50. }
  51. for (var i = 0; i < names.length; i++) {
  52. selector.push("[name='" + names[i] + "']");
  53. }
  54. return selector.join();
  55. },
  56. /**
  57. * Check if element has numeric rules
  58. *
  59. * @param element
  60. * @returns {boolean}
  61. */
  62. hasNumericRules: function (element) {
  63. return this.hasRules(element, this.numericRules);
  64. },
  65. /**
  66. * Check if element has passed ruls rules
  67. *
  68. * @param element
  69. * @param rules
  70. * @returns {boolean}
  71. */
  72. hasRules: function (element, rules) {
  73. var found = false;
  74. if (typeof rules === 'string') {
  75. rules = [rules];
  76. }
  77. var validator = $.data(element.form, "validator");
  78. var listRules = [];
  79. var cache = validator.arrayRulesCache;
  80. if (element.name in cache) {
  81. $.each(cache[element.name], function (index, arrayRule) {
  82. listRules.push(arrayRule);
  83. });
  84. }
  85. if (element.name in validator.settings.rules) {
  86. listRules.push(validator.settings.rules[element.name]);
  87. }
  88. $.each(listRules, function(index,objRules){
  89. if ('laravelValidation' in objRules) {
  90. var _rules=objRules.laravelValidation;
  91. for (var i = 0; i < _rules.length; i++) {
  92. if ($.inArray(_rules[i][0],rules) !== -1) {
  93. found = true;
  94. return false;
  95. }
  96. }
  97. }
  98. });
  99. return found;
  100. },
  101. /**
  102. * Return the string length using PHP function
  103. * http://php.net/manual/en/function.strlen.php
  104. * http://phpjs.org/functions/strlen/
  105. *
  106. * @param string
  107. */
  108. strlen: function (string) {
  109. return strlen(string);
  110. },
  111. /**
  112. * Get the size of the object depending of his type
  113. *
  114. * @param obj
  115. * @param element
  116. * @param value
  117. * @returns int
  118. */
  119. getSize: function getSize(obj, element, value) {
  120. if (this.hasNumericRules(element) && this.is_numeric(value)) {
  121. return parseFloat(value);
  122. } else if ($.isArray(value)) {
  123. return parseFloat(value.length);
  124. } else if (element.type === 'file') {
  125. return parseFloat(Math.floor(this.fileinfo(element).size));
  126. }
  127. return parseFloat(this.strlen(value));
  128. },
  129. /**
  130. * Return specified rule from element
  131. *
  132. * @param rule
  133. * @param element
  134. * @returns object
  135. */
  136. getLaravelValidation: function(rule, element) {
  137. var found = undefined;
  138. $.each($.validator.staticRules(element), function(key, rules) {
  139. if (key==="laravelValidation") {
  140. $.each(rules, function (i, value) {
  141. if (value[0]===rule) {
  142. found=value;
  143. }
  144. });
  145. }
  146. });
  147. return found;
  148. },
  149. /**
  150. * Return he timestamp of value passed using format or default format in element*
  151. *
  152. * @param value
  153. * @param format
  154. * @returns {boolean|int}
  155. */
  156. parseTime: function (value, format) {
  157. var timeValue = false;
  158. var fmt = new DateFormatter();
  159. if ($.type(format) === 'object') {
  160. var dateRule=this.getLaravelValidation('DateFormat', format);
  161. if (dateRule !== undefined) {
  162. format = dateRule[1][0];
  163. } else {
  164. format = null;
  165. }
  166. }
  167. if (format == null) {
  168. timeValue = this.strtotime(value);
  169. } else {
  170. timeValue = fmt.parseDate(value, format);
  171. if (timeValue) {
  172. timeValue = Math.round((timeValue.getTime() / 1000));
  173. }
  174. }
  175. return timeValue;
  176. },
  177. /**
  178. * This method allows you to intelligently guess the date by closely matching the specific format.
  179. * @param value
  180. * @param format
  181. * @returns {Date}
  182. */
  183. gessDate: function (value, format) {
  184. var fmt = new DateFormatter();
  185. return fmt.guessDate(value, format)
  186. },
  187. /**
  188. * Returns Unix timestamp based on PHP function strototime
  189. * http://php.net/manual/es/function.strtotime.php
  190. * http://phpjs.org/functions/strtotime/
  191. *
  192. * @param text
  193. * @param now
  194. * @returns {*}
  195. */
  196. strtotime: function (text, now) {
  197. return strtotime(text, now)
  198. },
  199. /**
  200. * Returns if value is numeric
  201. * http://php.net/manual/es/var.is_numeric.php
  202. * http://phpjs.org/functions/is_numeric/
  203. *
  204. * @param mixed_var
  205. * @returns {*}
  206. */
  207. is_numeric: function (mixed_var) {
  208. return is_numeric(mixed_var)
  209. },
  210. /**
  211. * Returns Array diff based on PHP function array_diff
  212. * http://php.net/manual/es/function.array_diff.php
  213. * http://phpjs.org/functions/array_diff/
  214. *
  215. * @param arr1
  216. * @param arr2
  217. * @returns {*}
  218. */
  219. arrayDiff: function (arr1, arr2) {
  220. return array_diff(arr1, arr2);
  221. },
  222. /**
  223. * Makes element dependant from other
  224. *
  225. * @param validator
  226. * @param element
  227. * @param name
  228. * @returns {*}
  229. */
  230. dependentElement: function(validator, element, name) {
  231. var el=validator.findByName(name);
  232. if ( el[0]!==undefined && validator.settings.onfocusout ) {
  233. var event = 'blur';
  234. if (el[0].tagName === 'SELECT' ||
  235. el[0].tagName === 'OPTION' ||
  236. el[0].type === 'checkbox' ||
  237. el[0].type === 'radio'
  238. ) {
  239. event = 'click';
  240. }
  241. var ruleName = '.validate-laravelValidation';
  242. el.off( ruleName )
  243. .off(event + ruleName + '-' + element.name)
  244. .on( event + ruleName + '-' + element.name, function() {
  245. $( element ).valid();
  246. });
  247. }
  248. return el[0];
  249. },
  250. /**
  251. * Parses error Ajax response and gets the message
  252. *
  253. * @param response
  254. * @returns {string[]}
  255. */
  256. parseErrorResponse: function (response) {
  257. var newResponse = ['Whoops, looks like something went wrong.'];
  258. if ('responseText' in response) {
  259. var errorMsg = response.responseText.match(/<h1\s*>(.*)<\/h1\s*>/i);
  260. if ($.isArray(errorMsg)) {
  261. newResponse = [errorMsg[1]];
  262. }
  263. }
  264. return newResponse;
  265. },
  266. /**
  267. * Escape string to use as Regular Expression
  268. * @param str
  269. * @returns string
  270. */
  271. escapeRegExp: function (str) {
  272. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  273. },
  274. /**
  275. * Generate RegExp from wildcard attributes
  276. * @param name
  277. * @returns {RegExp}
  278. */
  279. regexFromWildcard: function(name) {
  280. var nameParts = name.split("[*]");
  281. if (nameParts.length === 1) {
  282. nameParts.push('');
  283. }
  284. var regexpParts = nameParts.map(function(currentValue, index) {
  285. if (index % 2 === 0) {
  286. currentValue = currentValue + '[';
  287. } else {
  288. currentValue = ']' +currentValue;
  289. }
  290. return laravelValidation.helpers.escapeRegExp(currentValue);
  291. });
  292. return new RegExp('^'+regexpParts.join('.*')+'$');
  293. }
  294. }
  295. });