86fe809628277481c9e77f02bd85835ac012b13d
[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     name: 'True or False',
18     createListView: function(listNode) {
19         return new ListView(this, listNode, {
20             onItemViewAdded: function(itemView) {
21                 var switchView = new Switch(itemView.node.getAttr('answer') === 'true', function(choice) {
22                     itemView.node.document.transaction(function() {
23                         itemView.node.getParent('exercise.choice').object.setAnswer(itemView.node, choice);
24                     }, {
25                         metadata: {
26                             description: gettext('Change answer')
27                         }
28                     });
29                 });
30                 itemView.addPrefixView(switchView);
31             }
32         });
33     }
34 });
35
36 var Switch = function(checked, onValueChange) {
37     this.dom = $(_.template(switchTemplate)());
38     var trueBtn = this.dom.find('.true'),
39         falseBtn = this.dom.find('.false');
40
41     trueBtn.on('click', function() {
42         trueBtn.addClass('selected');
43         falseBtn.removeClass('selected');
44         onValueChange(true);
45     });
46     this.dom.find('.false').on('click', function() {
47         falseBtn.addClass('selected');
48         trueBtn.removeClass('selected');
49         onValueChange(false);
50     });
51     trueBtn.toggleClass('selected', checked);
52     falseBtn.toggleClass('selected', !checked);
53 };
54
55 return trueOrFalse;
56
57 });