EditorUi.js 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968
  1. /**
  2. * Copyright (c) 2006-2016, JGraph Ltd
  3. * Copyright (c) 2006-2016, Gaudenz Alder
  4. */
  5. (function()
  6. {
  7. /**
  8. * Version
  9. */
  10. EditorUi.VERSION = '@DRAWIO-VERSION@';
  11. /**
  12. * Overrides compact UI setting.
  13. */
  14. EditorUi.compactUi = uiTheme != 'atlas';
  15. /**
  16. * Overrides compact UI setting.
  17. */
  18. EditorUi.isElectronApp = window && window.process && window.process.type; // https://github.com/electron/electron/issues/2288
  19. /**
  20. *
  21. */
  22. if (urlParams['dev'] == '1')
  23. {
  24. Editor.prototype.editBlankUrl = Editor.prototype.editBlankUrl + '&dev=1';
  25. Editor.prototype.editBlankFallbackUrl = Editor.prototype.editBlankFallbackUrl + '&dev=1';
  26. }
  27. /**
  28. * Capability check for canvas export
  29. */
  30. (function()
  31. {
  32. EditorUi.prototype.useCanvasForExport = false;
  33. try
  34. {
  35. var canvas = document.createElement('canvas');
  36. var img = new Image();
  37. // LATER: Capability check should not be async
  38. img.onload = function()
  39. {
  40. try
  41. {
  42. var ctx = canvas.getContext('2d');
  43. ctx.drawImage(img, 0, 0);
  44. // LATER: Fix security error caused by foreignObjects in Safari for toDataUri (tainted canvas)
  45. var result = canvas.toDataURL('image/png');
  46. EditorUi.prototype.useCanvasForExport = result != null && result.length > 6;
  47. }
  48. catch (e)
  49. {
  50. // ignore
  51. }
  52. };
  53. // Checks if SVG with foreignObject can be exported
  54. var svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1px" height="1px" version="1.1"><foreignObject pointer-events="all" width="1" height="1"><div xmlns="http://www.w3.org/1999/xhtml"></div></foreignObject></svg>';
  55. img.src = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svg)));
  56. }
  57. catch (e)
  58. {
  59. // ignore
  60. }
  61. })();
  62. /**
  63. * Initializes math typesetting and loads respective code.
  64. */
  65. Editor.initMath = function(src, config)
  66. {
  67. src = (src != null) ? src : 'https://cdn.mathjax.org/mathjax/2.6-latest/MathJax.js?config=TeX-MML-AM_HTMLorMML';
  68. Editor.mathJaxQueue = [];
  69. Editor.doMathJaxRender = function(container)
  70. {
  71. MathJax.Hub.Queue(['Typeset', MathJax.Hub, container]);
  72. };
  73. // Disables global typesetting and messages on startup, adds queue for
  74. // asynchronous rendering while MathJax is loading
  75. window.MathJax =
  76. {
  77. skipStartupTypeset: true,
  78. showMathMenu: false,
  79. messageStyle: 'none',
  80. AuthorInit: function ()
  81. {
  82. // Specification recommends using SVG over HTML-CSS if browser is known
  83. // Check if too inconsistent with image export and print output
  84. MathJax.Hub.Config(config || {
  85. jax: ['input/TeX', 'input/MathML', 'input/AsciiMath', 'output/HTML-CSS'],
  86. extensions: ['tex2jax.js', 'mml2jax.js', 'asciimath2jax.js'],
  87. TeX: {
  88. extensions: ['AMSmath.js', 'AMSsymbols.js', 'noErrors.js', 'noUndefined.js']
  89. },
  90. // Ignores math in in-place editor
  91. tex2jax: {
  92. ignoreClass: 'mxCellEditor'
  93. },
  94. asciimath2jax: {
  95. ignoreClass: 'mxCellEditor'
  96. }
  97. });
  98. MathJax.Hub.Register.StartupHook('Begin', function()
  99. {
  100. for (var i = 0; i < Editor.mathJaxQueue.length; i++)
  101. {
  102. Editor.doMathJaxRender(Editor.mathJaxQueue[i]);
  103. }
  104. });
  105. }
  106. };
  107. // Adds global enqueue method for async rendering
  108. Editor.MathJaxRender = function(container)
  109. {
  110. // Initial rendering when MathJax finished loading
  111. if (typeof(MathJax) !== 'undefined' && typeof(MathJax.Hub) !== 'undefined')
  112. {
  113. Editor.doMathJaxRender(container);
  114. }
  115. else
  116. {
  117. Editor.mathJaxQueue.push(container);
  118. }
  119. };
  120. // Adds global clear queue method
  121. Editor.MathJaxClear = function()
  122. {
  123. Editor.mathJaxQueue = [];
  124. };
  125. // Updates typeset after changes
  126. var editorInit = Editor.prototype.init;
  127. Editor.prototype.init = function()
  128. {
  129. this.graph.addListener(mxEvent.SIZE, mxUtils.bind(this, function(sender, evt)
  130. {
  131. if (this.graph.mathEnabled)
  132. {
  133. Editor.MathJaxRender(this.graph.container);
  134. }
  135. }));
  136. };
  137. var tags = document.getElementsByTagName('script');
  138. if (tags != null && tags.length > 0)
  139. {
  140. var script = document.createElement('script');
  141. script.type = 'text/javascript';
  142. script.src = src;
  143. tags[0].parentNode.appendChild(script);
  144. }
  145. };
  146. /**
  147. * Used in the GraphViewer lightbox.
  148. */
  149. Editor.closeImage = (mxClient.IS_SVG) ? 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAMAAADzN3VRAAAApVBMVEUAAAD////k5OT///8AAAB1dXXMzMz9/f39/f37+/v5+fn+/v7///9iYmJaWlqFhYWnp6ejo6OHh4f////////////////7+/v5+fnx8fH///8AAAD///8bGxv7+/v5+fkoKCghISFDQ0MYGBjh4eHY2Njb29tQUFBvb29HR0c/Pz82NjYrKyu/v78SEhLu7u7s7OzV1dVVVVU7OzsVFRXAv78QEBBzqehMAAAAG3RSTlMAA/7p/vz5xZlrTiPL/v78+/v7+OXd2TYQDs8L70ZbAAABKUlEQVQoz3VS13LCMBBUXHChd8iukDslQChJ/v/TchaG4cXS+OSb1c7trU7V60OpdRz2ZtNZL4zXNlcN8BEtSG6+NxIXkeRPoBuQ1cjvZ31/VJFB10ISli6diYfH8iYO3WUNCcNlB0gTrXOtkxTo0O1aKKiBBMhhv2MNBQKoiA5wxlZo0JDzD3AYKbWacyj3fs01wxey0pyEP+R8pWKWXoqtIZ0DDg5pbki9krEKOa6LVDQsdoXEsi46Zqh69KFz7B1u7Hb2yDV8firXDKBlZ4UFiswKGRhXTS93/ECK7yxnJ3+S3y/ThpO+cfSD017nqa18aasabU0/t7d+tk0/1oMEJ1NaD67iwdF68OabFSLn+eHb0+vjy+uk8br9fdrftH0O2menfd7+AQfYM/lNjoDHAAAAAElFTkSuQmCC' : IMAGE_PATH + '/delete.png';
  150. /**
  151. * Adds a shadow filter to the given svg root.
  152. */
  153. Editor.prototype.addSvgShadow = function(svgRoot, group, createOnly)
  154. {
  155. createOnly = (createOnly != null) ? createOnly : false;
  156. var svgDoc = svgRoot.ownerDocument;
  157. var filter = (svgDoc.createElementNS != null) ?
  158. svgDoc.createElementNS(mxConstants.NS_SVG, 'filter') : svgDoc.createElement('filter');
  159. filter.setAttribute('id', this.graph.shadowId);
  160. var blur = (svgDoc.createElementNS != null) ?
  161. svgDoc.createElementNS(mxConstants.NS_SVG, 'feGaussianBlur') : svgDoc.createElement('feGaussianBlur');
  162. blur.setAttribute('in', 'SourceAlpha');
  163. blur.setAttribute('stdDeviation', '1.7');
  164. blur.setAttribute('result', 'blur');
  165. filter.appendChild(blur);
  166. var offset = (svgDoc.createElementNS != null) ?
  167. svgDoc.createElementNS(mxConstants.NS_SVG, 'feOffset') : svgDoc.createElement('feOffset');
  168. offset.setAttribute('in', 'blur');
  169. offset.setAttribute('dx', '3');
  170. offset.setAttribute('dy', '3');
  171. offset.setAttribute('result', 'offsetBlur');
  172. filter.appendChild(offset);
  173. var flood = (svgDoc.createElementNS != null) ?
  174. svgDoc.createElementNS(mxConstants.NS_SVG, 'feFlood') : svgDoc.createElement('feFlood');
  175. flood.setAttribute('flood-color', '#3D4574');
  176. flood.setAttribute('flood-opacity', '0.4');
  177. flood.setAttribute('result', 'offsetColor');
  178. filter.appendChild(flood);
  179. var composite = (svgDoc.createElementNS != null) ?
  180. svgDoc.createElementNS(mxConstants.NS_SVG, 'feComposite') : svgDoc.createElement('feComposite');
  181. composite.setAttribute('in', 'offsetColor');
  182. composite.setAttribute('in2', 'offsetBlur');
  183. composite.setAttribute('operator', 'in');
  184. composite.setAttribute('result', 'offsetBlur');
  185. filter.appendChild(composite);
  186. var feBlend = (svgDoc.createElementNS != null) ?
  187. svgDoc.createElementNS(mxConstants.NS_SVG, 'feBlend') : svgDoc.createElement('feBlend');
  188. feBlend.setAttribute('in', 'SourceGraphic');
  189. feBlend.setAttribute('in2', 'offsetBlur');
  190. filter.appendChild(feBlend);
  191. // Creates defs element if not available
  192. var defs = svgRoot.getElementsByTagName('defs');
  193. var defsElt = null;
  194. if (defs.length == 0)
  195. {
  196. defsElt = (svgDoc.createElementNS != null) ?
  197. svgDoc.createElementNS(mxConstants.NS_SVG, 'defs') : svgDoc.createElement('defs');
  198. if (svgRoot.firstChild != null)
  199. {
  200. svgRoot.insertBefore(defsElt, svgRoot.firstChild);
  201. }
  202. else
  203. {
  204. svgRoot.appendChild(defsElt);
  205. }
  206. }
  207. else
  208. {
  209. defsElt = defs[0];
  210. }
  211. defsElt.appendChild(filter);
  212. if (!createOnly)
  213. {
  214. (group || svgRoot.getElementsByTagName('g')[0]).setAttribute('filter', 'url(#' + this.graph.shadowId + ')');
  215. if (!isNaN(parseInt(svgRoot.getAttribute('width'))))
  216. {
  217. svgRoot.setAttribute('width', parseInt(svgRoot.getAttribute('width')) + 6);
  218. svgRoot.setAttribute('height', parseInt(svgRoot.getAttribute('height')) + 6);
  219. }
  220. }
  221. return filter;
  222. };
  223. /**
  224. * Math support.
  225. */
  226. Editor.prototype.originalNoForeignObject = mxClient.NO_FO;
  227. var editorUpdateGraphComponents = Editor.prototype.updateGraphComponents;
  228. Editor.prototype.updateGraphComponents = function()
  229. {
  230. editorUpdateGraphComponents.apply(this, arguments);
  231. mxClient.NO_FO = (this.graph.mathEnabled && Editor.MathJaxRender != null) ? true : this.originalNoForeignObject;
  232. };
  233. /**
  234. * Abstraction for local storage access.
  235. */
  236. EditorUi.prototype.getLocalData = function(key, fn)
  237. {
  238. fn(localStorage.getItem(key));
  239. };
  240. /**
  241. * Abstraction for local storage access.
  242. */
  243. EditorUi.prototype.setLocalData = function(key, data, fn)
  244. {
  245. localStorage.setItem(key, data);
  246. fn();
  247. };
  248. /**
  249. * Abstraction for local storage access.
  250. */
  251. EditorUi.prototype.removeLocalData = function(key, fn)
  252. {
  253. localStorage.removeItem(key)
  254. fn();
  255. };
  256. EditorUi.prototype.setMathEnabled = function(value)
  257. {
  258. this.editor.graph.mathEnabled = value;
  259. this.editor.updateGraphComponents();
  260. this.editor.graph.refresh();
  261. this.fireEvent(new mxEventObject('mathEnabledChanged'));
  262. };
  263. EditorUi.prototype.isMathEnabled = function(value)
  264. {
  265. return this.editor.graph.mathEnabled;
  266. };
  267. // Helper method to move picket to top
  268. EditorUi.prototype.movePickersToTop = function()
  269. {
  270. var divs = document.getElementsByTagName('div');
  271. for (var i = 0; i < divs.length; i++)
  272. {
  273. if (divs[i].className == 'picker modal-dialog picker-dialog')
  274. {
  275. divs[i].style.zIndex = mxPopupMenu.prototype.zIndex + 1;
  276. }
  277. }
  278. };
  279. /**
  280. * Global switches for the export dialog.
  281. */
  282. if (window.ExportDialog)
  283. {
  284. ExportDialog.showXmlOption = false;
  285. ExportDialog.showGifOption = false;
  286. ExportDialog.getExportParameter = function(ui, format)
  287. {
  288. return function()
  289. {
  290. // LATER Fix decoding of HTML file in backend (remove argument in getFileData)
  291. return 'xml=' + encodeURIComponent(ui.getFileData(true));
  292. };
  293. };
  294. }
  295. /**
  296. * Specifies the app name. Default is document.title.
  297. */
  298. Editor.prototype.appName = 'draw.io';
  299. /**
  300. * Translates this point by the given vector.
  301. *
  302. * @param {number} dx X-coordinate of the translation.
  303. * @param {number} dy Y-coordinate of the translation.
  304. */
  305. EditorUi.prototype.isOfflineApp = function()
  306. {
  307. return (urlParams['offline'] == '1');
  308. };
  309. /**
  310. * Returns true if this offline app is offline.
  311. */
  312. EditorUi.prototype.isOffline = function()
  313. {
  314. // In FF navigator.onLine is always true
  315. return (mxClient.IS_FF && this.isOfflineApp()) || !navigator.onLine || urlParams['stealth'] == '1';
  316. };
  317. /**
  318. * Translates this point by the given vector.
  319. *
  320. * @param {number} dx X-coordinate of the translation.
  321. * @param {number} dy Y-coordinate of the translation.
  322. */
  323. EditorUi.prototype.createSpinner = function(x, y, size)
  324. {
  325. size = (size != null) ? size : 24;
  326. var spinner = new Spinner({
  327. lines: 12, // The number of lines to draw
  328. length: size, // The length of each line
  329. width: Math.round(size / 3), // The line thickness
  330. radius: Math.round(size / 2), // The radius of the inner circle
  331. rotate: 0, // The rotation offset
  332. color: '#000', // #rgb or #rrggbb
  333. speed: 1.5, // Rounds per second
  334. trail: 60, // Afterglow percentage
  335. shadow: false, // Whether to render a shadow
  336. hwaccel: false, // Whether to use hardware acceleration
  337. zIndex: 2e9 // The z-index (defaults to 2000000000)
  338. });
  339. // Extends spin method to include an optional label
  340. var oldSpin = spinner.spin;
  341. spinner.spin = function(container, label)
  342. {
  343. var result = false;
  344. if (!this.active)
  345. {
  346. oldSpin.call(this, container);
  347. this.active = true;
  348. if (label != null)
  349. {
  350. var status = document.createElement('div');
  351. status.style.position = 'absolute';
  352. status.style.whiteSpace = 'nowrap';
  353. status.style.background = '#4B4243';
  354. status.style.color = 'white';
  355. status.style.fontFamily = 'Helvetica, Arial';
  356. status.style.fontSize = '9pt';
  357. status.style.padding = '6px';
  358. status.style.paddingLeft = '10px';
  359. status.style.paddingRight = '10px';
  360. status.style.zIndex = 2e9;
  361. status.style.left = Math.max(0, x) + 'px';
  362. status.style.top = Math.max(0, y + 70) + 'px';
  363. mxUtils.setPrefixedStyle(status.style, 'borderRadius', '6px');
  364. mxUtils.setPrefixedStyle(status.style, 'boxShadow', '2px 2px 3px 0px #ddd');
  365. mxUtils.setPrefixedStyle(status.style, 'transform', 'translate(-50%,-50%)');
  366. status.innerHTML = label + '...';
  367. container.appendChild(status);
  368. spinner.status = status;
  369. // Centers the label in older IE versions
  370. if (mxClient.IS_VML && (document.documentMode == null || document.documentMode <= 8))
  371. {
  372. status.style.left = Math.round(Math.max(0, x - status.offsetWidth / 2)) + 'px';
  373. status.style.top = Math.round(Math.max(0, y + 70 - status.offsetHeight / 2)) + 'px';
  374. }
  375. }
  376. // Pause returns a function to resume the spinner
  377. this.pause = mxUtils.bind(this, function()
  378. {
  379. var fn = function() { };
  380. if (this.active)
  381. {
  382. fn = mxUtils.bind(this, function()
  383. {
  384. this.spin(container, label);
  385. });
  386. }
  387. this.stop();
  388. return fn;
  389. });
  390. result = true;
  391. }
  392. return result;
  393. };
  394. // Extends stop method to remove the optional label
  395. var oldStop = spinner.stop;
  396. spinner.stop = function()
  397. {
  398. oldStop.call(this);
  399. this.active = false;
  400. if (spinner.status != null)
  401. {
  402. spinner.status.parentNode.removeChild(spinner.status);
  403. spinner.status = null;
  404. }
  405. };
  406. return spinner;
  407. };
  408. /**
  409. * Static method for pasing PNG files.
  410. */
  411. EditorUi.parsePng = function(f, fn, error)
  412. {
  413. var pos = 0;
  414. function fread(d, count)
  415. {
  416. var start = pos;
  417. pos += count;
  418. return d.substring(start, pos);
  419. };
  420. // Reads unsigned long 32 bit big endian
  421. function _freadint(d)
  422. {
  423. var bytes = fread(d, 4);
  424. return bytes.charCodeAt(3) + (bytes.charCodeAt(2) << 8) +
  425. (bytes.charCodeAt(1) << 16) + (bytes.charCodeAt(0) << 24);
  426. };
  427. // Checks signature
  428. if (fread(f,8) != String.fromCharCode(137) + 'PNG' + String.fromCharCode(13, 10, 26, 10))
  429. {
  430. if (error != null)
  431. {
  432. error();
  433. }
  434. return;
  435. }
  436. // Reads header chunk
  437. fread(f,4);
  438. if (fread(f,4) != 'IHDR')
  439. {
  440. if (error != null)
  441. {
  442. error();
  443. }
  444. return;
  445. }
  446. fread(f, 17);
  447. do
  448. {
  449. var n = _freadint(f);
  450. var type = fread(f,4);
  451. if (fn != null)
  452. {
  453. if (fn(pos - 8, type, n))
  454. {
  455. break;
  456. }
  457. }
  458. value = fread(f,n);
  459. fread(f,4);
  460. if (type == 'IEND')
  461. {
  462. break;
  463. }
  464. }
  465. while (n);
  466. };
  467. /**
  468. * Helper function to extract the graph model XML node.
  469. */
  470. Editor.prototype.extractGraphModel = function(node, allowMxFile)
  471. {
  472. if (node != null && typeof(pako) !== 'undefined')
  473. {
  474. var tmp = node.ownerDocument.getElementsByTagName('div');
  475. var divs = [];
  476. if (tmp != null && tmp.length > 0)
  477. {
  478. for (var i = 0; i < tmp.length; i++)
  479. {
  480. if (tmp[i].getAttribute('class') == 'mxgraph')
  481. {
  482. divs.push(tmp[i]);
  483. break;
  484. }
  485. }
  486. }
  487. if (divs.length > 0)
  488. {
  489. var data = divs[0].getAttribute('data-mxgraph');
  490. if (data != null)
  491. {
  492. var config = JSON.parse(data);
  493. if (config != null && config.xml != null)
  494. {
  495. var doc2 = mxUtils.parseXml(config.xml);
  496. node = doc2.documentElement;
  497. }
  498. }
  499. else
  500. {
  501. var divs2 = divs[0].getElementsByTagName('div');
  502. if (divs2.length > 0)
  503. {
  504. var data = mxUtils.getTextContent(divs2[0]);
  505. data = this.graph.decompress(data);
  506. if (data.length > 0)
  507. {
  508. var doc2 = mxUtils.parseXml(data);
  509. node = doc2.documentElement;
  510. }
  511. }
  512. }
  513. }
  514. }
  515. if (node != null && node.nodeName == 'svg')
  516. {
  517. var tmp = node.getAttribute('content');
  518. if (tmp != null && tmp.charAt(0) != '<' && tmp.charAt(0) != '%')
  519. {
  520. tmp = unescape((window.atob) ? atob(tmp) : Base64.decode(cont, tmp));
  521. }
  522. if (tmp != null && tmp.charAt(0) == '%')
  523. {
  524. tmp = decodeURIComponent(tmp);
  525. }
  526. if (tmp != null && tmp.length > 0)
  527. {
  528. node = mxUtils.parseXml(tmp).documentElement;
  529. }
  530. else
  531. {
  532. throw {message: mxResources.get('notADiagramFile')};
  533. }
  534. }
  535. if (node != null && !allowMxFile)
  536. {
  537. var diagramNode = null;
  538. if (node.nodeName == 'diagram')
  539. {
  540. diagramNode = node;
  541. }
  542. else if (node.nodeName == 'mxfile')
  543. {
  544. var diagrams = node.getElementsByTagName('diagram');
  545. if (diagrams.length > 0)
  546. {
  547. diagramNode = diagrams[Math.max(0, Math.min(diagrams.length - 1, urlParams['page'] || 0))];
  548. }
  549. }
  550. if (diagramNode != null)
  551. {
  552. node = mxUtils.parseXml(this.graph.decompress(mxUtils.getTextContent(diagramNode))).documentElement;
  553. }
  554. }
  555. if (node != null && node.nodeName != 'mxGraphModel' && (!allowMxFile || node.nodeName != 'mxfile'))
  556. {
  557. node = null;
  558. }
  559. return node;
  560. };
  561. /**
  562. * Returns true if the given string contains a compatible graph model.
  563. */
  564. EditorUi.prototype.isCompatibleString = function(data)
  565. {
  566. try
  567. {
  568. var doc = mxUtils.parseXml(data);
  569. var node = this.editor.extractGraphModel(doc.documentElement);
  570. return node != null && node.getElementsByTagName('parsererror').length == 0;
  571. }
  572. catch (e)
  573. {
  574. // ignore
  575. }
  576. return false;
  577. };
  578. /**
  579. * Extracts the mxfile from the given HTML data from a data transfer event.
  580. */
  581. var editorUiExtractGraphModelFromHtml = EditorUi.prototype.extractGraphModelFromHtml;
  582. EditorUi.prototype.extractGraphModelFromHtml = function(data)
  583. {
  584. var result = editorUiExtractGraphModelFromHtml.apply(this, arguments);
  585. if (result == null)
  586. {
  587. try
  588. {
  589. var idx = data.indexOf('&lt;mxfile ');
  590. if (idx >= 0)
  591. {
  592. var idx2 = data.lastIndexOf('&lt;/mxfile&gt;');
  593. if (idx2 > idx)
  594. {
  595. result = data.substring(idx, idx2 + 15).replace(/&gt;/g, '>').
  596. replace(/&lt;/g, '<').replace(/\\&quot;/g, '"').replace(/\n/g, '');
  597. }
  598. }
  599. else
  600. {
  601. // Gets compressed data from mxgraph element in HTML document
  602. var doc = mxUtils.parseXml(data);
  603. var node = this.editor.extractGraphModel(doc.documentElement);
  604. result = (node != null) ? mxUtils.getXml(node) : '';
  605. }
  606. }
  607. catch (e)
  608. {
  609. // ignore
  610. }
  611. }
  612. return result;
  613. };
  614. /**
  615. * Workaround for malformed xhtml meta element bug 07.08.16. The trailing slash was missing causing
  616. * reopen to fail trying to parse. Used in replaceFileData, setFileData and importFile.
  617. */
  618. EditorUi.prototype.validateFileData = function(data)
  619. {
  620. if (data != null && data.length > 0)
  621. {
  622. var index = data.indexOf('<meta charset="utf-8">');
  623. if (index >= 0)
  624. {
  625. var replaceString = '<meta charset="utf-8"/>';
  626. var replaceStrLen = replaceString.length;
  627. data = data.slice(0, index) + replaceString + data.slice(index + replaceStrLen - 1, data.length);
  628. }
  629. }
  630. return data;
  631. };
  632. /**
  633. *
  634. */
  635. EditorUi.prototype.replaceFileData = function(data)
  636. {
  637. data = this.validateFileData(data);
  638. var node = (data != null && data.length > 0) ? mxUtils.parseXml(data).documentElement : null;
  639. // Some nodes must be extracted here to find the mxfile node
  640. // LATER: Remove duplicate call to extractGraphModel in overridden setGraphXml
  641. var tmp = (node != null) ? this.editor.extractGraphModel(node, true) : null;
  642. if (tmp != null)
  643. {
  644. node = tmp;
  645. }
  646. if (node != null)
  647. {
  648. var graph = this.editor.graph;
  649. graph.model.beginUpdate();
  650. try
  651. {
  652. var oldPages = (this.pages != null) ? this.pages.slice() : null;
  653. var nodes = node.getElementsByTagName('diagram');
  654. if (urlParams['pages'] == '1' || nodes.length > 1 ||
  655. (nodes.length == 1 && nodes[0].hasAttribute('name')))
  656. {
  657. this.fileNode = node;
  658. this.pages = (this.pages != null) ? this.pages : [];
  659. // Wraps page nodes
  660. for (var i = nodes.length - 1; i >= 0; i--)
  661. {
  662. var page = this.updatePageRoot(new DiagramPage(nodes[i]));
  663. // Checks for invalid page names
  664. if (page.getName() == null)
  665. {
  666. page.setName(mxResources.get('pageWithNumber', [i + 1]));
  667. }
  668. graph.model.execute(new ChangePage(this, page, (i == 0) ? page : null, 0));
  669. }
  670. }
  671. else
  672. {
  673. // Creates tabbed file structure if enforced by URL
  674. if (urlParams['pages'] == '1' && this.fileNode == null)
  675. {
  676. this.fileNode = node.ownerDocument.createElement('mxfile');
  677. this.currentPage = new DiagramPage(node.ownerDocument.createElement('diagram'));
  678. this.currentPage.setName(mxResources.get('pageWithNumber', [1]));
  679. graph.model.execute(new ChangePage(this, this.currentPage, this.currentPage, 0));
  680. }
  681. // Avoids scroll offset when switching page
  682. this.editor.setGraphXml(node);
  683. // Avoids duplicate parsing of the XML stored in the node
  684. if (this.currentPage != null)
  685. {
  686. this.currentPage.root = this.editor.graph.model.root;
  687. }
  688. }
  689. if (oldPages != null)
  690. {
  691. for (var i = 0; i < oldPages.length; i++)
  692. {
  693. graph.model.execute(new ChangePage(this, oldPages[i], null));
  694. }
  695. }
  696. }
  697. finally
  698. {
  699. graph.model.endUpdate();
  700. }
  701. }
  702. };
  703. /**
  704. *
  705. */
  706. EditorUi.prototype.setFileData = function(data)
  707. {
  708. data = this.validateFileData(data);
  709. this.currentPage = null;
  710. this.fileNode = null;
  711. this.pages = null;
  712. var node = (data != null && data.length > 0) ? mxUtils.parseXml(data).documentElement : null;
  713. // Some nodes must be extracted here to find the mxfile node
  714. // LATER: Remove duplicate call to extractGraphModel in overridden setGraphXml
  715. var tmp = (node != null) ? this.editor.extractGraphModel(node, true) : null;
  716. if (tmp != null)
  717. {
  718. node = tmp;
  719. }
  720. if (node != null && node.nodeName == 'mxfile')
  721. {
  722. var nodes = node.getElementsByTagName('diagram');
  723. if (urlParams['pages'] == '1' || nodes.length > 1 ||
  724. (nodes.length == 1 && nodes[0].hasAttribute('name')))
  725. {
  726. this.fileNode = node;
  727. this.pages = [];
  728. // Wraps page nodes
  729. for (var i = 0; i < nodes.length; i++)
  730. {
  731. var page = new DiagramPage(nodes[i]);
  732. // Checks for invalid page names
  733. if (page.getName() == null)
  734. {
  735. page.setName(mxResources.get('pageWithNumber', [i + 1]));
  736. }
  737. this.pages.push(page);
  738. }
  739. this.currentPage = this.pages[Math.max(0, Math.min(this.pages.length - 1, urlParams['page'] || 0))];
  740. node = this.currentPage.node;
  741. }
  742. }
  743. // Creates tabbed file structure if enforced by URL
  744. if (urlParams['pages'] == '1' && this.fileNode == null)
  745. {
  746. this.fileNode = node.ownerDocument.createElement('mxfile');
  747. this.currentPage = new DiagramPage(node.ownerDocument.createElement('diagram'));
  748. this.currentPage.setName(mxResources.get('pageWithNumber', [1]));
  749. this.pages = [this.currentPage];
  750. }
  751. // Avoids scroll offset when switching page
  752. this.editor.setGraphXml(node);
  753. // Avoids duplicate parsing of the XML stored in the node
  754. if (this.currentPage != null)
  755. {
  756. this.currentPage.root = this.editor.graph.model.root;
  757. }
  758. };
  759. /**
  760. * Adds support for old stylesheets and compressed files
  761. */
  762. var editorSetGraphXml = Editor.prototype.setGraphXml;
  763. Editor.prototype.setGraphXml = function(node)
  764. {
  765. node = (node != null && node.nodeName != 'mxlibrary') ? this.extractGraphModel(node) : null;
  766. if (node != null)
  767. {
  768. // Checks input for parser errors
  769. var errs = node.getElementsByTagName('parsererror');
  770. if (errs != null && errs.length > 0)
  771. {
  772. var elt = errs[0];
  773. var divs = elt.getElementsByTagName('div');
  774. if (divs != null && divs.length > 0)
  775. {
  776. elt = divs[0];
  777. }
  778. throw {message: mxUtils.getTextContent(elt)};
  779. }
  780. else if (node.nodeName == 'mxGraphModel')
  781. {
  782. var style = node.getAttribute('style') || 'default-style2';
  783. // Decodes the style if required
  784. if (urlParams['embed'] != '1' && (style == null || style == ''))
  785. {
  786. var node2 = (this.graph.themes != null) ?
  787. this.graph.themes['default-old'] :
  788. mxUtils.load(STYLE_PATH + '/default-old.xml').getDocumentElement();
  789. if (node2 != null)
  790. {
  791. var dec2 = new mxCodec(node2.ownerDocument);
  792. dec2.decode(node2, this.graph.getStylesheet());
  793. }
  794. }
  795. else if (style != this.graph.currentStyle)
  796. {
  797. var node2 = (this.graph.themes != null) ?
  798. this.graph.themes[style] :
  799. mxUtils.load(STYLE_PATH + '/' + style + '.xml').getDocumentElement()
  800. if (node2 != null)
  801. {
  802. var dec2 = new mxCodec(node2.ownerDocument);
  803. dec2.decode(node2, this.graph.getStylesheet());
  804. }
  805. }
  806. this.graph.currentStyle = style;
  807. this.graph.mathEnabled = (urlParams['math'] == '1' || node.getAttribute('math') == '1');
  808. var bgImg = node.getAttribute('backgroundImage');
  809. if (bgImg != null)
  810. {
  811. bgImg = JSON.parse(bgImg);
  812. this.graph.setBackgroundImage(new mxImage(bgImg.src, bgImg.width, bgImg.height));
  813. }
  814. else
  815. {
  816. this.graph.setBackgroundImage(null);
  817. }
  818. mxClient.NO_FO = (this.graph.mathEnabled) ? true : this.originalNoForeignObject;
  819. this.graph.setShadowVisible(node.getAttribute('shadow') == '1', false);
  820. }
  821. // Calls updateGraphComponents
  822. editorSetGraphXml.apply(this, arguments);
  823. }
  824. else
  825. {
  826. throw {
  827. message: mxResources.get('notADiagramFile') || 'Invalid data',
  828. toString: function() { return this.message; }
  829. };
  830. }
  831. };
  832. /**
  833. * Adds persistent style to file
  834. */
  835. var editorGetGraphXml = Editor.prototype.getGraphXml;
  836. Editor.prototype.getGraphXml = function(ignoreSelection)
  837. {
  838. ignoreSelection = (ignoreSelection != null) ? ignoreSelection : true;
  839. var node = editorGetGraphXml.apply(this, arguments);
  840. // Adds the current style
  841. if (this.graph.currentStyle != null && this.graph.currentStyle != 'default-style2')
  842. {
  843. node.setAttribute('style', this.graph.currentStyle);
  844. }
  845. // Adds the background image
  846. if (this.graph.backgroundImage != null)
  847. {
  848. node.setAttribute('backgroundImage', JSON.stringify(this.graph.backgroundImage));
  849. }
  850. node.setAttribute('math', (this.graph.mathEnabled) ? '1' : '0');
  851. node.setAttribute('shadow', (this.graph.shadowVisible) ? '1' : '0');
  852. return node;
  853. };
  854. var editorResetGraph = Editor.prototype.resetGraph;
  855. Editor.prototype.resetGraph = function()
  856. {
  857. this.graph.mathEnabled = (urlParams['math'] == '1');
  858. this.graph.view.x0 = null;
  859. this.graph.view.y0 = null;
  860. mxClient.NO_FO = (this.graph.mathEnabled) ? true : this.originalNoForeignObject;
  861. editorResetGraph.apply(this, arguments);
  862. };
  863. /**
  864. * EditorUi Overrides
  865. */
  866. if (urlParams['offline'] == '1')
  867. {
  868. EditorUi.prototype.footerHeight = 4;
  869. }
  870. else
  871. {
  872. if (uiTheme == 'atlas')
  873. {
  874. if (typeof Toolbar !== 'undefined')
  875. {
  876. Toolbar.prototype.unselectedBackground = (mxClient.IS_QUIRKS) ? 'none' : 'linear-gradient(rgb(255, 255, 255) 0px, rgb(242, 242, 242) 100%)';
  877. Toolbar.prototype.selectedBackground = 'rgb(242, 242, 242)';
  878. }
  879. Editor.prototype.initialTopSpacing = 3;
  880. EditorUi.prototype.menubarHeight = 41;
  881. EditorUi.prototype.toolbarHeight = 38;
  882. EditorUi.prototype.hsplitPosition = 188;
  883. Sidebar.prototype.thumbWidth = 46;
  884. Sidebar.prototype.thumbHeight = 46;
  885. Sidebar.prototype.thumbPadding = (document.documentMode >= 5) ? 0 : 1;
  886. Sidebar.prototype.thumbBorder = 2;
  887. }
  888. else
  889. {
  890. if (urlParams['savesidebar'] == '1')
  891. {
  892. Sidebar.prototype.thumbWidth = 64;
  893. Sidebar.prototype.thumbHeight = 64;
  894. }
  895. }
  896. EditorUi.prototype.footerHeight = (screen.height <= 740) ? 5 : 46;
  897. // Fetches footer from page
  898. EditorUi.prototype.createFooter = function()
  899. {
  900. var footer = document.getElementById('geFooter');
  901. if (footer != null)
  902. {
  903. footer.style.visibility = 'visible';
  904. // Adds button to hide the footer
  905. var img = document.createElement('img');
  906. img.setAttribute('border', '0');
  907. img.setAttribute('src', Dialog.prototype.closeImage);
  908. img.setAttribute('title', mxResources.get('hide'));
  909. footer.appendChild(img)
  910. if (mxClient.IS_QUIRKS)
  911. {
  912. img.style.position = 'relative';
  913. img.style.styleFloat = 'right';
  914. img.style.top = '-30px';
  915. img.style.left = '164px';
  916. img.style.cursor = 'pointer';
  917. }
  918. mxEvent.addListener(img, 'click', mxUtils.bind(this, function()
  919. {
  920. this.hideFooter();
  921. }));
  922. }
  923. return footer;
  924. };
  925. }
  926. /**
  927. * Hides the footer.
  928. */
  929. EditorUi.prototype.hideFooter = function()
  930. {
  931. var footer = document.getElementById('geFooter');
  932. if (footer != null)
  933. {
  934. this.footerHeight = 0;
  935. footer.style.display = 'none';
  936. this.refresh();
  937. }
  938. };
  939. /**
  940. * Overrides image dialog to add image search and Google+.
  941. */
  942. EditorUi.prototype.showImageDialog = function(title, value, fn, ignoreExisting, convertDataUri)
  943. {
  944. // KNOWN: IE+FF don't return keyboard focus after image dialog (calling focus doesn't help)
  945. var dlg = new ImageDialog(this, title, value, fn, ignoreExisting, convertDataUri);
  946. this.showDialog(dlg.container, (Graph.fileSupport) ? 420 : 340, (Graph.fileSupport) ? 200 : 90, true, true);
  947. dlg.init();
  948. };
  949. /**
  950. * Hides the current menu.
  951. */
  952. EditorUi.prototype.showBackgroundImageDialog = function(apply)
  953. {
  954. apply = (apply != null) ? apply : mxUtils.bind(this, function(image)
  955. {
  956. this.setBackgroundImage(image);
  957. });
  958. var dlg = new BackgroundImageDialog(this, mxUtils.bind(this, function(image)
  959. {
  960. apply(image);
  961. }));
  962. this.showDialog(dlg.container, 360, 200, true, true);
  963. dlg.init();
  964. };
  965. /**
  966. * Hides the current menu.
  967. */
  968. EditorUi.prototype.showLibraryDialog = function(name, sidebar, images, file, mode)
  969. {
  970. var dlg = new LibraryDialog(this, name, sidebar, images, file, mode);
  971. this.showDialog(dlg.container, 620, 440, true, true, mxUtils.bind(this, function(cancel)
  972. {
  973. if (cancel && this.getCurrentFile() == null)
  974. {
  975. this.showSplash();
  976. }
  977. }));
  978. dlg.init();
  979. };
  980. /**
  981. * Allows for two buttons in the sidebar footer.
  982. */
  983. EditorUi.prototype.sidebarFooterHeight = (uiTheme == 'atlas') ? 36 : 36;
  984. /**
  985. * Specifies the default custom shape style.
  986. */
  987. EditorUi.prototype.defaultCustomShapeStyle = 'shape=stencil(tZRtTsQgEEBPw1+DJR7AoN6DbWftpAgE0Ortd/jYRGq72R+YNE2YgTePloEJGWblgA18ZuKFDcMj5/Sm8boZq+BgjCX4pTyqk6ZlKROitwusOMXKQDODx5iy4pXxZ5qTHiFHawxB0JrQZH7lCabQ0Fr+XWC1/E8zcsT/gAi+Subo2/3Mh6d/oJb5nU1b5tW7r2knautaa3T+U32o7f7vZwpJkaNDLORJjcu7t59m2jXxqX9un+tt022acsfmoKaQZ+vhhswZtS6Ne/ThQGt0IV0N3Yyv6P3CeT9/tHO0XFI5cAE=);whiteSpace=wrap;html=1;';
  988. /**
  989. * Hook for sidebar footer container.
  990. */
  991. EditorUi.prototype.createSidebarFooterContainer = function()
  992. {
  993. var div = this.createDiv('geSidebarContainer');
  994. div.style.position = 'absolute';
  995. div.style.overflow = 'hidden';
  996. div.style.borderWidth = '3px';
  997. var elt2 = document.createElement('a');
  998. elt2.setAttribute('href', 'javascript:void(0);');
  999. elt2.className = 'geTitle';
  1000. elt2.style.height = '100%';
  1001. elt2.style.paddingTop = '9px';
  1002. mxUtils.write(elt2, mxResources.get('moreShapes') + '...');
  1003. mxEvent.addListener(elt2, 'click', mxUtils.bind(this, function(evt)
  1004. {
  1005. this.actions.get('shapes').funct();
  1006. mxEvent.consume(evt);
  1007. }));
  1008. div.appendChild(elt2);
  1009. return div;
  1010. };
  1011. /**
  1012. * Setting the current file.
  1013. */
  1014. /**
  1015. * Defines the maximum size for images.
  1016. */
  1017. EditorUi.prototype.maxBackgroundSize = 1600;
  1018. /**
  1019. * Defines the maximum size for images.
  1020. */
  1021. EditorUi.prototype.maxImageSize = 520;
  1022. /**
  1023. * Images above 100K should be resampled.
  1024. */
  1025. EditorUi.prototype.resampleThreshold = 100000;
  1026. /**
  1027. * Maximum allowed size for images is 1 MB.
  1028. */
  1029. EditorUi.prototype.maxImageBytes = 1000000;
  1030. /**
  1031. * Maximum size for background images is 2.5 MB.
  1032. */
  1033. EditorUi.prototype.maxBackgroundBytes = 2500000;
  1034. /**
  1035. * Holds the current file.
  1036. */
  1037. EditorUi.prototype.currentFile = null;
  1038. /**
  1039. * Translates this point by the given vector.
  1040. *
  1041. * @param {number} dx X-coordinate of the translation.
  1042. * @param {number} dy Y-coordinate of the translation.
  1043. */
  1044. EditorUi.prototype.setCurrentFile = function(file)
  1045. {
  1046. this.currentFile = file;
  1047. };
  1048. /**
  1049. * Translates this point by the given vector.
  1050. *
  1051. * @param {number} dx X-coordinate of the translation.
  1052. * @param {number} dy Y-coordinate of the translation.
  1053. */
  1054. EditorUi.prototype.getCurrentFile = function()
  1055. {
  1056. return this.currentFile;
  1057. };
  1058. /**
  1059. * Translates this point by the given vector.
  1060. *
  1061. * @param {number} dx X-coordinate of the translation.
  1062. * @param {number} dy Y-coordinate of the translation.
  1063. */
  1064. EditorUi.prototype.isDiagramEmpty = function()
  1065. {
  1066. var model = this.editor.graph.getModel();
  1067. return model.getChildCount(model.root) == 1 && model.getChildCount(model.getChildAt(model.root, 0)) == 0;
  1068. };
  1069. /**
  1070. * Handling for canvas export.
  1071. */
  1072. /**
  1073. *
  1074. */
  1075. EditorUi.prototype.isExportToCanvas = function()
  1076. {
  1077. // LATER: Fix security error caused by foreignObjects in Safari for toDataUri (tainted canvas)
  1078. return mxClient.IS_CHROMEAPP || (!this.editor.graph.mathEnabled && this.useCanvasForExport);
  1079. };
  1080. /**
  1081. * Translates this point by the given vector.
  1082. *
  1083. * @param {number} dx X-coordinate of the translation.
  1084. * @param {number} dy Y-coordinate of the translation.
  1085. */
  1086. EditorUi.prototype.createSvgDataUri = function(svg)
  1087. {
  1088. return 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svg)));
  1089. };
  1090. /**
  1091. *
  1092. */
  1093. EditorUi.prototype.createPngDataUri = function(canvas, xml)
  1094. {
  1095. var data = canvas.toDataURL('image/png');
  1096. // Checks if output is invalid or empty
  1097. if (data.length <= 6 || data == canvas.cloneNode(false).toDataURL('image/png'))
  1098. {
  1099. throw {message: 'Invalid image'};
  1100. }
  1101. if (xml != null)
  1102. {
  1103. data = this.writeGraphModelToPng(data, 'zTXt', 'mxGraphModel', atob(this.editor.graph.compress(xml)));
  1104. }
  1105. return data;
  1106. };
  1107. /**
  1108. *
  1109. */
  1110. EditorUi.prototype.saveCanvas = function(canvas, xml)
  1111. {
  1112. var file = this.getCurrentFile();
  1113. var filename = (file != null && file.getTitle() != null) ? file.getTitle() : this.defaultFilename;
  1114. var dot = filename.lastIndexOf('.');
  1115. if (dot > 0)
  1116. {
  1117. filename = filename.substring(0, dot);
  1118. }
  1119. filename += '.png';
  1120. var data = this.createPngDataUri(canvas, xml);
  1121. this.saveLocalFile(data.substring(data.lastIndexOf(',') + 1), filename, 'image/png', true);
  1122. };
  1123. /**
  1124. *
  1125. */
  1126. EditorUi.prototype.showRemoteExportDialog = function(btnLabel, helpLink, callback)
  1127. {
  1128. var graph = this.editor.graph;
  1129. var content = document.createElement('div');
  1130. content.style.padding = '6px';
  1131. var cb2 = document.createElement('input');
  1132. cb2.style.marginRight = '8px';
  1133. cb2.setAttribute('type', 'checkbox');
  1134. if (graph.isSelectionEmpty())
  1135. {
  1136. cb2.setAttribute('disabled', 'disabled');
  1137. }
  1138. content.appendChild(cb2);
  1139. mxUtils.write(content, mxResources.get('selectionOnly'));
  1140. mxUtils.br(content);
  1141. var cb = document.createElement('input');
  1142. cb.setAttribute('type', 'checkbox');
  1143. cb.setAttribute('checked', 'checked');
  1144. cb.defaultChecked = true;
  1145. cb.style.marginRight = '8px';
  1146. cb.style.marginTop = '16px';
  1147. content.appendChild(cb);
  1148. mxUtils.write(content, mxResources.get('includeCopyOfMyDiagram'));
  1149. var dlg = new CustomDialog(this, content, mxUtils.bind(this, function()
  1150. {
  1151. callback(!cb2.checked, cb.checked);
  1152. }), null, btnLabel, helpLink);
  1153. this.showDialog(dlg.container, 300, 120, true, true);
  1154. }
  1155. /**
  1156. *
  1157. */
  1158. EditorUi.prototype.showExportDialog = function(embedOption, btnLabel, helpLink, callback)
  1159. {
  1160. var graph = this.editor.graph;
  1161. var content = document.createElement('div');
  1162. content.style.paddingTop = '20px';
  1163. content.style.paddingRight = '8px';
  1164. var cb = document.createElement('input');
  1165. cb.style.marginRight = '8px';
  1166. cb.setAttribute('type', 'checkbox');
  1167. if (graph.background == mxConstants.NONE || graph.background == null)
  1168. {
  1169. cb.setAttribute('checked', 'checked');
  1170. cb.defaultChecked = true;
  1171. }
  1172. content.appendChild(cb);
  1173. mxUtils.write(content, mxResources.get('transparentBackground'));
  1174. mxUtils.br(content);
  1175. var cb2 = document.createElement('input');
  1176. cb2.style.marginTop = '16px';
  1177. cb2.style.marginRight = '8px';
  1178. cb2.setAttribute('type', 'checkbox');
  1179. if (graph.isSelectionEmpty())
  1180. {
  1181. cb2.setAttribute('disabled', 'disabled');
  1182. }
  1183. content.appendChild(cb2);
  1184. mxUtils.write(content, mxResources.get('selectionOnly'));
  1185. mxUtils.br(content);
  1186. var cb3 = document.createElement('input');
  1187. cb3.style.marginTop = '16px';
  1188. cb3.style.marginRight = '8px';
  1189. cb3.setAttribute('type', 'checkbox');
  1190. content.appendChild(cb3);
  1191. mxUtils.write(content, mxResources.get('shadow'));
  1192. mxUtils.br(content);
  1193. if (graph.shadowVisible)
  1194. {
  1195. cb3.setAttribute('checked', 'checked');
  1196. cb3.defaultChecked = true;
  1197. }
  1198. var cb5 = document.createElement('input');
  1199. cb5.style.marginTop = '16px';
  1200. cb5.style.marginRight = '8px';
  1201. cb5.setAttribute('type', 'checkbox');
  1202. if (this.isOffline() || !this.canvasSupported)
  1203. {
  1204. cb5.setAttribute('disabled', 'disabled');
  1205. }
  1206. if (embedOption)
  1207. {
  1208. content.appendChild(cb5);
  1209. mxUtils.write(content, mxResources.get('embedImages'));
  1210. mxUtils.br(content);
  1211. }
  1212. var cb4 = document.createElement('input');
  1213. cb4.style.marginTop = '16px';
  1214. cb4.style.marginRight = '8px';
  1215. cb4.setAttribute('type', 'checkbox');
  1216. cb4.style.marginBottom = '8px';
  1217. cb4.setAttribute('checked', 'checked');
  1218. cb4.defaultChecked = true;
  1219. content.appendChild(cb4);
  1220. mxUtils.write(content, mxResources.get('includeCopyOfMyDiagram'));
  1221. var dlg = new FilenameDialog(this, 100, btnLabel, mxUtils.bind(this, function(newValue)
  1222. {
  1223. callback(newValue, cb.checked, !cb2.checked, cb3.checked, cb4.checked, cb5.checked);
  1224. }), mxResources.get('zoom') + ' (%)', null, content, (!this.isOffline()) ? helpLink : null);
  1225. this.showDialog(dlg.container, 320, (embedOption) ? 266 : 240, true, true);
  1226. dlg.init();
  1227. }
  1228. /**
  1229. *
  1230. */
  1231. EditorUi.prototype.uploadToGithub = function(file, base64Data, editable)
  1232. {
  1233. var resume = this.spinner.pause();
  1234. var content = document.createElement('div');
  1235. content.style.paddingTop = '20px';
  1236. content.style.paddingRight = '8px';
  1237. var table = document.createElement('table');
  1238. var tbody = document.createElement('tbody');
  1239. var tr = document.createElement('tr');
  1240. var td = document.createElement('td');
  1241. var uname = document.createElement('input');
  1242. uname.setAttribute('type', 'text');
  1243. mxUtils.write(td, 'Username:');
  1244. tr.appendChild(td);
  1245. td = td.cloneNode(false);
  1246. td.appendChild(uname);
  1247. tr.appendChild(td);
  1248. tbody.appendChild(tr);
  1249. td = td.cloneNode(false);
  1250. var pword = document.createElement('input');
  1251. pword.setAttribute('type', 'password');
  1252. mxUtils.write(td, 'Password:');
  1253. tr = tr.cloneNode(false);
  1254. tr.appendChild(td);
  1255. td = td.cloneNode(false);
  1256. td.appendChild(pword);
  1257. tr.appendChild(td);
  1258. tbody.appendChild(tr);
  1259. td = td.cloneNode(false);
  1260. var org = document.createElement('input');
  1261. org.setAttribute('type', 'text');
  1262. mxUtils.write(td, 'Organisation:');
  1263. tr = tr.cloneNode(false);
  1264. tr.appendChild(td);
  1265. td = td.cloneNode(false);
  1266. td.appendChild(org);
  1267. tr.appendChild(td);
  1268. tbody.appendChild(tr);
  1269. td = td.cloneNode(false);
  1270. var repo = document.createElement('input');
  1271. repo.setAttribute('type', 'text');
  1272. mxUtils.write(td, 'Repository:');
  1273. tr = tr.cloneNode(false);
  1274. tr.appendChild(td);
  1275. td = td.cloneNode(false);
  1276. td.appendChild(repo);
  1277. tr.appendChild(td);
  1278. tbody.appendChild(tr);
  1279. td = td.cloneNode(false);
  1280. var path = document.createElement('input');
  1281. path.setAttribute('type', 'text');
  1282. mxUtils.write(td, 'Path:');
  1283. tr = tr.cloneNode(false);
  1284. tr.appendChild(td);
  1285. td = td.cloneNode(false);
  1286. td.appendChild(path);
  1287. tr.appendChild(td);
  1288. tbody.appendChild(tr);
  1289. td = td.cloneNode(false);
  1290. var file = this.getCurrentFile();
  1291. var filename = (file != null && file.getTitle() != null) ? file.getTitle() : this.defaultFilename;
  1292. var dot = filename.lastIndexOf('.');
  1293. if (dot > 0)
  1294. {
  1295. filename = filename.substring(0, dot);
  1296. }
  1297. path.value = filename + '.png';
  1298. var ref = document.createElement('input');
  1299. ref.setAttribute('type', 'text');
  1300. mxUtils.write(td, 'Branch/Tag:');
  1301. ref.value = 'master';
  1302. tr = tr.cloneNode(false);
  1303. tr.appendChild(td);
  1304. td = td.cloneNode(false);
  1305. td.appendChild(ref);
  1306. tr.appendChild(td);
  1307. tbody.appendChild(tr);
  1308. td = td.cloneNode(false);
  1309. var msg = document.createElement('input');
  1310. msg.setAttribute('type', 'text');
  1311. mxUtils.write(td, 'Message:');
  1312. msg.value = 'Updated ' + filename + '.png';
  1313. tr = tr.cloneNode(false);
  1314. tr.appendChild(td);
  1315. td = td.cloneNode(false);
  1316. td.appendChild(msg);
  1317. tr.appendChild(td);
  1318. tbody.appendChild(tr);
  1319. td = td.cloneNode(false);
  1320. table.appendChild(tbody);
  1321. content.appendChild(table);
  1322. var dlg = new FilenameDialog(this, null, mxResources.get('publish'), mxUtils.bind(this, function()
  1323. {
  1324. var url = 'https://api.github.com/repos/' + org.value + '/' + repo.value +
  1325. '/contents/' + path.value + '?ref=' + encodeURIComponent(ref.value);
  1326. resume();
  1327. mxUtils.get(url, mxUtils.bind(this, function(req)
  1328. {
  1329. if (req.getStatus() == 200 || req.getStatus() == 404)
  1330. {
  1331. var obj = JSON.parse(req.getText());
  1332. var entity =
  1333. {
  1334. path: path.value,
  1335. message: msg.value,
  1336. content: base64Data
  1337. };
  1338. if (obj.sha != null)
  1339. {
  1340. entity.sha = obj.sha;
  1341. }
  1342. // Native PUT request
  1343. var req2 = new XMLHttpRequest();
  1344. req2.onreadystatechange = mxUtils.bind(this, function()
  1345. {
  1346. if (req2.readyState == 4)
  1347. {
  1348. if (req2.status >= 200 && req2.status < 300)
  1349. {
  1350. this.spinner.stop();
  1351. this.hideDialog();
  1352. url = 'https://github.com/' + org.value + '/' + repo.value + '/blob/' + ref.value + '/' + path.value;
  1353. var dlg = new ErrorDialog(this, mxResources.get('published'),
  1354. mxResources.get('publishedAt', ['<a href="' + url + '" target="_blank">' + url + '</a>']),
  1355. mxResources.get('close'), mxUtils.bind(this, function()
  1356. {
  1357. this.hideDialog();
  1358. }), null,
  1359. mxResources.get('openInNewWindow'), mxUtils.bind(this, function()
  1360. {
  1361. window.open(url);
  1362. }), false);
  1363. this.showDialog(dlg.container, 340, 170, true, false);
  1364. dlg.init();
  1365. }
  1366. else
  1367. {
  1368. resume = this.spinner.pause();
  1369. this.handleError(JSON.parse(req2.responseText));
  1370. }
  1371. }
  1372. });
  1373. req2.open('PUT', url, true);
  1374. req2.setRequestHeader('Authorization', 'Basic ' +
  1375. btoa(uname.value + ':' + pword.value));
  1376. req2.send(JSON.stringify(entity));
  1377. }
  1378. else
  1379. {
  1380. this.hideDialog();
  1381. this.spinner.stop();
  1382. this.handleError(JSON.parse(req.getText()));
  1383. }
  1384. }), mxUtils.bind(this, function(req)
  1385. {
  1386. this.hideDialog();
  1387. this.spinner.stop();
  1388. this.handleError({message: mxResources.get('unknownError')});
  1389. }));
  1390. }), null, null, content, null, false);
  1391. this.showDialog(dlg.container, 260, 260, true, false);
  1392. dlg.init();
  1393. };
  1394. /**
  1395. *
  1396. */
  1397. EditorUi.prototype.uploadToImgur = function(file, base64Data, editable, socialHandler)
  1398. {
  1399. var resume = this.spinner.pause();
  1400. // Shows a warning dialog before uploading
  1401. var dlg = new ErrorDialog(this, mxResources.get('warning'),
  1402. '<img style="max-width:300px;max-height:80px;margin-bottom:20px;padding:6px;border:1px solid gray;" ' +
  1403. 'src="data:image/png;base64,' + base64Data + '"/><br>' +
  1404. mxResources.get('publishConfirmation'),
  1405. mxResources.get('cancel'), mxUtils.bind(this, function()
  1406. {
  1407. // Do nothing
  1408. }), null,
  1409. mxResources.get('publish'), mxUtils.bind(this, function()
  1410. {
  1411. resume();
  1412. var title = (file.getTitle() != null) ? file.getTitle() : this.defaultFilename;
  1413. var dot = title.lastIndexOf('.');
  1414. var filename = title;
  1415. if (dot > 0)
  1416. {
  1417. title = filename.substring(0, dot);
  1418. filename = title;
  1419. }
  1420. filename += '.png';
  1421. // Indirection via servlet for billing and hiding secrets
  1422. var req = new mxXmlRequest('/imgur', JSON.stringify({type: 'base64', image: base64Data,
  1423. name: filename, title: title, description: 'Made with https://www.draw.io'}), 'POST');
  1424. var extractAndHandleError = mxUtils.bind(this, function(req)
  1425. {
  1426. var e = {message: mxResources.get('unknownError')};
  1427. try
  1428. {
  1429. var res = JSON.parse(req.getText());
  1430. e = {message: res.message || res.data.error};
  1431. }
  1432. catch (err)
  1433. {
  1434. // ignore
  1435. }
  1436. this.handleError(e);
  1437. });
  1438. // First request to upload image to Imgur
  1439. req.send(mxUtils.bind(this, function(req)
  1440. {
  1441. if (req.getStatus() == 200)
  1442. {
  1443. try
  1444. {
  1445. var res = JSON.parse(req.getText());
  1446. // Logs publishing of diagrams
  1447. try
  1448. {
  1449. var img = new Image();
  1450. // Timestamp is added to bypass client-side cache
  1451. var logDomain = window.DRAWIO_LOG_URL != null ? window.DRAWIO_LOG_URL : '';
  1452. img.src = logDomain + '/log?severity=CONFIG&msg=imgur-published:' + res.data.id + '&v=' +
  1453. encodeURIComponent(EditorUi.VERSION) + '&ts=' + new Date().getTime();
  1454. }
  1455. catch (e)
  1456. {
  1457. // ignore
  1458. }
  1459. var showResult = mxUtils.bind(this, function()
  1460. {
  1461. this.spinner.stop();
  1462. var url = 'http://i.imgur.com/' + res.data.id + '.png';
  1463. var deleteUrl = 'https://www.draw.io/imgur?delete=' + res.data.deletehash;
  1464. var dlg = new ErrorDialog(this, mxResources.get('published'),
  1465. mxResources.get('publishedAt', ['<a href="' + url + '" target="_blank">' + url + '</a>']) +
  1466. '<br>' + mxResources.get('deleteUrl', [deleteUrl]),
  1467. mxResources.get('close'), mxUtils.bind(this, function()
  1468. {
  1469. this.hideDialog();
  1470. }), null, mxResources.get('share'), function()
  1471. {
  1472. socialHandler(res.data.id);
  1473. }, false);
  1474. this.showDialog(dlg.container, 340, 170, true, false);
  1475. dlg.init();
  1476. });
  1477. if (!editable)
  1478. {
  1479. showResult();
  1480. }
  1481. else
  1482. {
  1483. // Second request to update the description with the edit link
  1484. // Replacing the .png is workaround for Imgur to handle it as an image
  1485. // Avoiding URL parameter avoids call to getParameter in the servlet
  1486. var url2 = '/imgur?' + res.data.deletehash;
  1487. var req2 = new mxXmlRequest(url2, JSON.stringify({
  1488. title: title, description: 'Edit a copy of this diagram at https://www.draw.io/i/' + res.data.id}), 'POST');
  1489. req2.send(mxUtils.bind(this, function()
  1490. {
  1491. if (req2.getStatus() == 200)
  1492. {
  1493. showResult();
  1494. }
  1495. else
  1496. {
  1497. extractAndHandleError(req2);
  1498. }
  1499. }), mxUtils.bind(this, function()
  1500. {
  1501. extractAndHandleError(req2);
  1502. }));
  1503. }
  1504. }
  1505. catch (e)
  1506. {
  1507. this.handleError(e);
  1508. }
  1509. }
  1510. else
  1511. {
  1512. extractAndHandleError(req);
  1513. }
  1514. }), mxUtils.bind(this, function(req)
  1515. {
  1516. extractAndHandleError(req);
  1517. }));
  1518. }));
  1519. this.showDialog(dlg.container, 320, 250, true, false);
  1520. dlg.init();
  1521. };
  1522. /**
  1523. *
  1524. */
  1525. EditorUi.prototype.publishImage = function(handler, socialHandler)
  1526. {
  1527. var file = this.getCurrentFile();
  1528. if (file != null)
  1529. {
  1530. if (this.isExportToCanvas())
  1531. {
  1532. this.showExportDialog(false, mxResources.get('publish'), 'https://support.draw.io/pages/viewpage.action?pageId=12222625', mxUtils.bind(this, function(scale, transparentBackground, ignoreSelection, addShadow, editable)
  1533. {
  1534. var val = parseInt(scale);
  1535. if (!isNaN(val) && val > 0)
  1536. {
  1537. var scale = val / 100;
  1538. var selectionEmpty = this.editor.graph.isSelectionEmpty();
  1539. ignoreSelection = (ignoreSelection != null) ? ignoreSelection : selectionEmpty;
  1540. if (this.spinner.spin(document.body, mxResources.get('publishing')))
  1541. {
  1542. try
  1543. {
  1544. this.exportToCanvas(mxUtils.bind(this, function(canvas)
  1545. {
  1546. try
  1547. {
  1548. var xml = (editable) ? mxUtils.getXml(this.editor.getGraphXml(ignoreSelection)) : null;
  1549. var data = this.createPngDataUri(canvas, xml);
  1550. handler(file, data.substring(data.lastIndexOf(',') + 1), editable, socialHandler);
  1551. }
  1552. catch (e)
  1553. {
  1554. this.handleError(e);
  1555. }
  1556. }), null, null, null, mxUtils.bind(this, function(e)
  1557. {
  1558. this.handleError(e);
  1559. }), null, ignoreSelection, scale || 1, transparentBackground, addShadow);
  1560. }
  1561. catch (e)
  1562. {
  1563. this.handleError(e);
  1564. }
  1565. }
  1566. }
  1567. }));
  1568. }
  1569. else
  1570. {
  1571. this.showRemoteExportDialog(mxResources.get('publish'), 'https://support.draw.io/pages/viewpage.action?pageId=12222625', mxUtils.bind(this, function(ignoreSelection, editable)
  1572. {
  1573. if (this.spinner.spin(document.body, mxResources.get('publishing')))
  1574. {
  1575. var bounds = this.editor.graph.getGraphBounds();
  1576. var data = this.getFileData(true, null, null, null, ignoreSelection);
  1577. if (bounds.width * bounds.height <= MAX_AREA && data.length <= MAX_REQUEST_SIZE)
  1578. {
  1579. var embed = (editable) ? '1' : '0';
  1580. try
  1581. {
  1582. var req = new mxXmlRequest(EXPORT_URL, 'format=png' +
  1583. '&base64=1&embedXml=' + embed + '&xml=' +
  1584. encodeURIComponent(data));
  1585. req.send(mxUtils.bind(this, function()
  1586. {
  1587. if (req.getStatus() == 200)
  1588. {
  1589. handler(file, req.getText(), editable, socialHandler);
  1590. }
  1591. else
  1592. {
  1593. this.handleError(req);
  1594. }
  1595. }));
  1596. }
  1597. catch (e)
  1598. {
  1599. this.handleError(e);
  1600. }
  1601. }
  1602. else
  1603. {
  1604. this.handleError({message: mxResources.get('drawingTooLarge')}, mxResources.get('error'));
  1605. }
  1606. }
  1607. }));
  1608. }
  1609. }
  1610. };
  1611. /**
  1612. * Translates this point by the given vector.
  1613. *
  1614. * @param {number} dx X-coordinate of the translation.
  1615. * @param {number} dy Y-coordinate of the translation.
  1616. */
  1617. EditorUi.prototype.timeSince = function(date)
  1618. {
  1619. var seconds = Math.floor((new Date() - date) / 1000);
  1620. var interval = Math.floor(seconds / 31536000);
  1621. if (interval > 1)
  1622. {
  1623. return interval + ' ' + mxResources.get('years');
  1624. }
  1625. interval = Math.floor(seconds / 2592000);
  1626. if (interval > 1)
  1627. {
  1628. return interval + ' ' + mxResources.get('months');
  1629. }
  1630. interval = Math.floor(seconds / 86400);
  1631. if (interval > 1)
  1632. {
  1633. return interval + ' ' + mxResources.get('days');
  1634. }
  1635. interval = Math.floor(seconds / 3600);
  1636. if (interval > 1)
  1637. {
  1638. return interval + ' ' + mxResources.get('hours');
  1639. }
  1640. interval = Math.floor(seconds / 60);
  1641. if (interval > 1)
  1642. {
  1643. return interval + ' ' + mxResources.get('minutes');
  1644. }
  1645. if (interval == 1)
  1646. {
  1647. return interval + ' ' + mxResources.get('minute');
  1648. }
  1649. return null;
  1650. };
  1651. /**
  1652. * Converts math in the given SVG
  1653. */
  1654. EditorUi.prototype.convertMath = function(graph, svgRoot, fixPosition, callback)
  1655. {
  1656. // FIXME: Only horizontal dash in output so better no conversion at all
  1657. if (false && graph.mathEnabled && typeof(MathJax) !== 'undefined' && typeof(MathJax.Hub) !== 'undefined')
  1658. {
  1659. // Workaround for lost gradients in Chrome after remove from DOM
  1660. var elts = svgRoot.getElementsByTagName('*');
  1661. for (var i = 0; i < elts.length; i++)
  1662. {
  1663. if (elts[i].getAttribute('id') != null)
  1664. {
  1665. elts[i].setAttribute('id', 'mxTemporaryPrefix-' + elts[i].getAttribute('id'));
  1666. }
  1667. }
  1668. // Temporarily attaches to DOM for rendering
  1669. svgRoot.style.visibility = 'hidden';
  1670. document.body.appendChild(svgRoot);
  1671. Editor.MathJaxRender(svgRoot);
  1672. MathJax.Hub.Queue(mxUtils.bind(this, function ()
  1673. {
  1674. // Removes from DOM
  1675. svgRoot.parentNode.removeChild(svgRoot);
  1676. svgRoot.style.visibility = '';
  1677. // Restores original IDs
  1678. for (var i = 0; i < elts.length; i++)
  1679. {
  1680. if (elts[i].getAttribute('id') != null)
  1681. {
  1682. elts[i].setAttribute('id', elts[i].getAttribute('id').substring('mxTemporaryPrefix-'.length));
  1683. }
  1684. }
  1685. // Keeping scale but moving translate only works for image export which
  1686. // is fine since we do not want the SVG export to contain a workaround.
  1687. // See https://github.com/mathjax/MathJax/issues/279
  1688. if (fixPosition && navigator.userAgent.indexOf('AppleWebKit/') >= 0)
  1689. {
  1690. var fo = svgRoot.getElementsByTagName('foreignObject');
  1691. for (var i = 0; i < fo.length; i++)
  1692. {
  1693. var tr = fo[i].parentNode.parentNode.getAttribute('transform');
  1694. var translate = /translate\(\s*([^\s,)]+)[ ,]([^\s,)]+)/.exec(tr);
  1695. fo[i].setAttribute('x', Math.round(translate[1]));
  1696. fo[i].setAttribute('y', Math.round(translate[2]));
  1697. // Must use translate for crisp rendering
  1698. fo[i].parentNode.parentNode.setAttribute('transform', 'translate(0.5,0.5)' + tr.substring(tr.indexOf(')') + 1));
  1699. }
  1700. }
  1701. callback();
  1702. }));
  1703. }
  1704. else
  1705. {
  1706. callback();
  1707. }
  1708. };
  1709. /**
  1710. * Returns the SVG of the diagram with embedded XML. If a callback function is
  1711. * used, the images are converted to data URIs.
  1712. */
  1713. EditorUi.prototype.getEmbeddedSvg = function(xml, graph, url, noHeader, callback, ignoreSelection, redirect)
  1714. {
  1715. var bg = null;
  1716. if (graph != null)
  1717. {
  1718. bg = graph.background;
  1719. if (bg == mxConstants.NONE)
  1720. {
  1721. bg = null;
  1722. }
  1723. }
  1724. // Sets or disables alternate text for foreignObjects. Disabling is needed
  1725. // because PhantomJS seems to ignore switch statements and paint all text.
  1726. var svgRoot = graph.getSvg(bg, null, null, null, null, ignoreSelection);
  1727. if (xml != null)
  1728. {
  1729. svgRoot.setAttribute('content', encodeURIComponent(xml));
  1730. }
  1731. if (url != null)
  1732. {
  1733. svgRoot.setAttribute('resource', url);
  1734. }
  1735. // LATER: Click on SVG content to start editing
  1736. // if (redirect != null)
  1737. // {
  1738. // // TODO: Ignore anchor tag source for click event
  1739. // svgRoot.setAttribute('style', 'cursor:pointer;');
  1740. // svgRoot.setAttribute('onclick', 'window.location.href=\'' + redirect + '\';');
  1741. // }
  1742. if (callback != null)
  1743. {
  1744. this.convertImages(svgRoot, mxUtils.bind(this, function(svgRoot)
  1745. {
  1746. callback(((!noHeader) ? '<?xml version="1.0" encoding="UTF-8"?>\n' +
  1747. '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' : '') +
  1748. mxUtils.getXml(svgRoot));
  1749. }));
  1750. }
  1751. else
  1752. {
  1753. return ((!noHeader) ? '<?xml version="1.0" encoding="UTF-8"?>\n' +
  1754. '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' : '') +
  1755. mxUtils.getXml(svgRoot);
  1756. }
  1757. };
  1758. /**
  1759. *
  1760. */
  1761. EditorUi.prototype.exportToCanvas = function(callback, width, imageCache, background, error, limitHeight,
  1762. ignoreSelection, scale, transparentBackground, addShadow, converter, graph)
  1763. {
  1764. limitHeight = (limitHeight != null) ? limitHeight : true;
  1765. ignoreSelection = (ignoreSelection != null) ? ignoreSelection : true;
  1766. graph = (graph != null) ? graph : this.editor.graph;
  1767. var bg = (transparentBackground) ? null : graph.background;
  1768. if (bg == mxConstants.NONE)
  1769. {
  1770. bg = null;
  1771. }
  1772. if (bg == null)
  1773. {
  1774. bg = background;
  1775. }
  1776. // Handles special case where background is null but transparent is false
  1777. if (bg == null && transparentBackground == false)
  1778. {
  1779. bg = '#ffffff';
  1780. }
  1781. this.convertImages(graph.getSvg(bg, null, null, null, null, ignoreSelection), mxUtils.bind(this, function(svgRoot)
  1782. {
  1783. var img = new Image();
  1784. img.onload = mxUtils.bind(this, function()
  1785. {
  1786. var canvas = document.createElement('canvas');
  1787. var w = parseInt(svgRoot.getAttribute('width'));
  1788. var h = parseInt(svgRoot.getAttribute('height'));
  1789. scale = (scale != null) ? scale : 1;
  1790. if (width != null)
  1791. {
  1792. scale = (!limitHeight) ? width / w : Math.min(1, Math.min((width * 3) / (h * 4), width / w));
  1793. }
  1794. canvas.setAttribute('width', Math.ceil(scale * w));
  1795. canvas.setAttribute('height', Math.ceil(scale * h));
  1796. var ctx = canvas.getContext('2d');
  1797. ctx.scale(scale, scale);
  1798. ctx.drawImage(img, 0, 0);
  1799. callback(canvas);
  1800. });
  1801. img.onerror = function(e)
  1802. {
  1803. //console.log('img', e, img.src);
  1804. if (error != null)
  1805. {
  1806. error(e);
  1807. }
  1808. };
  1809. try
  1810. {
  1811. if (addShadow)
  1812. {
  1813. this.editor.addSvgShadow(svgRoot);
  1814. }
  1815. this.convertMath(graph, svgRoot, true, mxUtils.bind(this, function()
  1816. {
  1817. img.src = this.createSvgDataUri(mxUtils.getXml(svgRoot));
  1818. }));
  1819. }
  1820. catch (e)
  1821. {
  1822. //console.log('src', e, img.src);
  1823. if (error != null)
  1824. {
  1825. error(e);
  1826. }
  1827. }
  1828. }), imageCache, converter);
  1829. };
  1830. /**
  1831. * Converts all images in the SVG output to data URIs for immediate rendering
  1832. */
  1833. EditorUi.prototype.createImageUrlConverter = function()
  1834. {
  1835. var converter = new mxUrlConverter();
  1836. converter.updateBaseUrl();
  1837. // Extends convert to avoid CORS using an image proxy server
  1838. // LATER: Use img.crossOrigin="anonymous" to avoid proxy
  1839. var convert = converter.convert;
  1840. converter.convert = function(src)
  1841. {
  1842. if (src != null)
  1843. {
  1844. if ((src.substring(0, 7) == 'http://' || src.substring(0, 8) == 'https://') &&
  1845. src.substring(0, converter.baseUrl.length) != converter.baseUrl)
  1846. {
  1847. src = PROXY_URL + '?url=' + encodeURIComponent(src);
  1848. }
  1849. else if (src.substring(0, 19) != 'chrome-extension://')
  1850. {
  1851. src = convert.apply(this, arguments);
  1852. }
  1853. }
  1854. return src;
  1855. };
  1856. return converter;
  1857. };
  1858. /**
  1859. * Converts all images in the SVG output to data URIs for immediate rendering
  1860. */
  1861. EditorUi.prototype.convertImages = function(svgRoot, callback, imageCache, converter)
  1862. {
  1863. // Converts images to data URLs for immediate painting
  1864. if (converter == null)
  1865. {
  1866. converter = this.createImageUrlConverter();
  1867. }
  1868. // Barrier for asynchronous image loading
  1869. var counter = 0;
  1870. function inc()
  1871. {
  1872. counter++;
  1873. };
  1874. function dec()
  1875. {
  1876. counter--;
  1877. if (counter == 0)
  1878. {
  1879. callback(svgRoot);
  1880. }
  1881. };
  1882. var cache = imageCache || new Object();
  1883. var convertImages = mxUtils.bind(this, function(tagName, srcAttr)
  1884. {
  1885. var images = svgRoot.getElementsByTagName(tagName);
  1886. for (var i = 0; i < images.length; i++)
  1887. {
  1888. (mxUtils.bind(this, function(img)
  1889. {
  1890. var src = converter.convert(img.getAttribute(srcAttr));
  1891. // Data URIs are pass-through
  1892. if (src != null && src.substring(0, 5) != 'data:')
  1893. {
  1894. var tmp = cache[src];
  1895. if (tmp == null)
  1896. {
  1897. inc();
  1898. this.convertImageToDataUri(src, function(uri)
  1899. {
  1900. if (uri != null)
  1901. {
  1902. cache[src] = uri;
  1903. img.setAttribute(srcAttr, uri);
  1904. }
  1905. dec();
  1906. });
  1907. }
  1908. else
  1909. {
  1910. img.setAttribute(srcAttr, tmp);
  1911. }
  1912. }
  1913. }))(images[i]);
  1914. }
  1915. });
  1916. // Converts all known image tags in output
  1917. // LATER: Add support for images in CSS
  1918. convertImages('image', 'xlink:href');
  1919. convertImages('img', 'src');
  1920. // All from cache or no images
  1921. if (counter == 0)
  1922. {
  1923. callback(svgRoot);
  1924. }
  1925. };
  1926. /**
  1927. * Translates this point by the given vector.
  1928. *
  1929. * @param {number} dx X-coordinate of the translation.
  1930. * @param {number} dy Y-coordinate of the translation.
  1931. */
  1932. EditorUi.prototype.convertImageToDataUri = function(url, callback)
  1933. {
  1934. if (/(\.svg)$/i.test(url))
  1935. {
  1936. mxUtils.get(url, mxUtils.bind(this, function(req)
  1937. {
  1938. callback(this.createSvgDataUri(req.getText()));
  1939. }),
  1940. function()
  1941. {
  1942. callback();
  1943. });
  1944. }
  1945. else
  1946. {
  1947. var img = new Image();
  1948. img.onload = function()
  1949. {
  1950. var canvas = document.createElement('canvas');
  1951. var ctx = canvas.getContext('2d');
  1952. canvas.height = img.height;
  1953. canvas.width = img.width;
  1954. ctx.drawImage(img, 0, 0);
  1955. callback(canvas.toDataURL());
  1956. };
  1957. img.onerror = function()
  1958. {
  1959. callback();
  1960. };
  1961. img.src = url;
  1962. }
  1963. };
  1964. /**
  1965. * Handling drag and drop and import.
  1966. */
  1967. /**
  1968. * Imports the given XML into the existing diagram.
  1969. */
  1970. EditorUi.prototype.importXml = function(xml, dx, dy, crop, noErrorHandling)
  1971. {
  1972. dx = (dx != null) ? dx : 0;
  1973. dy = (dy != null) ? dy : 0;
  1974. var cells = []
  1975. try
  1976. {
  1977. var graph = this.editor.graph;
  1978. if (xml != null && xml.length > 0)
  1979. {
  1980. var doc = mxUtils.parseXml(xml);
  1981. // Checks for mxfile with multiple pages
  1982. var node = this.editor.extractGraphModel(doc.documentElement, this.pages != null);
  1983. if (node != null && node.nodeName == 'mxfile' && this.pages != null)
  1984. {
  1985. var diagrams = node.getElementsByTagName('diagram');
  1986. if (diagrams.length == 1)
  1987. {
  1988. node = mxUtils.parseXml(graph.decompress(mxUtils.getTextContent(diagrams[0]))).documentElement;
  1989. }
  1990. else if (diagrams.length > 1)
  1991. {
  1992. // Adds pages
  1993. graph.model.beginUpdate();
  1994. try
  1995. {
  1996. for (var i = 0; i < diagrams.length; i++)
  1997. {
  1998. var page = this.updatePageRoot(new DiagramPage(diagrams[i]));
  1999. var index = this.pages.length;
  2000. // Checks for invalid page names
  2001. if (page.getName() == null)
  2002. {
  2003. page.setName(mxResources.get('pageWithNumber', [index + 1]));
  2004. }
  2005. graph.model.execute(new ChangePage(this, page, page, index));
  2006. }
  2007. }
  2008. finally
  2009. {
  2010. graph.model.endUpdate();
  2011. }
  2012. }
  2013. }
  2014. if (node != null && node.nodeName === 'mxGraphModel')
  2015. {
  2016. var model = new mxGraphModel();
  2017. var codec = new mxCodec(node.ownerDocument);
  2018. codec.decode(node, model);
  2019. var childCount = model.getChildCount(model.getRoot());
  2020. var targetChildCount = graph.model.getChildCount(graph.model.getRoot());
  2021. // Merges into active layer if one layer is pasted
  2022. graph.model.beginUpdate();
  2023. try
  2024. {
  2025. // Mapping for multiple calls to cloneCells with the same set of cells
  2026. var mapping = new Object();
  2027. for (var i = 0; i < childCount; i++)
  2028. {
  2029. var parent = model.getChildAt(model.getRoot(), i);
  2030. // Adds cells to existing layer if not locked
  2031. if (childCount == 1 && !graph.isCellLocked(graph.getDefaultParent()))
  2032. {
  2033. var children = model.getChildren(parent);
  2034. cells = cells.concat(graph.importCells(children, dx, dy, graph.getDefaultParent(), null, mapping));
  2035. }
  2036. else
  2037. {
  2038. // Delta is non cascading, needs separate move for layers
  2039. parent = graph.importCells([parent], 0, 0, graph.model.getRoot(), null, mapping)[0];
  2040. var children = graph.model.getChildren(parent);
  2041. graph.moveCells(children, dx, dy);
  2042. cells = cells.concat(children);
  2043. }
  2044. }
  2045. if (crop)
  2046. {
  2047. if (graph.isGridEnabled())
  2048. {
  2049. dx = graph.snap(dx);
  2050. dy = graph.snap(dy);
  2051. }
  2052. var bounds = graph.getBoundingBoxFromGeometry(cells, true);
  2053. if (bounds != null)
  2054. {
  2055. graph.moveCells(cells, dx - bounds.x, dy - bounds.y);
  2056. }
  2057. }
  2058. }
  2059. finally
  2060. {
  2061. graph.model.endUpdate();
  2062. }
  2063. }
  2064. }
  2065. }
  2066. catch (e)
  2067. {
  2068. if (!noErrorHandling)
  2069. {
  2070. this.handleError(e, mxResources.get('invalidOrMissingFile'));
  2071. }
  2072. throw e;
  2073. }
  2074. return cells;
  2075. };
  2076. /**
  2077. * Imports the given XML into the existing diagram.
  2078. * TODO: Make this function asynchronous
  2079. */
  2080. EditorUi.prototype.insertTextAt = function(text, dx, dy, html, asImage, crop)
  2081. {
  2082. crop = (crop != null) ? crop : true;
  2083. // Handles special case for Gliffy data which requires async server-side for parsing
  2084. if (text != null)
  2085. {
  2086. if (Graph.fileSupport && !this.isOffline() && new XMLHttpRequest().upload && this.isRemoteFileFormat(text))
  2087. {
  2088. // Fixes possible parsing problems with ASCII 160 (non-breaking space)
  2089. this.parseFile(new Blob([text.replace(/\s+/g,' ')], {type: 'application/octet-stream'}), mxUtils.bind(this, function(xhr)
  2090. {
  2091. if (xhr.readyState == 4 && xhr.status == 200)
  2092. {
  2093. this.editor.graph.setSelectionCells(this.insertTextAt(xhr.responseText, dx, dy, true));
  2094. }
  2095. }));
  2096. // Returns empty cells array as it is aysynchronous
  2097. return [];
  2098. }
  2099. // Handles special case of data URI which requires async loading for finding size
  2100. else if (!this.isOffline() && (asImage || text.substring(0, 5) == 'data:' || (/\.(gif|jpg|jpeg|tiff|png|svg)$/i).test(text)))
  2101. {
  2102. var graph = this.editor.graph;
  2103. // Checks for embedded XML in PNG
  2104. if (text.substring(0, 22) == 'data:image/png;base64,')
  2105. {
  2106. var xml = this.extractGraphModelFromPng(text);
  2107. var result = this.importXml(xml, dx, dy, crop, true);
  2108. if (result.length > 0)
  2109. {
  2110. return result;
  2111. }
  2112. }
  2113. // Tries to extract embedded XML from SVG data URI
  2114. if (text.substring(0, 19) == 'data:image/svg+xml;')
  2115. {
  2116. try
  2117. {
  2118. var xml = null;
  2119. if (text.substring(0, 26) == 'data:image/svg+xml;base64,')
  2120. {
  2121. xml = text.substring(text.indexOf(',') + 1);
  2122. xml = (window.atob && !mxClient.IS_SF) ? atob(xml) : Base64.decode(xml, true);
  2123. }
  2124. else
  2125. {
  2126. xml = decodeURIComponent(text.substring(text.indexOf(',') + 1));
  2127. }
  2128. var result = this.importXml(xml, dx, dy, crop, true);
  2129. if (result.length > 0)
  2130. {
  2131. return result;
  2132. }
  2133. }
  2134. catch (e)
  2135. {
  2136. // Ignore
  2137. }
  2138. }
  2139. this.loadImage(text, mxUtils.bind(this, function(img)
  2140. {
  2141. if (text.substring(0, 5) == 'data:')
  2142. {
  2143. this.resizeImage(img, text, mxUtils.bind(this, function(data2, w2, h2)
  2144. {
  2145. graph.setSelectionCell(graph.insertVertex(null, null, '', graph.snap(dx), graph.snap(dy),
  2146. w2, h2, 'shape=image;verticalLabelPosition=bottom;labelBackgroundColor=#ffffff;' +
  2147. 'verticalAlign=top;aspect=fixed;image=' + this.convertDataUri(data2) + ';'));
  2148. }), true, this.maxImageSize);
  2149. }
  2150. else
  2151. {
  2152. var s = Math.min(1, Math.min(this.maxImageSize / img.width, this.maxImageSize / img.height));
  2153. var w = Math.round(img.width * s);
  2154. var h = Math.round(img.height * s);
  2155. graph.setSelectionCell(graph.insertVertex(null, null, '', graph.snap(dx), graph.snap(dy),
  2156. w, h, 'shape=image;verticalLabelPosition=bottom;labelBackgroundColor=#ffffff;' +
  2157. 'verticalAlign=top;aspect=fixed;image=' + text + ';'));
  2158. }
  2159. }), mxUtils.bind(this, function()
  2160. {
  2161. var cell = null;
  2162. // Inserts invalid data URIs as text
  2163. graph.getModel().beginUpdate();
  2164. try
  2165. {
  2166. cell = graph.insertVertex(graph.getDefaultParent(), null, text,
  2167. graph.snap(dx), graph.snap(dy), 1, 1, 'text;' + ((html) ? 'html=1;' : ''));
  2168. graph.updateCellSize(cell);
  2169. graph.fireEvent(new mxEventObject('textInserted', 'cells', [cell]));
  2170. }
  2171. finally
  2172. {
  2173. graph.getModel().endUpdate();
  2174. }
  2175. graph.setSelectionCell(cell);
  2176. }));
  2177. return [];
  2178. }
  2179. else
  2180. {
  2181. text = this.editor.graph.zapGremlins(mxUtils.trim(text));
  2182. if (this.isCompatibleString(text))
  2183. {
  2184. return this.importXml(text, dx, dy, crop);
  2185. }
  2186. else if (text.length > 0)
  2187. {
  2188. var graph = this.editor.graph;
  2189. var cell = null;
  2190. graph.getModel().beginUpdate();
  2191. try
  2192. {
  2193. // Fires cellsInserted to apply the current style to the inserted text.
  2194. // This requires the value to be empty when the event is fired.
  2195. cell = graph.insertVertex(graph.getDefaultParent(), null, '',
  2196. graph.snap(dx), graph.snap(dy), 1, 1, 'text;' + ((html) ? 'html=1;' : ''));
  2197. graph.fireEvent(new mxEventObject('textInserted', 'cells', [cell]));
  2198. // Apply value and updates the cell size to fit the text block
  2199. cell.value = text;
  2200. graph.updateCellSize(cell);
  2201. // See http://stackoverflow.com/questions/6927719/url-regex-does-not-work-in-javascript
  2202. var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
  2203. if (regexp.test(cell.value))
  2204. {
  2205. graph.setLinkForCell(cell, cell.value);
  2206. }
  2207. // Adds spacing
  2208. cell.geometry.width += graph.gridSize;
  2209. cell.geometry.height += graph.gridSize;
  2210. }
  2211. finally
  2212. {
  2213. graph.getModel().endUpdate();
  2214. }
  2215. return [cell];
  2216. }
  2217. }
  2218. }
  2219. return [];
  2220. };
  2221. /**
  2222. * Formats the given file size.
  2223. */
  2224. EditorUi.prototype.formatFileSize = function(size)
  2225. {
  2226. var units = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
  2227. var i = -1;
  2228. do
  2229. {
  2230. size = size / 1024;
  2231. i++;
  2232. } while (size > 1024);
  2233. return Math.max(size, 0.1).toFixed(1) + units[i];
  2234. };
  2235. /**
  2236. * Imports the given XML into the existing diagram.
  2237. */
  2238. EditorUi.prototype.convertDataUri = function(uri)
  2239. {
  2240. // Handles special case of data URI which needs to be rewritten
  2241. // to be used in a cell style to remove the semicolon
  2242. if (uri.substring(0, 5) == 'data:')
  2243. {
  2244. var semi = uri.indexOf(';');
  2245. if (semi > 0)
  2246. {
  2247. uri = uri.substring(0, semi) + uri.substring(uri.indexOf(',', semi + 1));
  2248. }
  2249. }
  2250. return uri;
  2251. };
  2252. /**
  2253. * Returns true for Gliffy or GraphML data or .vsdx filenames.
  2254. */
  2255. EditorUi.prototype.isRemoteFileFormat = function(data, filename)
  2256. {
  2257. return /(\.*<graphml xmlns=\".*)/.test(data) ||
  2258. /(\"contentType\":\s*\"application\/gliffy\+json\")/.test(data) ||
  2259. (filename != null && /(\.vsdx)($|\?)/i.test(filename));
  2260. };
  2261. /**
  2262. * Imports the given XML into the existing diagram.
  2263. */
  2264. EditorUi.prototype.importFile = function(data, mimeType, dx, dy, w, h, filename, done, file, crop)
  2265. {
  2266. crop = (crop != null) ? crop : true;
  2267. var async = false;
  2268. var cells = null;
  2269. if (mimeType.substring(0, 5) == 'image')
  2270. {
  2271. var containsModel = false;
  2272. if (mimeType.substring(0, 9) == 'image/png')
  2273. {
  2274. var xml = this.extractGraphModelFromPng(data);
  2275. if (xml != null && xml.length > 0)
  2276. {
  2277. cells = this.importXml(xml, dx, dy, crop);
  2278. containsModel = true;
  2279. }
  2280. }
  2281. if (!containsModel)
  2282. {
  2283. var graph = this.editor.graph;
  2284. // Strips encoding bit (eg. ;base64,) for cell style
  2285. var semi = data.indexOf(';');
  2286. if (semi > 0)
  2287. {
  2288. data = data.substring(0, semi) + data.substring(data.indexOf(',', semi + 1));
  2289. }
  2290. if (crop && graph.isGridEnabled())
  2291. {
  2292. dx = graph.snap(dx);
  2293. dy = graph.snap(dy);
  2294. }
  2295. cells = [graph.insertVertex(null, null, '', dx, dy, w, h,
  2296. 'shape=image;verticalLabelPosition=bottom;labelBackgroundColor=#ffffff;' +
  2297. 'verticalAlign=top;aspect=fixed;image=' + data + ';')];
  2298. }
  2299. }
  2300. else if (!this.isOffline() && new XMLHttpRequest().upload && this.isRemoteFileFormat(data, filename))
  2301. {
  2302. // LATER: done and async are a hack before making this asynchronous
  2303. async = true;
  2304. // Returns empty cells array as it is aysynchronous
  2305. this.parseFile((file != null) ? file : new Blob([data], {type: 'application/octet-stream'}), mxUtils.bind(this, function(xhr)
  2306. {
  2307. if (xhr.readyState == 4)
  2308. {
  2309. var importedCells = null;
  2310. if (xhr.status == 200)
  2311. {
  2312. importedCells = this.importXml(xhr.responseText, dx, dy, crop);
  2313. }
  2314. if (done != null)
  2315. {
  2316. done(importedCells);
  2317. }
  2318. }
  2319. }), filename);
  2320. }
  2321. else
  2322. {
  2323. if (/(\.vsdx)($|\?)/i.test(filename))
  2324. {
  2325. var vsdxModel = new mxVsdxModel();
  2326. vsdxModel.decode(file);
  2327. }
  2328. else
  2329. {
  2330. cells = this.insertTextAt(this.validateFileData(data), dx, dy, true);
  2331. }
  2332. // else if (String.prototype.trim)
  2333. // {
  2334. // var trimmed = data.trim();
  2335. //
  2336. // if (trimmed.substring(0, 6) == 'strict ' || trimmed.substring(0, 5) == 'graph' || trimmed.substring(0, 7) == 'digraph')
  2337. // {
  2338. // // GraphViz dot format http://www.graphviz.org/content/dot-language
  2339. // //var digraph = graphlibDot.read("digraph { 1; 2; 1 -> 2 [label=\"label\"] }");
  2340. // }
  2341. // }
  2342. }
  2343. if (!async && done != null)
  2344. {
  2345. done(cells);
  2346. }
  2347. return cells;
  2348. };
  2349. /**
  2350. * Base64 encodes the given string. This method seems to be more
  2351. * robust for encoding PNG from binary AJAX responses.
  2352. */
  2353. EditorUi.prototype.base64Encode = function(str)
  2354. {
  2355. var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  2356. var out = "", i = 0, len = str.length, c1, c2, c3;
  2357. while (i < len)
  2358. {
  2359. c1 = str.charCodeAt(i++) & 0xff;
  2360. if (i == len)
  2361. {
  2362. out += CHARS.charAt(c1 >> 2);
  2363. out += CHARS.charAt((c1 & 0x3) << 4);
  2364. out += "==";
  2365. break;
  2366. }
  2367. c2 = str.charCodeAt(i++);
  2368. if (i == len)
  2369. {
  2370. out += CHARS.charAt(c1 >> 2);
  2371. out += CHARS.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  2372. out += CHARS.charAt((c2 & 0xF) << 2);
  2373. out += "=";
  2374. break;
  2375. }
  2376. c3 = str.charCodeAt(i++);
  2377. out += CHARS.charAt(c1 >> 2);
  2378. out += CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  2379. out += CHARS.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  2380. out += CHARS.charAt(c3 & 0x3F);
  2381. }
  2382. return out;
  2383. };
  2384. /**
  2385. *
  2386. */
  2387. EditorUi.prototype.importFiles = function(files, x, y, maxSize, fn, resultFn, filterFn, barrierFn, resizeImages, maxBytes, resampleThreshold)
  2388. {
  2389. var crop = x != null && y != null;
  2390. x = (x != null) ? x : 0;
  2391. y = (y != null) ? y : 0;
  2392. maxSize = (maxSize != null) ? maxSize : this.maxImageSize;
  2393. maxBytes = (maxBytes != null) ? maxBytes : this.maxImageBytes;
  2394. resizeImages = (resizeImages != null) ? resizeImages : true;
  2395. var graph = this.editor.graph;
  2396. var gs = graph.gridSize;
  2397. fn = (fn != null) ? fn : mxUtils.bind(this, function(data, mimeType, x, y, w, h, filename, done, file)
  2398. {
  2399. if (data != null && data.substring(0, 10) == '<mxlibrary')
  2400. {
  2401. this.spinner.stop();
  2402. this.loadLibrary(new LocalLibrary(this, data, filename));
  2403. return null;
  2404. }
  2405. else
  2406. {
  2407. return this.importFile(data, mimeType, x, y, w, h, filename, done, file, crop);
  2408. }
  2409. });
  2410. resultFn = (resultFn != null) ? resultFn : mxUtils.bind(this, function(cells)
  2411. {
  2412. graph.setSelectionCells(cells);
  2413. });
  2414. if (this.spinner.spin(document.body, mxResources.get('loading')))
  2415. {
  2416. var count = files.length;
  2417. var remain = count;
  2418. var queue = [];
  2419. // Barrier waits for all files to be loaded asynchronously
  2420. var barrier = mxUtils.bind(this, function(index, fnc)
  2421. {
  2422. queue[index] = fnc;
  2423. if (--remain == 0)
  2424. {
  2425. this.spinner.stop();
  2426. if (barrierFn != null)
  2427. {
  2428. barrierFn(queue);
  2429. }
  2430. else
  2431. {
  2432. var cells = [];
  2433. graph.getModel().beginUpdate();
  2434. try
  2435. {
  2436. for (var j = 0; j < queue.length; j++)
  2437. {
  2438. var tmp = queue[j]();
  2439. if (tmp != null)
  2440. {
  2441. cells = cells.concat(tmp);
  2442. }
  2443. }
  2444. }
  2445. finally
  2446. {
  2447. graph.getModel().endUpdate();
  2448. }
  2449. }
  2450. resultFn(cells);
  2451. }
  2452. });
  2453. for (var i = 0; i < count; i++)
  2454. {
  2455. (mxUtils.bind(this, function(index)
  2456. {
  2457. var file = files[index];
  2458. var reader = new FileReader();
  2459. reader.onload = mxUtils.bind(this, function(e)
  2460. {
  2461. if (filterFn == null || filterFn(file))
  2462. {
  2463. if (file.type.substring(0, 6) == 'image/')
  2464. {
  2465. if (file.type.substring(0, 9) == 'image/svg')
  2466. {
  2467. // Checks if SVG contains content attribute
  2468. var data = e.target.result;
  2469. var comma = data.indexOf(',');
  2470. var svgText = atob(data.substring(comma + 1));
  2471. var root = mxUtils.parseXml(svgText);
  2472. var svgs = root.getElementsByTagName('svg');
  2473. if (svgs.length > 0)
  2474. {
  2475. var svgRoot = svgs[0];
  2476. var cont = svgRoot.getAttribute('content');
  2477. if (cont != null && cont.charAt(0) != '<' && cont.charAt(0) != '%')
  2478. {
  2479. cont = unescape((window.atob) ? atob(cont) : Base64.decode(cont, true));
  2480. }
  2481. if (cont != null && cont.charAt(0) == '%')
  2482. {
  2483. cont = decodeURIComponent(cont);
  2484. }
  2485. if (cont != null && (cont.substring(0, 8) === '<mxfile ' ||
  2486. cont.substring(0, 14) === '<mxGraphModel '))
  2487. {
  2488. barrier(index, mxUtils.bind(this, function()
  2489. {
  2490. return fn(cont, 'text/xml', x + index * gs, y + index * gs, 0, 0, file.name);
  2491. }));
  2492. }
  2493. else
  2494. {
  2495. // SVG needs special handling to add viewbox if missing and
  2496. // find initial size from SVG attributes (only for IE11)
  2497. barrier(index, mxUtils.bind(this, function()
  2498. {
  2499. try
  2500. {
  2501. var prefix = data.substring(0, comma + 1);
  2502. // Parses SVG and find width and height
  2503. if (root != null)
  2504. {
  2505. var svgs = root.getElementsByTagName('svg');
  2506. if (svgs.length > 0)
  2507. {
  2508. var svgRoot = svgs[0];
  2509. var w = parseFloat(svgRoot.getAttribute('width'));
  2510. var h = parseFloat(svgRoot.getAttribute('height'));
  2511. // Check if viewBox attribute already exists
  2512. var vb = svgRoot.getAttribute('viewBox');
  2513. if (vb == null || vb.length == 0)
  2514. {
  2515. svgRoot.setAttribute('viewBox', '0 0 ' + w + ' ' + h);
  2516. }
  2517. // Uses width and height from viewbox for
  2518. // missing width and height attributes
  2519. else if (isNaN(w) || isNaN(h))
  2520. {
  2521. var tokens = vb.split(' ');
  2522. if (tokens.length > 3)
  2523. {
  2524. w = parseFloat(tokens[2]);
  2525. h = parseFloat(tokens[3]);
  2526. }
  2527. }
  2528. data = this.createSvgDataUri(mxUtils.getXml(svgs[0]));
  2529. var s = Math.min(1, Math.min(maxSize / Math.max(1, w)), maxSize / Math.max(1, h));
  2530. return fn(data, file.type, x + index * gs, y + index * gs,
  2531. Math.max(1, Math.round(w * s)), Math.max(1, Math.round(h * s)), file.name);
  2532. }
  2533. }
  2534. }
  2535. catch (e)
  2536. {
  2537. // ignores any SVG parsing errors
  2538. }
  2539. return null;
  2540. }));
  2541. }
  2542. }
  2543. }
  2544. else
  2545. {
  2546. // Checks if PNG+XML is available to bypass code below
  2547. var containsModel = false;
  2548. if (file.type == 'image/png')
  2549. {
  2550. var xml = this.extractGraphModelFromPng(e.target.result);
  2551. if (xml != null && xml.length > 0)
  2552. {
  2553. var img = new Image();
  2554. img.src = e.target.result;
  2555. barrier(index, mxUtils.bind(this, function()
  2556. {
  2557. return fn(xml, 'text/xml', x + index * gs, y + index * gs,
  2558. img.width, img.height, file.name);
  2559. }));
  2560. containsModel = true;
  2561. }
  2562. }
  2563. // Additional asynchronous step for finding image size
  2564. if (!containsModel)
  2565. {
  2566. // Cannot load local files in Chrome App
  2567. if (window.chrome != null && chrome.app != null && chrome.app.runtime != null)
  2568. {
  2569. this.spinner.stop();
  2570. this.showError(mxResources.get('error'), mxResources.get('dragAndDropNotSupported'),
  2571. mxResources.get('cancel'), mxUtils.bind(this, function()
  2572. {
  2573. // Hides the dialog
  2574. }), null, mxResources.get('ok'), mxUtils.bind(this, function()
  2575. {
  2576. // Redirects to import function
  2577. this.actions.get('import').funct();
  2578. })
  2579. );
  2580. }
  2581. else
  2582. {
  2583. this.loadImage(e.target.result, mxUtils.bind(this, function(img)
  2584. {
  2585. this.resizeImage(img, e.target.result, mxUtils.bind(this, function(data2, w2, h2)
  2586. {
  2587. barrier(index, mxUtils.bind(this, function()
  2588. {
  2589. // Refuses to insert images above a certain size as they kill the app
  2590. if (data2 != null && data2.length < maxBytes)
  2591. {
  2592. var s = (!resizeImages || !this.isResampleImage(e.target.result)) ? 1 : Math.min(1, Math.min(maxSize / w2, maxSize / h2));
  2593. return fn(data2, file.type, x + index * gs, y + index * gs, Math.round(w2 * s), Math.round(h2 * s), file.name);
  2594. }
  2595. else
  2596. {
  2597. this.handleError({message: mxResources.get('imageTooBig')});
  2598. return null;
  2599. }
  2600. }));
  2601. }), resizeImages, maxSize, resampleThreshold);
  2602. }));
  2603. }
  2604. }
  2605. }
  2606. }
  2607. else
  2608. {
  2609. fn(e.target.result, file.type, x + index * gs, y + index * gs, 240, 160, file.name, function(cells)
  2610. {
  2611. barrier(index, function()
  2612. {
  2613. return cells;
  2614. });
  2615. });
  2616. }
  2617. }
  2618. });
  2619. // Handles special case of binary file where the reader should not be used
  2620. if (/(\.vsdx)($|\?)/i.test(file.name))
  2621. {
  2622. fn(null, file.type, x + index * gs, y + index * gs, 240, 160, file.name, function(cells)
  2623. {
  2624. barrier(index, function()
  2625. {
  2626. return cells;
  2627. });
  2628. }, file);
  2629. }
  2630. else if (file.type.substring(0, 5) == 'image')
  2631. {
  2632. reader.readAsDataURL(file);
  2633. }
  2634. else
  2635. {
  2636. reader.readAsText(file);
  2637. }
  2638. }))(i);
  2639. }
  2640. }
  2641. };
  2642. /**
  2643. * Parses the file using XHR2 via the server. File can be a blob or file object.
  2644. * Filename is an optional parameter for blobs (that do not have a filename).
  2645. */
  2646. EditorUi.prototype.parseFile = function(file, fn, filename)
  2647. {
  2648. filename = (filename != null) ? filename : file.name;
  2649. var formData = new FormData();
  2650. formData.append('format', 'xml');
  2651. formData.append('upfile', file, filename);
  2652. var xhr = new XMLHttpRequest();
  2653. xhr.open('POST', OPEN_URL);
  2654. xhr.onreadystatechange = function()
  2655. {
  2656. fn(xhr);
  2657. };
  2658. xhr.send(formData);
  2659. };
  2660. /**
  2661. *
  2662. */
  2663. EditorUi.prototype.isResampleImage = function(data, thresh)
  2664. {
  2665. thresh = (thresh != null) ? thresh : this.resampleThreshold;
  2666. return data.length > thresh;
  2667. };
  2668. /**
  2669. * Resizes the given image if <maxImageBytes> is not null.
  2670. */
  2671. EditorUi.prototype.resizeImage = function(img, data, fn, enabled, maxSize, thresh)
  2672. {
  2673. maxSize = (maxSize != null) ? maxSize : this.maxImageSize;
  2674. var w = Math.max(1, img.width);
  2675. var h = Math.max(1, img.height);
  2676. if (enabled && this.isResampleImage(data, thresh))
  2677. {
  2678. try
  2679. {
  2680. var factor = Math.max(w / maxSize, h / maxSize);
  2681. if (factor > 1)
  2682. {
  2683. var w2 = Math.round(w / factor);
  2684. var h2 = Math.round(h / factor);
  2685. var canvas = document.createElement('canvas');
  2686. canvas.width = w2;
  2687. canvas.height = h2;
  2688. var ctx = canvas.getContext('2d');
  2689. ctx.drawImage(img, 0, 0, w2, h2);
  2690. var tmp = canvas.toDataURL();
  2691. // Uses new image if smaller
  2692. if (tmp.length < data.length)
  2693. {
  2694. // Checks if the image is empty by comparing
  2695. // with an empty image of the same size
  2696. var canvas2 = document.createElement('canvas');
  2697. canvas2.width = w2;
  2698. canvas2.height = h2;
  2699. var tmp2 = canvas2.toDataURL();
  2700. if (tmp !== tmp2)
  2701. {
  2702. data = tmp;
  2703. w = w2;
  2704. h = h2;
  2705. }
  2706. }
  2707. }
  2708. }
  2709. catch (e)
  2710. {
  2711. // ignores image scaling errors
  2712. }
  2713. }
  2714. fn(data, w, h);
  2715. };
  2716. /**
  2717. * Initializes CRC table.
  2718. */
  2719. (function()
  2720. {
  2721. EditorUi.prototype.crcTable = [];
  2722. for (var n = 0; n < 256; n++)
  2723. {
  2724. var c = n;
  2725. for (var k = 0; k < 8; k++)
  2726. {
  2727. if ((c & 1) == 1)
  2728. {
  2729. c = 0xedb88320 ^ (c >>> 1);
  2730. }
  2731. else
  2732. {
  2733. c >>>= 1;
  2734. }
  2735. EditorUi.prototype.crcTable[n] = c;
  2736. }
  2737. }
  2738. EditorUi.prototype.updateCRC = function(crc, data, off, len)
  2739. {
  2740. var c = crc;
  2741. for (var n = 0; n < len; n++)
  2742. {
  2743. c = EditorUi.prototype.crcTable[(c ^ data[off + n]) & 0xff] ^ (c >>> 8);
  2744. }
  2745. return c;
  2746. };
  2747. })();
  2748. /**
  2749. * Adds the given text to the compressed or non-compressed text chunk.
  2750. */
  2751. EditorUi.prototype.writeGraphModelToPng = function(data, type, key, value, error)
  2752. {
  2753. var base64 = data.substring(data.indexOf(',') + 1);
  2754. var f = (window.atob) ? atob(base64) : Base64.decode(base64, true);
  2755. var pos = 0;
  2756. function fread(d, count)
  2757. {
  2758. var start = pos;
  2759. pos += count;
  2760. return d.substring(start, pos);
  2761. };
  2762. // Reads unsigned long 32 bit big endian
  2763. function _freadint(d)
  2764. {
  2765. var bytes = fread(d, 4);
  2766. return bytes.charCodeAt(3) + (bytes.charCodeAt(2) << 8) +
  2767. (bytes.charCodeAt(1) << 16) + (bytes.charCodeAt(0) << 24);
  2768. };
  2769. function writeInt(num)
  2770. {
  2771. return String.fromCharCode((num >> 24) & 0x000000ff, (num >> 16) & 0x000000ff,
  2772. (num >> 8) & 0x000000ff, num & 0x000000ff);
  2773. };
  2774. // Checks signature
  2775. if (fread(f,8) != String.fromCharCode(137) + 'PNG' + String.fromCharCode(13, 10, 26, 10))
  2776. {
  2777. if (error != null)
  2778. {
  2779. error();
  2780. }
  2781. return;
  2782. }
  2783. // Reads header chunk
  2784. fread(f,4);
  2785. if (fread(f,4) != 'IHDR')
  2786. {
  2787. if (error != null)
  2788. {
  2789. error();
  2790. }
  2791. return;
  2792. }
  2793. fread(f, 17);
  2794. var result = f.substring(0, pos);
  2795. do
  2796. {
  2797. var n = _freadint(f);
  2798. var chunk = fread(f,4);
  2799. if (chunk == 'IDAT')
  2800. {
  2801. result = f.substring(0, pos - 8);
  2802. var crc = 0xffffffff;
  2803. crc = this.updateCRC(crc, type, 0, 4);
  2804. crc = this.updateCRC(crc, value, 0, value.length);
  2805. result += writeInt(key.length + value.length + 1 + ((type == 'zTXt') ? 1 : 0)) +
  2806. type + key + String.fromCharCode(0) +
  2807. ((type == 'zTXt') ? String.fromCharCode(0) : '') +
  2808. value + writeInt(crc ^ 0xffffffff);
  2809. result += f.substring(pos - 8, f.length);
  2810. break;
  2811. }
  2812. result += f.substring(pos - 8, pos - 4 + n);
  2813. value = fread(f,n);
  2814. fread(f,4);
  2815. }
  2816. while (n);
  2817. return 'data:image/png;base64,' + ((window.btoa) ? btoa(result) : Base64.encode(result, true));
  2818. }
  2819. /**
  2820. * Extracts the XML from the compressed or non-compressed text chunk.
  2821. */
  2822. EditorUi.prototype.extractGraphModelFromPng = function(data)
  2823. {
  2824. var result = null;
  2825. try
  2826. {
  2827. var base64 = data.substring(data.indexOf(',') + 1);
  2828. // Workaround for invalid character error in Safari
  2829. var binary = (window.atob && !mxClient.IS_SF) ? atob(base64) : Base64.decode(base64, true);
  2830. EditorUi.parsePng(binary, mxUtils.bind(this, function(pos, type, length)
  2831. {
  2832. var value = binary.substring(pos + 8, pos + 8 + length);
  2833. if (type == 'zTXt')
  2834. {
  2835. var idx = value.indexOf(String.fromCharCode(0));
  2836. if (value.substring(0, idx) == 'mxGraphModel')
  2837. {
  2838. // Workaround for Java URL Encoder using + for spaces, which isn't compatible with JS
  2839. var xmlData = this.editor.graph.bytesToString(pako.inflateRaw(
  2840. value.substring(idx + 2))).replace(/\+/g,' ');
  2841. if (xmlData != null && xmlData.length > 0)
  2842. {
  2843. result = xmlData;
  2844. }
  2845. }
  2846. }
  2847. // Uncompressed section is normally not used
  2848. else if (type == 'tEXt')
  2849. {
  2850. var vals = value.split(String.fromCharCode(0));
  2851. if (vals.length > 1 && vals[0] == 'mxGraphModel')
  2852. {
  2853. result = vals[1];
  2854. }
  2855. }
  2856. if (result != null || type == 'IDAT')
  2857. {
  2858. // Stops processing the file as our text chunks
  2859. // are always placed before the data section
  2860. return true;
  2861. }
  2862. }));
  2863. }
  2864. catch (e)
  2865. {
  2866. // ignores decoding errors
  2867. }
  2868. if (result != null && result.charAt(0) == '%')
  2869. {
  2870. result = decodeURIComponent(result);
  2871. }
  2872. // Workaround for double encoded content
  2873. if (result != null && result.charAt(0) == '%')
  2874. {
  2875. result = decodeURIComponent(result);
  2876. }
  2877. return result;
  2878. };
  2879. /**
  2880. * Loads the image from the given URI.
  2881. *
  2882. * @param {number} dx X-coordinate of the translation.
  2883. * @param {number} dy Y-coordinate of the translation.
  2884. */
  2885. EditorUi.prototype.loadImage = function(uri, onload, onerror)
  2886. {
  2887. var img = new Image();
  2888. img.onload = function()
  2889. {
  2890. onload(img);
  2891. }
  2892. if (onerror != null)
  2893. {
  2894. img.onerror = onerror;
  2895. }
  2896. img.src = uri;
  2897. };
  2898. // Initializes the user interface
  2899. var editorUiInit = EditorUi.prototype.init;
  2900. EditorUi.prototype.init = function()
  2901. {
  2902. editorUiInit.apply(this, arguments);
  2903. var graph = this.editor.graph;
  2904. var ui = this;
  2905. if (mxClient.IS_SVG)
  2906. {
  2907. // LATER: Add shadow for labels in graph.container (eg. math, NO_FO), scaling
  2908. this.editor.addSvgShadow(graph.view.canvas.ownerSVGElement, null, true);
  2909. }
  2910. /**
  2911. * Specifies the default filename.
  2912. */
  2913. this.defaultFilename = mxResources.get('untitledDiagram');
  2914. /**
  2915. * Adds placeholder for %page% and %pagenumber%
  2916. */
  2917. var graphGetGlobalVariable = graph.getGlobalVariable;
  2918. graph.getGlobalVariable = function(name)
  2919. {
  2920. if (name == 'page' && ui.currentPage != null)
  2921. {
  2922. return ui.currentPage.getName();
  2923. }
  2924. else if (name == 'pagenumber')
  2925. {
  2926. if (ui.currentPage != null && ui.pages != null)
  2927. {
  2928. return mxUtils.indexOf(ui.pages, ui.currentPage) + 1;
  2929. }
  2930. else
  2931. {
  2932. return 1;
  2933. }
  2934. }
  2935. return graphGetGlobalVariable.apply(this, arguments);
  2936. };
  2937. /**
  2938. * Overrides editor filename.
  2939. */
  2940. this.editor.getOrCreateFilename = function()
  2941. {
  2942. var filename = ui.defaultFilename;
  2943. var file = ui.getCurrentFile();
  2944. if (file != null)
  2945. {
  2946. filename = (file.getTitle() != null) ? file.getTitle() : filename;
  2947. }
  2948. return filename;
  2949. };
  2950. // Disables print action for standalone apps on iOS
  2951. // because there is no way to close the new window
  2952. // LATER: Use iframe for print, disable preview
  2953. var printAction = this.actions.get('print');
  2954. printAction.setEnabled(!mxClient.IS_IOS || !navigator.standalone);
  2955. printAction.visible = printAction.isEnabled();
  2956. // Scales pages/graph to fit available size
  2957. if (!this.editor.chromeless)
  2958. {
  2959. // Defines additional hotkeys
  2960. this.keyHandler.bindAction(67, true, 'copyStyle', true); // Ctrl+Shift+C
  2961. this.keyHandler.bindAction(86, true, 'pasteStyle', true); // Ctrl+Shift+V
  2962. this.keyHandler.bindAction(77, true, 'editGeometry', true); // Ctrl+Shift+M
  2963. this.keyHandler.bindAction(88, true, 'insertText', true); // Ctrl+Shift+X
  2964. this.keyHandler.bindAction(75, true, 'insertRectangle'); // Ctrl+K
  2965. this.keyHandler.bindAction(75, true, 'insertEllipse', true); // Ctrl+Shift+K
  2966. // Handles copy paste of images from clipboard
  2967. if (!mxClient.IS_IE)
  2968. {
  2969. graph.container.addEventListener('paste', mxUtils.bind(this, function(evt)
  2970. {
  2971. var graph = this.editor.graph;
  2972. if (!mxEvent.isConsumed(evt) && !graph.isEditing())
  2973. {
  2974. try
  2975. {
  2976. var data = (evt.clipboardData || evt.originalEvent.clipboardData);
  2977. var containsText = false;
  2978. // Workaround for asynchronous paste event processing in textInput
  2979. // is to ignore this event if it contains text/html/rtf (see below).
  2980. // NOTE: Image is not pasted into textInput so can't listen there.
  2981. for (var i = 0; i < data.types.length; i++)
  2982. {
  2983. if (data.types[i].substring(0, 5) === 'text/')
  2984. {
  2985. containsText = true;
  2986. break;
  2987. }
  2988. }
  2989. if (!containsText)
  2990. {
  2991. var items = data.items;
  2992. for (index in items)
  2993. {
  2994. var item = items[index];
  2995. if (item.kind === 'file')
  2996. {
  2997. // LATER: Fix sanitizeHtml to allow for data URIs as image in labels
  2998. // if (graph.isEditing())
  2999. // {
  3000. // this.importFiles([item.getAsFile()], 0, 0, this.maxImageSize, function(data, mimeType, x, y, w, h)
  3001. // {
  3002. // // Inserts image into current text box
  3003. // graph.insertImage(data, w, h);
  3004. // }, function()
  3005. // {
  3006. // // No post processing
  3007. // }, function(file)
  3008. // {
  3009. // // Handles only images
  3010. // return file.type.substring(0, 6) == 'image/';
  3011. // }, function(queue)
  3012. // {
  3013. // // Invokes elements of queue in order
  3014. // for (var i = 0; i < queue.length; i++)
  3015. // {
  3016. // queue[i]();
  3017. // }
  3018. // });
  3019. // }
  3020. // else
  3021. {
  3022. var pt = this.editor.graph.getInsertPoint();
  3023. this.importFiles([item.getAsFile()], pt.x, pt.y, this.maxImageSize);
  3024. mxEvent.consume(evt);
  3025. }
  3026. break;
  3027. }
  3028. }
  3029. }
  3030. }
  3031. catch (e)
  3032. {
  3033. // ignore
  3034. }
  3035. }
  3036. }), false);
  3037. }
  3038. // Focused but invisible textarea during control or meta key events
  3039. var textInput = document.createElement('div');
  3040. textInput.style.position = 'absolute';
  3041. textInput.style.whiteSpace = 'nowrap';
  3042. textInput.style.overflow = 'hidden';
  3043. textInput.style.display = 'block';
  3044. textInput.contentEditable = true;
  3045. mxUtils.setOpacity(textInput, 0);
  3046. textInput.style.width = '1px';
  3047. textInput.style.height = '1px';
  3048. textInput.innerHTML = '&nbsp;';
  3049. var restoreFocus = false;
  3050. // Disables built-in cut, copy and paste shortcuts
  3051. this.keyHandler.bindControlKey(88, null);
  3052. this.keyHandler.bindControlKey(67, null);
  3053. this.keyHandler.bindControlKey(86, null);
  3054. // Shows a textare when control/cmd is pressed to handle native clipboard actions
  3055. mxEvent.addListener(document, 'keydown', mxUtils.bind(this, function(evt)
  3056. {
  3057. // No dialog visible
  3058. var source = mxEvent.getSource(evt);
  3059. if (graph.container != null && graph.isEnabled() && !graph.isMouseDown && !graph.isEditing() &&
  3060. this.dialog == null && source.nodeName != 'INPUT' && source.nodeName != 'TEXTAREA')
  3061. {
  3062. if (evt.keyCode == 224 /* FF */ || (!mxClient.IS_MAC && evt.keyCode == 17 /* Control */) ||
  3063. (mxClient.IS_MAC && evt.keyCode == 91 /* Meta */))
  3064. {
  3065. // Cannot use parentNode for check in IE
  3066. if (!restoreFocus)
  3067. {
  3068. // Avoid autoscroll but allow handling of all pass-through ctrl shortcuts
  3069. textInput.style.left = (graph.container.scrollLeft + 10) + 'px';
  3070. textInput.style.top = (graph.container.scrollTop + 10) + 'px';
  3071. graph.container.appendChild(textInput);
  3072. restoreFocus = true;
  3073. // Workaround for selected document content in quirks mode
  3074. if (mxClient.IS_QUIRKS)
  3075. {
  3076. window.setTimeout(function()
  3077. {
  3078. textInput.focus();
  3079. document.execCommand('selectAll', false, null);
  3080. }, 0);
  3081. }
  3082. else
  3083. {
  3084. textInput.focus();
  3085. document.execCommand('selectAll', false, null);
  3086. }
  3087. }
  3088. }
  3089. }
  3090. }));
  3091. // Clears input and restores focus and selection
  3092. function clearInput()
  3093. {
  3094. window.setTimeout(function()
  3095. {
  3096. textInput.innerHTML = '&nbsp;';
  3097. textInput.focus();
  3098. document.execCommand('selectAll', false, null);
  3099. }, 0);
  3100. };
  3101. mxEvent.addListener(document, 'keyup', mxUtils.bind(this, function(evt)
  3102. {
  3103. // Workaround for asynchronous event read invalid in IE quirks mode
  3104. var keyCode = evt.keyCode;
  3105. // Asynchronous workaround for scroll to origin after paste if the
  3106. // Ctrl-key is not pressed for long enough in FF on Windows
  3107. window.setTimeout(mxUtils.bind(this, function()
  3108. {
  3109. if (restoreFocus && (keyCode == 224 /* FF */ || keyCode == 17 /* Control */ ||
  3110. keyCode == 91 /* Meta */))
  3111. {
  3112. restoreFocus = false;
  3113. if (!graph.isEditing() && this.dialog == null && graph.container != null)
  3114. {
  3115. graph.container.focus();
  3116. }
  3117. textInput.parentNode.removeChild(textInput);
  3118. }
  3119. }), 0);
  3120. }));
  3121. mxEvent.addListener(textInput, 'copy', mxUtils.bind(this, function(evt)
  3122. {
  3123. if (graph.isEnabled())
  3124. {
  3125. mxClipboard.copy(graph);
  3126. this.copyCells(textInput);
  3127. clearInput();
  3128. }
  3129. }));
  3130. mxEvent.addListener(textInput, 'cut', mxUtils.bind(this, function(evt)
  3131. {
  3132. if (graph.isEnabled())
  3133. {
  3134. this.copyCells(textInput, true);
  3135. clearInput();
  3136. }
  3137. }));
  3138. mxEvent.addListener(textInput, 'paste', mxUtils.bind(this, function(evt)
  3139. {
  3140. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  3141. {
  3142. textInput.innerHTML = '&nbsp;';
  3143. textInput.focus();
  3144. window.setTimeout(mxUtils.bind(this, function()
  3145. {
  3146. this.pasteCells(evt, textInput);
  3147. textInput.innerHTML = '&nbsp;';
  3148. }), 0);
  3149. }
  3150. }), true);
  3151. // Needed for IE11
  3152. var isSelectionAllowed2 = this.isSelectionAllowed;
  3153. this.isSelectionAllowed = function(evt)
  3154. {
  3155. if (mxEvent.getSource(evt) == textInput)
  3156. {
  3157. return true;
  3158. }
  3159. return isSelectionAllowed2.apply(this, arguments);
  3160. };
  3161. };
  3162. var y = Math.max(document.body.clientHeight || 0, document.documentElement.clientHeight || 0) / 2;
  3163. var x = document.body.clientWidth / 2 - 2;
  3164. // Holds the x-coordinate of the point
  3165. this.spinner = this.createSpinner(x, y, 24);
  3166. // Installs drag and drop handler for rich text editor
  3167. if (Graph.fileSupport)
  3168. {
  3169. this.editor.graph.addListener(mxEvent.EDITING_STARTED, mxUtils.bind(this, function(evt)
  3170. {
  3171. // Setup the dnd listeners
  3172. var graph = this.editor.graph;
  3173. var textElt = graph.cellEditor.text2;
  3174. var dropElt = null;
  3175. if (textElt != null)
  3176. {
  3177. mxEvent.addListener(textElt, 'dragleave', function(evt)
  3178. {
  3179. if (dropElt != null)
  3180. {
  3181. dropElt.parentNode.removeChild(dropElt);
  3182. dropElt = null;
  3183. }
  3184. evt.stopPropagation();
  3185. evt.preventDefault();
  3186. });
  3187. mxEvent.addListener(textElt, 'dragover', mxUtils.bind(this, function(evt)
  3188. {
  3189. // IE 10 does not implement pointer-events so it can't have a drop highlight
  3190. if (dropElt == null && (!mxClient.IS_IE || document.documentMode > 10))
  3191. {
  3192. dropElt = this.highlightElement(textElt);
  3193. }
  3194. evt.stopPropagation();
  3195. evt.preventDefault();
  3196. }));
  3197. mxEvent.addListener(textElt, 'drop', mxUtils.bind(this, function(evt)
  3198. {
  3199. if (dropElt != null)
  3200. {
  3201. dropElt.parentNode.removeChild(dropElt);
  3202. dropElt = null;
  3203. }
  3204. if (evt.dataTransfer.files.length > 0)
  3205. {
  3206. this.importFiles(evt.dataTransfer.files, 0, 0, this.maxImageSize, function(data, mimeType, x, y, w, h)
  3207. {
  3208. // Inserts image into current text box
  3209. graph.insertImage(data, w, h);
  3210. }, function()
  3211. {
  3212. // No post processing
  3213. }, function(file)
  3214. {
  3215. // Handles only images
  3216. return file.type.substring(0, 6) == 'image/';
  3217. }, function(queue)
  3218. {
  3219. // Invokes elements of queue in order
  3220. for (var i = 0; i < queue.length; i++)
  3221. {
  3222. queue[i]();
  3223. }
  3224. }, !mxEvent.isControlDown(evt));
  3225. }
  3226. else if (mxUtils.indexOf(evt.dataTransfer.types, 'text/uri-list') >= 0)
  3227. {
  3228. var uri = evt.dataTransfer.getData('text/uri-list');
  3229. if ((/\.(gif|jpg|jpeg|tiff|png|svg)$/i).test(uri))
  3230. {
  3231. this.loadImage(decodeURIComponent(uri), mxUtils.bind(this, function(img)
  3232. {
  3233. var w = Math.max(1, img.width);
  3234. var h = Math.max(1, img.height);
  3235. var maxSize = this.maxImageSize;
  3236. var s = Math.min(1, Math.min(maxSize / Math.max(1, w)), maxSize / Math.max(1, h));
  3237. graph.insertImage(decodeURIComponent(uri), w * s, h * s);
  3238. }));
  3239. }
  3240. else
  3241. {
  3242. document.execCommand('insertHTML', false, evt.dataTransfer.getData('text/plain'));
  3243. }
  3244. }
  3245. else
  3246. {
  3247. if (mxUtils.indexOf(evt.dataTransfer.types, 'text/html') >= 0)
  3248. {
  3249. document.execCommand('insertHTML', false, evt.dataTransfer.getData('text/html'));
  3250. }
  3251. else if (mxUtils.indexOf(evt.dataTransfer.types, 'text/plain') >= 0)
  3252. {
  3253. document.execCommand('insertHTML', false, evt.dataTransfer.getData('text/plain'));
  3254. }
  3255. }
  3256. evt.stopPropagation();
  3257. evt.preventDefault();
  3258. }));
  3259. }
  3260. }));
  3261. }
  3262. // Adds an element to edit the style in the footer in test mode
  3263. if (urlParams['test'] == '1')
  3264. {
  3265. var footer = document.getElementById('geFooter');
  3266. if (footer != null)
  3267. {
  3268. this.styleInput = document.createElement('input');
  3269. this.styleInput.setAttribute('type', 'text');
  3270. this.styleInput.style.position = 'absolute';
  3271. this.styleInput.style.top = '14px';
  3272. this.styleInput.style.left = '2px';
  3273. // Workaround for ignore right CSS property in FF
  3274. this.styleInput.style.width = '98%';
  3275. this.styleInput.style.visibility = 'hidden';
  3276. this.styleInput.style.opacity = '0.9';
  3277. mxEvent.addListener(this.styleInput, 'change', mxUtils.bind(this, function()
  3278. {
  3279. this.editor.graph.getModel().setStyle(this.editor.graph.getSelectionCell(), this.styleInput.value);
  3280. }));
  3281. footer.appendChild(this.styleInput);
  3282. this.editor.graph.getSelectionModel().addListener(mxEvent.CHANGE, mxUtils.bind(this, function(sender, evt)
  3283. {
  3284. if (this.editor.graph.getSelectionCount() > 0)
  3285. {
  3286. var cell = this.editor.graph.getSelectionCell();
  3287. var style = this.editor.graph.getModel().getStyle(cell);
  3288. this.styleInput.value = style || '';
  3289. this.styleInput.style.visibility = 'visible';
  3290. } else
  3291. {
  3292. this.styleInput.style.visibility = 'hidden';
  3293. }
  3294. }));
  3295. }
  3296. var isSelectionAllowed = this.isSelectionAllowed;
  3297. this.isSelectionAllowed = function(evt)
  3298. {
  3299. if (mxEvent.getSource(evt) == this.styleInput)
  3300. {
  3301. return true;
  3302. }
  3303. return isSelectionAllowed.apply(this, arguments);
  3304. };
  3305. }
  3306. // Removes info text in page
  3307. var info = document.getElementById('geInfo');
  3308. if (info != null)
  3309. {
  3310. info.parentNode.removeChild(info);
  3311. }
  3312. // Installs drag and drop handler for files
  3313. // Enables dropping files
  3314. if (Graph.fileSupport)
  3315. {
  3316. // Setup the dnd listeners
  3317. var dropElt = null;
  3318. mxEvent.addListener(graph.container, 'dragleave', function(evt)
  3319. {
  3320. if (graph.isEnabled())
  3321. {
  3322. if (dropElt != null)
  3323. {
  3324. dropElt.parentNode.removeChild(dropElt);
  3325. dropElt = null;
  3326. }
  3327. evt.stopPropagation();
  3328. evt.preventDefault();
  3329. }
  3330. });
  3331. mxEvent.addListener(graph.container, 'dragover', mxUtils.bind(this, function(evt)
  3332. {
  3333. // IE 10 does not implement pointer-events so it can't have a drop highlight
  3334. if (dropElt == null && (!mxClient.IS_IE || document.documentMode > 10))
  3335. {
  3336. dropElt = this.highlightElement(graph.container);
  3337. }
  3338. if (this.sidebar != null)
  3339. {
  3340. this.sidebar.hideTooltip();
  3341. }
  3342. evt.stopPropagation();
  3343. evt.preventDefault();
  3344. }));
  3345. mxEvent.addListener(graph.container, 'drop', mxUtils.bind(this, function(evt)
  3346. {
  3347. if (dropElt != null)
  3348. {
  3349. dropElt.parentNode.removeChild(dropElt);
  3350. dropElt = null;
  3351. }
  3352. if (graph.isEnabled())
  3353. {
  3354. var pt = mxUtils.convertPoint(graph.container, mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  3355. var tr = graph.view.translate;
  3356. var scale = graph.view.scale;
  3357. var x = pt.x / scale - tr.x;
  3358. var y = pt.y / scale - tr.y;
  3359. if (mxEvent.isAltDown(evt))
  3360. {
  3361. x = 0;
  3362. y = 0;
  3363. }
  3364. if (evt.dataTransfer.files.length > 0)
  3365. {
  3366. this.importFiles(evt.dataTransfer.files, x, y, this.maxImageSize, null, null,
  3367. null, null, !mxEvent.isControlDown(evt) && !mxEvent.isShiftDown(evt));
  3368. }
  3369. else
  3370. {
  3371. var uri = (mxUtils.indexOf(evt.dataTransfer.types, 'text/uri-list') >= 0) ?
  3372. evt.dataTransfer.getData('text/uri-list') : null;
  3373. var data = this.extractGraphModelFromEvent(evt);
  3374. if (data != null)
  3375. {
  3376. graph.setSelectionCells(this.importXml(data, x, y, true));
  3377. }
  3378. else if (mxUtils.indexOf(evt.dataTransfer.types, 'text/html') >= 0)
  3379. {
  3380. var html = evt.dataTransfer.getData('text/html');
  3381. var div = document.createElement('div');
  3382. div.innerHTML = html;
  3383. // The default is based on the extension
  3384. var asImage = null;
  3385. // Extracts single image
  3386. var imgs = div.getElementsByTagName('img');
  3387. if (imgs != null && imgs.length == 1)
  3388. {
  3389. html = imgs[0].getAttribute('src');
  3390. // Handles special case where the src attribute has no valid extension
  3391. // in which case the text would be inserted as text with a link
  3392. if (!(/\.(gif|jpg|jpeg|tiff|png|svg)$/i).test(html))
  3393. {
  3394. asImage = true;
  3395. }
  3396. }
  3397. else
  3398. {
  3399. // Extracts single link
  3400. var a = div.getElementsByTagName('a');
  3401. if (a != null && a.length == 1)
  3402. {
  3403. html = a[0].getAttribute('href');
  3404. }
  3405. }
  3406. graph.setSelectionCells(this.insertTextAt(html, x, y, true, asImage));
  3407. }
  3408. else if (uri != null && (/\.(gif|jpg|jpeg|tiff|png|svg)$/i).test(uri))
  3409. {
  3410. this.loadImage(decodeURIComponent(uri), mxUtils.bind(this, function(img)
  3411. {
  3412. var w = Math.max(1, img.width);
  3413. var h = Math.max(1, img.height);
  3414. var maxSize = this.maxImageSize;
  3415. var s = Math.min(1, Math.min(maxSize / Math.max(1, w)), maxSize / Math.max(1, h));
  3416. graph.setSelectionCell(graph.insertVertex(null, null, '', x, y, w * s, h * s,
  3417. 'shape=image;verticalLabelPosition=bottom;labelBackgroundColor=#ffffff;' +
  3418. 'verticalAlign=top;aspect=fixed;image=' + uri + ';'));
  3419. }), mxUtils.bind(this, function(img)
  3420. {
  3421. graph.setSelectionCells(this.insertTextAt(uri, x, y, true));
  3422. }));
  3423. }
  3424. else if (mxUtils.indexOf(evt.dataTransfer.types, 'text/plain') >= 0)
  3425. {
  3426. graph.setSelectionCells(this.insertTextAt(evt.dataTransfer.getData('text/plain'), x, y, true));
  3427. }
  3428. }
  3429. }
  3430. evt.stopPropagation();
  3431. evt.preventDefault();
  3432. }), false);
  3433. }
  3434. this.initPages();
  3435. // Embedded mode
  3436. if (urlParams['embed'] == '1')
  3437. {
  3438. this.initializeEmbedMode();
  3439. }
  3440. };
  3441. /**
  3442. * Creates the format panel and adds overrides.
  3443. */
  3444. EditorUi.prototype.copyCells = function(elt, removeCells)
  3445. {
  3446. var graph = this.editor.graph;
  3447. if (!graph.isSelectionEmpty())
  3448. {
  3449. var cells = mxUtils.sortCells(graph.model.getTopmostCells(graph.getSelectionCells()));
  3450. // LATER: Add span with XML in data attribute
  3451. // var span = document.createElement('span');
  3452. // span.setAttribute('data-jgraph-type', 'application/vnd.jgraph.xml');
  3453. // span.setAttribute('data-jgraph-content', mxUtils.getXml(graph.encodeCells(clones)));
  3454. // Fixes cross-platform clipboard UTF8 issues by encoding as URI
  3455. var xml = mxUtils.getXml(this.editor.graph.encodeCells(cells));
  3456. mxUtils.setTextContent(elt, encodeURIComponent(xml));
  3457. if (removeCells)
  3458. {
  3459. graph.removeCells(cells, false);
  3460. graph.lastPasteXml = null;
  3461. }
  3462. else
  3463. {
  3464. graph.lastPasteXml = xml;
  3465. graph.pasteCounter = 0;
  3466. }
  3467. elt.focus();
  3468. document.execCommand('selectAll', false, null);
  3469. }
  3470. else
  3471. {
  3472. // Disables copy on focused element
  3473. elt.innerHTML = '';
  3474. }
  3475. };
  3476. /**
  3477. * Creates the format panel and adds overrides.
  3478. */
  3479. EditorUi.prototype.pasteCells = function(evt, elt)
  3480. {
  3481. if (!mxEvent.isConsumed(evt))
  3482. {
  3483. var graph = this.editor.graph;
  3484. var xml = mxUtils.trim((mxClient.IS_QUIRKS || document.documentMode == 8) ?
  3485. mxUtils.getTextContent(elt) : elt.textContent);
  3486. var compat = false;
  3487. // Workaround for junk after XML in VM
  3488. try
  3489. {
  3490. var idx = xml.lastIndexOf('%3E');
  3491. if (idx < xml.length - 3)
  3492. {
  3493. xml = xml.substring(0, idx + 3);
  3494. }
  3495. }
  3496. catch (e)
  3497. {
  3498. // ignore
  3499. }
  3500. // Checks for embedded XML content
  3501. try
  3502. {
  3503. var spans = elt.getElementsByTagName('span');
  3504. var tmp = (spans != null && spans.length > 0) ?
  3505. mxUtils.trim(decodeURIComponent(spans[0].textContent)) :
  3506. decodeURIComponent(xml);
  3507. if (this.isCompatibleString(tmp))
  3508. {
  3509. compat = true;
  3510. xml = tmp;
  3511. }
  3512. }
  3513. catch (e)
  3514. {
  3515. // ignore
  3516. }
  3517. if (graph.lastPasteXml == xml)
  3518. {
  3519. graph.pasteCounter++;
  3520. }
  3521. else
  3522. {
  3523. graph.lastPasteXml = xml;
  3524. graph.pasteCounter = 0;
  3525. }
  3526. var dx = graph.pasteCounter * graph.gridSize;
  3527. if (xml != null && xml.length > 0)
  3528. {
  3529. if (compat || this.isCompatibleString(xml))
  3530. {
  3531. graph.setSelectionCells(this.importXml(xml, dx, dx));
  3532. }
  3533. else
  3534. {
  3535. var pt = graph.getInsertPoint();
  3536. graph.setSelectionCells(this.insertTextAt(xml, pt.x + dx, pt.y + dx, true));
  3537. }
  3538. if (!graph.isSelectionEmpty())
  3539. {
  3540. graph.scrollCellToVisible(graph.getSelectionCell());
  3541. if (this.hoverIcons != null)
  3542. {
  3543. this.hoverIcons.update(graph.view.getState(graph.getSelectionCell()));
  3544. }
  3545. try
  3546. {
  3547. mxEvent.consume(evt);
  3548. }
  3549. catch (e)
  3550. {
  3551. // ignore event no longer exists in async handler in IE8-
  3552. }
  3553. }
  3554. }
  3555. }
  3556. };
  3557. /**
  3558. * Creates the format panel and adds overrides.
  3559. */
  3560. var editorUiCreateFormat = EditorUi.prototype.createFormat;
  3561. EditorUi.prototype.createFormat = function(container)
  3562. {
  3563. /**
  3564. * Overrides for Format sidebar.
  3565. */
  3566. var formatInit = Format.prototype.init;
  3567. Format.prototype.init = function()
  3568. {
  3569. formatInit.apply(this, arguments);
  3570. var ui = this.editorUi;
  3571. ui.editor.addListener('fileLoaded', this.update);
  3572. };
  3573. var formatRefresh = Format.prototype.refresh;
  3574. Format.prototype.refresh = function()
  3575. {
  3576. var ui = this.editorUi;
  3577. if (ui.getCurrentFile() != null || urlParams['embed'] == '1')
  3578. {
  3579. formatRefresh.apply(this, arguments);
  3580. }
  3581. else
  3582. {
  3583. this.clear();
  3584. }
  3585. };
  3586. /**
  3587. * Adds autosave and math typesetting options.
  3588. */
  3589. var diagramFormatPanelAddOptions = DiagramFormatPanel.prototype.addOptions;
  3590. DiagramFormatPanel.prototype.addOptions = function(div)
  3591. {
  3592. div = diagramFormatPanelAddOptions.apply(this, arguments);
  3593. var ui = this.editorUi;
  3594. var editor = ui.editor;
  3595. var graph = editor.graph;
  3596. if (graph.isEnabled())
  3597. {
  3598. var file = ui.getCurrentFile();
  3599. if (file != null && file.isAutosaveOptional())
  3600. {
  3601. var opt = this.createOption(mxResources.get('autosave'), function()
  3602. {
  3603. return ui.editor.autosave;
  3604. }, function(checked)
  3605. {
  3606. ui.editor.setAutosave(checked);
  3607. },
  3608. {
  3609. install: function(apply)
  3610. {
  3611. this.listener = function()
  3612. {
  3613. apply(ui.editor.autosave);
  3614. };
  3615. ui.editor.addListener('autosaveChanged', this.listener);
  3616. },
  3617. destroy: function()
  3618. {
  3619. ui.editor.removeListener(this.listener);
  3620. }
  3621. });
  3622. div.appendChild(opt);
  3623. }
  3624. }
  3625. return div;
  3626. };
  3627. /**
  3628. * Adds predefiend styles.
  3629. */
  3630. var StyleFormatPanelInit = StyleFormatPanel.prototype.init;
  3631. StyleFormatPanel.prototype.init = function()
  3632. {
  3633. // TODO: Update sstate in Format
  3634. var sstate = this.format.createSelectionState();
  3635. if (sstate.style.shape != 'image')
  3636. {
  3637. this.container.appendChild(this.addStyles(this.createPanel()));
  3638. }
  3639. StyleFormatPanelInit.apply(this, arguments);
  3640. };
  3641. /**
  3642. * Overridden to add copy and paste style.
  3643. */
  3644. var styleFormatPanelAddStyleOps = StyleFormatPanel.prototype.addStyleOps;
  3645. StyleFormatPanel.prototype.addStyleOps = function(div)
  3646. {
  3647. var btn = mxUtils.button(mxResources.get('copyStyle'), mxUtils.bind(this, function(evt)
  3648. {
  3649. this.editorUi.actions.get('copyStyle').funct();
  3650. }));
  3651. btn.setAttribute('title', mxResources.get('copyStyle') + ' (' + this.editorUi.actions.get('copyStyle').shortcut + ')');
  3652. btn.style.marginBottom = '2px';
  3653. btn.style.width = '100px';
  3654. btn.style.marginRight = '2px';
  3655. div.appendChild(btn);
  3656. var btn = mxUtils.button(mxResources.get('pasteStyle'), mxUtils.bind(this, function(evt)
  3657. {
  3658. this.editorUi.actions.get('pasteStyle').funct();
  3659. }));
  3660. btn.setAttribute('title', mxResources.get('pasteStyle') + ' (' + this.editorUi.actions.get('pasteStyle').shortcut + ')');
  3661. btn.style.marginBottom = '2px';
  3662. btn.style.width = '100px';
  3663. div.appendChild(btn);
  3664. mxUtils.br(div);
  3665. return styleFormatPanelAddStyleOps.apply(this, arguments);
  3666. };
  3667. /**
  3668. * Creates the buttons for the predefined styles.
  3669. */
  3670. StyleFormatPanel.prototype.addStyles = function(div)
  3671. {
  3672. var graph = this.editorUi.editor.graph;
  3673. var picker = document.createElement('div');
  3674. picker.style.whiteSpace = 'normal';
  3675. picker.style.paddingLeft = '24px';
  3676. picker.style.paddingRight = '20px';
  3677. div.style.paddingLeft = '16px';
  3678. div.style.paddingBottom = '6px';
  3679. div.style.position = 'relative';
  3680. div.appendChild(picker);
  3681. var stylenames = ['plain-gray', 'plain-blue', 'plain-green', 'plain-orange',
  3682. 'plain-yellow', 'plain-red', 'plain-purple', null];
  3683. function updateScheme(colorsets)
  3684. {
  3685. function addButton(colorset)
  3686. {
  3687. var btn = mxUtils.button('', function(evt)
  3688. {
  3689. graph.getModel().beginUpdate();
  3690. try
  3691. {
  3692. var cells = graph.getSelectionCells();
  3693. for (var i = 0; i < cells.length; i++)
  3694. {
  3695. var style = graph.getModel().getStyle(cells[i]);
  3696. for (var j = 0; j < stylenames.length; j++)
  3697. {
  3698. style = mxUtils.removeStylename(style, stylenames[j]);
  3699. }
  3700. if (colorset != null)
  3701. {
  3702. style = mxUtils.setStyle(style, mxConstants.STYLE_FILLCOLOR, colorset['fill']);
  3703. style = mxUtils.setStyle(style, mxConstants.STYLE_STROKECOLOR, colorset['stroke']);
  3704. style = mxUtils.setStyle(style, mxConstants.STYLE_GRADIENTCOLOR, colorset['gradient']);
  3705. }
  3706. else
  3707. {
  3708. style = mxUtils.setStyle(style, mxConstants.STYLE_FILLCOLOR, '#ffffff');
  3709. style = mxUtils.setStyle(style, mxConstants.STYLE_STROKECOLOR, '#000000');
  3710. style = mxUtils.setStyle(style, mxConstants.STYLE_GRADIENTCOLOR, null);
  3711. }
  3712. graph.getModel().setStyle(cells[i], style);
  3713. }
  3714. }
  3715. finally
  3716. {
  3717. graph.getModel().endUpdate();
  3718. }
  3719. })
  3720. btn.style.width = '36px';
  3721. btn.style.height = '30px';
  3722. btn.style.margin = '0px 6px 6px 0px';
  3723. if (colorset != null)
  3724. {
  3725. if (colorset['gradient'] != null)
  3726. {
  3727. if (mxClient.IS_IE && (mxClient.IS_QUIRKS || document.documentMode < 10))
  3728. {
  3729. btn.style.filter = 'progid:DXImageTransform.Microsoft.Gradient('+
  3730. 'StartColorStr=\'' + colorset['fill'] +
  3731. '\', EndColorStr=\'' + colorset['gradient'] + '\', GradientType=0)';
  3732. }
  3733. else
  3734. {
  3735. btn.style.backgroundImage = 'linear-gradient(' + colorset['fill'] + ' 0px,' +
  3736. colorset['gradient'] + ' 100%)';
  3737. }
  3738. }
  3739. else
  3740. {
  3741. btn.style.backgroundColor = colorset['fill'];
  3742. }
  3743. btn.style.border = '1px solid ' + colorset['stroke'];
  3744. }
  3745. else
  3746. {
  3747. btn.style.backgroundColor = '#ffffff';
  3748. btn.style.border = '1px solid #000000';
  3749. }
  3750. picker.appendChild(btn);
  3751. };
  3752. picker.innerHTML = '';
  3753. for (var i = 0; i < colorsets.length; i++)
  3754. {
  3755. if (i > 0 && mxUtils.mod(i, 4) == 0)
  3756. {
  3757. mxUtils.br(picker);
  3758. }
  3759. addButton(colorsets[i]);
  3760. }
  3761. };
  3762. if (this.editorUi.currentScheme == null)
  3763. {
  3764. this.editorUi.currentScheme = 0;
  3765. }
  3766. var schemes = [[null, {fill: '#f5f5f5', stroke: '#666666'},
  3767. {fill: '#dae8fc', stroke: '#6c8ebf'}, {fill: '#d5e8d4', stroke: '#82b366'},
  3768. {fill: '#ffe6cc', stroke: '#d79b00'}, {fill: '#fff2cc', stroke: '#d6b656'},
  3769. {fill: '#f8cecc', stroke: '#b85450'}, {fill: '#e1d5e7', stroke: '#9673a6'}],
  3770. [null,
  3771. {fill: '#f5f5f5', stroke: '#666666', gradient: '#b3b3b3'},
  3772. {fill: '#dae8fc', stroke: '#6c8ebf', gradient: '#7ea6e0'},
  3773. {fill: '#d5e8d4', stroke: '#82b366', gradient: '#97d077'},
  3774. {fill: '#ffcd28', stroke: '#d79b00', gradient: '#ffa500'},
  3775. {fill: '#fff2cc', stroke: '#d6b656', gradient: '#ffd966'},
  3776. {fill: '#f8cecc', stroke: '#b85450', gradient: '#ea6b66'},
  3777. {fill: '#e6d0de', stroke: '#996185', gradient: '#d5739d'}],
  3778. [null, {fill: '#eeeeee', stroke: '#36393d'},
  3779. {fill: '#f9f7ed', stroke: '#36393d'}, {fill: '#ffcc99', stroke: '#36393d'},
  3780. {fill: '#cce5ff', stroke: '#36393d'}, {fill: '#ffff88', stroke: '#36393d'},
  3781. {fill: '#cdeb8b', stroke: '#36393d'}, {fill: '#ffcccc', stroke: '#36393d'}]];
  3782. var left = document.createElement('div');
  3783. left.style.cssText = 'position:absolute;left:10px;top:8px;bottom:8px;width:20px;margin:4px;opacity:0.5;' +
  3784. 'background-repeat:no-repeat;background-position:center center;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAQBAMAAADQT4M0AAAAIVBMVEUAAAB2dnZ4eHh3d3d1dXVxcXF2dnZ2dnZ2dnZxcXF2dnYmb3w1AAAACnRSTlMAfCTkhhvb7cQSPH2JPgAAADRJREFUCNdjwACMAmBKaiGYs2oJmLPKAZ3DabU8AMRTXpUKopislqFyVzCAuUZgikkBZjoAcMYLnp53P/UAAAAASUVORK5CYII=);';
  3785. div.appendChild(left);
  3786. mxEvent.addListener(left, 'click', mxUtils.bind(this, function()
  3787. {
  3788. this.editorUi.currentScheme = mxUtils.mod(this.editorUi.currentScheme - 1, schemes.length);
  3789. updateScheme(schemes[this.editorUi.currentScheme]);
  3790. }));
  3791. var right = document.createElement('div');
  3792. right.style.cssText = 'position:absolute;left:202px;top:8px;bottom:8px;width:20px;margin:4px;opacity:0.5;' +
  3793. 'background-repeat:no-repeat;background-position:center center;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAQBAMAAADQT4M0AAAAIVBMVEUAAAB2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnYBuwCcAAAACnRSTlMAfCTkhhvb7cQSPH2JPgAAADZJREFUCNdjQAOMAmBKaiGY8loF5rKswsZlrVo8AUiFrTICcbIWK8A5DF1gDoMymMPApIAwHwCS0Qx/U7qCBQAAAABJRU5ErkJggg==);';
  3794. div.appendChild(right);
  3795. mxEvent.addListener(right, 'click', mxUtils.bind(this, function()
  3796. {
  3797. this.editorUi.currentScheme = mxUtils.mod(this.editorUi.currentScheme + 1, schemes.length);
  3798. updateScheme(schemes[this.editorUi.currentScheme]);
  3799. }));
  3800. // Hover state
  3801. function addHoverState(elt)
  3802. {
  3803. mxEvent.addListener(elt, 'mouseenter', function()
  3804. {
  3805. elt.style.opacity = '1';
  3806. });
  3807. mxEvent.addListener(elt, 'mouseleave', function()
  3808. {
  3809. elt.style.opacity = '0.5';
  3810. });
  3811. };
  3812. addHoverState(left);
  3813. addHoverState(right);
  3814. updateScheme(schemes[this.editorUi.currentScheme]);
  3815. return div;
  3816. };
  3817. return editorUiCreateFormat.apply(this, arguments);
  3818. };
  3819. // Overridden to add edit shape option
  3820. if (window.StyleFormatPanel != null)
  3821. {
  3822. StyleFormatPanel.prototype.addEditOps = function(div)
  3823. {
  3824. var ss = this.format.getSelectionState();
  3825. var btn = null;
  3826. if (this.editorUi.editor.graph.getSelectionCount() == 1)
  3827. {
  3828. btn = mxUtils.button(mxResources.get('editStyle'), mxUtils.bind(this, function(evt)
  3829. {
  3830. this.editorUi.actions.get('editStyle').funct();
  3831. }));
  3832. btn.setAttribute('title', mxResources.get('editStyle') + ' (' + this.editorUi.actions.get('editStyle').shortcut + ')');
  3833. btn.style.width = '202px';
  3834. btn.style.marginBottom = '2px';
  3835. div.appendChild(btn);
  3836. }
  3837. var graph = this.editorUi.editor.graph;
  3838. var state = graph.view.getState(graph.getSelectionCell());
  3839. if (graph.getSelectionCount() == 1 && state != null && state.shape != null && state.shape.stencil != null)
  3840. {
  3841. var btn2 = mxUtils.button(mxResources.get('editShape'), mxUtils.bind(this, function(evt)
  3842. {
  3843. this.editorUi.actions.get('editShape').funct();
  3844. }));
  3845. btn2.setAttribute('title', mxResources.get('editShape'));
  3846. btn2.style.marginBottom = '2px';
  3847. if (btn == null)
  3848. {
  3849. btn2.style.width = '202px';
  3850. }
  3851. else
  3852. {
  3853. btn.style.width = '100px';
  3854. btn2.style.width = '100px';
  3855. btn2.style.marginLeft = '2px';
  3856. }
  3857. div.appendChild(btn2);
  3858. }
  3859. else if (ss.image)
  3860. {
  3861. var btn2 = mxUtils.button(mxResources.get('editImage'), mxUtils.bind(this, function(evt)
  3862. {
  3863. this.editorUi.actions.get('image').funct();
  3864. }));
  3865. btn2.setAttribute('title', mxResources.get('editImage'));
  3866. btn2.style.marginBottom = '2px';
  3867. if (btn == null)
  3868. {
  3869. btn2.style.width = '202px';
  3870. }
  3871. else
  3872. {
  3873. btn.style.width = '100px';
  3874. btn2.style.width = '100px';
  3875. btn2.style.marginLeft = '2px';
  3876. }
  3877. div.appendChild(btn2);
  3878. }
  3879. return div;
  3880. };
  3881. }
  3882. /**
  3883. * Adds a file drop handler for opening local files.
  3884. */
  3885. EditorUi.prototype.addFileDropHandler = function(elts)
  3886. {
  3887. // Installs drag and drop handler for files
  3888. if (Graph.fileSupport)
  3889. {
  3890. var dropElt = null;
  3891. for (var i = 0; i < elts.length; i++)
  3892. {
  3893. // Setup the dnd listeners
  3894. mxEvent.addListener(elts[i], 'dragleave', function(evt)
  3895. {
  3896. if (dropElt != null)
  3897. {
  3898. dropElt.parentNode.removeChild(dropElt);
  3899. dropElt = null;
  3900. }
  3901. evt.stopPropagation();
  3902. evt.preventDefault();
  3903. });
  3904. mxEvent.addListener(elts[i], 'dragover', mxUtils.bind(this, function(evt)
  3905. {
  3906. // IE 10 does not implement pointer-events so it can't have a drop highlight
  3907. if (dropElt == null && (!mxClient.IS_IE || (document.documentMode > 10 && document.documentMode < 12)))
  3908. {
  3909. dropElt = this.highlightElement();
  3910. }
  3911. evt.stopPropagation();
  3912. evt.preventDefault();
  3913. }));
  3914. mxEvent.addListener(elts[i], 'drop', mxUtils.bind(this, function(evt)
  3915. {
  3916. if (dropElt != null)
  3917. {
  3918. dropElt.parentNode.removeChild(dropElt);
  3919. dropElt = null;
  3920. }
  3921. if (evt.dataTransfer.files.length > 0)
  3922. {
  3923. this.hideDialog();
  3924. this.openFiles(evt.dataTransfer.files);
  3925. }
  3926. else
  3927. {
  3928. // Handles open special files via text drag and drop
  3929. var data = this.extractGraphModelFromEvent(evt);
  3930. // Tries additional and async parsing of text content such as HTML, Gliffy data
  3931. if (data == null)
  3932. {
  3933. var provider = (evt.dataTransfer != null) ? evt.dataTransfer : evt.clipboardData;
  3934. if (provider != null)
  3935. {
  3936. if (document.documentMode == 10 || document.documentMode == 11)
  3937. {
  3938. data = provider.getData('Text');
  3939. }
  3940. else
  3941. {
  3942. var data = null;
  3943. if (mxUtils.indexOf(provider.types, 'text/uri-list') >= 0)
  3944. {
  3945. var data = evt.dataTransfer.getData('text/uri-list');
  3946. }
  3947. else
  3948. {
  3949. data = (mxUtils.indexOf(provider.types, 'text/html') >= 0) ? provider.getData('text/html') : null;
  3950. }
  3951. if (data != null && data.length > 0)
  3952. {
  3953. var div = document.createElement('div');
  3954. div.innerHTML = data;
  3955. // Extracts single image
  3956. var imgs = div.getElementsByTagName('img');
  3957. if (imgs.length > 0)
  3958. {
  3959. data = imgs[0].getAttribute('src');
  3960. }
  3961. }
  3962. else if (mxUtils.indexOf(provider.types, 'text/plain') >= 0)
  3963. {
  3964. data = provider.getData('text/plain');
  3965. }
  3966. }
  3967. if (data != null)
  3968. {
  3969. // Checks for embedded XML in PNG
  3970. if (data.substring(0, 22) == 'data:image/png;base64,')
  3971. {
  3972. var xml = this.extractGraphModelFromPng(data);
  3973. if (xml != null && xml.length > 0)
  3974. {
  3975. this.openLocalFile(xml);
  3976. }
  3977. }
  3978. if (!this.isOffline() && this.isRemoteFileFormat(data))
  3979. {
  3980. new mxXmlRequest(OPEN_URL, 'format=xml&data=' + encodeURIComponent(data)).send(mxUtils.bind(this, function(req)
  3981. {
  3982. if (req.getStatus() == 200)
  3983. {
  3984. this.openLocalFile(req.getText());
  3985. }
  3986. }));
  3987. }
  3988. else if (/^https?:\/\//.test(data))
  3989. {
  3990. var url = this.getUrl(window.location.pathname + '?url=' + encodeURIComponent(data));
  3991. if (this.getCurrentFile() == null)
  3992. {
  3993. window.location.href = url;
  3994. }
  3995. else
  3996. {
  3997. window.openWindow(url);
  3998. }
  3999. }
  4000. }
  4001. }
  4002. }
  4003. else
  4004. {
  4005. this.openLocalFile(data);
  4006. }
  4007. }
  4008. evt.stopPropagation();
  4009. evt.preventDefault();
  4010. }));
  4011. }
  4012. }
  4013. };
  4014. /**
  4015. * Highlights the given element
  4016. */
  4017. EditorUi.prototype.highlightElement = function(elt)
  4018. {
  4019. var x = 0;
  4020. var y = 0;
  4021. var w = 0;
  4022. var h = 0;
  4023. if (elt == null)
  4024. {
  4025. var b = document.body;
  4026. var d = document.documentElement;
  4027. w = (b.clientWidth || d.clientWidth) - 3;
  4028. h = Math.max(b.clientHeight || 0, d.clientHeight) - 3;
  4029. }
  4030. else
  4031. {
  4032. x = elt.offsetTop;
  4033. y = elt.offsetLeft;
  4034. w = elt.clientWidth;
  4035. h = elt.clientHeight;
  4036. }
  4037. var hl = document.createElement('div');
  4038. hl.style.zIndex = mxPopupMenu.prototype.zIndex + 2;
  4039. hl.style.border = '3px dotted rgb(254, 137, 12)';
  4040. hl.style.pointerEvents = 'none';
  4041. hl.style.position = 'absolute';
  4042. hl.style.top = x + 'px';
  4043. hl.style.left = y + 'px';
  4044. hl.style.width = Math.max(0, w - 3) + 'px';
  4045. hl.style.height = Math.max(0, h - 3) + 'px';
  4046. if (elt != null && elt.parentNode == this.editor.graph.container)
  4047. {
  4048. this.editor.graph.container.appendChild(hl);
  4049. }
  4050. else
  4051. {
  4052. document.body.appendChild(hl);
  4053. }
  4054. return hl;
  4055. };
  4056. /**
  4057. * Highlights the given element
  4058. */
  4059. EditorUi.prototype.stringToCells = function(xml)
  4060. {
  4061. var doc = mxUtils.parseXml(xml);
  4062. var node = this.editor.extractGraphModel(doc.documentElement);
  4063. var cells = [];
  4064. if (node != null)
  4065. {
  4066. var codec = new mxCodec(node.ownerDocument);
  4067. var model = new mxGraphModel();
  4068. codec.decode(node, model);
  4069. var parent = model.getChildAt(model.getRoot(), 0);
  4070. for (var j = 0; j < model.getChildCount(parent); j++)
  4071. {
  4072. cells.push(model.getChildAt(parent, j));
  4073. }
  4074. }
  4075. return cells;
  4076. };
  4077. /**
  4078. * Opens the given files in the editor.
  4079. */
  4080. EditorUi.prototype.openFiles = function(files)
  4081. {
  4082. if (this.spinner.spin(document.body, mxResources.get('loading')))
  4083. {
  4084. for (var i = 0; i < files.length; i++)
  4085. {
  4086. (mxUtils.bind(this, function(file)
  4087. {
  4088. var reader = new FileReader();
  4089. reader.onload = mxUtils.bind(this, function(e)
  4090. {
  4091. var data = e.target.result;
  4092. var name = file.name;
  4093. if (name != null && name.length > 0)
  4094. {
  4095. if (/(\.png)$/i.test(name))
  4096. {
  4097. name = name.substring(0, name.length - 4) + '.xml';
  4098. }
  4099. if (Graph.fileSupport && !this.isOffline() && new XMLHttpRequest().upload &&
  4100. this.isRemoteFileFormat(data, name))
  4101. {
  4102. var dot = name.lastIndexOf('.');
  4103. if (dot >= 0)
  4104. {
  4105. name = name.substring(0, name.lastIndexOf('.')) + '.xml';
  4106. }
  4107. else
  4108. {
  4109. name = name + '.xml';
  4110. }
  4111. this.parseFile(file, mxUtils.bind(this, function(xhr)
  4112. {
  4113. if (xhr.readyState == 4)
  4114. {
  4115. this.spinner.stop();
  4116. if (xhr.status == 200)
  4117. {
  4118. this.openLocalFile(xhr.responseText, name);
  4119. }
  4120. else
  4121. {
  4122. this.handleError({message: mxResources.get((xhr.status == 413) ?
  4123. 'drawingTooLarge' : 'invalidOrMissingFile')},
  4124. mxResources.get('errorLoadingFile'));
  4125. }
  4126. }
  4127. }));
  4128. }
  4129. else if (e.target.result.substring(0, 10) == '<mxlibrary')
  4130. {
  4131. this.spinner.stop();
  4132. try
  4133. {
  4134. this.loadLibrary(new LocalLibrary(this, e.target.result, file.name));
  4135. }
  4136. catch (e)
  4137. {
  4138. this.handleError(e, mxResources.get('errorLoadingFile'));
  4139. }
  4140. }
  4141. else
  4142. {
  4143. if (file.type.substring(0, 9) == 'image/png')
  4144. {
  4145. data = this.extractGraphModelFromPng(data);
  4146. }
  4147. this.spinner.stop();
  4148. this.openLocalFile(data, name);
  4149. }
  4150. }
  4151. });
  4152. reader.onerror = mxUtils.bind(this, function(e)
  4153. {
  4154. this.spinner.stop();
  4155. this.handleError(e);
  4156. window.openFile = null;
  4157. });
  4158. if (file.type.substring(0, 5) === 'image' && file.type.substring(0, 9) !== 'image/svg')
  4159. {
  4160. reader.readAsDataURL(file);
  4161. }
  4162. else
  4163. {
  4164. reader.readAsText(file);
  4165. }
  4166. }))(files[i]);
  4167. }
  4168. }
  4169. };
  4170. /**
  4171. * Shows the layers dialog if the graph has more than one layer.
  4172. */
  4173. EditorUi.prototype.openLocalFile = function(data, name)
  4174. {
  4175. var fn = mxUtils.bind(this, function()
  4176. {
  4177. window.openFile = null;
  4178. if (name == null && this.getCurrentFile() != null && this.isDiagramEmpty())
  4179. {
  4180. var doc = mxUtils.parseXml(data);
  4181. if (doc != null)
  4182. {
  4183. this.editor.setGraphXml(doc.documentElement);
  4184. this.editor.graph.selectAll();
  4185. }
  4186. }
  4187. else
  4188. {
  4189. this.fileLoaded(new LocalFile(this, data, name || this.defaultFilename));
  4190. }
  4191. });
  4192. if (data != null && data.length > 0)
  4193. {
  4194. if (this.getCurrentFile() != null && !this.isDiagramEmpty())
  4195. {
  4196. window.openFile = new OpenFile(function()
  4197. {
  4198. window.openFile = null;
  4199. });
  4200. window.openFile.setData(data, name);
  4201. window.openWindow(this.getUrl(), null, fn);
  4202. }
  4203. else
  4204. {
  4205. fn();
  4206. }
  4207. }
  4208. };
  4209. /**
  4210. * Shows the layers dialog if the graph has more than one layer.
  4211. */
  4212. EditorUi.prototype.initializeEmbedMode = function()
  4213. {
  4214. this.diagramContainer.style.visibility = 'hidden';
  4215. this.formatContainer.style.visibility = 'hidden';
  4216. this.editor.graph.setEnabled(false);
  4217. var parent = window.opener || window.parent;
  4218. if (parent != window)
  4219. {
  4220. if (urlParams['spin'] != '1' || this.spinner.spin(document.body, mxResources.get('loading')))
  4221. {
  4222. this.installMessageHandler(mxUtils.bind(this, function(xml, evt, modified)
  4223. {
  4224. this.spinner.stop();
  4225. this.addEmbedButtons();
  4226. this.diagramContainer.style.visibility = '';
  4227. this.formatContainer.style.visibility = '';
  4228. this.editor.graph.setEnabled(true);
  4229. if (xml != null && xml.length > 0)
  4230. {
  4231. this.setFileData(xml);
  4232. this.showLayersDialog();
  4233. }
  4234. else
  4235. {
  4236. this.editor.graph.model.clear();
  4237. this.editor.fireEvent(new mxEventObject('resetGraphView'));
  4238. }
  4239. this.editor.undoManager.clear();
  4240. this.editor.modified = (modified != null) ? modified : false;
  4241. this.updateUi();
  4242. if (this.format != null)
  4243. {
  4244. this.format.refresh();
  4245. }
  4246. }));
  4247. }
  4248. }
  4249. };
  4250. /**
  4251. * Shows the layers dialog if the graph has more than one layer.
  4252. */
  4253. EditorUi.prototype.showLayersDialog = function()
  4254. {
  4255. if (this.editor.graph.getModel().getChildCount(this.editor.graph.getModel().getRoot()) > 1)
  4256. {
  4257. if (this.actions.layersWindow == null)
  4258. {
  4259. this.actions.get('layers').funct();
  4260. }
  4261. else
  4262. {
  4263. this.actions.layersWindow.window.setVisible(true);
  4264. }
  4265. }
  4266. };
  4267. /**
  4268. * Adds the buttons for embedded mode.
  4269. */
  4270. EditorUi.prototype.createLoadMessage = function(eventName)
  4271. {
  4272. var graph = this.editor.graph;
  4273. return {event: eventName, pageVisible: graph.pageVisible, translate: graph.view.translate,
  4274. scale: graph.view.scale, page: graph.view.getBackgroundPageBounds(), bounds: graph.getGraphBounds()};
  4275. };
  4276. /**
  4277. * Adds the buttons for embedded mode.
  4278. */
  4279. EditorUi.prototype.installMessageHandler = function(fn)
  4280. {
  4281. var autosave = false;
  4282. var updateStatus = mxUtils.bind(this, function(sender, eventObject)
  4283. {
  4284. if (urlParams['modified'] != null)
  4285. {
  4286. if (urlParams['modified'] == '0')
  4287. {
  4288. this.editor.setStatus('');
  4289. }
  4290. else
  4291. {
  4292. this.editor.setStatus(mxResources.get(urlParams['modified']));
  4293. }
  4294. }
  4295. });
  4296. this.editor.graph.model.addListener(mxEvent.CHANGE, updateStatus);
  4297. // Receives XML message from opener and puts it into the graph
  4298. mxEvent.addListener(window, 'message', mxUtils.bind(this, function(evt)
  4299. {
  4300. var data = evt.data;
  4301. if (urlParams['proto'] == 'json')
  4302. {
  4303. data = JSON.parse(data);
  4304. if (data.action == 'dialog')
  4305. {
  4306. this.showError((data.titleKey != null) ? mxResources.get(data.titleKey) : data.title,
  4307. (data.messageKey != null) ? mxResources.get(data.messageKey) : data.message,
  4308. (data.buttonKey != null) ? mxResources.get(data.buttonKey) : data.button);
  4309. if (data.modified != null)
  4310. {
  4311. this.editor.modified = data.modified;
  4312. }
  4313. return;
  4314. }
  4315. else if (data.action == 'template')
  4316. {
  4317. this.spinner.stop();
  4318. var dlg = new NewDialog(this, false, false, mxUtils.bind(this, function(xml)
  4319. {
  4320. // LATER: Add autosave option in template message
  4321. fn(xml || '', evt, xml != null);
  4322. }));
  4323. this.showDialog(dlg.container, 620, 440, true, true, mxUtils.bind(this, function(cancel)
  4324. {
  4325. if (cancel)
  4326. {
  4327. this.actions.get('exit').funct();
  4328. }
  4329. }));
  4330. dlg.init();
  4331. return;
  4332. }
  4333. else if (data.action == 'status')
  4334. {
  4335. if (data.messageKey != null)
  4336. {
  4337. this.editor.setStatus(mxResources.get(data.messageKey));
  4338. }
  4339. else if (data.message != null)
  4340. {
  4341. this.editor.setStatus(data.message);
  4342. }
  4343. if (data.modified != null)
  4344. {
  4345. this.editor.modified = data.modified;
  4346. }
  4347. return;
  4348. }
  4349. else if (data.action == 'spinner')
  4350. {
  4351. var msg = (data.messageKey != null) ? mxResources.get(data.messageKey) : data.message;
  4352. if (data.show != null && !data.show)
  4353. {
  4354. this.spinner.stop();
  4355. }
  4356. else
  4357. {
  4358. this.spinner.spin(document.body, msg)
  4359. }
  4360. return;
  4361. }
  4362. else if (data.action == 'export')
  4363. {
  4364. if (data.format == 'png' || data.format == 'xmlpng')
  4365. {
  4366. if ((data.spin == null && data.spinKey == null) || this.spinner.spin(document.body,
  4367. (data.spinKey != null) ? mxResources.get(data.spinKey) : data.spin))
  4368. {
  4369. var xml = (data.xml != null) ? data.xml : this.getFileData(true);
  4370. this.editor.graph.setEnabled(false);
  4371. var postDataBack = mxUtils.bind(this, function(bin)
  4372. {
  4373. var msg = this.createLoadMessage('export');
  4374. msg.format = data.format;
  4375. msg.xml = encodeURIComponent(xml);
  4376. msg.data = 'data:image/png;base64,' + bin;
  4377. parent.postMessage(JSON.stringify(msg), '*');
  4378. this.editor.graph.setEnabled(true);
  4379. });
  4380. if (this.isExportToCanvas())
  4381. {
  4382. var graph = this.editor.graph;
  4383. // Exports PNG for first page while other page is visible by creating a graph
  4384. // LATER: Add caching for the graph or SVG while not on first page
  4385. if (this.pages != null && this.currentPage != this.pages[0])
  4386. {
  4387. graph = this.createTemporaryGraph(graph.getStylesheet());
  4388. var graphGetGlobalVariable = graph.getGlobalVariable;
  4389. var page = this.pages[0];
  4390. graph.getGlobalVariable = function(name)
  4391. {
  4392. if (name == 'page')
  4393. {
  4394. return page.getName();
  4395. }
  4396. else if (name == 'pagenumber')
  4397. {
  4398. return 1;
  4399. }
  4400. return graphGetGlobalVariable.apply(this, arguments);
  4401. };
  4402. document.body.appendChild(graph.container);
  4403. graph.model.setRoot(page.root);
  4404. }
  4405. this.exportToCanvas(mxUtils.bind(this, function(canvas)
  4406. {
  4407. var uri = canvas.toDataURL('image/png');
  4408. if (data.format == 'xmlpng')
  4409. {
  4410. uri = this.writeGraphModelToPng(uri, 'zTXt', 'mxGraphModel',
  4411. atob(this.editor.graph.compress(xml)));
  4412. }
  4413. // Removes temporary graph from DOM
  4414. if (graph != this.editor.graph)
  4415. {
  4416. graph.container.parentNode.removeChild(graph.container);
  4417. }
  4418. postDataBack(uri.substring(uri.lastIndexOf(',') + 1));
  4419. }), null, null, null, null, null, null, null, null, null, null, graph);
  4420. }
  4421. else
  4422. {
  4423. // Data from server is base64 encoded to avoid binary XHR
  4424. // Double encoding for XML arg is needed for UTF8 encoding
  4425. var req = new mxXmlRequest(EXPORT_URL, 'format=png&embedXml=' +
  4426. ((data.format == 'xmlpng') ? '1' : '0') + '&base64=1&xml=' +
  4427. encodeURIComponent(encodeURIComponent(xml)));
  4428. req.send(mxUtils.bind(this, function(req)
  4429. {
  4430. this.editor.graph.setEnabled(true);
  4431. this.spinner.stop();
  4432. if (req.getStatus() == 200)
  4433. {
  4434. postDataBack(req.getText());
  4435. }
  4436. }), mxUtils.bind(this, function()
  4437. {
  4438. this.spinner.stop();
  4439. }));
  4440. }
  4441. }
  4442. }
  4443. else
  4444. {
  4445. // SVG is generated from graph so parse optional XML
  4446. if (data.xml != null && data.xml.length > 0)
  4447. {
  4448. this.setFileData(data.xml);
  4449. }
  4450. var msg = this.createLoadMessage('export');
  4451. // Forces new HTML format if pages exists
  4452. if (data.format == 'html2' || (data.format == 'html' && (urlParams['pages'] == '1' ||
  4453. (this.pages != null && this.pages.length > 1))))
  4454. {
  4455. var node = this.getXmlFileData();
  4456. msg.xml = mxUtils.getXml(node);
  4457. msg.data = this.getFileData(null, null, true, null, null, null, node);
  4458. msg.format = data.format;
  4459. }
  4460. else if (data.format == 'html')
  4461. {
  4462. var xml = this.editor.getGraphXml();
  4463. msg.data = this.getHtml(xml, this.editor.graph);
  4464. msg.xml = mxUtils.getXml(xml);
  4465. msg.format = data.format;
  4466. }
  4467. else
  4468. {
  4469. // Creates a preview with no alt text for unsupported browsers
  4470. mxSvgCanvas2D.prototype.foAltText = null;
  4471. var bg = this.editor.graph.background;
  4472. if (bg == mxConstants.NONE)
  4473. {
  4474. bg = null;
  4475. }
  4476. msg.xml = this.getFileData(true);
  4477. msg.format = 'svg';
  4478. if (data.embedImages || data.embedImages == null)
  4479. {
  4480. if ((data.spin == null && data.spinKey == null) || this.spinner.spin(document.body,
  4481. (data.spinKey != null) ? mxResources.get(data.spinKey) : data.spin))
  4482. {
  4483. this.editor.graph.setEnabled(false);
  4484. if (data.format == 'xmlsvg')
  4485. {
  4486. this.getEmbeddedSvg(msg.xml, this.editor.graph, null, true, mxUtils.bind(this, function(svg)
  4487. {
  4488. this.editor.graph.setEnabled(true);
  4489. this.spinner.stop();
  4490. msg.data = this.createSvgDataUri(svg);
  4491. parent.postMessage(JSON.stringify(msg), '*');
  4492. }));
  4493. }
  4494. else
  4495. {
  4496. this.convertImages(this.editor.graph.getSvg(bg), mxUtils.bind(this, function(svgRoot)
  4497. {
  4498. this.editor.graph.setEnabled(true);
  4499. this.spinner.stop();
  4500. msg.data = this.createSvgDataUri(mxUtils.getXml(svgRoot));
  4501. parent.postMessage(JSON.stringify(msg), '*');
  4502. }));
  4503. }
  4504. }
  4505. return;
  4506. }
  4507. else
  4508. {
  4509. var svg = (data.format == 'xmlsvg') ? this.getEmbeddedSvg(this.getFileData(true),
  4510. this.editor.graph, null, true) : mxUtils.getXml(this.editor.graph.getSvg(bg));
  4511. msg.data = this.createSvgDataUri(svg);
  4512. }
  4513. }
  4514. parent.postMessage(JSON.stringify(msg), '*');
  4515. }
  4516. return;
  4517. }
  4518. else if (data.action == 'load')
  4519. {
  4520. autosave = data.autosave == 1;
  4521. if (data.modified != null && urlParams['modified'] == null)
  4522. {
  4523. urlParams['modified'] = data.modified;
  4524. }
  4525. if (data.saveAndExit != null && urlParams['saveAndExit'] == null)
  4526. {
  4527. urlParams['saveAndExit'] = data.saveAndExit;
  4528. }
  4529. if (data.title != null && this.buttonContainer != null)
  4530. {
  4531. var tmp = document.createElement('span');
  4532. mxUtils.write(tmp, data.title);
  4533. if (uiTheme == 'atlas')
  4534. {
  4535. this.buttonContainer.style.paddingRight = '12px';
  4536. this.buttonContainer.style.paddingTop = '12px';
  4537. }
  4538. else
  4539. {
  4540. this.buttonContainer.style.paddingRight = '38px';
  4541. this.buttonContainer.style.paddingTop = '6px';
  4542. }
  4543. this.buttonContainer.appendChild(tmp);
  4544. }
  4545. if (data.xmlpng != null)
  4546. {
  4547. data = this.extractGraphModelFromPng(data.xmlpng);
  4548. }
  4549. else
  4550. {
  4551. data = data.xml;
  4552. }
  4553. }
  4554. else
  4555. {
  4556. // Unknown message
  4557. data = null;
  4558. }
  4559. }
  4560. if (data != null && typeof data.charAt === 'function' && data.charAt(0) != '<')
  4561. {
  4562. try
  4563. {
  4564. if (data.substring(0, 26) == 'data:image/svg+xml;base64,')
  4565. {
  4566. data = atob(data.substring(26));
  4567. }
  4568. else if (data.substring(0, 24) == 'data:image/svg+xml;utf8,')
  4569. {
  4570. data = data.substring(24);
  4571. }
  4572. if (data != null)
  4573. {
  4574. if (data.charAt(0) == '%')
  4575. {
  4576. data = decodeURIComponent(data);
  4577. }
  4578. else if (data.charAt(0) != '<')
  4579. {
  4580. data = this.editor.graph.decompress(data);
  4581. }
  4582. }
  4583. }
  4584. catch (e)
  4585. {
  4586. // ignore compression errors and use empty data
  4587. }
  4588. }
  4589. fn(data, evt);
  4590. if (urlParams['modified'] != null)
  4591. {
  4592. this.editor.setStatus('');
  4593. }
  4594. if (autosave)
  4595. {
  4596. var changeListener = mxUtils.bind(this, function(sender, eventObject)
  4597. {
  4598. var data = (urlParams['pages'] == '1' || (this.pages != null && this.pages.length > 1)) ?
  4599. this.getFileData(true): mxUtils.getXml(this.editor.getGraphXml());
  4600. var msg = this.createLoadMessage('autosave');
  4601. msg.xml = data;
  4602. data = JSON.stringify(msg);
  4603. var parent = window.opener || window.parent;
  4604. parent.postMessage(data, '*');
  4605. });
  4606. this.editor.graph.model.addListener(mxEvent.CHANGE, changeListener);
  4607. // Some options trigger autosave
  4608. this.addListener('pageFormatChanged', changeListener);
  4609. this.addListener('backgroundColorChanged', changeListener);
  4610. this.addListener('backgroundImageChanged', changeListener);
  4611. this.addListener('foldingEnabledChanged', changeListener);
  4612. this.addListener('mathEnabledChanged', changeListener);
  4613. this.addListener('gridEnabledChanged', changeListener);
  4614. this.addListener('guidesEnabledChanged', changeListener);
  4615. this.addListener('pageViewChanged', changeListener);
  4616. }
  4617. // Sends the bounds of the graph to the host after parsing
  4618. if (urlParams['returnbounds'] == '1' || urlParams['proto'] == 'json')
  4619. {
  4620. parent.postMessage(JSON.stringify(this.createLoadMessage('load')), '*');
  4621. }
  4622. }));
  4623. // Requests data from the sender. This is a workaround for not allowing
  4624. // the opener to listen for the onload event if not in the same origin.
  4625. var parent = window.opener || window.parent;
  4626. var msg = (urlParams['proto'] == 'json') ? JSON.stringify({event: 'init'}) : (urlParams['ready'] || 'ready');
  4627. parent.postMessage(msg, '*');
  4628. };
  4629. /**
  4630. * Adds the buttons for embedded mode.
  4631. */
  4632. EditorUi.prototype.addEmbedButtons = function()
  4633. {
  4634. if (this.menubar != null)
  4635. {
  4636. var div = document.createElement('div');
  4637. div.style.display = 'inline-block';
  4638. div.style.position = 'absolute';
  4639. div.style.paddingTop = (uiTheme == 'atlas') ? '2px' : '3px';
  4640. div.style.paddingLeft = '8px';
  4641. div.style.paddingBottom = '2px';
  4642. var button = document.createElement('button');
  4643. mxUtils.write(button, mxResources.get('save'));
  4644. button.className = 'geBigButton';
  4645. button.style.fontSize = '12px';
  4646. button.style.padding = '4px 6px 4px 6px';
  4647. button.style.borderRadius = '3px';
  4648. mxEvent.addListener(button, 'click', mxUtils.bind(this, function()
  4649. {
  4650. this.actions.get('save').funct();
  4651. }));
  4652. div.appendChild(button);
  4653. if (urlParams['saveAndExit'] == '1')
  4654. {
  4655. button = document.createElement('a');
  4656. mxUtils.write(button, mxResources.get('saveAndExit'));
  4657. button.style.fontSize = '12px';
  4658. button.style.marginLeft = '6px';
  4659. button.style.padding = '4px';
  4660. button.style.cursor = 'pointer';
  4661. mxEvent.addListener(button, 'click', mxUtils.bind(this, function()
  4662. {
  4663. this.actions.get('saveAndExit').funct();
  4664. }));
  4665. div.appendChild(button);
  4666. }
  4667. button = document.createElement('a');
  4668. mxUtils.write(button, mxResources.get('exit'));
  4669. button.style.fontSize = '12px';
  4670. button.style.marginLeft = '6px';
  4671. button.style.marginRight = '20px';
  4672. button.style.padding = '4px';
  4673. button.style.cursor = 'pointer';
  4674. mxEvent.addListener(button, 'click', mxUtils.bind(this, function()
  4675. {
  4676. this.actions.get('exit').funct();
  4677. }));
  4678. div.appendChild(button);
  4679. this.toolbar.container.appendChild(div);
  4680. this.toolbar.staticElements.push(div);
  4681. div.style.right = (uiTheme != 'atlas') ? '52px' : '42px';
  4682. }
  4683. };
  4684. /**
  4685. * Translates this point by the given vector.
  4686. *
  4687. * @param {number} dx X-coordinate of the translation.
  4688. * @param {number} dy Y-coordinate of the translation.
  4689. */
  4690. EditorUi.prototype.getSearch = function(exclude)
  4691. {
  4692. var result = '';
  4693. if (urlParams['offline'] != '1' && urlParams['demo'] != '1' && exclude != null && window.location.search.length > 0)
  4694. {
  4695. var amp = '?';
  4696. for (var key in urlParams)
  4697. {
  4698. if (mxUtils.indexOf(exclude, key) < 0 && urlParams[key] != null)
  4699. {
  4700. result += amp + key + '=' + urlParams[key];
  4701. amp = '&';
  4702. }
  4703. }
  4704. }
  4705. else
  4706. {
  4707. result = window.location.search;
  4708. }
  4709. return result;
  4710. };
  4711. /**
  4712. * Returns the URL for a copy of this editor with no state.
  4713. */
  4714. EditorUi.prototype.getUrl = function(pathname)
  4715. {
  4716. var href = (pathname != null) ? pathname : window.location.pathname;
  4717. var parms = (href.indexOf('?') > 0) ? 1 : 0;
  4718. if (urlParams['offline'] == '1')
  4719. {
  4720. href += window.location.search;
  4721. }
  4722. else
  4723. {
  4724. var ignored = ['tmp', 'libs', 'clibs', 'state', 'fileId', 'code', 'share', 'notitle',
  4725. 'url', 'embed', 'client', 'create', 'title', 'splash'];
  4726. // Removes template URL parameter for new blank diagram
  4727. for (var key in urlParams)
  4728. {
  4729. if (mxUtils.indexOf(ignored, key) < 0)
  4730. {
  4731. if (parms == 0)
  4732. {
  4733. href += '?';
  4734. }
  4735. else
  4736. {
  4737. href += '&';
  4738. }
  4739. if (urlParams[key] != null)
  4740. {
  4741. href += key + '=' + urlParams[key];
  4742. parms++;
  4743. }
  4744. }
  4745. }
  4746. }
  4747. return href;
  4748. };
  4749. /**
  4750. * Overrides createOutline
  4751. */
  4752. var editorUiCreateOutline = EditorUi.prototype.createOutline;
  4753. EditorUi.prototype.createOutline = function(wnd)
  4754. {
  4755. var outline = editorUiCreateOutline.apply(this, arguments);
  4756. var graph = this.editor.graph;
  4757. var outlineGetSourceGraphBounds = outline.getSourceGraphBounds;
  4758. outline.getSourceGraphBounds = function()
  4759. {
  4760. if (mxUtils.hasScrollbars(graph.container) && graph.pageVisible && this.source.minimumGraphSize != null)
  4761. {
  4762. var pb = this.source.getPagePadding();
  4763. var s = this.source.view.scale;
  4764. var result = new mxRectangle(0, 0, Math.ceil(this.source.minimumGraphSize.width - 2 * pb.x / s),
  4765. Math.ceil(this.source.minimumGraphSize.height - 2 * pb.y / s));
  4766. return result;
  4767. }
  4768. return outlineGetSourceGraphBounds.apply(this, arguments);
  4769. };
  4770. var outlineGetSourceContainerSize = outline.getSourceContainerSize;
  4771. outline.getSourceContainerSize = function()
  4772. {
  4773. if (mxUtils.hasScrollbars(graph.container) && this.source.minimumGraphSize != null)
  4774. {
  4775. var pad = this.source.getPagePadding();
  4776. var s = this.source.view.scale;
  4777. return new mxRectangle(0, 0, Math.ceil(this.source.minimumGraphSize.width * s - 2 * pad.x),
  4778. Math.ceil(this.source.minimumGraphSize.height * s - 2 * pad.y));
  4779. }
  4780. return outlineGetSourceContainerSize.apply(this, arguments);
  4781. };
  4782. outline.getOutlineOffset = function(scale)
  4783. {
  4784. if (mxUtils.hasScrollbars(graph.container) && this.source.minimumGraphSize != null)
  4785. {
  4786. var pb = this.source.getPagePadding();
  4787. var dx = Math.max(0, (outline.outline.container.clientWidth / scale - (this.source.minimumGraphSize.width - 2 * pb.x)) / 2);
  4788. var dy = Math.max(0, (outline.outline.container.clientHeight / scale - (this.source.minimumGraphSize.height - 2 * pb.y)) / 2);
  4789. // Why is vertical offset negative relative to dy
  4790. return new mxPoint(Math.round(dx - pb.x), Math.round(dy - pb.y - 5 / scale));
  4791. }
  4792. return new mxPoint(8 / scale, 8 / scale);
  4793. };
  4794. var outlineInit = outline.init;
  4795. outline.init = function()
  4796. {
  4797. outlineInit.apply(this, arguments);
  4798. // Problem: Need to override a function in the view but the view is created
  4799. // with the graph so a refresh of the page is needed to see this change.
  4800. outline.outline.view.getBackgroundPageBounds = function()
  4801. {
  4802. var layout = graph.getPageLayout();
  4803. var page = graph.getPageSize();
  4804. return new mxRectangle(this.scale * (this.translate.x + layout.x * page.width),
  4805. this.scale * (this.translate.y + layout.y * page.height),
  4806. this.scale * layout.width * page.width,
  4807. this.scale * layout.height * page.height);
  4808. };
  4809. outline.outline.view.validateBackgroundPage();
  4810. };
  4811. this.editor.addListener('pageSelected', function(sender, evt)
  4812. {
  4813. var change = evt.getProperty('change');
  4814. var graph = outline.source;
  4815. var g = outline.outline;
  4816. g.pageScale = graph.pageScale;
  4817. g.pageFormat = graph.pageFormat;
  4818. g.background = graph.background;
  4819. g.pageVisible = graph.pageVisible;
  4820. g.background = graph.background;
  4821. var current = mxUtils.getCurrentStyle(graph.container);
  4822. g.container.style.backgroundColor = current.backgroundColor;
  4823. if (graph.view.backgroundPageShape != null && g.view.backgroundPageShape != null)
  4824. {
  4825. g.view.backgroundPageShape.fill = graph.view.backgroundPageShape.fill;
  4826. }
  4827. outline.outline.view.clear(change.previousPage.root, true);
  4828. outline.outline.view.validate();
  4829. });
  4830. return outline;
  4831. };
  4832. /**
  4833. * Updates action states depending on the selection.
  4834. */
  4835. var editorUiUpdateActionStates = EditorUi.prototype.updateActionStates;
  4836. EditorUi.prototype.updateActionStates = function()
  4837. {
  4838. editorUiUpdateActionStates.apply(this, arguments);
  4839. var graph = this.editor.graph;
  4840. var file = this.getCurrentFile();
  4841. var active = (file != null && file.isEditable()) || urlParams['embed'] == '1';
  4842. this.actions.get('pageSetup').setEnabled(active);
  4843. this.actions.get('autosave').setEnabled(file != null && file.isEditable() && file.isAutosaveOptional());
  4844. this.actions.get('guides').setEnabled(active);
  4845. this.actions.get('shadowVisible').setEnabled(active);
  4846. this.actions.get('connectionArrows').setEnabled(active);
  4847. this.actions.get('connectionPoints').setEnabled(active);
  4848. this.actions.get('copyStyle').setEnabled(active && !graph.isSelectionEmpty());
  4849. this.actions.get('pasteStyle').setEnabled(active && !graph.isSelectionEmpty());
  4850. this.actions.get('editGeometry').setEnabled(graph.getModel().isVertex(graph.getSelectionCell()));
  4851. this.actions.get('createShape').setEnabled(active);
  4852. this.actions.get('createRevision').setEnabled(active);
  4853. this.actions.get('moveToFolder').setEnabled(file != null);
  4854. this.actions.get('makeCopy').setEnabled(file != null && !file.isRestricted());
  4855. this.actions.get('editDiagram').setEnabled(urlParams['embed'] == '1' ||
  4856. (file != null && !file.isRestricted()));
  4857. this.actions.get('imgur').setEnabled(file != null && !file.isRestricted());
  4858. this.actions.get('twitter').setEnabled(file != null && !file.isRestricted());
  4859. this.actions.get('facebook').setEnabled(file != null && !file.isRestricted());
  4860. this.actions.get('github').setEnabled(file != null && !file.isRestricted());
  4861. this.actions.get('publishLink').setEnabled(file != null && !file.isRestricted());
  4862. this.menus.get('publish').setEnabled(file != null && !file.isRestricted());
  4863. var state = graph.view.getState(graph.getSelectionCell());
  4864. this.actions.get('editShape').setEnabled(active && state != null && state.shape != null && state.shape.stencil != null);
  4865. };
  4866. /**
  4867. * Changes the default stylename so that it matches the old named style
  4868. * if one was specified in the XML.
  4869. */
  4870. Graph.prototype.defaultThemeName = 'default-style2';
  4871. /**
  4872. * Contains the last XML that was pasted.
  4873. */
  4874. Graph.prototype.lastPasteXml = null;
  4875. /**
  4876. * Contains the number of times the last XML was pasted.
  4877. */
  4878. Graph.prototype.pasteCounter = 0;
  4879. // Experimental edge mode
  4880. Graph.prototype.edgeMode = urlParams['edge'] != 'move';
  4881. /**
  4882. * Sets default style (used in editor.get/setGraphXml below)
  4883. */
  4884. var graphLoadStylesheet = Graph.prototype.loadStylesheet;
  4885. Graph.prototype.loadStylesheet = function()
  4886. {
  4887. graphLoadStylesheet.apply(this, arguments);
  4888. this.currentStyle = 'default-style2';
  4889. };
  4890. /**
  4891. * Graph Overrides
  4892. */
  4893. Graph.prototype.defaultScrollbars = urlParams['sb'] != '0';
  4894. /**
  4895. * Specifies if the page should be visible for new files. Default is true.
  4896. */
  4897. Graph.prototype.defaultPageVisible = urlParams['pv'] != '0';
  4898. /**
  4899. * Specifies if the page should be visible for new files. Default is true.
  4900. */
  4901. Graph.prototype.shadowId = 'dropShadow';
  4902. /**
  4903. * Loads the stylesheet for this graph.
  4904. */
  4905. Graph.prototype.setShadowVisible = function(value, fireEvent)
  4906. {
  4907. if (mxClient.IS_SVG)
  4908. {
  4909. fireEvent = (fireEvent != null) ? fireEvent : true;
  4910. this.shadowVisible = value;
  4911. if (this.shadowVisible)
  4912. {
  4913. this.view.getDrawPane().setAttribute('filter', 'url(#' + this.shadowId + ')');
  4914. }
  4915. else
  4916. {
  4917. this.view.getDrawPane().removeAttribute('filter');
  4918. }
  4919. if (fireEvent)
  4920. {
  4921. this.fireEvent(new mxEventObject('shadowVisibleChanged'));
  4922. }
  4923. }
  4924. };
  4925. /**
  4926. * Adds rack child layout style.
  4927. */
  4928. var graphInit = Graph.prototype.init;
  4929. Graph.prototype.init = function()
  4930. {
  4931. graphInit.apply(this, arguments);
  4932. // Override insert location for current mouse point
  4933. var mouseEvent = null;
  4934. function setMouseEvent(evt)
  4935. {
  4936. mouseEvent = evt;
  4937. // Workaround for member not found in IE8-
  4938. if (mxClient.IS_QUIRKS || document.documentMode == 7 || document.documentMode == 8)
  4939. {
  4940. mouseEvent = mxUtils.clone(evt);
  4941. }
  4942. };
  4943. mxEvent.addListener(this.container, 'mouseenter', setMouseEvent);
  4944. mxEvent.addListener(this.container, 'mousemove', setMouseEvent);
  4945. mxEvent.addListener(this.container, 'mouseleave', function(evt)
  4946. {
  4947. mouseEvent = null;
  4948. });
  4949. // Extends getInsertPoint to use the current mouse location
  4950. this.isMouseInsertPoint = function()
  4951. {
  4952. return mouseEvent != null;
  4953. };
  4954. var getInsertPoint = this.getInsertPoint;
  4955. this.getInsertPoint = function()
  4956. {
  4957. if (mouseEvent != null)
  4958. {
  4959. return this.getPointForEvent(mouseEvent);
  4960. }
  4961. return getInsertPoint.apply(this, arguments);
  4962. };
  4963. var layoutManagerGetLayout = this.layoutManager.getLayout;
  4964. this.layoutManager.getLayout = function(cell)
  4965. {
  4966. var state = this.graph.view.getState(cell);
  4967. var style = (state != null) ? state.style : this.graph.getCellStyle(cell);
  4968. // mxRackContainer may be undefined as it is dynamically loaded at render time
  4969. if (typeof(mxRackContainer) != 'undefined' && style['childLayout'] == 'rack')
  4970. {
  4971. var rackLayout = new mxStackLayout(this.graph, false);
  4972. rackLayout.setChildGeometry = function(child, geo)
  4973. {
  4974. var unitSize = 20;
  4975. geo.height = Math.max(geo.height, unitSize);
  4976. if (geo.height / unitSize > 1)
  4977. {
  4978. var mod = geo.height % unitSize;
  4979. geo.height += mod > unitSize / 2 ? (unitSize - mod) : -mod;
  4980. }
  4981. this.graph.getModel().setGeometry(child, geo);
  4982. };
  4983. rackLayout.fill = true;
  4984. rackLayout.unitSize = mxRackContainer.unitSize | 20;
  4985. rackLayout.marginLeft = style['marginLeft'] || 0;
  4986. rackLayout.marginRight = style['marginRight'] || 0;
  4987. rackLayout.marginTop = style['marginTop'] || 0;
  4988. rackLayout.marginBottom = style['marginBottom'] || 0;
  4989. rackLayout.resizeParent = false;
  4990. return rackLayout;
  4991. }
  4992. return layoutManagerGetLayout.apply(this, arguments);
  4993. }
  4994. };
  4995. /**
  4996. * Specifies special libraries that are loaded via dynamic JS.
  4997. *
  4998. *************************************************************
  4999. * IMPORTANT: Add all special cases in EmbedServlet.java and *
  5000. * jgraphcms/js/Graph.js lines 102 ff. *
  5001. *************************************************************
  5002. */
  5003. mxStencilRegistry.libraries['arrows2'] = [SHAPES_PATH + '/mxArrows.js'];
  5004. mxStencilRegistry.libraries['bpmn'] = [SHAPES_PATH + '/bpmn/mxBpmnShape2.js', STENCIL_PATH + '/bpmn.xml'];
  5005. mxStencilRegistry.libraries['er'] = [SHAPES_PATH + '/er/mxER.js'];
  5006. mxStencilRegistry.libraries['ios'] = [SHAPES_PATH + '/mockup/mxMockupiOS.js'];
  5007. mxStencilRegistry.libraries['rackGeneral'] = [SHAPES_PATH + '/rack/mxRack.js', STENCIL_PATH + '/rack/general.xml'];
  5008. mxStencilRegistry.libraries['rackF5'] = [STENCIL_PATH + '/rack/f5.xml'];
  5009. mxStencilRegistry.libraries['lean_mapping'] = [SHAPES_PATH + '/mxLeanMap.js', STENCIL_PATH + '/lean_mapping.xml'];
  5010. mxStencilRegistry.libraries['basic'] = [SHAPES_PATH + '/mxBasic.js', STENCIL_PATH + '/basic.xml'];
  5011. mxStencilRegistry.libraries['ios7icons'] = [STENCIL_PATH + '/ios7/icons.xml'];
  5012. mxStencilRegistry.libraries['ios7ui'] = [SHAPES_PATH + '/ios7/mxIOS7Ui.js', STENCIL_PATH + '/ios7/misc.xml'];
  5013. mxStencilRegistry.libraries['android'] = [SHAPES_PATH + '/mxAndroid.js', STENCIL_PATH + '/android/android.xml'];
  5014. mxStencilRegistry.libraries['eeLogicGates'] = [STENCIL_PATH + '/electrical/logic_gates.xml'];
  5015. mxStencilRegistry.libraries['eeResistors'] = [STENCIL_PATH + '/electrical/resistors.xml'];
  5016. mxStencilRegistry.libraries['eeCapacitors'] = [STENCIL_PATH + '/electrical/capacitors.xml'];
  5017. mxStencilRegistry.libraries['eeInductors'] = [STENCIL_PATH + '/electrical/inductors.xml'];
  5018. mxStencilRegistry.libraries['eeSwitchesRelays'] = [STENCIL_PATH + '/electrical/switchesRelays.xml', STENCIL_PATH + '/electrical/electro-mechanical.xml'];
  5019. mxStencilRegistry.libraries['eeDiodes'] = [STENCIL_PATH + '/electrical/diodes.xml'];
  5020. mxStencilRegistry.libraries['eeSources'] = [STENCIL_PATH + '/electrical/signal_sources.xml'];
  5021. mxStencilRegistry.libraries['eeTransistors'] = [STENCIL_PATH + '/electrical/mosfets1.xml', STENCIL_PATH + '/electrical/mosfets2.xml', STENCIL_PATH + '/electrical/transistors.xml'];
  5022. mxStencilRegistry.libraries['eeMisc'] = [STENCIL_PATH + '/electrical/electro-mechanical.xml', STENCIL_PATH + '/electrical/miscellaneous.xml'];
  5023. mxStencilRegistry.libraries['eeAudio'] = [STENCIL_PATH + '/electrical/radio.xml'];
  5024. mxStencilRegistry.libraries['eePlcLadder'] = [STENCIL_PATH + '/electrical/plc_ladder.xml'];
  5025. mxStencilRegistry.libraries['eeAbstract'] = [STENCIL_PATH + '/electrical/abstract.xml', STENCIL_PATH + '/electrical/logic_gates.xml'];
  5026. mxStencilRegistry.libraries['eeOptical'] = [STENCIL_PATH + '/electrical/opto_electronics.xml'];
  5027. mxStencilRegistry.libraries['eeVacuumTubes'] = [STENCIL_PATH + '/electrical/vacuum_tubes.xml'];
  5028. mxStencilRegistry.libraries['eeWaveforms'] = [STENCIL_PATH + '/electrical/waveforms.xml'];
  5029. mxStencilRegistry.libraries['eeInstruments'] = [STENCIL_PATH + '/electrical/instruments.xml'];
  5030. mxStencilRegistry.libraries['mscae/cloud'] = [STENCIL_PATH + '/mscae/cloud.xml'];
  5031. mxStencilRegistry.libraries['mockup/buttons'] = [SHAPES_PATH + '/mockup/mxMockupButtons.js'];
  5032. mxStencilRegistry.libraries['mockup/containers'] = [SHAPES_PATH + '/mockup/mxMockupContainers.js'];
  5033. mxStencilRegistry.libraries['mockup/forms'] = [SHAPES_PATH + '/mockup/mxMockupForms.js'];
  5034. mxStencilRegistry.libraries['mockup/graphics'] = [SHAPES_PATH + '/mockup/mxMockupGraphics.js', STENCIL_PATH + '/mockup/misc.xml'];
  5035. mxStencilRegistry.libraries['mockup/markup'] = [SHAPES_PATH + '/mockup/mxMockupMarkup.js'];
  5036. mxStencilRegistry.libraries['mockup/misc'] = [SHAPES_PATH + '/mockup/mxMockupMisc.js', STENCIL_PATH + '/mockup/misc.xml'];
  5037. mxStencilRegistry.libraries['mockup/navigation'] = [SHAPES_PATH + '/mockup/mxMockupNavigation.js', STENCIL_PATH + '/mockup/misc.xml'];
  5038. mxStencilRegistry.libraries['mockup/text'] = [SHAPES_PATH + '/mockup/mxMockupText.js'];
  5039. // Required to avoid 404 for mockup.xml since naming of mxgraph.mockup.anchor does not contain
  5040. // buttons even though it is defined in the mxMockupButtons.js file. This could only be fixed
  5041. // with aliases for existing shapes or aliases for basenames, but this is essentially the same.
  5042. mxStencilRegistry.libraries['mockup'] = [SHAPES_PATH + '/mockup/mxMockupButtons.js'];
  5043. mxStencilRegistry.libraries['pid2inst'] = [SHAPES_PATH + '/pid2/mxPidInstruments.js'];
  5044. mxStencilRegistry.libraries['pid2misc'] = [SHAPES_PATH + '/pid2/mxPidMisc.js', STENCIL_PATH + '/pid/misc.xml'];
  5045. mxStencilRegistry.libraries['pid2valves'] = [SHAPES_PATH + '/pid2/mxPidValves.js'];
  5046. mxStencilRegistry.libraries['pidFlowSensors'] = [STENCIL_PATH + '/pid/flow_sensors.xml'];
  5047. mxStencilRegistry.libraries['floorplan'] = [SHAPES_PATH + '/mxFloorplan.js', STENCIL_PATH + '/floorplan.xml'];
  5048. mxStencilRegistry.libraries['bootstrap'] = [SHAPES_PATH + '/mxBootstrap.js', STENCIL_PATH + '/bootstrap.xml'];
  5049. mxStencilRegistry.libraries['gmdl'] = [SHAPES_PATH + '/mxGmdl.js', STENCIL_PATH + '/gmdl.xml'];
  5050. mxStencilRegistry.libraries['cabinets'] = [SHAPES_PATH + '/mxCabinets.js', STENCIL_PATH + '/cabinets.xml'];
  5051. mxStencilRegistry.libraries['citrix'] = [STENCIL_PATH + '/citrix.xml'];
  5052. mxStencilRegistry.libraries['archimate'] = [SHAPES_PATH + '/mxArchiMate.js'];
  5053. mxStencilRegistry.libraries['archimate3'] = [SHAPES_PATH + '/mxArchiMate3.js'];
  5054. mxStencilRegistry.libraries['sysml'] = [SHAPES_PATH + '/mxSysML.js'];
  5055. mxStencilRegistry.libraries['eip'] = [SHAPES_PATH + '/mxEip.js', STENCIL_PATH + '/eip.xml'];
  5056. mxStencilRegistry.libraries['networks'] = [SHAPES_PATH + '/mxNetworks.js', STENCIL_PATH + '/networks.xml'];
  5057. mxStencilRegistry.libraries['aws3d'] = [SHAPES_PATH + '/mxAWS3D.js', STENCIL_PATH + '/aws3d.xml'];
  5058. // Triggers dynamic loading for markers
  5059. mxMarker.getPackageForType = function(type)
  5060. {
  5061. var name = null;
  5062. if (type != null && type.length > 0)
  5063. {
  5064. if (type.substring(0, 2) == 'ER')
  5065. {
  5066. name = 'mxgraph.er';
  5067. }
  5068. else if (type.substring(0, 5) == 'sysML')
  5069. {
  5070. name = 'mxgraph.sysml';
  5071. }
  5072. }
  5073. return name;
  5074. };
  5075. var mxMarkerCreateMarker = mxMarker.createMarker;
  5076. mxMarker.createMarker = function(canvas, shape, type, pe, unitX, unitY, size, source, sw, filled)
  5077. {
  5078. if (type != null)
  5079. {
  5080. var f = mxMarker.markers[type];
  5081. if (f == null)
  5082. {
  5083. var name = this.getPackageForType(type);
  5084. if (name != null)
  5085. {
  5086. mxStencilRegistry.getStencil(name);
  5087. }
  5088. }
  5089. }
  5090. return mxMarkerCreateMarker.apply(this, arguments);
  5091. };
  5092. })();