editor: Hide comment header if there is nothing to show in it
[fnpeditor.git] / src / editor / modules / documentCanvas / documentCanvas.js
1 // Module that implements main WYSIWIG edit area
2
3 define([
4 'libs/jquery',
5 'libs/underscore',
6 'fnpjs/logging/logging',
7 './canvas/canvas',
8 'libs/text!./template.html'], function($, _, logging, canvas3, template) {
9
10 'use strict';
11
12
13 var logger = logging.getLogger('documentCanvas');
14
15 return function(sandbox) {
16
17     var canvasElements = [];
18
19     sandbox.getPlugins().forEach(function(plugin) {
20         canvasElements = canvasElements.concat(plugin.canvasElements || []);
21     });
22
23     var canvas = canvas3.fromXMLDocument(null, canvasElements, {
24         user: sandbox.getConfig().user
25     });
26     var canvasWrapper = $(template);
27     var shownAlready = false;
28     var scrollbarPosition = 0,
29         actionHandlers = {},
30         cursorPosition;
31         
32     
33     canvas.on('selectionChanged', function(selection) {
34         sandbox.publish('selectionChanged', selection);
35     });
36
37     canvasWrapper.onShow = function() {
38         if(!shownAlready) {
39             shownAlready = true;
40             canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement());
41         } else {
42             canvas.setCursorPosition(cursorPosition);
43             this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
44         }
45     };
46     
47     canvasWrapper.onHide = function() {
48        scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
49        cursorPosition = canvas.getCursor().getPosition();
50     };
51
52     /* public api */
53     return {
54         start: function() {
55             sandbox.getPlugins().forEach(function(plugin) {
56                 var handlers;
57                 if(plugin.canvas) {
58                     handlers = plugin.canvas.actionHandlers;
59                     if(handlers && !_.isArray(handlers)) {
60                         handlers = [handlers];
61                     }
62                     actionHandlers[plugin.name] = handlers;
63                 }
64             });
65             sandbox.publish('ready');
66         },
67         getView: function() {
68             return canvasWrapper;
69         },
70         getCanvas: function() {
71             return canvas;
72         },
73         setDocument: function(wlxmlDocument) {
74             canvas.loadWlxmlDocument(wlxmlDocument);
75             canvasWrapper.find('#rng-module-documentCanvas-contentWrapper').empty().append(canvas.view());
76         },
77         highlightElement: function(node) {
78             canvas.toggleElementHighlight(node, true);
79         },
80         dimElement: function(node) {
81             canvas.toggleElementHighlight(node, false);
82         },
83         jumpToElement: function(node) {
84             canvas.setCurrentElement(node);
85         },
86         onAfterActionExecuted: function(action, ret) {
87             if(ret && ret.isValid()) {
88                 logger.debug('The action returned a valid fragment');
89                 return canvas.select(ret);
90             }
91
92             logger.debug('No valid fragment returned from the action');
93
94             (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
95                 handler(canvas, action, ret);
96             });
97         }
98     };
99     
100 };
101
102 });