Layout informs its views on being shown
[fnpeditor.git] / modules / documentCanvas / transformations.js
1 define(['libs/jquery-1.9.1.min'], function($) {\r
2 \r
3     'use strict';\r
4 \r
5     var transformations = {};\r
6 \r
7     transformations.fromXML = {\r
8         getHTMLTree: function(xml) {\r
9             var inner = $(xml).clone();\r
10             var toret = $('<div></div>');\r
11             toret.append(inner);\r
12             \r
13             var toBlock = ['div', 'section', 'header'];\r
14             var toInline = ['aside', 'span'];\r
15             \r
16             var transform = function(tags, replacingTagName) {\r
17                 tags.forEach(function(tagName) {\r
18                     tagName = tagName.toLowerCase();\r
19                     console.log('running ' + tagName);\r
20                     toret.find(tagName).replaceWith(function() {\r
21                         var currentTag = $(this);\r
22                         if(currentTag.attr('wlxml-tag'))\r
23                             return;\r
24                         var toret = $('<' + replacingTagName + '>').attr('wlxml-tag', tagName);\r
25                         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
26                         for(var i = 0; i < this.attributes.length; i++) {\r
27                             var attr = this.attributes.item(i);\r
28                             var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;\r
29                             toret.attr('wlxml-' + attr.name, value)\r
30                         }\r
31                         toret.append(currentTag.contents());\r
32                         return toret;\r
33                     });\r
34                 });\r
35             }\r
36             \r
37             transform(toBlock, 'div');\r
38             transform(toInline, 'span');\r
39 \r
40             return toret.children();\r
41         },\r
42         getMetaData: function(xml) {\r
43             var toret = {};\r
44             $(xml).find('metadata').children().each(function() {\r
45                 var node = $(this);\r
46                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();\r
47             })\r
48             return toret;\r
49         },\r
50         getDocumentDescription: function(xml) {\r
51             return {\r
52                 HTMLTree: this.getHTMLTree(xml),\r
53                 metadata: this.getMetaData(xml)\r
54             }\r
55         }\r
56     }\r
57 \r
58     transformations.toXML = {\r
59         getXML: function(body) {\r
60             \r
61             var inner = $(body);\r
62             var toret = $('<div></div>');\r
63             toret.append(inner);\r
64             \r
65             toret.find('div, span').replaceWith(function() {\r
66                 var div = $(this);\r
67                 var tagName = div.attr('wlxml-tag');\r
68                 var toret = $('<'+tagName+'>');\r
69                                    \r
70                 for(var i = 0; i < this.attributes.length; i++) {\r
71                     var attr = this.attributes.item(i);\r
72                     var split = attr.name.split('-')\r
73                     console.log(split);\r
74                     if(split[0] !== 'wlxml' || (split.length > 1 && split[1] === 'tag')) \r
75                         continue;\r
76                     var wlxmlName = split.splice(1).join('-');\r
77                     var value = wlxmlName === 'class' ? attr.value.replace(/-/g, '.') : attr.value;\r
78                     console.log(name + ': ' + value);\r
79                     toret.attr(wlxmlName, value);\r
80                 }\r
81                     \r
82                 toret.append(div.contents());\r
83                 return toret;\r
84             });\r
85 \r
86             return vkbeautify.xml(toret.html());\r
87         }\r
88     }\r
89 \r
90     return transformations;\r
91 \r
92 });