list fixes: headers, invalid elements, easier ending
[fnpeditor.git] / src / editor / plugins / core / edumed / actions.js
1 define(function(require) {
2     
3 'use strict';
4
5 /* globals gettext */
6
7 var _ = require('libs/underscore');
8
9 var descriptionText = gettext('Description goes here') + '...',
10     firstItemText = gettext('First item') + '...';
11
12 var createAction = function(actionConfig) {
13
14     return {
15         name: actionConfig.name,
16         params: {
17             fragment: {type: 'context', name: 'fragment'}
18         },
19         stateDefaults: {
20             icon: actionConfig.icon || null,
21             execute: function(callback, params) {
22                 /* globals Node */
23                 void(callback);
24
25                 var node = params.fragment.node;
26                 if(node.nodeType === Node.TEXT_NODE) {
27                     node = node.parent();
28                 }
29                 if(node.isInside('exercise')) {
30                     node = node.getParent('exercise');
31                 }
32
33                 node.document.transaction(function() {
34                     var exerciseNode = node.after(node.document.edumedCreateExerciseNode(actionConfig.exercise)),
35                         doc = params.fragment.document,
36                         cursorTarget;
37
38                     //@@
39                     if(actionConfig.exercise === 'order') {
40                         exerciseNode.object.addItem(firstItemText);
41                         cursorTarget = exerciseNode.contents('.p')[0].append({text: descriptionText});
42                     } else if(_.contains(['gap', 'replace'], actionConfig.exercise)) {
43                         cursorTarget = _.first(exerciseNode.contents('.p'));
44                         if(cursorTarget) {
45                             cursorTarget = cursorTarget.append({text: ''});
46                         }
47                     } else if(actionConfig.exercise.split('.')[0] === 'choice') {
48                         _.first(exerciseNode.find('p')).append({text: descriptionText});
49                         cursorTarget = _.first(exerciseNode.find('item.answer'));
50                         if(cursorTarget) {
51                             cursorTarget = cursorTarget.append({text: ''});
52                         }
53                     }
54                     if(cursorTarget) {
55                         callback(doc.createFragment(doc.CaretFragment, {node: cursorTarget, offset: cursorTarget.getText().length}));
56                     }
57                 });
58                 
59             }
60         },
61         getState: function(params) {
62             return {
63                 allowed: params.fragment && params.fragment.isValid() && params.fragment instanceof params.fragment.NodeFragment,
64                 description: gettext('Insert exercise') + ': ' + actionConfig.exerciseTitle
65             };
66         }
67     };
68
69 };
70
71 return [
72     createAction({name: 'insertOrderExercise', icon: 'random', exercise: 'order', exerciseTitle: gettext('Order')}),
73     createAction({name: 'insertChoiceSingleExercise', icon: 'ok-circle', exercise: 'choice.single', exerciseTitle: gettext('Single Choice')}),
74     createAction({name: 'insertChoiceMultiExercise', icon: 'check', exercise: 'choice', exerciseTitle: gettext('Multiple Choice')}),
75     createAction({name: 'insertChoiceTrueOrFalseExercise', icon: 'adjust', exercise: 'choice.true-or-false', exerciseTitle: gettext('True or False')}),
76     createAction({name: 'insertGapsExercise', icon: 'question-sign', exercise: 'gap', exerciseTitle: gettext('Gaps')}),
77     createAction({name: 'insertReplaceExercise', icon: 'thumbs-down', exercise: 'replace', exerciseTitle: gettext('Replace')})
78 ];
79
80 });