1 |
efrain |
1 |
M.gradingform_guideeditor = {'templates' : {}, 'eventhandler' : null, 'name' : null, 'Y' : null};
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This function is called for each guideeditor on page.
|
|
|
5 |
*/
|
|
|
6 |
M.gradingform_guideeditor.init = function(Y, options) {
|
|
|
7 |
M.gradingform_guideeditor.name = options.name
|
|
|
8 |
M.gradingform_guideeditor.Y = Y
|
|
|
9 |
M.gradingform_guideeditor.templates[options.name] = {
|
|
|
10 |
'criterion' : options.criteriontemplate,
|
|
|
11 |
'comment' : options.commenttemplate
|
|
|
12 |
}
|
|
|
13 |
M.gradingform_guideeditor.disablealleditors()
|
|
|
14 |
Y.on('click', M.gradingform_guideeditor.clickanywhere, 'body', null)
|
|
|
15 |
YUI().use('event-touch', function (Y) {
|
|
|
16 |
Y.one('body').on('touchstart', M.gradingform_guideeditor.clickanywhere);
|
|
|
17 |
Y.one('body').on('touchend', M.gradingform_guideeditor.clickanywhere);
|
|
|
18 |
})
|
|
|
19 |
M.gradingform_guideeditor.addhandlers()
|
|
|
20 |
};
|
|
|
21 |
|
|
|
22 |
// Adds handlers for clicking submit button. This function must be called each time JS adds new elements to html
|
|
|
23 |
M.gradingform_guideeditor.addhandlers = function() {
|
|
|
24 |
var Y = M.gradingform_guideeditor.Y
|
|
|
25 |
var name = M.gradingform_guideeditor.name
|
|
|
26 |
if (M.gradingform_guideeditor.eventhandler) {
|
|
|
27 |
M.gradingform_guideeditor.eventhandler.detach()
|
|
|
28 |
}
|
|
|
29 |
M.gradingform_guideeditor.eventhandler = Y.on('click', M.gradingform_guideeditor.buttonclick, '#guide-'+name+' input[type=submit]', null);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
// switches all input text elements to non-edit mode
|
|
|
33 |
M.gradingform_guideeditor.disablealleditors = function() {
|
|
|
34 |
var Y = M.gradingform_guideeditor.Y
|
|
|
35 |
var name = M.gradingform_guideeditor.name
|
|
|
36 |
Y.all('#guide-'+name+' .criteria .description input[type=text]:not(.pseudotablink)').each( function(node) {M.gradingform_guideeditor.editmode(node, false)} );
|
|
|
37 |
Y.all('#guide-'+name+' .criteria .description textarea').each( function(node) {M.gradingform_guideeditor.editmode(node, false)} );
|
|
|
38 |
Y.all('#guide-'+name+' .comments .description textarea').each( function(node) {M.gradingform_guideeditor.editmode(node, false)} );
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
// function invoked on each click on the page. If criterion values are clicked
|
|
|
42 |
// it switches the element to edit mode. If guide button is clicked it does nothing so the 'buttonclick'
|
|
|
43 |
// function is invoked
|
|
|
44 |
M.gradingform_guideeditor.clickanywhere = function(e) {
|
|
|
45 |
if (e.type == 'touchstart') {
|
|
|
46 |
return
|
|
|
47 |
}
|
|
|
48 |
var el = e.target
|
|
|
49 |
// if clicked on button - disablecurrenteditor, continue
|
|
|
50 |
if (el.get('tagName') == 'INPUT' && el.get('type') == 'submit') {
|
|
|
51 |
return
|
|
|
52 |
}
|
|
|
53 |
// if clicked on description item and this item is not enabled - enable it
|
|
|
54 |
var container = null
|
|
|
55 |
if ((container = el.ancestor('.criterionname')) || (container = el.ancestor('.criterionmaxscore'))) {
|
|
|
56 |
el = container.one('input[type=text]')
|
|
|
57 |
} else if ((container = el.ancestor('.criteriondesc')) || (container = el.ancestor('.criteriondescmarkers'))) {
|
|
|
58 |
el = container.one('textarea')
|
|
|
59 |
} else {
|
|
|
60 |
el = null
|
|
|
61 |
}
|
|
|
62 |
if (el) {
|
|
|
63 |
if (el.hasClass('hiddenelement')) {
|
|
|
64 |
M.gradingform_guideeditor.disablealleditors()
|
|
|
65 |
M.gradingform_guideeditor.editmode(el, true)
|
|
|
66 |
}
|
|
|
67 |
return
|
|
|
68 |
}
|
|
|
69 |
// else disablecurrenteditor
|
|
|
70 |
M.gradingform_guideeditor.disablealleditors()
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// switch the criterion item to edit mode or switch back
|
|
|
74 |
M.gradingform_guideeditor.editmode = function(el, editmode) {
|
|
|
75 |
var Y = M.gradingform_guideeditor.Y
|
|
|
76 |
var ta = el
|
|
|
77 |
if (!editmode && ta.hasClass('hiddenelement')) {
|
|
|
78 |
return;
|
|
|
79 |
}
|
|
|
80 |
if (editmode && !ta.hasClass('hiddenelement')) {
|
|
|
81 |
return;
|
|
|
82 |
}
|
|
|
83 |
var pseudotablink = '<span class="pseudotablink" tabindex="0"></span>',
|
|
|
84 |
taplain = ta.next('.plainvalue'),
|
|
|
85 |
tbplain = null,
|
|
|
86 |
tb = el.one('.score input[type=text]')
|
|
|
87 |
// add 'plainvalue' next to textarea for description/definition and next to input text field for score (if applicable)
|
|
|
88 |
if (!taplain && ta.get('name') != '') {
|
|
|
89 |
ta.insert('<div class="plainvalue">'+pseudotablink+'<span class="textvalue"> </span></div>', 'after')
|
|
|
90 |
taplain = ta.next('.plainvalue')
|
|
|
91 |
taplain.one('.pseudotablink').on('focus', M.gradingform_guideeditor.clickanywhere)
|
|
|
92 |
if (tb) {
|
|
|
93 |
tb.get('parentNode').append('<span class="plainvalue">'+pseudotablink+'<span class="textvalue"> </span></span>')
|
|
|
94 |
tbplain = tb.get('parentNode').one('.plainvalue')
|
|
|
95 |
tbplain.one('.pseudotablink').on('focus', M.gradingform_guideeditor.clickanywhere)
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
if (tb && !tbplain) {
|
|
|
99 |
tbplain = tb.get('parentNode').one('.plainvalue')
|
|
|
100 |
}
|
|
|
101 |
if (!editmode) {
|
|
|
102 |
// if we need to hide the input fields, copy their contents to plainvalue(s). If description/definition
|
|
|
103 |
// is empty, display the default text ('Click to edit ...') and add/remove 'empty' CSS class to element
|
|
|
104 |
var value = Y.Lang.trim(ta.get('value'));
|
|
|
105 |
if (value.length) {
|
|
|
106 |
taplain.removeClass('empty')
|
|
|
107 |
} else if (ta.get('name').indexOf('[shortname]') > 1){
|
|
|
108 |
value = M.util.get_string('clicktoeditname', 'gradingform_guide')
|
|
|
109 |
taplain.addClass('editname')
|
|
|
110 |
} else {
|
|
|
111 |
value = M.util.get_string('clicktoedit', 'gradingform_guide')
|
|
|
112 |
taplain.addClass('empty')
|
|
|
113 |
}
|
|
|
114 |
// Replace newlines with <br> tags, when displaying in the page.
|
|
|
115 |
taplain.one('.textvalue').set('innerHTML', Y.Escape.html(value).replace(/(?:\r\n|\r|\n)/g, '<br>'))
|
|
|
116 |
if (tb) {
|
|
|
117 |
tbplain.one('.textvalue').set('innerHTML', Y.Escape.html(tb.get('value')))
|
|
|
118 |
}
|
|
|
119 |
// hide/display textarea, textbox and plaintexts
|
|
|
120 |
taplain.removeClass('hiddenelement')
|
|
|
121 |
ta.addClass('hiddenelement')
|
|
|
122 |
if (tb) {
|
|
|
123 |
tbplain.removeClass('hiddenelement')
|
|
|
124 |
tb.addClass('hiddenelement')
|
|
|
125 |
}
|
|
|
126 |
} else {
|
|
|
127 |
// if we need to show the input fields, set the width/height for textarea so it fills the cell
|
|
|
128 |
try {
|
|
|
129 |
if (ta.get('name').indexOf('[maxscore]') > 1) {
|
|
|
130 |
ta.setStyle('width', '25px');
|
|
|
131 |
} else {
|
|
|
132 |
var width = parseFloat(ta.get('parentNode').getComputedStyle('width'))-10,
|
|
|
133 |
height = parseFloat(ta.get('parentNode').getComputedStyle('height'))
|
|
|
134 |
ta.setStyle('width', Math.max(width,50)+'px')
|
|
|
135 |
ta.setStyle('height', Math.max(height,30)+'px')
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
catch (err) {
|
|
|
139 |
// this browser do not support 'computedStyle', leave the default size of the textbox
|
|
|
140 |
}
|
|
|
141 |
// hide/display textarea, textbox and plaintexts
|
|
|
142 |
taplain.addClass('hiddenelement')
|
|
|
143 |
ta.removeClass('hiddenelement')
|
|
|
144 |
if (tb) {
|
|
|
145 |
tbplain.addClass('hiddenelement')
|
|
|
146 |
tb.removeClass('hiddenelement')
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
// focus the proper input field in edit mode
|
|
|
150 |
if (editmode) {
|
|
|
151 |
ta.focus()
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
// handler for clicking on submit buttons within guideeditor element. Adds/deletes/rearranges criteria/comments on client side
|
|
|
156 |
M.gradingform_guideeditor.buttonclick = function(e, confirmed) {
|
|
|
157 |
var Y = M.gradingform_guideeditor.Y
|
|
|
158 |
var name = M.gradingform_guideeditor.name
|
|
|
159 |
if (e.target.get('type') != 'submit') {
|
|
|
160 |
return;
|
|
|
161 |
}
|
|
|
162 |
M.gradingform_guideeditor.disablealleditors()
|
|
|
163 |
var chunks = e.target.get('id').split('-')
|
|
|
164 |
var section = chunks[1]
|
|
|
165 |
var action = chunks[chunks.length-1]
|
|
|
166 |
|
|
|
167 |
if (chunks[0] != name || (section != 'criteria' && section != 'comments')) {
|
|
|
168 |
return;
|
|
|
169 |
}
|
|
|
170 |
// prepare the id of the next inserted criterion
|
|
|
171 |
var elements_str;
|
|
|
172 |
if (section == 'criteria') {
|
|
|
173 |
elements_str = '#guide-'+name+' .criteria .criterion'
|
|
|
174 |
} else if (section == 'comments') {
|
|
|
175 |
elements_str = '#guide-'+name+' .comments .criterion'
|
|
|
176 |
}
|
|
|
177 |
var newid = 0;
|
|
|
178 |
if (action == 'addcriterion' || action == 'addcomment') {
|
|
|
179 |
newid = M.gradingform_guideeditor.calculatenewid(elements_str);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
if (chunks.length == 3 && (action == 'addcriterion' || action == 'addcomment')) {
|
|
|
183 |
// ADD NEW CRITERION OR COMMENT
|
|
|
184 |
var parentel = Y.one('#'+name+'-'+section)
|
|
|
185 |
if (parentel.one('>tbody')) {
|
|
|
186 |
parentel = parentel.one('>tbody')
|
|
|
187 |
}
|
|
|
188 |
if (section == 'criteria') {
|
|
|
189 |
var newcriterion = M.gradingform_guideeditor.templates[name]['criterion']
|
|
|
190 |
parentel.append(newcriterion.replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''))
|
|
|
191 |
} else if (section == 'comments') {
|
|
|
192 |
var newcomment = M.gradingform_guideeditor.templates[name]['comment']
|
|
|
193 |
parentel.append(newcomment.replace(/\{COMMENT-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''))
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
M.gradingform_guideeditor.addhandlers();
|
|
|
197 |
M.gradingform_guideeditor.disablealleditors()
|
|
|
198 |
M.gradingform_guideeditor.assignclasses(elements_str)
|
|
|
199 |
|
|
|
200 |
// Enable edit mode of the newly added criterion/comment entry.
|
|
|
201 |
var inputTarget = 'shortname';
|
|
|
202 |
if (action == 'addcomment') {
|
|
|
203 |
inputTarget = 'description';
|
|
|
204 |
}
|
|
|
205 |
var inputTargetId = '#guide-' + name + ' #' + name + '-' + section + '-NEWID' + newid + '-' + inputTarget;
|
|
|
206 |
M.gradingform_guideeditor.editmode(Y.one(inputTargetId), true);
|
|
|
207 |
} else if (chunks.length == 4 && action == 'moveup') {
|
|
|
208 |
// MOVE UP
|
|
|
209 |
el = Y.one('#'+name+'-'+section+'-'+chunks[2])
|
|
|
210 |
if (el.previous()) {
|
|
|
211 |
el.get('parentNode').insertBefore(el, el.previous())
|
|
|
212 |
}
|
|
|
213 |
M.gradingform_guideeditor.assignclasses(elements_str)
|
|
|
214 |
} else if (chunks.length == 4 && action == 'movedown') {
|
|
|
215 |
// MOVE DOWN
|
|
|
216 |
el = Y.one('#'+name+'-'+section+'-'+chunks[2])
|
|
|
217 |
if (el.next()) {
|
|
|
218 |
el.get('parentNode').insertBefore(el.next(), el)
|
|
|
219 |
}
|
|
|
220 |
M.gradingform_guideeditor.assignclasses(elements_str)
|
|
|
221 |
} else if (chunks.length == 4 && action == 'delete') {
|
|
|
222 |
// DELETE
|
|
|
223 |
if (confirmed) {
|
|
|
224 |
Y.one('#'+name+'-'+section+'-'+chunks[2]).remove()
|
|
|
225 |
M.gradingform_guideeditor.assignclasses(elements_str)
|
|
|
226 |
} else {
|
|
|
227 |
require(['core/notification', 'core/str'], function(Notification, Str) {
|
|
|
228 |
Notification.saveCancelPromise(
|
|
|
229 |
Str.get_string('confirmation', 'admin'),
|
|
|
230 |
Str.get_string('confirmdeletecriterion', 'gradingform_guide'),
|
|
|
231 |
Str.get_string('yes', 'moodle')
|
|
|
232 |
).then(function() {
|
|
|
233 |
M.gradingform_guideeditor.buttonclick.apply(this, [e, true]);
|
|
|
234 |
return;
|
|
|
235 |
}.bind(this)).catch(function() {
|
|
|
236 |
// User cancelled.
|
|
|
237 |
});
|
|
|
238 |
}.bind(this));
|
|
|
239 |
}
|
|
|
240 |
} else {
|
|
|
241 |
// unknown action
|
|
|
242 |
return;
|
|
|
243 |
}
|
|
|
244 |
e.preventDefault();
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
// properly set classes (first/last/odd/even) and/or criterion sortorder for elements Y.all(elements_str)
|
|
|
248 |
M.gradingform_guideeditor.assignclasses = function (elements_str) {
|
|
|
249 |
var elements = M.gradingform_guideeditor.Y.all(elements_str)
|
|
|
250 |
for (var i=0; i<elements.size(); i++) {
|
|
|
251 |
elements.item(i).removeClass('first').removeClass('last').removeClass('even').removeClass('odd').
|
|
|
252 |
addClass(((i%2)?'odd':'even') + ((i==0)?' first':'') + ((i==elements.size()-1)?' last':''))
|
|
|
253 |
elements.item(i).all('input[type=hidden]').each(
|
|
|
254 |
function(node) {if (node.get('name').match(/sortorder/)) { node.set('value', i)}}
|
|
|
255 |
);
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
// returns unique id for the next added element, it should not be equal to any of Y.all(elements_str) ids
|
|
|
260 |
M.gradingform_guideeditor.calculatenewid = function (elements_str) {
|
|
|
261 |
var newid = 1
|
|
|
262 |
M.gradingform_guideeditor.Y.all(elements_str).each( function(node) {
|
|
|
263 |
var idchunks = node.get('id').split('-'), id = idchunks.pop();
|
|
|
264 |
if (id.match(/^NEWID(\d+)$/)) { newid = Math.max(newid, parseInt(id.substring(5))+1)};
|
|
|
265 |
} );
|
|
|
266 |
return newid
|
|
|
267 |
}
|