mxEditor.js 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118
  1. /**
  2. * Copyright (c) 2006-2019, JGraph Ltd
  3. * Copyright (c) 2006-2019, draw.io AG
  4. */
  5. /**
  6. * Class: mxEditor
  7. *
  8. * Extends <mxEventSource> to implement an application wrapper for a graph that
  9. * adds <actions>, I/O using <mxCodec>, auto-layout using <mxLayoutManager>,
  10. * command history using <undoManager>, and standard dialogs and widgets, eg.
  11. * properties, help, outline, toolbar, and popupmenu. It also adds <templates>
  12. * to be used as cells in toolbars, auto-validation using the <validation>
  13. * flag, attribute cycling using <cycleAttributeValues>, higher-level events
  14. * such as <root>, and backend integration using <urlPost> and <urlImage>.
  15. *
  16. * Actions:
  17. *
  18. * Actions are functions stored in the <actions> array under their names. The
  19. * functions take the <mxEditor> as the first, and an optional <mxCell> as the
  20. * second argument and are invoked using <execute>. Any additional arguments
  21. * passed to execute are passed on to the action as-is.
  22. *
  23. * A list of built-in actions is available in the <addActions> description.
  24. *
  25. * Read/write Diagrams:
  26. *
  27. * To read a diagram from an XML string, for example from a textfield within the
  28. * page, the following code is used:
  29. *
  30. * (code)
  31. * var doc = mxUtils.parseXML(xmlString);
  32. * var node = doc.documentElement;
  33. * editor.readGraphModel(node);
  34. * (end)
  35. *
  36. * For reading a diagram from a remote location, use the <open> method.
  37. *
  38. * To save diagrams in XML on a server, you can set the <urlPost> variable.
  39. * This variable will be used in <getUrlPost> to construct a URL for the post
  40. * request that is issued in the <save> method. The post request contains the
  41. * XML representation of the diagram as returned by <writeGraphModel> in the
  42. * xml parameter.
  43. *
  44. * On the server side, the post request is processed using standard
  45. * technologies such as Java Servlets, CGI, .NET or ASP.
  46. *
  47. * Here are some examples of processing a post request in various languages.
  48. *
  49. * - Java: URLDecoder.decode(request.getParameter("xml"), "UTF-8").replace("\n", "&#xa;")
  50. *
  51. * Note that the linefeeds should only be replaced if the XML is
  52. * processed in Java, for example when creating an image, but not
  53. * if the XML is passed back to the client-side.
  54. *
  55. * - .NET: HttpUtility.UrlDecode(context.Request.Params["xml"])
  56. * - PHP: urldecode($_POST["xml"])
  57. *
  58. * Creating images:
  59. *
  60. * A backend (Java, PHP or C#) is required for creating images. The
  61. * distribution contains an example for each backend (ImageHandler.java,
  62. * ImageHandler.cs and graph.php). More information about using a backend
  63. * to create images can be found in the readme.html files. Note that the
  64. * preview is implemented using VML/SVG in the browser and does not require
  65. * a backend. The backend is only required to creates images (bitmaps).
  66. *
  67. * Special characters:
  68. *
  69. * Note There are five characters that should always appear in XML content as
  70. * escapes, so that they do not interact with the syntax of the markup. These
  71. * are part of the language for all documents based on XML and for HTML.
  72. *
  73. * - &lt; (<)
  74. * - &gt; (>)
  75. * - &amp; (&)
  76. * - &quot; (")
  77. * - &apos; (')
  78. *
  79. * Although it is part of the XML language, &apos; is not defined in HTML.
  80. * For this reason the XHTML specification recommends instead the use of
  81. * &#39; if text may be passed to a HTML user agent.
  82. *
  83. * If you are having problems with special characters on the server-side then
  84. * you may want to try the <escapePostData> flag.
  85. *
  86. * For converting decimal escape sequences inside strings, a user has provided
  87. * us with the following function:
  88. *
  89. * (code)
  90. * function html2js(text)
  91. * {
  92. * var entitySearch = /&#[0-9]+;/;
  93. * var entity;
  94. *
  95. * while (entity = entitySearch.exec(text))
  96. * {
  97. * var charCode = entity[0].substring(2, entity[0].length -1);
  98. * text = text.substring(0, entity.index)
  99. * + String.fromCharCode(charCode)
  100. * + text.substring(entity.index + entity[0].length);
  101. * }
  102. *
  103. * return text;
  104. * }
  105. * (end)
  106. *
  107. * Otherwise try using hex escape sequences and the built-in unescape function
  108. * for converting such strings.
  109. *
  110. * Local Files:
  111. *
  112. * For saving and opening local files, no standardized method exists that
  113. * works across all browsers. The recommended way of dealing with local files
  114. * is to create a backend that streams the XML data back to the browser (echo)
  115. * as an attachment so that a Save-dialog is displayed on the client-side and
  116. * the file can be saved to the local disk.
  117. *
  118. * For example, in PHP the code that does this looks as follows.
  119. *
  120. * (code)
  121. * $xml = stripslashes($_POST["xml"]);
  122. * header("Content-Disposition: attachment; filename=\"diagram.xml\"");
  123. * echo($xml);
  124. * (end)
  125. *
  126. * To open a local file, the file should be uploaded via a form in the browser
  127. * and then opened from the server in the editor.
  128. *
  129. * Cell Properties:
  130. *
  131. * The properties displayed in the properties dialog are the attributes and
  132. * values of the cell's user object, which is an XML node. The XML node is
  133. * defined in the templates section of the config file.
  134. *
  135. * The templates are stored in <mxEditor.templates> and contain cells which
  136. * are cloned at insertion time to create new vertices by use of drag and
  137. * drop from the toolbar. Each entry in the toolbar for adding a new vertex
  138. * must refer to an existing template.
  139. *
  140. * In the following example, the task node is a business object and only the
  141. * mxCell node and its mxGeometry child contain graph information:
  142. *
  143. * (code)
  144. * <Task label="Task" description="">
  145. * <mxCell vertex="true">
  146. * <mxGeometry as="geometry" width="72" height="32"/>
  147. * </mxCell>
  148. * </Task>
  149. * (end)
  150. *
  151. * The idea is that the XML representation is inverse from the in-memory
  152. * representation: The outer XML node is the user object and the inner node is
  153. * the cell. This means the user object of the cell is the Task node with no
  154. * children for the above example:
  155. *
  156. * (code)
  157. * <Task label="Task" description=""/>
  158. * (end)
  159. *
  160. * The Task node can have any tag name, attributes and child nodes. The
  161. * <mxCodec> will use the XML hierarchy as the user object, while removing the
  162. * "known annotations", such as the mxCell node. At save-time the cell data
  163. * will be "merged" back into the user object. The user object is only modified
  164. * via the properties dialog during the lifecycle of the cell.
  165. *
  166. * In the default implementation of <createProperties>, the user object's
  167. * attributes are put into a form for editing. Attributes are changed using
  168. * the <mxCellAttributeChange> action in the model. The dialog can be replaced
  169. * by overriding the <createProperties> hook or by replacing the showProperties
  170. * action in <actions>. Alternatively, the entry in the config file's popupmenu
  171. * section can be modified to invoke a different action.
  172. *
  173. * If you want to displey the properties dialog on a doubleclick, you can set
  174. * <mxEditor.dblClickAction> to showProperties as follows:
  175. *
  176. * (code)
  177. * editor.dblClickAction = 'showProperties';
  178. * (end)
  179. *
  180. * Popupmenu and Toolbar:
  181. *
  182. * The toolbar and popupmenu are typically configured using the respective
  183. * sections in the config file, that is, the popupmenu is defined as follows:
  184. *
  185. * (code)
  186. * <mxEditor>
  187. * <mxDefaultPopupMenu as="popupHandler">
  188. * <add as="cut" action="cut" icon="images/cut.gif"/>
  189. * ...
  190. * (end)
  191. *
  192. * New entries can be added to the toolbar by inserting an add-node into the
  193. * above configuration. Existing entries may be removed and changed by
  194. * modifying or removing the respective entries in the configuration.
  195. * The configuration is read by the <mxDefaultPopupMenuCodec>, the format of the
  196. * configuration is explained in <mxDefaultPopupMenu.decode>.
  197. *
  198. * The toolbar is defined in the mxDefaultToolbar section. Items can be added
  199. * and removed in this section.
  200. *
  201. * (code)
  202. * <mxEditor>
  203. * <mxDefaultToolbar>
  204. * <add as="save" action="save" icon="images/save.gif"/>
  205. * <add as="Swimlane" template="swimlane" icon="images/swimlane.gif"/>
  206. * ...
  207. * (end)
  208. *
  209. * The format of the configuration is described in
  210. * <mxDefaultToolbarCodec.decode>.
  211. *
  212. * Ids:
  213. *
  214. * For the IDs, there is an implicit behaviour in <mxCodec>: It moves the Id
  215. * from the cell to the user object at encoding time and vice versa at decoding
  216. * time. For example, if the Task node from above has an id attribute, then
  217. * the <mxCell.id> of the corresponding cell will have this value. If there
  218. * is no Id collision in the model, then the cell may be retrieved using this
  219. * Id with the <mxGraphModel.getCell> function. If there is a collision, a new
  220. * Id will be created for the cell using <mxGraphModel.createId>. At encoding
  221. * time, this new Id will replace the value previously stored under the id
  222. * attribute in the Task node.
  223. *
  224. * See <mxEditorCodec>, <mxDefaultToolbarCodec> and <mxDefaultPopupMenuCodec>
  225. * for information about configuring the editor and user interface.
  226. *
  227. * Programmatically inserting cells:
  228. *
  229. * For inserting a new cell, say, by clicking a button in the document,
  230. * the following code can be used. This requires an reference to the editor.
  231. *
  232. * (code)
  233. * var userObject = new Object();
  234. * var parent = editor.graph.getDefaultParent();
  235. * var model = editor.graph.model;
  236. * model.beginUpdate();
  237. * try
  238. * {
  239. * editor.graph.insertVertex(parent, null, userObject, 20, 20, 80, 30);
  240. * }
  241. * finally
  242. * {
  243. * model.endUpdate();
  244. * }
  245. * (end)
  246. *
  247. * If a template cell from the config file should be inserted, then a clone
  248. * of the template can be created as follows. The clone is then inserted using
  249. * the add function instead of addVertex.
  250. *
  251. * (code)
  252. * var template = editor.templates['task'];
  253. * var clone = editor.graph.model.cloneCell(template);
  254. * (end)
  255. *
  256. * Resources:
  257. *
  258. * resources/editor - Language resources for mxEditor
  259. *
  260. * Callback: onInit
  261. *
  262. * Called from within the constructor. In the callback,
  263. * "this" refers to the editor instance.
  264. *
  265. * Cookie: mxgraph=seen
  266. *
  267. * Set when the editor is started. Never expires. Use
  268. * <resetFirstTime> to reset this cookie. This cookie
  269. * only exists if <onInit> is implemented.
  270. *
  271. * Event: mxEvent.OPEN
  272. *
  273. * Fires after a file was opened in <open>. The <code>filename</code> property
  274. * contains the filename that was used. The same value is also available in
  275. * <filename>.
  276. *
  277. * Event: mxEvent.SAVE
  278. *
  279. * Fires after the current file was saved in <save>. The <code>url</code>
  280. * property contains the URL that was used for saving.
  281. *
  282. * Event: mxEvent.POST
  283. *
  284. * Fires if a successful response was received in <postDiagram>. The
  285. * <code>request</code> property contains the <mxXmlRequest>, the
  286. * <code>url</code> and <code>data</code> properties contain the URL and the
  287. * data that were used in the post request.
  288. *
  289. * Event: mxEvent.ROOT
  290. *
  291. * Fires when the current root has changed, or when the title of the current
  292. * root has changed. This event has no properties.
  293. *
  294. * Event: mxEvent.BEFORE_ADD_VERTEX
  295. *
  296. * Fires before a vertex is added in <addVertex>. The <code>vertex</code>
  297. * property contains the new vertex and the <code>parent</code> property
  298. * contains its parent.
  299. *
  300. * Event: mxEvent.ADD_VERTEX
  301. *
  302. * Fires between begin- and endUpdate in <addVertex>. The <code>vertex</code>
  303. * property contains the vertex that is being inserted.
  304. *
  305. * Event: mxEvent.AFTER_ADD_VERTEX
  306. *
  307. * Fires after a vertex was inserted and selected in <addVertex>. The
  308. * <code>vertex</code> property contains the new vertex.
  309. *
  310. * Example:
  311. *
  312. * For starting an in-place edit after a new vertex has been added to the
  313. * graph, the following code can be used.
  314. *
  315. * (code)
  316. * editor.addListener(mxEvent.AFTER_ADD_VERTEX, function(sender, evt)
  317. * {
  318. * var vertex = evt.getProperty('vertex');
  319. *
  320. * if (editor.graph.isCellEditable(vertex))
  321. * {
  322. * editor.graph.startEditingAtCell(vertex);
  323. * }
  324. * });
  325. * (end)
  326. *
  327. * Event: mxEvent.ESCAPE
  328. *
  329. * Fires when the escape key is pressed. The <code>event</code> property
  330. * contains the key event.
  331. *
  332. * Constructor: mxEditor
  333. *
  334. * Constructs a new editor. This function invokes the <onInit> callback
  335. * upon completion.
  336. *
  337. * Example:
  338. *
  339. * (code)
  340. * var config = mxUtils.load('config/diagrameditor.xml').getDocumentElement();
  341. * var editor = new mxEditor(config);
  342. * (end)
  343. *
  344. * Parameters:
  345. *
  346. * config - Optional XML node that contains the configuration.
  347. */
  348. function mxEditor(config)
  349. {
  350. this.actions = [];
  351. this.addActions();
  352. // Executes the following only if a document has been instanciated.
  353. // That is, don't execute when the editorcodec is setup.
  354. if (document.body != null)
  355. {
  356. // Defines instance fields
  357. this.cycleAttributeValues = [];
  358. this.popupHandler = new mxDefaultPopupMenu();
  359. this.undoManager = new mxUndoManager();
  360. // Creates the graph and toolbar without the containers
  361. this.graph = this.createGraph();
  362. this.toolbar = this.createToolbar();
  363. // Creates the global keyhandler (requires graph instance)
  364. this.keyHandler = new mxDefaultKeyHandler(this);
  365. // Configures the editor using the URI
  366. // which was passed to the ctor
  367. this.configure(config);
  368. // Assigns the swimlaneIndicatorColorAttribute on the graph
  369. this.graph.swimlaneIndicatorColorAttribute = this.cycleAttributeName;
  370. // Checks if the <onInit> hook has been set
  371. if (this.onInit != null)
  372. {
  373. // Invokes the <onInit> hook
  374. this.onInit();
  375. }
  376. // Automatic deallocation of memory
  377. if (mxClient.IS_IE)
  378. {
  379. mxEvent.addListener(window, 'unload', mxUtils.bind(this, function()
  380. {
  381. this.destroy();
  382. }));
  383. }
  384. }
  385. };
  386. /**
  387. * Installs the required language resources at class
  388. * loading time.
  389. */
  390. if (mxLoadResources)
  391. {
  392. mxResources.add(mxClient.basePath + '/resources/editor');
  393. }
  394. else
  395. {
  396. mxClient.defaultBundles.push(mxClient.basePath + '/resources/editor');
  397. }
  398. /**
  399. * Extends mxEventSource.
  400. */
  401. mxEditor.prototype = new mxEventSource();
  402. mxEditor.prototype.constructor = mxEditor;
  403. /**
  404. * Group: Controls and Handlers
  405. */
  406. /**
  407. * Variable: askZoomResource
  408. *
  409. * Specifies the resource key for the zoom dialog. If the resource for this
  410. * key does not exist then the value is used as the error message. Default
  411. * is 'askZoom'.
  412. */
  413. mxEditor.prototype.askZoomResource = (mxClient.language != 'none') ? 'askZoom' : '';
  414. /**
  415. * Variable: lastSavedResource
  416. *
  417. * Specifies the resource key for the last saved info. If the resource for
  418. * this key does not exist then the value is used as the error message.
  419. * Default is 'lastSaved'.
  420. */
  421. mxEditor.prototype.lastSavedResource = (mxClient.language != 'none') ? 'lastSaved' : '';
  422. /**
  423. * Variable: currentFileResource
  424. *
  425. * Specifies the resource key for the current file info. If the resource for
  426. * this key does not exist then the value is used as the error message.
  427. * Default is 'currentFile'.
  428. */
  429. mxEditor.prototype.currentFileResource = (mxClient.language != 'none') ? 'currentFile' : '';
  430. /**
  431. * Variable: propertiesResource
  432. *
  433. * Specifies the resource key for the properties window title. If the
  434. * resource for this key does not exist then the value is used as the
  435. * error message. Default is 'properties'.
  436. */
  437. mxEditor.prototype.propertiesResource = (mxClient.language != 'none') ? 'properties' : '';
  438. /**
  439. * Variable: tasksResource
  440. *
  441. * Specifies the resource key for the tasks window title. If the
  442. * resource for this key does not exist then the value is used as the
  443. * error message. Default is 'tasks'.
  444. */
  445. mxEditor.prototype.tasksResource = (mxClient.language != 'none') ? 'tasks' : '';
  446. /**
  447. * Variable: helpResource
  448. *
  449. * Specifies the resource key for the help window title. If the
  450. * resource for this key does not exist then the value is used as the
  451. * error message. Default is 'help'.
  452. */
  453. mxEditor.prototype.helpResource = (mxClient.language != 'none') ? 'help' : '';
  454. /**
  455. * Variable: outlineResource
  456. *
  457. * Specifies the resource key for the outline window title. If the
  458. * resource for this key does not exist then the value is used as the
  459. * error message. Default is 'outline'.
  460. */
  461. mxEditor.prototype.outlineResource = (mxClient.language != 'none') ? 'outline' : '';
  462. /**
  463. * Variable: outline
  464. *
  465. * Reference to the <mxWindow> that contains the outline. The <mxOutline>
  466. * is stored in outline.outline.
  467. */
  468. mxEditor.prototype.outline = null;
  469. /**
  470. * Variable: graph
  471. *
  472. * Holds a <mxGraph> for displaying the diagram. The graph
  473. * is created in <setGraphContainer>.
  474. */
  475. mxEditor.prototype.graph = null;
  476. /**
  477. * Variable: graphRenderHint
  478. *
  479. * Holds the render hint used for creating the
  480. * graph in <setGraphContainer>. See <mxGraph>.
  481. * Default is null.
  482. */
  483. mxEditor.prototype.graphRenderHint = null;
  484. /**
  485. * Variable: toolbar
  486. *
  487. * Holds a <mxDefaultToolbar> for displaying the toolbar. The
  488. * toolbar is created in <setToolbarContainer>.
  489. */
  490. mxEditor.prototype.toolbar = null;
  491. /**
  492. * Variable: status
  493. *
  494. * DOM container that holds the statusbar. Default is null.
  495. * Use <setStatusContainer> to set this value.
  496. */
  497. mxEditor.prototype.status = null;
  498. /**
  499. * Variable: popupHandler
  500. *
  501. * Holds a <mxDefaultPopupMenu> for displaying
  502. * popupmenus.
  503. */
  504. mxEditor.prototype.popupHandler = null;
  505. /**
  506. * Variable: undoManager
  507. *
  508. * Holds an <mxUndoManager> for the command history.
  509. */
  510. mxEditor.prototype.undoManager = null;
  511. /**
  512. * Variable: keyHandler
  513. *
  514. * Holds a <mxDefaultKeyHandler> for handling keyboard events.
  515. * The handler is created in <setGraphContainer>.
  516. */
  517. mxEditor.prototype.keyHandler = null;
  518. /**
  519. * Group: Actions and Options
  520. */
  521. /**
  522. * Variable: actions
  523. *
  524. * Maps from actionnames to actions, which are functions taking
  525. * the editor and the cell as arguments. Use <addAction>
  526. * to add or replace an action and <execute> to execute an action
  527. * by name, passing the cell to be operated upon as the second
  528. * argument.
  529. */
  530. mxEditor.prototype.actions = null;
  531. /**
  532. * Variable: dblClickAction
  533. *
  534. * Specifies the name of the action to be executed
  535. * when a cell is double clicked. Default is 'edit'.
  536. *
  537. * To handle a singleclick, use the following code.
  538. *
  539. * (code)
  540. * editor.graph.addListener(mxEvent.CLICK, function(sender, evt)
  541. * {
  542. * var e = evt.getProperty('event');
  543. * var cell = evt.getProperty('cell');
  544. *
  545. * if (cell != null && !e.isConsumed())
  546. * {
  547. * // Do something useful with cell...
  548. * e.consume();
  549. * }
  550. * });
  551. * (end)
  552. */
  553. mxEditor.prototype.dblClickAction = 'edit';
  554. /**
  555. * Variable: swimlaneRequired
  556. *
  557. * Specifies if new cells must be inserted
  558. * into an existing swimlane. Otherwise, cells
  559. * that are not swimlanes can be inserted as
  560. * top-level cells. Default is false.
  561. */
  562. mxEditor.prototype.swimlaneRequired = false;
  563. /**
  564. * Variable: disableContextMenu
  565. *
  566. * Specifies if the context menu should be disabled in the graph container.
  567. * Default is true.
  568. */
  569. mxEditor.prototype.disableContextMenu = true;
  570. /**
  571. * Group: Templates
  572. */
  573. /**
  574. * Variable: insertFunction
  575. *
  576. * Specifies the function to be used for inserting new
  577. * cells into the graph. This is assigned from the
  578. * <mxDefaultToolbar> if a vertex-tool is clicked.
  579. */
  580. mxEditor.prototype.insertFunction = null;
  581. /**
  582. * Variable: forcedInserting
  583. *
  584. * Specifies if a new cell should be inserted on a single
  585. * click even using <insertFunction> if there is a cell
  586. * under the mousepointer, otherwise the cell under the
  587. * mousepointer is selected. Default is false.
  588. */
  589. mxEditor.prototype.forcedInserting = false;
  590. /**
  591. * Variable: templates
  592. *
  593. * Maps from names to protoype cells to be used
  594. * in the toolbar for inserting new cells into
  595. * the diagram.
  596. */
  597. mxEditor.prototype.templates = null;
  598. /**
  599. * Variable: defaultEdge
  600. *
  601. * Prototype edge cell that is used for creating
  602. * new edges.
  603. */
  604. mxEditor.prototype.defaultEdge = null;
  605. /**
  606. * Variable: defaultEdgeStyle
  607. *
  608. * Specifies the edge style to be returned in <getEdgeStyle>.
  609. * Default is null.
  610. */
  611. mxEditor.prototype.defaultEdgeStyle = null;
  612. /**
  613. * Variable: defaultGroup
  614. *
  615. * Prototype group cell that is used for creating
  616. * new groups.
  617. */
  618. mxEditor.prototype.defaultGroup = null;
  619. /**
  620. * Variable: groupBorderSize
  621. *
  622. * Default size for the border of new groups. If null,
  623. * then then <mxGraph.gridSize> is used. Default is
  624. * null.
  625. */
  626. mxEditor.prototype.groupBorderSize = null;
  627. /**
  628. * Group: Backend Integration
  629. */
  630. /**
  631. * Variable: filename
  632. *
  633. * Contains the URL of the last opened file as a string.
  634. * Default is null.
  635. */
  636. mxEditor.prototype.filename = null;
  637. /**
  638. * Variable: lineFeed
  639. *
  640. * Character to be used for encoding linefeeds in <save>. Default is '&#xa;'.
  641. */
  642. mxEditor.prototype.linefeed = '&#xa;';
  643. /**
  644. * Variable: postParameterName
  645. *
  646. * Specifies if the name of the post parameter that contains the diagram
  647. * data in a post request to the server. Default is 'xml'.
  648. */
  649. mxEditor.prototype.postParameterName = 'xml';
  650. /**
  651. * Variable: escapePostData
  652. *
  653. * Specifies if the data in the post request for saving a diagram
  654. * should be converted using encodeURIComponent. Default is true.
  655. */
  656. mxEditor.prototype.escapePostData = true;
  657. /**
  658. * Variable: urlPost
  659. *
  660. * Specifies the URL to be used for posting the diagram
  661. * to a backend in <save>.
  662. */
  663. mxEditor.prototype.urlPost = null;
  664. /**
  665. * Variable: urlImage
  666. *
  667. * Specifies the URL to be used for creating a bitmap of
  668. * the graph in the image action.
  669. */
  670. mxEditor.prototype.urlImage = null;
  671. /**
  672. * Group: Autolayout
  673. */
  674. /**
  675. * Variable: horizontalFlow
  676. *
  677. * Specifies the direction of the flow
  678. * in the diagram. This is used in the
  679. * layout algorithms. Default is false,
  680. * ie. vertical flow.
  681. */
  682. mxEditor.prototype.horizontalFlow = false;
  683. /**
  684. * Variable: layoutDiagram
  685. *
  686. * Specifies if the top-level elements in the
  687. * diagram should be layed out using a vertical
  688. * or horizontal stack depending on the setting
  689. * of <horizontalFlow>. The spacing between the
  690. * swimlanes is specified by <swimlaneSpacing>.
  691. * Default is false.
  692. *
  693. * If the top-level elements are swimlanes, then
  694. * the intra-swimlane layout is activated by
  695. * the <layoutSwimlanes> switch.
  696. */
  697. mxEditor.prototype.layoutDiagram = false;
  698. /**
  699. * Variable: swimlaneSpacing
  700. *
  701. * Specifies the spacing between swimlanes if
  702. * automatic layout is turned on in
  703. * <layoutDiagram>. Default is 0.
  704. */
  705. mxEditor.prototype.swimlaneSpacing = 0;
  706. /**
  707. * Variable: maintainSwimlanes
  708. *
  709. * Specifies if the swimlanes should be kept at the same
  710. * width or height depending on the setting of
  711. * <horizontalFlow>. Default is false.
  712. *
  713. * For horizontal flows, all swimlanes
  714. * have the same height and for vertical flows, all swimlanes
  715. * have the same width. Furthermore, the swimlanes are
  716. * automatically "stacked" if <layoutDiagram> is true.
  717. */
  718. mxEditor.prototype.maintainSwimlanes = false;
  719. /**
  720. * Variable: layoutSwimlanes
  721. *
  722. * Specifies if the children of swimlanes should
  723. * be layed out, either vertically or horizontally
  724. * depending on <horizontalFlow>.
  725. * Default is false.
  726. */
  727. mxEditor.prototype.layoutSwimlanes = false;
  728. /**
  729. * Group: Attribute Cycling
  730. */
  731. /**
  732. * Variable: cycleAttributeValues
  733. *
  734. * Specifies the attribute values to be cycled when
  735. * inserting new swimlanes. Default is an empty
  736. * array.
  737. */
  738. mxEditor.prototype.cycleAttributeValues = null;
  739. /**
  740. * Variable: cycleAttributeIndex
  741. *
  742. * Index of the last consumed attribute index. If a new
  743. * swimlane is inserted, then the <cycleAttributeValues>
  744. * at this index will be used as the value for
  745. * <cycleAttributeName>. Default is 0.
  746. */
  747. mxEditor.prototype.cycleAttributeIndex = 0;
  748. /**
  749. * Variable: cycleAttributeName
  750. *
  751. * Name of the attribute to be assigned a <cycleAttributeValues>
  752. * when inserting new swimlanes. Default is 'fillColor'.
  753. */
  754. mxEditor.prototype.cycleAttributeName = 'fillColor';
  755. /**
  756. * Group: Windows
  757. */
  758. /**
  759. * Variable: tasks
  760. *
  761. * Holds the <mxWindow> created in <showTasks>.
  762. */
  763. mxEditor.prototype.tasks = null;
  764. /**
  765. * Variable: tasksWindowImage
  766. *
  767. * Icon for the tasks window.
  768. */
  769. mxEditor.prototype.tasksWindowImage = null;
  770. /**
  771. * Variable: tasksTop
  772. *
  773. * Specifies the top coordinate of the tasks window in pixels.
  774. * Default is 20.
  775. */
  776. mxEditor.prototype.tasksTop = 20;
  777. /**
  778. * Variable: help
  779. *
  780. * Holds the <mxWindow> created in <showHelp>.
  781. */
  782. mxEditor.prototype.help = null;
  783. /**
  784. * Variable: helpWindowImage
  785. *
  786. * Icon for the help window.
  787. */
  788. mxEditor.prototype.helpWindowImage = null;
  789. /**
  790. * Variable: urlHelp
  791. *
  792. * Specifies the URL to be used for the contents of the
  793. * Online Help window. This is usually specified in the
  794. * resources file under urlHelp for language-specific
  795. * online help support.
  796. */
  797. mxEditor.prototype.urlHelp = null;
  798. /**
  799. * Variable: helpWidth
  800. *
  801. * Specifies the width of the help window in pixels.
  802. * Default is 300.
  803. */
  804. mxEditor.prototype.helpWidth = 300;
  805. /**
  806. * Variable: helpHeight
  807. *
  808. * Specifies the height of the help window in pixels.
  809. * Default is 260.
  810. */
  811. mxEditor.prototype.helpHeight = 260;
  812. /**
  813. * Variable: propertiesWidth
  814. *
  815. * Specifies the width of the properties window in pixels.
  816. * Default is 240.
  817. */
  818. mxEditor.prototype.propertiesWidth = 240;
  819. /**
  820. * Variable: propertiesHeight
  821. *
  822. * Specifies the height of the properties window in pixels.
  823. * If no height is specified then the window will be automatically
  824. * sized to fit its contents. Default is null.
  825. */
  826. mxEditor.prototype.propertiesHeight = null;
  827. /**
  828. * Variable: movePropertiesDialog
  829. *
  830. * Specifies if the properties dialog should be automatically
  831. * moved near the cell it is displayed for, otherwise the
  832. * dialog is not moved. This value is only taken into
  833. * account if the dialog is already visible. Default is false.
  834. */
  835. mxEditor.prototype.movePropertiesDialog = false;
  836. /**
  837. * Variable: validating
  838. *
  839. * Specifies if <mxGraph.validateGraph> should automatically be invoked after
  840. * each change. Default is false.
  841. */
  842. mxEditor.prototype.validating = false;
  843. /**
  844. * Variable: modified
  845. *
  846. * True if the graph has been modified since it was last saved.
  847. */
  848. mxEditor.prototype.modified = false;
  849. /**
  850. * Function: isModified
  851. *
  852. * Returns <modified>.
  853. */
  854. mxEditor.prototype.isModified = function ()
  855. {
  856. return this.modified;
  857. };
  858. /**
  859. * Function: setModified
  860. *
  861. * Sets <modified> to the specified boolean value.
  862. */
  863. mxEditor.prototype.setModified = function (value)
  864. {
  865. this.modified = value;
  866. };
  867. /**
  868. * Function: addActions
  869. *
  870. * Adds the built-in actions to the editor instance.
  871. *
  872. * save - Saves the graph using <urlPost>.
  873. * print - Shows the graph in a new print preview window.
  874. * show - Shows the graph in a new window.
  875. * exportImage - Shows the graph as a bitmap image using <getUrlImage>.
  876. * refresh - Refreshes the graph's display.
  877. * cut - Copies the current selection into the clipboard
  878. * and removes it from the graph.
  879. * copy - Copies the current selection into the clipboard.
  880. * paste - Pastes the clipboard into the graph.
  881. * delete - Removes the current selection from the graph.
  882. * group - Puts the current selection into a new group.
  883. * ungroup - Removes the selected groups and selects the children.
  884. * undo - Undoes the last change on the graph model.
  885. * redo - Redoes the last change on the graph model.
  886. * zoom - Sets the zoom via a dialog.
  887. * zoomIn - Zooms into the graph.
  888. * zoomOut - Zooms out of the graph
  889. * actualSize - Resets the scale and translation on the graph.
  890. * fit - Changes the scale so that the graph fits into the window.
  891. * showProperties - Shows the properties dialog.
  892. * selectAll - Selects all cells.
  893. * selectNone - Clears the selection.
  894. * selectVertices - Selects all vertices.
  895. * selectEdges = Selects all edges.
  896. * edit - Starts editing the current selection cell.
  897. * enterGroup - Drills down into the current selection cell.
  898. * exitGroup - Moves up in the drilling hierachy
  899. * home - Moves to the topmost parent in the drilling hierarchy
  900. * selectPrevious - Selects the previous cell.
  901. * selectNext - Selects the next cell.
  902. * selectParent - Selects the parent of the selection cell.
  903. * selectChild - Selects the first child of the selection cell.
  904. * collapse - Collapses the currently selected cells.
  905. * expand - Expands the currently selected cells.
  906. * bold - Toggle bold text style.
  907. * italic - Toggle italic text style.
  908. * underline - Toggle underline text style.
  909. * alignCellsLeft - Aligns the selection cells at the left.
  910. * alignCellsCenter - Aligns the selection cells in the center.
  911. * alignCellsRight - Aligns the selection cells at the right.
  912. * alignCellsTop - Aligns the selection cells at the top.
  913. * alignCellsMiddle - Aligns the selection cells in the middle.
  914. * alignCellsBottom - Aligns the selection cells at the bottom.
  915. * alignFontLeft - Sets the horizontal text alignment to left.
  916. * alignFontCenter - Sets the horizontal text alignment to center.
  917. * alignFontRight - Sets the horizontal text alignment to right.
  918. * alignFontTop - Sets the vertical text alignment to top.
  919. * alignFontMiddle - Sets the vertical text alignment to middle.
  920. * alignFontBottom - Sets the vertical text alignment to bottom.
  921. * toggleTasks - Shows or hides the tasks window.
  922. * toggleHelp - Shows or hides the help window.
  923. * toggleOutline - Shows or hides the outline window.
  924. * toggleConsole - Shows or hides the console window.
  925. */
  926. mxEditor.prototype.addActions = function ()
  927. {
  928. this.addAction('save', function(editor)
  929. {
  930. editor.save();
  931. });
  932. this.addAction('print', function(editor)
  933. {
  934. var preview = new mxPrintPreview(editor.graph, 1);
  935. preview.open();
  936. });
  937. this.addAction('show', function(editor)
  938. {
  939. mxUtils.show(editor.graph, null, 10, 10);
  940. });
  941. this.addAction('exportImage', function(editor)
  942. {
  943. var url = editor.getUrlImage();
  944. if (url == null || mxClient.IS_LOCAL)
  945. {
  946. editor.execute('show');
  947. }
  948. else
  949. {
  950. var node = mxUtils.getViewXml(editor.graph, 1);
  951. var xml = mxUtils.getXml(node, '\n');
  952. mxUtils.submit(url, editor.postParameterName + '=' +
  953. encodeURIComponent(xml), document, '_blank');
  954. }
  955. });
  956. this.addAction('refresh', function(editor)
  957. {
  958. editor.graph.refresh();
  959. });
  960. this.addAction('cut', function(editor)
  961. {
  962. if (editor.graph.isEnabled())
  963. {
  964. mxClipboard.cut(editor.graph);
  965. }
  966. });
  967. this.addAction('copy', function(editor)
  968. {
  969. if (editor.graph.isEnabled())
  970. {
  971. mxClipboard.copy(editor.graph);
  972. }
  973. });
  974. this.addAction('paste', function(editor)
  975. {
  976. if (editor.graph.isEnabled())
  977. {
  978. mxClipboard.paste(editor.graph);
  979. }
  980. });
  981. this.addAction('delete', function(editor)
  982. {
  983. if (editor.graph.isEnabled())
  984. {
  985. editor.graph.removeCells();
  986. }
  987. });
  988. this.addAction('group', function(editor)
  989. {
  990. if (editor.graph.isEnabled())
  991. {
  992. editor.graph.setSelectionCell(editor.groupCells());
  993. }
  994. });
  995. this.addAction('ungroup', function(editor)
  996. {
  997. if (editor.graph.isEnabled())
  998. {
  999. editor.graph.setSelectionCells(editor.graph.ungroupCells());
  1000. }
  1001. });
  1002. this.addAction('removeFromParent', function(editor)
  1003. {
  1004. if (editor.graph.isEnabled())
  1005. {
  1006. editor.graph.removeCellsFromParent();
  1007. }
  1008. });
  1009. this.addAction('undo', function(editor)
  1010. {
  1011. if (editor.graph.isEnabled())
  1012. {
  1013. editor.undo();
  1014. }
  1015. });
  1016. this.addAction('redo', function(editor)
  1017. {
  1018. if (editor.graph.isEnabled())
  1019. {
  1020. editor.redo();
  1021. }
  1022. });
  1023. this.addAction('zoomIn', function(editor)
  1024. {
  1025. editor.graph.zoomIn();
  1026. });
  1027. this.addAction('zoomOut', function(editor)
  1028. {
  1029. editor.graph.zoomOut();
  1030. });
  1031. this.addAction('actualSize', function(editor)
  1032. {
  1033. editor.graph.zoomActual();
  1034. });
  1035. this.addAction('fit', function(editor)
  1036. {
  1037. editor.graph.fit();
  1038. });
  1039. this.addAction('showProperties', function(editor, cell)
  1040. {
  1041. editor.showProperties(cell);
  1042. });
  1043. this.addAction('selectAll', function(editor)
  1044. {
  1045. if (editor.graph.isEnabled())
  1046. {
  1047. editor.graph.selectAll();
  1048. }
  1049. });
  1050. this.addAction('selectNone', function(editor)
  1051. {
  1052. if (editor.graph.isEnabled())
  1053. {
  1054. editor.graph.clearSelection();
  1055. }
  1056. });
  1057. this.addAction('selectVertices', function(editor)
  1058. {
  1059. if (editor.graph.isEnabled())
  1060. {
  1061. editor.graph.selectVertices();
  1062. }
  1063. });
  1064. this.addAction('selectEdges', function(editor)
  1065. {
  1066. if (editor.graph.isEnabled())
  1067. {
  1068. editor.graph.selectEdges();
  1069. }
  1070. });
  1071. this.addAction('edit', function(editor, cell)
  1072. {
  1073. if (editor.graph.isEnabled() &&
  1074. editor.graph.isCellEditable(cell))
  1075. {
  1076. editor.graph.startEditingAtCell(cell);
  1077. }
  1078. });
  1079. this.addAction('toBack', function(editor, cell)
  1080. {
  1081. if (editor.graph.isEnabled())
  1082. {
  1083. editor.graph.orderCells(true);
  1084. }
  1085. });
  1086. this.addAction('toFront', function(editor, cell)
  1087. {
  1088. if (editor.graph.isEnabled())
  1089. {
  1090. editor.graph.orderCells(false);
  1091. }
  1092. });
  1093. this.addAction('enterGroup', function(editor, cell)
  1094. {
  1095. editor.graph.enterGroup(cell);
  1096. });
  1097. this.addAction('exitGroup', function(editor)
  1098. {
  1099. editor.graph.exitGroup();
  1100. });
  1101. this.addAction('home', function(editor)
  1102. {
  1103. editor.graph.home();
  1104. });
  1105. this.addAction('selectPrevious', function(editor)
  1106. {
  1107. if (editor.graph.isEnabled())
  1108. {
  1109. editor.graph.selectPreviousCell();
  1110. }
  1111. });
  1112. this.addAction('selectNext', function(editor)
  1113. {
  1114. if (editor.graph.isEnabled())
  1115. {
  1116. editor.graph.selectNextCell();
  1117. }
  1118. });
  1119. this.addAction('selectParent', function(editor)
  1120. {
  1121. if (editor.graph.isEnabled())
  1122. {
  1123. editor.graph.selectParentCell();
  1124. }
  1125. });
  1126. this.addAction('selectChild', function(editor)
  1127. {
  1128. if (editor.graph.isEnabled())
  1129. {
  1130. editor.graph.selectChildCell();
  1131. }
  1132. });
  1133. this.addAction('collapse', function(editor)
  1134. {
  1135. if (editor.graph.isEnabled())
  1136. {
  1137. editor.graph.foldCells(true);
  1138. }
  1139. });
  1140. this.addAction('collapseAll', function(editor)
  1141. {
  1142. if (editor.graph.isEnabled())
  1143. {
  1144. var cells = editor.graph.getChildVertices();
  1145. editor.graph.foldCells(true, false, cells);
  1146. }
  1147. });
  1148. this.addAction('expand', function(editor)
  1149. {
  1150. if (editor.graph.isEnabled())
  1151. {
  1152. editor.graph.foldCells(false);
  1153. }
  1154. });
  1155. this.addAction('expandAll', function(editor)
  1156. {
  1157. if (editor.graph.isEnabled())
  1158. {
  1159. var cells = editor.graph.getChildVertices();
  1160. editor.graph.foldCells(false, false, cells);
  1161. }
  1162. });
  1163. this.addAction('bold', function(editor)
  1164. {
  1165. if (editor.graph.isEnabled())
  1166. {
  1167. editor.graph.toggleCellStyleFlags(
  1168. mxConstants.STYLE_FONTSTYLE,
  1169. mxConstants.FONT_BOLD);
  1170. }
  1171. });
  1172. this.addAction('italic', function(editor)
  1173. {
  1174. if (editor.graph.isEnabled())
  1175. {
  1176. editor.graph.toggleCellStyleFlags(
  1177. mxConstants.STYLE_FONTSTYLE,
  1178. mxConstants.FONT_ITALIC);
  1179. }
  1180. });
  1181. this.addAction('underline', function(editor)
  1182. {
  1183. if (editor.graph.isEnabled())
  1184. {
  1185. editor.graph.toggleCellStyleFlags(
  1186. mxConstants.STYLE_FONTSTYLE,
  1187. mxConstants.FONT_UNDERLINE);
  1188. }
  1189. });
  1190. this.addAction('alignCellsLeft', function(editor)
  1191. {
  1192. if (editor.graph.isEnabled())
  1193. {
  1194. editor.graph.alignCells(mxConstants.ALIGN_LEFT);
  1195. }
  1196. });
  1197. this.addAction('alignCellsCenter', function(editor)
  1198. {
  1199. if (editor.graph.isEnabled())
  1200. {
  1201. editor.graph.alignCells(mxConstants.ALIGN_CENTER);
  1202. }
  1203. });
  1204. this.addAction('alignCellsRight', function(editor)
  1205. {
  1206. if (editor.graph.isEnabled())
  1207. {
  1208. editor.graph.alignCells(mxConstants.ALIGN_RIGHT);
  1209. }
  1210. });
  1211. this.addAction('alignCellsTop', function(editor)
  1212. {
  1213. if (editor.graph.isEnabled())
  1214. {
  1215. editor.graph.alignCells(mxConstants.ALIGN_TOP);
  1216. }
  1217. });
  1218. this.addAction('alignCellsMiddle', function(editor)
  1219. {
  1220. if (editor.graph.isEnabled())
  1221. {
  1222. editor.graph.alignCells(mxConstants.ALIGN_MIDDLE);
  1223. }
  1224. });
  1225. this.addAction('alignCellsBottom', function(editor)
  1226. {
  1227. if (editor.graph.isEnabled())
  1228. {
  1229. editor.graph.alignCells(mxConstants.ALIGN_BOTTOM);
  1230. }
  1231. });
  1232. this.addAction('alignFontLeft', function(editor)
  1233. {
  1234. editor.graph.setCellStyles(
  1235. mxConstants.STYLE_ALIGN,
  1236. mxConstants.ALIGN_LEFT);
  1237. });
  1238. this.addAction('alignFontCenter', function(editor)
  1239. {
  1240. if (editor.graph.isEnabled())
  1241. {
  1242. editor.graph.setCellStyles(
  1243. mxConstants.STYLE_ALIGN,
  1244. mxConstants.ALIGN_CENTER);
  1245. }
  1246. });
  1247. this.addAction('alignFontRight', function(editor)
  1248. {
  1249. if (editor.graph.isEnabled())
  1250. {
  1251. editor.graph.setCellStyles(
  1252. mxConstants.STYLE_ALIGN,
  1253. mxConstants.ALIGN_RIGHT);
  1254. }
  1255. });
  1256. this.addAction('alignFontTop', function(editor)
  1257. {
  1258. if (editor.graph.isEnabled())
  1259. {
  1260. editor.graph.setCellStyles(
  1261. mxConstants.STYLE_VERTICAL_ALIGN,
  1262. mxConstants.ALIGN_TOP);
  1263. }
  1264. });
  1265. this.addAction('alignFontMiddle', function(editor)
  1266. {
  1267. if (editor.graph.isEnabled())
  1268. {
  1269. editor.graph.setCellStyles(
  1270. mxConstants.STYLE_VERTICAL_ALIGN,
  1271. mxConstants.ALIGN_MIDDLE);
  1272. }
  1273. });
  1274. this.addAction('alignFontBottom', function(editor)
  1275. {
  1276. if (editor.graph.isEnabled())
  1277. {
  1278. editor.graph.setCellStyles(
  1279. mxConstants.STYLE_VERTICAL_ALIGN,
  1280. mxConstants.ALIGN_BOTTOM);
  1281. }
  1282. });
  1283. this.addAction('zoom', function(editor)
  1284. {
  1285. var current = editor.graph.getView().scale*100;
  1286. var scale = parseFloat(mxUtils.prompt(
  1287. mxResources.get(editor.askZoomResource) ||
  1288. editor.askZoomResource,
  1289. current))/100;
  1290. if (!isNaN(scale))
  1291. {
  1292. editor.graph.getView().setScale(scale);
  1293. }
  1294. });
  1295. this.addAction('toggleTasks', function(editor)
  1296. {
  1297. if (editor.tasks != null)
  1298. {
  1299. editor.tasks.setVisible(!editor.tasks.isVisible());
  1300. }
  1301. else
  1302. {
  1303. editor.showTasks();
  1304. }
  1305. });
  1306. this.addAction('toggleHelp', function(editor)
  1307. {
  1308. if (editor.help != null)
  1309. {
  1310. editor.help.setVisible(!editor.help.isVisible());
  1311. }
  1312. else
  1313. {
  1314. editor.showHelp();
  1315. }
  1316. });
  1317. this.addAction('toggleOutline', function(editor)
  1318. {
  1319. if (editor.outline == null)
  1320. {
  1321. editor.showOutline();
  1322. }
  1323. else
  1324. {
  1325. editor.outline.setVisible(!editor.outline.isVisible());
  1326. }
  1327. });
  1328. this.addAction('toggleConsole', function(editor)
  1329. {
  1330. mxLog.setVisible(!mxLog.isVisible());
  1331. });
  1332. };
  1333. /**
  1334. * Function: configure
  1335. *
  1336. * Configures the editor using the specified node. To load the
  1337. * configuration from a given URL the following code can be used to obtain
  1338. * the XML node.
  1339. *
  1340. * (code)
  1341. * var node = mxUtils.load(url).getDocumentElement();
  1342. * (end)
  1343. *
  1344. * Parameters:
  1345. *
  1346. * node - XML node that contains the configuration.
  1347. */
  1348. mxEditor.prototype.configure = function (node)
  1349. {
  1350. if (node != null)
  1351. {
  1352. // Creates a decoder for the XML data
  1353. // and uses it to configure the editor
  1354. var dec = new mxCodec(node.ownerDocument);
  1355. dec.decode(node, this);
  1356. // Resets the counters, modified state and
  1357. // command history
  1358. this.resetHistory();
  1359. }
  1360. };
  1361. /**
  1362. * Function: resetFirstTime
  1363. *
  1364. * Resets the cookie that is used to remember if the editor has already
  1365. * been used.
  1366. */
  1367. mxEditor.prototype.resetFirstTime = function ()
  1368. {
  1369. document.cookie =
  1370. 'mxgraph=seen; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';
  1371. };
  1372. /**
  1373. * Function: resetHistory
  1374. *
  1375. * Resets the command history, modified state and counters.
  1376. */
  1377. mxEditor.prototype.resetHistory = function ()
  1378. {
  1379. this.lastSnapshot = new Date().getTime();
  1380. this.undoManager.clear();
  1381. this.ignoredChanges = 0;
  1382. this.setModified(false);
  1383. };
  1384. /**
  1385. * Function: addAction
  1386. *
  1387. * Binds the specified actionname to the specified function.
  1388. *
  1389. * Parameters:
  1390. *
  1391. * actionname - String that specifies the name of the action
  1392. * to be added.
  1393. * funct - Function that implements the new action. The first
  1394. * argument of the function is the editor it is used
  1395. * with, the second argument is the cell it operates
  1396. * upon.
  1397. *
  1398. * Example:
  1399. * (code)
  1400. * editor.addAction('test', function(editor, cell)
  1401. * {
  1402. * mxUtils.alert("test "+cell);
  1403. * });
  1404. * (end)
  1405. */
  1406. mxEditor.prototype.addAction = function (actionname, funct)
  1407. {
  1408. this.actions[actionname] = funct;
  1409. };
  1410. /**
  1411. * Function: execute
  1412. *
  1413. * Executes the function with the given name in <actions> passing the
  1414. * editor instance and given cell as the first and second argument. All
  1415. * additional arguments are passed to the action as well. This method
  1416. * contains a try-catch block and displays an error message if an action
  1417. * causes an exception. The exception is re-thrown after the error
  1418. * message was displayed.
  1419. *
  1420. * Example:
  1421. *
  1422. * (code)
  1423. * editor.execute("showProperties", cell);
  1424. * (end)
  1425. */
  1426. mxEditor.prototype.execute = function (actionname, cell, evt)
  1427. {
  1428. var action = this.actions[actionname];
  1429. if (action != null)
  1430. {
  1431. try
  1432. {
  1433. // Creates the array of arguments by replacing the actionname
  1434. // with the editor instance in the args of this function
  1435. var args = arguments;
  1436. args[0] = this;
  1437. // Invokes the function on the editor using the args
  1438. action.apply(this, args);
  1439. }
  1440. catch (e)
  1441. {
  1442. mxUtils.error('Cannot execute ' + actionname +
  1443. ': ' + e.message, 280, true);
  1444. throw e;
  1445. }
  1446. }
  1447. else
  1448. {
  1449. mxUtils.error('Cannot find action '+actionname, 280, true);
  1450. }
  1451. };
  1452. /**
  1453. * Function: addTemplate
  1454. *
  1455. * Adds the specified template under the given name in <templates>.
  1456. */
  1457. mxEditor.prototype.addTemplate = function (name, template)
  1458. {
  1459. this.templates[name] = template;
  1460. };
  1461. /**
  1462. * Function: getTemplate
  1463. *
  1464. * Returns the template for the given name.
  1465. */
  1466. mxEditor.prototype.getTemplate = function (name)
  1467. {
  1468. return this.templates[name];
  1469. };
  1470. /**
  1471. * Function: createGraph
  1472. *
  1473. * Creates the <graph> for the editor. The graph is created with no
  1474. * container and is initialized from <setGraphContainer>.
  1475. */
  1476. mxEditor.prototype.createGraph = function ()
  1477. {
  1478. var graph = new mxGraph(null, null, this.graphRenderHint);
  1479. // Enables rubberband, tooltips, panning
  1480. graph.setTooltips(true);
  1481. graph.setPanning(true);
  1482. // Overrides the dblclick method on the graph to
  1483. // invoke the dblClickAction for a cell and reset
  1484. // the selection tool in the toolbar
  1485. this.installDblClickHandler(graph);
  1486. // Installs the command history
  1487. this.installUndoHandler(graph);
  1488. // Installs the handlers for the root event
  1489. this.installDrillHandler(graph);
  1490. // Installs the handler for validation
  1491. this.installChangeHandler(graph);
  1492. // Installs the handler for calling the
  1493. // insert function and consume the
  1494. // event if an insert function is defined
  1495. this.installInsertHandler(graph);
  1496. // Redirects the function for creating the
  1497. // popupmenu items
  1498. graph.popupMenuHandler.factoryMethod =
  1499. mxUtils.bind(this, function(menu, cell, evt)
  1500. {
  1501. return this.createPopupMenu(menu, cell, evt);
  1502. });
  1503. // Redirects the function for creating
  1504. // new connections in the diagram
  1505. graph.connectionHandler.factoryMethod =
  1506. mxUtils.bind(this, function(source, target)
  1507. {
  1508. return this.createEdge(source, target);
  1509. });
  1510. // Maintains swimlanes and installs autolayout
  1511. this.createSwimlaneManager(graph);
  1512. this.createLayoutManager(graph);
  1513. return graph;
  1514. };
  1515. /**
  1516. * Function: createSwimlaneManager
  1517. *
  1518. * Sets the graph's container using <mxGraph.init>.
  1519. */
  1520. mxEditor.prototype.createSwimlaneManager = function (graph)
  1521. {
  1522. var swimlaneMgr = new mxSwimlaneManager(graph, false);
  1523. swimlaneMgr.isHorizontal = mxUtils.bind(this, function()
  1524. {
  1525. return this.horizontalFlow;
  1526. });
  1527. swimlaneMgr.isEnabled = mxUtils.bind(this, function()
  1528. {
  1529. return this.maintainSwimlanes;
  1530. });
  1531. return swimlaneMgr;
  1532. };
  1533. /**
  1534. * Function: createLayoutManager
  1535. *
  1536. * Creates a layout manager for the swimlane and diagram layouts, that
  1537. * is, the locally defined inter- and intraswimlane layouts.
  1538. */
  1539. mxEditor.prototype.createLayoutManager = function (graph)
  1540. {
  1541. var layoutMgr = new mxLayoutManager(graph);
  1542. var self = this; // closure
  1543. layoutMgr.getLayout = function(cell)
  1544. {
  1545. var layout = null;
  1546. var model = self.graph.getModel();
  1547. if (model.getParent(cell) != null)
  1548. {
  1549. // Executes the swimlane layout if a child of
  1550. // a swimlane has been changed. The layout is
  1551. // lazy created in createSwimlaneLayout.
  1552. if (self.layoutSwimlanes &&
  1553. graph.isSwimlane(cell))
  1554. {
  1555. if (self.swimlaneLayout == null)
  1556. {
  1557. self.swimlaneLayout = self.createSwimlaneLayout();
  1558. }
  1559. layout = self.swimlaneLayout;
  1560. }
  1561. // Executes the diagram layout if the modified
  1562. // cell is a top-level cell. The layout is
  1563. // lazy created in createDiagramLayout.
  1564. else if (self.layoutDiagram &&
  1565. (graph.isValidRoot(cell) ||
  1566. model.getParent(model.getParent(cell)) == null))
  1567. {
  1568. if (self.diagramLayout == null)
  1569. {
  1570. self.diagramLayout = self.createDiagramLayout();
  1571. }
  1572. layout = self.diagramLayout;
  1573. }
  1574. }
  1575. return layout;
  1576. };
  1577. return layoutMgr;
  1578. };
  1579. /**
  1580. * Function: setGraphContainer
  1581. *
  1582. * Sets the graph's container using <mxGraph.init>.
  1583. */
  1584. mxEditor.prototype.setGraphContainer = function (container)
  1585. {
  1586. if (this.graph.container == null)
  1587. {
  1588. // Creates the graph instance inside the given container and render hint
  1589. //this.graph = new mxGraph(container, null, this.graphRenderHint);
  1590. this.graph.init(container);
  1591. // Install rubberband selection as the last
  1592. // action handler in the chain
  1593. this.rubberband = new mxRubberband(this.graph);
  1594. // Disables the context menu
  1595. if (this.disableContextMenu)
  1596. {
  1597. mxEvent.disableContextMenu(container);
  1598. }
  1599. // Workaround for stylesheet directives in IE
  1600. if (mxClient.IS_QUIRKS)
  1601. {
  1602. new mxDivResizer(container);
  1603. }
  1604. }
  1605. };
  1606. /**
  1607. * Function: installDblClickHandler
  1608. *
  1609. * Overrides <mxGraph.dblClick> to invoke <dblClickAction>
  1610. * on a cell and reset the selection tool in the toolbar.
  1611. */
  1612. mxEditor.prototype.installDblClickHandler = function (graph)
  1613. {
  1614. // Installs a listener for double click events
  1615. graph.addListener(mxEvent.DOUBLE_CLICK,
  1616. mxUtils.bind(this, function(sender, evt)
  1617. {
  1618. var cell = evt.getProperty('cell');
  1619. if (cell != null &&
  1620. graph.isEnabled() &&
  1621. this.dblClickAction != null)
  1622. {
  1623. this.execute(this.dblClickAction, cell);
  1624. evt.consume();
  1625. }
  1626. })
  1627. );
  1628. };
  1629. /**
  1630. * Function: installUndoHandler
  1631. *
  1632. * Adds the <undoManager> to the graph model and the view.
  1633. */
  1634. mxEditor.prototype.installUndoHandler = function (graph)
  1635. {
  1636. var listener = mxUtils.bind(this, function(sender, evt)
  1637. {
  1638. var edit = evt.getProperty('edit');
  1639. this.undoManager.undoableEditHappened(edit);
  1640. });
  1641. graph.getModel().addListener(mxEvent.UNDO, listener);
  1642. graph.getView().addListener(mxEvent.UNDO, listener);
  1643. // Keeps the selection state in sync
  1644. var undoHandler = function(sender, evt)
  1645. {
  1646. var changes = evt.getProperty('edit').changes;
  1647. graph.setSelectionCells(graph.getSelectionCellsForChanges(changes));
  1648. };
  1649. this.undoManager.addListener(mxEvent.UNDO, undoHandler);
  1650. this.undoManager.addListener(mxEvent.REDO, undoHandler);
  1651. };
  1652. /**
  1653. * Function: installDrillHandler
  1654. *
  1655. * Installs listeners for dispatching the <root> event.
  1656. */
  1657. mxEditor.prototype.installDrillHandler = function (graph)
  1658. {
  1659. var listener = mxUtils.bind(this, function(sender)
  1660. {
  1661. this.fireEvent(new mxEventObject(mxEvent.ROOT));
  1662. });
  1663. graph.getView().addListener(mxEvent.DOWN, listener);
  1664. graph.getView().addListener(mxEvent.UP, listener);
  1665. };
  1666. /**
  1667. * Function: installChangeHandler
  1668. *
  1669. * Installs the listeners required to automatically validate
  1670. * the graph. On each change of the root, this implementation
  1671. * fires a <root> event.
  1672. */
  1673. mxEditor.prototype.installChangeHandler = function (graph)
  1674. {
  1675. var listener = mxUtils.bind(this, function(sender, evt)
  1676. {
  1677. // Updates the modified state
  1678. this.setModified(true);
  1679. // Automatically validates the graph
  1680. // after each change
  1681. if (this.validating == true)
  1682. {
  1683. graph.validateGraph();
  1684. }
  1685. // Checks if the root has been changed
  1686. var changes = evt.getProperty('edit').changes;
  1687. for (var i = 0; i < changes.length; i++)
  1688. {
  1689. var change = changes[i];
  1690. if (change instanceof mxRootChange ||
  1691. (change instanceof mxValueChange &&
  1692. change.cell == this.graph.model.root) ||
  1693. (change instanceof mxCellAttributeChange &&
  1694. change.cell == this.graph.model.root))
  1695. {
  1696. this.fireEvent(new mxEventObject(mxEvent.ROOT));
  1697. break;
  1698. }
  1699. }
  1700. });
  1701. graph.getModel().addListener(mxEvent.CHANGE, listener);
  1702. };
  1703. /**
  1704. * Function: installInsertHandler
  1705. *
  1706. * Installs the handler for invoking <insertFunction> if
  1707. * one is defined.
  1708. */
  1709. mxEditor.prototype.installInsertHandler = function (graph)
  1710. {
  1711. var self = this; // closure
  1712. var insertHandler =
  1713. {
  1714. mouseDown: function(sender, me)
  1715. {
  1716. if (self.insertFunction != null &&
  1717. !me.isPopupTrigger() &&
  1718. (self.forcedInserting ||
  1719. me.getState() == null))
  1720. {
  1721. self.graph.clearSelection();
  1722. self.insertFunction(me.getEvent(), me.getCell());
  1723. // Consumes the rest of the events
  1724. // for this gesture (down, move, up)
  1725. this.isActive = true;
  1726. me.consume();
  1727. }
  1728. },
  1729. mouseMove: function(sender, me)
  1730. {
  1731. if (this.isActive)
  1732. {
  1733. me.consume();
  1734. }
  1735. },
  1736. mouseUp: function(sender, me)
  1737. {
  1738. if (this.isActive)
  1739. {
  1740. this.isActive = false;
  1741. me.consume();
  1742. }
  1743. }
  1744. };
  1745. graph.addMouseListener(insertHandler);
  1746. };
  1747. /**
  1748. * Function: createDiagramLayout
  1749. *
  1750. * Creates the layout instance used to layout the
  1751. * swimlanes in the diagram.
  1752. */
  1753. mxEditor.prototype.createDiagramLayout = function ()
  1754. {
  1755. var gs = this.graph.gridSize;
  1756. var layout = new mxStackLayout(this.graph, !this.horizontalFlow,
  1757. this.swimlaneSpacing, 2*gs, 2*gs);
  1758. // Overrides isIgnored to only take into account swimlanes
  1759. layout.isVertexIgnored = function(cell)
  1760. {
  1761. return !layout.graph.isSwimlane(cell);
  1762. };
  1763. return layout;
  1764. };
  1765. /**
  1766. * Function: createSwimlaneLayout
  1767. *
  1768. * Creates the layout instance used to layout the
  1769. * children of each swimlane.
  1770. */
  1771. mxEditor.prototype.createSwimlaneLayout = function ()
  1772. {
  1773. return new mxCompactTreeLayout(this.graph, this.horizontalFlow);
  1774. };
  1775. /**
  1776. * Function: createToolbar
  1777. *
  1778. * Creates the <toolbar> with no container.
  1779. */
  1780. mxEditor.prototype.createToolbar = function ()
  1781. {
  1782. return new mxDefaultToolbar(null, this);
  1783. };
  1784. /**
  1785. * Function: setToolbarContainer
  1786. *
  1787. * Initializes the toolbar for the given container.
  1788. */
  1789. mxEditor.prototype.setToolbarContainer = function (container)
  1790. {
  1791. this.toolbar.init(container);
  1792. // Workaround for stylesheet directives in IE
  1793. if (mxClient.IS_QUIRKS)
  1794. {
  1795. new mxDivResizer(container);
  1796. }
  1797. };
  1798. /**
  1799. * Function: setStatusContainer
  1800. *
  1801. * Creates the <status> using the specified container.
  1802. *
  1803. * This implementation adds listeners in the editor to
  1804. * display the last saved time and the current filename
  1805. * in the status bar.
  1806. *
  1807. * Parameters:
  1808. *
  1809. * container - DOM node that will contain the statusbar.
  1810. */
  1811. mxEditor.prototype.setStatusContainer = function (container)
  1812. {
  1813. if (this.status == null)
  1814. {
  1815. this.status = container;
  1816. // Prints the last saved time in the status bar
  1817. // when files are saved
  1818. this.addListener(mxEvent.SAVE, mxUtils.bind(this, function()
  1819. {
  1820. var tstamp = new Date().toLocaleString();
  1821. this.setStatus((mxResources.get(this.lastSavedResource) ||
  1822. this.lastSavedResource)+': '+tstamp);
  1823. }));
  1824. // Updates the statusbar to display the filename
  1825. // when new files are opened
  1826. this.addListener(mxEvent.OPEN, mxUtils.bind(this, function()
  1827. {
  1828. this.setStatus((mxResources.get(this.currentFileResource) ||
  1829. this.currentFileResource)+': '+this.filename);
  1830. }));
  1831. // Workaround for stylesheet directives in IE
  1832. if (mxClient.IS_QUIRKS)
  1833. {
  1834. new mxDivResizer(container);
  1835. }
  1836. }
  1837. };
  1838. /**
  1839. * Function: setStatus
  1840. *
  1841. * Display the specified message in the status bar.
  1842. *
  1843. * Parameters:
  1844. *
  1845. * message - String the specified the message to
  1846. * be displayed.
  1847. */
  1848. mxEditor.prototype.setStatus = function (message)
  1849. {
  1850. if (this.status != null && message != null)
  1851. {
  1852. this.status.innerHTML = message;
  1853. }
  1854. };
  1855. /**
  1856. * Function: setTitleContainer
  1857. *
  1858. * Creates a listener to update the inner HTML of the
  1859. * specified DOM node with the value of <getTitle>.
  1860. *
  1861. * Parameters:
  1862. *
  1863. * container - DOM node that will contain the title.
  1864. */
  1865. mxEditor.prototype.setTitleContainer = function (container)
  1866. {
  1867. this.addListener(mxEvent.ROOT, mxUtils.bind(this, function(sender)
  1868. {
  1869. container.innerHTML = this.getTitle();
  1870. }));
  1871. // Workaround for stylesheet directives in IE
  1872. if (mxClient.IS_QUIRKS)
  1873. {
  1874. new mxDivResizer(container);
  1875. }
  1876. };
  1877. /**
  1878. * Function: treeLayout
  1879. *
  1880. * Executes a vertical or horizontal compact tree layout
  1881. * using the specified cell as an argument. The cell may
  1882. * either be a group or the root of a tree.
  1883. *
  1884. * Parameters:
  1885. *
  1886. * cell - <mxCell> to use in the compact tree layout.
  1887. * horizontal - Optional boolean to specify the tree's
  1888. * orientation. Default is true.
  1889. */
  1890. mxEditor.prototype.treeLayout = function (cell, horizontal)
  1891. {
  1892. if (cell != null)
  1893. {
  1894. var layout = new mxCompactTreeLayout(this.graph, horizontal);
  1895. layout.execute(cell);
  1896. }
  1897. };
  1898. /**
  1899. * Function: getTitle
  1900. *
  1901. * Returns the string value for the current root of the
  1902. * diagram.
  1903. */
  1904. mxEditor.prototype.getTitle = function ()
  1905. {
  1906. var title = '';
  1907. var graph = this.graph;
  1908. var cell = graph.getCurrentRoot();
  1909. while (cell != null &&
  1910. graph.getModel().getParent(
  1911. graph.getModel().getParent(cell)) != null)
  1912. {
  1913. // Append each label of a valid root
  1914. if (graph.isValidRoot(cell))
  1915. {
  1916. title = ' > ' +
  1917. graph.convertValueToString(cell) + title;
  1918. }
  1919. cell = graph.getModel().getParent(cell);
  1920. }
  1921. var prefix = this.getRootTitle();
  1922. return prefix + title;
  1923. };
  1924. /**
  1925. * Function: getRootTitle
  1926. *
  1927. * Returns the string value of the root cell in
  1928. * <mxGraph.model>.
  1929. */
  1930. mxEditor.prototype.getRootTitle = function ()
  1931. {
  1932. var root = this.graph.getModel().getRoot();
  1933. return this.graph.convertValueToString(root);
  1934. };
  1935. /**
  1936. * Function: undo
  1937. *
  1938. * Undo the last change in <graph>.
  1939. */
  1940. mxEditor.prototype.undo = function ()
  1941. {
  1942. this.undoManager.undo();
  1943. };
  1944. /**
  1945. * Function: redo
  1946. *
  1947. * Redo the last change in <graph>.
  1948. */
  1949. mxEditor.prototype.redo = function ()
  1950. {
  1951. this.undoManager.redo();
  1952. };
  1953. /**
  1954. * Function: groupCells
  1955. *
  1956. * Invokes <createGroup> to create a new group cell and the invokes
  1957. * <mxGraph.groupCells>, using the grid size of the graph as the spacing
  1958. * in the group's content area.
  1959. */
  1960. mxEditor.prototype.groupCells = function ()
  1961. {
  1962. var border = (this.groupBorderSize != null) ?
  1963. this.groupBorderSize :
  1964. this.graph.gridSize;
  1965. return this.graph.groupCells(this.createGroup(), border);
  1966. };
  1967. /**
  1968. * Function: createGroup
  1969. *
  1970. * Creates and returns a clone of <defaultGroup> to be used
  1971. * as a new group cell in <group>.
  1972. */
  1973. mxEditor.prototype.createGroup = function ()
  1974. {
  1975. var model = this.graph.getModel();
  1976. return model.cloneCell(this.defaultGroup);
  1977. };
  1978. /**
  1979. * Function: open
  1980. *
  1981. * Opens the specified file synchronously and parses it using
  1982. * <readGraphModel>. It updates <filename> and fires an <open>-event after
  1983. * the file has been opened. Exceptions should be handled as follows:
  1984. *
  1985. * (code)
  1986. * try
  1987. * {
  1988. * editor.open(filename);
  1989. * }
  1990. * catch (e)
  1991. * {
  1992. * mxUtils.error('Cannot open ' + filename +
  1993. * ': ' + e.message, 280, true);
  1994. * }
  1995. * (end)
  1996. *
  1997. * Parameters:
  1998. *
  1999. * filename - URL of the file to be opened.
  2000. */
  2001. mxEditor.prototype.open = function (filename)
  2002. {
  2003. if (filename != null)
  2004. {
  2005. var xml = mxUtils.load(filename).getXml();
  2006. this.readGraphModel(xml.documentElement);
  2007. this.filename = filename;
  2008. this.fireEvent(new mxEventObject(mxEvent.OPEN, 'filename', filename));
  2009. }
  2010. };
  2011. /**
  2012. * Function: readGraphModel
  2013. *
  2014. * Reads the specified XML node into the existing graph model and resets
  2015. * the command history and modified state.
  2016. */
  2017. mxEditor.prototype.readGraphModel = function (node)
  2018. {
  2019. var dec = new mxCodec(node.ownerDocument);
  2020. dec.decode(node, this.graph.getModel());
  2021. this.resetHistory();
  2022. };
  2023. /**
  2024. * Function: save
  2025. *
  2026. * Posts the string returned by <writeGraphModel> to the given URL or the
  2027. * URL returned by <getUrlPost>. The actual posting is carried out by
  2028. * <postDiagram>. If the URL is null then the resulting XML will be
  2029. * displayed using <mxUtils.popup>. Exceptions should be handled as
  2030. * follows:
  2031. *
  2032. * (code)
  2033. * try
  2034. * {
  2035. * editor.save();
  2036. * }
  2037. * catch (e)
  2038. * {
  2039. * mxUtils.error('Cannot save : ' + e.message, 280, true);
  2040. * }
  2041. * (end)
  2042. */
  2043. mxEditor.prototype.save = function (url, linefeed)
  2044. {
  2045. // Gets the URL to post the data to
  2046. url = url || this.getUrlPost();
  2047. // Posts the data if the URL is not empty
  2048. if (url != null && url.length > 0)
  2049. {
  2050. var data = this.writeGraphModel(linefeed);
  2051. this.postDiagram(url, data);
  2052. // Resets the modified flag
  2053. this.setModified(false);
  2054. }
  2055. // Dispatches a save event
  2056. this.fireEvent(new mxEventObject(mxEvent.SAVE, 'url', url));
  2057. };
  2058. /**
  2059. * Function: postDiagram
  2060. *
  2061. * Hook for subclassers to override the posting of a diagram
  2062. * represented by the given node to the given URL. This fires
  2063. * an asynchronous <post> event if the diagram has been posted.
  2064. *
  2065. * Example:
  2066. *
  2067. * To replace the diagram with the diagram in the response, use the
  2068. * following code.
  2069. *
  2070. * (code)
  2071. * editor.addListener(mxEvent.POST, function(sender, evt)
  2072. * {
  2073. * // Process response (replace diagram)
  2074. * var req = evt.getProperty('request');
  2075. * var root = req.getDocumentElement();
  2076. * editor.graph.readGraphModel(root)
  2077. * });
  2078. * (end)
  2079. */
  2080. mxEditor.prototype.postDiagram = function (url, data)
  2081. {
  2082. if (this.escapePostData)
  2083. {
  2084. data = encodeURIComponent(data);
  2085. }
  2086. mxUtils.post(url, this.postParameterName+'='+data,
  2087. mxUtils.bind(this, function(req)
  2088. {
  2089. this.fireEvent(new mxEventObject(mxEvent.POST,
  2090. 'request', req, 'url', url, 'data', data));
  2091. })
  2092. );
  2093. };
  2094. /**
  2095. * Function: writeGraphModel
  2096. *
  2097. * Hook to create the string representation of the diagram. The default
  2098. * implementation uses an <mxCodec> to encode the graph model as
  2099. * follows:
  2100. *
  2101. * (code)
  2102. * var enc = new mxCodec();
  2103. * var node = enc.encode(this.graph.getModel());
  2104. * return mxUtils.getXml(node, this.linefeed);
  2105. * (end)
  2106. *
  2107. * Parameters:
  2108. *
  2109. * linefeed - Optional character to be used as the linefeed. Default is
  2110. * <linefeed>.
  2111. */
  2112. mxEditor.prototype.writeGraphModel = function (linefeed)
  2113. {
  2114. linefeed = (linefeed != null) ? linefeed : this.linefeed;
  2115. var enc = new mxCodec();
  2116. var node = enc.encode(this.graph.getModel());
  2117. return mxUtils.getXml(node, linefeed);
  2118. };
  2119. /**
  2120. * Function: getUrlPost
  2121. *
  2122. * Returns the URL to post the diagram to. This is used
  2123. * in <save>. The default implementation returns <urlPost>,
  2124. * adding <code>?draft=true</code>.
  2125. */
  2126. mxEditor.prototype.getUrlPost = function ()
  2127. {
  2128. return this.urlPost;
  2129. };
  2130. /**
  2131. * Function: getUrlImage
  2132. *
  2133. * Returns the URL to create the image with. This is typically
  2134. * the URL of a backend which accepts an XML representation
  2135. * of a graph view to create an image. The function is used
  2136. * in the image action to create an image. This implementation
  2137. * returns <urlImage>.
  2138. */
  2139. mxEditor.prototype.getUrlImage = function ()
  2140. {
  2141. return this.urlImage;
  2142. };
  2143. /**
  2144. * Function: swapStyles
  2145. *
  2146. * Swaps the styles for the given names in the graph's
  2147. * stylesheet and refreshes the graph.
  2148. */
  2149. mxEditor.prototype.swapStyles = function (first, second)
  2150. {
  2151. var style = this.graph.getStylesheet().styles[second];
  2152. this.graph.getView().getStylesheet().putCellStyle(
  2153. second, this.graph.getStylesheet().styles[first]);
  2154. this.graph.getStylesheet().putCellStyle(first, style);
  2155. this.graph.refresh();
  2156. };
  2157. /**
  2158. * Function: showProperties
  2159. *
  2160. * Creates and shows the properties dialog for the given
  2161. * cell. The content area of the dialog is created using
  2162. * <createProperties>.
  2163. */
  2164. mxEditor.prototype.showProperties = function (cell)
  2165. {
  2166. cell = cell || this.graph.getSelectionCell();
  2167. // Uses the root node for the properties dialog
  2168. // if not cell was passed in and no cell is
  2169. // selected
  2170. if (cell == null)
  2171. {
  2172. cell = this.graph.getCurrentRoot();
  2173. if (cell == null)
  2174. {
  2175. cell = this.graph.getModel().getRoot();
  2176. }
  2177. }
  2178. if (cell != null)
  2179. {
  2180. // Makes sure there is no in-place editor in the
  2181. // graph and computes the location of the dialog
  2182. this.graph.stopEditing(true);
  2183. var offset = mxUtils.getOffset(this.graph.container);
  2184. var x = offset.x+10;
  2185. var y = offset.y;
  2186. // Avoids moving the dialog if it is alredy open
  2187. if (this.properties != null && !this.movePropertiesDialog)
  2188. {
  2189. x = this.properties.getX();
  2190. y = this.properties.getY();
  2191. }
  2192. // Places the dialog near the cell for which it
  2193. // displays the properties
  2194. else
  2195. {
  2196. var bounds = this.graph.getCellBounds(cell);
  2197. if (bounds != null)
  2198. {
  2199. x += bounds.x+Math.min(200, bounds.width);
  2200. y += bounds.y;
  2201. }
  2202. }
  2203. // Hides the existing properties dialog and creates a new one with the
  2204. // contents created in the hook method
  2205. this.hideProperties();
  2206. var node = this.createProperties(cell);
  2207. if (node != null)
  2208. {
  2209. // Displays the contents in a window and stores a reference to the
  2210. // window for later hiding of the window
  2211. this.properties = new mxWindow(mxResources.get(this.propertiesResource) ||
  2212. this.propertiesResource, node, x, y, this.propertiesWidth, this.propertiesHeight, false);
  2213. this.properties.setVisible(true);
  2214. }
  2215. }
  2216. };
  2217. /**
  2218. * Function: isPropertiesVisible
  2219. *
  2220. * Returns true if the properties dialog is currently visible.
  2221. */
  2222. mxEditor.prototype.isPropertiesVisible = function ()
  2223. {
  2224. return this.properties != null;
  2225. };
  2226. /**
  2227. * Function: createProperties
  2228. *
  2229. * Creates and returns the DOM node that represents the contents
  2230. * of the properties dialog for the given cell. This implementation
  2231. * works for user objects that are XML nodes and display all the
  2232. * node attributes in a form.
  2233. */
  2234. mxEditor.prototype.createProperties = function (cell)
  2235. {
  2236. var model = this.graph.getModel();
  2237. var value = model.getValue(cell);
  2238. if (mxUtils.isNode(value))
  2239. {
  2240. // Creates a form for the user object inside
  2241. // the cell
  2242. var form = new mxForm('properties');
  2243. // Adds a readonly field for the cell id
  2244. var id = form.addText('ID', cell.getId());
  2245. id.setAttribute('readonly', 'true');
  2246. var geo = null;
  2247. var yField = null;
  2248. var xField = null;
  2249. var widthField = null;
  2250. var heightField = null;
  2251. // Adds fields for the location and size
  2252. if (model.isVertex(cell))
  2253. {
  2254. geo = model.getGeometry(cell);
  2255. if (geo != null)
  2256. {
  2257. yField = form.addText('top', geo.y);
  2258. xField = form.addText('left', geo.x);
  2259. widthField = form.addText('width', geo.width);
  2260. heightField = form.addText('height', geo.height);
  2261. }
  2262. }
  2263. // Adds a field for the cell style
  2264. var tmp = model.getStyle(cell);
  2265. var style = form.addText('Style', tmp || '');
  2266. // Creates textareas for each attribute of the
  2267. // user object within the cell
  2268. var attrs = value.attributes;
  2269. var texts = [];
  2270. for (var i = 0; i < attrs.length; i++)
  2271. {
  2272. // Creates a textarea with more lines for
  2273. // the cell label
  2274. var val = attrs[i].value;
  2275. texts[i] = form.addTextarea(attrs[i].nodeName, val,
  2276. (attrs[i].nodeName == 'label') ? 4 : 2);
  2277. }
  2278. // Adds an OK and Cancel button to the dialog
  2279. // contents and implements the respective
  2280. // actions below
  2281. // Defines the function to be executed when the
  2282. // OK button is pressed in the dialog
  2283. var okFunction = mxUtils.bind(this, function()
  2284. {
  2285. // Hides the dialog
  2286. this.hideProperties();
  2287. // Supports undo for the changes on the underlying
  2288. // XML structure / XML node attribute changes.
  2289. model.beginUpdate();
  2290. try
  2291. {
  2292. if (geo != null)
  2293. {
  2294. geo = geo.clone();
  2295. geo.x = parseFloat(xField.value);
  2296. geo.y = parseFloat(yField.value);
  2297. geo.width = parseFloat(widthField.value);
  2298. geo.height = parseFloat(heightField.value);
  2299. model.setGeometry(cell, geo);
  2300. }
  2301. // Applies the style
  2302. if (style.value.length > 0)
  2303. {
  2304. model.setStyle(cell, style.value);
  2305. }
  2306. else
  2307. {
  2308. model.setStyle(cell, null);
  2309. }
  2310. // Creates an undoable change for each
  2311. // attribute and executes it using the
  2312. // model, which will also make the change
  2313. // part of the current transaction
  2314. for (var i=0; i<attrs.length; i++)
  2315. {
  2316. var edit = new mxCellAttributeChange(
  2317. cell, attrs[i].nodeName,
  2318. texts[i].value);
  2319. model.execute(edit);
  2320. }
  2321. // Checks if the graph wants cells to
  2322. // be automatically sized and updates
  2323. // the size as an undoable step if
  2324. // the feature is enabled
  2325. if (this.graph.isAutoSizeCell(cell))
  2326. {
  2327. this.graph.updateCellSize(cell);
  2328. }
  2329. }
  2330. finally
  2331. {
  2332. model.endUpdate();
  2333. }
  2334. });
  2335. // Defines the function to be executed when the
  2336. // Cancel button is pressed in the dialog
  2337. var cancelFunction = mxUtils.bind(this, function()
  2338. {
  2339. // Hides the dialog
  2340. this.hideProperties();
  2341. });
  2342. form.addButtons(okFunction, cancelFunction);
  2343. return form.table;
  2344. }
  2345. return null;
  2346. };
  2347. /**
  2348. * Function: hideProperties
  2349. *
  2350. * Hides the properties dialog.
  2351. */
  2352. mxEditor.prototype.hideProperties = function ()
  2353. {
  2354. if (this.properties != null)
  2355. {
  2356. this.properties.destroy();
  2357. this.properties = null;
  2358. }
  2359. };
  2360. /**
  2361. * Function: showTasks
  2362. *
  2363. * Shows the tasks window. The tasks window is created using <createTasks>. The
  2364. * default width of the window is 200 pixels, the y-coordinate of the location
  2365. * can be specifies in <tasksTop> and the x-coordinate is right aligned with a
  2366. * 20 pixel offset from the right border. To change the location of the tasks
  2367. * window, the following code can be used:
  2368. *
  2369. * (code)
  2370. * var oldShowTasks = mxEditor.prototype.showTasks;
  2371. * mxEditor.prototype.showTasks = function()
  2372. * {
  2373. * oldShowTasks.apply(this, arguments); // "supercall"
  2374. *
  2375. * if (this.tasks != null)
  2376. * {
  2377. * this.tasks.setLocation(10, 10);
  2378. * }
  2379. * };
  2380. * (end)
  2381. */
  2382. mxEditor.prototype.showTasks = function ()
  2383. {
  2384. if (this.tasks == null)
  2385. {
  2386. var div = document.createElement('div');
  2387. div.style.padding = '4px';
  2388. div.style.paddingLeft = '20px';
  2389. var w = document.body.clientWidth;
  2390. var wnd = new mxWindow(
  2391. mxResources.get(this.tasksResource) ||
  2392. this.tasksResource,
  2393. div, w - 220, this.tasksTop, 200);
  2394. wnd.setClosable(true);
  2395. wnd.destroyOnClose = false;
  2396. // Installs a function to update the contents
  2397. // of the tasks window on every change of the
  2398. // model, selection or root.
  2399. var funct = mxUtils.bind(this, function(sender)
  2400. {
  2401. mxEvent.release(div);
  2402. div.innerHTML = '';
  2403. this.createTasks(div);
  2404. });
  2405. this.graph.getModel().addListener(mxEvent.CHANGE, funct);
  2406. this.graph.getSelectionModel().addListener(mxEvent.CHANGE, funct);
  2407. this.graph.addListener(mxEvent.ROOT, funct);
  2408. // Assigns the icon to the tasks window
  2409. if (this.tasksWindowImage != null)
  2410. {
  2411. wnd.setImage(this.tasksWindowImage);
  2412. }
  2413. this.tasks = wnd;
  2414. this.createTasks(div);
  2415. }
  2416. this.tasks.setVisible(true);
  2417. };
  2418. /**
  2419. * Function: refreshTasks
  2420. *
  2421. * Updates the contents of the tasks window using <createTasks>.
  2422. */
  2423. mxEditor.prototype.refreshTasks = function (div)
  2424. {
  2425. if (this.tasks != null)
  2426. {
  2427. var div = this.tasks.content;
  2428. mxEvent.release(div);
  2429. div.innerHTML = '';
  2430. this.createTasks(div);
  2431. }
  2432. };
  2433. /**
  2434. * Function: createTasks
  2435. *
  2436. * Updates the contents of the given DOM node to
  2437. * display the tasks associated with the current
  2438. * editor state. This is invoked whenever there
  2439. * is a possible change of state in the editor.
  2440. * Default implementation is empty.
  2441. */
  2442. mxEditor.prototype.createTasks = function (div)
  2443. {
  2444. // override
  2445. };
  2446. /**
  2447. * Function: showHelp
  2448. *
  2449. * Shows the help window. If the help window does not exist
  2450. * then it is created using an iframe pointing to the resource
  2451. * for the <code>urlHelp</code> key or <urlHelp> if the resource
  2452. * is undefined.
  2453. */
  2454. mxEditor.prototype.showHelp = function (tasks)
  2455. {
  2456. if (this.help == null)
  2457. {
  2458. var frame = document.createElement('iframe');
  2459. frame.setAttribute('src', mxResources.get('urlHelp') || this.urlHelp);
  2460. frame.setAttribute('height', '100%');
  2461. frame.setAttribute('width', '100%');
  2462. frame.setAttribute('frameBorder', '0');
  2463. frame.style.backgroundColor = 'white';
  2464. var w = document.body.clientWidth;
  2465. var h = (document.body.clientHeight || document.documentElement.clientHeight);
  2466. var wnd = new mxWindow(mxResources.get(this.helpResource) || this.helpResource,
  2467. frame, (w-this.helpWidth)/2, (h-this.helpHeight)/3, this.helpWidth, this.helpHeight);
  2468. wnd.setMaximizable(true);
  2469. wnd.setClosable(true);
  2470. wnd.destroyOnClose = false;
  2471. wnd.setResizable(true);
  2472. // Assigns the icon to the help window
  2473. if (this.helpWindowImage != null)
  2474. {
  2475. wnd.setImage(this.helpWindowImage);
  2476. }
  2477. // Workaround for ignored iframe height 100% in FF
  2478. if (mxClient.IS_NS)
  2479. {
  2480. var handler = function(sender)
  2481. {
  2482. var h = wnd.div.offsetHeight;
  2483. frame.setAttribute('height', (h-26)+'px');
  2484. };
  2485. wnd.addListener(mxEvent.RESIZE_END, handler);
  2486. wnd.addListener(mxEvent.MAXIMIZE, handler);
  2487. wnd.addListener(mxEvent.NORMALIZE, handler);
  2488. wnd.addListener(mxEvent.SHOW, handler);
  2489. }
  2490. this.help = wnd;
  2491. }
  2492. this.help.setVisible(true);
  2493. };
  2494. /**
  2495. * Function: showOutline
  2496. *
  2497. * Shows the outline window. If the window does not exist, then it is
  2498. * created using an <mxOutline>.
  2499. */
  2500. mxEditor.prototype.showOutline = function ()
  2501. {
  2502. var create = this.outline == null;
  2503. if (create)
  2504. {
  2505. var div = document.createElement('div');
  2506. div.style.overflow = 'hidden';
  2507. div.style.position = 'relative';
  2508. div.style.width = '100%';
  2509. div.style.height = '100%';
  2510. div.style.background = 'white';
  2511. div.style.cursor = 'move';
  2512. if (document.documentMode == 8)
  2513. {
  2514. div.style.filter = 'progid:DXImageTransform.Microsoft.alpha(opacity=100)';
  2515. }
  2516. var wnd = new mxWindow(
  2517. mxResources.get(this.outlineResource) ||
  2518. this.outlineResource,
  2519. div, 600, 480, 200, 200, false);
  2520. // Creates the outline in the specified div
  2521. // and links it to the existing graph
  2522. var outline = new mxOutline(this.graph, div);
  2523. wnd.setClosable(true);
  2524. wnd.setResizable(true);
  2525. wnd.destroyOnClose = false;
  2526. wnd.addListener(mxEvent.RESIZE_END, function()
  2527. {
  2528. outline.update();
  2529. });
  2530. this.outline = wnd;
  2531. this.outline.outline = outline;
  2532. }
  2533. // Finally shows the outline
  2534. this.outline.setVisible(true);
  2535. this.outline.outline.update(true);
  2536. };
  2537. /**
  2538. * Function: setMode
  2539. *
  2540. * Puts the graph into the specified mode. The following modenames are
  2541. * supported:
  2542. *
  2543. * select - Selects using the left mouse button, new connections
  2544. * are disabled.
  2545. * connect - Selects using the left mouse button or creates new
  2546. * connections if mouse over cell hotspot. See <mxConnectionHandler>.
  2547. * pan - Pans using the left mouse button, new connections are disabled.
  2548. */
  2549. mxEditor.prototype.setMode = function(modename)
  2550. {
  2551. if (modename == 'select')
  2552. {
  2553. this.graph.panningHandler.useLeftButtonForPanning = false;
  2554. this.graph.setConnectable(false);
  2555. }
  2556. else if (modename == 'connect')
  2557. {
  2558. this.graph.panningHandler.useLeftButtonForPanning = false;
  2559. this.graph.setConnectable(true);
  2560. }
  2561. else if (modename == 'pan')
  2562. {
  2563. this.graph.panningHandler.useLeftButtonForPanning = true;
  2564. this.graph.setConnectable(false);
  2565. }
  2566. };
  2567. /**
  2568. * Function: createPopupMenu
  2569. *
  2570. * Uses <popupHandler> to create the menu in the graph's
  2571. * panning handler. The redirection is setup in
  2572. * <setToolbarContainer>.
  2573. */
  2574. mxEditor.prototype.createPopupMenu = function (menu, cell, evt)
  2575. {
  2576. this.popupHandler.createMenu(this, menu, cell, evt);
  2577. };
  2578. /**
  2579. * Function: createEdge
  2580. *
  2581. * Uses <defaultEdge> as the prototype for creating new edges
  2582. * in the connection handler of the graph. The style of the
  2583. * edge will be overridden with the value returned by
  2584. * <getEdgeStyle>.
  2585. */
  2586. mxEditor.prototype.createEdge = function (source, target)
  2587. {
  2588. // Clones the defaultedge prototype
  2589. var e = null;
  2590. if (this.defaultEdge != null)
  2591. {
  2592. var model = this.graph.getModel();
  2593. e = model.cloneCell(this.defaultEdge);
  2594. }
  2595. else
  2596. {
  2597. e = new mxCell('');
  2598. e.setEdge(true);
  2599. var geo = new mxGeometry();
  2600. geo.relative = true;
  2601. e.setGeometry(geo);
  2602. }
  2603. // Overrides the edge style
  2604. var style = this.getEdgeStyle();
  2605. if (style != null)
  2606. {
  2607. e.setStyle(style);
  2608. }
  2609. return e;
  2610. };
  2611. /**
  2612. * Function: getEdgeStyle
  2613. *
  2614. * Returns a string identifying the style of new edges.
  2615. * The function is used in <createEdge> when new edges
  2616. * are created in the graph.
  2617. */
  2618. mxEditor.prototype.getEdgeStyle = function ()
  2619. {
  2620. return this.defaultEdgeStyle;
  2621. };
  2622. /**
  2623. * Function: consumeCycleAttribute
  2624. *
  2625. * Returns the next attribute in <cycleAttributeValues>
  2626. * or null, if not attribute should be used in the
  2627. * specified cell.
  2628. */
  2629. mxEditor.prototype.consumeCycleAttribute = function (cell)
  2630. {
  2631. return (this.cycleAttributeValues != null &&
  2632. this.cycleAttributeValues.length > 0 &&
  2633. this.graph.isSwimlane(cell)) ?
  2634. this.cycleAttributeValues[this.cycleAttributeIndex++ %
  2635. this.cycleAttributeValues.length] : null;
  2636. };
  2637. /**
  2638. * Function: cycleAttribute
  2639. *
  2640. * Uses the returned value from <consumeCycleAttribute>
  2641. * as the value for the <cycleAttributeName> key in
  2642. * the given cell's style.
  2643. */
  2644. mxEditor.prototype.cycleAttribute = function (cell)
  2645. {
  2646. if (this.cycleAttributeName != null)
  2647. {
  2648. var value = this.consumeCycleAttribute(cell);
  2649. if (value != null)
  2650. {
  2651. cell.setStyle(cell.getStyle()+';'+
  2652. this.cycleAttributeName+'='+value);
  2653. }
  2654. }
  2655. };
  2656. /**
  2657. * Function: addVertex
  2658. *
  2659. * Adds the given vertex as a child of parent at the specified
  2660. * x and y coordinate and fires an <addVertex> event.
  2661. */
  2662. mxEditor.prototype.addVertex = function (parent, vertex, x, y)
  2663. {
  2664. var model = this.graph.getModel();
  2665. while (parent != null && !this.graph.isValidDropTarget(parent))
  2666. {
  2667. parent = model.getParent(parent);
  2668. }
  2669. parent = (parent != null) ? parent : this.graph.getSwimlaneAt(x, y);
  2670. var scale = this.graph.getView().scale;
  2671. var geo = model.getGeometry(vertex);
  2672. var pgeo = model.getGeometry(parent);
  2673. if (this.graph.isSwimlane(vertex) &&
  2674. !this.graph.swimlaneNesting)
  2675. {
  2676. parent = null;
  2677. }
  2678. else if (parent == null && this.swimlaneRequired)
  2679. {
  2680. return null;
  2681. }
  2682. else if (parent != null && pgeo != null)
  2683. {
  2684. // Keeps vertex inside parent
  2685. var state = this.graph.getView().getState(parent);
  2686. if (state != null)
  2687. {
  2688. x -= state.origin.x * scale;
  2689. y -= state.origin.y * scale;
  2690. if (this.graph.isConstrainedMoving)
  2691. {
  2692. var width = geo.width;
  2693. var height = geo.height;
  2694. var tmp = state.x+state.width;
  2695. if (x+width > tmp)
  2696. {
  2697. x -= x+width - tmp;
  2698. }
  2699. tmp = state.y+state.height;
  2700. if (y+height > tmp)
  2701. {
  2702. y -= y+height - tmp;
  2703. }
  2704. }
  2705. }
  2706. else if (pgeo != null)
  2707. {
  2708. x -= pgeo.x*scale;
  2709. y -= pgeo.y*scale;
  2710. }
  2711. }
  2712. geo = geo.clone();
  2713. geo.x = this.graph.snap(x / scale -
  2714. this.graph.getView().translate.x -
  2715. this.graph.gridSize/2);
  2716. geo.y = this.graph.snap(y / scale -
  2717. this.graph.getView().translate.y -
  2718. this.graph.gridSize/2);
  2719. vertex.setGeometry(geo);
  2720. if (parent == null)
  2721. {
  2722. parent = this.graph.getDefaultParent();
  2723. }
  2724. this.cycleAttribute(vertex);
  2725. this.fireEvent(new mxEventObject(mxEvent.BEFORE_ADD_VERTEX,
  2726. 'vertex', vertex, 'parent', parent));
  2727. model.beginUpdate();
  2728. try
  2729. {
  2730. vertex = this.graph.addCell(vertex, parent);
  2731. if (vertex != null)
  2732. {
  2733. this.graph.constrainChild(vertex);
  2734. this.fireEvent(new mxEventObject(mxEvent.ADD_VERTEX, 'vertex', vertex));
  2735. }
  2736. }
  2737. finally
  2738. {
  2739. model.endUpdate();
  2740. }
  2741. if (vertex != null)
  2742. {
  2743. this.graph.setSelectionCell(vertex);
  2744. this.graph.scrollCellToVisible(vertex);
  2745. this.fireEvent(new mxEventObject(mxEvent.AFTER_ADD_VERTEX, 'vertex', vertex));
  2746. }
  2747. return vertex;
  2748. };
  2749. /**
  2750. * Function: destroy
  2751. *
  2752. * Removes the editor and all its associated resources. This does not
  2753. * normally need to be called, it is called automatically when the window
  2754. * unloads.
  2755. */
  2756. mxEditor.prototype.destroy = function ()
  2757. {
  2758. if (!this.destroyed)
  2759. {
  2760. this.destroyed = true;
  2761. if (this.tasks != null)
  2762. {
  2763. this.tasks.destroy();
  2764. }
  2765. if (this.outline != null)
  2766. {
  2767. this.outline.destroy();
  2768. }
  2769. if (this.properties != null)
  2770. {
  2771. this.properties.destroy();
  2772. }
  2773. if (this.keyHandler != null)
  2774. {
  2775. this.keyHandler.destroy();
  2776. }
  2777. if (this.rubberband != null)
  2778. {
  2779. this.rubberband.destroy();
  2780. }
  2781. if (this.toolbar != null)
  2782. {
  2783. this.toolbar.destroy();
  2784. }
  2785. if (this.graph != null)
  2786. {
  2787. this.graph.destroy();
  2788. }
  2789. this.status = null;
  2790. this.templates = null;
  2791. }
  2792. };