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