MDL-32657: course dragdrop: define elements changes within the format

It allows to define M.course.format.swap_sections and
M.course.format.get_section_selector in the course format javascript file, so
that course dragdrop is aware about perculiar layout and changes that need to
be done when sections are swapped.
This commit is contained in:
Ruslan Kabalin 2012-04-27 15:01:02 +01:00
parent aa753ac24f
commit c77582fe42
4 changed files with 84 additions and 20 deletions

View File

@ -0,0 +1,43 @@
// Javascript functions for course format
M.course = M.course || {};
M.course.format = M.course.format || {};
/**
* Get section list for this format
*
* @param {YUI} Y YUI3 instance
* @return {string} section list selector
*/
M.course.format.get_section_selector = function(Y) {
return 'li.section';
}
/**
* Swap section
*
* @param {YUI} Y YUI3 instance
* @param {string} node1 node to swap to
* @param {string} node2 node to swap with
* @return {NodeList} section list
*/
M.course.format.swap_sections = function(Y, node1, node2) {
var CSS = {
COURSECONTENT : 'course-content',
LEFT : 'left',
RIGHT : 'right',
SECTIONADDMENUS : 'section_add_menus',
WEEKDATES: 'weekdates'
};
var sectionlist = Y.Node.all('.'+CSS.COURSECONTENT+' '+M.course.format.get_section_selector(Y));
// Swap left block
sectionlist.item(node1).one('.'+CSS.LEFT).swap(sectionlist.item(node2).one('.'+CSS.LEFT));
// Swap right block
sectionlist.item(node1).one('.'+CSS.RIGHT).swap(sectionlist.item(node2).one('.'+CSS.RIGHT));
// Swap menus
sectionlist.item(node1).one('.'+CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one('.'+CSS.SECTIONADDMENUS));
// Swap week dates
sectionlist.item(node1).one('.'+CSS.WEEKDATES).swap(sectionlist.item(node2).one('.'+CSS.WEEKDATES));
}

View File

@ -286,3 +286,6 @@ defined('MOODLE_INTERNAL') || die();
$select->formid = 'sectionmenu';
echo $OUTPUT->render($select);
}
// Include course format js module
$PAGE->requires->js('/course/format/weeks/format.js');

View File

