Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * A module to handle CRUD operations within the UI.
18
 *
19
 * @module     core_calendar/crud
20
 * @copyright  2017 Andrew Nicols <andrew@nicols.co.uk>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define([
24
    'jquery',
25
    'core/str',
26
    'core/notification',
27
    'core/modal_events',
28
    'core_calendar/modal_event_form',
29
    'core_calendar/repository',
30
    'core_calendar/events',
31
    'core_calendar/modal_delete',
32
    'core_calendar/selectors',
33
    'core/pending',
34
    'core/modal_save_cancel',
35
    'core/config',
36
],
37
function(
38
    $,
39
    Str,
40
    Notification,
41
    ModalEvents,
42
    ModalEventForm,
43
    CalendarRepository,
44
    CalendarEvents,
45
    CalendarModalDelete,
46
    CalendarSelectors,
47
    Pending,
48
    ModalSaveCancel,
49
    Config,
50
) {
51
 
52
    /**
53
     * Prepares the action for the summary modal's delete action.
54
     *
55
     * @param {Number} eventId The ID of the event.
56
     * @param {string} eventTitle The event title.
57
     * @param {Number} eventCount The number of events in the series.
58
     * @return {Promise}
59
     */
60
    function confirmDeletion(eventId, eventTitle, eventCount) {
61
        var pendingPromise = new Pending('core_calendar/crud:confirmDeletion');
62
        var deleteStrings = [
63
            {
64
                key: 'deleteevent',
65
                component: 'calendar'
66
            },
67
        ];
68
 
69
        eventCount = parseInt(eventCount, 10);
70
        var deletePromise;
71
        var isRepeatedEvent = eventCount > 1;
72
        if (isRepeatedEvent) {
73
            deleteStrings.push({
74
                key: 'confirmeventseriesdelete',
75
                component: 'calendar',
76
                param: {
77
                    name: eventTitle,
78
                    count: eventCount,
79
                },
80
            });
81
 
82
            deletePromise = CalendarModalDelete.create();
83
        } else {
84
            deleteStrings.push({
85
                key: 'confirmeventdelete',
86
                component: 'calendar',
87
                param: eventTitle
88
            });
89
 
90
 
91
            deletePromise = ModalSaveCancel.create();
92
        }
93
 
94
        var stringsPromise = Str.get_strings(deleteStrings);
95
 
96
        var finalPromise = $.when(stringsPromise, deletePromise)
97
        .then(function(strings, deleteModal) {
98
            deleteModal.setRemoveOnClose(true);
99
            deleteModal.setTitle(strings[0]);
100
            deleteModal.setBody(strings[1]);
101
            if (!isRepeatedEvent) {
102
                deleteModal.setSaveButtonText(strings[0]);
103
            }
104
 
105
            deleteModal.show();
106
 
107
            deleteModal.getRoot().on(ModalEvents.save, function() {
108
                var pendingPromise = new Pending('calendar/crud:initModal:deletedevent');
109
                CalendarRepository.deleteEvent(eventId, false)
110
                    .then(function() {
111
                        $('body').trigger(CalendarEvents.deleted, [eventId, false]);
112
                        return;
113
                    })
114
                    .then(pendingPromise.resolve)
115
                    .catch(Notification.exception);
116
            });
117
 
118
            deleteModal.getRoot().on(CalendarEvents.deleteAll, function() {
119
                var pendingPromise = new Pending('calendar/crud:initModal:deletedallevent');
120
                CalendarRepository.deleteEvent(eventId, true)
121
                    .then(function() {
122
                        $('body').trigger(CalendarEvents.deleted, [eventId, true]);
123
                        return;
124
                    })
125
                    .then(pendingPromise.resolve)
126
                    .catch(Notification.exception);
127
            });
128
 
129
            return deleteModal;
130
        })
131
        .then(function(modal) {
132
            pendingPromise.resolve();
133
 
134
            return modal;
135
        })
136
        .catch(Notification.exception);
137
 
138
        return finalPromise;
139
    }
140
 
141
    /**
142
     * Create the event form modal for creating new events and
143
     * editing existing events.
144
     *
145
     * @method registerEventFormModal
146
     * @param {object} root The calendar root element
147
     * @return {object} The create modal promise
148
     */
149
    var registerEventFormModal = function(root) {
150
        var eventFormPromise = ModalEventForm.create();
151
 
152
        // Bind click event on the new event button.
153
        root.on('click', CalendarSelectors.actions.create, function(e) {
154
            eventFormPromise.then(function(modal) {
155
                var wrapper = root.find(CalendarSelectors.wrapper);
156
 
157
                var categoryId = wrapper.data('categoryid');
158
                const courseId = wrapper.data('courseid');
159
                if (typeof categoryId !== 'undefined' && courseId != Config.siteId) {
160
                    modal.setCategoryId(categoryId);
161
                }
162
 
163
                // Attempt to find the cell for today.
164
                // If it can't be found, then use the start time of the first day on the calendar.
165
                var today = root.find(CalendarSelectors.today);
166
                var firstDay = root.find(CalendarSelectors.day);
167
                if (!today.length && firstDay.length) {
168
                    modal.setStartTime(firstDay.data('newEventTimestamp'));
169
                }
170
 
171
                modal.setContextId(wrapper.data('contextId'));
172
                modal.setCourseId(wrapper.data('courseid'));
173
                modal.show();
174
                return;
175
            })
176
            .catch(Notification.exception);
177
 
178
            e.preventDefault();
179
        });
180
 
181
        root.on('click', CalendarSelectors.actions.edit, function(e) {
182
            e.preventDefault();
183
            var target = $(e.currentTarget),
184
                calendarWrapper = target.closest(CalendarSelectors.wrapper),
185
                eventWrapper = target.closest(CalendarSelectors.eventItem);
186
 
187
            eventFormPromise.then(function(modal) {
188
                // When something within the calendar tells us the user wants
189
                // to edit an event then show the event form modal.
190
                modal.setEventId(eventWrapper.data('eventId'));
191
 
192
                modal.setContextId(calendarWrapper.data('contextId'));
193
                modal.setCourseId(eventWrapper.data('courseId'));
194
                modal.show();
195
 
196
                e.stopImmediatePropagation();
197
                return;
198
            }).catch(Notification.exception);
199
        });
200
 
201
 
202
        return eventFormPromise;
203
    };
204
    /**
205
     * Register the listeners required to remove the event.
206
     *
207
     * @param   {jQuery} root
208
     */
209
    function registerRemove(root) {
210
        root.on('click', CalendarSelectors.actions.remove, function(e) {
211
            // Fetch the event title, count, and pass them into the new dialogue.
212
            var eventSource = $(this).closest(CalendarSelectors.eventItem);
213
            var eventId = eventSource.data('eventId'),
214
                eventTitle = eventSource.data('eventTitle'),
215
                eventCount = eventSource.data('eventCount');
216
            confirmDeletion(eventId, eventTitle, eventCount);
217
 
218
            e.preventDefault();
219
        });
220
    }
221
 
222
    /**
223
     * Register the listeners required to edit the event.
224
     *
225
     * @param   {jQuery} root
226
     * @param   {Promise} eventFormModalPromise
227
     * @returns {Promise}
228
     */
229
    function registerEditListeners(root, eventFormModalPromise) {
230
        var pendingPromise = new Pending('core_calendar/crud:registerEditListeners');
231
 
232
        return eventFormModalPromise
233
        .then(function(modal) {
234
            // When something within the calendar tells us the user wants
235
            // to edit an event then show the event form modal.
236
            $('body').on(CalendarEvents.editEvent, function(e, eventId) {
237
                var target = root.find(`[data-event-id=${eventId}]`),
238
                    calendarWrapper = root.find(CalendarSelectors.wrapper);
239
 
240
                modal.setEventId(eventId);
241
                modal.setContextId(calendarWrapper.data('contextId'));
242
                modal.setReturnElement(target);
243
                modal.show();
244
 
245
                e.stopImmediatePropagation();
246
            });
247
            return modal;
248
        })
249
        .then(function(modal) {
250
            pendingPromise.resolve();
251
 
252
            return modal;
253
        })
254
        .catch(Notification.exception);
255
    }
256
 
257
    return {
258
        registerRemove: registerRemove,
259
        registerEditListeners: registerEditListeners,
260
        registerEventFormModal: registerEventFormModal
261
    };
262
});