GraphViewer.js 49 KB

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