123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- /**
- * Copyright (c) 2006-2015, JGraph Ltd
- * Copyright (c) 2006-2015, Gaudenz Alder
- */
- /**
- * Class: mxTooltipHandler
- *
- * Graph event handler that displays tooltips. <mxGraph.getTooltip> is used to
- * get the tooltip for a cell or handle. This handler is built-into
- * <mxGraph.tooltipHandler> and enabled using <mxGraph.setTooltips>.
- *
- * Example:
- *
- * (code>
- * new mxTooltipHandler(graph);
- * (end)
- *
- * Constructor: mxTooltipHandler
- *
- * Constructs an event handler that displays tooltips with the specified
- * delay (in milliseconds). If no delay is specified then a default delay
- * of 500 ms (0.5 sec) is used.
- *
- * Parameters:
- *
- * graph - Reference to the enclosing <mxGraph>.
- * delay - Optional delay in milliseconds.
- */
- function mxTooltipHandler(graph, delay)
- {
- if (graph != null)
- {
- this.graph = graph;
- this.delay = delay || 500;
- this.graph.addMouseListener(this);
- }
- };
- /**
- * Variable: zIndex
- *
- * Specifies the zIndex for the tooltip and its shadow. Default is 10005.
- */
- mxTooltipHandler.prototype.zIndex = 10005;
- /**
- * Variable: graph
- *
- * Reference to the enclosing <mxGraph>.
- */
- mxTooltipHandler.prototype.graph = null;
- /**
- * Variable: delay
- *
- * Delay to show the tooltip in milliseconds. Default is 500.
- */
- mxTooltipHandler.prototype.delay = null;
- /**
- * Variable: ignoreTouchEvents
- *
- * Specifies if touch and pen events should be ignored. Default is true.
- */
- mxTooltipHandler.prototype.ignoreTouchEvents = true;
- /**
- * Variable: hideOnHover
- *
- * Specifies if the tooltip should be hidden if the mouse is moved over the
- * current cell. Default is false.
- */
- mxTooltipHandler.prototype.hideOnHover = false;
- /**
- * Variable: destroyed
- *
- * True if this handler was destroyed using <destroy>.
- */
- mxTooltipHandler.prototype.destroyed = false;
- /**
- * Variable: enabled
- *
- * Specifies if events are handled. Default is true.
- */
- mxTooltipHandler.prototype.enabled = true;
- /**
- * Function: isEnabled
- *
- * Returns true if events are handled. This implementation
- * returns <enabled>.
- */
- mxTooltipHandler.prototype.isEnabled = function()
- {
- return this.enabled;
- };
- /**
- * Function: setEnabled
- *
- * Enables or disables event handling. This implementation
- * updates <enabled>.
- */
- mxTooltipHandler.prototype.setEnabled = function(enabled)
- {
- this.enabled = enabled;
- };
- /**
- * Function: isHideOnHover
- *
- * Returns <hideOnHover>.
- */
- mxTooltipHandler.prototype.isHideOnHover = function()
- {
- return this.hideOnHover;
- };
- /**
- * Function: setHideOnHover
- *
- * Sets <hideOnHover>.
- */
- mxTooltipHandler.prototype.setHideOnHover = function(value)
- {
- this.hideOnHover = value;
- };
- /**
- * Function: init
- *
- * Initializes the DOM nodes required for this tooltip handler.
- */
- mxTooltipHandler.prototype.init = function()
- {
- if (document.body != null)
- {
- this.div = document.createElement('div');
- this.div.className = 'mxTooltip';
- this.div.style.visibility = 'hidden';
- document.body.appendChild(this.div);
- mxEvent.addGestureListeners(this.div, mxUtils.bind(this, function(evt)
- {
- var source = mxEvent.getSource(evt);
-
- if (source.nodeName != 'A')
- {
- this.hideTooltip();
- }
- }));
- }
- };
- /**
- * Function: getStateForEvent
- *
- * Returns the <mxCellState> to be used for showing a tooltip for this event.
- */
- mxTooltipHandler.prototype.getStateForEvent = function(me)
- {
- return me.getState();
- };
- /**
- * Function: mouseDown
- *
- * Handles the event by initiating a rubberband selection. By consuming the
- * event all subsequent events of the gesture are redirected to this
- * handler.
- */
- mxTooltipHandler.prototype.mouseDown = function(sender, me)
- {
- this.reset(me, false);
- this.hideTooltip();
- };
- /**
- * Function: mouseMove
- *
- * Handles the event by updating the rubberband selection.
- */
- mxTooltipHandler.prototype.mouseMove = function(sender, me)
- {
- if (me.getX() != this.lastX || me.getY() != this.lastY)
- {
- this.reset(me, true);
- var state = this.getStateForEvent(me);
-
- if (this.isHideOnHover() || state != this.state || (me.getSource() != this.node &&
- (!this.stateSource || (state != null && this.stateSource ==
- (me.isSource(state.shape) || !me.isSource(state.text))))))
- {
- this.hideTooltip();
- }
- }
-
- this.lastX = me.getX();
- this.lastY = me.getY();
- };
- /**
- * Function: mouseUp
- *
- * Handles the event by resetting the tooltip timer or hiding the existing
- * tooltip.
- */
- mxTooltipHandler.prototype.mouseUp = function(sender, me)
- {
- this.reset(me, true);
- this.hideTooltip();
- };
- /**
- * Function: resetTimer
- *
- * Resets the timer.
- */
- mxTooltipHandler.prototype.resetTimer = function()
- {
- if (this.thread != null)
- {
- window.clearTimeout(this.thread);
- this.thread = null;
- }
- };
- /**
- * Function: reset
- *
- * Resets and/or restarts the timer to trigger the display of the tooltip.
- */
- mxTooltipHandler.prototype.reset = function(me, restart, state)
- {
- if (!this.ignoreTouchEvents || mxEvent.isMouseEvent(me.getEvent()))
- {
- this.resetTimer();
- state = (state != null) ? state : this.getStateForEvent(me);
-
- if (restart && this.isEnabled() && state != null && (this.div == null ||
- this.div.style.visibility == 'hidden'))
- {
- var node = me.getSource();
- var x = me.getX();
- var y = me.getY();
- var stateSource = me.isSource(state.shape) || me.isSource(state.text);
-
- this.thread = window.setTimeout(mxUtils.bind(this, function()
- {
- if (!this.graph.isEditing() && !this.graph.popupMenuHandler.isMenuShowing() && !this.graph.isMouseDown)
- {
- // Uses information from inside event cause using the event at
- // this (delayed) point in time is not possible in IE as it no
- // longer contains the required information (member not found)
- var tip = this.graph.getTooltip(state, node, x, y);
- this.show(tip, x, y);
- this.state = state;
- this.node = node;
- this.stateSource = stateSource;
- }
- }), this.delay);
- }
- }
- };
- /**
- * Function: hide
- *
- * Hides the tooltip and resets the timer.
- */
- mxTooltipHandler.prototype.hide = function()
- {
- this.resetTimer();
- this.hideTooltip();
- };
- /**
- * Function: hideTooltip
- *
- * Hides the tooltip.
- */
- mxTooltipHandler.prototype.hideTooltip = function()
- {
- if (this.div != null)
- {
- this.div.style.visibility = 'hidden';
- this.div.innerHTML = '';
- }
- };
- /**
- * Function: show
- *
- * Shows the tooltip for the specified cell and optional index at the
- * specified location (with a vertical offset of 10 pixels).
- */
- mxTooltipHandler.prototype.show = function(tip, x, y)
- {
- if (!this.destroyed && tip != null && tip.length > 0)
- {
- // Initializes the DOM nodes if required
- if (this.div == null)
- {
- this.init();
- }
-
- var origin = mxUtils.getScrollOrigin();
- this.div.style.zIndex = this.zIndex;
- this.div.style.left = (x + origin.x) + 'px';
- this.div.style.top = (y + mxConstants.TOOLTIP_VERTICAL_OFFSET +
- origin.y) + 'px';
- if (!mxUtils.isNode(tip))
- {
- this.div.innerHTML = tip.replace(/\n/g, '<br>');
- }
- else
- {
- this.div.innerHTML = '';
- this.div.appendChild(tip);
- }
-
- this.div.style.visibility = '';
- mxUtils.fit(this.div);
- }
- };
- /**
- * Function: destroy
- *
- * Destroys the handler and all its resources and DOM nodes.
- */
- mxTooltipHandler.prototype.destroy = function()
- {
- if (!this.destroyed)
- {
- this.graph.removeMouseListener(this);
- mxEvent.release(this.div);
-
- if (this.div != null && this.div.parentNode != null)
- {
- this.div.parentNode.removeChild(this.div);
- }
-
- this.destroyed = true;
- this.div = null;
- }
- };
- __mxOutput.mxTooltipHandler = typeof mxTooltipHandler !== 'undefined' ? mxTooltipHandler : undefined;
|