6824d44a56dbc295cd426f0abe0633abfa98aaaf
[fnpeditor.git] / src / editor / plugins / core / edumed / choice / choiceSingle.js
1 define(function(require) {
2     
3 'use strict';
4
5 /* globals gettext */
6
7 var $ = require('libs/jquery'),
8     _ = require('libs/underscore'),
9     choiceBase = require('./choiceBase'),
10     ListView = require('./list');
11
12
13 var choiceSingle = Object.create(choiceBase);
14 _.extend(choiceSingle, {
15     type: 'single',
16     name: gettext('Single Choice'),
17     init: function() {
18         this._comboName = _.uniqueId('edumed_exercise_hash_');
19         choiceBase.init.call(this);
20     },
21     createListView: function(listNode) {
22         var el = this;
23         return new ListView(this, listNode, {
24             onItemViewAdded: function(itemView) {
25                 var radiobox = new RadioView(itemView.node.getAttr('answer') === 'true', el._comboName, function() {
26                     itemView.node.document.transaction(function() {
27                         itemView.node.getParent('exercise.choice').object.markAsAnswer(itemView.node);
28                     }, {
29                         metadata: {
30                             description: gettext('Change answer')
31                         }
32                     });
33                     
34                 });
35                 itemView.addPrefixView(radiobox);
36             }
37         });
38     },
39     onNodeAttrChange: function(event) {
40         if(this.listView.listNode.sameNode(event.meta.node.parent())) {
41             this.listView.getItemView(event.meta.node).prefixView.dom.attr('checked', event.meta.newVal === 'true');
42         }
43     }
44 });
45
46 var RadioView = function(checked, name, onValueChange) {
47     this.dom = $('<input type="radio" style="float: left; margin-right: 40px;">')
48             .attr('checked', checked)
49             .attr('name', name);
50     this.dom.on('change', function() {
51         onValueChange(this.checked);
52     });
53 };
54
55 return choiceSingle;
56
57 });