Clean up: removing unused template
[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         resetDocument: function(document, reason) {\r
73             var modules = [];\r
74             if(reason === 'source_edit')\r
75                 modules = ['documentCanvas', 'metadataEditor'];\r
76             else if (reason === 'edit')\r
77                 modules = ['sourceEditor'];\r
78             else if (reason === 'revert')\r
79                 modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];\r
80                 \r
81             modules.forEach(function(moduleName) {\r
82                 sandbox.getModule(moduleName).setDocument(document);\r
83             });\r
84         }\r
85     }\r
86     \r
87 \r
88     var views = {\r
89         mainLayout: new layout.Layout(mainLayoutTemplate),\r
90         mainTabs: (new tabs.View()).render(),\r
91         visualEditing: new layout.Layout(visualEditingLayoutTemplate),\r
92         visualEditingSidebar: (new tabs.View({stacked: true})).render(),\r
93         currentNodePaneLayout: new vbox.VBox(),\r
94         diffLayout: new layout.Layout(diffLayoutTemplate)\r
95     }\r
96     \r
97     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());\r
98     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());\r
99     \r
100     addMainTab('Historia', 'history', views.diffLayout.getAsView());\r
101     \r
102     sandbox.getDOM().append(views.mainLayout.getAsView());\r
103     \r
104     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());\r
105 \r
106     views.mainTabs.on('tabSelected', function(event) {\r
107         if(event.prevSlug) {\r
108             synchronizeTab(event.prevSlug);\r
109         }\r
110     });\r
111     \r
112     /* Events handling */\r
113     \r
114     var eventHandlers = {};\r
115      \r
116     eventHandlers.sourceEditor = {\r
117         ready: function() {\r
118             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());\r
119             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());\r
120         },\r
121         xmlChanged: function() {\r
122             dirty.sourceEditor = true;\r
123         },\r
124         documentSet: function() {\r
125             dirty.sourceEditor = false;\r
126         }\r
127     };\r
128     \r
129     eventHandlers.data = {\r
130         ready: function() {\r
131             views.mainLayout.setView('mainView', views.mainTabs.getAsView());\r
132             \r
133             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {\r
134                 sandbox.getModule(moduleName).start();\r
135             });\r
136         },\r
137         documentChanged: function(document, reason) {\r
138             commands.resetDocument(document, reason);\r
139         },\r
140         savingStarted: function() {\r
141             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
142             sandbox.getModule('indicator').showMessage(gettext('Saving...'));\r
143         },\r
144         savingEnded: function(status) {\r
145             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
146             sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});\r
147         },\r
148         restoringStarted: function(event) {\r
149             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
150             sandbox.getModule('indicator').showMessage(gettext('Restoring version ' + event.version + '...'));\r
151         },\r
152         historyItemAdded: function(item) {\r
153             sandbox.getModule('documentHistory').addHistory([item], {animate: true});\r
154         },\r
155         diffFetched: function(diff) {\r
156             sandbox.getModule('diffViewer').setDiff(diff);\r
157         },\r
158         documentReverted: function(event) {\r
159             commands.resetDocument(event.document, 'revert');\r
160             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
161             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.version + ' przywrócona'});\r
162         }\r
163     }\r
164     \r
165     eventHandlers.mainBar = {\r
166         ready: function() {\r
167             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());\r
168         },\r
169         'cmd.save': function() {\r
170             synchronizeTab(views.mainTabs.getCurrentSlug());\r
171             sandbox.getModule('data').saveDocument();\r
172         }\r
173     }\r
174     \r
175     eventHandlers.indicator = {\r
176         ready: function() {\r
177             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());\r
178         }\r
179     };\r
180     \r
181 \r
182     \r
183     eventHandlers.documentCanvas = {\r
184         ready: function() {\r
185             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());\r
186             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());\r
187         },\r
188         documentSet: function() {\r
189             dirty.documentCanvas = false;\r
190         },\r
191         \r
192         nodeSelected: function(wlxmlNode) {\r
193             commands.selectNode(wlxmlNode);\r
194         },\r
195         \r
196         contentChanged: function() {\r
197             dirty.documentCanvas = true;\r
198         },\r
199         \r
200         nodeHovered: function(wlxmlNode) {\r
201             commands.highlightDocumentNode(wlxmlNode);\r
202         },\r
203         \r
204         nodeBlured: function(wlxmlNode) {\r
205             commands.dimDocumentNode(wlxmlNode);\r
206         }\r
207     };\r
208 \r
209     eventHandlers.nodePane = {\r
210         ready: function() {\r
211             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());\r
212         },\r
213         \r
214         nodeChanged: function(attr, value) {\r
215             sandbox.getModule('documentCanvas').modifyCurrentNode(attr, value);\r
216         }\r
217     };\r
218     \r
219     eventHandlers.metadataEditor = {\r
220         ready: function() {\r
221             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());\r
222             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\r
223         },\r
224         metadataChanged: function(metadata) {\r
225             dirty.metadataEditor = true;\r
226         },\r
227         metadataSet: function() {\r
228             dirty.metadataEditor = false;\r
229         },\r
230     };\r
231     \r
232     eventHandlers.nodeFamilyTree = {\r
233         ready: function() {\r
234             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());\r
235         },\r
236         nodeEntered: function(wlxmlNode) {\r
237             commands.highlightDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
238         },\r
239         nodeLeft: function(wlxmlNode) {\r
240             commands.dimDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
241         },\r
242         nodeSelected: function(wlxmlNode) {\r
243             commands.selectNode(wlxmlNode);\r
244         }\r
245     };\r
246     \r
247     eventHandlers.documentToolbar = {\r
248         ready: function() {\r
249             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());\r
250         },\r
251         toggleGrid: function(toggle) {\r
252             sandbox.getModule('documentCanvas').toggleGrid(toggle);\r
253         },\r
254         newNodeRequested: function(wlxmlTag, wlxmlClass) {\r
255             if(window.getSelection().isCollapsed) {\r
256                 sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);\r
257             } else {\r
258                 sandbox.getModule('documentCanvas').wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
259             }\r
260         }\r
261     };\r
262     \r
263     eventHandlers.nodeBreadCrumbs = {\r
264         ready: function() {\r
265             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());\r
266         },\r
267         nodeHighlighted: function(wlxmlNode) {\r
268             commands.highlightDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
269         },\r
270         nodeDimmed: function(wlxmlNode) {\r
271             commands.dimDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
272         },\r
273         nodeSelected: function(wlxmlNode) {\r
274             commands.selectNode(wlxmlNode);\r
275         }        \r
276     }\r
277     \r
278     eventHandlers.documentHistory = {\r
279         ready: function() {\r
280             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());\r
281             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());\r
282         },\r
283         compare: function(ver1, ver2) {\r
284             sandbox.getModule('data').fetchDiff(ver1, ver2);\r
285         },\r
286         restoreVersion: function(event) {\r
287             sandbox.getModule('data').restoreVersion(event);\r
288         },\r
289         displayVersion: function(event) {\r
290             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());\r
291         }\r
292     }\r
293     \r
294     eventHandlers.diffViewer = {\r
295         ready: function() {\r
296             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());\r
297         }\r
298     }\r
299     \r
300     /* api */\r
301     \r
302     return {\r
303         start: function() {\r
304             sandbox.getModule('data').start();\r
305         },\r
306         handleEvent: function(moduleName, eventName, args) {\r
307             if('')\r
308                 wysiwigHandler.handleEvent(moduleName, eventName, args);\r
309             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {\r
310                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);\r
311             }\r
312         }\r
313     }\r
314 };\r
315 \r
316 });