3e6a2098a3fc1dc13ebb951ce7f3aade8e929973
[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 './canvas/canvas',
7 'libs/text!./template.html'], function($, _, canvas3, template) {
8
9 'use strict';
10
11 return function(sandbox) {
12
13     var canvas = canvas3.fromXMLDocument(null);
14     var canvasWrapper = $(template);
15     var shownAlready = false;
16     var scrollbarPosition = 0,
17         actionHandlers = {},
18         cursorPosition;
19         
20     
21     canvas.on('selectionChanged', function(selection) {
22         sandbox.publish('selectionChanged', selection);
23     });
24
25     canvasWrapper.onShow = function() {
26         if(!shownAlready) {
27             shownAlready = true;
28             canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement());
29         } else {
30             canvas.setCursorPosition(cursorPosition);
31             this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
32         }
33     };
34     
35     canvasWrapper.onHide = function() {
36        scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
37        cursorPosition = canvas.getCursor().getPosition();
38     };
39
40     /* public api */
41     return {
42         start: function() {
43             sandbox.getPlugins().forEach(function(plugin) {
44                 var handlers;
45                 if(plugin.canvas) {
46                     handlers = plugin.canvas.actionHandlers;
47                     if(handlers && !_.isArray(handlers)) {
48                         handlers = [handlers];
49                     }
50                     actionHandlers[plugin.name] = handlers;
51                 }
52             });
53             sandbox.publish('ready');
54         },
55         getView: function() {
56             return canvasWrapper;
57         },
58         getCanvas: function() {
59             return canvas;
60         },
61         setDocument: function(wlxmlDocument) {
62             canvas.loadWlxmlDocument(wlxmlDocument);
63             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
64         },
65         highlightElement: function(node) {
66             canvas.toggleElementHighlight(node, true);
67         },
68         dimElement: function(node) {
69             canvas.toggleElementHighlight(node, false);
70         },
71         jumpToElement: function(node) {
72             canvas.setCurrentElement(node);
73         },
74         onAfterActionExecuted: function(action, ret) {
75             (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
76                 handler(canvas, action, ret);
77             });
78         }
79     };
80     
81 };
82
83 });