maxGraph/javascript/examples/grapheditor/www/js/EditorUi.js

1847 lines
47 KiB
JavaScript
Raw Normal View History

2012-05-21 20:32:26 +00:00
/**
* Copyright (c) 2006-2012, JGraph Ltd
*/
/**
* Constructs a new graph editor
*/
2012-06-06 12:52:32 +00:00
EditorUi = function(editor, container)
2012-05-21 20:32:26 +00:00
{
2013-12-20 16:51:26 +00:00
mxEventSource.call(this);
2012-06-06 12:52:32 +00:00
this.editor = editor || new Editor();
this.container = container || document.body;
2013-09-19 07:24:45 +00:00
var graph = this.editor.graph;
2012-05-21 20:32:26 +00:00
2012-12-18 13:09:38 +00:00
// Pre-fetches submenu image
new Image().src = mxPopupMenu.prototype.submenuImage;
// Pre-fetches connect image
if (mxConnectionHandler.prototype.connectImage != null)
{
new Image().src = mxConnectionHandler.prototype.connectImage.src;
}
// Creates the user interface
this.actions = new Actions(this);
this.menus = new Menus(this);
this.createDivs();
this.createUi();
2014-04-01 11:30:48 +00:00
this.refresh();
2012-12-18 13:09:38 +00:00
// Disables HTML and text selection
2012-05-30 16:26:17 +00:00
var textEditing = mxUtils.bind(this, function(evt)
{
2012-06-22 13:48:16 +00:00
if (evt == null)
{
evt = window.event;
}
if (this.isSelectionAllowed(evt))
{
return true;
}
2013-05-23 16:14:48 +00:00
return graph.isEditing();
2012-05-30 16:26:17 +00:00
});
2012-06-06 12:52:32 +00:00
// Disables text selection while not editing and no dialog visible
if (this.container == document.body)
{
2013-05-23 16:14:48 +00:00
this.menubarContainer.onselectstart = textEditing;
this.menubarContainer.onmousedown = textEditing;
this.toolbarContainer.onselectstart = textEditing;
this.toolbarContainer.onmousedown = textEditing;
this.diagramContainer.onselectstart = textEditing;
this.diagramContainer.onmousedown = textEditing;
this.sidebarContainer.onselectstart = textEditing;
this.sidebarContainer.onmousedown = textEditing;
this.footerContainer.onselectstart = textEditing;
this.footerContainer.onmousedown = textEditing;
2012-06-06 12:52:32 +00:00
}
2012-05-30 16:26:17 +00:00
2012-09-22 15:02:45 +00:00
// And uses built-in context menu while editing
2013-05-23 16:14:48 +00:00
if (mxClient.IS_IE && (typeof(document.documentMode) === 'undefined' || document.documentMode < 9))
2012-05-21 20:32:26 +00:00
{
2012-12-18 13:09:38 +00:00
mxEvent.addListener(this.diagramContainer, 'contextmenu', textEditing);
2012-05-21 20:32:26 +00:00
}
else
{
2012-12-18 13:09:38 +00:00
// Allows browser context menu outside of diagram and sidebar
this.diagramContainer.oncontextmenu = textEditing;
2012-05-21 20:32:26 +00:00
}
// Contains the main graph instance inside the given panel
graph.init(this.diagramContainer);
2014-01-08 16:23:20 +00:00
var textMode = false;
var nodes = null;
var updateToolbar = mxUtils.bind(this, function()
{
if (textMode != graph.cellEditor.isContentEditing())
{
var node = this.toolbar.container.firstChild;
var newNodes = [];
while (node != null)
{
var tmp = node.nextSibling;
node.parentNode.removeChild(node);
newNodes.push(node);
node = tmp;
}
if (nodes == null)
{
this.toolbar.createTextToolbar();
}
else
{
for (var i = 0; i < nodes.length; i++)
{
this.toolbar.container.appendChild(nodes[i]);
}
}
textMode = graph.cellEditor.isContentEditing();
nodes = newNodes;
}
});
2014-06-11 13:13:38 +00:00
// Control-enter applies editing value
// FIXME: Fix for HTML editing
var cellEditorIsStopEditingEvent = graph.cellEditor.isStopEditingEvent;
graph.cellEditor.isStopEditingEvent = function(evt)
{
return cellEditorIsStopEditingEvent.apply(this, arguments) ||
(evt.keyCode == 13 && mxEvent.isControlDown(evt));
};
2014-01-08 16:23:20 +00:00
// Overrides cell editor to update toolbar
var cellEditorStartEditing = graph.cellEditor.startEditing;
graph.cellEditor.startEditing = function()
{
cellEditorStartEditing.apply(this, arguments);
updateToolbar();
};
var cellEditorStopEditing = graph.cellEditor.stopEditing;
graph.cellEditor.stopEditing = function(cell, trigger)
{
cellEditorStopEditing.apply(this, arguments);
updateToolbar();
};
2012-05-21 20:32:26 +00:00
// Enables scrollbars and sets cursor style for the container
graph.container.setAttribute('tabindex', '0');
graph.container.style.cursor = 'default';
2013-05-23 16:14:48 +00:00
graph.container.style.backgroundImage = 'url(' + editor.gridImage + ')';
graph.container.style.backgroundPosition = '-1px -1px';
2014-02-19 14:19:38 +00:00
var noBackground = (mxClient.IS_IE && document.documentMode >= 9) ? 'url(' + this.editor.transparentImage + ')' : 'none';
graph.container.style.backgroundImage = noBackground;
var bgImg = (!graph.pageVisible && graph.isGridEnabled()) ? 'url(' + this.editor.gridImage + ')' : noBackground;
if (graph.view.canvas.ownerSVGElement != null)
{
graph.view.canvas.ownerSVGElement.style.backgroundImage = bgImg;
}
else
{
graph.view.canvas.style.backgroundImage = bgImg;
}
graph.container.focus();
2012-05-21 20:32:26 +00:00
2014-04-13 06:49:20 +00:00
// Overrides double click handling to add the tolerance
2013-12-20 16:51:26 +00:00
var graphDblClick = graph.dblClick;
graph.dblClick = function(evt, cell)
{
if (cell == null)
{
var pt = mxUtils.convertPoint(this.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
cell = this.getCellAt(pt.x, pt.y);
}
2014-04-13 06:49:20 +00:00
graphDblClick.call(this, evt, cell);
2013-12-20 16:51:26 +00:00
};
2012-05-21 20:32:26 +00:00
// Keeps graph container focused on mouse down
var graphFireMouseEvent = graph.fireMouseEvent;
graph.fireMouseEvent = function(evtName, me, sender)
{
if (evtName == mxEvent.MOUSE_DOWN)
{
this.container.focus();
}
graphFireMouseEvent.apply(this, arguments);
};
// Configures automatic expand on mouseover
2013-06-28 19:07:06 +00:00
graph.popupMenuHandler.autoExpand = true;
2012-05-21 20:32:26 +00:00
// Installs context menu
2013-06-28 19:07:06 +00:00
graph.popupMenuHandler.factoryMethod = mxUtils.bind(this, function(menu, cell, evt)
2012-05-21 20:32:26 +00:00
{
this.menus.createPopupMenu(menu, cell, evt);
});
// Hides context menu
2013-05-23 16:14:48 +00:00
mxEvent.addGestureListeners(document, mxUtils.bind(this, function(evt)
2012-05-21 20:32:26 +00:00
{
2013-06-28 19:07:06 +00:00
graph.popupMenuHandler.hideMenu();
2012-05-21 20:32:26 +00:00
}));
// Create handler for key events
2014-06-11 13:13:38 +00:00
this.keyHandler = this.createKeyHandler(editor);
2012-05-21 20:32:26 +00:00
// Getter for key handler
this.getKeyHandler = function()
{
return keyHandler;
};
2014-06-11 13:13:38 +00:00
2014-10-09 13:52:12 +00:00
// Stores the current style and assigns it to new cells
// LATER: Update after copyStyle (handle defaults) and after menu Format, Style
var styles = ['shadow', 'dashed', 'dashPattern'];
var connectStyles = ['shape', 'edgeStyle', 'curved', 'rounded'];
2014-06-11 13:13:38 +00:00
2014-10-09 13:52:12 +00:00
// Sets the default edge style
2014-11-10 09:02:21 +00:00
var currentEdgeStyle = {'edgeStyle': 'orthogonalEdgeStyle', 'rounded': '0', 'html': '1'};
2014-10-09 13:52:12 +00:00
var currentStyle = {};
2014-06-11 13:13:38 +00:00
2014-10-09 13:52:12 +00:00
this.setDefaultStyle = function(cell)
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
var state = graph.view.getState(cell);
if (cell != null)
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
// Ignores default styles
var clone = cell.clone();
clone.style = ''
var defaultStyle = graph.getCellStyle(clone);
var values = [];
var keys = [];
for (var key in state.style)
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
if (defaultStyle[key] != state.style[key])
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
values.push(state.style[key]);
keys.push(key);
2014-06-11 13:13:38 +00:00
}
2014-10-09 13:52:12 +00:00
}
// Handles special case for value "none"
var cellStyle = graph.getModel().getStyle(state.cell);
var tokens = (cellStyle != null) ? cellStyle.split(';') : [];
for (var i = 0; i < tokens.length; i++)
{
var tmp = tokens[i];
var pos = tmp.indexOf('=');
if (pos >= 0)
{
var key = tmp.substring(0, pos);
var value = tmp.substring(pos + 1);
if (defaultStyle[key] != null && value == 'none')
{
values.push(value);
keys.push(key);
}
}
}
// Resets current style
if (graph.getModel().isEdge(state.cell))
{
currentEdgeStyle = {};
}
else
{
currentStyle = {}
}
this.fireEvent(new mxEventObject('styleChanged', 'keys', keys, 'values', values, 'cells', [state.cell]));
}
};
2014-11-10 09:02:21 +00:00
this.resetDefaultStyle = function()
{
currentEdgeStyle = {'edgeStyle': 'orthogonalEdgeStyle', 'rounded': '0', 'html': '1'};
currentStyle = {};
};
2014-10-09 13:52:12 +00:00
// Constructs the style for the initial edge type defined in the initial value for the currentEdgeStyle
// This function is overridden below as soon as any style is set in the app.
var initialEdgeCellStyle = '';
for (var key in currentEdgeStyle)
{
initialEdgeCellStyle += key + '=' + currentEdgeStyle[key] + ';';
}
// Uses the default edge style for connect preview
graph.connectionHandler.createEdgeState = function(me)
{
var edge = graph.createEdge(null, null, null, null, null, initialEdgeCellStyle);
return new mxCellState(graph.view, edge, graph.getCellStyle(edge));
};
2014-11-10 09:02:21 +00:00
// Keys that should be ignored if the cell has a value (known: new default for all cells is html=1 so
// for the html key this effecticely only works for edges inserted via the connection handler)
var valueStyles = ['fontFamily', 'fontSize', 'fontColor'];
2014-10-09 13:52:12 +00:00
2014-10-30 08:08:05 +00:00
// Keys that always update the current edge style regardless of selection
var alwaysEdgeStyles = ['edgeStyle', 'startArrow', 'startFill', 'startSize', 'endArrow', 'endFill', 'endSize'];
2014-10-09 13:52:12 +00:00
// Keys that are ignored together (if one appears all are ignored)
var keyGroups = [['startArrow', 'startFill', 'startSize', 'endArrow', 'endFill', 'endSize'],
['strokeColor', 'strokeWidth'],
2014-11-10 09:02:21 +00:00
['fillColor', 'gradientColor'],
valueStyles,
['align'],
['html']];
2014-10-09 13:52:12 +00:00
// Adds all keys used above to the styles array
for (var i = 0; i < keyGroups.length; i++)
{
for (var j = 0; j < keyGroups[i].length; j++)
{
styles.push(keyGroups[i][j]);
}
}
for (var i = 0; i < connectStyles.length; i++)
{
styles.push(connectStyles[i]);
}
// Implements a global current style for edges and vertices that is applied to new cells
var insertHandler = function(cells)
{
graph.getModel().beginUpdate();
try
{
for (var i = 0; i < cells.length; i++)
{
var cell = cells[i];
2014-06-11 13:13:38 +00:00
2014-10-09 13:52:12 +00:00
// Removes styles defined in the cell style from the styles to be applied
var cellStyle = graph.getModel().getStyle(cell);
var tokens = (cellStyle != null) ? cellStyle.split(';') : [];
var appliedStyles = styles.slice();
for (var j = 0; j < tokens.length; j++)
{
var tmp = tokens[j];
var pos = tmp.indexOf('=');
if (pos >= 0)
{
var key = tmp.substring(0, pos);
var index = mxUtils.indexOf(appliedStyles, key);
if (index >= 0)
{
appliedStyles.splice(index, 1);
}
// Handles special cases where one defined style ignores other styles
for (var k = 0; k < keyGroups.length; k++)
{
var group = keyGroups[k];
if (mxUtils.indexOf(group, key) >= 0)
{
for (var l = 0; l < group.length; l++)
{
var index2 = mxUtils.indexOf(appliedStyles, group[l]);
if (index2 >= 0)
{
appliedStyles.splice(index2, 1);
}
}
}
}
}
}
// Applies the current style to the cell
var value = graph.convertValueToString(cell);
var edge = graph.getModel().isEdge(cell);
var current = (edge) ? currentEdgeStyle : currentStyle;
for (var j = 0; j < appliedStyles.length; j++)
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
var key = appliedStyles[j];
var styleValue = current[key];
2014-11-10 09:02:21 +00:00
if (styleValue != null)
2014-06-11 13:13:38 +00:00
{
2014-11-10 09:02:21 +00:00
// Special case: Connect styles are not applied here but in the connection handler
if (!edge || mxUtils.indexOf(connectStyles, key) < 0)
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
graph.setCellStyles(key, styleValue, [cell]);
2014-06-11 13:13:38 +00:00
}
}
}
}
}
2014-10-09 13:52:12 +00:00
finally
{
graph.getModel().endUpdate();
}
2014-06-11 13:13:38 +00:00
};
this.addListener('cellsInserted', function(sender, evt)
{
2014-10-09 13:52:12 +00:00
insertHandler(evt.getProperty('cells'));
2014-06-11 13:13:38 +00:00
});
graph.connectionHandler.addListener(mxEvent.CONNECT, function(sender, evt)
{
2014-10-09 13:52:12 +00:00
var cells = [evt.getProperty('cell')];
if (evt.getProperty('terminalInserted'))
{
cells.push(evt.getProperty('terminal'));
}
insertHandler(cells, true);
2014-06-11 13:13:38 +00:00
});
2014-11-10 09:02:21 +00:00
// This is used below and in Sidebar.dropAndConnect
this.createCurrentEdgeStyle = function()
{
var style = 'edgeStyle=' + (currentEdgeStyle['edgeStyle'] || 'none') + ';';
if (currentEdgeStyle['shape'] != null)
{
style += 'shape=' + currentEdgeStyle['shape'] + ';';
}
if (currentEdgeStyle['curved'] != null)
{
style += 'curved=' + currentEdgeStyle['curved'] + ';';
}
if (currentEdgeStyle['rounded'] != null)
{
style += 'rounded=' + currentEdgeStyle['rounded'] + ';';
}
if (currentEdgeStyle['html'] != null)
{
style += 'html=' + currentEdgeStyle['html'] + ';';
}
else
{
style += 'html=1;';
}
return style;
};
// Uses current edge style for connect preview
// NOTE: Do not use "this" in here as it points to the UI
graph.connectionHandler.createEdgeState = mxUtils.bind(this, function(me)
{
var style = this.createCurrentEdgeStyle();
var edge = graph.createEdge(null, null, null, null, null, style);
return new mxCellState(graph.view, edge, graph.getCellStyle(edge));
});
2014-10-09 13:52:12 +00:00
this.addListener('styleChanged', mxUtils.bind(this, function(sender, evt)
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
// Checks if edges and/or vertices were modified
var cells = evt.getProperty('cells');
var vertex = false;
var edge = false;
if (cells.length > 0)
{
for (var i = 0; i < cells.length; i++)
{
vertex = graph.getModel().isVertex(cells[i]) || vertex;
edge = graph.getModel().isEdge(cells[i]) || edge;
if (edge && vertex)
{
break;
}
}
}
else
{
vertex = true;
edge = true;
}
2014-06-11 13:13:38 +00:00
var keys = evt.getProperty('keys');
var values = evt.getProperty('values');
for (var i = 0; i < keys.length; i++)
{
2014-10-09 13:52:12 +00:00
var common = mxUtils.indexOf(valueStyles, keys[i]) >= 0;
// Special case: Edge style and shape
if (mxUtils.indexOf(connectStyles, keys[i]) >= 0)
2014-06-11 13:13:38 +00:00
{
2014-10-30 08:08:05 +00:00
if (edge || mxUtils.indexOf(alwaysEdgeStyles, keys[i]) >= 0)
2014-10-09 13:52:12 +00:00
{
currentEdgeStyle[keys[i]] = values[i];
}
2014-06-11 13:13:38 +00:00
}
2014-10-09 13:52:12 +00:00
else if (mxUtils.indexOf(styles, keys[i]) >= 0)
2014-06-11 13:13:38 +00:00
{
2014-10-09 13:52:12 +00:00
if (vertex || common)
{
currentStyle[keys[i]] = values[i];
}
2014-10-30 08:08:05 +00:00
if (edge || common || mxUtils.indexOf(alwaysEdgeStyles, keys[i]) >= 0)
2014-10-09 13:52:12 +00:00
{
currentEdgeStyle[keys[i]] = values[i];
}
2014-06-11 13:13:38 +00:00
}
}
2014-10-09 13:52:12 +00:00
if (this.toolbar != null)
{
var ff = currentStyle['fontFamily'] || 'Helvetica';
this.toolbar.fontMenu.innerHTML = mxUtils.htmlEntities(ff);
var fs = String(currentStyle['fontSize'] || '12');
this.toolbar.sizeMenu.innerHTML = mxUtils.htmlEntities(fs);
// Updates toolbar icon for edge style
var edgeStyleDiv = this.toolbar.edgeStyleMenu.getElementsByTagName('div')[0];
if (currentEdgeStyle['shape'] == 'arrow')
{
edgeStyleDiv.className = 'geSprite geSprite-arrow';
}
else if (currentEdgeStyle['shape'] == 'link')
{
edgeStyleDiv.className = 'geSprite geSprite-linkedge';
}
else if (currentEdgeStyle['edgeStyle'] == 'orthogonalEdgeStyle' && currentEdgeStyle['curved'] == '1')
{
edgeStyleDiv.className = 'geSprite geSprite-curved';
}
else if (currentEdgeStyle['edgeStyle'] == 'straight' || currentEdgeStyle['edgeStyle'] == 'none' ||
currentEdgeStyle['edgeStyle'] == null)
{
edgeStyleDiv.className = 'geSprite geSprite-straight';
}
else if (currentEdgeStyle['edgeStyle'] == 'entityRelationEdgeStyle')
{
edgeStyleDiv.className = 'geSprite geSprite-entity';
}
else
{
edgeStyleDiv.className = 'geSprite geSprite-orthogonal';
}
}
}));
// Update font size and font family labels
if (this.toolbar != null)
{
var update = mxUtils.bind(this, function()
{
var ff = currentStyle['fontFamily'] || 'Helvetica';
var fs = String(currentStyle['fontSize'] || '12');
var state = graph.getView().getState(graph.getSelectionCell());
if (state != null)
{
ff = state.style[mxConstants.STYLE_FONTFAMILY] || ff;
fs = state.style[mxConstants.STYLE_FONTSIZE] || fs;
if (ff.length > 10)
{
ff = ff.substring(0, 8) + '...';
}
}
this.toolbar.fontMenu.innerHTML = ff;
this.toolbar.sizeMenu.innerHTML = fs;
});
graph.getSelectionModel().addListener(mxEvent.CHANGE, update);
graph.getModel().addListener(mxEvent.CHANGE, update);
}
2014-06-11 13:13:38 +00:00
// Makes sure the current layer is visible when cells are added
graph.addListener(mxEvent.CELLS_ADDED, function(sender, evt)
{
var cells = evt.getProperty('cells');
var parent = evt.getProperty('parent');
if (graph.getModel().isLayer(parent) && !graph.isCellVisible(parent) && cells != null && cells.length > 0)
{
graph.getModel().setVisible(parent, true);
}
});
2012-05-21 20:32:26 +00:00
2014-07-25 06:48:01 +00:00
// Updates the editor UI after the window has been resized or the orientation changes
2014-05-05 08:30:00 +00:00
// Timeout is workaround for old IE versions which have a delay for DOM client sizes.
// Should not use delay > 0 to avoid handle multiple repaints during window resize
2012-05-21 20:32:26 +00:00
mxEvent.addListener(window, 'resize', mxUtils.bind(this, function()
{
2014-05-05 08:30:00 +00:00
window.setTimeout(mxUtils.bind(this, function()
2014-04-01 11:30:48 +00:00
{
2014-07-25 06:48:01 +00:00
this.refresh();
2014-05-05 08:30:00 +00:00
}), 0);
2012-05-21 20:32:26 +00:00
}));
2014-07-25 06:48:01 +00:00
mxEvent.addListener(window, 'orientationchange', mxUtils.bind(this, function()
{
this.refresh();
}));
// Workaround for bug on iOS see
// http://stackoverflow.com/questions/19012135/ios-7-ipad-safari-landscape-innerheight-outerheight-layout-issue
if (mxClient.IS_IOS && !window.navigator.standalone)
{
mxEvent.addListener(window, 'scroll', mxUtils.bind(this, function()
{
window.scrollTo(0, 0);
}));
}
2012-05-21 20:32:26 +00:00
2014-06-02 08:21:19 +00:00
/**
* Sets the initial scrollbar locations after a file was loaded.
*/
this.editor.addListener('resetGraphView', mxUtils.bind(this, function()
{
// Timeout is a workaround for delay needed in older browsers and IE
window.setTimeout(mxUtils.bind(this, function()
{
this.editor.resetScrollbars();
}), 0);
}));
2014-10-30 08:08:05 +00:00
// Escape key hides dialogs
mxEvent.addListener(document, 'keydown', mxUtils.bind(this, function(evt)
{
// Cancels the editing if escape is pressed
if (!mxEvent.isConsumed(evt) && evt.keyCode == 27 /* Escape */)
{
this.hideDialog();
}
}));
2014-06-02 08:21:19 +00:00
2014-05-15 19:58:36 +00:00
// Resets UI, updates action and menu states
this.editor.resetGraph();
2012-05-21 20:32:26 +00:00
this.init();
this.open();
};
2013-12-20 16:51:26 +00:00
// Extends mxEventSource
mxUtils.extend(EditorUi, mxEventSource);
2012-05-21 20:32:26 +00:00
/**
* Specifies the size of the split bar.
*/
2013-06-28 19:07:06 +00:00
EditorUi.prototype.splitSize = (mxClient.IS_TOUCH || mxClient.IS_POINTER) ? 12 : 8;
2012-05-21 20:32:26 +00:00
/**
* Specifies the height of the menubar. Default is 34.
*/
2013-11-11 12:31:46 +00:00
EditorUi.prototype.menubarHeight = 30;
2012-05-21 20:32:26 +00:00
/**
2013-01-16 09:00:09 +00:00
* Specifies the height of the toolbar. Default is 36.
2012-05-21 20:32:26 +00:00
*/
2013-11-11 12:31:46 +00:00
EditorUi.prototype.toolbarHeight = 34;
2012-05-21 20:32:26 +00:00
/**
* Specifies the height of the footer. Default is 28.
*/
EditorUi.prototype.footerHeight = 28;
/**
2013-05-23 16:14:48 +00:00
* Specifies the height of the optional sidebarFooterContainer. Default is 34.
*/
EditorUi.prototype.sidebarFooterHeight = 34;
/**
* Specifies the height of the horizontal split bar. Default is 212.
2012-05-21 20:32:26 +00:00
*/
2013-05-23 16:14:48 +00:00
EditorUi.prototype.hsplitPosition = 204;
2012-05-21 20:32:26 +00:00
2013-07-12 13:37:03 +00:00
/**
* Specifies if animations are allowed in <executeLayout>. Default is true.
*/
EditorUi.prototype.allowAnimation = true;
2012-05-21 20:32:26 +00:00
/**
* Installs the listeners to update the action states.
*/
EditorUi.prototype.init = function()
{
// Updates action states
this.addUndoListener();
2013-12-09 08:35:20 +00:00
this.addBeforeUnloadListener();
2014-05-05 08:30:00 +00:00
this.editor.graph.getSelectionModel().addListener(mxEvent.CHANGE, mxUtils.bind(this, function()
{
this.updateActionStates();
}));
this.updateActionStates();
2012-05-21 20:32:26 +00:00
// Overrides clipboard to update paste action state
var paste = this.actions.get('paste');
2014-01-08 16:23:20 +00:00
var updatePaste = mxUtils.bind(this, function()
2012-05-21 20:32:26 +00:00
{
2014-01-08 16:23:20 +00:00
paste.setEnabled(this.editor.graph.cellEditor.isContentEditing() || !mxClipboard.isEmpty());
});
2012-05-21 20:32:26 +00:00
var mxClipboardCut = mxClipboard.cut;
2014-01-08 16:23:20 +00:00
mxClipboard.cut = function(graph)
2012-05-21 20:32:26 +00:00
{
2014-01-08 16:23:20 +00:00
if (graph.cellEditor.isContentEditing())
{
document.execCommand('cut');
}
else
{
mxClipboardCut.apply(this, arguments);
}
2012-05-21 20:32:26 +00:00
updatePaste();
};
var mxClipboardCopy = mxClipboard.copy;
2014-01-08 16:23:20 +00:00
mxClipboard.copy = function(graph)
{
if (graph.cellEditor.isContentEditing())
{
document.execCommand('copy');
}
else
{
mxClipboardCopy.apply(this, arguments);
}
updatePaste();
};
var mxClipboardPaste = mxClipboard.paste;
mxClipboard.paste = function(graph)
{
if (graph.cellEditor.isContentEditing())
{
document.execCommand('paste');
}
else
{
mxClipboardPaste.apply(this, arguments);
}
updatePaste();
};
// Overrides cell editor to update paste action state
var cellEditorStartEditing = this.editor.graph.cellEditor.startEditing;
this.editor.graph.cellEditor.startEditing = function()
{
cellEditorStartEditing.apply(this, arguments);
updatePaste();
};
var cellEditorStopEditing = this.editor.graph.cellEditor.stopEditing;
this.editor.graph.cellEditor.stopEditing = function(cell, trigger)
2012-05-21 20:32:26 +00:00
{
2014-01-08 16:23:20 +00:00
cellEditorStopEditing.apply(this, arguments);
2012-05-21 20:32:26 +00:00
updatePaste();
};
2013-07-19 09:51:30 +00:00
updatePaste();
2012-05-21 20:32:26 +00:00
};
2012-06-22 13:48:16 +00:00
/**
* Hook for allowing selection and context menu for certain events.
*/
EditorUi.prototype.isSelectionAllowed = function(evt)
{
2014-05-05 08:30:00 +00:00
return mxEvent.getSource(evt).nodeName == 'SELECT';
2012-06-22 13:48:16 +00:00
};
2013-12-09 08:35:20 +00:00
/**
* Installs dialog if browser window is closed without saving
* This must be disabled during save and image export.
*/
EditorUi.prototype.addBeforeUnloadListener = function()
{
// Installs dialog if browser window is closed without saving
// This must be disabled during save and image export
window.onbeforeunload = mxUtils.bind(this, function()
{
return this.onBeforeUnload();
});
};
/**
* Sets the onbeforeunload for the application
*/
EditorUi.prototype.onBeforeUnload = function()
{
if (this.editor.modified)
{
return mxResources.get('allChangesLost');
}
};
2012-05-21 20:32:26 +00:00
/**
* Opens the current diagram via the window.opener if one exists.
*/
EditorUi.prototype.open = function()
{
// Cross-domain window access is not allowed in FF, so if we
// were opened from another domain then this will fail.
try
{
if (window.opener != null && window.opener.openFile != null)
{
window.opener.openFile.setConsumer(mxUtils.bind(this, function(xml, filename)
{
try
{
var doc = mxUtils.parseXml(xml);
this.editor.setGraphXml(doc.documentElement);
2013-07-12 07:09:46 +00:00
this.editor.setModified(false);
2012-05-21 20:32:26 +00:00
this.editor.undoManager.clear();
if (filename != null)
{
2013-07-12 07:09:46 +00:00
this.editor.setFilename(filename);
2013-12-09 08:35:20 +00:00
this.updateDocumentTitle();
2012-05-21 20:32:26 +00:00
}
2014-05-15 19:58:36 +00:00
return;
2012-05-21 20:32:26 +00:00
}
catch (e)
{
mxUtils.alert(mxResources.get('invalidOrMissingFile') + ': ' + e.message);
}
}));
}
}
catch(e)
{
// ignore
}
2014-05-15 19:58:36 +00:00
// Fires as the last step if no file was loaded
this.editor.graph.view.validate();
2014-06-02 08:21:19 +00:00
// Required only in special cases where an initial file is opened
// and the minimumGraphSize changes and CSS must be updated.
this.editor.graph.sizeDidChange();
2014-05-15 19:58:36 +00:00
this.editor.fireEvent(new mxEventObject('resetGraphView'));
2012-05-21 20:32:26 +00:00
};
2013-12-09 08:35:20 +00:00
/**
* Updates the document title.
*/
EditorUi.prototype.updateDocumentTitle = function()
{
2013-12-20 16:51:26 +00:00
var title = this.editor.getOrCreateFilename();
2013-12-09 08:35:20 +00:00
2013-12-20 16:51:26 +00:00
if (this.editor.appName != null)
2013-12-09 08:35:20 +00:00
{
2013-12-20 16:51:26 +00:00
title += ' - ' + this.editor.appName;
2013-12-09 08:35:20 +00:00
}
document.title = title;
};
2013-11-11 12:31:46 +00:00
/**
* Returns the URL for a copy of this editor with no state.
*/
EditorUi.prototype.redo = function()
{
2014-01-08 16:23:20 +00:00
if (this.editor.graph.cellEditor.isContentEditing())
{
document.execCommand('redo');
}
else
{
this.editor.graph.stopEditing(false);
this.editor.undoManager.redo();
}
2013-11-11 12:31:46 +00:00
};
/**
* Returns the URL for a copy of this editor with no state.
*/
EditorUi.prototype.undo = function()
{
2014-01-08 16:23:20 +00:00
if (this.editor.graph.cellEditor.isContentEditing())
{
document.execCommand('undo');
}
else
{
this.editor.graph.stopEditing(false);
this.editor.undoManager.undo();
}
2013-11-11 12:31:46 +00:00
};
/**
* Returns the URL for a copy of this editor with no state.
*/
EditorUi.prototype.canRedo = function()
{
2014-01-08 16:23:20 +00:00
return this.editor.graph.cellEditor.isContentEditing() || this.editor.undoManager.canRedo();
2013-11-11 12:31:46 +00:00
};
/**
* Returns the URL for a copy of this editor with no state.
*/
EditorUi.prototype.canUndo = function()
{
2014-01-08 16:23:20 +00:00
return this.editor.graph.cellEditor.isContentEditing() || this.editor.undoManager.canUndo();
2013-11-11 12:31:46 +00:00
};
2012-05-21 20:32:26 +00:00
/**
* Returns the URL for a copy of this editor with no state.
*/
EditorUi.prototype.getUrl = function(pathname)
{
var href = (pathname != null) ? pathname : window.location.pathname;
2012-12-18 13:09:38 +00:00
var parms = (href.indexOf('?') > 0) ? 1 : 0;
2012-05-21 20:32:26 +00:00
// Removes template URL parameter for new blank diagram
for (var key in urlParams)
{
if (parms == 0)
{
href += '?';
}
else
{
href += '&';
}
href += key + '=' + urlParams[key];
parms++;
}
return href;
};
2013-12-20 16:51:26 +00:00
/**
* Loads the stylesheet for this graph.
*/
EditorUi.prototype.setBackgroundColor = function(value)
{
this.editor.graph.background = value;
this.editor.updateGraphComponents();
this.fireEvent(new mxEventObject('backgroundColorChanged'));
};
/**
* Loads the stylesheet for this graph.
*/
EditorUi.prototype.setPageFormat = function(value)
{
this.editor.graph.pageFormat = value;
2014-04-01 11:30:48 +00:00
2013-12-20 16:51:26 +00:00
if (!this.editor.graph.pageVisible)
{
this.actions.get('pageView').funct();
}
else
{
this.editor.updateGraphComponents();
this.editor.graph.view.validateBackground();
this.editor.graph.sizeDidChange();
}
this.fireEvent(new mxEventObject('pageFormatChanged'));
};
2012-05-21 20:32:26 +00:00
/**
* Updates the states of the given undo/redo items.
*/
EditorUi.prototype.addUndoListener = function()
{
var undo = this.actions.get('undo');
var redo = this.actions.get('redo');
var undoMgr = this.editor.undoManager;
2013-11-11 12:31:46 +00:00
var undoListener = mxUtils.bind(this, function()
2012-05-21 20:32:26 +00:00
{
2013-11-11 12:31:46 +00:00
undo.setEnabled(this.canUndo());
redo.setEnabled(this.canRedo());
});
2012-05-21 20:32:26 +00:00
undoMgr.addListener(mxEvent.ADD, undoListener);
undoMgr.addListener(mxEvent.UNDO, undoListener);
undoMgr.addListener(mxEvent.REDO, undoListener);
undoMgr.addListener(mxEvent.CLEAR, undoListener);
2014-01-08 16:23:20 +00:00
// Overrides cell editor to update action states
var cellEditorStartEditing = this.editor.graph.cellEditor.startEditing;
this.editor.graph.cellEditor.startEditing = function()
{
cellEditorStartEditing.apply(this, arguments);
undoListener();
};
var cellEditorStopEditing = this.editor.graph.cellEditor.stopEditing;
this.editor.graph.cellEditor.stopEditing = function(cell, trigger)
{
cellEditorStopEditing.apply(this, arguments);
undoListener();
};
2012-05-21 20:32:26 +00:00
// Updates the button states once
undoListener();
};
/**
2014-05-05 08:30:00 +00:00
* Updates the states of the given toolbar items based on the selection.
*/
EditorUi.prototype.updateActionStates = function()
2012-05-21 20:32:26 +00:00
{
2014-05-05 08:30:00 +00:00
var graph = this.editor.graph;
var selected = !graph.isSelectionEmpty();
var vertexSelected = false;
var edgeSelected = false;
2012-05-21 20:32:26 +00:00
2014-05-05 08:30:00 +00:00
var cells = graph.getSelectionCells();
if (cells != null)
{
for (var i = 0; i < cells.length; i++)
2012-05-21 20:32:26 +00:00
{
2014-05-05 08:30:00 +00:00
var cell = cells[i];
if (graph.getModel().isEdge(cell))
{
edgeSelected = true;
}
if (graph.getModel().isVertex(cell))
{
vertexSelected = true;
}
if (edgeSelected && vertexSelected)
{
break;
}
2012-05-21 20:32:26 +00:00
}
2014-05-05 08:30:00 +00:00
}
// Updates action states
2014-10-09 13:52:12 +00:00
var actions = ['cut', 'copy', 'bold', 'italic', 'underline', 'delete', 'duplicate',
'style', 'backgroundColor', 'borderColor', 'toFront', 'toBack',
2014-11-10 09:02:21 +00:00
'lockUnlock', 'editData'];
2014-05-05 08:30:00 +00:00
for (var i = 0; i < actions.length; i++)
{
this.actions.get(actions[i]).setEnabled(selected);
}
2014-10-09 13:52:12 +00:00
this.actions.get('setAsDefaultStyle').setEnabled(graph.getSelectionCount() == 1);
2014-11-10 09:02:21 +00:00
this.actions.get('switchDirection').setEnabled(!graph.isSelectionEmpty());
2014-10-30 08:08:05 +00:00
this.actions.get('curved').setEnabled(edgeSelected);
2014-05-05 08:30:00 +00:00
this.actions.get('rotation').setEnabled(vertexSelected);
this.actions.get('wordWrap').setEnabled(vertexSelected);
2014-10-30 08:08:05 +00:00
this.actions.get('autosize').setEnabled(vertexSelected);
this.actions.get('collapsible').setEnabled(vertexSelected);
2014-10-09 13:52:12 +00:00
this.actions.get('group').setEnabled(graph.getSelectionCount() > 1);
2014-05-05 08:30:00 +00:00
this.actions.get('ungroup').setEnabled(graph.getSelectionCount() == 1 &&
graph.getModel().getChildCount(graph.getSelectionCell()) > 0);
var oneVertexSelected = vertexSelected && graph.getSelectionCount() == 1;
this.actions.get('removeFromGroup').setEnabled(oneVertexSelected &&
graph.getModel().isVertex(graph.getModel().getParent(graph.getSelectionCell())));
// Updates menu states
2014-10-09 13:52:12 +00:00
var menus = ['alignment', 'position', 'spacing'];
2014-05-05 08:30:00 +00:00
for (var i = 0; i < menus.length; i++)
{
this.menus.get(menus[i]).setEnabled(selected);
}
2014-06-11 13:13:38 +00:00
var state = graph.view.getState(graph.getSelectionCell());
2014-05-05 08:30:00 +00:00
this.menus.get('align').setEnabled(graph.getSelectionCount() > 1);
this.menus.get('distribute').setEnabled(graph.getSelectionCount() > 1);
2014-06-11 13:13:38 +00:00
this.menus.get('direction').setEnabled(vertexSelected || (edgeSelected && state != null && graph.isLoop(state)));
2014-05-05 08:30:00 +00:00
this.actions.get('home').setEnabled(graph.view.currentRoot != null);
this.actions.get('exitGroup').setEnabled(graph.view.currentRoot != null);
var groupEnabled = graph.getSelectionCount() == 1 && graph.isValidRoot(graph.getSelectionCell());
this.actions.get('enterGroup').setEnabled(groupEnabled);
this.actions.get('expand').setEnabled(groupEnabled);
this.actions.get('collapse').setEnabled(groupEnabled);
this.actions.get('editLink').setEnabled(graph.getSelectionCount() == 1);
this.actions.get('openLink').setEnabled(graph.getSelectionCount() == 1 &&
graph.getLinkForCell(graph.getSelectionCell()) != null);
this.actions.get('guides').setEnabled(graph.isEnabled());
this.actions.get('grid').setEnabled(graph.isEnabled());
2012-05-21 20:32:26 +00:00
};
/**
* Refreshes the viewport.
*/
EditorUi.prototype.refresh = function()
{
var quirks = mxClient.IS_IE && (document.documentMode == null || document.documentMode == 5);
2012-06-06 12:52:32 +00:00
var w = this.container.clientWidth;
var h = this.container.clientHeight;
if (this.container == document.body)
{
w = document.body.clientWidth || document.documentElement.clientWidth;
h = (quirks) ? document.body.clientHeight || document.documentElement.clientHeight : document.documentElement.clientHeight;
}
2012-05-21 20:32:26 +00:00
2014-07-25 06:48:01 +00:00
// Workaround for bug on iOS see
// http://stackoverflow.com/questions/19012135/ios-7-ipad-safari-landscape-innerheight-outerheight-layout-issue
// FIXME: Fix if footer visible
var off = 0;
if (mxClient.IS_IOS && !window.navigator.standalone)
{
if (window.innerHeight != document.documentElement.clientHeight)
{
off = document.documentElement.clientHeight - window.innerHeight;
window.scrollTo(0, 0);
}
}
2012-05-21 20:32:26 +00:00
var effHsplitPosition = Math.max(0, Math.min(this.hsplitPosition, w - this.splitSize - 20));
2014-04-01 11:30:48 +00:00
var tmp = 0;
2012-05-21 20:32:26 +00:00
2014-04-01 11:30:48 +00:00
if (this.menubar != null)
{
this.menubarContainer.style.height = this.menubarHeight + 'px';
tmp += this.menubarHeight;
}
2012-12-18 13:09:38 +00:00
2014-04-01 11:30:48 +00:00
if (this.toolbar != null)
{
this.toolbarContainer.style.top = this.menubarHeight + 'px';
this.toolbarContainer.style.height = this.toolbarHeight + 'px';
tmp += this.toolbarHeight;
}
2012-12-18 13:09:38 +00:00
2014-04-01 11:30:48 +00:00
if (tmp > 0 && !mxClient.IS_QUIRKS)
2012-12-18 13:09:38 +00:00
{
tmp += 1;
}
2013-05-23 16:14:48 +00:00
var sidebarFooterHeight = 0;
if (this.sidebarFooterContainer != null)
{
2014-07-25 06:48:01 +00:00
var bottom = this.footerHeight + off;
2013-05-23 16:14:48 +00:00
sidebarFooterHeight = Math.max(0, Math.min(h - tmp - bottom, this.sidebarFooterHeight));
this.sidebarFooterContainer.style.width = effHsplitPosition + 'px';
this.sidebarFooterContainer.style.height = sidebarFooterHeight + 'px';
this.sidebarFooterContainer.style.bottom = bottom + 'px';
}
2012-12-18 13:09:38 +00:00
this.sidebarContainer.style.top = tmp + 'px';
2012-05-21 20:32:26 +00:00
this.sidebarContainer.style.width = effHsplitPosition + 'px';
2014-05-05 08:30:00 +00:00
2014-04-01 11:30:48 +00:00
this.diagramContainer.style.left = (this.hsplit.parentNode != null) ? (effHsplitPosition + this.splitSize) + 'px' : '0px';
2012-05-21 20:32:26 +00:00
this.diagramContainer.style.top = this.sidebarContainer.style.top;
this.footerContainer.style.height = this.footerHeight + 'px';
this.hsplit.style.top = this.sidebarContainer.style.top;
2014-07-25 06:48:01 +00:00
this.hsplit.style.bottom = (this.footerHeight + off) + 'px';
2012-05-21 20:32:26 +00:00
this.hsplit.style.left = effHsplitPosition + 'px';
if (quirks)
{
this.menubarContainer.style.width = w + 'px';
this.toolbarContainer.style.width = this.menubarContainer.style.width;
2014-05-05 08:30:00 +00:00
var sidebarHeight = Math.max(0, h - this.footerHeight - this.menubarHeight - this.toolbarHeight);
2013-05-23 16:14:48 +00:00
this.sidebarContainer.style.height = (sidebarHeight - sidebarFooterHeight) + 'px';
2014-04-01 11:30:48 +00:00
this.diagramContainer.style.width = (this.hsplit.parentNode != null) ? Math.max(0, w - effHsplitPosition - this.splitSize) + 'px' : w + 'px';
2014-05-05 08:30:00 +00:00
var diagramHeight = Math.max(0, h - this.footerHeight - this.menubarHeight - this.toolbarHeight);
2012-05-21 20:32:26 +00:00
this.diagramContainer.style.height = diagramHeight + 'px';
this.footerContainer.style.width = this.menubarContainer.style.width;
this.hsplit.style.height = diagramHeight + 'px';
}
else
{
2014-07-25 06:48:01 +00:00
if (this.footerHeight > 0)
{
this.footerContainer.style.bottom = off + 'px';
}
this.sidebarContainer.style.bottom = (this.footerHeight + sidebarFooterHeight + off) + 'px';
this.diagramContainer.style.bottom = (this.footerHeight + off) + 'px';
2012-05-21 20:32:26 +00:00
}
2014-05-12 09:58:11 +00:00
this.editor.graph.sizeDidChange();
2012-05-21 20:32:26 +00:00
};
/**
* Creates the required containers.
*/
EditorUi.prototype.createDivs = function()
{
2012-06-14 12:43:20 +00:00
this.menubarContainer = this.createDiv('geMenubarContainer');
this.toolbarContainer = this.createDiv('geToolbarContainer');
this.sidebarContainer = this.createDiv('geSidebarContainer');
this.diagramContainer = this.createDiv('geDiagramContainer');
this.footerContainer = this.createDiv('geFooterContainer');
this.hsplit = this.createDiv('geHsplit');
2012-05-21 20:32:26 +00:00
// Sets static style for containers
this.menubarContainer.style.top = '0px';
this.menubarContainer.style.left = '0px';
this.menubarContainer.style.right = '0px';
this.toolbarContainer.style.left = '0px';
this.toolbarContainer.style.right = '0px';
this.sidebarContainer.style.left = '0px';
this.diagramContainer.style.right = '0px';
this.footerContainer.style.left = '0px';
this.footerContainer.style.right = '0px';
this.footerContainer.style.bottom = '0px';
this.hsplit.style.width = this.splitSize + 'px';
2013-05-23 16:14:48 +00:00
this.sidebarFooterContainer = this.createSidebarFooterContainer();
if (this.sidebarFooterContainer)
{
this.sidebarFooterContainer.style.left = '0px';
}
};
/**
* Hook for sidebar footer container. This implementation returns null.
*/
EditorUi.prototype.createSidebarFooterContainer = function()
{
return null;
2012-05-21 20:32:26 +00:00
};
/**
* Creates the required containers.
*/
EditorUi.prototype.createUi = function()
{
// Creates menubar
2012-06-14 12:43:20 +00:00
this.menubar = this.menus.createMenubar(this.createDiv('geMenubar'));
2014-04-01 11:30:48 +00:00
if (this.menubar != null)
{
this.menubarContainer.appendChild(this.menubar.container);
}
2012-05-21 20:32:26 +00:00
2014-07-25 06:48:01 +00:00
// Adds status bar in menubar
if (this.menubar != null)
{
this.statusContainer = this.createStatusContainer();
// Connects the status bar to the editor status
this.editor.addListener('statusChanged', mxUtils.bind(this, function()
{
this.setStatusText(this.editor.getStatus());
}));
this.setStatusText(this.editor.getStatus());
this.menubar.container.appendChild(this.statusContainer);
// Inserts into DOM
this.container.appendChild(this.menubarContainer);
}
2012-05-21 20:32:26 +00:00
// Creates toolbar
2012-06-14 12:43:20 +00:00
this.toolbar = this.createToolbar(this.createDiv('geToolbar'));
2014-04-01 11:30:48 +00:00
if (this.toolbar != null)
{
this.toolbarContainer.appendChild(this.toolbar.container);
this.container.appendChild(this.toolbarContainer);
}
2014-01-08 16:23:20 +00:00
2012-05-21 20:32:26 +00:00
// Creates the sidebar
this.sidebar = this.createSidebar(this.sidebarContainer);
2014-04-01 11:30:48 +00:00
if (this.sidebar != null)
{
this.container.appendChild(this.sidebarContainer);
}
2012-05-21 20:32:26 +00:00
// Creates the footer
2014-04-01 11:30:48 +00:00
var footer = this.createFooter();
if (footer != null)
{
this.footerContainer.appendChild(footer);
this.container.appendChild(this.footerContainer);
}
2012-05-21 20:32:26 +00:00
2014-04-01 11:30:48 +00:00
if (this.sidebar != null && this.sidebarFooterContainer)
2012-05-21 20:32:26 +00:00
{
2014-04-01 11:30:48 +00:00
this.container.appendChild(this.sidebarFooterContainer);
}
this.container.appendChild(this.diagramContainer);
2012-05-21 20:32:26 +00:00
2014-04-01 11:30:48 +00:00
// HSplit
2014-05-05 08:30:00 +00:00
if (this.sidebar != null)
2012-05-21 20:32:26 +00:00
{
2014-04-01 11:30:48 +00:00
this.container.appendChild(this.hsplit);
this.addSplitHandler(this.hsplit, true, 0, mxUtils.bind(this, function(value)
{
this.hsplitPosition = value;
this.refresh();
}));
}
2012-05-21 20:32:26 +00:00
};
/**
* Creates a new toolbar for the given container.
*/
EditorUi.prototype.createStatusContainer = function()
{
var container = document.createElement('a');
2012-06-14 12:43:20 +00:00
container.className = 'geItem geStatus';
2012-05-21 20:32:26 +00:00
return container;
};
/**
* Creates a new toolbar for the given container.
*/
EditorUi.prototype.setStatusText = function(value)
{
this.statusContainer.innerHTML = value;
};
/**
* Creates a new toolbar for the given container.
*/
EditorUi.prototype.createToolbar = function(container)
{
return new Toolbar(this, container);
};
/**
* Creates a new sidebar for the given container.
*/
EditorUi.prototype.createSidebar = function(container)
{
return new Sidebar(this, container);
};
/**
* Creates and returns a new footer.
*/
EditorUi.prototype.createFooter = function()
{
2012-06-14 12:43:20 +00:00
return this.createDiv('geFooter');
2012-05-21 20:32:26 +00:00
};
/**
* Creates the actual toolbar for the toolbar container.
*/
EditorUi.prototype.createDiv = function(classname)
{
var elt = document.createElement('div');
elt.className = classname;
return elt;
};
/**
* Updates the states of the given undo/redo items.
*/
EditorUi.prototype.addSplitHandler = function(elt, horizontal, dx, onChange)
{
var start = null;
var initial = null;
2014-04-13 06:49:20 +00:00
var ignoreClick = true;
var last = null;
2013-06-28 19:07:06 +00:00
// Disables built-in pan and zoom in IE10 and later
if (mxClient.IS_POINTER)
{
elt.style.msTouchAction = 'none';
}
2012-05-21 20:32:26 +00:00
2014-04-13 06:49:20 +00:00
var getValue = mxUtils.bind(this, function()
2012-05-21 20:32:26 +00:00
{
2014-04-13 06:49:20 +00:00
var result = parseInt(((horizontal) ? elt.style.left : elt.style.bottom));
// Takes into account hidden footer
if (!horizontal)
{
result = result + dx - this.footerHeight;
}
return result;
});
2012-05-21 20:32:26 +00:00
function moveHandler(evt)
{
if (start != null)
{
var pt = new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
onChange(Math.max(0, initial + ((horizontal) ? (pt.x - start.x) : (start.y - pt.y)) - dx));
2014-04-13 06:49:20 +00:00
if (initial != getValue())
{
ignoreClick = true;
last = null;
}
2012-05-21 20:32:26 +00:00
}
2013-05-23 16:14:48 +00:00
};
2012-05-21 20:32:26 +00:00
2013-05-23 16:14:48 +00:00
function dropHandler(evt)
2012-05-21 20:32:26 +00:00
{
moveHandler(evt);
initial = null;
2014-04-13 06:49:20 +00:00
start = null;
2013-05-23 16:14:48 +00:00
};
mxEvent.addGestureListeners(elt, function(evt)
2013-06-28 19:07:06 +00:00
{
start = new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
initial = getValue();
2014-04-13 06:49:20 +00:00
ignoreClick = false;
2013-06-28 19:07:06 +00:00
mxEvent.consume(evt);
});
2014-04-13 06:49:20 +00:00
mxEvent.addListener(elt, 'click', function(evt)
{
if (!ignoreClick)
{
var next = (last != null) ? last - dx : 0;
last = getValue();
onChange(next);
}
});
2013-05-23 16:14:48 +00:00
2013-06-28 19:07:06 +00:00
mxEvent.addGestureListeners(document, null, moveHandler, dropHandler);
2012-05-21 20:32:26 +00:00
};
/**
* Displays a print dialog.
*/
EditorUi.prototype.showDialog = function(elt, w, h, modal, closable, onClose)
{
2013-07-12 07:09:46 +00:00
this.editor.graph.tooltipHandler.hideTooltip();
2014-05-05 08:30:00 +00:00
if (this.dialogs == null)
{
this.dialogs = [];
}
2013-12-09 08:35:20 +00:00
this.dialog = new Dialog(this, elt, w, h, modal, closable, onClose);
2014-05-05 08:30:00 +00:00
this.dialogs.push(this.dialog);
2012-05-21 20:32:26 +00:00
};
/**
* Displays a print dialog.
*/
2013-11-11 12:31:46 +00:00
EditorUi.prototype.hideDialog = function(cancel)
2012-05-21 20:32:26 +00:00
{
2014-05-05 08:30:00 +00:00
if (this.dialogs != null && this.dialogs.length > 0)
2012-05-21 20:32:26 +00:00
{
2014-05-05 08:30:00 +00:00
var dlg = this.dialogs.pop();
dlg.close(cancel);
this.dialog = (this.dialogs.length > 0) ? this.dialogs[this.dialogs.length - 1] : null;
if (this.dialog == null && this.editor.graph.container.style.visibility != 'hidden')
2013-12-09 08:35:20 +00:00
{
this.editor.graph.container.focus();
}
2014-08-29 08:54:05 +00:00
this.editor.fireEvent(new mxEventObject('hideDialog'));
2012-05-21 20:32:26 +00:00
}
};
/**
* Adds the label menu items to the given menu and parent.
*/
EditorUi.prototype.openFile = function()
{
// Closes dialog after open
2013-12-20 16:51:26 +00:00
window.openFile = new OpenFile(mxUtils.bind(this, function(cancel)
2012-05-21 20:32:26 +00:00
{
2013-12-20 16:51:26 +00:00
this.hideDialog(cancel);
2012-05-21 20:32:26 +00:00
}));
// Removes openFile if dialog is closed
2014-07-25 06:48:01 +00:00
this.showDialog(new OpenDialog(this).container, 360, 220, true, true, function()
2012-05-21 20:32:26 +00:00
{
window.openFile = null;
});
};
/**
* Adds the label menu items to the given menu and parent.
*/
EditorUi.prototype.saveFile = function(forceDialog)
{
if (!forceDialog && this.editor.filename != null)
{
this.save(this.editor.getOrCreateFilename());
}
else
{
2014-01-08 16:23:20 +00:00
var dlg = new FilenameDialog(this, this.editor.getOrCreateFilename(), mxResources.get('save'), mxUtils.bind(this, function(name)
2013-11-11 12:31:46 +00:00
{
2013-12-09 08:35:20 +00:00
this.save(name, true);
2014-06-11 13:13:38 +00:00
}), null, mxUtils.bind(this, function(name)
{
if (name != null && name.length > 0)
{
return true;
}
mxUtils.confirm(mxResources.get('invalidName'));
return false;
2014-01-08 16:23:20 +00:00
}));
this.showDialog(dlg.container, 300, 100, true, true);
dlg.init();
2012-05-21 20:32:26 +00:00
}
};
2013-11-18 15:18:56 +00:00
/**
* Saves the current graph under the given filename.
*/
EditorUi.prototype.save = function(name)
{
if (name != null)
{
var xml = mxUtils.getXml(this.editor.getGraphXml());
try
{
if (useLocalStorage)
{
if (localStorage.getItem(name) != null &&
!mxUtils.confirm(mxResources.get('replace', [name])))
{
return;
}
localStorage.setItem(name, xml);
this.editor.setStatus(mxResources.get('saved') + ' ' + new Date());
}
else
{
if (xml.length < MAX_REQUEST_SIZE)
{
xml = encodeURIComponent(xml);
2013-12-09 08:35:20 +00:00
name = encodeURIComponent(name);
new mxXmlRequest(SAVE_URL, 'filename=' + name + '&xml=' + xml).simulate(document, '_blank');
2013-11-18 15:18:56 +00:00
}
else
{
mxUtils.alert(mxResources.get('drawingTooLarge'));
mxUtils.popup(xml);
return;
}
}
this.editor.setModified(false);
this.editor.setFilename(name);
2013-12-09 08:35:20 +00:00
this.updateDocumentTitle();
2013-11-18 15:18:56 +00:00
}
catch (e)
{
this.editor.setStatus('Error saving file');
}
}
};
2012-05-21 20:32:26 +00:00
/**
* Executes the given layout.
*/
2013-06-28 19:07:06 +00:00
EditorUi.prototype.executeLayout = function(exec, animate, post)
2012-05-21 20:32:26 +00:00
{
2013-06-28 19:07:06 +00:00
var graph = this.editor.graph;
2012-05-21 20:32:26 +00:00
2014-01-13 16:13:21 +00:00
if (graph.isEnabled())
2012-05-21 20:32:26 +00:00
{
2014-01-13 16:13:21 +00:00
graph.getModel().beginUpdate();
try
2012-05-21 20:32:26 +00:00
{
2014-01-13 16:13:21 +00:00
exec();
2012-05-21 20:32:26 +00:00
}
2014-01-13 16:13:21 +00:00
catch (e)
{
throw e;
}
finally
2012-05-21 20:32:26 +00:00
{
2014-01-13 16:13:21 +00:00
// Animates the changes in the graph model except
// for Camino, where animation is too slow
if (this.allowAnimation && animate && navigator.userAgent.indexOf('Camino') < 0)
{
// New API for animating graph layout results asynchronously
var morph = new mxMorphing(graph);
morph.addListener(mxEvent.DONE, mxUtils.bind(this, function()
{
graph.getModel().endUpdate();
if (post != null)
{
post();
}
}));
morph.startAnimation();
}
else
{
graph.getModel().endUpdate();
}
2012-05-21 20:32:26 +00:00
}
}
};
2014-04-13 06:49:20 +00:00
/**
* Hides the current menu.
*/
EditorUi.prototype.showImageDialog = function(title, value, fn)
{
var cellEditor = this.editor.graph.cellEditor;
var selState = cellEditor.saveSelection();
var newValue = mxUtils.prompt(title, value);
cellEditor.restoreSelection(selState);
if (newValue != null && newValue.length > 0)
{
var img = new Image();
img.onload = function()
{
fn(newValue, img.width, img.height);
};
img.onerror = function()
{
mxUtils.alert(mxResources.get('fileNotFound'));
};
img.src = newValue;
}
};
2014-05-05 08:30:00 +00:00
/**
* Hides the current menu.
*/
EditorUi.prototype.showLinkDialog = function(value, btnLabel, fn)
{
var dlg = new LinkDialog(this, value, btnLabel, fn);
this.showDialog(dlg.container, 320, 90, true, true);
dlg.init();
};
2014-04-01 11:30:48 +00:00
/**
* Creates the keyboard event handler for the current graph and history.
*/
EditorUi.prototype.confirm = function(msg, okFn, cancelFn)
{
if (mxUtils.confirm(msg))
{
if (okFn != null)
{
okFn();
}
}
else if (cancelFn != null)
{
cancelFn();
}
};
2014-05-05 08:30:00 +00:00
/**
* Creates the keyboard event handler for the current graph and history.
*/
2014-06-11 13:13:38 +00:00
EditorUi.prototype.createOutline = function(wnd)
2014-05-05 08:30:00 +00:00
{
var outline = new mxOutline(this.editor.graph);
outline.border = 20;
2014-06-11 13:13:38 +00:00
mxEvent.addListener(window, 'resize', function()
{
outline.update();
});
2014-05-05 08:30:00 +00:00
return outline;
};
2012-05-21 20:32:26 +00:00
/**
* Creates the keyboard event handler for the current graph and history.
*/
EditorUi.prototype.createKeyHandler = function(editor)
{
var graph = this.editor.graph;
var keyHandler = new mxKeyHandler(graph);
// Routes command-key to control-key on Mac
keyHandler.isControlDown = function(evt)
{
return mxEvent.isControlDown(evt) || (mxClient.IS_MAC && evt.metaKey);
};
// Helper function to move cells with the cursor keys
2014-07-25 06:48:01 +00:00
function nudge(keyCode, stepSize)
2012-05-21 20:32:26 +00:00
{
2014-01-13 16:13:21 +00:00
if (!graph.isSelectionEmpty() && graph.isEnabled())
2012-05-21 20:32:26 +00:00
{
2014-07-25 06:48:01 +00:00
stepSize = (stepSize != null) ? stepSize : 1;
2012-05-21 20:32:26 +00:00
var dx = 0;
var dy = 0;
if (keyCode == 37)
{
2014-07-25 06:48:01 +00:00
dx = -stepSize;
2012-05-21 20:32:26 +00:00
}
else if (keyCode == 38)
{
2014-07-25 06:48:01 +00:00
dy = -stepSize;
2012-05-21 20:32:26 +00:00
}
else if (keyCode == 39)
{
2014-07-25 06:48:01 +00:00
dx = stepSize;
2012-05-21 20:32:26 +00:00
}
else if (keyCode == 40)
{
2014-07-25 06:48:01 +00:00
dy = stepSize;
2012-05-21 20:32:26 +00:00
}
graph.moveCells(graph.getSelectionCells(), dx, dy);
2013-02-21 15:55:28 +00:00
graph.scrollCellToVisible(graph.getSelectionCell());
2012-05-21 20:32:26 +00:00
}
};
// Binds keystrokes to actions
2014-06-11 13:13:38 +00:00
keyHandler.bindAction = mxUtils.bind(this, function(code, control, key, shift)
2012-05-21 20:32:26 +00:00
{
var action = this.actions.get(key);
if (action != null)
{
var f = function()
{
2014-01-13 16:13:21 +00:00
if (action.isEnabled())
2012-05-21 20:32:26 +00:00
{
action.funct();
}
};
if (control)
{
if (shift)
{
keyHandler.bindControlShiftKey(code, f);
}
else
{
keyHandler.bindControlKey(code, f);
}
}
else
{
if (shift)
{
keyHandler.bindShiftKey(code, f);
}
else
{
keyHandler.bindKey(code, f);
}
}
}
});
var ui = this;
var keyHandleEscape = keyHandler.escape;
keyHandler.escape = function(evt)
{
keyHandleEscape.apply(this, arguments);
};
// Ignores enter keystroke. Remove this line if you want the
// enter keystroke to stop editing.
keyHandler.enter = function() {};
2014-07-25 06:48:01 +00:00
keyHandler.bindControlKey(13, function() { graph.foldCells(false); }); // Ctrl+Enter
keyHandler.bindControlKey(8, function() { graph.foldCells(true); }); // Ctrl+Backspace
2012-05-21 20:32:26 +00:00
keyHandler.bindKey(33, function() { graph.exitGroup(); }); // Page Up
keyHandler.bindKey(34, function() { graph.enterGroup(); }); // Page Down
keyHandler.bindKey(36, function() { graph.home(); }); // Home
keyHandler.bindKey(35, function() { graph.refresh(); }); // End
keyHandler.bindKey(37, function() { nudge(37); }); // Left arrow
keyHandler.bindKey(38, function() { nudge(38); }); // Up arrow
keyHandler.bindKey(39, function() { nudge(39); }); // Right arrow
keyHandler.bindKey(40, function() { nudge(40); }); // Down arrow
2014-07-25 06:48:01 +00:00
keyHandler.bindShiftKey(37, function() { nudge(37, graph.gridSize); }); // Shift+Left arrow
keyHandler.bindShiftKey(38, function() { nudge(38, graph.gridSize); }); // Shift+Up arrow
keyHandler.bindShiftKey(39, function() { nudge(39, graph.gridSize); }); // Shift+Right arrow
keyHandler.bindShiftKey(40, function() { nudge(40, graph.gridSize); }); // Shift+Down arrow
2014-06-11 13:13:38 +00:00
keyHandler.bindAction(8, false, 'delete'); // Backspace
keyHandler.bindAction(46, false, 'delete'); // Delete
2014-11-10 09:02:21 +00:00
keyHandler.bindAction(82, true, 'switchDirection'); // Ctrl+R
2014-06-11 13:13:38 +00:00
keyHandler.bindAction(83, true, 'save'); // Ctrl+S
keyHandler.bindAction(83, true, 'saveAs', true); // Ctrl+Shift+S
keyHandler.bindAction(107, false, 'zoomIn'); // Add
keyHandler.bindAction(109, false, 'zoomOut'); // Subtract
keyHandler.bindAction(65, true, 'selectAll'); // Ctrl+A
keyHandler.bindAction(65, true, 'selectVertices', true); // Ctrl+Shift+A
keyHandler.bindAction(69, true, 'selectEdges', true); // Ctrl+Shift+E
2014-07-25 06:48:01 +00:00
keyHandler.bindAction(69, true, 'style'); // Ctrl+E
2014-06-11 13:13:38 +00:00
keyHandler.bindAction(66, true, 'toBack'); // Ctrl+B
keyHandler.bindAction(70, true, 'toFront', true); // Ctrl+Shift+F
keyHandler.bindAction(68, true, 'duplicate'); // Ctrl+D
2014-10-30 08:08:05 +00:00
keyHandler.bindAction(68, true, 'setAsDefaultStyle', true); // Ctrl+Shift+D
2014-06-11 13:13:38 +00:00
keyHandler.bindAction(90, true, 'undo'); // Ctrl+Z
keyHandler.bindAction(89, true, 'redo'); // Ctrl+Y
keyHandler.bindAction(88, true, 'cut'); // Ctrl+X
keyHandler.bindAction(67, true, 'copy'); // Ctrl+C
2014-07-25 06:48:01 +00:00
keyHandler.bindAction(81, true, 'connectionPoints'); // Ctrl+Q
2014-06-11 13:13:38 +00:00
keyHandler.bindAction(86, true, 'paste'); // Ctrl+V
keyHandler.bindAction(71, true, 'group'); // Ctrl+G
keyHandler.bindAction(77, true, 'editData'); // Ctrl+M
keyHandler.bindAction(71, true, 'grid', true); // Ctrl+Shift+G
keyHandler.bindAction(76, true, 'lockUnlock'); // Ctrl+L
keyHandler.bindAction(76, true, 'layers', true); // Ctrl+Shift+L
keyHandler.bindAction(79, true, 'outline', true); // Ctrl+Shift+O
keyHandler.bindAction(80, true, 'print'); // Ctrl+P
keyHandler.bindAction(85, true, 'ungroup'); // Ctrl+U
keyHandler.bindAction(112, false, 'about'); // F1
2014-07-25 06:48:01 +00:00
keyHandler.bindKey(113, function() { graph.startEditingAtCell(); }); // F2
2012-05-21 20:32:26 +00:00
return keyHandler;
};