nfp_sites/filadelfia_archive/app/videos.js

121 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-11-09 09:44:04 +00:00
const m = require('mithril')
const api = require('./api')
const Tree = []
2023-11-20 07:12:08 +00:00
const Articles = []
2023-11-09 09:44:04 +00:00
exports.Tree = Tree
2023-11-20 07:12:08 +00:00
exports.Articles = Articles
2023-11-09 09:44:04 +00:00
exports.loading = false
exports.error = ''
2023-11-20 07:12:08 +00:00
exports.year = null
exports.month = null
2023-11-09 09:44:04 +00:00
2023-11-20 07:12:08 +00:00
const matcher = /\/(\d+)(\/\d+)?/
function calculateActiveBranches() {
let path = matcher.exec(m.route.get())
if (path && path[1] !== exports.year?.title) {
for (let year of Tree) {
if (year.title === path[1]) {
exports.year = year
break
}
}
} else if (!path && m.route.get() === '/') {
2023-11-20 07:12:08 +00:00
exports.year = Tree[Tree.length - 1]
} else if (!path) {
exports.year = null
}
if (path && exports.year && path[2]) {
exports.month = exports.year.branches[Number(path[2].slice(1)) - 1] || null
} else if (!path?.[2]) {
exports.month = null
}
}
function rebuildTree() {
Tree.splice(0, Tree.length)
if (!Articles.length) return
let startYear = Articles[0].publish_at
let target = new Date()
let articleIndex = 0
for (let year = startYear.getFullYear(); year <= target.getFullYear(); year++) {
let branchYear = {
title: year.toString(),
type: 'year',
branches: [],
videos: []
}
Tree.push(branchYear)
let lastMonth = year === target.getFullYear() ? target.getMonth() + 1 : 12
for (let month = 1; month <= lastMonth; month++) {
let branchMonth = {
title: month.toString(),
type: 'month',
branches: [],
videos: []
}
branchYear.branches.push(branchMonth)
let start = new Date(year, month - 1)
let end = new Date(year, month)
for (; Articles[articleIndex] && Articles[articleIndex].publish_at >= start && Articles[articleIndex].publish_at < end; articleIndex++) {
branchYear.videos.push(Articles[articleIndex])
branchMonth.videos.push(Articles[articleIndex])
}
}
}
}
function removeArticle(id) {
let index = Articles.findIndex(article => article.id === id)
if (index >= 0) {
Articles.splice(index, 1)
rebuildTree()
}
}
2023-11-20 07:12:08 +00:00
function refreshTree() {
2023-11-09 09:44:04 +00:00
exports.error = ''
if (exports.loading) return Promise.resolve()
exports.loading = true
m.redraw()
return api.sendRequest({
method: 'GET',
url: '/api/articles',
2023-11-09 09:44:04 +00:00
})
2023-11-20 07:12:08 +00:00
.then(result => {
result.videos.forEach(video => {
video.publish_at = new Date(video.publish_at)
video.path_short = video.path.split('-')[2]
})
Articles.splice(0, Articles.length)
Articles.push.apply(Articles, result.videos)
rebuildTree()
2023-11-09 09:44:04 +00:00
}, err => {
2023-11-20 07:12:08 +00:00
exports.error = 'Error fetching videos: ' + err.message
})
.then(() => {
2023-11-09 09:44:04 +00:00
exports.loading = false
m.redraw()
})
2023-11-20 07:12:08 +00:00
}
exports.removeArticle = removeArticle
2023-11-20 07:12:08 +00:00
exports.rebuildTree = rebuildTree
exports.refreshTree = refreshTree
exports.calculateActiveBranches = calculateActiveBranches