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.

449 lines
14 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, 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. // load the images
  123. if (this.options.image !== undefined) {
  124. if (this.imagelist) {
  125. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
  126. }
  127. else {
  128. throw "No imagelist provided";
  129. }
  130. }
  131. this.updateLabelModule(options);
  132. this.updateShape(currentShape);
  133. this.labelModule.propagateFonts(this.nodeOptions, options, this.defaultOptions);
  134. if (options.hidden !== undefined || options.physics !== undefined) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * This process all possible shorthands in the new options and makes sure that the parentOptions are fully defined.
  141. * Static so it can also be used by the handler.
  142. * @param parentOptions
  143. * @param newOptions
  144. * @param allowDeletion
  145. * @param globalOptions
  146. */
  147. static parseOptions(parentOptions, newOptions, allowDeletion = false, globalOptions = {}) {
  148. var fields = [
  149. 'color',
  150. 'font',
  151. 'fixed',
  152. 'shadow'
  153. ];
  154. util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  155. // merge the shadow options into the parent.
  156. util.mergeOptions(parentOptions, newOptions, 'shadow', allowDeletion, globalOptions);
  157. // individual shape newOptions
  158. if (newOptions.color !== undefined && newOptions.color !== null) {
  159. let parsedColor = util.parseColor(newOptions.color);
  160. util.fillIfDefined(parentOptions.color, parsedColor);
  161. }
  162. else if (allowDeletion === true && newOptions.color === null) {
  163. parentOptions.color = util.bridgeObject(globalOptions.color); // set the object back to the global options
  164. }
  165. // handle the fixed options
  166. if (newOptions.fixed !== undefined && newOptions.fixed !== null) {
  167. if (typeof newOptions.fixed === 'boolean') {
  168. parentOptions.fixed.x = newOptions.fixed;
  169. parentOptions.fixed.y = newOptions.fixed;
  170. }
  171. else {
  172. if (newOptions.fixed.x !== undefined && typeof newOptions.fixed.x === 'boolean') {
  173. parentOptions.fixed.x = newOptions.fixed.x;
  174. }
  175. if (newOptions.fixed.y !== undefined && typeof newOptions.fixed.y === 'boolean') {
  176. parentOptions.fixed.y = newOptions.fixed.y;
  177. }
  178. }
  179. }
  180. // handle the font options
  181. if (newOptions.font !== undefined && newOptions.font !== null) {
  182. Label.parseOptions(parentOptions.font, newOptions);
  183. }
  184. else if (allowDeletion === true && newOptions.font === null) {
  185. parentOptions.font = util.bridgeObject(globalOptions.font); // set the object back to the global options
  186. }
  187. // handle the scaling options, specifically the label part
  188. if (newOptions.scaling !== undefined) {
  189. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label', allowDeletion, globalOptions.scaling);
  190. }
  191. }
  192. updateLabelModule(options) {
  193. if (this.options.label === undefined || this.options.label === null) {
  194. this.options.label = '';
  195. }
  196. this.labelModule.setOptions(this.options, true);
  197. if (this.labelModule.baseSize !== undefined) {
  198. this.baseFontSize = this.labelModule.baseSize;
  199. }
  200. this.labelModule.constrain(this.nodeOptions, options, this.defaultOptions);
  201. }
  202. updateShape(currentShape) {
  203. if (currentShape === this.options.shape && this.shape) {
  204. this.shape.setOptions(this.options, this.imageObj);
  205. }
  206. else {
  207. // choose draw method depending on the shape
  208. switch (this.options.shape) {
  209. case 'box':
  210. this.shape = new Box(this.options, this.body, this.labelModule);
  211. break;
  212. case 'circle':
  213. this.shape = new Circle(this.options, this.body, this.labelModule);
  214. break;
  215. case 'circularImage':
  216. this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
  217. break;
  218. case 'database':
  219. this.shape = new Database(this.options, this.body, this.labelModule);
  220. break;
  221. case 'diamond':
  222. this.shape = new Diamond(this.options, this.body, this.labelModule);
  223. break;
  224. case 'dot':
  225. this.shape = new Dot(this.options, this.body, this.labelModule);
  226. break;
  227. case 'ellipse':
  228. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  229. break;
  230. case 'icon':
  231. this.shape = new Icon(this.options, this.body, this.labelModule);
  232. break;
  233. case 'image':
  234. this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
  235. break;
  236. case 'square':
  237. this.shape = new Square(this.options, this.body, this.labelModule);
  238. break;
  239. case 'star':
  240. this.shape = new Star(this.options, this.body, this.labelModule);
  241. break;
  242. case 'text':
  243. this.shape = new Text(this.options, this.body, this.labelModule);
  244. break;
  245. case 'triangle':
  246. this.shape = new Triangle(this.options, this.body, this.labelModule);
  247. break;
  248. case 'triangleDown':
  249. this.shape = new TriangleDown(this.options, this.body, this.labelModule);
  250. break;
  251. default:
  252. this.shape = new Ellipse(this.options, this.body, this.labelModule);
  253. break;
  254. }
  255. }
  256. this._reset();
  257. }
  258. /**
  259. * select this node
  260. */
  261. select() {
  262. this.selected = true;
  263. this._reset();
  264. }
  265. /**
  266. * unselect this node
  267. */
  268. unselect() {
  269. this.selected = false;
  270. this._reset();
  271. }
  272. /**
  273. * Reset the calculated size of the node, forces it to recalculate its size
  274. * @private
  275. */
  276. _reset() {
  277. this.shape.width = undefined;
  278. this.shape.height = undefined;
  279. }
  280. /**
  281. * get the title of this node.
  282. * @return {string} title The title of the node, or undefined when no title
  283. * has been set.
  284. */
  285. getTitle() {
  286. return this.options.title;
  287. }
  288. /**
  289. * Calculate the distance to the border of the Node
  290. * @param {CanvasRenderingContext2D} ctx
  291. * @param {Number} angle Angle in radians
  292. * @returns {number} distance Distance to the border in pixels
  293. */
  294. distanceToBorder(ctx, angle) {
  295. return this.shape.distanceToBorder(ctx,angle);
  296. }
  297. /**
  298. * Check if this node has a fixed x and y position
  299. * @return {boolean} true if fixed, false if not
  300. */
  301. isFixed() {
  302. return (this.options.fixed.x && this.options.fixed.y);
  303. }
  304. /**
  305. * check if this node is selecte
  306. * @return {boolean} selected True if node is selected, else false
  307. */
  308. isSelected() {
  309. return this.selected;
  310. }
  311. /**
  312. * Retrieve the value of the node. Can be undefined
  313. * @return {Number} value
  314. */
  315. getValue() {
  316. return this.options.value;
  317. }
  318. /**
  319. * Adjust the value range of the node. The node will adjust it's size
  320. * based on its value.
  321. * @param {Number} min
  322. * @param {Number} max
  323. */
  324. setValueRange(min, max, total) {
  325. if (this.options.value !== undefined) {
  326. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  327. var sizeDiff = this.options.scaling.max - this.options.scaling.min;
  328. if (this.options.scaling.label.enabled === true) {
  329. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  330. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  331. }
  332. this.options.size = this.options.scaling.min + scale * sizeDiff;
  333. }
  334. else {
  335. this.options.size = this.baseSize;
  336. this.options.font.size = this.baseFontSize;
  337. }
  338. this.updateLabelModule();
  339. }
  340. /**
  341. * Draw this node in the given canvas
  342. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  343. * @param {CanvasRenderingContext2D} ctx
  344. */
  345. draw(ctx) {
  346. this.shape.draw(ctx, this.x, this.y, this.selected, this.hover);
  347. }
  348. /**
  349. * Update the bounding box of the shape
  350. */
  351. updateBoundingBox(ctx) {
  352. this.shape.updateBoundingBox(this.x,this.y,ctx);
  353. }
  354. /**
  355. * Recalculate the size of this node in the given canvas
  356. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  357. * @param {CanvasRenderingContext2D} ctx
  358. */
  359. resize(ctx) {
  360. this.shape.resize(ctx, this.selected);
  361. }
  362. /**
  363. * Check if this object is overlapping with the provided object
  364. * @param {Object} obj an object with parameters left, top, right, bottom
  365. * @return {boolean} True if location is located on node
  366. */
  367. isOverlappingWith(obj) {
  368. return (
  369. this.shape.left < obj.right &&
  370. this.shape.left + this.shape.width > obj.left &&
  371. this.shape.top < obj.bottom &&
  372. this.shape.top + this.shape.height > obj.top
  373. );
  374. }
  375. /**
  376. * Check if this object is overlapping with the provided object
  377. * @param {Object} obj an object with parameters left, top, right, bottom
  378. * @return {boolean} True if location is located on node
  379. */
  380. isBoundingBoxOverlappingWith(obj) {
  381. return (
  382. this.shape.boundingBox.left < obj.right &&
  383. this.shape.boundingBox.right > obj.left &&
  384. this.shape.boundingBox.top < obj.bottom &&
  385. this.shape.boundingBox.bottom > obj.top
  386. );
  387. }
  388. }
  389. export default Node;