blocks/starredcourses/amd/src/main.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Javascript to initialise the starred courses block.
  17. *
  18. * @module block_starredcourses/main
  19. * @copyright 2018 Simey Lameze <simey@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(
  23. [
  24. 'jquery',
  25. 'core/notification',
  26. 'block_starredcourses/repository',
  27. 'core/pubsub',
  28. 'core/templates',
  29. 'core_course/events'
  30. ],
  31. function(
  32. $,
  33. Notification,
  34. Repository,
  35. PubSub,
  36. Templates,
  37. CourseEvents
  38. ) {
  39. var SELECTORS = {
  40. BLOCK_CONTAINER: '[data-region="starred-courses"]',
  41. STARRED_COURSES_REGION_VIEW: '[data-region="starred-courses-view"]',
  42. STARRED_COURSES_REGION: '[data-region="starred-courses-view-content"]'
  43. };
  44. /**
  45. * Render the starred courses.
  46. *
  47. * @method renderCourses
  48. * @param {object} root The root element for the starred view.
  49. * @param {array} courses containing array of returned courses.
  50. * @returns {promise} Resolved with HTML and JS strings
  51. */
  52. var renderCourses = function(root, courses) {
  53. if (courses.length > 0) {
  54. return Templates.render('core_course/view-cards', {
  55. courses: courses
  56. });
  57. } else {
  58. var nocoursesimg = root.find(SELECTORS.STARRED_COURSES_REGION_VIEW).attr('data-nocoursesimg');
  59. return Templates.render('block_starredcourses/no-courses', {
  60. nocoursesimg: nocoursesimg
  61. });
  62. }
  63. };
  64. /**
  65. * Fetch user's starred courses and reload the content of the block.
  66. *
  67. * @param {object} root The root element for the starred view.
  68. * @returns {promise} The updated content for the block.
  69. */
  70. var reloadContent = function(root) {
  71. var content = root.find(SELECTORS.STARRED_COURSES_REGION);
  72. var args = {
  73. limit: 0,
  74. offset: 0,
  75. };
  76. return Repository.getStarredCourses(args)
  77. .then(function(courses) {
  78. // Whether the course category should be displayed in the course item.
  79. var showcoursecategory = $(SELECTORS.BLOCK_CONTAINER).data('displaycoursecategory');
  80. courses = courses.map(function(course) {
  81. course.showcoursecategory = showcoursecategory;
  82. return course;
  83. });
  84. return renderCourses(root, courses);
  85. }).then(function(html, js) {
  86. return Templates.replaceNodeContents(content, html, js);
  87. }).catch(Notification.exception);
  88. };
  89. /**
  90. * Register event listeners for the block.
  91. *
  92. * @param {object} root The calendar root element
  93. */
  94. var registerEventListeners = function(root) {
  95. PubSub.subscribe(CourseEvents.favourited, function() {
  96. reloadContent(root);
  97. });
  98. PubSub.subscribe(CourseEvents.unfavorited, function() {
  99. reloadContent(root);
  100. });
  101. };
  102. /**
  103. * Initialise all of the modules for the starred courses block.
  104. *
  105. * @param {object} root The root element for the block.
  106. */
  107. var init = function(root) {
  108. root = $(root);
  109. registerEventListeners(root);
  110. reloadContent(root);
  111. };
  112. return {
  113. init: init
  114. };
  115. });