Visual editor: selecting created node
[fnpeditor.git] / modules / visualEditor.transformations.js
1 if(typeof module !== 'undefined' && module.exports) {
2     var $ = require('jquery');
3 }
4
5 (function($) {
6
7     var transformations = {};
8
9     transformations.fromXML = {
10         getHTMLTree: function(xml) {
11             var inner = $(xml).clone();
12             var toret = $('<div></div>');
13             toret.append(inner);
14             
15             var toBlock = ['div', 'document', 'section', 'header'];
16             var toInline = ['aside', 'span'];
17             
18             toBlock.forEach(function(tagName) {
19                 tagName = tagName.toLowerCase();
20                 console.log('running ' + tagName);
21                 toret.find(tagName).replaceWith(function() {
22                     var currentTag = $(this);
23                     if(currentTag.attr('wlxml-tag'))
24                         return;
25                     var toret = $('<div></div>').attr('wlxml-tag', tagName);
26                     if(currentTag.attr('class'))
27                         toret.attr('wlxml-class', currentTag.attr('class').replace(/\./g, '-'));
28                     toret.append(currentTag.contents());
29                     return toret;
30                 });
31             });
32             
33             toInline.forEach(function(tagName) {
34                 tagName = tagName.toLowerCase();
35                 toret.find(tagName).replaceWith(function() {
36                     var currentTag = $(this);
37                     if(currentTag.attr('wlxml-tag'))
38                         return;
39                     var toret = $('<span></span>').attr('wlxml-tag', tagName);
40                     if(currentTag.attr('class'))
41                         toret.attr('wlxml-class', currentTag.attr('class').replace(/\./g, '-'));
42                     toret.append(currentTag.contents());
43                     return toret;
44                 });
45             });
46             return toret.children();
47         },
48         getMetaData: function(xml) {
49             var toret = {};
50             $(xml).find('metadata').children().each(function() {
51                 var node = $(this);
52                 toret[this.nodeName.split(':')[1].toLowerCase()] = node.text();
53             })
54             return toret;
55         },
56         getDocumentDescription: function(xml) {
57             return {
58                 HTMLTree: this.getHTMLTree(xml),
59                 metadata: this.getMetaData(xml)
60             }
61         }
62     }
63
64     transformations.toXML = {
65         getXML: function(documentDescription) {
66             
67             var inner = $(documentDescription.HTMLTree);
68             var toret = $('<div></div>');
69             toret.append(inner);
70             
71             toret.find('div, span').replaceWith(function() {
72                 var div = $(this);
73                 var tagName = div.attr('wlxml-tag');
74                 var toret = $('<'+tagName+'>');
75                 if(div.attr('wlxml-class'))
76                     toret.attr('class', div.attr('wlxml-class').replace(/-/g, '.'))
77                 toret.append(div.contents());
78                 return toret;
79             });
80             
81             var meta = $('<metadata></metadata>\n');
82             _.each(_.keys(documentDescription.metadata), function(key) {
83                 meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');
84             });
85             meta.append('\n');
86             
87             toret.find('metadata').replaceWith(meta);
88             
89             return toret.html();
90             
91         }
92     }
93
94
95     if(typeof module !== 'undefined' && module.exports) {
96         module.exports = transformations;
97     } else {
98         rng.modules.visualEditor.transformations = transformations;
99     }
100
101 })($);