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

97.81% Statements 179/183
90.09% Branches 100/111
100% Functions 20/20
97.75% Lines 174/178
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   1240× 1240×                             897× 897×   892×       892×   897× 897× 36×     861× 855×   861×   897×       861× 861× 861× 861× 861× 861× 861× 861× 861× 45× 45×   861× 45× 45× 45×   861× 719× 719× 719× 719× 45×   719× 45× 45× 45×   674× 45×   629× 17× 17×     612× 17× 17× 17×     595× 595×   58×   45× 45× 43× 43×         45×   45× 45×   45× 45×   58×     57×   58×     22× 22×   314× 103× 103×     211×         861×   15×   45× 45× 45× 28× 28×   45× 521× 45× 11× 11× 28× 17× 17× 17× 17× 17×   17× 17×     11× 11× 11×   34× 17× 17× 17×         17×   10×     20× 10× 20× 10×     272× 272×   124× 124× 124× 124× 29× 29× 13×   29×     124× 124× 111×     105×     124×   26333×   975× 949×          
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 ICalendarImport = (function () {
        function ICalendarImport(parent) {
            this.allDay = false;
            this.parent = parent;
        }
        ICalendarImport.prototype.initializeCalendarImport = function (fileContent) {
            var _this = this;
            Eif (fileContent && fileContent instanceof Blob) {
                var fileReader_1 = new FileReader();
                fileReader_1.onload = function () {
                    var iCalString = fileReader_1.result;
                    _this.iCalendarParser(iCalString);
                };
                fileReader_1.readAsText(fileContent, 'UTF-8');
            }
            else if (fileContent && typeof fileContent === 'string') {
                this.iCalendarParser(fileContent);
            }
        };
        ICalendarImport.prototype.iCalendarParser = function (iCalString) {
            var iCalData = {
                isEvent: false,
                curEvent: null,
                id: this.parent.eventBase.getEventMaxID(),
                count: 0,
                events: [],
                key: null
            };
            var iStringLength = iCalString.length;
            var lastPosition = iCalString.search(/[^ \t]/);
            var position = lastPosition;
            var iString;
            var newlineOffset;
            do {
                position = iCalString.indexOf('\n', lastPosition) + 1;
                if (position === 0) {
                    position = iStringLength;
                    newlineOffset = 0;
                }
                else Iif (position > 1 && iCalString[position - 2] === '\r') {
                    newlineOffset = 2;
                }
                else {
                    newlineOffset = 1;
                }
                var firstChar = iCalString.charAt(lastPosition);
                if (firstChar === ' ' || firstChar === '\n' || firstChar === '\t') {
                    iString += iCalString.slice(lastPosition + 1, position - newlineOffset);
                }
                else {
                    if (iString) {
                        iCalData = this.updateEventData(iString, iCalData);
                    }
                    iString = iCalString.slice(lastPosition, position - newlineOffset);
                }
                lastPosition = position;
            } while (position !== iStringLength);
            iString = iString.trim();
            Eif (iString.length) {
                iCalData = this.updateEventData(iString, iCalData);
            }
            var app = ej2_base_1.extend([], iCalData.events, null, true);
            this.parent.addEvent(this.processOccurrence(app, iCalData.id));
        };
        ICalendarImport.prototype.updateEventData = function (iString, iCalData) {
            var fields = this.parent.eventFields;
            var SEPARATOR = '\r\n';
            var id = iCalData.id;
            var events = iCalData.events;
            var isEvent = iCalData.isEvent;
            var count = iCalData.count;
            var curEvent = iCalData.curEvent;
            var key = iCalData.key;
            if (!isEvent && iString === 'BEGIN:VEVENT') {
                isEvent = true;
                curEvent = {};
            }
            if (isEvent && iString === 'END:VEVENT') {
                isEvent = false;
                events.push(curEvent);
                curEvent = null;
            }
            if (isEvent) {
                var index = iString.indexOf(':');
                var type_1 = iString.substring(0, index).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
                var value = iString.substring(index + 1, iString.length).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
                if (iString.indexOf('SUMMARY') !== -1) {
                    type_1 = 'SUMMARY';
                }
                if (iString.indexOf('DTSTART') !== -1) {
                    curEvent[fields.startTime] = this.dateParsing(iString);
                    curEvent[fields.isAllDay] = this.allDay;
                    this.allDay = false;
                }
                else if (iString.indexOf('DTEND') !== -1) {
                    curEvent[fields.endTime] = this.dateParsing(iString);
                }
                else if (iString.indexOf('EXDATE') !== -1) {
                    value = date_generator_1.getRecurrenceStringFromDate(this.dateParsing(iString));
                    curEvent[fields.recurrenceException] = ej2_base_1.isNullOrUndefined(curEvent[fields.recurrenceException]) ?
                        value : curEvent[fields.recurrenceException] + ',' + value;
                }
                else if (iString.indexOf('RECURRENCE-ID') !== -1) {
                    value = date_generator_1.getRecurrenceStringFromDate(this.dateParsing(iString));
                    curEvent[fields.recurrenceException] = value;
                    curEvent[fields.recurrenceID] = value;
                }
                else {
                    key = type_1 || key;
                    switch (key) {
                        case 'BEGIN':
                            break;
                        case 'UID':
                            curEvent["" + type_1] = value;
                            if (typeof (id) == 'number') {
                                curEvent[fields.id] = parseInt(value, 10);
                                if (isNaN(curEvent[fields.id])) {
                                    curEvent[fields.id] = id + count;
                                    count++;
                                }
                            }
                            else {
                                curEvent[fields.id] = value;
                            }
                            break;
                        case 'SUMMARY':
                            curEvent[fields.subject] = this.getFormattedString(value);
                            break;
                        case 'LOCATION':
                            curEvent[fields.location] = this.getFormattedString(value);
                            break;
                        case 'DESCRIPTION':
                            if (curEvent[fields.description]) {
                                curEvent[fields.description] = this.getFormattedString(curEvent[fields.description] + SEPARATOR + value);
                            }
                            else {
                                curEvent[fields.description] = this.getFormattedString(value);
                            }
                            break;
                        case 'ISREADONLY':
                            curEvent[fields.isReadonly] = (value.indexOf('true') > -1);
                            break;
                        case 'RRULE':
                            curEvent[fields.recurrenceRule] = value;
                            break;
                        default:
                            if (this.parent.resourceCollection.length > 0) {
                                var resData = this.parent.resourceCollection.filter(function (data) { return data.field === type_1; });
                                curEvent["" + type_1] = (resData.length > 0 && (typeof (resData[0].dataSource[0][resData[0].idField]) == 'number')) ? parseInt(value, 10) : value;
                            }
                            else {
                                curEvent["" + type_1] = value;
                            }
                    }
                }
            }
            return { isEvent: isEvent, curEvent: curEvent, id: id, count: count, events: events, key: key };
        };
        ICalendarImport.prototype.processOccurrence = function (app, maxId) {
            var _this = this;
            var appoint = [];
            var uId = 'UID';
            var fields = this.parent.eventFields;
            var appointmentIds = [];
            this.parent.eventsData.forEach(function (eventObj) {
                appointmentIds.push(eventObj[fields.id]);
            });
            app.forEach(function (eventObj) {
                var parentObj;
                var id;
                if (!eventObj.hasOwnProperty(fields.recurrenceID)) {
                    parentObj = eventObj;
                    id = eventObj[fields.id];
                }
                Eif (appointmentIds.indexOf(eventObj[fields.id]) < 0) {
                    var data = app.filter(function (data) { return data.UID === eventObj["" + uId]; });
                    if (data.length > 1 && ej2_base_1.isNullOrUndefined(eventObj[fields.recurrenceID])) {
                        id = typeof (maxId) === 'number' ? maxId++ : id;
                        for (var i = 0; i < data.length; i++) {
                            if (data[parseInt(i.toString(), 10)].hasOwnProperty(fields.recurrenceID)) {
                                var exdate = data[parseInt(i.toString(), 10)][fields.recurrenceID];
                                data[parseInt(i.toString(), 10)][fields.id] = typeof (maxId) === 'number' ? maxId++ : _this.parent.eventBase.generateGuid();
                                data[parseInt(i.toString(), 10)][fields.recurrenceID] = id;
                                data[parseInt(i.toString(), 10)][fields.recurrenceException] = null;
                                parentObj[fields.recurrenceException] =
                                    _this.getExcludeDateString(parentObj[fields.recurrenceException], exdate);
                                delete data[parseInt(i.toString(), 10)]["" + uId];
                                appoint.push(data[parseInt(i.toString(), 10)]);
                            }
                        }
                        delete parentObj["" + uId];
                        parentObj[fields.id] = id;
                        appoint.push(parentObj);
                    }
                    else if (!eventObj.hasOwnProperty(fields.recurrenceID)) {
                        delete eventObj["" + uId];
                        eventObj[fields.id] = typeof (maxId) === 'number' ? maxId++ : id;
                        appoint.push(eventObj);
                    }
                }
            });
            return appoint;
        };
        ICalendarImport.prototype.getExcludeDateString = function (parentException, occurrenceException) {
            if (ej2_base_1.isNullOrUndefined(parentException)) {
                return occurrenceException;
            }
            else Iif (ej2_base_1.isNullOrUndefined(occurrenceException)) {
                return parentException;
            }
            var parentExDate = parentException.split(',').map(function (x) { return x.split('T')[0]; });
            var childExDate = occurrenceException.split(',').map(function (x) { return x.split('T')[0]; });
            var exDate = parentExDate.filter(function (x) { return childExDate.indexOf(x) > -1; });
            if (exDate.length > 0) {
                return parentException;
            }
            return parentException + ',' + occurrenceException;
        };
        ICalendarImport.prototype.getFormattedString = function (value) {
            value = value || '';
            return (value.replace(/\\\,/g, ',').replace(/\\\;/g, ';').replace(/\\[nN]/g, '\n').replace(/\\\\/g, '\\'));
        };
        ICalendarImport.prototype.dateParsing = function (element) {
            var split = element.split(':');
            var value = split[split.length - 1];
            var newDate = new Date(this.getFormattedString(value));
            if (element && (element.indexOf('VALUE=DATE') > -1 || element.indexOf('RECURRENCE-ID;TZID') > -1)) {
                var data_1 = /^(\d{4})(\d{2})(\d{2})$/.exec(value);
                if (data_1 !== null) {
                    newDate = new Date(parseInt(data_1[1], 10), parseInt(data_1[2], 10) - 1, parseInt(data_1[3], 10));
                }
                if (element.indexOf('DTSTART') > -1) {
                    this.allDay = true;
                }
            }
            var data = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(Z)?$/.exec(value);
            if (data !== null) {
                if (data[7] === 'Z') {
                    newDate = new Date(Date.UTC(parseInt(data[1], 10), parseInt(data[2], 10) - 1, parseInt(data[3], 10), parseInt(data[4], 10), parseInt(data[5], 10), parseInt(data[6], 10)));
                }
                else {
                    newDate = new Date(parseInt(data[1], 10), parseInt(data[2], 10) - 1, parseInt(data[3], 10), parseInt(data[4], 10), parseInt(data[5], 10), parseInt(data[6], 10));
                }
            }
            return newDate;
        };
        ICalendarImport.prototype.getModuleName = function () {
            return 'iCalendarImport';
        };
        ICalendarImport.prototype.destroy = function () {
            if (!this.parent || this.parent && this.parent.isDestroyed) {
                return;
            }
        };
        return ICalendarImport;
    }());
    exports.ICalendarImport = ICalendarImport;
});