a05346cbe9fea5d06bd8e68c7b9d95e68b52d59a
[fnpeditor.git] / src / editor / plugins / core / edumed / choice / choiceMulti.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 choiceMulti = Object.create(choiceBase);
14 _.extend(choiceMulti, {
15     type: 'multi',
16     createListView: function(listNode) {
17         return new ListView(this, listNode, {
18             onItemViewAdded: function(itemView) {
19                 var checkbox = new CheckboxView(itemView.node.getAttr('answer') === 'true', function(checked) {
20                     itemView.node.document.transaction(function() {
21                         itemView.node.getParent('exercise.choice').object.setAnswer(itemView.node, checked);
22                     }, {
23                         metadata: {
24                             description: gettext('Change answer')
25                         }
26                     });
27                 });
28                 itemView.addPrefixView(checkbox);
29             }
30         });
31     },
32     onNodeAttrChange: function(event) {
33         if(this.listView.listNode.sameNode(event.meta.node.parent())) {
34             this.listView.getItemView(event.meta.node).prefixView.dom.attr('checked', event.meta.newVal === 'true');
35         }
36     }
37 });
38
39 var CheckboxView = function(checked, onValueChange) {
40     this.dom = $('<input type="checkbox">')
41             .attr('checked', checked);
42     this.dom.on('click', function() {
43         onValueChange(this.checked);
44     });
45 };
46
47 return choiceMulti;
48
49 });