7ac04700ddc4bdc689627a3227fe984df3c0473e
[fnpeditor.git] / src / editor / plugins / core / edumed / choice / choiceTrueOrFalse.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     switchTemplate = require('libs/text!./trueOrFalseSwitch.html'),
11     ListView = require('./list');
12
13
14 var trueOrFalse  = Object.create(choiceBase);
15 _.extend(trueOrFalse, {
16     type: 'true-or-false',
17     createListView: function(listNode) {
18         return new ListView(this, listNode, {
19             onItemViewAdded: function(itemView) {
20                 var switchView = new Switch(itemView.node.getAttr('answer') === 'true', function(choice) {
21                     itemView.node.document.transaction(function() {
22                         itemView.node.getParent('exercise.choice').object.setAnswer(itemView.node, choice);
23                     }, {
24                         metadata: {
25                             description: gettext('Change answer')
26                         }
27                     });
28                 });
29                 itemView.addPrefixView(switchView);
30             }
31         });
32     }
33 });
34
35 var Switch = function(checked, onValueChange) {
36     this.dom = $(_.template(switchTemplate)());
37     var trueBtn = this.dom.find('.true'),
38         falseBtn = this.dom.find('.false');
39
40     trueBtn.on('click', function() {
41         trueBtn.addClass('selected');
42         falseBtn.removeClass('selected');
43         onValueChange(true);
44     });
45     this.dom.find('.false').on('click', function() {
46         falseBtn.addClass('selected');
47         trueBtn.removeClass('selected');
48         onValueChange(false);
49     });
50     trueBtn.toggleClass('selected', checked);
51     falseBtn.toggleClass('selected', !checked);
52 };
53
54 return trueOrFalse;
55
56 });