|
| 1 | +var ElapsedTimeBar = { |
| 2 | + // default value |
| 3 | + barColor: 'rgb(200,0,0)', |
| 4 | + pausedBarColor: 'rgba(200,0,0,.6)', |
| 5 | + |
| 6 | + isPaused: false, |
| 7 | + isFinished: false, |
| 8 | + |
| 9 | + allottedTime: null, |
| 10 | + timeProgressBar: null, |
| 11 | + startTime: null, |
| 12 | + pauseTime: null, |
| 13 | + pauseTimeDuration: 0, |
| 14 | + |
| 15 | + /** |
| 16 | + * initialize elements |
| 17 | + */ |
| 18 | + handleReady() { |
| 19 | + var config = Reveal.getConfig(); |
| 20 | + |
| 21 | + // activate this plugin if config.allottedTime exists. |
| 22 | + if (!config.allottedTime) { |
| 23 | + console.warn('Failed to start ElapsedTimeBar plugin. "allottedTime" property is required.'); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // set configurations |
| 28 | + this.barColor = config.barColor || this.barColor; |
| 29 | + this.pausedBarColor = config.pausedBarColor || this.pausedBarColor; |
| 30 | + |
| 31 | + // calc barHeight from config.barHeight or page-progress container |
| 32 | + var barHeight; |
| 33 | + var pageProgressContainer = document.querySelector('.progress'); |
| 34 | + if (config.progressBarHeight) { |
| 35 | + barHeight = parseInt(config.progressBarHeight, 10) + 'px'; |
| 36 | + |
| 37 | + // override height of page-progress container |
| 38 | + pageProgressContainer && (pageProgressContainer.style.height = barHeight); |
| 39 | + } else if (config.progress && pageProgressContainer) { |
| 40 | + // get height from page-progress container |
| 41 | + barHeight = pageProgressContainer.getBoundingClientRect().height + 'px'; |
| 42 | + } else { |
| 43 | + // default |
| 44 | + barHeight = '3px'; |
| 45 | + } |
| 46 | + |
| 47 | + // create container of time-progress |
| 48 | + var timeProgressContainer = document.createElement('div'); |
| 49 | + timeProgressContainer.classList.add('progress'); |
| 50 | + Object.entries({ |
| 51 | + display: 'block', |
| 52 | + position: 'fixed', |
| 53 | + bottom: config.progress ? barHeight : 0, |
| 54 | + width: '100%', |
| 55 | + height: barHeight |
| 56 | + }).forEach(([k, v]) => { |
| 57 | + timeProgressContainer.style[k] = v; |
| 58 | + }); |
| 59 | + document.querySelector('.reveal').appendChild(timeProgressContainer); |
| 60 | + |
| 61 | + // create content of time-progress |
| 62 | + this.timeProgressBar = document.createElement('div'); |
| 63 | + Object.entries({ |
| 64 | + height: '100%', |
| 65 | + willChange: 'width' |
| 66 | + }).forEach(([k, v]) => { |
| 67 | + this.timeProgressBar.style[k] = v; |
| 68 | + }); |
| 69 | + timeProgressContainer.appendChild(this.timeProgressBar); |
| 70 | + |
| 71 | + // start timer |
| 72 | + this.start(config.allottedTime); |
| 73 | + }, |
| 74 | + |
| 75 | + /** |
| 76 | + * update repeatedly using requestAnimationFrame. |
| 77 | + */ |
| 78 | + loop() { |
| 79 | + if (this.isPaused) return; |
| 80 | + var now = +new Date(); |
| 81 | + var elapsedTime = now - this.startTime - this.pauseTimeDuration; |
| 82 | + if (elapsedTime > this.allottedTime) { |
| 83 | + this.timeProgressBar.style.width = '100%'; |
| 84 | + this.isFinished = true; |
| 85 | + } else { |
| 86 | + this.timeProgressBar.style.width = elapsedTime / this.allottedTime * 100 + '%'; |
| 87 | + requestAnimationFrame(this.loop.bind(this)); |
| 88 | + } |
| 89 | + }, |
| 90 | + |
| 91 | + /** |
| 92 | + * set color of progress bar |
| 93 | + */ |
| 94 | + setBarColor() { |
| 95 | + if (this.isPaused) { |
| 96 | + this.timeProgressBar.style.backgroundColor = this.pausedBarColor; |
| 97 | + } else { |
| 98 | + this.timeProgressBar.style.backgroundColor = this.barColor; |
| 99 | + } |
| 100 | + }, |
| 101 | + |
| 102 | + /** |
| 103 | + * start(reset) timer with new allotted time. |
| 104 | + * @param {number} allottedTime |
| 105 | + * @param {number} [elapsedTime=0] |
| 106 | + */ |
| 107 | + start(allottedTime, elapsedTime = 0) { |
| 108 | + this.isFinished = false; |
| 109 | + this.isPaused = false; |
| 110 | + this.allottedTime = allottedTime; |
| 111 | + this.startTime = +new Date() - elapsedTime; |
| 112 | + this.pauseTimeDuration = 0; |
| 113 | + this.setBarColor(); |
| 114 | + this.loop(); |
| 115 | + }, |
| 116 | + |
| 117 | + reset() { |
| 118 | + this.start(this.allottedTime); |
| 119 | + }, |
| 120 | + |
| 121 | + pause() { |
| 122 | + if (this.isPaused) return; |
| 123 | + this.isPaused = true; |
| 124 | + this.pauseTime = +new Date(); |
| 125 | + this.setBarColor(); |
| 126 | + }, |
| 127 | + |
| 128 | + resume() { |
| 129 | + if (!this.isPaused) return; |
| 130 | + |
| 131 | + // add paused time duration |
| 132 | + this.isPaused = false; |
| 133 | + this.pauseTimeDuration += new Date() - this.pauseTime; |
| 134 | + this.pauseTime = null; |
| 135 | + this.setBarColor(); |
| 136 | + this.loop(); |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +if (Reveal.isReady()) { |
| 141 | + ElapsedTimeBar.handleReady(); |
| 142 | +} else { |
| 143 | + Reveal.addEventListener('ready', () => ElapsedTimeBar.handleReady()); |
| 144 | +} |
0 commit comments