vis.js is a dynamic, browser-based visualization library
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.

743 lines
21 KiB

  1. var util = require('../../util');
  2. import ColorPicker from './components/ColorPicker'
  3. /**
  4. * The way this works is for all properties of this.possible options, you can supply the property name in any form to list the options.
  5. * Boolean options are recognised as Boolean
  6. * Number options should be written as array: [default value, min value, max value, stepsize]
  7. * Colors should be written as array: ['color', '#ffffff']
  8. * Strings with should be written as array: [option1, option2, option3, ..]
  9. *
  10. * The options are matched with their counterparts in each of the modules and the values used in the configuration are
  11. *
  12. */
  13. class ConfigurationSystem {
  14. constructor(network) {
  15. this.network = network;
  16. this.changedOptions = [];
  17. this.possibleOptions = {
  18. nodes: {
  19. borderWidth: [1, 0, 10, 1],
  20. borderWidthSelected: [2, 0, 10, 1],
  21. color: {
  22. border: ['color','#2B7CE9'],
  23. background: ['color','#97C2FC'],
  24. highlight: {
  25. border: ['color','#2B7CE9'],
  26. background: ['color','#D2E5FF']
  27. },
  28. hover: {
  29. border: ['color','#2B7CE9'],
  30. background: ['color','#D2E5FF']
  31. }
  32. },
  33. fixed: {
  34. x: false,
  35. y: false
  36. },
  37. font: {
  38. color: ['color','#343434'],
  39. size: [14, 0, 100, 1], // px
  40. face: ['arial', 'verdana', 'tahoma'],
  41. background: ['color','none'],
  42. stroke: [0, 0, 50, 1], // px
  43. strokeColor: ['color','#ffffff']
  44. },
  45. //group: 'string',
  46. hidden: false,
  47. //icon: {
  48. // face: 'string', //'FontAwesome',
  49. // code: 'string', //'\uf007',
  50. // size: [50, 0, 200, 1], //50,
  51. // color: ['color','#2B7CE9'] //'#aa00ff'
  52. //},
  53. //image: 'string', // --> URL
  54. physics: true,
  55. scaling: {
  56. min: [10, 0, 200, 1],
  57. max: [30, 0, 200, 1],
  58. label: {
  59. enabled: true,
  60. min: [14, 0, 200, 1],
  61. max: [30, 0, 200, 1],
  62. maxVisible: [30, 0, 200, 1],
  63. drawThreshold: [3, 0, 20, 1]
  64. }
  65. },
  66. shadow:{
  67. enabled: false,
  68. size:[10, 0, 20, 1],
  69. x:[5, -30, 30, 1],
  70. y:[5, -30, 30, 1]
  71. },
  72. shape: ['ellipse', 'box', 'circle', 'database', 'diamond', 'dot', 'square', 'star', 'text', 'triangle', 'triangleDown'],
  73. size: [25, 0, 200, 1]
  74. },
  75. edges: {
  76. arrows: {
  77. to: {enabled: false, scaleFactor: [1, 0, 3, 0.05]}, // boolean / {arrowScaleFactor:1} / {enabled: false, arrowScaleFactor:1}
  78. middle: {enabled: false, scaleFactor: [1, 0, 3, 0.05]},
  79. from: {enabled: false, scaleFactor: [1, 0, 3, 0.05]}
  80. },
  81. color: {
  82. color: ['color','#848484'],
  83. highlight: ['color','#848484'],
  84. hover: ['color','#848484'],
  85. inherit: {
  86. enabled: true,
  87. source: ['from', 'to'], // from / to
  88. useGradients: false
  89. },
  90. opacity: [1, 0, 1, 0.05]
  91. },
  92. dashes: {
  93. enabled: false,
  94. length: [5, 0, 50, 1],
  95. gap: [5, 0, 50, 1],
  96. altLength: [5, 0, 50, 1]
  97. },
  98. font: {
  99. color: ['color','#343434'],
  100. size: [14, 0, 100, 1], // px
  101. face: ['arial', 'verdana', 'tahoma'],
  102. background: ['color','none'],
  103. stroke: [1, 0, 50, 1], // px
  104. strokeColor: ['color','#ffffff'],
  105. align: ['horizontal', 'top', 'middle', 'bottom']
  106. },
  107. hidden: false,
  108. hoverWidth: [1.5, 0, 10, 0.1],
  109. physics: true,
  110. scaling: {
  111. min: [1, 0, 100, 1],
  112. max: [15, 0, 100, 1],
  113. label: {
  114. enabled: true,
  115. min: [14, 0, 200, 1],
  116. max: [30, 0, 200, 1],
  117. maxVisible: [30, 0, 200, 1],
  118. drawThreshold: [3, 0, 20, 1]
  119. }
  120. },
  121. selfReferenceSize: [20, 0, 200, 1],
  122. shadow:{
  123. enabled: false,
  124. size:[10, 0, 20, 1],
  125. x:[5, -30, 30, 1],
  126. y:[5, -30, 30, 1]
  127. },
  128. smooth: {
  129. enabled: true,
  130. dynamic: true,
  131. type: ['continuous', 'discrete', 'diagonalCross', 'straightCross', 'horizontal', 'vertical', 'curvedCW', 'curvedCCW'],
  132. roundness: [0.5, 0, 1, 0.05]
  133. },
  134. width: [1, 0, 30, 1],
  135. widthSelectionMultiplier: [2, 0, 5, 0.1]
  136. },
  137. layout: {
  138. randomSeed: [0, 0, 500, 1],
  139. hierarchical: {
  140. enabled: false,
  141. levelSeparation: [150, 20, 500, 5],
  142. direction: ['UD', 'DU', 'LR', 'RL'], // UD, DU, LR, RL
  143. sortMethod: ['hubsize', 'directed'] // hubsize, directed
  144. }
  145. },
  146. interaction: {
  147. dragNodes: true,
  148. dragView: true,
  149. zoomView: true,
  150. hoverEnabled: false,
  151. navigationButtons: false,
  152. tooltipDelay: [300, 0, 1000, 25],
  153. keyboard: {
  154. enabled: false,
  155. speed: {x: [10, 0, 40, 1], y: [10, 0, 40, 1], zoom: [0.02, 0, 0.1, 0.005]},
  156. bindToWindow: true
  157. }
  158. },
  159. manipulation: {
  160. enabled: false,
  161. initiallyVisible: false,
  162. locale: ['en', 'nl'],
  163. functionality: {
  164. addNode: true,
  165. addEdge: true,
  166. editNode: true,
  167. editEdge: true,
  168. deleteNode: true,
  169. deleteEdge: true
  170. }
  171. },
  172. physics: {
  173. barnesHut: {
  174. theta: [0.5, 0.1, 1, 0.05],
  175. gravitationalConstant: [-2000, -30000, 0, 50],
  176. centralGravity: [0.3, 0, 10, 0.05],
  177. springLength: [95, 0, 500, 5],
  178. springConstant: [0.04, 0, 5, 0.005],
  179. damping: [0.09, 0, 1, 0.01]
  180. },
  181. repulsion: {
  182. centralGravity: [0.2, 0, 10, 0.05],
  183. springLength: [200, 0, 500, 5],
  184. springConstant: [0.05, 0, 5, 0.005],
  185. nodeDistance: [100, 0, 500, 5],
  186. damping: [0.09, 0, 1, 0.01]
  187. },
  188. hierarchicalRepulsion: {
  189. centralGravity: [0.2, 0, 10, 0.05],
  190. springLength: [100, 0, 500, 5],
  191. springConstant: [0.01, 0, 5, 0.005],
  192. nodeDistance: [120, 0, 500, 5],
  193. damping: [0.09, 0, 1, 0.01]
  194. },
  195. maxVelocity: [50, 0, 150, 1],
  196. minVelocity: [0.1, 0.01, 0.5, 0.01],
  197. solver: ['barnesHut', 'repulsion', 'hierarchicalRepulsion'],
  198. timestep: [0.5, 0, 1, 0.05]
  199. },
  200. selection: {
  201. select: true,
  202. selectConnectedEdges: true
  203. },
  204. renderer: {
  205. hideEdgesOnDrag: false,
  206. hideNodesOnDrag: false
  207. }
  208. };
  209. this.actualOptions = {
  210. nodes:{},
  211. edges:{},
  212. layout:{},
  213. interaction:{},
  214. manipulation:{},
  215. physics:{},
  216. selection:{},
  217. renderer:{},
  218. configure: false,
  219. configureContainer: undefined
  220. };
  221. this.domElements = [];
  222. this.colorPicker = new ColorPicker(this.network.canvas.pixelRatio);
  223. }
  224. /**
  225. * refresh all options.
  226. * Because all modules parse their options by themselves, we just use their options. We copy them here.
  227. *
  228. * @param options
  229. */
  230. setOptions(options) {
  231. if (options !== undefined) {
  232. util.extend(this.actualOptions, options);
  233. }
  234. this._clean();
  235. if (this.actualOptions.configure !== undefined && this.actualOptions.configure !== false) {
  236. util.deepExtend(this.actualOptions.nodes, this.network.nodesHandler.options, true);
  237. util.deepExtend(this.actualOptions.edges, this.network.edgesHandler.options, true);
  238. util.deepExtend(this.actualOptions.layout, this.network.layoutEngine.options, true);
  239. util.deepExtend(this.actualOptions.interaction, this.network.interactionHandler.options, true);
  240. util.deepExtend(this.actualOptions.manipulation, this.network.manipulation.options, true);
  241. util.deepExtend(this.actualOptions.physics, this.network.physics.options, true);
  242. util.deepExtend(this.actualOptions.selection, this.network.selectionHandler.selection, true);
  243. util.deepExtend(this.actualOptions.renderer, this.network.renderer.selection, true);
  244. this.container = this.network.body.container;
  245. let config = true;
  246. if (typeof this.actualOptions.configure === 'string') {
  247. config = this.actualOptions.configure;
  248. }
  249. else if (this.actualOptions.configure instanceof Array) {
  250. config = this.actualOptions.configure.join();
  251. }
  252. else if (typeof this.actualOptions.configure === 'object') {
  253. if (this.actualOptions.configure.container !== undefined) {
  254. this.container = this.actualOptions.configure.container;
  255. }
  256. if (this.actualOptions.configure.filter !== undefined) {
  257. config = this.actualOptions.configure.filter;
  258. }
  259. }
  260. else if (typeof this.actualOptions.configure === 'boolean') {
  261. config = this.actualOptions.configure;
  262. }
  263. if (config === false) {
  264. this._create(config);
  265. }
  266. }
  267. }
  268. /**
  269. * Create all DOM elements
  270. * @param {Boolean | String} config
  271. * @private
  272. */
  273. _create(config) {
  274. this._clean();
  275. this.changedOptions = [];
  276. let counter = 0;
  277. for (let option in this.possibleOptions) {
  278. if (this.possibleOptions.hasOwnProperty(option)) {
  279. if (config === true || config.indexOf(option) !== -1) {
  280. let optionObj = this.possibleOptions[option];
  281. // linebreak between categories
  282. if (counter > 0) {
  283. this._makeItem([]);
  284. }
  285. // a header for the category
  286. this._makeHeader(option);
  287. // get the suboptions
  288. let path = [option];
  289. this._handleObject(optionObj, path);
  290. }
  291. counter++;
  292. }
  293. }
  294. let generateButton = document.createElement('div');
  295. generateButton.className = 'vis-network-configuration button';
  296. generateButton.innerHTML = 'generate options';
  297. generateButton.onclick = () => {this._printOptions();};
  298. generateButton.onmouseover = () => {generateButton.className = 'vis-network-configuration button hover';};
  299. generateButton.onmouseout = () => {generateButton.className = 'vis-network-configuration button';};
  300. this.optionsContainer = document.createElement('div');
  301. this.optionsContainer.className = 'vis-network-configuration vis-option-container';
  302. this.domElements.push(this.optionsContainer);
  303. this.domElements.push(generateButton);
  304. this._push();
  305. this.colorPicker.insertTo(this.container);
  306. }
  307. /**
  308. * draw all DOM elements on the screen
  309. * @private
  310. */
  311. _push() {
  312. for (var i = 0; i < this.domElements.length; i++) {
  313. this.container.appendChild(this.domElements[i]);
  314. }
  315. }
  316. /**
  317. * delete all DOM elements
  318. * @private
  319. */
  320. _clean() {
  321. for (var i = 0; i < this.domElements.length; i++) {
  322. this.container.removeChild(this.domElements[i]);
  323. }
  324. this.domElements = [];
  325. }
  326. /**
  327. * get the value from the actualOptions if it exists
  328. * @param {array} path | where to look for the actual option
  329. * @returns {*}
  330. * @private
  331. */
  332. _getValue(path) {
  333. let base = this.actualOptions;
  334. for (let i = 0; i < path.length; i++) {
  335. if (base[path[i]] !== undefined) {
  336. base = base[path[i]];
  337. }
  338. else {
  339. base = undefined;
  340. break;
  341. }
  342. }
  343. return base;
  344. }
  345. /**
  346. * Copy the path and add a step. It needs to copy because the path will keep stacking otherwise.
  347. * @param path
  348. * @param newValue
  349. * @returns {Array}
  350. * @private
  351. */
  352. _addToPath(path, newValue) {
  353. let newPath = [];
  354. for (let i = 0; i < path.length; i++) {
  355. newPath.push(path[i]);
  356. }
  357. newPath.push(newValue);
  358. return newPath;
  359. }
  360. /**
  361. * all option elements are wrapped in an item
  362. * @param path
  363. * @param domElements
  364. * @private
  365. */
  366. _makeItem(path,...domElements) {
  367. let item = document.createElement('div');
  368. item.className = 'vis-network-configuration item s' + path.length;
  369. domElements.forEach((element) => {
  370. item.appendChild(element);
  371. });
  372. this.domElements.push(item);
  373. }
  374. /**
  375. * header for major subjects
  376. * @param name
  377. * @private
  378. */
  379. _makeHeader(name) {
  380. let div = document.createElement('div');
  381. div.className = 'vis-network-configuration header';
  382. div.innerHTML = name;
  383. this._makeItem([],div);
  384. }
  385. /**
  386. * make a label, if it is an object label, it gets different styling.
  387. * @param name
  388. * @param path
  389. * @param objectLabel
  390. * @returns {HTMLElement}
  391. * @private
  392. */
  393. _makeLabel(name, path, objectLabel = false) {
  394. let div = document.createElement('div');
  395. div.className = 'vis-network-configuration label s' + path.length;
  396. if (objectLabel === true) {
  397. div.innerHTML = '<i><b>' + name + ':</b></i>';
  398. }
  399. else {
  400. div.innerHTML = name + ':';
  401. }
  402. return div;
  403. }
  404. /**
  405. * make a dropdown list for multiple possible string optoins
  406. * @param arr
  407. * @param value
  408. * @param path
  409. * @private
  410. */
  411. _makeDropdown(arr, value, path) {
  412. let select = document.createElement('select');
  413. select.className = 'vis-network-configuration select';
  414. let selectedValue = 0;
  415. if (value !== undefined) {
  416. if (arr.indexOf(value) !== -1) {
  417. selectedValue = arr.indexOf(value);
  418. }
  419. }
  420. for (let i = 0; i < arr.length; i++) {
  421. let option = document.createElement('option');
  422. option.value = arr[i];
  423. if (i === selectedValue) {
  424. option.selected = 'selected';
  425. }
  426. option.innerHTML = arr[i];
  427. select.appendChild(option);
  428. }
  429. let me = this;
  430. select.onchange = function () {me._update(this.value, path);};
  431. let label = this._makeLabel(path[path.length-1], path);
  432. this._makeItem(path, label, select);
  433. }
  434. /**
  435. * make a range object for numeric options
  436. * @param arr
  437. * @param value
  438. * @param path
  439. * @private
  440. */
  441. _makeRange(arr, value, path) {
  442. let defaultValue = arr[0];
  443. let min = arr[1];
  444. let max = arr[2];
  445. let step = arr[3];
  446. let range = document.createElement('input');
  447. range.type = 'range';
  448. range.className = 'vis-network-configuration range';
  449. range.min = min;
  450. range.max = max;
  451. range.step = step;
  452. if (value !== undefined) {
  453. if (value * 0.1 < min) {
  454. range.min = value / 10;
  455. }
  456. if (value * 2 > max && max !== 1) {
  457. range.max = value * 2;
  458. }
  459. range.value = value;
  460. }
  461. else {
  462. range.value = defaultValue;
  463. }
  464. let input = document.createElement('input');
  465. input.className = 'vis-network-configuration rangeinput';
  466. input.value = range.value;
  467. var me = this;
  468. range.onchange = function () {input.value = this.value; me._update(this.value, path);};
  469. range.oninput = function () {input.value = this.value; };
  470. let label = this._makeLabel(path[path.length-1], path);
  471. this._makeItem(path, label, range, input);
  472. }
  473. /**
  474. * make a checkbox for boolean options.
  475. * @param defaultValue
  476. * @param value
  477. * @param path
  478. * @private
  479. */
  480. _makeCheckbox(defaultValue, value, path) {
  481. var checkbox = document.createElement('input');
  482. checkbox.type = 'checkbox';
  483. checkbox.className = 'vis-network-configuration checkbox';
  484. checkbox.checked = defaultValue;
  485. if (value !== undefined) {
  486. checkbox.checked = value;
  487. if (value !== defaultValue) {
  488. if (typeof defaultValue === 'object') {
  489. if (value !== defaultValue.enabled) {
  490. this.changedOptions.push({path:path, value:value});
  491. }
  492. }
  493. else {
  494. this.changedOptions.push({path:path, value:value});
  495. }
  496. }
  497. }
  498. let me = this;
  499. checkbox.onchange = function() {me._update(this.checked, path)};
  500. let label = this._makeLabel(path[path.length-1], path);
  501. this._makeItem(path, label, checkbox);
  502. }
  503. /**
  504. * make a color field with a color picker for color fields
  505. * @param arr
  506. * @param value
  507. * @param path
  508. * @private
  509. */
  510. _makeColorField(arr, value, path) {
  511. let defaultColor = arr[1];
  512. let div = document.createElement('div');
  513. value = value === undefined ? defaultColor : value;
  514. if (value !== 'none') {
  515. div.className = 'vis-network-configuration colorBlock';
  516. div.style.backgroundColor = value;
  517. }
  518. else {
  519. div.className = 'vis-network-configuration colorBlock none';
  520. }
  521. value = value === undefined ? defaultColor : value;
  522. div.onclick = () => {
  523. this._showColorPicker(value,div,path);
  524. }
  525. let label = this._makeLabel(path[path.length-1], path);
  526. this._makeItem(path,label, div);
  527. }
  528. /**
  529. * used by the color buttons to call the color picker.
  530. * @param event
  531. * @param value
  532. * @param div
  533. * @param path
  534. * @private
  535. */
  536. _showColorPicker(value, div, path) {
  537. let rect = div.getBoundingClientRect();
  538. let bodyRect = document.body.getBoundingClientRect();
  539. let pickerX = rect.left + rect.width + 5;
  540. let pickerY = rect.top - bodyRect.top + rect.height*0.5;
  541. this.colorPicker.show(pickerX,pickerY);
  542. this.colorPicker.setColor(value);
  543. this.colorPicker.setCallback((color) => {
  544. let colorString = 'rgba(' + color.r + ',' + color.g + ',' + color.b + ',' + color.a + ')';
  545. div.style.backgroundColor = colorString;
  546. this._update(colorString,path);
  547. })
  548. }
  549. /**
  550. * parse an object and draw the correct items
  551. * @param obj
  552. * @param path
  553. * @private
  554. */
  555. _handleObject(obj, path = []) {
  556. for (let subObj in obj) {
  557. if (obj.hasOwnProperty(subObj)) {
  558. let item = obj[subObj];
  559. let newPath = this._addToPath(path, subObj);
  560. let value = this._getValue(newPath);
  561. if (item instanceof Array) {
  562. this._handleArray(item, value, newPath);
  563. }
  564. else if (typeof item === 'string') {
  565. this._handleString(item, value, newPath);
  566. }
  567. else if (typeof item === 'boolean') {
  568. this._makeCheckbox(item, value, newPath);
  569. }
  570. else if (item instanceof Object) {
  571. // collapse the physics options that are not enabled
  572. let draw = true;
  573. if (path.indexOf('physics') !== -1) {
  574. if (this.actualOptions.physics.solver !== subObj) {
  575. draw = false;
  576. }
  577. }
  578. if (draw === true) {
  579. // initially collapse options with an disabled enabled option.
  580. if (item.enabled !== undefined) {
  581. let enabledPath = this._addToPath(newPath, 'enabled');
  582. let enabledValue = this._getValue(enabledPath);
  583. if (enabledValue === true) {
  584. let label = this._makeLabel(subObj, newPath, true);
  585. this._makeItem(newPath, label);
  586. this._handleObject(item, newPath);
  587. }
  588. else {
  589. this._makeCheckbox(item, enabledValue, newPath);
  590. }
  591. }
  592. else {
  593. let label = this._makeLabel(subObj, newPath, true);
  594. this._makeItem(newPath, label);
  595. this._handleObject(item, newPath);
  596. }
  597. }
  598. }
  599. else {
  600. console.error('dont know how to handle', item, subObj, newPath);
  601. }
  602. }
  603. }
  604. }
  605. /**
  606. * handle the array type of option
  607. * @param optionName
  608. * @param arr
  609. * @param value
  610. * @param path
  611. * @private
  612. */
  613. _handleArray(arr, value, path) {
  614. if (typeof arr[0] === 'string' && arr[0] === 'color') {
  615. this._makeColorField(arr, value, path);
  616. if (arr[1] !== value) {this.changedOptions.push({path:path, value:value});}
  617. }
  618. else if (typeof arr[0] === 'string') {
  619. this._makeDropdown(arr, value, path);
  620. if (arr[0] !== value) {this.changedOptions.push({path:path, value:value});}
  621. }
  622. else if (typeof arr[0] === 'number') {
  623. this._makeRange(arr, value, path);
  624. if (arr[0] !== value) {this.changedOptions.push({path:path, value:value});}
  625. }
  626. }
  627. /**
  628. * handle the string type of option.
  629. * TODO: Not sure what to do with this
  630. * @param optionName
  631. * @param string
  632. * @param value
  633. * @param path
  634. * @private
  635. */
  636. _handleString(string, value, path) {
  637. if (string === 'string') {
  638. }
  639. else {
  640. //this._makeLabel(optionName, path);
  641. //console.log('string', string, value, path);
  642. }
  643. }
  644. /**
  645. * called to update the network with the new settings.
  646. * @param value
  647. * @param path
  648. * @private
  649. */
  650. _update(value, path) {
  651. let options = this._constructOptions(value,path);
  652. this.network.setOptions(options);
  653. }
  654. _constructOptions(value,path, optionsObj = {}) {
  655. let pointer = optionsObj;
  656. for (let i = 0; i < path.length; i++) {
  657. if (pointer[path[i]] === undefined) {
  658. pointer[path[i]] = {};
  659. }
  660. if (i !== path.length -1) {
  661. pointer = pointer[path[i]];
  662. }
  663. else {
  664. pointer[path[i]] = value;
  665. }
  666. }
  667. return optionsObj;
  668. }
  669. _printOptions() {
  670. let options = {};
  671. for (var i = 0; i < this.changedOptions.length; i++) {
  672. this._constructOptions(this.changedOptions[i].value, this.changedOptions[i].path, options)
  673. }
  674. this.optionsContainer.innerHTML = '<pre>var options = ' + JSON.stringify(options, null, 2) + '</pre>';
  675. }
  676. }
  677. export default ConfigurationSystem;