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.

589 lines
19 KiB

9 years ago
9 years ago
10 years ago
  1. var util = require('../../../util');
  2. var Label = require('./shared/Label').default;
  3. var Box = require('./nodes/shapes/Box').default;
  4. var Circle = require('./nodes/shapes/Circle').default;
  5. var CircularImage = require('./nodes/shapes/CircularImage').default;
  6. var Database = require('./nodes/shapes/Database').default;
  7. var Diamond = require('./nodes/shapes/Diamond').default;
  8. var Dot = require('./nodes/shapes/Dot').default;
  9. var Ellipse = require('./nodes/shapes/Ellipse').default;
  10. var Icon = require('./nodes/shapes/Icon').default;
  11. var Image = require('./nodes/shapes/Image').default;
  12. var Square = require('./nodes/shapes/Square').default;
  13. var Star = require('./nodes/shapes/Star').default;
  14. var Text = require('./nodes/shapes/Text').default;
  15. var Triangle = require('./nodes/shapes/Triangle').default;
  16. var TriangleDown = require('./nodes/shapes/TriangleDown').default;
  17. var { printStyle } = require("../../../shared/Validator");
  18. /**
  19. * A node. A node can be connected to other nodes via one or multiple edges.
  20. */
  21. class Node {
  22. /**
  23. *
  24. * @param {object} options An object containing options for the node. All
  25. * options are optional, except for the id.
  26. * {number} id Id of the node. Required
  27. * {string} label Text label for the node
  28. * {number} x Horizontal position of the node
  29. * {number} y Vertical position of the node
  30. * {string} shape Node shape, available:
  31. * "database", "circle", "ellipse",
  32. * "box", "image", "text", "dot",
  33. * "star", "triangle", "triangleDown",
  34. * "square", "icon"
  35. * {string} image An image url
  36. * {string} title An title text, can be HTML
  37. * {anytype} group A group name or number
  38. * @param {Object} body
  39. * @param {Network.Images} imagelist A list with images. Only needed
  40. * when the node has an image
  41. * @param {Groups} grouplist A list with groups. Needed for
  42. * retrieving group options
  43. * @param {Object} globalOptions An object with default values for
  44. * example for the color
  45. * @param {Object} defaultOptions
  46. * @param {Object} nodeOptions
  47. */
  48. constructor(options, body, imagelist, grouplist, globalOptions, defaultOptions, nodeOptions) {
  49. this.options = util.bridgeObject(globalOptions);
  50. this.globalOptions = globalOptions;
  51. this.defaultOptions = defaultOptions;
  52. this.nodeOptions = nodeOptions;
  53. this.body = body;
  54. this.edges = []; // all edges connected to this node
  55. // set defaults for the options
  56. this.id = undefined;
  57. this.imagelist = imagelist;
  58. this.grouplist = grouplist;
  59. // state options
  60. this.x = undefined;
  61. this.y = undefined;
  62. this.baseSize = this.options.size;
  63. this.baseFontSize = this.options.font.size;
  64. this.predefinedPosition = false; // used to check if initial fit should just take the range or approximate
  65. this.selected = false;
  66. this.hover = false;
  67. this.labelModule = new Label(this.body, this.options, false /* Not edge label */);
  68. this.setOptions(options);
  69. }
  70. /**
  71. * Attach a edge to the node
  72. * @param {Edge} edge
  73. */
  74. attachEdge(edge) {
  75. if (this.edges.indexOf(edge) === -1) {
  76. this.edges.push(edge);
  77. }
  78. }
  79. /**
  80. * Detach a edge from the node
  81. *
  82. * @param {Edge} edge
  83. */
  84. detachEdge(edge) {
  85. var index = this.edges.indexOf(edge);
  86. if (index != -1) {
  87. this.edges.splice(index, 1);
  88. }
  89. }
  90. /**
  91. * Set or overwrite options for the node
  92. *
  93. * @param {Object} options an object with options
  94. * @returns {null|boolean}
  95. */
  96. setOptions(options) {
  97. let currentShape = this.options.shape;
  98. if (!options) {
  99. return; // Note that the return value will be 'undefined'! This is OK.
  100. }
  101. // basic options
  102. if (options.id !== undefined) {this.id = options.id;}
  103. if (this.id === undefined) {
  104. throw "Node must have an id";
  105. }
  106. Node.checkMass(options, this.id);
  107. // set these options locally
  108. // clear x and y positions
  109. if (options.x !== undefined) {
  110. if (options.x === null) {this.x = undefined; this.predefinedPosition = false;}
  111. else {this.x = parseInt(options.x); this.predefinedPosition = true;}
  112. }
  113. if (options.y !== undefined) {
  114. if (options.y === null) {this.y = undefined; this.predefinedPosition = false;}
  115. else {this.y = parseInt(options.y); this.predefinedPosition = true;}
  116. }
  117. if (options.size !== undefined) {this.baseSize = options.size;}
  118. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  119. // copy group options
  120. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  121. var groupObj = this.grouplist.get(options.group);
  122. util.deepExtend(this.options, groupObj);
  123. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  124. this.options.color = util.parseColor(this.options.color);
  125. }
  126. // this transforms all shorthands into fully defined options
  127. Node.parseOptions(this.options, options, true, this.globalOptions);
  128. this.choosify(options);
  129. this._load_images();
  130. this.updateLabelModule(options);
  131. this.updateShape(currentShape);
  132. this.labelModule.propagateFonts(this.nodeOptions, options, this.defaultOptions);
  133. return (options.hidden !== undefined || options.physics !== undefined);
  134. }
  135. /**
  136. * Load the images from the options, for the nodes that need them.
  137. *
  138. * TODO: The imageObj members should be moved to CircularImageBase.
  139. * It's the only place where they are required.
  140. *
  141. * @private
  142. */
  143. _load_images() {
  144. // Don't bother loading for nodes without images
  145. if (this.options.shape !== 'circularImage' && this.options.shape !== 'image') {
  146. return;
  147. }
  148. if (this.options.image === undefined) {
  149. throw "Option image must be defined for node type '" + this.options.shape + "'";
  150. }
  151. if (this.imagelist === undefined) {
  152. throw "Internal Error: No images provided";
  153. }
  154. if (typeof this.options.image === 'string') {
  155. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  156. } else {
  157. if (this.options.image.unselected === undefined) {
  158. throw "No unselected image provided";
  159. }
  160. this.imageObj = this.imagelist.load(this.options.image.unselected, this.options.brokenImage, this.id);
  161. if (this.options.image.selected !== undefined) {
  162. this.imageObjAlt = this.imagelist.load(this.options.image.selected, this.options.brokenImage, this.id);
  163. } else {
  164. this.imageObjAlt = undefined;
  165. }
  166. }
  167. }
  168. /**
  169. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  170. * Static so it can also be used by the handler.
  171. *
  172. * @param {Object} parentOptions
  173. * @param {Object} newOptions
  174. * @param {boolean} [allowDeletion=false]
  175. * @param {Object} [globalOptions={}]
  176. */
  177. static parseOptions(parentOptions, newOptions, allowDeletion = false, globalOptions = {}) {
  178. var fields = [
  179. 'color',
  180. 'font',
  181. 'fixed',
  182. 'shadow'
  183. ];
  184. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  185. Node.checkMass(newOptions);
  186. // merge the shadow options into the parent.
  187. util.mergeOptions(parentOptions, newOptions, 'shadow', globalOptions);
  188. // individual shape newOptions
  189. if (newOptions.color !== undefined && newOptions.color !== null) {
  190. let parsedColor = util.parseColor(newOptions.color);
  191. util.fillIfDefined(parentOptions.color, parsedColor);
  192. }
  193. else if (allowDeletion === true && newOptions.color === null) {
  194. parentOptions.color = util.bridgeObject(globalOptions.color); // set the object back to the global options
  195. }
  196. // handle the fixed options
  197. if (newOptions.fixed !== undefined && newOptions.fixed !== null) {
  198. if (typeof newOptions.fixed === 'boolean') {
  199. parentOptions.fixed.x = newOptions.fixed;
  200. parentOptions.fixed.y = newOptions.fixed;
  201. }
  202. else {
  203. if (newOptions.fixed.x !== undefined && typeof newOptions.fixed.x === 'boolean') {
  204. parentOptions.fixed.x = newOptions.fixed.x;
  205. }
  206. if (newOptions.fixed.y !== undefined && typeof newOptions.fixed.y === 'boolean') {
  207. parentOptions.fixed.y = newOptions.fixed.y;
  208. }
  209. }
  210. }
  211. // handle the font options
  212. if (newOptions.font !== undefined && newOptions.font !== null) {
  213. Label.parseOptions(parentOptions.font, newOptions);
  214. }
  215. else if (allowDeletion === true && newOptions.font === null) {
  216. parentOptions.font = util.bridgeObject(globalOptions.font); // set the object back to the global options
  217. }
  218. // handle the scaling options, specifically the label part
  219. if (newOptions.scaling !== undefined) {
  220. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label', globalOptions.scaling);
  221. }
  222. }
  223. /**
  224. *
  225. * @param {Object} options
  226. */
  227. choosify(options) {
  228. this.chooser = true;
  229. let pile = [options, this.options, this.defaultOptions];
  230. let chosen = util.topMost(pile, 'chosen');
  231. if (typeof chosen === 'boolean') {
  232. this.chooser = chosen;
  233. } else if (typeof chosen === 'object') {
  234. let chosenNode = util.topMost(pile, ['chosen', 'node']);
  235. if ((typeof chosenNode === 'boolean') || (typeof chosenNode === 'function')) {
  236. this.chooser = chosenNode;
  237. }
  238. }
  239. }
  240. /**
  241. *
  242. * @returns {{color: *, borderWidth: *, borderColor: *, size: *, borderDashes: (boolean|Array|allOptions.nodes.shapeProperties.borderDashes|{boolean, array}), borderRadius: (number|allOptions.nodes.shapeProperties.borderRadius|{number}|Array), shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *}}
  243. */
  244. getFormattingValues() {
  245. let values = {
  246. color: this.options.color.background,
  247. borderWidth: this.options.borderWidth,
  248. borderColor: this.options.color.border,
  249. size: this.options.size,
  250. borderDashes: this.options.shapeProperties.borderDashes,
  251. borderRadius: this.options.shapeProperties.borderRadius,
  252. shadow: this.options.shadow.enabled,
  253. shadowColor: this.options.shadow.color,
  254. shadowSize: this.options.shadow.size,
  255. shadowX: this.options.shadow.x,
  256. shadowY: this.options.shadow.y
  257. };
  258. if (this.selected || this.hover) {
  259. if (this.chooser === true) {
  260. if (this.selected) {
  261. values.borderWidth *= 2;
  262. values.color = this.options.color.highlight.background;
  263. values.borderColor = this.options.color.highlight.border;
  264. values.shadow = this.options.shadow.enabled;
  265. } else if (this.hover) {
  266. values.color = this.options.color.hover.background;
  267. values.borderColor = this.options.color.hover.border;
  268. values.shadow = this.options.shadow.enabled;
  269. }
  270. } else if (typeof this.chooser === 'function') {
  271. this.chooser(values, this.options.id, this.selected, this.hover);
  272. if (values.shadow === false) {
  273. if ((values.shadowColor !== this.options.shadow.color) ||
  274. (values.shadowSize !== this.options.shadow.size) ||
  275. (values.shadowX !== this.options.shadow.x) ||
  276. (values.shadowY !== this.options.shadow.y)) {
  277. values.shadow = true;
  278. }
  279. }
  280. }
  281. } else {
  282. values.shadow = this.options.shadow.enabled;
  283. }
  284. return values;
  285. }
  286. /**
  287. *
  288. * @param {Object} options
  289. */
  290. updateLabelModule(options) {
  291. if (this.options.label === undefined || this.options.label === null) {
  292. this.options.label = '';
  293. }
  294. this.labelModule.setOptions(this.options, true);
  295. if (this.labelModule.baseSize !== undefined) {
  296. this.baseFontSize = this.labelModule.baseSize;
  297. }
  298. this.labelModule.constrain(this.nodeOptions, options, this.defaultOptions);
  299. this.labelModule.choosify(this.nodeOptions, options, this.defaultOptions);
  300. }
  301. /**
  302. *
  303. * @param {string} currentShape
  304. */
  305. updateShape(currentShape) {
  306. if (currentShape === this.options.shape && this.shape) {
  307. this.shape.setOptions(this.options, this.imageObj, this.imageObjAlt);
  308. }
  309. else {
  310. // choose draw method depending on the shape
  311. switch (this.options.shape) {
  312. case 'box':
  313. this.shape = new Box(this.options, this.body, this.labelModule);
  314. break;
  315. case 'circle':
  316. this.shape = new Circle(this.options, this.body, this.labelModule);
  317. break;
  318. case 'circularImage':
  319. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj, this.imageObjAlt);
  320. break;
  321. case 'database':
  322. this.shape = new Database(this.options, this.body, this.labelModule);
  323. break;
  324. case 'diamond':
  325. this.shape = new Diamond(this.options, this.body, this.labelModule);
  326. break;
  327. case 'dot':
  328. this.shape = new Dot(this.options, this.body, this.labelModule);
  329. break;
  330. case 'ellipse':
  331. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  332. break;
  333. case 'icon':
  334. this.shape = new Icon(this.options, this.body, this.labelModule);
  335. break;
  336. case 'image':
  337. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj, this.imageObjAlt);
  338. break;
  339. case 'square':
  340. this.shape = new Square(this.options, this.body, this.labelModule);
  341. break;
  342. case 'star':
  343. this.shape = new Star(this.options, this.body, this.labelModule);
  344. break;
  345. case 'text':
  346. this.shape = new Text(this.options, this.body, this.labelModule);
  347. break;
  348. case 'triangle':
  349. this.shape = new Triangle(this.options, this.body, this.labelModule);
  350. break;
  351. case 'triangleDown':
  352. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  353. break;
  354. default:
  355. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  356. break;
  357. }
  358. }
  359. this.needsRefresh();
  360. }
  361. /**
  362. * select this node
  363. */
  364. select() {
  365. this.selected = true;
  366. this.needsRefresh();
  367. }
  368. /**
  369. * unselect this node
  370. */
  371. unselect() {
  372. this.selected = false;
  373. this.needsRefresh();
  374. }
  375. /**
  376. * Reset the calculated size of the node, forces it to recalculate its size
  377. */
  378. needsRefresh() {
  379. this.shape.refreshNeeded = true;
  380. }
  381. /**
  382. * get the title of this node.
  383. * @return {string} title The title of the node, or undefined when no title
  384. * has been set.
  385. */
  386. getTitle() {
  387. return this.options.title;
  388. }
  389. /**
  390. * Calculate the distance to the border of the Node
  391. * @param {CanvasRenderingContext2D} ctx
  392. * @param {Number} angle Angle in radians
  393. * @returns {number} distance Distance to the border in pixels
  394. */
  395. distanceToBorder(ctx, angle) {
  396. return this.shape.distanceToBorder(ctx,angle);
  397. }
  398. /**
  399. * Check if this node has a fixed x and y position
  400. * @return {boolean} true if fixed, false if not
  401. */
  402. isFixed() {
  403. return (this.options.fixed.x && this.options.fixed.y);
  404. }
  405. /**
  406. * check if this node is selecte
  407. * @return {boolean} selected True if node is selected, else false
  408. */
  409. isSelected() {
  410. return this.selected;
  411. }
  412. /**
  413. * Retrieve the value of the node. Can be undefined
  414. * @return {Number} value
  415. */
  416. getValue() {
  417. return this.options.value;
  418. }
  419. /**
  420. * Adjust the value range of the node. The node will adjust it's size
  421. * based on its value.
  422. * @param {Number} min
  423. * @param {Number} max
  424. * @param {Number} total
  425. */
  426. setValueRange(min, max, total) {
  427. if (this.options.value !== undefined) {
  428. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  429. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  430. if (this.options.scaling.label.enabled === true) {
  431. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  432. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  433. }
  434. this.options.size = this.options.scaling.min + scale * sizeDiff;
  435. }
  436. else {
  437. this.options.size = this.baseSize;
  438. this.options.font.size = this.baseFontSize;
  439. }
  440. this.updateLabelModule();
  441. }
  442. /**
  443. * Draw this node in the given canvas
  444. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  445. * @param {CanvasRenderingContext2D} ctx
  446. */
  447. draw(ctx) {
  448. let values = this.getFormattingValues();
  449. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover, values);
  450. }
  451. /**
  452. * Update the bounding box of the shape
  453. * @param {CanvasRenderingContext2D} ctx
  454. */
  455. updateBoundingBox(ctx) {
  456. this.shape.updateBoundingBox(this.x,this.y,ctx);
  457. }
  458. /**
  459. * Recalculate the size of this node in the given canvas
  460. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  461. * @param {CanvasRenderingContext2D} ctx
  462. */
  463. resize(ctx) {
  464. let values = this.getFormattingValues();
  465. this.shape.resize(ctx, this.selected, this.hover, values);
  466. }
  467. /**
  468. * Check if this object is overlapping with the provided object
  469. * @param {Object} obj an object with parameters left, top, right, bottom
  470. * @return {boolean} True if location is located on node
  471. */
  472. isOverlappingWith(obj) {
  473. return (
  474. this.shape.left < obj.right &&
  475. this.shape.left + this.shape.width > obj.left &&
  476. this.shape.top < obj.bottom &&
  477. this.shape.top + this.shape.height > obj.top
  478. );
  479. }
  480. /**
  481. * Check if this object is overlapping with the provided object
  482. * @param {Object} obj an object with parameters left, top, right, bottom
  483. * @return {boolean} True if location is located on node
  484. */
  485. isBoundingBoxOverlappingWith(obj) {
  486. return (
  487. this.shape.boundingBox.left < obj.right &&
  488. this.shape.boundingBox.right > obj.left &&
  489. this.shape.boundingBox.top < obj.bottom &&
  490. this.shape.boundingBox.bottom > obj.top
  491. );
  492. }
  493. /**
  494. * Check valid values for mass
  495. *
  496. * The mass may not be negative or zero. If it is, reset to 1
  497. *
  498. * @param {object} options
  499. * @param {vis.Node.id} id
  500. */
  501. static checkMass(options, id) {
  502. if (options.mass !== undefined && options.mass <= 0) {
  503. let strId = '';
  504. if (id !== undefined) {
  505. strId = ' in node id: ' + id;
  506. }
  507. console.log('%cNegative or zero mass disallowed' + strId +
  508. ', setting mass to 1.' , printStyle);
  509. options.mass = 1;
  510. }
  511. }
  512. }
  513. export default Node;