@ -4504,14 +4504,14 @@ function include_course_ajax($course, $modules = array(), $config = null) {
// Include course dragdrop
if ($course->id != SITEID) {
$PAGE->requires->yui_module('moodle-course-dragdrop', 'M.core_course.init_section_dragdrop',
$PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_section_dragdrop',
array(array(
'courseid' => $course->id,
'ajaxurl' => $config->sectionurl,
'config' => $config,
)), null, true);
$PAGE->requires->yui_module('moodle-course-dragdrop', 'M.core_course.init_resource_dragdrop',
$PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_resource_dragdrop',
array(array(
'courseid' => $course->id,
'ajaxurl' => $config->resourceurl,

View File

@ -25,6 +25,8 @@ YUI.add('moodle-course-dragdrop', function(Y) {
DRAGSECTION.superclass.constructor.apply(this, arguments);
};
Y.extend(DRAGSECTION, M.core.dragdrop, {
sectionlistselector : null,
initializer : function(params) {
// Set group for parent class
this.groups = ['section'];
@ -36,8 +38,12 @@ YUI.add('moodle-course-dragdrop', function(Y) {
return false;
}
// Initialise sections dragging
this.setup_for_section('.'+CSS.COURSECONTENT+' li.'+CSS.SECTION);
M.course.coursebase.register_module(this);
try {
this.sectionlistselector = '.'+CSS.COURSECONTENT+' '+M.course.format.get_section_selector(Y);
} catch (e) {
this.sectionlistselector = '.'+CSS.COURSECONTENT+' li.'+CSS.SECTION;
}
this.setup_for_section(this.sectionlistselector);
},
/**
@ -131,7 +137,7 @@ YUI.add('moodle-course-dragdrop', function(Y) {
// Get the list of nodes
drag.get('dragNode').removeClass(CSS.COURSECONTENT);
var sectionlist = Y.Node.all('.'+CSS.COURSECONTENT+' li.'+CSS.SECTION);
var sectionlist = Y.Node.all(this.sectionlistselector);
// Add lightbox if it not there
var lightbox = M.util.add_lightbox(Y, dragnode);
@ -176,16 +182,16 @@ YUI.add('moodle-course-dragdrop', function(Y) {
var sectionid = sectionlist.item(i-1).get('id');
sectionlist.item(i-1).set('id', sectionlist.item(i).get('id'));
sectionlist.item(i).set('id', sectionid);
// Swap left block
sectionlist.item(i-1).one('.'+CSS.LEFT).swap(sectionlist.item(i).one('.'+CSS.LEFT));
// Swap right block
sectionlist.item(i-1).one('.'+CSS.RIGHT).swap(sectionlist.item(i).one('.'+CSS.RIGHT));
// Swap menus
sectionlist.item(i-1).one('.'+CSS.SECTIONADDMENUS).swap(sectionlist.item(i).one('.'+CSS.SECTIONADDMENUS));
// Swap week dates if in weekly format
var weekdates = sectionlist.item(i-1).one('.'+CSS.WEEKDATES);
if (weekdates) {
weekdates.swap(sectionlist.item(i).one('.'+CSS.WEEKDATES));
// See what format needs to be swapped
try {
M.course.format.swap_sections(Y, i-1, i);
} catch (e) {
// Swap left block
sectionlist.item(i-1).one('.'+CSS.LEFT).swap(sectionlist.item(i).one('.'+CSS.LEFT));
// Swap right block
sectionlist.item(i-1).one('.'+CSS.RIGHT).swap(sectionlist.item(i).one('.'+CSS.RIGHT));
// Swap menus
sectionlist.item(i-1).one('.'+CSS.SECTIONADDMENUS).swap(sectionlist.item(i).one('.'+CSS.SECTIONADDMENUS));
}
// Update flag
swapped = true;
@ -229,7 +235,13 @@ YUI.add('moodle-course-dragdrop', function(Y) {
this.parentnodeclass = CSS.SECTION;
// Go through all sections
this.setup_for_section('.'+CSS.COURSECONTENT+' li.'+CSS.SECTION);
try {
var sectionlistselector = '.'+CSS.COURSECONTENT+' '+M.course.format.get_section_selector(Y);
} catch (e) {
var sectionlistselector = '.'+CSS.COURSECONTENT+' li.'+CSS.SECTION;
}
this.setup_for_section(sectionlistselector);
M.course.coursebase.register_module(this);
M.course.dragres = this;
},
@ -311,6 +323,12 @@ YUI.add('moodle-course-dragdrop', function(Y) {
var dragnode = drag.get('node');
var dropnode = e.drop.get('node');
try {
var sectionselector = M.course.format.get_section_selector(Y);
} catch (e) {
var sectionselector = 'li.'+CSS.SECTION;
}
var params = {};
// Handle any variables which we must pass back through to
@ -325,7 +343,7 @@ YUI.add('moodle-course-dragdrop', function(Y) {
params['class'] = 'resource';
params.field = 'move';
params.id = Number(this.get_resource_id(dragnode));
params.sectionId = this.get_section_id(dropnode.ancestor('li.'+CSS.SECTION));
params.sectionId = this.get_section_id(dropnode.ancestor(sectionselector));
if (dragnode.next()) {
params.beforeId = Number(this.get_resource_id(dragnode.next()));
@ -368,11 +386,11 @@ YUI.add('moodle-course-dragdrop', function(Y) {
}
});
M.core_course = M.core_course || {};
M.core_course.init_resource_dragdrop = function(params) {
M.course = M.course || {};
M.course.init_resource_dragdrop = function(params) {
new DRAGRESOURCE(params);
}
M.core_course.init_section_dragdrop = function(params) {
M.course.init_section_dragdrop = function(params) {
new DRAGSECTION(params);
}
}, '@VERSION@', {requires:['base', 'node', 'io', 'dom', 'dd', 'moodle-core-dragdrop', 'moodle-enrol-notification', 'moodle-course-coursebase']});