mirrored from git://git.moodle.org/moodle.git
/
comment_chooser.js
139 lines (122 loc) · 6.1 KB
/
comment_chooser.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* AMD code for the frequently used comments chooser for the marking guide grading form.
*
* @module gradingform_guide/comment_chooser
* @class comment_chooser
* @package core
* @copyright 2015 Jun Pataleta <jun@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/templates', 'core/notification', 'core/yui'], function ($, templates, notification) {
// Private variables and functions.
return /** @alias module:gradingform_guide/comment_chooser */ {
// Public variables and functions.
/**
* Initialises the module.
*
* Basically, it performs the binding and handling of the button click event for
* the 'Insert frequently used comment' button.
*
* @param criterionId The criterion ID.
* @param buttonId The element ID of the button which the handler will be bound to.
* @param remarkId The element ID of the remark text area where the text of the selected comment will be copied to.
* @param commentOptions The array of frequently used comments to be used as options.
*/
initialise: function (criterionId, buttonId, remarkId, commentOptions) {
var chooserDialog;
/**
* Display the chooser dialog using the compiled HTML from the mustache template
* and binds onclick events for the generated comment options.
*
* @param compiledSource The compiled HTML from the mustache template
* @param comments Array containing comments.
*/
function displayChooserDialog(compiledSource, comments) {
var titleLabel = '<label>' + M.util.get_string('insertcomment', 'gradingform_guide') + '</label>';
var cancelButtonId = 'comment-chooser-' + criterionId + '-cancel';
var cancelButton = '<button id="' + cancelButtonId + '">' + M.util.get_string('cancel', 'moodle') + '</button>';
if (typeof chooserDialog === 'undefined') {
// Set dialog's body content.
chooserDialog = new M.core.dialogue({
modal: true,
headerContent: titleLabel,
bodyContent: compiledSource,
footerContent: cancelButton,
focusAfterHide: '#' + remarkId,
id: "comments-chooser-dialog-" + criterionId
});
// Bind click event to the cancel button.
$("#" + cancelButtonId).click(function() {
if (typeof chooserDialog !== 'undefined') {
chooserDialog.hide();
}
});
// Loop over each comment item and bind click events.
$.each(comments, function (index, comment) {
var commentOptionId = '#comment-option-' + criterionId + '-' + comment.id;
// Delegate click event for the generated option link.
$(commentOptionId).click(function () {
var remarkTextArea = $('#' + remarkId);
var remarkText = remarkTextArea.val();
// Add line break if the current value of the remark text is not empty.
if ($.trim(remarkText) !== '') {
remarkText += '\n';
}
remarkText += comment.description;
remarkTextArea.val(remarkText);
if (typeof chooserDialog !== 'undefined') {
chooserDialog.hide();
}
});
// Handle keypress on list items.
$(document).off('keypress', commentOptionId).on('keypress', commentOptionId, function () {
var keyCode = event.which || event.keyCode;
// Enter or space key.
if (keyCode == 13 || keyCode == 32) {
// Trigger click event.
$(commentOptionId).click();
}
});
});
}
// Show dialog.
chooserDialog.show();
}
/**
* Generates the comments chooser dialog from the grading_form/comment_chooser mustache template.
*/
function generateCommentsChooser() {
// Template context.
var context = {
criterionId: criterionId,
comments: commentOptions
};
// Render the template and display the comment chooser dialog.
templates.render('gradingform_guide/comment_chooser', context)
.done(function (compiledSource) {
displayChooserDialog(compiledSource, commentOptions);
})
.fail(notification.exception);
}
// Bind click event for the comments chooser button.
$("#" + buttonId).click(function (e) {
e.preventDefault();
generateCommentsChooser();
});
}
};
});