123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- /**
- * jQuery serializeObject
- * @copyright 2014, macek <paulmacek@gmail.com>
- * @link https://github.com/macek/jquery-serialize-object
- * @license BSD
- * @version 2.5.0
- */
- (function(root, factory) {
- // AMD
- if (typeof define === "function" && define.amd) {
- define(["exports", "jquery"], function(exports, $) {
- return factory(exports, $);
- });
- }
- // CommonJS
- else if (typeof exports !== "undefined") {
- var $ = require("jquery");
- factory(exports, $);
- }
- // Browser
- else {
- factory(root, (root.jQuery || root.Zepto || root.ender || root.$));
- }
- }(this, function(exports, $) {
- var patterns = {
- validate: /^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,
- key: /[a-z0-9_]+|(?=\[\])/gi,
- push: /^$/,
- fixed: /^\d+$/,
- named: /^[a-z0-9_]+$/i
- };
- function FormSerializer(helper, $form) {
- // private variables
- var data = {},
- pushes = {};
- // private API
- function build(base, key, value) {
- base[key] = value;
- return base;
- }
- function makeObject(root, value) {
- var keys = root.match(patterns.key), k;
- // nest, nest, ..., nest
- while ((k = keys.pop()) !== undefined) {
- // foo[]
- if (patterns.push.test(k)) {
- var idx = incrementPush(root.replace(/\[\]$/, ''));
- value = build([], idx, value);
- }
- // foo[n]
- else if (patterns.fixed.test(k)) {
- value = build([], k, value);
- }
- // foo; foo[bar]
- else if (patterns.named.test(k)) {
- value = build({}, k, value);
- }
- }
- return value;
- }
- function incrementPush(key) {
- if (pushes[key] === undefined) {
- pushes[key] = 0;
- }
- return pushes[key]++;
- }
- function encode(pair) {
- switch ($('[name="' + pair.name + '"]', $form).attr("type")) {
- case "checkbox":
- return pair.value === "on" ? true : pair.value;
- default:
- return pair.value;
- }
- }
- function addPair(pair) {
- if (!patterns.validate.test(pair.name)) return this;
- var obj = makeObject(pair.name, encode(pair));
- data = helper.extend(true, data, obj);
- return this;
- }
- function addPairs(pairs) {
- if (!helper.isArray(pairs)) {
- throw new Error("formSerializer.addPairs expects an Array");
- }
- for (var i=0, len=pairs.length; i<len; i++) {
- this.addPair(pairs[i]);
- }
- return this;
- }
- function serialize() {
- return data;
- }
- function serializeJSON() {
- return JSON.stringify(serialize());
- }
- // public API
- this.addPair = addPair;
- this.addPairs = addPairs;
- this.serialize = serialize;
- this.serializeJSON = serializeJSON;
- }
- FormSerializer.patterns = patterns;
- FormSerializer.serializeObject = function serializeObject() {
- return new FormSerializer($, this).
- addPairs(this.serializeArray()).
- serialize();
- };
- FormSerializer.serializeJSON = function serializeJSON() {
- return new FormSerializer($, this).
- addPairs(this.serializeArray()).
- serializeJSON();
- };
- if (typeof $.fn !== "undefined") {
- $.fn.serializeObject = FormSerializer.serializeObject;
- $.fn.serializeJSON = FormSerializer.serializeJSON;
- }
- exports.FormSerializer = FormSerializer;
- return FormSerializer;
- }));
|