format.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Collapsed Topics Information
  3. *
  4. * A topic based format that solves the issue of the 'Scroll of Death' when a course has many topics. All topics
  5. * except zero have a toggle that displays that topic. One or more topics can be displayed at any given time.
  6. * Toggles are persistent on a per browser session per course basis but can be made to persist longer by a small
  7. * code change. Full installation instructions, code adaptions and credits are included in the 'Readme.txt' file.
  8. *
  9. * @package format_topcoll
  10. * @version See the value of '$plugin->version' in version.php.
  11. * @copyright © 2012-onwards G J Barnard in respect to modifications of standard topics format.
  12. * @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195}
  13. * @link http://docs.moodle.org/en/Collapsed_Topics_course_format
  14. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. M.course = M.course || {};
  28. M.course.format = M.course.format || {};
  29. /**
  30. * Get sections config for this format
  31. *
  32. * The section structure is:
  33. * <ul class="ctopics">
  34. * <li class="section">...</li>
  35. * <li class="section">...</li>
  36. * ...
  37. * </ul>
  38. *
  39. * @return {object} section list configuration
  40. */
  41. M.course.format.get_config = function() {
  42. return {
  43. container_node : 'ul',
  44. container_class : 'ctopics',
  45. section_node : 'li',
  46. section_class : 'section'
  47. };
  48. };
  49. /**
  50. * Swap section
  51. *
  52. * @param {YUI} Y YUI3 instance.
  53. * @param {string} node1 node to swap to.
  54. * @param {string} node2 node to swap with.
  55. * @return {NodeList} section list.
  56. */
  57. M.course.format.swap_sections = function(Y, node1, node2) {
  58. var CSS = {
  59. COURSECONTENT : '.course-content',
  60. SECTIONADDMENUS : '.section_add_menus'
  61. };
  62. var sectionlist = Y.Node.all(CSS.COURSECONTENT + ' ' + M.course.format.get_section_selector(Y));
  63. // Swap menus.
  64. sectionlist.item(node1).one(CSS.SECTIONADDMENUS).swap(sectionlist.item(node2).one(CSS.SECTIONADDMENUS));
  65. };
  66. /**
  67. * Process sections after ajax response
  68. *
  69. * @param {YUI} Y YUI3 instance
  70. * @param {array} response ajax response
  71. * @param {string} sectionfrom first affected section
  72. * @param {string} sectionto last affected section
  73. * @return void
  74. */
  75. M.course.format.process_sections = function(Y, sectionlist, response, sectionfrom, sectionto) {
  76. var CSS = {
  77. SECTIONNAME : 'the_toggle h3'
  78. },
  79. SELECTORS = {
  80. LEFTCONTENT : '.left .cps_centre',
  81. SECTIONLEFTSIDE : '.left .section-handle .icon'
  82. };
  83. if (response.action == 'move') {
  84. if (sectionfrom > sectionto) { // MDL-34798.
  85. var temp = sectionto;
  86. sectionto = sectionfrom;
  87. sectionfrom = temp;
  88. }
  89. // Update titles and move icons in all affected sections.
  90. var leftcontent, ele, str, stridx, newstr;
  91. for (var i = sectionfrom; i <= sectionto; i++) {
  92. // Update section title.
  93. var content = Y.Node.create('<span>' + response.sectiontitles[i] + '</span>');
  94. sectionlist.item(i).all('.' + CSS.SECTIONNAME).setHTML(content);
  95. // If the left content section number exists, then set it.
  96. leftcontent = sectionlist.item(i).one(SELECTORS.LEFTCONTENT);
  97. if (leftcontent) { // Only set if the section number is shown otherwise JS crashes and stops working.
  98. leftcontent.setContent(i);
  99. }
  100. // Update move icon. MDL-37901.
  101. ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE);
  102. str = ele.getAttribute('alt');
  103. stridx = str.lastIndexOf(' ');
  104. newstr = str.substr(0, stridx + 1) + i;
  105. ele.setAttribute('alt', newstr);
  106. ele.setAttribute('title', newstr); // For FireFox as 'alt' is not refreshed.
  107. if (response.current !== -1) {
  108. if (sectionlist.item(i).hasClass('current')) {
  109. // Remove the current class as section has been moved. MDL-33546.
  110. sectionlist.item(i).removeClass('current');
  111. }
  112. }
  113. }
  114. // If there is a current section, apply corresponding class in order to highlight it. MDL-33546.
  115. if (response.current !== -1) {
  116. // Add current class to the required section.
  117. sectionlist.item(response.current).addClass('current');
  118. }
  119. }
  120. };