fnpjs: Action now triggers actionExecuted event instead of using callback
[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         var description;
20
21         if(!(params.template && params.template.id)) {
22             return {
23                 allowed: false,
24                 description: gettext('No template selected')
25             };
26         } else if(
27             !params.fragment || !params.fragment.isValid() ||
28             !(params.fragment instanceof params.fragment.NodeFragment) ||
29             params.fragment.node.getNearestElementNode().isRoot()
30             ) {
31                 return {
32                     allowed: false,
33                     description: gettext('Wrong node selected')
34             };
35         }
36
37         description = interpolate(gettext('Insert template %s'), [params.template.name]);
38         return {
39             allowed: true,
40             description: description,
41             execute: function(callback, params) {
42                 var node = params.fragment.node.getNearestElementNode();
43                 node.document.transaction(function() {
44                     var toAdd = node.document.createDocumentNode(params.template.content);
45                     node.after(toAdd);
46                 }, {
47                     metadata: {
48                         description: description
49                     },
50                     success: callback
51                 });
52             }
53         };
54     }
55 };
56
57
58 return {
59     actions: [insertTemplateAction]
60 };
61
62 });