mxGraphView.js 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxGraphView
  7. *
  8. * Extends <mxEventSource> to implement a view for a graph. This class is in
  9. * charge of computing the absolute coordinates for the relative child
  10. * geometries, the points for perimeters and edge styles and keeping them
  11. * cached in <mxCellStates> for faster retrieval. The states are updated
  12. * whenever the model or the view state (translate, scale) changes. The scale
  13. * and translate are honoured in the bounds.
  14. *
  15. * Event: mxEvent.UNDO
  16. *
  17. * Fires after the root was changed in <setCurrentRoot>. The <code>edit</code>
  18. * property contains the <mxUndoableEdit> which contains the
  19. * <mxCurrentRootChange>.
  20. *
  21. * Event: mxEvent.SCALE_AND_TRANSLATE
  22. *
  23. * Fires after the scale and translate have been changed in <scaleAndTranslate>.
  24. * The <code>scale</code>, <code>previousScale</code>, <code>translate</code>
  25. * and <code>previousTranslate</code> properties contain the new and previous
  26. * scale and translate, respectively.
  27. *
  28. * Event: mxEvent.SCALE
  29. *
  30. * Fires after the scale was changed in <setScale>. The <code>scale</code> and
  31. * <code>previousScale</code> properties contain the new and previous scale.
  32. *
  33. * Event: mxEvent.TRANSLATE
  34. *
  35. * Fires after the translate was changed in <setTranslate>. The
  36. * <code>translate</code> and <code>previousTranslate</code> properties contain
  37. * the new and previous value for translate.
  38. *
  39. * Event: mxEvent.DOWN and mxEvent.UP
  40. *
  41. * Fire if the current root is changed by executing an <mxCurrentRootChange>.
  42. * The event name depends on the location of the root in the cell hierarchy
  43. * with respect to the current root. The <code>root</code> and
  44. * <code>previous</code> properties contain the new and previous root,
  45. * respectively.
  46. *
  47. * Constructor: mxGraphView
  48. *
  49. * Constructs a new view for the given <mxGraph>.
  50. *
  51. * Parameters:
  52. *
  53. * graph - Reference to the enclosing <mxGraph>.
  54. */
  55. function mxGraphView(graph)
  56. {
  57. this.graph = graph;
  58. this.translate = new mxPoint();
  59. this.graphBounds = new mxRectangle();
  60. this.states = new mxDictionary();
  61. };
  62. /**
  63. * Extends mxEventSource.
  64. */
  65. mxGraphView.prototype = new mxEventSource();
  66. mxGraphView.prototype.constructor = mxGraphView;
  67. /**
  68. *
  69. */
  70. mxGraphView.prototype.EMPTY_POINT = new mxPoint();
  71. /**
  72. * Variable: doneResource
  73. *
  74. * Specifies the resource key for the status message after a long operation.
  75. * If the resource for this key does not exist then the value is used as
  76. * the status message. Default is 'done'.
  77. */
  78. mxGraphView.prototype.doneResource = (mxClient.language != 'none') ? 'done' : '';
  79. /**
  80. * Function: updatingDocumentResource
  81. *
  82. * Specifies the resource key for the status message while the document is
  83. * being updated. If the resource for this key does not exist then the
  84. * value is used as the status message. Default is 'updatingDocument'.
  85. */
  86. mxGraphView.prototype.updatingDocumentResource = (mxClient.language != 'none') ? 'updatingDocument' : '';
  87. /**
  88. * Variable: allowEval
  89. *
  90. * Specifies if string values in cell styles should be evaluated using
  91. * <mxUtils.eval>. This will only be used if the string values can't be mapped
  92. * to objects using <mxStyleRegistry>. Default is false. NOTE: Enabling this
  93. * switch carries a possible security risk.
  94. */
  95. mxGraphView.prototype.allowEval = false;
  96. /**
  97. * Variable: captureDocumentGesture
  98. *
  99. * Specifies if a gesture should be captured when it goes outside of the
  100. * graph container. Default is true.
  101. */
  102. mxGraphView.prototype.captureDocumentGesture = true;
  103. /**
  104. * Variable: optimizeVmlReflows
  105. *
  106. * Specifies if the <canvas> should be hidden while rendering in IE8 standards
  107. * mode and quirks mode. This will significantly improve rendering performance.
  108. * Default is true.
  109. */
  110. mxGraphView.prototype.optimizeVmlReflows = true;
  111. /**
  112. * Variable: rendering
  113. *
  114. * Specifies if shapes should be created, updated and destroyed using the
  115. * methods of <mxCellRenderer> in <graph>. Default is true.
  116. */
  117. mxGraphView.prototype.rendering = true;
  118. /**
  119. * Variable: graph
  120. *
  121. * Reference to the enclosing <mxGraph>.
  122. */
  123. mxGraphView.prototype.graph = null;
  124. /**
  125. * Variable: currentRoot
  126. *
  127. * <mxCell> that acts as the root of the displayed cell hierarchy.
  128. */
  129. mxGraphView.prototype.currentRoot = null;
  130. /**
  131. * Variable: graphBounds
  132. *
  133. * <mxRectangle> that caches the scales, translated bounds of the current view.
  134. */
  135. mxGraphView.prototype.graphBounds = null;
  136. /**
  137. * Variable: scale
  138. *
  139. * Specifies the scale. Default is 1 (100%).
  140. */
  141. mxGraphView.prototype.scale = 1;
  142. /**
  143. * Variable: translate
  144. *
  145. * <mxPoint> that specifies the current translation. Default is a new
  146. * empty <mxPoint>.
  147. */
  148. mxGraphView.prototype.translate = null;
  149. /**
  150. * Variable: states
  151. *
  152. * <mxDictionary> that maps from cell IDs to <mxCellStates>.
  153. */
  154. mxGraphView.prototype.states = null;
  155. /**
  156. * Variable: updateStyle
  157. *
  158. * Specifies if the style should be updated in each validation step. If this
  159. * is false then the style is only updated if the state is created or if the
  160. * style of the cell was changed. Default is false.
  161. */
  162. mxGraphView.prototype.updateStyle = false;
  163. /**
  164. * Variable: lastNode
  165. *
  166. * During validation, this contains the last DOM node that was processed.
  167. */
  168. mxGraphView.prototype.lastNode = null;
  169. /**
  170. * Variable: lastHtmlNode
  171. *
  172. * During validation, this contains the last HTML DOM node that was processed.
  173. */
  174. mxGraphView.prototype.lastHtmlNode = null;
  175. /**
  176. * Variable: lastForegroundNode
  177. *
  178. * During validation, this contains the last edge's DOM node that was processed.
  179. */
  180. mxGraphView.prototype.lastForegroundNode = null;
  181. /**
  182. * Variable: lastForegroundHtmlNode
  183. *
  184. * During validation, this contains the last edge HTML DOM node that was processed.
  185. */
  186. mxGraphView.prototype.lastForegroundHtmlNode = null;
  187. /**
  188. * Function: getGraphBounds
  189. *
  190. * Returns <graphBounds>.
  191. */
  192. mxGraphView.prototype.getGraphBounds = function()
  193. {
  194. return this.graphBounds;
  195. };
  196. /**
  197. * Function: setGraphBounds
  198. *
  199. * Sets <graphBounds>.
  200. */
  201. mxGraphView.prototype.setGraphBounds = function(value)
  202. {
  203. this.graphBounds = value;
  204. };
  205. /**
  206. * Function: getBounds
  207. *
  208. * Returns the union of all <mxCellStates> for the given array of <mxCells>.
  209. *
  210. * Parameters:
  211. *
  212. * cells - Array of <mxCells> whose bounds should be returned.
  213. */
  214. mxGraphView.prototype.getBounds = function(cells)
  215. {
  216. var result = null;
  217. if (cells != null && cells.length > 0)
  218. {
  219. var model = this.graph.getModel();
  220. for (var i = 0; i < cells.length; i++)
  221. {
  222. if (model.isVertex(cells[i]) || model.isEdge(cells[i]))
  223. {
  224. var state = this.getState(cells[i]);
  225. if (state != null)
  226. {
  227. if (result == null)
  228. {
  229. result = mxRectangle.fromRectangle(state);
  230. }
  231. else
  232. {
  233. result.add(state);
  234. }
  235. }
  236. }
  237. }
  238. }
  239. return result;
  240. };
  241. /**
  242. * Function: setCurrentRoot
  243. *
  244. * Sets and returns the current root and fires an <undo> event before
  245. * calling <mxGraph.sizeDidChange>.
  246. *
  247. * Parameters:
  248. *
  249. * root - <mxCell> that specifies the root of the displayed cell hierarchy.
  250. */
  251. mxGraphView.prototype.setCurrentRoot = function(root)
  252. {
  253. if (this.currentRoot != root)
  254. {
  255. var change = new mxCurrentRootChange(this, root);
  256. change.execute();
  257. var edit = new mxUndoableEdit(this, true);
  258. edit.add(change);
  259. this.fireEvent(new mxEventObject(mxEvent.UNDO, 'edit', edit));
  260. this.graph.sizeDidChange();
  261. }
  262. return root;
  263. };
  264. /**
  265. * Function: scaleAndTranslate
  266. *
  267. * Sets the scale and translation and fires a <scale> and <translate> event
  268. * before calling <revalidate> followed by <mxGraph.sizeDidChange>.
  269. *
  270. * Parameters:
  271. *
  272. * scale - Decimal value that specifies the new scale (1 is 100%).
  273. * dx - X-coordinate of the translation.
  274. * dy - Y-coordinate of the translation.
  275. */
  276. mxGraphView.prototype.scaleAndTranslate = function(scale, dx, dy)
  277. {
  278. var previousScale = this.scale;
  279. var previousTranslate = new mxPoint(this.translate.x, this.translate.y);
  280. if (this.scale != scale || this.translate.x != dx || this.translate.y != dy)
  281. {
  282. this.scale = scale;
  283. this.translate.x = dx;
  284. this.translate.y = dy;
  285. if (this.isEventsEnabled())
  286. {
  287. this.viewStateChanged();
  288. }
  289. }
  290. this.fireEvent(new mxEventObject(mxEvent.SCALE_AND_TRANSLATE,
  291. 'scale', scale, 'previousScale', previousScale,
  292. 'translate', this.translate, 'previousTranslate', previousTranslate));
  293. };
  294. /**
  295. * Function: getScale
  296. *
  297. * Returns the <scale>.
  298. */
  299. mxGraphView.prototype.getScale = function()
  300. {
  301. return this.scale;
  302. };
  303. /**
  304. * Function: setScale
  305. *
  306. * Sets the scale and fires a <scale> event before calling <revalidate> followed
  307. * by <mxGraph.sizeDidChange>.
  308. *
  309. * Parameters:
  310. *
  311. * value - Decimal value that specifies the new scale (1 is 100%).
  312. */
  313. mxGraphView.prototype.setScale = function(value)
  314. {
  315. var previousScale = this.scale;
  316. if (this.scale != value)
  317. {
  318. this.scale = value;
  319. if (this.isEventsEnabled())
  320. {
  321. this.viewStateChanged();
  322. }
  323. }
  324. this.fireEvent(new mxEventObject(mxEvent.SCALE,
  325. 'scale', value, 'previousScale', previousScale));
  326. };
  327. /**
  328. * Function: getTranslate
  329. *
  330. * Returns the <translate>.
  331. */
  332. mxGraphView.prototype.getTranslate = function()
  333. {
  334. return this.translate;
  335. };
  336. /**
  337. * Function: setTranslate
  338. *
  339. * Sets the translation and fires a <translate> event before calling
  340. * <revalidate> followed by <mxGraph.sizeDidChange>. The translation is the
  341. * negative of the origin.
  342. *
  343. * Parameters:
  344. *
  345. * dx - X-coordinate of the translation.
  346. * dy - Y-coordinate of the translation.
  347. */
  348. mxGraphView.prototype.setTranslate = function(dx, dy)
  349. {
  350. var previousTranslate = new mxPoint(this.translate.x, this.translate.y);
  351. if (this.translate.x != dx || this.translate.y != dy)
  352. {
  353. this.translate.x = dx;
  354. this.translate.y = dy;
  355. if (this.isEventsEnabled())
  356. {
  357. this.viewStateChanged();
  358. }
  359. }
  360. this.fireEvent(new mxEventObject(mxEvent.TRANSLATE,
  361. 'translate', this.translate, 'previousTranslate', previousTranslate));
  362. };
  363. /**
  364. * Function: viewStateChanged
  365. *
  366. * Invoked after <scale> and/or <translate> has changed.
  367. */
  368. mxGraphView.prototype.viewStateChanged = function()
  369. {
  370. this.revalidate();
  371. this.graph.sizeDidChange();
  372. };
  373. /**
  374. * Function: refresh
  375. *
  376. * Clears the view if <currentRoot> is not null and revalidates.
  377. */
  378. mxGraphView.prototype.refresh = function()
  379. {
  380. if (this.currentRoot != null)
  381. {
  382. this.clear();
  383. }
  384. this.revalidate();
  385. };
  386. /**
  387. * Function: revalidate
  388. *
  389. * Revalidates the complete view with all cell states.
  390. */
  391. mxGraphView.prototype.revalidate = function()
  392. {
  393. this.invalidate();
  394. this.validate();
  395. };
  396. /**
  397. * Function: clear
  398. *
  399. * Removes the state of the given cell and all descendants if the given
  400. * cell is not the current root.
  401. *
  402. * Parameters:
  403. *
  404. * cell - Optional <mxCell> for which the state should be removed. Default
  405. * is the root of the model.
  406. * force - Boolean indicating if the current root should be ignored for
  407. * recursion.
  408. */
  409. mxGraphView.prototype.clear = function(cell, force, recurse)
  410. {
  411. var model = this.graph.getModel();
  412. cell = cell || model.getRoot();
  413. force = (force != null) ? force : false;
  414. recurse = (recurse != null) ? recurse : true;
  415. this.removeState(cell);
  416. if (recurse && (force || cell != this.currentRoot))
  417. {
  418. var childCount = model.getChildCount(cell);
  419. for (var i = 0; i < childCount; i++)
  420. {
  421. this.clear(model.getChildAt(cell, i), force);
  422. }
  423. }
  424. else
  425. {
  426. this.invalidate(cell);
  427. }
  428. };
  429. /**
  430. * Function: invalidate
  431. *
  432. * Invalidates the state of the given cell, all its descendants and
  433. * connected edges.
  434. *
  435. * Parameters:
  436. *
  437. * cell - Optional <mxCell> to be invalidated. Default is the root of the
  438. * model.
  439. */
  440. mxGraphView.prototype.invalidate = function(cell, recurse, includeEdges)
  441. {
  442. var model = this.graph.getModel();
  443. cell = cell || model.getRoot();
  444. recurse = (recurse != null) ? recurse : true;
  445. includeEdges = (includeEdges != null) ? includeEdges : true;
  446. var state = this.getState(cell);
  447. if (state != null)
  448. {
  449. state.invalid = true;
  450. }
  451. // Avoids infinite loops for invalid graphs
  452. if (!cell.invalidating)
  453. {
  454. cell.invalidating = true;
  455. // Recursively invalidates all descendants
  456. if (recurse)
  457. {
  458. var childCount = model.getChildCount(cell);
  459. for (var i = 0; i < childCount; i++)
  460. {
  461. var child = model.getChildAt(cell, i);
  462. this.invalidate(child, recurse, includeEdges);
  463. }
  464. }
  465. // Propagates invalidation to all connected edges
  466. if (includeEdges)
  467. {
  468. var edgeCount = model.getEdgeCount(cell);
  469. for (var i = 0; i < edgeCount; i++)
  470. {
  471. this.invalidate(model.getEdgeAt(cell, i), recurse, includeEdges);
  472. }
  473. }
  474. delete cell.invalidating;
  475. }
  476. };
  477. /**
  478. * Function: validate
  479. *
  480. * Calls <validateCell> and <validateCellState> and updates the <graphBounds>
  481. * using <getBoundingBox>. Finally the background is validated using
  482. * <validateBackground>.
  483. *
  484. * Parameters:
  485. *
  486. * cell - Optional <mxCell> to be used as the root of the validation.
  487. * Default is <currentRoot> or the root of the model.
  488. */
  489. mxGraphView.prototype.validate = function(cell)
  490. {
  491. var t0 = mxLog.enter('mxGraphView.validate');
  492. window.status = mxResources.get(this.updatingDocumentResource) ||
  493. this.updatingDocumentResource;
  494. this.resetValidationState();
  495. // Improves IE rendering speed by minimizing reflows
  496. var prevDisplay = null;
  497. if (this.optimizeVmlReflows && this.canvas != null && this.textDiv == null &&
  498. ((document.documentMode == 8 && !mxClient.IS_EM) || mxClient.IS_QUIRKS))
  499. {
  500. // Placeholder keeps scrollbar positions when canvas is hidden
  501. this.placeholder = document.createElement('div');
  502. this.placeholder.style.position = 'absolute';
  503. this.placeholder.style.width = this.canvas.clientWidth + 'px';
  504. this.placeholder.style.height = this.canvas.clientHeight + 'px';
  505. this.canvas.parentNode.appendChild(this.placeholder);
  506. prevDisplay = this.drawPane.style.display;
  507. this.canvas.style.display = 'none';
  508. // Creates temporary DIV used for text measuring in mxText.updateBoundingBox
  509. this.textDiv = document.createElement('div');
  510. this.textDiv.style.position = 'absolute';
  511. this.textDiv.style.whiteSpace = 'nowrap';
  512. this.textDiv.style.visibility = 'hidden';
  513. this.textDiv.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  514. this.textDiv.style.zoom = '1';
  515. document.body.appendChild(this.textDiv);
  516. }
  517. var graphBounds = this.getBoundingBox(this.validateCellState(
  518. this.validateCell(cell || ((this.currentRoot != null) ?
  519. this.currentRoot : this.graph.getModel().getRoot()))));
  520. this.setGraphBounds((graphBounds != null) ? graphBounds : this.getEmptyBounds());
  521. this.validateBackground();
  522. if (prevDisplay != null)
  523. {
  524. this.canvas.style.display = prevDisplay;
  525. this.textDiv.parentNode.removeChild(this.textDiv);
  526. if (this.placeholder != null)
  527. {
  528. this.placeholder.parentNode.removeChild(this.placeholder);
  529. }
  530. // Textdiv cannot be reused
  531. this.textDiv = null;
  532. }
  533. this.resetValidationState();
  534. window.status = mxResources.get(this.doneResource) ||
  535. this.doneResource;
  536. mxLog.leave('mxGraphView.validate', t0);
  537. };
  538. /**
  539. * Function: getEmptyBounds
  540. *
  541. * Returns the bounds for an empty graph. This returns a rectangle at
  542. * <translate> with the size of 0 x 0.
  543. */
  544. mxGraphView.prototype.getEmptyBounds = function()
  545. {
  546. return new mxRectangle(this.translate.x * this.scale, this.translate.y * this.scale);
  547. };
  548. /**
  549. * Function: getBoundingBox
  550. *
  551. * Returns the bounding box of the shape and the label for the given
  552. * <mxCellState> and its children if recurse is true.
  553. *
  554. * Parameters:
  555. *
  556. * state - <mxCellState> whose bounding box should be returned.
  557. * recurse - Optional boolean indicating if the children should be included.
  558. * Default is true.
  559. */
  560. mxGraphView.prototype.getBoundingBox = function(state, recurse)
  561. {
  562. recurse = (recurse != null) ? recurse : true;
  563. var bbox = null;
  564. if (state != null)
  565. {
  566. if (state.shape != null && state.shape.boundingBox != null)
  567. {
  568. bbox = state.shape.boundingBox.clone();
  569. }
  570. // Adds label bounding box to graph bounds
  571. if (state.text != null && state.text.boundingBox != null)
  572. {
  573. if (bbox != null)
  574. {
  575. bbox.add(state.text.boundingBox);
  576. }
  577. else
  578. {
  579. bbox = state.text.boundingBox.clone();
  580. }
  581. }
  582. if (recurse)
  583. {
  584. var model = this.graph.getModel();
  585. var childCount = model.getChildCount(state.cell);
  586. for (var i = 0; i < childCount; i++)
  587. {
  588. var bounds = this.getBoundingBox(this.getState(model.getChildAt(state.cell, i)));
  589. if (bounds != null)
  590. {
  591. if (bbox == null)
  592. {
  593. bbox = bounds;
  594. }
  595. else
  596. {
  597. bbox.add(bounds);
  598. }
  599. }
  600. }
  601. }
  602. }
  603. return bbox;
  604. };
  605. /**
  606. * Function: createBackgroundPageShape
  607. *
  608. * Creates and returns the shape used as the background page.
  609. *
  610. * Parameters:
  611. *
  612. * bounds - <mxRectangle> that represents the bounds of the shape.
  613. */
  614. mxGraphView.prototype.createBackgroundPageShape = function(bounds)
  615. {
  616. return new mxRectangleShape(bounds, 'white', 'black');
  617. };
  618. /**
  619. * Function: validateBackground
  620. *
  621. * Calls <validateBackgroundImage> and <validateBackgroundPage>.
  622. */
  623. mxGraphView.prototype.validateBackground = function()
  624. {
  625. this.validateBackgroundImage();
  626. this.validateBackgroundPage();
  627. };
  628. /**
  629. * Function: validateBackgroundImage
  630. *
  631. * Validates the background image.
  632. */
  633. mxGraphView.prototype.validateBackgroundImage = function()
  634. {
  635. var bg = this.graph.getBackgroundImage();
  636. if (bg != null)
  637. {
  638. if (this.backgroundImage == null || this.backgroundImage.image != bg.src)
  639. {
  640. if (this.backgroundImage != null)
  641. {
  642. this.backgroundImage.destroy();
  643. }
  644. var bounds = new mxRectangle(0, 0, 1, 1);
  645. this.backgroundImage = new mxImageShape(bounds, bg.src);
  646. this.backgroundImage.dialect = this.graph.dialect;
  647. this.backgroundImage.init(this.backgroundPane);
  648. this.backgroundImage.redraw();
  649. // Workaround for ignored event on background in IE8 standards mode
  650. if (document.documentMode == 8 && !mxClient.IS_EM)
  651. {
  652. mxEvent.addGestureListeners(this.backgroundImage.node,
  653. mxUtils.bind(this, function(evt)
  654. {
  655. this.graph.fireMouseEvent(mxEvent.MOUSE_DOWN, new mxMouseEvent(evt));
  656. }),
  657. mxUtils.bind(this, function(evt)
  658. {
  659. this.graph.fireMouseEvent(mxEvent.MOUSE_MOVE, new mxMouseEvent(evt));
  660. }),
  661. mxUtils.bind(this, function(evt)
  662. {
  663. this.graph.fireMouseEvent(mxEvent.MOUSE_UP, new mxMouseEvent(evt));
  664. })
  665. );
  666. }
  667. }
  668. this.redrawBackgroundImage(this.backgroundImage, bg);
  669. }
  670. else if (this.backgroundImage != null)
  671. {
  672. this.backgroundImage.destroy();
  673. this.backgroundImage = null;
  674. }
  675. };
  676. /**
  677. * Function: validateBackgroundPage
  678. *
  679. * Validates the background page.
  680. */
  681. mxGraphView.prototype.validateBackgroundPage = function()
  682. {
  683. if (this.graph.pageVisible)
  684. {
  685. var bounds = this.getBackgroundPageBounds();
  686. if (this.backgroundPageShape == null)
  687. {
  688. this.backgroundPageShape = this.createBackgroundPageShape(bounds);
  689. this.backgroundPageShape.scale = this.scale;
  690. this.backgroundPageShape.isShadow = true;
  691. this.backgroundPageShape.dialect = this.graph.dialect;
  692. this.backgroundPageShape.init(this.backgroundPane);
  693. this.backgroundPageShape.redraw();
  694. // Adds listener for double click handling on background
  695. if (this.graph.nativeDblClickEnabled)
  696. {
  697. mxEvent.addListener(this.backgroundPageShape.node, 'dblclick', mxUtils.bind(this, function(evt)
  698. {
  699. this.graph.dblClick(evt);
  700. }));
  701. }
  702. // Adds basic listeners for graph event dispatching outside of the
  703. // container and finishing the handling of a single gesture
  704. mxEvent.addGestureListeners(this.backgroundPageShape.node,
  705. mxUtils.bind(this, function(evt)
  706. {
  707. this.graph.fireMouseEvent(mxEvent.MOUSE_DOWN, new mxMouseEvent(evt));
  708. }),
  709. mxUtils.bind(this, function(evt)
  710. {
  711. // Hides the tooltip if mouse is outside container
  712. if (this.graph.tooltipHandler != null && this.graph.tooltipHandler.isHideOnHover())
  713. {
  714. this.graph.tooltipHandler.hide();
  715. }
  716. if (this.graph.isMouseDown && !mxEvent.isConsumed(evt))
  717. {
  718. this.graph.fireMouseEvent(mxEvent.MOUSE_MOVE, new mxMouseEvent(evt));
  719. }
  720. }),
  721. mxUtils.bind(this, function(evt)
  722. {
  723. this.graph.fireMouseEvent(mxEvent.MOUSE_UP, new mxMouseEvent(evt));
  724. })
  725. );
  726. }
  727. else
  728. {
  729. this.backgroundPageShape.scale = this.scale;
  730. this.backgroundPageShape.bounds = bounds;
  731. this.backgroundPageShape.redraw();
  732. }
  733. }
  734. else if (this.backgroundPageShape != null)
  735. {
  736. this.backgroundPageShape.destroy();
  737. this.backgroundPageShape = null;
  738. }
  739. };
  740. /**
  741. * Function: getBackgroundPageBounds
  742. *
  743. * Returns the bounds for the background page.
  744. */
  745. mxGraphView.prototype.getBackgroundPageBounds = function()
  746. {
  747. var fmt = this.graph.pageFormat;
  748. var ps = this.scale * this.graph.pageScale;
  749. var bounds = new mxRectangle(this.scale * this.translate.x, this.scale * this.translate.y,
  750. fmt.width * ps, fmt.height * ps);
  751. return bounds;
  752. };
  753. /**
  754. * Function: redrawBackgroundImage
  755. *
  756. * Updates the bounds and redraws the background image.
  757. *
  758. * Example:
  759. *
  760. * If the background image should not be scaled, this can be replaced with
  761. * the following.
  762. *
  763. * (code)
  764. * mxGraphView.prototype.redrawBackground = function(backgroundImage, bg)
  765. * {
  766. * backgroundImage.bounds.x = this.translate.x;
  767. * backgroundImage.bounds.y = this.translate.y;
  768. * backgroundImage.bounds.width = bg.width;
  769. * backgroundImage.bounds.height = bg.height;
  770. *
  771. * backgroundImage.redraw();
  772. * };
  773. * (end)
  774. *
  775. * Parameters:
  776. *
  777. * backgroundImage - <mxImageShape> that represents the background image.
  778. * bg - <mxImage> that specifies the image and its dimensions.
  779. */
  780. mxGraphView.prototype.redrawBackgroundImage = function(backgroundImage, bg)
  781. {
  782. backgroundImage.scale = this.scale;
  783. backgroundImage.bounds.x = this.scale * this.translate.x;
  784. backgroundImage.bounds.y = this.scale * this.translate.y;
  785. backgroundImage.bounds.width = this.scale * bg.width;
  786. backgroundImage.bounds.height = this.scale * bg.height;
  787. backgroundImage.redraw();
  788. };
  789. /**
  790. * Function: validateCell
  791. *
  792. * Recursively creates the cell state for the given cell if visible is true and
  793. * the given cell is visible. If the cell is not visible but the state exists
  794. * then it is removed using <removeState>.
  795. *
  796. * Parameters:
  797. *
  798. * cell - <mxCell> whose <mxCellState> should be created.
  799. * visible - Optional boolean indicating if the cell should be visible. Default
  800. * is true.
  801. */
  802. mxGraphView.prototype.validateCell = function(cell, visible)
  803. {
  804. visible = (visible != null) ? visible : true;
  805. if (cell != null)
  806. {
  807. visible = visible && this.graph.isCellVisible(cell);
  808. var state = this.getState(cell, visible);
  809. if (state != null && !visible)
  810. {
  811. this.removeState(cell);
  812. }
  813. else
  814. {
  815. var model = this.graph.getModel();
  816. var childCount = model.getChildCount(cell);
  817. for (var i = 0; i < childCount; i++)
  818. {
  819. this.validateCell(model.getChildAt(cell, i), visible &&
  820. (!this.isCellCollapsed(cell) || cell == this.currentRoot));
  821. }
  822. }
  823. }
  824. return cell;
  825. };
  826. /**
  827. * Function: validateCellState
  828. *
  829. * Validates and repaints the <mxCellState> for the given <mxCell>.
  830. *
  831. * Parameters:
  832. *
  833. * cell - <mxCell> whose <mxCellState> should be validated.
  834. * recurse - Optional boolean indicating if the children of the cell should be
  835. * validated. Default is true.
  836. */
  837. mxGraphView.prototype.validateCellState = function(cell, recurse)
  838. {
  839. recurse = (recurse != null) ? recurse : true;
  840. var state = null;
  841. if (cell != null)
  842. {
  843. state = this.getState(cell);
  844. if (state != null)
  845. {
  846. var model = this.graph.getModel();
  847. if (state.invalid)
  848. {
  849. state.invalid = false;
  850. if (state.style == null || state.invalidStyle)
  851. {
  852. state.style = this.graph.getCellStyle(state.cell);
  853. state.invalidStyle = false;
  854. }
  855. if (cell != this.currentRoot)
  856. {
  857. this.validateCellState(model.getParent(cell), false);
  858. }
  859. state.setVisibleTerminalState(this.validateCellState(this.getVisibleTerminal(cell, true), false), true);
  860. state.setVisibleTerminalState(this.validateCellState(this.getVisibleTerminal(cell, false), false), false);
  861. this.updateCellState(state);
  862. // Repaint happens immediately after the cell is validated
  863. if (cell != this.currentRoot && !state.invalid)
  864. {
  865. this.graph.cellRenderer.redraw(state, false, this.isRendering());
  866. // Handles changes to invertex paintbounds after update of rendering shape
  867. state.updateCachedBounds();
  868. }
  869. }
  870. if (recurse && !state.invalid)
  871. {
  872. // Updates order in DOM if recursively traversing
  873. if (state.shape != null)
  874. {
  875. this.stateValidated(state);
  876. }
  877. var childCount = model.getChildCount(cell);
  878. for (var i = 0; i < childCount; i++)
  879. {
  880. this.validateCellState(model.getChildAt(cell, i));
  881. }
  882. }
  883. }
  884. }
  885. return state;
  886. };
  887. /**
  888. * Function: updateCellState
  889. *
  890. * Updates the given <mxCellState>.
  891. *
  892. * Parameters:
  893. *
  894. * state - <mxCellState> to be updated.
  895. */
  896. mxGraphView.prototype.updateCellState = function(state)
  897. {
  898. state.absoluteOffset.x = 0;
  899. state.absoluteOffset.y = 0;
  900. state.origin.x = 0;
  901. state.origin.y = 0;
  902. state.length = 0;
  903. if (state.cell != this.currentRoot)
  904. {
  905. var model = this.graph.getModel();
  906. var pState = this.getState(model.getParent(state.cell));
  907. if (pState != null && pState.cell != this.currentRoot)
  908. {
  909. state.origin.x += pState.origin.x;
  910. state.origin.y += pState.origin.y;
  911. }
  912. var offset = this.graph.getChildOffsetForCell(state.cell);
  913. if (offset != null)
  914. {
  915. state.origin.x += offset.x;
  916. state.origin.y += offset.y;
  917. }
  918. var geo = this.graph.getCellGeometry(state.cell);
  919. if (geo != null)
  920. {
  921. if (!model.isEdge(state.cell))
  922. {
  923. offset = (geo.offset != null) ? geo.offset : this.EMPTY_POINT;
  924. if (geo.relative && pState != null)
  925. {
  926. if (model.isEdge(pState.cell))
  927. {
  928. var origin = this.getPoint(pState, geo);
  929. if (origin != null)
  930. {
  931. state.origin.x += (origin.x / this.scale) - pState.origin.x - this.translate.x;
  932. state.origin.y += (origin.y / this.scale) - pState.origin.y - this.translate.y;
  933. }
  934. }
  935. else
  936. {
  937. state.origin.x += geo.x * pState.unscaledWidth + offset.x;
  938. state.origin.y += geo.y * pState.unscaledHeight + offset.y;
  939. }
  940. }
  941. else
  942. {
  943. state.absoluteOffset.x = this.scale * offset.x;
  944. state.absoluteOffset.y = this.scale * offset.y;
  945. state.origin.x += geo.x;
  946. state.origin.y += geo.y;
  947. }
  948. }
  949. state.x = this.scale * (this.translate.x + state.origin.x);
  950. state.y = this.scale * (this.translate.y + state.origin.y);
  951. state.width = this.scale * geo.width;
  952. state.unscaledWidth = geo.width;
  953. state.height = this.scale * geo.height;
  954. state.unscaledHeight = geo.height;
  955. if (model.isVertex(state.cell))
  956. {
  957. this.updateVertexState(state, geo);
  958. }
  959. if (model.isEdge(state.cell))
  960. {
  961. this.updateEdgeState(state, geo);
  962. }
  963. }
  964. }
  965. state.updateCachedBounds();
  966. };
  967. /**
  968. * Function: isCellCollapsed
  969. *
  970. * Returns true if the children of the given cell should not be visible in the
  971. * view. This implementation uses <mxGraph.isCellVisible> but it can be
  972. * overidden to use a separate condition.
  973. */
  974. mxGraphView.prototype.isCellCollapsed = function(cell)
  975. {
  976. return this.graph.isCellCollapsed(cell);
  977. };
  978. /**
  979. * Function: updateVertexState
  980. *
  981. * Validates the given cell state.
  982. */
  983. mxGraphView.prototype.updateVertexState = function(state, geo)
  984. {
  985. var model = this.graph.getModel();
  986. var pState = this.getState(model.getParent(state.cell));
  987. if (geo.relative && pState != null && !model.isEdge(pState.cell))
  988. {
  989. var alpha = mxUtils.toRadians(pState.style[mxConstants.STYLE_ROTATION] || '0');
  990. if (alpha != 0)
  991. {
  992. var cos = Math.cos(alpha);
  993. var sin = Math.sin(alpha);
  994. var ct = new mxPoint(state.getCenterX(), state.getCenterY());
  995. var cx = new mxPoint(pState.getCenterX(), pState.getCenterY());
  996. var pt = mxUtils.getRotatedPoint(ct, cos, sin, cx);
  997. state.x = pt.x - state.width / 2;
  998. state.y = pt.y - state.height / 2;
  999. }
  1000. }
  1001. this.updateVertexLabelOffset(state);
  1002. };
  1003. /**
  1004. * Function: updateEdgeState
  1005. *
  1006. * Validates the given cell state.
  1007. */
  1008. mxGraphView.prototype.updateEdgeState = function(state, geo)
  1009. {
  1010. var source = state.getVisibleTerminalState(true);
  1011. var target = state.getVisibleTerminalState(false);
  1012. // This will remove edges with no terminals and no terminal points
  1013. // as such edges are invalid and produce NPEs in the edge styles.
  1014. // Also removes connected edges that have no visible terminals.
  1015. if ((this.graph.model.getTerminal(state.cell, true) != null && source == null) ||
  1016. (source == null && geo.getTerminalPoint(true) == null) ||
  1017. (this.graph.model.getTerminal(state.cell, false) != null && target == null) ||
  1018. (target == null && geo.getTerminalPoint(false) == null))
  1019. {
  1020. this.clear(state.cell, true);
  1021. }
  1022. else
  1023. {
  1024. this.updateFixedTerminalPoints(state, source, target);
  1025. this.updatePoints(state, geo.points, source, target);
  1026. this.updateFloatingTerminalPoints(state, source, target);
  1027. var pts = state.absolutePoints;
  1028. if (state.cell != this.currentRoot && (pts == null || pts.length < 2 ||
  1029. pts[0] == null || pts[pts.length - 1] == null))
  1030. {
  1031. // This will remove edges with invalid points from the list of states in the view.
  1032. // Happens if the one of the terminals and the corresponding terminal point is null.
  1033. this.clear(state.cell, true);
  1034. }
  1035. else
  1036. {
  1037. this.updateEdgeBounds(state);
  1038. this.updateEdgeLabelOffset(state);
  1039. }
  1040. }
  1041. };
  1042. /**
  1043. * Function: updateVertexLabelOffset
  1044. *
  1045. * Updates the absoluteOffset of the given vertex cell state. This takes
  1046. * into account the label position styles.
  1047. *
  1048. * Parameters:
  1049. *
  1050. * state - <mxCellState> whose absolute offset should be updated.
  1051. */
  1052. mxGraphView.prototype.updateVertexLabelOffset = function(state)
  1053. {
  1054. var h = mxUtils.getValue(state.style, mxConstants.STYLE_LABEL_POSITION, mxConstants.ALIGN_CENTER);
  1055. if (h == mxConstants.ALIGN_LEFT)
  1056. {
  1057. var lw = mxUtils.getValue(state.style, mxConstants.STYLE_LABEL_WIDTH, null);
  1058. if (lw != null)
  1059. {
  1060. lw *= this.scale;
  1061. }
  1062. else
  1063. {
  1064. lw = state.width;
  1065. }
  1066. state.absoluteOffset.x -= lw;
  1067. }
  1068. else if (h == mxConstants.ALIGN_RIGHT)
  1069. {
  1070. state.absoluteOffset.x += state.width;
  1071. }
  1072. else if (h == mxConstants.ALIGN_CENTER)
  1073. {
  1074. var lw = mxUtils.getValue(state.style, mxConstants.STYLE_LABEL_WIDTH, null);
  1075. if (lw != null)
  1076. {
  1077. // Aligns text block with given width inside the vertex width
  1078. var align = mxUtils.getValue(state.style, mxConstants.STYLE_ALIGN, mxConstants.ALIGN_CENTER);
  1079. var dx = 0;
  1080. if (align == mxConstants.ALIGN_CENTER)
  1081. {
  1082. dx = 0.5;
  1083. }
  1084. else if (align == mxConstants.ALIGN_RIGHT)
  1085. {
  1086. dx = 1;
  1087. }
  1088. if (dx != 0)
  1089. {
  1090. state.absoluteOffset.x -= (lw * this.scale - state.width) * dx;
  1091. }
  1092. }
  1093. }
  1094. var v = mxUtils.getValue(state.style, mxConstants.STYLE_VERTICAL_LABEL_POSITION, mxConstants.ALIGN_MIDDLE);
  1095. if (v == mxConstants.ALIGN_TOP)
  1096. {
  1097. state.absoluteOffset.y -= state.height;
  1098. }
  1099. else if (v == mxConstants.ALIGN_BOTTOM)
  1100. {
  1101. state.absoluteOffset.y += state.height;
  1102. }
  1103. };
  1104. /**
  1105. * Function: resetValidationState
  1106. *
  1107. * Resets the current validation state.
  1108. */
  1109. mxGraphView.prototype.resetValidationState = function()
  1110. {
  1111. this.lastNode = null;
  1112. this.lastHtmlNode = null;
  1113. this.lastForegroundNode = null;
  1114. this.lastForegroundHtmlNode = null;
  1115. };
  1116. /**
  1117. * Function: stateValidated
  1118. *
  1119. * Invoked when a state has been processed in <validatePoints>. This is used
  1120. * to update the order of the DOM nodes of the shape.
  1121. *
  1122. * Parameters:
  1123. *
  1124. * state - <mxCellState> that represents the cell state.
  1125. */
  1126. mxGraphView.prototype.stateValidated = function(state)
  1127. {
  1128. var fg = (this.graph.getModel().isEdge(state.cell) && this.graph.keepEdgesInForeground) ||
  1129. (this.graph.getModel().isVertex(state.cell) && this.graph.keepEdgesInBackground);
  1130. var htmlNode = (fg) ? this.lastForegroundHtmlNode || this.lastHtmlNode : this.lastHtmlNode;
  1131. var node = (fg) ? this.lastForegroundNode || this.lastNode : this.lastNode;
  1132. var result = this.graph.cellRenderer.insertStateAfter(state, node, htmlNode);
  1133. if (fg)
  1134. {
  1135. this.lastForegroundHtmlNode = result[1];
  1136. this.lastForegroundNode = result[0];
  1137. }
  1138. else
  1139. {
  1140. this.lastHtmlNode = result[1];
  1141. this.lastNode = result[0];
  1142. }
  1143. };
  1144. /**
  1145. * Function: updateFixedTerminalPoints
  1146. *
  1147. * Sets the initial absolute terminal points in the given state before the edge
  1148. * style is computed.
  1149. *
  1150. * Parameters:
  1151. *
  1152. * edge - <mxCellState> whose initial terminal points should be updated.
  1153. * source - <mxCellState> which represents the source terminal.
  1154. * target - <mxCellState> which represents the target terminal.
  1155. */
  1156. mxGraphView.prototype.updateFixedTerminalPoints = function(edge, source, target)
  1157. {
  1158. this.updateFixedTerminalPoint(edge, source, true,
  1159. this.graph.getConnectionConstraint(edge, source, true));
  1160. this.updateFixedTerminalPoint(edge, target, false,
  1161. this.graph.getConnectionConstraint(edge, target, false));
  1162. };
  1163. /**
  1164. * Function: updateFixedTerminalPoint
  1165. *
  1166. * Sets the fixed source or target terminal point on the given edge.
  1167. *
  1168. * Parameters:
  1169. *
  1170. * edge - <mxCellState> whose terminal point should be updated.
  1171. * terminal - <mxCellState> which represents the actual terminal.
  1172. * source - Boolean that specifies if the terminal is the source.
  1173. * constraint - <mxConnectionConstraint> that specifies the connection.
  1174. */
  1175. mxGraphView.prototype.updateFixedTerminalPoint = function(edge, terminal, source, constraint)
  1176. {
  1177. edge.setAbsoluteTerminalPoint(this.getFixedTerminalPoint(edge, terminal, source, constraint), source);
  1178. };
  1179. /**
  1180. * Function: getFixedTerminalPoint
  1181. *
  1182. * Returns the fixed source or target terminal point for the given edge.
  1183. *
  1184. * Parameters:
  1185. *
  1186. * edge - <mxCellState> whose terminal point should be returned.
  1187. * terminal - <mxCellState> which represents the actual terminal.
  1188. * source - Boolean that specifies if the terminal is the source.
  1189. * constraint - <mxConnectionConstraint> that specifies the connection.
  1190. */
  1191. mxGraphView.prototype.getFixedTerminalPoint = function(edge, terminal, source, constraint)
  1192. {
  1193. var pt = null;
  1194. if (constraint != null)
  1195. {
  1196. pt = this.graph.getConnectionPoint(terminal, constraint, false); // FIXME Rounding introduced bugs when calculating label positions -> , this.graph.isOrthogonal(edge));
  1197. }
  1198. if (pt == null && terminal == null)
  1199. {
  1200. var s = this.scale;
  1201. var tr = this.translate;
  1202. var orig = edge.origin;
  1203. var geo = this.graph.getCellGeometry(edge.cell);
  1204. pt = geo.getTerminalPoint(source);
  1205. if (pt != null)
  1206. {
  1207. pt = new mxPoint(s * (tr.x + pt.x + orig.x),
  1208. s * (tr.y + pt.y + orig.y));
  1209. }
  1210. }
  1211. return pt;
  1212. };
  1213. /**
  1214. * Function: updateBoundsFromStencil
  1215. *
  1216. * Updates the bounds of the given cell state to reflect the bounds of the stencil
  1217. * if it has a fixed aspect and returns the previous bounds as an <mxRectangle> if
  1218. * the bounds have been modified or null otherwise.
  1219. *
  1220. * Parameters:
  1221. *
  1222. * edge - <mxCellState> whose bounds should be updated.
  1223. */
  1224. mxGraphView.prototype.updateBoundsFromStencil = function(state)
  1225. {
  1226. var previous = null;
  1227. if (state != null && state.shape != null && state.shape.stencil != null && state.shape.stencil.aspect == 'fixed')
  1228. {
  1229. previous = mxRectangle.fromRectangle(state);
  1230. var asp = state.shape.stencil.computeAspect(state.style, state.x, state.y, state.width, state.height);
  1231. state.setRect(asp.x, asp.y, state.shape.stencil.w0 * asp.width, state.shape.stencil.h0 * asp.height);
  1232. }
  1233. return previous;
  1234. };
  1235. /**
  1236. * Function: updatePoints
  1237. *
  1238. * Updates the absolute points in the given state using the specified array
  1239. * of <mxPoints> as the relative points.
  1240. *
  1241. * Parameters:
  1242. *
  1243. * edge - <mxCellState> whose absolute points should be updated.
  1244. * points - Array of <mxPoints> that constitute the relative points.
  1245. * source - <mxCellState> that represents the source terminal.
  1246. * target - <mxCellState> that represents the target terminal.
  1247. */
  1248. mxGraphView.prototype.updatePoints = function(edge, points, source, target)
  1249. {
  1250. if (edge != null)
  1251. {
  1252. var pts = [];
  1253. pts.push(edge.absolutePoints[0]);
  1254. var edgeStyle = this.getEdgeStyle(edge, points, source, target);
  1255. if (edgeStyle != null)
  1256. {
  1257. var src = this.getTerminalPort(edge, source, true);
  1258. var trg = this.getTerminalPort(edge, target, false);
  1259. // Uses the stencil bounds for routing and restores after routing
  1260. var srcBounds = this.updateBoundsFromStencil(src);
  1261. var trgBounds = this.updateBoundsFromStencil(trg);
  1262. edgeStyle(edge, src, trg, points, pts);
  1263. // Restores previous bounds
  1264. if (srcBounds != null)
  1265. {
  1266. src.setRect(srcBounds.x, srcBounds.y, srcBounds.width, srcBounds.height);
  1267. }
  1268. if (trgBounds != null)
  1269. {
  1270. trg.setRect(trgBounds.x, trgBounds.y, trgBounds.width, trgBounds.height);
  1271. }
  1272. }
  1273. else if (points != null)
  1274. {
  1275. for (var i = 0; i < points.length; i++)
  1276. {
  1277. if (points[i] != null)
  1278. {
  1279. var pt = mxUtils.clone(points[i]);
  1280. pts.push(this.transformControlPoint(edge, pt));
  1281. }
  1282. }
  1283. }
  1284. var tmp = edge.absolutePoints;
  1285. pts.push(tmp[tmp.length-1]);
  1286. edge.absolutePoints = pts;
  1287. }
  1288. };
  1289. /**
  1290. * Function: transformControlPoint
  1291. *
  1292. * Transforms the given control point to an absolute point.
  1293. */
  1294. mxGraphView.prototype.transformControlPoint = function(state, pt, ignoreScale)
  1295. {
  1296. if (state != null && pt != null)
  1297. {
  1298. var orig = state.origin;
  1299. var scale = ignoreScale ? 1 : this.scale
  1300. return new mxPoint(scale * (pt.x + this.translate.x + orig.x),
  1301. scale * (pt.y + this.translate.y + orig.y));
  1302. }
  1303. return null;
  1304. };
  1305. /**
  1306. * Function: isLoopStyleEnabled
  1307. *
  1308. * Returns true if the given edge should be routed with <mxGraph.defaultLoopStyle>
  1309. * or the <mxConstants.STYLE_LOOP> defined for the given edge. This implementation
  1310. * returns true if the given edge is a loop and does not have connections constraints
  1311. * associated.
  1312. */
  1313. mxGraphView.prototype.isLoopStyleEnabled = function(edge, points, source, target)
  1314. {
  1315. var sc = this.graph.getConnectionConstraint(edge, source, true);
  1316. var tc = this.graph.getConnectionConstraint(edge, target, false);
  1317. if ((points == null || points.length < 2) &&
  1318. (!mxUtils.getValue(edge.style, mxConstants.STYLE_ORTHOGONAL_LOOP, false) ||
  1319. ((sc == null || sc.point == null) && (tc == null || tc.point == null))))
  1320. {
  1321. return source != null && source == target;
  1322. }
  1323. return false;
  1324. };
  1325. /**
  1326. * Function: getEdgeStyle
  1327. *
  1328. * Returns the edge style function to be used to render the given edge state.
  1329. */
  1330. mxGraphView.prototype.getEdgeStyle = function(edge, points, source, target)
  1331. {
  1332. var edgeStyle = this.isLoopStyleEnabled(edge, points, source, target) ?
  1333. mxUtils.getValue(edge.style, mxConstants.STYLE_LOOP, this.graph.defaultLoopStyle) :
  1334. (!mxUtils.getValue(edge.style, mxConstants.STYLE_NOEDGESTYLE, false) ?
  1335. edge.style[mxConstants.STYLE_EDGE] : null);
  1336. // Converts string values to objects
  1337. if (typeof(edgeStyle) == "string")
  1338. {
  1339. var tmp = mxStyleRegistry.getValue(edgeStyle);
  1340. if (tmp == null && this.isAllowEval())
  1341. {
  1342. tmp = mxUtils.eval(edgeStyle);
  1343. }
  1344. edgeStyle = tmp;
  1345. }
  1346. if (typeof(edgeStyle) == "function")
  1347. {
  1348. return edgeStyle;
  1349. }
  1350. return null;
  1351. };
  1352. /**
  1353. * Function: updateFloatingTerminalPoints
  1354. *
  1355. * Updates the terminal points in the given state after the edge style was
  1356. * computed for the edge.
  1357. *
  1358. * Parameters:
  1359. *
  1360. * state - <mxCellState> whose terminal points should be updated.
  1361. * source - <mxCellState> that represents the source terminal.
  1362. * target - <mxCellState> that represents the target terminal.
  1363. */
  1364. mxGraphView.prototype.updateFloatingTerminalPoints = function(state, source, target)
  1365. {
  1366. var pts = state.absolutePoints;
  1367. var p0 = pts[0];
  1368. var pe = pts[pts.length - 1];
  1369. if (pe == null && target != null)
  1370. {
  1371. this.updateFloatingTerminalPoint(state, target, source, false);
  1372. }
  1373. if (p0 == null && source != null)
  1374. {
  1375. this.updateFloatingTerminalPoint(state, source, target, true);
  1376. }
  1377. };
  1378. /**
  1379. * Function: updateFloatingTerminalPoint
  1380. *
  1381. * Updates the absolute terminal point in the given state for the given
  1382. * start and end state, where start is the source if source is true.
  1383. *
  1384. * Parameters:
  1385. *
  1386. * edge - <mxCellState> whose terminal point should be updated.
  1387. * start - <mxCellState> for the terminal on "this" side of the edge.
  1388. * end - <mxCellState> for the terminal on the other side of the edge.
  1389. * source - Boolean indicating if start is the source terminal state.
  1390. */
  1391. mxGraphView.prototype.updateFloatingTerminalPoint = function(edge, start, end, source)
  1392. {
  1393. edge.setAbsoluteTerminalPoint(this.getFloatingTerminalPoint(edge, start, end, source), source);
  1394. };
  1395. /**
  1396. * Function: getFloatingTerminalPoint
  1397. *
  1398. * Returns the floating terminal point for the given edge, start and end
  1399. * state, where start is the source if source is true.
  1400. *
  1401. * Parameters:
  1402. *
  1403. * edge - <mxCellState> whose terminal point should be returned.
  1404. * start - <mxCellState> for the terminal on "this" side of the edge.
  1405. * end - <mxCellState> for the terminal on the other side of the edge.
  1406. * source - Boolean indicating if start is the source terminal state.
  1407. */
  1408. mxGraphView.prototype.getFloatingTerminalPoint = function(edge, start, end, source)
  1409. {
  1410. start = this.getTerminalPort(edge, start, source);
  1411. var next = this.getNextPoint(edge, end, source);
  1412. var orth = this.graph.isOrthogonal(edge);
  1413. var alpha = mxUtils.toRadians(Number(start.style[mxConstants.STYLE_ROTATION] || '0'));
  1414. var center = new mxPoint(start.getCenterX(), start.getCenterY());
  1415. if (alpha != 0)
  1416. {
  1417. var cos = Math.cos(-alpha);
  1418. var sin = Math.sin(-alpha);
  1419. next = mxUtils.getRotatedPoint(next, cos, sin, center);
  1420. }
  1421. var border = parseFloat(edge.style[mxConstants.STYLE_PERIMETER_SPACING] || 0);
  1422. border += parseFloat(edge.style[(source) ?
  1423. mxConstants.STYLE_SOURCE_PERIMETER_SPACING :
  1424. mxConstants.STYLE_TARGET_PERIMETER_SPACING] || 0);
  1425. var pt = this.getPerimeterPoint(start, next, alpha == 0 && orth, border);
  1426. if (alpha != 0)
  1427. {
  1428. var cos = Math.cos(alpha);
  1429. var sin = Math.sin(alpha);
  1430. pt = mxUtils.getRotatedPoint(pt, cos, sin, center);
  1431. }
  1432. return pt;
  1433. };
  1434. /**
  1435. * Function: getTerminalPort
  1436. *
  1437. * Returns an <mxCellState> that represents the source or target terminal or
  1438. * port for the given edge.
  1439. *
  1440. * Parameters:
  1441. *
  1442. * state - <mxCellState> that represents the state of the edge.
  1443. * terminal - <mxCellState> that represents the terminal.
  1444. * source - Boolean indicating if the given terminal is the source terminal.
  1445. */
  1446. mxGraphView.prototype.getTerminalPort = function(state, terminal, source)
  1447. {
  1448. var key = (source) ? mxConstants.STYLE_SOURCE_PORT :
  1449. mxConstants.STYLE_TARGET_PORT;
  1450. var id = mxUtils.getValue(state.style, key);
  1451. if (id != null)
  1452. {
  1453. var tmp = this.getState(this.graph.getModel().getCell(id));
  1454. // Only uses ports where a cell state exists
  1455. if (tmp != null)
  1456. {
  1457. terminal = tmp;
  1458. }
  1459. }
  1460. return terminal;
  1461. };
  1462. /**
  1463. * Function: getPerimeterPoint
  1464. *
  1465. * Returns an <mxPoint> that defines the location of the intersection point between
  1466. * the perimeter and the line between the center of the shape and the given point.
  1467. *
  1468. * Parameters:
  1469. *
  1470. * terminal - <mxCellState> for the source or target terminal.
  1471. * next - <mxPoint> that lies outside of the given terminal.
  1472. * orthogonal - Boolean that specifies if the orthogonal projection onto
  1473. * the perimeter should be returned. If this is false then the intersection
  1474. * of the perimeter and the line between the next and the center point is
  1475. * returned.
  1476. * border - Optional border between the perimeter and the shape.
  1477. */
  1478. mxGraphView.prototype.getPerimeterPoint = function(terminal, next, orthogonal, border)
  1479. {
  1480. var point = null;
  1481. if (terminal != null)
  1482. {
  1483. var perimeter = this.getPerimeterFunction(terminal);
  1484. if (perimeter != null && next != null)
  1485. {
  1486. var bounds = this.getPerimeterBounds(terminal, border);
  1487. if (bounds.width > 0 || bounds.height > 0)
  1488. {
  1489. point = new mxPoint(next.x, next.y);
  1490. var flipH = false;
  1491. var flipV = false;
  1492. if (this.graph.model.isVertex(terminal.cell))
  1493. {
  1494. flipH = mxUtils.getValue(terminal.style, mxConstants.STYLE_FLIPH, 0) == 1;
  1495. flipV = mxUtils.getValue(terminal.style, mxConstants.STYLE_FLIPV, 0) == 1;
  1496. // Legacy support for stencilFlipH/V
  1497. if (terminal.shape != null && terminal.shape.stencil != null)
  1498. {
  1499. flipH = (mxUtils.getValue(terminal.style, 'stencilFlipH', 0) == 1) || flipH;
  1500. flipV = (mxUtils.getValue(terminal.style, 'stencilFlipV', 0) == 1) || flipV;
  1501. }
  1502. if (flipH)
  1503. {
  1504. point.x = 2 * bounds.getCenterX() - point.x;
  1505. }
  1506. if (flipV)
  1507. {
  1508. point.y = 2 * bounds.getCenterY() - point.y;
  1509. }
  1510. }
  1511. point = perimeter(bounds, terminal, point, orthogonal);
  1512. if (point != null)
  1513. {
  1514. if (flipH)
  1515. {
  1516. point.x = 2 * bounds.getCenterX() - point.x;
  1517. }
  1518. if (flipV)
  1519. {
  1520. point.y = 2 * bounds.getCenterY() - point.y;
  1521. }
  1522. }
  1523. }
  1524. }
  1525. if (point == null)
  1526. {
  1527. point = this.getPoint(terminal);
  1528. }
  1529. }
  1530. return point;
  1531. };
  1532. /**
  1533. * Function: getRoutingCenterX
  1534. *
  1535. * Returns the x-coordinate of the center point for automatic routing.
  1536. */
  1537. mxGraphView.prototype.getRoutingCenterX = function (state)
  1538. {
  1539. var f = (state.style != null) ? parseFloat(state.style
  1540. [mxConstants.STYLE_ROUTING_CENTER_X]) || 0 : 0;
  1541. return state.getCenterX() + f * state.width;
  1542. };
  1543. /**
  1544. * Function: getRoutingCenterY
  1545. *
  1546. * Returns the y-coordinate of the center point for automatic routing.
  1547. */
  1548. mxGraphView.prototype.getRoutingCenterY = function (state)
  1549. {
  1550. var f = (state.style != null) ? parseFloat(state.style
  1551. [mxConstants.STYLE_ROUTING_CENTER_Y]) || 0 : 0;
  1552. return state.getCenterY() + f * state.height;
  1553. };
  1554. /**
  1555. * Function: getPerimeterBounds
  1556. *
  1557. * Returns the perimeter bounds for the given terminal, edge pair as an
  1558. * <mxRectangle>.
  1559. *
  1560. * If you have a model where each terminal has a relative child that should
  1561. * act as the graphical endpoint for a connection from/to the terminal, then
  1562. * this method can be replaced as follows:
  1563. *
  1564. * (code)
  1565. * var oldGetPerimeterBounds = mxGraphView.prototype.getPerimeterBounds;
  1566. * mxGraphView.prototype.getPerimeterBounds = function(terminal, edge, isSource)
  1567. * {
  1568. * var model = this.graph.getModel();
  1569. * var childCount = model.getChildCount(terminal.cell);
  1570. *
  1571. * if (childCount > 0)
  1572. * {
  1573. * var child = model.getChildAt(terminal.cell, 0);
  1574. * var geo = model.getGeometry(child);
  1575. *
  1576. * if (geo != null &&
  1577. * geo.relative)
  1578. * {
  1579. * var state = this.getState(child);
  1580. *
  1581. * if (state != null)
  1582. * {
  1583. * terminal = state;
  1584. * }
  1585. * }
  1586. * }
  1587. *
  1588. * return oldGetPerimeterBounds.apply(this, arguments);
  1589. * };
  1590. * (end)
  1591. *
  1592. * Parameters:
  1593. *
  1594. * terminal - <mxCellState> that represents the terminal.
  1595. * border - Number that adds a border between the shape and the perimeter.
  1596. */
  1597. mxGraphView.prototype.getPerimeterBounds = function(terminal, border)
  1598. {
  1599. border = (border != null) ? border : 0;
  1600. if (terminal != null)
  1601. {
  1602. border += parseFloat(terminal.style[mxConstants.STYLE_PERIMETER_SPACING] || 0);
  1603. }
  1604. return terminal.getPerimeterBounds(border * this.scale);
  1605. };
  1606. /**
  1607. * Function: getPerimeterFunction
  1608. *
  1609. * Returns the perimeter function for the given state.
  1610. */
  1611. mxGraphView.prototype.getPerimeterFunction = function(state)
  1612. {
  1613. var perimeter = state.style[mxConstants.STYLE_PERIMETER];
  1614. // Converts string values to objects
  1615. if (typeof(perimeter) == "string")
  1616. {
  1617. var tmp = mxStyleRegistry.getValue(perimeter);
  1618. if (tmp == null && this.isAllowEval())
  1619. {
  1620. tmp = mxUtils.eval(perimeter);
  1621. }
  1622. perimeter = tmp;
  1623. }
  1624. if (typeof(perimeter) == "function")
  1625. {
  1626. return perimeter;
  1627. }
  1628. return null;
  1629. };
  1630. /**
  1631. * Function: getNextPoint
  1632. *
  1633. * Returns the nearest point in the list of absolute points or the center
  1634. * of the opposite terminal.
  1635. *
  1636. * Parameters:
  1637. *
  1638. * edge - <mxCellState> that represents the edge.
  1639. * opposite - <mxCellState> that represents the opposite terminal.
  1640. * source - Boolean indicating if the next point for the source or target
  1641. * should be returned.
  1642. */
  1643. mxGraphView.prototype.getNextPoint = function(edge, opposite, source)
  1644. {
  1645. var pts = edge.absolutePoints;
  1646. var point = null;
  1647. if (pts != null && pts.length >= 2)
  1648. {
  1649. var count = pts.length;
  1650. point = pts[(source) ? Math.min(1, count - 1) : Math.max(0, count - 2)];
  1651. }
  1652. if (point == null && opposite != null)
  1653. {
  1654. point = new mxPoint(opposite.getCenterX(), opposite.getCenterY());
  1655. }
  1656. return point;
  1657. };
  1658. /**
  1659. * Function: getVisibleTerminal
  1660. *
  1661. * Returns the nearest ancestor terminal that is visible. The edge appears
  1662. * to be connected to this terminal on the display. The result of this method
  1663. * is cached in <mxCellState.getVisibleTerminalState>.
  1664. *
  1665. * Parameters:
  1666. *
  1667. * edge - <mxCell> whose visible terminal should be returned.
  1668. * source - Boolean that specifies if the source or target terminal
  1669. * should be returned.
  1670. */
  1671. mxGraphView.prototype.getVisibleTerminal = function(edge, source)
  1672. {
  1673. var model = this.graph.getModel();
  1674. var result = model.getTerminal(edge, source);
  1675. var best = result;
  1676. while (result != null && result != this.currentRoot)
  1677. {
  1678. if (!this.graph.isCellVisible(best) || this.isCellCollapsed(result))
  1679. {
  1680. best = result;
  1681. }
  1682. result = model.getParent(result);
  1683. }
  1684. // Checks if the result is valid for the current view state
  1685. if (best != null && (!model.contains(best) ||
  1686. model.getParent(best) == model.getRoot() ||
  1687. best == this.currentRoot))
  1688. {
  1689. best = null;
  1690. }
  1691. return best;
  1692. };
  1693. /**
  1694. * Function: updateEdgeBounds
  1695. *
  1696. * Updates the given state using the bounding box of t
  1697. * he absolute points.
  1698. * Also updates <mxCellState.terminalDistance>, <mxCellState.length> and
  1699. * <mxCellState.segments>.
  1700. *
  1701. * Parameters:
  1702. *
  1703. * state - <mxCellState> whose bounds should be updated.
  1704. */
  1705. mxGraphView.prototype.updateEdgeBounds = function(state)
  1706. {
  1707. var points = state.absolutePoints;
  1708. var p0 = points[0];
  1709. var pe = points[points.length - 1];
  1710. if (p0.x != pe.x || p0.y != pe.y)
  1711. {
  1712. var dx = pe.x - p0.x;
  1713. var dy = pe.y - p0.y;
  1714. state.terminalDistance = Math.sqrt(dx * dx + dy * dy);
  1715. }
  1716. else
  1717. {
  1718. state.terminalDistance = 0;
  1719. }
  1720. var length = 0;
  1721. var segments = [];
  1722. var pt = p0;
  1723. if (pt != null)
  1724. {
  1725. var minX = pt.x;
  1726. var minY = pt.y;
  1727. var maxX = minX;
  1728. var maxY = minY;
  1729. for (var i = 1; i < points.length; i++)
  1730. {
  1731. var tmp = points[i];
  1732. if (tmp != null)
  1733. {
  1734. var dx = pt.x - tmp.x;
  1735. var dy = pt.y - tmp.y;
  1736. var segment = Math.sqrt(dx * dx + dy * dy);
  1737. segments.push(segment);
  1738. length += segment;
  1739. pt = tmp;
  1740. minX = Math.min(pt.x, minX);
  1741. minY = Math.min(pt.y, minY);
  1742. maxX = Math.max(pt.x, maxX);
  1743. maxY = Math.max(pt.y, maxY);
  1744. }
  1745. }
  1746. state.length = length;
  1747. state.segments = segments;
  1748. var markerSize = 1; // TODO: include marker size
  1749. state.x = minX;
  1750. state.y = minY;
  1751. state.width = Math.max(markerSize, maxX - minX);
  1752. state.height = Math.max(markerSize, maxY - minY);
  1753. }
  1754. };
  1755. /**
  1756. * Function: getPoint
  1757. *
  1758. * Returns the absolute point on the edge for the given relative
  1759. * <mxGeometry> as an <mxPoint>. The edge is represented by the given
  1760. * <mxCellState>.
  1761. *
  1762. * Parameters:
  1763. *
  1764. * state - <mxCellState> that represents the state of the parent edge.
  1765. * geometry - <mxGeometry> that represents the relative location.
  1766. */
  1767. mxGraphView.prototype.getPoint = function(state, geometry)
  1768. {
  1769. var x = state.getCenterX();
  1770. var y = state.getCenterY();
  1771. if (state.segments != null && (geometry == null || geometry.relative))
  1772. {
  1773. var gx = (geometry != null) ? geometry.x / 2 : 0;
  1774. var pointCount = state.absolutePoints.length;
  1775. var dist = Math.round((gx + 0.5) * state.length);
  1776. var segment = state.segments[0];
  1777. var length = 0;
  1778. var index = 1;
  1779. while (dist >= Math.round(length + segment) && index < pointCount - 1)
  1780. {
  1781. length += segment;
  1782. segment = state.segments[index++];
  1783. }
  1784. var factor = (segment == 0) ? 0 : (dist - length) / segment;
  1785. var p0 = state.absolutePoints[index-1];
  1786. var pe = state.absolutePoints[index];
  1787. if (p0 != null && pe != null)
  1788. {
  1789. var gy = 0;
  1790. var offsetX = 0;
  1791. var offsetY = 0;
  1792. if (geometry != null)
  1793. {
  1794. gy = geometry.y;
  1795. var offset = geometry.offset;
  1796. if (offset != null)
  1797. {
  1798. offsetX = offset.x;
  1799. offsetY = offset.y;
  1800. }
  1801. }
  1802. var dx = pe.x - p0.x;
  1803. var dy = pe.y - p0.y;
  1804. var nx = (segment == 0) ? 0 : dy / segment;
  1805. var ny = (segment == 0) ? 0 : dx / segment;
  1806. x = p0.x + dx * factor + (nx * gy + offsetX) * this.scale;
  1807. y = p0.y + dy * factor - (ny * gy - offsetY) * this.scale;
  1808. }
  1809. }
  1810. else if (geometry != null)
  1811. {
  1812. var offset = geometry.offset;
  1813. if (offset != null)
  1814. {
  1815. x += offset.x;
  1816. y += offset.y;
  1817. }
  1818. }
  1819. return new mxPoint(x, y);
  1820. };
  1821. /**
  1822. * Function: getRelativePoint
  1823. *
  1824. * Gets the relative point that describes the given, absolute label
  1825. * position for the given edge state.
  1826. *
  1827. * Parameters:
  1828. *
  1829. * state - <mxCellState> that represents the state of the parent edge.
  1830. * x - Specifies the x-coordinate of the absolute label location.
  1831. * y - Specifies the y-coordinate of the absolute label location.
  1832. */
  1833. mxGraphView.prototype.getRelativePoint = function(edgeState, x, y)
  1834. {
  1835. var model = this.graph.getModel();
  1836. var geometry = model.getGeometry(edgeState.cell);
  1837. if (geometry != null)
  1838. {
  1839. var pointCount = edgeState.absolutePoints.length;
  1840. if (geometry.relative && pointCount > 1)
  1841. {
  1842. var totalLength = edgeState.length;
  1843. var segments = edgeState.segments;
  1844. // Works which line segment the point of the label is closest to
  1845. var p0 = edgeState.absolutePoints[0];
  1846. var pe = edgeState.absolutePoints[1];
  1847. var minDist = mxUtils.ptSegDistSq(p0.x, p0.y, pe.x, pe.y, x, y);
  1848. var index = 0;
  1849. var tmp = 0;
  1850. var length = 0;
  1851. for (var i = 2; i < pointCount; i++)
  1852. {
  1853. tmp += segments[i - 2];
  1854. pe = edgeState.absolutePoints[i];
  1855. var dist = mxUtils.ptSegDistSq(p0.x, p0.y, pe.x, pe.y, x, y);
  1856. if (dist <= minDist)
  1857. {
  1858. minDist = dist;
  1859. index = i - 1;
  1860. length = tmp;
  1861. }
  1862. p0 = pe;
  1863. }
  1864. var seg = segments[index];
  1865. p0 = edgeState.absolutePoints[index];
  1866. pe = edgeState.absolutePoints[index + 1];
  1867. var x2 = p0.x;
  1868. var y2 = p0.y;
  1869. var x1 = pe.x;
  1870. var y1 = pe.y;
  1871. var px = x;
  1872. var py = y;
  1873. var xSegment = x2 - x1;
  1874. var ySegment = y2 - y1;
  1875. px -= x1;
  1876. py -= y1;
  1877. var projlenSq = 0;
  1878. px = xSegment - px;
  1879. py = ySegment - py;
  1880. var dotprod = px * xSegment + py * ySegment;
  1881. if (dotprod <= 0.0)
  1882. {
  1883. projlenSq = 0;
  1884. }
  1885. else
  1886. {
  1887. projlenSq = dotprod * dotprod
  1888. / (xSegment * xSegment + ySegment * ySegment);
  1889. }
  1890. var projlen = Math.sqrt(projlenSq);
  1891. if (projlen > seg)
  1892. {
  1893. projlen = seg;
  1894. }
  1895. var yDistance = Math.sqrt(mxUtils.ptSegDistSq(p0.x, p0.y, pe
  1896. .x, pe.y, x, y));
  1897. var direction = mxUtils.relativeCcw(p0.x, p0.y, pe.x, pe.y, x, y);
  1898. if (direction == -1)
  1899. {
  1900. yDistance = -yDistance;
  1901. }
  1902. // Constructs the relative point for the label
  1903. return new mxPoint(((totalLength / 2 - length - projlen) / totalLength) * -2,
  1904. yDistance / this.scale);
  1905. }
  1906. }
  1907. return new mxPoint();
  1908. };
  1909. /**
  1910. * Function: updateEdgeLabelOffset
  1911. *
  1912. * Updates <mxCellState.absoluteOffset> for the given state. The absolute
  1913. * offset is normally used for the position of the edge label. Is is
  1914. * calculated from the geometry as an absolute offset from the center
  1915. * between the two endpoints if the geometry is absolute, or as the
  1916. * relative distance between the center along the line and the absolute
  1917. * orthogonal distance if the geometry is relative.
  1918. *
  1919. * Parameters:
  1920. *
  1921. * state - <mxCellState> whose absolute offset should be updated.
  1922. */
  1923. mxGraphView.prototype.updateEdgeLabelOffset = function(state)
  1924. {
  1925. var points = state.absolutePoints;
  1926. state.absoluteOffset.x = state.getCenterX();
  1927. state.absoluteOffset.y = state.getCenterY();
  1928. if (points != null && points.length > 0 && state.segments != null)
  1929. {
  1930. var geometry = this.graph.getCellGeometry(state.cell);
  1931. if (geometry.relative)
  1932. {
  1933. var offset = this.getPoint(state, geometry);
  1934. if (offset != null)
  1935. {
  1936. state.absoluteOffset = offset;
  1937. }
  1938. }
  1939. else
  1940. {
  1941. var p0 = points[0];
  1942. var pe = points[points.length - 1];
  1943. if (p0 != null && pe != null)
  1944. {
  1945. var dx = pe.x - p0.x;
  1946. var dy = pe.y - p0.y;
  1947. var x0 = 0;
  1948. var y0 = 0;
  1949. var off = geometry.offset;
  1950. if (off != null)
  1951. {
  1952. x0 = off.x;
  1953. y0 = off.y;
  1954. }
  1955. var x = p0.x + dx / 2 + x0 * this.scale;
  1956. var y = p0.y + dy / 2 + y0 * this.scale;
  1957. state.absoluteOffset.x = x;
  1958. state.absoluteOffset.y = y;
  1959. }
  1960. }
  1961. }
  1962. };
  1963. /**
  1964. * Function: getState
  1965. *
  1966. * Returns the <mxCellState> for the given cell. If create is true, then
  1967. * the state is created if it does not yet exist.
  1968. *
  1969. * Parameters:
  1970. *
  1971. * cell - <mxCell> for which the <mxCellState> should be returned.
  1972. * create - Optional boolean indicating if a new state should be created
  1973. * if it does not yet exist. Default is false.
  1974. */
  1975. mxGraphView.prototype.getState = function(cell, create)
  1976. {
  1977. create = create || false;
  1978. var state = null;
  1979. if (cell != null)
  1980. {
  1981. state = this.states.get(cell);
  1982. if (create && (state == null || this.updateStyle) && this.graph.isCellVisible(cell))
  1983. {
  1984. if (state == null)
  1985. {
  1986. state = this.createState(cell);
  1987. this.states.put(cell, state);
  1988. }
  1989. else
  1990. {
  1991. state.style = this.graph.getCellStyle(cell);
  1992. }
  1993. }
  1994. }
  1995. return state;
  1996. };
  1997. /**
  1998. * Function: isRendering
  1999. *
  2000. * Returns <rendering>.
  2001. */
  2002. mxGraphView.prototype.isRendering = function()
  2003. {
  2004. return this.rendering;
  2005. };
  2006. /**
  2007. * Function: setRendering
  2008. *
  2009. * Sets <rendering>.
  2010. */
  2011. mxGraphView.prototype.setRendering = function(value)
  2012. {
  2013. this.rendering = value;
  2014. };
  2015. /**
  2016. * Function: isAllowEval
  2017. *
  2018. * Returns <allowEval>.
  2019. */
  2020. mxGraphView.prototype.isAllowEval = function()
  2021. {
  2022. return this.allowEval;
  2023. };
  2024. /**
  2025. * Function: setAllowEval
  2026. *
  2027. * Sets <allowEval>.
  2028. */
  2029. mxGraphView.prototype.setAllowEval = function(value)
  2030. {
  2031. this.allowEval = value;
  2032. };
  2033. /**
  2034. * Function: getStates
  2035. *
  2036. * Returns <states>.
  2037. */
  2038. mxGraphView.prototype.getStates = function()
  2039. {
  2040. return this.states;
  2041. };
  2042. /**
  2043. * Function: setStates
  2044. *
  2045. * Sets <states>.
  2046. */
  2047. mxGraphView.prototype.setStates = function(value)
  2048. {
  2049. this.states = value;
  2050. };
  2051. /**
  2052. * Function: getCellStates
  2053. *
  2054. * Returns the <mxCellStates> for the given array of <mxCells>. The array
  2055. * contains all states that are not null, that is, the returned array may
  2056. * have less elements than the given array. If no argument is given, then
  2057. * this returns <states>.
  2058. */
  2059. mxGraphView.prototype.getCellStates = function(cells)
  2060. {
  2061. if (cells == null)
  2062. {
  2063. return this.states;
  2064. }
  2065. else
  2066. {
  2067. var result = [];
  2068. for (var i = 0; i < cells.length; i++)
  2069. {
  2070. var state = this.getState(cells[i]);
  2071. if (state != null)
  2072. {
  2073. result.push(state);
  2074. }
  2075. }
  2076. return result;
  2077. }
  2078. };
  2079. /**
  2080. * Function: removeState
  2081. *
  2082. * Removes and returns the <mxCellState> for the given cell.
  2083. *
  2084. * Parameters:
  2085. *
  2086. * cell - <mxCell> for which the <mxCellState> should be removed.
  2087. */
  2088. mxGraphView.prototype.removeState = function(cell)
  2089. {
  2090. var state = null;
  2091. if (cell != null)
  2092. {
  2093. state = this.states.remove(cell);
  2094. if (state != null)
  2095. {
  2096. this.graph.cellRenderer.destroy(state);
  2097. state.invalid = true;
  2098. state.destroy();
  2099. }
  2100. }
  2101. return state;
  2102. };
  2103. /**
  2104. * Function: createState
  2105. *
  2106. * Creates and returns an <mxCellState> for the given cell and initializes
  2107. * it using <mxCellRenderer.initialize>.
  2108. *
  2109. * Parameters:
  2110. *
  2111. * cell - <mxCell> for which a new <mxCellState> should be created.
  2112. */
  2113. mxGraphView.prototype.createState = function(cell)
  2114. {
  2115. return new mxCellState(this, cell, this.graph.getCellStyle(cell));
  2116. };
  2117. /**
  2118. * Function: getCanvas
  2119. *
  2120. * Returns the DOM node that contains the background-, draw- and
  2121. * overlay- and decoratorpanes.
  2122. */
  2123. mxGraphView.prototype.getCanvas = function()
  2124. {
  2125. return this.canvas;
  2126. };
  2127. /**
  2128. * Function: getBackgroundPane
  2129. *
  2130. * Returns the DOM node that represents the background layer.
  2131. */
  2132. mxGraphView.prototype.getBackgroundPane = function()
  2133. {
  2134. return this.backgroundPane;
  2135. };
  2136. /**
  2137. * Function: getDrawPane
  2138. *
  2139. * Returns the DOM node that represents the main drawing layer.
  2140. */
  2141. mxGraphView.prototype.getDrawPane = function()
  2142. {
  2143. return this.drawPane;
  2144. };
  2145. /**
  2146. * Function: getOverlayPane
  2147. *
  2148. * Returns the DOM node that represents the layer above the drawing layer.
  2149. */
  2150. mxGraphView.prototype.getOverlayPane = function()
  2151. {
  2152. return this.overlayPane;
  2153. };
  2154. /**
  2155. * Function: getDecoratorPane
  2156. *
  2157. * Returns the DOM node that represents the topmost drawing layer.
  2158. */
  2159. mxGraphView.prototype.getDecoratorPane = function()
  2160. {
  2161. return this.decoratorPane;
  2162. };
  2163. /**
  2164. * Function: isContainerEvent
  2165. *
  2166. * Returns true if the event origin is one of the drawing panes or
  2167. * containers of the view.
  2168. */
  2169. mxGraphView.prototype.isContainerEvent = function(evt)
  2170. {
  2171. var source = mxEvent.getSource(evt);
  2172. return (source == this.graph.container ||
  2173. source.parentNode == this.backgroundPane ||
  2174. (source.parentNode != null &&
  2175. source.parentNode.parentNode == this.backgroundPane) ||
  2176. source == this.canvas.parentNode ||
  2177. source == this.canvas ||
  2178. source == this.backgroundPane ||
  2179. source == this.drawPane ||
  2180. source == this.overlayPane ||
  2181. source == this.decoratorPane);
  2182. };
  2183. /**
  2184. * Function: isScrollEvent
  2185. *
  2186. * Returns true if the event origin is one of the scrollbars of the
  2187. * container in IE. Such events are ignored.
  2188. */
  2189. mxGraphView.prototype.isScrollEvent = function(evt)
  2190. {
  2191. var offset = mxUtils.getOffset(this.graph.container);
  2192. var pt = new mxPoint(evt.clientX - offset.x, evt.clientY - offset.y);
  2193. var outWidth = this.graph.container.offsetWidth;
  2194. var inWidth = this.graph.container.clientWidth;
  2195. if (outWidth > inWidth && pt.x > inWidth + 2 && pt.x <= outWidth)
  2196. {
  2197. return true;
  2198. }
  2199. var outHeight = this.graph.container.offsetHeight;
  2200. var inHeight = this.graph.container.clientHeight;
  2201. if (outHeight > inHeight && pt.y > inHeight + 2 && pt.y <= outHeight)
  2202. {
  2203. return true;
  2204. }
  2205. return false;
  2206. };
  2207. /**
  2208. * Function: init
  2209. *
  2210. * Initializes the graph event dispatch loop for the specified container
  2211. * and invokes <create> to create the required DOM nodes for the display.
  2212. */
  2213. mxGraphView.prototype.init = function()
  2214. {
  2215. this.installListeners();
  2216. // Creates the DOM nodes for the respective display dialect
  2217. var graph = this.graph;
  2218. if (graph.dialect == mxConstants.DIALECT_SVG)
  2219. {
  2220. this.createSvg();
  2221. }
  2222. else if (graph.dialect == mxConstants.DIALECT_VML)
  2223. {
  2224. this.createVml();
  2225. }
  2226. else
  2227. {
  2228. this.createHtml();
  2229. }
  2230. };
  2231. /**
  2232. * Function: installListeners
  2233. *
  2234. * Installs the required listeners in the container.
  2235. */
  2236. mxGraphView.prototype.installListeners = function()
  2237. {
  2238. var graph = this.graph;
  2239. var container = graph.container;
  2240. if (container != null)
  2241. {
  2242. // Support for touch device gestures (eg. pinch to zoom)
  2243. // Double-tap handling is implemented in mxGraph.fireMouseEvent
  2244. if (mxClient.IS_TOUCH)
  2245. {
  2246. mxEvent.addListener(container, 'gesturestart', mxUtils.bind(this, function(evt)
  2247. {
  2248. graph.fireGestureEvent(evt);
  2249. mxEvent.consume(evt);
  2250. }));
  2251. mxEvent.addListener(container, 'gesturechange', mxUtils.bind(this, function(evt)
  2252. {
  2253. graph.fireGestureEvent(evt);
  2254. mxEvent.consume(evt);
  2255. }));
  2256. mxEvent.addListener(container, 'gestureend', mxUtils.bind(this, function(evt)
  2257. {
  2258. graph.fireGestureEvent(evt);
  2259. mxEvent.consume(evt);
  2260. }));
  2261. }
  2262. // Fires event only for one pointer per gesture
  2263. var pointerId = null;
  2264. // Adds basic listeners for graph event dispatching
  2265. mxEvent.addGestureListeners(container, mxUtils.bind(this, function(evt)
  2266. {
  2267. // Condition to avoid scrollbar events starting a rubberband selection
  2268. if (this.isContainerEvent(evt) && ((!mxClient.IS_IE && !mxClient.IS_IE11 && !mxClient.IS_GC &&
  2269. !mxClient.IS_OP && !mxClient.IS_SF) || !this.isScrollEvent(evt)))
  2270. {
  2271. graph.fireMouseEvent(mxEvent.MOUSE_DOWN, new mxMouseEvent(evt));
  2272. pointerId = evt.pointerId;
  2273. }
  2274. }),
  2275. mxUtils.bind(this, function(evt)
  2276. {
  2277. if (this.isContainerEvent(evt) && (pointerId == null || evt.pointerId == pointerId))
  2278. {
  2279. graph.fireMouseEvent(mxEvent.MOUSE_MOVE, new mxMouseEvent(evt));
  2280. }
  2281. }),
  2282. mxUtils.bind(this, function(evt)
  2283. {
  2284. if (this.isContainerEvent(evt))
  2285. {
  2286. graph.fireMouseEvent(mxEvent.MOUSE_UP, new mxMouseEvent(evt));
  2287. }
  2288. pointerId = null;
  2289. }));
  2290. // Adds listener for double click handling on background, this does always
  2291. // use native event handler, we assume that the DOM of the background
  2292. // does not change during the double click
  2293. mxEvent.addListener(container, 'dblclick', mxUtils.bind(this, function(evt)
  2294. {
  2295. if (this.isContainerEvent(evt))
  2296. {
  2297. graph.dblClick(evt);
  2298. }
  2299. }));
  2300. // Workaround for touch events which started on some DOM node
  2301. // on top of the container, in which case the cells under the
  2302. // mouse for the move and up events are not detected.
  2303. var getState = function(evt)
  2304. {
  2305. var state = null;
  2306. // Workaround for touch events which started on some DOM node
  2307. // on top of the container, in which case the cells under the
  2308. // mouse for the move and up events are not detected.
  2309. if (mxClient.IS_TOUCH)
  2310. {
  2311. var x = mxEvent.getClientX(evt);
  2312. var y = mxEvent.getClientY(evt);
  2313. // Dispatches the drop event to the graph which
  2314. // consumes and executes the source function
  2315. var pt = mxUtils.convertPoint(container, x, y);
  2316. state = graph.view.getState(graph.getCellAt(pt.x, pt.y));
  2317. }
  2318. return state;
  2319. };
  2320. // Adds basic listeners for graph event dispatching outside of the
  2321. // container and finishing the handling of a single gesture
  2322. // Implemented via graph event dispatch loop to avoid duplicate events
  2323. // in Firefox and Chrome
  2324. graph.addMouseListener(
  2325. {
  2326. mouseDown: function(sender, me)
  2327. {
  2328. graph.popupMenuHandler.hideMenu();
  2329. },
  2330. mouseMove: function() { },
  2331. mouseUp: function() { }
  2332. });
  2333. this.moveHandler = mxUtils.bind(this, function(evt)
  2334. {
  2335. // Hides the tooltip if mouse is outside container
  2336. if (graph.tooltipHandler != null && graph.tooltipHandler.isHideOnHover())
  2337. {
  2338. graph.tooltipHandler.hide();
  2339. }
  2340. if (this.captureDocumentGesture && graph.isMouseDown && graph.container != null &&
  2341. !this.isContainerEvent(evt) && graph.container.style.display != 'none' &&
  2342. graph.container.style.visibility != 'hidden' && !mxEvent.isConsumed(evt))
  2343. {
  2344. graph.fireMouseEvent(mxEvent.MOUSE_MOVE, new mxMouseEvent(evt, getState(evt)));
  2345. }
  2346. });
  2347. this.endHandler = mxUtils.bind(this, function(evt)
  2348. {
  2349. if (this.captureDocumentGesture && graph.isMouseDown && graph.container != null &&
  2350. !this.isContainerEvent(evt) && graph.container.style.display != 'none' &&
  2351. graph.container.style.visibility != 'hidden')
  2352. {
  2353. graph.fireMouseEvent(mxEvent.MOUSE_UP, new mxMouseEvent(evt));
  2354. }
  2355. });
  2356. mxEvent.addGestureListeners(document, null, this.moveHandler, this.endHandler);
  2357. }
  2358. };
  2359. /**
  2360. * Function: createHtml
  2361. *
  2362. * Creates the DOM nodes for the HTML display.
  2363. */
  2364. mxGraphView.prototype.createHtml = function()
  2365. {
  2366. var container = this.graph.container;
  2367. if (container != null)
  2368. {
  2369. this.canvas = this.createHtmlPane('100%', '100%');
  2370. this.canvas.style.overflow = 'hidden';
  2371. // Uses minimal size for inner DIVs on Canvas. This is required
  2372. // for correct event processing in IE. If we have an overlapping
  2373. // DIV then the events on the cells are only fired for labels.
  2374. this.backgroundPane = this.createHtmlPane('1px', '1px');
  2375. this.drawPane = this.createHtmlPane('1px', '1px');
  2376. this.overlayPane = this.createHtmlPane('1px', '1px');
  2377. this.decoratorPane = this.createHtmlPane('1px', '1px');
  2378. this.canvas.appendChild(this.backgroundPane);
  2379. this.canvas.appendChild(this.drawPane);
  2380. this.canvas.appendChild(this.overlayPane);
  2381. this.canvas.appendChild(this.decoratorPane);
  2382. container.appendChild(this.canvas);
  2383. this.updateContainerStyle(container);
  2384. // Implements minWidth/minHeight in quirks mode
  2385. if (mxClient.IS_QUIRKS)
  2386. {
  2387. var onResize = mxUtils.bind(this, function(evt)
  2388. {
  2389. var bounds = this.getGraphBounds();
  2390. var width = bounds.x + bounds.width + this.graph.border;
  2391. var height = bounds.y + bounds.height + this.graph.border;
  2392. this.updateHtmlCanvasSize(width, height);
  2393. });
  2394. mxEvent.addListener(window, 'resize', onResize);
  2395. }
  2396. }
  2397. };
  2398. /**
  2399. * Function: updateHtmlCanvasSize
  2400. *
  2401. * Updates the size of the HTML canvas.
  2402. */
  2403. mxGraphView.prototype.updateHtmlCanvasSize = function(width, height)
  2404. {
  2405. if (this.graph.container != null)
  2406. {
  2407. var ow = this.graph.container.offsetWidth;
  2408. var oh = this.graph.container.offsetHeight;
  2409. if (ow < width)
  2410. {
  2411. this.canvas.style.width = width + 'px';
  2412. }
  2413. else
  2414. {
  2415. this.canvas.style.width = '100%';
  2416. }
  2417. if (oh < height)
  2418. {
  2419. this.canvas.style.height = height + 'px';
  2420. }
  2421. else
  2422. {
  2423. this.canvas.style.height = '100%';
  2424. }
  2425. }
  2426. };
  2427. /**
  2428. * Function: createHtmlPane
  2429. *
  2430. * Creates and returns a drawing pane in HTML (DIV).
  2431. */
  2432. mxGraphView.prototype.createHtmlPane = function(width, height)
  2433. {
  2434. var pane = document.createElement('DIV');
  2435. if (width != null && height != null)
  2436. {
  2437. pane.style.position = 'absolute';
  2438. pane.style.left = '0px';
  2439. pane.style.top = '0px';
  2440. pane.style.width = width;
  2441. pane.style.height = height;
  2442. }
  2443. else
  2444. {
  2445. pane.style.position = 'relative';
  2446. }
  2447. return pane;
  2448. };
  2449. /**
  2450. * Function: createVml
  2451. *
  2452. * Creates the DOM nodes for the VML display.
  2453. */
  2454. mxGraphView.prototype.createVml = function()
  2455. {
  2456. var container = this.graph.container;
  2457. if (container != null)
  2458. {
  2459. var width = container.offsetWidth;
  2460. var height = container.offsetHeight;
  2461. this.canvas = this.createVmlPane(width, height);
  2462. this.canvas.style.overflow = 'hidden';
  2463. this.backgroundPane = this.createVmlPane(width, height);
  2464. this.drawPane = this.createVmlPane(width, height);
  2465. this.overlayPane = this.createVmlPane(width, height);
  2466. this.decoratorPane = this.createVmlPane(width, height);
  2467. this.canvas.appendChild(this.backgroundPane);
  2468. this.canvas.appendChild(this.drawPane);
  2469. this.canvas.appendChild(this.overlayPane);
  2470. this.canvas.appendChild(this.decoratorPane);
  2471. container.appendChild(this.canvas);
  2472. }
  2473. };
  2474. /**
  2475. * Function: createVmlPane
  2476. *
  2477. * Creates a drawing pane in VML (group).
  2478. */
  2479. mxGraphView.prototype.createVmlPane = function(width, height)
  2480. {
  2481. var pane = document.createElement(mxClient.VML_PREFIX + ':group');
  2482. // At this point the width and height are potentially
  2483. // uninitialized. That's OK.
  2484. pane.style.position = 'absolute';
  2485. pane.style.left = '0px';
  2486. pane.style.top = '0px';
  2487. pane.style.width = width + 'px';
  2488. pane.style.height = height + 'px';
  2489. pane.setAttribute('coordsize', width + ',' + height);
  2490. pane.setAttribute('coordorigin', '0,0');
  2491. return pane;
  2492. };
  2493. /**
  2494. * Function: createSvg
  2495. *
  2496. * Creates and returns the DOM nodes for the SVG display.
  2497. */
  2498. mxGraphView.prototype.createSvg = function()
  2499. {
  2500. var container = this.graph.container;
  2501. this.canvas = document.createElementNS(mxConstants.NS_SVG, 'g');
  2502. // For background image
  2503. this.backgroundPane = document.createElementNS(mxConstants.NS_SVG, 'g');
  2504. this.canvas.appendChild(this.backgroundPane);
  2505. // Adds two layers (background is early feature)
  2506. this.drawPane = document.createElementNS(mxConstants.NS_SVG, 'g');
  2507. this.canvas.appendChild(this.drawPane);
  2508. this.overlayPane = document.createElementNS(mxConstants.NS_SVG, 'g');
  2509. this.canvas.appendChild(this.overlayPane);
  2510. this.decoratorPane = document.createElementNS(mxConstants.NS_SVG, 'g');
  2511. this.canvas.appendChild(this.decoratorPane);
  2512. var root = document.createElementNS(mxConstants.NS_SVG, 'svg');
  2513. root.style.left = '0px';
  2514. root.style.top = '0px';
  2515. root.style.width = '100%';
  2516. root.style.height = '100%';
  2517. // NOTE: In standards mode, the SVG must have block layout
  2518. // in order for the container DIV to not show scrollbars.
  2519. root.style.display = 'block';
  2520. root.appendChild(this.canvas);
  2521. // Workaround for scrollbars in IE11 and below
  2522. if (mxClient.IS_IE || mxClient.IS_IE11)
  2523. {
  2524. root.style.overflow = 'hidden';
  2525. }
  2526. if (container != null)
  2527. {
  2528. container.appendChild(root);
  2529. this.updateContainerStyle(container);
  2530. }
  2531. };
  2532. /**
  2533. * Function: updateContainerStyle
  2534. *
  2535. * Updates the style of the container after installing the SVG DOM elements.
  2536. */
  2537. mxGraphView.prototype.updateContainerStyle = function(container)
  2538. {
  2539. // Workaround for offset of container
  2540. var style = mxUtils.getCurrentStyle(container);
  2541. if (style != null && style.position == 'static')
  2542. {
  2543. container.style.position = 'relative';
  2544. }
  2545. // Disables built-in pan and zoom in IE10 and later
  2546. if (mxClient.IS_POINTER)
  2547. {
  2548. container.style.touchAction = 'none';
  2549. }
  2550. };
  2551. /**
  2552. * Function: destroy
  2553. *
  2554. * Destroys the view and all its resources.
  2555. */
  2556. mxGraphView.prototype.destroy = function()
  2557. {
  2558. var root = (this.canvas != null) ? this.canvas.ownerSVGElement : null;
  2559. if (root == null)
  2560. {
  2561. root = this.canvas;
  2562. }
  2563. if (root != null && root.parentNode != null)
  2564. {
  2565. this.clear(this.currentRoot, true);
  2566. mxEvent.removeGestureListeners(document, null, this.moveHandler, this.endHandler);
  2567. mxEvent.release(this.graph.container);
  2568. root.parentNode.removeChild(root);
  2569. this.moveHandler = null;
  2570. this.endHandler = null;
  2571. this.canvas = null;
  2572. this.backgroundPane = null;
  2573. this.drawPane = null;
  2574. this.overlayPane = null;
  2575. this.decoratorPane = null;
  2576. }
  2577. };
  2578. /**
  2579. * Class: mxCurrentRootChange
  2580. *
  2581. * Action to change the current root in a view.
  2582. *
  2583. * Constructor: mxCurrentRootChange
  2584. *
  2585. * Constructs a change of the current root in the given view.
  2586. */
  2587. function mxCurrentRootChange(view, root)
  2588. {
  2589. this.view = view;
  2590. this.root = root;
  2591. this.previous = root;
  2592. this.isUp = root == null;
  2593. if (!this.isUp)
  2594. {
  2595. var tmp = this.view.currentRoot;
  2596. var model = this.view.graph.getModel();
  2597. while (tmp != null)
  2598. {
  2599. if (tmp == root)
  2600. {
  2601. this.isUp = true;
  2602. break;
  2603. }
  2604. tmp = model.getParent(tmp);
  2605. }
  2606. }
  2607. };
  2608. /**
  2609. * Function: execute
  2610. *
  2611. * Changes the current root of the view.
  2612. */
  2613. mxCurrentRootChange.prototype.execute = function()
  2614. {
  2615. var tmp = this.view.currentRoot;
  2616. this.view.currentRoot = this.previous;
  2617. this.previous = tmp;
  2618. var translate = this.view.graph.getTranslateForRoot(this.view.currentRoot);
  2619. if (translate != null)
  2620. {
  2621. this.view.translate = new mxPoint(-translate.x, -translate.y);
  2622. }
  2623. if (this.isUp)
  2624. {
  2625. this.view.clear(this.view.currentRoot, true);
  2626. this.view.validate();
  2627. }
  2628. else
  2629. {
  2630. this.view.refresh();
  2631. }
  2632. var name = (this.isUp) ? mxEvent.UP : mxEvent.DOWN;
  2633. this.view.fireEvent(new mxEventObject(name,
  2634. 'root', this.view.currentRoot, 'previous', this.previous));
  2635. this.isUp = !this.isUp;
  2636. };
  2637. __mxOutput.mxGraphView = typeof mxGraphView !== 'undefined' ? mxGraphView : undefined;