props.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Sample plugin.
  3. */
  4. Draw.loadPlugin(function(ui) {
  5. var div = document.createElement('div');
  6. div.style.background = '#ffffff';
  7. div.style.border = '1px solid gray';
  8. div.style.opacity = '0.8';
  9. div.style.position = 'absolute';
  10. div.style.padding = '10px';
  11. div.style.paddingTop = '0px';
  12. div.style.width = '20%';
  13. div.style.minWidth = '200px';
  14. div.style.top = '40px';
  15. div.style.right = '20px';
  16. var graph = ui.editor.graph;
  17. // Made for chromeless mode
  18. if (!ui.editor.chromeless)
  19. {
  20. div.style.top = '100px';
  21. div.style.right = '260px';
  22. }
  23. div.innerHTML = '<p><i>Select a shape.</i></p>';
  24. document.body.appendChild(div);
  25. // Highlights current cell
  26. var highlight = new mxCellHighlight(graph, '#00ff00', 8);
  27. /**
  28. * Updates the properties panel
  29. */
  30. function cellClicked(cell)
  31. {
  32. // Forces focusout in IE
  33. graph.container.focus();
  34. // Gets the selection cell
  35. if (cell == null)
  36. {
  37. highlight.highlight(null);
  38. div.innerHTML = '<p><i>Select a shape.</i></p>';
  39. }
  40. else
  41. {
  42. var attrs = (cell.value != null) ? cell.value.attributes : null;
  43. if (attrs != null)
  44. {
  45. var ignored = ['label', 'tooltip', 'placeholders'];
  46. highlight.highlight(graph.view.getState(cell));
  47. var label = graph.sanitizeHtml(graph.getLabel(cell));
  48. if (label != null && label.length > 0)
  49. {
  50. div.innerHTML = '<h1>' + label + '</h1>';
  51. }
  52. else
  53. {
  54. div.innerHTML = '';
  55. }
  56. for (var i = 0; i < attrs.length; i++)
  57. {
  58. if (mxUtils.indexOf(ignored, attrs[i].nodeName) < 0 &&
  59. attrs[i].nodeValue.length > 0)
  60. {
  61. div.innerHTML += '<h2>' + graph.sanitizeHtml(attrs[i].nodeName) + '</h2>' +
  62. '<p>' + graph.sanitizeHtml(attrs[i].nodeValue) + '</p>';
  63. }
  64. }
  65. }
  66. }
  67. };
  68. /**
  69. * Creates the textfield for the given property.
  70. */
  71. function createTextField(graph, form, cell, attribute)
  72. {
  73. var input = form.addText(attribute.nodeName + ':', attribute.nodeValue);
  74. var applyHandler = function()
  75. {
  76. var newValue = input.value || '';
  77. var oldValue = cell.getAttribute(attribute.nodeName, '');
  78. if (newValue != oldValue)
  79. {
  80. graph.getModel().beginUpdate();
  81. try
  82. {
  83. var edit = new mxCellAttributeChange(
  84. cell, attribute.nodeName,
  85. newValue);
  86. graph.getModel().execute(edit);
  87. graph.updateCellSize(cell);
  88. }
  89. finally
  90. {
  91. graph.getModel().endUpdate();
  92. }
  93. }
  94. };
  95. mxEvent.addListener(input, 'keypress', function (evt)
  96. {
  97. // Needs to take shift into account for textareas
  98. if (evt.keyCode == /*enter*/13 &&
  99. !mxEvent.isShiftDown(evt))
  100. {
  101. input.blur();
  102. }
  103. });
  104. if (mxClient.IS_IE)
  105. {
  106. mxEvent.addListener(input, 'focusout', applyHandler);
  107. }
  108. else
  109. {
  110. // Note: Known problem is the blurring of fields in
  111. // Firefox by changing the selection, in which case
  112. // no event is fired in FF and the change is lost.
  113. // As a workaround you should use a local variable
  114. // that stores the focused field and invoke blur
  115. // explicitely where we do the graph.focus above.
  116. mxEvent.addListener(input, 'blur', applyHandler);
  117. }
  118. };
  119. graph.click = function(me)
  120. {
  121. // Async required to enable hyperlinks in labels
  122. window.setTimeout(function()
  123. {
  124. cellClicked(me.getCell());
  125. }, 0);
  126. };
  127. });