This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
272 lines (249 loc) · 5.78 KB
/
script.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
(function($){
var postId;
var next;
var last;
var finished = false;
var data = [];
var ojs = {};
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}
function getUrlParams(){
var params = location.href.split('?');
return !params[1]? {}: params[1].split('&')
.reduce((params, param) => {
let [ key, value ] = param.split('=');
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
return params;
}, { })
}
function getData(){
if(!postId) return Promise.reject('no postId');
last = next;
var url = 'https://share.jodel.com/post/' + postId + '/replies?ojFilter=true'
if(next)
url += "&next=" + next;
return $.get(url)
.then(transformData)
}
function transformData(res){
// save reference to next batch of replies
if(res.next){
next = res.next
} else {
finished = true
}
// match important data in html string and save to objects
// this is way faster than parsing the string to a dom representation
var regex = /<.*(oj-text|time-text|post-message|votes).*>(.*)<.*>/g;
var matches;
var lastObj = {};
while ((matches = regex.exec(res.html)) !== null) {
switch(matches[1]){
case "oj-text":
lastObj.ojId = matches[2].toUpperCase();
// map post to oj
if(Array.isArray(ojs[lastObj.ojId])){
ojs[lastObj.ojId].push(lastObj);
} else {
ojs[lastObj.ojId] = [lastObj];
}
break;
case "time-text":
lastObj.time = matches[2];
break;
case "post-message":
lastObj.references = [];
// check for reference to other poster
lastObj.message = matches[2].replace(/@(\w+)/g, (match, m1)=>{
lastObj.references.push(m1.toUpperCase());
//if found, make clickable
return '<span class="oj-link">' + match.toUpperCase() + '</span>'
})
break;
default:
lastObj.id = data.length;
lastObj.votes = matches[2];
// filters out image posts atm
if(lastObj.message){
data.push(lastObj);
}
lastObj = {};
break;
}
}
}
//define global event bus
const EventBus = new Vue()
Object.defineProperties(Vue.prototype, {
$bus: {
get: function () {
return EventBus
}
}
})
// filter component
Vue.component('state', {
props: ['state'],
methods: {
back: function(){
this.$bus.$emit('back');
}
},
template: '#state-template'
})
// post component
Vue.component('post', {
props: ['post'],
methods: {
filterOj: function(){
this.$bus.$emit('filter-oj', this.post.ojId);
},
thread: function($event){
// if clicked on reference -> show possible questions by the @mention user
if($($event.target).is('.oj-link')){
// set reference based on eventTarget as there might be multiple
var reference = $event.target.innerText.slice(1).toUpperCase();
this.$bus.$emit('thread', {
pinned: this.post,
filter: reference,
refIsAnswer: true,
name: "Possible questions by User " + reference
});
// if clicked on post in general, find answers from OJ
} else if(this.post.ojId != 'OJ' && !$($event.target).is('.oj')){
this.$bus.$emit('thread', {
pinned: this.post,
filter: {
ojToId: this.post.ojId
},
name: "Answers from OJ"
});
}
}
},
template: '#post-template'
})
// thread component
Vue.component('reference-post', {
props: ['post'],
methods: {
setPadding: function(){
this.$nextTick(function () {
var height = $('#reference-post').height()
height = height ? height + 14 : 0
$('#app').css('padding-bottom', height)
})
}
},
updated: function(){
this.setPadding()
},
template: '#reference-post-template'
})
var app = new Vue({
el: '#app',
data: {
states: [{
filter: null,
pinned: null,
scroll: 0
}],
loading: false,
finished: false,
input: false
},
computed: {
currentState: function () {
return this.states[this.states.length - 1]
},
showLoadMore: function(){
return !this.finished && !this.loading && this.currentState.name
}
},
methods:{
saveScroll: function(){
// save scroll position in original view
this.currentState.scroll = $(window).scrollTop()
},
reScroll: function(){
this.$nextTick(function () {
// re-set scroll position in original view
$(window).scrollTop(this.currentState.scroll)
})
},
loadMore: function(){
if(this.loading) return
this.loading = true
getData().then(()=>{
this.loading = false
this.finished = finished
});
},
posts: function(){
var filter = this.currentState.filter
if(typeof filter == "string")
{
return ojs[filter];
} else if(typeof filter =="object"
&& filter !== null
&& typeof filter.ojToId == "string")
{
return ojs.OJ.filter(post=>{
return post.references && post.references.indexOf(filter.ojToId) > -1
})
} else
{
return data
}
}
},
created() {
var params = getUrlParams();
if(params.postId){
postId = params.postId;
// get initial data
this.loadMore()
} else {
this.input = true
}
// endless scroll
$(window).on("scroll", debounce(() =>{
if(finished) return
var scrollLeft = $(document).height() - $(window).scrollTop();
var windowHeight = $(window).outerHeight();
if (scrollLeft <= 2 * windowHeight) {
this.loadMore()
}
}, 250));
// event listener for filter-oj
this.$bus.$on('filter-oj', (ojId) => {
this.saveScroll();
this.states.push({
scroll: 0,
filter: ojId,
name: "Posts by " + ojId
})
this.reScroll();
})
//event listener for thread
this.$bus.$on('thread', (state) => {
this.saveScroll();
state.scroll = 0
this.states.push(state)
this.reScroll();
})
this.$bus.$on('back', () => {
this.states.pop();
this.reScroll();
})
}
})
})(jQuery);