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