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.

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