GraphViewer.js 46 KB

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