-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.html
219 lines (209 loc) · 8.23 KB
/
demo.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Progress timer demo</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
#log {
max-height: 30em;
}
.progress .progress-bar {
-webkit-transition: none;
-moz-transition: none;
-ms-transition: none;
-o-transition: none;
transition: none;
}
.panel-heading {
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>Media progress timer</h1>
<div>
<span id="duration" class="pull-right"></span>
<span id="position"></span>
<div class="progress">
<div id="progress" class="progress-bar"></div>
</div>
</div>
<p>
Using the controls below you'll be able to experiment with the
<code>ProgressTimer</code> API right here in your browser.
For more usage info see <a target="_parent" href="https://github.com/adamcik/media-progress-timer">
https://github.com/adamcik/media-progress-timer</a>.
</p>
<pre id="log" class="well"></pre>
<form id="controls">
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">Create a new instance</div>
<div class="panel-body">
<div class="form-group">
<label>Fallback frame rate</label>
<input name="fallbackTargetFrameRate" value="30" class="form-control" />
<span class="help-block">
Sets the target FPS for the <code>setTimeout</code> fallback mode.
</span>
</div>
<div class="form-group">
<label>
Disable <abbr title="requestAnimationFrame">RAF</abbr>
</label>
<select class="form-control" name="disableRAF">
<option value="false">false</option>
<option value="true">true</option>
</select>
<span class="help-block">
Forces the use of <code>setTimout</code> based fallback code
on browsers that don't support <code>requestAnimationFrame</code>
(intended for testing).
</span>
</div>
<div class="form-group">
<div>
<button name="action" value="initialize" class="btn btn-default">
Re-initialize
</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">Call methods on existing instance</div>
<div class="panel-body">
<div class="form-group">
<label>Position</label>
<input name="position" value="0" class="form-control" />
<span class="help-block">
Current position of timer in milliseconds, used in <code>set</code>
calls.
</span>
</div>
<div class="form-group">
<label>Duration</label>
<input name="duration" value="60000" class="form-control" />
<span class="help-block">
Current duration of timer in milliseconds, used in <code>set</code>
calls. May be blank, in which case the previously configured
duration will be used.
</span>
</div>
<div class="form-group">
<div class="btn-group pull-right">
<button name="action" value="start" class="btn btn-default">Start</button>
<button name="action" value="stop" class="btn btn-default">Stop</button>
<button name="action" value="reset" class="btn btn-default">Reset</button>
</div>
<button name="action" value="set" class="btn btn-default">Set</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<script type="text/javascript" src="timer.js"></script>
<script type="text/javascript">
var timer;
var logger = createLogger(document.getElementById('log'));
var form = document.getElementById('controls');
var progressElement = document.getElementById('progress');
var positionNode = document.createTextNode('00:00');
var durationNode = document.createTextNode('00:00');
document.getElementById('position').appendChild(positionNode);
document.getElementById('duration').appendChild(durationNode);
function callback(position, duration) {
positionNode.nodeValue = format(position);
durationNode.nodeValue = format(duration || 0);
if (duration > 0) {
progressElement.style.width = ((position/duration)*100) + '%';
} else {
progressElement.style.width = null;
}
}
form.addEventListener('click', function(event) {
if (event.preventDefault) {
event.preventDefault();
}
if (event.target.name == 'action') {
var inputs = this.elements;
switch (event.target.value) {
case 'start':
logger.call(timer, 'start');
break;
case 'stop':
logger.call(timer, 'stop');
break;
case 'set':
var position = toInt(inputs['position'].value);
var duration = toInt(inputs['duration'].value);
logger.call(timer, 'set', duration === null ?
[position] : [position, duration]);
break;
case 'reset':
logger.call(timer, 'reset');
break;
case 'initialize':
if (timer) {
logger.reset();
timer.reset();
}
var fallbackTargetFrameRate = toInt(inputs['fallbackTargetFrameRate'].value);
var disableRAF = inputs['disableRAF'].value === 'true';
logger.log('var timer = new ProgressTimer({\n' +
' callback: function(position, duration) { /* UI code */ },\n' +
' disableRequestAnimationFrame: ' + disableRAF + ',\n' +
' fallbackTargetFrameRate: ' + fallbackTargetFrameRate + '\n' +
'});');
timer = new ProgressTimer({
callback: callback,
fallbackTargetFrameRate: fallbackTargetFrameRate,
disableRequestAnimationFrame: disableRAF
});
break;
}
}
return false;
});
form.querySelector('button[value=initialize]').click();
form.querySelector('button[value=set]').click();
form.querySelector('button[value=start]').click();
function createLogger(element) {
return {
call: function(timer, name, args) {
args = typeof args === 'undefined' ? [] : args;
this.log('timer.' + name + '(' + args.join(', ') + ');');
timer[name].apply(timer, args);
},
log: function(text) {
element.textContent += text + '\n';
element.scrollTop = element.scrollHeight;
},
reset: function() {
element.textContent = ''
}
};
}
function toInt(value) {
return value.match(/^\w*\d+\w*$/) ? parseInt(value) : null;
}
function format(milliseconds) {
if (milliseconds === Infinity) {
return '00:00';
}
var seconds = Math.floor(milliseconds / 1000);
var minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
return minutes + ':' + seconds;
}
</script>
</body>
</html>