This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
3rdparty.js
322 lines (261 loc) · 8 KB
/
3rdparty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
define('plugin/prnfb/3rdparty', [
'jquery'
], function($) {
/**
* jQuery serializeObject
* @copyright 2014, macek <[email protected]>
* @link https://github.com/macek/jquery-serialize-object
* @license BSD
* @version 2.5.0
*/
! function(e, i) {
if ("function" == typeof define && define.amd) define(["exports", "jquery"], function(e, r) {
return i(e, r)
});
else if ("undefined" != typeof exports) {
var r = require("jquery");
i(exports, r)
} else i(e, e.jQuery || e.Zepto || e.ender || e.$)
}(this, function(e, i) {
function r(e, r) {
function n(e, i, r) {
return e[i] = r, e
}
function a(e, i) {
for (var r, a = e.match(t.key); void 0 !== (r = a.pop());)
if (t.push.test(r)) {
var u = s(e.replace(/\[\]$/, ""));
i = n([], u, i)
} else t.fixed.test(r) ? i = n([], r, i) : t.named.test(r) && (i = n({}, r, i));
return i
}
function s(e) {
return void 0 === h[e] && (h[e] = 0), h[e]++
}
function u(e) {
switch (i('[name="' + e.name + '"]', r).attr("type")) {
case "checkbox":
return "on" === e.value ? !0 : e.value;
default:
return e.value
}
}
function f(i) {
if (!t.validate.test(i.name)) return this;
var r = a(i.name, u(i));
return l = e.extend(!0, l, r), this
}
function d(i) {
if (!e.isArray(i)) throw new Error("formSerializer.addPairs expects an Array");
for (var r = 0, t = i.length; t > r; r++) this.addPair(i[r]);
return this
}
function o() {
return l
}
function c() {
return JSON.stringify(o())
}
var l = {},
h = {};
this.addPair = f, this.addPairs = d, this.serialize = o, this.serializeJSON = c
}
var t = {
validate: /^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,
key: /[a-z0-9_]+|(?=\[\])/gi,
push: /^$/,
fixed: /^\d+$/,
named: /^[a-z0-9_]+$/i
};
return r.patterns = t, r.serializeObject = function() {
return new r(i, this).addPairs(this.serializeArray()).serialize()
}, r.serializeJSON = function() {
return new r(i, this).addPairs(this.serializeArray()).serializeJSON()
}, "undefined" != typeof i.fn && (i.fn.serializeObject = r.serializeObject, i.fn.serializeJSON = r.serializeJSON), e.FormSerializer = r, r
});
// http://davestewart.io/plugins/jquery/jquery-populate/
$.fn.populate = function(obj, options) {
// ------------------------------------------------------------------------------------------
// JSON conversion function
// convert
function parseJSON(obj, path) {
// prepare
path = path || '';
// iteration (objects / arrays)
if (obj == undefined) {
// do nothing
} else if (obj.constructor == Object) {
for (var prop in obj) {
var name = path + (path == '' ? prop : '[' + prop + ']');
parseJSON(obj[prop], name);
}
} else if (obj.constructor == Array) {
for (var i = 0; i < obj.length; i++) {
var index = options.useIndices ? i : '';
index = options.phpNaming ? '[' + index + ']' : index;
var name = path + index;
parseJSON(obj[i], name);
}
}
// assignment (values)
else {
// if the element name hasn't yet been defined, create it as a single value
if (arr[path] == undefined) {
arr[path] = obj;
}
// if the element name HAS been defined, but it's a single value, convert to an array and add the new value
else if (arr[path].constructor != Array) {
arr[path] = [arr[path], obj];
}
// if the element name HAS been defined, and is already an array, push the single value on the end of the stack
else {
arr[path].push(obj);
}
}
};
// ------------------------------------------------------------------------------------------
// population functions
function debug(str) {
if (window.console && console.log) {
console.log(str);
}
}
function getElementName(name) {
if (!options.phpNaming) {
name = name.replace(/\[\]$/, '');
}
return name;
}
function populateElement(parentElement, name, value) {
var selector = options.identifier == 'id' ? '#' + name : '[' + options.identifier + '="' + name + '"]';
var element = jQuery(selector, parentElement);
value = value.toString();
value = value == 'null' ? '' : value;
element.html(value);
}
function populateFormElement(form, name, value) {
// check that the named element exists in the form
var name = getElementName(name); // handle non-php naming
var element = form[name];
// if the form element doesn't exist, check if there is a tag with that id
if (element == undefined) {
// look for the element
element = jQuery('#' + name, form);
if (element) {
element.html(value);
return true;
}
// nope, so exit
if (options.debug) {
debug('No such element as ' + name);
}
return false;
}
// debug options
if (options.debug) {
_populate.elements.push(element);
}
// now, place any single elements in an array.
// this is so that the next bit of code (a loop) can treat them the
// same as any array-elements passed, ie radiobutton or checkox arrays,
// and the code will just work
elements = element.type == undefined && element.length ? element : [element];
// populate the element correctly
for (var e = 0; e < elements.length; e++) {
// grab the element
var element = elements[e];
// skip undefined elements or function objects (IE only)
if (!element || typeof element == 'undefined' || typeof element == 'function') {
continue;
}
// anything else, process
switch (element.type || element.tagName) {
case 'radio':
// use the single value to check the radio button
element.checked = (element.value != '' && value.toString() == element.value);
case 'checkbox':
// depends on the value.
// if it's an array, perform a sub loop
// if it's a value, just do the check
var values = value.constructor == Array ? value : [value];
for (var j = 0; j < values.length; j++) {
var valuesj = values[j];
if (valuesj == true) {
valuesj = "true";
}
element.checked |= element.value == valuesj;
}
//element.checked = (element.value != '' && value.toString().toLowerCase() == element.value.toLowerCase());
break;
case 'select-multiple':
var values = value.constructor == Array ? value : [value];
for (var i = 0; i < element.options.length; i++) {
for (var j = 0; j < values.length; j++) {
element.options[i].selected |= element.options[i].value == values[j];
}
}
break;
case 'select':
case 'select-one':
element.value = value.toString() || value;
break;
case 'text':
case 'button':
case 'textarea':
case 'submit':
default:
value = value == null ? '' : value;
element.value = value;
}
}
}
// ------------------------------------------------------------------------------------------
// options & setup
// exit if no data object supplied
if (obj === undefined) {
return this;
};
// options
var options = jQuery.extend({
phpNaming: true,
phpIndices: false,
resetForm: true,
identifier: 'id',
debug: false
},
options
);
if (options.phpIndices) {
options.phpNaming = true;
}
// ------------------------------------------------------------------------------------------
// convert hierarchical JSON to flat array
var arr = [];
parseJSON(obj);
if (options.debug) {
_populate = {
arr: arr,
obj: obj,
elements: []
}
}
// ------------------------------------------------------------------------------------------
// main process function
this.each(
function() {
// variables
var tagName = this.tagName.toLowerCase();
var method = tagName == 'form' ? populateFormElement : populateElement;
// reset form?
if (tagName == 'form' && options.resetForm) {
this.reset();
}
// update elements
for (var i in arr) {
method(this, i, arr[i]);
}
}
);
return this;
};
});