template get state fix
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 17 Apr 2014 10:36:13 +0000 (12:36 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Apr 2014 11:05:05 +0000 (13:05 +0200)
src/editor/plugins/core/templates.js

index 2b1f730..1114258 100644 (file)
@@ -13,12 +13,7 @@ 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) {
         if(!(params.template && params.template.id)) {
@@ -26,15 +21,24 @@ var insertTemplateAction = {
                 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')
             };
         }
         return {
             allowed: true,
-            description: interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()])
+            description: interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()]),
+            execute: function(params) {
+                var node = params.fragment.node.getNearestElementNode();
+                var toAdd = node.document.createDocumentNode(params.template.content);
+                node.after(toAdd);
+            }
         };
     }
 };