smartxml: Getting rid of nodeMoved event in favor of `move` flag on nodeDetached...
[fnpeditor.git] / src / editor / plugins / core / templates.js
index 2b1f730..bca8467 100644 (file)
@@ -13,28 +13,43 @@ var insertTemplateAction = {
     },
     stateDefaults: {
         label: '+',
-        icon: 'core.plus',
-        execute: function(params) {
-            var node = params.fragment.node.getNearestElementNode();
-            var toAdd = node.document.createDocumentNode(params.template.content);
-            node.after(toAdd);
-        }
+        icon: 'core.plus'
     },
     getState: function(params) {
+        var description;
+
         if(!(params.template && params.template.id)) {
             return {
                 allowed: false,
                 description: gettext('No template selected')
             };
-        } else if(!params.fragment || !params.fragment.isValid() || !(params.fragment instanceof params.fragment.NodeFragment)) {
-            return {
-                allowed: false,
-                description: gettext('Wrong node selected')
+        } else if(
+            !params.fragment || !params.fragment.isValid() ||
+            !(params.fragment instanceof params.fragment.NodeFragment) ||
+            params.fragment.node.getNearestElementNode().isRoot()
+            ) {
+                return {
+                    allowed: false,
+                    description: gettext('Wrong node selected')
             };
         }
+
+        description = interpolate(gettext('Insert template %s'), [params.template.name]);
         return {
             allowed: true,
-            description: interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()])
+            description: description,
+            execute: function(callback, params) {
+                var node = params.fragment.node.getNearestElementNode();
+                node.document.transaction(function() {
+                    var toAdd = node.document.createDocumentNode(params.template.content);
+                    node.after(toAdd);
+                }, {
+                    metadata: {
+                        description: description
+                    },
+                    success: callback
+                });
+            }
         };
     }
 };