-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlRpcSerialize.js
More file actions
97 lines (80 loc) · 2.43 KB
/
Copy pathxmlRpcSerialize.js
File metadata and controls
97 lines (80 loc) · 2.43 KB
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
// This file is part of Seastorm
// Copyright 2014 Jakob Kallin
'use strict';
seastorm.xmlRpcSerialize = function(methodName, params) {
return domFromMethodCall(methodName, params);
function domFromMethodCall(name, params) {
params = params || [];
var dom = document.implementation.createDocument(null, 'methodCall', null);
var methodNameNode = createElement(configFromMethodName(name));
dom.documentElement.appendChild(methodNameNode);
if ( params.length > 0 ) {
var paramNode = createElement(configFromParamList(params));
dom.documentElement.appendChild(paramNode);
}
return dom;
function configFromMethodName(name) {
return { methodName: name };
}
function configFromParamList(params) {
return { params: params.map(configFromParam) };
}
function configFromParam(value) {
return { param: configFromValue(value) };
}
function configFromValue(value) {
if ( typeof value === 'object' ) {
return configFromObject(value);
} else {
return configFromScalar(value);
}
}
function configFromScalar(value) {
var typeName = ({
string: 'string',
number: 'int' // Also converts doubles to `int`.
})[typeof value];
if ( typeName === 'string' ) {
return { value: value };
} else if ( typeName ) {
var config = { value: {} };
config.value[typeName] = value;
return config;
} else {
throw new Error(
'This value cannot be converted into an element: ' + value
);
}
}
function configFromObject(object) {
var memberConfig = Object.keys(object).map(function(property) {
return configFromProperty(object, property);
});
return { struct: memberConfig };
}
function configFromProperty(object, property) {
var value = object[property];
return { member: [
{ name: property },
configFromValue(value)
]};
}
function createElement(config) {
var tagName = Object.keys(config)[0];
var element = dom.createElement(tagName);
var contents = config[tagName];
if ( typeof contents === 'object' ) {
if ( contents instanceof Array ) {
contents.forEach(function(contentConfig) {
element.appendChild(createElement(contentConfig));
});
} else {
element.appendChild(createElement(contents));
}
} else {
element.textContent = contents;
}
return element;
}
}
};