410478424db87e43196425ec26af7dd0716a74e9
[fnpeditor.git] / src / editor / modules / documentCanvas / documentCanvas.js
1 // Module that implements main WYSIWIG edit area
2
3 define([
4 'libs/underscore',
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.fromXML('', 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(xml) {
41             canvas.loadWlxml(xml);
42             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
43             sandbox.publish('documentSet');
44         },
45         setDocument2: function(wlxmlDocument) {
46             canvas.loadWlxmlDocument(wlxmlDocument);
47             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
48             sandbox.publish('documentSet');
49         },
50         getDocument: function() {
51             return canvas.toXML();
52         },
53         modifyCurrentNodeElement: function(attr, value) {
54             var currentNodeElement = canvas.getCurrentNodeElement();
55             if(attr === 'class' || attr === 'tag') {
56                 currentNodeElement['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);
57             } else {
58                 currentNodeElement.setWlxmlMetaAttr(attr, value);
59             }
60             sandbox.publish('currentNodeElementChanged', currentNodeElement);
61         },
62         highlightElement: function(element) {
63             element.toggleHighlight(true);
64         },
65         dimElement: function(element) {
66             element.toggleHighlight(false);
67         },
68         jumpToElement: function(element) {
69             canvas.setCurrentElement(element);
70         },
71         command: function(command, params) {
72             commands.run(command, params, canvas);
73             sandbox.publish('contentChanged');
74         }
75     };
76     
77 };
78
79 });