123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- /**
- * Copyright (c) 2006-2015, JGraph Ltd
- * Copyright (c) 2006-2015, Gaudenz Alder
- */
- /**
- * Class: mxToolbar
- *
- * Creates a toolbar inside a given DOM node. The toolbar may contain icons,
- * buttons and combo boxes.
- *
- * Event: mxEvent.SELECT
- *
- * Fires when an item was selected in the toolbar. The <code>function</code>
- * property contains the function that was selected in <selectMode>.
- *
- * Constructor: mxToolbar
- *
- * Constructs a toolbar in the specified container.
- *
- * Parameters:
- *
- * container - DOM node that contains the toolbar.
- */
- function mxToolbar(container)
- {
- this.container = container;
- };
- /**
- * Extends mxEventSource.
- */
- mxToolbar.prototype = new mxEventSource();
- mxToolbar.prototype.constructor = mxToolbar;
- /**
- * Variable: container
- *
- * Reference to the DOM nodes that contains the toolbar.
- */
- mxToolbar.prototype.container = null;
- /**
- * Variable: enabled
- *
- * Specifies if events are handled. Default is true.
- */
- mxToolbar.prototype.enabled = true;
- /**
- * Variable: noReset
- *
- * Specifies if <resetMode> requires a forced flag of true for resetting
- * the current mode in the toolbar. Default is false. This is set to true
- * if the toolbar item is double clicked to avoid a reset after a single
- * use of the item.
- */
- mxToolbar.prototype.noReset = false;
- /**
- * Variable: updateDefaultMode
- *
- * Boolean indicating if the default mode should be the last selected
- * switch mode or the first inserted switch mode. Default is true, that
- * is the last selected switch mode is the default mode. The default mode
- * is the mode to be selected after a reset of the toolbar. If this is
- * false, then the default mode is the first inserted mode item regardless
- * of what was last selected. Otherwise, the selected item after a reset is
- * the previously selected item.
- */
- mxToolbar.prototype.updateDefaultMode = true;
- /**
- * Function: addItem
- *
- * Adds the given function as an image with the specified title and icon
- * and returns the new image node.
- *
- * Parameters:
- *
- * title - Optional string that is used as the tooltip.
- * icon - Optional URL of the image to be used. If no URL is given, then a
- * button is created.
- * funct - Function to execute on a mouse click.
- * pressedIcon - Optional URL of the pressed image. Default is a gray
- * background.
- * style - Optional style classname. Default is mxToolbarItem.
- * factoryMethod - Optional factory method for popup menu, eg.
- * function(menu, evt, cell) { menu.addItem('Hello, World!'); }
- */
- mxToolbar.prototype.addItem = function(title, icon, funct, pressedIcon, style, factoryMethod)
- {
- var img = document.createElement((icon != null) ? 'img' : 'button');
- var initialClassName = style || ((factoryMethod != null) ?
- 'mxToolbarMode' : 'mxToolbarItem');
- img.className = initialClassName;
- img.setAttribute('src', icon);
-
- if (title != null)
- {
- if (icon != null)
- {
- img.setAttribute('title', title);
- }
- else
- {
- mxUtils.write(img, title);
- }
- }
-
- this.container.appendChild(img);
- // Invokes the function on a click on the toolbar item
- if (funct != null)
- {
- mxEvent.addListener(img, 'click', funct);
-
- if (mxClient.IS_TOUCH)
- {
- mxEvent.addListener(img, 'touchend', funct);
- }
- }
- var mouseHandler = mxUtils.bind(this, function(evt)
- {
- if (pressedIcon != null)
- {
- img.setAttribute('src', icon);
- }
- else
- {
- img.style.backgroundColor = '';
- }
- });
- // Highlights the toolbar item with a gray background
- // while it is being clicked with the mouse
- mxEvent.addGestureListeners(img, mxUtils.bind(this, function(evt)
- {
- if (pressedIcon != null)
- {
- img.setAttribute('src', pressedIcon);
- }
- else
- {
- img.style.backgroundColor = 'gray';
- }
-
- // Popup Menu
- if (factoryMethod != null)
- {
- if (this.menu == null)
- {
- this.menu = new mxPopupMenu();
- this.menu.init();
- }
-
- var last = this.currentImg;
-
- if (this.menu.isMenuShowing())
- {
- this.menu.hideMenu();
- }
-
- if (last != img)
- {
- // Redirects factory method to local factory method
- this.currentImg = img;
- this.menu.factoryMethod = factoryMethod;
-
- var point = new mxPoint(
- img.offsetLeft,
- img.offsetTop + img.offsetHeight);
- this.menu.popup(point.x, point.y, null, evt);
- // Sets and overrides to restore classname
- if (this.menu.isMenuShowing())
- {
- img.className = initialClassName + 'Selected';
-
- this.menu.hideMenu = function()
- {
- mxPopupMenu.prototype.hideMenu.apply(this);
- img.className = initialClassName;
- this.currentImg = null;
- };
- }
- }
- }
- }), null, mouseHandler);
- mxEvent.addListener(img, 'mouseout', mouseHandler);
-
- return img;
- };
- /**
- * Function: addCombo
- *
- * Adds and returns a new SELECT element using the given style. The element
- * is placed inside a DIV with the mxToolbarComboContainer style classname.
- *
- * Parameters:
- *
- * style - Optional style classname. Default is mxToolbarCombo.
- */
- mxToolbar.prototype.addCombo = function(style)
- {
- var div = document.createElement('div');
- div.style.display = 'inline';
- div.className = 'mxToolbarComboContainer';
-
- var select = document.createElement('select');
- select.className = style || 'mxToolbarCombo';
- div.appendChild(select);
-
- this.container.appendChild(div);
-
- return select;
- };
- /**
- * Function: addActionCombo
- *
- * Adds and returns a new SELECT element using the given title as the
- * default element. The selection is reset to this element after each
- * change.
- *
- * Parameters:
- *
- * title - String that specifies the title of the default element.
- * style - Optional style classname. Default is mxToolbarCombo.
- */
- mxToolbar.prototype.addActionCombo = function(title, style)
- {
- var select = document.createElement('select');
- select.className = style || 'mxToolbarCombo';
- this.addOption(select, title, null);
-
- mxEvent.addListener(select, 'change', function(evt)
- {
- var value = select.options[select.selectedIndex];
- select.selectedIndex = 0;
-
- if (value.funct != null)
- {
- value.funct(evt);
- }
- });
-
- this.container.appendChild(select);
-
- return select;
- };
- /**
- * Function: addOption
- *
- * Adds and returns a new OPTION element inside the given SELECT element.
- * If the given value is a function then it is stored in the option's funct
- * field.
- *
- * Parameters:
- *
- * combo - SELECT element that will contain the new entry.
- * title - String that specifies the title of the option.
- * value - Specifies the value associated with this option.
- */
- mxToolbar.prototype.addOption = function(combo, title, value)
- {
- var option = document.createElement('option');
- mxUtils.writeln(option, title);
-
- if (typeof(value) == 'function')
- {
- option.funct = value;
- }
- else
- {
- option.setAttribute('value', value);
- }
-
- combo.appendChild(option);
-
- return option;
- };
- /**
- * Function: addSwitchMode
- *
- * Adds a new selectable item to the toolbar. Only one switch mode item may
- * be selected at a time. The currently selected item is the default item
- * after a reset of the toolbar.
- */
- mxToolbar.prototype.addSwitchMode = function(title, icon, funct, pressedIcon, style)
- {
- var img = document.createElement('img');
- img.initialClassName = style || 'mxToolbarMode';
- img.className = img.initialClassName;
- img.setAttribute('src', icon);
- img.altIcon = pressedIcon;
-
- if (title != null)
- {
- img.setAttribute('title', title);
- }
-
- mxEvent.addListener(img, 'click', mxUtils.bind(this, function(evt)
- {
- var tmp = this.selectedMode.altIcon;
-
- if (tmp != null)
- {
- this.selectedMode.altIcon = this.selectedMode.getAttribute('src');
- this.selectedMode.setAttribute('src', tmp);
- }
- else
- {
- this.selectedMode.className = this.selectedMode.initialClassName;
- }
-
- if (this.updateDefaultMode)
- {
- this.defaultMode = img;
- }
-
- this.selectedMode = img;
-
- var tmp = img.altIcon;
-
- if (tmp != null)
- {
- img.altIcon = img.getAttribute('src');
- img.setAttribute('src', tmp);
- }
- else
- {
- img.className = img.initialClassName+'Selected';
- }
-
- this.fireEvent(new mxEventObject(mxEvent.SELECT));
- funct();
- }));
-
- this.container.appendChild(img);
-
- if (this.defaultMode == null)
- {
- this.defaultMode = img;
-
- // Function should fire only once so
- // do not pass it with the select event
- this.selectMode(img);
- funct();
- }
-
- return img;
- };
- /**
- * Function: addMode
- *
- * Adds a new item to the toolbar. The selection is typically reset after
- * the item has been consumed, for example by adding a new vertex to the
- * graph. The reset is not carried out if the item is double clicked.
- *
- * The function argument uses the following signature: funct(evt, cell) where
- * evt is the native mouse event and cell is the cell under the mouse.
- */
- mxToolbar.prototype.addMode = function(title, icon, funct, pressedIcon, style, toggle)
- {
- toggle = (toggle != null) ? toggle : true;
- var img = document.createElement((icon != null) ? 'img' : 'button');
-
- img.initialClassName = style || 'mxToolbarMode';
- img.className = img.initialClassName;
- img.setAttribute('src', icon);
- img.altIcon = pressedIcon;
- if (title != null)
- {
- img.setAttribute('title', title);
- }
-
- if (this.enabled && toggle)
- {
- mxEvent.addListener(img, 'click', mxUtils.bind(this, function(evt)
- {
- this.selectMode(img, funct);
- this.noReset = false;
- }));
-
- mxEvent.addListener(img, 'dblclick', mxUtils.bind(this, function(evt)
- {
- this.selectMode(img, funct);
- this.noReset = true;
- }));
-
- if (this.defaultMode == null)
- {
- this.defaultMode = img;
- this.defaultFunction = funct;
- this.selectMode(img, funct);
- }
- }
- this.container.appendChild(img);
- return img;
- };
- /**
- * Function: selectMode
- *
- * Resets the state of the previously selected mode and displays the given
- * DOM node as selected. This function fires a select event with the given
- * function as a parameter.
- */
- mxToolbar.prototype.selectMode = function(domNode, funct)
- {
- if (this.selectedMode != domNode)
- {
- if (this.selectedMode != null)
- {
- var tmp = this.selectedMode.altIcon;
-
- if (tmp != null)
- {
- this.selectedMode.altIcon = this.selectedMode.getAttribute('src');
- this.selectedMode.setAttribute('src', tmp);
- }
- else
- {
- this.selectedMode.className = this.selectedMode.initialClassName;
- }
- }
-
- this.selectedMode = domNode;
- var tmp = this.selectedMode.altIcon;
-
- if (tmp != null)
- {
- this.selectedMode.altIcon = this.selectedMode.getAttribute('src');
- this.selectedMode.setAttribute('src', tmp);
- }
- else
- {
- this.selectedMode.className = this.selectedMode.initialClassName+'Selected';
- }
-
- this.fireEvent(new mxEventObject(mxEvent.SELECT, "function", funct));
- }
- };
- /**
- * Function: resetMode
- *
- * Selects the default mode and resets the state of the previously selected
- * mode.
- */
- mxToolbar.prototype.resetMode = function(forced)
- {
- if ((forced || !this.noReset) && this.selectedMode != this.defaultMode)
- {
- // The last selected switch mode will be activated
- // so the function was already executed and is
- // no longer required here
- this.selectMode(this.defaultMode, this.defaultFunction);
- }
- };
- /**
- * Function: addSeparator
- *
- * Adds the specifies image as a separator.
- *
- * Parameters:
- *
- * icon - URL of the separator icon.
- */
- mxToolbar.prototype.addSeparator = function(icon)
- {
- return this.addItem(null, icon, null);
- };
- /**
- * Function: addBreak
- *
- * Adds a break to the container.
- */
- mxToolbar.prototype.addBreak = function()
- {
- mxUtils.br(this.container);
- };
- /**
- * Function: addLine
- *
- * Adds a horizontal line to the container.
- */
- mxToolbar.prototype.addLine = function()
- {
- var hr = document.createElement('hr');
-
- hr.style.marginRight = '6px';
- hr.setAttribute('size', '1');
-
- this.container.appendChild(hr);
- };
- /**
- * Function: destroy
- *
- * Removes the toolbar and all its associated resources.
- */
- mxToolbar.prototype.destroy = function ()
- {
- mxEvent.release(this.container);
- this.container = null;
- this.defaultMode = null;
- this.defaultFunction = null;
- this.selectedMode = null;
-
- if (this.menu != null)
- {
- this.menu.destroy();
- }
- };
- __mxOutput.mxToolbar = typeof mxToolbar !== 'undefined' ? mxToolbar : undefined;
|