mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 16:32:18 +02:00
MDL-59538 core_calendar: PR fixes
* Fix eslint warnings about promises. * Simplify event handling. * Prevent double assignment of calendar subscription property for in core_calendar_external::get_calendar_event_by_id() for readability. Part of MDL-59333.
This commit is contained in:
parent
52380c823c
commit
a43ec9ebac
2
calendar/amd/build/calendar.min.js
vendored
2
calendar/amd/build/calendar.min.js
vendored
@ -1 +1 @@
|
||||
define(["jquery","core/ajax","core/str","core/templates","core/notification","core/custom_interaction_events","core/modal_factory","core_calendar/summary_modal","core_calendar/calendar_repository"],function(a,b,c,d,e,f,g,h,i){var j={ROOT:"[data-region='calendar']",EVENT_LINK:"[data-action='view-event']"},k=null,l=function(a){var b="type"+a;return c.get_string(b,"core_calendar").then(function(a){return a})},m=function(a){return c.get_string("subsource","core_calendar",a).then(function(b){return a.url?'<a href="'+a.url+'">'+b+"</a>":b})},n=function(b){var c=i.getEventById(b);return c.then(function(a){return a.event?a.event:void c.fail(e.exception)}).then(function(a){var b=l(a.eventtype);return b.then(function(c){return c?(a.eventtype=c,a):void b.fail(e.exception)})}).then(function(b){if(1==b.displayeventsource){b.subscription=JSON.parse(b.subscription);var c=m({url:b.subscription.url,name:b.subscription.name});c?(a.Deferred().resolve(),c.done(function(a){b.source=a})):c.fail(e.exception)}return k.done(function(a){a.setTitle(b.name),a.setBody(d.render("core_calendar/event_summary_body",b)),0==b.caneditevent&&a.setFooter(""),a.show()})})},o=function(b){b=a(b);var c=!1;b.on("click",j.EVENT_LINK,function(b){if(!c){c=!0,b.preventDefault();var d=a(b.target).closest(j.EVENT_LINK),f=d.attr("data-event-id");n(f).done(function(){c=!1}).fail(e.exception)}})};return{init:function(){k=g.create({type:h.TYPE}),o(j.ROOT)}}});
|
||||
define(["jquery","core/ajax","core/str","core/templates","core/notification","core/custom_interaction_events","core/modal_factory","core_calendar/summary_modal","core/modal_events","core_calendar/calendar_repository"],function(a,b,c,d,e,f,g,h,i,j){var k={ROOT:"[data-region='calendar']",EVENT_LINK:"[data-action='view-event']"},l=function(a){var b="type"+a;return c.get_string(b,"core_calendar").then(function(a){return a})},m=function(a){return c.get_string("subsource","core_calendar",a).then(function(b){return a.url?'<a href="'+a.url+'">'+b+"</a>":b})},n=function(b){j.getEventById(b).then(function(c){if(!c.event)throw new Error("Error encountered while trying to fetch calendar event with ID: "+b);var d=c.event,e=l(d.eventtype);if(d.displayeventsource){d.subscription=JSON.parse(d.subscription);var f={url:d.subscription.url,name:d.subscription.name},g=m(f);return a.when(e,g).then(function(a,b){return d.eventtype=a,d.source=b,d})}return e.then(function(a){return d.eventtype=a,d})}).then(function(a){var b={title:a.name,type:h.TYPE,body:d.render("core_calendar/event_summary_body",a)};return a.caneditevent||(b.footer=""),g.create(b)}).done(function(a){a.getRoot().on(i.hidden,function(){a.destroy()}),a.show()}).fail(e.exception)},o=function(){a(k.EVENT_LINK).click(function(b){b.preventDefault();var c=a(this).attr("data-event-id");n(c)})};return{init:function(){o()}}});
|
@ -22,16 +22,14 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
define(['jquery', 'core/ajax', 'core/str', 'core/templates', 'core/notification', 'core/custom_interaction_events',
|
||||
'core/modal_factory', 'core_calendar/summary_modal', 'core_calendar/calendar_repository'],
|
||||
function($, Ajax, Str, Templates, Notification, CustomEvents, ModalFactory, SummaryModal, CalendarRepository) {
|
||||
'core/modal_factory', 'core_calendar/summary_modal', 'core/modal_events', 'core_calendar/calendar_repository'],
|
||||
function($, Ajax, Str, Templates, Notification, CustomEvents, ModalFactory, SummaryModal, ModalEvents, CalendarRepository) {
|
||||
|
||||
var SELECTORS = {
|
||||
ROOT: "[data-region='calendar']",
|
||||
EVENT_LINK: "[data-action='view-event']",
|
||||
};
|
||||
|
||||
var modalPromise = null;
|
||||
|
||||
/**
|
||||
* Get the event type lang string.
|
||||
*
|
||||
@ -64,89 +62,80 @@ define(['jquery', 'core/ajax', 'core/str', 'core/templates', 'core/notification'
|
||||
* Render the event summary modal.
|
||||
*
|
||||
* @param {Number} eventId The calendar event id.
|
||||
* @return {promise} The summary modal promise.
|
||||
*/
|
||||
var renderEventSummaryModal = function(eventId) {
|
||||
|
||||
|
||||
// Calendar repository promise.
|
||||
var repositoryPromise = CalendarRepository.getEventById(eventId);
|
||||
return repositoryPromise.then(function(result) {
|
||||
if (!result.event) {
|
||||
repositoryPromise.fail(Notification.exception);
|
||||
} else {
|
||||
return result.event;
|
||||
CalendarRepository.getEventById(eventId).then(function(getEventResponse) {
|
||||
if (!getEventResponse.event) {
|
||||
throw new Error('Error encountered while trying to fetch calendar event with ID: ' + eventId);
|
||||
}
|
||||
}).then(function(eventdata) {
|
||||
// Event type promise.
|
||||
var eventTypePromise = getEventType(eventdata.eventtype);
|
||||
return eventTypePromise.then(function(langStr) {
|
||||
if(!langStr) {
|
||||
eventTypePromise.fail(Notification.exception);
|
||||
} else {
|
||||
eventdata.eventtype = langStr;
|
||||
return eventdata;
|
||||
}
|
||||
});
|
||||
}).then(function(eventdata) {
|
||||
// If the calendar event has event source, get the language string or the link.
|
||||
if (eventdata.displayeventsource == true) {
|
||||
eventdata.subscription = JSON.parse(eventdata.subscription);
|
||||
var eventpromise = getEventSource({url: eventdata.subscription.url, name: eventdata.subscription.name});
|
||||
if (eventpromise) {
|
||||
$.Deferred().resolve();
|
||||
eventpromise.done(function(source) {
|
||||
eventdata.source = source;
|
||||
});
|
||||
} else {
|
||||
eventpromise.fail(Notification.exception);
|
||||
}
|
||||
var eventData = getEventResponse.event;
|
||||
var eventTypePromise = getEventType(eventData.eventtype);
|
||||
|
||||
// If the calendar event has event source, get the source's language string/link.
|
||||
if (eventData.displayeventsource) {
|
||||
eventData.subscription = JSON.parse(eventData.subscription);
|
||||
var eventSourceParams = {
|
||||
url: eventData.subscription.url,
|
||||
name: eventData.subscription.name
|
||||
};
|
||||
var eventSourcePromise = getEventSource(eventSourceParams);
|
||||
|
||||
// Return event data with event type and event source info.
|
||||
return $.when(eventTypePromise, eventSourcePromise).then(function(eventType, eventSource) {
|
||||
eventData.eventtype = eventType;
|
||||
eventData.source = eventSource;
|
||||
return eventData;
|
||||
});
|
||||
}
|
||||
return modalPromise.done(function(modal) {
|
||||
modal.setTitle(eventdata.name);
|
||||
modal.setBody(Templates.render('core_calendar/event_summary_body', eventdata));
|
||||
// Hide edit and delete buttons if I don't have permission.
|
||||
if (eventdata.caneditevent == false) {
|
||||
modal.setFooter('');
|
||||
}
|
||||
|
||||
modal.show();
|
||||
// Return event data with event type info.
|
||||
return eventTypePromise.then(function(eventType) {
|
||||
eventData.eventtype = eventType;
|
||||
return eventData;
|
||||
});
|
||||
});
|
||||
|
||||
}).then(function(eventData) {
|
||||
// Build the modal parameters from the event data.
|
||||
var modalParams = {
|
||||
title: eventData.name,
|
||||
type: SummaryModal.TYPE,
|
||||
body: Templates.render('core_calendar/event_summary_body', eventData)
|
||||
};
|
||||
if (!eventData.caneditevent) {
|
||||
modalParams.footer = '';
|
||||
}
|
||||
// Create the modal.
|
||||
return ModalFactory.create(modalParams);
|
||||
|
||||
}).done(function(modal) {
|
||||
// Handle hidden event.
|
||||
modal.getRoot().on(ModalEvents.hidden, function() {
|
||||
// Destroy when hidden.
|
||||
modal.destroy();
|
||||
});
|
||||
|
||||
// Finally, render the modal!
|
||||
modal.show();
|
||||
|
||||
}).fail(Notification.exception);
|
||||
};
|
||||
|
||||
/**
|
||||
* Register event listeners for the module.
|
||||
*
|
||||
* @param {object} root The root element.
|
||||
*/
|
||||
var registerEventListeners = function(root) {
|
||||
root = $(root);
|
||||
|
||||
var loading = false;
|
||||
root.on('click', SELECTORS.EVENT_LINK, function(e) {
|
||||
if (!loading) {
|
||||
loading = true;
|
||||
e.preventDefault();
|
||||
|
||||
var eventElement = $(e.target).closest(SELECTORS.EVENT_LINK);
|
||||
var eventId = eventElement.attr('data-event-id');
|
||||
|
||||
renderEventSummaryModal(eventId).done(function() {
|
||||
loading = false;
|
||||
}).fail(Notification.exception);
|
||||
}
|
||||
var registerEventListeners = function() {
|
||||
// Bind click events to event links.
|
||||
$(SELECTORS.EVENT_LINK).click(function(e) {
|
||||
e.preventDefault();
|
||||
var eventId = $(this).attr('data-event-id');
|
||||
renderEventSummaryModal(eventId);
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
init: function() {
|
||||
modalPromise = ModalFactory.create({
|
||||
type: SummaryModal.TYPE
|
||||
});
|
||||
|
||||
registerEventListeners(SELECTORS.ROOT);
|
||||
registerEventListeners();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
@ -728,11 +728,12 @@ class core_calendar_external extends external_api {
|
||||
$subscription = calendar_get_subscription($event[$eventid]->subscriptionid);
|
||||
if (!empty($subscription) && $CFG->calendar_showicalsource) {
|
||||
$event[$eventid]->displayeventsource = true;
|
||||
$subscriptiondata = new stdClass();
|
||||
if (!empty($subscription->url)) {
|
||||
$event[$eventid]->subscription->url = $subscription->url;
|
||||
$subscriptiondata->url = $subscription->url;
|
||||
}
|
||||
$event[$eventid]->subscription->name = $subscription->name;
|
||||
$event[$eventid]->subscription = json_encode($event[$eventid]->subscription);
|
||||
$subscriptiondata->name = $subscription->name;
|
||||
$event[$eventid]->subscription = json_encode($subscriptiondata);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user