1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * JavaScript library for dealing with the question flags.
19 * This script, and the YUI libraries that it needs, are inluded by
20 * the $PAGE->requires->js calls in question_get_html_head_contributions in lib/questionlib.php.
23 * @subpackage questionengine
24 * @copyright 2010 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 M.core_question_flags = {
33 init: function(Y, actionurl, flagattributes) {
34 M.core_question_flags.flagattributes = flagattributes;
35 M.core_question_flags.actionurl = actionurl;
37 Y.all('div.questionflag').each(function(flagdiv, i) {
38 var checkbox = flagdiv.one('input[type=checkbox]');
43 var input = Y.Node.create('<input type="hidden" class="questionflagvalue" />');
44 input.set('id', checkbox.get('id'));
45 input.set('name', checkbox.get('name'));
46 input.set('value', checkbox.get('checked') ? 1 : 0);
48 var ariaPressed = checkbox.get('checked') ? 'true' : 'false';
49 var toggle = Y.Node.create('<a ' +
53 'aria-pressed="' + ariaPressed + '">' +
56 M.core_question_flags.update_flag(input, toggle);
59 flagdiv.one('label').remove();
60 flagdiv.append(input);
61 flagdiv.append(toggle);
64 Y.delegate('click', function(e) {
66 M.core_question_flags.process(this);
67 }, document.body, 'div.questionflag');
68 Y.delegate('key', function(e) {
70 if (e.keyCode == 13) {
71 M.core_question_flags.process(this);
73 }, document.body, 'down:enter, space', 'div.questionflag');
74 Y.delegate('key', function(e) {
76 M.core_question_flags.process(this);
77 }, document.body, 'up:space', 'div.questionflag');
80 update_flag: function(input, toggle) {
81 var value = input.get('value');
83 '<img class="questionflagimage" src="' + M.core_question_flags.flagattributes[value].src + '" alt="" />' +
84 M.core_question_flags.flagattributes[value].text
86 toggle.set('aria-pressed', parseInt(value) ? 'true' : 'false');
87 toggle.set('aria-label', M.core_question_flags.flagattributes[value].alt);
88 if (M.core_question_flags.flagattributes[value].title != M.core_question_flags.flagattributes[value].text) {
89 toggle.set('title', M.core_question_flags.flagattributes[value].title);
91 toggle.removeAttribute('title');
96 * Process the change of flag status.
98 * @param {Y.Node} target The root element
100 process: function(target) {
101 var input = target.one('input.questionflagvalue');
102 input.set('value', 1 - input.get('value'));
103 M.core_question_flags.update_flag(input, target.one('[aria-pressed]'));
104 var postdata = target.one('input.questionflagpostdata').get('value') +
107 Y.io(M.core_question_flags.actionurl, {method: 'POST', 'data': postdata});
108 M.core_question_flags.fire_listeners(postdata);
111 add_listener: function(listener) {
112 M.core_question_flags.listeners.push(listener);
115 fire_listeners: function(postdata) {
116 for (var i = 0; i < M.core_question_flags.listeners.length; i++) {
117 M.core_question_flags.listeners[i](
118 postdata.match(/\bqubaid=(\d+)\b/)[1],
119 postdata.match(/\bslot=(\d+)\b/)[1],
120 postdata.match(/\bnewstate=(\d+)\b/)[1]