UNPKG

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