kendo.data.xml.js
12.4 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
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
/**
* Kendo UI v2016.1.112 (http://www.telerik.com/kendo-ui)
* Copyright 2016 Telerik AD. All rights reserved.
*
* Kendo UI commercial licenses may be obtained at
* http://www.telerik.com/purchase/license-agreement/kendo-ui-complete
* If you do not own a commercial license, this file shall be governed by the trial license terms.
*/
(function (f, define) {
define('kendo.data.xml', ['kendo.core'], f);
}(function () {
var __meta__ = {
id: 'data.xml',
name: 'XML',
category: 'framework',
depends: ['core'],
hidden: true
};
(function ($, undefined) {
var kendo = window.kendo, isArray = $.isArray, isPlainObject = $.isPlainObject, map = $.map, each = $.each, extend = $.extend, getter = kendo.getter, Class = kendo.Class;
var XmlDataReader = Class.extend({
init: function (options) {
var that = this, total = options.total, model = options.model, parse = options.parse, errors = options.errors, serialize = options.serialize, data = options.data;
if (model) {
if (isPlainObject(model)) {
var base = options.modelBase || kendo.data.Model;
if (model.fields) {
each(model.fields, function (field, value) {
if (isPlainObject(value) && value.field) {
if (!$.isFunction(value.field)) {
value = extend(value, { field: that.getter(value.field) });
}
} else {
value = { field: that.getter(value) };
}
model.fields[field] = value;
});
}
var id = model.id;
if (id) {
var idField = {};
idField[that.xpathToMember(id, true)] = { field: that.getter(id) };
model.fields = extend(idField, model.fields);
model.id = that.xpathToMember(id);
}
model = base.define(model);
}
that.model = model;
}
if (total) {
if (typeof total == 'string') {
total = that.getter(total);
that.total = function (data) {
return parseInt(total(data), 10);
};
} else if (typeof total == 'function') {
that.total = total;
}
}
if (errors) {
if (typeof errors == 'string') {
errors = that.getter(errors);
that.errors = function (data) {
return errors(data) || null;
};
} else if (typeof errors == 'function') {
that.errors = errors;
}
}
if (data) {
if (typeof data == 'string') {
data = that.xpathToMember(data);
that.data = function (value) {
var result = that.evaluate(value, data), modelInstance;
result = isArray(result) ? result : [result];
if (that.model && model.fields) {
modelInstance = new that.model();
return map(result, function (value) {
if (value) {
var record = {}, field;
for (field in model.fields) {
record[field] = modelInstance._parse(field, model.fields[field].field(value));
}
return record;
}
});
}
return result;
};
} else if (typeof data == 'function') {
that.data = data;
}
}
if (typeof parse == 'function') {
var xmlParse = that.parse;
that.parse = function (data) {
var xml = parse.call(that, data);
return xmlParse.call(that, xml);
};
}
if (typeof serialize == 'function') {
that.serialize = serialize;
}
},
total: function (result) {
return this.data(result).length;
},
errors: function (data) {
return data ? data.errors : null;
},
serialize: function (data) {
return data;
},
parseDOM: function (element) {
var result = {}, parsedNode, node, nodeType, nodeName, member, attribute, attributes = element.attributes, attributeCount = attributes.length, idx;
for (idx = 0; idx < attributeCount; idx++) {
attribute = attributes[idx];
result['@' + attribute.nodeName] = attribute.nodeValue;
}
for (node = element.firstChild; node; node = node.nextSibling) {
nodeType = node.nodeType;
if (nodeType === 3 || nodeType === 4) {
result['#text'] = node.nodeValue;
} else if (nodeType === 1) {
parsedNode = this.parseDOM(node);
nodeName = node.nodeName;
member = result[nodeName];
if (isArray(member)) {
member.push(parsedNode);
} else if (member !== undefined) {
member = [
member,
parsedNode
];
} else {
member = parsedNode;
}
result[nodeName] = member;
}
}
return result;
},
evaluate: function (value, expression) {
var members = expression.split('.'), member, result, length, intermediateResult, idx;
while (member = members.shift()) {
value = value[member];
if (isArray(value)) {
result = [];
expression = members.join('.');
for (idx = 0, length = value.length; idx < length; idx++) {
intermediateResult = this.evaluate(value[idx], expression);
intermediateResult = isArray(intermediateResult) ? intermediateResult : [intermediateResult];
result.push.apply(result, intermediateResult);
}
return result;
}
}
return value;
},
parse: function (xml) {
var documentElement, tree, result = {};
documentElement = xml.documentElement || $.parseXML(xml).documentElement;
tree = this.parseDOM(documentElement);
result[documentElement.nodeName] = tree;
return result;
},
xpathToMember: function (member, raw) {
if (!member) {
return '';
}
member = member.replace(/^\//, '').replace(/\//g, '.');
if (member.indexOf('@') >= 0) {
return member.replace(/\.?(@.*)/, raw ? '$1' : '["$1"]');
}
if (member.indexOf('text()') >= 0) {
return member.replace(/(\.?text\(\))/, raw ? '#text' : '["#text"]');
}
return member;
},
getter: function (member) {
return getter(this.xpathToMember(member), true);
}
});
$.extend(true, kendo.data, {
XmlDataReader: XmlDataReader,
readers: { xml: XmlDataReader }
});
}(window.kendo.jQuery));
return window.kendo;
}, typeof define == 'function' && define.amd ? define : function (a1, a2, a3) {
(a3 || a2)();
}));