diff --git a/course/format/topics/format.js b/course/format/topics/format.js new file mode 100644 index 00000000000..26f28015f57 --- /dev/null +++ b/course/format/topics/format.js @@ -0,0 +1,40 @@ +// 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', + }; + + 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)); +} diff --git a/course/format/topics/format.php b/course/format/topics/format.php index 87724336acb..fc0d138da22 100644 --- a/course/format/topics/format.php +++ b/course/format/topics/format.php @@ -286,3 +286,6 @@ if (!empty($sectionmenu)) { $select->formid = 'sectionmenu'; echo $OUTPUT->render($select); } + + // Include course format js module + $PAGE->requires->js('/course/format/topics/format.js'); diff --git a/course/yui/dragdrop/dragdrop.js b/course/yui/dragdrop/dragdrop.js index 05b2109e263..595252d0c77 100644 --- a/course/yui/dragdrop/dragdrop.js +++ b/course/yui/dragdrop/dragdrop.js @@ -38,12 +38,10 @@ YUI.add('moodle-course-dragdrop', function(Y) { return false; } // Initialise sections dragging - try { + if (M.course.format && M.course.format.get_section_selector && typeof(M.course.format.get_section_selector) == 'function') { 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); } - this.setup_for_section(this.sectionlistselector); }, /** @@ -183,15 +181,8 @@ YUI.add('moodle-course-dragdrop', function(Y) { sectionlist.item(i-1).set('id', sectionlist.item(i).get('id')); sectionlist.item(i).set('id', sectionid); // See what format needs to be swapped - try { + if (M.course.format && M.course.format.swap_sections && typeof(M.course.format.swap_sections) == 'function') { 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; @@ -235,15 +226,12 @@ YUI.add('moodle-course-dragdrop', function(Y) { this.parentnodeclass = CSS.SECTION; // Go through all sections - try { + if (M.course.format && M.course.format.get_section_selector && typeof(M.course.format.get_section_selector) == 'function') { 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; } - - this.setup_for_section(sectionlistselector); - M.course.coursebase.register_module(this); - M.course.dragres = this; }, /** @@ -323,11 +311,7 @@ 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 sectionselector = M.course.format.get_section_selector(Y); var params = {};