wlxml: isContextRoot for the reminder of edumed exercises (fixup)
[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
30                 node.document.transaction(function() {
31                     var exerciseNode = node.after(node.document.edumedCreateExerciseNode(actionConfig.exercise)),
32                         doc = params.fragment.document,
33                         cursorTarget;
34
35                     //@@
36                     if(actionConfig.exercise === 'order') {
37                         exerciseNode.object.addItem(firstItemText);
38                         cursorTarget = exerciseNode.contents('.p')[0].append({text: descriptionText});
39                     } else if(_.contains(['gap', 'replace'], actionConfig.exercise)) {
40                         cursorTarget = _.first(exerciseNode.contents('.p'));
41                         if(cursorTarget) {
42                             cursorTarget = cursorTarget.append({text: ''});
43                         }
44                     } else if(actionConfig.exercise.split('.')[0] === 'choice') {
45                         _.first(exerciseNode.find('p')).append({text: descriptionText});
46                         cursorTarget = _.first(exerciseNode.find('item.answer'));
47                         if(cursorTarget) {
48                             cursorTarget = cursorTarget.append({text: ''});
49                         }
50                     }
51                     if(cursorTarget) {
52                         callback(doc.createFragment(doc.CaretFragment, {node: cursorTarget, offset: cursorTarget.getText().length}));
53                     }
54                 });
55                 
56             }
57         },
58         getState: function(params) {
59             return {
60                 allowed: params.fragment && params.fragment.isValid() && params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isInside('exercise'),
61                 description: gettext('Insert exercise: ' + actionConfig.exerciseTitle)
62             };
63         }
64     };
65
66 };
67
68 return [
69     createAction({name: 'insertOrderExercise', icon: 'random', exercise: 'order', exerciseTitle: gettext('Order')}),
70     createAction({name: 'insertChoiceSingleExercise', icon: 'ok-circle', exercise: 'choice.single', exerciseTitle: gettext('Single choice')}),
71     createAction({name: 'insertChoiceMultiExercise', icon: 'check', exercise: 'choice', exerciseTitle: gettext('Mutiple Choice')}),
72     createAction({name: 'insertChoiceTrueOrFalseExercise', icon: 'adjust', exercise: 'choice.true-or-false', exerciseTitle: gettext('True or False')}),
73     createAction({name: 'insertGapsExercise', icon: 'question-sign', exercise: 'gap', exerciseTitle: gettext('Gaps')}),
74     createAction({name: 'insertReplaceExercise', icon: 'thumbs-down', exercise: 'replace', exerciseTitle: gettext('Replace')})
75 ];
76
77 });