6f65c19436a4481f697aff0834f5bd4d5c133948
[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     var commands = {\r
17         highlightDocumentNode: function(wlxmlNode, origin) {\r
18             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
19                 if(!origin || moduleName != origin)\r
20                     sandbox.getModule(moduleName).highlightNode(wlxmlNode)\r
21             });\r
22         },\r
23         dimDocumentNode: function(wlxmlNode, origin) {\r
24             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
25                 if(!origin || moduleName != origin)\r
26                     sandbox.getModule(moduleName).dimNode(wlxmlNode)\r
27             });\r
28         },\r
29         selectNode: function(wlxmlNode, origin) {\r
30             sandbox.getModule('documentCanvas').selectNode(wlxmlNode);\r
31             sandbox.getModule('nodePane').setNode(wlxmlNode);\r
32             sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode);\r
33             sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode);\r
34             \r
35         }\r
36     }\r
37     \r
38 \r
39     var views = {\r
40         mainLayout: new layout.Layout(mainLayoutTemplate),\r
41         mainTabs: (new tabs.View()).render(),\r
42         visualEditing: new layout.Layout(visualEditingLayoutTemplate),\r
43         visualEditingSidebar: (new tabs.View({stacked: true})).render(),\r
44         currentNodePaneLayout: new vbox.VBox()\r
45     }\r
46     \r
47     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());\r
48     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());\r
49     \r
50     sandbox.getDOM().append(views.mainLayout.getAsView());\r
51     \r
52     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());\r
53 \r
54     \r
55     /* Events handling */\r
56     \r
57     var eventHandlers = {};\r
58      \r
59     eventHandlers.sourceEditor = {\r
60         ready: function() {\r
61             addMainTab(gettext('Source'), 'source',  sandbox.getModule('sourceEditor').getView());\r
62             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());\r
63         }\r
64     };\r
65     \r
66     eventHandlers.data = {\r
67         ready: function() {\r
68             views.mainLayout.setView('mainView', views.mainTabs.getAsView());\r
69             \r
70             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator'], function(moduleName) {\r
71                 sandbox.getModule(moduleName).start();\r
72             });\r
73         },\r
74         documentChanged: function(document, reason) {\r
75             var slug = (reason === 'visual_edit' ? 'source' : 'visual');\r
76             sandbox.getModule(slug+'Editor').setDocument(document);\r
77         },\r
78         savingStarted: function() {\r
79             sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
80             sandbox.getModule('indicator').showMessage(gettext('Saving...'));\r
81         },\r
82         savingEnded: function(status) {\r
83             sandbox.getModule('mainBar').setCommandEnabled('save', true);\r
84             sandbox.getModule('indicator').clearMessage();\r
85         }\r
86     }\r
87     \r
88     eventHandlers.mainBar = {\r
89         ready: function() {\r
90             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());\r
91         },\r
92         'cmd.save': function() {\r
93             sandbox.getModule('data').fakeSave();\r
94         }\r
95     }\r
96     \r
97     eventHandlers.indicator = {\r
98         ready: function() {\r
99             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());\r
100         }\r
101     };\r
102     \r
103 \r
104     \r
105     eventHandlers.documentCanvas = {\r
106         ready: function() {\r
107             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());\r
108             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());\r
109         },\r
110         \r
111         nodeSelected: function(wlxmlNode) {\r
112             commands.selectNode(wlxmlNode);\r
113         },\r
114         \r
115         contentChanged: function() {\r
116         \r
117         },\r
118         \r
119         nodeHovered: function(wlxmlNode) {\r
120             commands.highlightDocumentNode(wlxmlNode);\r
121         },\r
122         \r
123         nodeBlured: function(wlxmlNode) {\r
124             commands.dimDocumentNode(wlxmlNode);\r
125         }\r
126     };\r
127 \r
128     eventHandlers.nodePane = {\r
129         ready: function() {\r
130             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());\r
131         },\r
132         \r
133         nodeChanged: function(attr, value) {\r
134             sandbox.getModule('documentCanvas').modifyCurrentNode(attr, value);\r
135         }\r
136     };\r
137     \r
138     eventHandlers.metadataEditor = {\r
139         ready: function() {\r
140             sandbox.getModule('metadataEditor').setMetadata(sandbox.getModule('data').getDocument());\r
141             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\r
142         }\r
143     };\r
144     \r
145     eventHandlers.nodeFamilyTree = {\r
146         ready: function() {\r
147             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());\r
148         },\r
149         nodeEntered: function(wlxmlNode) {\r
150             commands.highlightDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
151         },\r
152         nodeLeft: function(wlxmlNode) {\r
153             commands.dimDocumentNode(wlxmlNode, 'nodeFamilyTree');\r
154         },\r
155         nodeSelected: function(wlxmlNode) {\r
156             commands.selectNode(wlxmlNode);\r
157         }\r
158     };\r
159     \r
160     eventHandlers.documentToolbar = {\r
161         ready: function() {\r
162             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());\r
163         },\r
164         toggleGrid: function(toggle) {\r
165             sandbox.getModule('documentCanvas').toggleGrid(toggle);\r
166         },\r
167         newNodeRequested: function(wlxmlTag, wlxmlClass) {\r
168             if(window.getSelection().isCollapsed) {\r
169                 sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);\r
170             } else {\r
171                 sandbox.getModule('documentCanvas').wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
172             }\r
173         }\r
174     };\r
175     \r
176     eventHandlers.nodeBreadCrumbs = {\r
177         ready: function() {\r
178             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());\r
179         },\r
180         nodeHighlighted: function(wlxmlNode) {\r
181             commands.highlightDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
182         },\r
183         nodeDimmed: function(wlxmlNode) {\r
184             commands.dimDocumentNode(wlxmlNode, 'nodeBreadCrumbs');\r
185         },\r
186         nodeSelected: function(wlxmlNode) {\r
187             commands.selectNode(wlxmlNode);\r
188         }        \r
189     }\r
190     \r
191     /* api */\r
192     \r
193     return {\r
194         start: function() {\r
195             sandbox.getModule('data').start();\r
196         },\r
197         handleEvent: function(moduleName, eventName, args) {\r
198             if('')\r
199                 wysiwigHandler.handleEvent(moduleName, eventName, args);\r
200             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {\r
201                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);\r
202             }\r
203         }\r
204     }\r
205 };\r
206 \r
207 });