anonymize.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. for (var id in model.cells)
  75. {
  76. var cell = model.cells[id];
  77. var label = graph.getLabel(cell);
  78. if (graph.isHtmlLabel(cell))
  79. {
  80. label = anonymizeHtml(label);
  81. }
  82. else
  83. {
  84. label = anonymizeString(label);
  85. }
  86. model.setValue(cell, label);
  87. }
  88. }
  89. finally
  90. {
  91. model.endUpdate();
  92. }
  93. });
  94. var menu = editorUi.menus.get('extras');
  95. var oldFunct = menu.funct;
  96. menu.funct = function(menu, parent)
  97. {
  98. oldFunct.apply(this, arguments);
  99. editorUi.menus.addMenuItems(menu, ['-', 'anonymizeCurrentPage'], parent);
  100. };
  101. });