MDL-59388 calendar: Add AJAX Monthly navigation

This commit is contained in:
Andrew Nicols 2017-07-19 09:39:34 +08:00 committed by Simey Lameze
parent 64ff737a2f
commit 695c5726b8
9 changed files with 192 additions and 7 deletions

View File

@ -1 +1,5 @@
define(["jquery","core/ajax","core/str","core/templates","core/notification","core/custom_interaction_events","core/modal_events","core/modal_factory","core_calendar/modal_event_form","core_calendar/summary_modal","core_calendar/repository","core_calendar/events"],function(a,b,c,d,e,f,g,h,i,j,k,l){var m={ROOT:"[data-region='calendar']",EVENT_LINK:"[data-action='view-event']",NEW_EVENT_BUTTON:"[data-action='new-event-button']"},n=function(a){var b="type"+a;return c.get_string(b,"core_calendar").then(function(a){return a})},o=function(a){return c.get_string("subsource","core_calendar",a).then(function(b){return a.url?'<a href="'+a.url+'">'+b+"</a>":b})},p=function(b){k.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=n(d.eventtype);if(d.displayeventsource){d.subscription=JSON.parse(d.subscription);var f={url:d.subscription.url,name:d.subscription.name},g=o(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:j.TYPE,body:d.render("core_calendar/event_summary_body",a),templateContext:{canedit:a.canedit,candelete:a.candelete}};return h.create(b)}).done(function(a){a.getRoot().on(g.hidden,function(){a.destroy()}),a.show()}).fail(e.exception)},q=function(a){var b=a.find(m.NEW_EVENT_BUTTON),c=b.attr("data-context-id");return h.create({type:i.TYPE,large:!0,templateContext:{contextid:c}},b)},r=function(b,c){var d=a("body");d.on(l.created,function(){window.location.reload()}),d.on(l.deleted,function(){window.location.reload()}),d.on(l.updated,function(){window.location.reload()}),d.on(l.editActionEvent,function(a,b){window.location.assign(b)}),c.then(function(a){d.on(l.editEvent,function(b,c){a.setEventId(c),a.show()})})},s=function(){var b=a(m.ROOT);a(m.EVENT_LINK).click(function(b){b.preventDefault();var c=a(this).attr("data-event-id");p(c)});var c=q(b);r(b,c)};return{init:function(){s()}}});
<<<<<<< d92c4aa6682300fc7cf86db748a9d1826c115e50
define(["jquery","core/ajax","core/str","core/templates","core/notification","core/custom_interaction_events","core/modal_events","core/modal_factory","core_calendar/modal_event_form","core_calendar/summary_modal","core_calendar/repository","core_calendar/events"],function(a,b,c,d,e,f,g,h,i,j,k,l){var m={ROOT:"[data-region='calendar']",EVENT_LINK:"[data-action='view-event']",NEW_EVENT_BUTTON:"[data-action='new-event-button']"},n=function(a){var b="type"+a;return c.get_string(b,"core_calendar").then(function(a){return a})},o=function(a){return c.get_string("subsource","core_calendar",a).then(function(b){return a.url?'<a href="'+a.url+'">'+b+"</a>":b})},p=function(b){k.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=n(d.eventtype);if(d.displayeventsource){d.subscription=JSON.parse(d.subscription);var f={url:d.subscription.url,name:d.subscription.name},g=o(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:j.TYPE,body:d.render("core_calendar/event_summary_body",a),templateContext:{canedit:a.canedit,candelete:a.candelete}};return h.create(b)}).done(function(a){a.getRoot().on(g.hidden,function(){a.destroy()}),a.show()}).fail(e.exception)},q=function(a){var b=a.find(m.NEW_EVENT_BUTTON),c=b.attr("data-context-id");return h.create({type:i.TYPE,large:!0,templateContext:{contextid:c}},b)},r=function(b,c){var d=a("body");d.on(l.created,function(){window.location.reload()}),d.on(l.deleted,function(){window.location.reload()}),d.on(l.updated,function(){window.location.reload()}),d.on(l.editActionEvent,function(a,b){window.location.assign(b)}),c.then(function(a){d.on(l.editEvent,function(b,c){a.setEventId(c),a.show()})})},s=function(){var b=a(m.ROOT);a(m.EVENT_LINK).click(function(b){b.preventDefault();var c=a(this).attr("data-event-id");p(c)});var c=q(b);r(b,c)};return{init:function(){s()}}});
=======
define(["jquery","core/ajax","core/str","core/templates","core/notification","core/custom_interaction_events","core/modal_events","core/modal_factory","core_calendar/modal_event_form","core_calendar/summary_modal","core_calendar/repository","core_calendar/events","core_calendar/view_manager"],function(a,b,c,d,e,f,g,h,i,j,k,l,m){var n={ROOT:"[data-region='calendar']",EVENT_LINK:"[data-action='view-event']",NEW_EVENT_BUTTON:"[data-action='new-event-button']"},o=function(a){var b="type"+a;return c.get_string(b,"core_calendar").then(function(a){return a})},p=function(a){return c.get_string("subsource","core_calendar",a).then(function(b){return a.url?'<a href="'+a.url+'">'+b+"</a>":b})},q=function(b){k.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=o(d.eventtype);if(d.displayeventsource){d.subscription=JSON.parse(d.subscription);var f={url:d.subscription.url,name:d.subscription.name},g=p(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:j.TYPE,body:d.render("core_calendar/event_summary_body",a)};return a.caneditevent||(b.footer=""),h.create(b)}).done(function(a){a.getRoot().on(g.hidden,function(){a.destroy()}),a.show()}).fail(e.exception)},r=function(a){var b=a.find(n.NEW_EVENT_BUTTON),c=b.attr("data-context-id");return h.create({type:i.TYPE,large:!0,templateContext:{contextid:c}},b)},s=function(b,c){var d=a("body");d.on(l.created,function(){window.location.reload()}),d.on(l.deleted,function(){window.location.reload()}),d.on(l.updated,function(){window.location.reload()}),c.then(function(a){d.on(l.editEvent,function(b,c){a.setEventId(c),a.show()})})},t=function(){var b=a(n.ROOT);a(n.EVENT_LINK).click(function(b){b.preventDefault();var c=a(this).attr("data-event-id");q(c)});var c=r(b);s(b,c)};return{init:function(){m.init(),t()}}});
>>>>>>> MDL-59388 calendar: Add AJAX Monthly navigation

