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.

568 lines
18 KiB

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