Refactoring
[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 './commands',
10 'libs/text!./template.html'], function(_, transformations, Canvas, CanvasManager, canvas3, commands, template) {
11
12 'use strict';
13
14 return function(sandbox) {
15
16     var canvas = canvas3.fromXML('', sandbox.publish); //canvasCanvas.create();
17     var manager;
18     var canvasWrapper = $(template);
19     var shownAlready = false;
20     var scrollbarPosition = 0,
21         cursorPosition;
22     
23     canvasWrapper.onShow = function() {
24         if(!shownAlready) {
25             shownAlready = true;
26             canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement());
27         } else {
28             canvas.setCursorPosition(cursorPosition);
29             this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
30         }
31     };
32     
33     canvasWrapper.onHide = function() {
34        scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
35        cursorPosition = canvas.getCursor().getPosition();
36     };
37
38     /* public api */
39     return {
40         start: function() { sandbox.publish('ready'); },
41         getView: function() { 
42             return canvasWrapper;
43         },
44         setDocument: function(xml) {
45             canvas.loadWlxml(xml); //canvas.setHTML(transformations.fromXML.getHTMLTree(xml));
46             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
47             manager = new CanvasManager(canvas, sandbox);
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             canvas.highlightElement(element);
64         },
65         dimElement: function(element) {
66             canvas.dimElement(element);
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 });