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