GraphViewer.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /**
  2. * Copyright (c) 2006-2016, JGraph Ltd
  3. */
  4. /**
  5. * No CSS and resources available in embed mode. Parameters and docs:
  6. * https://support.draw.io/pages/viewpage.action?pageId=12878123
  7. */
  8. GraphViewer = function(container, xmlNode, graphConfig)
  9. {
  10. this.graphConfig = (graphConfig != null) ? graphConfig : {};
  11. this.autoFit = (graphConfig['auto-fit'] != null) ?
  12. graphConfig['auto-fit'] : this.autoFit;
  13. this.checkVisibleState = (graphConfig['check-visible-state'] != null) ?
  14. graphConfig['check-visible-state'] : this.checkVisibleState;
  15. this.toolbarItems = (this.graphConfig.toolbar != null) ?
  16. this.graphConfig.toolbar.split(' ') : [];
  17. this.zoomEnabled = mxUtils.indexOf(this.toolbarItems, 'zoom') >= 0;
  18. this.layersEnabled = mxUtils.indexOf(this.toolbarItems, 'layers') >= 0;
  19. this.lightboxEnabled = mxUtils.indexOf(this.toolbarItems, 'lightbox') >= 0;
  20. this.lightboxClickEnabled = this.graphConfig.lightbox != false;
  21. this.widthIsEmpty = (container != null) ? container.style.width == '' : true;
  22. this.editor = null;
  23. if (xmlNode != null)
  24. {
  25. this.xmlDocument = xmlNode.ownerDocument;
  26. this.xmlNode = xmlNode;
  27. this.xml = mxUtils.getXml(xmlNode);
  28. if (container != null)
  29. {
  30. this.graph = new Graph(container);
  31. this.graph.transparentBackground = false;
  32. if (this.graphConfig.move)
  33. {
  34. this.graph.isMoveCellsEvent = function(evt)
  35. {
  36. return true;
  37. };
  38. }
  39. // Adds lightbox and link handling for shapes
  40. if (this.lightboxClickEnabled)
  41. {
  42. container.style.cursor = 'pointer';
  43. }
  44. // Hack for using EditorUi methods on the graph instance
  45. this.editor = new Editor(true, null, null, this.graph);
  46. this.editor.editBlankUrl = this.editBlankUrl;
  47. this.editor.editBlankFallbackUrl = this.editBlankFallbackUrl;
  48. this.graph.lightbox = true;
  49. this.graph.centerZoom = false;
  50. this.graph.autoExtend = false;
  51. this.graph.autoScroll = false;
  52. this.graph.setEnabled(false);
  53. // Handles relative images
  54. var self = this;
  55. this.graph.getImageFromBundles = function(key)
  56. {
  57. return self.getImageUrl(key);
  58. };
  59. if (mxClient.IS_SVG)
  60. {
  61. // LATER: Add shadow for labels in graph.container (eg. math, NO_FO), scaling
  62. this.editor.addSvgShadow(this.graph.view.canvas.ownerSVGElement, null, true);
  63. }
  64. // Adds page placeholders
  65. var pagenumber = this.graphConfig.page || 0;
  66. if (xmlNode.nodeName == 'mxfile')
  67. {
  68. var diagrams = xmlNode.getElementsByTagName('diagram');
  69. if (diagrams.length > 0)
  70. {
  71. var diagram = diagrams[pagenumber];
  72. var graphGetGlobalVariable = this.graph.getGlobalVariable;
  73. this.graph.getGlobalVariable = function(name)
  74. {
  75. if (name == 'page')
  76. {
  77. return diagram.getAttribute('name') || 'Page-' + (pagenumber + 1);
  78. }
  79. else if (name == 'pagenumber')
  80. {
  81. return pagenumber + 1;
  82. }
  83. return graphGetGlobalVariable.apply(this, arguments);
  84. };
  85. }
  86. }
  87. // Passes current page via urlParams global variable
  88. // to let the parser know which page we're using
  89. urlParams['page'] = pagenumber;
  90. var render = mxUtils.bind(this, function()
  91. {
  92. this.graph.getModel().beginUpdate();
  93. try
  94. {
  95. // Required for correct parsing of fold parameter
  96. urlParams['nav'] = (this.graphConfig.nav != false) ? '1' : '0';
  97. this.editor.setGraphXml(this.xmlNode);
  98. this.graph.border = this.graphConfig.border || 8;
  99. this.graph.view.scale = this.graphConfig.zoom || 1;
  100. }
  101. finally
  102. {
  103. this.graph.getModel().endUpdate();
  104. }
  105. // Adds left-button panning only if scrollbars are visible
  106. this.graph.panningHandler.useLeftButtonForPanning = true;
  107. this.graph.panningHandler.isForcePanningEvent = function(me)
  108. {
  109. return !mxEvent.isPopupTrigger(me.getEvent()) &&
  110. this.graph.container.style.overflow == 'auto';
  111. };
  112. this.graph.panningHandler.usePopupTrigger = false;
  113. this.graph.panningHandler.pinchEnabled = false;
  114. this.graph.panningHandler.ignoreCell = true;
  115. this.graph.setPanning(false);
  116. this.addSizeHandler();
  117. this.showLayers(this.graph);
  118. this.addClickHandler(this.graph);
  119. this.graph.setTooltips(this.graphConfig.tooltips != false);
  120. this.graph.initialViewState = {
  121. translate: this.graph.view.translate.clone(),
  122. scale: this.graph.view.scale
  123. };
  124. if (this.graphConfig.toolbar != null)
  125. {
  126. this.addToolbar();
  127. }
  128. else if (this.graphConfig.title != null)
  129. {
  130. container.setAttribute('title', this.graphConfig.title);
  131. }
  132. });
  133. if (this.checkVisibleState && container.offsetWidth == 0)
  134. {
  135. // Finds first parent with valid offset width or uses the document body
  136. // LATER: In some cases a closer ancestor may result in better performance
  137. // KNOWN: Possible side-effect of setting relative position on body
  138. var resizeSensor = new ResizeSensor(this.getVisibleParent(container), function()
  139. {
  140. if (container.offsetWidth > 0)
  141. {
  142. resizeSensor.detach();
  143. render();
  144. }
  145. });
  146. }
  147. else
  148. {
  149. render();
  150. }
  151. }
  152. }
  153. };
  154. /**
  155. * Redirects editing to absolue URLs.
  156. */
  157. GraphViewer.prototype.editBlankUrl = 'https://www.draw.io/?client=1';
  158. /**
  159. * Redirects editing to absolue URLs.
  160. */
  161. GraphViewer.prototype.editBlankFallbackUrl = 'https://www.draw.io/?create=drawdata&splash=0';
  162. /**
  163. * Base URL for relative images.
  164. */
  165. GraphViewer.prototype.imageBaseUrl = 'https://www.draw.io/';
  166. /**
  167. * Redirects editing to absolue URLs.
  168. */
  169. GraphViewer.prototype.toolbarHeight = (document.compatMode == 'BackCompat') ? 28 : 30;
  170. /**
  171. * Redirects editing to absolue URLs.
  172. */
  173. GraphViewer.prototype.lightboxChrome = true;
  174. /**
  175. * Redirects editing to absolue URLs.
  176. */
  177. GraphViewer.prototype.lightboxZIndex = 999;
  178. /**
  179. * Redirects editing to absolue URLs.
  180. */
  181. GraphViewer.prototype.toolbarZIndex = 999;
  182. /**
  183. * Base URL for relative images.
  184. */
  185. GraphViewer.prototype.imageBaseUrl = 'https://www.draw.io/';
  186. /**
  187. * If automatic fit should be enabled if zoom is disabled. Default is true.
  188. */
  189. GraphViewer.prototype.autoFit = true;
  190. /**
  191. * Specifies if the constructur should delay the rendering if the container
  192. * is not visible by default.
  193. */
  194. GraphViewer.prototype.checkVisibleState = true;
  195. /**
  196. *
  197. */
  198. GraphViewer.prototype.getVisibleParent = function(container)
  199. {
  200. var node = container;
  201. while (node != document.body && node.parentNode != null && node.offsetWidth == 0)
  202. {
  203. node = node.parentNode;
  204. }
  205. return node;
  206. };
  207. /**
  208. *
  209. */
  210. GraphViewer.prototype.getImageUrl = function(url)
  211. {
  212. if (url != null && url.substring(0, 7) != 'http://' &&
  213. url.substring(0, 8) != 'https://' && url.substring(0, 10) != 'data:image')
  214. {
  215. if (url.charAt(0) == '/')
  216. {
  217. url = url.substring(1, url.length);
  218. }
  219. url = this.imageBaseUrl + url;
  220. }
  221. return url;
  222. };
  223. /**
  224. *
  225. */
  226. GraphViewer.prototype.updateGraphXml = function(xmlNode)
  227. {
  228. this.xmlDocument = xmlNode.ownerDocument;
  229. this.xml = mxUtils.getXml(xmlNode);
  230. this.xmlNode = xmlNode;
  231. if (this.graph != null)
  232. {
  233. this.graph.view.translate = new mxPoint();
  234. this.graph.view.scale = 1;
  235. this.graph.getModel().clear();
  236. // Restores initial CSS state
  237. if (this.widthIsEmpty)
  238. {
  239. this.graph.container.style.width = '';
  240. this.graph.container.style.height = '';
  241. }
  242. this.editor.setGraphXml(this.xmlNode);
  243. this.positionGraph();
  244. this.graph.initialViewState = {
  245. translate: this.graph.view.translate.clone(),
  246. scale: this.graph.view.scale
  247. };
  248. }
  249. };
  250. /**
  251. *
  252. */
  253. GraphViewer.prototype.addSizeHandler = function()
  254. {
  255. var container = this.graph.container;
  256. var bounds = this.graph.getGraphBounds();
  257. var updatingOverflow = false;
  258. container.style.overflow = 'hidden';
  259. var updateOverflow = mxUtils.bind(this, function()
  260. {
  261. if (!updatingOverflow)
  262. {
  263. updatingOverflow = true;
  264. var tmp = this.graph.getGraphBounds();
  265. if (container.offsetWidth < tmp.width + this.graph.border)
  266. {
  267. container.style.overflow = 'auto';
  268. }
  269. else
  270. {
  271. container.style.overflow = 'hidden';
  272. }
  273. if (this.toolbar != null)
  274. {
  275. var r = container.getBoundingClientRect();
  276. // Workaround for position:relative set in ResizeSensor
  277. var origin = mxUtils.getScrollOrigin(document.body)
  278. var b = (document.body.style.position === 'relative') ? document.body.getBoundingClientRect() :
  279. {left: -origin.x, top: -origin.y};
  280. r = {left: r.left - b.left, top: r.top - b.top, bottom: r.bottom - b.top, right: r.right - b.left};
  281. this.toolbar.style.left = r.left + 'px';
  282. if (this.graphConfig['toolbar-position'] == 'bottom')
  283. {
  284. this.toolbar.style.top = r.bottom - 1 + 'px';
  285. }
  286. else
  287. {
  288. if (this.graphConfig['toolbar-position'] != 'inline')
  289. {
  290. this.toolbar.style.width = Math.max(this.minToolbarWidth, container.offsetWidth) + 'px';
  291. this.toolbar.style.top = r.top + 1 + 'px';
  292. }
  293. else
  294. {
  295. this.toolbar.style.top = r.top + 'px';
  296. }
  297. }
  298. }
  299. updatingOverflow = false;
  300. }
  301. });
  302. var lastOffsetWidth = null;
  303. var cachedOffsetWidth = null;
  304. var handlingResize = false;
  305. // Installs function on instance
  306. this.fitGraph = function(maxScale)
  307. {
  308. var cachedOffsetWidth = container.offsetWidth;
  309. if (cachedOffsetWidth != lastOffsetWidth)
  310. {
  311. if (!handlingResize)
  312. {
  313. handlingResize = true;
  314. this.graph.maxFitScale = (maxScale != null) ? maxScale : (this.graphConfig.zoom || 1);
  315. this.graph.fit(null, null, null, null, false, true);
  316. this.graph.maxFitScale = null;
  317. var tmp = this.graph.getGraphBounds();
  318. this.updateContainerHeight(container, tmp.height + 2 * this.graph.border + 1);
  319. this.graph.initialViewState = {
  320. translate: this.graph.view.translate.clone(),
  321. scale: this.graph.view.scale
  322. };
  323. lastOffsetWidth = cachedOffsetWidth;
  324. // Workaround for fit triggering scrollbars triggering doResize (infinite loop)
  325. window.setTimeout(function()
  326. {
  327. handlingResize = false;
  328. }, 0);
  329. }
  330. }
  331. };
  332. // Fallback for older browsers
  333. if (mxClient.IS_QUIRKS || document.documentMode <= 9)
  334. {
  335. mxEvent.addListener(window, 'resize', updateOverflow);
  336. this.graph.addListener('size', updateOverflow);
  337. }
  338. else
  339. {
  340. new ResizeSensor(this.graph.container, updateOverflow);
  341. }
  342. if (this.graphConfig.resize || ((this.zoomEnabled || !this.autoFit) && this.graphConfig.resize != false))
  343. {
  344. this.graph.minimumContainerSize = new mxRectangle(0, 0, 100, this.toolbarHeight);
  345. this.graph.resizeContainer = true;
  346. }
  347. else
  348. {
  349. // Sets initial size for responsive diagram to stop at actual size
  350. if (this.widthIsEmpty)
  351. {
  352. container.style.width = bounds.width + 2 * this.graph.border + 'px';
  353. }
  354. this.updateContainerHeight(container, bounds.height + 2 * this.graph.border + 1);
  355. if (!this.zoomEnabled && this.autoFit)
  356. {
  357. var lastOffsetWidth = null;
  358. var scheduledResize = null;
  359. var cachedOffsetWidth = null;
  360. var doResize = mxUtils.bind(this, function()
  361. {
  362. window.clearTimeout(scheduledResize);
  363. if (!handlingResize)
  364. {
  365. scheduledResize = window.setTimeout(mxUtils.bind(this, this.fitGraph), 100);
  366. }
  367. });
  368. // Fallback for older browsers
  369. if (mxClient.IS_QUIRKS || document.documentMode <= 9)
  370. {
  371. mxEvent.addListener(window, 'resize', doResize);
  372. }
  373. else
  374. {
  375. new ResizeSensor(this.graph.container, doResize);
  376. }
  377. }
  378. else if (!(mxClient.IS_QUIRKS || document.documentMode <= 9))
  379. {
  380. this.graph.addListener('size', updateOverflow);
  381. }
  382. }
  383. var positionGraph = mxUtils.bind(this, function()
  384. {
  385. // Allocates maximum width while setting initial view state
  386. var prev = container.style.minWidth;
  387. if (this.widthIsEmpty)
  388. {
  389. container.style.minWidth = '100%';
  390. }
  391. if (bounds.width + 2 * this.graph.border > container.offsetWidth ||
  392. bounds.height + 2 * this.graph.border > this.graphConfig['max-height'])
  393. {
  394. var maxScale = null;
  395. if (this.graphConfig['max-height'] != null)
  396. {
  397. maxScale = this.graphConfig['max-height'] / (bounds.height + 2 * this.graph.border);
  398. }
  399. this.fitGraph(maxScale);
  400. }
  401. else
  402. {
  403. this.graph.view.setTranslate(Math.floor((this.graph.border - bounds.x) / this.graph.view.scale),
  404. Math.floor((this.graph.border - bounds.y) / this.graph.view.scale));
  405. lastOffsetWidth = container.offsetWidth;
  406. }
  407. container.style.minWidth = prev
  408. });
  409. if (mxClient.IS_QUIRKS || document.documentMode == 8)
  410. {
  411. window.setTimeout(positionGraph, 0);
  412. }
  413. else
  414. {
  415. positionGraph();
  416. }
  417. // Installs function on instance
  418. this.positionGraph = function()
  419. {
  420. bounds = this.graph.getGraphBounds();
  421. lastOffsetWidth = null;
  422. positionGraph();
  423. };
  424. };
  425. /**
  426. *
  427. */
  428. GraphViewer.prototype.updateContainerHeight = function(container, height)
  429. {
  430. if (this.zoomEnabled || !this.autoFit || document.compatMode == 'BackCompat' ||
  431. mxClient.IS_QUIRKS || document.documentMode == 8)
  432. {
  433. container.style.height = height + 'px';
  434. }
  435. };
  436. /**
  437. * Shows the
  438. */
  439. GraphViewer.prototype.showLayers = function(graph, sourceGraph)
  440. {
  441. var layers = this.graphConfig.layers;
  442. if (layers != null || sourceGraph != null)
  443. {
  444. var idx = (layers != null) ? layers.split(' ') : null;
  445. if (sourceGraph != null || idx.length > 0)
  446. {
  447. var source = (sourceGraph != null) ? sourceGraph.getModel() : null;
  448. var model = graph.getModel();
  449. model.beginUpdate();
  450. try
  451. {
  452. var childCount = model.getChildCount(model.root);
  453. // Hides all layers
  454. for (var i = 0; i < childCount; i++)
  455. {
  456. model.setVisible(model.getChildAt(model.root, i),
  457. (sourceGraph != null) ? source.isVisible(source.getChildAt(source.root, i)) : false);
  458. }
  459. // Shows specified layers (eg. 0 1 3)
  460. if (source == null)
  461. {
  462. for (var i = 0; i < idx.length; i++)
  463. {
  464. model.setVisible(model.getChildAt(model.root, parseInt(idx[i])), true);
  465. }
  466. }
  467. }
  468. finally
  469. {
  470. model.endUpdate();
  471. }
  472. }
  473. }
  474. };
  475. /**
  476. *
  477. */
  478. GraphViewer.prototype.addToolbar = function()
  479. {
  480. var container = this.graph.container;
  481. var initialCursor = this.graph.container.style.cursor;
  482. if (this.graphConfig['toolbar-position'] == 'bottom')
  483. {
  484. container.style.marginBottom = this.toolbarHeight + 'px';
  485. }
  486. else if (this.graphConfig['toolbar-position'] != 'inline')
  487. {
  488. container.style.marginTop = this.toolbarHeight + 'px';
  489. }
  490. // Creates toolbar for viewer
  491. var toolbar = container.ownerDocument.createElement('div');
  492. toolbar.style.position = 'absolute';
  493. toolbar.style.overflow = 'hidden';
  494. toolbar.style.boxSizing = 'border-box';
  495. toolbar.style.whiteSpace = 'nowrap';
  496. toolbar.style.zIndex = this.toolbarZIndex;
  497. toolbar.style.backgroundColor = '#eee';
  498. toolbar.style.height = this.toolbarHeight + 'px';
  499. this.toolbar = toolbar;
  500. if (this.graphConfig['toolbar-position'] == 'inline')
  501. {
  502. mxUtils.setPrefixedStyle(toolbar.style, 'transition', 'opacity 100ms ease-in-out');
  503. mxUtils.setOpacity(toolbar, 30);
  504. // Changes toolbar opacity on hover
  505. var fadeThread = null;
  506. var fadeThread2 = null;
  507. var fadeOut = mxUtils.bind(this, function(delay)
  508. {
  509. if (fadeThread != null)
  510. {
  511. window.clearTimeout(fadeThread);
  512. fadeThead = null;
  513. }
  514. if (fadeThread2 != null)
  515. {
  516. window.clearTimeout(fadeThread2);
  517. fadeThead2 = null;
  518. }
  519. fadeThread = window.setTimeout(mxUtils.bind(this, function()
  520. {
  521. mxUtils.setOpacity(toolbar, 0);
  522. fadeThread = null;
  523. fadeThread2 = window.setTimeout(mxUtils.bind(this, function()
  524. {
  525. toolbar.style.display = 'none';
  526. fadeThread2 = null;
  527. }), 100);
  528. }), delay || 200);
  529. });
  530. var fadeIn = mxUtils.bind(this, function(opacity)
  531. {
  532. if (fadeThread != null)
  533. {
  534. window.clearTimeout(fadeThread);
  535. fadeThead = null;
  536. }
  537. if (fadeThread2 != null)
  538. {
  539. window.clearTimeout(fadeThread2);
  540. fadeThead2 = null;
  541. }
  542. toolbar.style.display = '';
  543. mxUtils.setOpacity(toolbar, opacity || 30);
  544. });
  545. mxEvent.addListener(this.graph.container, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', mxUtils.bind(this, function(evt)
  546. {
  547. if (!mxEvent.isTouchEvent(evt))
  548. {
  549. fadeIn(30);
  550. fadeOut();
  551. }
  552. }));
  553. mxEvent.addListener(toolbar, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', function(evt)
  554. {
  555. mxEvent.consume(evt);
  556. });
  557. mxEvent.addListener(toolbar, 'mouseenter', mxUtils.bind(this, function(evt)
  558. {
  559. fadeIn(100);
  560. }));
  561. mxEvent.addListener(toolbar, 'mousemove', mxUtils.bind(this, function(evt)
  562. {
  563. fadeIn(100);
  564. mxEvent.consume(evt);
  565. }));
  566. mxEvent.addListener(toolbar, 'mouseleave', mxUtils.bind(this, function(evt)
  567. {
  568. if (!mxEvent.isTouchEvent(evt))
  569. {
  570. fadeIn(30);
  571. }
  572. }));
  573. // Shows/hides toolbar for touch devices
  574. var graph = this.graph;
  575. var tol = graph.getTolerance();
  576. graph.addMouseListener(
  577. {
  578. startX: 0,
  579. startY: 0,
  580. scrollLeft: 0,
  581. scrollTop: 0,
  582. mouseDown: function(sender, me)
  583. {
  584. this.startX = me.getGraphX();
  585. this.startY = me.getGraphY();
  586. this.scrollLeft = graph.container.scrollLeft;
  587. this.scrollTop = graph.container.scrollTop;
  588. },
  589. mouseMove: function(sender, me) {},
  590. mouseUp: function(sender, me)
  591. {
  592. if (mxEvent.isTouchEvent(me.getEvent()))
  593. {
  594. if ((Math.abs(this.scrollLeft - graph.container.scrollLeft) < tol &&
  595. Math.abs(this.scrollTop - graph.container.scrollTop) < tol) &&
  596. (Math.abs(this.startX - me.getGraphX()) < tol &&
  597. Math.abs(this.startY - me.getGraphY()) < tol))
  598. {
  599. if (parseFloat(toolbar.style.opacity || 0) > 0)
  600. {
  601. fadeOut();
  602. }
  603. else
  604. {
  605. fadeIn(30);
  606. }
  607. }
  608. }
  609. }
  610. });
  611. }
  612. function addButton(fn, imgSrc, tip, enabled)
  613. {
  614. var a = document.createElement('div');
  615. a.style.borderRight = '1px solid #d0d0d0';
  616. a.style.padding = '3px 6px 3px 6px';
  617. mxEvent.addListener(a, 'click', fn);
  618. if (tip != null)
  619. {
  620. a.setAttribute('title', tip);
  621. }
  622. if (mxClient.IS_QUIRKS)
  623. {
  624. a.style.display = 'inline';
  625. }
  626. else
  627. {
  628. a.style.display = 'inline-block';
  629. }
  630. var img = document.createElement('img');
  631. img.setAttribute('border', '0');
  632. img.setAttribute('src', imgSrc);
  633. if (enabled == null || enabled)
  634. {
  635. mxEvent.addListener(a, 'mouseenter', function()
  636. {
  637. a.style.backgroundColor = '#ddd';
  638. });
  639. mxEvent.addListener(a, 'mouseleave', function()
  640. {
  641. a.style.backgroundColor = '#eee';
  642. });
  643. mxUtils.setOpacity(img, 60);
  644. a.style.cursor = 'pointer';
  645. }
  646. else
  647. {
  648. mxUtils.setOpacity(a, 30);
  649. }
  650. a.appendChild(img);
  651. toolbar.appendChild(a);
  652. buttonCount++;
  653. return a;
  654. };
  655. var buttonCount = 0;
  656. var tokens = this.toolbarItems;
  657. var layersDialog = null;
  658. var layersDialogEntered = false;
  659. for (var i = 0; i < tokens.length; i++)
  660. {
  661. var token = tokens[i];
  662. if (token == 'zoom')
  663. {
  664. if (this.zoomEnabled)
  665. {
  666. addButton(mxUtils.bind(this, function()
  667. {
  668. this.graph.zoomOut();
  669. }), Editor.zoomOutImage, mxResources.get('zoomOut') || 'Zoom Out');
  670. addButton(mxUtils.bind(this, function()
  671. {
  672. this.graph.zoomIn();
  673. }), Editor.zoomInImage, mxResources.get('zoomIn') || 'Zoom In');
  674. var a = addButton(mxUtils.bind(this, function()
  675. {
  676. this.graph.view.scaleAndTranslate(this.graph.initialViewState.scale,
  677. this.graph.initialViewState.translate.x,
  678. this.graph.initialViewState.translate.y);
  679. }), Editor.zoomFitImage, mxResources.get('fit') || 'Fit');
  680. }
  681. }
  682. else if (token == 'layers')
  683. {
  684. if (this.layersEnabled)
  685. {
  686. var model = this.graph.getModel();
  687. var childCount = model.getChildCount(model.root);
  688. if (childCount > 1)
  689. {
  690. var layersButton = addButton(mxUtils.bind(this, function(evt)
  691. {
  692. if (layersDialog != null)
  693. {
  694. layersDialog.parentNode.removeChild(layersDialog);
  695. layersDialog = null;
  696. }
  697. else
  698. {
  699. layersDialog = this.graph.createLayersDialog();
  700. mxEvent.addListener(layersDialog, 'mouseleave', function()
  701. {
  702. layersDialog.parentNode.removeChild(layersDialog);
  703. layersDialog = null;
  704. });
  705. var r = layersButton.getBoundingClientRect();
  706. layersDialog.style.width = '140px';
  707. layersDialog.style.padding = '2px 0px 2px 0px';
  708. layersDialog.style.border = '1px solid #d0d0d0';
  709. layersDialog.style.backgroundColor = '#eee';
  710. layersDialog.style.fontFamily = 'Helvetica Neue,Helvetica,Arial Unicode MS,Arial';
  711. layersDialog.style.fontSize = '11px';
  712. mxUtils.setOpacity(layersDialog, 80);
  713. var origin = mxUtils.getDocumentScrollOrigin(document);
  714. layersDialog.style.left = origin.x + r.left + 'px';
  715. layersDialog.style.top = origin.y + r.bottom + 'px';
  716. document.body.appendChild(layersDialog);
  717. }
  718. }), Editor.layersImage, mxResources.get('layers') || 'Layers');
  719. }
  720. }
  721. }
  722. else if (token == 'lightbox')
  723. {
  724. if (this.lightboxEnabled)
  725. {
  726. addButton(mxUtils.bind(this, function()
  727. {
  728. this.showLightbox();
  729. }), Editor.maximizeImage, mxResources.get('show') || 'Show');
  730. }
  731. }
  732. else if (this.graphConfig['toolbar-buttons'] != null)
  733. {
  734. var def = this.graphConfig['toolbar-buttons'][token];
  735. if (def != null)
  736. {
  737. addButton((def.enabled == null || def.enabled) ? def.handler : function() {},
  738. def.image, def.title, def.enabled);
  739. }
  740. }
  741. }
  742. if (this.graph.minimumContainerSize != null)
  743. {
  744. this.graph.minimumContainerSize.width = buttonCount * 34;
  745. }
  746. if (this.graphConfig.title != null)
  747. {
  748. var filename = container.ownerDocument.createElement('div');
  749. filename.style.cssText = 'display:inline-block;position:relative;padding:3px 6px 0 6px;' +
  750. 'vertical-align:top;font-family:Helvetica,Arial;font-size:12px;top:4px;cursor:default;'
  751. filename.setAttribute('title', this.graphConfig.title);
  752. mxUtils.write(filename, this.graphConfig.title);
  753. mxUtils.setOpacity(filename, 70);
  754. toolbar.appendChild(filename);
  755. }
  756. this.minToolbarWidth = buttonCount * 34;
  757. var prevBorder = container.style.border;
  758. var enter = mxUtils.bind(this, function()
  759. {
  760. var r = container.getBoundingClientRect();
  761. // Workaround for position:relative set in ResizeSensor
  762. var origin = mxUtils.getScrollOrigin(document.body)
  763. var b = (document.body.style.position === 'relative') ? document.body.getBoundingClientRect() :
  764. {left: -origin.x, top: -origin.y};
  765. r = {left: r.left - b.left, top: r.top - b.top, bottom: r.bottom - b.top, right: r.right - b.left};
  766. toolbar.style.left = r.left + 'px';
  767. toolbar.style.width = (this.graphConfig['toolbar-position'] == 'inline') ? 'auto' :
  768. Math.max(this.minToolbarWidth, container.offsetWidth) + 'px';
  769. toolbar.style.border = '1px solid #d0d0d0';
  770. if (this.graphConfig['toolbar-position'] == 'bottom')
  771. {
  772. toolbar.style.top = r.bottom - 1 + 'px';
  773. }
  774. else
  775. {
  776. if (this.graphConfig['toolbar-position'] != 'inline')
  777. {
  778. toolbar.style.marginTop = -this.toolbarHeight + 'px';
  779. toolbar.style.top = r.top + 1 + 'px';
  780. }
  781. else
  782. {
  783. toolbar.style.top = r.top + 'px';
  784. }
  785. }
  786. if (prevBorder == '1px solid transparent')
  787. {
  788. container.style.border = '1px solid #d0d0d0';
  789. }
  790. document.body.appendChild(toolbar);
  791. var hideToolbar = mxUtils.bind(this, function()
  792. {
  793. if (this.graphConfig['toolbar-nohide'] != true)
  794. {
  795. if (toolbar.parentNode != null)
  796. {
  797. toolbar.parentNode.removeChild(toolbar);
  798. }
  799. if (layersDialog != null)
  800. {
  801. layersDialog.parentNode.removeChild(layersDialog);
  802. layersDialog = null;
  803. }
  804. container.style.border = prevBorder;
  805. }
  806. });
  807. mxEvent.addListener(document, 'mousemove', function(evt)
  808. {
  809. var source = mxEvent.getSource(evt);
  810. while (source != null)
  811. {
  812. if (source == container || source == toolbar || source == layersDialog)
  813. {
  814. return;
  815. }
  816. source = source.parentNode;
  817. }
  818. hideToolbar();
  819. });
  820. mxEvent.addListener(document, 'mouseleave', function(evt)
  821. {
  822. hideToolbar();
  823. });
  824. });
  825. mxEvent.addListener(container, 'mouseenter', enter);
  826. };
  827. /**
  828. * Adds event handler for links and lightbox.
  829. */
  830. GraphViewer.prototype.addClickHandler = function(graph, ui)
  831. {
  832. var isBlankLink = graph.isBlankLink;
  833. var config = this.graphConfig;
  834. graph.isBlankLink = function(href)
  835. {
  836. return config.target != 'self' && isBlankLink.apply(this, arguments);
  837. };
  838. graph.addClickHandler(this.graphConfig.highlight, function(evt)
  839. {
  840. if (ui != null)
  841. {
  842. ui.destroy();
  843. }
  844. }, mxUtils.bind(this, function(evt)
  845. {
  846. if (ui == null && this.lightboxClickEnabled && (!mxEvent.isTouchEvent(evt) ||
  847. this.toolbarItems.length == 0))
  848. {
  849. this.showLightbox();
  850. }
  851. }));
  852. };
  853. /**
  854. * Adds the given array of stencils to avoid dynamic loading of shapes.
  855. */
  856. GraphViewer.prototype.showLightbox = function()
  857. {
  858. if (this.graphConfig.lightbox == 'open' || window.self !== window.top)
  859. {
  860. var p = (this.layersEnabled) ? '&layers=1' : '';
  861. if (typeof window.postMessage !== 'undefined' && (document.documentMode == null || document.documentMode >= 10))
  862. {
  863. var wnd = null;
  864. var receive = mxUtils.bind(this, function(evt)
  865. {
  866. if (evt.data == 'ready' && evt.source == wnd)
  867. {
  868. wnd.postMessage(this.xml, '*');
  869. mxEvent.removeListener(window, 'message', receive);
  870. }
  871. });
  872. mxEvent.addListener(window, 'message', receive);
  873. wnd = window.open('https://test.draw.io/?dev=1&client=1&chrome=0&lightbox=1&close=1&edit=_blank' + p);
  874. }
  875. else
  876. {
  877. // Data is pulled from global variable after tab loads
  878. window.drawdata = this.xml;
  879. window.open('https://test.draw.io/?dev=1&client=1&chrome=0&lightbox=1&edit=_blank' + p);
  880. }
  881. }
  882. else
  883. {
  884. this.showLocalLightbox();
  885. }
  886. };
  887. /**
  888. * Adds the given array of stencils to avoid dynamic loading of shapes.
  889. */
  890. GraphViewer.prototype.showLocalLightbox = function()
  891. {
  892. var origin = mxUtils.getDocumentScrollOrigin(document);
  893. var backdrop = document.createElement('div');
  894. var tabContainer = null;
  895. if (mxClient.IS_QUIRKS)
  896. {
  897. backdrop.style.position = 'absolute';
  898. backdrop.style.left = origin.x + 'px';
  899. backdrop.style.top = origin.y + 'px';
  900. backdrop.style.width = document.body.offsetWidth + 'px';
  901. backdrop.style.height = document.body.offsetHeight + 'px';
  902. }
  903. else
  904. {
  905. backdrop.style.cssText = 'position:fixed;top:0;left:0;bottom:0;right:0;';
  906. }
  907. backdrop.style.zIndex = this.lightboxZIndex;
  908. backdrop.style.backgroundColor = '#000000';
  909. mxUtils.setOpacity(backdrop, 70);
  910. document.body.appendChild(backdrop);
  911. var closeImg = document.createElement('img');
  912. closeImg.setAttribute('border', '0');
  913. closeImg.setAttribute('src', Editor.closeImage);
  914. if (mxClient.IS_QUIRKS)
  915. {
  916. closeImg.style.position = 'absolute';
  917. closeImg.style.right = 32 + 'px';
  918. closeImg.style.top = origin.y + 32 + 'px';
  919. }
  920. else
  921. {
  922. closeImg.style.cssText = 'position:fixed;top:32px;right:32px;';
  923. }
  924. closeImg.style.cursor = 'pointer';
  925. mxEvent.addListener(closeImg, 'click', function()
  926. {
  927. ui.destroy();
  928. });
  929. // LATER: Make possible to assign after instance was created
  930. urlParams['page'] = this.graphConfig.page || 0;
  931. urlParams['nav'] = (this.graphConfig.nav != false) ? '1' : '0';
  932. urlParams['layers'] = (this.layersEnabled) ? '1' : '0';
  933. // PostMessage not working and Permission denied for opened access in IE9-
  934. if (document.documentMode == null || document.documentMode >= 10)
  935. {
  936. Editor.prototype.editButtonLink = this.graphConfig.edit;
  937. }
  938. EditorUi.prototype.updateActionStates = function() {};
  939. EditorUi.prototype.addBeforeUnloadListener = function() {};
  940. EditorUi.prototype.addChromelessClickHandler = function() {};
  941. var ui = new EditorUi(new Editor(true), document.createElement('div'), true);
  942. // Disables refresh
  943. ui.refresh = function() {};
  944. var param = (this.graphConfig.page != null) ? '&page=' + this.graphConfig.page : '';
  945. ui.editor.editBlankUrl = this.editBlankUrl + param;
  946. ui.editor.editBlankFallbackUrl = this.editBlankFallbackUrl + param;
  947. mxEvent.addListener(backdrop, 'click', function()
  948. {
  949. ui.destroy();
  950. });
  951. // Handles escape keystroke
  952. var keydownHandler = mxUtils.bind(this, function(evt)
  953. {
  954. if (evt.keyCode == 27 /* Escape */)
  955. {
  956. ui.destroy();
  957. }
  958. });
  959. var destroy = ui.destroy;
  960. ui.destroy = function()
  961. {
  962. mxEvent.removeListener(document.documentElement, 'keydown', keydownHandler);
  963. document.body.removeChild(backdrop);
  964. document.body.removeChild(closeImg);
  965. if (tabContainer != null)
  966. {
  967. document.body.removeChild(tabContainer);
  968. }
  969. document.body.style.overflow = 'auto';
  970. GraphViewer.resizeSensorEnabled = true;
  971. destroy.apply(this, arguments);
  972. };
  973. var graph = ui.editor.graph;
  974. var lightbox = graph.container;
  975. lightbox.style.overflow = 'hidden';
  976. if (this.lightboxChrome)
  977. {
  978. lightbox.style.border = '1px solid #c0c0c0';
  979. lightbox.style.margin = '40px';
  980. // Installs the keystroke listener in the target
  981. mxEvent.addListener(document.documentElement, 'keydown', keydownHandler);
  982. }
  983. else
  984. {
  985. backdrop.style.display = 'none';
  986. closeImg.style.display = 'none';
  987. }
  988. // Handles relative images
  989. var self = this;
  990. graph.getImageFromBundles = function(key)
  991. {
  992. return self.getImageUrl(key);
  993. };
  994. if (this.graphConfig.move)
  995. {
  996. graph.isMoveCellsEvent = function(evt)
  997. {
  998. return true;
  999. };
  1000. }
  1001. if (!mxClient.IS_QUIRKS)
  1002. {
  1003. mxUtils.setPrefixedStyle(lightbox.style, 'border-radius', '4px');
  1004. lightbox.style.position = 'fixed';
  1005. }
  1006. GraphViewer.resizeSensorEnabled = false;
  1007. document.body.style.overflow = 'hidden';
  1008. // Workaround for possible rendering issues in Safari
  1009. if (!mxClient.IS_SF)
  1010. {
  1011. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(90deg)');
  1012. mxUtils.setPrefixedStyle(lightbox.style, 'transition', 'all .25s ease-in-out');
  1013. }
  1014. this.addClickHandler(graph, ui);
  1015. window.setTimeout(mxUtils.bind(this, function()
  1016. {
  1017. // Disables focus border in Chrome
  1018. lightbox.style.outline = 'none';
  1019. lightbox.style.zIndex = this.lightboxZIndex;
  1020. closeImg.style.zIndex = this.lightboxZIndex;
  1021. document.body.appendChild(lightbox);
  1022. document.body.appendChild(closeImg);
  1023. ui.setFileData(this.xml);
  1024. tabContainer = (ui.pages != null && ui.pages.length > 1) ? ui.tabContainer : null;
  1025. if (tabContainer != null)
  1026. {
  1027. if (mxClient.IS_QUIRKS)
  1028. {
  1029. tabContainer.style.position = 'absolute';
  1030. tabContainer.style.right = 32 + 'px';
  1031. tabContainer.style.top = origin.y + 32 + 'px';
  1032. }
  1033. else
  1034. {
  1035. // FIXME: Fix page menu CSS with sprite
  1036. // TODO: Update layers button after page change
  1037. tabContainer.style.fontFamily = 'Helvetica Neue,Helvetica,Arial Unicode MS,Arial';
  1038. tabContainer.style.position = 'fixed';
  1039. tabContainer.style.marginLeft = '40px';
  1040. tabContainer.style.marginRight = '40px';
  1041. tabContainer.style.bottom = '30px';
  1042. mxUtils.setPrefixedStyle(tabContainer.style, 'border-radius', '4px');
  1043. }
  1044. tabContainer.style.zIndex = this.lightboxZIndex;
  1045. document.body.appendChild(tabContainer);
  1046. ui.updateTabContainer();
  1047. }
  1048. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(0deg)');
  1049. ui.chromelessToolbar.style.bottom = ((tabContainer != null) ? 80 : 60) + 'px';
  1050. ui.chromelessToolbar.style.zIndex = this.lightboxZIndex;
  1051. // Workaround for clipping in IE11-
  1052. document.body.appendChild(ui.chromelessToolbar);
  1053. ui.getEditBlankXml = mxUtils.bind(this, function()
  1054. {
  1055. return this.xml;
  1056. });
  1057. if (mxClient.IS_QUIRKS)
  1058. {
  1059. lightbox.style.position = 'absolute';
  1060. lightbox.style.display = 'block';
  1061. lightbox.style.left = origin.x + 'px';
  1062. lightbox.style.top = origin.y + 'px';
  1063. lightbox.style.width = document.body.clientWidth - 80 + 'px';
  1064. lightbox.style.height = document.body.clientHeight -
  1065. ((tabContainer != null) ? 20 : 80) + 'px';
  1066. lightbox.style.backgroundColor = 'white';
  1067. ui.chromelessToolbar.style.display = 'block';
  1068. ui.chromelessToolbar.style.position = 'absolute';
  1069. ui.chromelessToolbar.style.bottom = '';
  1070. ui.chromelessToolbar.style.top = origin.y +
  1071. document.body.clientHeight - 100 + 'px';
  1072. }
  1073. else if (tabContainer != null)
  1074. {
  1075. lightbox.style.marginBottom = '60px';
  1076. }
  1077. ui.lightboxFit();
  1078. ui.chromelessResize();
  1079. this.showLayers(graph, this.graph);
  1080. }), 0);
  1081. return ui;
  1082. };
  1083. /**
  1084. *
  1085. */
  1086. GraphViewer.processElements = function(classname)
  1087. {
  1088. mxUtils.forEach(GraphViewer.getElementsByClassName(classname || 'mxgraph'), function(div)
  1089. {
  1090. try
  1091. {
  1092. div.innerHTML = '';
  1093. GraphViewer.createViewerForElement(div);
  1094. }
  1095. catch (e)
  1096. {
  1097. div.innerHTML = e.message;
  1098. throw e;
  1099. }
  1100. });
  1101. };
  1102. /**
  1103. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1104. */
  1105. GraphViewer.getElementsByClassName = function(classname)
  1106. {
  1107. if (document.getElementsByClassName)
  1108. {
  1109. var divs = document.getElementsByClassName(classname);
  1110. // Workaround for changing divs while processing
  1111. var result = [];
  1112. for (var i = 0; i < divs.length; i++)
  1113. {
  1114. result.push(divs[i]);
  1115. }
  1116. return result;
  1117. }
  1118. else
  1119. {
  1120. var tmp = document.getElementsByTagName('*');
  1121. var divs = [];
  1122. for (var i = 0; i < tmp.length; i++)
  1123. {
  1124. var cls = tmp[i].className;
  1125. if (cls != null && cls.length > 0)
  1126. {
  1127. var tokens = cls.split(' ');
  1128. if (mxUtils.indexOf(tokens, classname) >= 0)
  1129. {
  1130. divs.push(tmp[i]);
  1131. }
  1132. }
  1133. }
  1134. return divs;
  1135. }
  1136. };
  1137. /**
  1138. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1139. */
  1140. GraphViewer.createViewerForElement = function(element, callback)
  1141. {
  1142. var data = element.getAttribute('data-mxgraph');
  1143. if (data != null)
  1144. {
  1145. var config = JSON.parse(data);
  1146. var createViewer = function(xml)
  1147. {
  1148. var xmlDoc = mxUtils.parseXml(xml);
  1149. var viewer = new GraphViewer(element, xmlDoc.documentElement, config);
  1150. if (callback != null)
  1151. {
  1152. callback(viewer);
  1153. }
  1154. };
  1155. if (config.url != null)
  1156. {
  1157. GraphViewer.getUrl(config.url, function(xml)
  1158. {
  1159. createViewer(xml);
  1160. });
  1161. }
  1162. else
  1163. {
  1164. createViewer(config.xml);
  1165. }
  1166. }
  1167. };
  1168. /**
  1169. * Adds event if grid size is changed.
  1170. */
  1171. GraphViewer.initCss = function()
  1172. {
  1173. try
  1174. {
  1175. var style = document.createElement('style')
  1176. style.type = 'text/css'
  1177. style.innerHTML = ['div.mxTooltip {',
  1178. '-webkit-box-shadow: 3px 3px 12px #C0C0C0;',
  1179. '-moz-box-shadow: 3px 3px 12px #C0C0C0;',
  1180. 'box-shadow: 3px 3px 12px #C0C0C0;',
  1181. 'background: #FFFFCC;',
  1182. 'border-style: solid;',
  1183. 'border-width: 1px;',
  1184. 'border-color: black;',
  1185. 'font-family: Arial;',
  1186. 'font-size: 8pt;',
  1187. 'position: absolute;',
  1188. 'cursor: default;',
  1189. 'padding: 4px;',
  1190. 'color: black;}',
  1191. 'td.mxPopupMenuIcon div {',
  1192. 'width:16px;',
  1193. 'height:16px;}',
  1194. 'html div.mxPopupMenu {',
  1195. '-webkit-box-shadow:2px 2px 3px #d5d5d5;',
  1196. '-moz-box-shadow:2px 2px 3px #d5d5d5;',
  1197. 'box-shadow:2px 2px 3px #d5d5d5;',
  1198. '_filter:progid:DXImageTransform.Microsoft.DropShadow(OffX=2, OffY=2, Color=\'#d0d0d0\',Positive=\'true\');',
  1199. 'background:white;',
  1200. 'position:absolute;',
  1201. 'border:3px solid #e7e7e7;',
  1202. 'padding:3px;}',
  1203. 'html table.mxPopupMenu {',
  1204. 'border-collapse:collapse;',
  1205. 'margin:0px;}',
  1206. 'html td.mxPopupMenuItem {',
  1207. 'padding:7px 30px 7px 30px;',
  1208. 'font-family:Helvetica Neue,Helvetica,Arial Unicode MS,Arial;',
  1209. 'font-size:10pt;}',
  1210. 'html td.mxPopupMenuIcon {',
  1211. 'background-color:white;',
  1212. 'padding:0px;}',
  1213. 'td.mxPopupMenuIcon .geIcon {',
  1214. 'padding:2px;',
  1215. 'padding-bottom:4px;',
  1216. 'margin:2px;',
  1217. 'border:1px solid transparent;',
  1218. 'opacity:0.5;',
  1219. '_width:26px;',
  1220. '_height:26px;}',
  1221. 'td.mxPopupMenuIcon .geIcon:hover {',
  1222. 'border:1px solid gray;',
  1223. 'border-radius:2px;',
  1224. 'opacity:1;}',
  1225. 'html tr.mxPopupMenuItemHover {',
  1226. 'background-color: #eeeeee;',
  1227. 'color: black;}',
  1228. 'table.mxPopupMenu hr {',
  1229. 'color:#cccccc;',
  1230. 'background-color:#cccccc;',
  1231. 'border:none;',
  1232. 'height:1px;}',
  1233. 'table.mxPopupMenu tr { font-size:4pt;}'].join('\n');
  1234. document.getElementsByTagName('head')[0].appendChild(style)
  1235. }
  1236. catch (e)
  1237. {
  1238. // ignore
  1239. }
  1240. };
  1241. /**
  1242. * Workaround for unsupported CORS in IE9 XHR
  1243. */
  1244. GraphViewer.getUrl = function(url, onload, onerror)
  1245. {
  1246. var xhr = (navigator.userAgent.indexOf('MSIE 9') > 0) ? new XDomainRequest() : new XMLHttpRequest();
  1247. xhr.open('GET', url);
  1248. xhr.onload = function()
  1249. {
  1250. onload((xhr.getText != null) ? xhr.getText() : xhr.responseText);
  1251. };
  1252. xhr.onerror = onerror;
  1253. xhr.send();
  1254. };
  1255. /**
  1256. * Redirects editing to absolue URLs.
  1257. */
  1258. GraphViewer.resizeSensorEnabled = true;
  1259. /**
  1260. * Copyright Marc J. Schmidt. See the LICENSE file at the top-level
  1261. * directory of this distribution and at
  1262. * https://github.com/marcj/css-element-queries/blob/master/LICENSE.
  1263. */
  1264. (function() {
  1265. // Only used for the dirty checking, so the event callback count is limted to max 1 call per fps per sensor.
  1266. // In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and
  1267. // would generate too many unnecessary events.
  1268. var requestAnimationFrame = window.requestAnimationFrame ||
  1269. window.mozRequestAnimationFrame ||
  1270. window.webkitRequestAnimationFrame ||
  1271. function (fn) {
  1272. return window.setTimeout(fn, 20);
  1273. };
  1274. /**
  1275. * Class for dimension change detection.
  1276. *
  1277. * @param {Element|Element[]|Elements|jQuery} element
  1278. * @param {Function} callback
  1279. *
  1280. * @constructor
  1281. */
  1282. var ResizeSensor = function(element, fn) {
  1283. var callback = function()
  1284. {
  1285. if (GraphViewer.resizeSensorEnabled)
  1286. {
  1287. fn();
  1288. }
  1289. };
  1290. /**
  1291. *
  1292. * @constructor
  1293. */
  1294. function EventQueue() {
  1295. this.q = [];
  1296. this.add = function(ev) {
  1297. this.q.push(ev);
  1298. };
  1299. var i, j;
  1300. this.call = function() {
  1301. for (i = 0, j = this.q.length; i < j; i++) {
  1302. this.q[i].call();
  1303. }
  1304. };
  1305. }
  1306. /**
  1307. * @param {HTMLElement} element
  1308. * @param {String} prop
  1309. * @returns {String|Number}
  1310. */
  1311. function getComputedStyle(element, prop) {
  1312. if (element.currentStyle) {
  1313. return element.currentStyle[prop];
  1314. } else if (window.getComputedStyle) {
  1315. return window.getComputedStyle(element, null).getPropertyValue(prop);
  1316. } else {
  1317. return element.style[prop];
  1318. }
  1319. }
  1320. /**
  1321. *
  1322. * @param {HTMLElement} element
  1323. * @param {Function} resized
  1324. */
  1325. function attachResizeEvent(element, resized) {
  1326. if (!element.resizedAttached) {
  1327. element.resizedAttached = new EventQueue();
  1328. element.resizedAttached.add(resized);
  1329. } else if (element.resizedAttached) {
  1330. element.resizedAttached.add(resized);
  1331. return;
  1332. }
  1333. element.resizeSensor = document.createElement('div');
  1334. element.resizeSensor.className = 'resize-sensor';
  1335. var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
  1336. var styleChild = 'position: absolute; left: 0; top: 0; transition: 0s;';
  1337. element.resizeSensor.style.cssText = style;
  1338. element.resizeSensor.innerHTML =
  1339. '<div class="resize-sensor-expand" style="' + style + '">' +
  1340. '<div style="' + styleChild + '"></div>' +
  1341. '</div>' +
  1342. '<div class="resize-sensor-shrink" style="' + style + '">' +
  1343. '<div style="' + styleChild + ' width: 200%; height: 200%"></div>' +
  1344. '</div>';
  1345. element.appendChild(element.resizeSensor);
  1346. // FIXME: Should not change element style
  1347. if (getComputedStyle(element, 'position') == 'static') {
  1348. element.style.position = 'relative';
  1349. }
  1350. var expand = element.resizeSensor.childNodes[0];
  1351. var expandChild = expand.childNodes[0];
  1352. var shrink = element.resizeSensor.childNodes[1];
  1353. var reset = function() {
  1354. expandChild.style.width = 100000 + 'px';
  1355. expandChild.style.height = 100000 + 'px';
  1356. expand.scrollLeft = 100000;
  1357. expand.scrollTop = 100000;
  1358. shrink.scrollLeft = 100000;
  1359. shrink.scrollTop = 100000;
  1360. };
  1361. reset();
  1362. var dirty = false;
  1363. var dirtyChecking = function() {
  1364. if (!element.resizedAttached) return;
  1365. if (dirty) {
  1366. element.resizedAttached.call();
  1367. dirty = false;
  1368. }
  1369. requestAnimationFrame(dirtyChecking);
  1370. };
  1371. requestAnimationFrame(dirtyChecking);
  1372. var lastWidth, lastHeight;
  1373. var cachedWidth, cachedHeight; //useful to not query offsetWidth twice
  1374. var onScroll = function() {
  1375. if ((cachedWidth = element.offsetWidth) != lastWidth || (cachedHeight = element.offsetHeight) != lastHeight) {
  1376. dirty = true;
  1377. lastWidth = cachedWidth;
  1378. lastHeight = cachedHeight;
  1379. }
  1380. reset();
  1381. };
  1382. var addEvent = function(el, name, cb) {
  1383. if (el.attachEvent) {
  1384. el.attachEvent('on' + name, cb);
  1385. } else {
  1386. el.addEventListener(name, cb);
  1387. }
  1388. };
  1389. addEvent(expand, 'scroll', onScroll);
  1390. addEvent(shrink, 'scroll', onScroll);
  1391. }
  1392. var elementType = Object.prototype.toString.call(element);
  1393. var isCollectionTyped = ('[object Array]' === elementType
  1394. || ('[object NodeList]' === elementType)
  1395. || ('[object HTMLCollection]' === elementType)
  1396. || ('undefined' !== typeof jQuery && element instanceof jQuery) //jquery
  1397. || ('undefined' !== typeof Elements && element instanceof Elements) //mootools
  1398. );
  1399. if (isCollectionTyped) {
  1400. var i = 0, j = element.length;
  1401. for (; i < j; i++) {
  1402. attachResizeEvent(element[i], callback);
  1403. }
  1404. } else {
  1405. attachResizeEvent(element, callback);
  1406. }
  1407. this.detach = function() {
  1408. if (isCollectionTyped) {
  1409. var i = 0, j = element.length;
  1410. for (; i < j; i++) {
  1411. ResizeSensor.detach(element[i]);
  1412. }
  1413. } else {
  1414. ResizeSensor.detach(element);
  1415. }
  1416. };
  1417. };
  1418. ResizeSensor.detach = function(element) {
  1419. if (element.resizeSensor) {
  1420. element.removeChild(element.resizeSensor);
  1421. delete element.resizeSensor;
  1422. delete element.resizedAttached;
  1423. }
  1424. };
  1425. window.ResizeSensor = ResizeSensor;
  1426. })();