diff --git a/calendar/amd/build/event_form.min.js b/calendar/amd/build/event_form.min.js index 3a3c97625df..1f6e3ffad98 100644 --- a/calendar/amd/build/event_form.min.js +++ b/calendar/amd/build/event_form.min.js @@ -1 +1 @@ -define(["jquery","core/templates"],function(a,b){var c={EVENT_TYPE:'[name="eventtype"]',EVENT_COURSE_ID:'[name="courseid"]',EVENT_GROUP_COURSE_ID:'[name="groupcourseid"]',EVENT_GROUP_ID:'[name="groupid"]',FORM_GROUP:".form-group",SELECT_OPTION:"option",ADVANCED_ELEMENT:".fitem.advanced",FIELDSET_ADVANCED_ELEMENTS:"fieldset.containsadvancedelements",MORELESS_TOGGLE:".moreless-actions"},d={SHOW_ADVANCED:"event_form-show-advanced",HIDE_ADVANCED:"event_form-hide-advanced",ADVANCED_SHOWN:"event_form-advanced-shown",ADVANCED_HIDDEN:"event_form-advanced-hidden"},e=function(a){a.find(c.FIELDSET_ADVANCED_ELEMENTS).removeClass("containsadvancedelements");var d=a.find(c.MORELESS_TOGGLE);b.replaceNode(d,"","")},f=function(a){a.find(c.ADVANCED_ELEMENT).removeClass("hidden"),a.trigger(d.ADVANCED_SHOWN)},g=function(a){a.find(c.ADVANCED_ELEMENT).addClass("hidden"),a.trigger(d.ADVANCED_HIDDEN)},h=function(a){a.on(d.SHOW_ADVANCED,function(){f(a)}),a.on(d.HIDE_ADVANCED,function(){g(a)})},i=function(b){b.find(c.EVENT_GROUP_ID).find(c.SELECT_OPTION).each(function(b,c){c=a(c);var d=c.attr("value"),e=d.split("-"),f=e[0];c.attr("data-course-id",f)})},j=function(b){var d=b.find(c.EVENT_GROUP_COURSE_ID),e=b.find(c.EVENT_GROUP_ID),f=e.find(c.SELECT_OPTION),g=function(){var b=d.val(),c=null;f.each(function(d,e){e=a(e),e.attr("data-course-id")==b?(e.removeClass("hidden"),e.prop("disabled",!1),(null===c||e.attr("selected"))&&(c=d)):(e.addClass("hidden"),e.prop("disabled",!0))}),e.prop("selectedIndex",c)};d.on("change",g),g()},k=function(b,c){var d=a("#"+b);h(d),e(d),i(d),j(d),c?f(d):g(d)};return{init:k,events:d}}); \ No newline at end of file +define(["jquery"],function(a){var b={EVENT_GROUP_COURSE_ID:'[name="groupcourseid"]',EVENT_GROUP_ID:'[name="groupid"]',SELECT_OPTION:"option"},c=function(c){c.find(b.EVENT_GROUP_ID).find(b.SELECT_OPTION).each(function(b,c){c=a(c);var d=c.attr("value"),e=d.split("-"),f=e[0];c.attr("data-course-id",f)})},d=function(c){var d=c.find(b.EVENT_GROUP_COURSE_ID),e=c.find(b.EVENT_GROUP_ID),f=e.find(b.SELECT_OPTION),g=function(){var b=d.val(),c=null;f.each(function(d,e){e=a(e),e.attr("data-course-id")==b?(e.removeClass("hidden"),e.prop("disabled",!1),(null===c||e.attr("selected"))&&(c=d)):(e.addClass("hidden"),e.prop("disabled",!0))}),e.prop("selectedIndex",c)};d.on("change",g),g()},e=function(b){var e=a("#"+b);c(e),d(e)};return{init:e}}); \ No newline at end of file diff --git a/calendar/amd/build/modal_event_form.min.js b/calendar/amd/build/modal_event_form.min.js index b27c8236cc6..4d5280418c5 100644 --- a/calendar/amd/build/modal_event_form.min.js +++ b/calendar/amd/build/modal_event_form.min.js @@ -1 +1 @@ -define(["jquery","core/event","core/str","core/notification","core/templates","core/custom_interaction_events","core/modal","core/modal_registry","core/fragment","core_calendar/events","core_calendar/repository","core_calendar/event_form"],function(a,b,c,d,e,f,g,h,i,j,k,l){var m=!1,n={MORELESS_BUTTON:'[data-action="more-less-toggle"]',SAVE_BUTTON:'[data-action="save"]',LOADING_ICON_CONTAINER:'[data-region="loading-icon-container"]'},o=function(a){g.call(this,a),this.eventId=null,this.startTime=null,this.courseId=null,this.reloadingBody=!1,this.reloadingTitle=!1,this.saveButton=this.getFooter().find(n.SAVE_BUTTON),this.moreLessButton=this.getFooter().find(n.MORELESS_BUTTON)};return o.TYPE="core_calendar-modal_event_form",o.prototype=Object.create(g.prototype),o.prototype.constructor=o,o.prototype.setCourseId=function(a){this.courseId=a},o.prototype.getCourseId=function(){return this.courseId},o.prototype.hasCourseId=function(){return null!==this.courseId},o.prototype.setEventId=function(a){this.eventId=a},o.prototype.getEventId=function(){return this.eventId},o.prototype.hasEventId=function(){return null!==this.eventId},o.prototype.setStartTime=function(a){this.startTime=a},o.prototype.getStartTime=function(){return this.startTime},o.prototype.hasStartTime=function(){return null!==this.startTime},o.prototype.getForm=function(){return this.getBody().find("form")},o.prototype.disableButtons=function(){this.saveButton.prop("disabled",!0),this.moreLessButton.prop("disabled",!0)},o.prototype.enableButtons=function(){this.saveButton.prop("disabled",!1),this.moreLessButton.prop("disabled",!1)},o.prototype.setMoreButton=function(){this.moreLessButton.attr("data-collapsed","true"),c.get_string("more","calendar").then(function(a){this.moreLessButton.text(a)}.bind(this))},o.prototype.setLessButton=function(){this.moreLessButton.attr("data-collapsed","false"),c.get_string("less","calendar").then(function(a){this.moreLessButton.text(a)}.bind(this))},o.prototype.toggleMoreLessButton=function(){var a=this.getForm();"true"==this.moreLessButton.attr("data-collapsed")?(a.trigger(l.events.SHOW_ADVANCED),this.setLessButton()):(a.trigger(l.events.HIDE_ADVANCED),this.setMoreButton())},o.prototype.reloadTitleContent=function(){return this.reloadingTitle?this.titlePromise:(this.reloadingTitle=!0,this.hasEventId()?this.titlePromise=c.get_string("editevent","calendar"):this.titlePromise=c.get_string("newevent","calendar"),this.titlePromise.then(function(a){return this.setTitle(a),a}.bind(this)).always(function(){this.reloadingTitle=!1}.bind(this)),this.titlePromise)},o.prototype.reloadBodyContent=function(a,b){if(this.reloadingBody)return this.bodyPromise;this.reloadingBody=!0,this.disableButtons();var c=this.saveButton.attr("data-context-id"),e={};return this.hasEventId()&&(e.eventid=this.getEventId()),this.hasStartTime()&&(e.starttime=this.getStartTime()),this.hasCourseId()&&(e.courseid=this.getCourseId()),"undefined"!=typeof a&&(e.formdata=a),e.haserror="undefined"!=typeof b&&b,this.bodyPromise=i.loadFragment("calendar","event_form",c,e),this.setBody(this.bodyPromise),this.bodyPromise.then(function(){this.enableButtons()}.bind(this))["catch"](d.exception).always(function(){this.reloadingBody=!1}.bind(this)),this.bodyPromise},o.prototype.reloadAllContent=function(){return a.when(this.reloadTitleContent(),this.reloadBodyContent())},o.prototype.show=function(){this.reloadAllContent(),g.prototype.show.call(this)},o.prototype.hide=function(){g.prototype.hide.call(this),this.setEventId(null),this.setStartTime(null)},o.prototype.getFormData=function(){return this.getForm().serialize()},o.prototype.save=function(){var b=this.saveButton.find(n.LOADING_ICON_CONTAINER);b.removeClass("hidden"),this.disableButtons();var c=this.getFormData();return k.submitCreateUpdateForm(c).then(function(b){return b.validationerror?this.reloadBodyContent(c,!0):(this.hide(),void(this.hasEventId()?a("body").trigger(j.updated,[b.event]):a("body").trigger(j.created,[b.event])))}.bind(this)).always(function(){b.addClass("hidden"),this.enableButtons()}.bind(this))["catch"](d.exception)},o.prototype.registerEventListeners=function(){g.prototype.registerEventListeners.call(this),this.getModal().on(f.events.activate,n.SAVE_BUTTON,function(a,b){this.getForm().submit(),b.originalEvent.preventDefault(),a.stopPropagation()}.bind(this)),this.getModal().on("submit",function(a){this.save(),a.preventDefault(),a.stopPropagation()}.bind(this)),this.getModal().on(f.events.activate,n.MORELESS_BUTTON,function(a,b){this.toggleMoreLessButton(),b.originalEvent.preventDefault(),a.stopPropagation()}.bind(this)),this.getModal().on(l.events.ADVANCED_SHOWN,function(){this.setLessButton()}.bind(this)),this.getModal().on(l.events.ADVANCED_HIDDEN,function(){this.setMoreButton()}.bind(this))},m||(h.register(o.TYPE,o,"calendar/modal_event_form"),m=!0),o}); \ No newline at end of file +define(["jquery","core/event","core/str","core/notification","core/templates","core/custom_interaction_events","core/modal","core/modal_registry","core/fragment","core_calendar/events","core_calendar/repository"],function(a,b,c,d,e,f,g,h,i,j,k){var l=!1,m={SAVE_BUTTON:'[data-action="save"]',LOADING_ICON_CONTAINER:'[data-region="loading-icon-container"]'},n=function(a){g.call(this,a),this.eventId=null,this.startTime=null,this.courseId=null,this.reloadingBody=!1,this.reloadingTitle=!1,this.saveButton=this.getFooter().find(m.SAVE_BUTTON)};return n.TYPE="core_calendar-modal_event_form",n.prototype=Object.create(g.prototype),n.prototype.constructor=n,n.prototype.setCourseId=function(a){this.courseId=a},n.prototype.getCourseId=function(){return this.courseId},n.prototype.hasCourseId=function(){return null!==this.courseId},n.prototype.setEventId=function(a){this.eventId=a},n.prototype.getEventId=function(){return this.eventId},n.prototype.hasEventId=function(){return null!==this.eventId},n.prototype.setStartTime=function(a){this.startTime=a},n.prototype.getStartTime=function(){return this.startTime},n.prototype.hasStartTime=function(){return null!==this.startTime},n.prototype.getForm=function(){return this.getBody().find("form")},n.prototype.disableButtons=function(){this.saveButton.prop("disabled",!0)},n.prototype.enableButtons=function(){this.saveButton.prop("disabled",!1)},n.prototype.reloadTitleContent=function(){return this.reloadingTitle?this.titlePromise:(this.reloadingTitle=!0,this.hasEventId()?this.titlePromise=c.get_string("editevent","calendar"):this.titlePromise=c.get_string("newevent","calendar"),this.titlePromise.then(function(a){return this.setTitle(a),a}.bind(this)).always(function(){this.reloadingTitle=!1}.bind(this)),this.titlePromise)},n.prototype.reloadBodyContent=function(a){if(this.reloadingBody)return this.bodyPromise;this.reloadingBody=!0,this.disableButtons();var b=this.saveButton.attr("data-context-id"),c={};return this.hasEventId()&&(c.eventid=this.getEventId()),this.hasStartTime()&&(c.starttime=this.getStartTime()),this.hasCourseId()&&(c.courseid=this.getCourseId()),"undefined"!=typeof a&&(c.formdata=a),this.bodyPromise=i.loadFragment("calendar","event_form",b,c),this.setBody(this.bodyPromise),this.bodyPromise.then(function(){this.enableButtons()}.bind(this))["catch"](d.exception).always(function(){this.reloadingBody=!1}.bind(this)),this.bodyPromise},n.prototype.reloadAllContent=function(){return a.when(this.reloadTitleContent(),this.reloadBodyContent())},n.prototype.show=function(){this.reloadAllContent(),g.prototype.show.call(this)},n.prototype.hide=function(){g.prototype.hide.call(this),this.setEventId(null),this.setStartTime(null)},n.prototype.getFormData=function(){return this.getForm().serialize()},n.prototype.save=function(){var b=this.saveButton.find(m.LOADING_ICON_CONTAINER);b.removeClass("hidden"),this.disableButtons();var c=this.getFormData();return k.submitCreateUpdateForm(c).then(function(b){return b.validationerror?this.reloadBodyContent(c):(this.hide(),void(this.hasEventId()?a("body").trigger(j.updated,[b.event]):a("body").trigger(j.created,[b.event])))}.bind(this)).always(function(){b.addClass("hidden"),this.enableButtons()}.bind(this))["catch"](d.exception)},n.prototype.registerEventListeners=function(){g.prototype.registerEventListeners.call(this),this.getModal().on(f.events.activate,m.SAVE_BUTTON,function(a,b){this.getForm().submit(),b.originalEvent.preventDefault(),a.stopPropagation()}.bind(this)),this.getModal().on("submit",function(a){this.save(),a.preventDefault(),a.stopPropagation()}.bind(this))},l||(h.register(n.TYPE,n,"calendar/modal_event_form"),l=!0),n}); \ No newline at end of file diff --git a/calendar/amd/src/event_form.js b/calendar/amd/src/event_form.js index ea3e4172d08..f537f6ddf64 100644 --- a/calendar/amd/src/event_form.js +++ b/calendar/amd/src/event_form.js @@ -21,86 +21,12 @@ * @copyright 2017 Ryan Wyllie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/templates'], function($, Templates) { +define(['jquery'], function($) { var SELECTORS = { - EVENT_TYPE: '[name="eventtype"]', - EVENT_COURSE_ID: '[name="courseid"]', EVENT_GROUP_COURSE_ID: '[name="groupcourseid"]', EVENT_GROUP_ID: '[name="groupid"]', - FORM_GROUP: '.form-group', SELECT_OPTION: 'option', - ADVANCED_ELEMENT: '.fitem.advanced', - FIELDSET_ADVANCED_ELEMENTS: 'fieldset.containsadvancedelements', - MORELESS_TOGGLE: '.moreless-actions' - }; - - var EVENTS = { - SHOW_ADVANCED: 'event_form-show-advanced', - HIDE_ADVANCED: 'event_form-hide-advanced', - ADVANCED_SHOWN: 'event_form-advanced-shown', - ADVANCED_HIDDEN: 'event_form-advanced-hidden', - }; - - /** - * Find the old show more / show less toggle added by the mform and destroy it. - * We are handling the visibility of the advanced fields with the more/less button - * in the footer of the modal that this form is rendered within. - * - * @method destroyOldMoreLessToggle - * @param {object} formElement The root form element - */ - var destroyOldMoreLessToggle = function(formElement) { - formElement.find(SELECTORS.FIELDSET_ADVANCED_ELEMENTS).removeClass('containsadvancedelements'); - var element = formElement.find(SELECTORS.MORELESS_TOGGLE); - Templates.replaceNode(element, '', ''); - }; - - /** - * Find each of the advanced form elements and make them visible. - * - * This function triggers the ADVANCED_SHOWN event for any other - * component to handle (e.g. the event form modal). - * - * @method destroyOldMoreLessToggle - * @param {object} formElement The root form element - */ - var showAdvancedElements = function(formElement) { - formElement.find(SELECTORS.ADVANCED_ELEMENT).removeClass('hidden'); - formElement.trigger(EVENTS.ADVANCED_SHOWN); - }; - - /** - * Find each of the advanced form elements and hide them. - * - * This function triggers the ADVANCED_HIDDEN event for any other - * component to handle (e.g. the event form modal). - * - * @method hideAdvancedElements - * @param {object} formElement The root form element - */ - var hideAdvancedElements = function(formElement) { - formElement.find(SELECTORS.ADVANCED_ELEMENT).addClass('hidden'); - formElement.trigger(EVENTS.ADVANCED_HIDDEN); - }; - - /** - * Listen for any events telling this module to show or hide it's - * advanced elements. - * - * This function listens for SHOW_ADVANCED and HIDE_ADVANCED. - * - * @method listenForShowHideEvents - * @param {object} formElement The root form element - */ - var listenForShowHideEvents = function(formElement) { - formElement.on(EVENTS.SHOW_ADVANCED, function() { - showAdvancedElements(formElement); - }); - - formElement.on(EVENTS.HIDE_ADVANCED, function() { - hideAdvancedElements(formElement); - }); }; /** @@ -177,28 +103,15 @@ define(['jquery', 'core/templates'], function($, Templates) { * * @method init * @param {string} formId The value of the form's id attribute - * @param {bool} hasError If the form has errors rendered form the server. */ - var init = function(formId, hasError) { + var init = function(formId) { var formElement = $('#' + formId); - listenForShowHideEvents(formElement); - destroyOldMoreLessToggle(formElement); parseGroupSelect(formElement); addCourseGroupSelectListeners(formElement); - - // If we know that the form has been rendered with server side - // errors then we need to display all of the elements in the form - // in case one of those elements has the error. - if (hasError) { - showAdvancedElements(formElement); - } else { - hideAdvancedElements(formElement); - } }; return { init: init, - events: EVENTS, }; }); diff --git a/calendar/amd/src/modal_event_form.js b/calendar/amd/src/modal_event_form.js index 034e98de983..24f7b8458ee 100644 --- a/calendar/amd/src/modal_event_form.js +++ b/calendar/amd/src/modal_event_form.js @@ -33,8 +33,7 @@ define([ 'core/modal_registry', 'core/fragment', 'core_calendar/events', - 'core_calendar/repository', - 'core_calendar/event_form' + 'core_calendar/repository' ], function( $, @@ -47,13 +46,11 @@ define([ ModalRegistry, Fragment, CalendarEvents, - Repository, - EventForm + Repository ) { var registered = false; var SELECTORS = { - MORELESS_BUTTON: '[data-action="more-less-toggle"]', SAVE_BUTTON: '[data-action="save"]', LOADING_ICON_CONTAINER: '[data-region="loading-icon-container"]', }; @@ -71,7 +68,6 @@ define([ this.reloadingBody = false; this.reloadingTitle = false; this.saveButton = this.getFooter().find(SELECTORS.SAVE_BUTTON); - this.moreLessButton = this.getFooter().find(SELECTORS.MORELESS_BUTTON); }; ModalEventForm.TYPE = 'core_calendar-modal_event_form'; @@ -185,7 +181,6 @@ define([ */ ModalEventForm.prototype.disableButtons = function() { this.saveButton.prop('disabled', true); - this.moreLessButton.prop('disabled', true); }; /** @@ -195,53 +190,6 @@ define([ */ ModalEventForm.prototype.enableButtons = function() { this.saveButton.prop('disabled', false); - this.moreLessButton.prop('disabled', false); - }; - - /** - * Set the more/less button in the footer to the "more" - * state. - * - * @method setMoreButton - */ - ModalEventForm.prototype.setMoreButton = function() { - this.moreLessButton.attr('data-collapsed', 'true'); - Str.get_string('more', 'calendar').then(function(string) { - this.moreLessButton.text(string); - return; - }.bind(this)); - }; - - /** - * Set the more/less button in the footer to the "less" - * state. - * - * @method setLessButton - */ - ModalEventForm.prototype.setLessButton = function() { - this.moreLessButton.attr('data-collapsed', 'false'); - Str.get_string('less', 'calendar').then(function(string) { - this.moreLessButton.text(string); - return; - }.bind(this)); - }; - - /** - * Toggle the more/less button in the footer from the current - * state to it's opposite state. - * - * @method toggleMoreLessButton - */ - ModalEventForm.prototype.toggleMoreLessButton = function() { - var form = this.getForm(); - - if (this.moreLessButton.attr('data-collapsed') == 'true') { - form.trigger(EventForm.events.SHOW_ADVANCED); - this.setLessButton(); - } else { - form.trigger(EventForm.events.HIDE_ADVANCED); - this.setMoreButton(); - } }; /** @@ -288,10 +236,9 @@ define([ * * @method reloadBodyContent * @param {string} formData The serialised form data - * @param {bool} hasError True if we know the form data is erroneous * @return {object} A promise resolved with the fragment html and js from */ - ModalEventForm.prototype.reloadBodyContent = function(formData, hasError) { + ModalEventForm.prototype.reloadBodyContent = function(formData) { if (this.reloadingBody) { return this.bodyPromise; } @@ -318,8 +265,6 @@ define([ args.formdata = formData; } - args.haserror = (typeof hasError == 'undefined') ? false : hasError; - this.bodyPromise = Fragment.loadFragment('calendar', 'event_form', contextId, args); this.setBody(this.bodyPromise); @@ -416,7 +361,7 @@ define([ // If there was a server side validation error then // we need to re-request the rendered form from the server // in order to display the error for the user. - return this.reloadBodyContent(formData, true); + return this.reloadBodyContent(formData); } else { // No problemo! Our work here is done. this.hide(); @@ -469,28 +414,6 @@ define([ e.preventDefault(); e.stopPropagation(); }.bind(this)); - - // Toggle the state of the more/less button in the footer. - this.getModal().on(CustomEvents.events.activate, SELECTORS.MORELESS_BUTTON, function(e, data) { - this.toggleMoreLessButton(); - - data.originalEvent.preventDefault(); - e.stopPropagation(); - }.bind(this)); - - // When the event form tells us that the advanced fields are shown - // then the more/less button should be set to less to allow the user - // to hide the advanced fields. - this.getModal().on(EventForm.events.ADVANCED_SHOWN, function() { - this.setLessButton(); - }.bind(this)); - - // When the event form tells us that the advanced fields are hidden - // then the more/less button should be set to more to allow the user - // to show the advanced fields. - this.getModal().on(EventForm.events.ADVANCED_HIDDEN, function() { - this.setMoreButton(); - }.bind(this)); }; // Automatically register with the modal registry the first time this module is imported so that you can create modals diff --git a/calendar/classes/local/event/forms/create.php b/calendar/classes/local/event/forms/create.php index 165b14508d3..84599138083 100644 --- a/calendar/classes/local/event/forms/create.php +++ b/calendar/classes/local/event/forms/create.php @@ -41,7 +41,6 @@ class create extends \moodleform { global $PAGE; $mform = $this->_form; - $haserror = !empty($this->_customdata['haserror']); $starttime = isset($this->_customdata['starttime']) ? $this->_customdata['starttime'] : 0; $eventtypes = calendar_get_all_allowed_types(); @@ -82,8 +81,7 @@ class create extends \moodleform { $this->add_event_repeat_elements($mform); // Add the javascript required to enhance this mform. - // Including the show/hide of advanced elements and the display of the correct select elements for event types. - $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id'), $haserror]); + $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]); } /** diff --git a/calendar/lib.php b/calendar/lib.php index c1b342b9629..713dc0cb23c 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -3468,10 +3468,6 @@ function calendar_output_fragment_event_form($args) { parse_str(clean_param($args['formdata'], PARAM_TEXT), $data); } - if (isset($args['haserror'])) { - $formoptions['haserror'] = clean_param($args['haserror'], PARAM_BOOL); - } - if ($starttime) { $formoptions['starttime'] = $starttime; } diff --git a/calendar/templates/modal_event_form.mustache b/calendar/templates/modal_event_form.mustache index e99c4910dcc..99a2f59cd1f 100644 --- a/calendar/templates/modal_event_form.mustache +++ b/calendar/templates/modal_event_form.mustache @@ -40,13 +40,6 @@ {{< core/modal }} {{$footer}} -