dbbbd0e734939740d3029624d24174c6dfed03db
[fnpeditor.git] / modules / documentCanvas / documentCanvas.js
1 // Module that implements main WYSIWIG edit area
2
3 define([
4 'libs/underscore-min',
5 './transformations', 
6 './canvas',
7 './canvasManager',
8 './canvas/canvas',
9 'libs/text!./template.html'], function(_, transformations, Canvas, CanvasManager, canvas3, template) {
10
11 'use strict';
12
13 return function(sandbox) {
14
15     var canvas = canvas3.fromXML('', sandbox.publish); //canvasCanvas.create();
16     var manager;
17     var canvasWrapper = $(template);
18     var shownAlready = false;
19     var scrollbarPosition = 0,
20         cursorPosition;
21     
22     canvasWrapper.onShow = function() {
23         if(!shownAlready) {
24             shownAlready = true;
25             canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement());
26         } else {
27             canvas.setCursorPosition(cursorPosition);
28             this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
29         }
30     };
31     
32     canvasWrapper.onHide = function() {
33        scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
34        cursorPosition = canvas.getCursor().getPosition();
35     };
36
37     /* public api */
38     return {
39         start: function() { sandbox.publish('ready'); },
40         getView: function() { 
41             return canvasWrapper;
42         },
43         setDocument: function(xml) {
44             canvas.loadWlxml(xml); //canvas.setHTML(transformations.fromXML.getHTMLTree(xml));
45             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
46             manager = new CanvasManager(canvas, sandbox);
47             sandbox.publish('documentSet');
48         },
49         getDocument: function() {
50             return transformations.toXML.getXML(canvas.getContent());
51         },
52         modifyCurrentNodeElement: function(attr, value) {
53             var currentNodeElement = canvas.getCurrentNodeElement();
54             if(attr === 'class' || attr === 'tag') {
55                 currentNodeElement['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);
56             } else {
57                 currentNodeElement.setWlxmlMetaAttr(attr, value);
58             }
59         },
60         highlightElement: function(element) {
61             canvas.highlightElement(element);
62         },
63         dimElement: function(element) {
64             canvas.dimElement(element);
65         },
66         jumpToElement: function(element) {
67             canvas.setCurrentElement(element);
68         },
69         command: function(command, params) {
70             manager.command(command, params);
71         }
72     };
73     
74 };
75
76 });