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