MDL-20636 Finish off converting question import.
[moodle.git] / question / flags.js
CommitLineData
ff065f96
TH
1// This script, and the YUI libraries that it needs, are inluded by
2// the $PAGE->requires->js calls in question_get_html_head_contributions in lib/questionlib.php.
3
4M.core_question_flags = {
5 flagattributes: null,
6 actionurl: null,
7 listeners: [],
8
9 init: function(Y, actionurl, flagattributes) {
10 M.core_question_flags.flagattributes = flagattributes;
11 M.core_question_flags.actionurl = actionurl;
12
13 Y.all('div.questionflag').each(function(flagdiv, i) {
14 var checkbox = flagdiv.one('input.questionflagcheckbox');
15 if (!checkbox) {
16 return;
17 }
18
19 var input = Y.Node.create('<input type="hidden" />');
20 input.set('id', checkbox.get('id'));
21 input.set('name', checkbox.get('name'));
22 input.set('value', checkbox.get('checked') ? 1 : 0);
23
24 // Create an image input to replace the img tag.
25 var image = Y.Node.create('<input type="image" class="questionflagimage" />');
26 M.core_question_flags.update_flag(input, image);
27
28 checkbox.remove();
29 flagdiv.one('label').remove();
30 flagdiv.append(input);
31 flagdiv.append(image);
32 });
33
34 Y.delegate('click', function(e) {
35 var input = this.previous('input');
36 input.set('value', 1 - input.get('value'));
37 M.core_question_flags.update_flag(input, this);
38 var postdata = this.previous('input.questionflagpostdata').get('value') +
2be77520 39 input.get('value');
ff065f96
TH
40
41 e.halt();
42 Y.io(M.core_question_flags.actionurl , {method: 'POST', 'data': postdata});
43 M.core_question_flags.fire_listeners(postdata);
44 }, document.body, 'input.questionflagimage');
ff065f96
TH
45 },
46
47 update_flag: function(input, image) {
48 image.setAttrs(M.core_question_flags.flagattributes[input.get('value')]);
49 },
50
51 add_listener: function(listener) {
52 M.core_question_flags.listeners.push(listener);
53 },
54
55 fire_listeners: function(postdata) {
56 for (var i = 0; i < M.core_question_flags.listeners.length; i++) {
57 M.core_question_flags.listeners[i](
06f8ed54
TH
58 postdata.match(/\bqubaid=(\d+)\b/)[1],
59 postdata.match(/\bslot=(\d+)\b/)[1],
ff065f96
TH
60 postdata.match(/\bnewstate=(\d+)\b/)[1]
61 );
62 }
63 }
64};