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.

543 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. // set these options locally
  102. // clear x and y positions
  103. if (options.x !== undefined) {
  104. if (options.x === null) {this.x = undefined; this.predefinedPosition = false;}
  105. else {this.x = parseInt(options.x); this.predefinedPosition = true;}
  106. }
  107. if (options.y !== undefined) {
  108. if (options.y === null) {this.y = undefined; this.predefinedPosition = false;}
  109. else {this.y = parseInt(options.y); this.predefinedPosition = true;}
  110. }
  111. if (options.size !== undefined) {this.baseSize = options.size;}
  112. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  113. // copy group options
  114. if (typeof options.group === 'number' || (typeof options.group === 'string' && options.group != '')) {
  115. var groupObj = this.grouplist.get(options.group);
  116. util.deepExtend(this.options, groupObj);
  117. // the color object needs to be completely defined. Since groups can partially overwrite the colors, we parse it again, just in case.
  118. this.options.color = util.parseColor(this.options.color);
  119. }
  120. // this transforms all shorthands into fully defined options
  121. Node.parseOptions(this.options, options, true, this.globalOptions);
  122. this.choosify(options);
  123. this._load_images();
  124. this.updateLabelModule(options);
  125. this.updateShape(currentShape);
  126. this.labelModule.propagateFonts(this.nodeOptions, options, this.defaultOptions);
  127. if (options.hidden !== undefined || options.physics !== undefined) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * Load the images from the options, for the nodes that need them.
  134. *
  135. * TODO: The imageObj members should be moved to CircularImageBase.
  136. * It's the only place where they are required.
  137. *
  138. * @private
  139. */
  140. _load_images() {
  141. // Don't bother loading for nodes without images
  142. if (this.options.shape !== 'circularImage' && this.options.shape !== 'image') {
  143. return;
  144. }
  145. if (this.options.image === undefined) {
  146. throw "Option image must be defined for node type '" + this.options.shape + "'";
  147. }
  148. if (this.imagelist === undefined) {
  149. throw "Internal Error: No images provided";
  150. }
  151. if (typeof this.options.image === 'string') {
  152. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  153. } else {
  154. if (this.options.image.unselected === undefined) {
  155. throw "No unselected image provided";
  156. }
  157. this.imageObj = this.imagelist.load(this.options.image.unselected, this.options.brokenImage, this.id);
  158. if (this.options.image.selected !== undefined) {
  159. this.imageObjAlt = this.imagelist.load(this.options.image.selected, this.options.brokenImage, this.id);
  160. } else {
  161. this.imageObjAlt = undefined;
  162. }
  163. }
  164. }
  165. /**
  166. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  167. * Static so it can also be used by the handler.
  168. * @param parentOptions
  169. * @param newOptions
  170. * @param allowDeletion
  171. * @param globalOptions
  172. */
  173. static parseOptions(parentOptions, newOptions, allowDeletion = false, globalOptions = {}) {
  174. var fields = [
  175. 'color',
  176. 'font',
  177. 'fixed',
  178. 'shadow'
  179. ];
  180. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  181. // merge the shadow options into the parent.
  182. util.mergeOptions(parentOptions, newOptions, 'shadow', allowDeletion, 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', allowDeletion, 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. */
  404. setValueRange(min, max, total) {
  405. if (this.options.value !== undefined) {
  406. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  407. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  408. if (this.options.scaling.label.enabled === true) {
  409. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  410. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  411. }
  412. this.options.size = this.options.scaling.min + scale * sizeDiff;
  413. }
  414. else {
  415. this.options.size = this.baseSize;
  416. this.options.font.size = this.baseFontSize;
  417. }
  418. this.updateLabelModule();
  419. }
  420. /**
  421. * Draw this node in the given canvas
  422. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  423. * @param {CanvasRenderingContext2D} ctx
  424. */
  425. draw(ctx) {
  426. let values = this.getFormattingValues();
  427. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover, values);
  428. }
  429. /**
  430. * Update the bounding box of the shape
  431. */
  432. updateBoundingBox(ctx) {
  433. this.shape.updateBoundingBox(this.x,this.y,ctx);
  434. }
  435. /**
  436. * Recalculate the size of this node in the given canvas
  437. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  438. * @param {CanvasRenderingContext2D} ctx
  439. */
  440. resize(ctx) {
  441. let values = this.getFormattingValues();
  442. this.shape.resize(ctx, this.selected, this.hover, values);
  443. }
  444. /**
  445. * Check if this object is overlapping with the provided object
  446. * @param {Object} obj an object with parameters left, top, right, bottom
  447. * @return {boolean} True if location is located on node
  448. */
  449. isOverlappingWith(obj) {
  450. return (
  451. this.shape.left < obj.right &&
  452. this.shape.left + this.shape.width > obj.left &&
  453. this.shape.top < obj.bottom &&
  454. this.shape.top + this.shape.height > obj.top
  455. );
  456. }
  457. /**
  458. * Check if this object is overlapping with the provided object
  459. * @param {Object} obj an object with parameters left, top, right, bottom
  460. * @return {boolean} True if location is located on node
  461. */
  462. isBoundingBoxOverlappingWith(obj) {
  463. return (
  464. this.shape.boundingBox.left < obj.right &&
  465. this.shape.boundingBox.right > obj.left &&
  466. this.shape.boundingBox.top < obj.bottom &&
  467. this.shape.boundingBox.bottom > obj.top
  468. );
  469. }
  470. }
  471. export default Node;