-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaders-data.class.js
89 lines (76 loc) · 2.78 KB
/
readers-data.class.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
//=============================================================================
//
// File: /node_modules/rwt-reading-points/readers-data.class.js
// Language: ECMAScript 2015
// Copyright: Read Write Tools © 2020
// License: MIT
// Initial date: Jan 14, 2020
// Contents: An object containing a list of this visitor's pages
// suitable for storing in browser localStorage
//
//=============================================================================
import ReadersItem from './readers-item.class.js';
export default class ReadersData {
static localStorageKey() {
return 'readers-data';
}
constructor() {
this.readingTime = 0;
this.pointsPossible = 0;
this.pointsObtained = 0;
this.pagesVisited = 0;
this.pagesRead = 0;
this.itemsMap = new Map(); // filePath => ReadersItem
Object.seal(this);
}
static exists() {
return localStorage.getItem(ReadersData.localStorageKey()) != null;
}
// Read details and accumulate totals
//< true if localStorage exists
//< false if localStorage does not exist
readFromStorage() {
var json = localStorage.getItem(ReadersData.localStorageKey());
if (json) {
var anonymousMap = new Map(JSON.parse(json));
// cast the items of the map to be ReadersItems
for (let [itemKey, anonymousObj] of anonymousMap) {
this.itemsMap.set(itemKey, new ReadersItem(anonymousObj));
this.pointsPossible += anonymousObj.skillPoints;
this.pointsObtained += (anonymousObj.skillPoints * anonymousObj.percentRead);
this.readingTime += anonymousObj.readingTime;
this.pagesVisited++
if (anonymousObj.percentRead > 0)
this.pagesRead++
}
this.pointsObtained = Math.round(this.pointsObtained);
return true;
}
else
return false;
}
// Convert data to JSON and save to localStorage
writeToStorage() {
var json = JSON.stringify(Array.from(this.itemsMap.entries()));
localStorage.setItem(ReadersData.localStorageKey(), json);
}
//^ Add a page to the list of readersItems, if it doesn't already exist
// or update it when the user revisits it
addPage(filePath, title, skillCategory, skillLevel, skillPoints, suggestedReadingTime, percentRead, readingTime) {
var readersItem = this.itemsMap.get(filePath);
// first time visting this page
if (readersItem == undefined) {
readersItem = new ReadersItem(title, skillCategory, skillLevel, skillPoints, suggestedReadingTime, percentRead, readingTime);
this.itemsMap.set(filePath, readersItem);
}
// return visit
else {
// update the percent read only if it's greater than the previous time
if (percentRead > readersItem.percentRead)
readersItem.percentRead = percentRead;
// accumulate the reading time
readersItem.readingTime += readingTime;
this.itemsMap.set(filePath, readersItem);
}
}
}