[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 /** 17 * A system for displaying notifications to users from the session. 18 * 19 * Wrapper for the YUI M.core.notification class. Allows us to 20 * use the YUI version in AMD code until it is replaced. 21 * 22 * @module core/notification 23 * @class notification 24 * @package core 25 * @copyright 2015 Damyon Wiese <damyon@moodle.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 * @since 2.9 28 */ 29 define(['core/yui', 'jquery', 'core/log'], 30 function(Y, $, log) { 31 var notificationModule = { 32 types: { 33 'success': 'core/notification_success', 34 'info': 'core/notification_info', 35 'warning': 'core/notification_warning', 36 'error': 'core/notification_error', 37 }, 38 39 fieldName: 'user-notifications', 40 41 fetchNotifications: function() { 42 require(['core/ajax'], function(ajax) { 43 var promises = ajax.call([{ 44 methodname: 'core_fetch_notifications', 45 args: { 46 contextid: notificationModule.contextid 47 } 48 }]); 49 50 promises[0] 51 .done(notificationModule.addNotifications) 52 ; 53 }); 54 }, 55 56 addNotifications: function(notifications) { 57 if (!notifications) { 58 notifications = []; 59 } 60 61 $.each(notifications, function(i, notification) { 62 notificationModule.renderNotification(notification.template, notification.variables); 63 }); 64 }, 65 66 setupTargetRegion: function() { 67 var targetRegion = $('#' + notificationModule.fieldName); 68 if (targetRegion.length) { 69 return false; 70 } 71 72 var newRegion = $('<span>').attr('id', notificationModule.fieldName); 73 74 targetRegion = $('#region-main'); 75 if (targetRegion.length) { 76 return targetRegion.prepend(newRegion); 77 } 78 79 targetRegion = $('[role="main"]'); 80 if (targetRegion.length) { 81 return targetRegion.prepend(newRegion); 82 } 83 84 targetRegion = $('body'); 85 return targetRegion.prepend(newRegion); 86 }, 87 88 addNotification: function(notification) { 89 var template = notificationModule.types.error; 90 91 notification = $.extend({ 92 closebutton: true, 93 announce: true, 94 type: 'error' 95 }, notification); 96 97 if (notification.template) { 98 template = notification.template; 99 delete notification.template; 100 } else if (notification.type) { 101 if (typeof notificationModule.types[notification.type] !== 'undefined') { 102 template = notificationModule.types[notification.type]; 103 } 104 delete notification.type; 105 } 106 107 return notificationModule.renderNotification(template, notification); 108 }, 109 110 renderNotification: function(template, variables) { 111 if (typeof variables.message === 'undefined' || !variables.message) { 112 log.debug('Notification received without content. Skipping.'); 113 return; 114 } 115 require(['core/templates'], function(templates) { 116 templates.render(template, variables) 117 .done(function(html, js) { 118 $('#' + notificationModule.fieldName).prepend(html); 119 templates.runTemplateJS(js); 120 }) 121 .fail(notificationModule.exception) 122 ; 123 }); 124 }, 125 126 alert: function(title, message, yesLabel) { 127 // Here we are wrapping YUI. This allows us to start transitioning, but 128 // wait for a good alternative without having inconsistent dialogues. 129 Y.use('moodle-core-notification-alert', function() { 130 var alert = new M.core.alert({ 131 title: title, 132 message: message, 133 yesLabel: yesLabel 134 }); 135 136 alert.show(); 137 }); 138 }, 139 140 confirm: function(title, question, yesLabel, noLabel, yesCallback, noCallback) { 141 // Here we are wrapping YUI. This allows us to start transitioning, but 142 // wait for a good alternative without having inconsistent dialogues. 143 Y.use('moodle-core-notification-confirm', function() { 144 var modal = new M.core.confirm({ 145 title: title, 146 question: question, 147 yesLabel: yesLabel, 148 noLabel: noLabel 149 }); 150 151 modal.on('complete-yes', function() { 152 yesCallback(); 153 }); 154 if (noCallback) { 155 modal.on('complete-no', function() { 156 noCallback(); 157 }); 158 } 159 modal.show(); 160 }); 161 }, 162 163 exception: function(ex) { 164 // Fudge some parameters. 165 if (ex.backtrace) { 166 ex.lineNumber = ex.backtrace[0].line; 167 ex.fileName = ex.backtrace[0].file; 168 ex.fileName = '...' + ex.fileName.substr(ex.fileName.length - 20); 169 ex.stack = ex.debuginfo; 170 ex.name = ex.errorcode; 171 } 172 Y.use('moodle-core-notification-exception', function() { 173 var modal = new M.core.exception(ex); 174 175 modal.show(); 176 }); 177 } 178 }; 179 180 return /** @alias module:core/notification */{ 181 init: function(contextid, notifications) { 182 notificationModule.contextid = contextid; 183 184 // Setup the message target region if it isn't setup already 185 notificationModule.setupTargetRegion(); 186 187 // Add provided notifications. 188 notificationModule.addNotifications(notifications); 189 190 // Poll for any new notifications. 191 notificationModule.fetchNotifications(); 192 }, 193 194 /** 195 * Poll the server for any new notifications. 196 * 197 * @method fetchNotifications 198 */ 199 fetchNotifications: notificationModule.fetchNotifications, 200 201 /** 202 * Add a notification to the page. 203 * 204 * Note: This does not cause the notification to be added to the session. 205 * 206 * @method addNotification 207 * @param {Object} notification The notification to add. 208 * @param {string} notification.message The body of the notification 209 * @param {string} notification.type The type of notification to add (error, warning, info, success). 210 * @param {Boolean} notification.closebutton Whether to show the close button. 211 * @param {Boolean} notification.announce Whether to announce to screen readers. 212 */ 213 addNotification: notificationModule.addNotification, 214 215 /** 216 * Wrap M.core.alert. 217 * 218 * @method alert 219 * @param {string} title 220 * @param {string} message 221 * @param {string} yesLabel 222 */ 223 alert: notificationModule.alert, 224 225 /** 226 * Wrap M.core.confirm. 227 * 228 * @method confirm 229 * @param {string} title 230 * @param {string} question 231 * @param {string} yesLabel 232 * @param {string} noLabel 233 * @param {function} yesCallback 234 * @param {function} noCallback Optional parameter to be called if the user presses cancel. 235 */ 236 confirm: notificationModule.confirm, 237 238 /** 239 * Wrap M.core.exception. 240 * 241 * @method exception 242 * @param {Error} ex 243 */ 244 exception: notificationModule.exception 245 }; 246 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |