-
Notifications
You must be signed in to change notification settings - Fork 9
/
inventory.js
68 lines (55 loc) · 2.12 KB
/
inventory.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
(function inventoryUtil() {
'use strict';
// requires onPassagePresent.js
// Utility functions to manage inventory appearance.
// Inventory is displayed as PassageHeader (or PassageFooter) and consists of button that opens
// some kind of overlay. This button should have `js-toggleInventory` class. Overlay should have
// `js-inventory` class and open by assigning `open` class.
const $body = jQuery('body');
const $doc = jQuery(document);
const inventory = {
pingTimeout: 3500, // depends on how long animation is
beaconInterval: 4000,
pingClassName: 'ping-inventory',
setup({pingTimeout = this.pingTimeout, beaconInterval = this.beaconInterval, pingClassName = this.pingClassName} = {}) {
Object.assign(this, {
pingTimeout,
beaconInterval,
pingClassName,
});
},
onInventoryClick(event) {
jQuery(event.target)
.closest('.js-inventory') // we need to get this element every time
.toggleClass('open');
},
ping(className, timeout) {
$body.addClass(className);
setTimeout(() => {
$body.removeClass(className);
}, timeout);
},
hilite({className = this.pingClassName, timeout = this.pingTimeout} = {}) {
window.scUtils.onPassagePresent(() => {
this.ping(className, timeout);
});
},
beacon({className = this.pingClassName, timeout = this.pingTimeout, interval = this.beaconInterval} = {}) {
const intervalId = setInterval(() => {
this.hilite({className, timeout});
}, interval);
// stop beaconing when moved to another passage
$doc.on(':passagestart', () => {
clearInterval(intervalId);
});
return intervalId;
},
};
$body.on('click', '.js-toggleInventory', inventory.onInventoryClick);
window.scUtils = Object.assign(
window.scUtils || {},
{
inventory,
}
);
}());