bde08d3deebf6846f0ad1321b8ac59940e232ac6
[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         } else {
26             canvas.setCursorPosition(cursorPosition);
27             this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
28         }
29     };
30     
31     canvasWrapper.onHide = function() {
32        scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
33        cursorPosition = canvas.getCursor().getPosition();
34     };
35
36     /* public api */
37     return {
38         start: function() { sandbox.publish('ready'); },
39         getView: function() { 
40             return canvasWrapper;
41         },
42         setDocument: function(xml) {
43             canvas.loadWlxml(xml); //canvas.setHTML(transformations.fromXML.getHTMLTree(xml));
44             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
45             manager = new CanvasManager(canvas, sandbox);
46             sandbox.publish('documentSet');
47         },
48         getDocument: function() {
49             return transformations.toXML.getXML(canvas.getContent());
50         },
51         modifyCurrentNodeElement: function(attr, value) {
52             if(attr === 'class' || attr === 'tag') {
53                 canvas.getCurrentNodeElement()['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);    
54             }
55         },
56         highlightElement: function(element) {
57             canvas.highlightElement(element);
58         },
59         dimElement: function(element) {
60             canvas.dimElement(element);
61         },
62         jumpToElement: function(element) {
63             canvas.setCurrentElement(element);
64         },
65         command: function(command, params) {
66             manager.command(command, params);
67         }
68     };
69     
70 };
71
72 });