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.

515 lines
16 KiB

9 years ago
9 years ago
9 years ago
  1. var util = require('../../../util');
  2. import Label from './shared/Label'
  3. import Box from './nodes/shapes/Box'
  4. import Circle from './nodes/shapes/Circle'
  5. import CircularImage from './nodes/shapes/CircularImage'
  6. import Database from './nodes/shapes/Database'
  7. import Diamond from './nodes/shapes/Diamond'
  8. import Dot from './nodes/shapes/Dot'
  9. import Ellipse from './nodes/shapes/Ellipse'
  10. import Icon from './nodes/shapes/Icon'
  11. import Image from './nodes/shapes/Image'
  12. import Square from './nodes/shapes/Square'
  13. import Star from './nodes/shapes/Star'
  14. import Text from './nodes/shapes/Text'
  15. import Triangle from './nodes/shapes/Triangle'
  16. import TriangleDown from './nodes/shapes/TriangleDown'
  17. import Validator from "../../../shared/Validator";
  18. import {printStyle} from "../../../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) {
  46. this.options = util.bridgeObject(globalOptions);
  47. this.globalOptions = globalOptions;
  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);
  63. this.setOptions(options);
  64. }
  65. get x() {
  66. return this._x;
  67. }
  68. set x(newX) {
  69. this._x = newX;
  70. this.body.emitter.emit('_positionUpdate', {id: this.id, x: this._x, y: this._y});
  71. }
  72. /**
  73. * Non emitting version for use by physics engine so we don't create infinite loops.
  74. * @param newX
  75. */
  76. setX(newX) {
  77. this._x = newX;
  78. }
  79. get y() {
  80. return this._y;
  81. }
  82. set y(newY) {
  83. this._y = newY;
  84. this.body.emitter.emit('_positionUpdate', {id: this.id, x: this._x, y: this._y});
  85. }
  86. /**
  87. * Emitting version
  88. *
  89. * @param newFixed
  90. */
  91. setFixed(newFixed) {
  92. // TODO split out fixed portion?
  93. let physOpts = Node.parseOptions(this.options, {fixed: newFixed});
  94. if (Object.keys(physOpts).length > 0) {
  95. this.body.emitter.emit('_physicsUpdate', {type: 'node', id: this.id, options: physOpts});
  96. }
  97. }
  98. /**
  99. * Non emitting version for use by physics engine so we don't create infinite loops.
  100. * @param newY
  101. */
  102. setY(newY) {
  103. this._y = newY;
  104. }
  105. /**
  106. * Attach a edge to the node
  107. * @param {Edge} edge
  108. */
  109. attachEdge(edge) {
  110. if (this.edges.indexOf(edge) === -1) {
  111. this.edges.push(edge);
  112. }
  113. }
  114. /**
  115. * Detach a edge from the node
  116. * @param {Edge} edge
  117. */
  118. detachEdge(edge) {
  119. var index = this.edges.indexOf(edge);
  120. if (index != -1) {
  121. this.edges.splice(index, 1);
  122. }
  123. }
  124. /**
  125. * Set or overwrite options for the node
  126. * @param {Object} options an object with options
  127. * @param {Object} constants and object with default, global options
  128. */
  129. setOptions(options) {
  130. let currentShape = this.options.shape;
  131. if (!options) {
  132. return;
  133. }
  134. // basic options
  135. if (options.id !== undefined) {this.id = options.id;}
  136. if (this.id === undefined) {
  137. throw "Node must have an id";
  138. }
  139. // set these options locally
  140. // clear x and y positions
  141. if (options.x !== undefined) {
  142. if (options.x === null) {this.x = undefined; this.predefinedPosition = false;}
  143. else {this.x = parseInt(options.x); this.predefinedPosition = true;}
  144. }
  145. if (options.y !== undefined) {
  146. if (options.y === null) {this.y = undefined; this.predefinedPosition = false;}
  147. else {this.y = parseInt(options.y); this.predefinedPosition = true;}
  148. }
  149. if (options.size !== undefined) {this.baseSize = options.size;}
  150. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  151. // copy group options
  152. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  153. var groupObj = this.grouplist.get(options.group);
  154. util.deepExtend(this.options, groupObj);
  155. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  156. this.options.color = util.parseColor(this.options.color);
  157. }
  158. // this transforms all shorthands into fully defined options
  159. let physOpts = Node.parseOptions(this.options, options, true, this.globalOptions);
  160. // load the images
  161. if (this.options.image !== undefined) {
  162. if (this.imagelist) {
  163. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  164. }
  165. else {
  166. throw "No imagelist provided";
  167. }
  168. }
  169. this.updateLabelModule();
  170. this.updateShape(currentShape);
  171. if (options.mass !== undefined) {
  172. this.options.mass = options.mass;
  173. physOpts.mass = options.mass;
  174. }
  175. if (options.physics !== undefined) {
  176. this.options.physics = options.physics;
  177. physOpts.physics = options.physics;
  178. }
  179. if (Object.keys(physOpts).length > 0) {
  180. this.body.emitter.emit('_physicsUpdate', {type: 'node', id: this.id, options: physOpts});
  181. }
  182. // TODO make embedded physics trigger this or handle _physicsUpdate messages
  183. if (options.hidden !== undefined) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  190. * Static so it can also be used by the handler.
  191. * @param parentOptions
  192. * @param newOptions
  193. */
  194. static parseOptions(parentOptions, newOptions, allowDeletion = false, globalOptions = {}) {
  195. var fields = [
  196. 'color',
  197. 'font',
  198. 'fixed',
  199. 'shadow'
  200. ];
  201. var changedPhysicsOptions = {};
  202. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  203. // merge the shadow options into the parent.
  204. util.mergeOptions(parentOptions, newOptions, 'shadow', allowDeletion, globalOptions);
  205. // individual shape newOptions
  206. if (newOptions.color !== undefined && newOptions.color !== null) {
  207. let parsedColor = util.parseColor(newOptions.color);
  208. util.fillIfDefined(parentOptions.color, parsedColor);
  209. }
  210. else if (allowDeletion === true && newOptions.color === null) {
  211. parentOptions.color = Object.create(globalOptions.color); // this sets the pointer of the option back to the global option.
  212. }
  213. // handle the fixed options
  214. if (newOptions.fixed !== undefined && newOptions.fixed !== null) {
  215. if (typeof newOptions.fixed === 'boolean') {
  216. if (parentOptions.fixed.x !== newOptions.fixed || parentOptions.fixed.y !== newOptions.fixed) {
  217. parentOptions.fixed.x = newOptions.fixed;
  218. parentOptions.fixed.y = newOptions.fixed;
  219. changedPhysicsOptions.fixed = {x: newOptions.fixed, y: newOptions.fixed};
  220. }
  221. }
  222. else {
  223. if (newOptions.fixed.x !== undefined &&
  224. typeof newOptions.fixed.x === 'boolean' &&
  225. parentOptions.fixed.x !== newOptions.fixed.x)
  226. {
  227. parentOptions.fixed.x = newOptions.fixed.x;
  228. util.deepExtend(changedPhysicsOptions, {fixed: {x: newOptions.fixed.x}});
  229. }
  230. if (newOptions.fixed.y !== undefined &&
  231. typeof newOptions.fixed.y === 'boolean' &&
  232. parentOptions.fixed.y !== newOptions.fixed.y)
  233. {
  234. parentOptions.fixed.y = newOptions.fixed.y;
  235. util.deepExtend(changedPhysicsOptions, {fixed: {y: newOptions.fixed.y}});
  236. }
  237. }
  238. }
  239. // handle the font options
  240. if (newOptions.font !== undefined && newOptions.font !== null) {
  241. Label.parseOptions(parentOptions.font, newOptions);
  242. }
  243. else if (allowDeletion === true && newOptions.font === null) {
  244. parentOptions.font = Object.create(globalOptions.font); // this sets the pointer of the option back to the global option.
  245. }
  246. // handle the scaling options, specifically the label part
  247. if (newOptions.scaling !== undefined) {
  248. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label', allowDeletion, globalOptions.scaling);
  249. }
  250. return changedPhysicsOptions;
  251. }
  252. updateLabelModule() {
  253. if (this.options.label === undefined || this.options.label === null) {
  254. this.options.label = '';
  255. }
  256. this.labelModule.setOptions(this.options, true);
  257. if (this.labelModule.baseSize !== undefined) {
  258. this.baseFontSize = this.labelModule.baseSize;
  259. }
  260. }
  261. updateShape(currentShape) {
  262. if (currentShape === this.options.shape && this.shape) {
  263. this.shape.setOptions(this.options, this.imageObj);
  264. }
  265. else {
  266. // choose draw method depending on the shape
  267. switch (this.options.shape) {
  268. case 'box':
  269. this.shape = new Box(this.options, this.body, this.labelModule);
  270. break;
  271. case 'circle':
  272. this.shape = new Circle(this.options, this.body, this.labelModule);
  273. break;
  274. case 'circularImage':
  275. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  276. break;
  277. case 'database':
  278. this.shape = new Database(this.options, this.body, this.labelModule);
  279. break;
  280. case 'diamond':
  281. this.shape = new Diamond(this.options, this.body, this.labelModule);
  282. break;
  283. case 'dot':
  284. this.shape = new Dot(this.options, this.body, this.labelModule);
  285. break;
  286. case 'ellipse':
  287. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  288. break;
  289. case 'icon':
  290. this.shape = new Icon(this.options, this.body, this.labelModule);
  291. break;
  292. case 'image':
  293. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  294. break;
  295. case 'square':
  296. this.shape = new Square(this.options, this.body, this.labelModule);
  297. break;
  298. case 'star':
  299. this.shape = new Star(this.options, this.body, this.labelModule);
  300. break;
  301. case 'text':
  302. this.shape = new Text(this.options, this.body, this.labelModule);
  303. break;
  304. case 'triangle':
  305. this.shape = new Triangle(this.options, this.body, this.labelModule);
  306. break;
  307. case 'triangleDown':
  308. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  309. break;
  310. default:
  311. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  312. break;
  313. }
  314. }
  315. this._reset();
  316. }
  317. /**
  318. * select this node
  319. */
  320. select() {
  321. this.selected = true;
  322. this._reset();
  323. }
  324. /**
  325. * unselect this node
  326. */
  327. unselect() {
  328. this.selected = false;
  329. this._reset();
  330. }
  331. /**
  332. * Reset the calculated size of the node, forces it to recalculate its size
  333. * @private
  334. */
  335. _reset() {
  336. this.shape.width = undefined;
  337. this.shape.height = undefined;
  338. }
  339. /**
  340. * get the title of this node.
  341. * @return {string} title The title of the node, or undefined when no title
  342. * has been set.
  343. */
  344. getTitle() {
  345. return this.options.title;
  346. }
  347. /**
  348. * Calculate the distance to the border of the Node
  349. * @param {CanvasRenderingContext2D} ctx
  350. * @param {Number} angle Angle in radians
  351. * @returns {number} distance Distance to the border in pixels
  352. */
  353. distanceToBorder(ctx, angle) {
  354. return this.shape.distanceToBorder(ctx,angle);
  355. }
  356. /**
  357. * Check if this node has a fixed x and y position
  358. * @return {boolean} true if fixed, false if not
  359. */
  360. isFixed() {
  361. return (this.options.fixed.x && this.options.fixed.y);
  362. }
  363. /**
  364. * check if this node is selecte
  365. * @return {boolean} selected True if node is selected, else false
  366. */
  367. isSelected() {
  368. return this.selected;
  369. }
  370. /**
  371. * Retrieve the value of the node. Can be undefined
  372. * @return {Number} value
  373. */
  374. getValue() {
  375. return this.options.value;
  376. }
  377. /**
  378. * Adjust the value range of the node. The node will adjust it's size
  379. * based on its value.
  380. * @param {Number} min
  381. * @param {Number} max
  382. */
  383. setValueRange(min, max, total) {
  384. if (this.options.value !== undefined) {
  385. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  386. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  387. if (this.options.scaling.label.enabled === true) {
  388. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  389. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  390. }
  391. this.options.size = this.options.scaling.min + scale * sizeDiff;
  392. }
  393. else {
  394. this.options.size = this.baseSize;
  395. this.options.font.size = this.baseFontSize;
  396. }
  397. }
  398. /**
  399. * Draw this node in the given canvas
  400. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  401. * @param {CanvasRenderingContext2D} ctx
  402. */
  403. draw(ctx) {
  404. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover);
  405. }
  406. /**
  407. * Update the bounding box of the shape
  408. */
  409. updateBoundingBox(ctx) {
  410. this.shape.updateBoundingBox(this.x,this.y,ctx);
  411. }
  412. /**
  413. * Recalculate the size of this node in the given canvas
  414. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  415. * @param {CanvasRenderingContext2D} ctx
  416. */
  417. resize(ctx) {
  418. this.shape.resize(ctx, this.selected);
  419. }
  420. /**
  421. * Check if this object is overlapping with the provided object
  422. * @param {Object} obj an object with parameters left, top, right, bottom
  423. * @return {boolean} True if location is located on node
  424. */
  425. isOverlappingWith(obj) {
  426. return (
  427. this.shape.left < obj.right &&
  428. this.shape.left + this.shape.width > obj.left &&
  429. this.shape.top < obj.bottom &&
  430. this.shape.top + this.shape.height > obj.top
  431. );
  432. }
  433. /**
  434. * Check if this object is overlapping with the provided object
  435. * @param {Object} obj an object with parameters left, top, right, bottom
  436. * @return {boolean} True if location is located on node
  437. */
  438. isBoundingBoxOverlappingWith(obj) {
  439. return (
  440. this.shape.boundingBox.left < obj.right &&
  441. this.shape.boundingBox.right > obj.left &&
  442. this.shape.boundingBox.top < obj.bottom &&
  443. this.shape.boundingBox.bottom > obj.top
  444. );
  445. }
  446. }
  447. export default Node;