1 M.gradingform_rubriceditor = {'templates' : {}, 'eventhandler' : null, 'name' : null, 'Y' : null};
4 * This function is called for each rubriceditor on page.
6 M.gradingform_rubriceditor.init = function(Y, options) {
7 M.gradingform_rubriceditor.name = options.name
8 M.gradingform_rubriceditor.Y = Y
9 M.gradingform_rubriceditor.templates[options.name] = {
10 'criterion' : options.criteriontemplate,
11 'level' : options.leveltemplate
13 M.gradingform_rubriceditor.disablealleditors()
14 Y.on('click', M.gradingform_rubriceditor.clickanywhere, 'body', null)
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);
19 M.gradingform_rubriceditor.addhandlers()
22 // Adds handlers for clicking submit button. This function must be called each time JS adds new elements to html
23 M.gradingform_rubriceditor.addhandlers = function() {
24 var Y = M.gradingform_rubriceditor.Y
25 var name = M.gradingform_rubriceditor.name
26 if (M.gradingform_rubriceditor.eventhandler) M.gradingform_rubriceditor.eventhandler.detach()
27 M.gradingform_rubriceditor.eventhandler = Y.on('click', M.gradingform_rubriceditor.buttonclick, '#rubric-'+name+' input[type=submit]', null);
30 // switches all input text elements to non-edit mode
31 M.gradingform_rubriceditor.disablealleditors = function() {
32 var Y = M.gradingform_rubriceditor.Y
33 var name = M.gradingform_rubriceditor.name
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)} );
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
41 M.gradingform_rubriceditor.clickanywhere = function(e) {
42 if (e.type == 'touchstart') return
44 // if clicked on button - disablecurrenteditor, continue
45 if (el.get('tagName') == 'INPUT' && el.get('type') == 'submit') {
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
51 while (el && !(el.hasClass('level') || el.hasClass('description'))) {
52 if (el.hasClass('score')) focustb = true
53 el = el.get('parentNode')
56 if (el.one('textarea').hasClass('hiddenelement')) {
57 M.gradingform_rubriceditor.disablealleditors()
58 M.gradingform_rubriceditor.editmode(el, true, focustb)
62 // else disablecurrenteditor
63 M.gradingform_rubriceditor.disablealleditors()
66 // switch the criterion description or level to edit mode or switch back
67 M.gradingform_rubriceditor.editmode = function(el, editmode, focustb) {
68 var ta = el.one('textarea')
69 if (!editmode && ta.hasClass('hiddenelement')) return;
70 if (editmode && !ta.hasClass('hiddenelement')) return;
71 var pseudotablink = '<input type="text" size="1" class="pseudotablink"/>',
72 taplain = ta.get('parentNode').one('.plainvalue'),
74 tb = el.one('.score input[type=text]')
75 // add 'plainvalue' next to textarea for description/definition and next to input text field for score (if applicable)
77 ta.get('parentNode').append('<div class="plainvalue">'+pseudotablink+'<span class="textvalue"> </span></div>')
78 taplain = ta.get('parentNode').one('.plainvalue')
79 taplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
81 tb.get('parentNode').append('<span class="plainvalue">'+pseudotablink+'<span class="textvalue"> </span></span>')
82 tbplain = tb.get('parentNode').one('.plainvalue')
83 tbplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
86 if (tb && !tbplain) tbplain = tb.get('parentNode').one('.plainvalue')
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
90 var value = ta.get('value')
91 if (value.length) taplain.removeClass('empty')
93 value = (el.hasClass('level')) ? M.str.gradingform_rubric.levelempty : M.str.gradingform_rubric.criterionempty
94 taplain.addClass('empty')
96 taplain.one('.textvalue').set('innerHTML', value)
97 if (tb) tbplain.one('.textvalue').set('innerHTML', tb.get('value'))
98 // hide/display textarea, textbox and plaintexts
99 taplain.removeClass('hiddenelement')
100 ta.addClass('hiddenelement')
102 tbplain.removeClass('hiddenelement')
103 tb.addClass('hiddenelement')
106 // if we need to show the input fields, set the width/height for textarea so it fills the cell
108 var width = parseFloat(ta.get('parentNode').getComputedStyle('width')),
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'))
112 ta.setStyle('width', Math.max(width,50)+'px')
113 ta.setStyle('height', Math.max(height,20)+'px')
116 // this browser do not support 'computedStyle', leave the default size of the textbox
118 // hide/display textarea, textbox and plaintexts
119 taplain.addClass('hiddenelement')
120 ta.removeClass('hiddenelement')
122 tbplain.addClass('hiddenelement')
123 tb.removeClass('hiddenelement')
126 // focus the proper input field in edit mode
127 if (editmode) { if (tb && focustb) tb.focus(); else ta.focus() }
130 // handler for clicking on submit buttons within rubriceditor element. Adds/deletes/rearranges criteria and/or levels on client side
131 M.gradingform_rubriceditor.buttonclick = function(e, confirmed) {
132 var Y = M.gradingform_rubriceditor.Y
133 var name = M.gradingform_rubriceditor.name
134 if (e.target.get('type') != 'submit') return;
135 M.gradingform_rubriceditor.disablealleditors()
136 var chunks = e.target.get('id').split('-'),
137 action = chunks[chunks.length-1]
138 if (chunks[0] != name || chunks[1] != 'criteria') return;
140 if (chunks.length>4 || action == 'addlevel') {
141 elements_str = '#rubric-'+name+' #'+name+'-criteria-'+chunks[2]+'-levels .level'
143 elements_str = '#rubric-'+name+' .criterion'
145 // prepare the id of the next inserted level or criterion
146 if (action == 'addcriterion' || action == 'addlevel') {
147 var newid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .criterion')
148 var newlevid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .level')
150 var dialog_options = {
152 'callbackargs' : [e, true],
153 'callback' : M.gradingform_rubriceditor.buttonclick
155 if (chunks.length == 3 && action == 'addcriterion') {
157 var levelsscores = [0], levidx = 1
158 var parentel = Y.one('#'+name+'-criteria')
159 if (parentel.one('>tbody')) parentel = parentel.one('>tbody')
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')
164 for (levidx;levidx<3;levidx++) levelsscores[levidx] = parseFloat(levelsscores[levidx-1])+1
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])
169 var newcriterion = M.gradingform_rubriceditor.templates[name]['criterion'].replace(/\{LEVELS\}/, levelsstr)
170 parentel.append(newcriterion.replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''))
171 M.gradingform_rubriceditor.assignclasses('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-levels .level')
172 M.gradingform_rubriceditor.addhandlers();
173 M.gradingform_rubriceditor.disablealleditors()
174 M.gradingform_rubriceditor.assignclasses(elements_str)
175 M.gradingform_rubriceditor.editmode(Y.one('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-description'),true)
176 } else if (chunks.length == 5 && action == 'addlevel') {
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) })
181 var newlevel = M.gradingform_rubriceditor.templates[name]['level'].
182 replace(/\{CRITERION-id\}/g, chunks[2]).replace(/\{LEVEL-id\}/g, 'NEWID'+newlevid).replace(/\{LEVEL-score\}/g, newscore).replace(/\{.+?\}/g, '')
183 parent.append(newlevel)
184 M.gradingform_rubriceditor.addhandlers();
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)
188 } else if (chunks.length == 4 && action == 'moveup') {
190 el = Y.one('#'+name+'-criteria-'+chunks[2])
191 if (el.previous()) el.get('parentNode').insertBefore(el, el.previous())
192 M.gradingform_rubriceditor.assignclasses(elements_str)
193 } else if (chunks.length == 4 && action == 'movedown') {
194 // MOVE CRITERION DOWN
195 el = Y.one('#'+name+'-criteria-'+chunks[2])
196 if (el.next()) el.get('parentNode').insertBefore(el.next(), el)
197 M.gradingform_rubriceditor.assignclasses(elements_str)
198 } else if (chunks.length == 4 && action == 'delete') {
201 Y.one('#'+name+'-criteria-'+chunks[2]).remove()
202 M.gradingform_rubriceditor.assignclasses(elements_str)
204 dialog_options['message'] = M.str.gradingform_rubric.confirmdeletecriterion
205 M.util.show_confirm_dialog(e, dialog_options);
207 } else if (chunks.length == 6 && action == 'delete') {
210 Y.one('#'+name+'-criteria-'+chunks[2]+'-'+chunks[3]+'-'+chunks[4]).remove()
211 M.gradingform_rubriceditor.assignclasses(elements_str)
213 dialog_options['message'] = M.str.gradingform_rubric.confirmdeletelevel
214 M.util.show_confirm_dialog(e, dialog_options);
223 // properly set classes (first/last/odd/even), level width and/or criterion sortorder for elements Y.all(elements_str)
224 M.gradingform_rubriceditor.assignclasses = function (elements_str) {
225 var elements = M.gradingform_rubriceditor.Y.all(elements_str)
226 for (var i=0;i<elements.size();i++) {
227 elements.item(i).removeClass('first').removeClass('last').removeClass('even').removeClass('odd').
228 addClass(((i%2)?'odd':'even') + ((i==0)?' first':'') + ((i==elements.size()-1)?' last':''))
229 elements.item(i).all('input[type=hidden]').each(
230 function(node) {if (node.get('name').match(/sortorder/)) node.set('value', i)}
232 if (elements.item(i).hasClass('level')) elements.item(i).set('width', Math.round(100/elements.size())+'%')
236 // returns unique id for the next added element, it should not be equal to any of Y.all(elements_str) ids
237 M.gradingform_rubriceditor.calculatenewid = function (elements_str) {
239 M.gradingform_rubriceditor.Y.all(elements_str).each( function(node) {
240 var idchunks = node.get('id').split('-'), id = idchunks.pop();
241 if (id.match(/^NEWID(\d+)$/)) newid = Math.max(newid, parseInt(id.substring(5))+1);