editor: actions implementation
[fnpeditor.git] / src / editor / plugins / core / templates.js
diff --git a/src/editor/plugins/core/templates.js b/src/editor/plugins/core/templates.js
new file mode 100644 (file)
index 0000000..2b1f730
--- /dev/null
@@ -0,0 +1,47 @@
+define(function() {
+    
+'use strict';
+/* globals gettext, interpolate */
+
+
+var insertTemplateAction = {
+    name: 'template',
+    params: {
+        fragment: {type: 'context', name: 'fragment'},
+        template: {type: 'select', options: []},
+        ctrl: {type: 'key', key: 'ctrl'}
+    },
+    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);
+        }
+    },
+    getState: function(params) {
+        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')
+            };
+        }
+        return {
+            allowed: true,
+            description: interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()])
+        };
+    }
+};
+
+
+return {
+    actions: [insertTemplateAction]
+};
+
+});
\ No newline at end of file