all files / schedule/exports/ calendar-export.js

97.06% Statements 99/102
92.06% Branches 58/63
100% Functions 19/19
96.94% Lines 95/98
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   1240×       36× 36× 36×   36×     36× 36× 36× 36×                 36×       36× 32× 32×         36×   36×       36×     36× 36× 15× 49×     36× 36× 36× 36×                     540× 36× 259×   76× 76× 76× 76× 76× 76× 76× 76×               12×   26333×   975× 949×   26×        
define(["require", "exports", "@syncfusion/ej2-base", "../../recurrence-editor/date-generator"], function (require, exports, ej2_base_1, date_generator_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ICalendarExport = (function () {
        function ICalendarExport(parent) {
            this.parent = parent;
        }
        ICalendarExport.prototype.initializeCalendarExport = function (fileName, customData) {
            var icsString = this.getCalendarString(fileName, customData);
            this.download(icsString, fileName);
        };
        ICalendarExport.prototype.getCalendarString = function (fileName, customData) {
            var _this = this;
            var eventsData = (customData) ? customData :
                ej2_base_1.extend([], this.parent.eventsData, null, true);
            eventsData = this.parent.eventBase.sortByTime(eventsData);
            var SEPARATOR = (navigator.appVersion.indexOf('Win') !== -1) ? '\r\n' : '\n';
            var iCalendarEvents = [];
            var filterCollection = [];
            var timeZone = this.parent.timezone || this.parent.tzModule.getLocalTimezoneName();
            var fields = this.parent.eventFields;
            eventsData.forEach(function (eventObj) {
                var uId = eventObj[fields.id] || eventObj.Guid || _this.parent.eventBase.generateGuid();
                var editedExDate = [];
                if (eventObj[fields.recurrenceID]) {
                    var filter = _this.filterEvents(filterCollection, fields.id, eventObj[fields.recurrenceID]);
                    uId = filter.length > 0 ? filter[0].UID : uId;
                }
                if (!eventObj[fields.recurrenceID] && eventObj[fields.recurrenceRule] && eventObj[fields.recurrenceException]) {
                    var exDate = (eventObj[fields.recurrenceException]).split(',');
                    var editedObj = _this.filterEvents(eventsData, fields.recurrenceID, eventObj[fields.id]);
                    editedObj.forEach(function (edited) {
                        editedExDate.push(date_generator_1.getRecurrenceStringFromDate(edited[fields.startTime]));
                    });
                    var exceptionDateList = exDate.filter(function (value) { return editedExDate.indexOf(value) === -1; });
                    eventObj[fields.recurrenceException] = (exceptionDateList.length > 0) ? (exceptionDateList.join(',') + ',') : '';
                }
                var startZone = (eventObj[fields.startTimezone] || timeZone);
                var endZone = (eventObj[fields.endTimezone] || timeZone);
                var readonly = (eventObj[fields.isReadonly]) ? (eventObj[fields.isReadonly]) : false;
                var calendarEvent = [
                    'BEGIN:VEVENT',
                    'LOCATION:' + (eventObj[fields.location] || ''),
                    'SUMMARY:' + (eventObj[fields.subject] || ''),
                    'UID:' + uId,
                    'DESCRIPTION:' + (eventObj[fields.description] || ''),
                    'ISREADONLY:' + readonly,
                    'END:VEVENT'
                ];
                Iif (eventObj[fields.isAllDay]) {
                    calendarEvent.splice(4, 0, 'DTEND;VALUE=DATE:' + _this.convertDateToString(eventObj[fields.endTime], true));
                    calendarEvent.splice(4, 0, 'DTSTART;VALUE=DATE:' + _this.convertDateToString(eventObj[fields.startTime], true));
                }
                else if (!eventObj[fields.isAllDay] && !eventObj[fields.recurrenceRule]) {
                    calendarEvent.splice(4, 0, 'DTEND:' + _this.convertDateToString(eventObj[fields.endTime]));
                    calendarEvent.splice(4, 0, 'DTSTART:' + _this.convertDateToString(eventObj[fields.startTime]));
                }
                else {
                    calendarEvent.splice(4, 0, 'DTEND;TZID="' + endZone + '":' + _this.convertDateToString(eventObj[fields.endTime]));
                    calendarEvent.splice(4, 0, 'DTSTART;TZID="' + startZone + '":'
                        + _this.convertDateToString(eventObj[fields.startTime]));
                }
                if (eventObj[fields.recurrenceRule]) {
                    calendarEvent.splice(4, 0, 'RRULE:' + eventObj[fields.recurrenceRule]);
                }
                if (eventObj[fields.recurrenceException]) {
                    var exDate = eventObj[fields.recurrenceException].split(',');
                    for (var i = 0; i < exDate.length - 1; i++) {
                        calendarEvent.splice(5, 0, 'EXDATE:' +
                            _this.convertDateToString(date_generator_1.getDateFromRecurrenceDateString(exDate[parseInt(i.toString(), 10)]), eventObj[fields.isAllDay]));
                    }
                }
                if (eventObj[fields.recurrenceID]) {
                    calendarEvent.splice(4, 0, 'RECURRENCE-ID;TZID="' + startZone + '":'
                        + _this.convertDateToString(eventObj[fields.startTime], eventObj[fields.isAllDay]));
                }
                var customFields = _this.customFieldFilter(eventObj, fields);
                if (customFields.length > 0) {
                    customFields.forEach(function (customField) {
                        return calendarEvent.splice(4, 0, customField + ':' + (eventObj["" + customField] || ''));
                    });
                }
                var app = ej2_base_1.extend({}, eventObj);
                app.UID = uId;
                filterCollection.push(app);
                iCalendarEvents.push(calendarEvent.join(SEPARATOR));
            });
            var iCalendar = [
                'BEGIN:VCALENDAR',
                'PRODID:-//Syncfusion Inc//Scheduler//EN',
                'VERSION:2.0',
                'CALSCALE:GREGORIAN',
                'METHOD:PUBLISH',
                'X-WR-CALNAME:' + (fileName || 'Calendar'),
                'X-WR-TIMEZONE:' + timeZone
            ].join(SEPARATOR);
            var icsString = iCalendar + SEPARATOR + iCalendarEvents.join(SEPARATOR) + SEPARATOR + 'END:VCALENDAR';
            return icsString;
        };
        ICalendarExport.prototype.customFieldFilter = function (eventObj, fields) {
            var defaultFields = Object.keys(fields).map(function (key) { return fields["" + key]; });
            var eventFields = Object.keys(eventObj);
            return eventFields.filter(function (value) { return (defaultFields.indexOf(value) === -1) && (value !== 'Guid'); });
        };
        ICalendarExport.prototype.convertDateToString = function (eventDate, allDay) {
            var year = ('0000' + (eventDate.getFullYear().toString())).slice(-4);
            var month = ('00' + ((eventDate.getMonth() + 1).toString())).slice(-2);
            var date = ('00' + ((eventDate.getDate()).toString())).slice(-2);
            var hours = ('00' + (eventDate.getHours().toString())).slice(-2);
            var minutes = ('00' + (eventDate.getMinutes().toString())).slice(-2);
            var seconds = ('00' + (eventDate.getSeconds().toString())).slice(-2);
            var timeString = (allDay) ? year + month + date : year + month + date + 'T' + hours + minutes + seconds;
            return timeString;
        };
        ICalendarExport.prototype.download = function (icsString, fileName) {
            var buffer = new Blob([icsString], { type: 'data:text/calendar;charset=utf8' });
            fileName = (fileName || 'Calendar') + '.ics';
            Iif (navigator.msSaveBlob) {
                navigator.msSaveBlob(buffer, fileName);
            }
            else {
                var downloadLink_1 = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
                downloadLink_1.download = fileName;
                downloadLink_1.href = URL.createObjectURL(buffer);
                var event_1 = document.createEvent('MouseEvent');
                event_1.initEvent('click', true, true);
                downloadLink_1.dispatchEvent(event_1);
                setTimeout(function () {
                    URL.revokeObjectURL(downloadLink_1.href);
                    downloadLink_1.href = undefined;
                });
            }
        };
        ICalendarExport.prototype.filterEvents = function (data, field, value) {
            return data.filter(function (e) { return e["" + field] === value; });
        };
        ICalendarExport.prototype.getModuleName = function () {
            return 'iCalendarExport';
        };
        ICalendarExport.prototype.destroy = function () {
            if (!this.parent || this.parent && this.parent.isDestroyed) {
                return;
            }
            this.parent = null;
        };
        return ICalendarExport;
    }());
    exports.ICalendarExport = ICalendarExport;
});