smartxml: Allow Fragments to refresh their node references using their initial paths
[fnpeditor.git] / src / editor / modules / documentCanvas / documentCanvas.js
index 88538c4..5942d8f 100644 (file)
@@ -1,21 +1,37 @@
 // Module that implements main WYSIWIG edit area
 
 define([
+'libs/jquery',
 'libs/underscore',
+'fnpjs/logging/logging',
 './canvas/canvas',
-'./commands',
-'libs/text!./template.html'], function(_, canvas3, commands, template) {
+'libs/text!./template.html'], function($, _, logging, canvas3, template) {
 
 'use strict';
 
+
+var logger = logging.getLogger('documentCanvas');
+
 return function(sandbox) {
 
-    var canvas = canvas3.fromXMLDocument(null, sandbox.publish);
+    var canvasElements = [];
+
+    sandbox.getPlugins().forEach(function(plugin) {
+        canvasElements = canvasElements.concat(plugin.canvasElements || []);
+    });
+
+    var canvas = canvas3.fromXMLDocument(null, canvasElements);
     var canvasWrapper = $(template);
     var shownAlready = false;
     var scrollbarPosition = 0,
+        actionHandlers = {},
         cursorPosition;
+        
     
+    canvas.on('selectionChanged', function(selection) {
+        sandbox.publish('selectionChanged', selection);
+    });
+
     canvasWrapper.onShow = function() {
         if(!shownAlready) {
             shownAlready = true;
@@ -33,39 +49,52 @@ return function(sandbox) {
 
     /* public api */
     return {
-        start: function() { sandbox.publish('ready'); },
-        getView: function() { 
+        start: function() {
+            sandbox.getPlugins().forEach(function(plugin) {
+                var handlers;
+                if(plugin.canvas) {
+                    handlers = plugin.canvas.actionHandlers;
+                    if(handlers && !_.isArray(handlers)) {
+                        handlers = [handlers];
+                    }
+                    actionHandlers[plugin.name] = handlers;
+                }
+            });
+            sandbox.publish('ready');
+        },
+        getView: function() {
             return canvasWrapper;
         },
+        getCanvas: function() {
+            return canvas;
+        },
         setDocument: function(wlxmlDocument) {
             canvas.loadWlxmlDocument(wlxmlDocument);
             canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
-            sandbox.publish('documentSet');
-        },
-        getDocument: function() {
-            return canvas.toXML();
-        },
-        modifyCurrentNodeElement: function(attr, value) {
-            var currentNodeElement = canvas.getCurrentNodeElement();
-            if(attr === 'class' || attr === 'tag') {
-                currentNodeElement['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);
-            } else {
-                currentNodeElement.setWlxmlMetaAttr(attr, value);
-            }
-            sandbox.publish('currentNodeElementChanged', currentNodeElement);
         },
-        highlightElement: function(element) {
-            element.toggleHighlight(true);
+        highlightElement: function(node) {
+            canvas.toggleElementHighlight(node, true);
         },
-        dimElement: function(element) {
-            element.toggleHighlight(false);
+        dimElement: function(node) {
+            canvas.toggleElementHighlight(node, false);
         },
-        jumpToElement: function(element) {
-            canvas.setCurrentElement(element);
+        jumpToElement: function(node) {
+            canvas.setCurrentElement(node);
         },
-        command: function(command, params) {
-            commands.run(command, params, canvas);
-            sandbox.publish('contentChanged');
+        onAfterActionExecuted: function(action, ret) {
+            if(ret && ret.isValid() && ret instanceof canvas.wlxmlDocument.NodeFragment) {
+                logger.debug('The action returned a valid fragment');
+                var params = {
+                    caretTo: ret instanceof canvas.wlxmlDocument.CaretFragment ? ret.offset : 'start'
+                };
+                canvas.setCurrentElement(ret.node, params);
+                return;
+            }
+            logger.debug('No valid fragment returned from the action');
+
+            (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
+                handler(canvas, action, ret);
+            });
         }
     };