|
1 | | -import React, { useEffect } from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import PropTypes from 'prop-types'; |
| 3 | +import { GlobalActions } from '../../context/actions'; |
3 | 4 | import '../../styles/ProgressBar.scss'; |
4 | 5 |
|
5 | | -function ProgressBar({ current, max }) { |
6 | | - const node = { |
7 | | - rectPrimary: document.querySelector('.mux-lpi-rect--primary'), |
8 | | - buffer: document.querySelector('.mux-lpi-buffer'), |
9 | | - }; |
10 | | - |
11 | | - function setTransform(ref, value) { |
12 | | - if (ref !== null) { |
13 | | - const { style } = ref; |
14 | | - style.transform = value; |
15 | | - style.WebkitTransform = value; |
16 | | - style.MozTransform = value; |
17 | | - style.OTransform = value; |
18 | | - style.MSTransform = value; |
| 6 | +class ProgressBar extends React.Component { |
| 7 | + constructor(props) { |
| 8 | + super(props); |
| 9 | + |
| 10 | + this.handleMouseDown = this.handleMouseDown.bind(this); |
| 11 | + this.handleMouseMove = this.handleMouseMove.bind(this); |
| 12 | + this.handleMouseUp = this.handleMouseUp.bind(this); |
| 13 | + |
| 14 | + this.lastX = null; |
| 15 | + |
| 16 | + this.max; |
| 17 | + this.current; |
| 18 | + this.viewable; |
| 19 | + this.next; |
| 20 | + this.prev; |
| 21 | + |
| 22 | + this.inner; |
| 23 | + } |
| 24 | + |
| 25 | + handleMouseDown(e) { |
| 26 | + this.handleMouseMove(e); |
| 27 | + document.addEventListener('mousemove', this.handleMouseMove); |
| 28 | + document.addEventListener('mouseup', this.handleMouseUp); |
| 29 | + } |
| 30 | + |
| 31 | + handleMouseMove(e) { |
| 32 | + e.preventDefault(); |
| 33 | + let chunkNum; |
| 34 | + |
| 35 | + // how far around mouse on X to look for viewable chunk |
| 36 | + let searchRadius = 10; |
| 37 | + let rect = this.inner.getBoundingClientRect(); |
| 38 | + let width = rect.right - rect.left; |
| 39 | + |
| 40 | + let x = e.clientX - rect.left; |
| 41 | + if (x <= 0) { |
| 42 | + chunkNum = 0; |
| 43 | + |
| 44 | + } else if (x >= width) { |
| 45 | + chunkNum = this.max - 1; |
| 46 | + |
| 47 | + } else { |
| 48 | + // translate to viewable chunk array index |
| 49 | + x = Math.round((x / width) * this.max); |
| 50 | + if (x === this.current) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // search for the closest viewable chunk in a certain radius |
| 55 | + for (let i = 0; i <= searchRadius; i++) { |
| 56 | + if (i === 0) { |
| 57 | + if (this.viewable[x]) { |
| 58 | + chunkNum = x; |
| 59 | + break; |
| 60 | + } |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + if (this.viewable[x + i]) { |
| 65 | + chunkNum = x + i; |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + if (this.viewable[x - i]) { |
| 70 | + chunkNum = x - i; |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // move to chunk |
| 77 | + if (this.viewable[chunkNum]) { |
| 78 | + if (chunkNum > this.current) { |
| 79 | + this.next({stopAt: chunkNum, playing: false}); |
| 80 | + } |
| 81 | + if (chunkNum < this.current && chunkNum !== this.max - 1) { |
| 82 | + this.prev({stopAt: chunkNum, playing: false}); |
| 83 | + } |
19 | 84 | } |
20 | 85 | } |
21 | 86 |
|
22 | | - useEffect(() => { |
23 | | - function setProgress(ref, val) { |
| 87 | + handleMouseUp(e) { |
| 88 | + document.removeEventListener('mousemove', this.handleMouseMove); |
| 89 | + document.removeEventListener('mouseup', this.handleMouseUp); |
| 90 | + } |
| 91 | + |
| 92 | + refresh() { |
| 93 | + this.forceUpdate(); |
| 94 | + } |
| 95 | + |
| 96 | + render() { |
| 97 | + const { current, max, state, dispatch } = this.props; |
| 98 | + const node = { |
| 99 | + rectPrimary: document.querySelector('.mux-lpi-rect--primary'), |
| 100 | + buffer: document.querySelector('.mux-lpi-buffer'), |
| 101 | + inner: document.querySelector('.mux-lpi-inner'), |
| 102 | + thumb: document.querySelector('.progressThumb'), |
| 103 | + }; |
| 104 | + this.inner = node.inner; |
| 105 | + |
| 106 | + this.max = max; |
| 107 | + this.current = current; |
| 108 | + this.viewable = (state.chunker !== undefined) ? |
| 109 | + state.chunker.viewable : |
| 110 | + null; |
| 111 | + |
| 112 | + this.prev = (playing) => { |
| 113 | + dispatch(GlobalActions.PREV_LINE, playing); |
| 114 | + }; |
| 115 | + |
| 116 | + this.next = (playing) => { |
| 117 | + dispatch(GlobalActions.NEXT_LINE, playing); |
| 118 | + }; |
| 119 | + |
| 120 | + function setTransform(ref, value) { |
| 121 | + if (ref !== null) { |
| 122 | + const { style } = ref; |
| 123 | + style.transform = value; |
| 124 | + style.WebkitTransform = value; |
| 125 | + style.MozTransform = value; |
| 126 | + style.OTransform = value; |
| 127 | + style.MSTransform = value; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // set the progress bar body |
| 132 | + const setProgress = (ref, val) => { |
24 | 133 | setTransform(ref, `scaleX(${val})`); |
25 | 134 | } |
26 | 135 |
|
27 | 136 | function setBuffer(ref, val) { |
28 | 137 | setTransform(ref, `scaleX(${val})`); |
29 | 138 | } |
30 | 139 |
|
| 140 | + // set the slider thumb position |
| 141 | + const setThumb = (thumb, value) => { |
| 142 | + if (thumb !== null) { |
| 143 | + const { style } = thumb; |
| 144 | + let x = 0; |
| 145 | + |
| 146 | + if (this !== undefined) { |
| 147 | + x = value * this.inner.offsetWidth; |
| 148 | + } |
| 149 | + let transform = `translateX(${x}px)`; |
| 150 | + |
| 151 | + style.transform = transform; |
| 152 | + style.WebkitTransform = transform; |
| 153 | + style.MozTransform = transform; |
| 154 | + style.OTransform = transform; |
| 155 | + style.MSTransform = transform; |
| 156 | + } |
| 157 | + } |
| 158 | + |
31 | 159 | setProgress(node.rectPrimary, parseFloat(current / max, 10)); |
32 | 160 | setBuffer(node.buffer, 1); |
33 | | - }, [node, current, max]); |
34 | | - |
35 | | - |
36 | | - return ( |
37 | | - <div role="progressbar" className="mux-lpi"> |
38 | | - <div className="progressLable" id="progressLabel"> |
39 | | - <div className="innerText"> |
40 | | - { |
41 | | - // if the user enters a valid input and clicks on LOAD |
42 | | - // the progress bar displays the percentage of progress |
43 | | - // convert the lines of code to percentge by multiplying the division by 100 |
44 | | - `Progress: ${Math.round((current / max) * 100, 2)} %` |
45 | | - // if the user does not enter a valid input, initialise the progress bar as not loaded |
46 | | - } |
| 161 | + setThumb(node.thumb, parseFloat(current / max, 10)); |
| 162 | + |
| 163 | + |
| 164 | + return ( |
| 165 | + <div |
| 166 | + role="progressbar" |
| 167 | + className="mux-lpi" |
| 168 | + tabIndex={0} |
| 169 | + onMouseDown={this.handleMouseDown} |
| 170 | + > |
| 171 | + <div className="mux-lpi-padding" /> |
| 172 | + <div className="mux-lpi-inner"> |
| 173 | + <div className="progressLable" id="progressLabel"> |
| 174 | + <div className="innerText"> |
| 175 | + { |
| 176 | + // if the user enters a valid input and clicks on LOAD |
| 177 | + // the progress bar displays the percentage of progress |
| 178 | + // convert the lines of code to percentge by multiplying the division by 100 |
| 179 | + `Progress: ${Math.round((current / max) * 100, 2)} %` |
| 180 | + // if the user does not enter a valid input, initialise the progress bar as not loaded |
| 181 | + } |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + <div className="progressThumb"> |
| 185 | + <div className="innerThumb" /> |
| 186 | + </div> |
| 187 | + <div className="mux-lpi-buffer" /> |
| 188 | + <div className="mux-lpi-rect mux-lpi-rect--primary"> |
| 189 | + <span className="mux-lpi-rect-inner" /> |
| 190 | + </div> |
47 | 191 | </div> |
48 | 192 | </div> |
49 | | - <div className="mux-lpi-buffer" /> |
50 | | - <div className="mux-lpi-rect mux-lpi-rect--primary"> |
51 | | - <span className="mux-lpi-rect-inner" /> |
52 | | - </div> |
53 | | - </div> |
54 | | - ); |
| 193 | + ); |
| 194 | + } |
55 | 195 | } |
56 | 196 |
|
57 | 197 | export default ProgressBar; |
58 | 198 | ProgressBar.propTypes = { |
59 | 199 | current: PropTypes.number.isRequired, |
60 | 200 | max: PropTypes.number.isRequired, |
| 201 | + state: PropTypes.object.isRequired, |
| 202 | + dispatch: PropTypes.func.isRequired, |
61 | 203 | }; |
0 commit comments