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
 * AMD code for the frequently used comments chooser for the marking guide grading form.
18
 *
19
 * @module     gradingform_guide/comment_chooser
20
 * @copyright  2015 Jun Pataleta <jun@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
11 efrain 23
define(['jquery', 'core/templates', 'core/key_codes', 'core/notification', 'core/yui'],
24
        function($, templates, keycodes, notification) {
1 efrain 25
 
26
    // Private variables and functions.
27
 
28
    return /** @alias module:gradingform_guide/comment_chooser */ {
29
        // Public variables and functions.
30
        /**
31
         * Initialises the module.
32
         *
33
         * Basically, it performs the binding and handling of the button click event for
34
         * the 'Insert frequently used comment' button.
35
         *
36
         * @param {Integer} criterionId The criterion ID.
37
         * @param {String} buttonId The element ID of the button which the handler will be bound to.
38
         * @param {String} remarkId The element ID of the remark text area where the text of the selected comment will be copied to.
39
         * @param {Array} commentOptions The array of frequently used comments to be used as options.
40
         */
41
        initialise: function(criterionId, buttonId, remarkId, commentOptions) {
42
            /**
43
             * Display the chooser dialog using the compiled HTML from the mustache template
44
             * and binds onclick events for the generated comment options.
45
             *
46
             * @param {String} compiledSource The compiled HTML from the mustache template
47
             * @param {Array} comments Array containing comments.
48
             */
49
            function displayChooserDialog(compiledSource, comments) {
50
                var titleLabel = '<label>' + M.util.get_string('insertcomment', 'gradingform_guide') + '</label>';
51
                var cancelButtonId = 'comment-chooser-' + criterionId + '-cancel';
52
                var cancelButton = '<button id="' + cancelButtonId + '">' + M.util.get_string('cancel', 'moodle') + '</button>';
53
 
54
                // Set dialog's body content.
55
                var chooserDialog = new M.core.dialogue({
56
                    modal: true,
57
                    headerContent: titleLabel,
58
                    bodyContent: compiledSource,
59
                    footerContent: cancelButton,
60
                    focusAfterHide: '#' + remarkId,
61
                    id: "comments-chooser-dialog-" + criterionId
62
                });
63
 
64
                // Bind click event to the cancel button.
65
                $("#" + cancelButtonId).click(function() {
66
                    chooserDialog.hide();
67
                });
68
 
69
                // Loop over each comment item and bind click events.
70
                $.each(comments, function(index, comment) {
71
                    var commentOptionId = '#comment-option-' + criterionId + '-' + comment.id;
72
 
73
                    // Delegate click event for the generated option link.
74
                    $(commentOptionId).click(function() {
75
                        var remarkTextArea = $('#' + remarkId);
76
                        var remarkText = remarkTextArea.val();
77
 
78
                        // Add line break if the current value of the remark text is not empty.
79
                        if (remarkText.trim() !== '') {
80
                            remarkText += '\n';
81
                        }
82
                        remarkText += comment.description;
83
 
84
                        remarkTextArea.val(remarkText);
85
 
86
                        chooserDialog.hide();
87
                    });
88
 
89
                    // Handle keypress on list items.
90
                    $(document).off('keypress', commentOptionId).on('keypress', commentOptionId, function() {
91
                        var keyCode = event.which || event.keyCode;
92
 
11 efrain 93
                        // Trigger click event when user presses space.
94
                        if (keyCode === keycodes.space) {
1 efrain 95
                            $(commentOptionId).click();
96
                        }
97
                    });
98
                });
99
 
100
                // Destroy the dialog when it is hidden to allow the grading section to
101
                // be loaded as a fragment multiple times within the same page.
102
                chooserDialog.after('visibleChange', function(e) {
103
                    // Going from visible to hidden.
104
                    if (e.prevVal && !e.newVal) {
105
                        this.destroy();
106
                    }
107
                }, chooserDialog);
108
 
109
                // Show dialog.
110
                chooserDialog.show();
111
            }
112
 
113
            /**
114
             * Generates the comments chooser dialog from the grading_form/comment_chooser mustache template.
115
             */
116
            function generateCommentsChooser() {
117
                // Template context.
118
                var context = {
119
                    criterionId: criterionId,
120
                    comments: commentOptions
121
                };
122
 
123
                // Render the template and display the comment chooser dialog.
124
                templates.render('gradingform_guide/comment_chooser', context)
125
                    .done(function(compiledSource) {
126
                        displayChooserDialog(compiledSource, commentOptions);
127
                    })
128
                    .fail(notification.exception);
129
            }
130
 
131
            // Bind click event for the comments chooser button.
132
            $("#" + buttonId).click(function(e) {
133
                e.preventDefault();
134
                generateCommentsChooser();
135
            });
136
        }
137
    };
138
});