From 681f3ad2cd1163dbd049b4e917c4631541d3bb89 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Tue, 25 Jul 2023 15:57:05 +0800 Subject: Move JavaScript functions out of ActivityView.qml --- contents/ui/ActivityView.qml | 81 ++------------------------------------------ contents/ui/activity.js | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 78 deletions(-) create mode 100644 contents/ui/activity.js (limited to 'contents/ui') diff --git a/contents/ui/ActivityView.qml b/contents/ui/ActivityView.qml index 6c3bb87..9b547c5 100644 --- a/contents/ui/ActivityView.qml +++ b/contents/ui/ActivityView.qml @@ -8,88 +8,13 @@ import org.kde.plasma.components 2.0 as PlasmaComponents import org.kde.plasma.components 3.0 as PlasmaComponents3 import org.kde.plasma.extras 2.0 as PlasmaExtras +import "activity.js" as Activity + RowLayout { id: activityView width: (type == "announcement" ? announcementsListView : assignmentsListView).width Layout.fillWidth: true - function sameDay(due, now) { - return ( - due.getFullYear() == now.getFullYear() && - due.getMonth() == now.getMonth() && - due.getDate() == now.getDate() - ) - } - - function dayDiff(due, now, stopAt) { - // due and now are both Date objects - // if due has the same day as now (i.e. due is today), return 0 - // if due is tomorrow, return 1, the day after that 2, yesterday -1, etc. - // when the absolute value of diff reaches stopAt, return stopAt - let diff = 0 - const step = (due > now) ? 1 : -1 - while (!sameDay(due, now)) { - now.setDate(now.getDate() + step) - diff += step - if (Math.abs(diff) >= stopAt) { - return stopAt - } - } - return diff - } - - function humanDue(isoDue) { - // converts ISO 8601 datetime string to relative time, e.g. tomorrow midnight - const daysOfWeek = [ - i18n("Sunday"), - i18n("Monday"), - i18n("Tuesday"), - i18n("Wednesday"), - i18n("Thursday"), - i18n("Friday"), - i18n("Saturday"), - ] - - try { - let due = new Date(isoDue) - if (due.toString() == "Invalid Date") { - console.warn(`Cannot parse due date: ${isoDue}`) - return isoDue - } - - let now = new Date() - const pastDue = (due <= now) - const dayDifference = dayDiff(due, now, 8) - let humanDueString = "" - - if (dayDifference <= -2) { - humanDueString = Qt.formatDate(due) - } else if (dayDifference == -1) { - humanDueString = i18n("Yesterday") - } else if (dayDifference == 0) { - humanDueString = i18n("Today") - } else if (dayDifference == 1) { - humanDueString = i18n("Tomorrow") - } else if (dayDifference <= 6) { - humanDueString = daysOfWeek[due.getDay()] - } else { - humanDueString = Qt.formatDate(due) - } - - humanDueString += " " + Qt.formatTime(due) - return humanDueString - } catch (e) { - return isoDue // screw it - } - } - - function dueLabelText() { - if (type != "assignment") { return "" } - if (submitted) { return i18n("Submitted") } - if (!dueAt) { return i18n("Due date not announced") } - return i18n("Due: ") + humanDue(dueAt) - } - RowLayout { Layout.fillWidth: true PlasmaComponents3.CheckBox { @@ -132,7 +57,7 @@ RowLayout { PlasmaComponents3.Label { id: dueLabel visible: type == "assignment" - text: dueLabelText() + text: Activity.dueLabelText() color: activityLabel.color Layout.fillWidth: true } diff --git a/contents/ui/activity.js b/contents/ui/activity.js new file mode 100644 index 0000000..890082e --- /dev/null +++ b/contents/ui/activity.js @@ -0,0 +1,76 @@ +function sameDay(due, now) { + return ( + due.getFullYear() == now.getFullYear() && + due.getMonth() == now.getMonth() && + due.getDate() == now.getDate() + ) +} + +function dayDiff(due, now, stopAt) { + // due and now are both Date objects + // if due has the same day as now (i.e. due is today), return 0 + // if due is tomorrow, return 1, the day after that 2, yesterday -1, etc. + // when the absolute value of diff reaches stopAt, return stopAt + let diff = 0 + const step = (due > now) ? 1 : -1 + while (!sameDay(due, now)) { + now.setDate(now.getDate() + step) + diff += step + if (Math.abs(diff) >= stopAt) { + return stopAt + } + } + return diff +} + +function humanDue(isoDue) { + // converts ISO 8601 datetime string to relative time, e.g. tomorrow midnight + const daysOfWeek = [ + i18n("Sunday"), + i18n("Monday"), + i18n("Tuesday"), + i18n("Wednesday"), + i18n("Thursday"), + i18n("Friday"), + i18n("Saturday"), + ] + + try { + let due = new Date(isoDue) + if (due.toString() == "Invalid Date") { + console.warn(`Cannot parse due date: ${isoDue}`) + return isoDue + } + + let now = new Date() + const pastDue = (due <= now) + const dayDifference = dayDiff(due, now, 8) + let humanDueString = "" + + if (dayDifference <= -2) { + humanDueString = Qt.formatDate(due) + } else if (dayDifference == -1) { + humanDueString = i18n("Yesterday") + } else if (dayDifference == 0) { + humanDueString = i18n("Today") + } else if (dayDifference == 1) { + humanDueString = i18n("Tomorrow") + } else if (dayDifference <= 6) { + humanDueString = daysOfWeek[due.getDay()] + } else { + humanDueString = Qt.formatDate(due) + } + + humanDueString += " " + Qt.formatTime(due) + return humanDueString + } catch (e) { + return isoDue // screw it + } +} + +function dueLabelText() { + if (type != "assignment") { return "" } + if (submitted) { return i18n("Submitted") } + if (!dueAt) { return i18n("Due date not announced") } + return i18n("Due: ") + humanDue(dueAt) +} -- cgit v1.2.3