Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"preferences_modal.min.js","sources":["../src/preferences_modal.js"],"sourcesContent":["/**\n * Add a create new group modal to the page.\n *\n * @module     core_group/newgroup\n * @class      PreferencesModal\n * @copyright  2017 Damyon Wiese <damyon@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/str', 'core/modal_factory', 'core/modal_events',\n        'core/fragment', 'core/ajax', 'block_dash/select2', 'core/notification'],\n    function($, Str, ModalFactory, ModalEvents, Fragment, Ajax, Select2, Notification) {\n\n    /**\n     * Constructor\n     *\n     * @param {String} selector used to find triggers for the new group modal.\n     * @param {int} contextid\n     * @param {Function} onCloseCallback\n     *\n     * Each call to init gets it's own instance of this class.\n     */\n    var PreferencesModal = function(selector, contextid, onCloseCallback) {\n        this.contextid = contextid;\n        this.onCloseCallback = onCloseCallback;\n        this.tab = \"\";\n        this.init(selector);\n    };\n\n    /**\n     * @var {Modal} modal\n     * @private\n     */\n    PreferencesModal.prototype.modal = null;\n\n    /**\n     * @var {int} contextid\n     * @private\n     */\n    PreferencesModal.prototype.contextid = -1;\n\n    /**\n     * Initialise the class.\n     *\n     * @param {String} selector used to find triggers for the new group modal.\n     * @private\n     * @return {Promise}\n     */\n    PreferencesModal.prototype.init = function(selector) {\n        var triggers = $(selector);\n        // Fetch the title string.\n        return Str.get_string('editpreferences', 'block_dash').then(function(title) {\n            // Create the modal.\n            return ModalFactory.create({\n                type: ModalFactory.types.DEFAULT,\n                title: title,\n                body: this.getBody()\n            }, triggers);\n        }.bind(this)).then(function(modal) {\n            // Keep a reference to the modal.\n            this.modal = modal;\n\n            // Forms are big, we want a big modal.\n            this.modal.setLarge();\n\n            // We want to reset the form every time it is opened.\n            this.modal.getRoot().on(ModalEvents.shown, function() {\n                this.modal.setBody(this.getBody());\n            }.bind(this));\n\n            this.modal.getRoot().on('change', '#id_config_preferences_layout', this.submitFormAjax.bind(this, false));\n\n            this.modal.getRoot().on('click', '[data-action=cancel]', () => {\n                this.modal.hide();\n            });\n\n            // We catch the modal save event, and use it to submit the form inside the modal.\n            // Triggering a form submission will give JS validation scripts a chance to check for errors.\n            this.modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));\n            // We also catch the form submit event and use it to submit the form with ajax.\n            this.modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this, true));\n\n            this.modal.getRoot().on(ModalEvents.bodyRendered, function() {\n                $(\"#fgroup_id_available_fields .form-inline > fieldset > div\").sortable({\n                    items: \".form-check-inline.fitem\",\n                    handle: \".drag-handle\",\n                    axis: \"y\"\n                });\n\n                this.initSelect2();\n            }.bind(this));\n\n            this.modal.getRoot().on(ModalEvents.hidden, function(e) {\n                // Prevent \"changes may be lost\" popup.\n                window.onbeforeunload = null;\n                if (this.onCloseCallback) {\n                    this.onCloseCallback(e);\n                }\n            }.bind(this));\n\n            this.modal.getRoot().on('click', '[data-action=change-tab]', (e) => {\n                this.changeTab($(e.target).data('tab'));\n            });\n\n            return this.modal;\n        }.bind(this));\n    };\n\n    PreferencesModal.prototype.changeTab = function(tab) {\n        this.tab = tab;\n        this.submitFormAjax(false);\n    };\n\n    /**\n     * Get modal body content.\n     * @method getBody\n     * @param {Object} formdata\n     * @private\n     * @return {Promise}\n     */\n    PreferencesModal.prototype.getBody = function(formdata) {\n        if (typeof formdata === \"undefined\") {\n            formdata = {};\n        }\n\n        // Get the content of the modal.\n        var params = {\n            jsonformdata: JSON.stringify(formdata),\n            tab: this.tab\n        };\n        return Fragment.loadFragment('block_dash', 'block_preferences_form', this.contextid, params);\n    };\n\n    /**\n     * Form submission handler.\n     * @method handleFormSubmissionResponse\n     * @param {Object} formData\n     * @param {Boolean} closeWhenDone\n     * @param {Object} response\n     * @private\n     */\n    PreferencesModal.prototype.handleFormSubmissionResponse = function(formData, closeWhenDone, response) {\n        if (response.validationerrors || !closeWhenDone) {\n            this.modal.setBody(this.getBody(formData));\n        } else if (closeWhenDone) {\n            this.modal.hide();\n        }\n    };\n\n    /**\n     * @method handleFormSubmissionFailure\n     * @param {Object} data\n     * @private\n     */\n    PreferencesModal.prototype.handleFormSubmissionFailure = function(data) {\n        // Oh noes! Epic fail :(\n        // Ah wait - this is normal. We need to re-display the form with errors!\n        this.modal.setBody(this.getBody(data));\n    };\n\n    /**\n     * Private method\n     *\n     * @method submitFormAjax\n     * @private\n     * @param {boolean} closeWhenDone If true modal will close after successful submission.\n     * @param {Event} e Form submission event.\n     */\n    PreferencesModal.prototype.submitFormAjax = function(closeWhenDone, e) {\n        // We don't want to do a real form submission.\n        if (e) {\n            e.preventDefault();\n        }\n\n        // Now the change events have run, see if there are any \"invalid\" form fields.\n        var invalid = $.merge(\n            this.modal.getRoot().find('[aria-invalid=\"true\"]'),\n            this.modal.getRoot().find('.error')\n        );\n\n        // If we found invalid fields, focus on the first one and do not submit via ajax.\n        if (invalid.length) {\n            invalid.first().focus();\n            return;\n        }\n\n        // Convert all the form elements values to a serialised string.\n        var formData = this.modal.getRoot().find('form').serialize();\n\n        // Now we can continue...\n        Ajax.call([{\n            methodname: 'block_dash_submit_preferences_form',\n            args: {\n                contextid: this.contextid,\n                jsonformdata: JSON.stringify(formData)\n            },\n            done: this.handleFormSubmissionResponse.bind(this, formData, closeWhenDone),\n            fail: this.handleFormSubmissionFailure.bind(this, formData)\n        }])[0].fail(Notification.exception);\n    };\n\n    PreferencesModal.prototype.getModal = function() {\n        return this.modal;\n    };\n\n    /**\n     * This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.\n     *\n     * @method submitForm\n     * @param {Event} e Form submission event.\n     * @private\n     */\n    PreferencesModal.prototype.submitForm = function(e) {\n        e.preventDefault();\n        this.modal.getRoot().find('form').submit();\n    };\n\n    PreferencesModal.prototype.initSelect2 = function() {\n        this.modal.getRoot().find('.select2-form select').each(function(index, element) {\n            let placeholder = null;\n            if ($(element).find(\"option[value='-1']\")) {\n                placeholder = {\n                    id: '-1', // The value of the option\n                    text: $(element).find(\"option[value='-1']\").text()\n                };\n            }\n            $(element).select2({\n                dropdownParent: $(this).parent(),\n                allowClear: true,\n                theme: 'bootstrap4',\n                placeholder: placeholder\n            }).on('select2:unselecting', function() {\n                $(this).data('unselecting', true);\n            }).on('select2:opening', function(e) {\n                if ($(this).data('unselecting')) {\n                    $(this).removeData('unselecting');\n                    e.preventDefault();\n                }\n            });\n        });\n    };\n\n    return PreferencesModal;\n});\n"],"names":["define","$","Str","ModalFactory","ModalEvents","Fragment","Ajax","Select2","Notification","PreferencesModal","selector","contextid","onCloseCallback","tab","init","prototype","modal","triggers","get_string","then","title","create","type","types","DEFAULT","body","this","getBody","bind","setLarge","getRoot","on","shown","setBody","submitFormAjax","hide","save","submitForm","bodyRendered","sortable","items","handle","axis","initSelect2","hidden","e","window","onbeforeunload","changeTab","target","data","formdata","params","jsonformdata","JSON","stringify","loadFragment","handleFormSubmissionResponse","formData","closeWhenDone","response","validationerrors","handleFormSubmissionFailure","preventDefault","invalid","merge","find","length","first","focus","serialize","call","methodname","args","done","fail","exception","getModal","submit","each","index","element","placeholder","id","text","select2","dropdownParent","parent","allowClear","theme","removeData"],"mappings":";;;;;;;;AAQAA,sCAAO,CAAC,SAAU,WAAY,qBAAsB,oBAC5C,gBAAiB,YAAa,qBAAsB,sBACxD,SAASC,EAAGC,IAAKC,aAAcC,YAAaC,SAAUC,KAAMC,QAASC,kBAWjEC,iBAAmB,SAASC,SAAUC,UAAWC,sBAC5CD,UAAYA,eACZC,gBAAkBA,qBAClBC,IAAM,QACNC,KAAKJ,kBAOdD,iBAAiBM,UAAUC,MAAQ,KAMnCP,iBAAiBM,UAAUJ,WAAa,EASxCF,iBAAiBM,UAAUD,KAAO,SAASJ,cACnCO,SAAWhB,EAAES,iBAEVR,IAAIgB,WAAW,kBAAmB,cAAcC,KAAK,SAASC,cAE1DjB,aAAakB,OAAO,CACvBC,KAAMnB,aAAaoB,MAAMC,QACzBJ,MAAOA,MACPK,KAAMC,KAAKC,WACZV,WAC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