Toolbar.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /**
  2. * Copyright (c) 2006-2012, JGraph Ltd
  3. */
  4. /**
  5. * Construcs a new toolbar for the given editor.
  6. */
  7. function Toolbar(editorUi, container)
  8. {
  9. this.editorUi = editorUi;
  10. this.container = container;
  11. this.staticElements = [];
  12. this.init();
  13. // Global handler to hide the current menu
  14. this.gestureHandler = mxUtils.bind(this, function(evt)
  15. {
  16. if (this.editorUi.currentMenu != null && mxEvent.getSource(evt) != this.editorUi.currentMenu.div)
  17. {
  18. this.hideMenu();
  19. }
  20. });
  21. mxEvent.addGestureListeners(document, this.gestureHandler);
  22. };
  23. /**
  24. * Image for the dropdown arrow.
  25. */
  26. Toolbar.prototype.dropDownImage = (!mxClient.IS_SVG) ? IMAGE_PATH + '/dropdown.gif' : 'data:image/gif;base64,R0lGODlhDQANAIABAHt7e////yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjAgNjEuMTM0Nzc3LCAyMDEwLzAyLzEyLTE3OjMyOjAwICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IE1hY2ludG9zaCIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpCREM1NkJFMjE0NEMxMUU1ODk1Q0M5MjQ0MTA4QjNDMSIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpCREM1NkJFMzE0NEMxMUU1ODk1Q0M5MjQ0MTA4QjNDMSI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOkQzOUMzMjZCMTQ0QjExRTU4OTVDQzkyNDQxMDhCM0MxIiBzdFJlZjpkb2N1bWVudElEPSJ4bXAuZGlkOkQzOUMzMjZDMTQ0QjExRTU4OTVDQzkyNDQxMDhCM0MxIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+Af/+/fz7+vn49/b19PPy8fDv7u3s6+rp6Ofm5eTj4uHg397d3Nva2djX1tXU09LR0M/OzczLysnIx8bFxMPCwcC/vr28u7q5uLe2tbSzsrGwr66trKuqqainpqWko6KhoJ+enZybmpmYl5aVlJOSkZCPjo2Mi4qJiIeGhYSDgoGAf359fHt6eXh3dnV0c3JxcG9ubWxramloZ2ZlZGNiYWBfXl1cW1pZWFdWVVRTUlFQT05NTEtKSUhHRkVEQ0JBQD8+PTw7Ojk4NzY1NDMyMTAvLi0sKyopKCcmJSQjIiEgHx4dHBsaGRgXFhUUExIREA8ODQwLCgkIBwYFBAMCAQAAIfkEAQAAAQAsAAAAAA0ADQAAAhGMj6nL3QAjVHIu6azbvPtWAAA7';
  27. /**
  28. * Defines the background for selected buttons.
  29. */
  30. Toolbar.prototype.selectedBackground = '#d0d0d0';
  31. /**
  32. * Defines the background for selected buttons.
  33. */
  34. Toolbar.prototype.unselectedBackground = 'none';
  35. /**
  36. * Array that contains the DOM nodes that should never be removed.
  37. */
  38. Toolbar.prototype.staticElements = null;
  39. /**
  40. * Adds the toolbar elements.
  41. */
  42. Toolbar.prototype.init = function()
  43. {
  44. var sw = screen.width;
  45. // Takes into account initial compact mode
  46. sw -= (screen.height > 740) ? 56 : 0;
  47. if (sw >= 700)
  48. {
  49. var formatMenu = this.addMenu('', mxResources.get('view') + ' (' + mxResources.get('panTooltip') + ')', true, 'viewPanels', null, true);
  50. this.addDropDownArrow(formatMenu, 'geSprite-formatpanel', 38, 50, -4, -3, 36, -8);
  51. this.addSeparator();
  52. }
  53. var viewMenu = this.addMenu('', mxResources.get('zoom') + ' (Alt+Mousewheel)', true, 'viewZoom', null, true);
  54. viewMenu.showDisabled = true;
  55. viewMenu.style.whiteSpace = 'nowrap';
  56. viewMenu.style.position = 'relative';
  57. viewMenu.style.overflow = 'hidden';
  58. if (EditorUi.compactUi)
  59. {
  60. viewMenu.style.width = '50px';
  61. }
  62. else
  63. {
  64. viewMenu.style.width = '36px';
  65. }
  66. if (sw >= 420)
  67. {
  68. this.addSeparator();
  69. var elts = this.addItems(['zoomIn', 'zoomOut']);
  70. elts[0].setAttribute('title', mxResources.get('zoomIn') + ' (' + this.editorUi.actions.get('zoomIn').shortcut + ')');
  71. elts[1].setAttribute('title', mxResources.get('zoomOut') + ' (' + this.editorUi.actions.get('zoomOut').shortcut + ')');
  72. }
  73. // Updates the label if the scale changes
  74. this.updateZoom = mxUtils.bind(this, function(sender, evt, f)
  75. {
  76. f = (f != null) ? f : 1;
  77. viewMenu.innerHTML = Math.round(this.editorUi.editor.graph.view.scale * 100 * f) + '%';
  78. this.appendDropDownImageHtml(viewMenu);
  79. if (EditorUi.compactUi)
  80. {
  81. viewMenu.getElementsByTagName('img')[0].style.right = '1px';
  82. viewMenu.getElementsByTagName('img')[0].style.top = '5px';
  83. }
  84. });
  85. this.editorUi.editor.graph.view.addListener(mxEvent.EVENT_SCALE, this.updateZoom);
  86. this.editorUi.editor.addListener('resetGraphView', this.updateZoom);
  87. // Zoom Preview
  88. this.editorUi.editor.graph.addListener('zoomPreview', mxUtils.bind(this, function(sender, evt)
  89. {
  90. this.updateZoom(sender, evt, evt.getProperty('factor'));
  91. }));
  92. var elts = this.addItems(['-', 'undo', 'redo']);
  93. elts[1].setAttribute('title', mxResources.get('undo') + ' (' + this.editorUi.actions.get('undo').shortcut + ')');
  94. elts[2].setAttribute('title', mxResources.get('redo') + ' (' + this.editorUi.actions.get('redo').shortcut + ')');
  95. if (sw >= 320)
  96. {
  97. var elts = this.addItems(['-', 'delete']);
  98. elts[1].setAttribute('title', mxResources.get('delete') + ' (' + this.editorUi.actions.get('delete').shortcut + ')');
  99. }
  100. if (sw >= 550)
  101. {
  102. this.addItems(['-', 'toFront', 'toBack']);
  103. }
  104. if (sw >= 740)
  105. {
  106. this.addItems(['-', 'fillColor']);
  107. if (sw >= 780)
  108. {
  109. this.addItems(['strokeColor']);
  110. if (sw >= 820)
  111. {
  112. this.addItems(['shadow']);
  113. }
  114. }
  115. }
  116. if (sw >= 400)
  117. {
  118. this.addSeparator();
  119. if (sw >= 440)
  120. {
  121. this.edgeShapeMenu = this.addMenuFunction('', mxResources.get('connection'), false, mxUtils.bind(this, function(menu)
  122. {
  123. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_SHAPE, 'width'], [null, null], 'geIcon geSprite geSprite-connection', null, true).setAttribute('title', mxResources.get('line'));
  124. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_SHAPE, 'width'], ['link', null], 'geIcon geSprite geSprite-linkedge', null, true).setAttribute('title', mxResources.get('link'));
  125. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_SHAPE, 'width'], ['flexArrow', null], 'geIcon geSprite geSprite-arrow', null, true).setAttribute('title', mxResources.get('arrow'));
  126. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_SHAPE, 'width'], ['arrow', null], 'geIcon geSprite geSprite-simplearrow', null, true).setAttribute('title', mxResources.get('simpleArrow'));
  127. }));
  128. this.addDropDownArrow(this.edgeShapeMenu, 'geSprite-connection', 44, 50, 0, 0, 22, -4);
  129. }
  130. this.edgeStyleMenu = this.addMenuFunction('geSprite-orthogonal', mxResources.get('waypoints'), false, mxUtils.bind(this, function(menu)
  131. {
  132. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], [null, null, null], 'geIcon geSprite geSprite-straight', null, true).setAttribute('title', mxResources.get('straight'));
  133. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], ['orthogonalEdgeStyle', null, null], 'geIcon geSprite geSprite-orthogonal', null, true).setAttribute('title', mxResources.get('orthogonal'));
  134. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_ELBOW, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], ['elbowEdgeStyle', null, null, null], 'geIcon geSprite geSprite-horizontalelbow', null, true).setAttribute('title', mxResources.get('simple'));
  135. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_ELBOW, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], ['elbowEdgeStyle', 'vertical', null, null], 'geIcon geSprite geSprite-verticalelbow', null, true).setAttribute('title', mxResources.get('simple'));
  136. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_ELBOW, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], ['isometricEdgeStyle', null, null, null], 'geIcon geSprite geSprite-horizontalisometric', null, true).setAttribute('title', mxResources.get('isometric'));
  137. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_ELBOW, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], ['isometricEdgeStyle', 'vertical', null, null], 'geIcon geSprite geSprite-verticalisometric', null, true).setAttribute('title', mxResources.get('isometric'));
  138. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], ['orthogonalEdgeStyle', '1', null], 'geIcon geSprite geSprite-curved', null, true).setAttribute('title', mxResources.get('curved'));
  139. this.editorUi.menus.edgeStyleChange(menu, '', [mxConstants.STYLE_EDGE, mxConstants.STYLE_CURVED, mxConstants.STYLE_NOEDGESTYLE], ['entityRelationEdgeStyle', null, null], 'geIcon geSprite geSprite-entity', null, true).setAttribute('title', mxResources.get('entityRelation'));
  140. }));
  141. this.addDropDownArrow(this.edgeStyleMenu, 'geSprite-orthogonal', 44, 50, 0, 0, 22, -4);
  142. }
  143. this.addSeparator();
  144. var insertMenu = this.addMenu('', mxResources.get('insert') + ' (' + mxResources.get('doubleClickTooltip') + ')', true, 'insert', null, true);
  145. this.addDropDownArrow(insertMenu, 'geSprite-plus', 38, 48, -4, -3, 36, -8);
  146. this.addSeparator();
  147. this.addTableDropDown();
  148. };
  149. /**
  150. * Adds the toolbar elements.
  151. */
  152. Toolbar.prototype.appendDropDownImageHtml = function(elt)
  153. {
  154. var img = document.createElement('img');
  155. img.setAttribute('border', '0');
  156. img.setAttribute('valign', 'middle');
  157. img.setAttribute('src', Toolbar.prototype.dropDownImage);
  158. elt.appendChild(img);
  159. img.style.position = 'absolute';
  160. img.style.right = '4px';
  161. img.style.top = (!EditorUi.compactUi ? 8 : 6) + 'px';
  162. };
  163. /**
  164. * Adds the toolbar elements.
  165. */
  166. Toolbar.prototype.addTableDropDown = function()
  167. {
  168. // KNOWN: All table stuff does not work with undo/redo
  169. // KNOWN: Lost focus after click on submenu with text (not icon) in quirks and IE8. This is because the TD seems
  170. // to catch the focus on click in these browsers. NOTE: Workaround in mxPopupMenu for icon items (without text).
  171. var menuElt = this.addMenuFunction('geIcon geSprite geSprite-table', mxResources.get('table'), false, mxUtils.bind(this, function(menu)
  172. {
  173. this.editorUi.menus.addInsertTableCellItem(menu);
  174. }));
  175. menuElt.style.position = 'relative';
  176. menuElt.style.whiteSpace = 'nowrap';
  177. menuElt.style.overflow = 'hidden';
  178. menuElt.style.width = '30px';
  179. menuElt.innerHTML = '<div class="geSprite geSprite-table"></div>';
  180. this.appendDropDownImageHtml(menuElt);
  181. var div = menuElt.getElementsByTagName('div')[0];
  182. div.style.marginLeft = '-2px';
  183. // Fix for item size in kennedy theme
  184. if (EditorUi.compactUi)
  185. {
  186. menuElt.getElementsByTagName('img')[0].style.left = '22px';
  187. menuElt.getElementsByTagName('img')[0].style.top = '5px';
  188. }
  189. // Connects to insert menu enabled state
  190. var menu = this.editorUi.menus.get('insert');
  191. // Workaround for possible not a function
  192. // when extending HTML objects
  193. if (menu != null && typeof menuElt.setEnabled === 'function')
  194. {
  195. menu.addListener('stateChanged', function()
  196. {
  197. menuElt.setEnabled(menu.enabled);
  198. });
  199. }
  200. return menuElt;
  201. };
  202. /**
  203. * Adds the toolbar elements.
  204. */
  205. Toolbar.prototype.addDropDownArrow = function(menu, sprite, width, atlasWidth, left, top, atlasDelta, atlasLeft)
  206. {
  207. atlasDelta = (atlasDelta != null) ? atlasDelta : 32;
  208. left = (EditorUi.compactUi) ? left : atlasLeft;
  209. menu.style.whiteSpace = 'nowrap';
  210. menu.style.overflow = 'hidden';
  211. menu.style.position = 'relative';
  212. menu.style.width = (atlasWidth - atlasDelta) + 'px';
  213. menu.innerHTML = '<div class="geSprite ' + sprite + '"></div>';
  214. this.appendDropDownImageHtml(menu);
  215. var div = menu.getElementsByTagName('div')[0];
  216. div.style.marginLeft = left + 'px';
  217. div.style.marginTop = top + 'px';
  218. // Fix for item size in kennedy theme
  219. if (EditorUi.compactUi)
  220. {
  221. menu.getElementsByTagName('img')[0].style.left = '24px';
  222. menu.getElementsByTagName('img')[0].style.top = '5px';
  223. menu.style.width = (width - 10) + 'px';
  224. }
  225. };
  226. /**
  227. * Sets the current font name.
  228. */
  229. Toolbar.prototype.setFontName = function(value)
  230. {
  231. if (this.fontMenu != null)
  232. {
  233. this.fontMenu.innerText = '';
  234. var div = document.createElement('div');
  235. div.style.display = 'inline-block';
  236. div.style.overflow = 'hidden';
  237. div.style.textOverflow = 'ellipsis';
  238. div.style.maxWidth = '66px';
  239. mxUtils.write(div, value);
  240. this.fontMenu.appendChild(div);
  241. this.appendDropDownImageHtml(this.fontMenu);
  242. }
  243. };
  244. /**
  245. * Sets the current font name.
  246. */
  247. Toolbar.prototype.setFontSize = function(value)
  248. {
  249. if (this.sizeMenu != null)
  250. {
  251. this.sizeMenu.innerText = '';
  252. var div = document.createElement('div');
  253. div.style.display = 'inline-block';
  254. div.style.overflow = 'hidden';
  255. div.style.textOverflow = 'ellipsis';
  256. div.style.maxWidth = '24px';
  257. mxUtils.write(div, value);
  258. this.sizeMenu.appendChild(div);
  259. this.appendDropDownImageHtml(this.sizeMenu);
  260. }
  261. };
  262. /**
  263. * Hides the current menu.
  264. */
  265. Toolbar.prototype.createTextToolbar = function()
  266. {
  267. var ui = this.editorUi;
  268. var graph = ui.editor.graph;
  269. var styleElt = this.addMenu('', mxResources.get('style'), true, 'formatBlock');
  270. styleElt.style.position = 'relative';
  271. styleElt.style.whiteSpace = 'nowrap';
  272. styleElt.style.overflow = 'hidden';
  273. styleElt.innerHTML = mxResources.get('style');
  274. this.appendDropDownImageHtml(styleElt);
  275. if (EditorUi.compactUi)
  276. {
  277. styleElt.style.paddingRight = '18px';
  278. styleElt.getElementsByTagName('img')[0].style.right = '1px';
  279. styleElt.getElementsByTagName('img')[0].style.top = '5px';
  280. }
  281. this.addSeparator();
  282. this.fontMenu = this.addMenu('', mxResources.get('fontFamily'), true, 'fontFamily');
  283. this.fontMenu.style.position = 'relative';
  284. this.fontMenu.style.whiteSpace = 'nowrap';
  285. this.fontMenu.style.overflow = 'hidden';
  286. this.fontMenu.style.width = '68px';
  287. this.setFontName(Menus.prototype.defaultFont);
  288. if (EditorUi.compactUi)
  289. {
  290. this.fontMenu.style.paddingRight = '18px';
  291. this.fontMenu.getElementsByTagName('img')[0].style.right = '1px';
  292. this.fontMenu.getElementsByTagName('img')[0].style.top = '5px';
  293. }
  294. this.addSeparator();
  295. this.sizeMenu = this.addMenu(Menus.prototype.defaultFontSize, mxResources.get('fontSize'), true, 'fontSize');
  296. this.sizeMenu.style.position = 'relative';
  297. this.sizeMenu.style.whiteSpace = 'nowrap';
  298. this.sizeMenu.style.overflow = 'hidden';
  299. this.sizeMenu.style.width = '24px';
  300. this.setFontSize(Menus.prototype.defaultFontSize);
  301. if (EditorUi.compactUi)
  302. {
  303. this.sizeMenu.style.paddingRight = '18px';
  304. this.sizeMenu.getElementsByTagName('img')[0].style.right = '1px';
  305. this.sizeMenu.getElementsByTagName('img')[0].style.top = '5px';
  306. }
  307. var elts = this.addItems(['-', 'undo', 'redo','-', 'bold', 'italic', 'underline']);
  308. elts[1].setAttribute('title', mxResources.get('undo') + ' (' + ui.actions.get('undo').shortcut + ')');
  309. elts[2].setAttribute('title', mxResources.get('redo') + ' (' + ui.actions.get('redo').shortcut + ')');
  310. elts[4].setAttribute('title', mxResources.get('bold') + ' (' + ui.actions.get('bold').shortcut + ')');
  311. elts[5].setAttribute('title', mxResources.get('italic') + ' (' + ui.actions.get('italic').shortcut + ')');
  312. elts[6].setAttribute('title', mxResources.get('underline') + ' (' + ui.actions.get('underline').shortcut + ')');
  313. // KNOWN: Lost focus after click on submenu with text (not icon) in quirks and IE8. This is because the TD seems
  314. // to catch the focus on click in these browsers. NOTE: Workaround in mxPopupMenu for icon items (without text).
  315. var alignMenu = this.addMenuFunction('', mxResources.get('align'), false, mxUtils.bind(this, function(menu)
  316. {
  317. elt = menu.addItem('', null, mxUtils.bind(this, function(evt)
  318. {
  319. graph.cellEditor.alignText(mxConstants.ALIGN_LEFT, evt);
  320. ui.fireEvent(new mxEventObject('styleChanged',
  321. 'keys', [mxConstants.STYLE_ALIGN],
  322. 'values', [mxConstants.ALIGN_LEFT],
  323. 'cells', [graph.cellEditor.getEditingCell()]));
  324. }), null, 'geIcon geSprite geSprite-left');
  325. elt.setAttribute('title', mxResources.get('left'));
  326. elt = menu.addItem('', null, mxUtils.bind(this, function(evt)
  327. {
  328. graph.cellEditor.alignText(mxConstants.ALIGN_CENTER, evt);
  329. ui.fireEvent(new mxEventObject('styleChanged',
  330. 'keys', [mxConstants.STYLE_ALIGN],
  331. 'values', [mxConstants.ALIGN_CENTER],
  332. 'cells', [graph.cellEditor.getEditingCell()]));
  333. }), null, 'geIcon geSprite geSprite-center');
  334. elt.setAttribute('title', mxResources.get('center'));
  335. elt = menu.addItem('', null, mxUtils.bind(this, function(evt)
  336. {
  337. graph.cellEditor.alignText(mxConstants.ALIGN_RIGHT, evt);
  338. ui.fireEvent(new mxEventObject('styleChanged',
  339. 'keys', [mxConstants.STYLE_ALIGN],
  340. 'values', [mxConstants.ALIGN_RIGHT],
  341. 'cells', [graph.cellEditor.getEditingCell()]));
  342. }), null, 'geIcon geSprite geSprite-right');
  343. elt.setAttribute('title', mxResources.get('right'));
  344. elt = menu.addItem('', null, mxUtils.bind(this, function()
  345. {
  346. document.execCommand('justifyfull', false, null);
  347. }), null, 'geIcon geSprite geSprite-justifyfull');
  348. elt.setAttribute('title', mxResources.get('justifyfull'));
  349. elt = menu.addItem('', null, mxUtils.bind(this, function()
  350. {
  351. document.execCommand('insertorderedlist', false, null);
  352. }), null, 'geIcon geSprite geSprite-orderedlist');
  353. elt.setAttribute('title', mxResources.get('numberedList'));
  354. elt = menu.addItem('', null, mxUtils.bind(this, function()
  355. {
  356. document.execCommand('insertunorderedlist', false, null);
  357. }), null, 'geIcon geSprite geSprite-unorderedlist');
  358. elt.setAttribute('title', mxResources.get('bulletedList'));
  359. elt = menu.addItem('', null, mxUtils.bind(this, function()
  360. {
  361. document.execCommand('outdent', false, null);
  362. }), null, 'geIcon geSprite geSprite-outdent');
  363. elt.setAttribute('title', mxResources.get('decreaseIndent'));
  364. elt = menu.addItem('', null, mxUtils.bind(this, function()
  365. {
  366. document.execCommand('indent', false, null);
  367. }), null, 'geIcon geSprite geSprite-indent');
  368. elt.setAttribute('title', mxResources.get('increaseIndent'));
  369. }));
  370. alignMenu.style.position = 'relative';
  371. alignMenu.style.whiteSpace = 'nowrap';
  372. alignMenu.style.overflow = 'hidden';
  373. alignMenu.style.width = '30px';
  374. alignMenu.innerText = '';
  375. var div = document.createElement('div');
  376. div.className = 'geSprite geSprite-left';
  377. div.style.marginLeft = '-2px';
  378. alignMenu.appendChild(div);
  379. this.appendDropDownImageHtml(alignMenu);
  380. if (EditorUi.compactUi)
  381. {
  382. alignMenu.getElementsByTagName('img')[0].style.left = '22px';
  383. alignMenu.getElementsByTagName('img')[0].style.top = '5px';
  384. }
  385. var formatMenu = this.addMenuFunction('', mxResources.get('format'), false, mxUtils.bind(this, function(menu)
  386. {
  387. elt = menu.addItem('', null, this.editorUi.actions.get('subscript').funct,
  388. null, 'geIcon geSprite geSprite-subscript');
  389. elt.setAttribute('title', mxResources.get('subscript') + ' (' + Editor.ctrlKey + '+,)');
  390. elt = menu.addItem('', null, this.editorUi.actions.get('superscript').funct,
  391. null, 'geIcon geSprite geSprite-superscript');
  392. elt.setAttribute('title', mxResources.get('superscript') + ' (' + Editor.ctrlKey + '+.)');
  393. // KNOWN: IE+FF don't return keyboard focus after color dialog (calling focus doesn't help)
  394. elt = menu.addItem('', null, this.editorUi.actions.get('fontColor').funct,
  395. null, 'geIcon geSprite geSprite-fontcolor');
  396. elt.setAttribute('title', mxResources.get('fontColor'));
  397. elt = menu.addItem('', null, this.editorUi.actions.get('backgroundColor').funct,
  398. null, 'geIcon geSprite geSprite-fontbackground');
  399. elt.setAttribute('title', mxResources.get('backgroundColor'));
  400. elt = menu.addItem('', null, mxUtils.bind(this, function()
  401. {
  402. document.execCommand('removeformat', false, null);
  403. }), null, 'geIcon geSprite geSprite-removeformat');
  404. elt.setAttribute('title', mxResources.get('removeFormat'));
  405. }));
  406. formatMenu.style.position = 'relative';
  407. formatMenu.style.whiteSpace = 'nowrap';
  408. formatMenu.style.overflow = 'hidden';
  409. formatMenu.style.width = '30px';
  410. formatMenu.innerText = '';
  411. var div = document.createElement('div');
  412. div.className = 'geSprite geSprite-dots';
  413. div.style.marginLeft = '-2px';
  414. formatMenu.appendChild(div);
  415. this.appendDropDownImageHtml(formatMenu);
  416. if (EditorUi.compactUi)
  417. {
  418. formatMenu.getElementsByTagName('img')[0].style.left = '22px';
  419. formatMenu.getElementsByTagName('img')[0].style.top = '5px';
  420. }
  421. this.addSeparator();
  422. this.addButton('geIcon geSprite geSprite-code', mxResources.get('html'), function()
  423. {
  424. graph.cellEditor.toggleViewMode();
  425. if (graph.cellEditor.textarea.innerHTML.length > 0 && (graph.cellEditor.textarea.innerHTML != '&nbsp;' || !graph.cellEditor.clearOnChange))
  426. {
  427. window.setTimeout(function()
  428. {
  429. document.execCommand('selectAll', false, null);
  430. });
  431. }
  432. });
  433. this.addSeparator();
  434. var insertMenu = this.addMenuFunction('', mxResources.get('insert'), true, mxUtils.bind(this, function(menu)
  435. {
  436. menu.addItem(mxResources.get('insertLink'), null, mxUtils.bind(this, function()
  437. {
  438. this.editorUi.actions.get('link').funct();
  439. }));
  440. menu.addItem(mxResources.get('insertImage'), null, mxUtils.bind(this, function()
  441. {
  442. this.editorUi.actions.get('image').funct();
  443. }));
  444. menu.addItem(mxResources.get('insertHorizontalRule'), null, mxUtils.bind(this, function()
  445. {
  446. document.execCommand('inserthorizontalrule', false, null);
  447. }));
  448. }));
  449. insertMenu.style.whiteSpace = 'nowrap';
  450. insertMenu.style.overflow = 'hidden';
  451. insertMenu.style.position = 'relative';
  452. insertMenu.style.width = '16px';
  453. insertMenu.innerText = '';
  454. var div = document.createElement('div');
  455. div.className = 'geSprite geSprite-plus';
  456. div.style.marginLeft = '-4px';
  457. div.style.marginTop = '-3px';
  458. insertMenu.appendChild(div);
  459. this.appendDropDownImageHtml(insertMenu);
  460. // Fix for item size in kennedy theme
  461. if (EditorUi.compactUi)
  462. {
  463. insertMenu.getElementsByTagName('img')[0].style.left = '24px';
  464. insertMenu.getElementsByTagName('img')[0].style.top = '5px';
  465. insertMenu.style.width = '30px';
  466. }
  467. this.addSeparator();
  468. // KNOWN: All table stuff does not work with undo/redo
  469. // KNOWN: Lost focus after click on submenu with text (not icon) in quirks and IE8. This is because the TD seems
  470. // to catch the focus on click in these browsers. NOTE: Workaround in mxPopupMenu for icon items (without text).
  471. var elt = this.addMenuFunction('geIcon geSprite geSprite-table', mxResources.get('table'), false, mxUtils.bind(this, function(menu)
  472. {
  473. var elt = graph.getSelectedElement();
  474. var cell = graph.getParentByNames(elt, ['TD', 'TH'], graph.cellEditor.text2);
  475. var row = graph.getParentByName(elt, 'TR', graph.cellEditor.text2);
  476. if (row == null)
  477. {
  478. function createTable(rows, cols)
  479. {
  480. var html = ['<table>'];
  481. for (var i = 0; i < rows; i++)
  482. {
  483. html.push('<tr>');
  484. for (var j = 0; j < cols; j++)
  485. {
  486. html.push('<td><br></td>');
  487. }
  488. html.push('</tr>');
  489. }
  490. html.push('</table>');
  491. return html.join('');
  492. };
  493. this.editorUi.menus.addInsertTableItem(menu);
  494. }
  495. else
  496. {
  497. var table = graph.getParentByName(row, 'TABLE', graph.cellEditor.text2);
  498. elt = menu.addItem('', null, mxUtils.bind(this, function()
  499. {
  500. try
  501. {
  502. graph.selectNode(graph.insertColumn(table, (cell != null) ? cell.cellIndex : 0));
  503. }
  504. catch (e)
  505. {
  506. this.editorUi.handleError(e);
  507. }
  508. }), null, 'geIcon geSprite geSprite-insertcolumnbefore');
  509. elt.setAttribute('title', mxResources.get('insertColumnBefore'));
  510. elt = menu.addItem('', null, mxUtils.bind(this, function()
  511. {
  512. try
  513. {
  514. graph.selectNode(graph.insertColumn(table, (cell != null) ? cell.cellIndex + 1 : -1));
  515. }
  516. catch (e)
  517. {
  518. this.editorUi.handleError(e);
  519. }
  520. }), null, 'geIcon geSprite geSprite-insertcolumnafter');
  521. elt.setAttribute('title', mxResources.get('insertColumnAfter'));
  522. elt = menu.addItem('Delete column', null, mxUtils.bind(this, function()
  523. {
  524. if (cell != null)
  525. {
  526. try
  527. {
  528. graph.deleteColumn(table, cell.cellIndex);
  529. }
  530. catch (e)
  531. {
  532. this.editorUi.handleError(e);
  533. }
  534. }
  535. }), null, 'geIcon geSprite geSprite-deletecolumn');
  536. elt.setAttribute('title', mxResources.get('deleteColumn'));
  537. elt = menu.addItem('', null, mxUtils.bind(this, function()
  538. {
  539. try
  540. {
  541. graph.selectNode(graph.insertRow(table, row.sectionRowIndex));
  542. }
  543. catch (e)
  544. {
  545. this.editorUi.handleError(e);
  546. }
  547. }), null, 'geIcon geSprite geSprite-insertrowbefore');
  548. elt.setAttribute('title', mxResources.get('insertRowBefore'));
  549. elt = menu.addItem('', null, mxUtils.bind(this, function()
  550. {
  551. try
  552. {
  553. graph.selectNode(graph.insertRow(table, row.sectionRowIndex + 1));
  554. }
  555. catch (e)
  556. {
  557. this.editorUi.handleError(e);
  558. }
  559. }), null, 'geIcon geSprite geSprite-insertrowafter');
  560. elt.setAttribute('title', mxResources.get('insertRowAfter'));
  561. elt = menu.addItem('', null, mxUtils.bind(this, function()
  562. {
  563. try
  564. {
  565. graph.deleteRow(table, row.sectionRowIndex);
  566. }
  567. catch (e)
  568. {
  569. this.editorUi.handleError(e);
  570. }
  571. }), null, 'geIcon geSprite geSprite-deleterow');
  572. elt.setAttribute('title', mxResources.get('deleteRow'));
  573. elt = menu.addItem('', null, mxUtils.bind(this, function()
  574. {
  575. // Converts rgb(r,g,b) values
  576. var color = table.style.borderColor.replace(
  577. /\brgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/g,
  578. function($0, $1, $2, $3) {
  579. return "#" + ("0"+Number($1).toString(16)).substr(-2) + ("0"+Number($2).toString(16)).substr(-2) + ("0"+Number($3).toString(16)).substr(-2);
  580. });
  581. this.editorUi.pickColor(color, function(newColor)
  582. {
  583. if (newColor == null || newColor == mxConstants.NONE)
  584. {
  585. table.removeAttribute('border');
  586. table.style.border = '';
  587. table.style.borderCollapse = '';
  588. }
  589. else
  590. {
  591. table.setAttribute('border', '1');
  592. table.style.border = '1px solid ' + newColor;
  593. table.style.borderCollapse = 'collapse';
  594. }
  595. });
  596. }), null, 'geIcon geSprite geSprite-strokecolor');
  597. elt.setAttribute('title', mxResources.get('borderColor'));
  598. elt = menu.addItem('', null, mxUtils.bind(this, function()
  599. {
  600. // Converts rgb(r,g,b) values
  601. var color = table.style.backgroundColor.replace(
  602. /\brgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/g,
  603. function($0, $1, $2, $3) {
  604. return "#" + ("0"+Number($1).toString(16)).substr(-2) + ("0"+Number($2).toString(16)).substr(-2) + ("0"+Number($3).toString(16)).substr(-2);
  605. });
  606. this.editorUi.pickColor(color, function(newColor)
  607. {
  608. if (newColor == null || newColor == mxConstants.NONE)
  609. {
  610. table.style.backgroundColor = '';
  611. }
  612. else
  613. {
  614. table.style.backgroundColor = newColor;
  615. }
  616. });
  617. }), null, 'geIcon geSprite geSprite-fillcolor');
  618. elt.setAttribute('title', mxResources.get('backgroundColor'));
  619. elt = menu.addItem('', null, mxUtils.bind(this, function()
  620. {
  621. var value = table.getAttribute('cellPadding') || 0;
  622. var dlg = new FilenameDialog(this.editorUi, value, mxResources.get('apply'), mxUtils.bind(this, function(newValue)
  623. {
  624. if (newValue != null && newValue.length > 0)
  625. {
  626. table.setAttribute('cellPadding', newValue);
  627. }
  628. else
  629. {
  630. table.removeAttribute('cellPadding');
  631. }
  632. }), mxResources.get('spacing'));
  633. this.editorUi.showDialog(dlg.container, 300, 80, true, true);
  634. dlg.init();
  635. }), null, 'geIcon geSprite geSprite-fit');
  636. elt.setAttribute('title', mxResources.get('spacing'));
  637. elt = menu.addItem('', null, mxUtils.bind(this, function()
  638. {
  639. table.setAttribute('align', 'left');
  640. }), null, 'geIcon geSprite geSprite-left');
  641. elt.setAttribute('title', mxResources.get('left'));
  642. elt = menu.addItem('', null, mxUtils.bind(this, function()
  643. {
  644. table.setAttribute('align', 'center');
  645. }), null, 'geIcon geSprite geSprite-center');
  646. elt.setAttribute('title', mxResources.get('center'));
  647. elt = menu.addItem('', null, mxUtils.bind(this, function()
  648. {
  649. table.setAttribute('align', 'right');
  650. }), null, 'geIcon geSprite geSprite-right');
  651. elt.setAttribute('title', mxResources.get('right'));
  652. }
  653. }));
  654. elt.style.position = 'relative';
  655. elt.style.whiteSpace = 'nowrap';
  656. elt.style.overflow = 'hidden';
  657. elt.style.width = '30px';
  658. elt.innerText = '';
  659. var div = document.createElement('div');
  660. div.className = 'geSprite geSprite-table';
  661. div.style.marginLeft = '-2px';
  662. elt.appendChild(div);
  663. this.appendDropDownImageHtml(elt);
  664. // Fix for item size in kennedy theme
  665. if (EditorUi.compactUi)
  666. {
  667. elt.getElementsByTagName('img')[0].style.left = '22px';
  668. elt.getElementsByTagName('img')[0].style.top = '5px';
  669. }
  670. };
  671. /**
  672. * Hides the current menu.
  673. */
  674. Toolbar.prototype.hideMenu = function()
  675. {
  676. this.editorUi.hideCurrentMenu();
  677. };
  678. /**
  679. * Adds a label to the toolbar.
  680. */
  681. Toolbar.prototype.addMenu = function(label, tooltip, showLabels, name, c, showAll, ignoreState)
  682. {
  683. var menu = this.editorUi.menus.get(name);
  684. var elt = this.addMenuFunction(label, tooltip, showLabels, function()
  685. {
  686. menu.funct.apply(menu, arguments);
  687. }, c, showAll);
  688. // Workaround for possible not a function
  689. // when extending HTML objects
  690. if (!ignoreState && typeof elt.setEnabled === 'function')
  691. {
  692. menu.addListener('stateChanged', function()
  693. {
  694. elt.setEnabled(menu.enabled);
  695. });
  696. }
  697. return elt;
  698. };
  699. /**
  700. * Adds a label to the toolbar.
  701. */
  702. Toolbar.prototype.addMenuFunction = function(label, tooltip, showLabels, funct, c, showAll)
  703. {
  704. return this.addMenuFunctionInContainer((c != null) ? c : this.container, label, tooltip, showLabels, funct, showAll);
  705. };
  706. /**
  707. * Adds a label to the toolbar.
  708. */
  709. Toolbar.prototype.addMenuFunctionInContainer = function(container, label, tooltip, showLabels, funct, showAll)
  710. {
  711. var elt = (showLabels) ? this.createLabel(label) : this.createButton(label);
  712. this.initElement(elt, tooltip);
  713. this.addMenuHandler(elt, showLabels, funct, showAll);
  714. container.appendChild(elt);
  715. return elt;
  716. };
  717. /**
  718. * Adds a separator to the separator.
  719. */
  720. Toolbar.prototype.addSeparator = function(c)
  721. {
  722. c = (c != null) ? c : this.container;
  723. var elt = document.createElement('div');
  724. elt.className = 'geSeparator';
  725. c.appendChild(elt);
  726. return elt;
  727. };
  728. /**
  729. * Adds given action item
  730. */
  731. Toolbar.prototype.addItems = function(keys, c, ignoreDisabled)
  732. {
  733. var items = [];
  734. for (var i = 0; i < keys.length; i++)
  735. {
  736. var key = keys[i];
  737. if (key == '-')
  738. {
  739. items.push(this.addSeparator(c));
  740. }
  741. else
  742. {
  743. items.push(this.addItem('geSprite-' + key.toLowerCase(), key, c, ignoreDisabled));
  744. }
  745. }
  746. return items;
  747. };
  748. /**
  749. * Adds given action item
  750. */
  751. Toolbar.prototype.addItem = function(sprite, key, c, ignoreDisabled)
  752. {
  753. var action = this.editorUi.actions.get(key);
  754. var elt = null;
  755. if (action != null)
  756. {
  757. var tooltip = action.label;
  758. if (action.shortcut != null)
  759. {
  760. tooltip += ' (' + action.shortcut + ')';
  761. }
  762. elt = this.addButton(sprite, tooltip, action.funct, c);
  763. // Workaround for possible not a function
  764. // when extending HTML objects
  765. if (!ignoreDisabled && typeof elt.setEnabled === 'function')
  766. {
  767. elt.setEnabled(action.enabled);
  768. action.addListener('stateChanged', function()
  769. {
  770. elt.setEnabled(action.enabled);
  771. });
  772. }
  773. }
  774. return elt;
  775. };
  776. /**
  777. * Adds a button to the toolbar.
  778. */
  779. Toolbar.prototype.addButton = function(classname, tooltip, funct, c)
  780. {
  781. var elt = this.createButton(classname);
  782. c = (c != null) ? c : this.container;
  783. this.initElement(elt, tooltip);
  784. this.addClickHandler(elt, funct);
  785. c.appendChild(elt);
  786. return elt;
  787. };
  788. /**
  789. * Initializes the given toolbar element.
  790. */
  791. Toolbar.prototype.initElement = function(elt, tooltip)
  792. {
  793. // Adds tooltip
  794. if (tooltip != null)
  795. {
  796. elt.setAttribute('title', tooltip);
  797. }
  798. this.addEnabledState(elt);
  799. };
  800. /**
  801. * Adds enabled state with setter to DOM node (avoids JS wrapper).
  802. */
  803. Toolbar.prototype.addEnabledState = function(elt)
  804. {
  805. var classname = elt.className;
  806. elt.setEnabled = function(value)
  807. {
  808. elt.enabled = value;
  809. if (value)
  810. {
  811. elt.className = classname;
  812. }
  813. else
  814. {
  815. elt.className = classname + ' mxDisabled';
  816. }
  817. };
  818. elt.setEnabled(true);
  819. };
  820. /**
  821. * Adds enabled state with setter to DOM node (avoids JS wrapper).
  822. */
  823. Toolbar.prototype.addClickHandler = function(elt, funct)
  824. {
  825. if (funct != null)
  826. {
  827. mxEvent.addListener(elt, 'click', function(evt)
  828. {
  829. if (elt.enabled)
  830. {
  831. funct(evt);
  832. }
  833. mxEvent.consume(evt);
  834. });
  835. // Prevents focus
  836. mxEvent.addListener(elt, (mxClient.IS_POINTER) ? 'pointerdown' : 'mousedown',
  837. mxUtils.bind(this, function(evt)
  838. {
  839. evt.preventDefault();
  840. }));
  841. }
  842. };
  843. /**
  844. * Creates and returns a new button.
  845. */
  846. Toolbar.prototype.createButton = function(classname)
  847. {
  848. var elt = document.createElement('a');
  849. elt.className = 'geButton';
  850. var inner = document.createElement('div');
  851. if (classname != null)
  852. {
  853. inner.className = 'geSprite ' + classname;
  854. }
  855. elt.appendChild(inner);
  856. return elt;
  857. };
  858. /**
  859. * Creates and returns a new button.
  860. */
  861. Toolbar.prototype.createLabel = function(label, tooltip)
  862. {
  863. var elt = document.createElement('a');
  864. elt.className = 'geLabel';
  865. mxUtils.write(elt, label);
  866. return elt;
  867. };
  868. /**
  869. * Adds a handler for showing a menu in the given element.
  870. */
  871. Toolbar.prototype.addMenuHandler = function(elt, showLabels, funct, showAll)
  872. {
  873. if (funct != null)
  874. {
  875. var graph = this.editorUi.editor.graph;
  876. var menu = null;
  877. var show = true;
  878. mxEvent.addListener(elt, 'click', mxUtils.bind(this, function(evt)
  879. {
  880. if (show && (elt.enabled == null || elt.enabled))
  881. {
  882. graph.popupMenuHandler.hideMenu();
  883. menu = new mxPopupMenu(funct);
  884. menu.smartSeparators = true;
  885. menu.div.className += ' geToolbarMenu';
  886. menu.showDisabled = showAll;
  887. menu.labels = showLabels;
  888. menu.autoExpand = true;
  889. // Workaround for scrollbar hiding menu items
  890. if (!showLabels && menu.div.scrollHeight > menu.div.clientHeight)
  891. {
  892. menu.div.style.width = '40px';
  893. }
  894. menu.hideMenu = mxUtils.bind(this, function()
  895. {
  896. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  897. this.editorUi.resetCurrentMenu();
  898. menu.destroy();
  899. });
  900. var offset = mxUtils.getOffset(elt);
  901. menu.popup(offset.x, offset.y + elt.offsetHeight, null, evt);
  902. this.editorUi.setCurrentMenu(menu, elt);
  903. }
  904. show = true;
  905. mxEvent.consume(evt);
  906. }));
  907. // Hides menu if already showing and prevents focus
  908. mxEvent.addListener(elt, (mxClient.IS_POINTER) ? 'pointerdown' : 'mousedown',
  909. mxUtils.bind(this, function(evt)
  910. {
  911. show = menu == null || menu.div == null ||
  912. menu.div.parentNode == null;
  913. evt.preventDefault();
  914. }));
  915. }
  916. };
  917. /**
  918. * Adds a handler for showing a menu in the given element.
  919. */
  920. Toolbar.prototype.destroy = function()
  921. {
  922. if (this.gestureHandler != null)
  923. {
  924. mxEvent.removeGestureListeners(document, this.gestureHandler);
  925. this.gestureHandler = null;
  926. }
  927. };