From c1cb65d4707713e5d9cef6b63a73a11f2c51a5bf Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 3 Mar 2016 14:37:40 +0800 Subject: [PATCH] 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. --- lib/amd/build/notification.min.js | 2 +- lib/amd/src/notification.js | 10 ++++------ lib/templates/notification_error.mustache | 6 ++++++ lib/templates/notification_info.mustache | 6 ++++++ lib/templates/notification_success.mustache | 6 ++++++ lib/templates/notification_warning.mustache | 6 ++++++ 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/amd/build/notification.min.js b/lib/amd/build/notification.min.js index d6b1ee74e5f..f03cbbb04a0 100644 --- a/lib/amd/build/notification.min.js +++ b/lib/amd/build/notification.min.js @@ -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("").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}}); \ No newline at end of file +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("").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}}); \ No newline at end of file diff --git a/lib/amd/src/notification.js b/lib/amd/src/notification.js index bb8cdb4c65d..da34f43461e 100644 --- a/lib/amd/src/notification.js +++ b/lib/amd/src/notification.js @@ -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); diff --git a/lib/templates/notification_error.mustache b/lib/templates/notification_error.mustache index 0e7a8c8448b..8eb98846706 100644 --- a/lib/templates/notification_error.mustache +++ b/lib/templates/notification_error.mustache @@ -42,3 +42,9 @@ {{# closebutton }}{{/ closebutton }} {{{ message }}} +{{# js }} +require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) { + // Setup closing of bootstrap alerts. + $().alert(); +}); +{{/ js }} diff --git a/lib/templates/notification_info.mustache b/lib/templates/notification_info.mustache index 39cd1511c94..4a596494d5b 100644 --- a/lib/templates/notification_info.mustache +++ b/lib/templates/notification_info.mustache @@ -42,3 +42,9 @@ {{# closebutton }}{{/ closebutton }} {{{ message }}} +{{# js }} +require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) { + // Setup closing of bootstrap alerts. + $().alert(); +}); +{{/ js }} diff --git a/lib/templates/notification_success.mustache b/lib/templates/notification_success.mustache index 65b7e48321f..150fc3f51b4 100644 --- a/lib/templates/notification_success.mustache +++ b/lib/templates/notification_success.mustache @@ -42,3 +42,9 @@ {{# closebutton }}{{/ closebutton }} {{{ message }}} +{{# js }} +require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) { + // Setup closing of bootstrap alerts. + $().alert(); +}); +{{/ js }} diff --git a/lib/templates/notification_warning.mustache b/lib/templates/notification_warning.mustache index b359d830ae5..c5c99447a58 100644 --- a/lib/templates/notification_warning.mustache +++ b/lib/templates/notification_warning.mustache @@ -42,3 +42,9 @@ {{# closebutton }}{{/ closebutton }} {{{ message }}} +{{# js }} +require(['jquery', 'theme_bootstrapbase/bootstrap'], function($) { + // Setup closing of bootstrap alerts. + $().alert(); +}); +{{/ js }}