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 '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         insertNewNode: function(wlxmlTag, wlxmlClass) {
63             manager.insertNewNode(wlxmlTag, wlxmlClass);
64         },
65         command: function(command, meta) {
66             manager.command(command, meta);
67         }
68     };
69     
70 };
71
72 });