forked from adikus/jnovel-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (106 loc) · 3.72 KB
/
app.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
import Navigation from "./components/Navigation.js"
import Feed from "./pages/Feed.js";
import Part from "./pages/Part.js";
import Series from "./pages/Series.js"
import Events from "./pages/Events.js"
import api from "./services/api.js"
import sharedStore from "./services/sharedStore.js";
import ReadingList from "./services/ReadingList.js";
import EventParser from "./services/EventParser.js";
const routes = [
{ path: '/series', component: Series },
{ path: '/series/:filter', component: Series },
{ path: '/series/:serieId/part/:id', component: Part },
{ path: '/feed', component: Feed },
{ path: '/feed/:filter', component: Feed },
{ path: '/coming-up', component: Events },
{ path: '/coming-up/:filter', component: Events }
];
const router = new VueRouter({
routes
});
new Vue({
components: {
Navigation
},
data: {
api,
sharedStore,
readingList: new ReadingList(sharedStore, api),
eventParser: new EventParser(sharedStore),
noSleep: new NoSleep()
},
router,
created() {
this.sharedStore.retrieveUser();
this.sharedStore.retrievePreferences();
if(this.$route.path === '/') {
router.push('/series');
}
this.loadSeries();
this.loadFeed();
this.loadEvents();
this.loadUserDetails();
},
computed: {
isUserAuthValid() {
let now = new Date();
return this.sharedStore.user && this.sharedStore.user.auth &&
this.sharedStore.user.auth.authToken && this.sharedStore.user.auth.authExpiresAt &&
new Date(this.sharedStore.user.auth.authExpiresAt).getTime() > now.getTime();
},
},
methods: {
async loadSeries() {
let response = await this.api.loadSeries();
this.sharedStore.series.push.apply(this.sharedStore.series, response.data);
},
async loadFeed() {
let response = await this.$root.api.loadFeed();
this.sharedStore.feed.push.apply(this.sharedStore.feed, response.data);
},
async loadEvents() {
let response = await this.$root.api.loadEvents();
let predicate = event => event.details.toLowerCase().indexOf('release of part') > -1;
this.sharedStore.events.push.apply(this.sharedStore.events, response.data.filter(predicate));
},
async loadUserDetails() {
if(this.isUserAuthValid) {
let userDetailsResponse = await this.$root.api.loadUserDetails(this.$root.sharedStore.user.userId);
this.$root.sharedStore.setUserDetails(userDetailsResponse.data);
await this.readingList.updateFromUserDetails();
}
},
fuseSearchFilter(items, search) {
const options = {
shouldSort: false,
threshold: 0.3,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [
"title",
"titleShort",
"titleOriginal",
"author",
"illustrator",
"translator",
"editor",
"tags"
]
};
let fuse = new Fuse(items, options);
return fuse.search(search);
},
handleFilterChange(filter) {
if(filter) {
this.$root.sharedStore.preferredFilter = filter;
this.$root.sharedStore.savePreferences();
return filter;
} else {
return filter || 'all';
}
}
}
}).$mount('#app');