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.

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