-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathtag.js
223 lines (203 loc) · 6.1 KB
/
tag.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
214
215
216
217
218
219
220
221
222
223
import archerUtil from './util'
import sidebar from './initSidebar'
import Emitter from 'eventemitter3'
class MetaInfo {
constructor(metaName, labelsContainer, postContainer) {
this.currLabelName = ''
this.isInited = false
this.postsArr = null
this.metaName = metaName
this.labelsContainer = $(labelsContainer)[0]
this.postContainer = $(postContainer)[0]
this.indexMap = new Map()
this._bindLabelClick()
}
changeLabel(currLabelName) {
if (typeof currLabelName === 'string') {
this.currLabelName = currLabelName
this._changeList()
this._changeFocus()
}
}
_bindLabelClick() {
this.labelsContainer.addEventListener('click', (e) => {
const currLabelName = e.target.getAttribute(`data-${this.metaName}`)
this.changeLabel(currLabelName)
})
}
_changeFocus(label) {
const currFocus = this.labelsContainer.getElementsByClassName(
'sidebar-label-focus'
)
;[...currFocus].forEach((item) =>
item.classList.remove('sidebar-label-focus')
)
;[...this.labelsContainer.children].forEach((item) => {
if (item.getAttribute(`data-${this.metaName}`) === this.currLabelName) {
item.classList.add('sidebar-label-focus')
}
})
}
_changeList() {
const indexArr = this.indexMap.get(this.currLabelName)
try {
const corrArr = indexArr.map((index) => {
return this.postsArr[index]
})
this._createPostsDom(corrArr)
} catch (error) {
console.error(
'Please ensure set `tags: true` and `categories: true` of the hexo-content-json config'
)
}
}
_createPostsDom(corrArr) {
const frag = document.createDocumentFragment()
this.postContainer.innerHTML = ''
for (let i = 0; i < corrArr.length; i++) {
frag.appendChild(this._createPostDom(corrArr[i]))
}
this.postContainer.appendChild(frag)
}
_createPostDom(postInfo) {
const $tagItem = $(
'<li class="meta-post-item"><span class="meta-post-date">' +
archerUtil.dateFormater(
new Date(Date.parse(postInfo.date)),
'yyyy/MM/dd'
) +
'</span></li>'
)
const $aItem = $(
'<a class="meta-post-title" href="' +
siteMeta.root +
postInfo.path +
'">' +
postInfo.title +
'</a>'
)
$tagItem.append($aItem)
return $tagItem[0]
}
tryInit(postsArr) {
if (
this.isInited ||
Object.prototype.toString.call(postsArr) === '[object Null]'
) {
return
}
for (let postIndex = 0; postIndex < postsArr.length; postIndex++) {
const currPostLabels = postsArr[postIndex][this.metaName]
// if there is any post has a tag
if (currPostLabels && currPostLabels.length) {
currPostLabels.forEach((tagOrCatetory) => {
// if this.metaName is 'categories', tagOrCatetory['slug'] will be used as key in this.indexMap
// else if this.metaName is 'tag', tagOrCatetory['name'] will be used as key in this.indexMap
// check the array postsArr and you'll know why. (actually you can just use 'slug' in both case)
// const key = this.metaName === 'categories' ? 'slug' : 'name'
const key = 'slug'
if (this.indexMap.has(tagOrCatetory[key])) {
this.indexMap.get(tagOrCatetory[key]).push(postIndex)
} else {
this.indexMap.set(tagOrCatetory[key], [postIndex])
}
})
}
}
if (!this.indexMap.size) {
document
.getElementsByClassName(`sidebar-${this.metaName}-empty`)[0]
.classList.add(`sidebar-${this.metaName}-empty-active`)
}
this.postsArr = postsArr
this.isInited = true
}
}
class SidebarMeta {
constructor(tabCount) {
this.tabCount = 0
this.emitter = new Emitter()
this.postsArr = null
this.metas = []
this._initMap = this._initMap.bind(this)
this.dataMaps = new Map()
this._subscribe()
this._fetchInfo()
this._bindOtherClick()
}
// add a new tab and updata all metas
addTab(para) {
this.tabCount++
const newMeta = new MetaInfo(
para.metaName,
para.labelsContainer,
para.postsContainer
)
newMeta.tryInit(this.postsArr)
this.metas.push(newMeta)
}
// update every MetaInfo
_tryInitMetas() {
for (let i = 0; i < this.metas.length; i++) {
this.metas[i].tryInit(this.postsArr)
}
}
// subscribe data onload
_subscribe() {
this.emitter.on('DATA_FETCHED_SUCCESS', this._initMap)
}
// init maps
_initMap() {
if (!this.postsArr.length) {
return
}
this._tryInitMetas()
}
// fetch content.json
_fetchInfo() {
// siteMeta is from js-info.ejs
const contentURL = siteMeta.root + 'content.json?t=' + Number(new Date())
const xhr = new XMLHttpRequest()
xhr.responseType = ''
xhr.open('get', contentURL, true)
const $loadFailed = $('.tag-load-fail')
const that = this
xhr.onload = function () {
if (this.status === 200 || this.status === 304) {
$loadFailed.remove()
// defensive programming if content.json formart is not correct
// pr: https://github.com/fi3ework/hexo-theme-archer/pull/37
const contentJSON = JSON.parse(this.responseText)
const posts = Array.isArray(contentJSON)
? contentJSON
: contentJSON.posts
if (posts && posts.length) {
that.postsArr = posts
that.emitter.emit('DATA_FETCHED_SUCCESS')
}
} else {
this.$currPostsContainer.remove()
}
}
xhr.send()
}
_bindOtherClick() {
$('.post-tag').click((e) => {
e.stopPropagation()
sidebar.activateSidebar()
sidebar.switchTo(1)
this.currLabelName = e.target.getAttribute('data-tags')
const tagMeta = this.metas[0]
tagMeta.changeLabel(this.currLabelName)
})
$('.post-category').click((e) => {
e.stopPropagation()
sidebar.activateSidebar()
sidebar.switchTo(2)
this.currLabelName = e.target.getAttribute('data-categories')
const categoryMeta = this.metas[1]
categoryMeta.changeLabel(this.currLabelName)
})
}
}
export default SidebarMeta