View File

@ -1 +1 @@
define(["jquery","core/ajax"],function(a,b){var c=function(a){var c={methodname:"core_calendar_delete_calendar_events",args:{events:[{eventid:a,repeat:1}]}};return b.call([c])[0]},d=function(a){var c={methodname:"core_calendar_get_calendar_event_by_id",args:{eventid:a}};return b.call([c])[0]},e=function(a){var c={methodname:"core_calendar_submit_create_update_form",args:{formdata:a}};return b.call([c])[0]};return{getEventById:d,deleteEvent:c,submitCreateUpdateForm:e}});
define(["jquery","core/ajax"],function(a,b){var c=function(a){var c={methodname:"core_calendar_delete_calendar_events",args:{events:[{eventid:a,repeat:1}]}};return b.call([c])[0]},d=function(a){var c={methodname:"core_calendar_get_calendar_event_by_id",args:{eventid:a}};return b.call([c])[0]},e=function(a){var c={methodname:"core_calendar_submit_create_update_form",args:{formdata:a}};return b.call([c])[0]},f=function(a,c){var d={methodname:"core_calendar_get_calendar_monthly_view",args:{time:a,courseid:c}};return b.call([d])[0]};return{getEventById:d,deleteEvent:c,submitCreateUpdateForm:e,getCalendarMonthData:f}});

View File

@ -0,0 +1 @@
define(["jquery","core/templates","core/notification","core_calendar/repository"],function(a,b,c,d){var e={ROOT:"[data-region='calendar']",CALENDAR_NAV_LINK:"span.calendarwrapper .arrow_link",CALENDAR_MONTH_WRAPPER:".calendarwrapper"},f=function(b){b=a(b),a(b).on("click",e.CALENDAR_NAV_LINK,function(c){var d=a(b).find(e.CALENDAR_MONTH_WRAPPER).data("courseid"),f=a(c.currentTarget);g(f.attr("href"),f.data("time"),d),c.preventDefault()})},g=function(f,g,h){d.getCalendarMonthData(g,h).then(function(a){return window.history.pushState({},"Some new title",f),b.render("core_calendar/month_detailed",a)}).then(function(c,d){return a(e.CALENDAR_MONTH_WRAPPER).replaceWith(c),b.runTemplateJS(d)}).then(function(){}).fail(c.exception)};return{init:function(){f(e.ROOT)}}});

View File

