|
| 1 | +var Fuse = require('fuse.js'); |
| 2 | +var mocks = require('../../build/test_dashboard_mocks.json'); |
| 3 | + |
| 4 | + |
| 5 | +// Our gracious testing object |
| 6 | +var Tabs = { |
| 7 | + |
| 8 | + // Return the specified plot container (or default one) |
| 9 | + getGraph: function(id) { |
| 10 | + id = id || 'graph'; |
| 11 | + return document.getElementById(id); |
| 12 | + }, |
| 13 | + |
| 14 | + // Create a new plot container |
| 15 | + fresh: function(id) { |
| 16 | + id = id || 'graph'; |
| 17 | + |
| 18 | + var graphDiv = Tabs.getGraph(id); |
| 19 | + |
| 20 | + if(graphDiv) { |
| 21 | + graphDiv.remove(); |
| 22 | + } |
| 23 | + |
| 24 | + graphDiv = document.createElement('div'); |
| 25 | + graphDiv.className = 'dashboard-plot'; |
| 26 | + graphDiv.id = id; |
| 27 | + |
| 28 | + var plotArea = document.getElementById('plots'); |
| 29 | + plotArea.appendChild(graphDiv); |
| 30 | + |
| 31 | + return graphDiv; |
| 32 | + }, |
| 33 | + |
| 34 | + // Plot a mock by name (without .json) to the default or specified container |
| 35 | + plotMock: function(mockName, id) { |
| 36 | + var mockURL = '/test/image/mocks/' + mockName + '.json'; |
| 37 | + |
| 38 | + window.Plotly.d3.json(mockURL, function(err, fig) { |
| 39 | + window.Plotly.plot(Tabs.fresh(id), fig.data, fig.layout); |
| 40 | + |
| 41 | + console.warn('Plotting:', mockURL); |
| 42 | + }); |
| 43 | + }, |
| 44 | + |
| 45 | + // Save a png snapshot and display it below the plot |
| 46 | + snapshot: function(id) { |
| 47 | + var gd = Tabs.getGraph(id); |
| 48 | + |
| 49 | + if(!gd._fullLayout || !gd._fullData) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + var image = new Image(); |
| 54 | + |
| 55 | + window.Plotly.Snapshot.toImage(gd, { format: 'png' }).on('success', function(img) { |
| 56 | + image.src = img; |
| 57 | + |
| 58 | + var imageDiv = document.getElementById('snapshot'); |
| 59 | + imageDiv.appendChild(image); |
| 60 | + |
| 61 | + console.warn('Snapshot complete!'); |
| 62 | + }); |
| 63 | + }, |
| 64 | + |
| 65 | + // Remove all plots and snapshots from the page |
| 66 | + purge: function() { |
| 67 | + var plots = document.getElementsByClassName('dashboard-plot'); |
| 68 | + var images = document.getElementById('snapshot'); |
| 69 | + |
| 70 | + while(images.firstChild) { |
| 71 | + images.removeChild(images.firstChild); |
| 72 | + } |
| 73 | + |
| 74 | + for(var i = 0; i < plots.length; i++) { |
| 75 | + window.Plotly.purge(plots[i]); |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + // Specify what to do after each plotly.js script reload |
| 80 | + onReload: function() { |
| 81 | + return; |
| 82 | + }, |
| 83 | + |
| 84 | + // Refreshes the plotly.js source without needing to refresh the page |
| 85 | + reload: function() { |
| 86 | + var source = document.getElementById('source'); |
| 87 | + var reloaded = document.getElementById('reload-time'); |
| 88 | + |
| 89 | + source.remove(); |
| 90 | + |
| 91 | + window.Plotly = null; |
| 92 | + |
| 93 | + source = document.createElement('script'); |
| 94 | + source.id = 'source'; |
| 95 | + source.src = '../../build/plotly.js'; |
| 96 | + |
| 97 | + document.body.appendChild(source); |
| 98 | + |
| 99 | + var reloadTime = new Date().toLocaleTimeString(); |
| 100 | + console.warn('plotly.js reloaded at ' + reloadTime); |
| 101 | + reloaded.textContent = 'last reload at ' + reloadTime; |
| 102 | + |
| 103 | + Tabs.onReload(); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | + |
| 108 | +// Bind things to the window |
| 109 | +window.Tabs = Tabs; |
| 110 | +setInterval(function() { |
| 111 | + window.gd = Tabs.getGraph() || Tabs.fresh(); |
| 112 | + window.fullLayout = window.gd._fullLayout; |
| 113 | + window.fullData = window.gd._fullData; |
| 114 | +}, 1000); |
| 115 | + |
| 116 | + |
| 117 | +// Mocks search and plotting |
| 118 | +var f = new Fuse(mocks, { |
| 119 | + keys: [{ |
| 120 | + name: 'name', |
| 121 | + weight: 0.7 |
| 122 | + }, { |
| 123 | + name: 'keywords', |
| 124 | + weight: 0.3 |
| 125 | + }] |
| 126 | +}); |
| 127 | + |
| 128 | +var searchBar = document.getElementById('mocks-search'); |
| 129 | +var mocksList = document.getElementById('mocks-list'); |
| 130 | +var plotArea = document.getElementById('plots'); |
| 131 | + |
| 132 | +searchBar.addEventListener('keyup', function(e) { |
| 133 | + |
| 134 | + // Clear results. |
| 135 | + while(mocksList.firstChild) { |
| 136 | + mocksList.removeChild(mocksList.firstChild); |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + var results = f.search(e.target.value); |
| 141 | + |
| 142 | + results.forEach(function(r) { |
| 143 | + var result = document.createElement('span'); |
| 144 | + result.className = 'search-result'; |
| 145 | + result.innerText = r.name; |
| 146 | + |
| 147 | + result.addEventListener('click', function() { |
| 148 | + |
| 149 | + // Clear plots and plot selected. |
| 150 | + Tabs.purge(); |
| 151 | + Tabs.plotMock(r.file.slice(0, -5)); |
| 152 | + }); |
| 153 | + |
| 154 | + mocksList.appendChild(result); |
| 155 | + |
| 156 | + var listWidth = mocksList.getBoundingClientRect().width; |
| 157 | + var plotAreaWidth = Math.floor(window.innerWidth - listWidth); |
| 158 | + plotArea.setAttribute('style', 'width: ' + plotAreaWidth + 'px;'); |
| 159 | + }); |
| 160 | +}); |
0 commit comments