site.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /*
  4. * Copyright t lefering
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * These are the four essential freedoms with GNU GPL software:
  20. * 1: freedom to run the program, for any purpose
  21. * 2: freedom to study how the program works, and change it to make it do what you wish
  22. * 3: freedom to redistribute copies to help your Free Software friends
  23. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  24. * , ,
  25. * / \
  26. * ((__-^^-,-^^-__))
  27. * `-_---' `---_-'
  28. * `--|o` 'o|--'
  29. * \ ` /
  30. * ): :(
  31. * :o_o:
  32. * "-"
  33. */
  34. jQuery(document).ready(function () {
  35. // Installs error handling.
  36. jQuery.ajaxSetup({
  37. error: function(resp, e) {
  38. if (resp.status == 0){
  39. alert('You are offline!!\n Please Check Your Network.');
  40. } else if (resp.status == 404){
  41. alert('Requested URL not found.');
  42. } else if (resp.status == 500){
  43. alert('Internel Server Error:\n\t' + resp.responseText);
  44. } else if (e == 'parsererror') {
  45. alert('Error.\nParsing JSON Request failed.');
  46. } else if (e == 'timeout') {
  47. alert('Request timeout.');
  48. } else {
  49. alert('Unknown Error.\n' + resp.responseText);
  50. }
  51. }
  52. }); // error:function()
  53. var generate_btn = jQuery('#generate_btn');
  54. var sample_1_btn = jQuery('#sample_1_btn');
  55. var sample_2_btn = jQuery('#sample_2_btn');
  56. var sample_3_btn = jQuery('#sample_3_btn');
  57. var sample_4_btn = jQuery('#sample_4_btn');
  58. var sample_5_btn = jQuery('#sample_5_btn');
  59. var svg_div = jQuery('#graph_svg_div');
  60. var graph_data_textarea = jQuery('#graph_data');
  61. function InsertGraphText(text) {
  62. graph_data_textarea.val(text);
  63. }
  64. function UpdateGraph() {
  65. svg_div.html("");
  66. var data = graph_data_textarea.val();
  67. // Generate the Visualization of the Graph into "svg".
  68. (async () => {
  69. // Build the WebAssembly instance.
  70. // initial value is 64Kb * max memory
  71. const memory = new WebAssembly.Memory({ initial: 10 });
  72. const response = await fetch('graph2svg.wasm');
  73. const bytes = await response.arrayBuffer();
  74. const { instance } = await WebAssembly.instantiate(bytes, {
  75. env: { memory }
  76. });
  77. // the svg image data to inline in the html
  78. var svg ="";
  79. // number of chars in resulting svg
  80. var svgnchar = 0;
  81. var svgnchar2 = 0;
  82. // Text to copy.
  83. const text = graph_data_textarea.val();
  84. // Configure shared memory.
  85. const view = new Uint8Array(memory.buffer);
  86. const pInput = instance.exports.__heap_base;
  87. const poutput = instance.exports.__heap_base + 1024;
  88. // limit the amount of max. input text
  89. if(text.length < 1000){
  90. // copy text to pInput
  91. encode(view, pInput, text);
  92. // run our own graph layout in sfg.c return number of chars in svg
  93. svgnchar = instance.exports.graph2svg(pInput,poutput);
  94. svgnchar2 = instance.exports.svgdata(pInput,svgnchar);
  95. } else {
  96. // set to message too-much-input-text
  97. svgnchar = -1;
  98. }
  99. // console.log(svgnchar);
  100. // console.log(svgnchar2);
  101. if (svgnchar > 0) {
  102. // create copy of svg data
  103. // old: svg = decode(view, poutput);
  104. svg = decode(view, pInput);
  105. } else {
  106. if (svgnchar < 0) {
  107. svg = "too much input text in textarea maximum 1000 chars";
  108. } else {
  109. svg = "no svg data";
  110. }
  111. }
  112. // console.log(svg);
  113. // set svg data as html data
  114. svg_div.html("<hr>"+svg);
  115. })();
  116. // Encode string into memory starting at address base.
  117. const encode = (memory, base, string) => {
  118. for (let i = 0; i < string.length; i++) {
  119. memory[base + i] = string.charCodeAt(i);
  120. }
  121. memory[base + string.length] = 0;
  122. };
  123. // Decode a string from memory starting at address base.
  124. const decode = (memory, base) => {
  125. let cursor = base;
  126. let result = '';
  127. while (memory[cursor] !== 0) {
  128. result += String.fromCharCode(memory[cursor++]);
  129. }
  130. //console.log(result);
  131. return result;
  132. };
  133. }
  134. // Startup function: call UpdateGraph
  135. jQuery(function() {
  136. // The buttons are disabled, enable them now that this script has loaded.
  137. generate_btn.removeAttr("disabled")
  138. .text("Generate Graph layout");
  139. sample_1_btn.removeAttr("disabled");
  140. sample_2_btn.removeAttr("disabled");
  141. sample_3_btn.removeAttr("disabled");
  142. sample_4_btn.removeAttr("disabled");
  143. sample_5_btn.removeAttr("disabled");
  144. });
  145. // Bind actions to form buttons.
  146. generate_btn.click(UpdateGraph);
  147. sample_1_btn.click(function(){
  148. InsertGraphText(jQuery("#sample1_text").html().trim());
  149. });
  150. sample_2_btn.click(function(){
  151. InsertGraphText(jQuery("#sample2_text").html().trim());
  152. });
  153. sample_3_btn.click(function(){
  154. InsertGraphText(jQuery("#sample3_text").html().trim());
  155. });
  156. sample_4_btn.click(function(){
  157. InsertGraphText(jQuery("#sample4_text").html().trim());
  158. });
  159. sample_5_btn.click(function(){
  160. InsertGraphText(jQuery("#sample5_text").html().trim());
  161. });
  162. });