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 javascript module to handle calendar drag and drop in the calendar
18
 * month view.
19
 *
20
 * @module     core_calendar/month_view_drag_drop
21
 * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
define([
25
            'jquery',
26
            'core/notification',
27
            'core/str',
28
            'core_calendar/events',
29
            'core_calendar/drag_drop_data_store'
30
        ],
31
        function(
32
            $,
33
            Notification,
34
            Str,
35
            CalendarEvents,
36
            DataStore
37
        ) {
38
 
39
    var SELECTORS = {
40
        ROOT: "[data-region='calendar']",
41
        DRAGGABLE: '[draggable="true"][data-region="event-item"]',
42
        DROP_ZONE: '[data-drop-zone="month-view-day"]',
43
        WEEK: '[data-region="month-view-week"]',
44
    };
45
    var INVALID_DROP_ZONE_CLASS = 'bg-faded';
46
    var INVALID_HOVER_CLASS = 'bg-danger text-white';
47
    var VALID_HOVER_CLASS = 'bg-primary text-white';
48
    var ALL_CLASSES = INVALID_DROP_ZONE_CLASS + ' ' + INVALID_HOVER_CLASS + ' ' + VALID_HOVER_CLASS;
49
    /* @var {bool} registered If the event listeners have been added */
50
    var registered = false;
51
 
52
    /**
53
     * Get the correct drop zone element from the given javascript
54
     * event.
55
     *
56
     * @param {event} e The javascript event
57
     * @return {object|null}
58
     */
59
    var getDropZoneFromEvent = function(e) {
60
        var dropZone = $(e.target).closest(SELECTORS.DROP_ZONE);
61
        return (dropZone.length) ? dropZone : null;
62
    };
63
 
64
    /**
65
     * Determine if the given dropzone element is within the acceptable
66
     * time range.
67
     *
68
     * The drop zone timestamp is midnight on that day so we should check
69
     * that the event's acceptable timestart value
70
     *
71
     * @param {object} dropZone The drop zone day from the calendar
72
     * @return {bool}
73
     */
74
    var isValidDropZone = function(dropZone) {
75
        var dropTimestamp = dropZone.attr('data-day-timestamp');
76
        var minTimestart = DataStore.getMinTimestart();
77
        var maxTimestart = DataStore.getMaxTimestart();
78
 
79
        if (minTimestart && minTimestart > dropTimestamp) {
80
            return false;
81
        }
82
 
83
        if (maxTimestart && maxTimestart < dropTimestamp) {
84
            return false;
85
        }
86
 
87
        return true;
88
    };
89
 
90
    /**
91
     * Get the error string to display for a given drop zone element
92
     * if it is invalid.
93
     *
94
     * @param {object} dropZone The drop zone day from the calendar
95
     * @return {string}
96
     */
97
    var getDropZoneError = function(dropZone) {
98
        var dropTimestamp = dropZone.attr('data-day-timestamp');
99
        var minTimestart = DataStore.getMinTimestart();
100
        var maxTimestart = DataStore.getMaxTimestart();
101
 
102
        if (minTimestart && minTimestart > dropTimestamp) {
103
            return DataStore.getMinError();
104
        }
105
 
106
        if (maxTimestart && maxTimestart < dropTimestamp) {
107
            return DataStore.getMaxError();
108
        }
109
 
110
        return null;
111
    };
112
 
113
    /**
114
     * Remove all of the styling from each of the drop zones in the calendar.
115
     */
116
    var clearAllDropZonesState = function() {
117
        $(SELECTORS.ROOT).find(SELECTORS.DROP_ZONE).each(function(index, dropZone) {
118
            dropZone = $(dropZone);
119
            dropZone.removeClass(ALL_CLASSES);
120
        });
121
    };
122
 
123
    /**
124
     * Update the hover state for the event in the calendar to reflect
125
     * which days the event will be moved to.
126
     *
127
     * If the drop zone is not being hovered then it will apply some
128
     * styling to reflect whether the drop zone is a valid or invalid
129
     * drop place for the current dragging event.
130
     *
131
     * This funciton supports events spanning multiple days and will
132
     * recurse to highlight (or remove highlight) each of the days
133
     * that the event will be moved to.
134
     *
135
     * For example: An event with a duration of 3 days will have
136
     * 3 days highlighted when it's dragged elsewhere in the calendar.
137
     * The current drag target and the 2 days following it (including
138
     * wrapping to the next week if necessary).
139
     *
140
     * @param {string|object} dropZone The drag target element
141
     * @param {bool} hovered If the target is hovered or not
142
     * @param {Number} count How many days to highlight (default to duration)
143
     */
144
    var updateHoverState = function(dropZone, hovered, count) {
145
        if (typeof count === 'undefined') {
146
            // This is how many days we need to highlight.
147
            count = DataStore.getDurationDays();
148
        }
149
 
150
        var valid = isValidDropZone(dropZone);
151
        dropZone.removeClass(ALL_CLASSES);
152
 
153
        if (hovered) {
154
 
155
            if (valid) {
156
                dropZone.addClass(VALID_HOVER_CLASS);
157
            } else {
158
                dropZone.addClass(INVALID_HOVER_CLASS);
159
            }
160
        } else {
161
            dropZone.removeClass(VALID_HOVER_CLASS + ' ' + INVALID_HOVER_CLASS);
162
 
163
            if (!valid) {
164
                dropZone.addClass(INVALID_DROP_ZONE_CLASS);
165
            }
166
        }
167
 
168
        count--;
169
 
170
        // If we've still got days to highlight then we should
171
        // find the next day.
172
        if (count > 0) {
173
            var nextDropZone = dropZone.next();
174
 
175
            // If there are no more days in this week then we
176
            // need to move down to the next week in the calendar.
177
            if (!nextDropZone.length) {
178
                var nextWeek = dropZone.closest(SELECTORS.WEEK).next();
179
 
180
                if (nextWeek.length) {
181
                    nextDropZone = nextWeek.children(SELECTORS.DROP_ZONE).first();
182
                }
183
            }
184
 
185
            // If we found another day then let's recursively
186
            // update it's hover state.
187
            if (nextDropZone.length) {
188
                updateHoverState(nextDropZone, hovered, count);
189
            }
190
        }
191
    };
192
 
193
    /**
194
     * Find all of the calendar event drop zones in the calendar and update the display
195
     * for the user to indicate which zones are valid and invalid.
196
     */
197
    var updateAllDropZonesState = function() {
198
        $(SELECTORS.ROOT).find(SELECTORS.DROP_ZONE).each(function(index, dropZone) {
199
            dropZone = $(dropZone);
200
 
201
            if (!isValidDropZone(dropZone)) {
202
                updateHoverState(dropZone, false);
203
            }
204
        });
205
    };
206
 
207
 
208
    /**
209
     * Set up the module level variables to track which event is being
210
     * dragged and how many days it spans.
211
     *
212
     * @param {event} e The dragstart event
213
     */
214
    var dragstartHandler = function(e) {
215
        var target = $(e.target);
216
        var draggableElement = target.closest(SELECTORS.DRAGGABLE);
217
 
218
        if (!draggableElement.length) {
219
            return;
220
        }
221
 
222
        var eventElement = draggableElement.find('[data-event-id]');
223
        var eventId = eventElement.attr('data-event-id');
224
        var minTimestart = draggableElement.attr('data-min-day-timestamp');
225
        var maxTimestart = draggableElement.attr('data-max-day-timestamp');
226
        var minError = draggableElement.attr('data-min-day-error');
227
        var maxError = draggableElement.attr('data-max-day-error');
228
        var eventsSelector = SELECTORS.ROOT + ' [data-event-id="' + eventId + '"]';
229
        var duration = $(eventsSelector).length;
230
 
231
        DataStore.setEventId(eventId);
232
        DataStore.setDurationDays(duration);
233
 
234
        if (minTimestart) {
235
            DataStore.setMinTimestart(minTimestart);
236
        }
237
 
238
        if (maxTimestart) {
239
            DataStore.setMaxTimestart(maxTimestart);
240
        }
241
 
242
        if (minError) {
243
            DataStore.setMinError(minError);
244
        }
245
 
246
        if (maxError) {
247
            DataStore.setMaxError(maxError);
248
        }
249
 
250
        e.dataTransfer.effectAllowed = "move";
251
        e.dataTransfer.dropEffect = "move";
252
        // Firefox requires a value to be set here or the drag won't
253
        // work and the dragover handler won't fire.
254
        e.dataTransfer.setData('text/plain', eventId);
255
        e.dropEffect = "move";
256
 
257
        updateAllDropZonesState();
258
    };
259
 
260
    /**
261
     * Update the hover state of the target day element when
262
     * the user is dragging an event over it.
263
     *
264
     * This will add a visual indicator to the calendar UI to
265
     * indicate which day(s) the event will be moved to.
266
     *
267
     * @param {event} e The dragstart event
268
     */
269
    var dragoverHandler = function(e) {
270
        // Ignore dragging of non calendar events.
271
        if (!DataStore.hasEventId()) {
272
            return;
273
        }
274
 
275
        e.preventDefault();
276
 
277
        var dropZone = getDropZoneFromEvent(e);
278
 
279
        if (!dropZone) {
280
            return;
281
        }
282
 
283
        updateHoverState(dropZone, true);
284
    };
285
 
286
    /**
287
     * Update the hover state of the target day element that was
288
     * previously dragged over but has is no longer a drag target.
289
     *
290
     * This will remove the visual indicator from the calendar UI
291
     * that was added by the dragoverHandler.
292
     *
293
     * @param {event} e The dragstart event
294
     */
295
    var dragleaveHandler = function(e) {
296
        // Ignore dragging of non calendar events.
297
        if (!DataStore.hasEventId()) {
298
            return;
299
        }
300
 
301
        var dropZone = getDropZoneFromEvent(e);
302
 
303
        if (!dropZone) {
304
            return;
305
        }
306
 
307
        updateHoverState(dropZone, false);
308
        e.preventDefault();
309
    };
310
 
311
    /**
312
     * Determines the event element, origin day, and destination day
313
     * once the user drops the calendar event. These three bits of data
314
     * are provided as the payload to the "moveEvent" calendar javascript
315
     * event that is fired.
316
     *
317
     * This will remove the visual indicator from the calendar UI
318
     * that was added by the dragoverHandler.
319
     *
320
     * @param {event} e The dragstart event
321
     */
322
    var dropHandler = function(e) {
323
        // Ignore dragging of non calendar events.
324
        if (!DataStore.hasEventId()) {
325
            return;
326
        }
327
 
328
        var dropZone = getDropZoneFromEvent(e);
329
 
330
        if (!dropZone) {
331
            DataStore.clearAll();
332
            clearAllDropZonesState();
333
            return;
334
        }
335
 
336
        if (isValidDropZone(dropZone)) {
337
            var eventId = DataStore.getEventId();
338
            var eventElementSelector = SELECTORS.ROOT + ' [data-event-id="' + eventId + '"]';
339
            var eventElement = $(eventElementSelector);
340
            var origin = null;
341
 
342
            if (eventElement.length) {
343
                origin = eventElement.closest(SELECTORS.DROP_ZONE);
344
            }
345
 
346
            $('body').trigger(CalendarEvents.moveEvent, [eventId, origin, dropZone]);
347
        } else {
348
            // If the drop zone is not valid then there is not need for us to
349
            // try to process it. Instead we can just show an error to the user.
350
            var message = getDropZoneError(dropZone);
351
            Str.get_string('errorinvaliddate', 'calendar').then(function(string) {
352
                Notification.exception({
353
                    name: string,
354
                    message: message || string
355
                });
356
            });
357
        }
358
 
359
        DataStore.clearAll();
360
        clearAllDropZonesState();
361
 
362
        e.preventDefault();
363
    };
364
 
365
    /**
366
     * Clear the data store and remove the drag indicators from the UI
367
     * when the drag event has finished.
368
     */
369
    var dragendHandler = function() {
370
        DataStore.clearAll();
371
        clearAllDropZonesState();
372
    };
373
 
374
    /**
375
     * Re-render the drop zones in the new month to highlight
376
     * which areas are or aren't acceptable to drop the calendar
377
     * event.
378
     */
379
    var calendarMonthChangedHandler = function() {
380
        updateAllDropZonesState();
381
    };
382
 
383
    return {
384
        /**
385
         * Initialise the event handlers for the drag events.
386
         */
387
        init: function() {
388
            if (!registered) {
389
                // These handlers are only added the first time the module
390
                // is loaded because we don't want to have a new listener
391
                // added each time the "init" function is called otherwise we'll
392
                // end up with lots of stale handlers.
393
                document.addEventListener('dragstart', dragstartHandler, false);
394
                document.addEventListener('dragover', dragoverHandler, false);
395
                document.addEventListener('dragleave', dragleaveHandler, false);
396
                document.addEventListener('drop', dropHandler, false);
397
                document.addEventListener('dragend', dragendHandler, false);
398
                $('body').on(CalendarEvents.monthChanged, calendarMonthChangedHandler);
399
                registered = true;
400
            }
401
        },
402
    };
403
});