forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.events.js
899 lines (716 loc) · 27.2 KB
/
Grid.events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/* Event-rendering and event-interaction methods for the abstract Grid class
----------------------------------------------------------------------------------------------------------------------*/
Grid.mixin({
mousedOverSeg: null, // the segment object the user's mouse is over. null if over nothing
isDraggingSeg: false, // is a segment being dragged? boolean
isResizingSeg: false, // is a segment being resized? boolean
segs: null, // the event segments currently rendered in the grid
// Renders the given events onto the grid
renderEvents: function(events) {
var segs = this.eventsToSegs(events);
var bgSegs = [];
var fgSegs = [];
var i, seg;
for (i = 0; i < segs.length; i++) {
seg = segs[i];
if (isBgEvent(seg.event)) {
bgSegs.push(seg);
}
else {
fgSegs.push(seg);
}
}
// Render each different type of segment.
// Each function may return a subset of the segs, segs that were actually rendered.
bgSegs = this.renderBgSegs(bgSegs) || bgSegs;
fgSegs = this.renderFgSegs(fgSegs) || fgSegs;
this.segs = bgSegs.concat(fgSegs);
},
// Unrenders all events currently rendered on the grid
destroyEvents: function() {
this.triggerSegMouseout(); // trigger an eventMouseout if user's mouse is over an event
this.destroyFgSegs();
this.destroyBgSegs();
this.segs = null;
},
// Retrieves all rendered segment objects currently rendered on the grid
getEventSegs: function() {
return this.segs || [];
},
/* Foreground Segment Rendering
------------------------------------------------------------------------------------------------------------------*/
// Renders foreground event segments onto the grid. May return a subset of segs that were rendered.
renderFgSegs: function(segs) {
// subclasses must implement
},
// Unrenders all currently rendered foreground segments
destroyFgSegs: function() {
// subclasses must implement
},
// Renders and assigns an `el` property for each foreground event segment.
// Only returns segments that successfully rendered.
// A utility that subclasses may use.
renderFgSegEls: function(segs, disableResizing) {
var view = this.view;
var html = '';
var renderedSegs = [];
var i;
if (segs.length) { // don't build an empty html string
// build a large concatenation of event segment HTML
for (i = 0; i < segs.length; i++) {
html += this.fgSegHtml(segs[i], disableResizing);
}
// Grab individual elements from the combined HTML string. Use each as the default rendering.
// Then, compute the 'el' for each segment. An el might be null if the eventRender callback returned false.
$(html).each(function(i, node) {
var seg = segs[i];
var el = view.resolveEventEl(seg.event, $(node));
if (el) {
el.data('fc-seg', seg); // used by handlers
seg.el = el;
renderedSegs.push(seg);
}
});
}
return renderedSegs;
},
// Generates the HTML for the default rendering of a foreground event segment. Used by renderFgSegEls()
fgSegHtml: function(seg, disableResizing) {
// subclasses should implement
},
/* Background Segment Rendering
------------------------------------------------------------------------------------------------------------------*/
// Renders the given background event segments onto the grid.
// Returns a subset of the segs that were actually rendered.
renderBgSegs: function(segs) {
return this.renderFill('bgEvent', segs);
},
// Unrenders all the currently rendered background event segments
destroyBgSegs: function() {
this.destroyFill('bgEvent');
},
// Renders a background event element, given the default rendering. Called by the fill system.
bgEventSegEl: function(seg, el) {
return this.view.resolveEventEl(seg.event, el); // will filter through eventRender
},
// Generates an array of classNames to be used for the default rendering of a background event.
// Called by the fill system.
bgEventSegClasses: function(seg) {
var event = seg.event;
var source = event.source || {};
return [ 'fc-bgevent' ].concat(
event.className,
source.className || []
);
},
// Generates a semicolon-separated CSS string to be used for the default rendering of a background event.
// Called by the fill system.
// TODO: consolidate with getEventSkinCss?
bgEventSegStyles: function(seg) {
var view = this.view;
var event = seg.event;
var source = event.source || {};
var eventColor = event.color;
var sourceColor = source.color;
var optionColor = view.opt('eventColor');
var backgroundColor =
event.backgroundColor ||
eventColor ||
source.backgroundColor ||
sourceColor ||
view.opt('eventBackgroundColor') ||
optionColor;
if (backgroundColor) {
return 'background-color:' + backgroundColor;
}
return '';
},
// Generates an array of classNames to be used for the rendering business hours overlay. Called by the fill system.
businessHoursSegClasses: function(seg) {
return [ 'fc-nonbusiness', 'fc-bgevent' ];
},
/* Handlers
------------------------------------------------------------------------------------------------------------------*/
// Attaches event-element-related handlers to the container element and leverage bubbling
bindSegHandlers: function() {
var _this = this;
var view = this.view;
$.each(
{
mouseenter: function(seg, ev) {
_this.triggerSegMouseover(seg, ev);
},
mouseleave: function(seg, ev) {
_this.triggerSegMouseout(seg, ev);
},
click: function(seg, ev) {
return view.trigger('eventClick', this, seg.event, ev); // can return `false` to cancel
},
mousedown: function(seg, ev) {
if ($(ev.target).is('.fc-resizer') && view.isEventResizable(seg.event)) {
_this.segResizeMousedown(seg, ev);
}
else if (view.isEventDraggable(seg.event)) {
_this.segDragMousedown(seg, ev);
}
}
},
function(name, func) {
// attach the handler to the container element and only listen for real event elements via bubbling
_this.el.on(name, '.fc-event-container > *', function(ev) {
var seg = $(this).data('fc-seg'); // grab segment data. put there by View::renderEvents
// only call the handlers if there is not a drag/resize in progress
if (seg && !_this.isDraggingSeg && !_this.isResizingSeg) {
return func.call(this, seg, ev); // `this` will be the event element
}
});
}
);
},
// Updates internal state and triggers handlers for when an event element is moused over
triggerSegMouseover: function(seg, ev) {
if (!this.mousedOverSeg) {
this.mousedOverSeg = seg;
this.view.trigger('eventMouseover', seg.el[0], seg.event, ev);
}
},
// Updates internal state and triggers handlers for when an event element is moused out.
// Can be given no arguments, in which case it will mouseout the segment that was previously moused over.
triggerSegMouseout: function(seg, ev) {
ev = ev || {}; // if given no args, make a mock mouse event
if (this.mousedOverSeg) {
seg = seg || this.mousedOverSeg; // if given no args, use the currently moused-over segment
this.mousedOverSeg = null;
this.view.trigger('eventMouseout', seg.el[0], seg.event, ev);
}
},
/* Event Dragging
------------------------------------------------------------------------------------------------------------------*/
// Called when the user does a mousedown on an event, which might lead to dragging.
// Generic enough to work with any type of Grid.
segDragMousedown: function(seg, ev) {
var _this = this;
var view = this.view;
var el = seg.el;
var event = seg.event;
var dropLocation;
// A clone of the original element that will move with the mouse
var mouseFollower = new MouseFollower(seg.el, {
parentEl: view.el,
opacity: view.opt('dragOpacity'),
revertDuration: view.opt('dragRevertDuration'),
zIndex: 2 // one above the .fc-view
});
// Tracks mouse movement over the *view's* coordinate map. Allows dragging and dropping between subcomponents
// of the view.
var dragListener = new DragListener(view.coordMap, {
distance: 5,
scroll: view.opt('dragScroll'),
listenStart: function(ev) {
mouseFollower.hide(); // don't show until we know this is a real drag
mouseFollower.start(ev);
},
dragStart: function(ev) {
_this.triggerSegMouseout(seg, ev); // ensure a mouseout on the manipulated event has been reported
_this.isDraggingSeg = true;
view.hideEvent(event); // hide all event segments. our mouseFollower will take over
view.trigger('eventDragStart', el[0], event, ev, {}); // last argument is jqui dummy
},
cellOver: function(cell, isOrig) {
var origCell = seg.cell || dragListener.origCell; // starting cell could be forced (DayGrid.limit)
dropLocation = _this.computeEventDrop(origCell, cell, event);
if (dropLocation) {
if (view.renderDrag(dropLocation, seg)) { // have the subclass render a visual indication
mouseFollower.hide(); // if the subclass is already using a mock event "helper", hide our own
}
else {
mouseFollower.show();
}
if (isOrig) {
dropLocation = null; // needs to have moved cells to be a valid drop
}
}
else {
// have the helper follow the mouse (no snapping) with a warning-style cursor
mouseFollower.show();
disableCursor();
}
},
cellOut: function() { // called before mouse moves to a different cell OR moved out of all cells
dropLocation = null;
view.destroyDrag(); // unrender whatever was done in renderDrag
mouseFollower.show(); // show in case we are moving out of all cells
enableCursor();
},
dragStop: function(ev) {
// do revert animation if hasn't changed. calls a callback when finished (whether animation or not)
mouseFollower.stop(!dropLocation, function() {
_this.isDraggingSeg = false;
view.destroyDrag();
view.showEvent(event);
view.trigger('eventDragStop', el[0], event, ev, {}); // last argument is jqui dummy
if (dropLocation) {
view.reportEventDrop(event, dropLocation, el, ev);
}
});
enableCursor();
},
listenStop: function() {
mouseFollower.stop(); // put in listenStop in case there was a mousedown but the drag never started
}
});
dragListener.mousedown(ev); // start listening, which will eventually lead to a dragStart
},
// Given the cell an event drag began, and the cell event was dropped, calculates the new start/end/allDay
// values for the event. Subclasses may override and set additional properties to be used by renderDrag.
// A falsy returned value indicates an invalid drop.
computeEventDrop: function(startCell, endCell, event) {
var dragStart = startCell.start;
var dragEnd = endCell.start;
var delta;
var newStart;
var newEnd;
var newAllDay;
var dropLocation;
if (dragStart.hasTime() === dragEnd.hasTime()) {
delta = diffDayTime(dragEnd, dragStart);
newStart = event.start.clone().add(delta);
if (event.end === null) { // do we need to compute an end?
newEnd = null;
}
else {
newEnd = event.end.clone().add(delta);
}
newAllDay = event.allDay; // keep it the same
}
else {
// if switching from day <-> timed, start should be reset to the dropped date, and the end cleared
newStart = dragEnd.clone();
newEnd = null; // end should be cleared
newAllDay = !dragEnd.hasTime();
}
dropLocation = {
start: newStart,
end: newEnd,
allDay: newAllDay
};
if (!this.view.calendar.isEventRangeAllowed(dropLocation, event)) {
return null;
}
return dropLocation;
},
/* External Element Dragging
------------------------------------------------------------------------------------------------------------------*/
// Called when a jQuery UI drag is initiated anywhere in the DOM
documentDragStart: function(ev, ui) {
var view = this.view;
var el;
var accept;
if (view.opt('droppable')) { // only listen if this setting is on
el = $(ev.target);
// Test that the dragged element passes the dropAccept selector or filter function.
// FYI, the default is "*" (matches all)
accept = view.opt('dropAccept');
if ($.isFunction(accept) ? accept.call(el[0], el) : el.is(accept)) {
this.startExternalDrag(el, ev, ui);
}
}
},
// Called when a jQuery UI drag starts and it needs to be monitored for cell dropping
startExternalDrag: function(el, ev, ui) {
var _this = this;
var meta = getDraggedElMeta(el); // extra data about event drop, including possible event to create
var dragListener;
var dropLocation; // a null value signals an unsuccessful drag
// listener that tracks mouse movement over date-associated pixel regions
dragListener = new DragListener(this.coordMap, {
cellOver: function(cell) {
dropLocation = _this.computeExternalDrop(cell, meta);
if (dropLocation) {
_this.renderDrag(dropLocation); // called without a seg parameter
}
else { // invalid drop cell
disableCursor();
}
},
cellOut: function() {
dropLocation = null; // signal unsuccessful
_this.destroyDrag();
enableCursor();
}
});
// gets called, only once, when jqui drag is finished
$(document).one('dragstop', function(ev, ui) {
_this.destroyDrag();
enableCursor();
if (dropLocation) { // element was dropped on a valid date/time cell
_this.view.reportExternalDrop(meta, dropLocation, el, ev, ui);
}
});
dragListener.startDrag(ev); // start listening immediately
},
// Given a cell to be dropped upon, and misc data associated with the jqui drag (guaranteed to be a plain object),
// returns start/end dates for the event that would result from the hypothetical drop. end might be null.
// Returning a null value signals an invalid drop cell.
computeExternalDrop: function(cell, meta) {
var dropLocation = {
start: cell.start.clone(),
end: null
};
// if dropped on an all-day cell, and element's metadata specified a time, set it
if (meta.startTime && !dropLocation.start.hasTime()) {
dropLocation.start.time(meta.startTime);
}
if (meta.duration) {
dropLocation.end = dropLocation.start.clone().add(meta.duration);
}
if (!this.view.calendar.isExternalDropRangeAllowed(dropLocation, meta.eventProps)) {
return null;
}
return dropLocation;
},
/* Drag Rendering (for both events and an external elements)
------------------------------------------------------------------------------------------------------------------*/
// Renders a visual indication of an event or external element being dragged.
// `dropLocation` contains hypothetical start/end/allDay values the event would have if dropped. end can be null.
// `seg` is the internal segment object that is being dragged. If dragging an external element, `seg` is null.
// A truthy returned value indicates this method has rendered a helper element.
renderDrag: function(dropLocation, seg) {
// subclasses must implement
},
// Unrenders a visual indication of an event or external element being dragged
destroyDrag: function() {
// subclasses must implement
},
/* Resizing
------------------------------------------------------------------------------------------------------------------*/
// Called when the user does a mousedown on an event's resizer, which might lead to resizing.
// Generic enough to work with any type of Grid.
segResizeMousedown: function(seg, ev) {
var _this = this;
var view = this.view;
var calendar = view.calendar;
var el = seg.el;
var event = seg.event;
var start = event.start;
var oldEnd = calendar.getEventEnd(event);
var newEnd; // falsy if invalid resize
var dragListener;
function destroy() { // resets the rendering to show the original event
_this.destroyEventResize();
view.showEvent(event);
enableCursor();
}
// Tracks mouse movement over the *grid's* coordinate map
dragListener = new DragListener(this.coordMap, {
distance: 5,
scroll: view.opt('dragScroll'),
dragStart: function(ev) {
_this.triggerSegMouseout(seg, ev); // ensure a mouseout on the manipulated event has been reported
_this.isResizingSeg = true;
view.trigger('eventResizeStart', el[0], event, ev, {}); // last argument is jqui dummy
},
cellOver: function(cell) {
newEnd = cell.end;
if (!newEnd.isAfter(start)) { // was end moved before start?
newEnd = start.clone().add( // make the event span a single slot
diffDayTime(cell.end, cell.start) // assumes all slot durations are the same
);
}
if (newEnd.isSame(oldEnd)) {
newEnd = null;
}
else if (!calendar.isEventRangeAllowed({ start: start, end: newEnd }, event)) {
newEnd = null;
disableCursor();
}
else {
_this.renderEventResize({ start: start, end: newEnd }, seg);
view.hideEvent(event);
}
},
cellOut: function() { // called before mouse moves to a different cell OR moved out of all cells
newEnd = null;
destroy();
},
dragStop: function(ev) {
_this.isResizingSeg = false;
destroy();
view.trigger('eventResizeStop', el[0], event, ev, {}); // last argument is jqui dummy
if (newEnd) { // valid date to resize to?
view.reportEventResize(event, newEnd, el, ev);
}
}
});
dragListener.mousedown(ev); // start listening, which will eventually lead to a dragStart
},
// Renders a visual indication of an event being resized.
// `range` has the updated dates of the event. `seg` is the original segment object involved in the drag.
renderEventResize: function(range, seg) {
// subclasses must implement
},
// Unrenders a visual indication of an event being resized.
destroyEventResize: function() {
// subclasses must implement
},
/* Rendering Utils
------------------------------------------------------------------------------------------------------------------*/
// Compute the text that should be displayed on an event's element.
// `range` can be the Event object itself, or something range-like, with at least a `start`.
// The `timeFormat` options and the grid's default format is used, but `formatStr` can override.
getEventTimeText: function(range, formatStr) {
formatStr = formatStr || this.eventTimeFormat;
if (range.end && this.displayEventEnd) {
return this.view.formatRange(range, formatStr);
}
else {
return range.start.format(formatStr);
}
},
// Generic utility for generating the HTML classNames for an event segment's element
getSegClasses: function(seg, isDraggable, isResizable) {
var event = seg.event;
var classes = [
'fc-event',
seg.isStart ? 'fc-start' : 'fc-not-start',
seg.isEnd ? 'fc-end' : 'fc-not-end'
].concat(
event.className,
event.source ? event.source.className : []
);
if (isDraggable) {
classes.push('fc-draggable');
}
if (isResizable) {
classes.push('fc-resizable');
}
return classes;
},
// Utility for generating a CSS string with all the event skin-related properties
getEventSkinCss: function(event) {
var view = this.view;
var source = event.source || {};
var eventColor = event.color;
var sourceColor = source.color;
var optionColor = view.opt('eventColor');
var backgroundColor =
event.backgroundColor ||
eventColor ||
source.backgroundColor ||
sourceColor ||
view.opt('eventBackgroundColor') ||
optionColor;
var borderColor =
event.borderColor ||
eventColor ||
source.borderColor ||
sourceColor ||
view.opt('eventBorderColor') ||
optionColor;
var textColor =
event.textColor ||
source.textColor ||
view.opt('eventTextColor');
var statements = [];
if (backgroundColor) {
statements.push('background-color:' + backgroundColor);
}
if (borderColor) {
statements.push('border-color:' + borderColor);
}
if (textColor) {
statements.push('color:' + textColor);
}
return statements.join(';');
},
/* Converting events -> ranges -> segs
------------------------------------------------------------------------------------------------------------------*/
// Converts an array of event objects into an array of event segment objects.
// A custom `rangeToSegsFunc` may be given for arbitrarily slicing up events.
eventsToSegs: function(events, rangeToSegsFunc) {
var eventRanges = this.eventsToRanges(events);
var segs = [];
var i;
for (i = 0; i < eventRanges.length; i++) {
segs.push.apply(
segs,
this.eventRangeToSegs(eventRanges[i], rangeToSegsFunc)
);
}
return segs;
},
// Converts an array of events into an array of "range" objects.
// A "range" object is a plain object with start/end properties denoting the time it covers. Also an event property.
// For "normal" events, this will be identical to the event's start/end, but for "inverse-background" events,
// will create an array of ranges that span the time *not* covered by the given event.
eventsToRanges: function(events) {
var _this = this;
var eventsById = groupEventsById(events);
var ranges = [];
// group by ID so that related inverse-background events can be rendered together
$.each(eventsById, function(id, eventGroup) {
if (eventGroup.length) {
ranges.push.apply(
ranges,
isInverseBgEvent(eventGroup[0]) ?
_this.eventsToInverseRanges(eventGroup) :
_this.eventsToNormalRanges(eventGroup)
);
}
});
return ranges;
},
// Converts an array of "normal" events (not inverted rendering) into a parallel array of ranges
eventsToNormalRanges: function(events) {
var calendar = this.view.calendar;
var ranges = [];
var i, event;
var eventStart, eventEnd;
for (i = 0; i < events.length; i++) {
event = events[i];
// make copies and normalize by stripping timezone
eventStart = event.start.clone().stripZone();
eventEnd = calendar.getEventEnd(event).stripZone();
ranges.push({
event: event,
start: eventStart,
end: eventEnd,
eventStartMS: +eventStart,
eventDurationMS: eventEnd - eventStart
});
}
return ranges;
},
// Converts an array of events, with inverse-background rendering, into an array of range objects.
// The range objects will cover all the time NOT covered by the events.
eventsToInverseRanges: function(events) {
var view = this.view;
var viewStart = view.start.clone().stripZone(); // normalize timezone
var viewEnd = view.end.clone().stripZone(); // normalize timezone
var normalRanges = this.eventsToNormalRanges(events); // will give us normalized dates we can use w/o copies
var inverseRanges = [];
var event0 = events[0]; // assign this to each range's `.event`
var start = viewStart; // the end of the previous range. the start of the new range
var i, normalRange;
// ranges need to be in order. required for our date-walking algorithm
normalRanges.sort(compareNormalRanges);
for (i = 0; i < normalRanges.length; i++) {
normalRange = normalRanges[i];
// add the span of time before the event (if there is any)
if (normalRange.start > start) { // compare millisecond time (skip any ambig logic)
inverseRanges.push({
event: event0,
start: start,
end: normalRange.start
});
}
start = normalRange.end;
}
// add the span of time after the last event (if there is any)
if (start < viewEnd) { // compare millisecond time (skip any ambig logic)
inverseRanges.push({
event: event0,
start: start,
end: viewEnd
});
}
return inverseRanges;
},
// Slices the given event range into one or more segment objects.
// A `rangeToSegsFunc` custom slicing function can be given.
eventRangeToSegs: function(eventRange, rangeToSegsFunc) {
var segs;
var i, seg;
if (rangeToSegsFunc) {
segs = rangeToSegsFunc(eventRange);
}
else {
segs = this.rangeToSegs(eventRange); // defined by the subclass
}
for (i = 0; i < segs.length; i++) {
seg = segs[i];
seg.event = eventRange.event;
seg.eventStartMS = eventRange.eventStartMS;
seg.eventDurationMS = eventRange.eventDurationMS;
}
return segs;
}
});
/* Utilities
----------------------------------------------------------------------------------------------------------------------*/
function isBgEvent(event) { // returns true if background OR inverse-background
var rendering = getEventRendering(event);
return rendering === 'background' || rendering === 'inverse-background';
}
function isInverseBgEvent(event) {
return getEventRendering(event) === 'inverse-background';
}
function getEventRendering(event) {
return firstDefined((event.source || {}).rendering, event.rendering);
}
function groupEventsById(events) {
var eventsById = {};
var i, event;
for (i = 0; i < events.length; i++) {
event = events[i];
(eventsById[event._id] || (eventsById[event._id] = [])).push(event);
}
return eventsById;
}
// A cmp function for determining which non-inverted "ranges" (see above) happen earlier
function compareNormalRanges(range1, range2) {
return range1.eventStartMS - range2.eventStartMS; // earlier ranges go first
}
// A cmp function for determining which segments should take visual priority
// DOES NOT WORK ON INVERTED BACKGROUND EVENTS because they have no eventStartMS/eventDurationMS
function compareSegs(seg1, seg2) {
return seg1.eventStartMS - seg2.eventStartMS || // earlier events go first
seg2.eventDurationMS - seg1.eventDurationMS || // tie? longer events go first
seg2.event.allDay - seg1.event.allDay || // tie? put all-day events first (booleans cast to 0/1)
(seg1.event.title || '').localeCompare(seg2.event.title); // tie? alphabetically by title
}
fc.compareSegs = compareSegs; // export
/* External-Dragging-Element Data
----------------------------------------------------------------------------------------------------------------------*/
// Require all HTML5 data-* attributes used by FullCalendar to have this prefix.
// A value of '' will query attributes like data-event. A value of 'fc' will query attributes like data-fc-event.
fc.dataAttrPrefix = '';
// Given a jQuery element that might represent a dragged FullCalendar event, returns an intermediate data structure
// to be used for Event Object creation.
// A defined `.eventProps`, even when empty, indicates that an event should be created.
function getDraggedElMeta(el) {
var prefix = fc.dataAttrPrefix;
var eventProps; // properties for creating the event, not related to date/time
var startTime; // a Duration
var duration;
var stick;
if (prefix) { prefix += '-'; }
eventProps = el.data(prefix + 'event') || null;
if (eventProps) {
if (typeof eventProps === 'object') {
eventProps = $.extend({}, eventProps); // make a copy
}
else { // something like 1 or true. still signal event creation
eventProps = {};
}
// pluck special-cased date/time properties
startTime = eventProps.start;
if (startTime == null) { startTime = eventProps.time; } // accept 'time' as well
duration = eventProps.duration;
stick = eventProps.stick;
delete eventProps.start;
delete eventProps.time;
delete eventProps.duration;
delete eventProps.stick;
}
// fallback to standalone attribute values for each of the date/time properties
if (startTime == null) { startTime = el.data(prefix + 'start'); }
if (startTime == null) { startTime = el.data(prefix + 'time'); } // accept 'time' as well
if (duration == null) { duration = el.data(prefix + 'duration'); }
if (stick == null) { stick = el.data(prefix + 'stick'); }
// massage into correct data types
startTime = startTime != null ? moment.duration(startTime) : null;
duration = duration != null ? moment.duration(duration) : null;
stick = Boolean(stick);
return { eventProps: eventProps, startTime: startTime, duration: duration, stick: stick };
}