bfd7cf0381843c691749e5691ae3419a5ff8855d
[PlinnDocument.git] / skins / xml_io.js
1 // (c) BenoƮt PIN 2006-2007
2 // http://plinn.org
3 // Licence GPL
4
5 function XMLExport() {
6 this.domDoc = Sarissa.getDomDocument('http://plinn.org/namespaces/plinn_document/1.0', 'plinn');
7 this.rootNode = this.domDoc.documentElement;
8 }
9
10 XMLExport.prototype.getXML = function() {
11 this.exportDocument();
12 var s = new XMLSerializer();
13 XML_OUTPUT.value = s.serializeToString(this.domDoc);
14 };
15
16
17 XMLExport.prototype.exportDocument = function() {
18 this.exportRectangles(LAYER_MANAGER.space, this.rootNode);
19 };
20
21 XMLExport.prototype.exportRectangles = function(baseObj, baseNode) {
22 var doc = this.domDoc;
23 var childs = baseObj.childNodes;
24
25 for(var i = 0 ; i < childs.length ; i++) {
26 rectObj = childs[i].rectangle;
27 if (!rectObj)
28 continue;
29
30 // rectangle
31 var rectEl = doc.createElement("rectangle");
32 rectEl.setAttribute("width", rectObj.width);
33 rectEl.setAttribute("height", rectObj.height);
34 rectEl.setAttribute("elementKey", rectObj.elementKey);
35 rectEl.setAttribute("ddOptions", rectObj.ddOptions);
36 rectEl.setAttribute("ratio", rectObj.ratio);
37 rectEl.setAttribute("visibility", rectObj.style.visibility);
38
39 // upperLeftCorner
40 var ulc = doc.createElement("upperLeftCorner");
41 var point = doc.createElement("point");
42 point.setAttribute("x", rectObj.upperLeftCorner.x);
43 point.setAttribute("y", rectObj.upperLeftCorner.y);
44 ulc.appendChild(point);
45 rectEl.appendChild(ulc);
46
47 // raw data
48 var rdata = doc.createElement("rawData");
49 if (rectObj.getRawData) {
50 var rawEl = doc.createTextNode(rectObj.getRawData());
51 rdata.appendChild(rawEl);
52 }
53 rectEl.appendChild(rdata);
54
55 baseNode.appendChild(rectEl);
56
57 this.exportRectangles(rectObj.node, rectEl);
58 }
59 };
60
61 function XMLImport(url, root_container) {
62 this.root_container = root_container;
63 var thisImporter = this;
64 var req = new XMLHttpRequest();
65
66 req.onreadystatechange = function() {
67 if(req.readyState == 4)
68 thisImporter.constructDocument(req);
69 }
70 req.open("GET", url, true);
71 req.send(null);
72 }
73
74 XMLImport.prototype.constructDocument = function(req) {
75 var rootNode = req.responseXML.documentElement;
76 var layerElements = rootNode.childNodes;
77 initLayerManager(this.root_container, true);
78 var layerElement;
79 for (var i = 0 ; i < layerElements.length ; i++) {
80 layerElement = layerElements[i];
81 if (i==0) { // initialize LAYER_MANAGER from first layer data
82 LAYER_MANAGER.defaultLayerWidth = parseInt(layerElement.getAttribute("width"));
83 LAYER_MANAGER.defaultLayerHeight = parseInt(layerElement.getAttribute("height"));
84 LAYER_MANAGER.addLayer("relative");
85 }
86 else
87 LAYER_MANAGER.addLayer();
88
89 // common part
90 if (layerElement.getAttribute("visibility") == "hidden")
91 LAYER_MANAGER.toggleLayerVisibility();
92
93 this.constructRectangles(CURRENT_LAYER, layerElement)
94 }
95 };
96
97 XMLImport.prototype.constructRectangles = function(baseObj, baseNode) {
98 var rectangleElements = baseNode.childNodes;
99 var rectE, rect, ulcE, ulc, rawDataE, rawData, putFunc;
100
101 for (var i = 0 ; i < rectangleElements.length ; i ++) {
102 rectE = rectangleElements[i];
103 if (rectE.nodeName != "rectangle")
104 continue;
105 ulcE = rectE.childNodes[0].childNodes[0];
106 rawDataE = rectE.childNodes[1]
107
108 ulc = new Point( parseInt(ulcE.getAttribute("x")), parseInt(ulcE.getAttribute("y")) )
109 rect = new Rectangle(ulc,
110 parseInt(rectE.getAttribute("width")),
111 parseInt(rectE.getAttribute("height")),
112 rectE.getAttribute("elementKey"),
113 parseInt(rectE.getAttribute("ddOptions")),
114 parseFloat(rectE.getAttribute("ratio")));
115
116 putFunc = ELEMENTS_POOL[rectE.getAttribute("elementKey")]["putData"]
117 if (putFunc)
118 putFunc.apply(rect, [rawDataE.childNodes[0].nodeValue]);
119
120 rect.draw(baseObj);
121 }
122 };
123
124 /* utils */
125 function _plinnDocumentBeforeSubmit() {
126 with (GLOBAL_DD_CONTROLER.ddEventCaptureElmt) {
127 onmousedown=null;
128 onmousemouse=null;
129 onmouseup=null;
130 }
131 new XMLExport().getXML();
132 }