forked from aimacode/aima-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
globalHelpers.js
81 lines (77 loc) · 2.58 KB
/
globalHelpers.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
/**
* Class to create an animation controller.
* It creates the 'previous' button, 'next' button, slider and also attaches
event listeners to them.
* @example <caption>How to use AnimationController</caption>
* <div class = 'row' id = 'someAC'></div>
* <script>
* let ac = new AnimationController({
selector : '#someAC',
min : 0,
max : 15,
renderer : (n) => {
//Renders nth frame
}
});
* </script>
*/
class AnimationController {
/**
* Create Animation Controller.
* @param {Object} options - Options for the controller
* @param {String} options.selector - A selector to target the wrapper div for the controller.
* @param {Number} options.min - Minimum value for the slider. Defaults to 0
* @param {Number} options.max - Maximum value for the slider.
* @param {Function} options.renderer - A function that can take the slider value and render the frame.
*/
constructor(options) {
//Initializing options
this.selector = options.selector;
this.min = options.min || 0;
this.max = options.max;
this.renderer = options.renderer;
//Create Elements
this.root = d3.select(this.selector);
//Empty the root element
this.root.selectAll('*').remove();
this.prevButton = this.root.append('div')
.attr('class', 'btn btn-default ACPrev col-md-2')
.text('Previous');
this.slider = this.root.append('div')
.attr('class', 'form-group col-md-6')
.style('margin-top', '1%')
.append('input')
.attr('type', 'range')
.attr('name', 'rangeInput')
.attr('step', 1)
.attr('min', this.min)
.attr('max', this.max);
this.slider.property('value', this.min);
this.nextButton = this.root.append('div')
.attr('class', 'btn btn-default ACNext col-md-2')
.text('Next');
//Bind Event Listeners
this.prevButton.on('click', () => {
let value = parseInt(this.slider.property('value'));
value = (value - 1 < 0) ? value : value - 1;
this.slider.property('value', value);
this.renderer(value);
});
this.slider.on('change input', () => {
let value = parseInt(this.slider.property('value'));
this.renderer(value);
});
this.nextButton.on('click', () => {
let value = parseInt(this.slider.property('value'));
value = (value + 1 > this.max) ? value : value + 1;
this.slider.property('value', value);
this.renderer(value);
});
}
/**
* Renders the first frame
*/
renderFirst() {
this.renderer(this.min);
}
}