6015089b567a641afd9336fdf70947a14cf94d74
[fnpeditor.git] / src / editor / modules / documentCanvas / documentCanvas.js
1 // Module that implements main WYSIWIG edit area
2
3 define([
4 'libs/jquery',
5 './canvas/canvas',
6 './commands',
7 'libs/text!./template.html'], function($, canvas3, commands, template) {
8
9 'use strict';
10
11 return function(sandbox) {
12
13     var canvas = canvas3.fromXMLDocument(null, sandbox.publish);
14     var canvasWrapper = $(template);
15     var shownAlready = false;
16     var scrollbarPosition = 0,
17         cursorPosition;
18     
19     canvasWrapper.onShow = function() {
20         if(!shownAlready) {
21             shownAlready = true;
22             canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement());
23         } else {
24             canvas.setCursorPosition(cursorPosition);
25             this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
26         }
27     };
28     
29     canvasWrapper.onHide = function() {
30        scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
31        cursorPosition = canvas.getCursor().getPosition();
32     };
33
34     /* public api */
35     return {
36         start: function() { sandbox.publish('ready'); },
37         getView: function() {
38             return canvasWrapper;
39         },
40         setDocument: function(wlxmlDocument) {
41             canvas.loadWlxmlDocument(wlxmlDocument);
42             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
43         },
44         highlightElement: function(node) {
45             canvas.toggleElementHighlight(node, true);
46         },
47         dimElement: function(node) {
48             canvas.toggleElementHighlight(node, false);
49         },
50         jumpToElement: function(node) {
51             canvas.setCurrentElement(node);
52         },
53         command: function(command, params) {
54             commands.run(command, params, canvas, sandbox.getConfig().user);
55         }
56     };
57     
58 };
59
60 });