forked from datopian/frontend-oddk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
213 lines (200 loc) · 6.36 KB
/
index.js
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
const moment = require('moment')
module.exports = function (app) {
const utils = app.get('utils')
const dms = app.get('dms')
const cms = app.get('cms')
const config = app.get('config')
const DmsModel = new dms.DmsModel(config)
const CmsModel = new cms.CmsModel()
app.use((req, res, next) => {
req.setLocale('da')
next()
})
app.use(async (req, res, next) => {
// Get links for the navbar from CMS (WP)
res.locals.aboutPages = (await CmsModel.getListOfPosts({type: 'page'}))
.filter(page => page.parent && page.parent.ID === 11)
next()
})
// Need to do this again for routes in WP CMS plugin as it never hits `.use` middleware
// TODO: fix WP CMS plugin so it doesn't send back the response before theme
// controller is executed.
app.param('page', async (req, res, next) => {
if (!res.locals.aboutPages) {
res.locals.aboutPages = (await CmsModel.getListOfPosts({type: 'page'}))
.filter(page => page.parent && page.parent.ID === 11)
}
// Add featured posts
res.locals.featuredPosts = (await CmsModel.getListOfPosts(
{
tag: 'featured',
number: 5
}
)).map(post => {
return {
slug: post.slug,
title: post.title,
content: post.content,
published: moment(post.date).format('Do MMMM YYYY'),
modified: moment(post.modified).format('Do MMMM YYYY'),
image: post.featured_image
}
})
next()
})
app.get('/', async (req, res, next) => {
// Set up main heading text from wp:
const siteInfo = await CmsModel.getSiteInfo()
res.locals.home_heading = siteInfo.description || ''
// Get collections with extras
const collections = await DmsModel.getCollections({
all_fields: true,
include_extras: true,
include_dataset_count: false
})
// Filter collections as we want to show only featured items
const featured = collections.filter(collection => {
return collection.extras.find(extra => extra.key === 'featured' && extra.value)
})
// Shuffle array
let shuffled
if (featured.length >= 4) {
shuffled = featured.sort(() => 0.5 - Math.random())
} else {
shuffled = collections.sort(() => 0.5 - Math.random())
}
// Get sub-array of first n elements after shuffled
const randomFour = shuffled.slice(0, 4)
res.locals.collections = randomFour
// Get events
res.locals.events = (await CmsModel.getListOfPosts(
{
category: 'Events',
number: 5
}
)).map(post => {
let eventDate
for (let key in post.tags) {
if (key.startsWith('dato:')) {
eventDate = post.tags[key].name.match(/\d{2}([\/.-])\d{2}\1\d{4}/g)
}
}
const monthNames = ["jan", "feb", "mar", "apr", "maj", "jun", "jul",
"aug", "sep", "okt", "nov", "dec"
]
if (eventDate) {
const day = eventDate[0].substring(0, 2)
const month = eventDate[0].substring(3, 5)
const year = eventDate[0].substring(6, 10)
const date = new Date(`${year}-${month}-${day}`)
post.day = date.getDate()
post.month = monthNames[date.getMonth()]
// Check if event is completed:
post.completed = date < new Date() ? true : false
} else {
// If no 'dato' tag is found, use current date:
const now = new Date()
post.day = now.getUTCDate()
post.month = monthNames[now.getUTCMonth()]
}
return post
})
// Get title and content of about page to display it on front page:
const aboutPage = await CmsModel.getPost('about')
res.locals.aboutTitle = aboutPage.title
res.locals.aboutText = aboutPage.content
next()
})
app.get('/blog', async (req, res, next) => {
// Get list of categories
const {found, categories} = await CmsModel.getCategories()
res.locals.categories = categories
res.locals.selectedCategory = req.query.category
// Add featured posts
res.locals.featuredPosts = (await CmsModel.getListOfPosts(
{
tag: 'featured',
number: 5
}
)).map(post => {
return {
slug: post.slug,
title: post.title,
content: post.content,
published: moment(post.date).format('Do MMMM YYYY'),
modified: moment(post.modified).format('Do MMMM YYYY'),
image: post.featured_image
}
})
next()
})
app.get('/search', async (req, res, next) => {
try {
let facetNameToShowAll
for (let [key, value] of Object.entries(req.query)) {
if (key.includes('facet.limit.')) {
facetNameToShowAll = key.split('.')[2]
req.query['facet.limit'] = value
}
}
const result = await DmsModel.search(req.query)
if (facetNameToShowAll) {
for (let [key, value] of Object.entries(result.search_facets)) {
// Sort facets by count
result.search_facets[key].items = result.search_facets[key].items
.sort((a, b) => b.count - a.count)
if (key !== facetNameToShowAll) {
result.search_facets[key].items = result.search_facets[key].items
.slice(0, 5)
}
}
}
// Pagination
const from = req.query.from || 0
const size = req.query.size || 10
const total = result.count
const totalPages = Math.ceil(total / size)
const currentPage = parseInt(from, 10) / size + 1
const pages = utils.pagination(currentPage, totalPages)
res.render('search.html', {
title: 'Search',
result,
query: req.query,
totalPages,
pages,
currentPage
})
} catch (e) {
next(e)
}
})
app.get('/search/content', async (req, res, next) => {
try {
const result = await CmsModel.getListOfPostsWithMeta(
{
type: 'any',
search: req.query.q,
number: 10,
offset: req.query.from || 0
}
)
// Pagination
const from = req.query.from || 0
const size = 10
const total = result.found
const totalPages = Math.ceil(total / size)
const currentPage = parseInt(from, 10) / size + 1
const pages = utils.pagination(currentPage, totalPages)
res.render('search.html', {
title: 'Search content',
result,
query: req.query,
totalPages,
pages,
currentPage
})
} catch (e) {
next(e)
}
})
}