M.gradingform_rubriceditor = {'templates' : {}, 'eventhandler' : null, 'name' : null, 'Y' : null};
/**
* This function is called for each rubriceditor on page.
*/
M.gradingform_rubriceditor.init = function(Y, options) {
M.gradingform_rubriceditor.name = options.name
M.gradingform_rubriceditor.Y = Y
M.gradingform_rubriceditor.templates[options.name] = {
'criterion' : options.criteriontemplate,
'level' : options.leveltemplate
}
M.gradingform_rubriceditor.disablealleditors()
Y.on('click', M.gradingform_rubriceditor.clickanywhere, 'body', null)
YUI().use('event-touch', function (Y) {
Y.one('body').on('touchstart', M.gradingform_rubriceditor.clickanywhere);
Y.one('body').on('touchend', M.gradingform_rubriceditor.clickanywhere);
})
M.gradingform_rubriceditor.addhandlers()
};
// Adds handlers for clicking submit button. This function must be called each time JS adds new elements to html
M.gradingform_rubriceditor.addhandlers = function() {
var Y = M.gradingform_rubriceditor.Y
var name = M.gradingform_rubriceditor.name
if (M.gradingform_rubriceditor.eventhandler) M.gradingform_rubriceditor.eventhandler.detach()
M.gradingform_rubriceditor.eventhandler = Y.on('click', M.gradingform_rubriceditor.buttonclick, '#rubric-'+name+' input[type=submit]', null);
}
// switches all input text elements to non-edit mode
M.gradingform_rubriceditor.disablealleditors = function() {
var Y = M.gradingform_rubriceditor.Y
var name = M.gradingform_rubriceditor.name
Y.all('#rubric-'+name+' .level').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
Y.all('#rubric-'+name+' .description').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
}
// function invoked on each click on the page. If level and/or criterion description is clicked
// it switches this element to edit mode. If rubric button is clicked it does nothing so the 'buttonclick'
// function is invoked
M.gradingform_rubriceditor.clickanywhere = function(e) {
if (e.type == 'touchstart') return
var el = e.target
// if clicked on button - disablecurrenteditor, continue
if (el.get('tagName') == 'INPUT' && el.get('type') == 'submit') {
return
}
// else if clicked on level and this level is not enabled - enable it
// or if clicked on description and this description is not enabled - enable it
var focustb = false
while (el && !(el.hasClass('level') || el.hasClass('description'))) {
if (el.hasClass('score')) focustb = true
el = el.get('parentNode')
}
if (el) {
if (el.one('textarea').hasClass('hiddenelement')) {
M.gradingform_rubriceditor.disablealleditors()
M.gradingform_rubriceditor.editmode(el, true, focustb)
}
return
}
// else disablecurrenteditor
M.gradingform_rubriceditor.disablealleditors()
}
// switch the criterion description or level to edit mode or switch back
M.gradingform_rubriceditor.editmode = function(el, editmode, focustb) {
var ta = el.one('textarea')
if (!editmode && ta.hasClass('hiddenelement')) return;
if (editmode && !ta.hasClass('hiddenelement')) return;
var pseudotablink = '',
taplain = ta.get('parentNode').one('.plainvalue'),
tbplain = null,
tb = el.one('.score input[type=text]')
// add 'plainvalue' next to textarea for description/definition and next to input text field for score (if applicable)
if (!taplain) {
ta.get('parentNode').append('
'+pseudotablink+'
')
taplain = ta.get('parentNode').one('.plainvalue')
taplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
if (tb) {
tb.get('parentNode').append(''+pseudotablink+'')
tbplain = tb.get('parentNode').one('.plainvalue')
tbplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
}
}
if (tb && !tbplain) tbplain = tb.get('parentNode').one('.plainvalue')
if (!editmode) {
// if we need to hide the input fields, copy their contents to plainvalue(s). If description/definition
// is empty, display the default text ('Click to edit ...') and add/remove 'empty' CSS class to element
var value = ta.get('value')
if (value.length) taplain.removeClass('empty')
else {
value = (el.hasClass('level')) ? M.str.gradingform_rubric.levelempty : M.str.gradingform_rubric.criterionempty
taplain.addClass('empty')
}
taplain.one('.textvalue').set('innerHTML', Y.Escape.html(value));
if (tb) tbplain.one('.textvalue').set('innerHTML', Y.Escape.html(tb.get('value')));
// hide/display textarea, textbox and plaintexts
taplain.removeClass('hiddenelement')
ta.addClass('hiddenelement')
if (tb) {
tbplain.removeClass('hiddenelement')
tb.addClass('hiddenelement')
}
} else {
// if we need to show the input fields, set the width/height for textarea so it fills the cell
try {
var width = parseFloat(ta.get('parentNode').getComputedStyle('width')),
height
if (el.hasClass('level')) height = parseFloat(el.getComputedStyle('height')) - parseFloat(el.one('.score').getComputedStyle('height'))
else height = parseFloat(ta.get('parentNode').getComputedStyle('height'))
ta.setStyle('width', Math.max(width-16,50)+'px')
ta.setStyle('height', Math.max(height,20)+'px')
}
catch (err) {
// this browser do not support 'computedStyle', leave the default size of the textbox
}
// hide/display textarea, textbox and plaintexts
taplain.addClass('hiddenelement')
ta.removeClass('hiddenelement')
if (tb) {
tbplain.addClass('hiddenelement')
tb.removeClass('hiddenelement')
}
}
// focus the proper input field in edit mode
if (editmode) { if (tb && focustb) tb.focus(); else ta.focus() }
}
// handler for clicking on submit buttons within rubriceditor element. Adds/deletes/rearranges criteria and/or levels on client side
M.gradingform_rubriceditor.buttonclick = function(e, confirmed) {
var Y = M.gradingform_rubriceditor.Y
var name = M.gradingform_rubriceditor.name
if (e.target.get('type') != 'submit') return;
M.gradingform_rubriceditor.disablealleditors()
var chunks = e.target.get('id').split('-'),
action = chunks[chunks.length-1]
if (chunks[0] != name || chunks[1] != 'criteria') return;
var elements_str
if (chunks.length>4 || action == 'addlevel') {
elements_str = '#rubric-'+name+' #'+name+'-criteria-'+chunks[2]+'-levels .level'
} else {
elements_str = '#rubric-'+name+' .criterion'
}
// prepare the id of the next inserted level or criterion
if (action == 'addcriterion' || action == 'addlevel') {
var newid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .criterion')
var newlevid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .level')
}
var dialog_options = {
'scope' : this,
'callbackargs' : [e, true],
'callback' : M.gradingform_rubriceditor.buttonclick
};
if (chunks.length == 3 && action == 'addcriterion') {
// ADD NEW CRITERION
var levelsscores = [0], levidx = 1
var parentel = Y.one('#'+name+'-criteria')
if (parentel.one('>tbody')) parentel = parentel.one('>tbody')
if (parentel.all('.criterion').size()) {
var lastcriterion = parentel.all('.criterion').item(parentel.all('.criterion').size()-1).all('.level')
for (levidx=0;levidx