MDL-30811 javascript: alert JS should be in template

By moving the JS required for an alert to the JavaScript we make it
possible for themers to use frameworks other than bootstrap if they so
wish.
This commit is contained in:
Andrew Nicols 2016-03-03 14:37:40 +08:00
parent 11a67b211b
commit c1cb65d470
6 changed files with 29 additions and 7 deletions

View File

@ -1 +1 @@
define(["core/yui","jquery","theme_bootstrapbase/bootstrap","core/log"],function(a,b,c,d){var e={types:{success:"core/notification_success",info:"core/notification_info",warning:"core/notification_warning",error:"core/notification_error"},fieldName:"user-notifications",fetchNotifications:function(){require(["core/ajax"],function(a){var b=a.call([{methodname:"core_fetch_notifications",args:{contextid:e.contextid}}]);b[0].done(e.addNotifications)})},addNotifications:function(a){a||(a=[]),b.each(a,function(a,b){e.renderNotification(b.template,b.variables)})},setupTargetRegion:function(){var a=b("#"+e.fieldName);if(!a.length){var c=b("<span>").attr("id",e.fieldName);return a=b("#region-main"),a.length?a.prepend(c):(a=b('[role="main"]'),a.length?a.prepend(c):(a=b("body"),a.prepend(c)))}},addNotification:function(a){var c=e.types.error;return a=b.extend({closebutton:!0,announce:!0,type:"error"},a),a.template?(c=a.template,delete a.template):a.type&&("undefined"!=typeof e.types[a.type]&&(c=e.types[a.type]),delete a.type),e.renderNotification(c,a)},renderNotification:function(a,c){return"undefined"!=typeof c.message&&c.message?void require(["core/templates"],function(d){d.render(a,c).done(function(a){b("#"+e.fieldName).prepend(a)}).fail(e.exception)}):void d.debug("Notification received without content. Skipping.")},alert:function(b,c,d){a.use("moodle-core-notification-alert",function(){var a=new M.core.alert({title:b,message:c,yesLabel:d});a.show()})},confirm:function(b,c,d,e,f){a.use("moodle-core-notification-confirm",function(){var a=new M.core.confirm({title:b,question:c,yesLabel:d,noLabel:e});a.on("complete-yes",function(){f()}),a.show()})},exception:function(b){b.backtrace&&(b.lineNumber=b.backtrace[0].line,b.fileName=b.backtrace[0].file,b.fileName="..."+b.fileName.substr(b.fileName.length-20),b.stack=b.debuginfo,b.name=b.errorcode),a.use("moodle-core-notification-exception",function(){var a=new M.core.exception(b);a.show()})}};return{init:function(a,c){e.contextid=a,e.setupTargetRegion(),b().alert(),e.addNotifications(c),e.fetchNotifications()},fetchNotifications:e.fetchNotifications,addNotification:e.addNotification,alert:e.alert,confirm:e.confirm,exception:e.exception}});
define(["core/yui","jquery","core/log"],function(a,b,c){var d={types:{success:"core/notification_success",info:"core/notification_info",warning:"core/notification_warning",error:"core/notification_error"},fieldName:"user-notifications",fetchNotifications:function(){require(["core/ajax"],function(a){var b=a.call([{methodname:"core_fetch_notifications",args:{contextid:d.contextid}}]);b[0].done(d.addNotifications)})},addNotifications:function(a){a||(a=[]),b.each(a,function(a,b){d.renderNotification(b.template,b.variables)})},setupTargetRegion:function(){var a=b("#"+d.fieldName);if(!a.length){var c=b("<span>").attr("id",d.fieldName);return a=b("#region-main"),a.length?a.prepend(c):(a=b('[role="main"]'),a.length?a.prepend(c):(a=b("body"),a.prepend(c)))}},addNotification:function(a){var c=d.types.error;return a=b.extend({closebutton:!0,announce:!0,type:"error"},a),a.template?(c=a.template,delete a.template):a.type&&("undefined"!=typeof d.types[a.type]&&(c=d.types[a.type]),delete a.type),d.renderNotification(c,a)},renderNotification:function(a,e){return"undefined"!=typeof e.message&&e.message?void require(["core/templates"],function(c){c.render(a,e).done(function(a,e){b("#"+d.fieldName).prepend(a),c.runTemplateJS(e)}).fail(d.exception)}):void c.debug("Notification received without content. Skipping.")},alert:function(b,c,d){a.use("moodle-core-notification-alert",function(){var a=new M.core.alert({title:b,message:c,yesLabel:d});a.show()})},confirm:function(b,c,d,e,f){a.use("moodle-core-notification-confirm",function(){var a=new M.core.confirm({title:b,question:c,yesLabel:d,noLabel:e});a.on("complete-yes",function(){f()}),a.show()})},exception:function(b){b.backtrace&&(b.lineNumber=b.backtrace[0].line,b.fileName=b.backtrace[0].file,b.fileName="..."+b.fileName.substr(b.fileName.length-20),b.stack=b.debuginfo,b.name=b.errorcode),a.use("moodle-core-notification-exception",function(){var a=new M.core.exception(b);a.show()})}};return{init:function(a,b){d.contextid=a,d.setupTargetRegion(),d.addNotifications(b),d.fetchNotifications()},fetchNotifications:d.fetchNotifications,addNotification:d.addNotification,alert:d.alert,confirm:d.confirm,exception:d.exception}});

View File

@ -26,8 +26,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
define(['core/yui', 'jquery', 'theme_bootstrapbase/bootstrap', 'core/log'],
function(Y, $, bootstrap, log) {
define(['core/yui', 'jquery', 'core/log'],
function(Y, $, log) {
var notificationModule = {
types: {
'success': 'core/notification_success',
@ -114,8 +114,9 @@ function(Y, $, bootstrap, log) {
}
require(['core/templates'], function(templates) {
templates.render(template, variables)
.done(function(html) {
.done(function(html, js) {
$('#' + notificationModule.fieldName).prepend(html);
templates.runTemplateJS(js);
})
.fail(notificationModule.exception)
;
@ -178,9 +179,6 @@ function(Y, $, bootstrap, log) {
// Setup the message target region if it isn't setup already
notificationModule.setupTargetRegion();
// Setup closing of bootstrap alerts.
$().alert();
// Add provided notifications.
notificationModule.addNotifications(notifications);

View File

@ -42,3 +42,9 @@
{{# closebutton }}<button type="button" class="close" data-dismiss="alert">&times;</button>{{/ closebutton }}
{{{ message }}}
</div>
{{# js }}
require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) {
// Setup closing of bootstrap alerts.
$().alert();
});
{{/ js }}

View File

@ -42,3 +42,9 @@
{{# closebutton }}<button type="button" class="close" data-dismiss="alert">&times;</button>{{/ closebutton }}
{{{ message }}}
</div>
{{# js }}
require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) {
// Setup closing of bootstrap alerts.
$().alert();
});
{{/ js }}

View File

@ -42,3 +42,9 @@
{{# closebutton }}<button type="button" class="close" data-dismiss="alert">&times;</button>{{/ closebutton }}
{{{ message }}}
</div>
{{# js }}
require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) {
// Setup closing of bootstrap alerts.
$().alert();
});
{{/ js }}

View File

@ -42,3 +42,9 @@
{{# closebutton }}<button type="button" class="close" data-dismiss="alert">&times;</button>{{/ closebutton }}
{{{ message }}}
</div>
{{# js }}
require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) {
// Setup closing of bootstrap alerts.
$().alert();
});
{{/ js }}