anonymize.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Explore plugin.
  3. */
  4. Draw.loadPlugin(function(editorUi)
  5. {
  6. var div = document.createElement('div');
  7. var keep = '\n\t`~!@#$%^&*()_+{}|:"<>?-=[]\;\'.\/,\n\t';
  8. // Adds resource for action
  9. mxResources.parse('anonymizeCurrentPage=Anonymize Current Page');
  10. function anonymizeString(text)
  11. {
  12. var result = [];
  13. for (var i = 0; i < text.length; i++)
  14. {
  15. var c = text.charAt(i);
  16. if (keep.indexOf(c) >= 0)
  17. {
  18. result.push(c);
  19. }
  20. else if (!isNaN(parseInt(c)))
  21. {
  22. result.push(Math.round(Math.random() * 9));
  23. }
  24. else if (c.toLowerCase() != c)
  25. {
  26. result.push(String.fromCharCode(65 + Math.round(Math.random() * 25)));
  27. }
  28. else if (c.toUpperCase() != c)
  29. {
  30. result.push(String.fromCharCode(97 + Math.round(Math.random() * 25)));
  31. }
  32. else if (/\s/.test(c))
  33. {
  34. /* any whitespace */
  35. result.push(' ');
  36. }
  37. else
  38. {
  39. result.push('�');
  40. }
  41. }
  42. return result.join('');
  43. };
  44. function replaceTextContent(elt)
  45. {
  46. if (elt.nodeValue != null)
  47. {
  48. elt.nodeValue = anonymizeString(elt.nodeValue);
  49. }
  50. if (elt.nodeType == mxConstants.NODETYPE_ELEMENT)
  51. {
  52. var tmp = elt.firstChild;
  53. while (tmp != null)
  54. {
  55. replaceTextContent(tmp);
  56. tmp = tmp.nextSibling;
  57. }
  58. }
  59. };
  60. function anonymizeHtml(html)
  61. {
  62. div.innerHTML = html;
  63. replaceTextContent(div);
  64. return div.innerHTML;
  65. };
  66. // Adds action
  67. editorUi.actions.addAction('anonymizeCurrentPage', function()
  68. {
  69. var graph = editorUi.editor.graph;
  70. var model = graph.model;
  71. model.beginUpdate();
  72. try
  73. {
  74. // Queue used to fix ancestor placeholders
  75. var queue = [];
  76. for (var id in model.cells)
  77. {
  78. var cell = model.cells[id];
  79. var label = graph.getLabel(cell);
  80. if (graph.isHtmlLabel(cell))
  81. {
  82. label = anonymizeHtml(label);
  83. }
  84. else
  85. {
  86. label = anonymizeString(label);
  87. }
  88. queue.push({cell: cell, label: label});
  89. }
  90. for (var i = 0; i < queue.length; i++)
  91. {
  92. model.setValue(queue[i].cell, queue[i].label);
  93. }
  94. }
  95. finally
  96. {
  97. model.endUpdate();
  98. }
  99. });
  100. var menu = editorUi.menus.get('extras');
  101. var oldFunct = menu.funct;
  102. menu.funct = function(menu, parent)
  103. {
  104. oldFunct.apply(this, arguments);
  105. editorUi.menus.addMenuItems(menu, ['-', 'anonymizeCurrentPage'], parent);
  106. };
  107. });