{"version":3,"file":"modal_event_form.min.js","sources":["https:\/\/iie-academy.org\/calendar\/amd\/src\/modal_event_form.js"],"sourcesContent":["\/\/ This file is part of Moodle - http:\/\/moodle.org\/\n\/\/\n\/\/ Moodle is free software: you can redistribute it and\/or modify\n\/\/ it under the terms of the GNU General Public License as published by\n\/\/ the Free Software Foundation, either version 3 of the License, or\n\/\/ (at your option) any later version.\n\/\/\n\/\/ Moodle is distributed in the hope that it will be useful,\n\/\/ but WITHOUT ANY WARRANTY; without even the implied warranty of\n\/\/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\/\/ GNU General Public License for more details.\n\/\/\n\/\/ You should have received a copy of the GNU General Public License\n\/\/ along with Moodle. If not, see .\n\n\/**\n * Contain the logic for the quick add or update event modal.\n *\n * @module core_calendar\/modal_quick_add_event\n * @copyright 2017 Ryan Wyllie \n * @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n *\/\ndefine([\n 'jquery',\n 'core_form\/events',\n 'core\/str',\n 'core\/notification',\n 'core\/templates',\n 'core\/custom_interaction_events',\n 'core\/modal',\n 'core\/modal_registry',\n 'core\/fragment',\n 'core_calendar\/events',\n 'core_calendar\/repository'\n],\nfunction(\n $,\n FormEvents,\n Str,\n Notification,\n Templates,\n CustomEvents,\n Modal,\n ModalRegistry,\n Fragment,\n CalendarEvents,\n Repository\n) {\n var registered = false;\n var SELECTORS = {\n SAVE_BUTTON: '[data-action=\"save\"]',\n LOADING_ICON_CONTAINER: '[data-region=\"loading-icon-container\"]',\n };\n\n \/**\n * Constructor for the Modal.\n *\n * @param {object} root The root jQuery element for the modal\n *\/\n var ModalEventForm = function(root) {\n Modal.call(this, root);\n this.eventId = null;\n this.startTime = null;\n this.courseId = null;\n this.categoryId = null;\n this.contextId = null;\n this.reloadingBody = false;\n this.reloadingTitle = false;\n this.saveButton = this.getFooter().find(SELECTORS.SAVE_BUTTON);\n };\n\n ModalEventForm.TYPE = 'core_calendar-modal_event_form';\n ModalEventForm.prototype = Object.create(Modal.prototype);\n ModalEventForm.prototype.constructor = ModalEventForm;\n\n \/**\n * Set the context id to the given value.\n *\n * @method setContextId\n * @param {Number} id The event id\n *\/\n ModalEventForm.prototype.setContextId = function(id) {\n this.contextId = id;\n };\n\n \/**\n * Retrieve the current context id, if any.\n *\n * @method getContextId\n * @return {Number|null} The event id\n *\/\n ModalEventForm.prototype.getContextId = function() {\n return this.contextId;\n };\n\n \/**\n * Set the course id to the given value.\n *\n * @method setCourseId\n * @param {int} id The event id\n *\/\n ModalEventForm.prototype.setCourseId = function(id) {\n this.courseId = id;\n };\n\n \/**\n * Retrieve the current course id, if any.\n *\n * @method getCourseId\n * @return {int|null} The event id\n *\/\n ModalEventForm.prototype.getCourseId = function() {\n return this.courseId;\n };\n\n \/**\n * Set the category id to the given value.\n *\n * @method setCategoryId\n * @param {int} id The event id\n *\/\n ModalEventForm.prototype.setCategoryId = function(id) {\n this.categoryId = id;\n };\n\n \/**\n * Retrieve the current category id, if any.\n *\n * @method getCategoryId\n * @return {int|null} The event id\n *\/\n ModalEventForm.prototype.getCategoryId = function() {\n return this.categoryId;\n };\n\n \/**\n * Check if the modal has an course id.\n *\n * @method hasCourseId\n * @return {bool}\n *\/\n ModalEventForm.prototype.hasCourseId = function() {\n return this.courseId !== null;\n };\n\n \/**\n * Check if the modal has an category id.\n *\n * @method hasCategoryId\n * @return {bool}\n *\/\n ModalEventForm.prototype.hasCategoryId = function() {\n return this.categoryId !== null;\n };\n\n \/**\n * Set the event id to the given value.\n *\n * @method setEventId\n * @param {int} id The event id\n *\/\n ModalEventForm.prototype.setEventId = function(id) {\n this.eventId = id;\n };\n\n \/**\n * Retrieve the current event id, if any.\n *\n * @method getEventId\n * @return {int|null} The event id\n *\/\n ModalEventForm.prototype.getEventId = function() {\n return this.eventId;\n };\n\n \/**\n * Check if the modal has an event id.\n *\n * @method hasEventId\n * @return {bool}\n *\/\n ModalEventForm.prototype.hasEventId = function() {\n return this.eventId !== null;\n };\n\n \/**\n * Set the start time to the given value.\n *\n * @method setStartTime\n * @param {int} time The start time\n *\/\n ModalEventForm.prototype.setStartTime = function(time) {\n this.startTime = time;\n };\n\n \/**\n * Retrieve the current start time, if any.\n *\n * @method getStartTime\n * @return {int|null} The start time\n *\/\n ModalEventForm.prototype.getStartTime = function() {\n return this.startTime;\n };\n\n \/**\n * Check if the modal has start time.\n *\n * @method hasStartTime\n * @return {bool}\n *\/\n ModalEventForm.prototype.hasStartTime = function() {\n return this.startTime !== null;\n };\n\n \/**\n * Get the form element from the modal.\n *\n * @method getForm\n * @return {object}\n *\/\n ModalEventForm.prototype.getForm = function() {\n return this.getBody().find('form');\n };\n\n \/**\n * Disable the buttons in the footer.\n *\n * @method disableButtons\n *\/\n ModalEventForm.prototype.disableButtons = function() {\n this.saveButton.prop('disabled', true);\n };\n\n \/**\n * Enable the buttons in the footer.\n *\n * @method enableButtons\n *\/\n ModalEventForm.prototype.enableButtons = function() {\n this.saveButton.prop('disabled', false);\n };\n\n \/**\n * Reload the title for the modal to the appropriate value\n * depending on whether we are creating a new event or\n * editing an existing event.\n *\n * @method reloadTitleContent\n * @return {object} A promise resolved with the new title text\n *\/\n ModalEventForm.prototype.reloadTitleContent = function() {\n if (this.reloadingTitle) {\n return this.titlePromise;\n }\n\n this.reloadingTitle = true;\n\n if (this.hasEventId()) {\n this.titlePromise = Str.get_string('editevent', 'calendar');\n } else {\n this.titlePromise = Str.get_string('newevent', 'calendar');\n }\n\n this.titlePromise.then(function(string) {\n this.setTitle(string);\n return string;\n }.bind(this))\n .always(function() {\n this.reloadingTitle = false;\n return;\n }.bind(this))\n .fail(Notification.exception);\n\n return this.titlePromise;\n };\n\n \/**\n * Send a request to the server to get the event_form in a fragment\n * and render the result in the body of the modal.\n *\n * If serialised form data is provided then it will be sent in the\n * request to the server to have the form rendered with the data. This\n * is used when the form had a server side error and we need the server\n * to re-render it for us to display the error to the user.\n *\n * @method reloadBodyContent\n * @param {string} formData The serialised form data\n * @return {object} A promise resolved with the fragment html and js from\n *\/\n ModalEventForm.prototype.reloadBodyContent = function(formData) {\n if (this.reloadingBody) {\n return this.bodyPromise;\n }\n\n this.reloadingBody = true;\n this.disableButtons();\n\n var args = {};\n\n if (this.hasEventId()) {\n args.eventid = this.getEventId();\n }\n\n if (this.hasStartTime()) {\n args.starttime = this.getStartTime();\n }\n\n if (this.hasCourseId()) {\n args.courseid = this.getCourseId();\n }\n\n if (this.hasCategoryId()) {\n args.categoryid = this.getCategoryId();\n }\n\n if (typeof formData !== 'undefined') {\n args.formdata = formData;\n }\n\n this.bodyPromise = Fragment.loadFragment('calendar', 'event_form', this.getContextId(), args);\n\n this.setBody(this.bodyPromise);\n\n this.bodyPromise.then(function() {\n this.enableButtons();\n return;\n }.bind(this))\n .fail(Notification.exception)\n .always(function() {\n this.reloadingBody = false;\n return;\n }.bind(this))\n .fail(Notification.exception);\n\n return this.bodyPromise;\n };\n\n \/**\n * Reload both the title and body content.\n *\n * @method reloadAllContent\n * @return {object} promise\n *\/\n ModalEventForm.prototype.reloadAllContent = function() {\n return $.when(this.reloadTitleContent(), this.reloadBodyContent());\n };\n\n \/**\n * Kick off a reload the modal content before showing it. This\n * is to allow us to re-use the same modal for creating and\n * editing different events within the page.\n *\n * We do the reload when showing the modal rather than hiding it\n * to save a request to the server if the user closes the modal\n * and never re-opens it.\n *\n * @method show\n *\/\n ModalEventForm.prototype.show = function() {\n this.reloadAllContent();\n Modal.prototype.show.call(this);\n };\n\n \/**\n * Clear the event id from the modal when it's closed so\n * that it is loaded fresh next time it's displayed.\n *\n * The event id will be set by the calling code if it wants\n * to edit a specific event.\n *\n * @method hide\n *\/\n ModalEventForm.prototype.hide = function() {\n Modal.prototype.hide.call(this);\n this.setEventId(null);\n this.setStartTime(null);\n this.setCourseId(null);\n this.setCategoryId(null);\n };\n\n \/**\n * Get the serialised form data.\n *\n * @method getFormData\n * @return {string} serialised form data\n *\/\n ModalEventForm.prototype.getFormData = function() {\n return this.getForm().serialize();\n };\n\n \/**\n * Send the form data to the server to create or update\n * an event.\n *\n * If there is a server side validation error then we re-request the\n * rendered form (with the data) from the server in order to get the\n * server side errors to display.\n *\n * On success the modal is hidden and the page is reloaded so that the\n * new event will display.\n *\n * @method save\n * @return {object} A promise\n *\/\n ModalEventForm.prototype.save = function() {\n var invalid,\n loadingContainer = this.saveButton.find(SELECTORS.LOADING_ICON_CONTAINER);\n\n \/\/ Now the change events have run, see if there are any \"invalid\" form fields.\n invalid = this.getForm().find('[aria-invalid=\"true\"]');\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 Promise.resolve();\n }\n\n loadingContainer.removeClass('hidden');\n this.disableButtons();\n\n var formData = this.getFormData();\n \/\/ Send the form data to the server for processing.\n return Repository.submitCreateUpdateForm(formData)\n .then(function(response) {\n if (response.validationerror) {\n \/\/ If there was a server side validation error then\n \/\/ we need to re-request the rendered form from the server\n \/\/ in order to display the error for the user.\n this.reloadBodyContent(formData);\n return;\n } else {\n \/\/ Check whether this was a new event or not.\n \/\/ The hide function unsets the form data so grab this before the hide.\n var isExisting = this.hasEventId();\n\n \/\/ No problemo! Our work here is done.\n this.hide();\n\n \/\/ Trigger the appropriate calendar event so that the view can be updated.\n if (isExisting) {\n $('body').trigger(CalendarEvents.updated, [response.event]);\n } else {\n $('body').trigger(CalendarEvents.created, [response.event]);\n }\n }\n\n return;\n }.bind(this))\n .always(function() {\n \/\/ Regardless of success or error we should always stop\n \/\/ the loading icon and re-enable the buttons.\n loadingContainer.addClass('hidden');\n this.enableButtons();\n\n return;\n }.bind(this))\n .fail(Notification.exception);\n };\n\n \/**\n * Set up all of the event handling for the modal.\n *\n * @method registerEventListeners\n * @fires event:uploadStarted\n * @fires event:formSubmittedByJavascript\n *\/\n ModalEventForm.prototype.registerEventListeners = function() {\n \/\/ Apply parent event listeners.\n Modal.prototype.registerEventListeners.call(this);\n\n \/\/ When the user clicks the save button we trigger the form submission. We need to\n \/\/ trigger an actual submission because there is some JS code in the form that is\n \/\/ listening for this event and doing some stuff (e.g. saving draft areas etc).\n this.getModal().on(CustomEvents.events.activate, SELECTORS.SAVE_BUTTON, function(e, data) {\n this.getForm().submit();\n data.originalEvent.preventDefault();\n e.stopPropagation();\n }.bind(this));\n\n \/\/ Catch the submit event before it is actually processed by the browser and\n \/\/ prevent the submission. We'll take it from here.\n this.getModal().on('submit', function(e) {\n FormEvents.notifyFormSubmittedByJavascript(this.getForm()[0]);\n\n this.save();\n\n \/\/ Stop the form from actually submitting and prevent it's\n \/\/ propagation because we have already handled the event.\n e.preventDefault();\n e.stopPropagation();\n }.bind(this));\n };\n\n \/\/ Automatically register with the modal registry the first time this module is imported so that you can create modals\n \/\/ of this type using the modal factory.\n if (!registered) {\n ModalRegistry.register(ModalEventForm.TYPE, ModalEventForm, 'calendar\/modal_event_form');\n registered = true;\n }\n\n return ModalEventForm;\n});\n"],"names":["define","$","FormEvents","Str","Notification","Templates","CustomEvents","Modal","ModalRegistry","Fragment","CalendarEvents","Repository","registered","SELECTORS","ModalEventForm","root","call","this","eventId","startTime","courseId","categoryId","contextId","reloadingBody","reloadingTitle","saveButton","getFooter","find","TYPE","prototype","Object","create","constructor","setContextId","id","getContextId","setCourseId","getCourseId","setCategoryId","getCategoryId","hasCourseId","hasCategoryId","setEventId","getEventId","hasEventId","setStartTime","time","getStartTime","hasStartTime","getForm","getBody","disableButtons","prop","enableButtons","reloadTitleContent","titlePromise","get_string","then","string","setTitle","bind","always","fail","exception","reloadBodyContent","formData","bodyPromise","args","eventid","starttime","courseid","categoryid","formdata","loadFragment","setBody","reloadAllContent","when","show","hide","getFormData","serialize","save","invalid","loadingContainer","length","first","focus","Promise","resolve","removeClass","submitCreateUpdateForm","response","validationerror","isExisting","trigger","updated","event","created","addClass","registerEventListeners","getModal","on","events","activate","e","data","submit","originalEvent","preventDefault","stopPropagation","notifyFormSubmittedByJavascript","register"],"mappings":";;;;;;;AAsBAA,wCAAO,CACH,SACA,mBACA,WACA,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