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