Introducing Ace as the source editor
[fnpeditor.git] / modules / visualEditor.js
1 rng.modules.visualEditor = function(sandbox) {\r
2     var transformations = rng.modules.visualEditor.transformations;\r
3 \r
4     var view = {\r
5         node: $(sandbox.getTemplate('main')()),\r
6         setup: function() {\r
7             var view = this;\r
8
9             this.node.find('#rng-visualEditor-content').on('keyup', function() {\r
10                 isDirty = true;\r
11             });\r
12             \r
13             this.node.find('#rng-visualEditor-meta').on('keyup', function() {\r
14                 isDirty = true;\r
15             });\r
16
17             this.node.on('mouseover', '.rng', function(e) { $(e.target).addClass('rng-hover')});\r
18             this.node.on('mouseout', '.rng', function(e) { $(e.target).removeClass('rng-hover')});\r
19             this.node.on('click', '.rng', function(e) {\r
20                 view._markSelected($(e.target));\r
21             });\r
22 \r
23             this.node.on('keyup', function(e) {\r
24                 var anchor = $(window.getSelection().anchorNode);\r
25                 if(anchor[0].nodeType === Node.TEXT_NODE)\r
26                     anchor = anchor.parent();\r
27                 if(!anchor.hasClass('rng'))\r
28                     return;\r
29                 view._markSelected(anchor);\r
30             });
31         },\r
32         getMetaData: function() {\r
33             var toret = {};\r
34             this.node.find('#rng-visualEditor-meta table tr').each(function() {\r
35                 var tr = $(this);\r
36                 var key = $(tr.find('td')[0]).html();\r
37                 var value = $(tr.find('td input')[0]).val();\r
38                 toret[key] = value;\r
39             });\r
40             console.log(toret);\r
41             return toret;\r
42         },\r
43         setMetaData: function(metadata) {\r
44             var table = this.node.find('#rng-visualEditor-meta table');\r
45             table.empty();\r
46             _.each(_.keys(metadata), function(key) {    \r
47                 $(sandbox.getTemplate('metaItem')({key: key, value: metadata[key]})).appendTo(table);\r
48             });\r
49         },\r
50         setBody: function(HTMLTree) {\r
51             this.node.find('#rng-visualEditor-content').html(HTMLTree);\r
52         },\r
53         getBody: function() {\r
54             return this.node.find('#rng-visualEditor-content').html();\r
55         }, \r
56         _markSelected: function(node) {\r
57             this.node.find('.rng').removeClass('rng-current');\r
58             node.addClass('rng-current');\r
59         }\r
60     };\r
61     view.setup();\r
62     \r
63     var isDirty = false;\r
64     \r
65     \r
66     return {\r
67         start: function() {\r
68             sandbox.publish('ready');\r
69         },\r
70         getView: function() {\r
71             return view.node;\r
72         },\r
73         setDocument: function(xml) {\r
74             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
75             view.setBody(transformed.HTMLTree);\r
76             view.setMetaData(transformed.metadata);\r
77             isDirty = false;\r
78         },\r
79         getDocument: function() {\r
80             return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});\r
81         },\r
82         isDirty: function() {\r
83             return isDirty;\r
84         },\r
85         setDirty: function(dirty) {\r
86             isDirty = dirty;\r
87         }\r
88     \r
89     }\r
90 };