sigma.parsers.gexf.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;(function(undefined) {
  2. 'use strict';
  3. if (typeof sigma === 'undefined')
  4. throw 'sigma is not declared';
  5. // Initialize package:
  6. sigma.utils.pkg('sigma.parsers');
  7. // Just a basic ID generator:
  8. var _id = 0;
  9. function edgeId() {
  10. return 'e' + (_id++);
  11. }
  12. /**
  13. * If the first arguments is a valid URL, this function loads a GEXF file and
  14. * creates a new sigma instance or updates the graph of a given instance. It
  15. * is possible to give a callback that will be executed at the end of the
  16. * process. And if the first argument is a DOM element, it will skip the
  17. * loading step and parse the given XML tree to fill the graph.
  18. *
  19. * @param {string|DOMElement} target The URL of the GEXF file or a valid
  20. * GEXF tree.
  21. * @param {object|sigma} sig A sigma configuration object or a
  22. * sigma instance.
  23. * @param {?function} callback Eventually a callback to execute
  24. * after having parsed the file. It will
  25. * be called with the related sigma
  26. * instance as parameter.
  27. */
  28. sigma.parsers.gexf = function(target, sig, callback) {
  29. var i,
  30. l,
  31. arr,
  32. obj;
  33. function parse(graph) {
  34. // Adapt the graph:
  35. arr = graph.nodes;
  36. for (i = 0, l = arr.length; i < l; i++) {
  37. obj = arr[i];
  38. obj.id = obj.id;
  39. if (obj.viz && typeof obj.viz === 'object') {
  40. if (obj.viz.position && typeof obj.viz.position === 'object') {
  41. obj.x = obj.viz.position.x;
  42. obj.y = -obj.viz.position.y; // Needed otherwise it's up side down
  43. }
  44. obj.size = obj.viz.size;
  45. obj.color = obj.viz.color;
  46. if (obj.viz.shape)
  47. obj.type = obj.viz.shape
  48. }
  49. }
  50. arr = graph.edges;
  51. for (i = 0, l = arr.length; i < l; i++) {
  52. obj = arr[i];
  53. obj.id = typeof obj.id === 'string' ? obj.id : edgeId();
  54. obj.source = '' + obj.source;
  55. obj.target = '' + obj.target;
  56. if (obj.viz && typeof obj.viz === 'object') {
  57. obj.color = obj.viz.color;
  58. obj.size = obj.viz.thickness;
  59. }
  60. // Weight over viz.thickness?
  61. obj.size = obj.weight;
  62. // Changing type to be direction so it won't mess with sigma's naming
  63. obj.direction = obj.type;
  64. delete obj.type;
  65. }
  66. // Update the instance's graph:
  67. if (sig instanceof sigma) {
  68. sig.graph.clear();
  69. arr = graph.nodes;
  70. for (i = 0, l = arr.length; i < l; i++)
  71. sig.graph.addNode(arr[i]);
  72. arr = graph.edges;
  73. for (i = 0, l = arr.length; i < l; i++)
  74. sig.graph.addEdge(arr[i]);
  75. // ...or instantiate sigma if needed:
  76. } else if (typeof sig === 'object') {
  77. sig.graph = graph;
  78. sig = new sigma(sig);
  79. // ...or it's finally the callback:
  80. } else if (typeof sig === 'function') {
  81. callback = sig;
  82. sig = null;
  83. }
  84. // Call the callback if specified:
  85. if (callback) {
  86. callback(sig || graph);
  87. return;
  88. } else
  89. return graph;
  90. }
  91. if (typeof target === 'string')
  92. gexf.fetch(target, parse);
  93. else if (typeof target === 'object')
  94. return parse(gexf.parse(target));
  95. };
  96. }).call(this);