1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Javascript to initialise the starred courses block.
19 * @copyright 2018 Simey Lameze <simey@moodle.com>
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 'block_starredcourses/repository',
42 STARRED_COURSES_REGION_VIEW: '[data-region="starred-courses-view"]',
43 STARRED_COURSES_REGION: '[data-region="starred-courses-view-content"]'
47 * Render the starred courses.
49 * @method renderCourses
50 * @param {object} root The root element for the starred view.
51 * @param {array} courses containing array of returned courses.
52 * @returns {promise} Resolved with HTML and JS strings
54 var renderCourses = function(root, courses) {
55 if (courses.length > 0) {
56 return Templates.render('block_starredcourses/view-cards', {
60 var nocoursesimg = root.find(SELECTORS.STARRED_COURSES_REGION_VIEW).attr('data-nocoursesimg');
61 return Templates.render('block_starredcourses/no-courses', {
62 nocoursesimg: nocoursesimg
68 * Fetch user's starred courses and reload the content of the block.
70 * @param {object} root The root element for the starred view.
71 * @returns {promise} The updated content for the block.
73 var reloadContent = function(root) {
74 var content = root.find(SELECTORS.STARRED_COURSES_REGION);
81 return Repository.getStarredCourses(args)
82 .then(function(courses) {
83 return renderCourses(root, courses);
84 }).then(function(html, js) {
85 return Templates.replaceNodeContents(content, html, js);
86 }).catch(Notification.exception);
90 * Register event listeners for the block.
92 * @param {object} root The calendar root element
94 var registerEventListeners = function(root) {
95 PubSub.subscribe(CourseEvents.favourited, function() {
99 PubSub.subscribe(CourseEvents.unfavorited, function() {
105 * Initialise all of the modules for the starred courses block.
107 * @param {object} root The root element for the block.
109 var init = function(root) {
112 registerEventListeners(root);