20851554b166f52a896b7e502554bbd6b38c0f0a
[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(''); //canvasCanvas.create();
16     var manager;
17     var canvasWrapper = $(template);
18
19     /* public api */
20     return {
21         start: function() { sandbox.publish('ready'); },
22         getView: function() { 
23             return canvasWrapper;
24         },
25         setDocument: function(xml) {
26             canvas.loadWlxml(xml); //canvas.setHTML(transformations.fromXML.getHTMLTree(xml));
27             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.doc().dom());
28             manager = new CanvasManager(canvas, sandbox);
29             sandbox.publish('documentSet');
30         },
31         getDocument: function() {
32             return transformations.toXML.getXML(canvas.getContent());
33         },
34         modifyCurrentNode: function(attr, value) {
35             if(manager.currentNode) {
36                 if(attr === 'tag') {
37                     manager.getNodeElement(manager.currentNode).attr('wlxml-'+attr, value);
38                 }
39                 else if(attr === 'class') {
40                     manager.currentNode.setClass(value);
41                 } else {
42                     // changing node meta attr
43                     manager.currentNode.setMetaAttr(attr, value);
44                 }
45                 sandbox.publish('contentChanged');
46                 sandbox.publish('currentNodeChanged', manager.currentNode);
47             }
48         },
49         highlightNode: function(canvasNode) {
50             manager.highlightNode(canvasNode);
51         },
52         dimNode: function(canvasNode) {
53             manager.dimNode(canvasNode);
54         },
55         selectNode: function(canvasNode) {
56             if(!canvasNode.isSame(manager.currentNode))
57                 manager.selectNode(canvasNode, {movecaret: true});
58         },
59         toggleGrid: function(toggle) {
60             manager.toggleGrid(toggle);
61         },
62         toggleList: function(toggle) {
63             manager.toggleList(toggle);
64         },
65         insertNewNode: function(wlxmlTag, wlxmlClass) {
66             manager.insertNewNode(wlxmlTag, wlxmlClass);
67         },
68         command: function(command, meta) {
69             manager.command(command, meta);
70         }
71     };
72     
73 };
74
75 });