-
Notifications
You must be signed in to change notification settings - Fork 61
/
sampleCarousel.ts
131 lines (117 loc) · 3.83 KB
/
sampleCarousel.ts
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
import { Button } from "@hpcc-js/common";
import { Carousel, ChartPanel } from "@hpcc-js/layout";
import { text } from "d3-fetch";
// @ts-ignore
import * as config from "https://raw.githack.com/hpcc-systems/Visualization/master/demos/gallery/src-umd/config.js";
import { Sample } from "./sample.js";
const samplePath = "https://raw.githack.com/hpcc-systems/Visualization/master/demos/gallery/";
export class SampleCarousel extends ChartPanel {
_samples = [];
_sampleWidgets = [];
_sampleCarousel = new Carousel();
_prevButton = new Button()
.faChar("fa-step-backward")
.on("click", () => {
this.decIdx();
this.renderSample();
});
_playPauseButton = new Button()
.faChar("fa-pause")
.on("click", () => {
this._playPauseButton.faChar(this.isPaused() ? "fa-pause" : "fa-play").lazyRender();
});
_nextButton = new Button()
.faChar("fa-step-forward")
.on("click", () => {
this.incIdx();
this.renderSample();
});
constructor() {
super();
this.visit(config.samples);
this.shuffle();
this._sampleWidgets = [];
this._sampleCarousel.widgets(this._sampleWidgets);
this
.buttons([this._prevButton, this._playPauseButton, this._nextButton])
.titleFontSize(16)
.widget(this._sampleCarousel)
;
}
isPaused() {
return this._playPauseButton.faChar() === "fa-play";
}
shuffle() {
for (let i = this._samples.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this._samples[i], this._samples[j]] = [this._samples[j], this._samples[i]];
}
}
visit(item) {
if (item.type === "folder") {
item.children.forEach(item => this.visit(item));
} else if (item.type === "file") {
this._samples.push(samplePath + item.path);
}
}
renderSample() {
let retVal;
if (this._sampleWidgets[this._idx]) {
this._sampleCarousel
.active(this._idx)
;
retVal = Promise.resolve();
} else {
retVal = text(this._samples[this._idx]).then(js => {
this._sampleWidgets[this._idx] = new Sample().data([["", js]]);
this._sampleCarousel
.widgets(this._sampleWidgets)
.active(this._idx)
;
});
}
return retVal.then(() => {
this
.title(this._samples[this._idx].substring((samplePath + "./samples/").length))
.lazyRender()
;
});
}
_idx = -1;
incIdx(wrap = false) {
if (++this._idx >= this._samples.length) {
this._idx = wrap ? 0 : this._samples.length - 1;
}
this._prevButton.enabled(this._idx !== 0).lazyRender();
this._nextButton.enabled(this._idx !== this._samples.length - 1).lazyRender();
}
decIdx() {
if (--this._idx < 0) {
this._idx = 0;
}
this._prevButton.enabled(this._idx !== 0).lazyRender();
this._nextButton.enabled(this._idx !== this._samples.length - 1).lazyRender();
}
loadRandom(): Promise<void> {
if (!this.isPaused()) {
this.incIdx(true);
return this.renderSample().then(() => {
setTimeout(() => {
this.loadRandom();
}, 3000);
});
} else {
setTimeout(() => {
this.loadRandom();
}, 3000);
}
}
enter(domNode, element) {
super.enter(domNode, element);
this.height(240);
this.scale(0.5);
setTimeout(() => {
this.loadRandom();
}, 1000);
}
}