diff --git a/widget/assets/js/rrule.js b/widget/assets/js/rrule.js index b75e991..a5a0ea9 100644 --- a/widget/assets/js/rrule.js +++ b/widget/assets/js/rrule.js @@ -1884,7 +1884,8 @@ rrule._iter(iterResult) }) - var res = iterResult._result + // Filtering excluded and deleted dates to get the final updated events result + var res = iterResult._result.filter(date => !this._exdate.find(exdate => new Date(exdate).setHours(0,0,0) === new Date(date).setHours(0,0,0))) dateutil.sort(res) switch (iterResult.method) { case 'all': diff --git a/widget/controllers/widget.feed.controller.js b/widget/controllers/widget.feed.controller.js index 0536f66..aac993f 100644 --- a/widget/controllers/widget.feed.controller.js +++ b/widget/controllers/widget.feed.controller.js @@ -285,7 +285,6 @@ temp_result.tmpStartDate = temp_result.startDate; temp_result.startDate = Date.parse(dates[j]); temp_result.endDate = temp_result.startDate + eventDuration; - temp_result.UID = temp_result.UID + String(temp_result.startDate) + String(temp_result.endDate) if (temp_result.startDate >= +new Date(eventStartDate) && temp_result.startDate <= +new Date(eventRecEndDate)) if (AllEvent) repeat_results.push(temp_result); @@ -314,13 +313,15 @@ startDate += 86400000; while (startDate < endDate) { if (startDate >= +new Date(eventStartDate) && startDate <= +new Date(eventRecEndDate) && (AllEvent || startDate >= timeStampInMiliSec)) { - repeat_results.push({...result.events[i], startDate: new Date(startDate), UID: "_id" + Date.now() + Math.random()}); + repeat_results.push({...result.events[i], startDate: new Date(startDate)}); } startDate += 86400000; } } }} + // filter out the repeating events that have been updated + repeat_results = utils.filterUpdatedEvents(repeat_results); //sort the list by start date repeat_results.sort(function (a, b) { if (a.startDate > b.startDate) { diff --git a/widget/index.html b/widget/index.html index 731d0c2..5f23bc9 100644 --- a/widget/index.html +++ b/widget/index.html @@ -25,6 +25,8 @@ + + @@ -68,7 +70,7 @@ ng-swipe-left="WidgetFeed.addEvents($event, $index, false)" custom-class="getDayClass(date, mode)" ng-class="{'active' : WidgetFeed.swiped[$index]}" - ng-repeat="event in WidgetFeed.events track by event.UID"> + ng-repeat="event in WidgetFeed.events track by event.id">
diff --git a/widget/utils/index.js b/widget/utils/index.js new file mode 100644 index 0000000..ad8b6c4 --- /dev/null +++ b/widget/utils/index.js @@ -0,0 +1,43 @@ + +const utils = { + // this function will filter out the repeating events that have been updated + // it will skip all previous versions, and only show the latest version of the event + filterUpdatedEvents(events) { + let latestEvents = {}; + events.forEach(event => { + let uid = event.UID; + // id is a unique identifier key to separate event and avoid duplicate keys in the DOM + event.id = nanoid(); + let newStartDate = new Date(event.startDate); + let recurrenceId = 0; + let oldRecurrenceId = 0; + // check if this event has been seen before + // the key is the UID + the date of the event + let storedRecored = latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`]; + + for (const key in event) { + if (key.startsWith('RECURRENCE-ID')) { + recurrenceId = event[key]; + } + } + if (storedRecored) { + for (const key in storedRecored) { + if (key.startsWith('RECURRENCE-ID')) { + oldRecurrenceId = event[key]; + } + } + } + + // to add the event to the list, it must be the first time we see it + // or it must be a newer version of the event + if (!storedRecored + || (recurrenceId && !oldRecurrenceId) + || (recurrenceId && oldRecurrenceId && Number(recurrenceId.substr(0, 8)) > Number(oldRecurrenceId.substr(0, 8)) && Number(recurrenceId.substr(9, 15)) > Number(oldRecurrenceId.substr(9, 15))) + ) { + latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`] = event; + } + }); + // return the values of the object + return Object.values(latestEvents); + }, +}; \ No newline at end of file diff --git a/widget/utils/nanoid.js b/widget/utils/nanoid.js new file mode 100644 index 0000000..e5d3ce6 --- /dev/null +++ b/widget/utils/nanoid.js @@ -0,0 +1,15 @@ +let nanoid = (t = 21) => + crypto + .getRandomValues(new Uint8Array(t)) + .reduce( + (t, e) => + (t += + (e &= 63) < 36 + ? e.toString(36) + : e < 62 + ? (e - 26).toString(36).toUpperCase() + : e > 62 + ? '-' + : '_'), + '' + );