blocks/recentlyaccesseditems/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 Recently accessed items block.
  17. *
  18. * @module block_recentlyaccesseditems/main
  19. * @copyright 2018 Victor Deniz <victor@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(
  23. [
  24. 'jquery',
  25. 'block_recentlyaccesseditems/repository',
  26. 'core/templates',
  27. 'core/notification'
  28. ],
  29. function(
  30. $,
  31. Repository,
  32. Templates,
  33. Notification
  34. ) {
  35. var NUM_ITEMS = 9;
  36. // Maximum number of elements to display in the block initially.
  37. var NUM_ITEMS_INIT = 3;
  38. var SELECTORS = {
  39. CARDDECK_CONTAINER: '[data-region="recentlyaccesseditems-view"]',
  40. CARDDECK: '[data-region="recentlyaccesseditems-view-content"]',
  41. SHOWMORE_LINK: '[data-region="recentlyaccesseditems-view"] [data-action="more-items"]',
  42. };
  43. /**
  44. * Register event listeners.
  45. */
  46. const registerEventListeners = () => {
  47. const showmoreLink = document.querySelector(SELECTORS.SHOWMORE_LINK);
  48. // Hide "Show more" link and show additional items.
  49. showmoreLink.addEventListener('click', () => {
  50. showmoreLink.classList.add('d-none');
  51. const hiddenItems = document.querySelector('[data-region="items-list"]').children;
  52. for (const hiddenItem of hiddenItems) {
  53. hiddenItem.style = "display: block";
  54. }
  55. });
  56. };
  57. /**
  58. * Get recent items from backend.
  59. *
  60. * @method getRecentItems
  61. * @param {int} limit Only return this many results
  62. * @return {array} Items user most recently has accessed
  63. */
  64. var getRecentItems = function(limit) {
  65. return Repository.getRecentItems(limit);
  66. };
  67. /**
  68. * Render the block content.
  69. *
  70. * @method renderItems
  71. * @param {object} root The root element for the items view.
  72. * @param {array} items containing array of returned items.
  73. * @return {promise} Resolved with HTML and JS strings
  74. */
  75. var renderItems = function(root, items) {
  76. if (items.length > 0) {
  77. let hasmoreitems = false;
  78. if (items.length > NUM_ITEMS_INIT) {
  79. hasmoreitems = true;
  80. }
  81. return Templates.render('block_recentlyaccesseditems/view-cards', {
  82. items: items,
  83. hasmoreitems: hasmoreitems
  84. });
  85. } else {
  86. var noitemsimgurl = root.attr('data-noitemsimgurl');
  87. return Templates.render('block_recentlyaccesseditems/no-items', {
  88. noitemsimgurl: noitemsimgurl
  89. });
  90. }
  91. };
  92. /**
  93. * Get and show the recent items into the block.
  94. *
  95. * @param {object} root The root element for the items block.
  96. */
  97. var init = function(root) {
  98. root = $(root);
  99. var itemsContainer = root.find(SELECTORS.CARDDECK_CONTAINER);
  100. var itemsContent = root.find(SELECTORS.CARDDECK);
  101. var itemsPromise = getRecentItems(NUM_ITEMS);
  102. itemsPromise.then(function(items) {
  103. var pageContentPromise = renderItems(itemsContainer, items);
  104. pageContentPromise.then(function(html, js) {
  105. Templates.replaceNodeContents(itemsContent, html, js);
  106. if (items.length > 3) {
  107. registerEventListeners();
  108. }
  109. return null;
  110. }).catch(Notification.exception);
  111. }).catch(Notification.exception);
  112. };
  113. return {
  114. init: init
  115. };
  116. });