smartxml: additional text nodes merge strategies for ElementNode.detach
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / genericElement.js
index 8d0ba87..4dfbe59 100644 (file)
@@ -6,7 +6,8 @@ var $ = require('libs/jquery'),
     _ = require('libs/underscore'),
     documentElement = require('./documentElement'),
     utils = require('./utils'),
     _ = require('libs/underscore'),
     documentElement = require('./documentElement'),
     utils = require('./utils'),
-    wlxmlUtils = require('utils/wlxml');
+    wlxmlUtils = require('utils/wlxml'),
+    CommentsView = require('./comments/comments');
 
 var labelWidget = function(tag, klass) {
     return $('<span>')
 
 var labelWidget = function(tag, klass) {
     return $('<span>')
@@ -31,6 +32,16 @@ $.extend(generic, {
                 this._container().append(el.dom);
             }
         }.bind(this));
                 this._container().append(el.dom);
             }
         }.bind(this));
+
+        this.commentsView = new CommentsView(this.wlxmlNode, this.canvas.metadata.user);
+        this.addToGutter(this.commentsView);
+        this.commentTip = $('<div class="comment-tip"><i class="icon-comment"></i></div>');
+        this.addWidget(this.commentTip);
+
+        if(!this.wlxmlNode.hasChild({klass: 'comment'})) {
+            this.commentTip.hide();
+        }
+
         this.refresh();
     },
     
         this.refresh();
     },
     
@@ -69,13 +80,18 @@ $.extend(generic, {
         return toret;
     },
 
         return toret;
     },
 
-    getVerticallyFirstTextElement: function() {
+    getVerticallyFirstTextElement: function(params) {
         var toret;
         var toret;
+        
+        params = _.extend({
+            considerChildren: true
+        }, params);
+        
         this.children().some(function(child) {
             if(child instanceof documentElement.DocumentTextElement) {
                 toret = child;
                 return true; // break
         this.children().some(function(child) {
             if(child instanceof documentElement.DocumentTextElement) {
                 toret = child;
                 return true; // break
-            } else {
+            } else if(params.considerChildren) {
                 toret = child.getVerticallyFirstTextElement();
                 if(toret) {
                     return true; // break
                 toret = child.getVerticallyFirstTextElement();
                 if(toret) {
                     return true; // break
@@ -119,8 +135,14 @@ $.extend(generic, {
         }
 
         referenceElement[referenceAction](actionArg);
         }
 
         referenceElement[referenceAction](actionArg);
+
+        if(event.meta.node.is('comment')) {
+            this.commentTip.show();
+            this.commentsView.render();
+        }
     },
     onNodeDetached: function(event) {
     },
     onNodeDetached: function(event) {
+        var isComment = event.meta.node.is('comment');
         if(event.meta.node.sameNode(this)) {
             this.detach();
         } else {
         if(event.meta.node.sameNode(this)) {
             this.detach();
         } else {
@@ -130,12 +152,19 @@ $.extend(generic, {
                     return true;
                 }
             });
                     return true;
                 }
             });
+            if(isComment && !this.wlxmlNode.hasChild({klass: 'comment'})) {
+                this.commentTip.hide();
+            }
+            this.commentsView.render();
         }
     },
     onNodeTextChange: function(event) {
         }
     },
     onNodeTextChange: function(event) {
-        var toSet = event.meta.node.getText();
-        this.children().some(function(child) {
-            if(child.wlxmlNode.sameNode(event.meta.node)) {
+        var node = event.meta.node,
+            toSet = node.getText(),
+            handled;
+        
+        handled = this.children().some(function(child) {
+            if(child.wlxmlNode.sameNode(node)) {
                 if(toSet === '') {
                     toSet = utils.unicode.ZWS;
                 }
                 if(toSet === '') {
                     toSet = utils.unicode.ZWS;
                 }
@@ -145,6 +174,10 @@ $.extend(generic, {
                 return true;
             }
         });
                 return true;
             }
         });
+
+        if(!handled && node.parent() && node.parent().is('comment') && this.wlxmlNode.sameNode(node.parent().parent())) {
+            this.commentsView.render();
+        }
     },
 
     onStateChange: function(changes) {
     },
 
     onStateChange: function(changes) {