132c70f70eb94186811e1f227eaf4f9b08a2f35d
[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', '[wlxml-tag]', function(e) { $(e.target).addClass('rng-hover')});\r
18             this.node.on('mouseout', '[wlxml-tag]', function(e) { $(e.target).removeClass('rng-hover')});\r
19             this.node.on('click', '[wlxml-tag]', function(e) {\r
20                 console.log('clicked node type: '+e.target.nodeType);\r
21                 view._markSelected($(e.target));\r
22             });\r
23 \r
24             this.node.on('keyup', function(e) {\r
25                 var anchor = $(window.getSelection().anchorNode);\r
26                 if(anchor[0].nodeType === Node.TEXT_NODE)\r
27                     anchor = anchor.parent();\r
28                 if(!anchor.is('[wlxml-tag]'))\r
29                     return;\r
30                 view._markSelected(anchor);\r
31             });
32         },\r
33         getMetaData: function() {\r
34             var toret = {};\r
35             this.node.find('#rng-visualEditor-meta table tr').each(function() {\r
36                 var tr = $(this);\r
37                 var key = $(tr.find('td')[0]).html();\r
38                 var value = $(tr.find('td input')[0]).val();\r
39                 toret[key] = value;\r
40             });\r
41             console.log(toret);\r
42             return toret;\r
43         },\r
44         setMetaData: function(metadata) {\r
45             var table = this.node.find('#rng-visualEditor-meta table');\r
46             table.empty();\r
47             _.each(_.keys(metadata), function(key) {    \r
48                 $(sandbox.getTemplate('metaItem')({key: key, value: metadata[key]})).appendTo(table);\r
49             });\r
50         },\r
51         setBody: function(HTMLTree) {\r
52             this.node.find('#rng-visualEditor-content').html(HTMLTree);\r
53         },\r
54         getBody: function() {\r
55             return this.node.find('#rng-visualEditor-content').html();\r
56         }, \r
57         _markSelected: function(node) {\r
58             this.node.find('.rng-current').removeClass('rng-current');\r
59             node.addClass('rng-current');\r
60         }\r
61     };\r
62     view.setup();\r
63     \r
64     var isDirty = false;\r
65     \r
66     \r
67     return {\r
68         start: function() {\r
69             sandbox.publish('ready');\r
70         },\r
71         getView: function() {\r
72             return view.node;\r
73         },\r
74         setDocument: function(xml) {\r
75             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
76             view.setBody(transformed.HTMLTree);\r
77             view.setMetaData(transformed.metadata);\r
78             isDirty = false;\r
79         },\r
80         getDocument: function() {\r
81             return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});\r
82         },\r
83         isDirty: function() {\r
84             return isDirty;\r
85         },\r
86         setDirty: function(dirty) {\r
87             isDirty = dirty;\r
88         }\r
89     \r
90     }\r
91 };