Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
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.
7
//
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.
12
//
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/>.
15
 
16
/**
17
 * JavaScript library for dealing with the question flags.
18
 *
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.
21
 *
22
 * @package    moodlecore
23
 * @subpackage questionengine
24
 * @copyright  2010 The Open University
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
M.core_question_flags = {
29
    flagattributes: null,
30
    actionurl: null,
31
    listeners: [],
32
 
33
    init: function(Y, actionurl, flagattributes) {
34
        M.core_question_flags.flagattributes = flagattributes;
35
        M.core_question_flags.actionurl = actionurl;
36
 
37
        Y.all('div.questionflag').each(function(flagdiv, i) {
38
            var checkbox = flagdiv.one('input[type=checkbox]');
39
            if (!checkbox) {
40
                return;
41
            }
42
 
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);
47
 
48
            var ariaPressed = checkbox.get('checked') ? 'true' : 'false';
49
            var toggle = Y.Node.create('<a ' +
50
                'tabindex="0" ' +
51
                'class="aabtn" ' +
52
                'role="button" ' +
53
                'aria-pressed="' + ariaPressed + '">' +
54
                    '.' +
55
                '</a>');
56
            M.core_question_flags.update_flag(input, toggle);
57
 
58
            checkbox.remove();
59
            flagdiv.one('label').remove();
60
            flagdiv.append(input);
61
            flagdiv.append(toggle);
62
        });
63
 
64
        Y.delegate('click', function(e) {
65
            e.halt();
66
            M.core_question_flags.process(this);
67
        }, document.body, 'div.questionflag');
68
        Y.delegate('key', function(e) {
69
            e.halt();
70
            if (e.keyCode == 13) {
71
                M.core_question_flags.process(this);
72
            }
73
        }, document.body, 'down:enter, space', 'div.questionflag');
74
        Y.delegate('key', function(e) {
75
            e.halt();
76
            M.core_question_flags.process(this);
77
        }, document.body, 'up:space', 'div.questionflag');
78
    },
79
 
80
    update_flag: function(input, toggle) {
81
        var value = input.get('value');
82
        toggle.setContent(
83
            '<img class="questionflagimage" src="' + M.core_question_flags.flagattributes[value].src + '" alt="" />' +
84
            M.core_question_flags.flagattributes[value].text
85
        );
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);
90
        } else {
91
            toggle.removeAttribute('title');
92
        }
93
    },
94
 
95
    /**
96
     * Process the change of flag status.
97
     *
98
     * @param {Y.Node} target The root element
99
     */
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') +
105
            input.get('value');
106
 
107
        Y.io(M.core_question_flags.actionurl, {method: 'POST', 'data': postdata});
108
        M.core_question_flags.fire_listeners(postdata);
109
    },
110
 
111
    add_listener: function(listener) {
112
        M.core_question_flags.listeners.push(listener);
113
    },
114
 
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]
121
            );
122
        }
123
    }
124
};