fe666e053566e02328e203e573e5c7c6d69c4fa8
[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         historyItemAdded: function(item) {\r
135             sandbox.getModule('documentHistory').addHistory([item]);\r
136         }\r
137     }\r
138     \r
139     eventHandlers.mainBar = {\r
140         ready: function() {\r
141             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());\r
142         },\r
143         'cmd.save': function() {\r
144             synchronizeTab(views.mainTabs.getCurrentSlug());\r
145             sandbox.getModule('data').saveDocument();\r
146         }\r
147     }\r
148     \r
149     eventHandlers.indicator = {\r
150         ready: function() {\r
151             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());\r
152         }\r
153     };\r
154     \r
155 \r
156     \r
157     eventHandlers.documentCanvas = {\r
158         ready: function() {\r
159             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());\r
160             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());\r
161         },\r
162         documentSet: function() {\r
163             dirty.documentCanvas = false;\r
164         },\r
165         \r
166         nodeSelected: function(wlxmlNode) {\r
167             commands.selectNode(wlxmlNode);\r
168         },\r
169         \r
170         contentChanged: function() {\r
171             dirty.documentCanvas = true;\r
172         },\r
173         \r
174         nodeHovered: function(wlxmlNode) {\r
175             commands.highlightDocumentNode(wlxmlNode);\r
176         },\r
177         \r
178         nodeBlured: function(wlxmlNode) {\r
179             commands.dimDocumentNode(wlxmlNode);\r
180         }\r
181     };\r
182 \r
183     eventHandlers.nodePane = {\r
184         ready: function() {\r
185             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());\r
186         },\r
187         \r
188         nodeChanged: function(attr, value) {\r
189             sandbox.getModule('documentCanvas').modifyCurrentNode(attr, value);\r
190         }\r
191     };\r
192     \r
193     eventHandlers.metadataEditor = {\r
194         ready: function() {\r
195             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());\r
196             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\r
197         },\r
198         metadataChanged: function(metadata) {\r
199             dirty.metadataEditor = true;\r
200         },\r
201         metadataSet: function() {\r
202             dirty.metadataEditor = false;\r
203         },\r
204     };\r
205     \r
206     eventHandlers.nodeFamilyTree = {\r
207         ready: function() {\r
208             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());\r
209         },\r
210         nodeEntered: function(wlxmlNode) {\r
211             commands.highlightDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
212         },\r
213         nodeLeft: function(wlxmlNode) {\r
214             commands.dimDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
215         },\r
216         nodeSelected: function(wlxmlNode) {\r
217             commands.selectNode(wlxmlNode);\r
218         }\r
219     };\r
220     \r
221     eventHandlers.documentToolbar = {\r
222         ready: function() {\r
223             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());\r
224         },\r
225         toggleGrid: function(toggle) {\r
226             sandbox.getModule('documentCanvas').toggleGrid(toggle);\r
227         },\r
228         newNodeRequested: function(wlxmlTag, wlxmlClass) {\r
229             if(window.getSelection().isCollapsed) {\r
230                 sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);\r
231             } else {\r
232                 sandbox.getModule('documentCanvas').wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
233             }\r
234         }\r
235     };\r
236     \r
237     eventHandlers.nodeBreadCrumbs = {\r
238         ready: function() {\r
239             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());\r
240         },\r
241         nodeHighlighted: function(wlxmlNode) {\r
242             commands.highlightDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
243         },\r
244         nodeDimmed: function(wlxmlNode) {\r
245             commands.dimDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
246         },\r
247         nodeSelected: function(wlxmlNode) {\r
248             commands.selectNode(wlxmlNode);\r
249         }        \r
250     }\r
251     \r
252     eventHandlers.documentHistory = {\r
253         ready: function() {\r
254             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());\r
255             addMainTab('Historia', 'history', sandbox.getModule('documentHistory').getView());\r
256         }\r
257     }\r
258     \r
259     /* api */\r
260     \r
261     return {\r
262         start: function() {\r
263             sandbox.getModule('data').start();\r
264         },\r
265         handleEvent: function(moduleName, eventName, args) {\r
266             if('')\r
267                 wysiwigHandler.handleEvent(moduleName, eventName, args);\r
268             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {\r
269                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);\r
270             }\r
271         }\r
272     }\r
273 };\r
274 \r
275 });