forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresizer.js
More file actions
72 lines (64 loc) · 1.73 KB
/
resizer.js
File metadata and controls
72 lines (64 loc) · 1.73 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
69
70
71
72
app.views.Resizer = class Resizer extends app.View {
static className = "_resizer";
static events = {
dragstart: "onDragStart",
dragend: "onDragEnd",
};
static MIN = 260;
static MAX = 600;
static isSupported() {
return "ondragstart" in document.createElement("div") && !app.isMobile();
}
init() {
this.el.setAttribute("draggable", "true");
this.appendTo($("._app"));
}
resize(value, save) {
value -= app.el.offsetLeft;
if (!(value > 0)) {
return;
}
value = Math.min(Math.max(Math.round(value), Resizer.MIN), Resizer.MAX);
const newSize = `${value}px`;
document.documentElement.style.setProperty("--sidebarWidth", newSize);
if (save) {
app.settings.setSize(value);
}
}
onDragStart(event) {
event.dataTransfer.effectAllowed = "link";
event.dataTransfer.setData("Text", "");
this.onDrag = this.onDrag.bind(this);
$.on(window, "dragover", this.onDrag);
}
onDrag(event) {
const value = event.pageX;
if (!(value > 0)) {
return;
}
this.lastDragValue = value;
if (this.rafPending) {
return;
}
this.rafPending = requestAnimationFrame(() => {
this.rafPending = null;
this.resize(this.lastDragValue, false);
});
}
onDragEnd(event) {
if (this.rafPending) {
cancelAnimationFrame(this.rafPending);
this.rafPending = null;
}
$.off(window, "dragover", this.onDrag);
let value = event.pageX || event.screenX - window.screenX;
if (
this.lastDragValue &&
!(this.lastDragValue - 5 < value && value < this.lastDragValue + 5)
) {
// https://github.com/freeCodeCamp/devdocs/issues/265
value = this.lastDragValue;
}
this.resize(value, true);
}
};