forked from honestbleeps/Reddit-Enhancement-Suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floater.js
100 lines (88 loc) · 2.74 KB
/
floater.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
/* @flow */
import _ from 'lodash';
import { $ } from '../vendor';
import * as Modules from '../core/modules';
import { Module } from '../core/module';
import { getD2xBodyOffset, getHeaderOffset, isAppType, matchesPageLocation } from '../utils';
export const module: Module<*> = new Module('floater');
module.category = 'productivityCategory';
module.moduleName = 'floaterName';
module.description = 'floaterDesc';
module.alwaysEnabled = true;
module.hidden = true;
const defaultContainer = isAppType('d2x') ? 'belowFixedNavbar' : 'visibleAfterScroll';
const containers = {
belowFixedNavbar: {
include: ['d2x'],
$element: _.once(() =>
$('<div>', { class: 'res-floater-belowNavbar' })
.append('<ul>')
),
go() {
this.$element().css('top', 5 + getD2xBodyOffset());
},
add(element: *, { separate, order }: {| separate: boolean, order: number |}) {
if (separate) {
this.$element().append(element);
} else {
const $container = $('<li />');
$container.css('order', order);
$container.append(element);
this.$element().find('> ul').append($container);
}
},
},
visibleAfterScroll: {
exclude: ['d2x'],
$element: _.once(() =>
$('<div>', { id: 'NREFloat', class: 'res-floater-visibleAfterScroll' })
.append('<ul>')
),
go() {
this.$element().css('top', 8 + getHeaderOffset(true));
if (!document.querySelector('#RESPinnedHeaderSpacer')) { // No need to hide the floater if whole header is pinned
// $FlowIssue https://github.com/facebook/flow/pull/4664
new IntersectionObserver(entries => {
this.$element().get(0).hidden = entries[0].isIntersecting;
}).observe(document.querySelector('#header'));
}
},
add(element: *, { separate, order }: {| separate: boolean, order: number |}) {
if (separate) {
this.$element().append(element);
} else {
const $container = $('<li />');
$container.css('order', order);
$container.append(element);
this.$element().find('> ul').append($container);
}
},
},
};
function isRunning(container): boolean {
return matchesPageLocation(container.include || [], container.exclude || []);
}
module.afterLoad = () => {
for (const container of Object.values(containers)) {
if (!isRunning(container)) {
continue;
}
$(document.body).append(container.$element());
if (typeof container.go === 'function') {
container.go();
}
}
};
export function addElement(
element: HTMLElement | JQuery,
{ container: containerName = defaultContainer, separate = false, order = 0 }: {| container?: $Keys<typeof containers>, separate?: boolean, order?: number |} = {}
) {
if (!Modules.isRunning(module)) {
return;
}
const container = containers[containerName];
if (!isRunning(container)) {
return;
}
container.add(element, { separate, order });
}