Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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: [],
11 efrain 32
    editableSelector: 'div.questionflag.editable',
1 efrain 33
 
34
    init: function(Y, actionurl, flagattributes) {
35
        M.core_question_flags.flagattributes = flagattributes;
36
        M.core_question_flags.actionurl = actionurl;
37
 
11 efrain 38
        Y.all(M.core_question_flags.editableSelector).each(function(flagdiv) {
1 efrain 39
            var checkbox = flagdiv.one('input[type=checkbox]');
40
            if (!checkbox) {
41
                return;
42
            }
43
 
44
            var input = Y.Node.create('<input type="hidden" class="questionflagvalue" />');
45
            input.set('id', checkbox.get('id'));
46
            input.set('name', checkbox.get('name'));
47
            input.set('value', checkbox.get('checked') ? 1 : 0);
48
 
49
            var ariaPressed = checkbox.get('checked') ? 'true' : 'false';
50
            var toggle = Y.Node.create('<a ' +
51
                'tabindex="0" ' +
52
                'class="aabtn" ' +
53
                'role="button" ' +
54
                'aria-pressed="' + ariaPressed + '">' +
55
                    '.' +
56
                '</a>');
57
            M.core_question_flags.update_flag(input, toggle);
58
 
59
            checkbox.remove();
60
            flagdiv.one('label').remove();
61
            flagdiv.append(input);
62
            flagdiv.append(toggle);
63
        });
64
 
65
        Y.delegate('click', function(e) {
66
            e.halt();
67
            M.core_question_flags.process(this);
11 efrain 68
        }, document.body, M.core_question_flags.editableSelector);
1 efrain 69
        Y.delegate('key', function(e) {
70
            e.halt();
71
            if (e.keyCode == 13) {
72
                M.core_question_flags.process(this);
73
            }
11 efrain 74
        }, document.body, 'down:enter, space', M.core_question_flags.editableSelector);
1 efrain 75
        Y.delegate('key', function(e) {
76
            e.halt();
77
            M.core_question_flags.process(this);
11 efrain 78
        }, document.body, 'up:space', M.core_question_flags.editableSelector);
1 efrain 79
    },
80
 
81
    update_flag: function(input, toggle) {
82
        var value = input.get('value');
83
        toggle.setContent(
84
            '<img class="questionflagimage" src="' + M.core_question_flags.flagattributes[value].src + '" alt="" />' +
85
            M.core_question_flags.flagattributes[value].text
86
        );
87
        toggle.set('aria-pressed', parseInt(value) ? 'true' : 'false');
88
        toggle.set('aria-label', M.core_question_flags.flagattributes[value].alt);
89
        if (M.core_question_flags.flagattributes[value].title != M.core_question_flags.flagattributes[value].text) {
90
            toggle.set('title', M.core_question_flags.flagattributes[value].title);
91
        } else {
92
            toggle.removeAttribute('title');
93
        }
94
    },
95
 
96
    /**
97
     * Process the change of flag status.
98
     *
99
     * @param {Y.Node} target The root element
100
     */
101
    process: function(target) {
102
        var input = target.one('input.questionflagvalue');
103
        input.set('value', 1 - input.get('value'));
104
        M.core_question_flags.update_flag(input, target.one('[aria-pressed]'));
105
        var postdata = target.one('input.questionflagpostdata').get('value') +
106
            input.get('value');
107
 
108
        Y.io(M.core_question_flags.actionurl, {method: 'POST', 'data': postdata});
109
        M.core_question_flags.fire_listeners(postdata);
110
    },
111
 
112
    add_listener: function(listener) {
113
        M.core_question_flags.listeners.push(listener);
114
    },
115
 
116
    fire_listeners: function(postdata) {
117
        for (var i = 0; i < M.core_question_flags.listeners.length; i++) {
118
            M.core_question_flags.listeners[i](
119
                postdata.match(/\bqubaid=(\d+)\b/)[1],
120
                postdata.match(/\bslot=(\d+)\b/)[1],
121
                postdata.match(/\bnewstate=(\d+)\b/)[1]
122
            );
123
        }
124
    }
125
};