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