GraphViewer.js 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. /**
  2. * Copyright (c) 2006-2016, JGraph Ltd
  3. */
  4. /**
  5. * No CSS and resources available in embed mode. Parameters and docs:
  6. * https://www.diagrams.net/doc/faq/embed-html-options
  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://app.diagrams.net/';
  18. /**
  19. * Base URL for relative images.
  20. */
  21. GraphViewer.prototype.imageBaseUrl = 'https://viewer.diagrams.net/';
  22. /**
  23. * Redirects editing to absolue URLs.
  24. */
  25. GraphViewer.prototype.toolbarHeight = (document.compatMode == 'BackCompat') ? 24 : 26;
  26. /**
  27. * Redirects editing to absolue URLs.
  28. */
  29. GraphViewer.prototype.lightboxChrome = true;
  30. /**
  31. * Redirects editing to absolue URLs.
  32. */
  33. GraphViewer.prototype.lightboxZIndex = 999;
  34. /**
  35. * Redirects editing to absolue URLs.
  36. */
  37. GraphViewer.prototype.toolbarZIndex = 999;
  38. /**
  39. * If automatic fit should be enabled if zoom is disabled. Default is true.
  40. */
  41. GraphViewer.prototype.autoFit = false;
  42. /**
  43. * If automatic crop should be enabled when layers are toggled. Default is false.
  44. */
  45. GraphViewer.prototype.autoCrop = false;
  46. /**
  47. * Specifies if the graph should be moved if a layer is made visible that
  48. * extends the graph beyong the top left corner. Default is true. Is this is
  49. * false then the viewport of the viewer will include all cells in all layers
  50. * regardless of their initial visible state.
  51. */
  52. GraphViewer.prototype.autoOrigin = true;
  53. /**
  54. * If the diagram should be centered. Default is false.
  55. */
  56. GraphViewer.prototype.center = false;
  57. /**
  58. * Force centering of the diagram. Default is false.
  59. */
  60. GraphViewer.prototype.forceCenter = false;
  61. /**
  62. * Specifies if zooming in for auto fit is allowed. Default is false.
  63. */
  64. GraphViewer.prototype.allowZoomIn = false;
  65. /**
  66. * Specifies if zooming out for auto fit is allowed. Default is true.
  67. * If toolbar-nohide is true then overflow content is visible.
  68. */
  69. GraphViewer.prototype.allowZoomOut = true;
  70. /**
  71. * Whether the title should be shown as a tooltip if the toolbar is disabled.
  72. * Default is false.
  73. */
  74. GraphViewer.prototype.showTitleAsTooltip = false;
  75. /**
  76. * Specifies if the constructur should delay the rendering if the container
  77. * is not visible by default.
  78. */
  79. GraphViewer.prototype.checkVisibleState = true;
  80. /**
  81. * Defines the minimum height of the container. Default is 28.
  82. */
  83. GraphViewer.prototype.minHeight = 28;
  84. /**
  85. * Defines the minimum width of the container. Default is 100.
  86. */
  87. GraphViewer.prototype.minWidth = 100;
  88. /**
  89. * Implements viewBox to keep the contents inside the bounding box
  90. * of the container. This is currently not supported in Safari (due
  91. * to clipping in labels with viewBox) and all browsers that do not
  92. * support foreignObjects (eg. IE11).
  93. */
  94. GraphViewer.prototype.responsive = false;
  95. /**
  96. * Initializes the viewer.
  97. */
  98. GraphViewer.prototype.init = function(container, xmlNode, graphConfig)
  99. {
  100. this.graphConfig = (graphConfig != null) ? graphConfig : {};
  101. this.autoFit = (this.graphConfig['auto-fit'] != null) ?
  102. this.graphConfig['auto-fit'] : this.autoFit;
  103. this.autoCrop = (this.graphConfig['auto-crop'] != null) ?
  104. this.graphConfig['auto-crop'] : this.autoCrop;
  105. this.autoOrigin = (this.graphConfig['auto-origin'] != null) ?
  106. this.graphConfig['auto-origin'] : this.autoOrigin;
  107. this.allowZoomOut = (this.graphConfig['allow-zoom-out'] != null) ?
  108. this.graphConfig['allow-zoom-out'] : this.allowZoomOut;
  109. this.allowZoomIn = (this.graphConfig['allow-zoom-in'] != null) ?
  110. this.graphConfig['allow-zoom-in'] : this.allowZoomIn;
  111. this.forceCenter = (this.graphConfig['forceCenter'] != null) ?
  112. this.graphConfig['forceCenter'] : this.forceCenter;
  113. this.center = (this.graphConfig['center'] != null) ?
  114. this.graphConfig['center'] : (this.center || this.forceCenter);
  115. this.checkVisibleState = (this.graphConfig['check-visible-state'] != null) ?
  116. this.graphConfig['check-visible-state'] : this.checkVisibleState;
  117. this.toolbarItems = (this.graphConfig.toolbar != null) ?
  118. this.graphConfig.toolbar.split(' ') : [];
  119. this.zoomEnabled = mxUtils.indexOf(this.toolbarItems, 'zoom') >= 0;
  120. this.layersEnabled = mxUtils.indexOf(this.toolbarItems, 'layers') >= 0;
  121. this.tagsEnabled = mxUtils.indexOf(this.toolbarItems, 'tags') >= 0;
  122. this.lightboxEnabled = mxUtils.indexOf(this.toolbarItems, 'lightbox') >= 0;
  123. this.lightboxClickEnabled = this.graphConfig.lightbox != false;
  124. this.initialWidth = (container != null) ? container.style.width : null;
  125. this.widthIsEmpty = (this.initialWidth != null) ? this.initialWidth == '' : true;
  126. this.currentPage = parseInt(this.graphConfig.page) || 0;
  127. this.responsive = ((this.graphConfig['responsive'] != null) ?
  128. this.graphConfig['responsive'] : this.responsive) &&
  129. !this.zoomEnabled && !mxClient.NO_FO && !mxClient.IS_SF;
  130. this.pageId = this.graphConfig.pageId;
  131. this.editor = null;
  132. var self = this;
  133. if (this.graphConfig['toolbar-position'] == 'inline')
  134. {
  135. this.minHeight += this.toolbarHeight;
  136. }
  137. if (xmlNode != null)
  138. {
  139. this.xmlDocument = xmlNode.ownerDocument;
  140. this.xmlNode = xmlNode;
  141. this.xml = mxUtils.getXml(xmlNode);
  142. if (container != null)
  143. {
  144. var render = mxUtils.bind(this, function()
  145. {
  146. this.graph = new Graph(container);
  147. this.graph.enableFlowAnimation = true;
  148. this.graph.defaultPageBackgroundColor = 'transparent';
  149. this.graph.transparentBackground = false;
  150. if (this.responsive && this.graph.dialect == mxConstants.DIALECT_SVG)
  151. {
  152. var root = this.graph.view.getDrawPane().ownerSVGElement;
  153. var canvas = this.graph.view.getCanvas();
  154. if (this.graphConfig.border != null)
  155. {
  156. root.style.padding = this.graphConfig.border + 'px';
  157. }
  158. else if (container.style.padding == '')
  159. {
  160. root.style.padding = '8px';
  161. }
  162. root.style.boxSizing = 'border-box';
  163. root.style.overflow = 'visible';
  164. this.graph.fit = function()
  165. {
  166. // Automatic
  167. };
  168. this.graph.sizeDidChange = function()
  169. {
  170. var bounds = this.view.graphBounds;
  171. var tr = this.view.translate;
  172. root.setAttribute('viewBox',
  173. (bounds.x + tr.x - this.panDx) + ' ' +
  174. (bounds.y + tr.y - this.panDy) + ' ' +
  175. (bounds.width + 1) + ' ' +
  176. (bounds.height + 1));
  177. this.container.style.backgroundColor =
  178. root.style.backgroundColor;
  179. this.fireEvent(new mxEventObject(mxEvent.SIZE, 'bounds', bounds));
  180. };
  181. }
  182. if (this.graphConfig.move)
  183. {
  184. this.graph.isMoveCellsEvent = function(evt)
  185. {
  186. return true;
  187. };
  188. }
  189. // Adds lightbox and link handling for shapes
  190. if (this.lightboxClickEnabled)
  191. {
  192. container.style.cursor = 'pointer';
  193. }
  194. // Hack for using EditorUi methods on the graph instance
  195. this.editor = new Editor(true, null, null, this.graph);
  196. this.editor.editBlankUrl = this.editBlankUrl;
  197. this.graph.lightbox = true;
  198. this.graph.centerZoom = false;
  199. this.graph.autoExtend = false;
  200. this.graph.autoScroll = false;
  201. this.graph.setEnabled(false);
  202. if (this.graphConfig['toolbar-nohide'] == true)
  203. {
  204. this.editor.defaultGraphOverflow = 'visible';
  205. }
  206. //Extract graph model from html & svg formats
  207. this.xmlNode = this.editor.extractGraphModel(this.xmlNode, true);
  208. if (this.xmlNode != xmlNode)
  209. {
  210. this.xml = mxUtils.getXml(this.xmlNode);
  211. this.xmlDocument = this.xmlNode.ownerDocument;
  212. }
  213. // Handles relative images
  214. var self = this;
  215. this.graph.getImageFromBundles = function(key)
  216. {
  217. return self.getImageUrl(key);
  218. };
  219. if (mxClient.IS_SVG)
  220. {
  221. // LATER: Add shadow for labels in graph.container (eg. math, NO_FO), scaling
  222. this.graph.addSvgShadow(this.graph.view.canvas.ownerSVGElement, null, true);
  223. }
  224. // Adds page placeholders
  225. if (this.xmlNode.nodeName == 'mxfile')
  226. {
  227. var diagrams = this.xmlNode.getElementsByTagName('diagram');
  228. if (diagrams.length > 0)
  229. {
  230. //Find the page index if the pageId is provided
  231. if (this.pageId != null)
  232. {
  233. for (var i = 0; i < diagrams.length; i++)
  234. {
  235. if (this.pageId == diagrams[i].getAttribute('id'))
  236. {
  237. this.currentPage = i;
  238. break;
  239. }
  240. }
  241. }
  242. var graphGetGlobalVariable = this.graph.getGlobalVariable;
  243. var self = this;
  244. this.graph.getGlobalVariable = function(name)
  245. {
  246. var diagram = diagrams[self.currentPage];
  247. if (name == 'page')
  248. {
  249. return diagram.getAttribute('name') || 'Page-' + (self.currentPage + 1);
  250. }
  251. else if (name == 'pagenumber')
  252. {
  253. return self.currentPage + 1;
  254. }
  255. else if (name == 'pagecount')
  256. {
  257. return diagrams.length;
  258. }
  259. return graphGetGlobalVariable.apply(this, arguments);
  260. };
  261. }
  262. }
  263. this.diagrams = [];
  264. var lastXmlNode = null;
  265. this.selectPage = function(number)
  266. {
  267. if(this.handlingResize)
  268. return;
  269. this.currentPage = mxUtils.mod(number, this.diagrams.length);
  270. this.updateGraphXml(Editor.parseDiagramNode(this.diagrams[this.currentPage]));
  271. };
  272. this.selectPageById = function(id)
  273. {
  274. var index = this.getIndexById(id);
  275. var found = index >= 0;
  276. if (found)
  277. {
  278. this.selectPage(index);
  279. }
  280. return found;
  281. };
  282. var update = mxUtils.bind(this, function()
  283. {
  284. if (this.xmlNode == null || this.xmlNode.nodeName != 'mxfile')
  285. {
  286. this.diagrams = [];
  287. }
  288. if (this.xmlNode != lastXmlNode)
  289. {
  290. this.diagrams = this.xmlNode.getElementsByTagName('diagram');
  291. lastXmlNode = this.xmlNode;
  292. }
  293. });
  294. // Replaces background page reference with SVG
  295. var graphSetBackgroundImage = this.graph.setBackgroundImage;
  296. this.graph.setBackgroundImage = function(img)
  297. {
  298. if (img != null && Graph.isPageLink(img.src))
  299. {
  300. var src = img.src;
  301. var comma = src.indexOf(',');
  302. if (comma > 0)
  303. {
  304. var index = self.getIndexById(src.substring(comma + 1));
  305. if (index >= 0)
  306. {
  307. img = self.getImageForGraphModel(
  308. Editor.parseDiagramNode(
  309. self.diagrams[index]));
  310. img.originalSrc = src;
  311. }
  312. }
  313. }
  314. graphSetBackgroundImage.apply(this, arguments);
  315. };
  316. // Overrides graph bounds to include background pages
  317. var graphGetGraphBounds = this.graph.getGraphBounds;
  318. this.graph.getGraphBounds = function(img)
  319. {
  320. var bounds = graphGetGraphBounds.apply(this, arguments);
  321. var img = this.backgroundImage;
  322. // Check img.originalSrc to ignore background
  323. // images but not background pages
  324. if (img != null)
  325. {
  326. var t = this.view.translate;
  327. var s = this.view.scale;
  328. bounds.add(new mxRectangle(
  329. (t.x + img.x) * s, (t.y + img.y) * s,
  330. img.width * s, img.height * s));
  331. }
  332. return bounds;
  333. };
  334. // LATER: Add event for setGraphXml
  335. this.addListener('xmlNodeChanged', update);
  336. update();
  337. // Passes current page via urlParams global variable
  338. // to let the parser know which page we're using
  339. urlParams['page'] = self.currentPage;
  340. var visible = null;
  341. this.graph.getModel().beginUpdate();
  342. try
  343. {
  344. // Required for correct parsing of fold parameter
  345. urlParams['nav'] = (this.graphConfig.nav != false) ? '1' : '0';
  346. this.editor.setGraphXml(this.xmlNode);
  347. this.graph.view.scale = this.graphConfig.zoom || 1;
  348. visible = this.setLayersVisible();
  349. if (!this.responsive)
  350. {
  351. this.graph.border = (this.graphConfig.border != null) ? this.graphConfig.border : 8;
  352. }
  353. }
  354. finally
  355. {
  356. this.graph.getModel().endUpdate();
  357. }
  358. // Adds left-button panning only if scrollbars are visible
  359. if (!this.responsive)
  360. {
  361. this.graph.panningHandler.isForcePanningEvent = function(me)
  362. {
  363. return !mxEvent.isPopupTrigger(me.getEvent()) &&
  364. this.graph.container.style.overflow == 'auto';
  365. };
  366. this.graph.panningHandler.useLeftButtonForPanning = true;
  367. this.graph.panningHandler.ignoreCell = true;
  368. this.graph.panningHandler.usePopupTrigger = false;
  369. this.graph.panningHandler.pinchEnabled = false;
  370. }
  371. this.graph.setPanning(false);
  372. if (this.graphConfig.toolbar != null)
  373. {
  374. this.addToolbar();
  375. }
  376. else if (this.graphConfig.title != null && this.showTitleAsTooltip)
  377. {
  378. container.setAttribute('title', this.graphConfig.title);
  379. }
  380. if (!this.responsive)
  381. {
  382. this.addSizeHandler();
  383. }
  384. // Crops to visible layers if no layers toolbar button
  385. if (this.showLayers(this.graph) && !this.forceCenter && (!this.layersEnabled || this.autoCrop))
  386. {
  387. this.crop();
  388. }
  389. this.addClickHandler(this.graph);
  390. this.graph.setTooltips(this.graphConfig.tooltips != false);
  391. this.graph.initialViewState = {
  392. translate: this.graph.view.translate.clone(),
  393. scale: this.graph.view.scale
  394. };
  395. if (visible != null)
  396. {
  397. this.setLayersVisible(visible);
  398. }
  399. this.graph.customLinkClicked = function(href)
  400. {
  401. if (Graph.isPageLink(href))
  402. {
  403. var comma = href.indexOf(',');
  404. if (!self.selectPageById(href.substring(comma + 1)))
  405. {
  406. alert(mxResources.get('pageNotFound') || 'Page not found');
  407. }
  408. }
  409. else
  410. {
  411. this.handleCustomLink(href);
  412. }
  413. return true;
  414. };
  415. // Updates origin after tree cell folding
  416. var graphFoldTreeCell = this.graph.foldTreeCell;
  417. this.graph.foldTreeCell = mxUtils.bind(this, function()
  418. {
  419. this.treeCellFolded = true;
  420. return graphFoldTreeCell.apply(this.graph, arguments);
  421. });
  422. this.fireEvent(new mxEventObject('render'));
  423. });
  424. var MutObs = window.MutationObserver ||
  425. window.WebKitMutationObserver ||
  426. window.MozMutationObserver;
  427. if (this.checkVisibleState && container.offsetWidth == 0 && typeof MutObs !== 'undefined')
  428. {
  429. // Delayed rendering if inside hidden container and event available
  430. var par = this.getObservableParent(container);
  431. var observer = new MutObs(mxUtils.bind(this, function(mutation)
  432. {
  433. if (container.offsetWidth > 0)
  434. {
  435. observer.disconnect();
  436. render();
  437. }
  438. }));
  439. observer.observe(par, {attributes: true});
  440. }
  441. else
  442. {
  443. // Immediate rendering in all other cases
  444. render();
  445. }
  446. }
  447. }
  448. };
  449. /**
  450. *
  451. */
  452. GraphViewer.prototype.getObservableParent = function(container)
  453. {
  454. var node = container.parentNode;
  455. while (node != document.body && node.parentNode != null &&
  456. mxUtils.getCurrentStyle(node).display !== 'none')
  457. {
  458. node = node.parentNode;
  459. }
  460. return node;
  461. };
  462. /**
  463. *
  464. */
  465. GraphViewer.prototype.getImageUrl = function(url)
  466. {
  467. if (url != null && url.substring(0, 7) != 'http://' &&
  468. url.substring(0, 8) != 'https://' && url.substring(0, 10) != 'data:image')
  469. {
  470. if (url.charAt(0) == '/')
  471. {
  472. url = url.substring(1, url.length);
  473. }
  474. url = this.imageBaseUrl + url;
  475. }
  476. return url;
  477. };
  478. /**
  479. *
  480. */
  481. GraphViewer.prototype.getImageForGraphModel = function(node)
  482. {
  483. var graph = Graph.createOffscreenGraph(this.graph.getStylesheet());
  484. graph.getGlobalVariable = this.graph.getGlobalVariable;
  485. document.body.appendChild(graph.container);
  486. var codec = new mxCodec(node.ownerDocument);
  487. var root = codec.decode(node).root;
  488. graph.model.setRoot(root);
  489. var svgRoot = graph.getSvg();
  490. var bounds = graph.getGraphBounds();
  491. document.body.removeChild(graph.container);
  492. return new mxImage(Editor.createSvgDataUri(mxUtils.getXml(svgRoot)),
  493. bounds.width, bounds.height, bounds.x, bounds.y);
  494. };
  495. /**
  496. *
  497. */
  498. GraphViewer.prototype.getIndexById = function(id)
  499. {
  500. if (this.diagrams != null)
  501. {
  502. for (var i = 0; i < this.diagrams.length; i++)
  503. {
  504. if (this.diagrams[i].getAttribute('id') == id)
  505. {
  506. return i;
  507. }
  508. }
  509. }
  510. return -1;
  511. };
  512. /**
  513. *
  514. */
  515. GraphViewer.prototype.setXmlNode = function(xmlNode)
  516. {
  517. //Extract graph model from html & svg formats
  518. xmlNode = this.editor.extractGraphModel(xmlNode, true);
  519. this.xmlDocument = xmlNode.ownerDocument;
  520. this.xml = mxUtils.getXml(xmlNode);
  521. this.xmlNode = xmlNode;
  522. this.updateGraphXml(xmlNode);
  523. this.fireEvent(new mxEventObject('xmlNodeChanged'));
  524. };
  525. /**
  526. *
  527. */
  528. GraphViewer.prototype.setFileNode = function(xmlNode)
  529. {
  530. if (this.xmlNode == null)
  531. {
  532. this.xmlDocument = xmlNode.ownerDocument;
  533. this.xml = mxUtils.getXml(xmlNode);
  534. this.xmlNode = xmlNode;
  535. }
  536. this.setGraphXml(xmlNode);
  537. };
  538. /**
  539. *
  540. */
  541. GraphViewer.prototype.updateGraphXml = function(xmlNode)
  542. {
  543. this.setGraphXml(xmlNode);
  544. this.fireEvent(new mxEventObject('graphChanged'));
  545. };
  546. /**
  547. *
  548. */
  549. GraphViewer.prototype.setLayersVisible = function(visible)
  550. {
  551. var allVisible = true;
  552. if (!this.autoOrigin)
  553. {
  554. var result = [];
  555. var model = this.graph.getModel();
  556. model.beginUpdate();
  557. try
  558. {
  559. for (var i = 0; i < model.getChildCount(model.root); i++)
  560. {
  561. var layer = model.getChildAt(model.root, i);
  562. allVisible = allVisible && model.isVisible(layer);
  563. result.push(model.isVisible(layer));
  564. model.setVisible(layer, (visible != null) ? visible[i] : true);
  565. }
  566. }
  567. finally
  568. {
  569. model.endUpdate();
  570. }
  571. }
  572. return (allVisible) ? null : result;
  573. };
  574. /**
  575. *
  576. */
  577. GraphViewer.prototype.setGraphXml = function(xmlNode)
  578. {
  579. if (this.graph != null)
  580. {
  581. this.graph.view.translate = new mxPoint();
  582. this.graph.view.scale = 1;
  583. var visible = null;
  584. this.graph.getModel().beginUpdate();
  585. try
  586. {
  587. this.graph.getModel().clear();
  588. this.editor.setGraphXml(xmlNode);
  589. visible = this.setLayersVisible(true);
  590. }
  591. finally
  592. {
  593. this.graph.getModel().endUpdate();
  594. }
  595. if (!this.responsive)
  596. {
  597. // Restores initial CSS state
  598. if (this.widthIsEmpty)
  599. {
  600. this.graph.container.style.width = '';
  601. this.graph.container.style.height = '';
  602. }
  603. else
  604. {
  605. this.graph.container.style.width = this.initialWidth;
  606. }
  607. this.positionGraph();
  608. }
  609. this.graph.initialViewState = {
  610. translate: this.graph.view.translate.clone(),
  611. scale: this.graph.view.scale
  612. };
  613. if (visible)
  614. {
  615. this.setLayersVisible(visible);
  616. }
  617. }
  618. };
  619. /**
  620. *
  621. */
  622. GraphViewer.prototype.addSizeHandler = function()
  623. {
  624. var container = this.graph.container;
  625. var bounds = this.graph.getGraphBounds();
  626. var updatingOverflow = false;
  627. if (this.graphConfig['toolbar-nohide'] != true)
  628. {
  629. container.style.overflow = 'hidden';
  630. }
  631. else
  632. {
  633. container.style.overflow = 'visible';
  634. }
  635. var updateOverflow = mxUtils.bind(this, function()
  636. {
  637. if (!updatingOverflow)
  638. {
  639. updatingOverflow = true;
  640. var tmp = this.graph.getGraphBounds();
  641. if (this.graphConfig['toolbar-nohide'] != true)
  642. {
  643. // Shows scrollbars if graph is larger than available width
  644. if (tmp.width + 2 * this.graph.border > container.offsetWidth - 2)
  645. {
  646. container.style.overflow = 'auto';
  647. }
  648. else
  649. {
  650. container.style.overflow = 'hidden';
  651. }
  652. }
  653. else
  654. {
  655. container.style.overflow = 'visible';
  656. }
  657. if (this.toolbar != null && this.graphConfig['toolbar-nohide'] != true)
  658. {
  659. var r = container.getBoundingClientRect();
  660. // Workaround for position:relative set in ResizeSensor
  661. var origin = mxUtils.getScrollOrigin(document.body)
  662. var b = (document.body.style.position === 'relative') ?
  663. document.body.getBoundingClientRect() :
  664. {left: -origin.x, top: -origin.y};
  665. r = {left: r.left - b.left, top: r.top - b.top, bottom: r.bottom - b.top, right: r.right - b.left};
  666. this.toolbar.style.left = r.left + 'px';
  667. if (this.graphConfig['toolbar-position'] == 'bottom')
  668. {
  669. this.toolbar.style.top = r.bottom - 1 + 'px';
  670. }
  671. else
  672. {
  673. if (this.graphConfig['toolbar-position'] != 'inline')
  674. {
  675. this.toolbar.style.width = Math.max(this.minToolbarWidth, container.offsetWidth) + 'px';
  676. this.toolbar.style.top = r.top + 1 + 'px';
  677. }
  678. else
  679. {
  680. this.toolbar.style.top = r.top + 'px';
  681. }
  682. }
  683. }
  684. else if (this.toolbar != null)
  685. {
  686. this.toolbar.style.width = Math.max(this.minToolbarWidth, container.offsetWidth) + 'px';
  687. }
  688. // Updates origin after tree cell folding
  689. if (this.treeCellFolded)
  690. {
  691. this.treeCellFolded = false;
  692. this.positionGraph(this.graph.view.translate);
  693. this.graph.initialViewState.translate = this.graph.view.translate.clone();
  694. }
  695. updatingOverflow = false;
  696. }
  697. });
  698. var lastOffsetWidth = null;
  699. var cachedOffsetWidth = null;
  700. this.handlingResize = false;
  701. // Installs function on instance
  702. this.fitGraph = mxUtils.bind(this, function(maxScale)
  703. {
  704. var cachedOffsetWidth = container.offsetWidth;
  705. if (cachedOffsetWidth != lastOffsetWidth && !this.handlingResize)
  706. {
  707. this.handlingResize = true;
  708. // Hides scrollbars to force update of translate
  709. if (container.style.overflow == 'auto')
  710. {
  711. container.style.overflow = 'hidden';
  712. }
  713. this.graph.maxFitScale = (maxScale != null) ? maxScale : (this.graphConfig.zoom ||
  714. ((this.allowZoomIn) ? null : 1));
  715. this.graph.fit(null, null, null, null, null, true);
  716. if (this.center || !(this.graphConfig.resize != false || container.style.height == ''))
  717. {
  718. this.graph.center();
  719. }
  720. this.graph.maxFitScale = null;
  721. if (this.graphConfig.resize != false || container.style.height == '')
  722. {
  723. this.updateContainerHeight(container, Math.max(this.minHeight,
  724. this.graph.getGraphBounds().height +
  725. 2 * this.graph.border + 1));
  726. }
  727. this.graph.initialViewState = {
  728. translate: this.graph.view.translate.clone(),
  729. scale: this.graph.view.scale
  730. };
  731. lastOffsetWidth = cachedOffsetWidth;
  732. // Workaround for fit triggering scrollbars triggering doResize (infinite loop)
  733. window.setTimeout(mxUtils.bind(this, function()
  734. {
  735. this.handlingResize = false;
  736. }), 0);
  737. }
  738. });
  739. // Fallback for older browsers
  740. if (GraphViewer.useResizeSensor)
  741. {
  742. if (document.documentMode <= 9)
  743. {
  744. mxEvent.addListener(window, 'resize', updateOverflow);
  745. this.graph.addListener('size', updateOverflow);
  746. }
  747. else
  748. {
  749. new ResizeSensor(this.graph.container, updateOverflow);
  750. }
  751. }
  752. if (this.graphConfig.resize || ((this.zoomEnabled || !this.autoFit) && this.graphConfig.resize != false))
  753. {
  754. this.graph.minimumContainerSize = new mxRectangle(0, 0, this.minWidth, this.minHeight);
  755. this.graph.resizeContainer = true;
  756. }
  757. else
  758. {
  759. // Sets initial size for responsive diagram to stop at actual size
  760. if (this.widthIsEmpty && !(container.style.height != '' && this.autoFit))
  761. {
  762. this.updateContainerWidth(container, bounds.width + 2 * this.graph.border);
  763. }
  764. if (this.graphConfig.resize != false || container.style.height == '')
  765. {
  766. this.updateContainerHeight(container, Math.max(this.minHeight, bounds.height + 2 * this.graph.border + 1));
  767. }
  768. if (!this.zoomEnabled && this.autoFit)
  769. {
  770. var lastOffsetWidth = null;
  771. var scheduledResize = null;
  772. var cachedOffsetWidth = null;
  773. var doResize = mxUtils.bind(this, function()
  774. {
  775. window.clearTimeout(scheduledResize);
  776. if (!this.handlingResize)
  777. {
  778. scheduledResize = window.setTimeout(mxUtils.bind(this, this.fitGraph), 100);
  779. }
  780. });
  781. // Fallback for older browsers
  782. if (GraphViewer.useResizeSensor)
  783. {
  784. if (document.documentMode <= 9)
  785. {
  786. mxEvent.addListener(window, 'resize', doResize);
  787. }
  788. else
  789. {
  790. new ResizeSensor(this.graph.container, doResize);
  791. }
  792. }
  793. }
  794. else if (!(document.documentMode <= 9))
  795. {
  796. this.graph.addListener('size', updateOverflow);
  797. }
  798. }
  799. var positionGraph = mxUtils.bind(this, function(origin)
  800. {
  801. // Allocates maximum width while setting initial view state
  802. var prev = container.style.minWidth;
  803. if (this.widthIsEmpty)
  804. {
  805. container.style.minWidth = '100%';
  806. }
  807. var maxHeight = (this.graphConfig['max-height'] != null) ? this.graphConfig['max-height'] :
  808. ((container.style.height != '' && this.autoFit) ? container.offsetHeight : undefined);
  809. if (container.offsetWidth > 0 && origin == null && this.allowZoomOut && (this.allowZoomIn ||
  810. bounds.width + 2 * this.graph.border > container.offsetWidth ||
  811. bounds.height + 2 * this.graph.border > maxHeight))
  812. {
  813. var maxScale = null;
  814. if (maxHeight != null && bounds.height + 2 * this.graph.border > maxHeight - 2)
  815. {
  816. maxScale = (maxHeight - 2 * this.graph.border - 2) / bounds.height;
  817. }
  818. this.fitGraph(maxScale);
  819. }
  820. else if (!this.widthIsEmpty && origin == null && !(this.graphConfig.resize != false || container.style.height == ''))
  821. {
  822. this.graph.center((!this.widthIsEmpty || bounds.width < this.minWidth) && this.graphConfig.resize != true);
  823. }
  824. else
  825. {
  826. origin = (origin != null) ? origin : new mxPoint();
  827. this.graph.view.setTranslate(Math.floor(this.graph.border - bounds.x / this.graph.view.scale) + origin.x,
  828. Math.floor(this.graph.border - bounds.y / this.graph.view.scale) + origin.y);
  829. lastOffsetWidth = container.offsetWidth;
  830. }
  831. container.style.minWidth = prev
  832. });
  833. if (document.documentMode == 8)
  834. {
  835. window.setTimeout(positionGraph, 0);
  836. }
  837. else
  838. {
  839. positionGraph();
  840. }
  841. // Installs function on instance
  842. this.positionGraph = function(origin)
  843. {
  844. bounds = this.graph.getGraphBounds();
  845. lastOffsetWidth = null;
  846. positionGraph(origin);
  847. };
  848. };
  849. /**
  850. * Moves the origin of the graph to the top, right corner.
  851. */
  852. GraphViewer.prototype.crop = function()
  853. {
  854. var graph = this.graph;
  855. var bounds = graph.getGraphBounds();
  856. var border = graph.border;
  857. var s = graph.view.scale;
  858. var x0 = (bounds.x != null) ? Math.floor(graph.view.translate.x - bounds.x / s + border) : border;
  859. var y0 = (bounds.y != null) ? Math.floor(graph.view.translate.y - bounds.y / s + border) : border;
  860. graph.view.setTranslate(x0, y0);
  861. };
  862. /**
  863. *
  864. */
  865. GraphViewer.prototype.updateContainerWidth = function(container, width)
  866. {
  867. container.style.width = width + 'px';
  868. };
  869. /**
  870. *
  871. */
  872. GraphViewer.prototype.updateContainerHeight = function(container, height)
  873. {
  874. if (this.forceCenter || this.zoomEnabled || !this.autoFit || document.compatMode == 'BackCompat' ||
  875. document.documentMode == 8)
  876. {
  877. container.style.height = height + 'px';
  878. }
  879. };
  880. /**
  881. * Shows the
  882. */
  883. GraphViewer.prototype.showLayers = function(graph, sourceGraph)
  884. {
  885. var layers = this.graphConfig.layers;
  886. var idx = (layers != null && layers.length > 0) ? layers.split(' ') : [];
  887. var layerIds = this.graphConfig.layerIds;
  888. var hasLayerIds = layerIds != null && layerIds.length > 0;
  889. var result = false;
  890. if (idx.length > 0 || hasLayerIds || sourceGraph != null)
  891. {
  892. var source = (sourceGraph != null) ? sourceGraph.getModel() : null;
  893. var model = graph.getModel();
  894. model.beginUpdate();
  895. try
  896. {
  897. var childCount = model.getChildCount(model.root);
  898. // Shows specified layers (eg. 0 1 3)
  899. if (source == null)
  900. {
  901. var layersFound = false, visibleLayers = {};
  902. if (hasLayerIds)
  903. {
  904. for (var i = 0; i < layerIds.length; i++)
  905. {
  906. var layer = model.getCell(layerIds[i]);
  907. if (layer != null)
  908. {
  909. layersFound = true;
  910. visibleLayers[layer.id] = true;
  911. }
  912. }
  913. }
  914. else
  915. {
  916. for (var i = 0; i < idx.length; i++)
  917. {
  918. var layer = model.getChildAt(model.root, parseInt(idx[i]));
  919. if (layer != null)
  920. {
  921. layersFound = true;
  922. visibleLayers[layer.id] = true;
  923. }
  924. }
  925. }
  926. //To prevent hiding all layers, only apply if the specified layers are found
  927. //This prevents incorrect settings from showing an empty viewer
  928. for (var i = 0; layersFound && i < childCount; i++)
  929. {
  930. var layer = model.getChildAt(model.root, i);
  931. model.setVisible(layer, visibleLayers[layer.id] || false);
  932. }
  933. }
  934. else
  935. {
  936. // Match visible layers in source graph
  937. for (var i = 0; i < childCount; i++)
  938. {
  939. model.setVisible(model.getChildAt(model.root, i),
  940. source.isVisible(source.getChildAt(source.root, i)));
  941. }
  942. }
  943. }
  944. finally
  945. {
  946. model.endUpdate();
  947. }
  948. result = true;
  949. }
  950. return result;
  951. };
  952. /**
  953. *
  954. */
  955. GraphViewer.prototype.addToolbar = function()
  956. {
  957. var container = this.graph.container;
  958. var initialCursor = this.graph.container.style.cursor;
  959. if (this.graphConfig['toolbar-position'] == 'bottom')
  960. {
  961. container.style.marginBottom = this.toolbarHeight + 'px';
  962. }
  963. else if (this.graphConfig['toolbar-position'] != 'inline')
  964. {
  965. container.style.marginTop = this.toolbarHeight + 'px';
  966. }
  967. // Creates toolbar for viewer
  968. var toolbar = container.ownerDocument.createElement('div');
  969. toolbar.style.position = 'absolute';
  970. toolbar.style.overflow = 'hidden';
  971. toolbar.style.boxSizing = 'border-box';
  972. toolbar.style.whiteSpace = 'nowrap';
  973. toolbar.style.textAlign = 'left';
  974. toolbar.style.zIndex = this.toolbarZIndex;
  975. toolbar.style.backgroundColor = '#eee';
  976. toolbar.style.height = this.toolbarHeight + 'px';
  977. this.toolbar = toolbar;
  978. if (this.graphConfig['toolbar-position'] == 'inline')
  979. {
  980. mxUtils.setPrefixedStyle(toolbar.style, 'transition', 'opacity 100ms ease-in-out');
  981. mxUtils.setOpacity(toolbar, 30);
  982. // Changes toolbar opacity on hover
  983. var fadeThread = null;
  984. var fadeThread2 = null;
  985. var fadeOut = mxUtils.bind(this, function(delay)
  986. {
  987. if (fadeThread != null)
  988. {
  989. window.clearTimeout(fadeThread);
  990. fadeThead = null;
  991. }
  992. if (fadeThread2 != null)
  993. {
  994. window.clearTimeout(fadeThread2);
  995. fadeThead2 = null;
  996. }
  997. fadeThread = window.setTimeout(mxUtils.bind(this, function()
  998. {
  999. mxUtils.setOpacity(toolbar, 0);
  1000. fadeThread = null;
  1001. fadeThread2 = window.setTimeout(mxUtils.bind(this, function()
  1002. {
  1003. toolbar.style.display = 'none';
  1004. fadeThread2 = null;
  1005. }), 100);
  1006. }), delay || 200);
  1007. });
  1008. var fadeIn = mxUtils.bind(this, function(opacity)
  1009. {
  1010. if (fadeThread != null)
  1011. {
  1012. window.clearTimeout(fadeThread);
  1013. fadeThead = null;
  1014. }
  1015. if (fadeThread2 != null)
  1016. {
  1017. window.clearTimeout(fadeThread2);
  1018. fadeThead2 = null;
  1019. }
  1020. toolbar.style.display = '';
  1021. mxUtils.setOpacity(toolbar, opacity || 30);
  1022. });
  1023. mxEvent.addListener(this.graph.container, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', mxUtils.bind(this, function(evt)
  1024. {
  1025. if (!mxEvent.isTouchEvent(evt))
  1026. {
  1027. fadeIn(30);
  1028. fadeOut();
  1029. }
  1030. }));
  1031. mxEvent.addListener(toolbar, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', function(evt)
  1032. {
  1033. mxEvent.consume(evt);
  1034. });
  1035. mxEvent.addListener(toolbar, 'mouseenter', mxUtils.bind(this, function(evt)
  1036. {
  1037. fadeIn(100);
  1038. }));
  1039. mxEvent.addListener(toolbar, 'mousemove', mxUtils.bind(this, function(evt)
  1040. {
  1041. fadeIn(100);
  1042. mxEvent.consume(evt);
  1043. }));
  1044. mxEvent.addListener(toolbar, 'mouseleave', mxUtils.bind(this, function(evt)
  1045. {
  1046. if (!mxEvent.isTouchEvent(evt))
  1047. {
  1048. fadeIn(30);
  1049. }
  1050. }));
  1051. // Shows/hides toolbar for touch devices
  1052. var graph = this.graph;
  1053. var tol = graph.getTolerance();
  1054. graph.addMouseListener(
  1055. {
  1056. startX: 0,
  1057. startY: 0,
  1058. scrollLeft: 0,
  1059. scrollTop: 0,
  1060. mouseDown: function(sender, me)
  1061. {
  1062. this.startX = me.getGraphX();
  1063. this.startY = me.getGraphY();
  1064. this.scrollLeft = graph.container.scrollLeft;
  1065. this.scrollTop = graph.container.scrollTop;
  1066. },
  1067. mouseMove: function(sender, me) {},
  1068. mouseUp: function(sender, me)
  1069. {
  1070. if (mxEvent.isTouchEvent(me.getEvent()))
  1071. {
  1072. if ((Math.abs(this.scrollLeft - graph.container.scrollLeft) < tol &&
  1073. Math.abs(this.scrollTop - graph.container.scrollTop) < tol) &&
  1074. (Math.abs(this.startX - me.getGraphX()) < tol &&
  1075. Math.abs(this.startY - me.getGraphY()) < tol))
  1076. {
  1077. if (parseFloat(toolbar.style.opacity || 0) > 0)
  1078. {
  1079. fadeOut();
  1080. }
  1081. else
  1082. {
  1083. fadeIn(30);
  1084. }
  1085. }
  1086. }
  1087. }
  1088. });
  1089. }
  1090. var tokens = this.toolbarItems;
  1091. var buttonCount = 0;
  1092. function addButton(fn, imgSrc, tip, enabled)
  1093. {
  1094. var a = document.createElement('div');
  1095. a.style.borderRight = '1px solid #d0d0d0';
  1096. a.style.padding = '3px 6px 3px 6px';
  1097. mxEvent.addListener(a, 'click', fn);
  1098. if (tip != null)
  1099. {
  1100. a.setAttribute('title', tip);
  1101. }
  1102. a.style.display = 'inline-block';
  1103. var img = document.createElement('img');
  1104. img.setAttribute('border', '0');
  1105. img.setAttribute('src', imgSrc);
  1106. img.style.width = '18px';
  1107. if (enabled == null || enabled)
  1108. {
  1109. mxEvent.addListener(a, 'mouseenter', function()
  1110. {
  1111. a.style.backgroundColor = '#ddd';
  1112. });
  1113. mxEvent.addListener(a, 'mouseleave', function()
  1114. {
  1115. a.style.backgroundColor = '#eee';
  1116. });
  1117. mxUtils.setOpacity(img, 60);
  1118. a.style.cursor = 'pointer';
  1119. }
  1120. else
  1121. {
  1122. mxUtils.setOpacity(a, 30);
  1123. }
  1124. a.appendChild(img);
  1125. toolbar.appendChild(a);
  1126. buttonCount++;
  1127. return a;
  1128. };
  1129. var layersDialog = null;
  1130. var tagsComponent = null;
  1131. var tagsDialog = null;
  1132. var pageInfo = null;
  1133. for (var i = 0; i < tokens.length; i++)
  1134. {
  1135. var token = tokens[i];
  1136. if (token == 'pages')
  1137. {
  1138. pageInfo = container.ownerDocument.createElement('div');
  1139. pageInfo.style.cssText = 'display:inline-block;position:relative;top:5px;padding:0 4px 0 4px;' +
  1140. 'vertical-align:top;font-family:Helvetica,Arial;font-size:12px;;cursor:default;'
  1141. mxUtils.setOpacity(pageInfo, 70);
  1142. var prevButton = addButton(mxUtils.bind(this, function()
  1143. {
  1144. this.selectPage(this.currentPage - 1);
  1145. }), Editor.previousImage, mxResources.get('previousPage') || 'Previous Page');
  1146. prevButton.style.borderRightStyle = 'none';
  1147. prevButton.style.paddingLeft = '0px';
  1148. prevButton.style.paddingRight = '0px';
  1149. toolbar.appendChild(pageInfo);
  1150. var nextButton = addButton(mxUtils.bind(this, function()
  1151. {
  1152. this.selectPage(this.currentPage + 1);
  1153. }), Editor.nextImage, mxResources.get('nextPage') || 'Next Page');
  1154. nextButton.style.paddingLeft = '0px';
  1155. nextButton.style.paddingRight = '0px';
  1156. var lastXmlNode = null;
  1157. var update = mxUtils.bind(this, function()
  1158. {
  1159. pageInfo.innerHTML = '';
  1160. mxUtils.write(pageInfo, (this.currentPage + 1) + ' / ' + this.diagrams.length);
  1161. pageInfo.style.display = (this.diagrams.length > 1) ? 'inline-block' : 'none';
  1162. prevButton.style.display = pageInfo.style.display;
  1163. nextButton.style.display = pageInfo.style.display;
  1164. });
  1165. // LATER: Add event for setGraphXml
  1166. this.addListener('graphChanged', update);
  1167. update();
  1168. }
  1169. else if (token == 'zoom')
  1170. {
  1171. if (this.zoomEnabled)
  1172. {
  1173. addButton(mxUtils.bind(this, function()
  1174. {
  1175. this.graph.zoomOut();
  1176. }), Editor.zoomOutImage, mxResources.get('zoomOut') || 'Zoom Out');
  1177. addButton(mxUtils.bind(this, function()
  1178. {
  1179. this.graph.zoomIn();
  1180. }), Editor.zoomInImage, mxResources.get('zoomIn') || 'Zoom In');
  1181. addButton(mxUtils.bind(this, function()
  1182. {
  1183. this.graph.view.scaleAndTranslate(this.graph.initialViewState.scale,
  1184. this.graph.initialViewState.translate.x,
  1185. this.graph.initialViewState.translate.y);
  1186. }), Editor.zoomFitImage, mxResources.get('fit') || 'Fit');
  1187. }
  1188. }
  1189. else if (token == 'layers')
  1190. {
  1191. if (this.layersEnabled)
  1192. {
  1193. var model = this.graph.getModel();
  1194. var layersButton = addButton(mxUtils.bind(this, function(evt)
  1195. {
  1196. if (layersDialog != null)
  1197. {
  1198. layersDialog.parentNode.removeChild(layersDialog);
  1199. layersDialog = null;
  1200. }
  1201. else
  1202. {
  1203. layersDialog = this.graph.createLayersDialog(mxUtils.bind(this, function()
  1204. {
  1205. if (this.autoCrop)
  1206. {
  1207. this.crop();
  1208. }
  1209. else if (this.autoOrigin)
  1210. {
  1211. var bounds = this.graph.getGraphBounds();
  1212. var v = this.graph.view;
  1213. if (bounds.x < 0 || bounds.y < 0)
  1214. {
  1215. this.crop();
  1216. this.graph.originalViewState = this.graph.initialViewState;
  1217. this.graph.initialViewState = {
  1218. translate: v.translate.clone(),
  1219. scale: v.scale
  1220. };
  1221. }
  1222. else if (this.graph.originalViewState != null &&
  1223. bounds.x / v.scale + this.graph.originalViewState.translate.x - v.translate.x > 0 &&
  1224. bounds.y / v.scale + this.graph.originalViewState.translate.y - v.translate.y > 0)
  1225. {
  1226. v.setTranslate(this.graph.originalViewState.translate.x,
  1227. this.graph.originalViewState.translate.y);
  1228. this.graph.originalViewState = null;
  1229. this.graph.initialViewState = {
  1230. translate: v.translate.clone(),
  1231. scale: v.scale
  1232. };
  1233. }
  1234. }
  1235. }));
  1236. mxEvent.addListener(layersDialog, 'mouseleave', function()
  1237. {
  1238. layersDialog.parentNode.removeChild(layersDialog);
  1239. layersDialog = null;
  1240. });
  1241. var r = layersButton.getBoundingClientRect();
  1242. layersDialog.style.width = '140px';
  1243. layersDialog.style.padding = '2px 0px 2px 0px';
  1244. layersDialog.style.border = '1px solid #d0d0d0';
  1245. layersDialog.style.backgroundColor = '#eee';
  1246. layersDialog.style.fontFamily = Editor.defaultHtmlFont;
  1247. layersDialog.style.fontSize = '11px';
  1248. layersDialog.style.zIndex = this.toolbarZIndex + 1;
  1249. mxUtils.setOpacity(layersDialog, 80);
  1250. var origin = mxUtils.getDocumentScrollOrigin(document);
  1251. layersDialog.style.left = origin.x + r.left - 1 + 'px';
  1252. layersDialog.style.top = origin.y + r.bottom - 2 + 'px';
  1253. document.body.appendChild(layersDialog);
  1254. }
  1255. }), Editor.layersImage, mxResources.get('layers') || 'Layers');
  1256. model.addListener(mxEvent.CHANGE, function()
  1257. {
  1258. layersButton.style.display = (model.getChildCount(model.root) > 1) ? 'inline-block' : 'none';
  1259. });
  1260. layersButton.style.display = (model.getChildCount(model.root) > 1) ? 'inline-block' : 'none';
  1261. }
  1262. }
  1263. else if (token == 'tags')
  1264. {
  1265. if (this.tagsEnabled)
  1266. {
  1267. var tagsButton = addButton(mxUtils.bind(this, function(evt)
  1268. {
  1269. if (tagsComponent == null)
  1270. {
  1271. tagsComponent = this.graph.createTagsDialog(mxUtils.bind(this, function()
  1272. {
  1273. return true;
  1274. }));
  1275. tagsComponent.div.getElementsByTagName('div')[0].style.position = '';
  1276. tagsComponent.div.style.maxHeight = '160px';
  1277. tagsComponent.div.style.maxWidth = '120px';
  1278. tagsComponent.div.style.padding = '2px';
  1279. tagsComponent.div.style.overflow = 'auto';
  1280. tagsComponent.div.style.height = 'auto';
  1281. tagsComponent.div.style.position = 'fixed';
  1282. tagsComponent.div.style.fontFamily = Editor.defaultHtmlFont;
  1283. tagsComponent.div.style.fontSize = '11px';
  1284. tagsComponent.div.style.backgroundColor = '#eee';
  1285. tagsComponent.div.style.color = '#000';
  1286. tagsComponent.div.style.border = '1px solid #d0d0d0';
  1287. tagsComponent.div.style.zIndex = this.toolbarZIndex + 1;
  1288. mxUtils.setOpacity(tagsComponent.div, 80);
  1289. }
  1290. if (tagsDialog != null)
  1291. {
  1292. tagsDialog.parentNode.removeChild(tagsDialog);
  1293. tagsDialog = null;
  1294. }
  1295. else
  1296. {
  1297. tagsDialog = tagsComponent.div;
  1298. mxEvent.addListener(tagsDialog, 'mouseleave', function()
  1299. {
  1300. tagsDialog.parentNode.removeChild(tagsDialog);
  1301. tagsDialog = null;
  1302. });
  1303. var r = tagsButton.getBoundingClientRect();
  1304. var origin = mxUtils.getDocumentScrollOrigin(document);
  1305. tagsDialog.style.left = origin.x + r.left - 1 + 'px';
  1306. tagsDialog.style.top = origin.y + r.bottom - 2 + 'px';
  1307. document.body.appendChild(tagsDialog);
  1308. tagsComponent.refresh();
  1309. }
  1310. }), Editor.tagsImage, mxResources.get('tags') || 'Tags');
  1311. model.addListener(mxEvent.CHANGE, mxUtils.bind(this, function()
  1312. {
  1313. tagsButton.style.display = (this.graph.getAllTags().length > 0) ? 'inline-block' : 'none';
  1314. }));
  1315. tagsButton.style.display = (this.graph.getAllTags().length > 0) ? 'inline-block' : 'none';
  1316. }
  1317. }
  1318. else if (token == 'lightbox')
  1319. {
  1320. if (this.lightboxEnabled)
  1321. {
  1322. addButton(mxUtils.bind(this, function()
  1323. {
  1324. this.showLightbox();
  1325. }), Editor.fullscreenImage, (mxResources.get('fullscreen') || 'Fullscreen'));
  1326. }
  1327. }
  1328. else if (this.graphConfig['toolbar-buttons'] != null)
  1329. {
  1330. var def = this.graphConfig['toolbar-buttons'][token];
  1331. if (def != null)
  1332. {
  1333. def.elem = addButton((def.enabled == null || def.enabled) ? def.handler : function() {},
  1334. def.image, def.title, def.enabled);
  1335. }
  1336. }
  1337. }
  1338. if (this.graph.minimumContainerSize != null)
  1339. {
  1340. this.graph.minimumContainerSize.width = buttonCount * 34;
  1341. }
  1342. if (this.graphConfig.title != null)
  1343. {
  1344. var filename = container.ownerDocument.createElement('div');
  1345. filename.style.cssText = 'display:inline-block;position:relative;padding:3px 6px 0 6px;' +
  1346. 'vertical-align:top;font-family:Helvetica,Arial;font-size:12px;top:4px;cursor:default;'
  1347. filename.setAttribute('title', this.graphConfig.title);
  1348. mxUtils.write(filename, this.graphConfig.title);
  1349. mxUtils.setOpacity(filename, 70);
  1350. toolbar.appendChild(filename);
  1351. this.filename = filename;
  1352. }
  1353. this.minToolbarWidth = buttonCount * 34;
  1354. var prevBorder = container.style.border;
  1355. var enter = mxUtils.bind(this, function()
  1356. {
  1357. toolbar.style.width = (this.graphConfig['toolbar-position'] == 'inline') ? 'auto' :
  1358. Math.max(this.minToolbarWidth, container.offsetWidth) + 'px';
  1359. toolbar.style.border = '1px solid #d0d0d0';
  1360. if (this.graphConfig['toolbar-nohide'] != true)
  1361. {
  1362. var r = container.getBoundingClientRect();
  1363. // Workaround for position:relative set in ResizeSensor
  1364. var origin = mxUtils.getScrollOrigin(document.body)
  1365. var b = (document.body.style.position === 'relative') ? document.body.getBoundingClientRect() :
  1366. {left: -origin.x, top: -origin.y};
  1367. r = {left: r.left - b.left, top: r.top - b.top, bottom: r.bottom - b.top, right: r.right - b.left};
  1368. toolbar.style.left = r.left + 'px';
  1369. if (this.graphConfig['toolbar-position'] == 'bottom')
  1370. {
  1371. toolbar.style.top = r.bottom - 1 + 'px';
  1372. }
  1373. else
  1374. {
  1375. if (this.graphConfig['toolbar-position'] != 'inline')
  1376. {
  1377. toolbar.style.marginTop = -this.toolbarHeight + 'px';
  1378. toolbar.style.top = r.top + 1 + 'px';
  1379. }
  1380. else
  1381. {
  1382. toolbar.style.top = r.top + 'px';
  1383. }
  1384. }
  1385. if (prevBorder == '1px solid transparent')
  1386. {
  1387. container.style.border = '1px solid #d0d0d0';
  1388. }
  1389. document.body.appendChild(toolbar);
  1390. var hideToolbar = mxUtils.bind(this, function()
  1391. {
  1392. if (toolbar.parentNode != null)
  1393. {
  1394. toolbar.parentNode.removeChild(toolbar);
  1395. }
  1396. if (layersDialog != null)
  1397. {
  1398. layersDialog.parentNode.removeChild(layersDialog);
  1399. layersDialog = null;
  1400. }
  1401. container.style.border = prevBorder;
  1402. });
  1403. mxEvent.addListener(document, 'mousemove', function(evt)
  1404. {
  1405. var source = mxEvent.getSource(evt);
  1406. while (source != null)
  1407. {
  1408. if (source == container || source == toolbar || source == layersDialog)
  1409. {
  1410. return;
  1411. }
  1412. source = source.parentNode;
  1413. }
  1414. hideToolbar();
  1415. });
  1416. mxEvent.addListener(document.body, 'mouseleave', function(evt)
  1417. {
  1418. hideToolbar();
  1419. });
  1420. }
  1421. else
  1422. {
  1423. toolbar.style.top = -this.toolbarHeight + 'px';
  1424. container.appendChild(toolbar);
  1425. }
  1426. });
  1427. if (this.graphConfig['toolbar-nohide'] != true)
  1428. {
  1429. mxEvent.addListener(container, 'mouseenter', enter);
  1430. }
  1431. else
  1432. {
  1433. enter();
  1434. }
  1435. if (this.responsive && typeof ResizeObserver !== 'undefined')
  1436. {
  1437. new ResizeObserver(function()
  1438. {
  1439. if (toolbar.parentNode != null)
  1440. {
  1441. enter();
  1442. }
  1443. }).observe(container)
  1444. }
  1445. };
  1446. GraphViewer.prototype.disableButton = function(token)
  1447. {
  1448. var def = this.graphConfig['toolbar-buttons'][token];
  1449. if (def != null)
  1450. {
  1451. mxUtils.setOpacity(def.elem, 30);
  1452. mxEvent.removeListener(def.elem, 'click', def.handler);
  1453. //Workaround to stop highlighting the disabled button
  1454. mxEvent.addListener(def.elem, 'mouseenter', function()
  1455. {
  1456. def.elem.style.backgroundColor = '#eee';
  1457. });
  1458. }
  1459. };
  1460. /**
  1461. * Adds event handler for links and lightbox.
  1462. */
  1463. GraphViewer.prototype.addClickHandler = function(graph, ui)
  1464. {
  1465. graph.linkPolicy = this.graphConfig.target || graph.linkPolicy;
  1466. graph.addClickHandler(this.graphConfig.highlight, mxUtils.bind(this, function(evt, href)
  1467. {
  1468. if (href == null)
  1469. {
  1470. var source = mxEvent.getSource(evt);
  1471. while (source != graph.container && source != null && href == null)
  1472. {
  1473. if (source.nodeName.toLowerCase() == 'a')
  1474. {
  1475. href = source.getAttribute('href');
  1476. }
  1477. source = source.parentNode;
  1478. }
  1479. }
  1480. if (ui != null)
  1481. {
  1482. if (href == null || graph.isCustomLink(href))
  1483. {
  1484. mxEvent.consume(evt);
  1485. }
  1486. else if (!graph.isExternalProtocol(href) &&
  1487. !graph.isBlankLink(href))
  1488. {
  1489. // Hides lightbox if any links are clicked
  1490. // Async handling needed for anchors to work
  1491. window.setTimeout(function()
  1492. {
  1493. ui.destroy();
  1494. }, 0);
  1495. }
  1496. }
  1497. else if (href != null && ui == null && graph.isCustomLink(href) &&
  1498. (mxEvent.isTouchEvent(evt) || !mxEvent.isPopupTrigger(evt)) &&
  1499. graph.customLinkClicked(href))
  1500. {
  1501. // Workaround for text selection in Firefox on Windows
  1502. mxUtils.clearSelection();
  1503. mxEvent.consume(evt);
  1504. }
  1505. }), mxUtils.bind(this, function(evt)
  1506. {
  1507. if (ui == null && this.lightboxClickEnabled &&
  1508. (!mxEvent.isTouchEvent(evt) ||
  1509. this.toolbarItems.length == 0))
  1510. {
  1511. this.showLightbox();
  1512. }
  1513. }));
  1514. };
  1515. /**
  1516. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1517. */
  1518. GraphViewer.prototype.showLightbox = function(editable, closable, target)
  1519. {
  1520. if (this.graphConfig.lightbox == 'open' || window.self !== window.top)
  1521. {
  1522. if (this.lightboxWindow != null && !this.lightboxWindow.closed)
  1523. {
  1524. this.lightboxWindow.focus();
  1525. }
  1526. else
  1527. {
  1528. editable = (editable != null) ? editable :
  1529. ((this.graphConfig.editable != null) ?
  1530. this.graphConfig.editable : true);
  1531. closable = (closable != null) ? closable : true;
  1532. target = (target != null) ? target : 'blank';
  1533. var param = {'client': 1, 'target': target};
  1534. if (editable)
  1535. {
  1536. param.edit = this.graphConfig.edit || '_blank';
  1537. }
  1538. if (closable)
  1539. {
  1540. param.close = 1;
  1541. }
  1542. if (this.layersEnabled)
  1543. {
  1544. param.layers = 1;
  1545. }
  1546. if (this.tagsEnabled)
  1547. {
  1548. param.tags = {};
  1549. }
  1550. if (this.graphConfig != null && this.graphConfig.nav != false)
  1551. {
  1552. param.nav = 1;
  1553. }
  1554. if (this.graphConfig != null && this.graphConfig.highlight != null)
  1555. {
  1556. param.highlight = this.graphConfig.highlight.substring(1);
  1557. }
  1558. if (this.currentPage != null && this.currentPage > 0)
  1559. {
  1560. param.page = this.currentPage;
  1561. }
  1562. if (typeof window.postMessage !== 'undefined' && (document.documentMode == null || document.documentMode >= 10))
  1563. {
  1564. if (this.lightboxWindow == null)
  1565. {
  1566. mxEvent.addListener(window, 'message', mxUtils.bind(this, function(evt)
  1567. {
  1568. if (evt.data == 'ready' && evt.source == this.lightboxWindow)
  1569. {
  1570. this.lightboxWindow.postMessage(this.xml, '*');
  1571. }
  1572. }));
  1573. }
  1574. }
  1575. else
  1576. {
  1577. // Data is pulled from global variable after tab loads
  1578. param.data = encodeURIComponent(this.xml);
  1579. }
  1580. if (urlParams['dev'] == '1')
  1581. {
  1582. param.dev = '1';
  1583. }
  1584. this.lightboxWindow = window.open(((urlParams['dev'] != '1') ?
  1585. EditorUi.lightboxHost : 'https://test.draw.io') +
  1586. '/#P' + encodeURIComponent(JSON.stringify(param)));
  1587. }
  1588. }
  1589. else
  1590. {
  1591. this.showLocalLightbox();
  1592. }
  1593. };
  1594. /**
  1595. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1596. */
  1597. GraphViewer.prototype.showLocalLightbox = function()
  1598. {
  1599. var origin = mxUtils.getDocumentScrollOrigin(document);
  1600. var backdrop = document.createElement('div');
  1601. backdrop.style.cssText = 'position:fixed;top:0;left:0;bottom:0;right:0;';
  1602. backdrop.style.zIndex = this.lightboxZIndex;
  1603. backdrop.style.backgroundColor = '#000000';
  1604. mxUtils.setOpacity(backdrop, 70);
  1605. document.body.appendChild(backdrop);
  1606. var closeImg = document.createElement('img');
  1607. closeImg.setAttribute('border', '0');
  1608. closeImg.setAttribute('src', Editor.closeBlackImage);
  1609. closeImg.style.cssText = 'position:fixed;top:32px;right:32px;';
  1610. closeImg.style.cursor = 'pointer';
  1611. mxEvent.addListener(closeImg, 'click', function()
  1612. {
  1613. ui.destroy();
  1614. });
  1615. // LATER: Make possible to assign after instance was created
  1616. urlParams['pages'] = '1';
  1617. urlParams['page'] = this.currentPage;
  1618. urlParams['page-id'] = this.graphConfig.pageId;
  1619. urlParams['layer-ids'] = (this.graphConfig.layerIds != null && this.graphConfig.layerIds.length > 0)
  1620. ? this.graphConfig.layerIds.join(' ') : null;
  1621. urlParams['nav'] = (this.graphConfig.nav != false) ? '1' : '0';
  1622. urlParams['layers'] = (this.layersEnabled) ? '1' : '0';
  1623. if (this.tagsEnabled)
  1624. {
  1625. urlParams['tags'] = '{}';
  1626. }
  1627. // PostMessage not working and Permission denied for opened access in IE9-
  1628. if (document.documentMode == null || document.documentMode >= 10)
  1629. {
  1630. Editor.prototype.editButtonLink = this.graphConfig.edit;
  1631. Editor.prototype.editButtonFunc = this.graphConfig.editFunc;
  1632. }
  1633. EditorUi.prototype.updateActionStates = function() {};
  1634. EditorUi.prototype.addBeforeUnloadListener = function() {};
  1635. EditorUi.prototype.addChromelessClickHandler = function() {};
  1636. // Workaround for lost reference with same ID is to change
  1637. // ID which must be done before calling EditorUi constructor
  1638. var previousShadowId = Graph.prototype.shadowId;
  1639. Graph.prototype.shadowId = 'lightboxDropShadow';
  1640. var ui = new EditorUi(new Editor(true), document.createElement('div'), true);
  1641. ui.editor.editBlankUrl = this.editBlankUrl;
  1642. // Overrides instance variable and restores prototype state
  1643. ui.editor.graph.shadowId = 'lightboxDropShadow';
  1644. Graph.prototype.shadowId = previousShadowId;
  1645. // Disables refresh
  1646. ui.refresh = function() {};
  1647. // Handles escape keystroke
  1648. var keydownHandler = mxUtils.bind(this, function(evt)
  1649. {
  1650. if (evt.keyCode == 27 /* Escape */)
  1651. {
  1652. ui.destroy();
  1653. }
  1654. });
  1655. var destroy = ui.destroy;
  1656. ui.destroy = function()
  1657. {
  1658. mxEvent.removeListener(document.documentElement, 'keydown', keydownHandler);
  1659. document.body.removeChild(backdrop);
  1660. document.body.removeChild(closeImg);
  1661. document.body.style.overflow = 'auto';
  1662. GraphViewer.resizeSensorEnabled = true;
  1663. destroy.apply(this, arguments);
  1664. };
  1665. var graph = ui.editor.graph;
  1666. var lightbox = graph.container;
  1667. lightbox.style.overflow = 'hidden';
  1668. if (this.lightboxChrome)
  1669. {
  1670. lightbox.style.border = '1px solid #c0c0c0';
  1671. lightbox.style.margin = '40px';
  1672. // Installs the keystroke listener in the target
  1673. mxEvent.addListener(document.documentElement, 'keydown', keydownHandler);
  1674. }
  1675. else
  1676. {
  1677. backdrop.style.display = 'none';
  1678. closeImg.style.display = 'none';
  1679. }
  1680. // Handles relative images
  1681. var self = this;
  1682. graph.getImageFromBundles = function(key)
  1683. {
  1684. return self.getImageUrl(key);
  1685. };
  1686. // Handles relative images in print output and temporary graphs
  1687. var uiCreateTemporaryGraph = ui.createTemporaryGraph;
  1688. ui.createTemporaryGraph = function()
  1689. {
  1690. var newGraph = uiCreateTemporaryGraph.apply(this, arguments);
  1691. newGraph.getImageFromBundles = function(key)
  1692. {
  1693. return self.getImageUrl(key);
  1694. };
  1695. // newGraph.defaultPageBackgroundColor = graph.defaultPageBackgroundColor;
  1696. // newGraph.defaultPageBorderColor = graph.defaultPageBorderColor;
  1697. // newGraph.defaultThemeName = graph.defaultThemeName;
  1698. return newGraph;
  1699. };
  1700. if (this.graphConfig.move)
  1701. {
  1702. graph.isMoveCellsEvent = function(evt)
  1703. {
  1704. return true;
  1705. };
  1706. }
  1707. mxUtils.setPrefixedStyle(lightbox.style, 'border-radius', '4px');
  1708. lightbox.style.position = 'fixed';
  1709. GraphViewer.resizeSensorEnabled = false;
  1710. document.body.style.overflow = 'hidden';
  1711. // Workaround for possible rendering issues
  1712. if (!mxClient.IS_SF && !mxClient.IS_EDGE)
  1713. {
  1714. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(90deg)');
  1715. mxUtils.setPrefixedStyle(lightbox.style, 'transition', 'all .25s ease-in-out');
  1716. }
  1717. this.addClickHandler(graph, ui);
  1718. window.setTimeout(mxUtils.bind(this, function()
  1719. {
  1720. // Disables focus border in Chrome
  1721. lightbox.style.outline = 'none';
  1722. lightbox.style.zIndex = this.lightboxZIndex;
  1723. closeImg.style.zIndex = this.lightboxZIndex;
  1724. document.body.appendChild(lightbox);
  1725. document.body.appendChild(closeImg);
  1726. ui.setFileData(this.xml);
  1727. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(0deg)');
  1728. ui.chromelessToolbar.style.bottom = 60 + 'px';
  1729. ui.chromelessToolbar.style.zIndex = this.lightboxZIndex;
  1730. // Workaround for clipping in IE11-
  1731. document.body.appendChild(ui.chromelessToolbar);
  1732. ui.getEditBlankXml = mxUtils.bind(this, function()
  1733. {
  1734. return this.xml;
  1735. });
  1736. ui.lightboxFit();
  1737. ui.chromelessResize();
  1738. this.showLayers(graph, this.graph);
  1739. // Click on backdrop closes lightbox
  1740. mxEvent.addListener(backdrop, 'click', function()
  1741. {
  1742. ui.destroy();
  1743. });
  1744. }), 0);
  1745. return ui;
  1746. };
  1747. GraphViewer.prototype.updateTitle = function(title)
  1748. {
  1749. title = title || '';
  1750. if (this.showTitleAsTooltip && this.graph != null && this.graph.container != null)
  1751. {
  1752. this.graph.container.setAttribute('title', title);
  1753. }
  1754. if (this.filename != null)
  1755. {
  1756. this.filename.innerHTML = '';
  1757. mxUtils.write(this.filename, title);
  1758. this.filename.setAttribute('title', title);
  1759. }
  1760. };
  1761. /**
  1762. *
  1763. */
  1764. GraphViewer.processElements = function(classname)
  1765. {
  1766. mxUtils.forEach(GraphViewer.getElementsByClassName(classname || 'mxgraph'), function(div)
  1767. {
  1768. try
  1769. {
  1770. div.innerHTML = '';
  1771. GraphViewer.createViewerForElement(div);
  1772. }
  1773. catch (e)
  1774. {
  1775. div.innerHTML = e.message;
  1776. if (window.console != null)
  1777. {
  1778. console.error(e);
  1779. }
  1780. }
  1781. });
  1782. };
  1783. /**
  1784. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1785. */
  1786. GraphViewer.getElementsByClassName = function(classname)
  1787. {
  1788. if (document.getElementsByClassName)
  1789. {
  1790. var divs = document.getElementsByClassName(classname);
  1791. // Workaround for changing divs while processing
  1792. var result = [];
  1793. for (var i = 0; i < divs.length; i++)
  1794. {
  1795. result.push(divs[i]);
  1796. }
  1797. return result;
  1798. }
  1799. else
  1800. {
  1801. var tmp = document.getElementsByTagName('*');
  1802. var divs = [];
  1803. for (var i = 0; i < tmp.length; i++)
  1804. {
  1805. var cls = tmp[i].className;
  1806. if (cls != null && cls.length > 0)
  1807. {
  1808. var tokens = cls.split(' ');
  1809. if (mxUtils.indexOf(tokens, classname) >= 0)
  1810. {
  1811. divs.push(tmp[i]);
  1812. }
  1813. }
  1814. }
  1815. return divs;
  1816. }
  1817. };
  1818. /**
  1819. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1820. */
  1821. GraphViewer.createViewerForElement = function(element, callback)
  1822. {
  1823. var data = element.getAttribute('data-mxgraph');
  1824. if (data != null)
  1825. {
  1826. var config = JSON.parse(data);
  1827. var createViewer = function(xml)
  1828. {
  1829. var xmlDoc = mxUtils.parseXml(xml);
  1830. var viewer = new GraphViewer(element, xmlDoc.documentElement, config);
  1831. if (callback != null)
  1832. {
  1833. callback(viewer);
  1834. }
  1835. };
  1836. if (config.url != null)
  1837. {
  1838. GraphViewer.getUrl(config.url, function(xml)
  1839. {
  1840. createViewer(xml);
  1841. });
  1842. }
  1843. else
  1844. {
  1845. createViewer(config.xml);
  1846. }
  1847. }
  1848. };
  1849. /**
  1850. * Adds event if grid size is changed.
  1851. */
  1852. GraphViewer.initCss = function()
  1853. {
  1854. try
  1855. {
  1856. var style = document.createElement('style')
  1857. style.type = 'text/css';
  1858. style.innerHTML = ['div.mxTooltip {',
  1859. '-webkit-box-shadow: 3px 3px 12px #C0C0C0;',
  1860. '-moz-box-shadow: 3px 3px 12px #C0C0C0;',
  1861. 'box-shadow: 3px 3px 12px #C0C0C0;',
  1862. 'background: #FFFFCC;',
  1863. 'border-style: solid;',
  1864. 'border-width: 1px;',
  1865. 'border-color: black;',
  1866. 'font-family: Arial;',
  1867. 'font-size: 8pt;',
  1868. 'position: absolute;',
  1869. 'cursor: default;',
  1870. 'padding: 4px;',
  1871. 'color: black;}',
  1872. 'td.mxPopupMenuIcon div {',
  1873. 'width:16px;',
  1874. 'height:16px;}',
  1875. 'html div.mxPopupMenu {',
  1876. '-webkit-box-shadow:2px 2px 3px #d5d5d5;',
  1877. '-moz-box-shadow:2px 2px 3px #d5d5d5;',
  1878. 'box-shadow:2px 2px 3px #d5d5d5;',
  1879. '_filter:progid:DXImageTransform.Microsoft.DropShadow(OffX=2, OffY=2, Color=\'#d0d0d0\',Positive=\'true\');',
  1880. 'background:white;',
  1881. 'position:absolute;',
  1882. 'border:3px solid #e7e7e7;',
  1883. 'padding:3px;}',
  1884. 'html table.mxPopupMenu {',
  1885. 'border-collapse:collapse;',
  1886. 'margin:0px;}',
  1887. 'html td.mxPopupMenuItem {',
  1888. 'padding:7px 30px 7px 30px;',
  1889. 'font-family:Helvetica Neue,Helvetica,Arial Unicode MS,Arial;',
  1890. 'font-size:10pt;}',
  1891. 'html td.mxPopupMenuIcon {',
  1892. 'background-color:white;',
  1893. 'padding:0px;}',
  1894. 'td.mxPopupMenuIcon .geIcon {',
  1895. 'padding:2px;',
  1896. 'padding-bottom:4px;',
  1897. 'margin:2px;',
  1898. 'border:1px solid transparent;',
  1899. 'opacity:0.5;',
  1900. '_width:26px;',
  1901. '_height:26px;}',
  1902. 'td.mxPopupMenuIcon .geIcon:hover {',
  1903. 'border:1px solid gray;',
  1904. 'border-radius:2px;',
  1905. 'opacity:1;}',
  1906. 'html tr.mxPopupMenuItemHover {',
  1907. 'background-color: #eeeeee;',
  1908. 'color: black;}',
  1909. 'table.mxPopupMenu hr {',
  1910. 'color:#cccccc;',
  1911. 'background-color:#cccccc;',
  1912. 'border:none;',
  1913. 'height:1px;}',
  1914. 'table.mxPopupMenu tr { font-size:4pt;}',
  1915. // Modified to only apply to the print dialog
  1916. '.geDialog { font-family:Helvetica Neue,Helvetica,Arial Unicode MS,Arial;',
  1917. 'font-size:10pt;',
  1918. 'border:none;',
  1919. 'margin:0px;}',
  1920. // These are required for the print dialog
  1921. '.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;}',
  1922. '.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');
  1923. document.getElementsByTagName('head')[0].appendChild(style);
  1924. }
  1925. catch (e)
  1926. {
  1927. // ignore
  1928. }
  1929. };
  1930. /**
  1931. * Lookup for URLs.
  1932. */
  1933. GraphViewer.cachedUrls = {};
  1934. /**
  1935. * Workaround for unsupported CORS in IE9 XHR
  1936. */
  1937. GraphViewer.getUrl = function(url, onload, onerror)
  1938. {
  1939. if (GraphViewer.cachedUrls[url] != null)
  1940. {
  1941. onload(GraphViewer.cachedUrls[url]);
  1942. }
  1943. else
  1944. {
  1945. var xhr = (navigator.userAgent != null && navigator.userAgent.indexOf('MSIE 9') > 0) ?
  1946. new XDomainRequest() : new XMLHttpRequest();
  1947. xhr.open('GET', url);
  1948. xhr.onload = function()
  1949. {
  1950. onload((xhr.getText != null) ? xhr.getText() : xhr.responseText);
  1951. };
  1952. xhr.onerror = onerror;
  1953. xhr.send();
  1954. }
  1955. };
  1956. /**
  1957. * Redirects editing to absolue URLs.
  1958. */
  1959. GraphViewer.resizeSensorEnabled = true;
  1960. /**
  1961. * Redirects editing to absolue URLs.
  1962. */
  1963. GraphViewer.useResizeSensor = true;
  1964. /**
  1965. * Copyright Marc J. Schmidt. See the LICENSE file at the top-level
  1966. * directory of this distribution and at
  1967. * https://github.com/marcj/css-element-queries/blob/master/LICENSE.
  1968. */
  1969. (function() {
  1970. // Only used for the dirty checking, so the event callback count is limted to max 1 call per fps per sensor.
  1971. // In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and
  1972. // would generate too many unnecessary events.
  1973. var requestAnimationFrame = window.requestAnimationFrame ||
  1974. window.mozRequestAnimationFrame ||
  1975. window.webkitRequestAnimationFrame ||
  1976. function (fn) {
  1977. return window.setTimeout(fn, 20);
  1978. };
  1979. /**
  1980. * Class for dimension change detection.
  1981. *
  1982. * @param {Element|Element[]|Elements|jQuery} element
  1983. * @param {Function} callback
  1984. *
  1985. * @constructor
  1986. */
  1987. var ResizeSensor = function(element, fn) {
  1988. var callback = function()
  1989. {
  1990. if (GraphViewer.resizeSensorEnabled)
  1991. {
  1992. fn();
  1993. }
  1994. };
  1995. /**
  1996. *
  1997. * @constructor
  1998. */
  1999. function EventQueue() {
  2000. this.q = [];
  2001. this.add = function(ev) {
  2002. this.q.push(ev);
  2003. };
  2004. var i, j;
  2005. this.call = function() {
  2006. for (i = 0, j = this.q.length; i < j; i++) {
  2007. this.q[i].call();
  2008. }
  2009. };
  2010. }
  2011. /**
  2012. * @param {HTMLElement} element
  2013. * @param {String} prop
  2014. * @returns {String|Number}
  2015. */
  2016. function getComputedStyle(element, prop) {
  2017. if (element.currentStyle) {
  2018. return element.currentStyle[prop];
  2019. } else if (window.getComputedStyle) {
  2020. return window.getComputedStyle(element, null).getPropertyValue(prop);
  2021. } else {
  2022. return element.style[prop];
  2023. }
  2024. }
  2025. /**
  2026. *
  2027. * @param {HTMLElement} element
  2028. * @param {Function} resized
  2029. */
  2030. function attachResizeEvent(element, resized) {
  2031. if (!element.resizedAttached) {
  2032. element.resizedAttached = new EventQueue();
  2033. element.resizedAttached.add(resized);
  2034. } else if (element.resizedAttached) {
  2035. element.resizedAttached.add(resized);
  2036. return;
  2037. }
  2038. element.resizeSensor = document.createElement('div');
  2039. element.resizeSensor.className = 'resize-sensor';
  2040. var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
  2041. var styleChild = 'position: absolute; left: 0; top: 0; transition: 0s;';
  2042. element.resizeSensor.style.cssText = style;
  2043. element.resizeSensor.innerHTML =
  2044. '<div class="resize-sensor-expand" style="' + style + '">' +
  2045. '<div style="' + styleChild + '"></div>' +
  2046. '</div>' +
  2047. '<div class="resize-sensor-shrink" style="' + style + '">' +
  2048. '<div style="' + styleChild + ' width: 200%; height: 200%"></div>' +
  2049. '</div>';
  2050. element.appendChild(element.resizeSensor);
  2051. // FIXME: Should not change element style
  2052. if (getComputedStyle(element, 'position') == 'static') {
  2053. element.style.position = 'relative';
  2054. }
  2055. var expand = element.resizeSensor.childNodes[0];
  2056. var expandChild = expand.childNodes[0];
  2057. var shrink = element.resizeSensor.childNodes[1];
  2058. var reset = function() {
  2059. expandChild.style.width = 100000 + 'px';
  2060. expandChild.style.height = 100000 + 'px';
  2061. expand.scrollLeft = 100000;
  2062. expand.scrollTop = 100000;
  2063. shrink.scrollLeft = 100000;
  2064. shrink.scrollTop = 100000;
  2065. };
  2066. reset();
  2067. var dirty = false;
  2068. var dirtyChecking = function(){
  2069. if (!element.resizedAttached) return;
  2070. if (dirty) {
  2071. element.resizedAttached.call();
  2072. dirty = false;
  2073. }
  2074. requestAnimationFrame(dirtyChecking);
  2075. };
  2076. requestAnimationFrame(dirtyChecking);
  2077. var lastWidth, lastHeight;
  2078. var cachedWidth, cachedHeight; //useful to not query offsetWidth twice
  2079. var onScroll = function() {
  2080. if ((cachedWidth = element.offsetWidth) != lastWidth || (cachedHeight = element.offsetHeight) != lastHeight) {
  2081. dirty = true;
  2082. lastWidth = cachedWidth;
  2083. lastHeight = cachedHeight;
  2084. }
  2085. reset();
  2086. };
  2087. var addEvent = function(el, name, cb) {
  2088. if (el.attachEvent) {
  2089. el.attachEvent('on' + name, cb);
  2090. } else {
  2091. el.addEventListener(name, cb);
  2092. }
  2093. };
  2094. addEvent(expand, 'scroll', onScroll);
  2095. addEvent(shrink, 'scroll', onScroll);
  2096. }
  2097. var elementType = Object.prototype.toString.call(element);
  2098. var isCollectionTyped = ('[object Array]' === elementType
  2099. || ('[object NodeList]' === elementType)
  2100. || ('[object HTMLCollection]' === elementType)
  2101. || ('undefined' !== typeof jQuery && element instanceof jQuery) //jquery
  2102. || ('undefined' !== typeof Elements && element instanceof Elements) //mootools
  2103. );
  2104. if (isCollectionTyped) {
  2105. var i = 0, j = element.length;
  2106. for (; i < j; i++) {
  2107. attachResizeEvent(element[i], callback);
  2108. }
  2109. } else {
  2110. attachResizeEvent(element, callback);
  2111. }
  2112. this.detach = function() {
  2113. if (isCollectionTyped) {
  2114. var i = 0, j = element.length;
  2115. for (; i < j; i++) {
  2116. ResizeSensor.detach(element[i]);
  2117. }
  2118. } else {
  2119. ResizeSensor.detach(element);
  2120. }
  2121. };
  2122. };
  2123. ResizeSensor.detach = function(element) {
  2124. if (element.resizeSensor) {
  2125. element.removeChild(element.resizeSensor);
  2126. delete element.resizeSensor;
  2127. delete element.resizedAttached;
  2128. }
  2129. };
  2130. window.ResizeSensor = ResizeSensor;
  2131. })();