-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLocationFinder.js
134 lines (109 loc) · 3.92 KB
/
LocationFinder.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
(function LocationFinder() {
'use strict';
/* globals Story, Config, State */
class LocationFinder {
constructor(onChange = null, classNamePrefix = 'location-', eventHandlers = null) {
this.markers = [];
// we don't really do lookUp, but it's the only way to iterate over all passages
Story.lookupWith((passage) => {
const marker = LocationFinder.extractLocations(passage.tags);
if (marker) {
this.markers.push(marker);
}
});
if (Config.debug) {
console.info(
`Locations detected:
${this.markers.map((marker) => `\t${marker}`).join('\n')}`
);
}
this.latestLocation = this.detectLocation();
this.$doc = jQuery(document);
this.$html = jQuery(document.documentElement);
this.onChange = onChange;
this.classNamePrefix = classNamePrefix;
this._listenPassageStart();
if (eventHandlers) {
this._processHandlers(eventHandlers);
}
this._toggleHtmlClass(this.latestLocation);
}
_processHandlers(eventHandlers) {
Object.keys(eventHandlers).forEach((eventName) => {
this.$doc.on(eventName, (event) => {
eventHandlers[eventName](this.detectLocation(), event);
});
});
}
_onChange(newLocation, oldLocation) {
if (Config.debug) {
console.info(`Location "${oldLocation}" changed to "${newLocation}"`);
}
if (this.onChange) {
this.onChange(newLocation, oldLocation);
}
this._toggleHtmlClass(newLocation, oldLocation);
}
_listenPassageStart() {
this.$doc.on(':passagestart', () => {
const newLocation = this.detectLocation();
if (newLocation !== this.latestLocation) {
this._onChange(newLocation, this.latestLocation);
this.latestLocation = newLocation;
};
});
}
_toggleHtmlClass(newLocation, oldLocation) {
if (this.classNamePrefix) {
this.$html
.removeClass(this.classNamePrefix + oldLocation)
.addClass(this.classNamePrefix + newLocation);
}
}
/**
* Iterates over history detecting latest visited location, or defaults to the first one found in story.
*
* @returns {string}
*/
detectLocation() {
let counter = State.length - 1;
while (counter > 0) {
const moment = State.index(counter);
const passage = Story.get(moment.title);
for (const tag of passage.tags) {
if (tag.startsWith(LocationFinder.nameToken)) {
return LocationFinder.getLocationNameFromTag(tag);
}
}
counter--;
}
return this.markers[0];
}
/**
* @param {string[]} tags
* @returns {string}
*/
static extractLocations(tags) {
const locationTag = tags.find(tag => tag.startsWith(LocationFinder.nameToken));
if (locationTag) {
return LocationFinder.getLocationNameFromTag(locationTag);
}
}
/**
* @param {string} tag
* @return {string}
*/
static getLocationNameFromTag(tag) {
return tag.substring(LocationFinder.nameToken.length);
}
static get nameToken() {
return 'locationName-';
}
}
window.scUtils = Object.assign(
window.scUtils || {},
{
LocationFinder,
}
);
}());