Save and restore params from the fragment identifier
Read location.hash when the switcher loads and use the parameters parsed out of it for the initial network conditions. Update location.hash after every simulation run completes so that it's easy to save scenarios for later re-use.
Showing
2 changed files
with
40 additions
and
3 deletions
... | @@ -85,6 +85,13 @@ | ... | @@ -85,6 +85,13 @@ |
85 | capacity. | 85 | capacity. |
86 | <button type=button class=add-time-period>Add time period</button> | 86 | <button type=button class=add-time-period>Add time period</button> |
87 | </p> | 87 | </p> |
88 | <p> | ||
89 | If you've created a complex scenario you'd like to retry | ||
90 | later, run the simulation and then save the URL of this | ||
91 | page. All of the network conditions you specify are | ||
92 | saved into the URL fragment after the results of a | ||
93 | simulation run are displayed. | ||
94 | </p> | ||
88 | The video is available at | 95 | The video is available at |
89 | <ul> | 96 | <ul> |
90 | <li><input class=bitrate type=number min=1 value=65536> bits per second</li> | 97 | <li><input class=bitrate type=number min=1 value=65536> bits per second</li> | ... | ... |
... | @@ -29,10 +29,10 @@ | ... | @@ -29,10 +29,10 @@ |
29 | 29 | ||
30 | // a dynamic number of time-bandwidth pairs may be defined to drive the simulation | 30 | // a dynamic number of time-bandwidth pairs may be defined to drive the simulation |
31 | (function() { | 31 | (function() { |
32 | var addTimePeriod = document.querySelector('.add-time-period'), | 32 | var params, |
33 | networkTimeline = document.querySelector('.network-timeline'), | 33 | networkTimeline = document.querySelector('.network-timeline'), |
34 | timePeriod = networkTimeline.querySelector('li:last-child').cloneNode(true); | 34 | timePeriod = networkTimeline.querySelector('li:last-child').cloneNode(true), |
35 | addTimePeriod.addEventListener('click', function() { | 35 | appendTimePeriod = function() { |
36 | var clone = timePeriod.cloneNode(true), | 36 | var clone = timePeriod.cloneNode(true), |
37 | count = networkTimeline.querySelectorAll('input.bandwidth').length, | 37 | count = networkTimeline.querySelectorAll('input.bandwidth').length, |
38 | time = clone.querySelector('.time'), | 38 | time = clone.querySelector('.time'), |
... | @@ -41,6 +41,31 @@ | ... | @@ -41,6 +41,31 @@ |
41 | time.name = 'time' + count; | 41 | time.name = 'time' + count; |
42 | bandwidth.name = 'bandwidth' + count; | 42 | bandwidth.name = 'bandwidth' + count; |
43 | networkTimeline.appendChild(clone); | 43 | networkTimeline.appendChild(clone); |
44 | }; | ||
45 | document.querySelector('.add-time-period') | ||
46 | .addEventListener('click', appendTimePeriod); | ||
47 | |||
48 | // apply any simulation parameters that were set in the fragment identifier | ||
49 | if (!window.location.hash) { | ||
50 | return; | ||
51 | } | ||
52 | |||
53 | // time periods are specified as t<seconds>=<bitrate> | ||
54 | // e.g. #t15=450560&t150=65530 | ||
55 | params = window.location.hash.substring(1) | ||
56 | .split('&') | ||
57 | .map(function(param) { | ||
58 | return ((/t(\d+)=(\d+)/i).exec(param) || []) | ||
59 | .map(window.parseFloat).slice(1); | ||
60 | }).filter(function(pair) { | ||
61 | return pair.length === 2; | ||
62 | }); | ||
63 | |||
64 | networkTimeline.innerHTML = ''; | ||
65 | params.forEach(function(param) { | ||
66 | appendTimePeriod(); | ||
67 | networkTimeline.querySelector('li:last-child .time').value = param[0]; | ||
68 | networkTimeline.querySelector('li:last-child input.bandwidth').value = param[1]; | ||
44 | }); | 69 | }); |
45 | })(); | 70 | })(); |
46 | 71 | ||
... | @@ -242,6 +267,11 @@ | ... | @@ -242,6 +267,11 @@ |
242 | clock.restore(); | 267 | clock.restore(); |
243 | fakeXhr.restore(); | 268 | fakeXhr.restore(); |
244 | 269 | ||
270 | // update the fragment identifier so this scenario can be re-run easily | ||
271 | window.location.hash = '#' + options.bandwidths.map(function(interval) { | ||
272 | return 't' + interval.time + '=' + interval.bandwidth; | ||
273 | }).join('&'); | ||
274 | |||
245 | done(null, results); | 275 | done(null, results); |
246 | }, 0); | 276 | }, 0); |
247 | }); | 277 | }); | ... | ... |
-
Please register or sign in to post a comment