site.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Delay loading any function until the html dom has loaded. All functions are
  2. // defined in this top level function to ensure private scope.
  3. jQuery(document).ready(function () {
  4. // Installs error handling.
  5. jQuery.ajaxSetup({
  6. error: function(resp, e) {
  7. if (resp.status == 0){
  8. alert('You are offline!!\n Please Check Your Network.');
  9. } else if (resp.status == 404){
  10. alert('Requested URL not found.');
  11. } else if (resp.status == 500){
  12. alert('Internel Server Error:\n\t' + resp.responseText);
  13. } else if (e == 'parsererror') {
  14. alert('Error.\nParsing JSON Request failed.');
  15. } else if (e == 'timeout') {
  16. alert('Request timeout.');
  17. } else {
  18. alert('Unknown Error.\n' + resp.responseText);
  19. }
  20. }
  21. }); // error:function()
  22. var generate_btn = jQuery('#generate_btn');
  23. var svg_div = jQuery('#graph_svg_div');
  24. var graph_data_textarea = jQuery('#graph_data');
  25. function InsertGraphText(text) {
  26. graph_data_textarea.val(text);
  27. }
  28. function UpdateGraph() {
  29. svg_div.html("");
  30. var data = graph_data_textarea.val();
  31. // Generate the Visualization of the Graph into "svg".
  32. var svg = "initial svg string is here";
  33. (async () => {
  34. // Build the WebAssembly instance.
  35. const memory = new WebAssembly.Memory({ initial: 2 });
  36. const response = await fetch('./lenstr.wasm');
  37. const bytes = await response.arrayBuffer();
  38. const { instance } = await WebAssembly.instantiate(bytes, {
  39. env: { memory }
  40. });
  41. // Text to copy.
  42. const text = graph_data_textarea.val();
  43. // Configure shared memory.
  44. const view = new Uint8Array(memory.buffer);
  45. const pInput = instance.exports.__heap_base;
  46. encode(view, pInput, text);
  47. // run our own strlen()
  48. const bytesCopied = instance.exports.lenstr(pInput);
  49. console.log('text.length is ', text.length + ' lenstr() says ' + bytesCopied);
  50. })();
  51. // Encode string into memory starting at address base.
  52. const encode = (memory, base, string) => {
  53. for (let i = 0; i < string.length; i++) {
  54. memory[base + i] = string.charCodeAt(i);
  55. }
  56. memory[base + string.length] = 0;
  57. };
  58. svg_div.html("<hr>"+svg);
  59. }
  60. // Startup function: call UpdateGraph
  61. jQuery(function() {
  62. // The buttons are disabled, enable them now that this script
  63. // has loaded.
  64. generate_btn.removeAttr("disabled")
  65. .text("Generate Graph layout");
  66. });
  67. // Bind actions to form buttons.
  68. generate_btn.click(UpdateGraph);
  69. });