UNPKG

1.61 kBJavaScriptView Raw
1'use strict'
2const d3 = require('./d3.js')
3
4class CategoryContent {
5 constructor (node) {
6 this.category = node.dataset.category
7 this.order = parseInt(node.dataset.order, 10)
8 this.issue = node.dataset.issue === 'yes'
9 this.menu = node.dataset.menu
10 this.title = node.dataset.title
11 this.summary = null
12 this.readMore = null
13 this.articleHeadings = null
14 }
15
16 addContent (node) {
17 switch (node.dataset.type) {
18 case 'summary':
19 this.summary = document.adoptNode(node.content)
20 break
21 case 'read-more':
22 this.readMore = document.adoptNode(node.content)
23 break
24 default:
25 throw new Error('unknow type: ' + node.dataset.type)
26 }
27 }
28
29 getSummary () {
30 return this.summary.cloneNode(true)
31 }
32
33 hasSummary () {
34 return this.summary !== null
35 }
36
37 getReadMore () {
38 return this.readMore.cloneNode(true)
39 }
40
41 hasReadMore () {
42 return this.readMore !== null
43 }
44}
45
46class Categories {
47 constructor () {
48 const categories = new Map()
49 for (const node of d3.selectAll('.recommendation-text').nodes()) {
50 if (!categories.has(node.dataset.category)) {
51 categories.set(node.dataset.category, new CategoryContent(node))
52 }
53 categories.get(node.dataset.category).addContent(node)
54 }
55
56 this.categories = categories
57 this.categoriesAsArray = Array.from(
58 this.categories.values()
59 ).sort((a, b) => a.order - b.order)
60 }
61
62 getContent (category) {
63 return this.categories.get(category)
64 }
65
66 asArray () {
67 return this.categoriesAsArray
68 }
69}
70
71module.exports = new Categories()