Skip to content

Commit

Permalink
sort by categorie
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Jan 22, 2024
1 parent ffe5312 commit 7fc4116
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
43 changes: 26 additions & 17 deletions application/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,6 @@ const find_closest_date = function (date) {
try {
let m = events.findIndex((event) => dayjs(event.DTSTAMP).unix() < search);
t = events[m].UID;
// console.log(m);

focusAndScroll();
} catch (e) {
Expand Down Expand Up @@ -1801,13 +1800,14 @@ var page_events_filtered = {
oninit: () => {
view_history_update();
status.shortCut = false;
side_toaster('Category: ' + query, 8000);
},
oncreate: function () {
document.querySelectorAll('.item')[0].focus();
bottom_bar(
"<img src='assets/image/pencil.svg'>",
"<img src='assets/image/calendar.svg'>",
''
"<img src='assets/image/E257.svg'>"
);
},
},
Expand Down Expand Up @@ -4582,24 +4582,29 @@ export let set_tabindex = () => {
}
});
};
let h = 0;

const sort_events = () => {
let sort_mode = settings.eventsfilter
? ['dateEndUnix', 'lastmod', settings.eventsfilter]
: ['dateEndUnix', 'lastmod'];

h++;
if (h === sort_mode.length) h = 0;

document.getElementById('search').value = '';
if (status.sortEvents == 'startDate') {
sort_array_last_mod().then(() => {
m.route.set('/page_events');
status.sortEvents = 'lastmod';
});
document.activeElement.parentElement.firstChild.focus();

side_toaster('the last modified ones now appear at the top', 6000);
if (h == 2) {
m.route.set('/page_events_filtered', { query: settings.eventsfilter });
} else {
sort_array(events, 'dateEndUnix', 'date');
document.activeElement.parentElement.firstChild.focus();
side_toaster('the date start ones now appear at the top', 6000);
status.sortEvents = 'startDate';
sort_array(events, sort_mode[h], 'date').then(() => {
m.redraw();
if (h == 0)
side_toaster('The last modified ones now appear at the top', 8000);
if (h == 1)
side_toaster('The date start ones now appear at the top', 8000);
});
}
m.redraw();
};

let backup_events = function () {
Expand Down Expand Up @@ -4772,15 +4777,18 @@ function shortpress_action(param) {
});
break;

case '7':
break;

case '8':
m.route.set('/page_events', { query: 'sort_by_last_mod' });

break;
case 'SoftRight':
case 'Alt':
if (m.route.get().startsWith('/page_events_filtered')) {
m.route.set('/page_events');

return true;
}

if (m.route.get() == '/page_calendar' && status.shortCut) {
let n = '';
let f = document.querySelectorAll('#event-slider article');
Expand Down Expand Up @@ -4817,6 +4825,7 @@ function shortpress_action(param) {
sort_events();
return true;
}

if (m.route.get().startsWith('/page_add_event')) {
get_contact(callback_get_contact);
}
Expand Down
30 changes: 28 additions & 2 deletions application/assets/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { events, background_sync_interval } from '../../app.js';
import { status } from '../../app.js';
import { uid } from 'uid';

export function sort_array(arr, itemKey, type) {
export async function sort_array(arr, itemKey, type) {
const sortFunction = (a, b) => {
if (type === 'date') {
return new Date(b[itemKey]) - new Date(a[itemKey]);
Expand All @@ -17,7 +17,33 @@ export function sort_array(arr, itemKey, type) {
}
};

arr.sort(sortFunction);
if (itemKey == 'lastmod') {
// Custom comparison function to sort by date-like properties
function compareDateObjects(a, b) {
const dateA = new Date(
a['LAST-MODIFIED']['_time'].year,
a['LAST-MODIFIED']['_time'].month - 1,
a['LAST-MODIFIED']['_time'].day,
a['LAST-MODIFIED']['_time'].hour,
a['LAST-MODIFIED']['_time'].minute,
a['LAST-MODIFIED']['_time'].second
);
const dateB = new Date(
b['LAST-MODIFIED']['_time'].year,
b['LAST-MODIFIED']['_time'].month - 1,
b['LAST-MODIFIED']['_time'].day,
b['LAST-MODIFIED']['_time'].hour,
b['LAST-MODIFIED']['_time'].minute,
b['LAST-MODIFIED']['_time'].second
);

return dateB - dateA;
}

events.sort(compareDateObjects);
} else {
arr.sort(sortFunction);
}
}

export async function sort_array_last_mod() {
Expand Down
2 changes: 1 addition & 1 deletion application/manifest.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],

"b2g_features": {
"version": "3.0.645",
"version": "3.0.647",
"id": "greg",
"subtitle": "easy to use calendar",
"core": true,
Expand Down

0 comments on commit 7fc4116

Please sign in to comment.