From 3b0738f04ce09523c41d1f651d2167d4d45d7975 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 10 Jul 2017 10:04:54 +0800 Subject: [PATCH] MDL-59383 core_calendar: create main calendar AMD module Part of MDL-59333. --- calendar/amd/build/calendar.min.js | Bin 0 -> 1054 bytes calendar/amd/src/calendar.js | 117 +++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 calendar/amd/build/calendar.min.js create mode 100644 calendar/amd/src/calendar.js diff --git a/calendar/amd/build/calendar.min.js b/calendar/amd/build/calendar.min.js new file mode 100644 index 0000000000000000000000000000000000000000..67a1cb2fa9821abba5deb1f9625d142e5f46f24a GIT binary patch literal 1054 zcmZ`&VQbqk5dAByKQt0kmG*0b7?fpDHrg@5V3cCc(m7FLOPMUE4JQA6XC+R$(PD#z zbobu9=O=@ub;zEw)t?r_E)$vd0mra{*J0hnFf1e1n+hT}!-n(GmR3Ww-VJ-YZKAJp z>mmlw{Yj2nbn($>l(r?PgO_E^XX8Q2i*9^CWL2Ewk{Dd z5QWgf2o$AQh^4T++d{ZlsqW|F<9w1m8HjKla6wb4(Ev7j&cy83>|vgNzkm2PR2^nl zqpii4>!g6{_F}DETUDY`XVL>np|ZH!VD<^70b?87g_aA9Ir(SZf)xjAPf{{2;}RWv z3v=v)J(nfeilM|;jho&>z8Cd7l(wgs_4eNF?hOMza2Fq=%OvJA9KMsSx8|aFhw$S8 zD|T$|1^-`G@NUp?VCaifAxIVJ`bhIy8c#tyxj`s}Y%tDktdQs&)W}8Jy7Okoj0}_) z3@ioq2Tr<}4ZOW;fY!e~l*ggd+4&!T`8 zeA_U$BI>itK=9dSDqL(0}Ao zSH8g}vbTs_pfVC-q$yK;6UwzU8K2gS?ElxeM0ZOH=skv^v(CoJ$y(m6)k5lkbdy=v X|DDNF&VT=y?YVG7-Cq@I-}C8T<-c_5 literal 0 HcmV?d00001 diff --git a/calendar/amd/src/calendar.js b/calendar/amd/src/calendar.js new file mode 100644 index 00000000000..ace33647cb9 --- /dev/null +++ b/calendar/amd/src/calendar.js @@ -0,0 +1,117 @@ +// 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 . + +/** + * A javascript module to calendar events. + * + * @module core_calendar/calendar + * @package core_calendar + * @copyright 2017 Simey Lameze + * @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) { + + var SELECTORS = { + ROOT: "[data-region='calendar']", + EVENT_LINK: "[data-action='view-event']", + }; + + var modalPromise = null; + + /** + * Get the event type lang string. + * + * @param {String} eventType The event type. + * @return {String} The lang string of the event type. + */ + var getEventType = function(eventType) { + var lang = 'type' + eventType; + return Str.get_string(lang, 'core_calendar').then(function(langStr) { + return langStr; + }).fail(Notification.exception); + }; + + /** + * Render the event summary modal. + * + * @param {Number} eventId The calendar event id. + * @return {promise} The summary modal promise. + */ + var renderEventSummaryModal = function(eventId) { + + var promise = CalendarRepository.getEventById(eventId); + + return promise.then(function(result) { + if (!result.event) { + promise.fail(Notification.exception); + } else { + return result.event; + } + }).then(function(eventdata) { + return getEventType(eventdata.eventtype).then(function(langStr) { + eventdata.eventtype = langStr; + return eventdata; + }); + }).then(function(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(); + }); + }); + }; + + /** + * 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; + }); + } + }); + }; + + return { + init: function() { + modalPromise = ModalFactory.create({ + type: SummaryModal.TYPE + }); + + registerEventListeners(SELECTORS.ROOT); + } + }; + }); -- 2.17.1