forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventManager.js
1085 lines (871 loc) · 27.3 KB
/
EventManager.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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
fc.sourceNormalizers = [];
fc.sourceFetchers = [];
var ajaxDefaults = {
dataType: 'json',
cache: false
};
var eventGUID = 1;
function EventManager(options) { // assumed to be a calendar
var t = this;
// exports
t.isFetchNeeded = isFetchNeeded;
t.fetchEvents = fetchEvents;
t.addEventSource = addEventSource;
t.removeEventSource = removeEventSource;
t.updateEvent = updateEvent;
t.renderEvent = renderEvent;
t.removeEvents = removeEvents;
t.clientEvents = clientEvents;
t.mutateEvent = mutateEvent;
t.normalizeEventDateProps = normalizeEventDateProps;
t.ensureVisibleEventRange = ensureVisibleEventRange;
// imports
var trigger = t.trigger;
var getView = t.getView;
var reportEvents = t.reportEvents;
// locals
var stickySource = { events: [] };
var sources = [ stickySource ];
var rangeStart, rangeEnd;
var currentFetchID = 0;
var pendingSourceCnt = 0;
var loadingLevel = 0;
var cache = []; // holds events that have already been expanded
$.each(
(options.events ? [ options.events ] : []).concat(options.eventSources || []),
function(i, sourceInput) {
var source = buildEventSource(sourceInput);
if (source) {
sources.push(source);
}
}
);
/* Fetching
-----------------------------------------------------------------------------*/
function isFetchNeeded(start, end) {
return !rangeStart || // nothing has been fetched yet?
// or, a part of the new range is outside of the old range? (after normalizing)
start.clone().stripZone() < rangeStart.clone().stripZone() ||
end.clone().stripZone() > rangeEnd.clone().stripZone();
}
function fetchEvents(start, end) {
rangeStart = start;
rangeEnd = end;
cache = [];
var fetchID = ++currentFetchID;
var len = sources.length;
pendingSourceCnt = len;
for (var i=0; i<len; i++) {
fetchEventSource(sources[i], fetchID);
}
}
function fetchEventSource(source, fetchID) {
_fetchEventSource(source, function(eventInputs) {
var isArraySource = $.isArray(source.events);
var i, eventInput;
var abstractEvent;
if (fetchID == currentFetchID) {
if (eventInputs) {
for (i = 0; i < eventInputs.length; i++) {
eventInput = eventInputs[i];
if (isArraySource) { // array sources have already been convert to Event Objects
abstractEvent = eventInput;
}
else {
abstractEvent = buildEventFromInput(eventInput, source);
}
if (abstractEvent) { // not false (an invalid event)
cache.push.apply(
cache,
expandEvent(abstractEvent) // add individual expanded events to the cache
);
}
}
}
pendingSourceCnt--;
if (!pendingSourceCnt) {
reportEvents(cache);
}
}
});
}
function _fetchEventSource(source, callback) {
var i;
var fetchers = fc.sourceFetchers;
var res;
for (i=0; i<fetchers.length; i++) {
res = fetchers[i].call(
t, // this, the Calendar object
source,
rangeStart.clone(),
rangeEnd.clone(),
options.timezone,
callback
);
if (res === true) {
// the fetcher is in charge. made its own async request
return;
}
else if (typeof res == 'object') {
// the fetcher returned a new source. process it
_fetchEventSource(res, callback);
return;
}
}
var events = source.events;
if (events) {
if ($.isFunction(events)) {
pushLoading();
events.call(
t, // this, the Calendar object
rangeStart.clone(),
rangeEnd.clone(),
options.timezone,
function(events) {
callback(events);
popLoading();
}
);
}
else if ($.isArray(events)) {
callback(events);
}
else {
callback();
}
}else{
var url = source.url;
if (url) {
var success = source.success;
var error = source.error;
var complete = source.complete;
// retrieve any outbound GET/POST $.ajax data from the options
var customData;
if ($.isFunction(source.data)) {
// supplied as a function that returns a key/value object
customData = source.data();
}
else {
// supplied as a straight key/value object
customData = source.data;
}
// use a copy of the custom data so we can modify the parameters
// and not affect the passed-in object.
var data = $.extend({}, customData || {});
var startParam = firstDefined(source.startParam, options.startParam);
var endParam = firstDefined(source.endParam, options.endParam);
var timezoneParam = firstDefined(source.timezoneParam, options.timezoneParam);
if (startParam) {
data[startParam] = rangeStart.format();
}
if (endParam) {
data[endParam] = rangeEnd.format();
}
if (options.timezone && options.timezone != 'local') {
data[timezoneParam] = options.timezone;
}
pushLoading();
$.ajax($.extend({}, ajaxDefaults, source, {
data: data,
success: function(events) {
events = events || [];
var res = applyAll(success, this, arguments);
if ($.isArray(res)) {
events = res;
}
callback(events);
},
error: function() {
applyAll(error, this, arguments);
callback();
},
complete: function() {
applyAll(complete, this, arguments);
popLoading();
}
}));
}else{
callback();
}
}
}
/* Sources
-----------------------------------------------------------------------------*/
function addEventSource(sourceInput) {
var source = buildEventSource(sourceInput);
if (source) {
sources.push(source);
pendingSourceCnt++;
fetchEventSource(source, currentFetchID); // will eventually call reportEvents
}
}
function buildEventSource(sourceInput) { // will return undefined if invalid source
var normalizers = fc.sourceNormalizers;
var source;
var i;
if ($.isFunction(sourceInput) || $.isArray(sourceInput)) {
source = { events: sourceInput };
}
else if (typeof sourceInput === 'string') {
source = { url: sourceInput };
}
else if (typeof sourceInput === 'object') {
source = $.extend({}, sourceInput); // shallow copy
}
if (source) {
// TODO: repeat code, same code for event classNames
if (source.className) {
if (typeof source.className === 'string') {
source.className = source.className.split(/\s+/);
}
// otherwise, assumed to be an array
}
else {
source.className = [];
}
// for array sources, we convert to standard Event Objects up front
if ($.isArray(source.events)) {
source.origArray = source.events; // for removeEventSource
source.events = $.map(source.events, function(eventInput) {
return buildEventFromInput(eventInput, source);
});
}
for (i=0; i<normalizers.length; i++) {
normalizers[i].call(t, source);
}
return source;
}
}
function removeEventSource(source) {
sources = $.grep(sources, function(src) {
return !isSourcesEqual(src, source);
});
// remove all client events from that source
cache = $.grep(cache, function(e) {
return !isSourcesEqual(e.source, source);
});
reportEvents(cache);
}
function isSourcesEqual(source1, source2) {
return source1 && source2 && getSourcePrimitive(source1) == getSourcePrimitive(source2);
}
function getSourcePrimitive(source) {
return (
(typeof source === 'object') ? // a normalized event source?
(source.origArray || source.googleCalendarId || source.url || source.events) : // get the primitive
null
) ||
source; // the given argument *is* the primitive
}
/* Manipulation
-----------------------------------------------------------------------------*/
// Only ever called from the externally-facing API
function updateEvent(event) {
// massage start/end values, even if date string values
event.start = t.moment(event.start);
if (event.end) {
event.end = t.moment(event.end);
}
else {
event.end = null;
}
mutateEvent(event, getMiscEventProps(event)); // will handle start/end/allDay normalization
reportEvents(cache); // reports event modifications (so we can redraw)
}
// Returns a hash of misc event properties that should be copied over to related events.
function getMiscEventProps(event) {
var props = {};
$.each(event, function(name, val) {
if (isMiscEventPropName(name)) {
if (val !== undefined && isAtomic(val)) { // a defined non-object
props[name] = val;
}
}
});
return props;
}
// non-date-related, non-id-related, non-secret
function isMiscEventPropName(name) {
return !/^_|^(id|allDay|start|end)$/.test(name);
}
// returns the expanded events that were created
function renderEvent(eventInput, stick) {
var abstractEvent = buildEventFromInput(eventInput);
var events;
var i, event;
if (abstractEvent) { // not false (a valid input)
events = expandEvent(abstractEvent);
for (i = 0; i < events.length; i++) {
event = events[i];
if (!event.source) {
if (stick) {
stickySource.events.push(event);
event.source = stickySource;
}
cache.push(event);
}
}
reportEvents(cache);
return events;
}
return [];
}
function removeEvents(filter) {
var eventID;
var i;
if (filter == null) { // null or undefined. remove all events
filter = function() { return true; }; // will always match
}
else if (!$.isFunction(filter)) { // an event ID
eventID = filter + '';
filter = function(event) {
return event._id == eventID;
};
}
// Purge event(s) from our local cache
cache = $.grep(cache, filter, true); // inverse=true
// Remove events from array sources.
// This works because they have been converted to official Event Objects up front.
// (and as a result, event._id has been calculated).
for (i=0; i<sources.length; i++) {
if ($.isArray(sources[i].events)) {
sources[i].events = $.grep(sources[i].events, filter, true);
}
}
reportEvents(cache);
}
function clientEvents(filter) {
if ($.isFunction(filter)) {
return $.grep(cache, filter);
}
else if (filter != null) { // not null, not undefined. an event ID
filter += '';
return $.grep(cache, function(e) {
return e._id == filter;
});
}
return cache; // else, return all
}
/* Loading State
-----------------------------------------------------------------------------*/
function pushLoading() {
if (!(loadingLevel++)) {
trigger('loading', null, true, getView());
}
}
function popLoading() {
if (!(--loadingLevel)) {
trigger('loading', null, false, getView());
}
}
/* Event Normalization
-----------------------------------------------------------------------------*/
// Given a raw object with key/value properties, returns an "abstract" Event object.
// An "abstract" event is an event that, if recurring, will not have been expanded yet.
// Will return `false` when input is invalid.
// `source` is optional
function buildEventFromInput(input, source) {
var out = {};
var start, end;
var allDay;
if (options.eventDataTransform) {
input = options.eventDataTransform(input);
}
if (source && source.eventDataTransform) {
input = source.eventDataTransform(input);
}
// Copy all properties over to the resulting object.
// The special-case properties will be copied over afterwards.
$.extend(out, input);
if (source) {
out.source = source;
}
out._id = input._id || (input.id === undefined ? '_fc' + eventGUID++ : input.id + '');
if (input.className) {
if (typeof input.className == 'string') {
out.className = input.className.split(/\s+/);
}
else { // assumed to be an array
out.className = input.className;
}
}
else {
out.className = [];
}
start = input.start || input.date; // "date" is an alias for "start"
end = input.end;
// parse as a time (Duration) if applicable
if (isTimeString(start)) {
start = moment.duration(start);
}
if (isTimeString(end)) {
end = moment.duration(end);
}
if (input.dow || moment.isDuration(start) || moment.isDuration(end)) {
// the event is "abstract" (recurring) so don't calculate exact start/end dates just yet
out.start = start ? moment.duration(start) : null; // will be a Duration or null
out.end = end ? moment.duration(end) : null; // will be a Duration or null
out._recurring = true; // our internal marker
}
else {
if (start) {
start = t.moment(start);
if (!start.isValid()) {
return false;
}
}
if (end) {
end = t.moment(end);
if (!end.isValid()) {
end = null; // let defaults take over
}
}
allDay = input.allDay;
if (allDay === undefined) { // still undefined? fallback to default
allDay = firstDefined(
source ? source.allDayDefault : undefined,
options.allDayDefault
);
// still undefined? normalizeEventDateProps will calculate it
}
assignDatesToEvent(start, end, allDay, out);
}
return out;
}
// Normalizes and assigns the given dates to the given partially-formed event object.
// NOTE: mutates the given start/end moments. does not make a copy.
function assignDatesToEvent(start, end, allDay, event) {
event.start = start;
event.end = end;
event.allDay = allDay;
normalizeEventDateProps(event);
backupEventDates(event);
}
// Ensures the allDay property exists.
// Ensures the start/end dates are consistent with allDay and forceEventDuration.
// Accepts an Event object, or a plain object with event-ish properties.
// NOTE: Will modify the given object.
function normalizeEventDateProps(props) {
if (props.allDay == null) {
props.allDay = !(props.start.hasTime() || (props.end && props.end.hasTime()));
}
if (props.allDay) {
props.start.stripTime();
if (props.end) {
props.end.stripTime();
}
}
else {
if (!props.start.hasTime()) {
props.start = t.rezoneDate(props.start); // will also give it a 00:00 time
}
if (props.end && !props.end.hasTime()) {
props.end = t.rezoneDate(props.end); // will also give it a 00:00 time
}
}
if (props.end && !props.end.isAfter(props.start)) {
props.end = null;
}
if (!props.end) {
if (options.forceEventDuration) {
props.end = t.getDefaultEventEnd(props.allDay, props.start);
}
else {
props.end = null;
}
}
}
// If `range` is a proper range with a start and end, returns the original object.
// If missing an end, computes a new range with an end, computing it as if it were an event.
// TODO: make this a part of the event -> eventRange system
function ensureVisibleEventRange(range) {
var allDay;
if (!range.end) {
allDay = range.allDay; // range might be more event-ish than we think
if (allDay == null) {
allDay = !range.start.hasTime();
}
range = {
start: range.start,
end: t.getDefaultEventEnd(allDay, range.start)
};
}
return range;
}
// If the given event is a recurring event, break it down into an array of individual instances.
// If not a recurring event, return an array with the single original event.
// If given a falsy input (probably because of a failed buildEventFromInput call), returns an empty array.
// HACK: can override the recurring window by providing custom rangeStart/rangeEnd (for businessHours).
function expandEvent(abstractEvent, _rangeStart, _rangeEnd) {
var events = [];
var dowHash;
var dow;
var i;
var date;
var startTime, endTime;
var start, end;
var event;
_rangeStart = _rangeStart || rangeStart;
_rangeEnd = _rangeEnd || rangeEnd;
if (abstractEvent) {
if (abstractEvent._recurring) {
// make a boolean hash as to whether the event occurs on each day-of-week
if ((dow = abstractEvent.dow)) {
dowHash = {};
for (i = 0; i < dow.length; i++) {
dowHash[dow[i]] = true;
}
}
// iterate through every day in the current range
date = _rangeStart.clone().stripTime(); // holds the date of the current day
while (date.isBefore(_rangeEnd)) {
if (!dowHash || dowHash[date.day()]) { // if everyday, or this particular day-of-week
startTime = abstractEvent.start; // the stored start and end properties are times (Durations)
endTime = abstractEvent.end; // "
start = date.clone();
end = null;
if (startTime) {
start = start.time(startTime);
}
if (endTime) {
end = date.clone().time(endTime);
}
event = $.extend({}, abstractEvent); // make a copy of the original
assignDatesToEvent(
start, end,
!startTime && !endTime, // allDay?
event
);
events.push(event);
}
date.add(1, 'days');
}
}
else {
events.push(abstractEvent); // return the original event. will be a one-item array
}
}
return events;
}
/* Event Modification Math
-----------------------------------------------------------------------------------------*/
// Modifies an event and all related events by applying the given properties.
// Special date-diffing logic is used for manipulation of dates.
// If `props` does not contain start/end dates, the updated values are assumed to be the event's current start/end.
// All date comparisons are done against the event's pristine _start and _end dates.
// Returns an object with delta information and a function to undo all operations.
//
function mutateEvent(event, props) {
var miscProps = {};
var clearEnd;
var dateDelta;
var durationDelta;
var undoFunc;
props = props || {};
// ensure new date-related values to compare against
if (!props.start) {
props.start = event.start.clone();
}
if (props.end === undefined) {
props.end = event.end ? event.end.clone() : null;
}
if (props.allDay == null) { // is null or undefined?
props.allDay = event.allDay;
}
normalizeEventDateProps(props); // massages start/end/allDay
// clear the end date if explicitly changed to null
clearEnd = event._end !== null && props.end === null;
// compute the delta for moving the start and end dates together
if (props.allDay) {
dateDelta = diffDay(props.start, event._start); // whole-day diff from start-of-day
}
else {
dateDelta = diffDayTime(props.start, event._start);
}
// compute the delta for moving the end date (after applying dateDelta)
if (!clearEnd && props.end) {
durationDelta = diffDayTime(
// new duration
props.end,
props.start
).subtract(diffDayTime(
// subtract old duration
event._end || t.getDefaultEventEnd(event._allDay, event._start),
event._start
));
}
// gather all non-date-related properties
$.each(props, function(name, val) {
if (isMiscEventPropName(name)) {
if (val !== undefined) {
miscProps[name] = val;
}
}
});
// apply the operations to the event and all related events
undoFunc = mutateEvents(
clientEvents(event._id), // get events with this ID
clearEnd,
props.allDay,
dateDelta,
durationDelta,
miscProps
);
return {
dateDelta: dateDelta,
durationDelta: durationDelta,
undo: undoFunc
};
}
// Modifies an array of events in the following ways (operations are in order):
// - clear the event's `end`
// - convert the event to allDay
// - add `dateDelta` to the start and end
// - add `durationDelta` to the event's duration
// - assign `miscProps` to the event
//
// Returns a function that can be called to undo all the operations.
//
// TODO: don't use so many closures. possible memory issues when lots of events with same ID.
//
function mutateEvents(events, clearEnd, allDay, dateDelta, durationDelta, miscProps) {
var isAmbigTimezone = t.getIsAmbigTimezone();
var undoFunctions = [];
// normalize zero-length deltas to be null
if (dateDelta && !dateDelta.valueOf()) { dateDelta = null; }
if (durationDelta && !durationDelta.valueOf()) { durationDelta = null; }
$.each(events, function(i, event) {
var oldProps;
var newProps;
// build an object holding all the old values, both date-related and misc.
// for the undo function.
oldProps = {
start: event.start.clone(),
end: event.end ? event.end.clone() : null,
allDay: event.allDay
};
$.each(miscProps, function(name) {
oldProps[name] = event[name];
});
// new date-related properties. work off the original date snapshot.
// ok to use references because they will be thrown away when backupEventDates is called.
newProps = {
start: event._start,
end: event._end,
allDay: event._allDay
};
if (clearEnd) {
newProps.end = null;
}
newProps.allDay = allDay;
normalizeEventDateProps(newProps); // massages start/end/allDay
if (dateDelta) {
newProps.start.add(dateDelta);
if (newProps.end) {
newProps.end.add(dateDelta);
}
}
if (durationDelta) {
if (!newProps.end) {
newProps.end = t.getDefaultEventEnd(newProps.allDay, newProps.start);
}
newProps.end.add(durationDelta);
}
// if the dates have changed, and we know it is impossible to recompute the
// timezone offsets, strip the zone.
if (
isAmbigTimezone &&
!newProps.allDay &&
(dateDelta || durationDelta)
) {
newProps.start.stripZone();
if (newProps.end) {
newProps.end.stripZone();
}
}
$.extend(event, miscProps, newProps); // copy over misc props, then date-related props
backupEventDates(event); // regenerate internal _start/_end/_allDay
undoFunctions.push(function() {
$.extend(event, oldProps);
backupEventDates(event); // regenerate internal _start/_end/_allDay
});
});
return function() {
for (var i = 0; i < undoFunctions.length; i++) {
undoFunctions[i]();
}
};
}
/* Business Hours
-----------------------------------------------------------------------------------------*/
t.getBusinessHoursEvents = getBusinessHoursEvents;
// Returns an array of events as to when the business hours occur in the given view.
// Abuse of our event system :(
function getBusinessHoursEvents() {
var optionVal = options.businessHours;
var defaultVal = {
className: 'fc-nonbusiness',
start: '09:00',
end: '17:00',
dow: [ 1, 2, 3, 4, 5 ], // monday - friday
rendering: 'inverse-background'
};
var view = t.getView();
var eventInput;
if (optionVal) {
if (typeof optionVal === 'object') {
// option value is an object that can override the default business hours
eventInput = $.extend({}, defaultVal, optionVal);
}
else {
// option value is `true`. use default business hours
eventInput = defaultVal;
}
}
if (eventInput) {
return expandEvent(
buildEventFromInput(eventInput),
view.start,
view.end
);
}
return [];
}
/* Overlapping / Constraining
-----------------------------------------------------------------------------------------*/
t.isEventRangeAllowed = isEventRangeAllowed;
t.isSelectionRangeAllowed = isSelectionRangeAllowed;
t.isExternalDropRangeAllowed = isExternalDropRangeAllowed;
function isEventRangeAllowed(range, event) {
var source = event.source || {};
var constraint = firstDefined(
event.constraint,
source.constraint,
options.eventConstraint
);
var overlap = firstDefined(
event.overlap,
source.overlap,
options.eventOverlap
);
range = ensureVisibleEventRange(range); // ensure a proper range with an end for isRangeAllowed
return isRangeAllowed(range, constraint, overlap, event);
}
function isSelectionRangeAllowed(range) {
return isRangeAllowed(range, options.selectConstraint, options.selectOverlap);
}
// when `eventProps` is defined, consider this an event.
// `eventProps` can contain misc non-date-related info about the event.
function isExternalDropRangeAllowed(range, eventProps) {
var eventInput;
var event;
// note: very similar logic is in View's reportExternalDrop
if (eventProps) {
eventInput = $.extend({}, eventProps, range);
event = expandEvent(buildEventFromInput(eventInput))[0];
}
if (event) {
return isEventRangeAllowed(range, event);
}
else { // treat it as a selection
range = ensureVisibleEventRange(range); // ensure a proper range with an end for isSelectionRangeAllowed
return isSelectionRangeAllowed(range);
}
}
// Returns true if the given range (caused by an event drop/resize or a selection) is allowed to exist
// according to the constraint/overlap settings.
// `event` is not required if checking a selection.
function isRangeAllowed(range, constraint, overlap, event) {
var constraintEvents;
var anyContainment;
var i, otherEvent;
var otherOverlap;
// normalize. fyi, we're normalizing in too many places :(
range = {
start: range.start.clone().stripZone(),
end: range.end.clone().stripZone()
};
// the range must be fully contained by at least one of produced constraint events
if (constraint != null) {
// not treated as an event! intermediate data structure
// TODO: use ranges in the future
constraintEvents = constraintToEvents(constraint);
anyContainment = false;
for (i = 0; i < constraintEvents.length; i++) {
if (eventContainsRange(constraintEvents[i], range)) {
anyContainment = true;
break;
}
}
if (!anyContainment) {
return false;
}
}
for (i = 0; i < cache.length; i++) { // loop all events and detect overlap
otherEvent = cache[i];