anonymize.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Explore plugin.
  3. */
  4. Draw.loadPlugin(function(editorUi)
  5. {
  6. var div = document.createElement('div');
  7. // Adds resource for action
  8. mxResources.parse('anonymizeCurrentPage=Anonymize Current Page');
  9. function replaceTextContent(elt)
  10. {
  11. if (elt.nodeValue != null)
  12. {
  13. elt.nodeValue = editorUi.anonymizeString(elt.nodeValue);
  14. }
  15. if (elt.nodeType == mxConstants.NODETYPE_ELEMENT)
  16. {
  17. var tmp = elt.firstChild;
  18. while (tmp != null)
  19. {
  20. replaceTextContent(tmp);
  21. tmp = tmp.nextSibling;
  22. }
  23. }
  24. };
  25. function anonymizeHtml(html)
  26. {
  27. div.innerHTML = Graph.sanitizeHtml(html);
  28. replaceTextContent(div);
  29. return div.innerHTML;
  30. };
  31. // Adds action
  32. editorUi.actions.addAction('anonymizeCurrentPage', function()
  33. {
  34. var graph = editorUi.editor.graph;
  35. var model = graph.model;
  36. model.beginUpdate();
  37. try
  38. {
  39. // Queue used to fix ancestor placeholders
  40. var queue = [];
  41. for (var id in model.cells)
  42. {
  43. var cell = model.cells[id];
  44. var label = graph.getLabel(cell);
  45. if (graph.isHtmlLabel(cell))
  46. {
  47. label = anonymizeHtml(label);
  48. }
  49. else
  50. {
  51. label = editorUi.anonymizeString(label);
  52. }
  53. queue.push({cell: cell, label: label});
  54. }
  55. for (var i = 0; i < queue.length; i++)
  56. {
  57. model.setValue(queue[i].cell, queue[i].label);
  58. }
  59. // Change page title
  60. if (editorUi.currentPage != null)
  61. {
  62. model.execute(new RenamePage(editorUi, editorUi.currentPage,
  63. editorUi.anonymizeString(editorUi.currentPage.getName())));
  64. }
  65. }
  66. finally
  67. {
  68. model.endUpdate();
  69. }
  70. });
  71. var menu = editorUi.menus.get('extras');
  72. var oldFunct = menu.funct;
  73. menu.funct = function(menu, parent)
  74. {
  75. oldFunct.apply(this, arguments);
  76. editorUi.menus.addMenuItems(menu, ['-', 'anonymizeCurrentPage'], parent);
  77. };
  78. });