| 1 |
var startButton = document.getElementById( "startChuck" ); |
| 2 |
var compileButton = document.getElementById( "compileButton" ); |
| 3 |
var replaceButton = document.getElementById( "replaceButton" ); |
| 4 |
var removeButton = document.getElementById( "removeButton" ); |
| 5 |
var clearButton = document.getElementById( "clearButton" ); |
| 6 |
var micButton = document.getElementById( "micButton" ); |
| 7 |
var fileFormButton = document.getElementById( "fileFormButton" ); |
| 8 |
var fileForm = document.getElementById( "fileForm" ); |
| 9 |
var uploadButton = document.getElementById( "fileButton" ); |
| 10 |
var uploadFilename = document.getElementById( "uploadFilename" ); |
| 11 |
var uploadFile = document.getElementById( "uploadFile" ); |
| 12 |
var shredsTable = document.getElementById( "shredstable" ); |
| 13 |
var shredsToRows = {} |
| 14 |
var serverFilesToPreload = serverFilesToPreload || []; |
| 15 |
|
| 16 |
|
| 17 |
var preloadedFilesReady = preloadFilenames( serverFilesToPreload ); |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
var chuckCompileButton = function() |
| 22 |
{ |
| 23 |
|
| 24 |
theChuck.runCode( chuckEditor.getValue() ).then( function( shredID ) |
| 25 |
{ |
| 26 |
addShredRow( shredID ); |
| 27 |
}, function( failure ) {} ); |
| 28 |
} |
| 29 |
|
| 30 |
var chuckReplaceButton = function() |
| 31 |
{ |
| 32 |
|
| 33 |
theChuck.replaceCode( chuckEditor.getValue() ).then( function( shreds ) |
| 34 |
{ |
| 35 |
removeShredRow( shreds.oldShred ); |
| 36 |
addShredRow( shreds.newShred ); |
| 37 |
}, function( failure ) {} ); |
| 38 |
} |
| 39 |
|
| 40 |
var chuckRemoveButton = function() |
| 41 |
{ |
| 42 |
|
| 43 |
theChuck.removeLastCode().then( function( shred ) |
| 44 |
{ |
| 45 |
removeShredRow( shred ); |
| 46 |
}, function( failure ) {} ); |
| 47 |
} |
| 48 |
|
| 49 |
var chuckClearButton = function() |
| 50 |
{ |
| 51 |
|
| 52 |
theChuck.clearChuckInstance(); |
| 53 |
|
| 54 |
theChuck.clearGlobals(); |
| 55 |
} |
| 56 |
|
| 57 |
var chuckMicButton = function() |
| 58 |
{ |
| 59 |
navigator.mediaDevices.getUserMedia({ audio: true, video: false }) |
| 60 |
.then( function( stream ) |
| 61 |
{ |
| 62 |
micButton.disabled = true; |
| 63 |
const source = audioContext.createMediaStreamSource( stream ); |
| 64 |
source.connect( theChuck ); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
var chuckUploadButton = function() |
| 69 |
{ |
| 70 |
if (uploadFile.files.length == 0) |
| 71 |
{ |
| 72 |
return; |
| 73 |
} |
| 74 |
var file = uploadFile.files[0]; |
| 75 |
|
| 76 |
var fr = new FileReader(); |
| 77 |
fr.onload = function () { |
| 78 |
var data = new Uint8Array(fr.result); |
| 79 |
|
| 80 |
theChuck.createFile( '', uploadFilename.value, data ); |
| 81 |
|
| 82 |
uploadFile.value = ''; |
| 83 |
uploadFilename.value = ''; |
| 84 |
}; |
| 85 |
fr.readAsArrayBuffer(file); |
| 86 |
} |
| 87 |
|
| 88 |
startButton.addEventListener( "click", async function() { |
| 89 |
startButton.disabled = true; |
| 90 |
await preloadedFilesReady; |
| 91 |
await startChuck(); |
| 92 |
}); |
| 93 |
|
| 94 |
startButton.disabled = false; |
| 95 |
|
| 96 |
compileButton.addEventListener( "click", chuckCompileButton ); |
| 97 |
replaceButton.addEventListener( "click", chuckReplaceButton ); |
| 98 |
removeButton.addEventListener( "click", chuckRemoveButton ); |
| 99 |
clearButton.addEventListener( "click", chuckClearButton ); |
| 100 |
micButton.addEventListener( "click", chuckMicButton ); |
| 101 |
uploadButton.addEventListener( "click", chuckUploadButton ); |
| 102 |
|
| 103 |
theChuckReady.then( function() |
| 104 |
{ |
| 105 |
compileButton.disabled = false; |
| 106 |
replaceButton.disabled = false; |
| 107 |
removeButton.disabled = false; |
| 108 |
clearButton.disabled = false; |
| 109 |
micButton.disabled = false; |
| 110 |
uploadButton.disabled = false; |
| 111 |
}); |
| 112 |
|
| 113 |
(function() { |
| 114 |
var showingFileForm = false; |
| 115 |
fileFormButton.addEventListener( "click", function() |
| 116 |
{ |
| 117 |
if( showingFileForm ) |
| 118 |
{ |
| 119 |
|
| 120 |
fileFormButton.innerHTML = "Show File Uploader"; |
| 121 |
fileForm.classList.add( "hidden" ); |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
|
| 126 |
fileFormButton.innerHTML = "Hide File Uploader"; |
| 127 |
fileForm.classList.remove( "hidden" ); |
| 128 |
} |
| 129 |
showingFileForm = !showingFileForm; |
| 130 |
}); |
| 131 |
})(); |
| 132 |
|
| 133 |
chuckPrint = (function() { |
| 134 |
var element = document.getElementById('output'); |
| 135 |
if (element) element.value = ''; |
| 136 |
return function(text) { |
| 137 |
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' '); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if (element) { |
| 144 |
element.value += text + "\n"; |
| 145 |
element.scrollTop = element.scrollHeight; |
| 146 |
} |
| 147 |
}; |
| 148 |
})(); |
| 149 |
|
| 150 |
function addShredRow( theShred ) |
| 151 |
{ |
| 152 |
var row = shredsTable.insertRow(); |
| 153 |
var cell0 = row.insertCell(0); |
| 154 |
var cell1 = row.insertCell(1); |
| 155 |
var cell2 = row.insertCell(2); |
| 156 |
var cell3 = row.insertCell(3); |
| 157 |
|
| 158 |
shredsToRows[ theShred ] = row; |
| 159 |
|
| 160 |
cell0.innerHTML = "" + theShred; |
| 161 |
cell1.innerHTML = chuckEditor.getValue().substring(0, 30) + "..."; |
| 162 |
(function( cell, myShred ) |
| 163 |
{ |
| 164 |
var getTime = function() { return Math.floor( Date.now() / 1000 ); } |
| 165 |
var formatTime = function(i) { |
| 166 |
|
| 167 |
if (i < 10) {i = "0" + i}; |
| 168 |
return i; |
| 169 |
} |
| 170 |
|
| 171 |
var startTime = getTime(); |
| 172 |
var removed = false; |
| 173 |
function updateTime() |
| 174 |
{ |
| 175 |
var now = getTime(); |
| 176 |
var elapsed = now - startTime; |
| 177 |
var m = Math.floor( elapsed / 60 ); |
| 178 |
var s = Math.floor( elapsed % 60 ); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if( !( myShred in shredsToRows ) ) |
| 183 |
{ |
| 184 |
removed = true; |
| 185 |
} |
| 186 |
theChuck.isShredActive( myShred ).then( function( result ) |
| 187 |
{ |
| 188 |
if( !result && !removed ) |
| 189 |
{ |
| 190 |
removed = true; |
| 191 |
removeShredRow( myShred ); |
| 192 |
return; |
| 193 |
} |
| 194 |
}); |
| 195 |
|
| 196 |
|
| 197 |
if( !removed && document.contains( cell ) ) |
| 198 |
{ |
| 199 |
cell.innerHTML = formatTime(m) + ":" + formatTime(s); |
| 200 |
setTimeout( updateTime, 1000 ); |
| 201 |
} |
| 202 |
} |
| 203 |
updateTime(); |
| 204 |
})( cell2, theShred ); |
| 205 |
var removeButton = document.createElement("BUTTON"); |
| 206 |
removeButton.innerHTML = "Remove" |
| 207 |
cell3.appendChild( removeButton ); |
| 208 |
|
| 209 |
removeButton.addEventListener( "click", (function( shredID ) |
| 210 |
{ |
| 211 |
return function() |
| 212 |
{ |
| 213 |
theChuck.removeShred( shredID ).then( function( removedShred ) |
| 214 |
{ |
| 215 |
removeShredRow( theShred ); |
| 216 |
}, function( failure ) { console.log( failure ); } ); |
| 217 |
} |
| 218 |
})( theShred ) ); |
| 219 |
} |
| 220 |
|
| 221 |
function removeShredRow( theShred ) |
| 222 |
{ |
| 223 |
if( theShred in shredsToRows ) |
| 224 |
{ |
| 225 |
shredsToRows[ theShred ].parentNode.removeChild( shredsToRows[ theShred ] ); |
| 226 |
delete shredsToRows[ theShred ]; |
| 227 |
} |
| 228 |
} |