message/output/popup/amd/src/notification_area_content_area.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. * Controls the content area of the notification area on the
  17. * notification page.
  18. *
  19. * @module message_popup/notification_area_content_area
  20. * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. define(['jquery', 'core/templates', 'core/notification', 'core/custom_interaction_events',
  24. 'message_popup/notification_repository', 'message_popup/notification_area_events'],
  25. function($, Templates, DebugNotification, CustomEvents, NotificationRepo, NotificationAreaEvents) {
  26. var SELECTORS = {
  27. CONTAINER: '[data-region="notification-area"]',
  28. CONTENT: '[data-region="content"]',
  29. HEADER: '[data-region="header"]',
  30. FOOTER: '[data-region="footer"]',
  31. TOGGLE_MODE: '[data-action="toggle-mode"]',
  32. };
  33. var TEMPLATES = {
  34. HEADER: 'message_popup/notification_area_content_area_header',
  35. CONTENT: 'message_popup/notification_area_content_area_content',
  36. FOOTER: 'message_popup/notification_area_content_area_footer',
  37. };
  38. /**
  39. * Constructor for the ContentArea
  40. *
  41. * @class
  42. * @param {object} root The root element for the content area
  43. * @param {int} userId The user id of the current user
  44. */
  45. var ContentArea = function(root, userId) {
  46. this.root = $(root);
  47. this.container = this.root.closest(SELECTORS.CONTAINER);
  48. this.userId = userId;
  49. this.header = this.root.find(SELECTORS.HEADER);
  50. this.content = this.root.find(SELECTORS.CONTENT);
  51. this.footer = this.root.find(SELECTORS.FOOTER);
  52. this.registerEventListeners();
  53. };
  54. /**
  55. * Get the root element.
  56. *
  57. * @method getRoot
  58. * @return {object} jQuery element
  59. */
  60. ContentArea.prototype.getRoot = function() {
  61. return this.root;
  62. };
  63. /**
  64. * Get the container element (which the content area is within).
  65. *
  66. * @method getContainer
  67. * @return {object} jQuery element
  68. */
  69. ContentArea.prototype.getContainer = function() {
  70. return this.container;
  71. };
  72. /**
  73. * Get the user id.
  74. *
  75. * @method getUserId
  76. * @return {int}
  77. */
  78. ContentArea.prototype.getUserId = function() {
  79. return this.userId;
  80. };
  81. /**
  82. * Get the content area header element.
  83. *
  84. * @method getHeader
  85. * @return {object} jQuery element
  86. */
  87. ContentArea.prototype.getHeader = function() {
  88. return this.header;
  89. };
  90. /**
  91. * Get the content area content element.
  92. *
  93. * @method getContent
  94. * @return {object} jQuery element
  95. */
  96. ContentArea.prototype.getContent = function() {
  97. return this.content;
  98. };
  99. /**
  100. * Get the content area footer element.
  101. *
  102. * @method getFooter
  103. * @return {object} jQuery element
  104. */
  105. ContentArea.prototype.getFooter = function() {
  106. return this.footer;
  107. };
  108. /**
  109. * Display the content area. Typically used with responsive
  110. * styling on smaller screens.
  111. *
  112. * @method show
  113. */
  114. ContentArea.prototype.show = function() {
  115. this.getContainer().addClass('show-content-area');
  116. };
  117. /**
  118. * Hide the content area. Typically used with responsive
  119. * styling on smaller screens.
  120. *
  121. * @method hide
  122. */
  123. ContentArea.prototype.hide = function() {
  124. this.getContainer().removeClass('show-content-area');
  125. };
  126. /**
  127. * Change the HTML in the content area header element.
  128. *
  129. * @method setHeaderHTML
  130. * @param {string} html The HTML to be set
  131. */
  132. ContentArea.prototype.setHeaderHTML = function(html) {
  133. this.getHeader().empty().html(html);
  134. };
  135. /**
  136. * Change the HTML in the content area content element.
  137. *
  138. * @method setContentHMTL
  139. * @param {string} html The HTML to be set.
  140. */
  141. ContentArea.prototype.setContentHTML = function(html) {
  142. this.getContent().empty().html(html);
  143. };
  144. /**
  145. * Change the HTML in the content area footer element.
  146. *
  147. * @method setFooterHTML
  148. * @param {string} html The HTML to be set.
  149. */
  150. ContentArea.prototype.setFooterHTML = function(html) {
  151. this.getFooter().empty().html(html);
  152. };
  153. /**
  154. * Render the given notification context in the content area.
  155. *
  156. * @method showNotification
  157. * @param {object} notification The notification context (from a webservice)
  158. * @return {object} jQuery promise
  159. */
  160. ContentArea.prototype.showNotification = function(notification) {
  161. var headerPromise = Templates.render(TEMPLATES.HEADER, notification).done(function(html) {
  162. this.setHeaderHTML(html);
  163. }.bind(this));
  164. var contentPromise = Templates.render(TEMPLATES.CONTENT, notification).done(function(html) {
  165. this.setContentHTML(html);
  166. }.bind(this));
  167. var footerPromise = Templates.render(TEMPLATES.FOOTER, notification).done(function(html) {
  168. this.setFooterHTML(html);
  169. }.bind(this));
  170. return $.when(headerPromise, contentPromise, footerPromise).done(function() {
  171. this.show();
  172. this.getContainer().trigger(NotificationAreaEvents.notificationShown, [notification]);
  173. }.bind(this));
  174. };
  175. /**
  176. * Create the event listeners for the content area.
  177. *
  178. * @method registerEventListeners
  179. */
  180. ContentArea.prototype.registerEventListeners = function() {
  181. CustomEvents.define(this.getRoot(), [
  182. CustomEvents.events.activate
  183. ]);
  184. this.getRoot().on(CustomEvents.events.activate, SELECTORS.VIEW_TOGGLE, function() {
  185. this.hide();
  186. }.bind(this));
  187. this.getContainer().on(NotificationAreaEvents.showNotification, function(e, notification) {
  188. this.showNotification(notification);
  189. }.bind(this));
  190. };
  191. return ContentArea;
  192. });