Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1624 lines
45 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /**
  2. * TODO - add tests for:
  3. * ====
  4. *
  5. * - html unclosed or unopened tags
  6. * - html tag combinations with no font defined (e.g. bold within mono)
  7. * - Unit tests for bad font shorthands.
  8. * Currently, only "size[px] name color" is valid, always 3 items with this exact spacing.
  9. * All other combinations should either be rejected as error or handled gracefully.
  10. */
  11. var assert = require('assert')
  12. var Label = require('../lib/network/modules/components/shared/Label').default;
  13. var NodesHandler = require('../lib/network/modules/NodesHandler').default;
  14. var util = require('../lib/util');
  15. var jsdom_global = require('jsdom-global');
  16. var vis = require('../dist/vis');
  17. var Network = vis.network;
  18. /**************************************************************
  19. * Dummy class definitions for minimal required functionality.
  20. **************************************************************/
  21. class DummyContext {
  22. measureText(text) {
  23. return {
  24. width: 12*text.length,
  25. height: 14
  26. };
  27. }
  28. }
  29. class DummyLayoutEngine {
  30. positionInitially() {}
  31. }
  32. /**************************************************************
  33. * End Dummy class definitions
  34. **************************************************************/
  35. describe('Network Label', function() {
  36. /**
  37. * Retrieve options object from a NodesHandler instance
  38. *
  39. * NOTE: these are options at the node-level
  40. */
  41. function getOptions(options = {}) {
  42. var body = {
  43. functions: {},
  44. emitter: {
  45. on: function() {}
  46. }
  47. }
  48. var nodesHandler = new NodesHandler(body, {}, options, new DummyLayoutEngine() );
  49. //console.log(JSON.stringify(nodesHandler.options, null, 2));
  50. return nodesHandler.options;
  51. }
  52. /**
  53. * Check if the returned lines and blocks are as expected.
  54. *
  55. * All width/height fields and font info are ignored.
  56. * Within blocks, only the text is compared
  57. */
  58. function checkBlocks(returned, expected) {
  59. let showBlocks = () => {
  60. return '\nreturned: ' + JSON.stringify(returned, null, 2) + '\n' +
  61. 'expected: ' + JSON.stringify(expected, null, 2);
  62. }
  63. assert.equal(expected.lines.length, returned.lines.length, 'Number of lines does not match, ' + showBlocks());
  64. for (let i = 0; i < returned.lines.length; ++i) {
  65. let retLine = returned.lines[i];
  66. let expLine = expected.lines[i];
  67. assert(retLine.blocks.length === expLine.blocks.length, 'Number of blocks does not match, ' + showBlocks());
  68. for (let j = 0; j < retLine.blocks.length; ++j) {
  69. let retBlock = retLine.blocks[j];
  70. let expBlock = expLine.blocks[j];
  71. assert(retBlock.text === expBlock.text, 'Text does not match, ' + showBlocks());
  72. assert(retBlock.mod !== undefined);
  73. if (retBlock.mod === 'normal' || retBlock.mod === '') {
  74. assert(expBlock.mod === undefined || expBlock.mod === 'normal' || expBlock === '',
  75. 'No mod field expected in returned, ' + showBlocks());
  76. } else {
  77. assert(retBlock.mod === expBlock.mod, 'Mod fields do not match, line: ' + i + ', block: ' + j +
  78. '; ret: ' + retBlock.mod + ', exp: ' + expBlock.mod + '\n' + showBlocks());
  79. }
  80. }
  81. }
  82. }
  83. function checkProcessedLabels(label, text, expected) {
  84. var ctx = new DummyContext();
  85. for (var i in text) {
  86. var ret = label._processLabelText(ctx, false, false, text[i]);
  87. //console.log(JSON.stringify(ret, null, 2));
  88. checkBlocks(ret, expected[i]);
  89. }
  90. }
  91. /**************************************************************
  92. * Test data
  93. **************************************************************/
  94. var normal_text = [
  95. "label text",
  96. "label\nwith\nnewlines",
  97. "OnereallylongwordthatshouldgooverwidthConstraint.maximumifdefined",
  98. "One really long sentence that should go over widthConstraint.maximum if defined",
  99. "Reallyoneenormouslylargelabel withtwobigwordsgoingoverwayovermax"
  100. ]
  101. var html_text = [
  102. "label <b>with</b> <code>some</code> <i>multi <b>tags</b></i>",
  103. "label <b>with</b> <code>some</code> \n <i>multi <b>tags</b></i>\n and newlines" // NB spaces around \n's
  104. ];
  105. var markdown_text = [
  106. "label *with* `some` _multi *tags*_",
  107. "label *with* `some` \n _multi *tags*_\n and newlines" // NB spaces around \n's
  108. ];
  109. /**************************************************************
  110. * Expected Results
  111. **************************************************************/
  112. var normal_expected = [{
  113. // In first item, width/height kept in for reference
  114. width: 120,
  115. height: 14,
  116. lines: [{
  117. width: 120,
  118. height: 14,
  119. blocks: [{
  120. text: "label text",
  121. width: 120,
  122. height: 14,
  123. }]
  124. }]
  125. }, {
  126. lines: [{
  127. blocks: [{text: "label"}]
  128. }, {
  129. blocks: [{text: "with"}]
  130. }, {
  131. blocks: [{text: "newlines"}]
  132. }]
  133. }, {
  134. // From here onward, changes width max width set
  135. lines: [{
  136. blocks: [{text: "OnereallylongwordthatshouldgooverwidthConstraint.maximumifdefined"}]
  137. }]
  138. }, {
  139. lines: [{
  140. blocks: [{text: "One really long sentence that should go over widthConstraint.maximum if defined"}]
  141. }]
  142. }, {
  143. lines: [{
  144. blocks: [{text: "Reallyoneenormouslylargelabel withtwobigwordsgoingoverwayovermax"}]
  145. }]
  146. }];
  147. const indexWidthConstrained = 2; // index of first item that will be different with max width set
  148. var normal_widthConstraint_expected = normal_expected.slice(0, indexWidthConstrained);
  149. Array.prototype.push.apply(normal_widthConstraint_expected, [{
  150. lines: [{
  151. blocks: [{text: "Onereallylongwordthatshoul"}]
  152. }, {
  153. blocks: [{text: "dgooverwidthConstraint.max"}]
  154. }, {
  155. blocks: [{text: "imumifdefined"}]
  156. }]
  157. }, {
  158. lines: [{
  159. blocks: [{text: "One really long"}]
  160. }, {
  161. blocks: [{text: "sentence that should"}]
  162. }, {
  163. blocks: [{text: "go over"}]
  164. }, {
  165. blocks: [{text: "widthConstraint.maximum"}]
  166. }, {
  167. blocks: [{text: "if defined"}]
  168. }]
  169. }, {
  170. lines: [{
  171. blocks: [{text: "Reallyoneenormouslylargela"}]
  172. }, {
  173. blocks: [{text: "bel"}]
  174. }, {
  175. blocks: [{text: "withtwobigwordsgoingoverwa"}]
  176. }, {
  177. blocks: [{text: "yovermax"}]
  178. }]
  179. }]);
  180. var html_unchanged_expected = [{
  181. lines: [{
  182. blocks: [{text: "label <b>with</b> <code>some</code> <i>multi <b>tags</b></i>"}]
  183. }]
  184. }, {
  185. lines: [{
  186. blocks: [{text: "label <b>with</b> <code>some</code> "}]
  187. }, {
  188. blocks: [{text: " <i>multi <b>tags</b></i>"}]
  189. }, {
  190. blocks: [{text: " and newlines"}]
  191. }]
  192. }];
  193. var html_widthConstraint_unchanged = [{
  194. lines: [{
  195. blocks: [{text: "label <b>with</b>"}]
  196. }, {
  197. blocks: [{text: "<code>some</code>"}]
  198. }, {
  199. blocks: [{text: "<i>multi"}]
  200. }, {
  201. blocks: [{text: "<b>tags</b></i>"}]
  202. }]
  203. }, {
  204. lines: [{
  205. blocks: [{text: "label <b>with</b>"}]
  206. }, {
  207. blocks: [{text: "<code>some</code> "}]
  208. }, {
  209. blocks: [{text: " <i>multi"}]
  210. }, {
  211. blocks: [{text: "<b>tags</b></i>"}]
  212. }, {
  213. blocks: [{text: " and newlines"}]
  214. }]
  215. }];
  216. var markdown_unchanged_expected = [{
  217. lines: [{
  218. blocks: [{text: "label *with* `some` _multi *tags*_"}]
  219. }]
  220. }, {
  221. lines: [{
  222. blocks: [{text: "label *with* `some` "}]
  223. }, {
  224. blocks: [{text: " _multi *tags*_"}]
  225. }, {
  226. blocks: [{text: " and newlines"}]
  227. }]
  228. }];
  229. var markdown_widthConstraint_expected= [{
  230. lines: [{
  231. blocks: [{text: "label *with* `some`"}]
  232. }, {
  233. blocks: [{text: "_multi *tags*_"}]
  234. }]
  235. }, {
  236. lines: [{
  237. blocks: [{text: "label *with* `some` "}]
  238. }, {
  239. blocks: [{text: " _multi *tags*_"}]
  240. }, {
  241. blocks: [{text: " and newlines"}]
  242. }]
  243. }];
  244. var multi_expected = [{
  245. lines: [{
  246. blocks: [
  247. {text: "label "},
  248. {text: "with" , mod: 'bold'},
  249. {text: " "},
  250. {text: "some" , mod: 'mono'},
  251. {text: " "},
  252. {text: "multi ", mod: 'ital'},
  253. {text: "tags" , mod: 'boldital'}
  254. ]
  255. }]
  256. }, {
  257. lines: [{
  258. blocks: [
  259. {text: "label "},
  260. {text: "with" , mod: 'bold'},
  261. {text: " "},
  262. {text: "some" , mod: 'mono'},
  263. {text: " "}
  264. ]
  265. }, {
  266. blocks: [
  267. {text: " "},
  268. {text: "multi ", mod: 'ital'},
  269. {text: "tags" , mod: 'boldital'}
  270. ]
  271. }, {
  272. blocks: [{text: " and newlines"}]
  273. }]
  274. }];
  275. /**************************************************************
  276. * End Expected Results
  277. **************************************************************/
  278. before(function() {
  279. this.jsdom_global = jsdom_global(
  280. "<div id='mynetwork'></div>",
  281. { skipWindowCheck: true}
  282. );
  283. this.container = document.getElementById('mynetwork');
  284. });
  285. after(function() {
  286. this.jsdom_global();
  287. });
  288. it('parses normal text labels', function (done) {
  289. var label = new Label({}, getOptions());
  290. checkProcessedLabels(label, normal_text , normal_expected);
  291. checkProcessedLabels(label, html_text , html_unchanged_expected); // html unchanged
  292. checkProcessedLabels(label, markdown_text, markdown_unchanged_expected); // markdown unchanged
  293. done();
  294. });
  295. it('parses html labels', function (done) {
  296. var options = getOptions(options);
  297. options.font.multi = true; // TODO: also test 'html', also test illegal value here
  298. var label = new Label({}, options);
  299. checkProcessedLabels(label, normal_text , normal_expected); // normal as usual
  300. checkProcessedLabels(label, html_text , multi_expected);
  301. checkProcessedLabels(label, markdown_text, markdown_unchanged_expected); // markdown unchanged
  302. done();
  303. });
  304. it('parses markdown labels', function (done) {
  305. var options = getOptions(options);
  306. options.font.multi = 'markdown'; // TODO: also test 'md', also test illegal value here
  307. var label = new Label({}, options);
  308. checkProcessedLabels(label, normal_text , normal_expected); // normal as usual
  309. checkProcessedLabels(label, html_text , html_unchanged_expected); // html unchanged
  310. checkProcessedLabels(label, markdown_text, multi_expected);
  311. done();
  312. });
  313. it('handles normal text with widthConstraint.maximum', function (done) {
  314. var options = getOptions(options);
  315. //
  316. // What the user would set:
  317. //
  318. // options.widthConstraint = { minimum: 100, maximum: 200};
  319. //
  320. // No sense in adding minWdt, not used when splitting labels into lines
  321. //
  322. // This comment also applies to the usage of maxWdt in the test cases below
  323. //
  324. options.font.maxWdt = 300;
  325. var label = new Label({}, options);
  326. checkProcessedLabels(label, normal_text , normal_widthConstraint_expected);
  327. checkProcessedLabels(label, html_text , html_widthConstraint_unchanged); // html unchanged
  328. // Following is an unlucky selection, because the first line broken on the final character (space)
  329. // So we cheat a bit here
  330. options.font.maxWdt = 320;
  331. label = new Label({}, options);
  332. checkProcessedLabels(label, markdown_text, markdown_widthConstraint_expected); // markdown unchanged
  333. done();
  334. });
  335. it('handles html tags with widthConstraint.maximum', function (done) {
  336. var options = getOptions(options);
  337. options.font.multi = true;
  338. options.font.maxWdt = 300;
  339. var label = new Label({}, options);
  340. checkProcessedLabels(label, normal_text , normal_widthConstraint_expected);
  341. checkProcessedLabels(label, html_text , multi_expected);
  342. // Following is an unlucky selection, because the first line broken on the final character (space)
  343. // So we cheat a bit here
  344. options.font.maxWdt = 320;
  345. label = new Label({}, options);
  346. checkProcessedLabels(label, markdown_text, markdown_widthConstraint_expected);
  347. done();
  348. });
  349. it('handles markdown tags with widthConstraint.maximum', function (done) {
  350. var options = getOptions(options);
  351. options.font.multi = 'markdown';
  352. options.font.maxWdt = 300;
  353. var label = new Label({}, options);
  354. checkProcessedLabels(label, normal_text , normal_widthConstraint_expected);
  355. checkProcessedLabels(label, html_text , html_widthConstraint_unchanged);
  356. checkProcessedLabels(label, markdown_text, multi_expected);
  357. done();
  358. });
  359. describe('Multi-Fonts', function() {
  360. class HelperNode {
  361. constructor(network) {
  362. this.nodes = network.body.nodes;
  363. }
  364. fontOption(index) {
  365. return this.nodes[index].labelModule.fontOptions;
  366. };
  367. modBold(index) {
  368. return this.fontOption(index).bold;
  369. };
  370. }
  371. describe('Node Labels', function() {
  372. function createNodeNetwork(newOptions) {
  373. var dataNodes = [
  374. {id: 0, label: '<b>0</b>'},
  375. {id: 1, label: '<b>1</b>'},
  376. {id: 2, label: '<b>2</b>', group: 'group1'},
  377. {id: 3, label: '<b>3</b>',
  378. font: {
  379. bold: { color: 'green' },
  380. }
  381. },
  382. {id: 4, label: '<b>4</b>', group: 'group1',
  383. font: {
  384. bold: { color: 'green' },
  385. }
  386. },
  387. ];
  388. // create a network
  389. var container = document.getElementById('mynetwork');
  390. var data = {
  391. nodes: new vis.DataSet(dataNodes),
  392. edges: []
  393. };
  394. var options = {
  395. nodes: {
  396. font: {
  397. multi: true
  398. }
  399. },
  400. groups: {
  401. group1: {
  402. font: { color: 'red' },
  403. },
  404. group2: {
  405. font: { color: 'white' },
  406. },
  407. },
  408. };
  409. if (newOptions !== undefined) {
  410. util.deepExtend(options, newOptions);
  411. }
  412. var network = new vis.Network(container, data, options);
  413. return [network, data, options];
  414. }
  415. /**
  416. * Check that setting options for multi-font works as expected
  417. *
  418. * - using multi-font 'bold' for test, the rest should work analogously
  419. * - using multi-font option 'color' for test, the rest should work analogously
  420. */
  421. it('respects the font option precedence', function (done) {
  422. var [network, data, options] = createNodeNetwork();
  423. var h = new HelperNode(network);
  424. assert.equal(h.modBold(0).color, '#343434'); // Default value
  425. assert.equal(h.modBold(1).color, '#343434'); // Default value
  426. assert.equal(h.modBold(2).color, 'red'); // Group value overrides default
  427. assert.equal(h.modBold(3).color, 'green'); // Local value overrides default
  428. assert.equal(h.modBold(4).color, 'green'); // Local value overrides group
  429. done();
  430. });
  431. it('handles dynamic data and option updates', function (done) {
  432. var [network, data, options] = createNodeNetwork();
  433. var h = new HelperNode(network);
  434. //
  435. // Change some node values dynamically
  436. //
  437. data.nodes.update([
  438. {id: 1, group: 'group2'},
  439. {id: 4, font: { bold: { color: 'orange'}}},
  440. ]);
  441. assert.equal(h.modBold(0).color, '#343434'); // unchanged
  442. assert.equal(h.modBold(1).color, 'white'); // new group value
  443. assert.equal(h.modBold(3).color, 'green'); // unchanged
  444. assert.equal(h.modBold(4).color, 'orange'); // new local value
  445. //
  446. // Change group options dynamically
  447. //
  448. network.setOptions({
  449. groups: {
  450. group1: {
  451. font: { color: 'brown' },
  452. },
  453. },
  454. });
  455. assert.equal(h.modBold(0).color, '#343434'); // unchanged
  456. assert.equal(h.modBold(1).color, 'white'); // Unchanged
  457. assert.equal(h.modBold(2).color, 'brown'); // New group values
  458. assert.equal(h.modBold(3).color, 'green'); // unchanged
  459. assert.equal(h.modBold(4).color, 'orange'); // unchanged
  460. network.setOptions({
  461. nodes: {
  462. font: {
  463. multi: true,
  464. bold: {
  465. color: 'black'
  466. }
  467. }
  468. },
  469. });
  470. assert.equal(h.modBold(0).color, 'black'); // nodes default
  471. assert.equal(h.modBold(1).color, 'black'); // more specific bold value overrides group value
  472. assert.equal(h.modBold(2).color, 'black'); // idem
  473. assert.equal(h.modBold(3).color, 'green'); // unchanged
  474. assert.equal(h.modBold(4).color, 'orange'); // unchanged
  475. network.setOptions({
  476. groups: {
  477. group1: {
  478. font: { bold: {color: 'brown'} },
  479. },
  480. },
  481. });
  482. assert.equal(h.modBold(0).color, 'black'); // nodes default
  483. assert.equal(h.modBold(1).color, 'black'); // more specific bold value overrides group value
  484. assert.equal(h.modBold(2).color, 'brown'); // bold group value overrides bold node value
  485. assert.equal(h.modBold(3).color, 'green'); // unchanged
  486. assert.equal(h.modBold(4).color, 'orange'); // unchanged
  487. done();
  488. });
  489. it('handles normal font values in default options', function (done) {
  490. var newOptions = {
  491. nodes: {
  492. font: {
  493. color: 'purple' // Override the default value
  494. }
  495. },
  496. };
  497. var [network, data, options] = createNodeNetwork(newOptions);
  498. var h = new HelperNode(network);
  499. assert.equal(h.modBold(0).color, 'purple'); // Nodes value
  500. assert.equal(h.modBold(1).color, 'purple'); // Nodes value
  501. assert.equal(h.modBold(2).color, 'red'); // Group value overrides nodes
  502. assert.equal(h.modBold(3).color, 'green'); // Local value overrides all
  503. assert.equal(h.modBold(4).color, 'green'); // Idem
  504. done();
  505. });
  506. it('handles multi-font values in default options/groups', function (done) {
  507. var newOptions = {
  508. nodes: {
  509. font: {
  510. color: 'purple' // This set value should be overridden
  511. }
  512. },
  513. };
  514. newOptions.nodes.font.bold = { color: 'yellow'};
  515. newOptions.groups = {
  516. group1: {
  517. font: { bold: { color: 'red'}}
  518. }
  519. };
  520. var [network, data, options] = createNodeNetwork(newOptions);
  521. var h = new HelperNode(network);
  522. assert(options.nodes.font.multi);
  523. assert.equal(h.modBold(0).color, 'yellow'); // bold value
  524. assert.equal(h.modBold(1).color, 'yellow'); // bold value
  525. assert.equal(h.modBold(2).color, 'red'); // Group value overrides nodes
  526. assert.equal(h.modBold(3).color, 'green'); // Local value overrides all
  527. assert.equal(h.modBold(4).color, 'green'); // Idem
  528. done();
  529. });
  530. }); // Node Labels
  531. describe('Edge Labels', function() {
  532. function createEdgeNetwork(newOptions) {
  533. var dataNodes = [
  534. {id: 1, label: '1'},
  535. {id: 2, label: '2'},
  536. {id: 3, label: '3'},
  537. {id: 4, label: '4'},
  538. ];
  539. var dataEdges = [
  540. {id: 1, from: 1, to: 2, label: '<b>1</b>'},
  541. {id: 2, from: 1, to: 4, label: '<b>2</b>',
  542. font: {
  543. bold: { color: 'green' },
  544. }
  545. },
  546. {id: 3, from: 2, to: 3, label: '<b>3</b>',
  547. font: {
  548. bold: { color: 'green' },
  549. }
  550. },
  551. ];
  552. // create a network
  553. var container = document.getElementById('mynetwork');
  554. var data = {
  555. nodes: new vis.DataSet(dataNodes),
  556. edges: new vis.DataSet(dataEdges),
  557. };
  558. var options = {
  559. edges: {
  560. font: {
  561. multi: true
  562. }
  563. },
  564. };
  565. if (newOptions !== undefined) {
  566. util.deepExtend(options, newOptions);
  567. }
  568. var network = new vis.Network(container, data, options);
  569. return [network, data, options];
  570. }
  571. class HelperEdge {
  572. constructor(network) {
  573. this.edges = network.body.edges;
  574. }
  575. fontOption(index) {
  576. return this.edges[index].labelModule.fontOptions;
  577. };
  578. modBold(index) {
  579. return this.fontOption(index).bold;
  580. };
  581. }
  582. /**
  583. * Check that setting options for multi-font works as expected
  584. *
  585. * - using multi-font 'bold' for test, the rest should work analogously
  586. * - using multi-font option 'color' for test, the rest should work analogously
  587. * - edges have no groups
  588. */
  589. it('respects the font option precedence', function (done) {
  590. var [network, data, options] = createEdgeNetwork();
  591. var h = new HelperEdge(network);
  592. assert.equal(h.modBold(1).color, '#343434'); // Default value
  593. assert.equal(h.modBold(2).color, 'green'); // Local value overrides default
  594. assert.equal(h.modBold(3).color, 'green'); // Local value overrides group
  595. done();
  596. });
  597. it('handles dynamic data and option updates', function (done) {
  598. var [network, data, options] = createEdgeNetwork();
  599. var h = new HelperEdge(network);
  600. data.edges.update([
  601. {id: 3, font: { bold: { color: 'orange'}}},
  602. ]);
  603. assert.equal(h.modBold(1).color, '#343434'); // unchanged
  604. assert.equal(h.modBold(2).color, 'green'); // unchanged
  605. assert.equal(h.modBold(3).color, 'orange'); // new local value
  606. network.setOptions({
  607. edges: {
  608. font: {
  609. multi: true,
  610. bold: {
  611. color: 'black'
  612. }
  613. }
  614. },
  615. });
  616. assert.equal(h.modBold(1).color, 'black'); // more specific bold value overrides group value
  617. assert.equal(h.modBold(2).color, 'green'); // unchanged
  618. assert.equal(h.modBold(3).color, 'orange'); // unchanged
  619. done();
  620. });
  621. it('handles font values in default options', function (done) {
  622. var newOptions = {
  623. edges: {
  624. font: {
  625. color: 'purple' // Override the default value
  626. }
  627. },
  628. };
  629. var [network, data, options] = createEdgeNetwork(newOptions);
  630. var h = new HelperEdge(network);
  631. assert.equal(h.modBold(1).color, 'purple'); // Nodes value
  632. assert.equal(h.modBold(2).color, 'green'); // Local value overrides all
  633. assert.equal(h.modBold(3).color, 'green'); // Idem
  634. done();
  635. });
  636. }); // Edge Labels
  637. describe('Shorthand Font Options', function() {
  638. var testFonts = {
  639. 'default': {color: '#343434', face: 'arial' , size: 14},
  640. 'monodef': {color: '#343434', face: 'monospace', size: 15},
  641. 'font1' : {color: '#010101', face: 'Font1' , size: 1},
  642. 'font2' : {color: '#020202', face: 'Font2' , size: 2},
  643. 'font3' : {color: '#030303', face: 'Font3' , size: 3},
  644. 'font4' : {color: '#040404', face: 'Font4' , size: 4},
  645. 'font5' : {color: '#050505', face: 'Font5' , size: 5},
  646. 'font6' : {color: '#060606', face: 'Font6' , size: 6},
  647. 'font7' : {color: '#070707', face: 'Font7' , size: 7},
  648. };
  649. function checkFont(opt, expectedLabel) {
  650. var expected = testFonts[expectedLabel];
  651. util.forEach(expected, (item, key) => {
  652. assert.equal(opt[key], item);
  653. });
  654. };
  655. function createNetwork() {
  656. var dataNodes = [
  657. {id: 1, label: '1'},
  658. {id: 2, label: '2', group: 'group1'},
  659. {id: 3, label: '3', group: 'group2'},
  660. {id: 4, label: '4', font: '5px Font5 #050505'},
  661. ];
  662. var dataEdges = [];
  663. // create a network
  664. var container = document.getElementById('mynetwork');
  665. var data = {
  666. nodes: new vis.DataSet(dataNodes),
  667. edges: new vis.DataSet(dataEdges),
  668. };
  669. var options = {
  670. nodes: {
  671. font: {
  672. multi: true,
  673. bold: '1 Font1 #010101',
  674. ital: '2 Font2 #020202',
  675. }
  676. },
  677. groups: {
  678. group1: {
  679. font: '3 Font3 #030303'
  680. },
  681. group2: {
  682. font: {
  683. bold: '4 Font4 #040404'
  684. }
  685. }
  686. }
  687. };
  688. var network = new vis.Network(container, data, options);
  689. return [network, data];
  690. }
  691. it('handles shorthand options correctly', function (done) {
  692. var [network, data] = createNetwork();
  693. var h = new HelperNode(network);
  694. // NOTE: 'mono' has its own global default font and size, which will
  695. // trump any other font values set.
  696. var opt = h.fontOption(1);
  697. checkFont(opt, 'default');
  698. checkFont(opt.bold, 'font1');
  699. checkFont(opt.ital, 'font2');
  700. checkFont(opt.mono, 'monodef'); // Mono should have defaults
  701. // Node 2 should be using group1 options
  702. opt = h.fontOption(2);
  703. checkFont(opt, 'font3');
  704. checkFont(opt.bold, 'font1'); // bold retains nodes default options
  705. checkFont(opt.ital, 'font2'); // ital retains nodes default options
  706. assert.equal(opt.mono.color, '#030303'); // New color
  707. assert.equal(opt.mono.face, 'monospace'); // own global default font
  708. assert.equal(opt.mono.size, 15); // Own global default size
  709. // Node 3 should be using group2 options
  710. opt = h.fontOption(3);
  711. checkFont(opt, 'default');
  712. checkFont(opt.bold, 'font4');
  713. checkFont(opt.ital, 'font2');
  714. checkFont(opt.mono, 'monodef'); // Mono should have defaults
  715. // Node 4 has its own base font definition
  716. opt = h.fontOption(4);
  717. checkFont(opt, 'font5');
  718. checkFont(opt.bold, 'font1');
  719. checkFont(opt.ital, 'font2');
  720. assert.equal(opt.mono.color, '#050505'); // New color
  721. assert.equal(opt.mono.face, 'monospace');
  722. assert.equal(opt.mono.size, 15);
  723. done();
  724. });
  725. function dynamicAdd1(network, data) {
  726. // Add new shorthand at every level
  727. data.nodes.update([
  728. {id: 1, font: '5 Font5 #050505'},
  729. {id: 4, font: { bold: '6 Font6 #060606'} }, // kills node instance base font
  730. ]);
  731. network.setOptions({
  732. nodes: {
  733. font: {
  734. multi: true,
  735. ital: '4 Font4 #040404',
  736. }
  737. },
  738. groups: {
  739. group1: {
  740. font: {
  741. bold: '7 Font7 #070707' // Kills node instance base font
  742. }
  743. },
  744. group2: {
  745. font: '6 Font6 #060606' // Note: 'bold' removed by this
  746. }
  747. }
  748. });
  749. }
  750. function dynamicAdd2(network, data) {
  751. network.setOptions({
  752. nodes: {
  753. font: '7 Font7 #070707' // Note: this kills the font.multi, bold and ital settings!
  754. }
  755. });
  756. }
  757. it('deals with dynamic data and option updates for shorthand', function (done) {
  758. var [network, data] = createNetwork();
  759. var h = new HelperNode(network);
  760. dynamicAdd1(network, data);
  761. var opt = h.fontOption(1);
  762. checkFont(opt, 'font5'); // New base font
  763. checkFont(opt.bold, 'font1');
  764. checkFont(opt.ital, 'font4'); // New global node default
  765. assert.equal(opt.mono.color, '#050505'); // New color
  766. assert.equal(opt.mono.face, 'monospace');
  767. assert.equal(opt.mono.size, 15);
  768. opt = h.fontOption(2);
  769. checkFont(opt, 'default');
  770. checkFont(opt.bold, 'font7');
  771. checkFont(opt.ital, 'font4'); // New global node default
  772. checkFont(opt.mono, 'monodef'); // Mono should have defaults again
  773. opt = h.fontOption(3);
  774. checkFont(opt, 'font6'); // New base font
  775. checkFont(opt.bold, 'font1'); // group bold option removed, using global default node
  776. checkFont(opt.ital, 'font4'); // New global node default
  777. assert.equal(opt.mono.color, '#060606'); // New color
  778. assert.equal(opt.mono.face, 'monospace');
  779. assert.equal(opt.mono.size, 15);
  780. opt = h.fontOption(4);
  781. checkFont(opt, 'default');
  782. checkFont(opt.bold, 'font6');
  783. checkFont(opt.ital, 'font4');
  784. assert.equal(opt.mono.face, 'monospace');
  785. assert.equal(opt.mono.size, 15);
  786. done();
  787. });
  788. it('deals with dynamic change of global node default', function (done) {
  789. var [network, data] = createNetwork();
  790. var h = new HelperNode(network);
  791. dynamicAdd1(network, data); // Accumulate data of dynamic add
  792. dynamicAdd2(network, data);
  793. var opt = h.fontOption(1);
  794. checkFont(opt, 'font5'); // Node instance value
  795. checkFont(opt.bold, 'font5'); // bold def removed from global default node
  796. checkFont(opt.ital, 'font5'); // idem
  797. assert.equal(opt.mono.color, '#050505'); // New color
  798. assert.equal(opt.mono.face, 'monospace');
  799. assert.equal(opt.mono.size, 15);
  800. opt = h.fontOption(2);
  801. checkFont(opt, 'font7'); // global node default applies for all settings
  802. checkFont(opt.bold, 'font7');
  803. checkFont(opt.ital, 'font7');
  804. assert.equal(opt.mono.color, '#070707');
  805. assert.equal(opt.mono.face, 'monospace');
  806. assert.equal(opt.mono.size, 15);
  807. opt = h.fontOption(3);
  808. checkFont(opt, 'font6'); // Group base font
  809. checkFont(opt.bold, 'font6'); // idem
  810. checkFont(opt.ital, 'font6'); // idem
  811. assert.equal(opt.mono.color, '#060606'); // idem
  812. assert.equal(opt.mono.face, 'monospace');
  813. assert.equal(opt.mono.size, 15);
  814. opt = h.fontOption(4);
  815. checkFont(opt, 'font7'); // global node default
  816. checkFont(opt.bold, 'font6'); // node instance bold
  817. checkFont(opt.ital, 'font7'); // global node default
  818. assert.equal(opt.mono.color, '#070707'); // idem
  819. assert.equal(opt.mono.face, 'monospace');
  820. assert.equal(opt.mono.size, 15);
  821. done();
  822. });
  823. it('deals with dynamic delete of shorthand options', function (done) {
  824. var [network, data] = createNetwork();
  825. var h = new HelperNode(network);
  826. dynamicAdd1(network, data); // Accumulate data of previous dynamic steps
  827. dynamicAdd2(network, data); // idem
  828. data.nodes.update([
  829. {id: 1, font: null},
  830. {id: 4, font: { bold: null}},
  831. ]);
  832. var opt;
  833. /*
  834. // Interesting: following flagged as error in options parsing, avoiding it for that reason
  835. network.setOptions({
  836. nodes: {
  837. font: {
  838. multi: true,
  839. ital: null,
  840. }
  841. },
  842. });
  843. */
  844. network.setOptions({
  845. groups: {
  846. group1: {
  847. font: {
  848. bold: null
  849. }
  850. },
  851. group2: {
  852. font: null
  853. }
  854. }
  855. });
  856. // global defaults for all
  857. for (let n = 1; n <= 4; ++ n) {
  858. opt = h.fontOption(n);
  859. checkFont(opt, 'font7');
  860. checkFont(opt.bold, 'font7');
  861. checkFont(opt.ital, 'font7');
  862. assert.equal(opt.mono.color, '#070707');
  863. assert.equal(opt.mono.face, 'monospace');
  864. assert.equal(opt.mono.size, 15);
  865. }
  866. /*
  867. // Not testing following because it is an error in options parsing
  868. network.setOptions({
  869. nodes: {
  870. font: null
  871. },
  872. });
  873. */
  874. done();
  875. });
  876. }); // Shorthand Font Options
  877. it('sets and uses font.multi in group options', function (done) {
  878. /**
  879. * Helper function for easily accessing font options in a node
  880. */
  881. var fontOption = (index) => {
  882. var nodes = network.body.nodes;
  883. return nodes[index].labelModule.fontOptions;
  884. };
  885. /**
  886. * Helper function for easily accessing bold options in a node
  887. */
  888. var modBold = (index) => {
  889. return fontOption(index).bold;
  890. };
  891. var dataNodes = [
  892. {id: 1, label: '<b>1</b>', group: 'group1'},
  893. {
  894. // From example 1 in #3408
  895. id: 6,
  896. label: '<i>\uf286</i> <b>\uf2cd</b> colored glyph icon',
  897. shape: 'icon',
  898. group: 'colored',
  899. icon : { color: 'blue' },
  900. font:
  901. {
  902. bold : { color : 'blue' },
  903. ital : { color : 'green' }
  904. }
  905. },
  906. ];
  907. // create a network
  908. var container = document.getElementById('mynetwork');
  909. var data = {
  910. nodes: new vis.DataSet(dataNodes),
  911. edges: []
  912. };
  913. var options = {
  914. groups: {
  915. group1: {
  916. font: {
  917. multi: true,
  918. color: 'red'
  919. },
  920. },
  921. colored :
  922. {
  923. // From example 1 in 3408
  924. icon :
  925. {
  926. face : 'FontAwesome',
  927. code : '\uf2b5',
  928. },
  929. font:
  930. {
  931. face : 'FontAwesome',
  932. multi: true,
  933. bold : { mod : '' },
  934. ital : { mod : '' }
  935. }
  936. },
  937. },
  938. };
  939. var network = new vis.Network(container, data, options);
  940. assert.equal(modBold(1).color, 'red'); // Group value
  941. assert(fontOption(1).multi); // Group value
  942. assert.equal(modBold(6).color, 'blue'); // node instance value
  943. assert(fontOption(6).multi); // Group value
  944. network.setOptions({
  945. groups: {
  946. group1: {
  947. //font: { color: 'brown' }, // Can not just change one field, entire font object is reset
  948. font: {
  949. multi: true,
  950. color: 'brown'
  951. },
  952. },
  953. },
  954. });
  955. assert.equal(modBold(1).color, 'brown'); // New value
  956. assert(fontOption(1).multi); // Group value
  957. assert.equal(modBold(6).color, 'blue'); // unchanged
  958. assert(fontOption(6).multi); // unchanged
  959. network.setOptions({
  960. groups: {
  961. group1: {
  962. font: null, // Remove font from group
  963. },
  964. },
  965. });
  966. // console.log("===============");
  967. // console.log(fontOption(1));
  968. assert.equal(modBold(1).color, '#343434'); // Reverts to default
  969. assert(!fontOption(1).multi); // idem
  970. assert.equal(modBold(6).color, 'blue'); // unchanged
  971. assert(fontOption(6).multi); // unchanged
  972. done();
  973. });
  974. it('compresses spaces for Multi-Font', function (done) {
  975. var options = getOptions(options);
  976. var text = [
  977. "Too many spaces here!",
  978. "one two three four five six .",
  979. "This thing:\n - could be\n - a kind\n - of list", // multifont: 2 spaces at start line reduced to 1
  980. ];
  981. //
  982. // multifont disabled: spaces are preserved
  983. //
  984. var label = new Label({}, options);
  985. var expected = [{
  986. lines: [{
  987. blocks: [{text: "Too many spaces here!"}],
  988. }]
  989. }, {
  990. lines: [{
  991. blocks: [{text: "one two three four five six ."}],
  992. }]
  993. }, {
  994. lines: [{
  995. blocks: [{text: "This thing:"}],
  996. }, {
  997. blocks: [{text: " - could be"}],
  998. }, {
  999. blocks: [{text: " - a kind"}],
  1000. }, {
  1001. blocks: [{text: " - of list"}],
  1002. }]
  1003. }];
  1004. checkProcessedLabels(label, text, expected);
  1005. //
  1006. // multifont disabled width maxwidth: spaces are preserved
  1007. //
  1008. options.font.maxWdt = 300;
  1009. var label = new Label({}, options);
  1010. var expected_maxwidth = [{
  1011. lines: [{
  1012. blocks: [{text: "Too many spaces"}],
  1013. }, {
  1014. blocks: [{text: " here!"}],
  1015. }]
  1016. }, {
  1017. lines: [{
  1018. blocks: [{text: "one two three "}],
  1019. }, {
  1020. blocks: [{text: "four five six"}],
  1021. }, {
  1022. blocks: [{text: " ."}],
  1023. }]
  1024. }, {
  1025. lines: [{
  1026. blocks: [{text: "This thing:"}],
  1027. }, {
  1028. blocks: [{text: " - could be"}],
  1029. }, {
  1030. blocks: [{text: " - a kind"}],
  1031. }, {
  1032. blocks: [{text: " - of list"}],
  1033. }]
  1034. }];
  1035. checkProcessedLabels(label, text, expected_maxwidth);
  1036. //
  1037. // multifont enabled: spaces are compressed
  1038. //
  1039. options = getOptions(options);
  1040. options.font.multi = true;
  1041. var label = new Label({}, options);
  1042. var expected_multifont = [{
  1043. lines: [{
  1044. blocks: [{text: "Too many spaces here!"}],
  1045. }]
  1046. }, {
  1047. lines: [{
  1048. blocks: [{text: "one two three four five six ."}],
  1049. }]
  1050. }, {
  1051. lines: [{
  1052. blocks: [{text: "This thing:"}],
  1053. }, {
  1054. blocks: [{text: " - could be"}],
  1055. }, {
  1056. blocks: [{text: " - a kind"}],
  1057. }, {
  1058. blocks: [{text: " - of list"}],
  1059. }]
  1060. }];
  1061. checkProcessedLabels(label, text, expected_multifont);
  1062. //
  1063. // multifont enabled with max width: spaces are compressed
  1064. //
  1065. options.font.maxWdt = 300;
  1066. var label = new Label({}, options);
  1067. var expected_multifont_maxwidth = [{
  1068. lines: [{
  1069. blocks: [{text: "Too many spaces"}],
  1070. }, {
  1071. blocks: [{text: "here!"}],
  1072. }]
  1073. }, {
  1074. lines: [{
  1075. blocks: [{text: "one two three four"}],
  1076. }, {
  1077. blocks: [{text: "five six ."}],
  1078. }]
  1079. }, {
  1080. lines: [{
  1081. blocks: [{text: "This thing:"}],
  1082. }, {
  1083. blocks: [{text: " - could be"}],
  1084. }, {
  1085. blocks: [{text: " - a kind"}],
  1086. }, {
  1087. blocks: [{text: " - of list"}],
  1088. }]
  1089. }];
  1090. checkProcessedLabels(label, text, expected_multifont_maxwidth);
  1091. done();
  1092. });
  1093. }); // Multi-Fonts
  1094. it('parses single huge word on line with preceding whitespace when max width set', function (done) {
  1095. var options = getOptions(options);
  1096. options.font.maxWdt = 300;
  1097. assert.equal(options.font.multi, false);
  1098. /**
  1099. * Split a string at the given location, return either first or last part
  1100. *
  1101. * Allows negative indexing, counting from back (ruby style)
  1102. */
  1103. let splitAt = (text, pos, getFirst) => {
  1104. if (pos < 0) pos = text.length + pos;
  1105. if (getFirst) {
  1106. return text.substring(0, pos);
  1107. } else {
  1108. return text.substring(pos);
  1109. }
  1110. };
  1111. var label = new Label({}, options);
  1112. var longWord = "asd;lkfja;lfkdj;alkjfd;alskfj";
  1113. var text = [
  1114. "Mind the space!\n " + longWord,
  1115. "Mind the empty line!\n\n" + longWord,
  1116. "Mind the dos empty line!\r\n\r\n" + longWord
  1117. ];
  1118. var expected = [{
  1119. lines: [{
  1120. blocks: [{text: "Mind the space!"}]
  1121. }, {
  1122. blocks: [{text: ""}]
  1123. }, {
  1124. blocks: [{text: splitAt(longWord, -3, true)}]
  1125. }, {
  1126. blocks: [{text: splitAt(longWord, -3, false)}]
  1127. }]
  1128. }, {
  1129. lines: [{
  1130. blocks: [{text: "Mind the empty"}]
  1131. }, {
  1132. blocks: [{text: "line!"}]
  1133. }, {
  1134. blocks: [{text: ""}]
  1135. }, {
  1136. blocks: [{text: splitAt(longWord, -3, true)}]
  1137. }, {
  1138. blocks: [{text: splitAt(longWord, -3, false)}]
  1139. }]
  1140. }, {
  1141. lines: [{
  1142. blocks: [{text: "Mind the dos empty"}]
  1143. }, {
  1144. blocks: [{text: "line!"}]
  1145. }, {
  1146. blocks: [{text: ""}]
  1147. }, {
  1148. blocks: [{text: splitAt(longWord, -3, true)}]
  1149. }, {
  1150. blocks: [{text: splitAt(longWord, -3, false)}]
  1151. }]
  1152. }];
  1153. checkProcessedLabels(label, text, expected);
  1154. //
  1155. // Multi font enabled. For current case, output should be identical to no multi font
  1156. //
  1157. options.font.multi = true;
  1158. var label = new Label({}, options);
  1159. checkProcessedLabels(label, text, expected);
  1160. done();
  1161. });
  1162. /**
  1163. *
  1164. * The test network is derived from example `network/nodeStyles/widthHeight.html`,
  1165. * where the associated issue (i.e. widthConstraint values not copied) was most poignant.
  1166. *
  1167. * NOTE: boolean shorthand values for widthConstraint and heightConstraint do nothing.
  1168. */
  1169. it('Sets the width/height constraints in the font label options', function (done) {
  1170. var nodes = [
  1171. { id: 100, label: 'node 100'},
  1172. { id: 210, group: 'group1', label: 'node 210'},
  1173. { id: 211, widthConstraint: { minimum: 120 }, label: 'node 211'},
  1174. { id: 212, widthConstraint: { minimum: 120, maximum: 140 }, group: 'group1', label: 'node 212'}, // group override
  1175. { id: 220, widthConstraint: { maximum: 170 }, label: 'node 220'},
  1176. { id: 200, font: { multi: true }, widthConstraint: 150, label: 'node <b>200</b>'},
  1177. { id: 201, widthConstraint: 150, label: 'node 201'},
  1178. { id: 202, group: 'group2', label: 'node 202'},
  1179. { id: 203, heightConstraint: { minimum: 75, valign: 'bottom'}, group: 'group2', label: 'node 203'}, // group override
  1180. { id: 204, heightConstraint: 80, group: 'group2', label: 'node 204'}, // group override
  1181. { id: 300, heightConstraint: { minimum: 70 }, label: 'node 300'},
  1182. { id: 400, heightConstraint: { minimum: 100, valign: 'top' }, label: 'node 400'},
  1183. { id: 401, heightConstraint: { minimum: 100, valign: 'middle' }, label: 'node 401'},
  1184. { id: 402, heightConstraint: { minimum: 100, valign: 'bottom' }, label: 'node 402'}
  1185. ];
  1186. var edges = [
  1187. { id: 1, from: 100, to: 210, label: "edge 1"},
  1188. { id: 2, widthConstraint: 80, from: 210, to: 211, label: "edge 2"},
  1189. { id: 3, heightConstraint: 90, from: 100, to: 220, label: "edge 3"},
  1190. { id: 4, from: 401, to: 402, widthConstraint: { maximum: 150 }, label: "edge 12"},
  1191. ];
  1192. var container = document.getElementById('mynetwork');
  1193. var data = {
  1194. nodes: nodes,
  1195. edges: edges
  1196. };
  1197. var options = {
  1198. edges: {
  1199. font: {
  1200. size: 12
  1201. },
  1202. widthConstraint: {
  1203. maximum: 90
  1204. }
  1205. },
  1206. nodes: {
  1207. shape: 'box',
  1208. margin: 10,
  1209. widthConstraint: {
  1210. maximum: 200
  1211. }
  1212. },
  1213. groups: {
  1214. group1: {
  1215. shape: 'dot',
  1216. widthConstraint: {
  1217. maximum: 130
  1218. }
  1219. },
  1220. // Following group serves to test all font options
  1221. group2: {
  1222. shape: 'dot',
  1223. widthConstraint: {
  1224. minimum: 150,
  1225. maximum: 180,
  1226. },
  1227. heightConstraint: {
  1228. minimum: 210,
  1229. valign: 'top',
  1230. }
  1231. },
  1232. },
  1233. physics: {
  1234. enabled: false
  1235. }
  1236. };
  1237. var network = new vis.Network(container, data, options);
  1238. var nodes_expected = [
  1239. { nodeId: 100, minWdt: -1, maxWdt: 200, minHgt: -1, valign: 'middle'},
  1240. { nodeId: 210, minWdt: -1, maxWdt: 130, minHgt: -1, valign: 'middle'},
  1241. { nodeId: 211, minWdt: 120, maxWdt: 200, minHgt: -1, valign: 'middle'},
  1242. { nodeId: 212, minWdt: 120, maxWdt: 140, minHgt: -1, valign: 'middle'},
  1243. { nodeId: 220, minWdt: -1, maxWdt: 170, minHgt: -1, valign: 'middle'},
  1244. { nodeId: 200, minWdt: 150, maxWdt: 150, minHgt: -1, valign: 'middle'},
  1245. { nodeId: 201, minWdt: 150, maxWdt: 150, minHgt: -1, valign: 'middle'},
  1246. { nodeId: 202, minWdt: 150, maxWdt: 180, minHgt: 210, valign: 'top'},
  1247. { nodeId: 203, minWdt: 150, maxWdt: 180, minHgt: 75, valign: 'bottom'},
  1248. { nodeId: 204, minWdt: 150, maxWdt: 180, minHgt: 80, valign: 'middle'},
  1249. { nodeId: 300, minWdt: -1, maxWdt: 200, minHgt: 70, valign: 'middle'},
  1250. { nodeId: 400, minWdt: -1, maxWdt: 200, minHgt: 100, valign: 'top'},
  1251. { nodeId: 401, minWdt: -1, maxWdt: 200, minHgt: 100, valign: 'middle'},
  1252. { nodeId: 402, minWdt: -1, maxWdt: 200, minHgt: 100, valign: 'bottom'},
  1253. ];
  1254. // For edge labels, only maxWdt is set. We check the rest anyway, be it for
  1255. // checking incorrect settings or for future code changes.
  1256. //
  1257. // There is a lot of repetitiveness here. Perhaps using a direct copy of the
  1258. // example should be let go.
  1259. var edges_expected = [
  1260. { id: 1, minWdt: -1, maxWdt: 90, minHgt: -1, valign: 'middle'},
  1261. { id: 2, minWdt: 80, maxWdt: 80, minHgt: -1, valign: 'middle'},
  1262. { id: 3, minWdt: -1, maxWdt: 90, minHgt: 90, valign: 'middle'},
  1263. { id: 4, minWdt: -1, maxWdt: 150, minHgt: -1, valign: 'middle'},
  1264. ];
  1265. let assertConstraints = (expected, fontOptions, label) => {
  1266. assert.equal(expected.minWdt, fontOptions.minWdt, 'Incorrect min width' + label);
  1267. assert.equal(expected.maxWdt, fontOptions.maxWdt, 'Incorrect max width' + label);
  1268. assert.equal(expected.minHgt, fontOptions.minHgt, 'Incorrect min height' + label);
  1269. assert.equal(expected.valign, fontOptions.valign, 'Incorrect valign' + label);
  1270. }
  1271. // Check nodes
  1272. util.forEach(nodes_expected, function(expected) {
  1273. let networkNode = network.body.nodes[expected.nodeId];
  1274. assert(networkNode !== undefined && networkNode !== null, 'node not found for id: ' + expected.nodeId);
  1275. let fontOptions = networkNode.labelModule.fontOptions;
  1276. var label = ' for node id: ' + expected.nodeId;
  1277. assertConstraints(expected, fontOptions, label);
  1278. });
  1279. // Check edges
  1280. util.forEach(edges_expected, function(expected) {
  1281. let networkEdge = network.body.edges[expected.id];
  1282. var label = ' for edge id: ' + expected.id;
  1283. assert(networkEdge !== undefined, 'Edge not found' + label);
  1284. let fontOptions = networkEdge.labelModule.fontOptions;
  1285. assertConstraints(expected, fontOptions, label);
  1286. });
  1287. done();
  1288. });
  1289. it('deals with null labels and other awkward values', function (done) {
  1290. var ctx = new DummyContext();
  1291. var options = getOptions({});
  1292. var checkHandling = (label, index, text) => {
  1293. assert.doesNotThrow(() => {label.getTextSize(ctx, false, false)}, "Unexpected throw for " + text + " " + index);
  1294. //label.getTextSize(ctx, false, false); // Use this to determine the error thrown
  1295. // There should not be a label for any of the cases
  1296. //
  1297. let labelVal = label.elementOptions.label;
  1298. let validLabel = (typeof labelVal === 'string' && labelVal !== '');
  1299. assert(!validLabel, "Unexpected label value '" + labelVal+ "' for " + text +" " + index);
  1300. };
  1301. var nodes = [
  1302. {id: 1},
  1303. {id: 2, label: null},
  1304. {id: 3, label: undefined},
  1305. {id: 4, label: {a: 42}},
  1306. {id: 5, label: [ 'an', 'array']},
  1307. {id: 6, label: true},
  1308. {id: 7, label: 3.419},
  1309. ];
  1310. var edges = [
  1311. {from: 1, to: 2, label: null},
  1312. {from: 1, to: 3, label: undefined},
  1313. {from: 1, to: 4, label: {a: 42}},
  1314. {from: 1, to: 5, label: ['an', 'array']},
  1315. {from: 1, to: 6, label: false},
  1316. {from: 1, to: 7, label: 2.71828},
  1317. ];
  1318. // Isolate the specific call where a problem with null-label was detected
  1319. // Following loops should plain not throw
  1320. // Node labels
  1321. for (let i = 0; i < nodes.length; ++i) {
  1322. let label = new Label(null, nodes[i], false);
  1323. checkHandling(label, i, 'node');
  1324. }
  1325. // Edge labels
  1326. for (let i = 0; i < edges.length; ++i) {
  1327. let label = new Label(null, edges[i], true);
  1328. checkHandling(label, i, 'edge');
  1329. }
  1330. //
  1331. // Following extracted from example 'nodeLegend', where the problem was detected.
  1332. //
  1333. // In the example, only `label:null` was present. The weird thing is that it fails
  1334. // in the example, but succeeds in the unit tests.
  1335. // Kept in for regression testing.
  1336. var container = document.getElementById('mynetwork');
  1337. var data = {
  1338. nodes: new vis.DataSet(nodes),
  1339. edges: new vis.DataSet(edges)
  1340. };
  1341. var options = {};
  1342. var network = new vis.Network(container, data, options);
  1343. done();
  1344. });
  1345. });