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