EditorUi.js 180 KB

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