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.

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