documentCanvas fix: node now dims correctly
[fnpeditor.git] / modules / documentCanvas / transformations.js
1 define(['libs/jquery-1.9.1.min'], function($) {\r
2 \r
3     var transformations = {};\r
4 \r
5     transformations.fromXML = {\r
6         getHTMLTree: function(xml) {\r
7             var inner = $(xml).clone();\r
8             var toret = $('<div></div>');\r
9             toret.append(inner);\r
10             \r
11             var toBlock = ['div', 'section', 'header'];\r
12             var toInline = ['aside', 'span'];\r
13             \r
14             var transform = function(tags, replacingTagName) {\r
15                 tags.forEach(function(tagName) {\r
16                     tagName = tagName.toLowerCase();\r
17                     console.log('running ' + tagName);\r
18                     toret.find(tagName).replaceWith(function() {\r
19                         var currentTag = $(this);\r
20                         if(currentTag.attr('wlxml-tag'))\r
21                             return;\r
22                         var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);\r
23                         toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));\r
24                         for(var i = 0; i < this.attributes.length; i++) {\r
25                             var attr = this.attributes.item(i);\r
26                             var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;\r
27                             toret.attr('wlxml-' + attr.name, value)\r
28                         }\r
29                         toret.append(currentTag.contents());\r
30                         return toret;\r
31                     });\r
32                 });\r
33             }\r
34             \r
35             transform(toBlock, 'div');\r
36             transform(toInline, 'span');\r
37 \r
38             return toret.children();\r
39         },\r
40         getMetaData: function(xml) {\r
41             var toret = {};\r
42             $(xml).find('metadata').children().each(function() {\r
43                 var node = $(this);\r
44                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();\r
45             })\r
46             return toret;\r
47         },\r
48         getDocumentDescription: function(xml) {\r
49             return {\r
50                 HTMLTree: this.getHTMLTree(xml),\r
51                 metadata: this.getMetaData(xml)\r
52             }\r
53         }\r
54     }\r
55 \r
56     transformations.toXML = {\r
57         getXML: function(documentDescription) {\r
58             \r
59             var inner = $(documentDescription.HTMLTree);\r
60             var toret = $('<div></div>');\r
61             toret.append(inner);\r
62             \r
63             toret.find('div, span').replaceWith(function() {\r
64                 var div = $(this);\r
65                 var tagName = div.attr('wlxml-tag');\r
66                 var toret = $('<'+tagName+'>');\r
67                                    \r
68                 for(var i = 0; i < this.attributes.length; i++) {\r
69                     var attr = this.attributes.item(i);\r
70                     var split = attr.name.split('-')\r
71                     console.log(split);\r
72                     if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag')) \r
73                         continue;\r
74                     var wlxmlName = split.splice(1).join('-');\r
75                     var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;\r
76                     console.log(name + ': ' + value);\r
77                     toret.attr(wlxmlName, value);\r
78                 }\r
79                     \r
80                 toret.append(div.contents());\r
81                 return toret;\r
82             });\r
83             \r
84             var meta = $('<metadata></metadata>\n');\r
85             _.each(_.keys(documentDescription.metadata), function(key) {\r
86                 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');\r
87             });\r
88             meta.append('\n');\r
89             \r
90             var metadata = toret.find('metadata');\r
91             if(metadata.length === 0) {\r
92                 var section = toret.find('section');\r
93                 section = section.length ? $(section[0]) : null;\r
94                 if(section) {\r
95                     section.prepend(meta)\r
96                 }\r
97             } else {\r
98                 metadata.replaceWith(meta);\r
99             }\r
100             \r
101             \r
102             return vkbeautify.xml(toret.html());\r
103         }\r
104     }\r
105 \r
106     return transformations;\r
107 \r
108 });