| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286 |
- FC.sourceNormalizers = [];
- FC.sourceFetchers = [];
- var ajaxDefaults = {
- dataType: 'json',
- cache: false
- };
- var eventGUID = 1;
- function EventManager() { // assumed to be a calendar
- var t = this;
-
-
- // exports
- t.isFetchNeeded = isFetchNeeded;
- t.fetchEvents = fetchEvents;
- t.fetchEventSources = fetchEventSources;
- t.getEventSources = getEventSources;
- t.getEventSourceById = getEventSourceById;
- t.getEventSourcesByMatchArray = getEventSourcesByMatchArray;
- t.getEventSourcesByMatch = getEventSourcesByMatch;
- t.addEventSource = addEventSource;
- t.removeEventSource = removeEventSource;
- t.removeEventSources = removeEventSources;
- t.updateEvent = updateEvent;
- t.renderEvent = renderEvent;
- t.removeEvents = removeEvents;
- t.clientEvents = clientEvents;
- t.mutateEvent = mutateEvent;
- t.normalizeEventDates = normalizeEventDates;
- t.normalizeEventTimes = normalizeEventTimes;
-
-
- // imports
- var reportEvents = t.reportEvents;
-
-
- // locals
- var stickySource = { events: [] };
- var sources = [ stickySource ];
- var rangeStart, rangeEnd;
- var pendingSourceCnt = 0; // outstanding fetch requests, max one per source
- var cache = []; // holds events that have already been expanded
- $.each(
- (t.options.events ? [ t.options.events ] : []).concat(t.options.eventSources || []),
- function(i, sourceInput) {
- var source = buildEventSource(sourceInput);
- if (source) {
- sources.push(source);
- }
- }
- );
-
-
-
- /* Fetching
- -----------------------------------------------------------------------------*/
- // start and end are assumed to be unzoned
- function isFetchNeeded(start, end) {
- return !rangeStart || // nothing has been fetched yet?
- start < rangeStart || end > rangeEnd; // is part of the new range outside of the old range?
- }
-
-
- function fetchEvents(start, end) {
- rangeStart = start;
- rangeEnd = end;
- fetchEventSources(sources, 'reset');
- }
- // expects an array of event source objects (the originals, not copies)
- // `specialFetchType` is an optimization parameter that affects purging of the event cache.
- function fetchEventSources(specificSources, specialFetchType) {
- var i, source;
- if (specialFetchType === 'reset') {
- cache = [];
- }
- else if (specialFetchType !== 'add') {
- cache = excludeEventsBySources(cache, specificSources);
- }
- for (i = 0; i < specificSources.length; i++) {
- source = specificSources[i];
- // already-pending sources have already been accounted for in pendingSourceCnt
- if (source._status !== 'pending') {
- pendingSourceCnt++;
- }
- source._fetchId = (source._fetchId || 0) + 1;
- source._status = 'pending';
- }
- for (i = 0; i < specificSources.length; i++) {
- source = specificSources[i];
- tryFetchEventSource(source, source._fetchId);
- }
- }
- // fetches an event source and processes its result ONLY if it is still the current fetch.
- // caller is responsible for incrementing pendingSourceCnt first.
- function tryFetchEventSource(source, fetchId) {
- _fetchEventSource(source, function(eventInputs) {
- var isArraySource = $.isArray(source.events);
- var i, eventInput;
- var abstractEvent;
- if (
- // is this the source's most recent fetch?
- // if not, rely on an upcoming fetch of this source to decrement pendingSourceCnt
- fetchId === source._fetchId &&
- // event source no longer valid?
- source._status !== 'rejected'
- ) {
- source._status = 'resolved';
- 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
- );
- }
- }
- }
- decrementPendingSourceCnt();
- }
- });
- }
- function rejectEventSource(source) {
- var wasPending = source._status === 'pending';
- source._status = 'rejected';
- if (wasPending) {
- decrementPendingSourceCnt();
- }
- }
- function decrementPendingSourceCnt() {
- 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(),
- t.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)) {
- t.pushLoading();
- events.call(
- t, // this, the Calendar object
- rangeStart.clone(),
- rangeEnd.clone(),
- t.options.timezone,
- function(events) {
- callback(events);
- t.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, t.options.startParam);
- var endParam = firstDefined(source.endParam, t.options.endParam);
- var timezoneParam = firstDefined(source.timezoneParam, t.options.timezoneParam);
- if (startParam) {
- data[startParam] = rangeStart.format();
- }
- if (endParam) {
- data[endParam] = rangeEnd.format();
- }
- if (t.options.timezone && t.options.timezone != 'local') {
- data[timezoneParam] = t.options.timezone;
- }
- t.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);
- t.popLoading();
- }
- }));
- }else{
- callback();
- }
- }
- }
-
-
-
- /* Sources
- -----------------------------------------------------------------------------*/
- function addEventSource(sourceInput) {
- var source = buildEventSource(sourceInput);
- if (source) {
- sources.push(source);
- fetchEventSources([ source ], 'add'); // 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(matchInput) {
- removeSpecificEventSources(
- getEventSourcesByMatch(matchInput)
- );
- }
- // if called with no arguments, removes all.
- function removeEventSources(matchInputs) {
- if (matchInputs == null) {
- removeSpecificEventSources(sources, true); // isAll=true
- }
- else {
- removeSpecificEventSources(
- getEventSourcesByMatchArray(matchInputs)
- );
- }
- }
- function removeSpecificEventSources(targetSources, isAll) {
- var i;
- // cancel pending requests
- for (i = 0; i < targetSources.length; i++) {
- rejectEventSource(targetSources[i]);
- }
- if (isAll) { // an optimization
- sources = [];
- cache = [];
- }
- else {
- // remove from persisted source list
- sources = $.grep(sources, function(source) {
- for (i = 0; i < targetSources.length; i++) {
- if (source === targetSources[i]) {
- return false; // exclude
- }
- }
- return true; // include
- });
- cache = excludeEventsBySources(cache, targetSources);
- }
- reportEvents(cache);
- }
- function getEventSources() {
- return sources.slice(1); // returns a shallow copy of sources with stickySource removed
- }
- function getEventSourceById(id) {
- return $.grep(sources, function(source) {
- return source.id && source.id === id;
- })[0];
- }
- // like getEventSourcesByMatch, but accepts multple match criteria (like multiple IDs)
- function getEventSourcesByMatchArray(matchInputs) {
- // coerce into an array
- if (!matchInputs) {
- matchInputs = [];
- }
- else if (!$.isArray(matchInputs)) {
- matchInputs = [ matchInputs ];
- }
- var matchingSources = [];
- var i;
- // resolve raw inputs to real event source objects
- for (i = 0; i < matchInputs.length; i++) {
- matchingSources.push.apply( // append
- matchingSources,
- getEventSourcesByMatch(matchInputs[i])
- );
- }
- return matchingSources;
- }
- // matchInput can either by a real event source object, an ID, or the function/URL for the source.
- // returns an array of matching source objects.
- function getEventSourcesByMatch(matchInput) {
- var i, source;
- // given an proper event source object
- for (i = 0; i < sources.length; i++) {
- source = sources[i];
- if (source === matchInput) {
- return [ source ];
- }
- }
- // an ID match
- source = getEventSourceById(matchInput);
- if (source) {
- return [ source ];
- }
- return $.grep(sources, function(source) {
- return isSourcesEquivalent(matchInput, source);
- });
- }
- function isSourcesEquivalent(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
- }
- // util
- // returns a filtered array without events that are part of any of the given sources
- function excludeEventsBySources(specificEvents, specificSources) {
- return $.grep(specificEvents, function(event) {
- for (var i = 0; i < specificSources.length; i++) {
- if (event.source === specificSources[i]) {
- return false; // exclude
- }
- }
- return true; // keep
- });
- }
-
-
-
- /* 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
- }
- // Makes sure all array event sources have their internal event objects
- // converted over to the Calendar's current timezone.
- t.rezoneArrayEventSources = function() {
- var i;
- var events;
- var j;
- for (i = 0; i < sources.length; i++) {
- events = sources[i].events;
- if ($.isArray(events)) {
- for (j = 0; j < events.length; j++) {
- rezoneEventDates(events[j]);
- }
- }
- }
- };
- function rezoneEventDates(event) {
- event.start = t.moment(event.start);
- if (event.end) {
- event.end = t.moment(event.end);
- }
- backupEventDates(event);
- }
-
-
- /* 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 (t.options.eventDataTransform) {
- input = t.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,
- t.options.allDayDefault
- );
- // still undefined? normalizeEventDates will calculate it
- }
- assignDatesToEvent(start, end, allDay, out);
- }
- t.normalizeEvent(out); // hook for external use. a prototype method
- return out;
- }
- t.buildEventFromInput = buildEventFromInput;
- // 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;
- normalizeEventDates(event);
- backupEventDates(event);
- }
- // Ensures proper values for allDay/start/end. Accepts an Event object, or a plain object with event-ish properties.
- // NOTE: Will modify the given object.
- function normalizeEventDates(eventProps) {
- normalizeEventTimes(eventProps);
- if (eventProps.end && !eventProps.end.isAfter(eventProps.start)) {
- eventProps.end = null;
- }
- if (!eventProps.end) {
- if (t.options.forceEventDuration) {
- eventProps.end = t.getDefaultEventEnd(eventProps.allDay, eventProps.start);
- }
- else {
- eventProps.end = null;
- }
- }
- }
- // Ensures the allDay property exists and the timeliness of the start/end dates are consistent
- function normalizeEventTimes(eventProps) {
- if (eventProps.allDay == null) {
- eventProps.allDay = !(eventProps.start.hasTime() || (eventProps.end && eventProps.end.hasTime()));
- }
- if (eventProps.allDay) {
- eventProps.start.stripTime();
- if (eventProps.end) {
- // TODO: consider nextDayThreshold here? If so, will require a lot of testing and adjustment
- eventProps.end.stripTime();
- }
- }
- else {
- if (!eventProps.start.hasTime()) {
- eventProps.start = t.applyTimezone(eventProps.start.time(0)); // will assign a 00:00 time
- }
- if (eventProps.end && !eventProps.end.hasTime()) {
- eventProps.end = t.applyTimezone(eventProps.end.time(0)); // will assign a 00:00 time
- }
- }
- }
- // 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;
- }
- t.expandEvent = expandEvent;
- /* 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.
- // For making computations in a granularity greater than day/time, specify largeUnit.
- // NOTE: The given `newProps` might be mutated for normalization purposes.
- function mutateEvent(event, newProps, largeUnit) {
- var miscProps = {};
- var oldProps;
- var clearEnd;
- var startDelta;
- var endDelta;
- var durationDelta;
- var undoFunc;
- // diffs the dates in the appropriate way, returning a duration
- function diffDates(date1, date0) { // date1 - date0
- if (largeUnit) {
- return diffByUnit(date1, date0, largeUnit);
- }
- else if (newProps.allDay) {
- return diffDay(date1, date0);
- }
- else {
- return diffDayTime(date1, date0);
- }
- }
- newProps = newProps || {};
- // normalize new date-related properties
- if (!newProps.start) {
- newProps.start = event.start.clone();
- }
- if (newProps.end === undefined) {
- newProps.end = event.end ? event.end.clone() : null;
- }
- if (newProps.allDay == null) { // is null or undefined?
- newProps.allDay = event.allDay;
- }
- normalizeEventDates(newProps);
- // create normalized versions of the original props to compare against
- // need a real end value, for diffing
- oldProps = {
- start: event._start.clone(),
- end: event._end ? event._end.clone() : t.getDefaultEventEnd(event._allDay, event._start),
- allDay: newProps.allDay // normalize the dates in the same regard as the new properties
- };
- normalizeEventDates(oldProps);
- // need to clear the end date if explicitly changed to null
- clearEnd = event._end !== null && newProps.end === null;
- // compute the delta for moving the start date
- startDelta = diffDates(newProps.start, oldProps.start);
- // compute the delta for moving the end date
- if (newProps.end) {
- endDelta = diffDates(newProps.end, oldProps.end);
- durationDelta = endDelta.subtract(startDelta);
- }
- else {
- durationDelta = null;
- }
- // gather all non-date-related properties
- $.each(newProps, 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,
- newProps.allDay,
- startDelta,
- durationDelta,
- miscProps
- );
- return {
- dateDelta: startDelta,
- 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: allDay // normalize the dates in the same regard as the new properties
- };
- normalizeEventDates(newProps); // massages start/end/allDay
- // strip or ensure the end date
- if (clearEnd) {
- newProps.end = null;
- }
- else if (durationDelta && !newProps.end) { // the duration translation requires an end date
- newProps.end = t.getDefaultEventEnd(newProps.allDay, newProps.start);
- }
- if (dateDelta) {
- newProps.start.add(dateDelta);
- if (newProps.end) {
- newProps.end.add(dateDelta);
- }
- }
- if (durationDelta) {
- newProps.end.add(durationDelta); // end already ensured above
- }
- // 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]();
- }
- };
- }
- /* Overlapping / Constraining
- -----------------------------------------------------------------------------------------*/
- t.isEventSpanAllowed = isEventSpanAllowed;
- t.isExternalSpanAllowed = isExternalSpanAllowed;
- t.isSelectionSpanAllowed = isSelectionSpanAllowed;
- // Determines if the given event can be relocated to the given span (unzoned start/end with other misc data)
- function isEventSpanAllowed(span, event) {
- var source = event.source || {};
- var constraint = firstDefined(
- event.constraint,
- source.constraint,
- t.options.eventConstraint
- );
- var overlap = firstDefined(
- event.overlap,
- source.overlap,
- t.options.eventOverlap
- );
- return isSpanAllowed(span, constraint, overlap, event);
- }
- // Determines if an external event can be relocated to the given span (unzoned start/end with other misc data)
- function isExternalSpanAllowed(eventSpan, eventLocation, eventProps) {
- var eventInput;
- var event;
- // note: very similar logic is in View's reportExternalDrop
- if (eventProps) {
- eventInput = $.extend({}, eventProps, eventLocation);
- event = expandEvent(buildEventFromInput(eventInput))[0];
- }
- if (event) {
- return isEventSpanAllowed(eventSpan, event);
- }
- else { // treat it as a selection
- return isSelectionSpanAllowed(eventSpan);
- }
- }
- // Determines the given span (unzoned start/end with other misc data) can be selected.
- function isSelectionSpanAllowed(span) {
- return isSpanAllowed(span, t.options.selectConstraint, t.options.selectOverlap);
- }
- // Returns true if the given span (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 isSpanAllowed(span, constraint, overlap, event) {
- var constraintEvents;
- var anyContainment;
- var peerEvents;
- var i, peerEvent;
- var peerOverlap;
- // 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], span)) {
- anyContainment = true;
- break;
- }
- }
- if (!anyContainment) {
- return false;
- }
- }
- peerEvents = t.getPeerEvents(span, event);
- for (i = 0; i < peerEvents.length; i++) {
- peerEvent = peerEvents[i];
- // there needs to be an actual intersection before disallowing anything
- if (eventIntersectsRange(peerEvent, span)) {
- // evaluate overlap for the given range and short-circuit if necessary
- if (overlap === false) {
- return false;
- }
- // if the event's overlap is a test function, pass the peer event in question as the first param
- else if (typeof overlap === 'function' && !overlap(peerEvent, event)) {
- return false;
- }
- // if we are computing if the given range is allowable for an event, consider the other event's
- // EventObject-specific or Source-specific `overlap` property
- if (event) {
- peerOverlap = firstDefined(
- peerEvent.overlap,
- (peerEvent.source || {}).overlap
- // we already considered the global `eventOverlap`
- );
- if (peerOverlap === false) {
- return false;
- }
- // if the peer event's overlap is a test function, pass the subject event as the first param
- if (typeof peerOverlap === 'function' && !peerOverlap(event, peerEvent)) {
- return false;
- }
- }
- }
- }
- return true;
- }
- // Given an event input from the API, produces an array of event objects. Possible event inputs:
- // 'businessHours'
- // An event ID (number or string)
- // An object with specific start/end dates or a recurring event (like what businessHours accepts)
- function constraintToEvents(constraintInput) {
- if (constraintInput === 'businessHours') {
- return t.getCurrentBusinessHourEvents();
- }
- if (typeof constraintInput === 'object') {
- return expandEvent(buildEventFromInput(constraintInput));
- }
- return clientEvents(constraintInput); // probably an ID
- }
- // Does the event's date range fully contain the given range?
- // start/end already assumed to have stripped zones :(
- function eventContainsRange(event, range) {
- var eventStart = event.start.clone().stripZone();
- var eventEnd = t.getEventEnd(event).stripZone();
- return range.start >= eventStart && range.end <= eventEnd;
- }
- // Does the event's date range intersect with the given range?
- // start/end already assumed to have stripped zones :(
- function eventIntersectsRange(event, range) {
- var eventStart = event.start.clone().stripZone();
- var eventEnd = t.getEventEnd(event).stripZone();
- return range.start < eventEnd && range.end > eventStart;
- }
- t.getEventCache = function() {
- return cache;
- };
- }
- // hook for external libs to manipulate event properties upon creation.
- // should manipulate the event in-place.
- Calendar.prototype.normalizeEvent = function(event) {
- };
- // Returns a list of events that the given event should be compared against when being considered for a move to
- // the specified span. Attached to the Calendar's prototype because EventManager is a mixin for a Calendar.
- Calendar.prototype.getPeerEvents = function(span, event) {
- var cache = this.getEventCache();
- var peerEvents = [];
- var i, otherEvent;
- for (i = 0; i < cache.length; i++) {
- otherEvent = cache[i];
- if (
- !event ||
- event._id !== otherEvent._id // don't compare the event to itself or other related [repeating] events
- ) {
- peerEvents.push(otherEvent);
- }
- }
- return peerEvents;
- };
- // updates the "backup" properties, which are preserved in order to compute diffs later on.
- function backupEventDates(event) {
- event._allDay = event.allDay;
- event._start = event.start.clone();
- event._end = event.end ? event.end.clone() : null;
- }
- /* Business Hours
- -----------------------------------------------------------------------------------------*/
- // Return events objects for business hours within the current view.
- // Abuse of our event system :(
- Calendar.prototype.getCurrentBusinessHourEvents = function(wholeDay) {
- return this.computeBusinessHourEvents(wholeDay, this.options.businessHours);
- };
- // Given a raw input value from options, return events objects for business hours within the current view.
- Calendar.prototype.computeBusinessHourEvents = function(wholeDay, optionVal) {
- var defaultVal = {
- className: 'fc-nonbusiness',
- start: '09:00',
- end: '17:00',
- dow: [ 1, 2, 3, 4, 5 ], // monday - friday
- rendering: 'inverse-background'
- };
- var view = this.getView();
- var eventInput;
- if (optionVal) { // `true` (which means "use the defaults") or an override object
- eventInput = $.extend(
- {}, // copy to a new object in either case
- defaultVal,
- typeof optionVal === 'object' ? optionVal : {} // override the defaults
- );
- }
- if (eventInput) {
- // if a whole-day series is requested, clear the start/end times
- if (wholeDay) {
- eventInput.start = null;
- eventInput.end = null;
- }
- return this.expandEvent(
- this.buildEventFromInput(eventInput),
- view.start,
- view.end
- );
- }
- return [];
- };
|