MDL-78306 message: Convert notification_processor_settings modal to ESM

This commit is contained in:
Andrew Nicols 2023-05-18 14:25:16 +08:00
parent 406623116c
commit c8a7a11230
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
3 changed files with 80 additions and 110 deletions

View File

@ -1,10 +1,3 @@
/**
* Load the settings for a message processor.
*
* @module core_message/notification_processor_settings
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("core_message/notification_processor_settings",["jquery","core/ajax","core/str","core/notification","core/custom_interaction_events","core/modal","core/modal_registry","core/fragment"],(function($,Ajax,Str,Notification,CustomEvents,Modal,ModalRegistry,Fragment){var registered=!1,SELECTORS_SAVE_BUTTON='[data-action="save"]',SELECTORS_CANCEL_BUTTON='[data-action="cancel"]',SELECTORS_PROCESSOR="[data-processor-name]",SELECTORS_PREFERENCE_ROW='[data-region="preference-row"]',NotificationProcessorSettings=function(root){Modal.call(this,root),this.name=null,this.userId=null,this.contextId=null,this.element=null,this.saveButton=this.getFooter().find(SELECTORS_SAVE_BUTTON),this.cancelButton=this.getFooter().find(SELECTORS_CANCEL_BUTTON)};return NotificationProcessorSettings.TYPE="core_message-notification_processor_settings",(NotificationProcessorSettings.prototype=Object.create(Modal.prototype)).constructor=NotificationProcessorSettings,NotificationProcessorSettings.prototype.setUserId=function(id){this.userId=id},NotificationProcessorSettings.prototype.getUserId=function(){return this.userId},NotificationProcessorSettings.prototype.setElement=function(element){this.element=element},NotificationProcessorSettings.prototype.getElement=function(){return this.element},NotificationProcessorSettings.prototype.setName=function(name){this.name=name},NotificationProcessorSettings.prototype.getName=function(){return this.name},NotificationProcessorSettings.prototype.setContextId=function(id){this.contextId=id},NotificationProcessorSettings.prototype.getContextId=function(){return this.contextId},NotificationProcessorSettings.prototype.getForm=function(){return this.getBody().find("form")},NotificationProcessorSettings.prototype.disableButtons=function(){this.saveButton.prop("disabled",!0),this.cancelButton.prop("disabled",!0)},NotificationProcessorSettings.prototype.enableButtons=function(){this.saveButton.prop("disabled",!1),this.cancelButton.prop("disabled",!1)},NotificationProcessorSettings.prototype.loadTitleContent=function(){return this.titlePromise=Str.get_string("processorsettings","message"),this.setTitle(this.titlePromise),this.titlePromise},NotificationProcessorSettings.prototype.loadBodyContent=function(){this.disableButtons();var args={userid:this.getUserId(),type:this.getName()};return this.bodyPromise=Fragment.loadFragment("message","processor_settings",this.getContextId(),args),this.setBody(this.bodyPromise),this.bodyPromise.then(function(){this.enableButtons()}.bind(this)).fail(Notification.exception),this.bodyPromise},NotificationProcessorSettings.prototype.loadAllContent=function(){return $.when(this.loadTitleContent(),this.loadBodyContent())},NotificationProcessorSettings.prototype.show=function(){this.loadAllContent(),Modal.prototype.show.call(this)},NotificationProcessorSettings.prototype.hide=function(){Modal.prototype.hide.call(this),this.setContextId(null),this.setName(null),this.setUserId(null)},NotificationProcessorSettings.prototype.updateConfiguredStatus=function(){var processorHeader=$(this.getElement()).closest(SELECTORS_PROCESSOR);if(!processorHeader.hasClass("unconfigured"))return!1;var processorName=processorHeader.attr("data-processor-name"),request={methodname:"core_message_get_message_processor",args:{name:processorName,userid:this.userId}};return Ajax.call([request])[0].fail(Notification.exception).done((function(result){if(result.userconfigured){var notifications=$(SELECTORS_PREFERENCE_ROW+' [data-processor-name="'+processorName+'"]');processorHeader.removeClass("unconfigured"),notifications.removeClass("disabled")}}))},NotificationProcessorSettings.prototype.registerEventListeners=function(){Modal.prototype.registerEventListeners.call(this),this.getModal().on(CustomEvents.events.activate,SELECTORS_SAVE_BUTTON,function(e,data){this.getForm().submit(),data.originalEvent.preventDefault()}.bind(this)),this.getModal().on("mpp:formsubmitted",function(e){this.hide(),this.updateConfiguredStatus(),e.stopPropagation()}.bind(this)),this.getModal().on(CustomEvents.events.activate,SELECTORS_CANCEL_BUTTON,function(e,data){this.hide(),data.originalEvent.preventDefault(),e.stopPropagation()}.bind(this))},registered||(ModalRegistry.register(NotificationProcessorSettings.TYPE,NotificationProcessorSettings,"core/modal_save_cancel"),registered=!0),NotificationProcessorSettings}));
define("core_message/notification_processor_settings",["exports","jquery","core/ajax","core/str","core/notification","core/custom_interaction_events","core/modal","core/fragment"],(function(_exports,_jquery,Ajax,Str,Notification,CustomEvents,_modal,Fragment){function _getRequireWildcardCache(nodeInterop){if("function"!=typeof WeakMap)return null;var cacheBabelInterop=new WeakMap,cacheNodeInterop=new WeakMap;return(_getRequireWildcardCache=function(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop})(nodeInterop)}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule)return obj;if(null===obj||"object"!=typeof obj&&"function"!=typeof obj)return{default:obj};var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj))return cache.get(obj);var newObj={},hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj)if("default"!==key&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;desc&&(desc.get||desc.set)?Object.defineProperty(newObj,key,desc):newObj[key]=obj[key]}return newObj.default=obj,cache&&cache.set(obj,newObj),newObj}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _defineProperty(obj,key,value){return key in obj?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value,obj}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,_jquery=_interopRequireDefault(_jquery),Ajax=_interopRequireWildcard(Ajax),Str=_interopRequireWildcard(Str),Notification=_interopRequireWildcard(Notification),CustomEvents=_interopRequireWildcard(CustomEvents),_modal=_interopRequireDefault(_modal),Fragment=_interopRequireWildcard(Fragment);const SELECTORS_SAVE_BUTTON='[data-action="save"]',SELECTORS_CANCEL_BUTTON='[data-action="cancel"]',SELECTORS_PROCESSOR="[data-processor-name]",SELECTORS_PREFERENCE_ROW='[data-region="preference-row"]';class NotificationProcessorSettings extends _modal.default{constructor(root){super(root),this.name=null,this.userId=null,this.contextId=null,this.element=null,this.saveButton=this.getFooter().find(SELECTORS_SAVE_BUTTON),this.cancelButton=this.getFooter().find(SELECTORS_CANCEL_BUTTON)}setUserId(id){this.userId=id}getUserId(){return this.userId}setElement(element){this.element=element}getElement(){return this.element}setName(name){this.name=name}getName(){return this.name}setContextId(id){this.contextId=id}getContextId(){return this.contextId}getForm(){return this.getBody().find("form")}disableButtons(){this.saveButton.prop("disabled",!0),this.cancelButton.prop("disabled",!0)}enableButtons(){this.saveButton.prop("disabled",!1),this.cancelButton.prop("disabled",!1)}loadTitleContent(){return this.titlePromise=Str.get_string("processorsettings","message"),this.setTitle(this.titlePromise),this.titlePromise}loadBodyContent(){this.disableButtons();const args={userid:this.getUserId(),type:this.getName()};return this.bodyPromise=Fragment.loadFragment("message","processor_settings",this.getContextId(),args),this.setBody(this.bodyPromise),this.bodyPromise.then((()=>{this.enableButtons()})).catch(Notification.exception),this.bodyPromise}loadAllContent(){return _jquery.default.when(this.loadTitleContent(),this.loadBodyContent())}show(){this.loadAllContent(),super.show(this)}hide(){super.hide(this),this.setContextId(null),this.setName(null),this.setUserId(null)}updateConfiguredStatus(){const processorHeader=(0,_jquery.default)(this.getElement()).closest(SELECTORS_PROCESSOR);if(!processorHeader.hasClass("unconfigured"))return!1;const processorName=processorHeader.attr("data-processor-name"),request={methodname:"core_message_get_message_processor",args:{name:processorName,userid:this.userId}};return Ajax.call([request])[0].then((result=>{if(result.userconfigured){const notifications=(0,_jquery.default)(SELECTORS_PREFERENCE_ROW+' [data-processor-name="'+processorName+'"]');processorHeader.removeClass("unconfigured"),notifications.removeClass("disabled")}return result}))}registerEventListeners(){super.registerEventListeners(this),this.getModal().on(CustomEvents.events.activate,SELECTORS_SAVE_BUTTON,((e,data)=>{this.getForm().submit(),data.originalEvent.preventDefault()})),this.getModal().on("mpp:formsubmitted",(e=>{this.hide(),this.updateConfiguredStatus(),e.stopPropagation()})),this.getModal().on(CustomEvents.events.activate,SELECTORS_CANCEL_BUTTON,((e,data)=>{this.hide(),data.originalEvent.preventDefault(),e.stopPropagation()}))}}return _exports.default=NotificationProcessorSettings,_defineProperty(NotificationProcessorSettings,"TYPE","core_message-notification_processor_settings"),_defineProperty(NotificationProcessorSettings,"TEMPLATE","core/modal_save_cancel"),NotificationProcessorSettings.registerModalType(),_exports.default}));
//# sourceMappingURL=notification_processor_settings.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -20,34 +20,26 @@
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define([
'jquery',
'core/ajax',
'core/str',
'core/notification',
'core/custom_interaction_events',
'core/modal',
'core/modal_registry',
'core/fragment',
],
function(
$,
Ajax,
Str,
Notification,
CustomEvents,
Modal,
ModalRegistry,
Fragment
) {
var registered = false;
var SELECTORS = {
SAVE_BUTTON: '[data-action="save"]',
CANCEL_BUTTON: '[data-action="cancel"]',
PROCESSOR: '[data-processor-name]',
PREFERENCE_ROW: '[data-region="preference-row"]',
};
import $ from 'jquery';
import * as Ajax from 'core/ajax';
import * as Str from 'core/str';
import * as Notification from 'core/notification';
import * as CustomEvents from 'core/custom_interaction_events';
import Modal from 'core/modal';
import * as Fragment from 'core/fragment';
const SELECTORS = {
SAVE_BUTTON: '[data-action="save"]',
CANCEL_BUTTON: '[data-action="cancel"]',
PROCESSOR: '[data-processor-name]',
PREFERENCE_ROW: '[data-region="preference-row"]',
};
export default class NotificationProcessorSettings extends Modal {
static TYPE = 'core_message-notification_processor_settings';
static TEMPLATE = 'core/modal_save_cancel';
/**
* Constructor for the Modal.
@ -55,19 +47,15 @@ define([
* @class
* @param {object} root The root jQuery element for the modal.
*/
var NotificationProcessorSettings = function(root) {
Modal.call(this, root);
constructor(root) {
super(root);
this.name = null;
this.userId = null;
this.contextId = null;
this.element = null;
this.saveButton = this.getFooter().find(SELECTORS.SAVE_BUTTON);
this.cancelButton = this.getFooter().find(SELECTORS.CANCEL_BUTTON);
};
NotificationProcessorSettings.TYPE = 'core_message-notification_processor_settings';
NotificationProcessorSettings.prototype = Object.create(Modal.prototype);
NotificationProcessorSettings.prototype.constructor = NotificationProcessorSettings;
}
/**
* Set the userid to the given value.
@ -75,9 +63,9 @@ define([
* @method setUserId
* @param {int} id The notification userid
*/
NotificationProcessorSettings.prototype.setUserId = function(id) {
setUserId(id) {
this.userId = id;
};
}
/**
* Retrieve the current userid, if any.
@ -85,9 +73,9 @@ define([
* @method getUserId
* @return {int|null} The notification userid
*/
NotificationProcessorSettings.prototype.getUserId = function() {
getUserId() {
return this.userId;
};
}
/**
* Set the object to the given value.
@ -95,9 +83,9 @@ define([
* @method setElement
* @param {object} element The notification node element.
*/
NotificationProcessorSettings.prototype.setElement = function(element) {
setElement(element) {
this.element = element;
};
}
/**
* Retrieve the current element, if any.
@ -105,9 +93,9 @@ define([
* @method getElement
* @return {object|null} The notification node element.
*/
NotificationProcessorSettings.prototype.getElement = function() {
getElement() {
return this.element;
};
}
/**
* Set the name to the given value.
@ -115,9 +103,9 @@ define([
* @method setName
* @param {string} name The notification name.
*/
NotificationProcessorSettings.prototype.setName = function(name) {
setName(name) {
this.name = name;
};
}
/**
* Retrieve the current name, if any.
@ -125,18 +113,18 @@ define([
* @method getName
* @return {string|null} The notification name.
*/
NotificationProcessorSettings.prototype.getName = function() {
getName() {
return this.name;
};
}
/**
* Set the context id to the given value.
*
* @method setContextId
* @param {Number} id The notification context id
*/
NotificationProcessorSettings.prototype.setContextId = function(id) {
setContextId(id) {
this.contextId = id;
};
}
/**
* Retrieve the current context id, if any.
@ -144,9 +132,9 @@ define([
* @method getContextId
* @return {Number|null} The notification context id
*/
NotificationProcessorSettings.prototype.getContextId = function() {
getContextId() {
return this.contextId;
};
}
/**
* Get the form element from the modal.
@ -154,29 +142,29 @@ define([
* @method getForm
* @return {object}
*/
NotificationProcessorSettings.prototype.getForm = function() {
getForm() {
return this.getBody().find('form');
};
}
/**
* Disable the buttons in the footer.
*
* @method disableButtons
*/
NotificationProcessorSettings.prototype.disableButtons = function() {
disableButtons() {
this.saveButton.prop('disabled', true);
this.cancelButton.prop('disabled', true);
};
}
/**
* Enable the buttons in the footer.
*
* @method enableButtons
*/
NotificationProcessorSettings.prototype.enableButtons = function() {
enableButtons() {
this.saveButton.prop('disabled', false);
this.cancelButton.prop('disabled', false);
};
}
/**
* Load the title for the modal to the appropriate value
@ -185,12 +173,12 @@ define([
* @method loadTitleContent
* @return {object} A promise resolved with the new title text.
*/
NotificationProcessorSettings.prototype.loadTitleContent = function() {
loadTitleContent() {
this.titlePromise = Str.get_string('processorsettings', 'message');
this.setTitle(this.titlePromise);
return this.titlePromise;
};
}
/**
* Load the body for the modal to the appropriate value
@ -199,10 +187,10 @@ define([
* @method loadBodyContent
* @return {object} A promise resolved with the fragment html and js from
*/
NotificationProcessorSettings.prototype.loadBodyContent = function() {
loadBodyContent() {
this.disableButtons();
var args = {
const args = {
userid: this.getUserId(),
type: this.getName(),
};
@ -210,14 +198,14 @@ define([
this.bodyPromise = Fragment.loadFragment('message', 'processor_settings', this.getContextId(), args);
this.setBody(this.bodyPromise);
this.bodyPromise.then(function() {
this.bodyPromise.then(() => {
this.enableButtons();
return;
}.bind(this))
.fail(Notification.exception);
})
.catch(Notification.exception);
return this.bodyPromise;
};
}
/**
* Load both the title and body content.
@ -225,9 +213,9 @@ define([
* @method loadAllContent
* @return {object} promise
*/
NotificationProcessorSettings.prototype.loadAllContent = function() {
loadAllContent() {
return $.when(this.loadTitleContent(), this.loadBodyContent());
};
}
/**
* Load the modal content before showing it. This
@ -236,10 +224,10 @@ define([
*
* @method show
*/
NotificationProcessorSettings.prototype.show = function() {
show() {
this.loadAllContent();
Modal.prototype.show.call(this);
};
super.show(this);
}
/**
* Clear the notification from the modal when it's closed so
@ -247,12 +235,12 @@ define([
*
* @method hide
*/
NotificationProcessorSettings.prototype.hide = function() {
Modal.prototype.hide.call(this);
hide() {
super.hide(this);
this.setContextId(null);
this.setName(null);
this.setUserId(null);
};
}
/**
* Checks if the processor has been configured. If so then remove the unconfigured
@ -261,15 +249,15 @@ define([
* @method updateConfiguredStatus
* @return {Promise|boolean}
*/
NotificationProcessorSettings.prototype.updateConfiguredStatus = function() {
var processorHeader = $(this.getElement()).closest(SELECTORS.PROCESSOR);
updateConfiguredStatus() {
const processorHeader = $(this.getElement()).closest(SELECTORS.PROCESSOR);
if (!processorHeader.hasClass('unconfigured')) {
return false;
}
var processorName = processorHeader.attr('data-processor-name');
var request = {
const processorName = processorHeader.attr('data-processor-name');
const request = {
methodname: 'core_message_get_message_processor',
args: {
name: processorName,
@ -278,56 +266,45 @@ define([
};
return Ajax.call([request])[0]
.fail(Notification.exception)
.done(function(result) {
.then((result) => {
// Check if the user has figured configuring the processor.
if (result.userconfigured) {
// If they have then we can enable the settings.
var notifications = $(SELECTORS.PREFERENCE_ROW + ' [data-processor-name="' + processorName + '"]');
const notifications = $(SELECTORS.PREFERENCE_ROW + ' [data-processor-name="' + processorName + '"]');
processorHeader.removeClass('unconfigured');
notifications.removeClass('disabled');
}
return result;
});
};
}
/**
* Set up all of the event handling for the modal.
*
* @method registerEventListeners
*/
NotificationProcessorSettings.prototype.registerEventListeners = function() {
registerEventListeners() {
// Apply parent event listeners.
Modal.prototype.registerEventListeners.call(this);
super.registerEventListeners(this);
// When the user clicks the save button we trigger the form submission.
this.getModal().on(CustomEvents.events.activate, SELECTORS.SAVE_BUTTON, function(e, data) {
this.getModal().on(CustomEvents.events.activate, SELECTORS.SAVE_BUTTON, (e, data) => {
this.getForm().submit();
data.originalEvent.preventDefault();
}.bind(this));
});
this.getModal().on('mpp:formsubmitted', function(e) {
this.getModal().on('mpp:formsubmitted', (e) => {
this.hide();
this.updateConfiguredStatus();
e.stopPropagation();
}.bind(this));
});
this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, function(e, data) {
this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, (e, data) => {
this.hide();
data.originalEvent.preventDefault();
e.stopPropagation();
}.bind(this));
};
// Automatically register with the modal registry the first time this module is imported
// so that you can create modals
// of this type using the modal factory.
if (!registered) {
ModalRegistry.register(
NotificationProcessorSettings.TYPE,
NotificationProcessorSettings,
'core/modal_save_cancel');
registered = true;
});
}
}
return NotificationProcessorSettings;
});
NotificationProcessorSettings.registerModalType();