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