HyperlinkText.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. /*
  3. * Copyright (C) 1998-2022 by Northwoods Software Corporation. All Rights Reserved.
  4. */
  5. // A "HyperlinkText" is either a TextBlock or a Panel containing a TextBlock that when clicked
  6. // opens a new browser window with a given or computed URL.
  7. // When the user's mouse passes over a "HyperlinkText", the text is underlined.
  8. // When the mouse hovers over a "HyperlinkText", it shows a tooltip that displays the URL.
  9. // This "HyperlinkText" builder is not pre-defined in the GoJS library, so you will need to load this definition.
  10. // Typical usages:
  11. // $("HyperlinkText", "https://gojs.net", "Visit GoJS")
  12. //
  13. // $("HyperlinkText",
  14. // function(node) { return "https://gojs.net/" + node.data.version; },
  15. // function(node) { return "Visit GoJS version " + node.data.version; })
  16. //
  17. // $("HyperlinkText",
  18. // function(node) { return "https://gojs.net/" + node.data.version; },
  19. // $(go.Panel, "Auto",
  20. // $(go.Shape, ...),
  21. // $(go.TextBlock, ...)
  22. // )
  23. // )
  24. // The first argument to the "HyperlinkText" builder should be either the URL string or a function
  25. // that takes the data-bound Panel and returns the URL string.
  26. // If the URL string is empty or if the function returns an empty string,
  27. // the text will not be underlined on a mouse-over and a click has no effect.
  28. // The second argument to the "HyperlinkText" builder may be either a string to display in a TextBlock,
  29. // or a function that takes the data-bound Panel and returns the string to display in a TextBlock.
  30. // If no text string or function is provided, it assumes all of the arguments are used to
  31. // define the visual tree for the "HyperlinkText", in the normal fashion for a Panel.
  32. // The result is either a TextBlock or a Panel.
  33. go.GraphObject.defineBuilder("HyperlinkText", function(args) {
  34. // the URL is required as the first argument, either a string or a side-effect-free function returning a string
  35. var url = go.GraphObject.takeBuilderArgument(args, undefined, function(x) { return typeof x === "string" || typeof x === "function"; });
  36. // the text for the HyperlinkText is the optional second argument, either a string or a side-effect-free function returning a string
  37. var text = go.GraphObject.takeBuilderArgument(args, null, function(x) { return typeof x === "string" || typeof x === "function"; });
  38. // see if the visual tree is supplied in the arguments to the "HyperlinkText"
  39. var anyGraphObjects = false;
  40. for (var i = 0; i < args.length; i++) {
  41. var a = args[i];
  42. if (a && a instanceof go.GraphObject) anyGraphObjects = true;
  43. }
  44. // define the click behavior
  45. var click =
  46. function(e, obj) {
  47. var u = obj._url;
  48. if (typeof u === "function") u = u(obj.findBindingPanel());
  49. if (u) window.open(u, "_blank");
  50. };
  51. // define the tooltip
  52. var tooltip =
  53. go.GraphObject.make("ToolTip",
  54. go.GraphObject.make(go.TextBlock,
  55. { name: "TB", margin: 4 },
  56. new go.Binding("text", "", function(obj) {
  57. // here OBJ will be in the Adornment, need to get the HyperlinkText/TextBlock
  58. obj = obj.part.adornedObject;
  59. var u = obj._url;
  60. if (typeof u === "function") u = u(obj.findBindingPanel());
  61. return u;
  62. }).ofObject()
  63. ),
  64. new go.Binding("visible", "text", function(t) { return !!t; }).ofObject("TB")
  65. );
  66. // if the text is provided, use a new TextBlock; otherwise assume the TextBlock is provided
  67. if (typeof (text) === "string" || typeof (text) === "function" || !anyGraphObjects) {
  68. if (text === null && typeof (url) === "string") text = url;
  69. var tb = go.GraphObject.make(go.TextBlock,
  70. {
  71. "_url": url,
  72. cursor: "pointer",
  73. mouseEnter: function(e, obj) {
  74. var u = obj._url;
  75. if (typeof u === "function") u = u(obj.findBindingPanel());
  76. if (u) obj.isUnderline = true;
  77. },
  78. mouseLeave: function(e, obj) { obj.isUnderline = false; },
  79. click: click, // defined above
  80. toolTip: tooltip // shared by all HyperlinkText textblocks
  81. }
  82. );
  83. if (typeof(text) === "string") {
  84. tb.text = text;
  85. } else if (typeof(text) === "function") {
  86. tb.bind(new go.Binding("text", "", text).ofObject())
  87. } else if (typeof (url) === "function") {
  88. tb.bind(new go.Binding("text", "", url).ofObject())
  89. }
  90. return tb;
  91. } else {
  92. function findTextBlock(obj) {
  93. if (obj instanceof go.TextBlock) return obj;
  94. if (obj instanceof go.Panel) {
  95. var it = obj.elements;
  96. while (it.next()) {
  97. var result = findTextBlock(it.value);
  98. if (result !== null) return result;
  99. }
  100. }
  101. return null;
  102. }
  103. return go.GraphObject.make(go.Panel,
  104. {
  105. "_url": url,
  106. cursor: "pointer",
  107. mouseEnter: function(e, panel) {
  108. var tb = findTextBlock(panel);
  109. var u = panel._url;
  110. if (typeof u === "function") u = u(panel.findBindingPanel());
  111. if (tb !== null && u) tb.isUnderline = true;
  112. },
  113. mouseLeave: function(e, panel) {
  114. var tb = findTextBlock(panel);
  115. if (tb !== null) tb.isUnderline = false;
  116. },
  117. click: click, // defined above
  118. toolTip: tooltip // shared by all HyperlinkText panels
  119. }
  120. );
  121. }
  122. });