-
Notifications
You must be signed in to change notification settings - Fork 1
/
guide.js
149 lines (132 loc) · 5.3 KB
/
guide.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
'use strict'
Promise.all([
_getGuideSection(),
_getCollections(),
_getSubscriptions()
]).then(valueArr => {
var guideSection = valueArr[0]
var collections = valueArr[1]
var subscriptions = valueArr[2]
var _getSubscriptionCount = function(id) {
var guideItem = document.getElementById(id+'-guide-item')
return guideItem.querySelector('span.no-count') ? 0 : parseInt(guideItem.querySelector('.guide-count-value').textContent)
}
var _getCollectionCount = function(id) {
return Object.keys(subscriptions).filter(k => {
return (_getSubscriptionGuideItem(k) !== null)
&& (subscriptions[k] == id)
})
.map(k => _getSubscriptionCount(k)).reduce((p,c) => p+c,0)
}
var _updateCollectionCount = function(id) {
var count = Array.prototype.slice.call(_getCollectionGuideItem(id)
.querySelector('ul').querySelectorAll('.guide-item'))
.map(e => {
return e.querySelector('span.no-count') ? 0 : parseInt(e.querySelector('.guide-count-value').textContent)
}).reduce((p,c) => p+c,0)
_getCollectionGuideItem(id).querySelector('.guide-count-value').textContent = count
}
var _getCollectionGuideItem = function(id) {
return document.getElementById(id+'-collection-guide-item')
}
var _getSubscriptionGuideItem = function(id) {
return document.getElementById(id+'-guide-item')
}
var _renderGuideCollectionItem = function(id,name) {
return template.render('guide-section-item',{
id: id,
name: name,
count: function(count) { return count === 0 ? '':count; }(_getCollectionCount(id))
}).then(html => {
var elem = document.createElement('li')
document.querySelector('#guide-collection-list .guide-channels-list').appendChild(elem)
elem.outerHTML = html
Object.keys(subscriptions).filter(key => {
return (_getSubscriptionGuideItem(key) !== null) &&
(subscriptions[key] === id)
}).forEach(key => {
_cloneSubscriptionItem(key,id)
})
})
}
var _cloneSubscriptionItem = function(subId,collectionId) {
var newNode = _getSubscriptionGuideItem(subId).cloneNode(true)
newNode.removeAttribute('id')
var thumb = newNode.querySelector('img')
var imgSrc = thumb.dataset.thumb
if(imgSrc !== undefined) {
thumb.setAttribute('src',thumb.dataset.thumb)
}
document.getElementById(collectionId+'-guide-channel-list').appendChild(newNode)
}
var _updateCollectionListHeight = function(id) {
var elem = _getCollectionGuideItem(id)
if(elem.classList.contains('collection-list-open')) {
var height = 28
var id = elem.querySelector('.collection-item a').dataset.collectionId
var listNode = document.getElementById(id+'-guide-channel-list')
height += listNode.offsetHeight
elem.style.maxHeight = height+'px'
}
}
template.render('guide-section',{ title:'Collections' }).then(html => {
var node = document.createElement('li')
guideSection.parentNode.insertBefore(node,guideSection)
node.outerHTML = html
return document.querySelector('#guide-collection-list .guide-channels-list')
}).then(node => {
node.addEventListener('click',e => {
if(e.target.closest('.collection-item a').hasAttribute('data-collection-id')) {
var height = 28
if(e.target.closest('.collection-item').classList.toggle('collection-list-open')) {
var id = e.target.closest('.collection-item a').dataset.collectionId
var listNode = document.getElementById(id+'-guide-channel-list')
height += listNode.offsetHeight
}
e.target.closest('.collection-item').style.maxHeight = height+'px'
}
})
return document.querySelector('#guide-collection-list .guide-channels-list')
}).then(node => {
return Object.keys(collections).map(id => {
_renderGuideCollectionItem(id,collections[id].name)
})
})
window.addEventListener('message',event => {
if (event.source != window) {
return
}
if (event.data.type) {
switch(event.data.type) {
case 'SUBSCRIPTION_ADDED':
_cloneSubscriptionItem(event.data.subscriptionId,event.data.collectionId)
_updateCollectionListHeight(event.data.collectionId)
_updateCollectionCount(event.data.collectionId)
break;
case 'SUBSCRIPTION_REMOVED':
var elem = _getCollectionGuideItem(event.data.collectionId)
if(elem) {
elem.querySelector('a[data-external-id="'+event.data.subscriptionId+'"]').closest('li').remove()
_updateCollectionCount(event.data.collectionId)
}
break;
case 'SUBSCRIPTION_UPDATED':
_getCollectionGuideItem(event.data.oldValue)
.querySelector('a[data-external-id="'+event.data.subscriptionId+'"]').closest('li').remove()
_cloneSubscriptionItem(event.data.subscriptionId,event.data.newValue)
_updateCollectionListHeight(event.data.newValue)
_updateCollectionCount(event.data.newValue)
_updateCollectionCount(event.data.oldValue)
break;
case 'COLLECTION_ADDED':
_renderGuideCollectionItem(event.data.id,event.data.name)
break;
case 'COLLECTION_REMOVED':
_getCollectionGuideItem(event.data.id).remove()
break;
case 'COLLECTION_UPDATED':
break;
}
}
})
})