-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.js
More file actions
68 lines (57 loc) · 2.33 KB
/
history.js
File metadata and controls
68 lines (57 loc) · 2.33 KB
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
/**
* This class manages the browsing history between the items the useer visits.
* When users chooses to display an item's information dialog, a new element is added to the history.
* The user can browse back and forth from within the item-info-dialog by pressing the Prev and Next buttons.
*/
class History {
/**
* initializes the array containing the UUIDs of the visited items, the pointer to the array and the maximum size of the array.
*/
constructor() {
this.HistoryUUIDs = []; // The UUIDs of the items the user has recently visited (=displayed their information dialog). Works in conjuction with HistoryPointer
this.HistoryPointer = -1;
this.HistorySize = 50;
}
getCurrent() {
if( this.HistoryPointer >= 0 && this.HistoryPointer < this.HistoryUUIDs.length ) {
return this.HistoryUUIDs[this.HistoryPointer];
} else {
return "";
}
}
getNum_of_HistoryElements() {
return this.HistoryUUIDs.length;
}
Add( UUID ) {
if( this.HistoryPointer == this.HistoryUUIDs.length - 1 ) { // user is currently at the last history element, so add the new history element
if( this.HistoryUUIDs[ this.HistoryUUIDs.length - 1 ] != UUID ) { // check if this is a new item or the same again
this.HistoryUUIDs.push( UUID );
this.HistoryPointer = this.HistoryUUIDs.length - 1;
}
} else { // user is currently at an intermediate history element, so erase the items at the right of the pointer and add the new history element
if( this.HistoryUUIDs[ this.HistoryPointer ] != UUID ) { // check if this is a new item or the same again
var num_of_elements_to_delete = this.HistoryUUIDs.length - this.HistoryPointer - 1;
this.HistoryUUIDs.splice(this.HistoryUUIDs.length - num_of_elements_to_delete, num_of_elements_to_delete);
this.HistoryUUIDs.push( UUID );
this.HistoryPointer = this.HistoryUUIDs.length - 1;
}
}
// keep size in limits
if( this.HistoryUUIDs.length > this.HistorySize ) {
this.HistoryUUIDs.shift();
this.HistoryPointer = this.HistoryUUIDs.length - 1;
}
}
Back() {
if( this.HistoryPointer - 1 >= 0 ) {
this.HistoryPointer -= 1;
}
return this.getCurrent();
}
Forward() {
if( this.HistoryPointer + 1 < this.HistoryUUIDs.length ) {
this.HistoryPointer += 1;
}
return this.getCurrent();
}
}