EditorUi.js 169 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679
  1. /**
  2. * Copyright (c) 2006-2012, JGraph Ltd
  3. */
  4. /**
  5. * Constructs a new graph editor
  6. */
  7. EditorUi = function(editor, container, lightbox)
  8. {
  9. mxEventSource.call(this);
  10. this.destroyFunctions = [];
  11. this.editor = editor || new Editor();
  12. this.container = container || document.body;
  13. var ui = this;
  14. var graph = this.editor.graph;
  15. graph.lightbox = lightbox;
  16. // Overrides graph bounds to include background images
  17. var graphGetGraphBounds = graph.getGraphBounds;
  18. graph.getGraphBounds = function()
  19. {
  20. var bounds = graphGetGraphBounds.apply(this, arguments);
  21. var img = this.backgroundImage;
  22. if (img != null && img.width != null && img.height != null)
  23. {
  24. var t = this.view.translate;
  25. var s = this.view.scale;
  26. bounds = mxRectangle.fromRectangle(bounds);
  27. bounds.add(new mxRectangle(
  28. (t.x + img.x) * s, (t.y + img.y) * s,
  29. img.width * s, img.height * s));
  30. }
  31. return bounds;
  32. };
  33. // Faster scrollwheel zoom is possible with CSS transforms
  34. if (graph.useCssTransforms)
  35. {
  36. this.lazyZoomDelay = 0;
  37. }
  38. // Pre-fetches submenu image or replaces with embedded image if supported
  39. if (mxClient.IS_SVG)
  40. {
  41. mxPopupMenu.prototype.submenuImage = 'data:image/gif;base64,R0lGODlhCQAJAIAAAP///zMzMyH5BAEAAAAALAAAAAAJAAkAAAIPhI8WebHsHopSOVgb26AAADs=';
  42. }
  43. else
  44. {
  45. new Image().src = mxPopupMenu.prototype.submenuImage;
  46. }
  47. // Pre-fetches connect image
  48. if (!mxClient.IS_SVG && mxConnectionHandler.prototype.connectImage != null)
  49. {
  50. new Image().src = mxConnectionHandler.prototype.connectImage.src;
  51. }
  52. // Installs selection state listener
  53. this.selectionStateListener = mxUtils.bind(this, function(sender, evt)
  54. {
  55. this.clearSelectionState();
  56. });
  57. graph.getSelectionModel().addListener(mxEvent.CHANGE, this.selectionStateListener);
  58. graph.getModel().addListener(mxEvent.CHANGE, this.selectionStateListener);
  59. graph.addListener(mxEvent.EDITING_STARTED, this.selectionStateListener);
  60. graph.addListener(mxEvent.EDITING_STOPPED, this.selectionStateListener);
  61. graph.getView().addListener('unitChanged', this.selectionStateListener);
  62. // Disables graph and forced panning in chromeless mode
  63. if (this.editor.chromeless && !this.editor.editable)
  64. {
  65. this.footerHeight = 0;
  66. graph.isEnabled = function() { return false; };
  67. // Enables text selection in lightbox
  68. graph.panningHandler.isForcePanningEvent = function(me)
  69. {
  70. var source = me.getSource();
  71. while (source != null && source != graph.container)
  72. {
  73. if (source.nodeName === 'foreignObject' ||
  74. source.nodeName === 'text')
  75. {
  76. return false;
  77. }
  78. source = source.parentNode;
  79. }
  80. return !mxEvent.isPopupTrigger(me.getEvent());
  81. };
  82. // Clears selection on start panning
  83. var panningHandlerStart = graph.panningHandler.start;
  84. graph.panningHandler.start = function()
  85. {
  86. panningHandlerStart.apply(this, arguments);
  87. mxUtils.clearSelection();
  88. };
  89. }
  90. // Creates the user interface
  91. this.actions = new Actions(this);
  92. this.menus = this.createMenus();
  93. if (!graph.standalone)
  94. {
  95. var vertexStyleIgnored = false;
  96. var edgeStyleIgnored = false;
  97. // Note: Everything that is not in styles is ignored (styles is augmented below)
  98. this.setDefaultStyle = function(cell)
  99. {
  100. try
  101. {
  102. if (graph.getModel().isEdge(cell))
  103. {
  104. graph.pasteEdgeStyle = false;
  105. edgeStyleIgnored = false;
  106. }
  107. else
  108. {
  109. vertexStyleIgnored = false;
  110. }
  111. var style = graph.getCellStyle(cell, false);
  112. var values = [];
  113. var keys = [];
  114. for (var key in style)
  115. {
  116. values.push(style[key]);
  117. keys.push(key);
  118. }
  119. // Resets current style
  120. if (graph.getModel().isEdge(cell))
  121. {
  122. graph.currentEdgeStyle = {};
  123. }
  124. else
  125. {
  126. graph.currentVertexStyle = {}
  127. }
  128. this.fireEvent(new mxEventObject('styleChanged',
  129. 'keys', keys, 'values', values,
  130. 'cells', [cell], 'force', true));
  131. // Blocks update of default style with style changes
  132. // and allows change of edge style if default style
  133. // was changed using this function via app UI
  134. if (graph.getModel().isEdge(cell))
  135. {
  136. graph.pasteEdgeStyle = true;
  137. edgeStyleIgnored = true;
  138. }
  139. else
  140. {
  141. vertexStyleIgnored = true;
  142. }
  143. }
  144. catch (e)
  145. {
  146. this.handleError(e);
  147. }
  148. };
  149. this.clearDefaultStyle = function()
  150. {
  151. graph.currentEdgeStyle = mxUtils.clone(graph.defaultEdgeStyle);
  152. graph.currentVertexStyle = mxUtils.clone(graph.defaultVertexStyle);
  153. graph.pasteEdgeStyle = false;
  154. edgeStyleIgnored = false;
  155. vertexStyleIgnored = false;
  156. // Updates UI
  157. this.fireEvent(new mxEventObject('styleChanged', 'keys', [], 'values', [], 'cells', []));
  158. };
  159. graph.addListener('cellsInserted', function(sender, evt)
  160. {
  161. graph.pasteCellStyles(graph.includeDescendants(evt.getProperty('cells')));
  162. });
  163. graph.addListener('textInserted', function(sender, evt)
  164. {
  165. graph.pasteCellStyles(evt.getProperty('cells'));
  166. });
  167. this.createDivs();
  168. this.createUi();
  169. this.refresh();
  170. // Disables HTML and text selection
  171. var textEditing = mxUtils.bind(this, function(evt)
  172. {
  173. if (evt == null)
  174. {
  175. evt = window.event;
  176. }
  177. return graph.isEditing() || (evt != null && this.isSelectionAllowed(evt));
  178. });
  179. // Disables text selection while not editing and no dialog visible
  180. if (this.container == document.body && (!this.editor.chromeless ||
  181. this.editor.editable))
  182. {
  183. this.menubarContainer.onselectstart = textEditing;
  184. this.menubarContainer.onmousedown = textEditing;
  185. this.toolbarContainer.onselectstart = textEditing;
  186. this.toolbarContainer.onmousedown = textEditing;
  187. this.diagramContainer.onselectstart = textEditing;
  188. this.diagramContainer.onmousedown = textEditing;
  189. this.sidebarContainer.onselectstart = textEditing;
  190. this.sidebarContainer.onmousedown = textEditing;
  191. this.formatContainer.onselectstart = textEditing;
  192. this.formatContainer.onmousedown = textEditing;
  193. this.footerContainer.onselectstart = textEditing;
  194. this.footerContainer.onmousedown = textEditing;
  195. if (this.tabContainer != null)
  196. {
  197. // Mouse down is needed for drag and drop
  198. this.tabContainer.onselectstart = textEditing;
  199. }
  200. // Workaround for rubberband selection on iPadOS 16
  201. // Avoid on previous versions to allow label editing
  202. if (mxClient.IS_IOS)
  203. {
  204. function iOSversion()
  205. {
  206. var result = null;
  207. if (/iP(hone|od|ad)/.test(navigator.platform))
  208. {
  209. // supports iOS 2.0 and later: <http://bit.ly/TJjs1V>
  210. var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
  211. try
  212. {
  213. result = [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
  214. }
  215. catch (e)
  216. {
  217. // ignore
  218. }
  219. }
  220. return result;
  221. };
  222. var ver = iOSversion();
  223. if (ver != null && ver[0] >= 16)
  224. {
  225. mxUtils.setPrefixedStyle(this.menubarContainer.style, 'userSelect', 'none');
  226. mxUtils.setPrefixedStyle(this.diagramContainer.style, 'userSelect', 'none');
  227. mxUtils.setPrefixedStyle(this.sidebarContainer.style, 'userSelect', 'none');
  228. mxUtils.setPrefixedStyle(this.formatContainer.style, 'userSelect', 'none');
  229. mxUtils.setPrefixedStyle(this.footerContainer.style, 'userSelect', 'none');
  230. if (this.tabContainer != null)
  231. {
  232. mxUtils.setPrefixedStyle(this.tabContainer.style, 'userSelect', 'none');
  233. }
  234. }
  235. }
  236. }
  237. // And uses built-in context menu while editing
  238. if (!this.editor.chromeless || this.editor.editable)
  239. {
  240. // Allows context menu for links in hints
  241. var linkHandler = function(evt)
  242. {
  243. if (evt != null)
  244. {
  245. var source = mxEvent.getSource(evt);
  246. if (source.nodeName == 'A')
  247. {
  248. while (source != null)
  249. {
  250. if (source.className == 'geHint')
  251. {
  252. return true;
  253. }
  254. source = source.parentNode;
  255. }
  256. }
  257. }
  258. return textEditing(evt);
  259. };
  260. if (mxClient.IS_IE && (typeof(document.documentMode) === 'undefined' || document.documentMode < 9))
  261. {
  262. mxEvent.addListener(this.diagramContainer, 'contextmenu', linkHandler);
  263. }
  264. else
  265. {
  266. // Allows browser context menu outside of diagram and sidebar
  267. this.diagramContainer.oncontextmenu = linkHandler;
  268. }
  269. }
  270. else
  271. {
  272. graph.panningHandler.usePopupTrigger = false;
  273. }
  274. // Contains the main graph instance inside the given panel
  275. graph.init(this.diagramContainer);
  276. // Improves line wrapping for in-place editor
  277. if (mxClient.IS_SVG && graph.view.getDrawPane() != null)
  278. {
  279. var root = graph.view.getDrawPane().ownerSVGElement;
  280. if (root != null)
  281. {
  282. root.style.position = 'absolute';
  283. }
  284. }
  285. // Creates hover icons
  286. this.hoverIcons = this.createHoverIcons();
  287. // Hides hover icons when cells are moved
  288. if (graph.graphHandler != null)
  289. {
  290. var graphHandlerStart = graph.graphHandler.start;
  291. graph.graphHandler.start = function()
  292. {
  293. if (ui.hoverIcons != null)
  294. {
  295. ui.hoverIcons.reset();
  296. }
  297. graphHandlerStart.apply(this, arguments);
  298. };
  299. }
  300. // Adds tooltip when mouse is over scrollbars to show space-drag panning option
  301. mxEvent.addListener(this.diagramContainer, 'mousemove', mxUtils.bind(this, function(evt)
  302. {
  303. var off = mxUtils.getOffset(this.diagramContainer);
  304. if (mxEvent.getClientX(evt) - off.x - this.diagramContainer.clientWidth > 0 ||
  305. mxEvent.getClientY(evt) - off.y - this.diagramContainer.clientHeight > 0)
  306. {
  307. this.diagramContainer.setAttribute('title', mxResources.get('panTooltip'));
  308. }
  309. else
  310. {
  311. this.diagramContainer.removeAttribute('title');
  312. }
  313. }));
  314. // Overrides hovericons to disable while space key is pressed
  315. var hoverIconsIsResetEvent = this.hoverIcons.isResetEvent;
  316. this.hoverIcons.isResetEvent = function(evt, allowShift)
  317. {
  318. return ui.isSpaceDown() || hoverIconsIsResetEvent.apply(this, arguments);
  319. };
  320. this.keydownHandler = mxUtils.bind(this, function(evt)
  321. {
  322. if (evt.which == 16 /* Shift */)
  323. {
  324. this.shiftDown = true;
  325. }
  326. else if (evt.which == 32 /* Space */ && !graph.isEditing())
  327. {
  328. this.spaceDown = true;
  329. this.hoverIcons.reset();
  330. graph.container.style.cursor = 'move';
  331. // Disables scroll after space keystroke with scrollbars
  332. if (!graph.isEditing() && mxEvent.getSource(evt) == graph.container)
  333. {
  334. mxEvent.consume(evt);
  335. }
  336. }
  337. else if (!mxEvent.isConsumed(evt) && evt.keyCode == 27 /* Escape */)
  338. {
  339. this.hideDialog(null, true);
  340. }
  341. });
  342. mxEvent.addListener(document, 'keydown', this.keydownHandler);
  343. this.keyupHandler = mxUtils.bind(this, function(evt)
  344. {
  345. graph.container.style.cursor = '';
  346. this.spaceDown = false;
  347. this.shiftDown = false;
  348. });
  349. mxEvent.addListener(document, 'keyup', this.keyupHandler);
  350. // Forces panning for middle and right mouse buttons
  351. var panningHandlerIsForcePanningEvent = graph.panningHandler.isForcePanningEvent;
  352. graph.panningHandler.isForcePanningEvent = function(me)
  353. {
  354. // Ctrl+left button is reported as right button in FF on Mac
  355. return panningHandlerIsForcePanningEvent.apply(this, arguments) ||
  356. ui.isSpaceDown() || (mxEvent.isMouseEvent(me.getEvent()) &&
  357. (this.usePopupTrigger || !mxEvent.isPopupTrigger(me.getEvent())) &&
  358. ((!mxEvent.isControlDown(me.getEvent()) &&
  359. mxEvent.isRightMouseButton(me.getEvent())) ||
  360. mxEvent.isMiddleMouseButton(me.getEvent())));
  361. };
  362. // Ctrl/Cmd+Enter applies editing value except in Safari where Ctrl+Enter creates
  363. // a new line (while Enter creates a new paragraph and Shift+Enter stops)
  364. var cellEditorIsStopEditingEvent = graph.cellEditor.isStopEditingEvent;
  365. graph.cellEditor.isStopEditingEvent = function(evt)
  366. {
  367. return cellEditorIsStopEditingEvent.apply(this, arguments) ||
  368. (evt.keyCode == 13 && ((!mxClient.IS_SF && mxEvent.isControlDown(evt)) ||
  369. (mxClient.IS_MAC && mxEvent.isMetaDown(evt)) ||
  370. (mxClient.IS_SF && mxEvent.isShiftDown(evt))));
  371. };
  372. // Adds space+wheel for zoom
  373. var graphIsZoomWheelEvent = graph.isZoomWheelEvent;
  374. graph.isZoomWheelEvent = function()
  375. {
  376. return ui.isSpaceDown() || graphIsZoomWheelEvent.apply(this, arguments);
  377. };
  378. // Switches toolbar for text editing
  379. var textMode = false;
  380. var fontMenu = null;
  381. var sizeMenu = null;
  382. var nodes = null;
  383. var updateToolbar = mxUtils.bind(this, function()
  384. {
  385. if (this.toolbar != null && textMode != graph.cellEditor.isContentEditing())
  386. {
  387. var node = this.toolbar.container.firstChild;
  388. var newNodes = [];
  389. while (node != null)
  390. {
  391. var tmp = node.nextSibling;
  392. if (mxUtils.indexOf(this.toolbar.staticElements, node) < 0)
  393. {
  394. node.parentNode.removeChild(node);
  395. newNodes.push(node);
  396. }
  397. node = tmp;
  398. }
  399. // Saves references to special items
  400. var tmp1 = this.toolbar.fontMenu;
  401. var tmp2 = this.toolbar.sizeMenu;
  402. if (nodes == null)
  403. {
  404. this.toolbar.createTextToolbar();
  405. }
  406. else
  407. {
  408. for (var i = 0; i < nodes.length; i++)
  409. {
  410. this.toolbar.container.appendChild(nodes[i]);
  411. }
  412. // Restores references to special items
  413. this.toolbar.fontMenu = fontMenu;
  414. this.toolbar.sizeMenu = sizeMenu;
  415. }
  416. textMode = graph.cellEditor.isContentEditing();
  417. fontMenu = tmp1;
  418. sizeMenu = tmp2;
  419. nodes = newNodes;
  420. }
  421. });
  422. // Overrides cell editor to update toolbar
  423. var cellEditorStartEditing = graph.cellEditor.startEditing;
  424. graph.cellEditor.startEditing = function()
  425. {
  426. cellEditorStartEditing.apply(this, arguments);
  427. updateToolbar();
  428. if (graph.cellEditor.isContentEditing())
  429. {
  430. var updating = false;
  431. var updateCssHandler = function()
  432. {
  433. if (!updating)
  434. {
  435. updating = true;
  436. window.setTimeout(function()
  437. {
  438. var node = graph.getSelectedEditingElement();
  439. if (node != null)
  440. {
  441. var css = mxUtils.getCurrentStyle(node);
  442. if (css != null && ui.toolbar != null)
  443. {
  444. ui.toolbar.setFontName(mxUtils.getCssFontFamily(css.fontFamily));
  445. ui.toolbar.setFontSize(parseInt(css.fontSize));
  446. }
  447. }
  448. updating = false;
  449. }, 0);
  450. }
  451. };
  452. mxEvent.addListener(graph.cellEditor.textarea, 'input', updateCssHandler)
  453. mxEvent.addListener(graph.cellEditor.textarea, 'touchend', updateCssHandler);
  454. mxEvent.addListener(graph.cellEditor.textarea, 'mouseup', updateCssHandler);
  455. mxEvent.addListener(graph.cellEditor.textarea, 'keyup', updateCssHandler);
  456. updateCssHandler();
  457. }
  458. };
  459. // Updates toolbar and handles possible errors
  460. var cellEditorStopEditing = graph.cellEditor.stopEditing;
  461. graph.cellEditor.stopEditing = function(cell, trigger)
  462. {
  463. try
  464. {
  465. cellEditorStopEditing.apply(this, arguments);
  466. updateToolbar();
  467. }
  468. catch (e)
  469. {
  470. ui.handleError(e);
  471. }
  472. };
  473. // Enables scrollbars and sets cursor style for the container
  474. graph.container.setAttribute('tabindex', '0');
  475. graph.container.style.cursor = 'default';
  476. // Workaround for page scroll if embedded via iframe
  477. if (window.self === window.top && graph.container.parentNode != null)
  478. {
  479. try
  480. {
  481. graph.container.focus();
  482. }
  483. catch (e)
  484. {
  485. // ignores error in old versions of IE
  486. }
  487. }
  488. // Keeps graph container focused on mouse down
  489. var graphFireMouseEvent = graph.fireMouseEvent;
  490. graph.fireMouseEvent = function(evtName, me, sender)
  491. {
  492. try
  493. {
  494. if (evtName == mxEvent.MOUSE_DOWN)
  495. {
  496. this.container.focus();
  497. }
  498. graphFireMouseEvent.apply(this, arguments);
  499. }
  500. catch (e)
  501. {
  502. ui.handleError(e);
  503. }
  504. };
  505. // Adds error handling for foldCells
  506. var graphFoldCells = graph.foldCells;
  507. graph.foldCells = function(collapse, recurse, cells, checkFoldable, evt)
  508. {
  509. try
  510. {
  511. graphFoldCells.apply(this, arguments);
  512. }
  513. catch (e)
  514. {
  515. ui.handleError(e);
  516. }
  517. };
  518. // Configures automatic expand on mouseover
  519. graph.popupMenuHandler.autoExpand = true;
  520. // Installs context menu
  521. if (this.menus != null)
  522. {
  523. graph.popupMenuHandler.factoryMethod = mxUtils.bind(this, function(menu, cell, evt)
  524. {
  525. this.menus.createPopupMenu(menu, cell, evt);
  526. });
  527. }
  528. // Hides context menu
  529. mxEvent.addGestureListeners(document, mxUtils.bind(this, function(evt)
  530. {
  531. graph.popupMenuHandler.hideMenu();
  532. }));
  533. // Create handler for key events
  534. this.keyHandler = this.createKeyHandler(editor);
  535. // Getter for key handler
  536. this.getKeyHandler = function()
  537. {
  538. return keyHandler;
  539. };
  540. graph.connectionHandler.addListener(mxEvent.CONNECT, function(sender, evt)
  541. {
  542. var cells = [evt.getProperty('cell')];
  543. if (evt.getProperty('terminalInserted'))
  544. {
  545. cells.push(evt.getProperty('terminal'));
  546. window.setTimeout(function()
  547. {
  548. if (ui.hoverIcons != null)
  549. {
  550. ui.hoverIcons.update(graph.view.getState(cells[cells.length - 1]));
  551. }
  552. }, 0);
  553. }
  554. graph.pasteCellStyles(cells);
  555. });
  556. this.addListener('styleChanged', mxUtils.bind(this, function(sender, evt)
  557. {
  558. var force = evt.getProperty('force');
  559. // Checks if edges and/or vertices were modified
  560. if (this.updateDefaultStyle || force)
  561. {
  562. graph.copyCellStyles(evt.getProperty('cells'),
  563. evt.getProperty('keys'), evt.getProperty('values'),
  564. graph.currentVertexStyle, graph.currentEdgeStyle,
  565. vertexStyleIgnored, edgeStyleIgnored);
  566. }
  567. if (this.toolbar != null)
  568. {
  569. this.toolbar.setFontName(graph.currentVertexStyle['fontFamily'] || Menus.prototype.defaultFont);
  570. this.toolbar.setFontSize(graph.currentVertexStyle['fontSize'] || Menus.prototype.defaultFontSize);
  571. if (this.toolbar.edgeStyleMenu != null)
  572. {
  573. // Updates toolbar icon for edge style
  574. var edgeStyleDiv = this.toolbar.edgeStyleMenu.getElementsByTagName('div')[0];
  575. if (graph.currentEdgeStyle['edgeStyle'] == 'orthogonalEdgeStyle' && graph.currentEdgeStyle['curved'] == '1')
  576. {
  577. edgeStyleDiv.className = 'geSprite geSprite-curved';
  578. }
  579. else if (graph.currentEdgeStyle['edgeStyle'] == 'straight' || graph.currentEdgeStyle['edgeStyle'] == 'none' ||
  580. graph.currentEdgeStyle['edgeStyle'] == null)
  581. {
  582. edgeStyleDiv.className = 'geSprite geSprite-straight';
  583. }
  584. else if (graph.currentEdgeStyle['edgeStyle'] == 'entityRelationEdgeStyle')
  585. {
  586. edgeStyleDiv.className = 'geSprite geSprite-entity';
  587. }
  588. else if (graph.currentEdgeStyle['edgeStyle'] == 'elbowEdgeStyle')
  589. {
  590. edgeStyleDiv.className = 'geSprite geSprite-' + ((graph.currentEdgeStyle['elbow'] == 'vertical') ?
  591. 'verticalelbow' : 'horizontalelbow');
  592. }
  593. else if (graph.currentEdgeStyle['edgeStyle'] == 'isometricEdgeStyle')
  594. {
  595. edgeStyleDiv.className = 'geSprite geSprite-' + ((graph.currentEdgeStyle['elbow'] == 'vertical') ?
  596. 'verticalisometric' : 'horizontalisometric');
  597. }
  598. else
  599. {
  600. edgeStyleDiv.className = 'geSprite geSprite-orthogonal';
  601. }
  602. }
  603. if (this.toolbar.edgeShapeMenu != null)
  604. {
  605. // Updates icon for edge shape
  606. var edgeShapeDiv = this.toolbar.edgeShapeMenu.getElementsByTagName('div')[0];
  607. if (graph.currentEdgeStyle['shape'] == 'link')
  608. {
  609. edgeShapeDiv.className = 'geSprite geSprite-linkedge';
  610. }
  611. else if (graph.currentEdgeStyle['shape'] == 'flexArrow')
  612. {
  613. edgeShapeDiv.className = 'geSprite geSprite-arrow';
  614. }
  615. else if (graph.currentEdgeStyle['shape'] == 'arrow')
  616. {
  617. edgeShapeDiv.className = 'geSprite geSprite-simplearrow';
  618. }
  619. else
  620. {
  621. edgeShapeDiv.className = 'geSprite geSprite-connection';
  622. }
  623. }
  624. }
  625. }));
  626. // Update font size and font family labels
  627. if (this.toolbar != null)
  628. {
  629. var update = mxUtils.bind(this, function()
  630. {
  631. var ff = graph.currentVertexStyle['fontFamily'] || 'Helvetica';
  632. var fs = String(graph.currentVertexStyle['fontSize'] || '12');
  633. var state = graph.getView().getState(graph.getSelectionCell());
  634. if (state != null)
  635. {
  636. ff = state.style[mxConstants.STYLE_FONTFAMILY] || ff;
  637. fs = state.style[mxConstants.STYLE_FONTSIZE] || fs;
  638. if (ff.length > 10)
  639. {
  640. ff = ff.substring(0, 8) + '...';
  641. }
  642. }
  643. this.toolbar.setFontName(ff);
  644. this.toolbar.setFontSize(fs);
  645. });
  646. graph.getSelectionModel().addListener(mxEvent.CHANGE, update);
  647. graph.getModel().addListener(mxEvent.CHANGE, update);
  648. }
  649. // Makes sure the current layer is visible when cells are added
  650. graph.addListener(mxEvent.CELLS_ADDED, function(sender, evt)
  651. {
  652. var cells = evt.getProperty('cells');
  653. var parent = evt.getProperty('parent');
  654. if (parent != null && graph.getModel().isLayer(parent) &&
  655. !graph.isCellVisible(parent) && cells != null &&
  656. cells.length > 0)
  657. {
  658. graph.getModel().setVisible(parent, true);
  659. }
  660. });
  661. // Selects parent layer for current selection
  662. if (Graph.selectParentLayer)
  663. {
  664. graph.selectionModel.addListener(mxEvent.CHANGE, function()
  665. {
  666. if (graph.isEnabled() && !graph.isSelectionEmpty())
  667. {
  668. var layer = graph.getLayerForCells(graph.getSelectionCells());
  669. if (layer != null)
  670. {
  671. graph.setDefaultParent(layer);
  672. }
  673. }
  674. });
  675. }
  676. // Global handler to hide the current menu
  677. this.gestureHandler = mxUtils.bind(this, function(evt)
  678. {
  679. if (this.currentMenu != null && mxEvent.getSource(evt) != this.currentMenu.div)
  680. {
  681. this.hideCurrentMenu();
  682. }
  683. });
  684. mxEvent.addGestureListeners(document, this.gestureHandler);
  685. // Updates the editor UI after the window has been resized or the orientation changes
  686. // Timeout is workaround for old IE versions which have a delay for DOM client sizes.
  687. var resizeThread = null;
  688. this.resizeHandler = mxUtils.bind(this, function()
  689. {
  690. if (resizeThread != null)
  691. {
  692. window.clearTimeout(resizeThread);
  693. }
  694. resizeThread = window.setTimeout(mxUtils.bind(this, function()
  695. {
  696. resizeThread = null;
  697. this.windowResized();
  698. }), 100);
  699. });
  700. mxEvent.addListener(window, 'resize', this.resizeHandler);
  701. this.orientationChangeHandler = mxUtils.bind(this, function()
  702. {
  703. this.refresh();
  704. });
  705. mxEvent.addListener(window, 'orientationchange', this.orientationChangeHandler);
  706. // Workaround for bug on iOS see
  707. // http://stackoverflow.com/questions/19012135/ios-7-ipad-safari-landscape-innerheight-outerheight-layout-issue
  708. if (mxClient.IS_IOS && !window.navigator.standalone && typeof Menus !== 'undefined')
  709. {
  710. this.scrollHandler = mxUtils.bind(this, function()
  711. {
  712. window.scrollTo(0, 0);
  713. });
  714. mxEvent.addListener(window, 'scroll', this.scrollHandler);
  715. }
  716. /**
  717. * Sets the initial scrollbar locations after a file was loaded.
  718. */
  719. this.editor.addListener('resetGraphView', mxUtils.bind(this, function()
  720. {
  721. this.resetScrollbars();
  722. }));
  723. /**
  724. * Repaints the grid.
  725. */
  726. this.addListener('gridEnabledChanged', mxUtils.bind(this, function()
  727. {
  728. graph.view.validateBackground();
  729. }));
  730. this.addListener('backgroundColorChanged', mxUtils.bind(this, function()
  731. {
  732. graph.view.validateBackground();
  733. }));
  734. /**
  735. * Repaints the grid.
  736. */
  737. graph.addListener('gridSizeChanged', mxUtils.bind(this, function()
  738. {
  739. if (graph.isGridEnabled())
  740. {
  741. graph.view.validateBackground();
  742. }
  743. }));
  744. // Resets UI, updates action and menu states
  745. this.editor.resetGraph();
  746. }
  747. this.init();
  748. if (!graph.standalone)
  749. {
  750. this.open();
  751. }
  752. };
  753. /**
  754. * Global config that specifies if the compact UI elements should be used.
  755. */
  756. EditorUi.compactUi = true;
  757. /**
  758. * Static method for pasing PNG files.
  759. */
  760. EditorUi.parsePng = function(f, fn, error)
  761. {
  762. var pos = 0;
  763. function fread(d, count)
  764. {
  765. var start = pos;
  766. pos += count;
  767. return d.substring(start, pos);
  768. };
  769. // Reads unsigned long 32 bit big endian
  770. function _freadint(d)
  771. {
  772. var bytes = fread(d, 4);
  773. return bytes.charCodeAt(3) + (bytes.charCodeAt(2) << 8) +
  774. (bytes.charCodeAt(1) << 16) + (bytes.charCodeAt(0) << 24);
  775. };
  776. // Checks signature
  777. if (fread(f,8) != String.fromCharCode(137) + 'PNG' + String.fromCharCode(13, 10, 26, 10))
  778. {
  779. if (error != null)
  780. {
  781. error();
  782. }
  783. return;
  784. }
  785. // Reads header chunk
  786. fread(f,4);
  787. if (fread(f,4) != 'IHDR')
  788. {
  789. if (error != null)
  790. {
  791. error();
  792. }
  793. return;
  794. }
  795. fread(f, 17);
  796. do
  797. {
  798. var n = _freadint(f);
  799. var type = fread(f,4);
  800. if (fn != null)
  801. {
  802. if (fn(pos - 8, type, n))
  803. {
  804. break;
  805. }
  806. }
  807. value = fread(f,n);
  808. fread(f,4);
  809. if (type == 'IEND')
  810. {
  811. break;
  812. }
  813. }
  814. while (n);
  815. };
  816. // Extends mxEventSource
  817. mxUtils.extend(EditorUi, mxEventSource);
  818. /**
  819. * Specifies the size of the split bar.
  820. */
  821. EditorUi.prototype.splitSize = (mxClient.IS_TOUCH || mxClient.IS_POINTER) ? 12 : 8;
  822. /**
  823. * Specifies the height of the menubar. Default is 30.
  824. */
  825. EditorUi.prototype.menubarHeight = 30;
  826. /**
  827. * Specifies the width of the format panel should be enabled. Default is true.
  828. */
  829. EditorUi.prototype.formatEnabled = true;
  830. /**
  831. * Specifies the width of the format panel. Default is 240.
  832. */
  833. EditorUi.prototype.formatWidth = 240;
  834. /**
  835. * Specifies the height of the toolbar. Default is 38.
  836. */
  837. EditorUi.prototype.toolbarHeight = 38;
  838. /**
  839. * Specifies the height of the footer. Default is 28.
  840. */
  841. EditorUi.prototype.footerHeight = 28;
  842. /**
  843. * Specifies the default sidebar width.
  844. */
  845. EditorUi.prototype.defaultSidebarWidth = Math.min(screen.width / 2,
  846. (urlParams['sidebar-entries'] != 'large') ? 212 : 240);
  847. /**
  848. * Specifies the position of the horizontal split bar.
  849. */
  850. EditorUi.prototype.hsplitPosition = (screen.width <= Editor.smallScreenWidth) ? 0 :
  851. EditorUi.prototype.defaultSidebarWidth;
  852. /**
  853. * Specifies if animations are allowed in <executeLayout>. Default is true.
  854. */
  855. EditorUi.prototype.allowAnimation = true;
  856. /**
  857. * Default is 2.
  858. */
  859. EditorUi.prototype.lightboxMaxFitScale = 2;
  860. /**
  861. * Default is 4.
  862. */
  863. EditorUi.prototype.lightboxVerticalDivider = 4;
  864. /**
  865. * Specifies if single click on horizontal split should collapse sidebar. Default is false.
  866. */
  867. EditorUi.prototype.hsplitClickEnabled = false;
  868. /**
  869. * Whether the default styles should be updated when styles are changed. Default is true.
  870. */
  871. EditorUi.prototype.updateDefaultStyle = false;
  872. /**
  873. * Whether the default styles should be updated when styles are changed. Default is true.
  874. */
  875. EditorUi.prototype.spaceDown = false;
  876. /**
  877. * Whether the default styles should be updated when styles are changed. Default is true.
  878. */
  879. EditorUi.prototype.shiftDown = false;
  880. /**
  881. * Installs the listeners to update the action states.
  882. */
  883. EditorUi.prototype.init = function()
  884. {
  885. var graph = this.editor.graph;
  886. if (!graph.standalone)
  887. {
  888. if (urlParams['shape-picker'] != '0')
  889. {
  890. this.installShapePicker();
  891. }
  892. // Hides tooltips and connection points when scrolling
  893. mxEvent.addListener(graph.container, 'scroll', mxUtils.bind(this, function()
  894. {
  895. graph.tooltipHandler.hide();
  896. if (graph.connectionHandler != null && graph.connectionHandler.constraintHandler != null)
  897. {
  898. graph.connectionHandler.constraintHandler.reset();
  899. }
  900. }));
  901. // Hides tooltip on escape
  902. graph.addListener(mxEvent.ESCAPE, mxUtils.bind(this, function()
  903. {
  904. graph.tooltipHandler.hide();
  905. var rb = graph.getRubberband();
  906. if (rb != null)
  907. {
  908. rb.cancel();
  909. }
  910. }));
  911. mxEvent.addListener(graph.container, 'keydown', mxUtils.bind(this, function(evt)
  912. {
  913. this.onKeyDown(evt);
  914. }));
  915. mxEvent.addListener(graph.container, 'keypress', mxUtils.bind(this, function(evt)
  916. {
  917. this.onKeyPress(evt);
  918. }));
  919. // Updates action states
  920. this.addUndoListener();
  921. this.addBeforeUnloadListener();
  922. graph.getSelectionModel().addListener(mxEvent.CHANGE, mxUtils.bind(this, function()
  923. {
  924. this.updateActionStates();
  925. }));
  926. graph.getModel().addListener(mxEvent.CHANGE, mxUtils.bind(this, function()
  927. {
  928. this.updateActionStates();
  929. }));
  930. // Changes action states after change of default parent
  931. var graphSetDefaultParent = graph.setDefaultParent;
  932. var ui = this;
  933. this.editor.graph.setDefaultParent = function()
  934. {
  935. graphSetDefaultParent.apply(this, arguments);
  936. ui.updateActionStates();
  937. };
  938. // Hack to make showLinkDialog and editLink available in vertex handler
  939. graph.showLinkDialog = mxUtils.bind(ui, ui.showLinkDialog);
  940. graph.editLink = ui.actions.get('editLink').funct;
  941. this.updateActionStates();
  942. this.initClipboard();
  943. this.initCanvas();
  944. if (this.format != null)
  945. {
  946. this.format.init();
  947. }
  948. }
  949. };
  950. /**
  951. * Returns information about the current selection.
  952. */
  953. EditorUi.prototype.clearSelectionState = function()
  954. {
  955. this.selectionState = null;
  956. };
  957. /**
  958. * Returns information about the current selection.
  959. */
  960. EditorUi.prototype.getSelectionState = function()
  961. {
  962. if (this.selectionState == null)
  963. {
  964. this.selectionState = this.createSelectionState();
  965. }
  966. return this.selectionState;
  967. };
  968. /**
  969. * Returns information about the current selection.
  970. */
  971. EditorUi.prototype.createSelectionState = function()
  972. {
  973. var graph = this.editor.graph;
  974. var cells = graph.getSelectionCells();
  975. var result = this.initSelectionState();
  976. var initial = true;
  977. for (var i = 0; i < cells.length; i++)
  978. {
  979. var style = graph.getCurrentCellStyle(cells[i]);
  980. if (mxUtils.getValue(style, mxConstants.STYLE_EDITABLE, '1') != '0')
  981. {
  982. this.updateSelectionStateForCell(result, cells[i], cells, initial);
  983. initial = false;
  984. }
  985. }
  986. this.updateSelectionStateForTableCells(result);
  987. return result;
  988. };
  989. /**
  990. * Returns information about the current selection.
  991. */
  992. EditorUi.prototype.initSelectionState = function()
  993. {
  994. return {vertices: [], edges: [], cells: [], x: null, y: null, width: null, height: null,
  995. style: {}, containsImage: false, containsLabel: false, fill: true, glass: true, html: true,
  996. rounded: true, autoSize: false, image: false, shadow: true, lineJumps: true, resizable: true,
  997. table: false, cell: false, row: false, movable: true, rotatable: true, stroke: true,
  998. swimlane: false, unlocked: this.editor.graph.isEnabled(), connections: false};
  999. };
  1000. /**
  1001. * Adds information about current selected table cells range.
  1002. */
  1003. EditorUi.prototype.updateSelectionStateForTableCells = function(result)
  1004. {
  1005. if (result.cells.length > 1 && result.cell)
  1006. {
  1007. var cells = mxUtils.sortCells(result.cells);
  1008. var model = this.editor.graph.model;
  1009. var parent = model.getParent(cells[0]);
  1010. var table = model.getParent(parent);
  1011. if (parent != null && table != null)
  1012. {
  1013. var col = parent.getIndex(cells[0]);
  1014. var row = table.getIndex(parent);
  1015. var lastspan = null;
  1016. var colspan = 1;
  1017. var rowspan = 1;
  1018. var index = 0;
  1019. var nextRowCell = (row < table.getChildCount() - 1) ?
  1020. model.getChildAt(model.getChildAt(
  1021. table, row + 1), col) : null;
  1022. while (index < cells.length - 1)
  1023. {
  1024. var next = cells[++index];
  1025. if (nextRowCell != null && nextRowCell == next &&
  1026. (lastspan == null || colspan == lastspan))
  1027. {
  1028. lastspan = colspan;
  1029. colspan = 0;
  1030. rowspan++;
  1031. parent = model.getParent(nextRowCell);
  1032. nextRowCell = (row + rowspan < table.getChildCount()) ?
  1033. model.getChildAt(model.getChildAt(
  1034. table, row + rowspan), col) : null;
  1035. }
  1036. var state = this.editor.graph.view.getState(next);
  1037. if (next == model.getChildAt(parent, col + colspan) && state != null &&
  1038. mxUtils.getValue(state.style, 'colspan', 1) == 1 &&
  1039. mxUtils.getValue(state.style, 'rowspan', 1) == 1)
  1040. {
  1041. colspan++;
  1042. }
  1043. else
  1044. {
  1045. break;
  1046. }
  1047. }
  1048. if (index == rowspan * colspan - 1)
  1049. {
  1050. result.mergeCell = cells[0];
  1051. result.colspan = colspan;
  1052. result.rowspan = rowspan;
  1053. }
  1054. }
  1055. }
  1056. };
  1057. /**
  1058. * Returns information about the current selection.
  1059. */
  1060. EditorUi.prototype.windowResized = function()
  1061. {
  1062. window.setTimeout(mxUtils.bind(this, function()
  1063. {
  1064. if (this.editor.graph != null)
  1065. {
  1066. this.refresh();
  1067. }
  1068. }), 0);
  1069. };
  1070. /**
  1071. * Returns information about the current selection.
  1072. */
  1073. EditorUi.prototype.createTimeout = function(timeout, fn, error)
  1074. {
  1075. var acceptResponse = true;
  1076. var result = null;
  1077. var handleError = mxUtils.bind(this, function(e)
  1078. {
  1079. if (result.clear())
  1080. {
  1081. acceptResponse = false;
  1082. e = (e != null) ? e : {code: App.ERROR_TIMEOUT,
  1083. message: mxResources.get('timeout'),
  1084. retry: mxUtils.bind(this, function()
  1085. {
  1086. this.createTimeout(timeout, fn, error);
  1087. })};
  1088. if (error != null)
  1089. {
  1090. error(e);
  1091. }
  1092. else
  1093. {
  1094. this.handleError(e);
  1095. }
  1096. }
  1097. });
  1098. var timeoutThread = window.setTimeout(handleError,
  1099. (timeout != null) ? timeout : this.timeout);
  1100. var result = {
  1101. clear: function()
  1102. {
  1103. window.clearTimeout(timeoutThread);
  1104. return acceptResponse;
  1105. },
  1106. isAlive: function()
  1107. {
  1108. return acceptResponse;
  1109. }
  1110. };
  1111. if (fn != null)
  1112. {
  1113. this.tryAndHandle(mxUtils.bind(this, function()
  1114. {
  1115. fn(result);
  1116. }), handleError);
  1117. }
  1118. return result;
  1119. };
  1120. /**
  1121. * Returns information about the current selection.
  1122. */
  1123. EditorUi.prototype.tryAndHandle = function(fn, error)
  1124. {
  1125. try
  1126. {
  1127. fn();
  1128. }
  1129. catch (e)
  1130. {
  1131. if (error != null)
  1132. {
  1133. error(e);
  1134. }
  1135. else
  1136. {
  1137. this.handleError(e);
  1138. }
  1139. }
  1140. };
  1141. /**
  1142. * Returns information about the current selection.
  1143. */
  1144. EditorUi.prototype.convertDarkModeColors = function(cells, keys)
  1145. {
  1146. var graph = this.editor.graph;
  1147. cells = (cells != null) ? cells : graph.getSelectionCells();
  1148. keys = (keys != null) ? keys : [mxConstants.STYLE_FONTCOLOR,
  1149. mxConstants.STYLE_FILLCOLOR, mxConstants.STYLE_GRADIENTCOLOR,
  1150. mxConstants.STYLE_STROKECOLOR, mxConstants.STYLE_IMAGE_BORDER,
  1151. mxConstants.STYLE_IMAGE_BACKGROUND, mxConstants.STYLE_LABEL_BORDERCOLOR,
  1152. mxConstants.STYLE_SWIMLANE_FILLCOLOR, mxConstants.STYLE_LABEL_BACKGROUNDCOLOR];
  1153. var colorCache = {};
  1154. var canvas = document.createElement('canvas');
  1155. canvas.width = 1;
  1156. canvas.height = 1;
  1157. var ctx = canvas.getContext('2d', {willReadFrequently: true});
  1158. ctx.filter = 'invert(100%) hue-rotate(180deg)';
  1159. graph.model.beginUpdate();
  1160. try
  1161. {
  1162. for (var i = 0; i < cells.length; i++)
  1163. {
  1164. if (graph.model.isEdge(cells[i]) || graph.model.isVertex(cells[i]))
  1165. {
  1166. var style = graph.getCurrentCellStyle(cells[i]);
  1167. if (style != null)
  1168. {
  1169. for (var j = 0; j < keys.length; j++)
  1170. {
  1171. try
  1172. {
  1173. var value = style[keys[j]];
  1174. if (value != null && value.charAt(0) == '#')
  1175. {
  1176. var result = colorCache[value];
  1177. if (result == null)
  1178. {
  1179. result = Graph.invertColor(value, ctx);
  1180. colorCache[value] = result;
  1181. }
  1182. graph.setCellStyles(keys[j], result, [cells[i]]);
  1183. }
  1184. }
  1185. catch (e)
  1186. {
  1187. // ignore
  1188. }
  1189. }
  1190. }
  1191. }
  1192. }
  1193. }
  1194. finally
  1195. {
  1196. graph.model.endUpdate();
  1197. }
  1198. };
  1199. /**
  1200. * Returns information about the current selection.
  1201. */
  1202. EditorUi.prototype.updateSelectionStateForCell = function(result, cell, cells, initial)
  1203. {
  1204. var graph = this.editor.graph;
  1205. result.cells.push(cell);
  1206. if (graph.getModel().isVertex(cell))
  1207. {
  1208. result.connections = graph.model.getEdgeCount(cell) > 0;
  1209. result.unlocked = result.unlocked && !graph.isCellLocked(cell);
  1210. result.resizable = result.resizable && graph.isCellResizable(cell);
  1211. result.rotatable = result.rotatable && graph.isCellRotatable(cell);
  1212. result.movable = result.movable && graph.isCellMovable(cell) &&
  1213. !graph.isTableRow(cell) && !graph.isTableCell(cell);
  1214. result.swimlane = result.swimlane || graph.isSwimlane(cell);
  1215. result.table = result.table || graph.isTable(cell);
  1216. result.cell = result.cell || graph.isTableCell(cell);
  1217. result.row = result.row || graph.isTableRow(cell);
  1218. result.vertices.push(cell);
  1219. var geo = graph.getCellGeometry(cell);
  1220. if (geo != null)
  1221. {
  1222. if (geo.width > 0)
  1223. {
  1224. if (result.width == null)
  1225. {
  1226. result.width = geo.width;
  1227. }
  1228. else if (result.width != geo.width)
  1229. {
  1230. result.width = '';
  1231. }
  1232. }
  1233. else
  1234. {
  1235. result.containsLabel = true;
  1236. }
  1237. if (geo.height > 0)
  1238. {
  1239. if (result.height == null)
  1240. {
  1241. result.height = geo.height;
  1242. }
  1243. else if (result.height != geo.height)
  1244. {
  1245. result.height = '';
  1246. }
  1247. }
  1248. else
  1249. {
  1250. result.containsLabel = true;
  1251. }
  1252. if (!geo.relative || geo.offset != null)
  1253. {
  1254. var x = (geo.relative) ? geo.offset.x : geo.x;
  1255. var y = (geo.relative) ? geo.offset.y : geo.y;
  1256. if (result.x == null)
  1257. {
  1258. result.x = x;
  1259. }
  1260. else if (result.x != x)
  1261. {
  1262. result.x = '';
  1263. }
  1264. if (result.y == null)
  1265. {
  1266. result.y = y;
  1267. }
  1268. else if (result.y != y)
  1269. {
  1270. result.y = '';
  1271. }
  1272. }
  1273. }
  1274. }
  1275. else if (graph.getModel().isEdge(cell))
  1276. {
  1277. result.edges.push(cell);
  1278. result.connections = true;
  1279. result.resizable = false;
  1280. result.rotatable = false;
  1281. result.movable = false;
  1282. }
  1283. var state = graph.view.getState(cell);
  1284. if (state != null)
  1285. {
  1286. result.html = result.html && graph.isHtmlLabel(cell);
  1287. result.autoSize = result.autoSize || graph.isAutoSizeState(state);
  1288. result.glass = result.glass && graph.isGlassState(state);
  1289. result.rounded = result.rounded && graph.isRoundedState(state);
  1290. result.lineJumps = result.lineJumps && graph.isLineJumpState(state);
  1291. result.image = result.image || graph.isImageState(state);
  1292. result.shadow = result.shadow && graph.isShadowState(state);
  1293. result.fill = result.fill && graph.isFillState(state);
  1294. result.gradient = result.fill && graph.isGradientState(state);
  1295. result.stroke = result.stroke && graph.isStrokeState(state);
  1296. var shape = mxUtils.getValue(state.style, mxConstants.STYLE_SHAPE, null);
  1297. result.containsImage = result.containsImage || shape == 'image';
  1298. graph.mergeStyle(state.style, result.style, initial);
  1299. }
  1300. };
  1301. /**
  1302. * Returns true if the given event should start editing. This implementation returns true.
  1303. */
  1304. EditorUi.prototype.installShapePicker = function()
  1305. {
  1306. var graph = this.editor.graph;
  1307. var ui = this;
  1308. // Uses this event to process mouseDown to check the selection state before it is changed
  1309. graph.addListener(mxEvent.FIRE_MOUSE_EVENT, mxUtils.bind(this, function(sender, evt)
  1310. {
  1311. if (evt.getProperty('eventName') == 'mouseDown')
  1312. {
  1313. ui.hideShapePicker();
  1314. }
  1315. }));
  1316. var hidePicker = mxUtils.bind(this, function()
  1317. {
  1318. ui.hideShapePicker(true);
  1319. });
  1320. graph.addListener('wheel', hidePicker);
  1321. graph.addListener(mxEvent.ESCAPE, hidePicker);
  1322. graph.view.addListener(mxEvent.SCALE, hidePicker);
  1323. graph.view.addListener(mxEvent.SCALE_AND_TRANSLATE, hidePicker);
  1324. graph.getSelectionModel().addListener(mxEvent.CHANGE, hidePicker);
  1325. // Counts as popup menu
  1326. var popupMenuHandlerIsMenuShowing = graph.popupMenuHandler.isMenuShowing;
  1327. graph.popupMenuHandler.isMenuShowing = function()
  1328. {
  1329. return popupMenuHandlerIsMenuShowing.apply(this, arguments) ||
  1330. ui.shapePicker != null || ui.currentMenu != null;
  1331. };
  1332. // Adds dbl click dialog for inserting shapes
  1333. var graphDblClick = graph.dblClick;
  1334. graph.dblClick = function(evt, cell)
  1335. {
  1336. if (this.isEnabled())
  1337. {
  1338. if (cell == null && ui.sidebar != null && !mxEvent.isShiftDown(evt) &&
  1339. !graph.isCellLocked(graph.getDefaultParent()))
  1340. {
  1341. var pt = mxUtils.convertPoint(this.container, mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  1342. mxEvent.consume(evt);
  1343. // Asynchronous to avoid direct insert after double tap
  1344. window.setTimeout(mxUtils.bind(this, function()
  1345. {
  1346. ui.showShapePicker(pt.x, pt.y);
  1347. }), 30);
  1348. }
  1349. else
  1350. {
  1351. graphDblClick.apply(this, arguments);
  1352. }
  1353. }
  1354. };
  1355. if (this.hoverIcons != null)
  1356. {
  1357. this.hoverIcons.addListener('reset', hidePicker);
  1358. var hoverIconsDrag = this.hoverIcons.drag;
  1359. this.hoverIcons.drag = function()
  1360. {
  1361. ui.hideShapePicker();
  1362. hoverIconsDrag.apply(this, arguments);
  1363. };
  1364. var hoverIconsExecute = this.hoverIcons.execute;
  1365. this.hoverIcons.execute = function(state, dir, me)
  1366. {
  1367. var evt = me.getEvent();
  1368. if (!this.graph.isCloneEvent(evt) && !mxEvent.isShiftDown(evt))
  1369. {
  1370. this.graph.connectVertex(state.cell, dir, this.graph.defaultEdgeLength, evt, null, null, mxUtils.bind(this, function(x, y, execute)
  1371. {
  1372. var temp = graph.getCompositeParent(state.cell);
  1373. var geo = graph.getCellGeometry(temp);
  1374. me.consume();
  1375. while (temp != null && graph.model.isVertex(temp) && geo != null && geo.relative)
  1376. {
  1377. cell = temp;
  1378. temp = graph.model.getParent(cell)
  1379. geo = graph.getCellGeometry(temp);
  1380. }
  1381. // Asynchronous to avoid direct insert after double tap
  1382. window.setTimeout(mxUtils.bind(this, function()
  1383. {
  1384. ui.showShapePicker(me.getGraphX(), me.getGraphY(), temp, mxUtils.bind(this, function(cell)
  1385. {
  1386. execute(cell);
  1387. if (ui.hoverIcons != null)
  1388. {
  1389. ui.hoverIcons.update(graph.view.getState(cell));
  1390. }
  1391. }), dir);
  1392. }), 30);
  1393. }), mxUtils.bind(this, function(result)
  1394. {
  1395. this.graph.selectCellsForConnectVertex(result, evt, this);
  1396. }));
  1397. }
  1398. else
  1399. {
  1400. hoverIconsExecute.apply(this, arguments);
  1401. }
  1402. };
  1403. var thread = null;
  1404. this.hoverIcons.addListener('focus', mxUtils.bind(this, function(sender, evt)
  1405. {
  1406. if (thread != null)
  1407. {
  1408. window.clearTimeout(thread);
  1409. }
  1410. thread = window.setTimeout(mxUtils.bind(this, function()
  1411. {
  1412. var arrow = evt.getProperty('arrow');
  1413. var dir = evt.getProperty('direction');
  1414. var mouseEvent = evt.getProperty('event');
  1415. var rect = arrow.getBoundingClientRect();
  1416. var offset = mxUtils.getOffset(graph.container);
  1417. var x = graph.container.scrollLeft + rect.x - offset.x;
  1418. var y = graph.container.scrollTop + rect.y - offset.y;
  1419. var temp = graph.getCompositeParent((this.hoverIcons.currentState != null) ?
  1420. this.hoverIcons.currentState.cell : null);
  1421. var div = ui.showShapePicker(x, y, temp, mxUtils.bind(this, function(cell)
  1422. {
  1423. if (cell != null)
  1424. {
  1425. graph.connectVertex(temp, dir, graph.defaultEdgeLength, mouseEvent, true, false, function(x, y, execute)
  1426. {
  1427. execute(cell);
  1428. if (ui.hoverIcons != null)
  1429. {
  1430. ui.hoverIcons.update(graph.view.getState(cell));
  1431. }
  1432. }, function(cells)
  1433. {
  1434. graph.selectCellsForConnectVertex(cells);
  1435. }, mouseEvent, this.hoverIcons);
  1436. }
  1437. }), dir, true);
  1438. if (div != null)
  1439. {
  1440. this.centerShapePicker(div, rect, x, y, dir);
  1441. mxUtils.setOpacity(div, 30);
  1442. mxEvent.addListener(div, 'mouseenter', function()
  1443. {
  1444. mxUtils.setOpacity(div, 100);
  1445. });
  1446. mxEvent.addListener(div, 'mouseleave', function()
  1447. {
  1448. ui.hideShapePicker();
  1449. });
  1450. }
  1451. }), Editor.shapePickerHoverDelay);
  1452. }));
  1453. this.hoverIcons.addListener('blur', mxUtils.bind(this, function(sender, evt)
  1454. {
  1455. if (thread != null)
  1456. {
  1457. window.clearTimeout(thread);
  1458. }
  1459. }));
  1460. }
  1461. };
  1462. /**
  1463. * Creates a temporary graph instance for rendering off-screen content.
  1464. */
  1465. EditorUi.prototype.centerShapePicker = function(div, rect, x, y, dir)
  1466. {
  1467. if (dir == mxConstants.DIRECTION_EAST || dir == mxConstants.DIRECTION_WEST)
  1468. {
  1469. div.style.width = '40px';
  1470. }
  1471. var r2 = div.getBoundingClientRect();
  1472. if (dir == mxConstants.DIRECTION_NORTH)
  1473. {
  1474. x -= r2.width / 2 - 10;
  1475. y -= r2.height + 6;
  1476. }
  1477. else if (dir == mxConstants.DIRECTION_SOUTH)
  1478. {
  1479. x -= r2.width / 2 - 10;
  1480. y += rect.height + 6;
  1481. }
  1482. else if (dir == mxConstants.DIRECTION_WEST)
  1483. {
  1484. x -= r2.width + 6;
  1485. y -= r2.height / 2 - 10;
  1486. }
  1487. else if (dir == mxConstants.DIRECTION_EAST)
  1488. {
  1489. x += rect.width + 6;
  1490. y -= r2.height / 2 - 10;
  1491. }
  1492. div.style.left = x + 'px';
  1493. div.style.top = y + 'px';
  1494. };
  1495. /**
  1496. * Creates a temporary graph instance for rendering off-screen content.
  1497. */
  1498. EditorUi.prototype.showShapePicker = function(x, y, source, callback, direction, hovering,
  1499. getInsertLocationFn, showEdges, startEditing)
  1500. {
  1501. var div = null;
  1502. if (!this.editor.graph.freehand.isDrawing())
  1503. {
  1504. showEdges = showEdges || source == null;
  1505. div = this.createShapePicker(x, y, source, callback, direction, mxUtils.bind(this, function()
  1506. {
  1507. this.hideShapePicker();
  1508. }), this.getCellsForShapePicker(source, hovering, showEdges), hovering,
  1509. getInsertLocationFn, showEdges, startEditing);
  1510. if (div != null)
  1511. {
  1512. if (this.hoverIcons != null && !hovering)
  1513. {
  1514. this.hoverIcons.reset();
  1515. }
  1516. var graph = this.editor.graph;
  1517. graph.popupMenuHandler.hideMenu();
  1518. graph.tooltipHandler.hideTooltip();
  1519. this.hideCurrentMenu();
  1520. this.hideShapePicker();
  1521. this.shapePickerCallback = callback;
  1522. this.shapePicker = div;
  1523. }
  1524. }
  1525. return div;
  1526. };
  1527. /**
  1528. * Creates a temporary graph instance for rendering off-screen content.
  1529. */
  1530. EditorUi.prototype.createShapePicker = function(x, y, source, callback, direction,
  1531. afterClick, cells, hovering, getInsertLocationFn, showEdges, startEditing)
  1532. {
  1533. startEditing = (startEditing != null) ? startEditing : true;
  1534. var graph = this.editor.graph;
  1535. var div = null;
  1536. getInsertLocationFn = (getInsertLocationFn != null) ? getInsertLocationFn : function(cells)
  1537. {
  1538. var cell = cells[0];
  1539. var w = 0;
  1540. var h = 0;
  1541. var geo = cell.geometry;
  1542. if (geo != null)
  1543. {
  1544. if (graph.model.isEdge(cell))
  1545. {
  1546. var pt = geo.getTerminalPoint(false);
  1547. geo = new mxRectangle(0, 0, pt.x, pt.y);
  1548. }
  1549. w = geo.width / 2;
  1550. h = geo.height / 2;
  1551. }
  1552. return new mxPoint(graph.snap(Math.round(x / graph.view.scale) - graph.view.translate.x - w),
  1553. graph.snap(Math.round(y / graph.view.scale) - graph.view.translate.y - h));
  1554. };
  1555. if (cells != null && cells.length > 0)
  1556. {
  1557. var ui = this;
  1558. var graph = this.editor.graph;
  1559. div = document.createElement('div');
  1560. var sourceState = graph.view.getState(source);
  1561. var style = (source != null && (sourceState == null ||
  1562. !graph.isTransparentState(sourceState))) ?
  1563. graph.copyStyle(source) : null;
  1564. // Do not place entry under pointer for touch devices
  1565. var w = (cells.length < 6) ? cells.length * 35 : 140;
  1566. div.className = 'geToolbarContainer geSidebarContainer geShapePicker';
  1567. div.setAttribute('title', mxResources.get('sidebarTooltip'));
  1568. div.style.left = x + 'px';
  1569. div.style.top = y + 'px';
  1570. div.style.width = w + 'px';
  1571. // Disables built-in pan and zoom on touch devices
  1572. if (mxClient.IS_POINTER)
  1573. {
  1574. div.style.touchAction = 'none';
  1575. }
  1576. if (!hovering)
  1577. {
  1578. mxUtils.setPrefixedStyle(div.style, 'transform', 'translate(-22px,-22px)');
  1579. }
  1580. if (graph.background != null && graph.background != mxConstants.NONE)
  1581. {
  1582. div.style.backgroundColor = graph.background;
  1583. }
  1584. graph.container.appendChild(div);
  1585. var addCell = mxUtils.bind(this, function(cell)
  1586. {
  1587. // Wrapper needed to catch events
  1588. var node = document.createElement('a');
  1589. node.className = 'geItem';
  1590. node.style.cssText = 'position:relative;display:inline-block;position:relative;' +
  1591. 'width:30px;height:30px;cursor:pointer;overflow:hidden;padding:1px';
  1592. div.appendChild(node);
  1593. if (style != null && urlParams['sketch'] != '1')
  1594. {
  1595. this.sidebar.graph.pasteStyle(style, [cell]);
  1596. }
  1597. else
  1598. {
  1599. this.sidebar.graph.pasteCellStyles([cell],
  1600. graph.currentVertexStyle,
  1601. graph.currentEdgeStyle);
  1602. }
  1603. var geo = cell.geometry;
  1604. if (graph.model.isEdge(cell))
  1605. {
  1606. var pt = geo.getTerminalPoint(false);
  1607. geo = new mxRectangle(0, 0, pt.x, pt.y);
  1608. }
  1609. if (geo != null)
  1610. {
  1611. node.appendChild(this.sidebar.createVertexTemplateFromCells([cell],
  1612. geo.width, geo.height, '', true, false, null, false,
  1613. mxUtils.bind(this, function(evt)
  1614. {
  1615. if (mxEvent.isShiftDown(evt) && (source != null ||
  1616. !graph.isSelectionEmpty()))
  1617. {
  1618. var temp = graph.getEditableCells((source != null) ?
  1619. [source] : graph.getSelectionCells());
  1620. graph.updateShapes(cell, temp);
  1621. }
  1622. else
  1623. {
  1624. var clone = graph.cloneCell(cell);
  1625. if (callback != null)
  1626. {
  1627. callback(clone);
  1628. }
  1629. else
  1630. {
  1631. var pt = getInsertLocationFn([clone]);
  1632. if (graph.model.isEdge(clone))
  1633. {
  1634. clone.geometry.translate(pt.x, pt.y);
  1635. }
  1636. else
  1637. {
  1638. clone.geometry.x = pt.x;
  1639. clone.geometry.y = pt.y;
  1640. }
  1641. graph.model.beginUpdate();
  1642. try
  1643. {
  1644. graph.addCell(clone);
  1645. if (graph.model.isVertex(clone) &&
  1646. graph.isAutoSizeCell(clone))
  1647. {
  1648. graph.updateCellSize(clone);
  1649. }
  1650. }
  1651. finally
  1652. {
  1653. graph.model.endUpdate();
  1654. }
  1655. graph.setSelectionCell(clone);
  1656. graph.scrollCellToVisible(clone);
  1657. if (startEditing)
  1658. {
  1659. graph.startEditing(clone);
  1660. }
  1661. if (ui.hoverIcons != null)
  1662. {
  1663. ui.hoverIcons.update(graph.view.getState(clone));
  1664. }
  1665. }
  1666. }
  1667. if (afterClick != null)
  1668. {
  1669. afterClick(evt);
  1670. }
  1671. mxEvent.consume(evt);
  1672. }), 25, 25, null, null, source));
  1673. }
  1674. });
  1675. for (var i = 0; i < (hovering ? Math.min(cells.length, 4) : cells.length); i++)
  1676. {
  1677. addCell(cells[i]);
  1678. }
  1679. var b = graph.container.scrollTop + graph.container.offsetHeight;
  1680. var dy = div.offsetTop + div.clientHeight - b;
  1681. if (dy > 0)
  1682. {
  1683. div.style.top = Math.max(graph.container.scrollTop + 22, y - dy) + 'px';
  1684. }
  1685. var r = graph.container.scrollLeft + graph.container.offsetWidth;
  1686. var dx = div.offsetLeft + div.clientWidth - r;
  1687. if (dx > 0)
  1688. {
  1689. div.style.left = Math.max(graph.container.scrollLeft + 22, x - dx) + 'px';
  1690. }
  1691. }
  1692. return div;
  1693. };
  1694. /**
  1695. * Creates a temporary graph instance for rendering off-screen content.
  1696. */
  1697. EditorUi.prototype.getCellsForShapePicker = function(cell, hovering, showEdges)
  1698. {
  1699. var graph = this.editor.graph;
  1700. var createVertex = mxUtils.bind(this, function(style, w, h, value)
  1701. {
  1702. return graph.createVertex(null, null, value || '', 0, 0, w || 120, h || 60, style, false);
  1703. });
  1704. var createEdge = mxUtils.bind(this, function(style, y, value)
  1705. {
  1706. var cell = new mxCell(value || '', new mxGeometry(0, 0, graph.defaultEdgeLength + 20, 0), style);
  1707. cell.geometry.setTerminalPoint(new mxPoint(0, 0), true);
  1708. cell.geometry.setTerminalPoint(new mxPoint(cell.geometry.width, (y != null) ? y : 0), false);
  1709. cell.geometry.points = (y != null) ? [new mxPoint(cell.geometry.width / 2, y)] : [];
  1710. cell.geometry.relative = true;
  1711. cell.edge = true;
  1712. return cell;
  1713. });
  1714. // Creates a clone of the source cell and moves it to the origin
  1715. if (cell != null)
  1716. {
  1717. try
  1718. {
  1719. cell = graph.cloneCell(cell);
  1720. if (graph.model.isVertex(cell) && cell.geometry != null)
  1721. {
  1722. cell.geometry.x = 0;
  1723. cell.geometry.y = 0;
  1724. }
  1725. }
  1726. catch (e)
  1727. {
  1728. cell = null;
  1729. }
  1730. }
  1731. if (cell == null)
  1732. {
  1733. cell = createVertex(graph.appendFontSize('text;html=1;align=center;verticalAlign=middle;resizable=0;' +
  1734. 'points=[];autosize=1;strokeColor=none;fillColor=none;', graph.vertexFontSize), 40, 20, 'Text');
  1735. if (graph.model.isVertex(cell) && graph.isAutoSizeCell(cell))
  1736. {
  1737. // Uses offscreen graph to bypass undo history
  1738. var tempGraph = Graph.createOffscreenGraph(graph.getStylesheet());
  1739. tempGraph.updateCellSize(cell);
  1740. }
  1741. }
  1742. var cells = [cell, createVertex('whiteSpace=wrap;html=1;'),
  1743. createVertex('ellipse;whiteSpace=wrap;html=1;', 80, 80),
  1744. createVertex('rhombus;whiteSpace=wrap;html=1;', 80, 80),
  1745. createVertex('rounded=1;whiteSpace=wrap;html=1;'),
  1746. createVertex('shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;'),
  1747. createVertex('shape=trapezoid;perimeter=trapezoidPerimeter;whiteSpace=wrap;html=1;fixedSize=1;', 120, 60),
  1748. createVertex('shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;', 120, 80),
  1749. createVertex('shape=step;perimeter=stepPerimeter;whiteSpace=wrap;html=1;fixedSize=1;', 120, 80),
  1750. createVertex('shape=process;whiteSpace=wrap;html=1;backgroundOutline=1;'),
  1751. createVertex('triangle;whiteSpace=wrap;html=1;', 60, 80),
  1752. createVertex('shape=document;whiteSpace=wrap;html=1;boundedLbl=1;', 120, 80),
  1753. createVertex('shape=tape;whiteSpace=wrap;html=1;', 120, 100),
  1754. createVertex('ellipse;shape=cloud;whiteSpace=wrap;html=1;', 120, 80),
  1755. createVertex('shape=singleArrow;whiteSpace=wrap;html=1;arrowWidth=0.4;arrowSize=0.4;', 80, 60),
  1756. createVertex('shape=waypoint;sketch=0;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;' +
  1757. 'rotatable=0;perimeter=centerPerimeter;snapToPoint=1;', 20, 20)];
  1758. if (showEdges)
  1759. {
  1760. cells = cells.concat([
  1761. createEdge('edgeStyle=none;orthogonalLoop=1;jettySize=auto;html=1;'),
  1762. createEdge('edgeStyle=none;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;startArrow=classic;endSize=8;startSize=8;'),
  1763. createEdge('edgeStyle=none;orthogonalLoop=1;jettySize=auto;html=1;shape=flexArrow;rounded=1;startSize=8;endSize=8;'),
  1764. createEdge('edgeStyle=segmentEdgeStyle;endArrow=classic;html=1;curved=0;rounded=0;endSize=8;startSize=8;sourcePerimeterSpacing=0;targetPerimeterSpacing=0;',
  1765. this.editor.graph.defaultEdgeLength / 2)
  1766. ]);
  1767. }
  1768. return cells;
  1769. };
  1770. /**
  1771. * Creates a temporary graph instance for rendering off-screen content.
  1772. */
  1773. EditorUi.prototype.isShapePickerVisible = function(cancel)
  1774. {
  1775. return this.shapePicker != null;
  1776. };
  1777. /**
  1778. * Creates a temporary graph instance for rendering off-screen content.
  1779. */
  1780. EditorUi.prototype.hideShapePicker = function(cancel)
  1781. {
  1782. if (this.shapePicker != null)
  1783. {
  1784. this.shapePicker.parentNode.removeChild(this.shapePicker);
  1785. this.shapePicker = null;
  1786. if (!cancel && this.shapePickerCallback != null)
  1787. {
  1788. this.shapePickerCallback();
  1789. }
  1790. this.shapePickerCallback = null;
  1791. }
  1792. };
  1793. /**
  1794. * Whether the default styles should be updated when styles are changed. Default is true.
  1795. */
  1796. EditorUi.prototype.isSpaceDown = function()
  1797. {
  1798. return this.spaceDown;
  1799. };
  1800. /**
  1801. * Whether the default styles should be updated when styles are changed. Default is true.
  1802. */
  1803. EditorUi.prototype.isShiftDown = function()
  1804. {
  1805. return this.shiftDown;
  1806. };
  1807. /**
  1808. * Returns true if the given event should start editing. This implementation returns true.
  1809. */
  1810. EditorUi.prototype.onKeyDown = function(evt)
  1811. {
  1812. var graph = this.editor.graph;
  1813. // Alt+tab for task switcher in Windows, ctrl+tab for tab control in Chrome
  1814. if (evt.which == 9 && graph.isEnabled() && !mxEvent.isControlDown(evt))
  1815. {
  1816. if (graph.isEditing())
  1817. {
  1818. if (mxEvent.isAltDown(evt))
  1819. {
  1820. graph.stopEditing(false);
  1821. }
  1822. else
  1823. {
  1824. try
  1825. {
  1826. var nesting = graph.cellEditor.isContentEditing() && graph.cellEditor.isTextSelected();
  1827. if (window.getSelection && graph.cellEditor.isContentEditing() &&
  1828. !nesting && !mxClient.IS_IE && !mxClient.IS_IE11)
  1829. {
  1830. var selection = window.getSelection();
  1831. var container = (selection.rangeCount > 0) ? selection.getRangeAt(0).commonAncestorContainer : null;
  1832. nesting = container != null && (container.nodeName == 'LI' || (container.parentNode != null &&
  1833. container.parentNode.nodeName == 'LI'));
  1834. }
  1835. if (nesting)
  1836. {
  1837. // (Shift+)tab indents/outdents with text selection or inside list elements
  1838. document.execCommand(mxEvent.isShiftDown(evt) ? 'outdent' : 'indent', false, null);
  1839. }
  1840. // Shift+tab applies value with cursor
  1841. else if (mxEvent.isShiftDown(evt))
  1842. {
  1843. graph.stopEditing(false);
  1844. }
  1845. else
  1846. {
  1847. // Inserts tab character
  1848. graph.cellEditor.insertTab(!graph.cellEditor.isContentEditing() ? 4 : null);
  1849. }
  1850. }
  1851. catch (e)
  1852. {
  1853. // ignore
  1854. }
  1855. }
  1856. }
  1857. else if (mxEvent.isAltDown(evt))
  1858. {
  1859. graph.selectParentCell();
  1860. }
  1861. else
  1862. {
  1863. graph.selectCell(!mxEvent.isShiftDown(evt));
  1864. }
  1865. mxEvent.consume(evt);
  1866. }
  1867. };
  1868. /**
  1869. * Returns true if the given event should start editing. This implementation returns true.
  1870. */
  1871. EditorUi.prototype.onKeyPress = function(evt)
  1872. {
  1873. var graph = this.editor.graph;
  1874. // KNOWN: Focus does not work if label is empty in quirks mode
  1875. if (this.isImmediateEditingEvent(evt) && !graph.isEditing() && !graph.isSelectionEmpty() && evt.which !== 0 &&
  1876. evt.which !== 27 && !mxEvent.isAltDown(evt) && !mxEvent.isControlDown(evt) && !mxEvent.isMetaDown(evt))
  1877. {
  1878. graph.escape();
  1879. graph.startEditing(null, String.fromCharCode(evt.which));
  1880. }
  1881. };
  1882. /**
  1883. * Returns true if the given event should start editing. This implementation returns true.
  1884. */
  1885. EditorUi.prototype.isImmediateEditingEvent = function(evt)
  1886. {
  1887. return true;
  1888. };
  1889. /**
  1890. * Updates the CSS for the given element to match the selection.
  1891. */
  1892. EditorUi.prototype.updateCssForMarker = function(markerDiv, prefix, shape, marker, fill)
  1893. {
  1894. markerDiv.style.display = 'inline-flex';
  1895. markerDiv.style.alignItems = 'center';
  1896. markerDiv.style.justifyContent = 'center';
  1897. markerDiv.innerText = '';
  1898. if (shape == 'flexArrow')
  1899. {
  1900. markerDiv.className = (marker != null && marker != mxConstants.NONE) ?
  1901. 'geSprite geSprite-' + prefix + 'blocktrans' : 'geSprite geSprite-noarrow';
  1902. }
  1903. else
  1904. {
  1905. var src = this.getImageForMarker(marker, fill);
  1906. if (src != null)
  1907. {
  1908. var img = document.createElement('img');
  1909. img.className = 'geAdaptiveAsset';
  1910. img.setAttribute('src', src);
  1911. markerDiv.className = '';
  1912. if (prefix == 'end')
  1913. {
  1914. mxUtils.setPrefixedStyle(img.style, 'transform', 'scaleX(-1)');
  1915. }
  1916. markerDiv.appendChild(img);
  1917. }
  1918. else
  1919. {
  1920. markerDiv.className = 'geSprite geSprite-noarrow';
  1921. markerDiv.innerHTML = mxUtils.htmlEntities(mxResources.get('none'));
  1922. markerDiv.style.backgroundImage = 'none';
  1923. markerDiv.style.fontSize = '11px';
  1924. markerDiv.style.filter = 'none';
  1925. }
  1926. }
  1927. };
  1928. /**
  1929. * Returns true if the given event should start editing. This implementation returns true.
  1930. */
  1931. EditorUi.prototype.getImageForMarker = function(marker, fill)
  1932. {
  1933. var result = null;
  1934. if (marker == mxConstants.ARROW_CLASSIC)
  1935. {
  1936. result = (fill != '1') ? Format.classicMarkerImage.src :
  1937. Format.classicFilledMarkerImage.src
  1938. }
  1939. else if (marker == mxConstants.ARROW_CLASSIC_THIN)
  1940. {
  1941. result = (fill != '1') ? Format.classicThinMarkerImage.src :
  1942. Format.openThinFilledMarkerImage.src;
  1943. }
  1944. else if (marker == mxConstants.ARROW_OPEN)
  1945. {
  1946. result = Format.openFilledMarkerImage.src;
  1947. }
  1948. else if (marker == mxConstants.ARROW_OPEN_THIN)
  1949. {
  1950. result = Format.openThinFilledMarkerImage.src;
  1951. }
  1952. else if (marker == mxConstants.ARROW_BLOCK)
  1953. {
  1954. result = (fill != '1') ? Format.blockMarkerImage.src :
  1955. Format.blockFilledMarkerImage.src;
  1956. }
  1957. else if (marker == mxConstants.ARROW_BLOCK_THIN)
  1958. {
  1959. result = (fill != '1') ? Format.blockThinMarkerImage.src :
  1960. Format.blockThinFilledMarkerImage.src;
  1961. }
  1962. else if (marker == mxConstants.ARROW_OVAL)
  1963. {
  1964. result = (fill != '1') ? Format.ovalMarkerImage.src :
  1965. Format.ovalFilledMarkerImage.src;
  1966. }
  1967. else if (marker == mxConstants.ARROW_DIAMOND)
  1968. {
  1969. result = (fill != '1') ? Format.diamondMarkerImage.src :
  1970. Format.diamondFilledMarkerImage.src;
  1971. }
  1972. else if (marker == mxConstants.ARROW_DIAMOND_THIN)
  1973. {
  1974. result = (fill != '1') ? Format.diamondThinMarkerImage.src :
  1975. Format.diamondThinFilledMarkerImage.src;
  1976. }
  1977. else if (marker == 'doubleBlock')
  1978. {
  1979. result = (fill != '1') ? Format.doubleBlockMarkerImage.src :
  1980. Format.doubleBlockFilledMarkerImage.src;
  1981. }
  1982. else if (marker == 'box')
  1983. {
  1984. result = Format.boxMarkerImage.src;
  1985. }
  1986. else if (marker == 'halfCircle')
  1987. {
  1988. result = Format.halfCircleMarkerImage.src;
  1989. }
  1990. else if (marker == 'openAsync')
  1991. {
  1992. result = Format.openAsyncFilledMarkerImage.src;
  1993. }
  1994. else if (marker == 'async')
  1995. {
  1996. result = (fill != '1') ? Format.asyncMarkerImage.src :
  1997. Format.asyncFilledMarkerImage.src;
  1998. }
  1999. else if (marker == 'dash')
  2000. {
  2001. result = Format.dashMarkerImage.src;
  2002. }
  2003. else if (marker == 'baseDash')
  2004. {
  2005. result = Format.baseDashMarkerImage.src;
  2006. }
  2007. else if (marker == 'cross')
  2008. {
  2009. result = Format.crossMarkerImage.src;
  2010. }
  2011. else if (marker == 'circle')
  2012. {
  2013. result = Format.circleMarkerImage.src;
  2014. }
  2015. else if (marker == 'circlePlus')
  2016. {
  2017. result = Format.circlePlusMarkerImage.src;
  2018. }
  2019. else if (marker == 'ERone')
  2020. {
  2021. result = Format.EROneMarkerImage.src;
  2022. }
  2023. else if (marker == 'ERmandOne')
  2024. {
  2025. result = Format.ERmandOneMarkerImage.src;
  2026. }
  2027. else if (marker == 'ERmany')
  2028. {
  2029. result = Format.ERmanyMarkerImage.src;
  2030. }
  2031. else if (marker == 'ERoneToMany')
  2032. {
  2033. result = Format.ERoneToManyMarkerImage.src;
  2034. }
  2035. else if (marker == 'ERzeroToOne')
  2036. {
  2037. result = Format.ERzeroToOneMarkerImage.src;
  2038. }
  2039. else if (marker == 'ERzeroToMany')
  2040. {
  2041. result = Format.ERzeroToManyMarkerImage.src;
  2042. }
  2043. return result;
  2044. };
  2045. /**
  2046. * Overridden in Menus.js
  2047. */
  2048. EditorUi.prototype.createMenus = function()
  2049. {
  2050. return null;
  2051. };
  2052. /**
  2053. * Hook for allowing selection and context menu for certain events.
  2054. */
  2055. EditorUi.prototype.updatePasteActionStates = function()
  2056. {
  2057. var graph = this.editor.graph;
  2058. var paste = this.actions.get('paste');
  2059. var pasteHere = this.actions.get('pasteHere');
  2060. paste.setEnabled(this.editor.graph.cellEditor.isContentEditing() ||
  2061. (((!mxClient.IS_FF && navigator.clipboard != null) || !mxClipboard.isEmpty()) &&
  2062. graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent())));
  2063. pasteHere.setEnabled(paste.isEnabled());
  2064. };
  2065. /**
  2066. * Hook for allowing selection and context menu for certain events.
  2067. */
  2068. EditorUi.prototype.initClipboard = function()
  2069. {
  2070. var ui = this;
  2071. var mxClipboardCut = mxClipboard.cut;
  2072. mxClipboard.cut = function(graph)
  2073. {
  2074. if (graph.cellEditor.isContentEditing())
  2075. {
  2076. document.execCommand('cut', false, null);
  2077. }
  2078. else
  2079. {
  2080. mxClipboardCut.apply(this, arguments);
  2081. }
  2082. ui.updatePasteActionStates();
  2083. };
  2084. var mxClipboardCopy = mxClipboard.copy;
  2085. mxClipboard.copy = function(graph)
  2086. {
  2087. var result = null;
  2088. if (graph.cellEditor.isContentEditing())
  2089. {
  2090. document.execCommand('copy', false, null);
  2091. }
  2092. else
  2093. {
  2094. result = result || graph.getSelectionCells();
  2095. result = graph.getExportableCells(graph.model.getTopmostCells(result));
  2096. var cloneMap = new Object();
  2097. var lookup = graph.createCellLookup(result);
  2098. var clones = graph.cloneCells(result, null, cloneMap);
  2099. // Uses temporary model to force new IDs to be assigned
  2100. // to avoid having to carry over the mapping from object
  2101. // ID to cell ID to the paste operation
  2102. var model = new mxGraphModel();
  2103. var parent = model.getChildAt(model.getRoot(), 0);
  2104. for (var i = 0; i < clones.length; i++)
  2105. {
  2106. model.add(parent, clones[i]);
  2107. // Checks for orphaned relative children and makes absolute
  2108. var state = graph.view.getState(result[i]);
  2109. if (state != null)
  2110. {
  2111. var geo = graph.getCellGeometry(clones[i]);
  2112. if (geo != null && geo.relative && !model.isEdge(result[i]) &&
  2113. lookup[mxObjectIdentity.get(model.getParent(result[i]))] == null)
  2114. {
  2115. geo.offset = null;
  2116. geo.relative = false;
  2117. geo.x = state.x / state.view.scale - state.view.translate.x;
  2118. geo.y = state.y / state.view.scale - state.view.translate.y;
  2119. }
  2120. }
  2121. }
  2122. graph.updateCustomLinks(graph.createCellMapping(cloneMap, lookup), clones);
  2123. mxClipboard.insertCount = 1;
  2124. mxClipboard.setCells(clones);
  2125. }
  2126. ui.updatePasteActionStates();
  2127. return result;
  2128. };
  2129. var mxClipboardPaste = mxClipboard.paste;
  2130. mxClipboard.paste = function(graph)
  2131. {
  2132. var result = null;
  2133. if (graph.cellEditor.isContentEditing())
  2134. {
  2135. document.execCommand('paste', false, null);
  2136. }
  2137. else
  2138. {
  2139. result = mxClipboardPaste.apply(this, arguments);
  2140. }
  2141. ui.updatePasteActionStates();
  2142. return result;
  2143. };
  2144. // Overrides cell editor to update paste action state
  2145. var cellEditorStartEditing = this.editor.graph.cellEditor.startEditing;
  2146. this.editor.graph.cellEditor.startEditing = function()
  2147. {
  2148. cellEditorStartEditing.apply(this, arguments);
  2149. ui.updatePasteActionStates();
  2150. };
  2151. var cellEditorStopEditing = this.editor.graph.cellEditor.stopEditing;
  2152. this.editor.graph.cellEditor.stopEditing = function(cell, trigger)
  2153. {
  2154. cellEditorStopEditing.apply(this, arguments);
  2155. ui.updatePasteActionStates();
  2156. };
  2157. this.updatePasteActionStates();
  2158. };
  2159. /**
  2160. * Delay between zoom steps when not using preview.
  2161. */
  2162. EditorUi.prototype.lazyZoomDelay = 20;
  2163. /**
  2164. * Delay before update of DOM when using preview.
  2165. */
  2166. EditorUi.prototype.wheelZoomDelay = 500;
  2167. /**
  2168. * Delay before update of DOM when using preview.
  2169. */
  2170. EditorUi.prototype.buttonZoomDelay = 600;
  2171. /**
  2172. * Initializes the infinite canvas.
  2173. */
  2174. EditorUi.prototype.initCanvas = function()
  2175. {
  2176. // Initial page layout view, scrollBuffer and timer-based scrolling
  2177. var graph = this.editor.graph;
  2178. graph.timerAutoScroll = true;
  2179. /**
  2180. * Returns the padding for pages in page view with scrollbars.
  2181. */
  2182. graph.getPagePadding = function()
  2183. {
  2184. return new mxPoint(Math.max(0, Math.round((graph.container.offsetWidth - 34) / graph.view.scale)),
  2185. Math.max(0, Math.round((graph.container.offsetHeight - 34) / graph.view.scale)));
  2186. };
  2187. // Fits the number of background pages to the graph
  2188. graph.view.getBackgroundPageBounds = function()
  2189. {
  2190. var layout = this.graph.getPageLayout();
  2191. var page = this.graph.getPageSize();
  2192. return new mxRectangle(this.scale * (this.translate.x + layout.x * page.width),
  2193. this.scale * (this.translate.y + layout.y * page.height),
  2194. this.scale * layout.width * page.width,
  2195. this.scale * layout.height * page.height);
  2196. };
  2197. graph.getPreferredPageSize = function(bounds, width, height)
  2198. {
  2199. var pages = this.getPageLayout();
  2200. var size = this.getPageSize();
  2201. return new mxRectangle(0, 0, pages.width * size.width, pages.height * size.height);
  2202. };
  2203. // Scales pages/graph to fit available size
  2204. var resize = null;
  2205. var ui = this;
  2206. if (this.editor.isChromelessView())
  2207. {
  2208. resize = mxUtils.bind(this, function(autoscale, maxScale, cx, cy)
  2209. {
  2210. if (graph.container != null && !graph.isViewer())
  2211. {
  2212. cx = (cx != null) ? cx : 0;
  2213. cy = (cy != null) ? cy : 0;
  2214. var bds = (graph.pageVisible) ?
  2215. graph.view.getBackgroundPageBounds() :
  2216. graph.getGraphBounds();
  2217. var scroll = mxUtils.hasScrollbars(graph.container);
  2218. var tr = graph.view.translate;
  2219. var s = graph.view.scale;
  2220. // Normalizes the bounds
  2221. var b = mxRectangle.fromRectangle(bds);
  2222. b.x = b.x / s - tr.x;
  2223. b.y = b.y / s - tr.y;
  2224. b.width /= s;
  2225. b.height /= s;
  2226. var st = graph.container.scrollTop;
  2227. var sl = graph.container.scrollLeft;
  2228. var sb = (document.documentMode >= 8) ? 20 : 14;
  2229. if (document.documentMode == 8 || document.documentMode == 9)
  2230. {
  2231. sb += 3;
  2232. }
  2233. var cw = graph.container.offsetWidth - sb;
  2234. var ch = graph.container.offsetHeight - sb;
  2235. var ns = (autoscale) ? Math.max(0.3, Math.min(maxScale || 1, cw / b.width)) : s;
  2236. var dx = ((cw - ns * b.width) / 2) / ns;
  2237. var dy = (this.lightboxVerticalDivider == 0) ? 0 : ((ch - ns * b.height) / this.lightboxVerticalDivider) / ns;
  2238. if (scroll)
  2239. {
  2240. dx = Math.max(dx, 0);
  2241. dy = Math.max(dy, 0);
  2242. }
  2243. if (scroll || bds.width < cw || bds.height < ch)
  2244. {
  2245. graph.view.scaleAndTranslate(ns, Math.floor(dx - b.x), Math.floor(dy - b.y));
  2246. graph.container.scrollTop = st * ns / s;
  2247. graph.container.scrollLeft = sl * ns / s;
  2248. }
  2249. else if (cx != 0 || cy != 0)
  2250. {
  2251. var t = graph.view.translate;
  2252. graph.view.setTranslate(Math.floor(t.x + cx / s), Math.floor(t.y + cy / s));
  2253. }
  2254. }
  2255. });
  2256. // Hack to make function available to subclassers
  2257. this.chromelessResize = resize;
  2258. // Hook for subclassers for override
  2259. this.chromelessWindowResize = mxUtils.bind(this, function()
  2260. {
  2261. this.chromelessResize(false);
  2262. });
  2263. // Removable resize listener
  2264. var autoscaleResize = mxUtils.bind(this, function()
  2265. {
  2266. this.chromelessWindowResize(false);
  2267. });
  2268. mxEvent.addListener(window, 'resize', autoscaleResize);
  2269. this.destroyFunctions.push(function()
  2270. {
  2271. mxEvent.removeListener(window, 'resize', autoscaleResize);
  2272. });
  2273. this.editor.addListener('resetGraphView', mxUtils.bind(this, function()
  2274. {
  2275. this.chromelessResize(true);
  2276. }));
  2277. this.actions.get('zoomIn').funct = mxUtils.bind(this, function(evt)
  2278. {
  2279. graph.zoomIn();
  2280. this.chromelessResize(false);
  2281. });
  2282. this.actions.get('zoomOut').funct = mxUtils.bind(this, function(evt)
  2283. {
  2284. graph.zoomOut();
  2285. this.chromelessResize(false);
  2286. });
  2287. // Creates toolbar for viewer - do not use CSS here
  2288. // as this may be used in a viewer that has no CSS
  2289. if (urlParams['toolbar'] != '0')
  2290. {
  2291. var toolbarConfig = JSON.parse(decodeURIComponent(urlParams['toolbar-config'] || '{}'));
  2292. this.chromelessToolbar = document.createElement('div');
  2293. this.chromelessToolbar.style.position = 'fixed';
  2294. this.chromelessToolbar.style.overflow = 'hidden';
  2295. this.chromelessToolbar.style.boxSizing = 'border-box';
  2296. this.chromelessToolbar.style.whiteSpace = 'nowrap';
  2297. this.chromelessToolbar.style.padding = '10px 10px 8px 10px';
  2298. this.chromelessToolbar.style.left = (graph.isViewer()) ? '0' : '50%';
  2299. if (!mxClient.IS_IE && !mxClient.IS_IE11)
  2300. {
  2301. this.chromelessToolbar.style.backgroundColor = '#000000';
  2302. }
  2303. else
  2304. {
  2305. this.chromelessToolbar.style.backgroundColor = '#ffffff';
  2306. this.chromelessToolbar.style.border = '3px solid black';
  2307. }
  2308. mxUtils.setPrefixedStyle(this.chromelessToolbar.style, 'borderRadius', '16px');
  2309. mxUtils.setPrefixedStyle(this.chromelessToolbar.style, 'transition', 'opacity 600ms ease-in-out');
  2310. var updateChromelessToolbarPosition = mxUtils.bind(this, function()
  2311. {
  2312. var css = mxUtils.getCurrentStyle(graph.container);
  2313. if (graph.isViewer())
  2314. {
  2315. this.chromelessToolbar.style.top = '0';
  2316. }
  2317. else
  2318. {
  2319. this.chromelessToolbar.style.bottom = ((css != null) ? parseInt(css['margin-bottom'] || 0) : 0) +
  2320. ((this.tabContainer != null) ? (20 + parseInt(this.tabContainer.style.height)) : 20) + 'px';
  2321. }
  2322. });
  2323. this.editor.addListener('resetGraphView', updateChromelessToolbarPosition);
  2324. updateChromelessToolbarPosition();
  2325. var btnCount = 0;
  2326. var addButton = mxUtils.bind(this, function(fn, imgSrc, tip)
  2327. {
  2328. btnCount++;
  2329. var a = document.createElement('span');
  2330. a.style.paddingLeft = '8px';
  2331. a.style.paddingRight = '8px';
  2332. a.style.cursor = 'pointer';
  2333. mxEvent.addListener(a, 'click', fn);
  2334. if (tip != null)
  2335. {
  2336. a.setAttribute('title', tip);
  2337. }
  2338. var img = document.createElement('img');
  2339. img.setAttribute('border', '0');
  2340. img.setAttribute('src', imgSrc);
  2341. img.style.width = '36px';
  2342. img.style.filter = 'invert(100%)';
  2343. a.appendChild(img);
  2344. this.chromelessToolbar.appendChild(a);
  2345. return a;
  2346. });
  2347. if (toolbarConfig.backBtn != null)
  2348. {
  2349. var backUrl = Graph.sanitizeLink(toolbarConfig.backBtn.url);
  2350. if (backUrl != null)
  2351. {
  2352. addButton(mxUtils.bind(this, function(evt)
  2353. {
  2354. window.location.href = backUrl;
  2355. mxEvent.consume(evt);
  2356. }), Editor.backImage, mxResources.get('back', null, 'Back'));
  2357. }
  2358. }
  2359. if (this.isPagesEnabled())
  2360. {
  2361. var prevButton = addButton(mxUtils.bind(this, function(evt)
  2362. {
  2363. this.actions.get('previousPage').funct();
  2364. mxEvent.consume(evt);
  2365. }), Editor.previousImage, mxResources.get('previousPage'));
  2366. var pageInfo = document.createElement('div');
  2367. pageInfo.style.fontFamily = Editor.defaultHtmlFont;
  2368. pageInfo.style.display = 'inline-block';
  2369. pageInfo.style.verticalAlign = 'top';
  2370. pageInfo.style.fontWeight = 'bold';
  2371. pageInfo.style.marginTop = '8px';
  2372. pageInfo.style.fontSize = '14px';
  2373. pageInfo.style.cursor = 'default';
  2374. if (!mxClient.IS_IE && !mxClient.IS_IE11)
  2375. {
  2376. pageInfo.style.color = '#ffffff';
  2377. }
  2378. else
  2379. {
  2380. pageInfo.style.color = '#000000';
  2381. }
  2382. this.chromelessToolbar.appendChild(pageInfo);
  2383. var nextButton = addButton(mxUtils.bind(this, function(evt)
  2384. {
  2385. this.actions.get('nextPage').funct();
  2386. mxEvent.consume(evt);
  2387. }), Editor.nextImage, mxResources.get('nextPage'));
  2388. var updatePageInfo = mxUtils.bind(this, function()
  2389. {
  2390. if (this.pages != null && this.pages.length > 1 && this.currentPage != null)
  2391. {
  2392. pageInfo.innerText = '';
  2393. var index = mxUtils.indexOf(this.pages, this.currentPage);
  2394. mxUtils.write(pageInfo, (index + 1) + ' / ' + this.pages.length);
  2395. pageInfo.setAttribute('title', mxResources.get('currentPage') + ': ' +
  2396. this.currentPage.getName());
  2397. prevButton.setAttribute('title', mxResources.get('previousPage') + ': ' +
  2398. this.pages[mxUtils.mod(index - 1, this.pages.length)].getName());
  2399. nextButton.setAttribute('title', mxResources.get('nextPage') + ': ' +
  2400. this.pages[mxUtils.mod(index + 1, this.pages.length)].getName());
  2401. }
  2402. });
  2403. prevButton.style.paddingLeft = '0px';
  2404. prevButton.style.paddingRight = '4px';
  2405. nextButton.style.paddingLeft = '4px';
  2406. nextButton.style.paddingRight = '0px';
  2407. var updatePageButtons = mxUtils.bind(this, function()
  2408. {
  2409. if (this.pages != null && this.pages.length > 1 && this.currentPage != null)
  2410. {
  2411. nextButton.style.display = '';
  2412. prevButton.style.display = '';
  2413. pageInfo.style.display = 'inline-block';
  2414. }
  2415. else
  2416. {
  2417. nextButton.style.display = 'none';
  2418. prevButton.style.display = 'none';
  2419. pageInfo.style.display = 'none';
  2420. }
  2421. updatePageInfo();
  2422. });
  2423. if (this.menus != null)
  2424. {
  2425. var pagesMenu = this.menus.get('pages');
  2426. if (pagesMenu != null)
  2427. {
  2428. mxEvent.addListener(pageInfo, 'click', mxUtils.bind(this, function(evt)
  2429. {
  2430. var menu = new mxPopupMenu(pagesMenu.funct);
  2431. menu.div.className += ' geMenubarMenu';
  2432. menu.smartSeparators = true;
  2433. menu.showDisabled = true;
  2434. menu.autoExpand = true;
  2435. // Disables autoexpand and destroys menu when hidden
  2436. menu.hideMenu = mxUtils.bind(this, function()
  2437. {
  2438. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  2439. menu.destroy();
  2440. });
  2441. var offset = mxUtils.getOffset(pageInfo);
  2442. menu.popup(offset.x, offset.y + pageInfo.offsetHeight, null, evt);
  2443. mxEvent.addListener(menu.div, 'mouseleave', mxUtils.bind(this, function()
  2444. {
  2445. menu.hideMenu();
  2446. }));
  2447. }));
  2448. }
  2449. }
  2450. this.editor.addListener('resetGraphView', updatePageButtons);
  2451. this.editor.addListener('pageSelected', updatePageInfo);
  2452. }
  2453. addButton(mxUtils.bind(this, function(evt)
  2454. {
  2455. this.actions.get('zoomOut').funct();
  2456. mxEvent.consume(evt);
  2457. }), Editor.zoomOutImage, mxResources.get('zoomOut') + ' (Alt+Mousewheel)');
  2458. addButton(mxUtils.bind(this, function(evt)
  2459. {
  2460. this.actions.get('zoomIn').funct();
  2461. mxEvent.consume(evt);
  2462. }), Editor.zoomInImage, mxResources.get('zoomIn') + ' (Alt+Mousewheel)');
  2463. addButton(mxUtils.bind(this, function(evt)
  2464. {
  2465. if (graph.isLightboxView())
  2466. {
  2467. if (graph.view.scale == 1)
  2468. {
  2469. this.lightboxFit();
  2470. }
  2471. else
  2472. {
  2473. graph.zoomTo(1);
  2474. }
  2475. this.chromelessResize(false);
  2476. }
  2477. else
  2478. {
  2479. this.chromelessResize(true);
  2480. }
  2481. mxEvent.consume(evt);
  2482. }), Editor.zoomFitImage, mxResources.get('fit'));
  2483. // Changes toolbar opacity on hover
  2484. var fadeThread = null;
  2485. var fadeThread2 = null;
  2486. var fadeOut = mxUtils.bind(this, function(delay)
  2487. {
  2488. if (fadeThread != null)
  2489. {
  2490. window.clearTimeout(fadeThread);
  2491. fadeThread = null;
  2492. }
  2493. if (fadeThread2 != null)
  2494. {
  2495. window.clearTimeout(fadeThread2);
  2496. fadeThread2 = null;
  2497. }
  2498. fadeThread = window.setTimeout(mxUtils.bind(this, function()
  2499. {
  2500. mxUtils.setOpacity(this.chromelessToolbar, 0);
  2501. fadeThread = null;
  2502. fadeThread2 = window.setTimeout(mxUtils.bind(this, function()
  2503. {
  2504. this.chromelessToolbar.style.display = 'none';
  2505. fadeThread2 = null;
  2506. }), 600);
  2507. }), delay || 200);
  2508. });
  2509. var fadeIn = mxUtils.bind(this, function(opacity)
  2510. {
  2511. if (fadeThread != null)
  2512. {
  2513. window.clearTimeout(fadeThread);
  2514. fadeThread = null;
  2515. }
  2516. if (fadeThread2 != null)
  2517. {
  2518. window.clearTimeout(fadeThread2);
  2519. fadeThread2 = null;
  2520. }
  2521. this.chromelessToolbar.style.display = '';
  2522. mxUtils.setOpacity(this.chromelessToolbar, opacity || 30);
  2523. });
  2524. if (urlParams['layers'] == '1')
  2525. {
  2526. this.layersDialog = null;
  2527. var layersButton = addButton(mxUtils.bind(this, function(evt)
  2528. {
  2529. if (this.layersDialog != null)
  2530. {
  2531. this.layersDialog.parentNode.removeChild(this.layersDialog);
  2532. this.layersDialog = null;
  2533. }
  2534. else
  2535. {
  2536. this.layersDialog = graph.createLayersDialog(mxUtils.bind(this, function()
  2537. {
  2538. if (this.chromelessResize)
  2539. {
  2540. this.chromelessResize();
  2541. }
  2542. }), true);
  2543. mxEvent.addListener(this.layersDialog, 'mouseleave', mxUtils.bind(this, function()
  2544. {
  2545. this.layersDialog.parentNode.removeChild(this.layersDialog);
  2546. this.layersDialog = null;
  2547. }));
  2548. var r = layersButton.getBoundingClientRect();
  2549. mxUtils.setPrefixedStyle(this.layersDialog.style, 'borderRadius', '5px');
  2550. this.layersDialog.style.position = 'fixed';
  2551. this.layersDialog.style.fontFamily = Editor.defaultHtmlFont;
  2552. this.layersDialog.style.width = '160px';
  2553. this.layersDialog.style.padding = '4px 2px 4px 2px';
  2554. this.layersDialog.style.left = r.left + 'px';
  2555. this.layersDialog.style.bottom = parseInt(this.chromelessToolbar.style.bottom) +
  2556. this.chromelessToolbar.offsetHeight + 4 + 'px';
  2557. if (!mxClient.IS_IE && !mxClient.IS_IE11)
  2558. {
  2559. this.layersDialog.style.backgroundColor = '#000000';
  2560. this.layersDialog.style.color = '#ffffff';
  2561. mxUtils.setOpacity(this.layersDialog, 80);
  2562. }
  2563. else
  2564. {
  2565. this.layersDialog.style.backgroundColor = '#ffffff';
  2566. this.layersDialog.style.border = '2px solid black';
  2567. this.layersDialog.style.color = '#000000';
  2568. }
  2569. // Puts the dialog on top of the container z-index
  2570. var style = mxUtils.getCurrentStyle(this.editor.graph.container);
  2571. this.layersDialog.style.zIndex = style.zIndex;
  2572. document.body.appendChild(this.layersDialog);
  2573. this.editor.fireEvent(new mxEventObject('layersDialogShown'));
  2574. }
  2575. mxEvent.consume(evt);
  2576. }), Editor.layersImage, mxResources.get('layers'));
  2577. // Shows/hides layers button depending on content
  2578. var model = graph.getModel();
  2579. model.addListener(mxEvent.CHANGE, function()
  2580. {
  2581. layersButton.style.display = (model.getChildCount(model.root) > 1) ? '' : 'none';
  2582. });
  2583. }
  2584. if (urlParams['openInSameWin'] != '1' || navigator.standalone)
  2585. {
  2586. this.addChromelessToolbarItems(addButton);
  2587. }
  2588. if (this.editor.editButtonLink != null || this.editor.editButtonFunc != null)
  2589. {
  2590. addButton(mxUtils.bind(this, function(evt)
  2591. {
  2592. if (this.editor.editButtonFunc != null)
  2593. {
  2594. this.editor.editButtonFunc();
  2595. }
  2596. else if (this.editor.editButtonLink == '_blank')
  2597. {
  2598. this.editor.editAsNew(this.getEditBlankXml());
  2599. }
  2600. else
  2601. {
  2602. graph.openLink(this.editor.editButtonLink, 'editWindow');
  2603. }
  2604. mxEvent.consume(evt);
  2605. }), Editor.editImage, mxResources.get('edit'));
  2606. }
  2607. if (this.lightboxToolbarActions != null)
  2608. {
  2609. for (var i = 0; i < this.lightboxToolbarActions.length; i++)
  2610. {
  2611. var lbAction = this.lightboxToolbarActions[i];
  2612. lbAction.elem = addButton(lbAction.fn, lbAction.icon, lbAction.tooltip);
  2613. }
  2614. }
  2615. if (toolbarConfig.refreshBtn != null)
  2616. {
  2617. var refreshUrl = (toolbarConfig.refreshBtn.url == null) ? null :
  2618. Graph.sanitizeLink(toolbarConfig.refreshBtn.url);
  2619. addButton(mxUtils.bind(this, function(evt)
  2620. {
  2621. if (refreshUrl != null)
  2622. {
  2623. window.location.href = refreshUrl;
  2624. }
  2625. else
  2626. {
  2627. window.location.reload();
  2628. }
  2629. mxEvent.consume(evt);
  2630. }), Editor.refreshImage, mxResources.get('refresh', null, 'Refresh'));
  2631. }
  2632. if (toolbarConfig.fullscreenBtn != null && window.self !== window.top)
  2633. {
  2634. addButton(mxUtils.bind(this, function(evt)
  2635. {
  2636. if (toolbarConfig.fullscreenBtn.url)
  2637. {
  2638. graph.openLink(toolbarConfig.fullscreenBtn.url);
  2639. }
  2640. else
  2641. {
  2642. graph.openLink(window.location.href);
  2643. }
  2644. mxEvent.consume(evt);
  2645. }), Editor.fullscreenImage, mxResources.get('openInNewWindow', null, 'Open in New Window'));
  2646. }
  2647. if (!toolbarConfig.noCloseBtn && ((toolbarConfig.closeBtn && window.self === window.top) ||
  2648. (graph.lightbox && (urlParams['close'] == '1' || this.container != document.body))))
  2649. {
  2650. addButton(mxUtils.bind(this, function(evt)
  2651. {
  2652. if (urlParams['close'] == '1' || toolbarConfig.closeBtn)
  2653. {
  2654. window.close();
  2655. }
  2656. else
  2657. {
  2658. this.destroy();
  2659. mxEvent.consume(evt);
  2660. }
  2661. }), Editor.closeImage, mxResources.get('close') + ' (Escape)');
  2662. }
  2663. // Initial state invisible
  2664. this.chromelessToolbar.style.display = 'none';
  2665. if (!graph.isViewer())
  2666. {
  2667. mxUtils.setPrefixedStyle(this.chromelessToolbar.style, 'transform', 'translate(-50%,0)');
  2668. }
  2669. graph.container.appendChild(this.chromelessToolbar);
  2670. mxEvent.addListener(graph.container, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', mxUtils.bind(this, function(evt)
  2671. {
  2672. if (!mxEvent.isTouchEvent(evt))
  2673. {
  2674. if (!mxEvent.isShiftDown(evt))
  2675. {
  2676. fadeIn(30);
  2677. }
  2678. fadeOut();
  2679. }
  2680. }));
  2681. mxEvent.addListener(this.chromelessToolbar, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', function(evt)
  2682. {
  2683. mxEvent.consume(evt);
  2684. });
  2685. mxEvent.addListener(this.chromelessToolbar, 'mouseenter', mxUtils.bind(this, function(evt)
  2686. {
  2687. graph.tooltipHandler.resetTimer();
  2688. graph.tooltipHandler.hideTooltip();
  2689. if (!mxEvent.isShiftDown(evt))
  2690. {
  2691. fadeIn(100);
  2692. }
  2693. else
  2694. {
  2695. fadeOut();
  2696. }
  2697. }));
  2698. mxEvent.addListener(this.chromelessToolbar, 'mousemove', mxUtils.bind(this, function(evt)
  2699. {
  2700. if (!mxEvent.isShiftDown(evt))
  2701. {
  2702. fadeIn(100);
  2703. }
  2704. else
  2705. {
  2706. fadeOut();
  2707. }
  2708. mxEvent.consume(evt);
  2709. }));
  2710. mxEvent.addListener(this.chromelessToolbar, 'mouseleave', mxUtils.bind(this, function(evt)
  2711. {
  2712. if (!mxEvent.isTouchEvent(evt))
  2713. {
  2714. fadeIn(30);
  2715. }
  2716. }));
  2717. // Shows/hides toolbar for touch devices
  2718. var tol = graph.getTolerance();
  2719. graph.addMouseListener(
  2720. {
  2721. startX: 0,
  2722. startY: 0,
  2723. scrollLeft: 0,
  2724. scrollTop: 0,
  2725. mouseDown: function(sender, me)
  2726. {
  2727. this.startX = me.getGraphX();
  2728. this.startY = me.getGraphY();
  2729. this.scrollLeft = graph.container.scrollLeft;
  2730. this.scrollTop = graph.container.scrollTop;
  2731. },
  2732. mouseMove: function(sender, me) {},
  2733. mouseUp: function(sender, me)
  2734. {
  2735. if (mxEvent.isTouchEvent(me.getEvent()))
  2736. {
  2737. if ((Math.abs(this.scrollLeft - graph.container.scrollLeft) < tol &&
  2738. Math.abs(this.scrollTop - graph.container.scrollTop) < tol) &&
  2739. (Math.abs(this.startX - me.getGraphX()) < tol &&
  2740. Math.abs(this.startY - me.getGraphY()) < tol))
  2741. {
  2742. if (parseFloat(ui.chromelessToolbar.style.opacity || 0) > 0)
  2743. {
  2744. fadeOut();
  2745. }
  2746. else
  2747. {
  2748. fadeIn(30);
  2749. }
  2750. }
  2751. }
  2752. }
  2753. });
  2754. } // end if toolbar
  2755. // Installs handling of highlight and handling links to relative links and anchors
  2756. if (!this.editor.editable)
  2757. {
  2758. this.addChromelessClickHandler();
  2759. }
  2760. }
  2761. else if (this.editor.extendCanvas)
  2762. {
  2763. /**
  2764. * Guesses autoTranslate to avoid another repaint (see below).
  2765. * Works if only the scale of the graph changes or if pages
  2766. * are visible and the visible pages do not change. Uses
  2767. * geometries to guess the bounding box of the graph.
  2768. */
  2769. var graphViewValidate = graph.view.validate;
  2770. var zero = new mxPoint();
  2771. var pageChanged = false;
  2772. var lastPage = null;
  2773. graph.view.validate = function()
  2774. {
  2775. if (graph.container != null &&
  2776. mxUtils.hasScrollbars(graph.container))
  2777. {
  2778. // Sets initial state after page changes
  2779. if (ui.currentPage != null &&
  2780. lastPage != ui.currentPage)
  2781. {
  2782. lastPage = ui.currentPage;
  2783. pageChanged = true;
  2784. // Sets initial translate based on geometries
  2785. // to avoid revalidation in sizeDidChange
  2786. var bbox = graph.getBoundingBoxFromGeometry(
  2787. graph.model.getCells(), true, null, true);
  2788. // Handles blank diagrams
  2789. if (bbox == null)
  2790. {
  2791. bbox = new mxRectangle(
  2792. graph.view.translate.x * graph.view.scale,
  2793. graph.view.translate.y * graph.view.scale);
  2794. }
  2795. var pageLayout = graph.getPageLayout(bbox, zero, 1);
  2796. var tr = graph.getDefaultTranslate(pageLayout);
  2797. this.x0 = pageLayout.x;
  2798. this.y0 = pageLayout.y;
  2799. if (tr.x != this.translate.x ||
  2800. tr.y != this.translate.y)
  2801. {
  2802. this.invalidate();
  2803. this.translate.x = tr.x;
  2804. this.translate.y = tr.y;
  2805. }
  2806. }
  2807. var pad = graph.getPagePadding();
  2808. var size = graph.getPageSize();
  2809. var tx = pad.x - (this.x0 || 0) * size.width;
  2810. var ty = pad.y - (this.y0 || 0) * size.height;
  2811. if (this.translate.x != tx || this.translate.y != ty)
  2812. {
  2813. this.invalidate();
  2814. this.translate.x = tx
  2815. this.translate.y = ty
  2816. }
  2817. }
  2818. graphViewValidate.apply(this, arguments);
  2819. };
  2820. if (!graph.isViewer())
  2821. {
  2822. var graphSizeDidChange = graph.sizeDidChange;
  2823. graph.sizeDidChange = function()
  2824. {
  2825. var skipScroll = pageChanged;
  2826. pageChanged = false;
  2827. if (this.container != null &&
  2828. mxUtils.hasScrollbars(this.container))
  2829. {
  2830. this.updateMinimumSize();
  2831. if (!this.autoTranslate)
  2832. {
  2833. var pageLayout = this.getPageLayout();
  2834. var tr = this.getDefaultTranslate(pageLayout);
  2835. var tx = this.view.translate.x;
  2836. var ty = this.view.translate.y;
  2837. if (tr.x != tx || tr.y != ty)
  2838. {
  2839. this.view.x0 = pageLayout.x;
  2840. this.view.y0 = pageLayout.y;
  2841. // Requires full revalidation
  2842. this.autoTranslate = true;
  2843. this.view.setTranslate(tr.x, tr.y);
  2844. this.autoTranslate = false;
  2845. // Skipped if initial autoTranslate is wrong
  2846. if (!skipScroll)
  2847. {
  2848. this.container.scrollLeft += Math.round((tr.x - tx) * this.view.scale);
  2849. this.container.scrollTop += Math.round((tr.y - ty) * this.view.scale);
  2850. }
  2851. return;
  2852. }
  2853. }
  2854. graphSizeDidChange.apply(this, arguments);
  2855. }
  2856. else
  2857. {
  2858. // Fires event but does not invoke superclass
  2859. this.fireEvent(new mxEventObject(mxEvent.SIZE,
  2860. 'bounds', this.getGraphBounds()));
  2861. }
  2862. };
  2863. }
  2864. }
  2865. // Accumulates the zoom factor while the rendering is taking place
  2866. // so that not the complete sequence of zoom steps must be painted
  2867. var bgGroup = graph.view.getBackgroundPane();
  2868. var mainGroup = graph.view.getDrawPane();
  2869. graph.cumulativeZoomFactor = 1;
  2870. var updateZoomTimeout = null;
  2871. var cursorPosition = null;
  2872. var scrollPosition = null;
  2873. var forcedZoom = null;
  2874. var filter = null;
  2875. var mult = 20;
  2876. var scheduleZoom = function(delay)
  2877. {
  2878. if (updateZoomTimeout != null)
  2879. {
  2880. window.clearTimeout(updateZoomTimeout);
  2881. }
  2882. if (delay >= 0)
  2883. {
  2884. window.setTimeout(function()
  2885. {
  2886. if (!graph.isMouseDown || forcedZoom)
  2887. {
  2888. updateZoomTimeout = window.setTimeout(mxUtils.bind(this, function()
  2889. {
  2890. if (graph.isFastZoomEnabled())
  2891. {
  2892. // Transforms background page
  2893. if (graph.view.backgroundPageShape != null && graph.view.backgroundPageShape.node != null)
  2894. {
  2895. mxUtils.setPrefixedStyle(graph.view.backgroundPageShape.node.style, 'transform-origin', null);
  2896. mxUtils.setPrefixedStyle(graph.view.backgroundPageShape.node.style, 'transform', null);
  2897. }
  2898. // Transforms graph and background image
  2899. mainGroup.style.transformOrigin = '';
  2900. bgGroup.style.transformOrigin = '';
  2901. // Workaround for no reset of transform in Safari
  2902. if (mxClient.IS_SF)
  2903. {
  2904. mainGroup.style.transform = 'scale(1)';
  2905. bgGroup.style.transform = 'scale(1)';
  2906. window.setTimeout(function()
  2907. {
  2908. mainGroup.style.transform = '';
  2909. bgGroup.style.transform = '';
  2910. }, 0)
  2911. }
  2912. else
  2913. {
  2914. mainGroup.style.transform = '';
  2915. bgGroup.style.transform = '';
  2916. }
  2917. // Shows interactive elements
  2918. graph.view.getDecoratorPane().style.opacity = '';
  2919. graph.view.getOverlayPane().style.opacity = '';
  2920. }
  2921. var sp = new mxPoint(graph.container.scrollLeft, graph.container.scrollTop);
  2922. var offset = mxUtils.getOffset(graph.container);
  2923. var prev = graph.view.scale;
  2924. var dx = 0;
  2925. var dy = 0;
  2926. if (cursorPosition != null)
  2927. {
  2928. dx = graph.container.offsetWidth / 2 - cursorPosition.x + offset.x;
  2929. dy = graph.container.offsetHeight / 2 - cursorPosition.y + offset.y;
  2930. }
  2931. graph.zoom(graph.cumulativeZoomFactor, null,
  2932. graph.isFastZoomEnabled() ? mult : null);
  2933. var s = graph.view.scale;
  2934. if (s != prev)
  2935. {
  2936. if (scrollPosition != null)
  2937. {
  2938. dx += sp.x - scrollPosition.x;
  2939. dy += sp.y - scrollPosition.y;
  2940. }
  2941. if (resize != null)
  2942. {
  2943. ui.chromelessResize(false, null, dx * (graph.cumulativeZoomFactor - 1),
  2944. dy * (graph.cumulativeZoomFactor - 1));
  2945. }
  2946. if (mxUtils.hasScrollbars(graph.container) && (dx != 0 || dy != 0))
  2947. {
  2948. graph.container.scrollLeft -= dx * (graph.cumulativeZoomFactor - 1);
  2949. graph.container.scrollTop -= dy * (graph.cumulativeZoomFactor - 1);
  2950. }
  2951. }
  2952. if (filter != null)
  2953. {
  2954. mainGroup.setAttribute('filter', filter);
  2955. }
  2956. graph.cumulativeZoomFactor = 1;
  2957. updateZoomTimeout = null;
  2958. scrollPosition = null;
  2959. cursorPosition = null;
  2960. forcedZoom = null;
  2961. filter = null;
  2962. }), (delay != null) ? delay : ((graph.isFastZoomEnabled()) ? ui.wheelZoomDelay : ui.lazyZoomDelay));
  2963. }
  2964. }, 0);
  2965. }
  2966. };
  2967. graph.lazyZoom = function(zoomIn, ignoreCursorPosition, delay, factor)
  2968. {
  2969. factor = (factor != null) ? factor : this.zoomFactor;
  2970. // TODO: Fix ignored cursor position if scrollbars are disabled
  2971. ignoreCursorPosition = ignoreCursorPosition || !graph.scrollbars;
  2972. if (ignoreCursorPosition)
  2973. {
  2974. cursorPosition = new mxPoint(
  2975. graph.container.offsetLeft + graph.container.clientWidth / 2,
  2976. graph.container.offsetTop + graph.container.clientHeight / 2);
  2977. }
  2978. // Switches to 5% zoom steps below 15%
  2979. if (zoomIn)
  2980. {
  2981. if (this.view.scale * this.cumulativeZoomFactor <= 0.15)
  2982. {
  2983. this.cumulativeZoomFactor *= (this.view.scale + 0.05) / this.view.scale;
  2984. }
  2985. else
  2986. {
  2987. this.cumulativeZoomFactor *= factor;
  2988. this.cumulativeZoomFactor = Math.round(this.view.scale * this.cumulativeZoomFactor * 100) / 100 / this.view.scale;
  2989. }
  2990. }
  2991. else
  2992. {
  2993. if (this.view.scale * this.cumulativeZoomFactor <= 0.15)
  2994. {
  2995. this.cumulativeZoomFactor *= (this.view.scale - 0.05) / this.view.scale;
  2996. }
  2997. else
  2998. {
  2999. this.cumulativeZoomFactor /= factor;
  3000. this.cumulativeZoomFactor = Math.round(this.view.scale * this.cumulativeZoomFactor * 100) / 100 / this.view.scale;
  3001. }
  3002. }
  3003. this.cumulativeZoomFactor = Math.max(0.05, Math.min(this.view.scale * this.cumulativeZoomFactor, 160)) / this.view.scale;
  3004. if (graph.isFastZoomEnabled())
  3005. {
  3006. if (filter == null && mainGroup.getAttribute('filter') != '')
  3007. {
  3008. filter = mainGroup.getAttribute('filter');
  3009. mainGroup.removeAttribute('filter');
  3010. }
  3011. scrollPosition = new mxPoint(graph.container.scrollLeft, graph.container.scrollTop);
  3012. // Applies final rounding to preview
  3013. var f = Math.round((Math.round(this.view.scale * this.cumulativeZoomFactor *
  3014. 100) / 100) * mult) / (mult * this.view.scale);
  3015. var cx = (ignoreCursorPosition || cursorPosition == null) ?
  3016. graph.container.scrollLeft + graph.container.clientWidth / 2 :
  3017. cursorPosition.x + graph.container.scrollLeft - graph.container.offsetLeft;
  3018. var cy = (ignoreCursorPosition || cursorPosition == null) ?
  3019. graph.container.scrollTop + graph.container.clientHeight / 2 :
  3020. cursorPosition.y + graph.container.scrollTop - graph.container.offsetTop;
  3021. mainGroup.style.transformOrigin = cx + 'px ' + cy + 'px';
  3022. mainGroup.style.transform = 'scale(' + f + ')';
  3023. bgGroup.style.transformOrigin = cx + 'px ' + cy + 'px';
  3024. bgGroup.style.transform = 'scale(' + f + ')';
  3025. if (graph.view.backgroundPageShape != null && graph.view.backgroundPageShape.node != null)
  3026. {
  3027. var page = graph.view.backgroundPageShape.node;
  3028. mxUtils.setPrefixedStyle(page.style, 'transform-origin',
  3029. ((ignoreCursorPosition || cursorPosition == null) ?
  3030. ((graph.container.clientWidth / 2 + graph.container.scrollLeft -
  3031. page.offsetLeft) + 'px') : ((cursorPosition.x + graph.container.scrollLeft -
  3032. page.offsetLeft - graph.container.offsetLeft) + 'px')) + ' ' +
  3033. ((ignoreCursorPosition || cursorPosition == null) ?
  3034. ((graph.container.clientHeight / 2 + graph.container.scrollTop -
  3035. page.offsetTop) + 'px') : ((cursorPosition.y + graph.container.scrollTop -
  3036. page.offsetTop - graph.container.offsetTop) + 'px')));
  3037. mxUtils.setPrefixedStyle(page.style, 'transform', 'scale(' + f + ')');
  3038. }
  3039. else
  3040. {
  3041. graph.view.validateBackgroundStyles(f, cx, cy);
  3042. }
  3043. graph.view.getDecoratorPane().style.opacity = '0';
  3044. graph.view.getOverlayPane().style.opacity = '0';
  3045. if (ui.hoverIcons != null)
  3046. {
  3047. ui.hoverIcons.reset();
  3048. }
  3049. graph.fireEvent(new mxEventObject('zoomPreview', 'factor', f));
  3050. }
  3051. scheduleZoom(graph.isFastZoomEnabled() ? delay : 0);
  3052. };
  3053. // Holds back repaint until after mouse gestures
  3054. mxEvent.addGestureListeners(graph.container, function(evt)
  3055. {
  3056. if (updateZoomTimeout != null)
  3057. {
  3058. window.clearTimeout(updateZoomTimeout);
  3059. }
  3060. }, null, function(evt)
  3061. {
  3062. if (graph.cumulativeZoomFactor != 1)
  3063. {
  3064. scheduleZoom(0);
  3065. }
  3066. });
  3067. // Holds back repaint until scroll ends
  3068. mxEvent.addListener(graph.container, 'scroll', function(evt)
  3069. {
  3070. if (updateZoomTimeout != null && !graph.isMouseDown && graph.cumulativeZoomFactor != 1)
  3071. {
  3072. scheduleZoom(0);
  3073. }
  3074. });
  3075. mxEvent.addMouseWheelListener(mxUtils.bind(this, function(evt, up, force, cx, cy)
  3076. {
  3077. graph.fireEvent(new mxEventObject('wheel'));
  3078. if (this.dialogs == null || this.dialogs.length == 0)
  3079. {
  3080. // Scrolls with scrollbars turned off
  3081. if (!graph.scrollbars && !force && graph.isScrollWheelEvent(evt))
  3082. {
  3083. var t = graph.view.getTranslate();
  3084. var step = 40 / graph.view.scale;
  3085. if (!mxEvent.isShiftDown(evt))
  3086. {
  3087. graph.view.setTranslate(t.x, t.y + ((up) ? step : -step));
  3088. }
  3089. else
  3090. {
  3091. graph.view.setTranslate(t.x + ((up) ? -step : step), t.y);
  3092. }
  3093. }
  3094. else if (force || graph.isZoomWheelEvent(evt))
  3095. {
  3096. var source = mxEvent.getSource(evt);
  3097. while (source != null)
  3098. {
  3099. if (source == graph.container)
  3100. {
  3101. graph.tooltipHandler.hideTooltip();
  3102. cursorPosition = (cx != null && cy!= null) ? new mxPoint(cx, cy) :
  3103. new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  3104. forcedZoom = force;
  3105. var factor = graph.zoomFactor;
  3106. var delay = null;
  3107. // Slower zoom for pinch gesture on trackpad with max delta to
  3108. // filter out mouse wheel events in Brave browser for Windows
  3109. if (evt.ctrlKey && evt.deltaY != null && Math.abs(evt.deltaY) < 40 &&
  3110. Math.round(evt.deltaY) != evt.deltaY)
  3111. {
  3112. factor = 1 + (Math.abs(evt.deltaY) / 20) * (factor - 1);
  3113. }
  3114. // Slower zoom for pinch gesture on touch screens
  3115. else if (evt.movementY != null && evt.type == 'pointermove')
  3116. {
  3117. factor = 1 + (Math.max(1, Math.abs(evt.movementY)) / 20) * (factor - 1);
  3118. delay = -1;
  3119. }
  3120. graph.lazyZoom(up, null, delay, factor);
  3121. mxEvent.consume(evt);
  3122. return false;
  3123. }
  3124. source = source.parentNode;
  3125. }
  3126. }
  3127. }
  3128. }), graph.container);
  3129. // Uses fast zoom for pinch gestures on iOS
  3130. graph.panningHandler.zoomGraph = function(evt)
  3131. {
  3132. graph.cumulativeZoomFactor = evt.scale;
  3133. graph.lazyZoom(evt.scale > 0, true);
  3134. mxEvent.consume(evt);
  3135. };
  3136. };
  3137. /**
  3138. * Creates a temporary graph instance for rendering off-screen content.
  3139. */
  3140. EditorUi.prototype.addChromelessToolbarItems = function(addButton)
  3141. {
  3142. addButton(mxUtils.bind(this, function(evt)
  3143. {
  3144. this.actions.get('print').funct();
  3145. mxEvent.consume(evt);
  3146. }), Editor.printImage, mxResources.get('print'));
  3147. };
  3148. /**
  3149. * Creates a temporary graph instance for rendering off-screen content.
  3150. */
  3151. EditorUi.prototype.isPagesEnabled = function()
  3152. {
  3153. return this.editor.editable || urlParams['hide-pages'] != '1';
  3154. };
  3155. /**
  3156. * Creates a temporary graph instance for rendering off-screen content.
  3157. */
  3158. EditorUi.prototype.createTemporaryGraph = function(stylesheet)
  3159. {
  3160. return Graph.createOffscreenGraph(stylesheet);
  3161. };
  3162. /**
  3163. *
  3164. */
  3165. EditorUi.prototype.addChromelessClickHandler = function()
  3166. {
  3167. var hl = urlParams['highlight'];
  3168. // Adds leading # for highlight color code
  3169. if (hl != null && hl.length > 0)
  3170. {
  3171. hl = '#' + hl;
  3172. }
  3173. this.editor.graph.addClickHandler(hl);
  3174. };
  3175. /**
  3176. *
  3177. */
  3178. EditorUi.prototype.toggleFormatPanel = function(visible)
  3179. {
  3180. visible = (visible != null) ? visible : this.formatWidth == 0;
  3181. if (this.format != null)
  3182. {
  3183. this.formatWidth = (visible) ? 240 : 0;
  3184. this.formatContainer.style.width = this.formatWidth + 'px';
  3185. this.refresh();
  3186. this.format.refresh();
  3187. this.fireEvent(new mxEventObject('formatWidthChanged'));
  3188. }
  3189. };
  3190. /**
  3191. *
  3192. */
  3193. EditorUi.prototype.isFormatPanelVisible = function()
  3194. {
  3195. return this.formatWidth > 0;
  3196. };
  3197. /**
  3198. * Adds support for placeholders in labels.
  3199. */
  3200. EditorUi.prototype.lightboxFit = function(maxHeight)
  3201. {
  3202. if (this.isDiagramEmpty())
  3203. {
  3204. this.editor.graph.view.setScale(1);
  3205. }
  3206. else
  3207. {
  3208. var p = urlParams['border'];
  3209. var border = 60;
  3210. if (p != null)
  3211. {
  3212. border = parseInt(p);
  3213. }
  3214. // LATER: Use initial graph bounds to avoid rounding errors
  3215. this.editor.graph.maxFitScale = this.lightboxMaxFitScale;
  3216. this.editor.graph.fit(border, null, null, null, null, null, maxHeight);
  3217. this.editor.graph.maxFitScale = null;
  3218. }
  3219. };
  3220. /**
  3221. * Translates this point by the given vector.
  3222. *
  3223. * @param {number} dx X-coordinate of the translation.
  3224. * @param {number} dy Y-coordinate of the translation.
  3225. */
  3226. EditorUi.prototype.isDiagramEmpty = function()
  3227. {
  3228. var model = this.editor.graph.getModel();
  3229. return model.getChildCount(model.root) == 1 && model.getChildCount(model.getChildAt(model.root, 0)) == 0;
  3230. };
  3231. /**
  3232. * Hook for allowing selection and context menu for certain events.
  3233. */
  3234. EditorUi.prototype.isSelectionAllowed = function(evt)
  3235. {
  3236. return mxEvent.getSource(evt).nodeName == 'SELECT' || (mxEvent.getSource(evt).nodeName == 'INPUT' &&
  3237. mxUtils.isAncestorNode(this.formatContainer, mxEvent.getSource(evt)));
  3238. };
  3239. /**
  3240. * Installs dialog if browser window is closed without saving
  3241. * This must be disabled during save and image export.
  3242. */
  3243. EditorUi.prototype.addBeforeUnloadListener = function()
  3244. {
  3245. // Installs dialog if browser window is closed without saving
  3246. // This must be disabled during save and image export
  3247. window.onbeforeunload = mxUtils.bind(this, function()
  3248. {
  3249. if (!this.editor.isChromelessView())
  3250. {
  3251. return this.onBeforeUnload();
  3252. }
  3253. });
  3254. };
  3255. /**
  3256. * Sets the onbeforeunload for the application
  3257. */
  3258. EditorUi.prototype.onBeforeUnload = function()
  3259. {
  3260. if (this.editor.modified)
  3261. {
  3262. return mxResources.get('allChangesLost');
  3263. }
  3264. };
  3265. /**
  3266. * Opens the current diagram via the window.opener if one exists.
  3267. */
  3268. EditorUi.prototype.open = function()
  3269. {
  3270. // Cross-domain window access is not allowed in FF, so if we
  3271. // were opened from another domain then this will fail.
  3272. try
  3273. {
  3274. if (window.opener != null && window.opener.openFile != null)
  3275. {
  3276. window.opener.openFile.setConsumer(mxUtils.bind(this, function(xml, filename)
  3277. {
  3278. try
  3279. {
  3280. var doc = mxUtils.parseXml(xml);
  3281. this.editor.setGraphXml(doc.documentElement);
  3282. this.editor.setModified(false);
  3283. this.editor.undoManager.clear();
  3284. if (filename != null)
  3285. {
  3286. this.editor.setFilename(filename);
  3287. this.updateDocumentTitle();
  3288. }
  3289. return;
  3290. }
  3291. catch (e)
  3292. {
  3293. mxUtils.alert(mxResources.get('invalidOrMissingFile') + ': ' + e.message);
  3294. }
  3295. }));
  3296. }
  3297. }
  3298. catch(e)
  3299. {
  3300. // ignore
  3301. }
  3302. // Fires as the last step if no file was loaded
  3303. this.editor.graph.view.validate();
  3304. // Required only in special cases where an initial file is opened
  3305. // and the minimumGraphSize changes and CSS must be updated.
  3306. this.editor.graph.sizeDidChange();
  3307. this.editor.fireEvent(new mxEventObject('resetGraphView'));
  3308. };
  3309. /**
  3310. *
  3311. */
  3312. EditorUi.prototype.showPrintDialog = function(title, fn)
  3313. {
  3314. this.showDialog(new PrintDialog(this, title, fn).container, 300, 180, true, true);
  3315. };
  3316. /**
  3317. * Shows the given popup menu.
  3318. */
  3319. EditorUi.prototype.showPopupMenu = function(fn, x, y, evt)
  3320. {
  3321. this.editor.graph.popupMenuHandler.hideMenu();
  3322. var menu = new mxPopupMenu(fn);
  3323. menu.div.className += ' geMenubarMenu';
  3324. menu.smartSeparators = true;
  3325. menu.showDisabled = true;
  3326. menu.autoExpand = true;
  3327. // Disables autoexpand and destroys menu when hidden
  3328. menu.hideMenu = mxUtils.bind(this, function()
  3329. {
  3330. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  3331. menu.destroy();
  3332. });
  3333. menu.popup(x, y, null, evt);
  3334. // Allows hiding by clicking on document
  3335. this.setCurrentMenu(menu);
  3336. };
  3337. /**
  3338. * Sets the current menu and element.
  3339. */
  3340. EditorUi.prototype.setCurrentMenu = function(menu, elt)
  3341. {
  3342. this.currentMenuElt = elt;
  3343. this.currentMenu = menu;
  3344. this.hideShapePicker();
  3345. };
  3346. /**
  3347. * Resets the current menu and element.
  3348. */
  3349. EditorUi.prototype.resetCurrentMenu = function()
  3350. {
  3351. this.currentMenuElt = null;
  3352. this.currentMenu = null;
  3353. };
  3354. /**
  3355. * Hides and destroys the current menu.
  3356. */
  3357. EditorUi.prototype.hideCurrentMenu = function()
  3358. {
  3359. if (this.currentMenu != null)
  3360. {
  3361. this.currentMenu.hideMenu();
  3362. this.resetCurrentMenu();
  3363. }
  3364. };
  3365. /**
  3366. * Updates the document title.
  3367. */
  3368. EditorUi.prototype.updateDocumentTitle = function()
  3369. {
  3370. var title = this.editor.getOrCreateFilename();
  3371. if (this.editor.appName != null)
  3372. {
  3373. title += ' - ' + this.editor.appName;
  3374. }
  3375. document.title = title;
  3376. };
  3377. /**
  3378. * Updates the document title.
  3379. */
  3380. EditorUi.prototype.createHoverIcons = function()
  3381. {
  3382. return new HoverIcons(this.editor.graph);
  3383. };
  3384. /**
  3385. * Returns the URL for a copy of this editor with no state.
  3386. */
  3387. EditorUi.prototype.redo = function()
  3388. {
  3389. try
  3390. {
  3391. var graph = this.editor.graph;
  3392. if (graph.isEditing())
  3393. {
  3394. document.execCommand('redo', false, null);
  3395. }
  3396. else
  3397. {
  3398. this.editor.undoManager.redo();
  3399. }
  3400. }
  3401. catch (e)
  3402. {
  3403. // ignore all errors
  3404. }
  3405. };
  3406. /**
  3407. * Returns the URL for a copy of this editor with no state.
  3408. */
  3409. EditorUi.prototype.undo = function()
  3410. {
  3411. try
  3412. {
  3413. var graph = this.editor.graph;
  3414. if (graph.isEditing())
  3415. {
  3416. // Stops editing and executes undo on graph if native undo
  3417. // does not affect current editing value
  3418. var value = graph.cellEditor.textarea.innerHTML;
  3419. document.execCommand('undo', false, null);
  3420. if (value == graph.cellEditor.textarea.innerHTML)
  3421. {
  3422. graph.stopEditing(true);
  3423. this.editor.undoManager.undo();
  3424. }
  3425. }
  3426. else
  3427. {
  3428. this.editor.undoManager.undo();
  3429. }
  3430. }
  3431. catch (e)
  3432. {
  3433. // ignore all errors
  3434. }
  3435. };
  3436. /**
  3437. * Returns the URL for a copy of this editor with no state.
  3438. */
  3439. EditorUi.prototype.canRedo = function()
  3440. {
  3441. return this.editor.graph.isEditing() || this.editor.undoManager.canRedo();
  3442. };
  3443. /**
  3444. * Returns the URL for a copy of this editor with no state.
  3445. */
  3446. EditorUi.prototype.canUndo = function()
  3447. {
  3448. return this.editor.graph.isEditing() || this.editor.undoManager.canUndo();
  3449. };
  3450. /**
  3451. * Returns the current page and XML for the given page.
  3452. */
  3453. EditorUi.prototype.getDiagramSnapshot = function()
  3454. {
  3455. return {node: this.editor.getGraphXml()};
  3456. };
  3457. /**
  3458. *
  3459. */
  3460. EditorUi.prototype.updateDiagramData = function(snapshot, node)
  3461. {
  3462. this.replaceDiagramData(xUtils.getXml(node));
  3463. };
  3464. /**
  3465. *
  3466. */
  3467. EditorUi.prototype.replaceDiagramData = function(data)
  3468. {
  3469. this.editor.graph.model.beginUpdate();
  3470. try
  3471. {
  3472. this.editor.setGraphXml(mxUtils.parseXml(data).documentElement);
  3473. }
  3474. finally
  3475. {
  3476. this.editor.graph.model.endUpdate();
  3477. }
  3478. };
  3479. /**
  3480. *
  3481. */
  3482. EditorUi.prototype.getEditBlankXml = function()
  3483. {
  3484. return mxUtils.getXml(this.editor.getGraphXml());
  3485. };
  3486. /**
  3487. * Returns the URL for a copy of this editor with no state.
  3488. */
  3489. EditorUi.prototype.getUrl = function(pathname)
  3490. {
  3491. var href = (pathname != null) ? pathname : window.location.pathname;
  3492. var parms = (href.indexOf('?') > 0) ? 1 : 0;
  3493. // Removes template URL parameter for new blank diagram
  3494. for (var key in urlParams)
  3495. {
  3496. if (parms == 0)
  3497. {
  3498. href += '?';
  3499. }
  3500. else
  3501. {
  3502. href += '&';
  3503. }
  3504. href += key + '=' + urlParams[key];
  3505. parms++;
  3506. }
  3507. return href;
  3508. };
  3509. /**
  3510. * Specifies if the graph has scrollbars.
  3511. */
  3512. EditorUi.prototype.setScrollbars = function(value)
  3513. {
  3514. var graph = this.editor.graph;
  3515. var prev = graph.container.style.overflow;
  3516. graph.scrollbars = value;
  3517. this.editor.updateGraphComponents();
  3518. if (prev != graph.container.style.overflow)
  3519. {
  3520. graph.container.scrollTop = 0;
  3521. graph.container.scrollLeft = 0;
  3522. graph.view.scaleAndTranslate(1, 0, 0);
  3523. this.resetScrollbars();
  3524. }
  3525. this.fireEvent(new mxEventObject('scrollbarsChanged'));
  3526. };
  3527. /**
  3528. * Function: fitDiagramToWindow
  3529. *
  3530. * Zooms the diagram to fit into the window.
  3531. */
  3532. EditorUi.prototype.fitDiagramToWindow = function()
  3533. {
  3534. var graph = this.editor.graph;
  3535. var bounds = (graph.isSelectionEmpty()) ?
  3536. mxRectangle.fromRectangle(graph.getGraphBounds()) :
  3537. graph.getBoundingBox(graph.getSelectionCells())
  3538. var t = graph.view.translate;
  3539. var s = graph.view.scale;
  3540. bounds.x = bounds.x / s - t.x;
  3541. bounds.y = bounds.y / s - t.y;
  3542. bounds.width /= s;
  3543. bounds.height /= s;
  3544. if (graph.backgroundImage != null)
  3545. {
  3546. bounds.add(new mxRectangle(0, 0,
  3547. graph.backgroundImage.width,
  3548. graph.backgroundImage.height));
  3549. }
  3550. if (bounds.width == 0 || bounds.height == 0)
  3551. {
  3552. graph.zoomTo(1);
  3553. this.resetScrollbars();
  3554. }
  3555. else
  3556. {
  3557. var b = Editor.fitWindowBorders;
  3558. if (b != null)
  3559. {
  3560. bounds.x -= b.x;
  3561. bounds.y -= b.y;
  3562. bounds.width += b.width + b.x;
  3563. bounds.height += b.height + b.y;
  3564. }
  3565. graph.fitWindow(bounds);
  3566. }
  3567. };
  3568. /**
  3569. * Returns true if the graph has scrollbars.
  3570. */
  3571. EditorUi.prototype.hasScrollbars = function()
  3572. {
  3573. return this.editor.graph.scrollbars;
  3574. };
  3575. /**
  3576. * Resets the state of the scrollbars.
  3577. */
  3578. EditorUi.prototype.resetScrollbars = function()
  3579. {
  3580. var graph = this.editor.graph;
  3581. var c = graph.container;
  3582. if (!this.editor.extendCanvas)
  3583. {
  3584. c.scrollTop = 0;
  3585. c.scrollLeft = 0;
  3586. if (!mxUtils.hasScrollbars(c))
  3587. {
  3588. graph.view.setTranslate(0, 0);
  3589. }
  3590. }
  3591. else if (!this.editor.isChromelessView())
  3592. {
  3593. if (mxUtils.hasScrollbars(c))
  3594. {
  3595. if (graph.pageVisible)
  3596. {
  3597. var pad = graph.getPagePadding();
  3598. c.scrollTop = Math.floor(pad.y - this.editor.initialTopSpacing) - 1;
  3599. c.scrollLeft = Math.floor(Math.min(pad.x,
  3600. (c.scrollWidth - c.clientWidth) / 2)) - 1;
  3601. // Scrolls graph to visible area
  3602. var bounds = graph.getGraphBounds();
  3603. if (bounds.width > 0 && bounds.height > 0)
  3604. {
  3605. if (bounds.x > c.scrollLeft + c.clientWidth * 0.9)
  3606. {
  3607. c.scrollLeft = Math.min(bounds.x + bounds.width - c.clientWidth, bounds.x - 10);
  3608. }
  3609. if (bounds.y > c.scrollTop + c.clientHeight * 0.9)
  3610. {
  3611. c.scrollTop = Math.min(bounds.y + bounds.height - c.clientHeight, bounds.y - 10);
  3612. }
  3613. }
  3614. }
  3615. else
  3616. {
  3617. var bounds = graph.getGraphBounds();
  3618. if (bounds.width == 0 && bounds.height == 0)
  3619. {
  3620. c.scrollLeft = (c.scrollWidth - c.clientWidth) / 2;
  3621. c.scrollTop = (c.scrollHeight - c.clientHeight) / 2;
  3622. }
  3623. else
  3624. {
  3625. var width = Math.max(bounds.width, graph.scrollTileSize.width * graph.view.scale);
  3626. var height = Math.max(bounds.height, graph.scrollTileSize.height * graph.view.scale);
  3627. c.scrollLeft = Math.floor(Math.max(0, bounds.x - Math.max(0, (c.clientWidth - width) / 2)));
  3628. c.scrollTop = Math.floor(Math.max(0, bounds.y - Math.max(20, (c.clientHeight - height) / 4)));
  3629. }
  3630. }
  3631. }
  3632. else
  3633. {
  3634. var b = mxRectangle.fromRectangle((graph.pageVisible) ?
  3635. graph.view.getBackgroundPageBounds() :
  3636. graph.getGraphBounds())
  3637. var tr = graph.view.translate;
  3638. var s = graph.view.scale;
  3639. b.x = b.x / s - tr.x;
  3640. b.y = b.y / s - tr.y;
  3641. b.width /= s;
  3642. b.height /= s;
  3643. var dy = (graph.pageVisible) ? 0 : Math.max(0, (c.clientHeight - b.height) / 4);
  3644. graph.view.setTranslate(Math.floor(Math.max(0,
  3645. (c.clientWidth - b.width) / 2) - b.x + 2),
  3646. Math.floor(dy - b.y + 1));
  3647. }
  3648. }
  3649. };
  3650. /**
  3651. * Loads the stylesheet for this graph.
  3652. */
  3653. EditorUi.prototype.setPageVisible = function(value)
  3654. {
  3655. var graph = this.editor.graph;
  3656. var hasScrollbars = mxUtils.hasScrollbars(graph.container);
  3657. var tx = 0;
  3658. var ty = 0;
  3659. if (hasScrollbars)
  3660. {
  3661. tx = graph.view.translate.x * graph.view.scale - graph.container.scrollLeft;
  3662. ty = graph.view.translate.y * graph.view.scale - graph.container.scrollTop;
  3663. }
  3664. graph.pageVisible = value;
  3665. graph.pageBreaksVisible = value;
  3666. graph.preferPageSize = value;
  3667. graph.view.validateBackground();
  3668. // Workaround for possible handle offset
  3669. if (hasScrollbars)
  3670. {
  3671. var cells = graph.getSelectionCells();
  3672. graph.clearSelection();
  3673. graph.setSelectionCells(cells);
  3674. }
  3675. // Calls updatePageBreaks
  3676. graph.sizeDidChange();
  3677. if (hasScrollbars)
  3678. {
  3679. graph.container.scrollLeft = graph.view.translate.x * graph.view.scale - tx;
  3680. graph.container.scrollTop = graph.view.translate.y * graph.view.scale - ty;
  3681. }
  3682. graph.defaultPageVisible = value;
  3683. this.fireEvent(new mxEventObject('pageViewChanged'));
  3684. };
  3685. /**
  3686. * Loads the stylesheet for this graph.
  3687. */
  3688. EditorUi.prototype.installResizeHandler = function(dialog, resizable, destroy)
  3689. {
  3690. if (resizable)
  3691. {
  3692. dialog.window.setSize = function(w, h)
  3693. {
  3694. if (!this.minimized)
  3695. {
  3696. var iw = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth;
  3697. var ih = window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight;
  3698. w = Math.min(w, iw - this.getX());
  3699. h = Math.min(h, ih - this.getY());
  3700. }
  3701. mxWindow.prototype.setSize.apply(this, arguments);
  3702. };
  3703. }
  3704. dialog.window.setLocation = function(x, y)
  3705. {
  3706. var iw = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth;
  3707. var ih = window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight;
  3708. var w = parseInt(this.div.style.width);
  3709. var h = parseInt(this.div.style.height);
  3710. x = Math.max(0, Math.min(x, iw - w));
  3711. y = Math.max(0, Math.min(y, ih - h));
  3712. if (this.getX() != x || this.getY() != y)
  3713. {
  3714. mxWindow.prototype.setLocation.apply(this, arguments);
  3715. }
  3716. if (resizable && !this.minimized)
  3717. {
  3718. this.setSize(w, h);
  3719. }
  3720. };
  3721. var resizeListener = mxUtils.bind(this, function()
  3722. {
  3723. var x = dialog.window.getX();
  3724. var y = dialog.window.getY();
  3725. dialog.window.setLocation(x, y);
  3726. });
  3727. mxEvent.addListener(window, 'resize', resizeListener);
  3728. dialog.destroy = function()
  3729. {
  3730. mxEvent.removeListener(window, 'resize', resizeListener);
  3731. dialog.window.destroy();
  3732. if (destroy != null)
  3733. {
  3734. destroy();
  3735. }
  3736. }
  3737. };
  3738. /**
  3739. * Class: ChangeGridColor
  3740. *
  3741. * Undoable change to grid color.
  3742. */
  3743. function ChangeGridColor(ui, color)
  3744. {
  3745. this.ui = ui;
  3746. this.color = color;
  3747. };
  3748. /**
  3749. * Executes selection of a new page.
  3750. */
  3751. ChangeGridColor.prototype.execute = function()
  3752. {
  3753. var temp = this.ui.editor.graph.view.gridColor;
  3754. this.ui.setGridColor(this.color);
  3755. this.color = temp;
  3756. };
  3757. // Registers codec for ChangePageSetup
  3758. (function()
  3759. {
  3760. var codec = new mxObjectCodec(new ChangeGridColor(), ['ui']);
  3761. mxCodecRegistry.register(codec);
  3762. })();
  3763. /**
  3764. * Change types
  3765. */
  3766. function ChangePageSetup(ui, color, image, format, pageScale)
  3767. {
  3768. this.ui = ui;
  3769. this.color = color;
  3770. this.previousColor = color;
  3771. this.image = image;
  3772. this.previousImage = image;
  3773. this.format = format;
  3774. this.previousFormat = format;
  3775. this.pageScale = pageScale;
  3776. this.previousPageScale = pageScale;
  3777. // Needed since null are valid values for color and image
  3778. this.ignoreColor = false;
  3779. this.ignoreImage = false;
  3780. }
  3781. /**
  3782. * Implementation of the undoable page rename.
  3783. */
  3784. ChangePageSetup.prototype.execute = function()
  3785. {
  3786. var graph = this.ui.editor.graph;
  3787. if (!this.ignoreColor)
  3788. {
  3789. this.color = this.previousColor;
  3790. var tmp = graph.background;
  3791. this.ui.setBackgroundColor(this.previousColor);
  3792. this.previousColor = tmp;
  3793. }
  3794. if (!this.ignoreImage)
  3795. {
  3796. this.image = this.previousImage;
  3797. var tmp = graph.backgroundImage;
  3798. var img = this.previousImage;
  3799. if (img != null && Graph.isPageLink(img.src))
  3800. {
  3801. img = this.ui.createImageForPageLink(img.src, this.ui.currentPage);
  3802. }
  3803. this.ui.setBackgroundImage(img);
  3804. this.previousImage = tmp;
  3805. }
  3806. if (this.previousFormat != null)
  3807. {
  3808. this.format = this.previousFormat;
  3809. var tmp = graph.pageFormat;
  3810. if (this.previousFormat.width != tmp.width ||
  3811. this.previousFormat.height != tmp.height)
  3812. {
  3813. this.ui.setPageFormat(this.previousFormat);
  3814. this.previousFormat = tmp;
  3815. }
  3816. }
  3817. if (this.foldingEnabled != null && this.foldingEnabled != this.ui.editor.graph.foldingEnabled)
  3818. {
  3819. this.ui.setFoldingEnabled(this.foldingEnabled);
  3820. this.foldingEnabled = !this.foldingEnabled;
  3821. }
  3822. if (this.previousPageScale != null)
  3823. {
  3824. var currentPageScale = this.ui.editor.graph.pageScale;
  3825. if (this.previousPageScale != currentPageScale)
  3826. {
  3827. this.ui.setPageScale(this.previousPageScale);
  3828. this.previousPageScale = currentPageScale;
  3829. }
  3830. }
  3831. };
  3832. // Registers codec for ChangePageSetup
  3833. (function()
  3834. {
  3835. var codec = new mxObjectCodec(new ChangePageSetup(), ['ui', 'previousColor', 'previousImage', 'previousFormat', 'previousPageScale']);
  3836. codec.afterDecode = function(dec, node, obj)
  3837. {
  3838. obj.previousColor = obj.color;
  3839. obj.previousImage = obj.image;
  3840. obj.previousFormat = obj.format;
  3841. obj.previousPageScale = obj.pageScale;
  3842. if (obj.foldingEnabled != null)
  3843. {
  3844. obj.foldingEnabled = !obj.foldingEnabled;
  3845. }
  3846. return obj;
  3847. };
  3848. mxCodecRegistry.register(codec);
  3849. })();
  3850. /**
  3851. * Loads the stylesheet for this graph.
  3852. */
  3853. EditorUi.prototype.setBackgroundColor = function(value)
  3854. {
  3855. this.editor.graph.background = value;
  3856. this.editor.graph.view.validateBackground();
  3857. this.fireEvent(new mxEventObject('backgroundColorChanged'));
  3858. };
  3859. /**
  3860. * Loads the stylesheet for this graph.
  3861. */
  3862. EditorUi.prototype.setFoldingEnabled = function(value)
  3863. {
  3864. this.editor.graph.foldingEnabled = value;
  3865. this.editor.graph.view.revalidate();
  3866. this.fireEvent(new mxEventObject('foldingEnabledChanged'));
  3867. };
  3868. /**
  3869. * Loads the stylesheet for this graph.
  3870. */
  3871. EditorUi.prototype.setPageFormat = function(value, ignorePageVisible)
  3872. {
  3873. ignorePageVisible = (ignorePageVisible != null) ? ignorePageVisible : urlParams['sketch'] == '1';
  3874. this.editor.graph.pageFormat = value;
  3875. if (!ignorePageVisible)
  3876. {
  3877. if (!this.editor.graph.pageVisible)
  3878. {
  3879. this.actions.get('pageView').funct();
  3880. }
  3881. else
  3882. {
  3883. this.editor.graph.view.validateBackground();
  3884. this.editor.graph.sizeDidChange();
  3885. }
  3886. }
  3887. this.fireEvent(new mxEventObject('pageFormatChanged'));
  3888. };
  3889. /**
  3890. * Loads the stylesheet for this graph.
  3891. */
  3892. EditorUi.prototype.setPageScale = function(value)
  3893. {
  3894. this.editor.graph.pageScale = value;
  3895. if (!this.editor.graph.pageVisible)
  3896. {
  3897. this.actions.get('pageView').funct();
  3898. }
  3899. else
  3900. {
  3901. this.editor.graph.view.validateBackground();
  3902. this.editor.graph.sizeDidChange();
  3903. }
  3904. this.fireEvent(new mxEventObject('pageScaleChanged'));
  3905. };
  3906. /**
  3907. * Loads the stylesheet for this graph.
  3908. */
  3909. EditorUi.prototype.setGridColor = function(value)
  3910. {
  3911. this.editor.graph.view.gridColor = value;
  3912. this.editor.graph.view.validateBackground();
  3913. this.fireEvent(new mxEventObject('gridColorChanged'));
  3914. };
  3915. /**
  3916. * Updates the states of the given undo/redo items.
  3917. */
  3918. EditorUi.prototype.addUndoListener = function()
  3919. {
  3920. var undoMgr = this.editor.undoManager;
  3921. var undoListener = mxUtils.bind(this, function()
  3922. {
  3923. this.updateActionStates();
  3924. });
  3925. undoMgr.addListener(mxEvent.ADD, undoListener);
  3926. undoMgr.addListener(mxEvent.UNDO, undoListener);
  3927. undoMgr.addListener(mxEvent.REDO, undoListener);
  3928. undoMgr.addListener(mxEvent.CLEAR, undoListener);
  3929. // Overrides cell editor to update action states
  3930. var cellEditorStartEditing = this.editor.graph.cellEditor.startEditing;
  3931. this.editor.graph.cellEditor.startEditing = function()
  3932. {
  3933. cellEditorStartEditing.apply(this, arguments);
  3934. undoListener();
  3935. };
  3936. var cellEditorStopEditing = this.editor.graph.cellEditor.stopEditing;
  3937. this.editor.graph.cellEditor.stopEditing = function(cell, trigger)
  3938. {
  3939. cellEditorStopEditing.apply(this, arguments);
  3940. undoListener();
  3941. };
  3942. // Updates the button states once
  3943. undoListener();
  3944. };
  3945. /**
  3946. * Updates the states of the given toolbar items based on the selection.
  3947. */
  3948. EditorUi.prototype.updateActionStates = function()
  3949. {
  3950. var graph = this.editor.graph;
  3951. var ss = this.getSelectionState();
  3952. var unlocked = graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent());
  3953. var editable = !this.editor.chromeless || this.editor.editable;
  3954. // Updates action states
  3955. var actions = ['cut', 'copy', 'bold', 'italic', 'underline', 'delete', 'duplicate',
  3956. 'editStyle', 'editTooltip', 'editLink', 'backgroundColor', 'borderColor',
  3957. 'edit', 'toFront', 'toBack', 'solid', 'dashed', 'pasteSize',
  3958. 'dotted', 'fillColor', 'gradientColor', 'shadow', 'fontColor',
  3959. 'formattedText', 'rounded', 'toggleRounded', 'strokeColor',
  3960. 'sharp', 'snapToGrid'];
  3961. for (var i = 0; i < actions.length; i++)
  3962. {
  3963. this.actions.get(actions[i]).setEnabled(ss.cells.length > 0);
  3964. }
  3965. this.actions.get('grid').setEnabled(editable);
  3966. this.actions.get('undo').setEnabled(this.canUndo() && editable);
  3967. this.actions.get('redo').setEnabled(this.canRedo() && editable);
  3968. this.actions.get('swap').setEnabled(ss.cells.length == 2 && ss.vertices.length == 2);
  3969. this.actions.get('pasteSize').setEnabled(this.copiedSize != null && ss.vertices.length > 0);
  3970. this.actions.get('pasteData').setEnabled(this.copiedValue != null && ss.cells.length > 0);
  3971. this.actions.get('setAsDefaultStyle').setEnabled(graph.getSelectionCount() == 1);
  3972. this.actions.get('lockUnlock').setEnabled(!graph.isSelectionEmpty());
  3973. this.actions.get('bringForward').setEnabled(ss.cells.length == 1);
  3974. this.actions.get('sendBackward').setEnabled(ss.cells.length == 1);
  3975. this.actions.get('rotation').setEnabled(ss.vertices.length == 1);
  3976. this.actions.get('wordWrap').setEnabled(ss.vertices.length == 1);
  3977. this.actions.get('autosize').setEnabled(ss.vertices.length > 0);
  3978. this.actions.get('copySize').setEnabled(ss.vertices.length == 1);
  3979. this.actions.get('clearWaypoints').setEnabled(ss.connections);
  3980. this.actions.get('clearAnchors').setEnabled(ss.connections);
  3981. this.actions.get('curved').setEnabled(ss.edges.length > 0);
  3982. this.actions.get('turn').setEnabled(ss.cells.length > 0);
  3983. this.actions.get('group').setEnabled((ss.cells.length > 1 ||
  3984. (ss.vertices.length == 1 && graph.model.getChildCount(ss.cells[0]) == 0 &&
  3985. !graph.isContainer(ss.vertices[0]))));
  3986. this.actions.get('ungroup').setEnabled(!ss.row && !ss.cell && !ss.table &&
  3987. ss.vertices.length > 0 && (graph.isContainer(ss.vertices[0]) ||
  3988. graph.getModel().getChildCount(ss.vertices[0]) > 0));
  3989. this.actions.get('removeFromGroup').setEnabled(ss.cells.length == 1 &&
  3990. graph.getModel().isVertex(graph.getModel().getParent(ss.cells[0])));
  3991. this.actions.get('collapsible').setEnabled(ss.vertices.length == 1 &&
  3992. (graph.model.getChildCount(ss.vertices[0]) > 0 ||
  3993. graph.isContainer(ss.vertices[0])));
  3994. this.actions.get('exitGroup').setEnabled(graph.view.currentRoot != null);
  3995. this.actions.get('home').setEnabled(graph.view.currentRoot != null);
  3996. this.actions.get('enterGroup').setEnabled(ss.cells.length == 1 &&
  3997. graph.isValidRoot(ss.cells[0]));
  3998. this.actions.get('copyData').setEnabled(ss.cells.length == 1);
  3999. this.actions.get('copyAsText').setEnabled(ss.cells.length == 1);
  4000. this.actions.get('editLink').setEnabled(ss.cells.length == 1);
  4001. this.actions.get('editStyle').setEnabled(ss.cells.length > 0);
  4002. this.actions.get('editTooltip').setEnabled(ss.cells.length == 1);
  4003. this.actions.get('openLink').setEnabled(ss.cells.length == 1 &&
  4004. graph.getLinkForCell(ss.cells[0]) != null);
  4005. this.actions.get('guides').setEnabled(graph.isEnabled());
  4006. this.actions.get('selectVertices').setEnabled(unlocked);
  4007. this.actions.get('selectEdges').setEnabled(unlocked);
  4008. this.actions.get('selectAll').setEnabled(unlocked);
  4009. this.actions.get('selectNone').setEnabled(unlocked);
  4010. var foldable = ss.vertices.length == 1 &&
  4011. graph.isCellFoldable(ss.vertices[0]);
  4012. this.actions.get('expand').setEnabled(foldable);
  4013. this.actions.get('collapse').setEnabled(foldable);
  4014. // Updates menu states
  4015. this.menus.get('navigation').setEnabled(ss.cells.length > 0 ||
  4016. graph.view.currentRoot != null);
  4017. this.menus.get('layout').setEnabled(unlocked);
  4018. this.menus.get('insert').setEnabled(unlocked);
  4019. this.menus.get('direction').setEnabled(ss.unlocked &&
  4020. ss.vertices.length == 1);
  4021. this.menus.get('distribute').setEnabled(ss.unlocked &&
  4022. ss.vertices.length > 1);
  4023. this.menus.get('align').setEnabled(ss.unlocked &&
  4024. ss.cells.length > 0);
  4025. this.updatePasteActionStates();
  4026. };
  4027. EditorUi.prototype.zeroOffset = new mxPoint(0, 0);
  4028. EditorUi.prototype.getDiagramContainerOffset = function()
  4029. {
  4030. return this.zeroOffset;
  4031. };
  4032. /**
  4033. * Refreshes the viewport.
  4034. */
  4035. EditorUi.prototype.refresh = function(sizeDidChange)
  4036. {
  4037. sizeDidChange = (sizeDidChange != null) ? sizeDidChange : true;
  4038. var w = this.container.clientWidth;
  4039. var h = this.container.clientHeight;
  4040. if (this.container == document.body)
  4041. {
  4042. w = document.body.clientWidth || document.documentElement.clientWidth;
  4043. h = document.documentElement.clientHeight;
  4044. }
  4045. // Workaround for bug on iOS see
  4046. // http://stackoverflow.com/questions/19012135/ios-7-ipad-safari-landscape-innerheight-outerheight-layout-issue
  4047. // FIXME: Fix if footer visible
  4048. var off = 0;
  4049. if (mxClient.IS_IOS && !window.navigator.standalone && typeof Menus !== 'undefined')
  4050. {
  4051. if (window.innerHeight != document.documentElement.clientHeight)
  4052. {
  4053. off = document.documentElement.clientHeight - window.innerHeight;
  4054. window.scrollTo(0, 0);
  4055. }
  4056. }
  4057. var effHsplitPosition = Math.max(0, Math.min(
  4058. this.hsplitPosition, w - this.splitSize - 40));
  4059. var tmp = 0;
  4060. if (this.menubar != null)
  4061. {
  4062. this.menubarContainer.style.height = this.menubarHeight + 'px';
  4063. tmp += this.menubarHeight;
  4064. }
  4065. if (this.toolbar != null)
  4066. {
  4067. this.toolbarContainer.style.top = this.menubarHeight + 'px';
  4068. this.toolbarContainer.style.height = this.toolbarHeight + 'px';
  4069. tmp += this.toolbarHeight;
  4070. }
  4071. if (tmp > 0)
  4072. {
  4073. tmp += 1;
  4074. }
  4075. var fw = (this.format != null) ? this.formatWidth : 0;
  4076. this.sidebarContainer.style.top = tmp + 'px';
  4077. this.sidebarContainer.style.width = effHsplitPosition + 'px';
  4078. this.formatContainer.style.top = tmp + 'px';
  4079. this.formatContainer.style.width = fw + 'px';
  4080. this.formatContainer.style.display = (this.format != null) ? '' : 'none';
  4081. var diagContOffset = this.getDiagramContainerOffset();
  4082. var contLeft = (this.hsplit.parentNode != null) ? (effHsplitPosition) : 0;
  4083. this.footerContainer.style.height = this.footerHeight + 'px';
  4084. this.hsplit.style.top = this.sidebarContainer.style.top;
  4085. this.hsplit.style.left = effHsplitPosition + 'px';
  4086. this.footerContainer.style.display = (this.footerHeight == 0) ? 'none' : '';
  4087. if (this.tabContainer != null)
  4088. {
  4089. this.tabContainer.style.left = contLeft + 'px';
  4090. this.hsplit.style.bottom = this.tabContainer.offsetHeight + 'px';
  4091. }
  4092. else
  4093. {
  4094. this.hsplit.style.bottom = (this.footerHeight + off) + 'px';
  4095. }
  4096. if (this.footerHeight > 0)
  4097. {
  4098. this.footerContainer.style.bottom = off + 'px';
  4099. }
  4100. var th = 0;
  4101. if (this.tabContainer != null)
  4102. {
  4103. this.tabContainer.style.bottom = (this.footerHeight + off) + 'px';
  4104. this.tabContainer.style.right = fw + 'px';
  4105. th = this.tabContainer.clientHeight;
  4106. this.checkTabScrollerOverflow();
  4107. }
  4108. this.sidebarContainer.style.bottom = (this.footerHeight + off) + 'px';
  4109. this.formatContainer.style.bottom = (this.footerHeight + off) + 'px';
  4110. this.diagramContainer.style.left = (contLeft + diagContOffset.x) + 'px';
  4111. this.diagramContainer.style.top = (tmp + diagContOffset.y) + 'px';
  4112. this.diagramContainer.style.right = fw + 'px';
  4113. this.diagramContainer.style.bottom = (this.footerHeight + off + th) + 'px';
  4114. if (sizeDidChange)
  4115. {
  4116. this.editor.graph.sizeDidChange();
  4117. }
  4118. };
  4119. /**
  4120. * Creates the required containers.
  4121. */
  4122. EditorUi.prototype.createTabContainer = function()
  4123. {
  4124. return null;
  4125. };
  4126. /**
  4127. * Creates the required containers.
  4128. */
  4129. EditorUi.prototype.createDivs = function()
  4130. {
  4131. this.menubarContainer = this.createDiv('geMenubarContainer');
  4132. this.toolbarContainer = this.createDiv('geToolbarContainer');
  4133. this.sidebarContainer = this.createDiv('geSidebarContainer');
  4134. this.formatContainer = this.createDiv('geSidebarContainer geFormatContainer');
  4135. this.diagramContainer = this.createDiv('geDiagramContainer');
  4136. this.footerContainer = this.createDiv('geFooterContainer');
  4137. this.hsplit = this.createDiv('geHsplit');
  4138. // Sets static style for containers
  4139. this.menubarContainer.style.top = '0px';
  4140. this.menubarContainer.style.left = '0px';
  4141. this.menubarContainer.style.right = '0px';
  4142. this.toolbarContainer.style.left = '0px';
  4143. this.toolbarContainer.style.right = '0px';
  4144. this.sidebarContainer.style.left = '0px';
  4145. this.sidebarContainer.style.zIndex = '1';
  4146. this.formatContainer.style.right = '0px';
  4147. this.formatContainer.style.zIndex = '1';
  4148. this.diagramContainer.style.right = ((this.format != null) ? this.formatWidth : 0) + 'px';
  4149. this.footerContainer.style.left = '0px';
  4150. this.footerContainer.style.right = '0px';
  4151. this.footerContainer.style.bottom = '0px';
  4152. this.footerContainer.style.zIndex = mxPopupMenu.prototype.zIndex - 3;
  4153. this.hsplit.style.width = this.splitSize + 'px';
  4154. this.hsplit.style.zIndex = '1';
  4155. if (!this.editor.chromeless)
  4156. {
  4157. this.tabContainer = this.createTabContainer();
  4158. }
  4159. else
  4160. {
  4161. this.diagramContainer.style.border = 'none';
  4162. }
  4163. };
  4164. /**
  4165. * Hook for sidebar footer container. This implementation returns null.
  4166. */
  4167. EditorUi.prototype.createSidebarContainer = function()
  4168. {
  4169. var div = document.createElement('div');
  4170. div.className = 'geSidebarContainer';
  4171. return div;
  4172. };
  4173. /**
  4174. * Creates the required containers.
  4175. */
  4176. EditorUi.prototype.createUi = function()
  4177. {
  4178. // Creates menubar
  4179. this.menubar = (this.editor.chromeless) ? null : this.menus.createMenubar(this.createDiv('geMenubar'));
  4180. if (this.menubar != null)
  4181. {
  4182. this.menubarContainer.appendChild(this.menubar.container);
  4183. }
  4184. // Adds status bar in menubar
  4185. if (this.menubar != null)
  4186. {
  4187. this.statusContainer = this.createStatusContainer();
  4188. // Connects the status bar to the editor status
  4189. this.editor.addListener('statusChanged', mxUtils.bind(this, function()
  4190. {
  4191. this.setStatusText(this.editor.getStatus());
  4192. }));
  4193. this.setStatusText(this.editor.getStatus());
  4194. this.menubar.container.appendChild(this.statusContainer);
  4195. // Inserts into DOM
  4196. this.container.appendChild(this.menubarContainer);
  4197. }
  4198. // Creates the sidebar
  4199. this.sidebar = (this.editor.chromeless) ? null : this.createSidebar(this.sidebarContainer);
  4200. if (this.sidebar != null)
  4201. {
  4202. this.container.appendChild(this.sidebarContainer);
  4203. }
  4204. // Creates the format sidebar
  4205. this.format = (this.editor.chromeless || !this.formatEnabled) ? null : this.createFormat(this.formatContainer);
  4206. if (this.format != null)
  4207. {
  4208. this.container.appendChild(this.formatContainer);
  4209. }
  4210. // Creates the footer
  4211. var footer = (this.editor.chromeless) ? null : this.createFooter();
  4212. if (footer != null)
  4213. {
  4214. this.footerContainer.appendChild(footer);
  4215. this.container.appendChild(this.footerContainer);
  4216. }
  4217. this.container.appendChild(this.diagramContainer);
  4218. if (this.container != null && this.tabContainer != null)
  4219. {
  4220. this.container.appendChild(this.tabContainer);
  4221. }
  4222. // Creates toolbar
  4223. this.toolbar = (this.editor.chromeless) ? null : this.createToolbar(this.createDiv('geToolbar'));
  4224. if (this.toolbar != null)
  4225. {
  4226. this.toolbarContainer.appendChild(this.toolbar.container);
  4227. this.container.appendChild(this.toolbarContainer);
  4228. }
  4229. // HSplit
  4230. if (this.sidebar != null)
  4231. {
  4232. this.container.appendChild(this.hsplit);
  4233. this.addSplitHandler(this.hsplit, true, 0, mxUtils.bind(this, function(value)
  4234. {
  4235. this.hsplitPosition = value;
  4236. this.refresh();
  4237. }));
  4238. }
  4239. };
  4240. /**
  4241. * Creates a new toolbar for the given container.
  4242. */
  4243. EditorUi.prototype.createStatusContainer = function()
  4244. {
  4245. var container = document.createElement('a');
  4246. container.className = 'geItem geStatus';
  4247. // Handles data-action attribute
  4248. mxEvent.addListener(container, 'click', mxUtils.bind(this, function(evt)
  4249. {
  4250. var elt = mxEvent.getSource(evt);
  4251. if (elt != container)
  4252. {
  4253. while (elt.parentNode != container)
  4254. {
  4255. elt = elt.parentNode;
  4256. }
  4257. }
  4258. if (elt.nodeName != 'A')
  4259. {
  4260. var name = elt.getAttribute('data-action');
  4261. // Make generic
  4262. if (name == 'statusFunction' && this.editor.statusFunction != null)
  4263. {
  4264. this.editor.statusFunction();
  4265. }
  4266. else if (name != null)
  4267. {
  4268. var action = this.actions.get(name);
  4269. if (action != null)
  4270. {
  4271. action.funct();
  4272. }
  4273. }
  4274. else
  4275. {
  4276. var title = elt.getAttribute('data-title');
  4277. var msg = elt.getAttribute('data-message');
  4278. if (title != null && msg != null)
  4279. {
  4280. this.showError(title, msg);
  4281. }
  4282. else
  4283. {
  4284. var link = elt.getAttribute('data-link');
  4285. if (link != null)
  4286. {
  4287. this.editor.graph.openLink(link);
  4288. }
  4289. }
  4290. }
  4291. mxEvent.consume(evt);
  4292. }
  4293. }));
  4294. return container;
  4295. };
  4296. /**
  4297. * Creates a new toolbar for the given container.
  4298. */
  4299. EditorUi.prototype.setStatusText = function(value)
  4300. {
  4301. this.statusContainer.innerHTML = Graph.sanitizeHtml(value);
  4302. // Wraps simple status messages in a div for styling
  4303. if (this.statusContainer.getElementsByTagName('div').length == 0 &&
  4304. value != null && value.length > 0)
  4305. {
  4306. this.statusContainer.innerText = '';
  4307. var div = this.createStatusDiv(value);
  4308. this.statusContainer.appendChild(div);
  4309. }
  4310. // Handles data-effect attribute
  4311. var spans = this.statusContainer.querySelectorAll('[data-effect="fade"]');
  4312. if (spans != null)
  4313. {
  4314. for (var i = 0; i < spans.length; i++)
  4315. {
  4316. (function(temp)
  4317. {
  4318. mxUtils.setOpacity(temp, 0);
  4319. mxUtils.setPrefixedStyle(temp.style, 'transform', 'scaleX(0)');
  4320. mxUtils.setPrefixedStyle(temp.style, 'transition', 'all 0.2s ease');
  4321. window.setTimeout(mxUtils.bind(this, function()
  4322. {
  4323. mxUtils.setOpacity(temp, 100);
  4324. mxUtils.setPrefixedStyle(temp.style, 'transform', 'scaleX(1)');
  4325. mxUtils.setPrefixedStyle(temp.style, 'transition', 'all 1s ease');
  4326. window.setTimeout(mxUtils.bind(this, function()
  4327. {
  4328. mxUtils.setPrefixedStyle(temp.style, 'transform', 'scaleX(0)');
  4329. mxUtils.setOpacity(temp, 0);
  4330. window.setTimeout(mxUtils.bind(this, function()
  4331. {
  4332. if (temp.parentNode != null)
  4333. {
  4334. temp.parentNode.removeChild(temp);
  4335. }
  4336. }), 1000);
  4337. }), Editor.updateStatusInterval / 2);
  4338. }), 0);
  4339. })(spans[i]);
  4340. }
  4341. }
  4342. };
  4343. /**
  4344. * Creates a new toolbar for the given container.
  4345. */
  4346. EditorUi.prototype.createStatusDiv = function(value)
  4347. {
  4348. var div = document.createElement('div');
  4349. div.style.textOverflow = 'ellipsis';
  4350. div.style.display = 'inline-block';
  4351. div.style.whiteSpace = 'nowrap';
  4352. div.style.overflow = 'hidden';
  4353. div.style.minWidth = '0';
  4354. div.setAttribute('title', value);
  4355. div.innerHTML = Graph.sanitizeHtml(value);
  4356. return div;
  4357. };
  4358. /**
  4359. * Creates a new toolbar for the given container.
  4360. */
  4361. EditorUi.prototype.createToolbar = function(container)
  4362. {
  4363. return new Toolbar(this, container);
  4364. };
  4365. /**
  4366. * Creates a new sidebar for the given container.
  4367. */
  4368. EditorUi.prototype.createSidebar = function(container)
  4369. {
  4370. return new Sidebar(this, container);
  4371. };
  4372. /**
  4373. * Creates a new sidebar for the given container.
  4374. */
  4375. EditorUi.prototype.createFormat = function(container)
  4376. {
  4377. return new Format(this, container);
  4378. };
  4379. /**
  4380. * Creates and returns a new footer.
  4381. */
  4382. EditorUi.prototype.createFooter = function()
  4383. {
  4384. return this.createDiv('geFooter');
  4385. };
  4386. /**
  4387. * Creates the actual toolbar for the toolbar container.
  4388. */
  4389. EditorUi.prototype.createDiv = function(classname)
  4390. {
  4391. var elt = document.createElement('div');
  4392. elt.className = classname;
  4393. return elt;
  4394. };
  4395. /**
  4396. * Updates the states of the given undo/redo items.
  4397. */
  4398. EditorUi.prototype.addSplitHandler = function(elt, horizontal, dx, onChange)
  4399. {
  4400. var start = null;
  4401. var initial = null;
  4402. var ignoreClick = true;
  4403. var last = null;
  4404. // Disables built-in pan and zoom in IE10 and later
  4405. if (mxClient.IS_POINTER)
  4406. {
  4407. elt.style.touchAction = 'none';
  4408. }
  4409. var getValue = mxUtils.bind(this, function()
  4410. {
  4411. var result = parseInt(((horizontal) ? elt.style.left : elt.style.bottom));
  4412. // Takes into account hidden footer
  4413. if (!horizontal)
  4414. {
  4415. result = result + dx - this.footerHeight;
  4416. }
  4417. return result;
  4418. });
  4419. function moveHandler(evt)
  4420. {
  4421. if (start != null)
  4422. {
  4423. var pt = new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  4424. onChange(Math.max(0, initial + ((horizontal) ? (pt.x - start.x) : (start.y - pt.y)) - dx));
  4425. mxEvent.consume(evt);
  4426. if (initial != getValue())
  4427. {
  4428. ignoreClick = true;
  4429. last = null;
  4430. }
  4431. }
  4432. };
  4433. function dropHandler(evt)
  4434. {
  4435. moveHandler(evt);
  4436. initial = null;
  4437. start = null;
  4438. };
  4439. mxEvent.addGestureListeners(elt, function(evt)
  4440. {
  4441. start = new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  4442. initial = getValue();
  4443. ignoreClick = false;
  4444. mxEvent.consume(evt);
  4445. });
  4446. mxEvent.addListener(elt, 'click', mxUtils.bind(this, function(evt)
  4447. {
  4448. if (!ignoreClick && this.hsplitClickEnabled)
  4449. {
  4450. var next = (last != null) ? last - dx : 0;
  4451. last = getValue();
  4452. onChange(next);
  4453. mxEvent.consume(evt);
  4454. }
  4455. }));
  4456. mxEvent.addGestureListeners(document, null, moveHandler, dropHandler);
  4457. this.destroyFunctions.push(function()
  4458. {
  4459. mxEvent.removeGestureListeners(document, null, moveHandler, dropHandler);
  4460. });
  4461. };
  4462. /**
  4463. * Translates this point by the given vector.
  4464. *
  4465. * @param {number} dx X-coordinate of the translation.
  4466. * @param {number} dy Y-coordinate of the translation.
  4467. */
  4468. EditorUi.prototype.prompt = function(title, defaultValue, fn)
  4469. {
  4470. var dlg = new FilenameDialog(this, defaultValue,
  4471. mxResources.get('apply'), function(newValue)
  4472. {
  4473. fn(parseFloat(newValue));
  4474. }, title);
  4475. this.showDialog(dlg.container, 300, 80, true, true);
  4476. dlg.init();
  4477. };
  4478. /**
  4479. * Translates this point by the given vector.
  4480. *
  4481. * @param {number} dx X-coordinate of the translation.
  4482. * @param {number} dy Y-coordinate of the translation.
  4483. */
  4484. EditorUi.prototype.handleError = function(resp, title, fn, invokeFnOnClose, notFoundMessage)
  4485. {
  4486. var e = (resp != null && resp.error != null) ? resp.error : resp;
  4487. if (e != null || title != null)
  4488. {
  4489. var msg = mxUtils.htmlEntities(mxResources.get('unknownError'));
  4490. var btn = mxResources.get('ok');
  4491. title = (title != null) ? title : mxResources.get('error');
  4492. if (e != null && e.message != null)
  4493. {
  4494. msg = mxUtils.htmlEntities(e.message);
  4495. }
  4496. this.showError(title, msg, btn, fn, null, null, null, null, null,
  4497. null, null, null, (invokeFnOnClose) ? fn : null);
  4498. }
  4499. else if (fn != null)
  4500. {
  4501. fn();
  4502. }
  4503. };
  4504. /**
  4505. * Translates this point by the given vector.
  4506. *
  4507. * @param {number} dx X-coordinate of the translation.
  4508. * @param {number} dy Y-coordinate of the translation.
  4509. */
  4510. EditorUi.prototype.showError = function(title, msg, btn, fn, retry, btn2, fn2, btn3, fn3, w, h, hide, onClose)
  4511. {
  4512. var dlg = new ErrorDialog(this, title, msg, btn || mxResources.get('ok'),
  4513. fn, retry, btn2, fn2, hide, btn3, fn3);
  4514. var lines = Math.ceil((msg != null) ? msg.length / 50 : 1);
  4515. this.showDialog(dlg.container, w || 340, h || (100 + lines * 20), true, false, onClose);
  4516. dlg.init();
  4517. };
  4518. /**
  4519. * Displays a print dialog.
  4520. */
  4521. EditorUi.prototype.showDialog = function(elt, w, h, modal, closable, onClose, noScroll, transparent, onResize, ignoreBgClick)
  4522. {
  4523. this.editor.graph.tooltipHandler.resetTimer();
  4524. this.editor.graph.tooltipHandler.hideTooltip();
  4525. if (this.dialogs == null)
  4526. {
  4527. this.dialogs = [];
  4528. }
  4529. this.dialog = new Dialog(this, elt, w, h, modal, closable, onClose, noScroll, transparent, onResize, ignoreBgClick);
  4530. this.dialogs.push(this.dialog);
  4531. };
  4532. /**
  4533. * Displays a print dialog.
  4534. */
  4535. EditorUi.prototype.hideDialog = function(cancel, isEsc, matchContainer)
  4536. {
  4537. // Finds topmost non-closing dialog
  4538. // This closes dialogs underneath the closing dialog when hideDialog
  4539. // is called in the process of closing the current dialog
  4540. var dlg = null;
  4541. if (this.dialogs != null && this.dialogs.length > 0)
  4542. {
  4543. for (var i = this.dialogs.length - 1; i >= 0; i--)
  4544. {
  4545. if (!this.dialogs[i].closing)
  4546. {
  4547. dlg = this.dialogs[i];
  4548. break;
  4549. }
  4550. }
  4551. }
  4552. if (dlg != null)
  4553. {
  4554. if (matchContainer != null && matchContainer != this.dialog.container.firstChild)
  4555. {
  4556. return;
  4557. }
  4558. dlg.closing = true;
  4559. if (dlg.close(cancel, isEsc) == false)
  4560. {
  4561. delete dlg.closing;
  4562. return;
  4563. }
  4564. // Removes dialog from stack
  4565. delete dlg.closing;
  4566. var index = mxUtils.lastIndexOf(this.dialogs, dlg);
  4567. if (index >= 0)
  4568. {
  4569. this.dialogs.splice(index, 1);
  4570. }
  4571. this.dialog = (this.dialogs.length > 0) ? this.dialogs[this.dialogs.length - 1] : null;
  4572. // Restores existing dialogs and adds new dialogs
  4573. this.editor.fireEvent(new mxEventObject('hideDialog'));
  4574. if (this.dialog == null && this.editor.graph.container != null &&
  4575. this.editor.graph.container.style.visibility != 'hidden')
  4576. {
  4577. window.setTimeout(mxUtils.bind(this, function()
  4578. {
  4579. if (this.editor != null && (this.dialogs == null || this.dialogs.length == 0))
  4580. {
  4581. if (this.editor.graph.isEditing() && this.editor.graph.cellEditor.textarea != null)
  4582. {
  4583. this.editor.graph.cellEditor.textarea.focus();
  4584. }
  4585. else
  4586. {
  4587. mxUtils.clearSelection();
  4588. this.editor.graph.container.focus();
  4589. }
  4590. }
  4591. }), 0);
  4592. }
  4593. }
  4594. };
  4595. /**
  4596. * Handles ctrl+enter keystroke to clone cells.
  4597. */
  4598. EditorUi.prototype.ctrlEnter = function()
  4599. {
  4600. var graph = this.editor.graph;
  4601. if (graph.isEnabled())
  4602. {
  4603. try
  4604. {
  4605. var cells = graph.getSelectionCells();
  4606. var lookup = new mxDictionary();
  4607. var newCells = [];
  4608. for (var i = 0; i < cells.length; i++)
  4609. {
  4610. // Clones table rows instead of cells
  4611. var cell = (graph.isTableCell(cells[i])) ? graph.model.getParent(cells[i]) : cells[i];
  4612. if (cell != null && !lookup.get(cell))
  4613. {
  4614. lookup.put(cell, true);
  4615. newCells.push(cell);
  4616. }
  4617. }
  4618. graph.setSelectionCells(graph.duplicateCells(newCells, false));
  4619. }
  4620. catch (e)
  4621. {
  4622. this.handleError(e);
  4623. }
  4624. }
  4625. };
  4626. /**
  4627. * Display a color dialog.
  4628. */
  4629. EditorUi.prototype.pickColor = function(color, apply, defaultColor, defaultColorValue)
  4630. {
  4631. var graph = this.editor.graph;
  4632. var selState = graph.cellEditor.saveSelection();
  4633. var h = 230 + ((Math.ceil(ColorDialog.prototype.presetColors.length / 12) +
  4634. Math.ceil(ColorDialog.prototype.defaultColors.length / 12)) * 17);
  4635. var dlg = new ColorDialog(this, (color != 'default') ?
  4636. (mxUtils.rgba2hex(color) || 'none') : color, function(color)
  4637. {
  4638. graph.cellEditor.restoreSelection(selState);
  4639. apply(color);
  4640. }, function()
  4641. {
  4642. graph.cellEditor.restoreSelection(selState);
  4643. }, defaultColor, defaultColorValue);
  4644. this.showDialog(dlg.container, 230, h, true, false);
  4645. dlg.init();
  4646. };
  4647. /**
  4648. * Adds the label menu items to the given menu and parent.
  4649. */
  4650. EditorUi.prototype.openFile = function()
  4651. {
  4652. // Closes dialog after open
  4653. window.openFile = new OpenFile(mxUtils.bind(this, function(cancel)
  4654. {
  4655. this.hideDialog(cancel);
  4656. }));
  4657. // Removes openFile if dialog is closed
  4658. this.showDialog(new OpenDialog(this).container, (Editor.useLocalStorage) ? 640 : 320,
  4659. (Editor.useLocalStorage) ? 480 : 220, true, true, function()
  4660. {
  4661. window.openFile = null;
  4662. });
  4663. };
  4664. /**
  4665. * Translates this point by the given vector.
  4666. *
  4667. * @param {number} dx X-coordinate of the translation.
  4668. * @param {number} dy Y-coordinate of the translation.
  4669. */
  4670. EditorUi.prototype.base64ToBlob = function(base64Data, contentType)
  4671. {
  4672. contentType = contentType || '';
  4673. var sliceSize = 1024;
  4674. var byteCharacters = atob(base64Data);
  4675. var bytesLength = byteCharacters.length;
  4676. var slicesCount = Math.ceil(bytesLength / sliceSize);
  4677. var byteArrays = new Array(slicesCount);
  4678. for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex)
  4679. {
  4680. var begin = sliceIndex * sliceSize;
  4681. var end = Math.min(begin + sliceSize, bytesLength);
  4682. var bytes = new Array(end - begin);
  4683. for (var offset = begin, i = 0 ; offset < end; ++i, ++offset)
  4684. {
  4685. bytes[i] = byteCharacters[offset].charCodeAt(0);
  4686. }
  4687. byteArrays[sliceIndex] = new Uint8Array(bytes);
  4688. }
  4689. return new Blob(byteArrays, {type: contentType});
  4690. };
  4691. /**
  4692. * Copies the given cells and XML to the clipboard as an embedded image.
  4693. */
  4694. EditorUi.prototype.writeImageToClipboard = function(dataUrl, w, h, error)
  4695. {
  4696. var blob = this.base64ToBlob(dataUrl.substring(dataUrl.indexOf(',') + 1), 'image/png');
  4697. var html = '<img src="' + dataUrl + '" width="' + w + '" height="' + h + '">';
  4698. var cbi = new ClipboardItem({'image/png': blob,
  4699. 'text/html': new Blob([html], {type: 'text/html'})});
  4700. navigator.clipboard.write([cbi])['catch'](error);
  4701. };
  4702. /**
  4703. * Copies the given cells and XML to the clipboard as an embedded image.
  4704. */
  4705. EditorUi.prototype.writeHtmlToClipboard = function(html, error)
  4706. {
  4707. var cbi = new ClipboardItem(
  4708. {
  4709. 'text/plain': new Blob([Editor.convertHtmlToText(html)], {type: 'text/plain'}),
  4710. 'text/html': new Blob([html], {type: 'text/html'})
  4711. });
  4712. navigator.clipboard.write([cbi])['catch'](error);
  4713. };
  4714. /**
  4715. * Writes the given text to the clipboard.
  4716. */
  4717. EditorUi.prototype.writeTextToClipboard = function(text, error)
  4718. {
  4719. navigator.clipboard.writeText(text)['catch'](error);
  4720. };
  4721. /**
  4722. * Extracs the graph model from the given HTML data from a data transfer event.
  4723. */
  4724. EditorUi.prototype.extractGraphModelFromHtml = function(data)
  4725. {
  4726. var result = null;
  4727. try
  4728. {
  4729. var idx = data.indexOf('&lt;mxGraphModel ');
  4730. if (idx >= 0)
  4731. {
  4732. var idx2 = data.lastIndexOf('&lt;/mxGraphModel&gt;');
  4733. if (idx2 > idx)
  4734. {
  4735. result = data.substring(idx, idx2 + 21).replace(/&gt;/g, '>').
  4736. replace(/&lt;/g, '<').replace(/\\&quot;/g, '"').replace(/\n/g, '');
  4737. }
  4738. }
  4739. }
  4740. catch (e)
  4741. {
  4742. // ignore
  4743. }
  4744. return result;
  4745. };
  4746. /**
  4747. * Opens the given files in the editor.
  4748. */
  4749. EditorUi.prototype.readGraphModelFromClipboard = function(fn)
  4750. {
  4751. this.readGraphModelFromClipboardWithType(mxUtils.bind(this, function(xml)
  4752. {
  4753. if (xml != null)
  4754. {
  4755. fn(xml);
  4756. }
  4757. else
  4758. {
  4759. this.readGraphModelFromClipboardWithType(mxUtils.bind(this, function(xml)
  4760. {
  4761. if (xml != null)
  4762. {
  4763. var tmp = decodeURIComponent(xml);
  4764. if (this.isCompatibleString(tmp))
  4765. {
  4766. xml = tmp;
  4767. }
  4768. }
  4769. fn(xml);
  4770. }), 'text');
  4771. }
  4772. }), 'html');
  4773. };
  4774. /**
  4775. * Opens the given files in the editor.
  4776. */
  4777. EditorUi.prototype.readGraphModelFromClipboardWithType = function(fn, type)
  4778. {
  4779. navigator.clipboard.read().then(mxUtils.bind(this, function(data)
  4780. {
  4781. if (data != null && data.length > 0 && type == 'html' &&
  4782. mxUtils.indexOf(data[0].types, 'text/html') >= 0)
  4783. {
  4784. data[0].getType('text/html').then(mxUtils.bind(this, function(blob)
  4785. {
  4786. blob.text().then(mxUtils.bind(this, function(value)
  4787. {
  4788. try
  4789. {
  4790. var elt = this.parseHtmlData(value);
  4791. var asHtml = elt.getAttribute('data-type') != 'text/plain';
  4792. // KNOWN: Paste from IE11 to other browsers on Windows
  4793. // seems to paste the contents of index.html
  4794. var xml = (asHtml) ? elt.innerHTML :
  4795. mxUtils.trim((elt.innerText == null) ?
  4796. mxUtils.getTextContent(elt) : elt.innerText);
  4797. // Workaround for junk after XML in VM
  4798. try
  4799. {
  4800. var idx = xml.lastIndexOf('%3E');
  4801. if (idx >= 0 && idx < xml.length - 3)
  4802. {
  4803. xml = xml.substring(0, idx + 3);
  4804. }
  4805. }
  4806. catch (e)
  4807. {
  4808. // ignore
  4809. }
  4810. // Checks for embedded XML content
  4811. try
  4812. {
  4813. var spans = elt.getElementsByTagName('span');
  4814. var tmp = (spans != null && spans.length > 0) ?
  4815. mxUtils.trim(decodeURIComponent(spans[0].textContent)) :
  4816. decodeURIComponent(xml);
  4817. if (this.isCompatibleString(tmp))
  4818. {
  4819. xml = tmp;
  4820. }
  4821. }
  4822. catch (e)
  4823. {
  4824. // ignore
  4825. }
  4826. }
  4827. catch (e)
  4828. {
  4829. // ignore
  4830. }
  4831. fn(this.isCompatibleString(xml) ? xml : null);
  4832. }))['catch'](function(data)
  4833. {
  4834. fn(null);
  4835. });
  4836. }))['catch'](function(data)
  4837. {
  4838. fn(null);
  4839. });
  4840. }
  4841. else if (data != null && data.length > 0 && type == 'text' &&
  4842. mxUtils.indexOf(data[0].types, 'text/plain') >= 0)
  4843. {
  4844. data[0].getType('text/plain').then(function(blob)
  4845. {
  4846. blob.text().then(function(value)
  4847. {
  4848. fn(value);
  4849. })['catch'](function()
  4850. {
  4851. fn(null);
  4852. });
  4853. })['catch'](function()
  4854. {
  4855. fn(null);
  4856. });
  4857. }
  4858. else
  4859. {
  4860. fn(null);
  4861. }
  4862. }))['catch'](function(data)
  4863. {
  4864. fn(null);
  4865. });
  4866. };
  4867. /**
  4868. * Parses the given HTML data and returns a DIV.
  4869. */
  4870. EditorUi.prototype.parseHtmlData = function(data)
  4871. {
  4872. var elt = null;
  4873. if (data != null && data.length > 0)
  4874. {
  4875. var hasMeta = data.substring(0, 6) == '<meta ';
  4876. elt = document.createElement('div');
  4877. elt.innerHTML = ((hasMeta) ? '<meta charset="utf-8">' : '') +
  4878. Graph.sanitizeHtml(data);
  4879. asHtml = true;
  4880. // Workaround for innerText not ignoring style elements in Chrome
  4881. var styles = elt.getElementsByTagName('style');
  4882. if (styles != null)
  4883. {
  4884. while (styles.length > 0)
  4885. {
  4886. styles[0].parentNode.removeChild(styles[0]);
  4887. }
  4888. }
  4889. // Special case of link pasting from Chrome
  4890. if (elt.firstChild != null && elt.firstChild.nodeType == mxConstants.NODETYPE_ELEMENT &&
  4891. elt.firstChild.nextSibling != null && elt.firstChild.nextSibling.nodeType == mxConstants.NODETYPE_ELEMENT &&
  4892. elt.firstChild.nodeName == 'META' && elt.firstChild.nextSibling.nodeName == 'A' &&
  4893. elt.firstChild.nextSibling.nextSibling == null)
  4894. {
  4895. var temp = (elt.firstChild.nextSibling.innerText == null) ?
  4896. mxUtils.getTextContent(elt.firstChild.nextSibling) :
  4897. elt.firstChild.nextSibling.innerText;
  4898. if (temp == elt.firstChild.nextSibling.getAttribute('href'))
  4899. {
  4900. mxUtils.setTextContent(elt, temp);
  4901. asHtml = false;
  4902. }
  4903. }
  4904. // Extracts single image source address with meta tag in markup
  4905. var img = (hasMeta && elt.firstChild != null) ? elt.firstChild.nextSibling : elt.firstChild;
  4906. if (img != null && img.nextSibling == null &&
  4907. img.nodeType == mxConstants.NODETYPE_ELEMENT &&
  4908. img.nodeName == 'IMG')
  4909. {
  4910. var temp = img.getAttribute('src');
  4911. if (temp != null)
  4912. {
  4913. if (Editor.isPngDataUrl(temp))
  4914. {
  4915. var xml = Editor.extractGraphModelFromPng(temp);
  4916. if (xml != null && xml.length > 0)
  4917. {
  4918. temp = xml;
  4919. }
  4920. }
  4921. mxUtils.setTextContent(elt, temp);
  4922. asHtml = false;
  4923. }
  4924. }
  4925. else
  4926. {
  4927. // Extracts embedded XML or image source address from single PNG image
  4928. var images = elt.getElementsByTagName('img');
  4929. if (images.length == 1)
  4930. {
  4931. var img = images[0];
  4932. var temp = img.getAttribute('src');
  4933. if (temp != null && img.parentNode == elt && elt.children.length == 1)
  4934. {
  4935. if (Editor.isPngDataUrl(temp))
  4936. {
  4937. var xml = Editor.extractGraphModelFromPng(temp);
  4938. if (xml != null && xml.length > 0)
  4939. {
  4940. temp = xml;
  4941. }
  4942. }
  4943. mxUtils.setTextContent(elt, temp);
  4944. asHtml = false;
  4945. }
  4946. }
  4947. }
  4948. if (asHtml)
  4949. {
  4950. Graph.removePasteFormatting(elt);
  4951. }
  4952. }
  4953. if (!asHtml)
  4954. {
  4955. elt.setAttribute('data-type', 'text/plain');
  4956. }
  4957. return elt;
  4958. };
  4959. /**
  4960. * Opens the given files in the editor.
  4961. */
  4962. EditorUi.prototype.extractGraphModelFromEvent = function(evt)
  4963. {
  4964. var result = null;
  4965. var data = null;
  4966. if (evt != null)
  4967. {
  4968. var provider = (evt.dataTransfer != null) ?
  4969. evt.dataTransfer : evt.clipboardData;
  4970. if (provider != null)
  4971. {
  4972. if (document.documentMode == 10 || document.documentMode == 11)
  4973. {
  4974. data = provider.getData('Text');
  4975. }
  4976. else
  4977. {
  4978. data = (mxUtils.indexOf(provider.types, 'text/html') >= 0) ?
  4979. provider.getData('text/html') : null;
  4980. if (mxUtils.indexOf(provider.types, 'text/plain') >= 0 &&
  4981. (data == null || data.length == 0))
  4982. {
  4983. data = provider.getData('text/plain');
  4984. }
  4985. }
  4986. if (data != null)
  4987. {
  4988. data = Graph.zapGremlins(mxUtils.trim(data));
  4989. // Tries parsing as HTML document with embedded XML
  4990. var xml = this.extractGraphModelFromHtml(data);
  4991. if (xml != null)
  4992. {
  4993. data = xml;
  4994. }
  4995. }
  4996. }
  4997. }
  4998. if (data != null && this.isCompatibleString(data))
  4999. {
  5000. result = data;
  5001. }
  5002. return result;
  5003. };
  5004. /**
  5005. * Hook for subclassers to return true if event data is a supported format.
  5006. * This implementation always returns false.
  5007. */
  5008. EditorUi.prototype.isCompatibleString = function(data)
  5009. {
  5010. return false;
  5011. };
  5012. /**
  5013. * Adds the label menu items to the given menu and parent.
  5014. */
  5015. EditorUi.prototype.saveFile = function(forceDialog)
  5016. {
  5017. if (!forceDialog && this.editor.filename != null)
  5018. {
  5019. this.save(this.editor.getOrCreateFilename());
  5020. }
  5021. else
  5022. {
  5023. var dlg = new FilenameDialog(this, this.editor.getOrCreateFilename(),
  5024. mxResources.get('save'), mxUtils.bind(this, function(name)
  5025. {
  5026. this.save(name);
  5027. }), null, mxUtils.bind(this, function(name)
  5028. {
  5029. if (name != null && name.length > 0)
  5030. {
  5031. return true;
  5032. }
  5033. mxUtils.confirm(mxResources.get('invalidName'));
  5034. return false;
  5035. }));
  5036. this.showDialog(dlg.container, 340, 96, true, true);
  5037. dlg.init();
  5038. }
  5039. };
  5040. /**
  5041. * Saves the current graph under the given filename.
  5042. */
  5043. EditorUi.prototype.save = function(name)
  5044. {
  5045. if (name != null)
  5046. {
  5047. if (this.editor.graph.isEditing())
  5048. {
  5049. this.editor.graph.stopEditing();
  5050. }
  5051. var xml = mxUtils.getXml(this.editor.getGraphXml());
  5052. try
  5053. {
  5054. if (Editor.useLocalStorage)
  5055. {
  5056. if (localStorage.getItem(name) != null &&
  5057. !mxUtils.confirm(mxResources.get('replaceIt', [name])))
  5058. {
  5059. return;
  5060. }
  5061. localStorage.setItem(name, xml);
  5062. this.editor.setStatus(mxUtils.htmlEntities(mxResources.get('saved')) + ' ' + new Date());
  5063. }
  5064. else
  5065. {
  5066. if (xml.length < MAX_REQUEST_SIZE)
  5067. {
  5068. new mxXmlRequest(SAVE_URL, 'filename=' + encodeURIComponent(name) +
  5069. '&xml=' + encodeURIComponent(xml)).simulate(document, '_blank');
  5070. }
  5071. else
  5072. {
  5073. mxUtils.alert(mxResources.get('drawingTooLarge'));
  5074. mxUtils.popup(xml);
  5075. return;
  5076. }
  5077. }
  5078. this.editor.setModified(false);
  5079. this.editor.setFilename(name);
  5080. this.updateDocumentTitle();
  5081. }
  5082. catch (e)
  5083. {
  5084. this.editor.setStatus(mxUtils.htmlEntities(mxResources.get('errorSavingFile')));
  5085. }
  5086. }
  5087. };
  5088. /**
  5089. * Executes the given array of graph layouts using executeLayout and
  5090. * calls done after the last layout has finished.
  5091. */
  5092. EditorUi.prototype.executeLayouts = function(layouts, post)
  5093. {
  5094. this.executeLayout(mxUtils.bind(this, function()
  5095. {
  5096. var layout = new mxCompositeLayout(this.editor.graph, layouts);
  5097. var cells = this.editor.graph.getSelectionCells();
  5098. layout.execute(this.editor.graph.getDefaultParent(),
  5099. cells.length == 0 ? null : cells);
  5100. }), true, post);
  5101. };
  5102. /**
  5103. * Executes the given layout.
  5104. */
  5105. EditorUi.prototype.executeLayout = function(exec, animate, post)
  5106. {
  5107. var graph = this.editor.graph;
  5108. graph.getModel().beginUpdate();
  5109. try
  5110. {
  5111. exec();
  5112. }
  5113. catch (e)
  5114. {
  5115. throw e;
  5116. }
  5117. finally
  5118. {
  5119. // Animates the changes in the graph model
  5120. if (this.allowAnimation && animate && graph.isEnabled())
  5121. {
  5122. // New API for animating graph layout results asynchronously
  5123. var morph = new mxMorphing(graph);
  5124. morph.addListener(mxEvent.DONE, mxUtils.bind(this, function()
  5125. {
  5126. graph.getModel().endUpdate();
  5127. if (post != null)
  5128. {
  5129. post();
  5130. }
  5131. }));
  5132. morph.startAnimation();
  5133. }
  5134. else
  5135. {
  5136. graph.getModel().endUpdate();
  5137. if (post != null)
  5138. {
  5139. post();
  5140. }
  5141. }
  5142. }
  5143. };
  5144. /**
  5145. * Hides the current menu.
  5146. */
  5147. EditorUi.prototype.showImageDialog = function(title, value, fn, ignoreExisting)
  5148. {
  5149. var cellEditor = this.editor.graph.cellEditor;
  5150. var selState = cellEditor.saveSelection();
  5151. var newValue = mxUtils.prompt(title, value);
  5152. cellEditor.restoreSelection(selState);
  5153. if (newValue != null && newValue.length > 0)
  5154. {
  5155. var img = new Image();
  5156. img.onload = function()
  5157. {
  5158. fn(newValue, img.width, img.height);
  5159. };
  5160. img.onerror = function()
  5161. {
  5162. fn(null);
  5163. mxUtils.alert(mxResources.get('fileNotFound'));
  5164. };
  5165. img.src = newValue;
  5166. }
  5167. else
  5168. {
  5169. fn(null);
  5170. }
  5171. };
  5172. /**
  5173. * Hides the current menu.
  5174. */
  5175. EditorUi.prototype.showLinkDialog = function(value, btnLabel, fn)
  5176. {
  5177. var dlg = new LinkDialog(this, value, btnLabel, fn);
  5178. this.showDialog(dlg.container, 420, 90, true, true);
  5179. dlg.init();
  5180. };
  5181. /**
  5182. * Hides the current menu.
  5183. */
  5184. EditorUi.prototype.showDataDialog = function(cell)
  5185. {
  5186. if (cell != null && typeof window.EditDataDialog !== 'undefined')
  5187. {
  5188. var dlg = new EditDataDialog(this, cell);
  5189. this.showDialog(dlg.container, 480, 420, true, false, null, false);
  5190. dlg.init();
  5191. }
  5192. };
  5193. /**
  5194. * Hides the current menu.
  5195. */
  5196. EditorUi.prototype.showBackgroundImageDialog = function(apply, img)
  5197. {
  5198. apply = (apply != null) ? apply : mxUtils.bind(this, function(image)
  5199. {
  5200. var change = new ChangePageSetup(this, null, image);
  5201. change.ignoreColor = true;
  5202. this.editor.graph.model.execute(change);
  5203. });
  5204. var newValue = mxUtils.prompt(mxResources.get('backgroundImage'), (img != null) ? img.src : '');
  5205. if (newValue != null && newValue.length > 0)
  5206. {
  5207. var img = new Image();
  5208. img.onload = function()
  5209. {
  5210. apply(new mxImage(newValue, img.width, img.height), false);
  5211. };
  5212. img.onerror = function()
  5213. {
  5214. apply(null, true);
  5215. mxUtils.alert(mxResources.get('fileNotFound'));
  5216. };
  5217. img.src = newValue;
  5218. }
  5219. else
  5220. {
  5221. apply(null);
  5222. }
  5223. };
  5224. /**
  5225. * Loads the stylesheet for this graph.
  5226. */
  5227. EditorUi.prototype.setBackgroundImage = function(image)
  5228. {
  5229. this.editor.graph.setBackgroundImage(image);
  5230. this.editor.graph.view.validateBackgroundImage();
  5231. this.fireEvent(new mxEventObject('backgroundImageChanged'));
  5232. };
  5233. /**
  5234. * Creates the keyboard event handler for the current graph and history.
  5235. */
  5236. EditorUi.prototype.confirm = function(msg, okFn, cancelFn)
  5237. {
  5238. if (mxUtils.confirm(msg))
  5239. {
  5240. if (okFn != null)
  5241. {
  5242. okFn();
  5243. }
  5244. }
  5245. else if (cancelFn != null)
  5246. {
  5247. cancelFn();
  5248. }
  5249. };
  5250. /**
  5251. * Creates the keyboard event handler for the current graph and history.
  5252. */
  5253. EditorUi.prototype.createOutline = function(wnd)
  5254. {
  5255. var outline = new mxOutline(this.editor.graph);
  5256. mxEvent.addListener(window, 'resize', function()
  5257. {
  5258. outline.update(false);
  5259. });
  5260. return outline;
  5261. };
  5262. // Alt+Shift+Keycode mapping to action
  5263. EditorUi.prototype.altShiftActions = {
  5264. 65: 'connectionArrows', // Alt+Shift+A
  5265. 82: 'clearWaypoints', // Alt+Shift+R
  5266. 76: 'editLink', // Alt+Shift+L
  5267. 79: 'connectionPoints', // Alt+Shift+O
  5268. 81: 'editConnectionPoints', // Alt+Shift+Q
  5269. 84: 'editTooltip', // Alt+Shift+T
  5270. 86: 'pasteSize', // Alt+Shift+V
  5271. 70: 'copySize', // Alt+Shift+F
  5272. 66: 'copyData', // Alt+Shift+B
  5273. 69: 'pasteData' // Alt+Shift+E
  5274. };
  5275. // Ctrl+Alt+Shift+Keycode mapping to action
  5276. EditorUi.prototype.ctrlAltShiftActions = {
  5277. 70: 'bringForward', // Ctrl+Alt+Shift+F
  5278. 66: 'sendBackward', // Ctrl+Alt+Shift+B
  5279. };
  5280. // Ctrl+Alt+Keycode mapping to action
  5281. EditorUi.prototype.ctrlAltActions = {
  5282. 88: 'copyAsImage' // Ctrl+Alt+X
  5283. };
  5284. /**
  5285. * Creates the keyboard event handler for the current graph and history.
  5286. */
  5287. EditorUi.prototype.createKeyHandler = function(editor)
  5288. {
  5289. var editorUi = this;
  5290. var graph = this.editor.graph;
  5291. var keyHandler = new mxKeyHandler(graph);
  5292. var isEventIgnored = keyHandler.isEventIgnored;
  5293. keyHandler.isEventIgnored = function(evt)
  5294. {
  5295. // Handles undo/redo/ctrl+./,/u via action and allows ctrl+b/i
  5296. // only if editing value is HTML (except for FF and Safari)
  5297. // 66, 73 are keycodes for editing actions like bold, italic
  5298. return !(mxEvent.isShiftDown(evt) && evt.keyCode == 9) &&
  5299. ((!this.isControlDown(evt) || mxEvent.isShiftDown(evt) ||
  5300. (evt.keyCode != 90 && evt.keyCode != 89 && evt.keyCode != 188 &&
  5301. evt.keyCode != 190 && evt.keyCode != 85)) && ((evt.keyCode != 66 && evt.keyCode != 73) ||
  5302. !this.isControlDown(evt) || (this.graph.cellEditor.isContentEditing() &&
  5303. !mxClient.IS_FF && !mxClient.IS_SF)) &&
  5304. ((evt.keyCode != 109 && evt.keyCode != 107) ||
  5305. (!this.isControlDown(evt) && !mxEvent.isShiftDown(evt)) ||
  5306. (!this.graph.cellEditor.isContentEditing() &&
  5307. !mxClient.IS_FF && !mxClient.IS_SF)) &&
  5308. isEventIgnored.apply(this, arguments));
  5309. };
  5310. // Ignores graph enabled state but not chromeless state
  5311. keyHandler.isEnabledForEvent = function(evt)
  5312. {
  5313. return (!mxEvent.isConsumed(evt) && this.isGraphEvent(evt) && this.isEnabled() &&
  5314. (editorUi.dialogs == null || editorUi.dialogs.length == 0));
  5315. };
  5316. // Routes command-key to control-key on Mac
  5317. keyHandler.isControlDown = function(evt)
  5318. {
  5319. return mxEvent.isControlDown(evt) || (mxClient.IS_MAC && evt.metaKey);
  5320. };
  5321. var thread = null;
  5322. // Helper function to move cells with the cursor keys
  5323. function nudge(keyCode, stepSize, resize)
  5324. {
  5325. if (!graph.isSelectionEmpty() && graph.isEnabled())
  5326. {
  5327. stepSize = (stepSize != null) ? stepSize : 1;
  5328. var cells = graph.getCompositeParents(graph.getSelectionCells());
  5329. var cell = (cells.length > 0) ? cells[0] : null;
  5330. if (cell != null)
  5331. {
  5332. if (resize)
  5333. {
  5334. // Resizes all selected vertices
  5335. graph.getModel().beginUpdate();
  5336. try
  5337. {
  5338. for (var i = 0; i < cells.length; i++)
  5339. {
  5340. if (graph.getModel().isVertex(cells[i]) && graph.isCellResizable(cells[i]))
  5341. {
  5342. var geo = graph.getCellGeometry(cells[i]);
  5343. if (geo != null)
  5344. {
  5345. geo = geo.clone();
  5346. if (keyCode == 37)
  5347. {
  5348. geo.width = Math.max(0, geo.width - stepSize);
  5349. }
  5350. else if (keyCode == 38)
  5351. {
  5352. geo.height = Math.max(0, geo.height - stepSize);
  5353. }
  5354. else if (keyCode == 39)
  5355. {
  5356. geo.width += stepSize;
  5357. }
  5358. else if (keyCode == 40)
  5359. {
  5360. geo.height += stepSize;
  5361. }
  5362. graph.getModel().setGeometry(cells[i], geo);
  5363. }
  5364. }
  5365. }
  5366. }
  5367. finally
  5368. {
  5369. graph.getModel().endUpdate();
  5370. }
  5371. }
  5372. else
  5373. {
  5374. // Moves vertices up/down in a stack layout
  5375. var parent = graph.model.getParent(cell);
  5376. var scale = graph.getView().scale;
  5377. var layout = null;
  5378. if (graph.getSelectionCount() == 1 && graph.model.isVertex(cell) &&
  5379. graph.layoutManager != null && !graph.isCellLocked(cell))
  5380. {
  5381. layout = graph.layoutManager.getLayout(parent);
  5382. }
  5383. if (layout != null && layout.constructor == mxStackLayout)
  5384. {
  5385. var index = parent.getIndex(cell);
  5386. if (keyCode == 37 || keyCode == 38)
  5387. {
  5388. graph.model.add(parent, cell, Math.max(0, index - 1));
  5389. }
  5390. else if (keyCode == 39 ||keyCode == 40)
  5391. {
  5392. graph.model.add(parent, cell, Math.min(graph.model.getChildCount(parent), index + 1));
  5393. }
  5394. }
  5395. else
  5396. {
  5397. var handler = graph.graphHandler;
  5398. if (handler != null)
  5399. {
  5400. if (handler.first == null)
  5401. {
  5402. handler.start(cell, 0, 0, graph.getMovableCells(cells));
  5403. }
  5404. if (handler.first != null)
  5405. {
  5406. var dx = 0;
  5407. var dy = 0;
  5408. if (keyCode == 37)
  5409. {
  5410. dx = -stepSize;
  5411. }
  5412. else if (keyCode == 38)
  5413. {
  5414. dy = -stepSize;
  5415. }
  5416. else if (keyCode == 39)
  5417. {
  5418. dx = stepSize;
  5419. }
  5420. else if (keyCode == 40)
  5421. {
  5422. dy = stepSize;
  5423. }
  5424. handler.currentDx += dx * scale;
  5425. handler.currentDy += dy * scale;
  5426. handler.checkPreview();
  5427. handler.updatePreview();
  5428. }
  5429. // Groups move steps in undoable change
  5430. if (thread != null)
  5431. {
  5432. window.clearTimeout(thread);
  5433. }
  5434. thread = window.setTimeout(function()
  5435. {
  5436. if (handler.first != null)
  5437. {
  5438. var dx = handler.roundLength(handler.currentDx / scale);
  5439. var dy = handler.roundLength(handler.currentDy / scale);
  5440. handler.moveCells(handler.cells, dx, dy);
  5441. handler.reset();
  5442. }
  5443. }, 400);
  5444. }
  5445. }
  5446. }
  5447. }
  5448. }
  5449. };
  5450. // Overridden to handle special alt+shift+cursor keyboard shortcuts
  5451. var directions = {37: mxConstants.DIRECTION_WEST, 38: mxConstants.DIRECTION_NORTH,
  5452. 39: mxConstants.DIRECTION_EAST, 40: mxConstants.DIRECTION_SOUTH};
  5453. var keyHandlerGetFunction = keyHandler.getFunction;
  5454. mxKeyHandler.prototype.getFunction = function(evt)
  5455. {
  5456. if (graph.isEnabled())
  5457. {
  5458. var action = null;
  5459. // TODO: Add alt modifier state in core API, here are some specific cases
  5460. if (mxEvent.isShiftDown(evt) && this.isControlDown(evt) && mxEvent.isAltDown(evt))
  5461. {
  5462. action = editorUi.actions.get(editorUi.ctrlAltShiftActions[evt.keyCode]);
  5463. }
  5464. else if (mxEvent.isShiftDown(evt) && mxEvent.isAltDown(evt))
  5465. {
  5466. action = editorUi.actions.get(editorUi.altShiftActions[evt.keyCode]);
  5467. }
  5468. else if (this.isControlDown(evt) && mxEvent.isAltDown(evt))
  5469. {
  5470. action = editorUi.actions.get(editorUi.ctrlAltActions[evt.keyCode]);
  5471. }
  5472. if (action != null)
  5473. {
  5474. return action.funct;
  5475. }
  5476. else if (directions[evt.keyCode] != null && !graph.isSelectionEmpty())
  5477. {
  5478. // On macOS, Control+Cursor is used by Expose so allow for Alt+Control to resize
  5479. if (!this.isControlDown(evt) && mxEvent.isShiftDown(evt) && mxEvent.isAltDown(evt))
  5480. {
  5481. if (graph.model.isVertex(graph.getSelectionCell()))
  5482. {
  5483. return function()
  5484. {
  5485. var cells = graph.connectVertex(graph.getSelectionCell(), directions[evt.keyCode],
  5486. graph.defaultEdgeLength, evt, true);
  5487. if (cells != null && cells.length > 0)
  5488. {
  5489. if (cells.length == 1 && graph.model.isEdge(cells[0]))
  5490. {
  5491. graph.setSelectionCell(graph.model.getTerminal(cells[0], false));
  5492. }
  5493. else
  5494. {
  5495. graph.setSelectionCell(cells[cells.length - 1]);
  5496. }
  5497. graph.scrollCellToVisible(graph.getSelectionCell());
  5498. if (editorUi.hoverIcons != null)
  5499. {
  5500. editorUi.hoverIcons.update(graph.view.getState(graph.getSelectionCell()));
  5501. }
  5502. }
  5503. };
  5504. }
  5505. }
  5506. else
  5507. {
  5508. // Avoids consuming event if no vertex is selected by returning null below
  5509. // Cursor keys move and resize (ctrl) cells
  5510. if (this.isControlDown(evt))
  5511. {
  5512. return function()
  5513. {
  5514. nudge(evt.keyCode, (mxEvent.isShiftDown(evt)) ? graph.gridSize : null, true);
  5515. };
  5516. }
  5517. else
  5518. {
  5519. return function()
  5520. {
  5521. nudge(evt.keyCode, (mxEvent.isShiftDown(evt)) ? graph.gridSize : null);
  5522. };
  5523. }
  5524. }
  5525. }
  5526. }
  5527. return keyHandlerGetFunction.apply(this, arguments);
  5528. };
  5529. // Binds keystrokes to actions
  5530. keyHandler.bindAction = mxUtils.bind(this, function(code, control, key, shift)
  5531. {
  5532. var action = this.actions.get(key);
  5533. if (action != null)
  5534. {
  5535. var f = function()
  5536. {
  5537. if (action.isEnabled())
  5538. {
  5539. action.funct.apply(this, arguments);
  5540. }
  5541. };
  5542. if (control)
  5543. {
  5544. if (shift)
  5545. {
  5546. keyHandler.bindControlShiftKey(code, f);
  5547. }
  5548. else
  5549. {
  5550. keyHandler.bindControlKey(code, f);
  5551. }
  5552. }
  5553. else
  5554. {
  5555. if (shift)
  5556. {
  5557. keyHandler.bindShiftKey(code, f);
  5558. }
  5559. else
  5560. {
  5561. keyHandler.bindKey(code, f);
  5562. }
  5563. }
  5564. }
  5565. });
  5566. var ui = this;
  5567. var keyHandlerEscape = keyHandler.escape;
  5568. keyHandler.escape = function(evt)
  5569. {
  5570. keyHandlerEscape.apply(this, arguments);
  5571. };
  5572. // Ignores enter keystroke. Remove this line if you want the
  5573. // enter keystroke to stop editing. N, W, T are reserved.
  5574. keyHandler.enter = function() {};
  5575. keyHandler.bindControlShiftKey(36, function() { graph.exitGroup(); }); // Ctrl+Shift+Home
  5576. keyHandler.bindControlShiftKey(35, function() { graph.enterGroup(); }); // Ctrl+Shift+End
  5577. keyHandler.bindShiftKey(36, function() { graph.home(); }); // Ctrl+Shift+Home
  5578. keyHandler.bindKey(35, function() { graph.refresh(); }); // End
  5579. keyHandler.bindAction(80, true, 'print'); // Ctrl+P
  5580. // Zoom keys are best effort for international keyboards, the actual
  5581. // US keycodes for + is 61 and - is 173. Keypad + is 107 and - is 109.
  5582. keyHandler.bindAction(107, true, 'zoomIn'); // Ctrl+Plus (Numpad)
  5583. keyHandler.bindAction(109, true, 'zoomOut'); // Ctrl+Minus (Numpad)
  5584. keyHandler.bindAction(61, true, 'zoomIn'); // Ctrl + tested by DB, firefox only.
  5585. keyHandler.bindAction(187, true, 'zoomIn'); // Ctrl + (US) tested by DB, chrome and desktop
  5586. keyHandler.bindAction(222, true, 'zoomIn'); // Ctrl Minus (CH: '/?) tested by GA, CH keyboard
  5587. keyHandler.bindAction(173, true, 'zoomOut'); // Ctrl - (US) tested by DB, firefox only.
  5588. keyHandler.bindAction(189, true, 'zoomOut'); // Ctrl Slash (CH: -/_) tested by DB, chrome and desktop
  5589. if (!this.editor.chromeless || this.editor.editable)
  5590. {
  5591. keyHandler.bindAction(79, true, 'outline', true); // Ctrl+Shift+O
  5592. keyHandler.bindControlKey(36, function() { if (graph.isEnabled()) { graph.foldCells(true); }}); // Ctrl+Home
  5593. keyHandler.bindControlKey(35, function() { if (graph.isEnabled()) { graph.foldCells(false); }}); // Ctrl+End
  5594. keyHandler.bindControlKey(13, function() { ui.ctrlEnter(); }); // Ctrl+Enter
  5595. keyHandler.bindAction(8, false, 'delete'); // Backspace
  5596. keyHandler.bindAction(8, true, 'deleteAll'); // Ctrl+Backspace
  5597. keyHandler.bindAction(8, false, 'deleteLabels', true); // Shift+Backspace
  5598. keyHandler.bindAction(46, false, 'delete'); // Delete
  5599. keyHandler.bindAction(46, true, 'deleteAll'); // Ctrl+Delete
  5600. keyHandler.bindAction(46, false, 'deleteLabels', true); // Shift+Delete
  5601. keyHandler.bindAction(36, false, 'resetView'); // Home
  5602. keyHandler.bindAction(72, true, 'fitWindow', true); // Ctrl+Shift+H
  5603. keyHandler.bindAction(74, true, 'fitPage'); // Ctrl+J
  5604. keyHandler.bindAction(74, true, 'fitTwoPages', true); // Ctrl+Shift+J
  5605. keyHandler.bindAction(48, true, 'customZoom'); // Ctrl+0
  5606. keyHandler.bindAction(82, true, 'turn'); // Ctrl+R
  5607. keyHandler.bindAction(82, true, 'clearDefaultStyle', true); // Ctrl+Shift+R
  5608. keyHandler.bindAction(83, true, 'save'); // Ctrl+S
  5609. keyHandler.bindAction(83, true, 'saveAs', true); // Ctrl+Shift+S
  5610. keyHandler.bindAction(65, true, 'selectAll'); // Ctrl+A
  5611. keyHandler.bindAction(65, true, 'selectNone', true); // Ctrl+A
  5612. keyHandler.bindAction(73, true, 'selectVertices', true); // Ctrl+Shift+I
  5613. keyHandler.bindAction(69, true, 'selectEdges', true); // Ctrl+Shift+E
  5614. keyHandler.bindAction(69, true, 'editStyle'); // Ctrl+E
  5615. keyHandler.bindAction(66, true, 'bold'); // Ctrl+B
  5616. keyHandler.bindAction(66, true, 'toBack', true); // Ctrl+Shift+B
  5617. keyHandler.bindAction(70, true, 'toFront', true); // Ctrl+Shift+F
  5618. keyHandler.bindAction(68, true, 'duplicate'); // Ctrl+D
  5619. keyHandler.bindAction(68, true, 'setAsDefaultStyle', true); // Ctrl+Shift+D
  5620. keyHandler.bindAction(90, true, 'undo'); // Ctrl+Z
  5621. keyHandler.bindAction(90, true, 'redo', true); // Ctrl+Shift+Z
  5622. keyHandler.bindAction(89, true, 'redo'); // Ctrl+Y
  5623. keyHandler.bindAction(89, true, 'autosize', true); // Ctrl+Shift+Y
  5624. keyHandler.bindAction(88, true, 'cut'); // Ctrl+X
  5625. keyHandler.bindAction(67, true, 'copy'); // Ctrl+C
  5626. keyHandler.bindAction(86, true, 'paste'); // Ctrl+V
  5627. keyHandler.bindAction(71, true, 'group'); // Ctrl+G
  5628. keyHandler.bindAction(77, true, 'editData'); // Ctrl+M
  5629. keyHandler.bindAction(71, true, 'grid', true); // Ctrl+Shift+G
  5630. keyHandler.bindAction(73, true, 'italic'); // Ctrl+I
  5631. keyHandler.bindAction(76, true, 'lockUnlock'); // Ctrl+L
  5632. keyHandler.bindAction(76, true, 'layers', true); // Ctrl+Shift+L
  5633. keyHandler.bindAction(80, true, 'format', true); // Ctrl+Shift+P
  5634. keyHandler.bindAction(85, true, 'underline'); // Ctrl+U
  5635. keyHandler.bindAction(85, true, 'ungroup', true); // Ctrl+Shift+U
  5636. keyHandler.bindAction(109, true, 'decreaseFontSize', true); // Ctrl+Shift+Minus
  5637. keyHandler.bindAction(107, true, 'increaseFontSize', true); // Ctrl+Shift+Plus
  5638. keyHandler.bindAction(219, true, 'decreaseFontSize', true); // Ctrl+{
  5639. keyHandler.bindAction(221, true, 'increaseFontSize', true); // Ctrl+}
  5640. keyHandler.bindAction(190, true, 'superscript'); // Ctrl+.
  5641. keyHandler.bindAction(188, true, 'subscript'); // Ctrl+,
  5642. keyHandler.bindAction(13, false, 'keyPressEnter'); // Enter
  5643. keyHandler.bindKey(113, function() { if (graph.isEnabled()) { graph.startEditingAtCell(); }}); // F2
  5644. }
  5645. return keyHandler;
  5646. };
  5647. /**
  5648. * Creates the keyboard event handler for the current graph and history.
  5649. */
  5650. EditorUi.prototype.createHelpIcon = function(href)
  5651. {
  5652. var link = document.createElement('div');
  5653. link.setAttribute('title', mxResources.get('help'));
  5654. link.style.display = 'inline';
  5655. link.style.marginLeft = '8px';
  5656. link.style.marginRight = '4px';
  5657. link.style.cursor = 'help';
  5658. var icon = document.createElement('img');
  5659. icon.setAttribute('src', Editor.helpImage);
  5660. icon.setAttribute('valign', 'middle');
  5661. icon.setAttribute('border', '0');
  5662. icon.className = 'geAdaptiveAsset';
  5663. mxUtils.setOpacity(icon, 60);
  5664. icon.style.marginTop = '-4px';
  5665. icon.style.height = '16px';
  5666. icon.style.width = '16px';
  5667. link.appendChild(icon);
  5668. mxEvent.addListener(link, 'click', mxUtils.bind(this, function(evt)
  5669. {
  5670. this.hideCurrentMenu();
  5671. this.openLink(href);
  5672. mxEvent.consume(evt);
  5673. }));
  5674. return link;
  5675. };
  5676. /**
  5677. * Creates the keyboard event handler for the current graph and history.
  5678. */
  5679. EditorUi.prototype.destroy = function()
  5680. {
  5681. var graph = this.editor.graph;
  5682. if (graph != null && this.selectionStateListener != null)
  5683. {
  5684. graph.getSelectionModel().removeListener(this.selectionStateListener);
  5685. graph.getModel().removeListener(this.selectionStateListener);
  5686. graph.getView().removeListener(this.selectionStateListener);
  5687. graph.removeListener(this.selectionStateListener);
  5688. this.selectionStateListener = null;
  5689. }
  5690. if (this.editor != null)
  5691. {
  5692. this.editor.destroy();
  5693. this.editor = null;
  5694. }
  5695. if (this.menubar != null)
  5696. {
  5697. this.menubar.destroy();
  5698. this.menubar = null;
  5699. }
  5700. if (this.toolbar != null)
  5701. {
  5702. this.toolbar.destroy();
  5703. this.toolbar = null;
  5704. }
  5705. if (this.sidebar != null)
  5706. {
  5707. this.sidebar.destroy();
  5708. this.sidebar = null;
  5709. }
  5710. if (this.keyHandler != null)
  5711. {
  5712. this.keyHandler.destroy();
  5713. this.keyHandler = null;
  5714. }
  5715. if (this.keydownHandler != null)
  5716. {
  5717. mxEvent.removeListener(document, 'keydown', this.keydownHandler);
  5718. this.keydownHandler = null;
  5719. }
  5720. if (this.keyupHandler != null)
  5721. {
  5722. mxEvent.removeListener(document, 'keyup', this.keyupHandler);
  5723. this.keyupHandler = null;
  5724. }
  5725. if (this.resizeHandler != null)
  5726. {
  5727. mxEvent.removeListener(window, 'resize', this.resizeHandler);
  5728. this.resizeHandler = null;
  5729. }
  5730. if (this.gestureHandler != null)
  5731. {
  5732. mxEvent.removeGestureListeners(document, this.gestureHandler);
  5733. this.gestureHandler = null;
  5734. }
  5735. if (this.orientationChangeHandler != null)
  5736. {
  5737. mxEvent.removeListener(window, 'orientationchange', this.orientationChangeHandler);
  5738. this.orientationChangeHandler = null;
  5739. }
  5740. if (this.scrollHandler != null)
  5741. {
  5742. mxEvent.removeListener(window, 'scroll', this.scrollHandler);
  5743. this.scrollHandler = null;
  5744. }
  5745. if (this.destroyFunctions != null)
  5746. {
  5747. for (var i = 0; i < this.destroyFunctions.length; i++)
  5748. {
  5749. this.destroyFunctions[i]();
  5750. }
  5751. this.destroyFunctions = null;
  5752. }
  5753. var c = [this.menubarContainer, this.toolbarContainer, this.sidebarContainer,
  5754. this.formatContainer, this.diagramContainer, this.footerContainer,
  5755. this.chromelessToolbar, this.hsplit, this.layersDialog];
  5756. for (var i = 0; i < c.length; i++)
  5757. {
  5758. if (c[i] != null && c[i].parentNode != null)
  5759. {
  5760. c[i].parentNode.removeChild(c[i]);
  5761. }
  5762. }
  5763. };