50a37770151d659e96485ca9774c64f98e3c94dd
[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 'libs/text!./template.html'], function(_, transformations, Canvas, CanvasManager, template) {
9
10 'use strict';
11
12 return function(sandbox) {
13
14     var canvas = Canvas.create();
15     var manager = new CanvasManager(canvas, sandbox);
16
17     /* public api */
18     return {
19         start: function() { sandbox.publish('ready'); },
20         getView: function() { 
21             return canvas.dom;
22         },
23         setDocument: function(xml) {
24             canvas.setHTML(transformations.fromXML.getHTMLTree(xml));
25             sandbox.publish('documentSet');
26         },
27         getDocument: function() {
28             return transformations.toXML.getXML(canvas.getContent());
29         },
30         modifyCurrentNode: function(attr, value) {
31             if(manager.currentNode) {
32                 if(_.contains(['tag', 'class'], attr)) {
33                     manager.getNodeElement(manager.currentNode).attr('wlxml-'+attr, value);
34                 } else {
35                     // changing node meta attr
36                     manager.currentNode.setMetaAttr(attr, value);
37                 }
38                 sandbox.publish('contentChanged');
39                 sandbox.publish('currentNodeChanged', manager.currentNode);
40             }
41         },
42         highlightNode: function(canvasNode) {
43             manager.highlightNode(canvasNode);
44         },
45         dimNode: function(canvasNode) {
46             manager.dimNode(canvasNode);
47         },
48         selectNode: function(canvasNode) {
49             if(!canvasNode.isSame(manager.currentNode))
50                 manager.selectNode(canvasNode, {movecaret: true});
51         },
52         toggleGrid: function(toggle) {
53             manager.toggleGrid(toggle);
54         },
55         insertNewNode: function(wlxmlTag, wlxmlClass) {
56             manager.insertNewNode(wlxmlTag, wlxmlClass);
57         },
58         command: function(command, meta) {
59             manager.command(command, meta);
60         }
61     };
62     
63 };
64
65 });