EditorUi.js 196 KB

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