template get state fix
[fnpeditor.git] / src / editor / plugins / core / templates.js
1 define(function() {
2     
3 'use strict';
4 /* globals gettext, interpolate */
5
6
7 var insertTemplateAction = {
8     name: 'template',
9     params: {
10         fragment: {type: 'context', name: 'fragment'},
11         template: {type: 'select', options: []},
12         ctrl: {type: 'key', key: 'ctrl'}
13     },
14     stateDefaults: {
15         label: '+',
16         icon: 'core.plus'
17     },
18     getState: function(params) {
19         if(!(params.template && params.template.id)) {
20             return {
21                 allowed: false,
22                 description: gettext('No template selected')
23             };
24         } else if(
25             !params.fragment || !params.fragment.isValid() ||
26             !(params.fragment instanceof params.fragment.NodeFragment) ||
27             params.fragment.node.getNearestElementNode().isRoot()
28             ) {
29                 return {
30                     allowed: false,
31                     description: gettext('Wrong node selected')
32             };
33         }
34         return {
35             allowed: true,
36             description: interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()]),
37             execute: function(params) {
38                 var node = params.fragment.node.getNearestElementNode();
39                 var toAdd = node.document.createDocumentNode(params.template.content);
40                 node.after(toAdd);
41             }
42         };
43     }
44 };
45
46
47 return {
48     actions: [insertTemplateAction]
49 };
50
51 });