@ -37,7 +37,8 @@ define([
'core_calendar/modal_event_form',
'core_calendar/summary_modal',
'core_calendar/repository',
'core_calendar/events'
'core_calendar/events',
'core_calendar/view_manager'
],
function(
$,
@ -51,7 +52,8 @@ define([
ModalEventForm,
SummaryModal,
CalendarRepository,
CalendarEvents
CalendarEvents,
CalendarViewManager
) {
var SELECTORS = {
@ -234,6 +236,7 @@ define([
return {
init: function() {
CalendarViewManager.init();
registerEventListeners();
}
};

View File

@ -83,9 +83,30 @@ define(['jquery', 'core/ajax'], function($, Ajax) {
return Ajax.call([request])[0];
};
/**
* Get calendar data for the month view.
*
* @method getCalendarMonthData
* @param {Number} time Timestamp.
* @param {Number} courseid The course id.
* @return {promise} Resolved with the month view data.
*/
var getCalendarMonthData = function (time, courseid) {
var request = {
methodname: 'core_calendar_get_calendar_monthly_view',
args: {
time: time,
courseid: courseid,
}
};
return Ajax.call([request])[0];
};
return {
getEventById: getEventById,
deleteEvent: deleteEvent,
submitCreateUpdateForm: submitCreateUpdateForm
submitCreateUpdateForm: submitCreateUpdateForm,
getCalendarMonthData: getCalendarMonthData
};
});

View File

@ -0,0 +1,81 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A javascript module to handler calendar view changes.
*
* @module core_calendar/view_manager
* @package core_calendar
* @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/templates', 'core/notification', 'core_calendar/repository'],
function($, Templates, Notification, CalendarRepository) {
var SELECTORS = {
ROOT: "[data-region='calendar']",
CALENDAR_NAV_LINK: "span.calendarwrapper .arrow_link",
CALENDAR_MONTH_WRAPPER: ".calendarwrapper",
};
/**
* Register event listeners for the module.
*
* @param {object} root The root element.
*/
var registerEventListeners = function(root) {
root = $(root);
$(root).on('click', SELECTORS.CALENDAR_NAV_LINK, function(e) {
var courseId = $(root).find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid');
var link = $(e.currentTarget);
changeMonth(link.attr('href'), link.data('time'), courseId);
e.preventDefault();
});
};
/**
* Handle changes to the current calendar view.
*
* @param {Number} time The calendar time to be shown
* @param {Number} courseid The id of the course whose events are shown
*/
var changeMonth = function(url, time, courseid) {
CalendarRepository.getCalendarMonthData(time, courseid)
.then(function(context) {
// TODO Fetch the page title from somewhere..?
window.history.pushState({}, 'Some new title', url);
return Templates.render('core_calendar/month_detailed', context);
})
.then(function(html, js) {
$(SELECTORS.CALENDAR_MONTH_WRAPPER).replaceWith(html);
return Templates.runTemplateJS(js);
})
.then(function() {
// TODO Fire an event to say the month changed.
})
.fail(Notification.exception);
};
return {
init: function() {
registerEventListeners(SELECTORS.ROOT);
}
};
});

View File

@ -705,6 +705,7 @@ class core_calendar_external extends external_api {
)
);
}
/**
* Get calendar event by id.
*
@ -745,7 +746,7 @@ class core_calendar_external extends external_api {
*
* @return external_description
*/
public static function get_calendar_event_by_id_returns() {
public static function get_calendar_event_by_id_returns() {
$eventstructure = event_exporter::get_read_structure();
return new external_single_structure(array(
@ -844,4 +845,67 @@ class core_calendar_external extends external_api {
)
);
}
/**
* Get data for the monthly calendar view.
*
* @param int $time The time to be shown
* @param int $courseid The course to be included
* @return array
*/
public static function get_calendar_monthly_view($time, $courseid) {
global $CFG, $DB, $USER, $PAGE;
require_once($CFG->dirroot."/calendar/lib.php");
$warnings = [];
// Parameter validation.
$params = self::validate_parameters(self::get_calendar_monthly_view_parameters(), [
'time' => $time,
'courseid' => $courseid,
]);
if ($courseid != SITEID && !empty($courseid)) {
// Course ID must be valid and existing.
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$courses = [$course->id => $course];
} else {
$course = get_site();
$courses = calendar_get_default_courses();
}
// TODO: Copy what we do in calendar/view.php.
$context = \context_user::instance($USER->id);
self::validate_context($context);
$calendar = new calendar_information(0, 0, 0, $time);
$calendar->prepare_for_view($course, $courses);
list($data, $template) = calendar_get_view($calendar, 'month');
return $data;
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
*/
public static function get_calendar_monthly_view_parameters() {
return new external_function_parameters(
[
'time' => new external_value(PARAM_INT, 'Time to be viewed', VALUE_REQUIRED, '', NULL_NOT_ALLOWED),
'courseid' => new external_value(PARAM_INT, 'Course being viewed', VALUE_DEFAULT, SITEID, NULL_ALLOWED),
]
);
}
/**
* Returns description of method result value.
*
* @return external_description
*/
public static function get_calendar_monthly_view_returns() {
return \core_calendar\external\month_exporter::get_read_structure();
}
}

View File

@ -58,6 +58,16 @@ $functions = array(
'capabilities' => 'moodle/badges:viewotherbadges',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_calendar_get_calendar_monthly_view' => array(
'classname' => 'core_calendar_external',
'methodname' => 'get_calendar_monthly_view',
'description' => 'Fetch the monthly view data for a calendar',
'classpath' => 'calendar/externallib.php',
'type' => 'read',
'capabilities' => '',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_calendar_create_calendar_events' => array(
'classname' => 'core_calendar_external',
'methodname' => 'create_calendar_events',

View File

@ -29,7 +29,8 @@
defined('MOODLE_INTERNAL') || die();
$version = 2017081000.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2017081000.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.