1
0

grViz.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. HTMLWidgets.widget({
  2. name: 'grViz',
  3. type: 'output',
  4. initialize: function(el, width, height) {
  5. return {
  6. // TODO: add instance fields as required
  7. }
  8. },
  9. renderValue: function(el, x, instance) {
  10. // use this to sort of make our diagram responsive
  11. // or at a minimum fit within the bounds set by htmlwidgets
  12. // for the parent container
  13. function makeResponsive(el){
  14. var svg = el.getElementsByTagName("svg")[0];
  15. if(svg){
  16. if(svg.width) {svg.removeAttribute("width")};
  17. if(svg.height) {svg.removeAttribute("height")};
  18. svg.style.width = "100%";
  19. svg.style.height = "100%";
  20. }
  21. };
  22. if ( x.diagram != "" ) {
  23. if ( typeof x.config === "undefined" ){
  24. x.config = {};
  25. x.config.engine = "dot";
  26. x.config.options = {};
  27. }
  28. try {
  29. el.innerHTML = Viz( x.diagram, format="svg", engine=x.config.engine, options=x.config.options );
  30. makeResponsive(el);
  31. // set up a container for tasks to perform after completion
  32. // one example would be add callbacks for event handling
  33. // styling
  34. if (!(typeof x.tasks === "undefined") ){
  35. if ( (typeof x.tasks.length === "undefined") ||
  36. (typeof x.tasks === "function" ) ) {
  37. // handle a function not enclosed in array
  38. // should be able to remove once using jsonlite
  39. x.tasks = [x.tasks];
  40. }
  41. x.tasks.map(function(t){
  42. // for each tasks add it to the mermaid.tasks with el
  43. t.call(el);
  44. })
  45. }
  46. } catch(e){
  47. var p = document.createElement("pre")
  48. p.innerText = e;
  49. el.appendChild(p);
  50. }
  51. }
  52. },
  53. resize: function(el, width, height, instance) {
  54. }
  55. });