site.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2021
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * These are the four essential freedoms with GNU GPL software:
  18. * 1: freedom to run the program, for any purpose
  19. * 2: freedom to study how the program works, and change it to make it do what you wish
  20. * 3: freedom to redistribute copies to help your Free Software friends
  21. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  22. * , ,
  23. * / \
  24. * ((__-^^-,-^^-__))
  25. * `-_---' `---_-'
  26. * `--|o` 'o|--'
  27. * \ ` /
  28. * ): :(
  29. * :o_o:
  30. * "-"
  31. */
  32. jQuery(document).ready(function () {
  33. // Installs error handling.
  34. jQuery.ajaxSetup({
  35. error: function(resp, e) {
  36. if (resp.status == 0){
  37. } else if (resp.status == 404){
  38. alert('Requested URL not found.');
  39. } else if (resp.status == 500){
  40. alert('Internel Server Error:\n\t' + resp.responseText);
  41. } else if (e == 'parsererror') {
  42. alert('Error.\nParsing JSON Request failed.');
  43. } else if (e == 'timeout') {
  44. alert('Request timeout.');
  45. } else {
  46. alert('Unknown Error.\n' + resp.responseText);
  47. }
  48. }
  49. });
  50. var svg="";
  51. var generate_btn = jQuery('#generate_btn');
  52. var sample_1_btn = jQuery('#sample_1_btn');
  53. var sample_2_btn = jQuery('#sample_2_btn');
  54. var sample_3_btn = jQuery('#sample_3_btn');
  55. var sample_4_btn = jQuery('#sample_4_btn');
  56. var sample_5_btn = jQuery('#sample_5_btn');
  57. var sample_6_btn = jQuery('#sample_6_btn');
  58. var svg_div = jQuery('#graph_svg_div');
  59. var graph_data_textarea = jQuery('#graph_data');
  60. function isObject(value) {
  61. return (value && Object.prototype.toString.call(value) === '[object Object]');
  62. }
  63. function forIn(object, callback) {
  64. Object.keys(object).forEach(function (key) {
  65. callback(key, object[key]);
  66. });
  67. }
  68. function InsertGraphText(text) {
  69. graph_data_textarea.val(text);
  70. }
  71. function UpdateGraph() {
  72. var data = graph_data_textarea.val();
  73. try {
  74. parsed = JSON.parse(data);
  75. }
  76. catch (err) {
  77. alert("syntax error in json jgf input data");
  78. return false;
  79. }
  80. if (!isObject(parsed.graph)) {
  81. alert("no \"graph\": {} statement in json jgf input data");
  82. return false;
  83. }
  84. // assume this is a correct jgf file now.
  85. if (!isObject(parsed.graph.nodes)) {
  86. alert("no nodes in json jgf input data");
  87. return false;
  88. }
  89. var graph = new dagreD3.graphlib.Graph().setGraph({});
  90. graph.setGraph({
  91. nodesep: 15, // 25
  92. ranksep: 25, // 82
  93. rankdir: "TB", // or LR
  94. marginx: 10, // 20
  95. marginy: 10 // 20
  96. });
  97. forIn(parsed.graph.nodes, function (key, value) {
  98. if(value.label) {
  99. graph.setNode(key, {label:value.label});
  100. } else {
  101. // no label specified, use key as label.
  102. graph.setNode(key, {label:key});
  103. }
  104. });
  105. forIn(parsed.graph.edges, function (key, value) {
  106. graph.setEdge(value.source, value.target, {
  107. curve: d3.curveBasis,
  108. minlen: 2
  109. });
  110. });
  111. var render = new dagreD3.render();
  112. // remove old svg drawing if any
  113. d3.selectAll("g > *").remove();
  114. svg = d3.select("svg"),
  115. inner = svg.append("g");
  116. // Run the renderer. This is what draws the final graph.
  117. render(inner, graph);
  118. // Center the graph or left-most min. value 1
  119. var xCenterOffset = 40; // or center: (svg.attr("width") - graph.graph().width) / 2;
  120. inner.attr("transform", "translate(" + xCenterOffset + ", 20)");
  121. svg.attr("height", graph.graph().height + 80); // +40
  122. svg.attr("width", graph.graph().width + 80); // +40
  123. }
  124. // Startup function: call UpdateGraph
  125. jQuery(function() {
  126. // The buttons are disabled, enable them now that this script has loaded.
  127. generate_btn.removeAttr("disabled")
  128. .text("Generate Graph layout");
  129. sample_1_btn.removeAttr("disabled");
  130. sample_2_btn.removeAttr("disabled");
  131. sample_3_btn.removeAttr("disabled");
  132. sample_4_btn.removeAttr("disabled");
  133. sample_5_btn.removeAttr("disabled");
  134. sample_6_btn.removeAttr("disabled");
  135. });
  136. // Bind actions to form buttons.
  137. generate_btn.click(UpdateGraph);
  138. sample_1_btn.click(function(){
  139. InsertGraphText(jQuery("#sample1_text").html().trim());
  140. });
  141. sample_2_btn.click(function(){
  142. InsertGraphText(jQuery("#sample2_text").html().trim());
  143. });
  144. sample_3_btn.click(function(){
  145. InsertGraphText(jQuery("#sample3_text").html().trim());
  146. });
  147. sample_4_btn.click(function(){
  148. InsertGraphText(jQuery("#sample4_text").html().trim());
  149. });
  150. sample_5_btn.click(function(){
  151. InsertGraphText(jQuery("#sample5_text").html().trim());
  152. });
  153. sample_6_btn.click(function(){
  154. InsertGraphText(jQuery("#sample6_text").html().trim());
  155. });
  156. });
  157. // end.