util.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import _ from 'lodash'
  2. /*
  3. * Returns true if the specified node in the graph is a subgraph node. A
  4. * subgraph node is one that contains other nodes.
  5. */
  6. function isSubgraph (g, v) {
  7. return !!g.children(v).length
  8. }
  9. function edgeToId (e) {
  10. return escapeId(e.v) + ':' + escapeId(e.w) + ':' + escapeId(e.name)
  11. }
  12. const ID_DELIM = /:/g
  13. function escapeId (str) {
  14. return str ? String(str).replace(ID_DELIM, '\\:') : ''
  15. }
  16. function applyStyle (dom, styleFn) {
  17. if (styleFn) {
  18. dom.attr('style', styleFn)
  19. }
  20. }
  21. function applyClass (dom, classFn, otherClasses) {
  22. if (classFn) {
  23. dom
  24. .attr('class', classFn)
  25. .attr('class', otherClasses + ' ' + dom.attr('class'))
  26. }
  27. }
  28. function applyTransition (selection, g) {
  29. const graph = g.graph()
  30. if (_.isPlainObject(graph)) {
  31. const transition = graph.transition
  32. if (_.isFunction(transition)) {
  33. return transition(selection)
  34. }
  35. }
  36. return selection
  37. }
  38. // Public utility functions
  39. export default {
  40. isSubgraph,
  41. edgeToId,
  42. applyStyle,
  43. applyClass,
  44. applyTransition
  45. }