@logicflow/extension 1.1.13 → 1.2.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/NodeResize/Control/Control.js +8 -8
- package/cjs/NodeResize/Control/Util.js +11 -11
- package/cjs/bpmn-adapter/index.js +124 -76
- package/cjs/bpmn-adapter/json2xml.js +5 -2
- package/cjs/bpmn-adapter/xml2json.js +38 -284
- package/cjs/components/context-menu/index.js +2 -2
- package/cjs/components/control/index.js +13 -13
- package/cjs/components/menu/index.js +32 -32
- package/cjs/components/mini-map/index.js +2 -2
- package/cjs/components/selection-select/index.js +5 -5
- package/cjs/index.js +1 -0
- package/cjs/insert-node-in-polyline/edge.js +4 -4
- package/cjs/insert-node-in-polyline/index.js +1 -1
- package/cjs/locale/en-locale/en.js +22 -0
- package/cjs/locale/en-locale/index.js +29 -0
- package/cjs/locale/locale.js +19 -0
- package/cjs/materials/group/GroupNode.js +2 -2
- package/cjs/materials/group/index.js +3 -3
- package/cjs/tools/auto-layout/index.js +9 -9
- package/cjs/turbo-adapter/index.js +2 -2
- package/es/NodeResize/Control/Control.d.ts +1 -1
- package/es/NodeResize/Control/Control.js +9 -9
- package/es/NodeResize/Control/Util.d.ts +3 -3
- package/es/NodeResize/Control/Util.js +7 -7
- package/es/bpmn-adapter/index.d.ts +41 -33
- package/es/bpmn-adapter/index.js +124 -76
- package/es/bpmn-adapter/json2xml.js +5 -2
- package/es/bpmn-adapter/xml2json.js +38 -284
- package/es/components/context-menu/index.js +2 -2
- package/es/components/control/index.d.ts +2 -3
- package/es/components/control/index.js +13 -13
- package/es/components/menu/index.d.ts +2 -3
- package/es/components/menu/index.js +32 -32
- package/es/components/mini-map/index.js +2 -2
- package/es/components/selection-select/index.d.ts +1 -1
- package/es/components/selection-select/index.js +5 -5
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/insert-node-in-polyline/edge.d.ts +3 -3
- package/es/insert-node-in-polyline/edge.js +3 -3
- package/es/insert-node-in-polyline/index.js +2 -2
- package/es/locale/en-locale/en.d.ts +19 -0
- package/es/locale/en-locale/en.js +19 -0
- package/es/locale/en-locale/index.d.ts +9 -0
- package/es/locale/en-locale/index.js +26 -0
- package/es/locale/locale.d.ts +6 -0
- package/es/locale/locale.js +16 -0
- package/es/materials/group/GroupNode.d.ts +1 -1
- package/es/materials/group/GroupNode.js +2 -2
- package/es/materials/group/index.d.ts +1 -1
- package/es/materials/group/index.js +3 -3
- package/es/tools/auto-layout/index.d.ts +2 -2
- package/es/tools/auto-layout/index.js +9 -9
- package/es/turbo-adapter/index.js +2 -2
- package/lib/AutoLayout.js +1 -1
- package/lib/BpmnAdapter.js +1 -1
- package/lib/BpmnElement.js +1 -1
- package/lib/ContextMenu.js +1 -1
- package/lib/Control.js +1 -1
- package/lib/CurvedEdge.js +1 -1
- package/lib/DndPanel.js +1 -1
- package/lib/EnLocale.js +1 -0
- package/lib/FlowPath.js +1 -1
- package/lib/Group.js +1 -1
- package/lib/InsertNodeInPolyline.js +1 -1
- package/lib/Menu.js +1 -1
- package/lib/MiniMap.js +1 -1
- package/lib/NodeResize.js +1 -1
- package/lib/RectLabelNode.js +1 -1
- package/lib/SelectionSelect.js +1 -1
- package/lib/Snapshot.js +1 -1
- package/lib/TurboAdapter.js +1 -1
- package/lib/lfJson2Xml.js +1 -1
- package/lib/lfXml2Json.js +1 -1
- package/package.json +2 -2
- package/types/NodeResize/Control/Control.d.ts +1 -1
- package/types/NodeResize/Control/Util.d.ts +3 -3
- package/types/bpmn-adapter/index.d.ts +41 -33
- package/types/components/control/index.d.ts +2 -3
- package/types/components/menu/index.d.ts +2 -3
- package/types/components/selection-select/index.d.ts +1 -1
- package/types/index.d.ts +1 -0
- package/types/insert-node-in-polyline/edge.d.ts +3 -3
- package/types/locale/en-locale/en.d.ts +19 -0
- package/types/locale/en-locale/index.d.ts +9 -0
- package/types/locale/locale.d.ts +6 -0
- package/types/materials/group/GroupNode.d.ts +1 -1
- package/types/materials/group/index.d.ts +1 -1
- package/types/tools/auto-layout/index.d.ts +2 -2
package/es/bpmn-adapter/index.js
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
extendStatics(d, b);
|
|
10
|
+
function __() { this.constructor = d; }
|
|
11
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
12
|
+
};
|
|
13
|
+
})();
|
|
14
|
+
var __assign = (this && this.__assign) || function () {
|
|
15
|
+
__assign = Object.assign || function(t) {
|
|
16
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
17
|
+
s = arguments[i];
|
|
18
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
19
|
+
t[p] = s[p];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
return __assign.apply(this, arguments);
|
|
24
|
+
};
|
|
1
25
|
var __read = (this && this.__read) || function (o, n) {
|
|
2
26
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
27
|
if (!m) return o;
|
|
@@ -29,8 +53,8 @@ var BpmnElements;
|
|
|
29
53
|
})(BpmnElements || (BpmnElements = {}));
|
|
30
54
|
var defaultAttrs = ['-name', '-id', 'bpmn:incoming', 'bpmn:outgoing', '-sourceRef', '-targetRef'];
|
|
31
55
|
/**
|
|
32
|
-
* 将普通json转换为
|
|
33
|
-
*
|
|
56
|
+
* 将普通json转换为xmlJson
|
|
57
|
+
* xmlJson中property会以“-”开头
|
|
34
58
|
* 如果没有“-”表示为子节点
|
|
35
59
|
*/
|
|
36
60
|
function toXmlJson(json) {
|
|
@@ -114,8 +138,6 @@ function convertLf2ProcessData(bpmnProcessData, data) {
|
|
|
114
138
|
var sequenceFlow = data.edges.map(function (edge) {
|
|
115
139
|
var _a, _b;
|
|
116
140
|
var targetNode = nodeMap.get(edge.targetNodeId);
|
|
117
|
-
// @see https://github.com/didi/LogicFlow/issues/325
|
|
118
|
-
// 需要保证incomming在outgoing之前
|
|
119
141
|
if (!targetNode['bpmn:incoming']) {
|
|
120
142
|
targetNode['bpmn:incoming'] = edge.id;
|
|
121
143
|
}
|
|
@@ -128,19 +150,17 @@ function convertLf2ProcessData(bpmnProcessData, data) {
|
|
|
128
150
|
edge.id,
|
|
129
151
|
];
|
|
130
152
|
}
|
|
131
|
-
|
|
132
|
-
if (!sourceNode['bpmn:outgoing']) {
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
];
|
|
143
|
-
}
|
|
153
|
+
// const sourceNode = nodeMap.get(edge.sourceNodeId);
|
|
154
|
+
// if (!sourceNode['bpmn:outgoing']) {
|
|
155
|
+
// sourceNode['bpmn:outgoing'] = edge.id;
|
|
156
|
+
// } else if (Array.isArray(sourceNode['bpmn:outgoing'])) {
|
|
157
|
+
// sourceNode['bpmn:outgoing'].push(edge.id);
|
|
158
|
+
// } else { // 字符串转数组
|
|
159
|
+
// sourceNode['bpmn:outgoing'] = [
|
|
160
|
+
// sourceNode['bpmn:outgoing'],
|
|
161
|
+
// edge.id,
|
|
162
|
+
// ];
|
|
163
|
+
// }
|
|
144
164
|
var edgeConfig = {
|
|
145
165
|
'-id': edge.id,
|
|
146
166
|
'-sourceRef': edge.sourceNodeId,
|
|
@@ -155,6 +175,23 @@ function convertLf2ProcessData(bpmnProcessData, data) {
|
|
|
155
175
|
}
|
|
156
176
|
return edgeConfig;
|
|
157
177
|
});
|
|
178
|
+
// @see https://github.com/didi/LogicFlow/issues/325
|
|
179
|
+
// 需要保证incomming在outgoing之前
|
|
180
|
+
data.edges.forEach(function (edge) {
|
|
181
|
+
var sourceNode = nodeMap.get(edge.sourceNodeId);
|
|
182
|
+
if (!sourceNode['bpmn:outgoing']) {
|
|
183
|
+
sourceNode['bpmn:outgoing'] = edge.id;
|
|
184
|
+
}
|
|
185
|
+
else if (Array.isArray(sourceNode['bpmn:outgoing'])) {
|
|
186
|
+
sourceNode['bpmn:outgoing'].push(edge.id);
|
|
187
|
+
}
|
|
188
|
+
else { // 字符串转数组
|
|
189
|
+
sourceNode['bpmn:outgoing'] = [
|
|
190
|
+
sourceNode['bpmn:outgoing'],
|
|
191
|
+
edge.id,
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
});
|
|
158
195
|
bpmnProcessData[BpmnElements.FLOW] = sequenceFlow;
|
|
159
196
|
}
|
|
160
197
|
/**
|
|
@@ -399,53 +436,59 @@ function getEdgeConfig(edgeValue, processValue) {
|
|
|
399
436
|
}
|
|
400
437
|
return edge;
|
|
401
438
|
}
|
|
402
|
-
var BpmnAdapter = {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
lf
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
'
|
|
439
|
+
var BpmnAdapter = /** @class */ (function () {
|
|
440
|
+
function BpmnAdapter(_a) {
|
|
441
|
+
var _this = this;
|
|
442
|
+
var lf = _a.lf;
|
|
443
|
+
this.adapterOut = function (data) {
|
|
444
|
+
var bpmnProcessData = __assign({}, _this.processAttributes);
|
|
445
|
+
convertLf2ProcessData(bpmnProcessData, data);
|
|
446
|
+
var bpmnDiagramData = {
|
|
447
|
+
'-id': 'BPMNPlane_1',
|
|
448
|
+
'-bpmnElement': bpmnProcessData['-id'],
|
|
449
|
+
};
|
|
450
|
+
convertLf2DiagramData(bpmnDiagramData, data);
|
|
451
|
+
var definitions = _this.definitionAttributes;
|
|
452
|
+
definitions['bpmn:process'] = bpmnProcessData;
|
|
453
|
+
definitions['bpmndi:BPMNDiagram'] = {
|
|
454
|
+
'-id': 'BPMNDiagram_1',
|
|
455
|
+
'bpmndi:BPMNPlane': bpmnDiagramData,
|
|
456
|
+
};
|
|
457
|
+
var bpmnData = {
|
|
458
|
+
'bpmn:definitions': definitions,
|
|
459
|
+
};
|
|
460
|
+
return bpmnData;
|
|
461
|
+
};
|
|
462
|
+
this.adapterIn = function (bpmnData) {
|
|
463
|
+
if (bpmnData) {
|
|
464
|
+
return convertBpmn2LfData(bpmnData);
|
|
465
|
+
}
|
|
416
466
|
};
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
'-
|
|
467
|
+
lf.adapterIn = function (data) { return _this.adapterIn(data); };
|
|
468
|
+
lf.adapterOut = function (data) { return _this.adapterOut(data); };
|
|
469
|
+
this.processAttributes = {
|
|
470
|
+
'-isExecutable': 'true',
|
|
471
|
+
'-id': "Process_" + getBpmnId(),
|
|
421
472
|
};
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
'
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
'-exporter': 'bpmn-js (https://demo.bpmn.io)',
|
|
433
|
-
'-exporterVersion': '7.3.0',
|
|
434
|
-
'bpmn:process': bpmnProcessData,
|
|
435
|
-
'bpmndi:BPMNDiagram': {
|
|
436
|
-
'-id': 'BPMNDiagram_1',
|
|
437
|
-
'bpmndi:BPMNPlane': bpmnDiagramData,
|
|
438
|
-
},
|
|
439
|
-
},
|
|
473
|
+
this.definitionAttributes = {
|
|
474
|
+
'-id': "Definitions_" + getBpmnId(),
|
|
475
|
+
'-xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
|
|
476
|
+
'-xmlns:bpmn': 'http://www.omg.org/spec/BPMN/20100524/MODEL',
|
|
477
|
+
'-xmlns:bpmndi': 'http://www.omg.org/spec/BPMN/20100524/DI',
|
|
478
|
+
'-xmlns:dc': 'http://www.omg.org/spec/DD/20100524/DC',
|
|
479
|
+
'-xmlns:di': 'http://www.omg.org/spec/DD/20100524/DI',
|
|
480
|
+
'-targetNamespace': 'http://logic-flow.org',
|
|
481
|
+
'-exporter': 'logicflow',
|
|
482
|
+
'-exporterVersion': '1.2.0',
|
|
440
483
|
};
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
};
|
|
484
|
+
}
|
|
485
|
+
BpmnAdapter.prototype.setCustomShape = function (key, val) {
|
|
486
|
+
BpmnAdapter.shapeConfigMap.set(key, val);
|
|
487
|
+
};
|
|
488
|
+
BpmnAdapter.pluginName = 'bpmn-adapter';
|
|
489
|
+
BpmnAdapter.shapeConfigMap = new Map();
|
|
490
|
+
return BpmnAdapter;
|
|
491
|
+
}());
|
|
449
492
|
BpmnAdapter.shapeConfigMap.set(BpmnElements.START, {
|
|
450
493
|
width: StartEventConfig.width,
|
|
451
494
|
height: StartEventConfig.height,
|
|
@@ -466,20 +509,25 @@ BpmnAdapter.shapeConfigMap.set(BpmnElements.USER, {
|
|
|
466
509
|
width: UserTaskConfig.width,
|
|
467
510
|
height: UserTaskConfig.height,
|
|
468
511
|
});
|
|
469
|
-
var BpmnXmlAdapter = {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
512
|
+
var BpmnXmlAdapter = /** @class */ (function (_super) {
|
|
513
|
+
__extends(BpmnXmlAdapter, _super);
|
|
514
|
+
function BpmnXmlAdapter(data) {
|
|
515
|
+
var _this = _super.call(this, data) || this;
|
|
516
|
+
_this.adapterXmlIn = function (bpmnData) {
|
|
517
|
+
var json = lfXml2Json(bpmnData);
|
|
518
|
+
return _this.adapterIn(json);
|
|
519
|
+
};
|
|
520
|
+
_this.adapterXmlOut = function (data) {
|
|
521
|
+
var outData = _this.adapterOut(data);
|
|
522
|
+
return lfJson2Xml(outData);
|
|
523
|
+
};
|
|
524
|
+
var lf = data.lf;
|
|
525
|
+
lf.adapterIn = _this.adapterXmlIn;
|
|
526
|
+
lf.adapterOut = _this.adapterXmlOut;
|
|
527
|
+
return _this;
|
|
528
|
+
}
|
|
529
|
+
BpmnXmlAdapter.pluginName = 'bpmnXmlAdapter';
|
|
530
|
+
return BpmnXmlAdapter;
|
|
531
|
+
}(BpmnAdapter));
|
|
484
532
|
export { BpmnAdapter, BpmnXmlAdapter, };
|
|
485
533
|
export default BpmnAdapter;
|
|
@@ -22,10 +22,13 @@ function toXml(v, name, ind, deep) {
|
|
|
22
22
|
var hasChild = false;
|
|
23
23
|
xml += addIndSpace(ind, deep) + "<" + name;
|
|
24
24
|
for (var m in v) {
|
|
25
|
-
if (m.charAt(0) == "-")
|
|
25
|
+
if (m.charAt(0) == "-") {
|
|
26
|
+
console.log(m, v[m]);
|
|
26
27
|
xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
|
|
27
|
-
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
28
30
|
hasChild = true;
|
|
31
|
+
}
|
|
29
32
|
}
|
|
30
33
|
xml += hasChild ? ">" : " />";
|
|
31
34
|
if (hasChild) {
|
|
@@ -35,26 +35,26 @@ XML.ObjTree.prototype.parseXML = function (xml) {
|
|
|
35
35
|
};
|
|
36
36
|
// method: parseHTTP( url, options, callback )
|
|
37
37
|
XML.ObjTree.prototype.parseHTTP = function (url, options, callback) {
|
|
38
|
-
var
|
|
38
|
+
var myOpt = {};
|
|
39
39
|
for (var key in options) {
|
|
40
|
-
|
|
40
|
+
myOpt[key] = options[key]; // copy object
|
|
41
41
|
}
|
|
42
|
-
if (!
|
|
43
|
-
if (typeof (
|
|
44
|
-
typeof (
|
|
45
|
-
typeof (
|
|
46
|
-
|
|
42
|
+
if (!myOpt.method) {
|
|
43
|
+
if (typeof (myOpt.postBody) == "undefined" &&
|
|
44
|
+
typeof (myOpt.postbody) == "undefined" &&
|
|
45
|
+
typeof (myOpt.parameters) == "undefined") {
|
|
46
|
+
myOpt.method = "get";
|
|
47
47
|
}
|
|
48
48
|
else {
|
|
49
|
-
|
|
49
|
+
myOpt.method = "post";
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
if (callback) {
|
|
53
|
-
|
|
53
|
+
myOpt.asynchronous = true; // async-mode
|
|
54
54
|
var __this = this;
|
|
55
55
|
var __func = callback;
|
|
56
|
-
var __save =
|
|
57
|
-
|
|
56
|
+
var __save = myOpt.onComplete;
|
|
57
|
+
myOpt.onComplete = function (trans) {
|
|
58
58
|
var tree;
|
|
59
59
|
if (trans && trans.responseXML && trans.responseXML.documentElement) {
|
|
60
60
|
tree = __this.parseDOM(trans.responseXML.documentElement);
|
|
@@ -65,17 +65,17 @@ XML.ObjTree.prototype.parseHTTP = function (url, options, callback) {
|
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
else {
|
|
68
|
-
|
|
68
|
+
myOpt.asynchronous = false;
|
|
69
69
|
}
|
|
70
70
|
var trans;
|
|
71
71
|
if (typeof (HTTP) != "undefined" && HTTP.Request) {
|
|
72
|
-
|
|
73
|
-
var req = new HTTP.Request(
|
|
72
|
+
myOpt.uri = url;
|
|
73
|
+
var req = new HTTP.Request(myOpt);
|
|
74
74
|
if (req)
|
|
75
75
|
trans = req.transport;
|
|
76
76
|
}
|
|
77
77
|
else if (typeof (Ajax) != "undefined" && Ajax.Request) {
|
|
78
|
-
var req = new Ajax.Request(url,
|
|
78
|
+
var req = new Ajax.Request(url, myOpt);
|
|
79
79
|
if (req)
|
|
80
80
|
trans = req.transport;
|
|
81
81
|
}
|
|
@@ -85,7 +85,7 @@ XML.ObjTree.prototype.parseHTTP = function (url, options, callback) {
|
|
|
85
85
|
return this.parseDOM(trans.responseXML.documentElement);
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
|
-
// method: parseDOM(
|
|
88
|
+
// method: parseDOM( document root )
|
|
89
89
|
XML.ObjTree.prototype.parseDOM = function (root) {
|
|
90
90
|
if (!root)
|
|
91
91
|
return;
|
|
@@ -119,11 +119,11 @@ XML.ObjTree.prototype.parseElement = function (elem) {
|
|
|
119
119
|
return; // ignore white spaces
|
|
120
120
|
return elem.nodeValue;
|
|
121
121
|
}
|
|
122
|
-
var
|
|
122
|
+
var retVal;
|
|
123
123
|
var cnt = {};
|
|
124
124
|
// parse attributes
|
|
125
125
|
if (elem.attributes && elem.attributes.length) {
|
|
126
|
-
|
|
126
|
+
retVal = {};
|
|
127
127
|
for (var i = 0; i < elem.attributes.length; i++) {
|
|
128
128
|
var key = elem.attributes[i].nodeName;
|
|
129
129
|
if (typeof (key) != "string")
|
|
@@ -135,30 +135,30 @@ XML.ObjTree.prototype.parseElement = function (elem) {
|
|
|
135
135
|
if (typeof (cnt[key]) == "undefined")
|
|
136
136
|
cnt[key] = 0;
|
|
137
137
|
cnt[key]++;
|
|
138
|
-
this.addNode(
|
|
138
|
+
this.addNode(retVal, key, cnt[key], val);
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
// parse child nodes (recursive)
|
|
142
142
|
if (elem.childNodes && elem.childNodes.length) {
|
|
143
|
-
var
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
for (var i = 0; i < elem.childNodes.length &&
|
|
147
|
-
var
|
|
148
|
-
if (
|
|
143
|
+
var textOnly = true;
|
|
144
|
+
if (retVal)
|
|
145
|
+
textOnly = false; // some attributes exists
|
|
146
|
+
for (var i = 0; i < elem.childNodes.length && textOnly; i++) {
|
|
147
|
+
var nType = elem.childNodes[i].nodeType;
|
|
148
|
+
if (nType == 3 || nType == 4)
|
|
149
149
|
continue;
|
|
150
|
-
|
|
150
|
+
textOnly = false;
|
|
151
151
|
}
|
|
152
|
-
if (
|
|
153
|
-
if (!
|
|
154
|
-
|
|
152
|
+
if (textOnly) {
|
|
153
|
+
if (!retVal)
|
|
154
|
+
retVal = "";
|
|
155
155
|
for (var i = 0; i < elem.childNodes.length; i++) {
|
|
156
|
-
|
|
156
|
+
retVal += elem.childNodes[i].nodeValue;
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
else {
|
|
160
|
-
if (!
|
|
161
|
-
|
|
160
|
+
if (!retVal)
|
|
161
|
+
retVal = {};
|
|
162
162
|
for (var i = 0; i < elem.childNodes.length; i++) {
|
|
163
163
|
var key = elem.childNodes[i].nodeName;
|
|
164
164
|
if (typeof (key) != "string")
|
|
@@ -169,23 +169,23 @@ XML.ObjTree.prototype.parseElement = function (elem) {
|
|
|
169
169
|
if (typeof (cnt[key]) == "undefined")
|
|
170
170
|
cnt[key] = 0;
|
|
171
171
|
cnt[key]++;
|
|
172
|
-
this.addNode(
|
|
172
|
+
this.addNode(retVal, key, cnt[key], val);
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
|
-
return
|
|
176
|
+
return retVal;
|
|
177
177
|
};
|
|
178
178
|
// method: addNode( hash, key, count, value )
|
|
179
|
-
XML.ObjTree.prototype.addNode = function (hash, key,
|
|
179
|
+
XML.ObjTree.prototype.addNode = function (hash, key, counts, val) {
|
|
180
180
|
if (this.__force_array[key]) {
|
|
181
|
-
if (
|
|
181
|
+
if (counts == 1)
|
|
182
182
|
hash[key] = [];
|
|
183
183
|
hash[key][hash[key].length] = val; // push
|
|
184
184
|
}
|
|
185
|
-
else if (
|
|
185
|
+
else if (counts == 1) { // 1st sibling
|
|
186
186
|
hash[key] = val;
|
|
187
187
|
}
|
|
188
|
-
else if (
|
|
188
|
+
else if (counts == 2) { // 2nd sibling
|
|
189
189
|
hash[key] = [hash[key], val];
|
|
190
190
|
}
|
|
191
191
|
else { // 3rd sibling and more
|
|
@@ -274,252 +274,6 @@ XML.ObjTree.prototype.scalar_to_xml = function (name, text) {
|
|
|
274
274
|
XML.ObjTree.prototype.xml_escape = function (text) {
|
|
275
275
|
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
276
276
|
};
|
|
277
|
-
/*
|
|
278
|
-
// ========================================================================
|
|
279
|
-
|
|
280
|
-
=head1 NAME
|
|
281
|
-
|
|
282
|
-
XML.ObjTree -- XML source code from/to JavaScript object like E4X
|
|
283
|
-
|
|
284
|
-
=head1 SYNOPSIS
|
|
285
|
-
|
|
286
|
-
var xotree = new XML.ObjTree();
|
|
287
|
-
var tree1 = {
|
|
288
|
-
root: {
|
|
289
|
-
node: "Hello, World!"
|
|
290
|
-
}
|
|
291
|
-
};
|
|
292
|
-
var xml1 = xotree.writeXML( tree1 ); // object tree to XML source
|
|
293
|
-
alert( "xml1: "+xml1 );
|
|
294
|
-
|
|
295
|
-
var xml2 = '<?xml version="1.0"?><response><error>0</error></response>';
|
|
296
|
-
var tree2 = xotree.parseXML( xml2 ); // XML source to object tree
|
|
297
|
-
alert( "error: "+tree2.response.error );
|
|
298
|
-
|
|
299
|
-
=head1 DESCRIPTION
|
|
300
|
-
|
|
301
|
-
XML.ObjTree class is a parser/generater between XML source code
|
|
302
|
-
and JavaScript object like E4X, ECMAScript for XML.
|
|
303
|
-
This is a JavaScript version of the XML::TreePP module for Perl.
|
|
304
|
-
This also works as a wrapper for XMLHTTPRequest and successor to JKL.ParseXML class
|
|
305
|
-
when this is used with prototype.js or JSAN's HTTP.Request class.
|
|
306
|
-
|
|
307
|
-
=head2 JavaScript object tree format
|
|
308
|
-
|
|
309
|
-
A sample XML source:
|
|
310
|
-
|
|
311
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
312
|
-
<family name="Kawasaki">
|
|
313
|
-
<father>Yasuhisa</father>
|
|
314
|
-
<mother>Chizuko</mother>
|
|
315
|
-
<children>
|
|
316
|
-
<girl>Shiori</girl>
|
|
317
|
-
<boy>Yusuke</boy>
|
|
318
|
-
<boy>Kairi</boy>
|
|
319
|
-
</children>
|
|
320
|
-
</family>
|
|
321
|
-
|
|
322
|
-
Its JavaScript object tree like JSON/E4X:
|
|
323
|
-
|
|
324
|
-
{
|
|
325
|
-
'family': {
|
|
326
|
-
'-name': 'Kawasaki',
|
|
327
|
-
'father': 'Yasuhisa',
|
|
328
|
-
'mother': 'Chizuko',
|
|
329
|
-
'children': {
|
|
330
|
-
'girl': 'Shiori'
|
|
331
|
-
'boy': [
|
|
332
|
-
'Yusuke',
|
|
333
|
-
'Kairi'
|
|
334
|
-
]
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
Each elements are parsed into objects:
|
|
340
|
-
|
|
341
|
-
tree.family.father; # the father's given name.
|
|
342
|
-
|
|
343
|
-
Prefix '-' is inserted before every attributes' name.
|
|
344
|
-
|
|
345
|
-
tree.family["-name"]; # this family's family name
|
|
346
|
-
|
|
347
|
-
A array is used because this family has two boys.
|
|
348
|
-
|
|
349
|
-
tree.family.children.boy[0]; # first boy's name
|
|
350
|
-
tree.family.children.boy[1]; # second boy's name
|
|
351
|
-
tree.family.children.girl; # (girl has no other sisiters)
|
|
352
|
-
|
|
353
|
-
=head1 METHODS
|
|
354
|
-
|
|
355
|
-
=head2 xotree = new XML.ObjTree()
|
|
356
|
-
|
|
357
|
-
This constructor method returns a new XML.ObjTree object.
|
|
358
|
-
|
|
359
|
-
=head2 xotree.force_array = [ "rdf:li", "item", "-xmlns" ];
|
|
360
|
-
|
|
361
|
-
This property allows you to specify a list of element names
|
|
362
|
-
which should always be forced into an array representation.
|
|
363
|
-
The default value is null, it means that context of the elements
|
|
364
|
-
will determine to make array or to keep it scalar.
|
|
365
|
-
|
|
366
|
-
=head2 xotree.attr_prefix = '@';
|
|
367
|
-
|
|
368
|
-
This property allows you to specify a prefix character which is
|
|
369
|
-
inserted before each attribute names.
|
|
370
|
-
Instead of default prefix '-', E4X-style prefix '@' is also available.
|
|
371
|
-
The default character is '-'.
|
|
372
|
-
Or set '@' to access attribute values like E4X, ECMAScript for XML.
|
|
373
|
-
The length of attr_prefix must be just one character and not be empty.
|
|
374
|
-
|
|
375
|
-
=head2 tree = xotree.parseXML( xmlsrc );
|
|
376
|
-
|
|
377
|
-
This method loads an XML document using the supplied string
|
|
378
|
-
and returns its JavaScript object converted.
|
|
379
|
-
|
|
380
|
-
=head2 tree = xotree.parseDOM( domnode );
|
|
381
|
-
|
|
382
|
-
This method parses a DOM tree (ex. responseXML.documentElement)
|
|
383
|
-
and returns its JavaScript object converted.
|
|
384
|
-
|
|
385
|
-
=head2 tree = xotree.parseHTTP( url, options );
|
|
386
|
-
|
|
387
|
-
This method loads a XML file from remote web server
|
|
388
|
-
and returns its JavaScript object converted.
|
|
389
|
-
XMLHTTPRequest's synchronous mode is always used.
|
|
390
|
-
This mode blocks the process until the response is completed.
|
|
391
|
-
|
|
392
|
-
First argument is a XML file's URL
|
|
393
|
-
which must exist in the same domain as parent HTML file's.
|
|
394
|
-
Cross-domain loading is not available for security reasons.
|
|
395
|
-
|
|
396
|
-
Second argument is options' object which can contains some parameters:
|
|
397
|
-
method, postBody, parameters, onLoading, etc.
|
|
398
|
-
|
|
399
|
-
This method requires JSAN's L<HTTP.Request> class or prototype.js's Ajax.Request class.
|
|
400
|
-
|
|
401
|
-
=head2 xotree.parseHTTP( url, options, callback );
|
|
402
|
-
|
|
403
|
-
If a callback function is set as third argument,
|
|
404
|
-
XMLHTTPRequest's asynchronous mode is used.
|
|
405
|
-
|
|
406
|
-
This mode calls a callback function with XML file's JavaScript object converted
|
|
407
|
-
after the response is completed.
|
|
408
|
-
|
|
409
|
-
=head2 xmlsrc = xotree.writeXML( tree );
|
|
410
|
-
|
|
411
|
-
This method parses a JavaScript object tree
|
|
412
|
-
and returns its XML source generated.
|
|
413
|
-
|
|
414
|
-
=head1 EXAMPLES
|
|
415
|
-
|
|
416
|
-
=head2 Text node and attributes
|
|
417
|
-
|
|
418
|
-
If a element has both of a text node and attributes
|
|
419
|
-
or both of a text node and other child nodes,
|
|
420
|
-
text node's value is moved to a special node named "#text".
|
|
421
|
-
|
|
422
|
-
var xotree = new XML.ObjTree();
|
|
423
|
-
var xmlsrc = '<span class="author">Kawasaki Yusuke</span>';
|
|
424
|
-
var tree = xotree.parseXML( xmlsrc );
|
|
425
|
-
var class = tree.span["-class"]; # attribute
|
|
426
|
-
var name = tree.span["#text"]; # text node
|
|
427
|
-
|
|
428
|
-
=head2 parseHTTP() method with HTTP-GET and sync-mode
|
|
429
|
-
|
|
430
|
-
HTTP/Request.js or prototype.js must be loaded before calling this method.
|
|
431
|
-
|
|
432
|
-
var xotree = new XML.ObjTree();
|
|
433
|
-
var url = "http://example.com/index.html";
|
|
434
|
-
var tree = xotree.parseHTTP( url );
|
|
435
|
-
xotree.attr_prefix = '@'; // E4X-style
|
|
436
|
-
alert( tree.html["@lang"] );
|
|
437
|
-
|
|
438
|
-
This code shows C<lang=""> attribute from a X-HTML source code.
|
|
439
|
-
|
|
440
|
-
=head2 parseHTTP() method with HTTP-POST and async-mode
|
|
441
|
-
|
|
442
|
-
Third argument is a callback function which is called on onComplete.
|
|
443
|
-
|
|
444
|
-
var xotree = new XML.ObjTree();
|
|
445
|
-
var url = "http://example.com/mt-tb.cgi";
|
|
446
|
-
var opts = {
|
|
447
|
-
postBody: "title=...&excerpt=...&url=...&blog_name=..."
|
|
448
|
-
};
|
|
449
|
-
var func = function ( tree ) {
|
|
450
|
-
alert( tree.response.error );
|
|
451
|
-
};
|
|
452
|
-
xotree.parseHTTP( url, opts, func );
|
|
453
|
-
|
|
454
|
-
This code send a trackback ping and shows its response code.
|
|
455
|
-
|
|
456
|
-
=head2 Simple RSS reader
|
|
457
|
-
|
|
458
|
-
This is a RSS reader which loads RDF file and displays all items.
|
|
459
|
-
|
|
460
|
-
var xotree = new XML.ObjTree();
|
|
461
|
-
xotree.force_array = [ "rdf:li", "item" ];
|
|
462
|
-
var url = "http://example.com/news-rdf.xml";
|
|
463
|
-
var func = function( tree ) {
|
|
464
|
-
var elem = document.getElementById("rss_here");
|
|
465
|
-
for( var i=0; i<tree["rdf:RDF"].item.length; i++ ) {
|
|
466
|
-
var divtag = document.createElement( "div" );
|
|
467
|
-
var atag = document.createElement( "a" );
|
|
468
|
-
atag.href = tree["rdf:RDF"].item[i].link;
|
|
469
|
-
var title = tree["rdf:RDF"].item[i].title;
|
|
470
|
-
var tnode = document.createTextNode( title );
|
|
471
|
-
atag.appendChild( tnode );
|
|
472
|
-
divtag.appendChild( atag );
|
|
473
|
-
elem.appendChild( divtag );
|
|
474
|
-
}
|
|
475
|
-
};
|
|
476
|
-
xotree.parseHTTP( url, {}, func );
|
|
477
|
-
|
|
478
|
-
=head2 XML-RPC using writeXML, prototype.js and parseDOM
|
|
479
|
-
|
|
480
|
-
If you wish to use prototype.js's Ajax.Request class by yourself:
|
|
481
|
-
|
|
482
|
-
var xotree = new XML.ObjTree();
|
|
483
|
-
var reqtree = {
|
|
484
|
-
methodCall: {
|
|
485
|
-
methodName: "weblogUpdates.ping",
|
|
486
|
-
params: {
|
|
487
|
-
param: [
|
|
488
|
-
{ value: "Kawa.net xp top page" }, // 1st param
|
|
489
|
-
{ value: "http://www.kawa.net/" } // 2nd param
|
|
490
|
-
]
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
var reqxml = xotree.writeXML( reqtree ); // JS-Object to XML code
|
|
495
|
-
var url = "http://example.com/xmlrpc";
|
|
496
|
-
var func = function( req ) {
|
|
497
|
-
var resdom = req.responseXML.documentElement;
|
|
498
|
-
xotree.force_array = [ "member" ];
|
|
499
|
-
var restree = xotree.parseDOM( resdom ); // XML-DOM to JS-Object
|
|
500
|
-
alert( restree.methodResponse.params.param.value.struct.member[0].value.string );
|
|
501
|
-
};
|
|
502
|
-
var opt = {
|
|
503
|
-
method: "post",
|
|
504
|
-
postBody: reqxml,
|
|
505
|
-
asynchronous: true,
|
|
506
|
-
onComplete: func
|
|
507
|
-
};
|
|
508
|
-
new Ajax.Request( url, opt );
|
|
509
|
-
|
|
510
|
-
=head1 AUTHOR
|
|
511
|
-
|
|
512
|
-
Yusuke Kawasaki http://www.kawa.net/
|
|
513
|
-
=head1 COPYRIGHT AND LICENSE
|
|
514
|
-
|
|
515
|
-
Copyright (c) 2005-2006 Yusuke Kawasaki. All rights reserved.
|
|
516
|
-
This program is free software; you can redistribute it and/or
|
|
517
|
-
modify it under the Artistic license. Or whatever license I choose,
|
|
518
|
-
which I will do instead of keeping this documentation like it is.
|
|
519
|
-
|
|
520
|
-
=cut
|
|
521
|
-
// ========================================================================
|
|
522
|
-
*/
|
|
523
277
|
var lfXml2Json = function (xmlData) {
|
|
524
278
|
return new XML.ObjTree().parseXML(xmlData);
|
|
525
279
|
};
|