MDL-68442 user: Rewrite enrolment actions as ES6
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 17 Apr 2020 05:40:18 +0000 (13:40 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Tue, 28 Apr 2020 02:21:28 +0000 (10:21 +0800)
user/amd/build/status_field.min.js
user/amd/build/status_field.min.js.map
user/amd/src/status_field.js

index 7a8a14c..727cb9a 100644 (file)
Binary files a/user/amd/build/status_field.min.js and b/user/amd/build/status_field.min.js differ
index 5dd1fb9..5bdddb0 100644 (file)
Binary files a/user/amd/build/status_field.min.js.map and b/user/amd/build/status_field.min.js.map differ
index c03d9de..5fdca82 100644 (file)
  * @copyright  2017 Jun Pataleta
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-define(['core/templates',
-        'jquery',
-        'core/str',
-        'core/config',
-        'core/notification',
-        'core/modal_factory',
-        'core/modal_events',
-        'core/fragment',
-        'core/ajax'
-    ],
-    function(Template, $, Str, Config, Notification, ModalFactory, ModalEvents, Fragment, Ajax) {
-
-        /**
-         * Action selectors.
-         *
-         * @access private
-         * @type {{EDIT_ENROLMENT: string, SHOW_DETAILS: string, UNENROL: string}}
-         */
-        var SELECTORS = {
-            EDIT_ENROLMENT: '[data-action="editenrolment"]',
-            SHOW_DETAILS: '[data-action="showdetails"]',
-            UNENROL: '[data-action="unenrol"]'
-        };
-
-        /**
-         * Constructor
-         *
-         * @param {Object} options Object containing options. The only valid option at this time is contextid.
-         * Each call to templates.render gets it's own instance of this class.
-         */
-        var StatusFieldActions = function(options) {
-            this.contextid = options.contextid;
-            this.courseid = options.courseid;
 
-            // Bind click event to editenrol buttons.
-            this.bindEditEnrol();
-
-            // Bind click event to unenrol buttons.
-            this.bindUnenrol();
+import Ajax from 'core/ajax';
+import Fragment from 'core/fragment';
+import ModalEvents from 'core/modal_events';
+import ModalFactory from 'core/modal_factory';
+import Notification from 'core/notification';
+import * as Str from 'core/str';
+import Templates from 'core/templates';
+import jQuery from 'jquery';
+
+const Selectors = {
+    editEnrolment: '[data-action="editenrolment"]',
+    showDetails: '[data-action="showdetails"]',
+    unenrol: '[data-action="unenrol"]'
+};
+
+class StatusFieldActions {
+    /**
+     * Constructor
+     *
+     * @param {Object} options Object containing options. The only valid option at this time is contextid.
+     * Each call to templates.render gets it's own instance of this class.
+     */
+    constructor(options) {
+        this.contextid = options.contextid;
+        this.courseid = options.courseid;
+
+        // Bind click event to editenrol buttons.
+        this.bindEditEnrol();
+
+        // Bind click event to unenrol buttons.
+        this.bindUnenrol();
+
+        // Bind click event to status details buttons.
+        this.bindStatusDetails();
+    }
+
+    bindEditEnrol() {
+        var statusFieldInstsance = this;
+
+        jQuery(Selectors.editEnrolment).click(function(e) {
+            e.preventDefault();
+
+            // The particular edit button that was clicked.
+            var clickedEditTrigger = jQuery(this);
+            // Get the parent container (it contains the data attributes associated with the status field).
+            var parentContainer = clickedEditTrigger.parent();
+            // Get the name of the user whose enrolment status is being edited.
+            var fullname = parentContainer.data('fullname');
+            // Get the user enrolment ID.
+            var ueid = clickedEditTrigger.attr('rel');
+
+            jQuery.when(Str.get_string('edituserenrolment', 'enrol', fullname)).then(function(modalTitle) {
+                return ModalFactory.create({
+                    large: true,
+                    title: modalTitle,
+                    type: ModalFactory.types.SAVE_CANCEL
+                });
+            }).done(function(modal) {
+                // Handle save event.
+                modal.getRoot().on(ModalEvents.save, function(e) {
+                    // Don't close the modal yet.
+                    e.preventDefault();
+                    // Submit form data.
+                    statusFieldInstsance.submitEditFormAjax(modal);
+                });
 
-            // Bind click event to status details buttons.
-            this.bindStatusDetails();
-        };
-        // Class variables and functions.
-
-        /** @var {number} courseid The course ID. */
-        StatusFieldActions.prototype.courseid = 0;
-
-        /**
-         * Private method
-         *
-         * @method initModal
-         * @private
-         */
-        StatusFieldActions.prototype.bindEditEnrol = function() {
-            var statusFieldInstsance = this;
-
-            $(SELECTORS.EDIT_ENROLMENT).click(function(e) {
-                e.preventDefault();
-
-                // The particular edit button that was clicked.
-                var clickedEditTrigger = $(this);
-                // Get the parent container (it contains the data attributes associated with the status field).
-                var parentContainer = clickedEditTrigger.parent();
-                // Get the name of the user whose enrolment status is being edited.
-                var fullname = parentContainer.data('fullname');
-                // Get the user enrolment ID.
-                var ueid = clickedEditTrigger.attr('rel');
-
-                $.when(Str.get_string('edituserenrolment', 'enrol', fullname)).then(function(modalTitle) {
-                    return ModalFactory.create({
-                        large: true,
-                        title: modalTitle,
-                        type: ModalFactory.types.SAVE_CANCEL
-                    });
-                }).done(function(modal) {
-                    // Handle save event.
-                    modal.getRoot().on(ModalEvents.save, function(e) {
-                        // Don't close the modal yet.
-                        e.preventDefault();
-                        // Submit form data.
-                        statusFieldInstsance.submitEditFormAjax(modal);
-                    });
+                // Handle hidden event.
+                modal.getRoot().on(ModalEvents.hidden, function() {
+                    // Destroy when hidden.
+                    modal.destroy();
+                });
 
-                    // Handle hidden event.
-                    modal.getRoot().on(ModalEvents.hidden, function() {
-                        // Destroy when hidden.
-                        modal.destroy();
-                    });
+                // Set the modal body.
+                modal.setBody(statusFieldInstsance.getBody(ueid));
 
-                    // Set the modal body.
-                    modal.setBody(statusFieldInstsance.getBody(ueid));
+                // Show the modal!
+                modal.show();
+            }).fail(Notification.exception);
+        });
+    }
+
+    bindUnenrol() {
+        var statusFieldInstsance = this;
+
+        jQuery(Selectors.unenrol).click(function(e) {
+            e.preventDefault();
+            var unenrolLink = jQuery(this);
+            var parentContainer = unenrolLink.parent();
+            var strings = [
+                {
+                    key: 'unenrol',
+                    component: 'enrol'
+                },
+                {
+                    key: 'unenrolconfirm',
+                    component: 'enrol',
+                    param: {
+                        user: parentContainer.data('fullname'),
+                        course: parentContainer.data('coursename'),
+                        enrolinstancename: parentContainer.data('enrolinstancename')
+                    }
+                }
+            ];
 
-                    // Show the modal!
-                    modal.show();
-                }).fail(Notification.exception);
+            var deleteModalPromise = ModalFactory.create({
+                type: ModalFactory.types.SAVE_CANCEL
             });
-        };
-
-        /**
-         * Private method
-         *
-         * @method bindUnenrol
-         * @private
-         */
-        StatusFieldActions.prototype.bindUnenrol = function() {
-            var statusFieldInstsance = this;
-
-            $(SELECTORS.UNENROL).click(function(e) {
-                e.preventDefault();
-                var unenrolLink = $(this);
-                var parentContainer = unenrolLink.parent();
-                var strings = [
-                    {
-                        key: 'unenrol',
-                        component: 'enrol'
-                    },
-                    {
-                        key: 'unenrolconfirm',
-                        component: 'enrol',
-                        param: {
-                            user: parentContainer.data('fullname'),
-                            course: parentContainer.data('coursename'),
-                            enrolinstancename: parentContainer.data('enrolinstancename')
-                        }
-                    }
-                ];
 
-                var deleteModalPromise = ModalFactory.create({
-                    type: ModalFactory.types.SAVE_CANCEL
+            jQuery.when(Str.get_strings(strings), deleteModalPromise).done(function(results, modal) {
+                var title = results[0];
+                var confirmMessage = results[1];
+                modal.setTitle(title);
+                modal.setBody(confirmMessage);
+                modal.setSaveButtonText(title);
+
+                // Handle confirm event.
+                modal.getRoot().on(ModalEvents.save, function() {
+                    // Build params.
+                    var unenrolParams = {
+                        'ueid': jQuery(unenrolLink).attr('rel')
+                    };
+                    // Don't close the modal yet.
+                    e.preventDefault();
+                    // Submit data.
+                    statusFieldInstsance.submitUnenrolFormAjax(modal, unenrolParams);
                 });
 
-                $.when(Str.get_strings(strings), deleteModalPromise).done(function(results, modal) {
-                    var title = results[0];
-                    var confirmMessage = results[1];
-                    modal.setTitle(title);
-                    modal.setBody(confirmMessage);
-                    modal.setSaveButtonText(title);
-
-                    // Handle confirm event.
-                    modal.getRoot().on(ModalEvents.save, function() {
-                        // Build params.
-                        var unenrolParams = {
-                            'ueid': $(unenrolLink).attr('rel')
-                        };
-                        // Don't close the modal yet.
-                        e.preventDefault();
-                        // Submit data.
-                        statusFieldInstsance.submitUnenrolFormAjax(modal, unenrolParams);
-                    });
-
-                    // Handle hidden event.
-                    modal.getRoot().on(ModalEvents.hidden, function() {
-                        // Destroy when hidden.
-                        modal.destroy();
-                    });
-
-                    // Display the delete confirmation modal.
-                    modal.show();
-                }).fail(Notification.exception);
-            });
-        };
+                // Handle hidden event.
+                modal.getRoot().on(ModalEvents.hidden, function() {
+                    // Destroy when hidden.
+                    modal.destroy();
+                });
 
-        /**
-         * Private method
-         *
-         * @method bindStatusDetails
-         * @private
-         */
-        StatusFieldActions.prototype.bindStatusDetails = function() {
-            $(SELECTORS.SHOW_DETAILS).click(function(e) {
-                e.preventDefault();
-
-                var detailsButton = $(this);
-                var parentContainer = detailsButton.parent();
-                var context = {
-                    "fullname": parentContainer.data('fullname'),
-                    "coursename": parentContainer.data('coursename'),
-                    "enrolinstancename": parentContainer.data('enrolinstancename'),
-                    "status": parentContainer.data('status'),
-                    "statusclass": parentContainer.find('span').attr('class'),
-                    "timestart": parentContainer.data('timestart'),
-                    "timeend": parentContainer.data('timeend'),
-                    "timeenrolled": parentContainer.data('timeenrolled')
-                };
-
-                // Get default string for the modal and modal type.
-                var strings = [
-                    {
-                        key: 'enroldetails',
-                        component: 'enrol'
-                    }
-                ];
+                // Display the delete confirmation modal.
+                modal.show();
+            }).fail(Notification.exception);
+        });
+    }
+
+    bindStatusDetails() {
+        jQuery(Selectors.showDetails).click(function(e) {
+            e.preventDefault();
+
+            var detailsButton = jQuery(this);
+            var parentContainer = detailsButton.parent();
+            var context = {
+                "fullname": parentContainer.data('fullname'),
+                "coursename": parentContainer.data('coursename'),
+                "enrolinstancename": parentContainer.data('enrolinstancename'),
+                "status": parentContainer.data('status'),
+                "statusclass": parentContainer.find('span').attr('class'),
+                "timestart": parentContainer.data('timestart'),
+                "timeend": parentContainer.data('timeend'),
+                "timeenrolled": parentContainer.data('timeenrolled')
+            };
 
-                // Find the edit enrolment link.
-                var editEnrolLink = detailsButton.next(SELECTORS.EDIT_ENROLMENT);
-                if (editEnrolLink.length) {
-                    // If there's an edit enrolment link for this user, clone it into the context for the modal.
-                    context.editenrollink = $('<div>').append(editEnrolLink.clone()).html();
+            // Get default string for the modal and modal type.
+            var strings = [
+                {
+                    key: 'enroldetails',
+                    component: 'enrol'
                 }
+            ];
 
-                var modalStringsPromise = Str.get_strings(strings);
-                var modalPromise = ModalFactory.create({large: true, type: ModalFactory.types.CANCEL});
-                $.when(modalStringsPromise, modalPromise).done(function(strings, modal) {
-                    var modalBodyPromise = Template.render('core_user/status_details', context);
-                    modal.setTitle(strings[0]);
-                    modal.setBody(modalBodyPromise);
-
-                    if (editEnrolLink.length) {
-                        modal.getRoot().on('click', SELECTORS.EDIT_ENROLMENT, function(e) {
-                            e.preventDefault();
-                            modal.hide();
-                            // Trigger click event for the edit enrolment link to show the edit enrolment modal.
-                            $(editEnrolLink).trigger('click');
-                        });
-                    }
+            // Find the edit enrolment link.
+            var editEnrolLink = detailsButton.next(Selectors.editEnrolment);
+            if (editEnrolLink.length) {
+                // If there's an edit enrolment link for this user, clone it into the context for the modal.
+                context.editenrollink = jQuery('<div>').append(editEnrolLink.clone()).html();
+            }
 
-                    modal.show();
+            var modalStringsPromise = Str.get_strings(strings);
+            var modalPromise = ModalFactory.create({large: true, type: ModalFactory.types.CANCEL});
+            jQuery.when(modalStringsPromise, modalPromise).done(function(strings, modal) {
+                var modalBodyPromise = Templates.render('core_user/status_details', context);
+                modal.setTitle(strings[0]);
+                modal.setBody(modalBodyPromise);
 
-                    // Handle hidden event.
-                    modal.getRoot().on(ModalEvents.hidden, function() {
-                        // Destroy when hidden.
-                        modal.destroy();
+                if (editEnrolLink.length) {
+                    modal.getRoot().on('click', Selectors.editEnrolment, function(e) {
+                        e.preventDefault();
+                        modal.hide();
+                        // Trigger click event for the edit enrolment link to show the edit enrolment modal.
+                        jQuery(editEnrolLink).trigger('click');
                     });
-                }).fail(Notification.exception);
-            });
-        };
-
-        /**
-         * Private method
-         *
-         * @method submitEditFormAjax
-         * @param {Object} modal The the AMD modal object containing the form.
-         * @private
-         */
-        StatusFieldActions.prototype.submitEditFormAjax = function(modal) {
-            var statusFieldInstsance = this;
-            var form = modal.getRoot().find('form');
-
-            // User enrolment ID.
-            var ueid = $(form).find('[name="ue"]').val();
-
-            var request = {
-                methodname: 'core_enrol_submit_user_enrolment_form',
-                args: {
-                    formdata: form.serialize()
                 }
-            };
 
-            Ajax.call([request])[0].done(function(data) {
-                if (data.result) {
-                    // Dismiss the modal.
-                    modal.hide();
+                modal.show();
 
-                    // Reload the page, don't show changed data warnings.
-                    if (typeof window.M.core_formchangechecker !== "undefined") {
-                        window.M.core_formchangechecker.reset_form_dirty_state();
-                    }
-                    window.location.reload();
-                } else {
-                    // Serialise the form data and reload the form fragment to show validation errors.
-                    var formData = JSON.stringify(form.serialize());
-                    modal.setBody(statusFieldInstsance.getBody(ueid, formData));
-                }
+                // Handle hidden event.
+                modal.getRoot().on(ModalEvents.hidden, function() {
+                    // Destroy when hidden.
+                    modal.destroy();
+                });
             }).fail(Notification.exception);
-        };
+        });
+    }
 
-        /**
-         * Private method
-         *
-         * @method submitUnenrolFormAjax
-         * @param {Object} modal The the AMD modal object containing the form.
-         * @param {Object} unenrolParams The unenrol parameters.
-         * @private
-         */
-        StatusFieldActions.prototype.submitUnenrolFormAjax = function(modal, unenrolParams) {
-            var request = {
-                methodname: 'core_enrol_unenrol_user_enrolment',
-                args: unenrolParams
-            };
+    submitEditFormAjax(modal) {
+        var statusFieldInstsance = this;
+        var form = modal.getRoot().find('form');
 
-            Ajax.call([request])[0].done(function(data) {
-                if (data.result) {
-                    // Dismiss the modal.
-                    modal.hide();
+        // User enrolment ID.
+        var ueid = jQuery(form).find('[name="ue"]').val();
 
-                    // Reload the page, don't show changed data warnings.
-                    if (typeof window.M.core_formchangechecker !== "undefined") {
-                        window.M.core_formchangechecker.reset_form_dirty_state();
-                    }
-                    window.location.reload();
-                } else {
-                    // Display an alert containing the error message
-                    Notification.alert(data.errors[0].key, data.errors[0].message);
-                }
-            }).fail(Notification.exception);
+        var request = {
+            methodname: 'core_enrol_submit_user_enrolment_form',
+            args: {
+                formdata: form.serialize()
+            }
         };
 
-        /**
-         * Private method
-         *
-         * @method getBody
-         * @private
-         * @param {Number} ueid The user enrolment ID associated with the user.
-         * @param {string} formData Serialized string of the edit enrolment form data.
-         * @return {Promise}
-         */
-        StatusFieldActions.prototype.getBody = function(ueid, formData) {
-            var params = {
-                'ueid': ueid
-            };
-            if (typeof formData !== 'undefined') {
-                params.formdata = formData;
+        Ajax.call([request])[0].done(function(data) {
+            if (data.result) {
+                // Dismiss the modal.
+                modal.hide();
+
+                // Reload the page, don't show changed data warnings.
+                if (typeof window.M.core_formchangechecker !== "undefined") {
+                    window.M.core_formchangechecker.reset_form_dirty_state();
+                }
+                window.location.reload();
+            } else {
+                // Serialise the form data and reload the form fragment to show validation errors.
+                var formData = JSON.stringify(form.serialize());
+                modal.setBody(statusFieldInstsance.getBody(ueid, formData));
             }
-            return Fragment.loadFragment('enrol', 'user_enrolment_form', this.contextid, params).fail(Notification.exception);
+        }).fail(Notification.exception);
+    }
+
+    submitUnenrolFormAjax(modal, unenrolParams) {
+        var request = {
+            methodname: 'core_enrol_unenrol_user_enrolment',
+            args: unenrolParams
         };
 
-        return /** @alias module:core_user/editenrolment */ {
-            // Public variables and functions.
-            /**
-             * Every call to init creates a new instance of the class with it's own event listeners etc.
-             *
-             * @method init
-             * @public
-             * @param {object} config - config variables for the module.
-             */
-            init: function(config) {
-                (new StatusFieldActions(config));
+        Ajax.call([request])[0].done(function(data) {
+            if (data.result) {
+                // Dismiss the modal.
+                modal.hide();
+
+                // Reload the page, don't show changed data warnings.
+                if (typeof window.M.core_formchangechecker !== "undefined") {
+                    window.M.core_formchangechecker.reset_form_dirty_state();
+                }
+                window.location.reload();
+            } else {
+                // Display an alert containing the error message
+                Notification.alert(data.errors[0].key, data.errors[0].message);
             }
+        }).fail(Notification.exception);
+    }
+
+    getBody(ueid, formData) {
+        var params = {
+            'ueid': ueid
         };
-    });
+        if (typeof formData !== 'undefined') {
+            params.formdata = formData;
+        }
+        return Fragment.loadFragment('enrol', 'user_enrolment_form', this.contextid, params).fail(Notification.exception);
+    }
+}
+
+export const init = config => {
+    new StatusFieldActions(config);
+};