EditorUi.js 182 KB

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