63319227f0c54ecc3c4adc6b3880797775b4ff74
[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         getDocument: function() {
46             return canvas.toXML();
47         },
48         modifyCurrentNodeElement: function(attr, value) {
49             var currentNodeElement = canvas.getCurrentNodeElement();
50             if(attr === 'class' || attr === 'tag') {
51                 currentNodeElement['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);
52             } else {
53                 currentNodeElement.setWlxmlMetaAttr(attr, value);
54             }
55             sandbox.publish('currentNodeElementChanged', currentNodeElement);
56         },
57         highlightElement: function(node) {
58             canvas.toggleElementHighlight(node, true);
59         },
60         dimElement: function(node) {
61             canvas.toggleElementHighlight(node, false);
62         },
63         jumpToElement: function(node) {
64             canvas.setCurrentElement(node);
65         },
66         command: function(command, params) {
67             commands.run(command, params, canvas);
68             sandbox.publish('contentChanged');
69         }
70     };
71     
72 };
73
74 });