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.

148 lines
4.3 KiB

2 years ago
  1. /**
  2. * jQuery serializeObject
  3. * @copyright 2014, macek <paulmacek@gmail.com>
  4. * @link https://github.com/macek/jquery-serialize-object
  5. * @license BSD
  6. * @version 2.5.0
  7. */
  8. (function (root, factory) {
  9. // AMD
  10. if (typeof define === "function" && define.amd) {
  11. define(["exports", "jquery"], function (exports, $) {
  12. return factory(exports, $);
  13. });
  14. }
  15. // CommonJS
  16. else if (typeof exports !== "undefined") {
  17. var $ = require("jquery");
  18. factory(exports, $);
  19. }
  20. // Browser
  21. else {
  22. factory(root, (root.jQuery || root.Zepto || root.ender || root.$));
  23. }
  24. }(this, function (exports, $) {
  25. var patterns = {
  26. validate: /^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,
  27. key: /[a-z0-9_]+|(?=\[\])/gi,
  28. push: /^$/,
  29. fixed: /^\d+$/,
  30. named: /^[a-z0-9_]+$/i
  31. };
  32. function FormSerializer(helper, $form) {
  33. // private variables
  34. var data = {},
  35. pushes = {};
  36. // private API
  37. function build(base, key, value) {
  38. base[key] = value;
  39. return base;
  40. }
  41. function makeObject(root, value) {
  42. var keys = root.match(patterns.key), k;
  43. // nest, nest, ..., nest
  44. while ((k = keys.pop()) !== undefined) {
  45. // foo[]
  46. if (patterns.push.test(k)) {
  47. var idx = incrementPush(root.replace(/\[\]$/, ''));
  48. value = build([], idx, value);
  49. }
  50. // foo[n]
  51. else if (patterns.fixed.test(k)) {
  52. value = build([], k, value);
  53. }
  54. // foo; foo[bar]
  55. else if (patterns.named.test(k)) {
  56. value = build({}, k, value);
  57. }
  58. }
  59. return value;
  60. }
  61. function incrementPush(key) {
  62. if (pushes[key] === undefined) {
  63. pushes[key] = 0;
  64. }
  65. return pushes[key]++;
  66. }
  67. function encode(pair) {
  68. //debugger;
  69. switch ($('[name="' + pair.name + '"]', $form).attr("type")) {
  70. case "checkbox":
  71. return pair.value === "on" ? true : pair.value;
  72. default:
  73. var sDataType = $('[name="' + pair.name + '"]', $form).attr("data-type"),
  74. sDataName = $('[name="' + pair.name + '"]', $form).attr("data-name");
  75. if ((sDataType && sDataName && sDataName == 'int') || sDataType == 'pop') {
  76. return $('[name="' + pair.name + '"]', $form).attr("data-value") || pair.value;
  77. }
  78. else {
  79. return pair.value;
  80. }
  81. }
  82. }
  83. function addPair(pair) {
  84. if (!patterns.validate.test(pair.name)) return this;
  85. var obj = makeObject(pair.name, encode(pair));
  86. data = helper.extend(true, data, obj);
  87. return this;
  88. }
  89. function addPairs(pairs) {
  90. if (!helper.isArray(pairs)) {
  91. throw new Error("formSerializer.addPairs expects an Array");
  92. }
  93. for (var i = 0, len = pairs.length; i < len; i++) {
  94. this.addPair(pairs[i]);
  95. }
  96. return this;
  97. }
  98. function serialize() {
  99. return data;
  100. }
  101. function serializeJSON() {
  102. return JSON.stringify(serialize());
  103. }
  104. // public API
  105. this.addPair = addPair;
  106. this.addPairs = addPairs;
  107. this.serialize = serialize;
  108. this.serializeJSON = serializeJSON;
  109. }
  110. FormSerializer.patterns = patterns;
  111. FormSerializer.serializeObject = function serializeObject() {
  112. return new FormSerializer($, this).
  113. addPairs(this.serializeArray()).
  114. serialize();
  115. };
  116. FormSerializer.serializeJSON = function serializeJSON() {
  117. return new FormSerializer($, this).
  118. addPairs(this.serializeArray()).
  119. serializeJSON();
  120. };
  121. if (typeof $.fn !== "undefined") {
  122. $.fn.serializeObject = FormSerializer.serializeObject;
  123. $.fn.serializeJSON = FormSerializer.serializeJSON;
  124. }
  125. exports.FormSerializer = FormSerializer;
  126. return FormSerializer;
  127. }));