9346363f281b1e6175b9b5525bc8e6497394c557
[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     init: function() {
17         this._comboName = _.uniqueId('edumed_exercise_hash_');
18         choiceBase.init.call(this);
19     },
20     createListView: function(listNode) {
21         var el = this;
22         return new ListView(this, listNode, {
23             onItemViewAdded: function(itemView) {
24                 var radiobox = new RadioView(itemView.node.getAttr('answer') === 'true', el._comboName, function() {
25                     itemView.node.document.transaction(function() {
26                         itemView.node.getParent('exercise.choice').object.markAsAnswer(itemView.node);
27                     }, {
28                         metadata: {
29                             description: gettext('Change answer')
30                         }
31                     });
32                     
33                 });
34                 itemView.addPrefixView(radiobox);
35             }
36         });
37     },
38     onNodeAttrChange: function(event) {
39         if(this.listView.listNode.sameNode(event.meta.node.parent())) {
40             this.listView.getItemView(event.meta.node).prefixView.dom.attr('checked', event.meta.newVal === 'true');
41         }
42     }
43 });
44
45 var RadioView = function(checked, name, onValueChange) {
46     this.dom = $('<input type="radio" style="float: left; margin-right: 40px;">')
47             .attr('checked', checked)
48             .attr('name', name);
49     this.dom.on('change', function() {
50         onValueChange(this.checked);
51     });
52 };
53
54 return choiceSingle;
55
56 });