6029e7c4a812d5774d29e191734bd812e545b18b
[fnpeditor.git] / modules / rng / rng.js
1 define([\r
2 'fnpjs/layout',\r
3 'fnpjs/vbox',\r
4 'views/tabs/tabs',\r
5 'libs/text!./mainLayout.html',\r
6 'libs/text!./editingLayout.html',\r
7 ], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate) {\r
8 \r
9 return function(sandbox) {\r
10     'use strict';\r
11     \r
12     function addMainTab(title, slug, view) {\r
13         views.mainTabs.addTab(title, slug, view);\r
14     }\r
15     \r
16     var dirty = {\r
17         sourceEditor: false,\r
18         documentCanvas: false,\r
19         metadataEditor: false,\r
20     };\r
21     \r
22     var commands = {\r
23         highlightDocumentNode: function(wlxmlNode, origin) {\r
24             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
25                 if(!origin || moduleName != origin)\r
26                     sandbox.getModule(moduleName).highlightNode(wlxmlNode)\r
27             });\r
28         },\r
29         dimDocumentNode: function(wlxmlNode, origin) {\r
30             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
31                 if(!origin || moduleName != origin)\r
32                     sandbox.getModule(moduleName).dimNode(wlxmlNode)\r
33             });\r
34         },\r
35         selectNode: function(wlxmlNode, origin) {\r
36             sandbox.getModule('documentCanvas').selectNode(wlxmlNode);\r
37             sandbox.getModule('nodePane').setNode(wlxmlNode);\r
38             sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode);\r
39             sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode);\r
40             \r
41         }\r
42     }\r
43     \r
44 \r
45     var views = {\r
46         mainLayout: new layout.Layout(mainLayoutTemplate),\r
47         mainTabs: (new tabs.View()).render(),\r
48         visualEditing: new layout.Layout(visualEditingLayoutTemplate),\r
49         visualEditingSidebar: (new tabs.View({stacked: true})).render(),\r
50         currentNodePaneLayout: new vbox.VBox()\r
51     }\r
52     \r
53     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());\r
54     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());\r
55     \r
56     sandbox.getDOM().append(views.mainLayout.getAsView());\r
57     \r
58     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());\r
59 \r
60     views.mainTabs.on('tabSelected', function(event) {\r
61         if(event.prevSlug) {\r
62             if(event.prevSlug === 'sourceEditor' && dirty.sourceEditor) {\r
63                 sandbox.getModule('data').commitDocument(sandbox.getModule('sourceEditor').getDocument(), 'source_edit');\r
64             }\r
65             if(event.prevSlug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor)) {\r
66                 var doc = dirty.documentCanvas ? sandbox.getModule('documentCanvas').getDocument() : sandbox.getModule('data').getDocument();\r
67                 if(dirty.metadataEditor) {\r
68                     doc = sandbox.getModule('metadataEditor').attachMetadata(doc);\r
69                 }\r
70                 sandbox.getModule('data').commitDocument(doc, 'edit');\r
71             }\r
72             \r
73         }\r
74     });\r
75     \r
76     /* Events handling */\r
77     \r
78     var eventHandlers = {};\r
79      \r
80     eventHandlers.sourceEditor = {\r
81         ready: function() {\r
82             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());\r
83             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());\r
84         },\r
85         xmlChanged: function() {\r
86             dirty.sourceEditor = true;\r
87         },\r
88         documentSet: function() {\r
89             dirty.sourceEditor = false;\r
90         }\r
91     };\r
92     \r
93     eventHandlers.data = {\r
94         ready: function() {\r
95             views.mainLayout.setView('mainView', views.mainTabs.getAsView());\r
96             \r
97             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator'], function(moduleName) {\r
98                 sandbox.getModule(moduleName).start();\r
99             });\r
100         },\r
101         documentChanged: function(document, reason) {\r
102             var modules = [];\r
103             if(reason === 'source_edit')\r
104                 modules = ['documentCanvas', 'metadataEditor'];\r
105             else if (reason === 'edit')\r
106                 modules = ['sourceEditor'];\r
107                 \r
108             modules.forEach(function(moduleName) {\r
109                 sandbox.getModule(moduleName).setDocument(document);\r
110             });\r
111         },\r
112         savingStarted: function() {\r
113             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
114             sandbox.getModule('indicator').showMessage(gettext('Saving...'));\r
115         },\r
116         savingEnded: function(status) {\r
117             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
118             sandbox.getModule('indicator').clearMessage();\r
119         }\r
120     }\r
121     \r
122     eventHandlers.mainBar = {\r
123         ready: function() {\r
124             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());\r
125         },\r
126         'cmd.save': function() {\r
127             sandbox.getModule('data').fakeSave();\r
128         }\r
129     }\r
130     \r
131     eventHandlers.indicator = {\r
132         ready: function() {\r
133             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());\r
134         }\r
135     };\r
136     \r
137 \r
138     \r
139     eventHandlers.documentCanvas = {\r
140         ready: function() {\r
141             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());\r
142             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());\r
143         },\r
144         documentSet: function() {\r
145             dirty.documentCanvas = false;\r
146         },\r
147         \r
148         nodeSelected: function(wlxmlNode) {\r
149             commands.selectNode(wlxmlNode);\r
150         },\r
151         \r
152         contentChanged: function() {\r
153             dirty.documentCanvas = true;\r
154         },\r
155         \r
156         nodeHovered: function(wlxmlNode) {\r
157             commands.highlightDocumentNode(wlxmlNode);\r
158         },\r
159         \r
160         nodeBlured: function(wlxmlNode) {\r
161             commands.dimDocumentNode(wlxmlNode);\r
162         }\r
163     };\r
164 \r
165     eventHandlers.nodePane = {\r
166         ready: function() {\r
167             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());\r
168         },\r
169         \r
170         nodeChanged: function(attr, value) {\r
171             sandbox.getModule('documentCanvas').modifyCurrentNode(attr, value);\r
172         }\r
173     };\r
174     \r
175     eventHandlers.metadataEditor = {\r
176         ready: function() {\r
177             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());\r
178             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\r
179         },\r
180         metadataChanged: function(metadata) {\r
181             dirty.metadataEditor = true;\r
182         },\r
183         metadataSet: function() {\r
184             dirty.metadataEditor = false;\r
185         },\r
186     };\r
187     \r
188     eventHandlers.nodeFamilyTree = {\r
189         ready: function() {\r
190             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());\r
191         },\r
192         nodeEntered: function(wlxmlNode) {\r
193             commands.highlightDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
194         },\r
195         nodeLeft: function(wlxmlNode) {\r
196             commands.dimDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
197         },\r
198         nodeSelected: function(wlxmlNode) {\r
199             commands.selectNode(wlxmlNode);\r
200         }\r
201     };\r
202     \r
203     eventHandlers.documentToolbar = {\r
204         ready: function() {\r
205             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());\r
206         },\r
207         toggleGrid: function(toggle) {\r
208             sandbox.getModule('documentCanvas').toggleGrid(toggle);\r
209         },\r
210         newNodeRequested: function(wlxmlTag, wlxmlClass) {\r
211             if(window.getSelection().isCollapsed) {\r
212                 sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);\r
213             } else {\r
214                 sandbox.getModule('documentCanvas').wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
215             }\r
216         }\r
217     };\r
218     \r
219     eventHandlers.nodeBreadCrumbs = {\r
220         ready: function() {\r
221             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());\r
222         },\r
223         nodeHighlighted: function(wlxmlNode) {\r
224             commands.highlightDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
225         },\r
226         nodeDimmed: function(wlxmlNode) {\r
227             commands.dimDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
228         },\r
229         nodeSelected: function(wlxmlNode) {\r
230             commands.selectNode(wlxmlNode);\r
231         }        \r
232     }\r
233     \r
234     /* api */\r
235     \r
236     return {\r
237         start: function() {\r
238             sandbox.getModule('data').start();\r
239         },\r
240         handleEvent: function(moduleName, eventName, args) {\r
241             if('')\r
242                 wysiwigHandler.handleEvent(moduleName, eventName, args);\r
243             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {\r
244                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);\r
245             }\r
246         }\r
247     }\r
248 };\r
249 \r
250 });