private _createEventSource()

in legacy/src/Calendar/Calendar.ts [265:348]


    private _createEventSource(source: Calendar_Contracts.IEventSource, options: FullCalendar.Options): CalendarEventSource {
        const state: CalendarEventSourceState = {};

        const getEventsMethod = (
            start: Date,
            end: Date,
            timezone: string | boolean,
            callback: (events: FullCalendar.EventObject[]) => void,
        ) => {
            if (!state.dirty && state.cachedEvents) {
                callback(state.cachedEvents);
                return;
            }
            const loadSourcePromise = <PromiseLike<Calendar_Contracts.CalendarEvent[]>>source.load();
            timeout(loadSourcePromise, 5000, "Could not load event source " + source.name + ". Request timed out.").then(
                results => {
                    const calendarEvents = results.map((value, index) => {
                        const start = value.startDate;
                        const end = value.endDate ? Utils_Date.addDays(new Date(value.endDate), 1).toISOString() : start;
                        const event: any = {
                            id: value.id || Calendar_Utils_Guid.newGuid(),
                            title: value.title,
                            description: value.description,
                            allDay: true,
                            start: start,
                            end: end,
                            eventType: source.id,
                            rendering: (<any>options).rendering || "",
                            category: value.category,
                            iterationId: value.iterationId,
                            member: value.member,
                            editable: value.movable,
                            icons: value.icons,
                            eventData: value.eventData,
                        };

                        if (value.__etag) {
                            event.__etag = value.__etag;
                        }

                        if ($.isFunction(source.addEvent) && value.category) {
                            const color =
                                <any>value.category.color ||
                                Calendar_ColorUtils.generateColor(
                                    (<string>value.category.title || "uncategorized").toLowerCase(),
                                );
                            event.backgroundColor = color;
                            event.borderColor = color;
                            event.textColor = value.category.textColor || "#FFFFFF";
                        }

                        if ((<any>options).rendering === "background" && value.category) {
                            const color =
                                <any>value.category.color ||
                                Calendar_ColorUtils.generateBackgroundColor(
                                    (<string>event.category.title || "uncategorized").toLowerCase(),
                                );
                            event.backgroundColor = color;
                            event.borderColor = color;
                            event.textColor = value.category.textColor || "#FFFFFF";
                        }

                        return event;
                    });

                    state.dirty = false;
                    state.cachedEvents = calendarEvents;
                    callback(calendarEvents);
                },
                reason => {
                    console.error(
                        Utils_String.format("Error getting event data.\nEvent source: {0}\nReason: {1}", source.name, reason),
                    );
                    callback([]);
                },
            );
        };

        const calendarEventSource: CalendarEventSource = <any>getEventsMethod;
        calendarEventSource.eventSource = source;
        calendarEventSource.state = state;

        return calendarEventSource;
    }