MDL-50283 gradingform_rubric: Minor styling fixes
[moodle.git] / grade / grading / form / rubric / js / rubriceditor.js
CommitLineData
59be9011 1M.gradingform_rubriceditor = {'templates' : {}, 'eventhandler' : null, 'name' : null, 'Y' : null};
c586d2bf
MG
2
3/**
4 * This function is called for each rubriceditor on page.
5 */
6M.gradingform_rubriceditor.init = function(Y, options) {
59be9011
MG
7 M.gradingform_rubriceditor.name = options.name
8 M.gradingform_rubriceditor.Y = Y
c586d2bf
MG
9 M.gradingform_rubriceditor.templates[options.name] = {
10 'criterion' : options.criteriontemplate,
11 'level' : options.leveltemplate
12 }
59be9011
MG
13 M.gradingform_rubriceditor.disablealleditors()
14 Y.on('click', M.gradingform_rubriceditor.clickanywhere, 'body', null)
a19d1057
MG
15 YUI().use('event-touch', function (Y) {
16 Y.one('body').on('touchstart', M.gradingform_rubriceditor.clickanywhere);
17 Y.one('body').on('touchend', M.gradingform_rubriceditor.clickanywhere);
18 })
59be9011 19 M.gradingform_rubriceditor.addhandlers()
c586d2bf
MG
20};
21
22// Adds handlers for clicking submit button. This function must be called each time JS adds new elements to html
59be9011
MG
23M.gradingform_rubriceditor.addhandlers = function() {
24 var Y = M.gradingform_rubriceditor.Y
25 var name = M.gradingform_rubriceditor.name
c586d2bf 26 if (M.gradingform_rubriceditor.eventhandler) M.gradingform_rubriceditor.eventhandler.detach()
59be9011 27 M.gradingform_rubriceditor.eventhandler = Y.on('click', M.gradingform_rubriceditor.buttonclick, '#rubric-'+name+' input[type=submit]', null);
ab156741
MG
28}
29
ffbf4370 30// switches all input text elements to non-edit mode
59be9011
MG
31M.gradingform_rubriceditor.disablealleditors = function() {
32 var Y = M.gradingform_rubriceditor.Y
33 var name = M.gradingform_rubriceditor.name
ab156741
MG
34 Y.all('#rubric-'+name+' .level').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
35 Y.all('#rubric-'+name+' .description').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
36}
37
ffbf4370
MG
38// function invoked on each click on the page. If level and/or criterion description is clicked
39// it switches this element to edit mode. If rubric button is clicked it does nothing so the 'buttonclick'
40// function is invoked
59be9011 41M.gradingform_rubriceditor.clickanywhere = function(e) {
a19d1057 42 if (e.type == 'touchstart') return
ab156741
MG
43 var el = e.target
44 // if clicked on button - disablecurrenteditor, continue
45 if (el.get('tagName') == 'INPUT' && el.get('type') == 'submit') {
ab156741
MG
46 return
47 }
48 // else if clicked on level and this level is not enabled - enable it
49 // or if clicked on description and this description is not enabled - enable it
59be9011
MG
50 var focustb = false
51 while (el && !(el.hasClass('level') || el.hasClass('description'))) {
52 if (el.hasClass('score')) focustb = true
53 el = el.get('parentNode')
54 }
ab156741 55 if (el) {
a19d1057 56 if (el.one('textarea').hasClass('hiddenelement')) {
59be9011
MG
57 M.gradingform_rubriceditor.disablealleditors()
58 M.gradingform_rubriceditor.editmode(el, true, focustb)
ab156741
MG
59 }
60 return
61 }
62 // else disablecurrenteditor
59be9011 63 M.gradingform_rubriceditor.disablealleditors()
ab156741
MG
64}
65
ffbf4370 66// switch the criterion description or level to edit mode or switch back
59be9011 67M.gradingform_rubriceditor.editmode = function(el, editmode, focustb) {
ab156741 68 var ta = el.one('textarea')
a19d1057
MG
69 if (!editmode && ta.hasClass('hiddenelement')) return;
70 if (editmode && !ta.hasClass('hiddenelement')) return;
71 var pseudotablink = '<input type="text" size="1" class="pseudotablink"/>',
ffbf4370
MG
72 taplain = ta.get('parentNode').one('.plainvalue'),
73 tbplain = null,
a19d1057 74 tb = el.one('.score input[type=text]')
ffbf4370
MG
75 // add 'plainvalue' next to textarea for description/definition and next to input text field for score (if applicable)
76 if (!taplain) {
a19d1057 77 ta.get('parentNode').append('<div class="plainvalue">'+pseudotablink+'<span class="textvalue">&nbsp;</span></div>')
ffbf4370
MG
78 taplain = ta.get('parentNode').one('.plainvalue')
79 taplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
80 if (tb) {
a19d1057 81 tb.get('parentNode').append('<span class="plainvalue">'+pseudotablink+'<span class="textvalue">&nbsp;</span></span>')
ffbf4370
MG
82 tbplain = tb.get('parentNode').one('.plainvalue')
83 tbplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
84 }
ab156741 85 }
ffbf4370 86 if (tb && !tbplain) tbplain = tb.get('parentNode').one('.plainvalue')
ab156741 87 if (!editmode) {
ffbf4370
MG
88 // if we need to hide the input fields, copy their contents to plainvalue(s). If description/definition
89 // is empty, display the default text ('Click to edit ...') and add/remove 'empty' CSS class to element
ab156741 90 var value = ta.get('value')
ffbf4370 91 if (value.length) taplain.removeClass('empty')
ab156741 92 else {
64e7aa4d 93 value = (el.hasClass('level')) ? M.util.get_string('levelempty', 'gradingform_rubric') : M.util.get_string('criterionempty', 'gradingform_rubric')
ffbf4370 94 taplain.addClass('empty')
ab156741 95 }
b9b2968e
DM
96 taplain.one('.textvalue').set('innerHTML', Y.Escape.html(value));
97 if (tb) tbplain.one('.textvalue').set('innerHTML', Y.Escape.html(tb.get('value')));
a19d1057
MG
98 // hide/display textarea, textbox and plaintexts
99 taplain.removeClass('hiddenelement')
100 ta.addClass('hiddenelement')
101 if (tb) {
102 tbplain.removeClass('hiddenelement')
103 tb.addClass('hiddenelement')
104 }
ab156741 105 } else {
ffbf4370 106 // if we need to show the input fields, set the width/height for textarea so it fills the cell
a19d1057
MG
107 try {
108 var width = parseFloat(ta.get('parentNode').getComputedStyle('width')),
109 height
110 if (el.hasClass('level')) height = parseFloat(el.getComputedStyle('height')) - parseFloat(el.one('.score').getComputedStyle('height'))
111 else height = parseFloat(ta.get('parentNode').getComputedStyle('height'))
73eb9f43 112 ta.setStyle('width', Math.max(width-16,50)+'px')
a19d1057
MG
113 ta.setStyle('height', Math.max(height,20)+'px')
114 }
115 catch (err) {
116 // this browser do not support 'computedStyle', leave the default size of the textbox
117 }
118 // hide/display textarea, textbox and plaintexts
119 taplain.addClass('hiddenelement')
120 ta.removeClass('hiddenelement')
121 if (tb) {
122 tbplain.addClass('hiddenelement')
123 tb.removeClass('hiddenelement')
124 }
ab156741 125 }
ffbf4370
MG
126 // focus the proper input field in edit mode
127 if (editmode) { if (tb && focustb) tb.focus(); else ta.focus() }
c586d2bf
MG
128}
129
130// handler for clicking on submit buttons within rubriceditor element. Adds/deletes/rearranges criteria and/or levels on client side
59be9011
MG
131M.gradingform_rubriceditor.buttonclick = function(e, confirmed) {
132 var Y = M.gradingform_rubriceditor.Y
133 var name = M.gradingform_rubriceditor.name
c586d2bf 134 if (e.target.get('type') != 'submit') return;
59be9011 135 M.gradingform_rubriceditor.disablealleditors()
c586d2bf
MG
136 var chunks = e.target.get('id').split('-'),
137 action = chunks[chunks.length-1]
39c6f4b6 138 if (chunks[0] != name || chunks[1] != 'criteria') return;
c586d2bf 139 var elements_str
39c6f4b6
MG
140 if (chunks.length>4 || action == 'addlevel') {
141 elements_str = '#rubric-'+name+' #'+name+'-criteria-'+chunks[2]+'-levels .level'
c586d2bf 142 } else {
ab156741 143 elements_str = '#rubric-'+name+' .criterion'
c586d2bf
MG
144 }
145 // prepare the id of the next inserted level or criterion
e22c8d2a 146 if (action == 'addcriterion' || action == 'addlevel' || action == 'duplicate' ) {
59be9011
MG
147 var newid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .criterion')
148 var newlevid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .level')
c586d2bf
MG
149 }
150 var dialog_options = {
151 'scope' : this,
59be9011 152 'callbackargs' : [e, true],
c586d2bf
MG
153 'callback' : M.gradingform_rubriceditor.buttonclick
154 };
39c6f4b6 155 if (chunks.length == 3 && action == 'addcriterion') {
c586d2bf 156 // ADD NEW CRITERION
ffbf4370 157 var levelsscores = [0], levidx = 1
39c6f4b6
MG
158 var parentel = Y.one('#'+name+'-criteria')
159 if (parentel.one('>tbody')) parentel = parentel.one('>tbody')
ffbf4370
MG
160 if (parentel.all('.criterion').size()) {
161 var lastcriterion = parentel.all('.criterion').item(parentel.all('.criterion').size()-1).all('.level')
162 for (levidx=0;levidx<lastcriterion.size();levidx++) levelsscores[levidx] = lastcriterion.item(levidx).one('.score input[type=text]').get('value')
163 }
164 for (levidx;levidx<3;levidx++) levelsscores[levidx] = parseFloat(levelsscores[levidx-1])+1
165 var levelsstr = '';
166 for (levidx=0;levidx<levelsscores.length;levidx++) {
167 levelsstr += M.gradingform_rubriceditor.templates[name]['level'].replace(/\{LEVEL-id\}/g, 'NEWID'+(newlevid+levidx)).replace(/\{LEVEL-score\}/g, levelsscores[levidx])
168 }
169 var newcriterion = M.gradingform_rubriceditor.templates[name]['criterion'].replace(/\{LEVELS\}/, levelsstr)
170 parentel.append(newcriterion.replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''))
59be9011 171 M.gradingform_rubriceditor.assignclasses('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-levels .level')
ffbf4370
MG
172 M.gradingform_rubriceditor.addhandlers();
173 M.gradingform_rubriceditor.disablealleditors()
174 M.gradingform_rubriceditor.assignclasses(elements_str)
59be9011 175 M.gradingform_rubriceditor.editmode(Y.one('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-description'),true)
39c6f4b6 176 } else if (chunks.length == 5 && action == 'addlevel') {
c586d2bf 177 // ADD NEW LEVEL
ffbf4370
MG
178 var newscore = 0;
179 parent = Y.one('#'+name+'-criteria-'+chunks[2]+'-levels')
180 parent.all('.level').each(function (node) { newscore = Math.max(newscore, parseFloat(node.one('.score input[type=text]').get('value'))+1) })
c586d2bf 181 var newlevel = M.gradingform_rubriceditor.templates[name]['level'].
ffbf4370
MG
182 replace(/\{CRITERION-id\}/g, chunks[2]).replace(/\{LEVEL-id\}/g, 'NEWID'+newlevid).replace(/\{LEVEL-score\}/g, newscore).replace(/\{.+?\}/g, '')
183 parent.append(newlevel)
59be9011 184 M.gradingform_rubriceditor.addhandlers();
ffbf4370
MG
185 M.gradingform_rubriceditor.disablealleditors()
186 M.gradingform_rubriceditor.assignclasses(elements_str)
187 M.gradingform_rubriceditor.editmode(parent.all('.level').item(parent.all('.level').size()-1), true)
39c6f4b6 188 } else if (chunks.length == 4 && action == 'moveup') {
c586d2bf 189 // MOVE CRITERION UP
39c6f4b6 190 el = Y.one('#'+name+'-criteria-'+chunks[2])
c586d2bf 191 if (el.previous()) el.get('parentNode').insertBefore(el, el.previous())
ffbf4370 192 M.gradingform_rubriceditor.assignclasses(elements_str)
39c6f4b6 193 } else if (chunks.length == 4 && action == 'movedown') {
c586d2bf 194 // MOVE CRITERION DOWN
39c6f4b6 195 el = Y.one('#'+name+'-criteria-'+chunks[2])
c586d2bf 196 if (el.next()) el.get('parentNode').insertBefore(el.next(), el)
ffbf4370 197 M.gradingform_rubriceditor.assignclasses(elements_str)
39c6f4b6 198 } else if (chunks.length == 4 && action == 'delete') {
c586d2bf
MG
199 // DELETE CRITERION
200 if (confirmed) {
39c6f4b6 201 Y.one('#'+name+'-criteria-'+chunks[2]).remove()
ffbf4370 202 M.gradingform_rubriceditor.assignclasses(elements_str)
c586d2bf 203 } else {
64e7aa4d 204 dialog_options['message'] = M.util.get_string('confirmdeletecriterion', 'gradingform_rubric')
c586d2bf
MG
205 M.util.show_confirm_dialog(e, dialog_options);
206 }
e22c8d2a 207 } else if (chunks.length == 4 && action == 'duplicate') {
a939ae6d
DM
208 // Duplicate criterion.
209 var levelsdef = [], levelsscores = [0], levidx = null;
e22c8d2a
S
210 var parentel = Y.one('#'+name+'-criteria');
211 if (parentel.one('>tbody')) { parentel = parentel.one('>tbody'); }
212
213 var source = Y.one('#'+name+'-criteria-'+chunks[2]);
214 if (source.all('.level')) {
215 var lastcriterion = source.all('.level');
a939ae6d 216 for (levidx = 0; levidx < lastcriterion.size(); levidx++) {
e22c8d2a
S
217 levelsdef[levidx] = lastcriterion.item(levidx).one('.definition .textvalue').get('innerHTML');
218 }
a939ae6d 219 for (levidx = 0; levidx < lastcriterion.size(); levidx++) {
e22c8d2a
S
220 levelsscores[levidx] = lastcriterion.item(levidx).one('.score input[type=text]').get('value');
221 }
222 }
223
a939ae6d 224 for (levidx; levidx < 3; levidx++) { levelsscores[levidx] = parseFloat(levelsscores[levidx-1]) + 1; }
e22c8d2a 225 var levelsstr = '';
a939ae6d
DM
226 for (levidx = 0; levidx < levelsscores.length; levidx++) {
227 levelsstr += M.gradingform_rubriceditor.templates[name].level
e22c8d2a
S
228 .replace(/\{LEVEL-id\}/g, 'NEWID'+(newlevid+levidx))
229 .replace(/\{LEVEL-score\}/g, levelsscores[levidx])
230 .replace(/\{LEVEL-definition\}/g, Y.Escape.html(levelsdef[levidx]));
231 }
232 var description = source.one('.description .textvalue');
a939ae6d 233 var newcriterion = M.gradingform_rubriceditor.templates[name].criterion
e22c8d2a
S
234 .replace(/\{LEVELS\}/, levelsstr)
235 .replace(/\{CRITERION-description\}/, Y.Escape.html(description.get('innerHTML')));
236 parentel.append(newcriterion.replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''));
237 M.gradingform_rubriceditor.assignclasses('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-levels .level');
238 M.gradingform_rubriceditor.addhandlers();
239 M.gradingform_rubriceditor.disablealleditors();
240 M.gradingform_rubriceditor.assignclasses(elements_str);
241 M.gradingform_rubriceditor.editmode(Y.one('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-description'),true);
39c6f4b6 242 } else if (chunks.length == 6 && action == 'delete') {
c586d2bf
MG
243 // DELETE LEVEL
244 if (confirmed) {
39c6f4b6 245 Y.one('#'+name+'-criteria-'+chunks[2]+'-'+chunks[3]+'-'+chunks[4]).remove()
ffbf4370 246 M.gradingform_rubriceditor.assignclasses(elements_str)
c586d2bf 247 } else {
64e7aa4d 248 dialog_options['message'] = M.util.get_string('confirmdeletelevel', 'gradingform_rubric')
c586d2bf
MG
249 M.util.show_confirm_dialog(e, dialog_options);
250 }
251 } else {
252 // unknown action
253 return;
254 }
255 e.preventDefault();
59be9011
MG
256}
257
ffbf4370 258// properly set classes (first/last/odd/even), level width and/or criterion sortorder for elements Y.all(elements_str)
59be9011
MG
259M.gradingform_rubriceditor.assignclasses = function (elements_str) {
260 var elements = M.gradingform_rubriceditor.Y.all(elements_str)
c586d2bf
MG
261 for (var i=0;i<elements.size();i++) {
262 elements.item(i).removeClass('first').removeClass('last').removeClass('even').removeClass('odd').
263 addClass(((i%2)?'odd':'even') + ((i==0)?' first':'') + ((i==elements.size()-1)?' last':''))
264 elements.item(i).all('input[type=hidden]').each(
ffbf4370 265 function(node) {if (node.get('name').match(/sortorder/)) node.set('value', i)}
c586d2bf 266 );
ffbf4370 267 if (elements.item(i).hasClass('level')) elements.item(i).set('width', Math.round(100/elements.size())+'%')
c586d2bf
MG
268 }
269}
59be9011 270
ffbf4370 271// returns unique id for the next added element, it should not be equal to any of Y.all(elements_str) ids
59be9011
MG
272M.gradingform_rubriceditor.calculatenewid = function (elements_str) {
273 var newid = 1
274 M.gradingform_rubriceditor.Y.all(elements_str).each( function(node) {
275 var idchunks = node.get('id').split('-'), id = idchunks.pop();
276 if (id.match(/^NEWID(\d+)$/)) newid = Math.max(newid, parseInt(id.substring(5))+1);
277 } );
278 return newid
a939ae6d 279}