svg-editor-copy-ok.js 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121
  1. /*globals globalStorage, widget, svgEditor, svgedit, canvg, DOMParser, FileReader, jQuery, $ */
  2. /*jslint vars: true, eqeq: true, todo: true, forin: true, continue: true, regexp: true */
  3. /*
  4. * svg-editor.js
  5. *
  6. * Licensed under the MIT License
  7. *
  8. * Copyright(c) 2010 Alexis Deveria
  9. * Copyright(c) 2010 Pavol Rusnak
  10. * Copyright(c) 2010 Jeff Schiller
  11. * Copyright(c) 2010 Narendra Sisodiya
  12. *
  13. */
  14. // Dependencies:
  15. // 1) units.js
  16. // 2) browser.js
  17. // 3) svgcanvas.js
  18. /*
  19. TO-DOS
  20. 1. JSDoc
  21. */
  22. (function() {
  23. if (window.svgEditor) {
  24. return;
  25. }
  26. window.svgEditor = (function($) {
  27. var editor = {};
  28. // EDITOR PROPERTIES: (defined below)
  29. // curPrefs, curConfig, canvas, storage, uiStrings
  30. //
  31. // STATE MAINTENANCE PROPERTIES
  32. editor.tool_scale = 1; // Dependent on icon size, so no need to make configurable?
  33. editor.langChanged = false;
  34. editor.showSaveWarning = false;
  35. editor.storagePromptClosed = false; // For use with ext-storage.js
  36. var svgCanvas, urldata,
  37. isReady = false,
  38. callbacks = [],
  39. customHandlers = {},
  40. /**
  41. * PREFS AND CONFIG
  42. */
  43. // The iteration algorithm for defaultPrefs does not currently support array/objects
  44. defaultPrefs = {
  45. // EDITOR OPTIONS (DIALOG)
  46. lang: '', // Default to "en" if locale.js detection does not detect another language
  47. iconsize: '', // Will default to 's' if the window height is smaller than the minimum height and 'm' otherwise
  48. bkgd_color: '#FFF',
  49. bkgd_url: '',
  50. // DOCUMENT PROPERTIES (DIALOG)
  51. img_save: 'embed',
  52. // ALERT NOTICES
  53. // Only shows in UI as far as alert notices, but useful to remember, so keeping as pref
  54. save_notice_done: false,
  55. export_notice_done: false
  56. },
  57. curPrefs = {},
  58. // Note: The difference between Prefs and Config is that Prefs
  59. // can be changed in the UI and are stored in the browser,
  60. // while config cannot
  61. curConfig = {
  62. // We do not put on defaultConfig to simplify object copying
  63. // procedures (we obtain instead from defaultExtensions)
  64. extensions: []
  65. },
  66. defaultExtensions = [
  67. 'ext-overview_window.js',
  68. 'ext-markers.js',
  69. 'ext-connector.js',
  70. 'ext-eyedropper.js',
  71. 'ext-shapes.js',
  72. 'ext-imagelib.js',
  73. 'ext-grid.js',
  74. 'ext-polygon.js',
  75. 'ext-star.js',
  76. 'ext-panning.js',
  77. 'ext-storage.js'
  78. ],
  79. defaultConfig = {
  80. // Todo: svgcanvas.js also sets and checks: show_outside_canvas, selectNew; add here?
  81. // Change the following to preferences and add pref controls to the UI (e.g., initTool, wireframe, showlayers)?
  82. canvasName: 'default',
  83. canvas_expansion: 3,
  84. initFill: {
  85. color: 'FF0000', // solid red
  86. opacity: 1
  87. },
  88. initStroke: {
  89. width: 5,
  90. color: '000000', // solid black
  91. opacity: 1
  92. },
  93. initOpacity: 1,
  94. colorPickerCSS: null,
  95. initTool: 'select',
  96. wireframe: false,
  97. showlayers: false,
  98. no_save_warning: false,
  99. // PATH CONFIGURATION
  100. // The following path configuration items are disallowed in the URL (as should any future path configurations)
  101. imgPath: 'images/',
  102. langPath: 'locale/',
  103. extPath: 'extensions/',
  104. jGraduatePath: 'jgraduate/images/',
  105. // DOCUMENT PROPERTIES
  106. // Change the following to a preference (already in the Document Properties dialog)?
  107. dimensions: [640, 480],
  108. // EDITOR OPTIONS
  109. // Change the following to preferences (already in the Editor Options dialog)?
  110. gridSnapping: false,
  111. gridColor: '#000',
  112. baseUnit: 'px',
  113. snappingStep: 10,
  114. showRulers: true,
  115. // URL BEHAVIOR CONFIGURATION
  116. preventAllURLConfig: false,
  117. preventURLContentLoading: false,
  118. // EXTENSION CONFIGURATION (see also preventAllURLConfig)
  119. lockExtensions: false, // Disallowed in URL setting
  120. noDefaultExtensions: false, // noDefaultExtensions can only be meaningfully used in config.js or in the URL
  121. // EXTENSION-RELATED (GRID)
  122. showGrid: false, // Set by ext-grid.js
  123. // EXTENSION-RELATED (STORAGE)
  124. noStorageOnLoad: false, // Some interaction with ext-storage.js; prevent even the loading of previously saved local storage
  125. forceStorage: false, // Some interaction with ext-storage.js; strongly discouraged from modification as it bypasses user privacy by preventing them from choosing whether to keep local storage or not
  126. emptyStorageOnDecline: false // Used by ext-storage.js; empty any prior storage if the user declines to store
  127. },
  128. /**
  129. * LOCALE
  130. * @todo Can we remove now that we are always loading even English? (unless locale is set to null)
  131. */
  132. uiStrings = editor.uiStrings = {
  133. common: {
  134. ok: 'OK',
  135. cancel: 'Cancel',
  136. key_up: 'Up',
  137. key_down: 'Down',
  138. key_backspace: 'Backspace',
  139. key_del: 'Del'
  140. },
  141. // This is needed if the locale is English, since the locale strings are not read in that instance.
  142. layers: {
  143. layer: 'Layer'
  144. },
  145. notification: {
  146. invalidAttrValGiven: 'Invalid value given',
  147. noContentToFitTo: 'No content to fit to',
  148. dupeLayerName: 'There is already a layer named that!',
  149. enterUniqueLayerName: 'Please enter a unique layer name',
  150. enterNewLayerName: 'Please enter the new layer name',
  151. layerHasThatName: 'Layer already has that name',
  152. QmoveElemsToLayer: 'Move selected elements to layer \'%s\'?',
  153. QwantToClear: 'Do you want to clear the drawing?\nThis will also erase your undo history!',
  154. QwantToOpen: 'Do you want to open a new file?\nThis will also erase your undo history!',
  155. QerrorsRevertToSource: 'There were parsing errors in your SVG source.\nRevert back to original SVG source?',
  156. QignoreSourceChanges: 'Ignore changes made to SVG source?',
  157. featNotSupported: 'Feature not supported',
  158. enterNewImgURL: 'Enter the new image URL',
  159. defsFailOnSave: 'NOTE: Due to a bug in your browser, this image may appear wrong (missing gradients or elements). It will however appear correct once actually saved.',
  160. loadingImage: 'Loading image, please wait...',
  161. saveFromBrowser: 'Select \'Save As...\' in your browser to save this image as a %s file.',
  162. noteTheseIssues: 'Also note the following issues: ',
  163. unsavedChanges: 'There are unsaved changes.',
  164. enterNewLinkURL: 'Enter the new hyperlink URL',
  165. errorLoadingSVG: 'Error: Unable to load SVG data',
  166. URLloadFail: 'Unable to load from URL',
  167. retrieving: 'Retrieving \'%s\' ...'
  168. }
  169. };
  170. function loadSvgString (str, callback) {
  171. var success = svgCanvas.setSvgString(str) !== false;
  172. callback = callback || $.noop;
  173. if (success) {
  174. callback(true);
  175. } else {
  176. $.alert(uiStrings.notification.errorLoadingSVG, function() {
  177. callback(false);
  178. });
  179. }
  180. }
  181. /**
  182. * EXPORTS
  183. */
  184. /**
  185. * Store and retrieve preferences
  186. * @param {string} key The preference name to be retrieved or set
  187. * @param {string} [val] The value. If the value supplied is missing or falsey, no change to the preference will be made.
  188. * @returns {string} If val is missing or falsey, the value of the previously stored preference will be returned.
  189. * @todo Can we change setting on the jQuery namespace (onto editor) to avoid conflicts?
  190. * @todo Review whether any remaining existing direct references to
  191. * getting curPrefs can be changed to use $.pref() getting to ensure
  192. * defaultPrefs fallback (also for sake of allowInitialUserOverride); specifically, bkgd_color could be changed so that
  193. * the pref dialog has a button to auto-calculate background, but otherwise uses $.pref() to be able to get default prefs
  194. * or overridable settings
  195. */
  196. $.pref = function (key, val) {
  197. if (val) {
  198. curPrefs[key] = val;
  199. editor.curPrefs = curPrefs; // Update exported value
  200. return;
  201. }
  202. return (key in curPrefs) ? curPrefs[key] : defaultPrefs[key];
  203. };
  204. /**
  205. * EDITOR PUBLIC METHODS
  206. * @todo Sort these methods per invocation order, ideally with init at the end
  207. * @todo Prevent execution until init executes if dependent on it?
  208. */
  209. /**
  210. * Where permitted, sets canvas and/or defaultPrefs based on previous
  211. * storage. This will override URL settings (for security reasons) but
  212. * not config.js configuration (unless initial user overriding is explicitly
  213. * permitted there via allowInitialUserOverride).
  214. * @todo Split allowInitialUserOverride into allowOverrideByURL and
  215. * allowOverrideByUserStorage so config.js can disallow some
  216. * individual items for URL setting but allow for user storage AND/OR
  217. * change URL setting so that it always uses a different namespace,
  218. * so it won't affect pre-existing user storage (but then if users saves
  219. * that, it will then be subject to tampering
  220. */
  221. editor.loadContentAndPrefs = function () {
  222. if (!curConfig.forceStorage && (curConfig.noStorageOnLoad || !document.cookie.match(/(?:^|;\s*)store=(?:prefsAndContent|prefsOnly)/))) {
  223. return;
  224. }
  225. // LOAD CONTENT
  226. if ('localStorage' in window && // Cookies do not have enough available memory to hold large documents
  227. (curConfig.forceStorage || (!curConfig.noStorageOnLoad && document.cookie.match(/(?:^|;\s*)store=prefsAndContent/)))
  228. ) {
  229. var name = 'svgedit-' + curConfig.canvasName;
  230. var cached = window.localStorage.getItem(name);
  231. if (cached) {
  232. editor.loadFromString(cached);
  233. }
  234. }
  235. // LOAD PREFS
  236. var key, storage = false;
  237. // var host = location.hostname,
  238. // onWeb = host && host.indexOf('.') >= 0;
  239. // Some FF versions throw security errors here
  240. try {
  241. if (window.localStorage) { // && onWeb removed so Webkit works locally
  242. storage = localStorage;
  243. }
  244. } catch(err) {}
  245. editor.storage = storage;
  246. for (key in defaultPrefs) {
  247. if (defaultPrefs.hasOwnProperty(key)) { // It's our own config, so we don't need to iterate up the prototype chain
  248. var storeKey = 'svg-edit-' + key;
  249. if (storage) {
  250. var val = storage.getItem(storeKey);
  251. if (val) {
  252. defaultPrefs[key] = String(val); // Convert to string for FF (.value fails in Webkit)
  253. }
  254. }
  255. else if (window.widget) {
  256. defaultPrefs[key] = widget.preferenceForKey(storeKey);
  257. }
  258. else {
  259. var result = document.cookie.match(new RegExp('(?:^|;\\s*)' + storeKey + '=([^;]+)'));
  260. defaultPrefs[key] = result ? decodeURIComponent(result[1]) : '';
  261. }
  262. }
  263. }
  264. };
  265. /**
  266. * Allows setting of preferences or configuration (including extensions).
  267. * @param {object} opts The preferences or configuration (including extensions)
  268. * @param {object} [cfgCfg] Describes configuration which applies to the particular batch of supplied options
  269. * @param {boolean} [cfgCfg.allowInitialUserOverride=false] Set to true if you wish
  270. * to allow initial overriding of settings by the user via the URL
  271. * (if permitted) or previously stored preferences (if permitted);
  272. * note that it will be too late if you make such calls in extension
  273. * code because the URL or preference storage settings will
  274. * have already taken place.
  275. * @param {boolean} [cfgCfg.overwrite=true] Set to false if you wish to
  276. * prevent the overwriting of prior-set preferences or configuration
  277. * (URL settings will always follow this requirement for security
  278. * reasons, so config.js settings cannot be overridden unless it
  279. * explicitly permits via "allowInitialUserOverride" but extension config
  280. * can be overridden as they will run after URL settings). Should
  281. * not be needed in config.js.
  282. */
  283. editor.setConfig = function (opts, cfgCfg) {
  284. cfgCfg = cfgCfg || {};
  285. function extendOrAdd (cfgObj, key, val) {
  286. if (cfgObj[key] && typeof cfgObj[key] === 'object') {
  287. $.extend(true, cfgObj[key], val);
  288. }
  289. else {
  290. cfgObj[key] = val;
  291. }
  292. return;
  293. }
  294. $.each(opts, function(key, val) {
  295. if (opts.hasOwnProperty(key)) {
  296. // Only allow prefs defined in defaultPrefs
  297. if (defaultPrefs.hasOwnProperty(key)) {
  298. if (cfgCfg.overwrite === false && (
  299. curConfig.preventAllURLConfig ||
  300. curPrefs.hasOwnProperty(key)
  301. )) {
  302. return;
  303. }
  304. if (cfgCfg.allowInitialUserOverride === true) {
  305. defaultPrefs[key] = val;
  306. }
  307. else {
  308. $.pref(key, val);
  309. }
  310. }
  311. else if (key === 'extensions') {
  312. if (cfgCfg.overwrite === false &&
  313. (curConfig.preventAllURLConfig || curConfig.lockExtensions)
  314. ) {
  315. return;
  316. }
  317. curConfig.extensions = curConfig.extensions.concat(val); // We will handle any dupes later
  318. }
  319. // Only allow other curConfig if defined in defaultConfig
  320. else if (defaultConfig.hasOwnProperty(key)) {
  321. if (cfgCfg.overwrite === false && (
  322. curConfig.preventAllURLConfig ||
  323. curConfig.hasOwnProperty(key)
  324. )) {
  325. return;
  326. }
  327. // Potentially overwriting of previously set config
  328. if (curConfig.hasOwnProperty(key)) {
  329. if (cfgCfg.overwrite === false) {
  330. return;
  331. }
  332. extendOrAdd(curConfig, key, val);
  333. }
  334. else {
  335. if (cfgCfg.allowInitialUserOverride === true) {
  336. extendOrAdd(defaultConfig, key, val);
  337. }
  338. else {
  339. if (defaultConfig[key] && typeof defaultConfig[key] === 'object') {
  340. curConfig[key] = {};
  341. $.extend(true, curConfig[key], val); // Merge properties recursively, e.g., on initFill, initStroke objects
  342. }
  343. else {
  344. curConfig[key] = val;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. });
  351. editor.curConfig = curConfig; // Update exported value
  352. };
  353. /**
  354. * @param {object} opts Extension mechanisms may call setCustomHandlers with three functions: opts.open, opts.save, and opts.exportImage
  355. * opts.open's responsibilities are:
  356. * - invoke a file chooser dialog in 'open' mode
  357. * - let user pick a SVG file
  358. * - calls setCanvas.setSvgString() with the string contents of that file
  359. * opts.save's responsibilities are:
  360. * - accept the string contents of the current document
  361. * - invoke a file chooser dialog in 'save' mode
  362. * - save the file to location chosen by the user
  363. * opts.exportImage's responsibilities (with regard to the object it is supplied in its 2nd argument) are:
  364. * - inform user of any issues supplied via the "issues" property
  365. * - convert the "svg" property SVG string into an image for export;
  366. * utilize the properties "type" (currently 'PNG', 'JPEG', 'BMP',
  367. * 'WEBP'), "mimeType", and "quality" (for 'JPEG' and 'WEBP'
  368. * types) to determine the proper output.
  369. */
  370. editor.setCustomHandlers = function (opts) {
  371. editor.ready(function() {
  372. if (opts.open) {
  373. $('#tool_open > input[type="file"]').remove();
  374. $('#tool_open').show();
  375. svgCanvas.open = opts.open;
  376. }
  377. if (opts.save) {
  378. editor.showSaveWarning = false;
  379. svgCanvas.bind('saved', opts.save);
  380. }
  381. if (opts.exportImage || opts.pngsave) { // Deprecating pngsave
  382. svgCanvas.bind('exported', opts.exportImage || opts.pngsave);
  383. }
  384. customHandlers = opts;
  385. });
  386. };
  387. editor.randomizeIds = function () {
  388. svgCanvas.randomizeIds(arguments);
  389. };
  390. editor.init = function () {
  391. // Todo: Avoid var-defined functions and group functions together, etc. where possible
  392. var good_langs = [];
  393. $('#lang_select option').each(function() {
  394. good_langs.push(this.value);
  395. });
  396. function setupCurPrefs () {
  397. curPrefs = $.extend(true, {}, defaultPrefs, curPrefs); // Now safe to merge with priority for curPrefs in the event any are already set
  398. // Export updated prefs
  399. editor.curPrefs = curPrefs;
  400. }
  401. function setupCurConfig () {
  402. curConfig = $.extend(true, {}, defaultConfig, curConfig); // Now safe to merge with priority for curConfig in the event any are already set
  403. // Now deal with extensions
  404. if (!curConfig.noDefaultExtensions) {
  405. curConfig.extensions = curConfig.extensions.concat(defaultExtensions);
  406. }
  407. // ...and remove any dupes
  408. curConfig.extensions = $.grep(curConfig.extensions, function (n, i) {
  409. return i === curConfig.extensions.indexOf(n);
  410. });
  411. // Export updated config
  412. editor.curConfig = curConfig;
  413. }
  414. (function() {
  415. // Load config/data from URL if given
  416. var src, qstr;
  417. urldata = $.deparam.querystring(true);
  418. if (!$.isEmptyObject(urldata)) {
  419. if (urldata.dimensions) {
  420. urldata.dimensions = urldata.dimensions.split(',');
  421. }
  422. if (urldata.bkgd_color) {
  423. urldata.bkgd_color = '#' + urldata.bkgd_color;
  424. }
  425. if (urldata.extensions) {
  426. // For security reasons, disallow cross-domain or cross-folder extensions via URL
  427. urldata.extensions = urldata.extensions.match(/[:\/\\]/) ? '' : urldata.extensions.split(',');
  428. }
  429. // Disallowing extension paths via URL for
  430. // security reasons, even for same-domain
  431. // ones given potential to interact in undesirable
  432. // ways with other script resources
  433. $.each(
  434. [
  435. 'extPath', 'imgPath',
  436. 'langPath', 'jGraduatePath'
  437. ],
  438. function (pathConfig) {
  439. if (urldata[pathConfig]) {
  440. delete urldata[pathConfig];
  441. }
  442. }
  443. );
  444. svgEditor.setConfig(urldata, {overwrite: false}); // Note: source, url, and paramurl (as with storagePrompt later) are not set on config but are used below
  445. setupCurConfig();
  446. if (!curConfig.preventURLContentLoading) {
  447. src = urldata.source;
  448. qstr = $.param.querystring();
  449. if (!src) { // urldata.source may have been null if it ended with '='
  450. if (qstr.indexOf('source=data:') >= 0) {
  451. src = qstr.match(/source=(data:[^&]*)/)[1];
  452. }
  453. }
  454. if (src) {
  455. if (src.indexOf('data:') === 0) {
  456. // plusses get replaced by spaces, so re-insert
  457. src = src.replace(/ /g, '+');
  458. editor.loadFromDataURI(src);
  459. } else {
  460. editor.loadFromString(src);
  461. }
  462. return;
  463. }
  464. if (qstr.indexOf('paramurl=') !== -1) {
  465. // Get parameter URL (use full length of remaining location.href)
  466. svgEditor.loadFromURL(qstr.substr(9));
  467. return;
  468. }
  469. if (urldata.url) {
  470. svgEditor.loadFromURL(urldata.url);
  471. return;
  472. }
  473. }
  474. if (!urldata.noStorageOnLoad || curConfig.forceStorage) {
  475. svgEditor.loadContentAndPrefs();
  476. }
  477. setupCurPrefs();
  478. }
  479. else {
  480. setupCurConfig();
  481. svgEditor.loadContentAndPrefs();
  482. setupCurPrefs();
  483. }
  484. }());
  485. // For external openers
  486. (function() {
  487. // let the opener know SVG Edit is ready (now that config is set up)
  488. var svgEditorReadyEvent,
  489. w = window.opener;
  490. if (w) {
  491. try {
  492. svgEditorReadyEvent = w.document.createEvent('Event');
  493. svgEditorReadyEvent.initEvent('svgEditorReady', true, true);
  494. w.document.documentElement.dispatchEvent(svgEditorReadyEvent);
  495. }
  496. catch(e) {}
  497. }
  498. }());
  499. var setIcon = editor.setIcon = function(elem, icon_id, forcedSize) {
  500. var icon = (typeof icon_id === 'string') ? $.getSvgIcon(icon_id, true) : icon_id.clone();
  501. if (!icon) {
  502. console.log('NOTE: Icon image missing: ' + icon_id);
  503. return;
  504. }
  505. $(elem).empty().append(icon);
  506. };
  507. var extFunc = function() {
  508. $.each(curConfig.extensions, function() {
  509. var extname = this;
  510. $.getScript(curConfig.extPath + extname, function(d) {
  511. // Fails locally in Chrome 5
  512. if (!d) {
  513. var s = document.createElement('script');
  514. s.src = curConfig.extPath + extname;
  515. document.querySelector('head').appendChild(s);
  516. }
  517. });
  518. });
  519. // var lang = ('lang' in curPrefs) ? curPrefs.lang : null;
  520. editor.putLocale(null, good_langs);
  521. };
  522. // Load extensions
  523. // Bit of a hack to run extensions in local Opera/IE9
  524. if (document.location.protocol === 'file:') {
  525. setTimeout(extFunc, 100);
  526. } else {
  527. extFunc();
  528. }
  529. $.svgIcons(curConfig.imgPath + 'svg_edit_icons.svg', {
  530. w:24, h:24,
  531. id_match: false,
  532. no_img: !svgedit.browser.isWebkit(), // Opera & Firefox 4 gives odd behavior w/images
  533. fallback_path: curConfig.imgPath,
  534. fallback: {
  535. 'new_image': 'clear.png',
  536. 'save': 'save.png',
  537. 'open': 'open.png',
  538. 'source': 'source.png',
  539. 'docprops': 'document-properties.png',
  540. 'wireframe': 'wireframe.png',
  541. 'undo': 'undo.png',
  542. 'redo': 'redo.png',
  543. 'select': 'select.png',
  544. 'select_node': 'select_node.png',
  545. 'pencil': 'fhpath.png',
  546. 'pen': 'line.png',
  547. 'square': 'square.png',
  548. 'rect': 'rect.png',
  549. 'fh_rect': 'freehand-square.png',
  550. 'circle': 'circle.png',
  551. 'ellipse': 'ellipse.png',
  552. 'fh_ellipse': 'freehand-circle.png',
  553. 'path': 'path.png',
  554. 'text': 'text.png',
  555. 'image': 'image.png',
  556. 'zoom': 'zoom.png',
  557. 'clone': 'clone.png',
  558. 'node_clone': 'node_clone.png',
  559. 'delete': 'delete.png',
  560. 'node_delete': 'node_delete.png',
  561. 'group': 'shape_group_elements.png',
  562. 'ungroup': 'shape_ungroup.png',
  563. 'move_top': 'move_top.png',
  564. 'move_bottom': 'move_bottom.png',
  565. 'to_path': 'to_path.png',
  566. 'link_controls': 'link_controls.png',
  567. 'reorient': 'reorient.png',
  568. 'align_left': 'align-left.png',
  569. 'align_center': 'align-center.png',
  570. 'align_right': 'align-right.png',
  571. 'align_top': 'align-top.png',
  572. 'align_middle': 'align-middle.png',
  573. 'align_bottom': 'align-bottom.png',
  574. 'go_up': 'go-up.png',
  575. 'go_down': 'go-down.png',
  576. 'ok': 'save.png',
  577. 'cancel': 'cancel.png',
  578. 'arrow_right': 'flyouth.png',
  579. 'arrow_down': 'dropdown.gif'
  580. },
  581. placement: {
  582. '#logo': 'logo',
  583. '#tool_clear div,#layer_new': 'new_image',
  584. '#tool_save div': 'save',
  585. '#tool_export div': 'export',
  586. '#tool_open div div': 'open',
  587. '#tool_import div div': 'import',
  588. '#tool_source': 'source',
  589. '#tool_docprops > div': 'docprops',
  590. '#tool_wireframe': 'wireframe',
  591. '#tool_undo': 'undo',
  592. '#tool_redo': 'redo',
  593. '#tool_select': 'select',
  594. '#tool_fhpath': 'pencil',
  595. '#tool_line': 'pen',
  596. '#tool_rect,#tools_rect_show': 'rect',
  597. '#tool_square': 'square',
  598. '#tool_fhrect': 'fh_rect',
  599. '#tool_ellipse,#tools_ellipse_show': 'ellipse',
  600. '#tool_circle': 'circle',
  601. '#tool_fhellipse': 'fh_ellipse',
  602. '#tool_path': 'path',
  603. '#tool_text,#layer_rename': 'text',
  604. '#tool_image': 'image',
  605. '#tool_zoom': 'zoom',
  606. '#tool_clone,#tool_clone_multi': 'clone',
  607. '#tool_node_clone': 'node_clone',
  608. '#layer_delete,#tool_delete,#tool_delete_multi': 'delete',
  609. '#tool_node_delete': 'node_delete',
  610. '#tool_add_subpath': 'add_subpath',
  611. '#tool_openclose_path': 'open_path',
  612. '#tool_move_top': 'move_top',
  613. '#tool_move_bottom': 'move_bottom',
  614. '#tool_topath': 'to_path',
  615. '#tool_node_link': 'link_controls',
  616. '#tool_reorient': 'reorient',
  617. '#tool_group_elements': 'group_elements',
  618. '#tool_ungroup': 'ungroup',
  619. '#tool_unlink_use': 'unlink_use',
  620. '#tool_alignleft, #tool_posleft': 'align_left',
  621. '#tool_aligncenter, #tool_poscenter': 'align_center',
  622. '#tool_alignright, #tool_posright': 'align_right',
  623. '#tool_aligntop, #tool_postop': 'align_top',
  624. '#tool_alignmiddle, #tool_posmiddle': 'align_middle',
  625. '#tool_alignbottom, #tool_posbottom': 'align_bottom',
  626. '#cur_position': 'align',
  627. '#linecap_butt,#cur_linecap': 'linecap_butt',
  628. '#linecap_round': 'linecap_round',
  629. '#linecap_square': 'linecap_square',
  630. '#linejoin_miter,#cur_linejoin': 'linejoin_miter',
  631. '#linejoin_round': 'linejoin_round',
  632. '#linejoin_bevel': 'linejoin_bevel',
  633. '#url_notice': 'warning',
  634. '#layer_up': 'go_up',
  635. '#layer_down': 'go_down',
  636. '#layer_moreopts': 'context_menu',
  637. '#layerlist td.layervis': 'eye',
  638. '#tool_source_save,#tool_docprops_save,#tool_prefs_save': 'ok',
  639. '#tool_source_cancel,#tool_docprops_cancel,#tool_prefs_cancel': 'cancel',
  640. '#rwidthLabel, #iwidthLabel': 'width',
  641. '#rheightLabel, #iheightLabel': 'height',
  642. '#cornerRadiusLabel span': 'c_radius',
  643. '#angleLabel': 'angle',
  644. '#linkLabel,#tool_make_link,#tool_make_link_multi': 'globe_link',
  645. '#zoomLabel': 'zoom',
  646. '#tool_fill label': 'fill',
  647. '#tool_stroke .icon_label': 'stroke',
  648. '#group_opacityLabel': 'opacity',
  649. '#blurLabel': 'blur',
  650. '#font_sizeLabel': 'fontsize',
  651. '.flyout_arrow_horiz': 'arrow_right',
  652. '.dropdown button, #main_button .dropdown': 'arrow_down',
  653. '#palette .palette_item:first, #fill_bg, #stroke_bg': 'no_color'
  654. },
  655. resize: {
  656. '#logo .svg_icon': 28,
  657. '.flyout_arrow_horiz .svg_icon': 5,
  658. '.layer_button .svg_icon, #layerlist td.layervis .svg_icon': 14,
  659. '.dropdown button .svg_icon': 7,
  660. '#main_button .dropdown .svg_icon': 9,
  661. '.palette_item:first .svg_icon' : 15,
  662. '#fill_bg .svg_icon, #stroke_bg .svg_icon': 16,
  663. '.toolbar_button button .svg_icon': 16,
  664. '.stroke_tool div div .svg_icon': 20,
  665. '#tools_bottom label .svg_icon': 18
  666. },
  667. callback: function(icons) {
  668. $('.toolbar_button button > svg, .toolbar_button button > img').each(function() {
  669. $(this).parent().prepend(this);
  670. });
  671. var min_height,
  672. tleft = $('#tools_left');
  673. if (tleft.length !== 0) {
  674. min_height = tleft.offset().top + tleft.outerHeight();
  675. }
  676. var size = $.pref('iconsize');
  677. svgEditor.setIconSize(size || ($(window).height() < min_height ? 's': 'm'));
  678. // Look for any missing flyout icons from plugins
  679. $('.tools_flyout').each(function() {
  680. var shower = $('#' + this.id + '_show');
  681. var sel = shower.attr('data-curopt');
  682. // Check if there's an icon here
  683. if (!shower.children('svg, img').length) {
  684. var clone = $(sel).children().clone();
  685. if (clone.length) {
  686. clone[0].removeAttribute('style'); //Needed for Opera
  687. shower.append(clone);
  688. }
  689. }
  690. });
  691. svgEditor.runCallbacks();
  692. setTimeout(function() {
  693. $('.flyout_arrow_horiz:empty').each(function() {
  694. $(this).append($.getSvgIcon('arrow_right').width(5).height(5));
  695. });
  696. }, 1);
  697. }
  698. });
  699. editor.canvas = svgCanvas = new $.SvgCanvas(document.getElementById('svgcanvas'), curConfig);
  700. var supportsNonSS, resize_timer, changeZoom, Actions, curScrollPos,
  701. palette = [ // Todo: Make into configuration item?
  702. '#000000', '#3f3f3f', '#7f7f7f', '#bfbfbf', '#ffffff',
  703. '#ff0000', '#ff7f00', '#ffff00', '#7fff00',
  704. '#00ff00', '#00ff7f', '#00ffff', '#007fff',
  705. '#0000ff', '#7f00ff', '#ff00ff', '#ff007f',
  706. '#7f0000', '#7f3f00', '#7f7f00', '#3f7f00',
  707. '#007f00', '#007f3f', '#007f7f', '#003f7f',
  708. '#00007f', '#3f007f', '#7f007f', '#7f003f',
  709. '#ffaaaa', '#ffd4aa', '#ffffaa', '#d4ffaa',
  710. '#aaffaa', '#aaffd4', '#aaffff', '#aad4ff',
  711. '#aaaaff', '#d4aaff', '#ffaaff', '#ffaad4'
  712. ],
  713. modKey = (svgedit.browser.isMac() ? 'meta+' : 'ctrl+'), // ⌘
  714. path = svgCanvas.pathActions,
  715. undoMgr = svgCanvas.undoMgr,
  716. Utils = svgedit.utilities,
  717. defaultImageURL = curConfig.imgPath + 'logo.png',
  718. workarea = $('#workarea'),
  719. canv_menu = $('#cmenu_canvas'),
  720. // layer_menu = $('#cmenu_layers'), // Unused
  721. exportWindow = null,
  722. zoomInIcon = 'crosshair',
  723. zoomOutIcon = 'crosshair',
  724. ui_context = 'toolbars',
  725. origSource = '',
  726. paintBox = {fill: null, stroke:null};
  727. // This sets up alternative dialog boxes. They mostly work the same way as
  728. // their UI counterparts, expect instead of returning the result, a callback
  729. // needs to be included that returns the result as its first parameter.
  730. // In the future we may want to add additional types of dialog boxes, since
  731. // they should be easy to handle this way.
  732. (function() {
  733. $('#dialog_container').draggable({cancel: '#dialog_content, #dialog_buttons *', containment: 'window'});
  734. var box = $('#dialog_box'),
  735. btn_holder = $('#dialog_buttons'),
  736. dialog_content = $('#dialog_content'),
  737. dbox = function(type, msg, callback, defaultVal, opts, changeCb, checkbox) {
  738. var ok, ctrl, chkbx;
  739. dialog_content.html('<p>'+msg.replace(/\n/g, '</p><p>')+'</p>')
  740. .toggleClass('prompt', (type == 'prompt'));
  741. btn_holder.empty();
  742. ok = $('<input type="button" value="' + uiStrings.common.ok + '">').appendTo(btn_holder);
  743. if (type !== 'alert') {
  744. $('<input type="button" value="' + uiStrings.common.cancel + '">')
  745. .appendTo(btn_holder)
  746. .click(function() { box.hide(); callback(false);});
  747. }
  748. if (type === 'prompt') {
  749. ctrl = $('<input type="text">').prependTo(btn_holder);
  750. ctrl.val(defaultVal || '');
  751. ctrl.bind('keydown', 'return', function() {ok.click();});
  752. }
  753. else if (type === 'select') {
  754. var div = $('<div style="text-align:center;">');
  755. ctrl = $('<select>').appendTo(div);
  756. if (checkbox) {
  757. var label = $('<label>').text(checkbox.label);
  758. chkbx = $('<input type="checkbox">').appendTo(label);
  759. chkbx.val(checkbox.value);
  760. if (checkbox.tooltip) {
  761. label.attr('title', checkbox.tooltip);
  762. }
  763. chkbx.prop('checked', !!checkbox.checked);
  764. div.append($('<div>').append(label));
  765. }
  766. $.each(opts || [], function (opt, val) {
  767. if (typeof val === 'object') {
  768. ctrl.append($('<option>').val(val.value).html(val.text));
  769. }
  770. else {
  771. ctrl.append($('<option>').html(val));
  772. }
  773. });
  774. dialog_content.append(div);
  775. if (defaultVal) {
  776. ctrl.val(defaultVal);
  777. }
  778. if (changeCb) {
  779. ctrl.bind('change', 'return', changeCb);
  780. }
  781. ctrl.bind('keydown', 'return', function() {ok.click();});
  782. }
  783. if (type === 'process') {
  784. ok.hide();
  785. }
  786. box.show();
  787. ok.click(function() {
  788. box.hide();
  789. var resp = (type === 'prompt' || type === 'select') ? ctrl.val() : true;
  790. if (callback) {
  791. if (chkbx) {
  792. callback(resp, chkbx.prop('checked'));
  793. }
  794. else {
  795. callback(resp);
  796. }
  797. }
  798. }).focus();
  799. if (type === 'prompt' || type === 'select') {
  800. ctrl.focus();
  801. }
  802. };
  803. $.alert = function(msg, cb) { dbox('alert', msg, cb);};
  804. $.confirm = function(msg, cb) { dbox('confirm', msg, cb);};
  805. $.process_cancel = function(msg, cb) { dbox('process', msg, cb);};
  806. $.prompt = function(msg, txt, cb) { dbox('prompt', msg, cb, txt);};
  807. $.select = function(msg, opts, cb, changeCb, txt, checkbox) { dbox('select', msg, cb, txt, opts, changeCb, checkbox);};
  808. }());
  809. var setSelectMode = function() {
  810. var curr = $('.tool_button_current');
  811. if (curr.length && curr[0].id !== 'tool_select') {
  812. curr.removeClass('tool_button_current').addClass('tool_button');
  813. $('#tool_select').addClass('tool_button_current').removeClass('tool_button');
  814. $('#styleoverrides').text('#svgcanvas svg *{cursor:move;pointer-events:all} #svgcanvas svg{cursor:default}');
  815. }
  816. svgCanvas.setMode('select');
  817. workarea.css('cursor', 'auto');
  818. };
  819. // used to make the flyouts stay on the screen longer the very first time
  820. // var flyoutspeed = 1250; // Currently unused
  821. var textBeingEntered = false;
  822. var selectedElement = null;
  823. var multiselected = false;
  824. var editingsource = false;
  825. var docprops = false;
  826. var preferences = false;
  827. var cur_context = '';
  828. var origTitle = $('title:first').text();
  829. // Make [1,2,5] array
  830. var r_intervals = [];
  831. var i;
  832. for (i = 0.1; i < 1E5; i *= 10) {
  833. r_intervals.push(i);
  834. r_intervals.push(2 * i);
  835. r_intervals.push(5 * i);
  836. }
  837. // This function highlights the layer passed in (by fading out the other layers)
  838. // if no layer is passed in, this function restores the other layers
  839. var toggleHighlightLayer = function(layerNameToHighlight) {
  840. var i, curNames = [], numLayers = svgCanvas.getCurrentDrawing().getNumLayers();
  841. for (i = 0; i < numLayers; i++) {
  842. curNames[i] = svgCanvas.getCurrentDrawing().getLayerName(i);
  843. }
  844. if (layerNameToHighlight) {
  845. for (i = 0; i < numLayers; ++i) {
  846. if (curNames[i] != layerNameToHighlight) {
  847. svgCanvas.getCurrentDrawing().setLayerOpacity(curNames[i], 0.5);
  848. }
  849. }
  850. } else {
  851. for (i = 0; i < numLayers; ++i) {
  852. svgCanvas.getCurrentDrawing().setLayerOpacity(curNames[i], 1.0);
  853. }
  854. }
  855. };
  856. var populateLayers = function() {
  857. svgCanvas.clearSelection();
  858. var layerlist = $('#layerlist tbody').empty();
  859. var selLayerNames = $('#selLayerNames').empty();
  860. var drawing = svgCanvas.getCurrentDrawing();
  861. var currentLayerName = drawing.getCurrentLayerName();
  862. var layer = svgCanvas.getCurrentDrawing().getNumLayers();
  863. var icon = $.getSvgIcon('eye');
  864. // we get the layers in the reverse z-order (the layer rendered on top is listed first)
  865. while (layer--) {
  866. var name = drawing.getLayerName(layer);
  867. var layerTr = $('<tr class="layer">').toggleClass('layersel', name === currentLayerName);
  868. var layerVis = $('<td class="layervis">').toggleClass('layerinvis', !drawing.getLayerVisibility(name));
  869. var layerName = $('<td class="layername">' + name + '</td>');
  870. layerlist.append(layerTr.append(layerVis, layerName));
  871. selLayerNames.append('<option value="' + name + '">' + name + '</option>');
  872. }
  873. if (icon !== undefined) {
  874. var copy = icon.clone();
  875. $('td.layervis', layerlist).append(copy);
  876. $.resizeSvgIcons({'td.layervis .svg_icon': 14});
  877. }
  878. // handle selection of layer
  879. $('#layerlist td.layername')
  880. .mouseup(function(evt) {
  881. $('#layerlist tr.layer').removeClass('layersel');
  882. $(this.parentNode).addClass('layersel');
  883. svgCanvas.setCurrentLayer(this.textContent);
  884. evt.preventDefault();
  885. })
  886. .mouseover(function() {
  887. toggleHighlightLayer(this.textContent);
  888. })
  889. .mouseout(function() {
  890. toggleHighlightLayer();
  891. });
  892. $('#layerlist td.layervis').click(function() {
  893. var row = $(this.parentNode).prevAll().length;
  894. var name = $('#layerlist tr.layer:eq(' + row + ') td.layername').text();
  895. var vis = $(this).hasClass('layerinvis');
  896. svgCanvas.setLayerVisibility(name, vis);
  897. $(this).toggleClass('layerinvis');
  898. });
  899. // if there were too few rows, let's add a few to make it not so lonely
  900. var num = 5 - $('#layerlist tr.layer').size();
  901. while (num-- > 0) {
  902. // FIXME: there must a better way to do this
  903. layerlist.append('<tr><td style="color:white">_</td><td/></tr>');
  904. }
  905. };
  906. var showSourceEditor = function(e, forSaving) {
  907. if (editingsource) {return;}
  908. editingsource = true;
  909. origSource = svgCanvas.getSvgString();
  910. $('#save_output_btns').toggle(!!forSaving);
  911. $('#tool_source_back').toggle(!forSaving);
  912. $('#svg_source_textarea').val(origSource);
  913. $('#svg_source_editor').fadeIn();
  914. $('#svg_source_textarea').focus();
  915. };
  916. var togglePathEditMode = function(editmode, elems) {
  917. $('#path_node_panel').toggle(editmode);
  918. $('#tools_bottom_2,#tools_bottom_3').toggle(!editmode);
  919. if (editmode) {
  920. // Change select icon
  921. $('.tool_button_current').removeClass('tool_button_current').addClass('tool_button');
  922. $('#tool_select').addClass('tool_button_current').removeClass('tool_button');
  923. setIcon('#tool_select', 'select_node');
  924. multiselected = false;
  925. if (elems.length) {
  926. selectedElement = elems[0];
  927. }
  928. } else {
  929. setTimeout(function () {
  930. setIcon('#tool_select', 'select');
  931. }, 1000);
  932. }
  933. };
  934. var saveHandler = function(wind, svg) {
  935. editor.showSaveWarning = false;
  936. // by default, we add the XML prolog back, systems integrating SVG-edit (wikis, CMSs)
  937. // can just provide their own custom save handler and might not want the XML prolog
  938. svg = '<?xml version="1.0"?>\n' + svg;
  939. // IE9 doesn't allow standalone Data URLs
  940. // https://connect.microsoft.com/IE/feedback/details/542600/data-uri-images-fail-when-loaded-by-themselves
  941. if (svgedit.browser.isIE()) {
  942. showSourceEditor(0, true);
  943. return;
  944. }
  945. // Opens the SVG in new window
  946. var win = wind.open('data:image/svg+xml;base64,' + Utils.encode64(svg));
  947. // Alert will only appear the first time saved OR the first time the bug is encountered
  948. var done = $.pref('save_notice_done');
  949. if (done !== 'all') {
  950. var note = uiStrings.notification.saveFromBrowser.replace('%s', 'SVG');
  951. // Check if FF and has <defs/>
  952. if (svgedit.browser.isGecko()) {
  953. if (svg.indexOf('<defs') !== -1) {
  954. // warning about Mozilla bug #308590 when applicable (seems to be fixed now in Feb 2013)
  955. note += '\n\n' + uiStrings.notification.defsFailOnSave;
  956. $.pref('save_notice_done', 'all');
  957. done = 'all';
  958. } else {
  959. $.pref('save_notice_done', 'part');
  960. }
  961. } else {
  962. $.pref('save_notice_done', 'all');
  963. }
  964. if (done !== 'part') {
  965. win.alert(note);
  966. }
  967. }
  968. };
  969. var exportHandler = function(win, data) {
  970. var issues = data.issues,
  971. type = data.type || 'PNG',
  972. dataURLType = (type === 'ICO' ? 'BMP' : type).toLowerCase();
  973. if (!$('#export_canvas').length) {
  974. $('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
  975. }
  976. var c = $('#export_canvas')[0];
  977. c.width = svgCanvas.contentW;
  978. c.height = svgCanvas.contentH;
  979. canvg(c, data.svg, {renderCallback: function() {
  980. var datauri = data.quality ? c.toDataURL('image/' + dataURLType, data.quality) : c.toDataURL('image/' + dataURLType);
  981. exportWindow.location.href = datauri;
  982. var done = $.pref('export_notice_done');
  983. if (done !== 'all') {
  984. var note = uiStrings.notification.saveFromBrowser.replace('%s', type);
  985. // Check if there's issues
  986. if (issues.length) {
  987. var pre = '\n \u2022 ';
  988. note += ('\n\n' + uiStrings.notification.noteTheseIssues + pre + issues.join(pre));
  989. }
  990. // Note that this will also prevent the notice even though new issues may appear later.
  991. // May want to find a way to deal with that without annoying the user
  992. $.pref('export_notice_done', 'all');
  993. exportWindow.alert(note);
  994. }
  995. }});
  996. };
  997. var operaRepaint = function() {
  998. // Repaints canvas in Opera. Needed for stroke-dasharray change as well as fill change
  999. if (!window.opera) {
  1000. return;
  1001. }
  1002. $('<p/>').hide().appendTo('body').remove();
  1003. };
  1004. function setStrokeOpt(opt, changeElem) {
  1005. var id = opt.id;
  1006. var bits = id.split('_');
  1007. var pre = bits[0];
  1008. var val = bits[1];
  1009. if (changeElem) {
  1010. svgCanvas.setStrokeAttr('stroke-' + pre, val);
  1011. }
  1012. operaRepaint();
  1013. setIcon('#cur_' + pre, id, 20);
  1014. $(opt).addClass('current').siblings().removeClass('current');
  1015. }
  1016. // This is a common function used when a tool has been clicked (chosen)
  1017. // It does several common things:
  1018. // - removes the tool_button_current class from whatever tool currently has it
  1019. // - hides any flyouts
  1020. // - adds the tool_button_current class to the button passed in
  1021. var toolButtonClick = editor.toolButtonClick = function(button, noHiding) {
  1022. if ($(button).hasClass('disabled')) {return false;}
  1023. if ($(button).parent().hasClass('tools_flyout')) {return true;}
  1024. var fadeFlyouts = 'normal';
  1025. if (!noHiding) {
  1026. $('.tools_flyout').fadeOut(fadeFlyouts);
  1027. }
  1028. $('#styleoverrides').text('');
  1029. workarea.css('cursor', 'auto');
  1030. $('.tool_button_current').removeClass('tool_button_current').addClass('tool_button');
  1031. $(button).addClass('tool_button_current').removeClass('tool_button');
  1032. return true;
  1033. };
  1034. var clickSelect = editor.clickSelect = function() {
  1035. if (toolButtonClick('#tool_select')) {
  1036. svgCanvas.setMode('select');
  1037. $('#styleoverrides').text('#svgcanvas svg *{cursor:move;pointer-events:all}, #svgcanvas svg{cursor:default}');
  1038. }
  1039. };
  1040. var setImageURL = editor.setImageURL = function(url) {
  1041. if (!url) {
  1042. url = defaultImageURL;
  1043. }
  1044. svgCanvas.setImageURL(url);
  1045. $('#image_url').val(url);
  1046. if (url.indexOf('data:') === 0) {
  1047. // data URI found
  1048. $('#image_url').hide();
  1049. $('#change_image_url').show();
  1050. } else {
  1051. // regular URL
  1052. svgCanvas.embedImage(url, function(dataURI) {
  1053. // Couldn't embed, so show warning
  1054. $('#url_notice').toggle(!dataURI);
  1055. defaultImageURL = url;
  1056. });
  1057. $('#image_url').show();
  1058. $('#change_image_url').hide();
  1059. }
  1060. };
  1061. function setBackground (color, url) {
  1062. // if (color == $.pref('bkgd_color') && url == $.pref('bkgd_url')) {return;}
  1063. $.pref('bkgd_color', color);
  1064. $.pref('bkgd_url', url);
  1065. // This should be done in svgcanvas.js for the borderRect fill
  1066. svgCanvas.setBackground(color, url);
  1067. }
  1068. function promptImgURL() {
  1069. var curhref = svgCanvas.getHref(selectedElement);
  1070. curhref = curhref.indexOf('data:') === 0 ? '' : curhref;
  1071. $.prompt(uiStrings.notification.enterNewImgURL, curhref, function(url) {
  1072. if (url) {setImageURL(url);}
  1073. });
  1074. }
  1075. var setInputWidth = function(elem) {
  1076. var w = Math.min(Math.max(12 + elem.value.length * 6, 50), 300);
  1077. $(elem).width(w);
  1078. };
  1079. function updateRulers(scanvas, zoom) {
  1080. if (!zoom) {zoom = svgCanvas.getZoom();}
  1081. if (!scanvas) {scanvas = $('#svgcanvas');}
  1082. var d, i;
  1083. var limit = 30000;
  1084. var contentElem = svgCanvas.getContentElem();
  1085. var units = svgedit.units.getTypeMap();
  1086. var unit = units[curConfig.baseUnit]; // 1 = 1px
  1087. // draw x ruler then y ruler
  1088. for (d = 0; d < 2; d++) {
  1089. var isX = (d === 0);
  1090. var dim = isX ? 'x' : 'y';
  1091. var lentype = isX ? 'width' : 'height';
  1092. var contentDim = Number(contentElem.getAttribute(dim));
  1093. var $hcanv_orig = $('#ruler_' + dim + ' canvas:first');
  1094. // Bit of a hack to fully clear the canvas in Safari & IE9
  1095. var $hcanv = $hcanv_orig.clone();
  1096. $hcanv_orig.replaceWith($hcanv);
  1097. var hcanv = $hcanv[0];
  1098. // Set the canvas size to the width of the container
  1099. var ruler_len = scanvas[lentype]();
  1100. var total_len = ruler_len;
  1101. hcanv.parentNode.style[lentype] = total_len + 'px';
  1102. var ctx_num = 0;
  1103. var ctx = hcanv.getContext('2d');
  1104. var ctx_arr, num, ctx_arr_num;
  1105. ctx.fillStyle = 'rgb(200,0,0)';
  1106. ctx.fillRect(0, 0, hcanv.width, hcanv.height);
  1107. // Remove any existing canvasses
  1108. $hcanv.siblings().remove();
  1109. // Create multiple canvases when necessary (due to browser limits)
  1110. if (ruler_len >= limit) {
  1111. ctx_arr_num = parseInt(ruler_len / limit, 10) + 1;
  1112. ctx_arr = [];
  1113. ctx_arr[0] = ctx;
  1114. var copy;
  1115. for (i = 1; i < ctx_arr_num; i++) {
  1116. hcanv[lentype] = limit;
  1117. copy = hcanv.cloneNode(true);
  1118. hcanv.parentNode.appendChild(copy);
  1119. ctx_arr[i] = copy.getContext('2d');
  1120. }
  1121. copy[lentype] = ruler_len % limit;
  1122. // set copy width to last
  1123. ruler_len = limit;
  1124. }
  1125. hcanv[lentype] = ruler_len;
  1126. var u_multi = unit * zoom;
  1127. // Calculate the main number interval
  1128. var raw_m = 50 / u_multi;
  1129. var multi = 1;
  1130. for (i = 0; i < r_intervals.length; i++) {
  1131. num = r_intervals[i];
  1132. multi = num;
  1133. if (raw_m <= num) {
  1134. break;
  1135. }
  1136. }
  1137. var big_int = multi * u_multi;
  1138. ctx.font = '9px sans-serif';
  1139. var ruler_d = ((contentDim / u_multi) % multi) * u_multi;
  1140. var label_pos = ruler_d - big_int;
  1141. // draw big intervals
  1142. while (ruler_d < total_len) {
  1143. label_pos += big_int;
  1144. // var real_d = ruler_d - contentDim; // Currently unused
  1145. var cur_d = Math.round(ruler_d) + 0.5;
  1146. if (isX) {
  1147. ctx.moveTo(cur_d, 15);
  1148. ctx.lineTo(cur_d, 0);
  1149. }
  1150. else {
  1151. ctx.moveTo(15, cur_d);
  1152. ctx.lineTo(0, cur_d);
  1153. }
  1154. num = (label_pos - contentDim) / u_multi;
  1155. var label;
  1156. if (multi >= 1) {
  1157. label = Math.round(num);
  1158. }
  1159. else {
  1160. var decs = String(multi).split('.')[1].length;
  1161. label = num.toFixed(decs);
  1162. }
  1163. // Change 1000s to Ks
  1164. if (label !== 0 && label !== 1000 && label % 1000 === 0) {
  1165. label = (label / 1000) + 'K';
  1166. }
  1167. if (isX) {
  1168. ctx.fillText(label, ruler_d+2, 8);
  1169. } else {
  1170. // draw label vertically
  1171. var str = String(label).split('');
  1172. for (i = 0; i < str.length; i++) {
  1173. ctx.fillText(str[i], 1, (ruler_d+9) + i*9);
  1174. }
  1175. }
  1176. var part = big_int / 10;
  1177. // draw the small intervals
  1178. for (i = 1; i < 10; i++) {
  1179. var sub_d = Math.round(ruler_d + part * i) + 0.5;
  1180. if (ctx_arr && sub_d > ruler_len) {
  1181. ctx_num++;
  1182. ctx.stroke();
  1183. if (ctx_num >= ctx_arr_num) {
  1184. i = 10;
  1185. ruler_d = total_len;
  1186. continue;
  1187. }
  1188. ctx = ctx_arr[ctx_num];
  1189. ruler_d -= limit;
  1190. sub_d = Math.round(ruler_d + part * i) + 0.5;
  1191. }
  1192. // odd lines are slighly longer
  1193. var line_num = (i % 2) ? 12 : 10;
  1194. if (isX) {
  1195. ctx.moveTo(sub_d, 15);
  1196. ctx.lineTo(sub_d, line_num);
  1197. } else {
  1198. ctx.moveTo(15, sub_d);
  1199. ctx.lineTo(line_num, sub_d);
  1200. }
  1201. }
  1202. ruler_d += big_int;
  1203. }
  1204. ctx.strokeStyle = '#000';
  1205. ctx.stroke();
  1206. }
  1207. }
  1208. var updateCanvas = editor.updateCanvas = function(center, new_ctr) {
  1209. var w = workarea.width(), h = workarea.height();
  1210. var w_orig = w, h_orig = h;
  1211. var zoom = svgCanvas.getZoom();
  1212. var w_area = workarea;
  1213. var cnvs = $('#svgcanvas');
  1214. var old_ctr = {
  1215. x: w_area[0].scrollLeft + w_orig/2,
  1216. y: w_area[0].scrollTop + h_orig/2
  1217. };
  1218. var multi = curConfig.canvas_expansion;
  1219. w = Math.max(w_orig, svgCanvas.contentW * zoom * multi);
  1220. h = Math.max(h_orig, svgCanvas.contentH * zoom * multi);
  1221. if (w == w_orig && h == h_orig) {
  1222. workarea.css('overflow', 'hidden');
  1223. } else {
  1224. workarea.css('overflow', 'scroll');
  1225. }
  1226. var old_can_y = cnvs.height()/2;
  1227. var old_can_x = cnvs.width()/2;
  1228. cnvs.width(w).height(h);
  1229. var new_can_y = h/2;
  1230. var new_can_x = w/2;
  1231. var offset = svgCanvas.updateCanvas(w, h);
  1232. var ratio = new_can_x / old_can_x;
  1233. var scroll_x = w/2 - w_orig/2;
  1234. var scroll_y = h/2 - h_orig/2;
  1235. if (!new_ctr) {
  1236. var old_dist_x = old_ctr.x - old_can_x;
  1237. var new_x = new_can_x + old_dist_x * ratio;
  1238. var old_dist_y = old_ctr.y - old_can_y;
  1239. var new_y = new_can_y + old_dist_y * ratio;
  1240. new_ctr = {
  1241. x: new_x,
  1242. y: new_y
  1243. };
  1244. } else {
  1245. new_ctr.x += offset.x;
  1246. new_ctr.y += offset.y;
  1247. }
  1248. if (center) {
  1249. // Go to top-left for larger documents
  1250. if (svgCanvas.contentW > w_area.width()) {
  1251. // Top-left
  1252. workarea[0].scrollLeft = offset.x - 10;
  1253. workarea[0].scrollTop = offset.y - 10;
  1254. } else {
  1255. // Center
  1256. w_area[0].scrollLeft = scroll_x;
  1257. w_area[0].scrollTop = scroll_y;
  1258. }
  1259. } else {
  1260. w_area[0].scrollLeft = new_ctr.x - w_orig/2;
  1261. w_area[0].scrollTop = new_ctr.y - h_orig/2;
  1262. }
  1263. if (curConfig.showRulers) {
  1264. updateRulers(cnvs, zoom);
  1265. workarea.scroll();
  1266. }
  1267. if (urldata.storagePrompt !== true && !editor.storagePromptClosed) {
  1268. $('#dialog_box').hide();
  1269. }
  1270. };
  1271. var updateToolButtonState = function() {
  1272. var index, button;
  1273. var bNoFill = (svgCanvas.getColor('fill') == 'none');
  1274. var bNoStroke = (svgCanvas.getColor('stroke') == 'none');
  1275. var buttonsNeedingStroke = [ '#tool_fhpath', '#tool_line' ];
  1276. var buttonsNeedingFillAndStroke = [ '#tools_rect .tool_button', '#tools_ellipse .tool_button', '#tool_text', '#tool_path'];
  1277. if (bNoStroke) {
  1278. for (index in buttonsNeedingStroke) {
  1279. button = buttonsNeedingStroke[index];
  1280. if ($(button).hasClass('tool_button_current')) {
  1281. clickSelect();
  1282. }
  1283. $(button).addClass('disabled');
  1284. }
  1285. } else {
  1286. for (index in buttonsNeedingStroke) {
  1287. button = buttonsNeedingStroke[index];
  1288. $(button).removeClass('disabled');
  1289. }
  1290. }
  1291. if (bNoStroke && bNoFill) {
  1292. for (index in buttonsNeedingFillAndStroke) {
  1293. button = buttonsNeedingFillAndStroke[index];
  1294. if ($(button).hasClass('tool_button_current')) {
  1295. clickSelect();
  1296. }
  1297. $(button).addClass('disabled');
  1298. }
  1299. } else {
  1300. for (index in buttonsNeedingFillAndStroke) {
  1301. button = buttonsNeedingFillAndStroke[index];
  1302. $(button).removeClass('disabled');
  1303. }
  1304. }
  1305. svgCanvas.runExtensions('toolButtonStateUpdate', {
  1306. nofill: bNoFill,
  1307. nostroke: bNoStroke
  1308. });
  1309. // Disable flyouts if all inside are disabled
  1310. $('.tools_flyout').each(function() {
  1311. var shower = $('#' + this.id + '_show');
  1312. var has_enabled = false;
  1313. $(this).children().each(function() {
  1314. if (!$(this).hasClass('disabled')) {
  1315. has_enabled = true;
  1316. }
  1317. });
  1318. shower.toggleClass('disabled', !has_enabled);
  1319. });
  1320. operaRepaint();
  1321. };
  1322. // Updates the toolbar (colors, opacity, etc) based on the selected element
  1323. // This function also updates the opacity and id elements that are in the context panel
  1324. var updateToolbar = function() {
  1325. var i, len;
  1326. if (selectedElement != null) {
  1327. switch (selectedElement.tagName) {
  1328. case 'use':
  1329. case 'image':
  1330. case 'foreignObject':
  1331. break;
  1332. case 'g':
  1333. case 'a':
  1334. // Look for common styles
  1335. var gWidth = null;
  1336. var childs = selectedElement.getElementsByTagName('*');
  1337. for (i = 0, len = childs.length; i < len; i++) {
  1338. var swidth = childs[i].getAttribute('stroke-width');
  1339. if (i === 0) {
  1340. gWidth = swidth;
  1341. } else if (gWidth !== swidth) {
  1342. gWidth = null;
  1343. }
  1344. }
  1345. $('#stroke_width').val(gWidth === null ? '' : gWidth);
  1346. paintBox.fill.update(true);
  1347. paintBox.stroke.update(true);
  1348. break;
  1349. default:
  1350. paintBox.fill.update(true);
  1351. paintBox.stroke.update(true);
  1352. $('#stroke_width').val(selectedElement.getAttribute('stroke-width') || 1);
  1353. $('#stroke_style').val(selectedElement.getAttribute('stroke-dasharray') || 'none');
  1354. var attr = selectedElement.getAttribute('stroke-linejoin') || 'miter';
  1355. if ($('#linejoin_' + attr).length != 0) {
  1356. setStrokeOpt($('#linejoin_' + attr)[0]);
  1357. }
  1358. attr = selectedElement.getAttribute('stroke-linecap') || 'butt';
  1359. if ($('#linecap_' + attr).length != 0) {
  1360. setStrokeOpt($('#linecap_' + attr)[0]);
  1361. }
  1362. }
  1363. }
  1364. // All elements including image and group have opacity
  1365. if (selectedElement != null) {
  1366. var opac_perc = ((selectedElement.getAttribute('opacity')||1.0)*100);
  1367. $('#group_opacity').val(opac_perc);
  1368. $('#opac_slider').slider('option', 'value', opac_perc);
  1369. $('#elem_id').val(selectedElement.id);
  1370. }
  1371. updateToolButtonState();
  1372. };
  1373. // updates the context panel tools based on the selected element
  1374. var updateContextPanel = function() {
  1375. var elem = selectedElement;
  1376. // If element has just been deleted, consider it null
  1377. if (elem != null && !elem.parentNode) {elem = null;}
  1378. var currentLayerName = svgCanvas.getCurrentDrawing().getCurrentLayerName();
  1379. var currentMode = svgCanvas.getMode();
  1380. var unit = curConfig.baseUnit !== 'px' ? curConfig.baseUnit : null;
  1381. var is_node = currentMode == 'pathedit'; //elem ? (elem.id && elem.id.indexOf('pathpointgrip') == 0) : false;
  1382. var menu_items = $('#cmenu_canvas li');
  1383. $('#selected_panel, #multiselected_panel, #g_panel, #rect_panel, #circle_panel,'+
  1384. '#ellipse_panel, #line_panel, #text_panel, #image_panel, #container_panel,'+
  1385. ' #use_panel, #a_panel').hide();
  1386. if (elem != null) {
  1387. var elname = elem.nodeName;
  1388. // If this is a link with no transform and one child, pretend
  1389. // its child is selected
  1390. // if (elname === 'a') { // && !$(elem).attr('transform')) {
  1391. // elem = elem.firstChild;
  1392. // }
  1393. var angle = svgCanvas.getRotationAngle(elem);
  1394. $('#angle').val(angle);
  1395. var blurval = svgCanvas.getBlur(elem);
  1396. $('#blur').val(blurval);
  1397. $('#blur_slider').slider('option', 'value', blurval);
  1398. if (svgCanvas.addedNew) {
  1399. if (elname === 'image') {
  1400. // Prompt for URL if not a data URL
  1401. if (svgCanvas.getHref(elem).indexOf('data:') !== 0) {
  1402. promptImgURL();
  1403. }
  1404. } /*else if (elname == 'text') {
  1405. // TODO: Do something here for new text
  1406. }*/
  1407. }
  1408. if (!is_node && currentMode != 'pathedit') {
  1409. $('#selected_panel').show();
  1410. // Elements in this array already have coord fields
  1411. if (['line', 'circle', 'ellipse'].indexOf(elname) >= 0) {
  1412. $('#xy_panel').hide();
  1413. } else {
  1414. var x, y;
  1415. // Get BBox vals for g, polyline and path
  1416. if (['g', 'polyline', 'path'].indexOf(elname) >= 0) {
  1417. var bb = svgCanvas.getStrokedBBox([elem]);
  1418. if (bb) {
  1419. x = bb.x;
  1420. y = bb.y;
  1421. }
  1422. } else {
  1423. x = elem.getAttribute('x');
  1424. y = elem.getAttribute('y');
  1425. }
  1426. if (unit) {
  1427. x = svgedit.units.convertUnit(x);
  1428. y = svgedit.units.convertUnit(y);
  1429. }
  1430. $('#selected_x').val(x || 0);
  1431. $('#selected_y').val(y || 0);
  1432. $('#xy_panel').show();
  1433. }
  1434. // Elements in this array cannot be converted to a path
  1435. var no_path = ['image', 'text', 'path', 'g', 'use'].indexOf(elname) == -1;
  1436. $('#tool_topath').toggle(no_path);
  1437. $('#tool_reorient').toggle(elname === 'path');
  1438. $('#tool_reorient').toggleClass('disabled', angle === 0);
  1439. } else {
  1440. var point = path.getNodePoint();
  1441. $('#tool_add_subpath').removeClass('push_button_pressed').addClass('tool_button');
  1442. $('#tool_node_delete').toggleClass('disabled', !path.canDeleteNodes);
  1443. // Show open/close button based on selected point
  1444. setIcon('#tool_openclose_path', path.closed_subpath ? 'open_path' : 'close_path');
  1445. if (point) {
  1446. var seg_type = $('#seg_type');
  1447. if (unit) {
  1448. point.x = svgedit.units.convertUnit(point.x);
  1449. point.y = svgedit.units.convertUnit(point.y);
  1450. }
  1451. $('#path_node_x').val(point.x);
  1452. $('#path_node_y').val(point.y);
  1453. if (point.type) {
  1454. seg_type.val(point.type).removeAttr('disabled');
  1455. } else {
  1456. seg_type.val(4).attr('disabled', 'disabled');
  1457. }
  1458. }
  1459. return;
  1460. }
  1461. // update contextual tools here
  1462. var panels = {
  1463. g: [],
  1464. a: [],
  1465. rect: ['rx', 'width', 'height'],
  1466. image: ['width', 'height'],
  1467. circle: ['cx', 'cy', 'r'],
  1468. ellipse: ['cx', 'cy', 'rx', 'ry'],
  1469. line: ['x1', 'y1', 'x2', 'y2'],
  1470. text: [],
  1471. use: []
  1472. };
  1473. var el_name = elem.tagName;
  1474. // if ($(elem).data('gsvg')) {
  1475. // $('#g_panel').show();
  1476. // }
  1477. var link_href = null;
  1478. if (el_name === 'a') {
  1479. link_href = svgCanvas.getHref(elem);
  1480. $('#g_panel').show();
  1481. }
  1482. if (elem.parentNode.tagName === 'a') {
  1483. if (!$(elem).siblings().length) {
  1484. $('#a_panel').show();
  1485. link_href = svgCanvas.getHref(elem.parentNode);
  1486. }
  1487. }
  1488. // Hide/show the make_link buttons
  1489. $('#tool_make_link, #tool_make_link').toggle(!link_href);
  1490. if (link_href) {
  1491. $('#link_url').val(link_href);
  1492. }
  1493. if (panels[el_name]) {
  1494. var cur_panel = panels[el_name];
  1495. $('#' + el_name + '_panel').show();
  1496. $.each(cur_panel, function(i, item) {
  1497. var attrVal = elem.getAttribute(item);
  1498. if (curConfig.baseUnit !== 'px' && elem[item]) {
  1499. var bv = elem[item].baseVal.value;
  1500. attrVal = svgedit.units.convertUnit(bv);
  1501. }
  1502. $('#' + el_name + '_' + item).val(attrVal || 0);
  1503. });
  1504. if (el_name == 'text') {
  1505. $('#text_panel').css('display', 'inline');
  1506. if (svgCanvas.getItalic()) {
  1507. $('#tool_italic').addClass('push_button_pressed').removeClass('tool_button');
  1508. } else {
  1509. $('#tool_italic').removeClass('push_button_pressed').addClass('tool_button');
  1510. }
  1511. if (svgCanvas.getBold()) {
  1512. $('#tool_bold').addClass('push_button_pressed').removeClass('tool_button');
  1513. } else {
  1514. $('#tool_bold').removeClass('push_button_pressed').addClass('tool_button');
  1515. }
  1516. $('#font_family').val(elem.getAttribute('font-family'));
  1517. $('#font_size').val(elem.getAttribute('font-size'));
  1518. $('#text').val(elem.textContent);
  1519. if (svgCanvas.addedNew) {
  1520. // Timeout needed for IE9
  1521. setTimeout(function() {
  1522. $('#text').focus().select();
  1523. }, 100);
  1524. }
  1525. } // text
  1526. else if (el_name == 'image') {
  1527. setImageURL(svgCanvas.getHref(elem));
  1528. } // image
  1529. else if (el_name === 'g' || el_name === 'use') {
  1530. $('#container_panel').show();
  1531. var title = svgCanvas.getTitle();
  1532. var label = $('#g_title')[0];
  1533. label.value = title;
  1534. setInputWidth(label);
  1535. $('#g_title').prop('disabled', el_name == 'use');
  1536. }
  1537. }
  1538. menu_items[(el_name === 'g' ? 'en' : 'dis') + 'ableContextMenuItems']('#ungroup');
  1539. menu_items[((el_name === 'g' || !multiselected) ? 'dis' : 'en') + 'ableContextMenuItems']('#group');
  1540. } // if (elem != null)
  1541. else if (multiselected) {
  1542. $('#multiselected_panel').show();
  1543. menu_items
  1544. .enableContextMenuItems('#group')
  1545. .disableContextMenuItems('#ungroup');
  1546. } else {
  1547. menu_items.disableContextMenuItems('#delete,#cut,#copy,#group,#ungroup,#move_front,#move_up,#move_down,#move_back');
  1548. }
  1549. // update history buttons
  1550. $('#tool_undo').toggleClass('disabled', undoMgr.getUndoStackSize() === 0);
  1551. $('#tool_redo').toggleClass('disabled', undoMgr.getRedoStackSize() === 0);
  1552. svgCanvas.addedNew = false;
  1553. if ( (elem && !is_node) || multiselected) {
  1554. // update the selected elements' layer
  1555. $('#selLayerNames').removeAttr('disabled').val(currentLayerName);
  1556. // Enable regular menu options
  1557. canv_menu.enableContextMenuItems('#delete,#cut,#copy,#move_front,#move_up,#move_down,#move_back');
  1558. } else {
  1559. $('#selLayerNames').attr('disabled', 'disabled');
  1560. }
  1561. };
  1562. var updateWireFrame = function() {
  1563. // Test support
  1564. if (supportsNonSS) {return;}
  1565. var rule = '#workarea.wireframe #svgcontent * { stroke-width: ' + 1/svgCanvas.getZoom() + 'px; }';
  1566. $('#wireframe_rules').text(workarea.hasClass('wireframe') ? rule : '');
  1567. };
  1568. var updateTitle = function(title) {
  1569. title = title || svgCanvas.getDocumentTitle();
  1570. var newTitle = origTitle + (title ? ': ' + title : '');
  1571. // Remove title update with current context info, isn't really necessary
  1572. // if (cur_context) {
  1573. // new_title = new_title + cur_context;
  1574. // }
  1575. $('title:first').text(newTitle);
  1576. };
  1577. // called when we've selected a different element
  1578. var selectedChanged = function(win, elems) {
  1579. var mode = svgCanvas.getMode();
  1580. if (mode === 'select') {
  1581. setSelectMode();
  1582. }
  1583. var is_node = (mode == "pathedit");
  1584. // if elems[1] is present, then we have more than one element
  1585. selectedElement = (elems.length === 1 || elems[1] == null ? elems[0] : null);
  1586. multiselected = (elems.length >= 2 && elems[1] != null);
  1587. if (selectedElement != null) {
  1588. // unless we're already in always set the mode of the editor to select because
  1589. // upon creation of a text element the editor is switched into
  1590. // select mode and this event fires - we need our UI to be in sync
  1591. if (!is_node) {
  1592. updateToolbar();
  1593. }
  1594. } // if (elem != null)
  1595. // Deal with pathedit mode
  1596. togglePathEditMode(is_node, elems);
  1597. updateContextPanel();
  1598. svgCanvas.runExtensions('selectedChanged', {
  1599. elems: elems,
  1600. selectedElement: selectedElement,
  1601. multiselected: multiselected
  1602. });
  1603. };
  1604. // Call when part of element is in process of changing, generally
  1605. // on mousemove actions like rotate, move, etc.
  1606. var elementTransition = function(win, elems) {
  1607. var mode = svgCanvas.getMode();
  1608. var elem = elems[0];
  1609. if (!elem) {
  1610. return;
  1611. }
  1612. multiselected = (elems.length >= 2 && elems[1] != null);
  1613. // Only updating fields for single elements for now
  1614. if (!multiselected) {
  1615. switch (mode) {
  1616. case 'rotate':
  1617. var ang = svgCanvas.getRotationAngle(elem);
  1618. $('#angle').val(ang);
  1619. $('#tool_reorient').toggleClass('disabled', ang === 0);
  1620. break;
  1621. // TODO: Update values that change on move/resize, etc
  1622. // case "select":
  1623. // case "resize":
  1624. // break;
  1625. }
  1626. }
  1627. svgCanvas.runExtensions('elementTransition', {
  1628. elems: elems
  1629. });
  1630. };
  1631. // called when any element has changed
  1632. var elementChanged = function(win, elems) {
  1633. var i,
  1634. mode = svgCanvas.getMode();
  1635. if (mode === 'select') {
  1636. setSelectMode();
  1637. }
  1638. for (i = 0; i < elems.length; ++i) {
  1639. var elem = elems[i];
  1640. // if the element changed was the svg, then it could be a resolution change
  1641. if (elem && elem.tagName === 'svg') {
  1642. populateLayers();
  1643. updateCanvas();
  1644. }
  1645. // Update selectedElement if element is no longer part of the image.
  1646. // This occurs for the text elements in Firefox
  1647. else if (elem && selectedElement && selectedElement.parentNode == null) {
  1648. // || elem && elem.tagName == "path" && !multiselected) { // This was added in r1430, but not sure why
  1649. selectedElement = elem;
  1650. }
  1651. }
  1652. editor.showSaveWarning = true;
  1653. // we update the contextual panel with potentially new
  1654. // positional/sizing information (we DON'T want to update the
  1655. // toolbar here as that creates an infinite loop)
  1656. // also this updates the history buttons
  1657. // we tell it to skip focusing the text control if the
  1658. // text element was previously in focus
  1659. updateContextPanel();
  1660. // In the event a gradient was flipped:
  1661. if (selectedElement && mode === 'select') {
  1662. paintBox.fill.update();
  1663. paintBox.stroke.update();
  1664. }
  1665. svgCanvas.runExtensions('elementChanged', {
  1666. elems: elems
  1667. });
  1668. };
  1669. var zoomDone = function() {
  1670. updateWireFrame();
  1671. // updateCanvas(); // necessary?
  1672. };
  1673. var zoomChanged = svgCanvas.zoomChanged = function(win, bbox, autoCenter) {
  1674. var scrbar = 15,
  1675. // res = svgCanvas.getResolution(), // Currently unused
  1676. w_area = workarea;
  1677. // var canvas_pos = $('#svgcanvas').position(); // Currently unused
  1678. var z_info = svgCanvas.setBBoxZoom(bbox, w_area.width()-scrbar, w_area.height()-scrbar);
  1679. if (!z_info) {return;}
  1680. var zoomlevel = z_info.zoom,
  1681. bb = z_info.bbox;
  1682. if (zoomlevel < 0.001) {
  1683. changeZoom({value: 0.1});
  1684. return;
  1685. }
  1686. $('#zoom').val((zoomlevel*100).toFixed(1));
  1687. if (autoCenter) {
  1688. updateCanvas();
  1689. } else {
  1690. updateCanvas(false, {x: bb.x * zoomlevel + (bb.width * zoomlevel)/2, y: bb.y * zoomlevel + (bb.height * zoomlevel)/2});
  1691. }
  1692. if (svgCanvas.getMode() == 'zoom' && bb.width) {
  1693. // Go to select if a zoom box was drawn
  1694. setSelectMode();
  1695. }
  1696. zoomDone();
  1697. };
  1698. changeZoom = function(ctl) {
  1699. var zoomlevel = ctl.value / 100;
  1700. if (zoomlevel < 0.001) {
  1701. ctl.value = 0.1;
  1702. return;
  1703. }
  1704. var zoom = svgCanvas.getZoom();
  1705. var w_area = workarea;
  1706. zoomChanged(window, {
  1707. width: 0,
  1708. height: 0,
  1709. // center pt of scroll position
  1710. x: (w_area[0].scrollLeft + w_area.width()/2)/zoom,
  1711. y: (w_area[0].scrollTop + w_area.height()/2)/zoom,
  1712. zoom: zoomlevel
  1713. }, true);
  1714. };
  1715. $('#cur_context_panel').delegate('a', 'click', function() {
  1716. var link = $(this);
  1717. if (link.attr('data-root')) {
  1718. svgCanvas.leaveContext();
  1719. } else {
  1720. svgCanvas.setContext(link.text());
  1721. }
  1722. svgCanvas.clearSelection();
  1723. return false;
  1724. });
  1725. var contextChanged = function(win, context) {
  1726. var link_str = '';
  1727. if (context) {
  1728. var str = '';
  1729. link_str = '<a href="#" data-root="y">' + svgCanvas.getCurrentDrawing().getCurrentLayerName() + '</a>';
  1730. $(context).parentsUntil('#svgcontent > g').andSelf().each(function() {
  1731. if (this.id) {
  1732. str += ' > ' + this.id;
  1733. if (this !== context) {
  1734. link_str += ' > <a href="#">' + this.id + '</a>';
  1735. } else {
  1736. link_str += ' > ' + this.id;
  1737. }
  1738. }
  1739. });
  1740. cur_context = str;
  1741. } else {
  1742. cur_context = null;
  1743. }
  1744. $('#cur_context_panel').toggle(!!context).html(link_str);
  1745. updateTitle();
  1746. };
  1747. // Makes sure the current selected paint is available to work with
  1748. var prepPaints = function() {
  1749. paintBox.fill.prep();
  1750. paintBox.stroke.prep();
  1751. };
  1752. var flyout_funcs = {};
  1753. var setFlyoutTitles = function() {
  1754. $('.tools_flyout').each(function() {
  1755. var shower = $('#' + this.id + '_show');
  1756. if (shower.data('isLibrary')) {
  1757. return;
  1758. }
  1759. var tooltips = [];
  1760. $(this).children().each(function() {
  1761. tooltips.push(this.title);
  1762. });
  1763. shower[0].title = tooltips.join(' / ');
  1764. });
  1765. };
  1766. var setFlyoutPositions = function() {
  1767. $('.tools_flyout').each(function() {
  1768. var shower = $('#' + this.id + '_show');
  1769. var pos = shower.offset();
  1770. var w = shower.outerWidth();
  1771. $(this).css({left: (pos.left + w) * editor.tool_scale, top: pos.top});
  1772. });
  1773. };
  1774. var setupFlyouts = function(holders) {
  1775. $.each(holders, function(hold_sel, btn_opts) {
  1776. var buttons = $(hold_sel).children();
  1777. var show_sel = hold_sel + '_show';
  1778. var shower = $(show_sel);
  1779. var def = false;
  1780. buttons.addClass('tool_button')
  1781. .unbind('click mousedown mouseup') // may not be necessary
  1782. .each(function(i) {
  1783. // Get this buttons options
  1784. var opts = btn_opts[i];
  1785. // Remember the function that goes with this ID
  1786. flyout_funcs[opts.sel] = opts.fn;
  1787. if (opts.isDefault) {def = i;}
  1788. // Clicking the icon in flyout should set this set's icon
  1789. var func = function(event) {
  1790. var options = opts;
  1791. //find the currently selected tool if comes from keystroke
  1792. if (event.type === 'keydown') {
  1793. var flyoutIsSelected = $(options.parent + '_show').hasClass('tool_button_current');
  1794. var currentOperation = $(options.parent + '_show').attr('data-curopt');
  1795. $.each(holders[opts.parent], function(i, tool) {
  1796. if (tool.sel == currentOperation) {
  1797. if (!event.shiftKey || !flyoutIsSelected) {
  1798. options = tool;
  1799. } else {
  1800. options = holders[opts.parent][i+1] || holders[opts.parent][0];
  1801. }
  1802. }
  1803. });
  1804. }
  1805. if ($(this).hasClass('disabled')) {return false;}
  1806. if (toolButtonClick(show_sel)) {
  1807. options.fn();
  1808. }
  1809. var icon;
  1810. if (options.icon) {
  1811. icon = $.getSvgIcon(options.icon, true);
  1812. } else {
  1813. icon = $(options.sel).children().eq(0).clone();
  1814. }
  1815. icon[0].setAttribute('width', shower.width());
  1816. icon[0].setAttribute('height', shower.height());
  1817. shower.children(':not(.flyout_arrow_horiz)').remove();
  1818. shower.append(icon).attr('data-curopt', options.sel); // This sets the current mode
  1819. };
  1820. $(this).mouseup(func);
  1821. if (opts.key) {
  1822. $(document).bind('keydown', opts.key[0] + ' shift+' + opts.key[0], func);
  1823. }
  1824. });
  1825. if (def) {
  1826. shower.attr('data-curopt', btn_opts[def].sel);
  1827. } else if (!shower.attr('data-curopt')) {
  1828. // Set first as default
  1829. shower.attr('data-curopt', btn_opts[0].sel);
  1830. }
  1831. var timer;
  1832. var pos = $(show_sel).position();
  1833. // Clicking the "show" icon should set the current mode
  1834. shower.mousedown(function(evt) {
  1835. if (shower.hasClass('disabled')) {
  1836. return false;
  1837. }
  1838. var holder = $(hold_sel);
  1839. var l = pos.left + 34;
  1840. var w = holder.width() * -1;
  1841. var time = holder.data('shown_popop') ? 200 : 0;
  1842. timer = setTimeout(function() {
  1843. // Show corresponding menu
  1844. if (!shower.data('isLibrary')) {
  1845. holder.css('left', w).show().animate({
  1846. left: l
  1847. }, 150);
  1848. } else {
  1849. holder.css('left', l).show();
  1850. }
  1851. holder.data('shown_popop', true);
  1852. },time);
  1853. evt.preventDefault();
  1854. }).mouseup(function(evt) {
  1855. clearTimeout(timer);
  1856. var opt = $(this).attr('data-curopt');
  1857. // Is library and popped up, so do nothing
  1858. if (shower.data('isLibrary') && $(show_sel.replace('_show', '')).is(':visible')) {
  1859. toolButtonClick(show_sel, true);
  1860. return;
  1861. }
  1862. if (toolButtonClick(show_sel) && flyout_funcs[opt]) {
  1863. flyout_funcs[opt]();
  1864. }
  1865. });
  1866. // $('#tools_rect').mouseleave(function(){$('#tools_rect').fadeOut();});
  1867. });
  1868. setFlyoutTitles();
  1869. setFlyoutPositions();
  1870. };
  1871. var makeFlyoutHolder = function(id, child) {
  1872. var div = $('<div>', {
  1873. 'class': 'tools_flyout',
  1874. id: id
  1875. }).appendTo('#svg_editor').append(child);
  1876. return div;
  1877. };
  1878. var uaPrefix = (function() {
  1879. var prop;
  1880. var regex = /^(Moz|Webkit|Khtml|O|ms|Icab)(?=[A-Z])/;
  1881. var someScript = document.getElementsByTagName('script')[0];
  1882. for (prop in someScript.style) {
  1883. if (regex.test(prop)) {
  1884. // test is faster than match, so it's better to perform
  1885. // that on the lot and match only when necessary
  1886. return prop.match(regex)[0];
  1887. }
  1888. }
  1889. // Nothing found so far?
  1890. if ('WebkitOpacity' in someScript.style) {return 'Webkit';}
  1891. if ('KhtmlOpacity' in someScript.style) {return 'Khtml';}
  1892. return '';
  1893. }());
  1894. var scaleElements = function(elems, scale) {
  1895. // var prefix = '-' + uaPrefix.toLowerCase() + '-'; // Currently unused
  1896. var sides = ['top', 'left', 'bottom', 'right'];
  1897. elems.each(function() {
  1898. // Handled in CSS
  1899. // this.style[uaPrefix + 'Transform'] = 'scale(' + scale + ')';
  1900. var i;
  1901. var el = $(this);
  1902. var w = el.outerWidth() * (scale - 1);
  1903. var h = el.outerHeight() * (scale - 1);
  1904. // var margins = {}; // Currently unused
  1905. for (i = 0; i < 4; i++) {
  1906. var s = sides[i];
  1907. var cur = el.data('orig_margin-' + s);
  1908. if (cur == null) {
  1909. cur = parseInt(el.css('margin-' + s), 10);
  1910. // Cache the original margin
  1911. el.data('orig_margin-' + s, cur);
  1912. }
  1913. var val = cur * scale;
  1914. if (s === 'right') {
  1915. val += w;
  1916. } else if (s === 'bottom') {
  1917. val += h;
  1918. }
  1919. el.css('margin-' + s, val);
  1920. // el.css('outline', '1px solid red');
  1921. }
  1922. });
  1923. };
  1924. var setIconSize = editor.setIconSize = function (size) {
  1925. // var elems = $('.tool_button, .push_button, .tool_button_current, .disabled, .icon_label, #url_notice, #tool_open');
  1926. var sel_toscale = '#tools_top .toolset, #editor_panel > *, #history_panel > *,'+
  1927. ' #main_button, #tools_left > *, #path_node_panel > *, #multiselected_panel > *,'+
  1928. ' #g_panel > *, #tool_font_size > *, .tools_flyout';
  1929. var elems = $(sel_toscale);
  1930. var scale = 1;
  1931. if (typeof size === 'number') {
  1932. scale = size;
  1933. } else {
  1934. var icon_sizes = {s: 0.75, m:1, l: 1.25, xl: 1.5};
  1935. scale = icon_sizes[size];
  1936. }
  1937. editor.tool_scale = scale;
  1938. setFlyoutPositions();
  1939. // $('.tools_flyout').each(function() {
  1940. // var pos = $(this).position();
  1941. // console.log($(this), pos.left+(34 * scale));
  1942. // $(this).css({'left': pos.left+(34 * scale), 'top': pos.top+(77 * scale)});
  1943. // console.log('l', $(this).css('left'));
  1944. // });
  1945. // var scale = .75;
  1946. var hidden_ps = elems.parents(':hidden');
  1947. hidden_ps.css('visibility', 'hidden').show();
  1948. scaleElements(elems, scale);
  1949. hidden_ps.css('visibility', 'visible').hide();
  1950. // return;
  1951. $.pref('iconsize', size);
  1952. $('#iconsize').val(size);
  1953. // Change icon size
  1954. // $('.tool_button, .push_button, .tool_button_current, .disabled, .icon_label, #url_notice, #tool_open')
  1955. // .find('> svg, > img').each(function() {
  1956. // this.setAttribute('width',size_num);
  1957. // this.setAttribute('height',size_num);
  1958. // });
  1959. //
  1960. // $.resizeSvgIcons({
  1961. // '.flyout_arrow_horiz > svg, .flyout_arrow_horiz > img': size_num / 5,
  1962. // '#logo > svg, #logo > img': size_num * 1.3,
  1963. // '#tools_bottom .icon_label > *': (size_num === 16 ? 18 : size_num * .75)
  1964. // });
  1965. // if (size != 's') {
  1966. // $.resizeSvgIcons({'#layerbuttons svg, #layerbuttons img': size_num * .6});
  1967. // }
  1968. // Note that all rules will be prefixed with '#svg_editor' when parsed
  1969. var cssResizeRules = {
  1970. // '.tool_button,\
  1971. // .push_button,\
  1972. // .tool_button_current,\
  1973. // .push_button_pressed,\
  1974. // .disabled,\
  1975. // .icon_label,\
  1976. // .tools_flyout .tool_button': {
  1977. // 'width': {s: '16px', l: '32px', xl: '48px'},
  1978. // 'height': {s: '16px', l: '32px', xl: '48px'},
  1979. // 'padding': {s: '1px', l: '2px', xl: '3px'}
  1980. // },
  1981. // '.tool_sep': {
  1982. // 'height': {s: '16px', l: '32px', xl: '48px'},
  1983. // 'margin': {s: '2px 2px', l: '2px 5px', xl: '2px 8px'}
  1984. // },
  1985. // '#main_icon': {
  1986. // 'width': {s: '31px', l: '53px', xl: '75px'},
  1987. // 'height': {s: '22px', l: '42px', xl: '64px'}
  1988. // },
  1989. '#tools_top': {
  1990. 'left': 50,
  1991. 'height': 72
  1992. },
  1993. '#tools_left': {
  1994. 'width': 31,
  1995. 'top': 74
  1996. },
  1997. 'div#workarea': {
  1998. 'left': 38,
  1999. 'top': 74
  2000. }
  2001. // '#tools_bottom': {
  2002. // 'left': {s: '27px', l: '46px', xl: '65px'},
  2003. // 'height': {s: '58px', l: '98px', xl: '145px'}
  2004. // },
  2005. // '#color_tools': {
  2006. // 'border-spacing': {s: '0 1px'},
  2007. // 'margin-top': {s: '-1px'}
  2008. // },
  2009. // '#color_tools .icon_label': {
  2010. // 'width': {l:'43px', xl: '60px'}
  2011. // },
  2012. // '.color_tool': {
  2013. // 'height': {s: '20px'}
  2014. // },
  2015. // '#tool_opacity': {
  2016. // 'top': {s: '1px'},
  2017. // 'height': {s: 'auto', l:'auto', xl:'auto'}
  2018. // },
  2019. // '#tools_top input, #tools_bottom input': {
  2020. // 'margin-top': {s: '2px', l: '4px', xl: '5px'},
  2021. // 'height': {s: 'auto', l: 'auto', xl: 'auto'},
  2022. // 'border': {s: '1px solid #555', l: 'auto', xl: 'auto'},
  2023. // 'font-size': {s: '.9em', l: '1.2em', xl: '1.4em'}
  2024. // },
  2025. // '#zoom_panel': {
  2026. // 'margin-top': {s: '3px', l: '4px', xl: '5px'}
  2027. // },
  2028. // '#copyright, #tools_bottom .label': {
  2029. // 'font-size': {l: '1.5em', xl: '2em'},
  2030. // 'line-height': {s: '15px'}
  2031. // },
  2032. // '#tools_bottom_2': {
  2033. // 'width': {l: '295px', xl: '355px'},
  2034. // 'top': {s: '4px'}
  2035. // },
  2036. // '#tools_top > div, #tools_top': {
  2037. // 'line-height': {s: '17px', l: '34px', xl: '50px'}
  2038. // },
  2039. // '.dropdown button': {
  2040. // 'height': {s: '18px', l: '34px', xl: '40px'},
  2041. // 'line-height': {s: '18px', l: '34px', xl: '40px'},
  2042. // 'margin-top': {s: '3px'}
  2043. // },
  2044. // '#tools_top label, #tools_bottom label': {
  2045. // 'font-size': {s: '1em', l: '1.5em', xl: '2em'},
  2046. // 'height': {s: '25px', l: '42px', xl: '64px'}
  2047. // },
  2048. // 'div.toolset': {
  2049. // 'height': {s: '25px', l: '42px', xl: '64px'}
  2050. // },
  2051. // '#tool_bold, #tool_italic': {
  2052. // 'font-size': {s: '1.5em', l: '3em', xl: '4.5em'}
  2053. // },
  2054. // '#sidepanels': {
  2055. // 'top': {s: '50px', l: '88px', xl: '125px'},
  2056. // 'bottom': {s: '51px', l: '68px', xl: '65px'}
  2057. // },
  2058. // '#layerbuttons': {
  2059. // 'width': {l: '130px', xl: '175px'},
  2060. // 'height': {l: '24px', xl: '30px'}
  2061. // },
  2062. // '#layerlist': {
  2063. // 'width': {l: '128px', xl: '150px'}
  2064. // },
  2065. // '.layer_button': {
  2066. // 'width': {l: '19px', xl: '28px'},
  2067. // 'height': {l: '19px', xl: '28px'}
  2068. // },
  2069. // 'input.spin-button': {
  2070. // 'background-image': {l: 'url('images/spinbtn_updn_big.png')', xl: 'url('images/spinbtn_updn_big.png')'},
  2071. // 'background-position': {l: '100% -5px', xl: '100% -2px'},
  2072. // 'padding-right': {l: '24px', xl: '24px' }
  2073. // },
  2074. // 'input.spin-button.up': {
  2075. // 'background-position': {l: '100% -45px', xl: '100% -42px'}
  2076. // },
  2077. // 'input.spin-button.down': {
  2078. // 'background-position': {l: '100% -85px', xl: '100% -82px'}
  2079. // },
  2080. // '#position_opts': {
  2081. // 'width': {all: (size_num*4) +'px'}
  2082. // }
  2083. };
  2084. var rule_elem = $('#tool_size_rules');
  2085. if (!rule_elem.length) {
  2086. rule_elem = $('<style id="tool_size_rules"><\/style>').appendTo('head');
  2087. } else {
  2088. rule_elem.empty();
  2089. }
  2090. if (size !== 'm') {
  2091. var styleStr = '';
  2092. $.each(cssResizeRules, function(selector, rules) {
  2093. selector = '#svg_editor ' + selector.replace(/,/g,', #svg_editor');
  2094. styleStr += selector + '{';
  2095. $.each(rules, function(prop, values) {
  2096. var val;
  2097. if (typeof values === 'number') {
  2098. val = (values * scale) + 'px';
  2099. } else if (values[size] || values.all) {
  2100. val = (values[size] || values.all);
  2101. }
  2102. styleStr += (prop + ':' + val + ';');
  2103. });
  2104. styleStr += '}';
  2105. });
  2106. //this.style[uaPrefix + 'Transform'] = 'scale(' + scale + ')';
  2107. var prefix = '-' + uaPrefix.toLowerCase() + '-';
  2108. styleStr += (sel_toscale + '{' + prefix + 'transform: scale(' + scale + ');}'
  2109. + ' #svg_editor div.toolset .toolset {' + prefix + 'transform: scale(1); margin: 1px !important;}' // Hack for markers
  2110. + ' #svg_editor .ui-slider {' + prefix + 'transform: scale(' + (1/scale) + ');}' // Hack for sliders
  2111. );
  2112. rule_elem.text(styleStr);
  2113. }
  2114. setFlyoutPositions();
  2115. };
  2116. // TODO: Combine this with addDropDown or find other way to optimize
  2117. var addAltDropDown = function(elem, list, callback, opts) {
  2118. var button = $(elem);
  2119. list = $(list);
  2120. var on_button = false;
  2121. var dropUp = opts.dropUp;
  2122. if (dropUp) {
  2123. $(elem).addClass('dropup');
  2124. }
  2125. list.find('li').bind('mouseup', function() {
  2126. if (opts.seticon) {
  2127. setIcon('#cur_' + button[0].id , $(this).children());
  2128. $(this).addClass('current').siblings().removeClass('current');
  2129. }
  2130. callback.apply(this, arguments);
  2131. });
  2132. $(window).mouseup(function(evt) {
  2133. if (!on_button) {
  2134. button.removeClass('down');
  2135. list.hide();
  2136. list.css({top:0, left:0});
  2137. }
  2138. on_button = false;
  2139. });
  2140. // var height = list.height(); // Currently unused
  2141. button.bind('mousedown',function() {
  2142. var off = button.offset();
  2143. if (dropUp) {
  2144. off.top -= list.height();
  2145. off.left += 8;
  2146. } else {
  2147. off.top += button.height();
  2148. }
  2149. list.offset(off);
  2150. if (!button.hasClass('down')) {
  2151. list.show();
  2152. on_button = true;
  2153. } else {
  2154. // CSS position must be reset for Webkit
  2155. list.hide();
  2156. list.css({top:0, left:0});
  2157. }
  2158. button.toggleClass('down');
  2159. }).hover(function() {
  2160. on_button = true;
  2161. }).mouseout(function() {
  2162. on_button = false;
  2163. });
  2164. if (opts.multiclick) {
  2165. list.mousedown(function() {
  2166. on_button = true;
  2167. });
  2168. }
  2169. };
  2170. var extsPreLang = [];
  2171. var extAdded = function(win, ext) {
  2172. if (!ext) {
  2173. return;
  2174. }
  2175. var cb_called = false;
  2176. var resize_done = false;
  2177. var cb_ready = true; // Set to false to delay callback (e.g. wait for $.svgIcons)
  2178. if (ext.langReady) {
  2179. if (editor.langChanged) { // We check for this since the "lang" pref could have been set by storage
  2180. var lang = $.pref('lang');
  2181. ext.langReady({lang:lang, uiStrings:uiStrings});
  2182. }
  2183. else {
  2184. extsPreLang.push(ext);
  2185. }
  2186. }
  2187. function prepResize() {
  2188. if (resize_timer) {
  2189. clearTimeout(resize_timer);
  2190. resize_timer = null;
  2191. }
  2192. if (!resize_done) {
  2193. resize_timer = setTimeout(function() {
  2194. resize_done = true;
  2195. setIconSize($.pref('iconsize'));
  2196. }, 50);
  2197. }
  2198. }
  2199. var runCallback = function() {
  2200. if (ext.callback && !cb_called && cb_ready) {
  2201. cb_called = true;
  2202. ext.callback();
  2203. }
  2204. };
  2205. var btn_selects = [];
  2206. if (ext.context_tools) {
  2207. $.each(ext.context_tools, function(i, tool) {
  2208. // Add select tool
  2209. var html;
  2210. var cont_id = tool.container_id ? (' id="' + tool.container_id + '"') : '';
  2211. var panel = $('#' + tool.panel);
  2212. // create the panel if it doesn't exist
  2213. if (!panel.length) {
  2214. panel = $('<div>', {id: tool.panel}).appendTo('#tools_top');
  2215. }
  2216. // TODO: Allow support for other types, or adding to existing tool
  2217. switch (tool.type) {
  2218. case 'tool_button':
  2219. html = '<div class="tool_button">' + tool.id + '</div>';
  2220. var div = $(html).appendTo(panel);
  2221. if (tool.events) {
  2222. $.each(tool.events, function(evt, func) {
  2223. $(div).bind(evt, func);
  2224. });
  2225. }
  2226. break;
  2227. case 'select':
  2228. html = '<label' + cont_id + '>'
  2229. + '<select id="' + tool.id + '">';
  2230. $.each(tool.options, function(val, text) {
  2231. var sel = (val == tool.defval) ? " selected":"";
  2232. html += '<option value="'+val+'"' + sel + '>' + text + '</option>';
  2233. });
  2234. html += "</select></label>";
  2235. // Creates the tool, hides & adds it, returns the select element
  2236. var sel = $(html).appendTo(panel).find('select');
  2237. $.each(tool.events, function(evt, func) {
  2238. $(sel).bind(evt, func);
  2239. });
  2240. break;
  2241. case 'button-select':
  2242. html = '<div id="' + tool.id + '" class="dropdown toolset" title="' + tool.title + '">'
  2243. + '<div id="cur_' + tool.id + '" class="icon_label"></div><button></button></div>';
  2244. var list = $('<ul id="' + tool.id + '_opts"></ul>').appendTo('#option_lists');
  2245. if (tool.colnum) {
  2246. list.addClass('optcols' + tool.colnum);
  2247. }
  2248. // Creates the tool, hides & adds it, returns the select element
  2249. var dropdown = $(html).appendTo(panel).children();
  2250. btn_selects.push({
  2251. elem: ('#' + tool.id),
  2252. list: ('#' + tool.id + '_opts'),
  2253. title: tool.title,
  2254. callback: tool.events.change,
  2255. cur: ('#cur_' + tool.id)
  2256. });
  2257. break;
  2258. case 'input':
  2259. html = '<label' + cont_id + '>'
  2260. + '<span id="' + tool.id + '_label">'
  2261. + tool.label + ':</span>'
  2262. + '<input id="' + tool.id + '" title="' + tool.title
  2263. + '" size="' + (tool.size || "4") + '" value="' + (tool.defval || "") + '" type="text"/></label>';
  2264. // Creates the tool, hides & adds it, returns the select element
  2265. // Add to given tool.panel
  2266. var inp = $(html).appendTo(panel).find('input');
  2267. if (tool.spindata) {
  2268. inp.SpinButton(tool.spindata);
  2269. }
  2270. if (tool.events) {
  2271. $.each(tool.events, function(evt, func) {
  2272. inp.bind(evt, func);
  2273. });
  2274. }
  2275. break;
  2276. default:
  2277. break;
  2278. }
  2279. });
  2280. }
  2281. if (ext.buttons) {
  2282. var fallback_obj = {},
  2283. placement_obj = {},
  2284. svgicons = ext.svgicons,
  2285. holders = {};
  2286. // Add buttons given by extension
  2287. $.each(ext.buttons, function(i, btn) {
  2288. var icon, svgicon, tls_id;
  2289. var id = btn.id;
  2290. var num = i;
  2291. // Give button a unique ID
  2292. while($('#'+id).length) {
  2293. id = btn.id + '_' + (++num);
  2294. }
  2295. if (!svgicons) {
  2296. icon = $('<img src="' + btn.icon + '">');
  2297. } else {
  2298. fallback_obj[id] = btn.icon;
  2299. svgicon = btn.svgicon || btn.id;
  2300. if (btn.type == 'app_menu') {
  2301. placement_obj['#' + id + ' > div'] = svgicon;
  2302. } else {
  2303. placement_obj['#' + id] = svgicon;
  2304. }
  2305. }
  2306. var cls, parent;
  2307. // Set button up according to its type
  2308. switch ( btn.type ) {
  2309. case 'mode_flyout':
  2310. case 'mode':
  2311. cls = 'tool_button';
  2312. parent = '#tools_left';
  2313. break;
  2314. case 'context':
  2315. cls = 'tool_button';
  2316. parent = '#' + btn.panel;
  2317. // create the panel if it doesn't exist
  2318. if (!$(parent).length) {
  2319. $('<div>', {id: btn.panel}).appendTo('#tools_top');
  2320. }
  2321. break;
  2322. case 'app_menu':
  2323. cls = '';
  2324. parent = '#main_menu ul';
  2325. break;
  2326. }
  2327. var flyout_holder, cur_h, show_btn, ref_data, ref_btn;
  2328. var button = $((btn.list || btn.type == 'app_menu') ? '<li/>' : '<div/>')
  2329. .attr('id', id)
  2330. .attr('title', btn.title)
  2331. .addClass(cls);
  2332. if (!btn.includeWith && !btn.list) {
  2333. if ('position' in btn) {
  2334. if ($(parent).children().eq(btn.position).length) {
  2335. $(parent).children().eq(btn.position).before(button);
  2336. }
  2337. else {
  2338. $(parent).children().last().before(button);
  2339. }
  2340. } else {
  2341. button.appendTo(parent);
  2342. }
  2343. if (btn.type =='mode_flyout') {
  2344. // Add to flyout menu / make flyout menu
  2345. // var opts = btn.includeWith;
  2346. // // opts.button, default, position
  2347. ref_btn = $(button);
  2348. flyout_holder = ref_btn.parent();
  2349. // Create a flyout menu if there isn't one already
  2350. if (!ref_btn.parent().hasClass('tools_flyout')) {
  2351. // Create flyout placeholder
  2352. tls_id = ref_btn[0].id.replace('tool_', 'tools_');
  2353. show_btn = ref_btn.clone()
  2354. .attr('id', tls_id + '_show')
  2355. .append($('<div>', {'class': 'flyout_arrow_horiz'}));
  2356. ref_btn.before(show_btn);
  2357. // Create a flyout div
  2358. flyout_holder = makeFlyoutHolder(tls_id, ref_btn);
  2359. flyout_holder.data('isLibrary', true);
  2360. show_btn.data('isLibrary', true);
  2361. }
  2362. // ref_data = Actions.getButtonData(opts.button);
  2363. placement_obj['#' + tls_id + '_show'] = btn.id;
  2364. // TODO: Find way to set the current icon using the iconloader if this is not default
  2365. // Include data for extension button as well as ref button
  2366. cur_h = holders['#'+flyout_holder[0].id] = [{
  2367. sel: '#'+id,
  2368. fn: btn.events.click,
  2369. icon: btn.id,
  2370. // key: btn.key,
  2371. isDefault: true
  2372. }, ref_data];
  2373. //
  2374. // // {sel:'#tool_rect', fn: clickRect, evt: 'mouseup', key: 4, parent: '#tools_rect', icon: 'rect'}
  2375. //
  2376. // var pos = ('position' in opts)?opts.position:'last';
  2377. // var len = flyout_holder.children().length;
  2378. //
  2379. // // Add at given position or end
  2380. // if (!isNaN(pos) && pos >= 0 && pos < len) {
  2381. // flyout_holder.children().eq(pos).before(button);
  2382. // } else {
  2383. // flyout_holder.append(button);
  2384. // cur_h.reverse();
  2385. // }
  2386. } else if (btn.type == 'app_menu') {
  2387. button.append('<div>').append(btn.title);
  2388. }
  2389. }
  2390. else if (btn.list) {
  2391. // Add button to list
  2392. button.addClass('push_button');
  2393. $('#' + btn.list + '_opts').append(button);
  2394. if (btn.isDefault) {
  2395. $('#cur_' + btn.list).append(button.children().clone());
  2396. svgicon = btn.svgicon || btn.id;
  2397. placement_obj['#cur_' + btn.list] = svgicon;
  2398. }
  2399. }
  2400. else if (btn.includeWith) {
  2401. // Add to flyout menu / make flyout menu
  2402. var opts = btn.includeWith;
  2403. // opts.button, default, position
  2404. ref_btn = $(opts.button);
  2405. flyout_holder = ref_btn.parent();
  2406. // Create a flyout menu if there isn't one already
  2407. if (!ref_btn.parent().hasClass('tools_flyout')) {
  2408. // Create flyout placeholder
  2409. tls_id = ref_btn[0].id.replace('tool_', 'tools_');
  2410. show_btn = ref_btn.clone()
  2411. .attr('id',tls_id + '_show')
  2412. .append($('<div>', {'class': 'flyout_arrow_horiz'}));
  2413. ref_btn.before(show_btn);
  2414. // Create a flyout div
  2415. flyout_holder = makeFlyoutHolder(tls_id, ref_btn);
  2416. }
  2417. ref_data = Actions.getButtonData(opts.button);
  2418. if (opts.isDefault) {
  2419. placement_obj['#' + tls_id + '_show'] = btn.id;
  2420. }
  2421. // TODO: Find way to set the current icon using the iconloader if this is not default
  2422. // Include data for extension button as well as ref button
  2423. cur_h = holders['#'+flyout_holder[0].id] = [{
  2424. sel: '#'+id,
  2425. fn: btn.events.click,
  2426. icon: btn.id,
  2427. key: btn.key,
  2428. isDefault: btn.includeWith?btn.includeWith.isDefault:0
  2429. }, ref_data];
  2430. // {sel:'#tool_rect', fn: clickRect, evt: 'mouseup', key: 4, parent: '#tools_rect', icon: 'rect'}
  2431. var pos = ('position' in opts) ? opts.position : 'last';
  2432. var len = flyout_holder.children().length;
  2433. // Add at given position or end
  2434. if (!isNaN(pos) && pos >= 0 && pos < len) {
  2435. flyout_holder.children().eq(pos).before(button);
  2436. } else {
  2437. flyout_holder.append(button);
  2438. cur_h.reverse();
  2439. }
  2440. }
  2441. if (!svgicons) {
  2442. button.append(icon);
  2443. }
  2444. if (!btn.list) {
  2445. // Add given events to button
  2446. $.each(btn.events, function(name, func) {
  2447. if (name == 'click' && btn.type == 'mode') {
  2448. if (btn.includeWith) {
  2449. button.bind(name, func);
  2450. } else {
  2451. button.bind(name, function() {
  2452. if (toolButtonClick(button)) {
  2453. func();
  2454. }
  2455. });
  2456. }
  2457. if (btn.key) {
  2458. $(document).bind('keydown', btn.key, func);
  2459. if (btn.title) {
  2460. button.attr('title', btn.title + ' ['+btn.key+']');
  2461. }
  2462. }
  2463. } else {
  2464. button.bind(name, func);
  2465. }
  2466. });
  2467. }
  2468. setupFlyouts(holders);
  2469. });
  2470. $.each(btn_selects, function() {
  2471. addAltDropDown(this.elem, this.list, this.callback, {seticon: true});
  2472. });
  2473. if (svgicons) {
  2474. cb_ready = false; // Delay callback
  2475. }
  2476. $.svgIcons(svgicons, {
  2477. w: 24, h: 24,
  2478. id_match: false,
  2479. no_img: (!svgedit.browser.isWebkit()),
  2480. fallback: fallback_obj,
  2481. placement: placement_obj,
  2482. callback: function (icons) {
  2483. // Non-ideal hack to make the icon match the current size
  2484. //if (curPrefs.iconsize && curPrefs.iconsize !== 'm') {
  2485. if ($.pref('iconsize') !== 'm') {
  2486. prepResize();
  2487. }
  2488. cb_ready = true; // Ready for callback
  2489. runCallback();
  2490. }
  2491. });
  2492. }
  2493. runCallback();
  2494. };
  2495. var getPaint = function(color, opac, type) {
  2496. // update the editor's fill paint
  2497. var opts = { alpha: opac };
  2498. if (color.indexOf('url(#') === 0) {
  2499. var refElem = svgCanvas.getRefElem(color);
  2500. if (refElem) {
  2501. refElem = refElem.cloneNode(true);
  2502. } else {
  2503. refElem = $('#' + type + '_color defs *')[0];
  2504. }
  2505. opts[refElem.tagName] = refElem;
  2506. } else if (color.indexOf('#') === 0) {
  2507. opts.solidColor = color.substr(1);
  2508. } else {
  2509. opts.solidColor = 'none';
  2510. }
  2511. return new $.jGraduate.Paint(opts);
  2512. };
  2513. $('#text').focus( function(){ textBeingEntered = true; } );
  2514. $('#text').blur( function(){ textBeingEntered = false; } );
  2515. // bind the selected event to our function that handles updates to the UI
  2516. svgCanvas.bind('selected', selectedChanged);
  2517. svgCanvas.bind('transition', elementTransition);
  2518. svgCanvas.bind('changed', elementChanged);
  2519. svgCanvas.bind('saved', saveHandler);
  2520. svgCanvas.bind('exported', exportHandler);
  2521. svgCanvas.bind('zoomed', zoomChanged);
  2522. svgCanvas.bind('contextset', contextChanged);
  2523. svgCanvas.bind('extension_added', extAdded);
  2524. svgCanvas.textActions.setInputElem($('#text')[0]);
  2525. var str = '<div class="palette_item" data-rgb="none"></div>';
  2526. $.each(palette, function(i, item) {
  2527. str += '<div class="palette_item" style="background-color: ' + item + ';" data-rgb="' + item + '"></div>';
  2528. });
  2529. $('#palette').append(str);
  2530. // Set up editor background functionality
  2531. // TODO add checkerboard as "pattern"
  2532. var color_blocks = ['#FFF', '#888', '#000']; // ,'url(data:image/gif;base64,R0lGODlhEAAQAIAAAP%2F%2F%2F9bW1iH5BAAAAAAALAAAAAAQABAAAAIfjG%2Bgq4jM3IFLJgpswNly%2FXkcBpIiVaInlLJr9FZWAQA7)'];
  2533. str = '';
  2534. $.each(color_blocks, function() {
  2535. str += '<div class="color_block" style="background-color:' + this + ';"></div>';
  2536. });
  2537. $('#bg_blocks').append(str);
  2538. var blocks = $('#bg_blocks div');
  2539. var cur_bg = 'cur_background';
  2540. blocks.each(function() {
  2541. var blk = $(this);
  2542. blk.click(function() {
  2543. blocks.removeClass(cur_bg);
  2544. $(this).addClass(cur_bg);
  2545. });
  2546. });
  2547. setBackground($.pref('bkgd_color'), $.pref('bkgd_url'));
  2548. $('#image_save_opts input').val([$.pref('img_save')]);
  2549. var changeRectRadius = function(ctl) {
  2550. svgCanvas.setRectRadius(ctl.value);
  2551. };
  2552. var changeFontSize = function(ctl) {
  2553. svgCanvas.setFontSize(ctl.value);
  2554. };
  2555. var changeStrokeWidth = function(ctl) {
  2556. var val = ctl.value;
  2557. if (val == 0 && selectedElement && ['line', 'polyline'].indexOf(selectedElement.nodeName) >= 0) {
  2558. val = ctl.value = 1;
  2559. }
  2560. svgCanvas.setStrokeWidth(val);
  2561. };
  2562. var changeRotationAngle = function(ctl) {
  2563. svgCanvas.setRotationAngle(ctl.value);
  2564. $('#tool_reorient').toggleClass('disabled', parseInt(ctl.value, 10) === 0);
  2565. };
  2566. var changeOpacity = function(ctl, val) {
  2567. if (val == null) {val = ctl.value;}
  2568. $('#group_opacity').val(val);
  2569. if (!ctl || !ctl.handle) {
  2570. $('#opac_slider').slider('option', 'value', val);
  2571. }
  2572. svgCanvas.setOpacity(val/100);
  2573. };
  2574. var changeBlur = function(ctl, val, noUndo) {
  2575. if (val == null) {val = ctl.value;}
  2576. $('#blur').val(val);
  2577. var complete = false;
  2578. if (!ctl || !ctl.handle) {
  2579. $('#blur_slider').slider('option', 'value', val);
  2580. complete = true;
  2581. }
  2582. if (noUndo) {
  2583. svgCanvas.setBlurNoUndo(val);
  2584. } else {
  2585. svgCanvas.setBlur(val, complete);
  2586. }
  2587. };
  2588. $('#stroke_style').change(function() {
  2589. svgCanvas.setStrokeAttr('stroke-dasharray', $(this).val());
  2590. operaRepaint();
  2591. });
  2592. $('#stroke_linejoin').change(function() {
  2593. svgCanvas.setStrokeAttr('stroke-linejoin', $(this).val());
  2594. operaRepaint();
  2595. });
  2596. // Lose focus for select elements when changed (Allows keyboard shortcuts to work better)
  2597. $('select').change(function(){$(this).blur();});
  2598. // fired when user wants to move elements to another layer
  2599. var promptMoveLayerOnce = false;
  2600. $('#selLayerNames').change(function() {
  2601. var destLayer = this.options[this.selectedIndex].value;
  2602. var confirmStr = uiStrings.notification.QmoveElemsToLayer.replace('%s', destLayer);
  2603. var moveToLayer = function(ok) {
  2604. if (!ok) {return;}
  2605. promptMoveLayerOnce = true;
  2606. svgCanvas.moveSelectedToLayer(destLayer);
  2607. svgCanvas.clearSelection();
  2608. populateLayers();
  2609. };
  2610. if (destLayer) {
  2611. if (promptMoveLayerOnce) {
  2612. moveToLayer(true);
  2613. } else {
  2614. $.confirm(confirmStr, moveToLayer);
  2615. }
  2616. }
  2617. });
  2618. $('#font_family').change(function() {
  2619. svgCanvas.setFontFamily(this.value);
  2620. });
  2621. $('#seg_type').change(function() {
  2622. svgCanvas.setSegType($(this).val());
  2623. });
  2624. $('#text').keyup(function() {
  2625. svgCanvas.setTextContent(this.value);
  2626. });
  2627. $('#image_url').change(function() {
  2628. setImageURL(this.value);
  2629. });
  2630. $('#link_url').change(function() {
  2631. if (this.value.length) {
  2632. svgCanvas.setLinkURL(this.value);
  2633. } else {
  2634. svgCanvas.removeHyperlink();
  2635. }
  2636. });
  2637. $('#g_title').change(function() {
  2638. svgCanvas.setGroupTitle(this.value);
  2639. });
  2640. $('.attr_changer').change(function() {
  2641. var attr = this.getAttribute('data-attr');
  2642. var val = this.value;
  2643. var valid = svgedit.units.isValidUnit(attr, val, selectedElement);
  2644. if (!valid) {
  2645. $.alert(uiStrings.notification.invalidAttrValGiven);
  2646. this.value = selectedElement.getAttribute(attr);
  2647. return false;
  2648. }
  2649. if (attr !== 'id') {
  2650. if (isNaN(val)) {
  2651. val = svgCanvas.convertToNum(attr, val);
  2652. } else if (curConfig.baseUnit !== 'px') {
  2653. // Convert unitless value to one with given unit
  2654. var unitData = svgedit.units.getTypeMap();
  2655. if (selectedElement[attr] || svgCanvas.getMode() === 'pathedit' || attr === 'x' || attr === 'y') {
  2656. val *= unitData[curConfig.baseUnit];
  2657. }
  2658. }
  2659. }
  2660. // if the user is changing the id, then de-select the element first
  2661. // change the ID, then re-select it with the new ID
  2662. if (attr === 'id') {
  2663. var elem = selectedElement;
  2664. svgCanvas.clearSelection();
  2665. elem.id = val;
  2666. svgCanvas.addToSelection([elem],true);
  2667. } else {
  2668. svgCanvas.changeSelectedAttribute(attr, val);
  2669. }
  2670. this.blur();
  2671. });
  2672. // Prevent selection of elements when shift-clicking
  2673. $('#palette').mouseover(function() {
  2674. var inp = $('<input type="hidden">');
  2675. $(this).append(inp);
  2676. inp.focus().remove();
  2677. });
  2678. $('.palette_item').mousedown(function(evt) {
  2679. // shift key or right click for stroke
  2680. var picker = evt.shiftKey || evt.button === 2 ? 'stroke' : 'fill';
  2681. var color = $(this).data('rgb');
  2682. var paint;
  2683. // Webkit-based browsers returned 'initial' here for no stroke
  2684. if (color === 'none' || color === 'transparent' || color === 'initial') {
  2685. color = 'none';
  2686. paint = new $.jGraduate.Paint();
  2687. } else {
  2688. paint = new $.jGraduate.Paint({alpha: 100, solidColor: color.substr(1)});
  2689. }
  2690. paintBox[picker].setPaint(paint);
  2691. svgCanvas.setColor(picker, color);
  2692. if (color !== 'none' && svgCanvas.getPaintOpacity(picker) !== 1) {
  2693. svgCanvas.setPaintOpacity(picker, 1.0);
  2694. }
  2695. updateToolButtonState();
  2696. }).bind('contextmenu', function(e) {e.preventDefault();});
  2697. $('#toggle_stroke_tools').on('click', function() {
  2698. $('#tools_bottom').toggleClass('expanded');
  2699. });
  2700. (function() {
  2701. var last_x = null, last_y = null, w_area = workarea[0],
  2702. panning = false, keypan = false;
  2703. $('#svgcanvas').bind('mousemove mouseup', function(evt) {
  2704. if (panning === false) {return;}
  2705. w_area.scrollLeft -= (evt.clientX - last_x);
  2706. w_area.scrollTop -= (evt.clientY - last_y);
  2707. last_x = evt.clientX;
  2708. last_y = evt.clientY;
  2709. if (evt.type === 'mouseup') {panning = false;}
  2710. return false;
  2711. }).mousedown(function(evt) {
  2712. if (evt.button === 1 || keypan === true) {
  2713. panning = true;
  2714. last_x = evt.clientX;
  2715. last_y = evt.clientY;
  2716. return false;
  2717. }
  2718. });
  2719. $(window).mouseup(function() {
  2720. panning = false;
  2721. });
  2722. $(document).bind('keydown', 'space', function(evt) {
  2723. svgCanvas.spaceKey = keypan = true;
  2724. evt.preventDefault();
  2725. }).bind('keyup', 'space', function(evt) {
  2726. evt.preventDefault();
  2727. svgCanvas.spaceKey = keypan = false;
  2728. }).bind('keydown', 'shift', function(evt) {
  2729. if (svgCanvas.getMode() === 'zoom') {
  2730. workarea.css('cursor', zoomOutIcon);
  2731. }
  2732. }).bind('keyup', 'shift', function(evt) {
  2733. if (svgCanvas.getMode() === 'zoom') {
  2734. workarea.css('cursor', zoomInIcon);
  2735. }
  2736. });
  2737. editor.setPanning = function(active) {
  2738. svgCanvas.spaceKey = keypan = active;
  2739. };
  2740. }());
  2741. (function () {
  2742. var button = $('#main_icon');
  2743. var overlay = $('#main_icon span');
  2744. var list = $('#main_menu');
  2745. var on_button = false;
  2746. var height = 0;
  2747. var js_hover = true;
  2748. var set_click = false;
  2749. /*
  2750. // Currently unused
  2751. var hideMenu = function() {
  2752. list.fadeOut(200);
  2753. };
  2754. */
  2755. $(window).mouseup(function(evt) {
  2756. if (!on_button) {
  2757. button.removeClass('buttondown');
  2758. // do not hide if it was the file input as that input needs to be visible
  2759. // for its change event to fire
  2760. if (evt.target.tagName != 'INPUT') {
  2761. list.fadeOut(200);
  2762. } else if (!set_click) {
  2763. set_click = true;
  2764. $(evt.target).click(function() {
  2765. list.css('margin-left', '-9999px').show();
  2766. });
  2767. }
  2768. }
  2769. on_button = false;
  2770. }).mousedown(function(evt) {
  2771. // $('.contextMenu').hide();
  2772. var islib = $(evt.target).closest('div.tools_flyout, .contextMenu').length;
  2773. if (!islib) {$('.tools_flyout:visible,.contextMenu').fadeOut(250);}
  2774. });
  2775. overlay.bind('mousedown',function() {
  2776. if (!button.hasClass('buttondown')) {
  2777. // Margin must be reset in case it was changed before;
  2778. list.css('margin-left', 0).show();
  2779. if (!height) {
  2780. height = list.height();
  2781. }
  2782. // Using custom animation as slideDown has annoying 'bounce effect'
  2783. list.css('height',0).animate({
  2784. 'height': height
  2785. }, 200);
  2786. on_button = true;
  2787. } else {
  2788. list.fadeOut(200);
  2789. }
  2790. button.toggleClass('buttondown buttonup');
  2791. }).hover(function() {
  2792. on_button = true;
  2793. }).mouseout(function() {
  2794. on_button = false;
  2795. });
  2796. var list_items = $('#main_menu li');
  2797. // Check if JS method of hovering needs to be used (Webkit bug)
  2798. list_items.mouseover(function() {
  2799. js_hover = ($(this).css('background-color') == 'rgba(0, 0, 0, 0)');
  2800. list_items.unbind('mouseover');
  2801. if (js_hover) {
  2802. list_items.mouseover(function() {
  2803. this.style.backgroundColor = '#FFC';
  2804. }).mouseout(function() {
  2805. this.style.backgroundColor = 'transparent';
  2806. return true;
  2807. });
  2808. }
  2809. });
  2810. }());
  2811. // Made public for UI customization.
  2812. // TODO: Group UI functions into a public svgEditor.ui interface.
  2813. editor.addDropDown = function(elem, callback, dropUp) {
  2814. if ($(elem).length == 0) {return;} // Quit if called on non-existant element
  2815. var button = $(elem).find('button');
  2816. var list = $(elem).find('ul').attr('id', $(elem)[0].id + '-list');
  2817. var on_button = false;
  2818. if (dropUp) {
  2819. $(elem).addClass('dropup');
  2820. } else {
  2821. // Move list to place where it can overflow container
  2822. $('#option_lists').append(list);
  2823. }
  2824. list.find('li').bind('mouseup', callback);
  2825. $(window).mouseup(function(evt) {
  2826. if (!on_button) {
  2827. button.removeClass('down');
  2828. list.hide();
  2829. }
  2830. on_button = false;
  2831. });
  2832. button.bind('mousedown',function() {
  2833. if (!button.hasClass('down')) {
  2834. if (!dropUp) {
  2835. var pos = $(elem).position();
  2836. list.css({
  2837. top: pos.top + 24,
  2838. left: pos.left - 10
  2839. });
  2840. }
  2841. list.show();
  2842. on_button = true;
  2843. } else {
  2844. list.hide();
  2845. }
  2846. button.toggleClass('down');
  2847. }).hover(function() {
  2848. on_button = true;
  2849. }).mouseout(function() {
  2850. on_button = false;
  2851. });
  2852. };
  2853. editor.addDropDown('#font_family_dropdown', function() {
  2854. $('#font_family').val($(this).text()).change();
  2855. });
  2856. editor.addDropDown('#opacity_dropdown', function() {
  2857. if ($(this).find('div').length) {return;}
  2858. var perc = parseInt($(this).text().split('%')[0], 10);
  2859. changeOpacity(false, perc);
  2860. }, true);
  2861. // For slider usage, see: http://jqueryui.com/demos/slider/
  2862. $('#opac_slider').slider({
  2863. start: function() {
  2864. $('#opacity_dropdown li:not(.special)').hide();
  2865. },
  2866. stop: function() {
  2867. $('#opacity_dropdown li').show();
  2868. $(window).mouseup();
  2869. },
  2870. slide: function(evt, ui) {
  2871. changeOpacity(ui);
  2872. }
  2873. });
  2874. editor.addDropDown('#blur_dropdown', $.noop);
  2875. var slideStart = false;
  2876. $('#blur_slider').slider({
  2877. max: 10,
  2878. step: 0.1,
  2879. stop: function(evt, ui) {
  2880. slideStart = false;
  2881. changeBlur(ui);
  2882. $('#blur_dropdown li').show();
  2883. $(window).mouseup();
  2884. },
  2885. start: function() {
  2886. slideStart = true;
  2887. },
  2888. slide: function(evt, ui) {
  2889. changeBlur(ui, null, slideStart);
  2890. }
  2891. });
  2892. editor.addDropDown('#zoom_dropdown', function() {
  2893. var item = $(this);
  2894. var val = item.data('val');
  2895. if (val) {
  2896. zoomChanged(window, val);
  2897. } else {
  2898. changeZoom({value: parseInt(item.text(), 10)});
  2899. }
  2900. }, true);
  2901. addAltDropDown('#stroke_linecap', '#linecap_opts', function() {
  2902. setStrokeOpt(this, true);
  2903. }, {dropUp: true});
  2904. addAltDropDown('#stroke_linejoin', '#linejoin_opts', function() {
  2905. setStrokeOpt(this, true);
  2906. }, {dropUp: true});
  2907. addAltDropDown('#tool_position', '#position_opts', function() {
  2908. var letter = this.id.replace('tool_pos', '').charAt(0);
  2909. svgCanvas.alignSelectedElements(letter, 'page');
  2910. }, {multiclick: true});
  2911. /*
  2912. When a flyout icon is selected
  2913. (if flyout) {
  2914. - Change the icon
  2915. - Make pressing the button run its stuff
  2916. }
  2917. - Run its stuff
  2918. When its shortcut key is pressed
  2919. - If not current in list, do as above
  2920. , else:
  2921. - Just run its stuff
  2922. */
  2923. // Unfocus text input when workarea is mousedowned.
  2924. (function() {
  2925. var inp;
  2926. var unfocus = function() {
  2927. $(inp).blur();
  2928. };
  2929. $('#svg_editor').find('button, select, input:not(#text)').focus(function() {
  2930. inp = this;
  2931. ui_context = 'toolbars';
  2932. workarea.mousedown(unfocus);
  2933. }).blur(function() {
  2934. ui_context = 'canvas';
  2935. workarea.unbind('mousedown', unfocus);
  2936. // Go back to selecting text if in textedit mode
  2937. if (svgCanvas.getMode() == 'textedit') {
  2938. $('#text').focus();
  2939. }
  2940. });
  2941. }());
  2942. var clickFHPath = function() {
  2943. if (toolButtonClick('#tool_fhpath')) {
  2944. svgCanvas.setMode('fhpath');
  2945. }
  2946. };
  2947. var clickLine = function() {
  2948. if (toolButtonClick('#tool_line')) {
  2949. svgCanvas.setMode('line');
  2950. }
  2951. };
  2952. var clickSquare = function() {
  2953. if (toolButtonClick('#tool_square')) {
  2954. svgCanvas.setMode('square');
  2955. }
  2956. };
  2957. var clickRect = function() {
  2958. if (toolButtonClick('#tool_rect')) {
  2959. svgCanvas.setMode('rect');
  2960. }
  2961. };
  2962. var clickFHRect = function() {
  2963. if (toolButtonClick('#tool_fhrect')) {
  2964. svgCanvas.setMode('fhrect');
  2965. }
  2966. };
  2967. var clickCircle = function() {
  2968. if (toolButtonClick('#tool_circle')) {
  2969. svgCanvas.setMode('circle');
  2970. }
  2971. };
  2972. var clickEllipse = function() {
  2973. if (toolButtonClick('#tool_ellipse')) {
  2974. svgCanvas.setMode('ellipse');
  2975. }
  2976. };
  2977. var clickFHEllipse = function() {
  2978. if (toolButtonClick('#tool_fhellipse')) {
  2979. svgCanvas.setMode('fhellipse');
  2980. }
  2981. };
  2982. var clickImage = function() {
  2983. if (toolButtonClick('#tool_image')) {
  2984. svgCanvas.setMode('image');
  2985. }
  2986. };
  2987. var clickZoom = function() {
  2988. if (toolButtonClick('#tool_zoom')) {
  2989. svgCanvas.setMode('zoom');
  2990. workarea.css('cursor', zoomInIcon);
  2991. }
  2992. };
  2993. var zoomImage = function(multiplier) {
  2994. var res = svgCanvas.getResolution();
  2995. multiplier = multiplier ? res.zoom * multiplier : 1;
  2996. // setResolution(res.w * multiplier, res.h * multiplier, true);
  2997. $('#zoom').val(multiplier * 100);
  2998. svgCanvas.setZoom(multiplier);
  2999. zoomDone();
  3000. updateCanvas(true);
  3001. };
  3002. var dblclickZoom = function() {
  3003. if (toolButtonClick('#tool_zoom')) {
  3004. zoomImage();
  3005. setSelectMode();
  3006. }
  3007. };
  3008. var clickText = function() {
  3009. if (toolButtonClick('#tool_text')) {
  3010. svgCanvas.setMode('text');
  3011. }
  3012. };
  3013. var clickPath = function() {
  3014. if (toolButtonClick('#tool_path')) {
  3015. svgCanvas.setMode('path');
  3016. }
  3017. };
  3018. // Delete is a contextual tool that only appears in the ribbon if
  3019. // an element has been selected
  3020. var deleteSelected = function() {
  3021. if (selectedElement != null || multiselected) {
  3022. svgCanvas.deleteSelectedElements();
  3023. }
  3024. };
  3025. var cutSelected = function() {
  3026. if (selectedElement != null || multiselected) {
  3027. svgCanvas.cutSelectedElements();
  3028. }
  3029. };
  3030. var copySelected = function() {
  3031. if (selectedElement != null || multiselected) {
  3032. svgCanvas.copySelectedElements();
  3033. }
  3034. };
  3035. var pasteInCenter = function() {
  3036. var zoom = svgCanvas.getZoom();
  3037. var x = (workarea[0].scrollLeft + workarea.width()/2)/zoom - svgCanvas.contentW;
  3038. var y = (workarea[0].scrollTop + workarea.height()/2)/zoom - svgCanvas.contentH;
  3039. svgCanvas.pasteElements('point', x, y);
  3040. };
  3041. var moveToTopSelected = function() {
  3042. if (selectedElement != null) {
  3043. svgCanvas.moveToTopSelectedElement();
  3044. }
  3045. };
  3046. var moveToBottomSelected = function() {
  3047. if (selectedElement != null) {
  3048. svgCanvas.moveToBottomSelectedElement();
  3049. }
  3050. };
  3051. var moveUpDownSelected = function(dir) {
  3052. if (selectedElement != null) {
  3053. svgCanvas.moveUpDownSelected(dir);
  3054. }
  3055. };
  3056. var convertToPath = function() {
  3057. if (selectedElement != null) {
  3058. svgCanvas.convertToPath();
  3059. }
  3060. };
  3061. var reorientPath = function() {
  3062. if (selectedElement != null) {
  3063. path.reorient();
  3064. }
  3065. };
  3066. var makeHyperlink = function() {
  3067. if (selectedElement != null || multiselected) {
  3068. $.prompt(uiStrings.notification.enterNewLinkURL, 'http://', function(url) {
  3069. if (url) {svgCanvas.makeHyperlink(url);}
  3070. });
  3071. }
  3072. };
  3073. var moveSelected = function(dx,dy) {
  3074. if (selectedElement != null || multiselected) {
  3075. if (curConfig.gridSnapping) {
  3076. // Use grid snap value regardless of zoom level
  3077. var multi = svgCanvas.getZoom() * curConfig.snappingStep;
  3078. dx *= multi;
  3079. dy *= multi;
  3080. }
  3081. svgCanvas.moveSelectedElements(dx,dy);
  3082. }
  3083. };
  3084. var linkControlPoints = function() {
  3085. $('#tool_node_link').toggleClass('push_button_pressed tool_button');
  3086. var linked = $('#tool_node_link').hasClass('push_button_pressed');
  3087. path.linkControlPoints(linked);
  3088. };
  3089. var clonePathNode = function() {
  3090. if (path.getNodePoint()) {
  3091. path.clonePathNode();
  3092. }
  3093. };
  3094. var deletePathNode = function() {
  3095. if (path.getNodePoint()) {
  3096. path.deletePathNode();
  3097. }
  3098. };
  3099. var addSubPath = function() {
  3100. var button = $('#tool_add_subpath');
  3101. var sp = !button.hasClass('push_button_pressed');
  3102. button.toggleClass('push_button_pressed tool_button');
  3103. path.addSubPath(sp);
  3104. };
  3105. var opencloseSubPath = function() {
  3106. path.opencloseSubPath();
  3107. };
  3108. var selectNext = function() {
  3109. svgCanvas.cycleElement(1);
  3110. };
  3111. var selectPrev = function() {
  3112. svgCanvas.cycleElement(0);
  3113. };
  3114. var rotateSelected = function(cw, step) {
  3115. if (selectedElement == null || multiselected) {return;}
  3116. if (!cw) {step *= -1;}
  3117. var angle = parseFloat($('#angle').val()) + step;
  3118. svgCanvas.setRotationAngle(angle);
  3119. updateContextPanel();
  3120. };
  3121. var clickClear = function() {
  3122. var dims = curConfig.dimensions;
  3123. $.confirm(uiStrings.notification.QwantToClear, function(ok) {
  3124. if (!ok) {return;}
  3125. setSelectMode();
  3126. svgCanvas.clear();
  3127. svgCanvas.setResolution(dims[0], dims[1]);
  3128. updateCanvas(true);
  3129. zoomImage();
  3130. populateLayers();
  3131. updateContextPanel();
  3132. prepPaints();
  3133. svgCanvas.runExtensions('onNewDocument');
  3134. });
  3135. };
  3136. var clickBold = function() {
  3137. svgCanvas.setBold( !svgCanvas.getBold() );
  3138. updateContextPanel();
  3139. return false;
  3140. };
  3141. var clickItalic = function() {
  3142. svgCanvas.setItalic( !svgCanvas.getItalic() );
  3143. updateContextPanel();
  3144. return false;
  3145. };
  3146. var clickSave = function() {
  3147. // In the future, more options can be provided here
  3148. var saveOpts = {
  3149. 'images': $.pref('img_save'),
  3150. 'round_digits': 6
  3151. };
  3152. svgCanvas.save(saveOpts);
  3153. };
  3154. var clickExport = function() {
  3155. $.select('Select an image type for export: ', [
  3156. // See http://kangax.github.io/jstests/toDataUrl_mime_type_test/ for a useful list of MIME types and browser support
  3157. // 'ICO', // Todo: Find a way to preserve transparency in SVG-Edit if not working presently and do full packaging for x-icon; then switch back to position after 'PNG'
  3158. 'PNG',
  3159. 'JPEG', 'BMP', 'WEBP'
  3160. ], function (imgType) { // todo: replace hard-coded msg with uiStrings.notification.
  3161. if (!imgType) {
  3162. return;
  3163. }
  3164. // Open placeholder window (prevents popup)
  3165. if (!customHandlers.exportImage && !customHandlers.pngsave) {
  3166. var str = uiStrings.notification.loadingImage;
  3167. exportWindow = window.open('data:text/html;charset=utf-8,<title>' + str + '<\/title><h1>' + str + '<\/h1>');
  3168. }
  3169. var quality = parseInt($('#image-slider').val(), 10)/100;
  3170. if (window.canvg) {
  3171. svgCanvas.rasterExport(imgType, quality);
  3172. } else {
  3173. $.getScript('canvg/rgbcolor.js', function() {
  3174. $.getScript('canvg/canvg.js', function() {
  3175. svgCanvas.rasterExport(imgType, quality);
  3176. });
  3177. });
  3178. }
  3179. }, function () {
  3180. var sel = $(this);
  3181. if (sel.val() === 'JPEG' || sel.val() === 'WEBP') {
  3182. if (!$('#image-slider').length) {
  3183. $('<div><label>Quality: <input id="image-slider" type="range" min="1" max="100" value="92" /></label></div>').appendTo(sel.parent()); // Todo: i18n-ize label
  3184. }
  3185. }
  3186. else {
  3187. $('#image-slider').parent().remove();
  3188. }
  3189. });
  3190. };
  3191. // by default, svgCanvas.open() is a no-op.
  3192. // it is up to an extension mechanism (opera widget, etc)
  3193. // to call setCustomHandlers() which will make it do something
  3194. var clickOpen = function() {
  3195. svgCanvas.open();
  3196. };
  3197. var clickImport = function() {
  3198. };
  3199. var clickUndo = function() {
  3200. if (undoMgr.getUndoStackSize() > 0) {
  3201. undoMgr.undo();
  3202. populateLayers();
  3203. }
  3204. };
  3205. var clickRedo = function() {
  3206. if (undoMgr.getRedoStackSize() > 0) {
  3207. undoMgr.redo();
  3208. populateLayers();
  3209. }
  3210. };
  3211. var clickGroup = function() {
  3212. // group
  3213. if (multiselected) {
  3214. svgCanvas.groupSelectedElements();
  3215. }
  3216. // ungroup
  3217. else if (selectedElement) {
  3218. svgCanvas.ungroupSelectedElement();
  3219. }
  3220. };
  3221. var clickClone = function() {
  3222. svgCanvas.cloneSelectedElements(20, 20);
  3223. };
  3224. var clickAlign = function() {
  3225. var letter = this.id.replace('tool_align', '').charAt(0);
  3226. svgCanvas.alignSelectedElements(letter, $('#align_relative_to').val());
  3227. };
  3228. var clickWireframe = function() {
  3229. $('#tool_wireframe').toggleClass('push_button_pressed tool_button');
  3230. workarea.toggleClass('wireframe');
  3231. if (supportsNonSS) {return;}
  3232. var wf_rules = $('#wireframe_rules');
  3233. if (!wf_rules.length) {
  3234. wf_rules = $('<style id="wireframe_rules"><\/style>').appendTo('head');
  3235. } else {
  3236. wf_rules.empty();
  3237. }
  3238. updateWireFrame();
  3239. };
  3240. $('#svg_docprops_container, #svg_prefs_container').draggable({cancel: 'button,fieldset', containment: 'window'});
  3241. var showDocProperties = function() {
  3242. if (docprops) {return;}
  3243. docprops = true;
  3244. // This selects the correct radio button by using the array notation
  3245. $('#image_save_opts input').val([$.pref('img_save')]);
  3246. // update resolution option with actual resolution
  3247. var res = svgCanvas.getResolution();
  3248. if (curConfig.baseUnit !== 'px') {
  3249. res.w = svgedit.units.convertUnit(res.w) + curConfig.baseUnit;
  3250. res.h = svgedit.units.convertUnit(res.h) + curConfig.baseUnit;
  3251. }
  3252. $('#canvas_width').val(res.w);
  3253. $('#canvas_height').val(res.h);
  3254. $('#canvas_title').val(svgCanvas.getDocumentTitle());
  3255. $('#svg_docprops').show();
  3256. };
  3257. var showPreferences = function() {
  3258. if (preferences) {return;}
  3259. preferences = true;
  3260. $('#main_menu').hide();
  3261. // Update background color with current one
  3262. var blocks = $('#bg_blocks div');
  3263. var cur_bg = 'cur_background';
  3264. var canvas_bg = curPrefs.bkgd_color;
  3265. var url = $.pref('bkgd_url');
  3266. blocks.each(function() {
  3267. var blk = $(this);
  3268. var is_bg = blk.css('background-color') == canvas_bg;
  3269. blk.toggleClass(cur_bg, is_bg);
  3270. if (is_bg) {$('#canvas_bg_url').removeClass(cur_bg);}
  3271. });
  3272. if (!canvas_bg) {blocks.eq(0).addClass(cur_bg);}
  3273. if (url) {
  3274. $('#canvas_bg_url').val(url);
  3275. }
  3276. $('#grid_snapping_on').prop('checked', curConfig.gridSnapping);
  3277. $('#grid_snapping_step').attr('value', curConfig.snappingStep);
  3278. $('#grid_color').attr('value', curConfig.gridColor);
  3279. $('#svg_prefs').show();
  3280. };
  3281. var hideSourceEditor = function() {
  3282. $('#svg_source_editor').hide();
  3283. editingsource = false;
  3284. $('#svg_source_textarea').blur();
  3285. };
  3286. var saveSourceEditor = function() {
  3287. if (!editingsource) {return;}
  3288. var saveChanges = function() {
  3289. svgCanvas.clearSelection();
  3290. hideSourceEditor();
  3291. zoomImage();
  3292. populateLayers();
  3293. updateTitle();
  3294. prepPaints();
  3295. };
  3296. if (!svgCanvas.setSvgString($('#svg_source_textarea').val())) {
  3297. $.confirm(uiStrings.notification.QerrorsRevertToSource, function(ok) {
  3298. if (!ok) {return false;}
  3299. saveChanges();
  3300. });
  3301. } else {
  3302. saveChanges();
  3303. }
  3304. setSelectMode();
  3305. };
  3306. var hideDocProperties = function() {
  3307. $('#svg_docprops').hide();
  3308. $('#canvas_width,#canvas_height').removeAttr('disabled');
  3309. $('#resolution')[0].selectedIndex = 0;
  3310. $('#image_save_opts input').val([$.pref('img_save')]);
  3311. docprops = false;
  3312. };
  3313. var hidePreferences = function() {
  3314. $('#svg_prefs').hide();
  3315. preferences = false;
  3316. };
  3317. var saveDocProperties = function() {
  3318. // set title
  3319. var newTitle = $('#canvas_title').val();
  3320. updateTitle(newTitle);
  3321. svgCanvas.setDocumentTitle(newTitle);
  3322. // update resolution
  3323. var width = $('#canvas_width'), w = width.val();
  3324. var height = $('#canvas_height'), h = height.val();
  3325. if (w != 'fit' && !svgedit.units.isValidUnit('width', w)) {
  3326. $.alert(uiStrings.notification.invalidAttrValGiven);
  3327. width.parent().addClass('error');
  3328. return false;
  3329. }
  3330. width.parent().removeClass('error');
  3331. if (h != 'fit' && !svgedit.units.isValidUnit('height', h)) {
  3332. $.alert(uiStrings.notification.invalidAttrValGiven);
  3333. height.parent().addClass('error');
  3334. return false;
  3335. }
  3336. height.parent().removeClass('error');
  3337. if (!svgCanvas.setResolution(w, h)) {
  3338. $.alert(uiStrings.notification.noContentToFitTo);
  3339. return false;
  3340. }
  3341. // Set image save option
  3342. $.pref('img_save', $('#image_save_opts :checked').val());
  3343. updateCanvas();
  3344. hideDocProperties();
  3345. };
  3346. var savePreferences = editor.savePreferences = function() {
  3347. // Set background
  3348. var color = $('#bg_blocks div.cur_background').css('background-color') || '#FFF';
  3349. setBackground(color, $('#canvas_bg_url').val());
  3350. // set language
  3351. var lang = $('#lang_select').val();
  3352. if (lang !== $.pref('lang')) {
  3353. editor.putLocale(lang, good_langs);
  3354. }
  3355. // set icon size
  3356. setIconSize($('#iconsize').val());
  3357. // set grid setting
  3358. curConfig.gridSnapping = $('#grid_snapping_on')[0].checked;
  3359. curConfig.snappingStep = $('#grid_snapping_step').val();
  3360. curConfig.gridColor = $('#grid_color').val();
  3361. curConfig.showRulers = $('#show_rulers')[0].checked;
  3362. $('#rulers').toggle(curConfig.showRulers);
  3363. if (curConfig.showRulers) {updateRulers();}
  3364. curConfig.baseUnit = $('#base_unit').val();
  3365. svgCanvas.setConfig(curConfig);
  3366. updateCanvas();
  3367. hidePreferences();
  3368. };
  3369. var resetScrollPos = $.noop;
  3370. var cancelOverlays = function() {
  3371. $('#dialog_box').hide();
  3372. if (!editingsource && !docprops && !preferences) {
  3373. if (cur_context) {
  3374. svgCanvas.leaveContext();
  3375. }
  3376. return;
  3377. }
  3378. if (editingsource) {
  3379. if (origSource !== $('#svg_source_textarea').val()) {
  3380. $.confirm(uiStrings.notification.QignoreSourceChanges, function(ok) {
  3381. if (ok) {hideSourceEditor();}
  3382. });
  3383. } else {
  3384. hideSourceEditor();
  3385. }
  3386. } else if (docprops) {
  3387. hideDocProperties();
  3388. } else if (preferences) {
  3389. hidePreferences();
  3390. }
  3391. resetScrollPos();
  3392. };
  3393. var win_wh = {width:$(window).width(), height:$(window).height()};
  3394. // Fix for Issue 781: Drawing area jumps to top-left corner on window resize (IE9)
  3395. if (svgedit.browser.isIE()) {
  3396. (function() {
  3397. resetScrollPos = function() {
  3398. if (workarea[0].scrollLeft === 0 && workarea[0].scrollTop === 0) {
  3399. workarea[0].scrollLeft = curScrollPos.left;
  3400. workarea[0].scrollTop = curScrollPos.top;
  3401. }
  3402. };
  3403. curScrollPos = {
  3404. left: workarea[0].scrollLeft,
  3405. top: workarea[0].scrollTop
  3406. };
  3407. $(window).resize(resetScrollPos);
  3408. svgEditor.ready(function() {
  3409. // TODO: Find better way to detect when to do this to minimize
  3410. // flickering effect
  3411. setTimeout(function() {
  3412. resetScrollPos();
  3413. }, 500);
  3414. });
  3415. workarea.scroll(function() {
  3416. curScrollPos = {
  3417. left: workarea[0].scrollLeft,
  3418. top: workarea[0].scrollTop
  3419. };
  3420. });
  3421. }());
  3422. }
  3423. $(window).resize(function(evt) {
  3424. $.each(win_wh, function(type, val) {
  3425. var curval = $(window)[type]();
  3426. workarea[0]['scroll' + (type === 'width' ? 'Left' : 'Top')] -= (curval - val)/2;
  3427. win_wh[type] = curval;
  3428. });
  3429. setFlyoutPositions();
  3430. });
  3431. (function() {
  3432. workarea.scroll(function() {
  3433. // TODO: jQuery's scrollLeft/Top() wouldn't require a null check
  3434. if ($('#ruler_x').length != 0) {
  3435. $('#ruler_x')[0].scrollLeft = workarea[0].scrollLeft;
  3436. }
  3437. if ($('#ruler_y').length != 0) {
  3438. $('#ruler_y')[0].scrollTop = workarea[0].scrollTop;
  3439. }
  3440. });
  3441. }());
  3442. $('#url_notice').click(function() {
  3443. $.alert(this.title);
  3444. });
  3445. $('#change_image_url').click(promptImgURL);
  3446. // added these event handlers for all the push buttons so they
  3447. // behave more like buttons being pressed-in and not images
  3448. (function() {
  3449. var toolnames = ['clear', 'open', 'save', 'source', 'delete', 'delete_multi', 'paste', 'clone', 'clone_multi', 'move_top', 'move_bottom'];
  3450. var all_tools = '';
  3451. var cur_class = 'tool_button_current';
  3452. $.each(toolnames, function(i,item) {
  3453. all_tools += '#tool_' + item + (i == toolnames.length-1 ? ',' : '');
  3454. });
  3455. $(all_tools).mousedown(function() {
  3456. $(this).addClass(cur_class);
  3457. }).bind('mousedown mouseout', function() {
  3458. $(this).removeClass(cur_class);
  3459. });
  3460. $('#tool_undo, #tool_redo').mousedown(function() {
  3461. if (!$(this).hasClass('disabled')) {$(this).addClass(cur_class);}
  3462. }).bind('mousedown mouseout',function() {
  3463. $(this).removeClass(cur_class);}
  3464. );
  3465. }());
  3466. // switch modifier key in tooltips if mac
  3467. // NOTE: This code is not used yet until I can figure out how to successfully bind ctrl/meta
  3468. // in Opera and Chrome
  3469. if (svgedit.browser.isMac() && !window.opera) {
  3470. var shortcutButtons = ['tool_clear', 'tool_save', 'tool_source', 'tool_undo', 'tool_redo', 'tool_clone'];
  3471. i = shortcutButtons.length;
  3472. while (i--) {
  3473. var button = document.getElementById(shortcutButtons[i]);
  3474. if (button) {
  3475. var title = button.title;
  3476. var index = title.indexOf('Ctrl+');
  3477. button.title = [title.substr(0, index), 'Cmd+', title.substr(index + 5)].join('');
  3478. }
  3479. }
  3480. }
  3481. // TODO: go back to the color boxes having white background-color and then setting
  3482. // background-image to none.png (otherwise partially transparent gradients look weird)
  3483. var colorPicker = function(elem) {
  3484. var picker = elem.attr('id') == 'stroke_color' ? 'stroke' : 'fill';
  3485. // var opacity = (picker == 'stroke' ? $('#stroke_opacity') : $('#fill_opacity'));
  3486. var paint = paintBox[picker].paint;
  3487. var title = (picker == 'stroke' ? 'Pick a Stroke Paint and Opacity' : 'Pick a Fill Paint and Opacity');
  3488. // var was_none = false; // Currently unused
  3489. var pos = elem.offset();
  3490. $('#color_picker')
  3491. .draggable({cancel: '.jGraduate_tabs, .jGraduate_colPick, .jGraduate_gradPick, .jPicker', containment: 'window'})
  3492. .css(curConfig.colorPickerCSS || {'left': pos.left-140, 'bottom': 40})
  3493. .jGraduate(
  3494. {
  3495. paint: paint,
  3496. window: { pickerTitle: title },
  3497. images: { clientPath: curConfig.jGraduatePath },
  3498. newstop: 'inverse'
  3499. },
  3500. function(p) {
  3501. paint = new $.jGraduate.Paint(p);
  3502. paintBox[picker].setPaint(paint);
  3503. svgCanvas.setPaint(picker, paint);
  3504. $('#color_picker').hide();
  3505. },
  3506. function() {
  3507. $('#color_picker').hide();
  3508. });
  3509. };
  3510. var PaintBox = function(container, type) {
  3511. var paintColor, paintOpacity,
  3512. cur = curConfig[type === 'fill' ? 'initFill' : 'initStroke'];
  3513. // set up gradients to be used for the buttons
  3514. var svgdocbox = new DOMParser().parseFromString(
  3515. '<svg xmlns="http://www.w3.org/2000/svg"><rect width="16.5" height="16.5"'+
  3516. ' fill="#' + cur.color + '" opacity="' + cur.opacity + '"/>'+
  3517. ' <defs><linearGradient id="gradbox_"/></defs></svg>', 'text/xml');
  3518. var docElem = svgdocbox.documentElement;
  3519. docElem = $(container)[0].appendChild(document.importNode(docElem, true));
  3520. docElem.setAttribute('width',16.5);
  3521. this.rect = docElem.firstChild;
  3522. this.defs = docElem.getElementsByTagName('defs')[0];
  3523. this.grad = this.defs.firstChild;
  3524. this.paint = new $.jGraduate.Paint({solidColor: cur.color});
  3525. this.type = type;
  3526. this.setPaint = function(paint, apply) {
  3527. this.paint = paint;
  3528. var fillAttr = 'none';
  3529. var ptype = paint.type;
  3530. var opac = paint.alpha / 100;
  3531. switch ( ptype ) {
  3532. case 'solidColor':
  3533. fillAttr = (paint[ptype] != 'none') ? '#' + paint[ptype] : paint[ptype];
  3534. break;
  3535. case 'linearGradient':
  3536. case 'radialGradient':
  3537. this.defs.removeChild(this.grad);
  3538. this.grad = this.defs.appendChild(paint[ptype]);
  3539. var id = this.grad.id = 'gradbox_' + this.type;
  3540. fillAttr = 'url(#' + id + ')';
  3541. break;
  3542. }
  3543. this.rect.setAttribute('fill', fillAttr);
  3544. this.rect.setAttribute('opacity', opac);
  3545. if (apply) {
  3546. svgCanvas.setColor(this.type, paintColor, true);
  3547. svgCanvas.setPaintOpacity(this.type, paintOpacity, true);
  3548. }
  3549. };
  3550. this.update = function(apply) {
  3551. if (!selectedElement) {return;}
  3552. var i, len;
  3553. var type = this.type;
  3554. switch (selectedElement.tagName) {
  3555. case 'use':
  3556. case 'image':
  3557. case 'foreignObject':
  3558. // These elements don't have fill or stroke, so don't change
  3559. // the current value
  3560. return;
  3561. case 'g':
  3562. case 'a':
  3563. var gPaint = null;
  3564. var childs = selectedElement.getElementsByTagName('*');
  3565. for (i = 0, len = childs.length; i < len; i++) {
  3566. var elem = childs[i];
  3567. var p = elem.getAttribute(type);
  3568. if (i === 0) {
  3569. gPaint = p;
  3570. } else if (gPaint !== p) {
  3571. gPaint = null;
  3572. break;
  3573. }
  3574. }
  3575. if (gPaint === null) {
  3576. // No common color, don't update anything
  3577. paintColor = null;
  3578. return;
  3579. }
  3580. paintColor = gPaint;
  3581. paintOpacity = 1;
  3582. break;
  3583. default:
  3584. paintOpacity = parseFloat(selectedElement.getAttribute(type + '-opacity'));
  3585. if (isNaN(paintOpacity)) {
  3586. paintOpacity = 1.0;
  3587. }
  3588. var defColor = type === 'fill' ? 'black' : 'none';
  3589. paintColor = selectedElement.getAttribute(type) || defColor;
  3590. }
  3591. if (apply) {
  3592. svgCanvas.setColor(type, paintColor, true);
  3593. svgCanvas.setPaintOpacity(type, paintOpacity, true);
  3594. }
  3595. paintOpacity *= 100;
  3596. var paint = getPaint(paintColor, paintOpacity, type);
  3597. // update the rect inside #fill_color/#stroke_color
  3598. this.setPaint(paint);
  3599. };
  3600. this.prep = function() {
  3601. var ptype = this.paint.type;
  3602. switch ( ptype ) {
  3603. case 'linearGradient':
  3604. case 'radialGradient':
  3605. var paint = new $.jGraduate.Paint({copy: this.paint});
  3606. svgCanvas.setPaint(type, paint);
  3607. break;
  3608. }
  3609. };
  3610. };
  3611. paintBox.fill = new PaintBox('#fill_color', 'fill');
  3612. paintBox.stroke = new PaintBox('#stroke_color', 'stroke');
  3613. $('#stroke_width').val(curConfig.initStroke.width);
  3614. $('#group_opacity').val(curConfig.initOpacity * 100);
  3615. // Use this SVG elem to test vectorEffect support
  3616. var testEl = paintBox.fill.rect.cloneNode(false);
  3617. testEl.setAttribute('style', 'vector-effect:non-scaling-stroke');
  3618. supportsNonSS = (testEl.style.vectorEffect === 'non-scaling-stroke');
  3619. testEl.removeAttribute('style');
  3620. var svgdocbox = paintBox.fill.rect.ownerDocument;
  3621. // Use this to test support for blur element. Seems to work to test support in Webkit
  3622. var blurTest = svgdocbox.createElementNS(svgedit.NS.SVG, 'feGaussianBlur');
  3623. if (blurTest.stdDeviationX === undefined) {
  3624. $('#tool_blur').hide();
  3625. }
  3626. $(blurTest).remove();
  3627. // Test for zoom icon support
  3628. (function() {
  3629. var pre = '-' + uaPrefix.toLowerCase() + '-zoom-';
  3630. var zoom = pre + 'in';
  3631. workarea.css('cursor', zoom);
  3632. if (workarea.css('cursor') === zoom) {
  3633. zoomInIcon = zoom;
  3634. zoomOutIcon = pre + 'out';
  3635. }
  3636. workarea.css('cursor', 'auto');
  3637. }());
  3638. // Test for embedImage support (use timeout to not interfere with page load)
  3639. setTimeout(function() {
  3640. svgCanvas.embedImage('images/logo.png', function(datauri) {
  3641. if (!datauri) {
  3642. // Disable option
  3643. $('#image_save_opts [value=embed]').attr('disabled', 'disabled');
  3644. $('#image_save_opts input').val(['ref']);
  3645. $.pref('img_save', 'ref');
  3646. $('#image_opt_embed').css('color', '#666').attr('title', uiStrings.notification.featNotSupported);
  3647. }
  3648. });
  3649. }, 1000);
  3650. $('#fill_color, #tool_fill .icon_label').click(function() {
  3651. colorPicker($('#fill_color'));
  3652. updateToolButtonState();
  3653. });
  3654. $('#stroke_color, #tool_stroke .icon_label').click(function() {
  3655. colorPicker($('#stroke_color'));
  3656. updateToolButtonState();
  3657. });
  3658. $('#group_opacityLabel').click(function() {
  3659. $('#opacity_dropdown button').mousedown();
  3660. $(window).mouseup();
  3661. });
  3662. $('#zoomLabel').click(function() {
  3663. $('#zoom_dropdown button').mousedown();
  3664. $(window).mouseup();
  3665. });
  3666. $('#tool_move_top').mousedown(function(evt) {
  3667. $('#tools_stacking').show();
  3668. evt.preventDefault();
  3669. });
  3670. $('.layer_button').mousedown(function() {
  3671. $(this).addClass('layer_buttonpressed');
  3672. }).mouseout(function() {
  3673. $(this).removeClass('layer_buttonpressed');
  3674. }).mouseup(function() {
  3675. $(this).removeClass('layer_buttonpressed');
  3676. });
  3677. $('.push_button').mousedown(function() {
  3678. if (!$(this).hasClass('disabled')) {
  3679. $(this).addClass('push_button_pressed').removeClass('push_button');
  3680. }
  3681. }).mouseout(function() {
  3682. $(this).removeClass('push_button_pressed').addClass('push_button');
  3683. }).mouseup(function() {
  3684. $(this).removeClass('push_button_pressed').addClass('push_button');
  3685. });
  3686. // ask for a layer name
  3687. $('#layer_new').click(function() {
  3688. var uniqName,
  3689. i = svgCanvas.getCurrentDrawing().getNumLayers();
  3690. do {
  3691. uniqName = uiStrings.layers.layer + ' ' + (++i);
  3692. } while(svgCanvas.getCurrentDrawing().hasLayer(uniqName));
  3693. $.prompt(uiStrings.notification.enterUniqueLayerName, uniqName, function(newName) {
  3694. if (!newName) {return;}
  3695. if (svgCanvas.getCurrentDrawing().hasLayer(newName)) {
  3696. $.alert(uiStrings.notification.dupeLayerName);
  3697. return;
  3698. }
  3699. svgCanvas.createLayer(newName);
  3700. updateContextPanel();
  3701. populateLayers();
  3702. });
  3703. });
  3704. function deleteLayer() {
  3705. if (svgCanvas.deleteCurrentLayer()) {
  3706. updateContextPanel();
  3707. populateLayers();
  3708. // This matches what SvgCanvas does
  3709. // TODO: make this behavior less brittle (svg-editor should get which
  3710. // layer is selected from the canvas and then select that one in the UI)
  3711. $('#layerlist tr.layer').removeClass('layersel');
  3712. $('#layerlist tr.layer:first').addClass('layersel');
  3713. }
  3714. }
  3715. function cloneLayer() {
  3716. var name = svgCanvas.getCurrentDrawing().getCurrentLayerName() + ' copy';
  3717. $.prompt(uiStrings.notification.enterUniqueLayerName, name, function(newName) {
  3718. if (!newName) {return;}
  3719. if (svgCanvas.getCurrentDrawing().hasLayer(newName)) {
  3720. $.alert(uiStrings.notification.dupeLayerName);
  3721. return;
  3722. }
  3723. svgCanvas.cloneLayer(newName);
  3724. updateContextPanel();
  3725. populateLayers();
  3726. });
  3727. }
  3728. function mergeLayer() {
  3729. if ($('#layerlist tr.layersel').index() == svgCanvas.getCurrentDrawing().getNumLayers()-1) {
  3730. return;
  3731. }
  3732. svgCanvas.mergeLayer();
  3733. updateContextPanel();
  3734. populateLayers();
  3735. }
  3736. function moveLayer(pos) {
  3737. var curIndex = $('#layerlist tr.layersel').index();
  3738. var total = svgCanvas.getCurrentDrawing().getNumLayers();
  3739. if (curIndex > 0 || curIndex < total-1) {
  3740. curIndex += pos;
  3741. svgCanvas.setCurrentLayerPosition(total-curIndex-1);
  3742. populateLayers();
  3743. }
  3744. }
  3745. $('#layer_delete').click(deleteLayer);
  3746. $('#layer_up').click(function() {
  3747. moveLayer(-1);
  3748. });
  3749. $('#layer_down').click(function() {
  3750. moveLayer(1);
  3751. });
  3752. $('#layer_rename').click(function() {
  3753. // var curIndex = $('#layerlist tr.layersel').prevAll().length; // Currently unused
  3754. var oldName = $('#layerlist tr.layersel td.layername').text();
  3755. $.prompt(uiStrings.notification.enterNewLayerName, '', function(newName) {
  3756. if (!newName) {return;}
  3757. if (oldName == newName || svgCanvas.getCurrentDrawing().hasLayer(newName)) {
  3758. $.alert(uiStrings.notification.layerHasThatName);
  3759. return;
  3760. }
  3761. svgCanvas.renameCurrentLayer(newName);
  3762. populateLayers();
  3763. });
  3764. });
  3765. var SIDEPANEL_MAXWIDTH = 300;
  3766. var SIDEPANEL_OPENWIDTH = 150;
  3767. var sidedrag = -1, sidedragging = false, allowmove = false;
  3768. var changeSidePanelWidth = function(delta) {
  3769. var rulerX = $('#ruler_x');
  3770. $('#sidepanels').width('+=' + delta);
  3771. $('#layerpanel').width('+=' + delta);
  3772. rulerX.css('right', parseInt(rulerX.css('right'), 10) + delta);
  3773. workarea.css('right', parseInt(workarea.css('right'), 10) + delta);
  3774. svgCanvas.runExtensions("workareaResized");
  3775. };
  3776. var resizeSidePanel = function(evt) {
  3777. if (!allowmove) {return;}
  3778. if (sidedrag == -1) {return;}
  3779. sidedragging = true;
  3780. var deltaX = sidedrag - evt.pageX;
  3781. var sideWidth = $('#sidepanels').width();
  3782. if (sideWidth + deltaX > SIDEPANEL_MAXWIDTH) {
  3783. deltaX = SIDEPANEL_MAXWIDTH - sideWidth;
  3784. sideWidth = SIDEPANEL_MAXWIDTH;
  3785. } else if (sideWidth + deltaX < 2) {
  3786. deltaX = 2 - sideWidth;
  3787. sideWidth = 2;
  3788. }
  3789. if (deltaX == 0) {return;}
  3790. sidedrag -= deltaX;
  3791. changeSidePanelWidth(deltaX);
  3792. };
  3793. // if width is non-zero, then fully close it, otherwise fully open it
  3794. // the optional close argument forces the side panel closed
  3795. var toggleSidePanel = function(close) {
  3796. var w = $('#sidepanels').width();
  3797. var deltaX = (w > 2 || close ? 2 : SIDEPANEL_OPENWIDTH) - w;
  3798. changeSidePanelWidth(deltaX);
  3799. };
  3800. $('#sidepanel_handle')
  3801. .mousedown(function(evt) {
  3802. sidedrag = evt.pageX;
  3803. $(window).mousemove(resizeSidePanel);
  3804. allowmove = false;
  3805. // Silly hack for Chrome, which always runs mousemove right after mousedown
  3806. setTimeout(function() {
  3807. allowmove = true;
  3808. }, 20);
  3809. })
  3810. .mouseup(function(evt) {
  3811. if (!sidedragging) {toggleSidePanel();}
  3812. sidedrag = -1;
  3813. sidedragging = false;
  3814. });
  3815. $(window).mouseup(function() {
  3816. sidedrag = -1;
  3817. sidedragging = false;
  3818. $('#svg_editor').unbind('mousemove', resizeSidePanel);
  3819. });
  3820. populateLayers();
  3821. // function changeResolution(x,y) {
  3822. // var zoom = svgCanvas.getResolution().zoom;
  3823. // setResolution(x * zoom, y * zoom);
  3824. // }
  3825. var centerCanvas = function() {
  3826. // this centers the canvas vertically in the workarea (horizontal handled in CSS)
  3827. workarea.css('line-height', workarea.height() + 'px');
  3828. };
  3829. $(window).bind('load resize', centerCanvas);
  3830. function stepFontSize(elem, step) {
  3831. var orig_val = Number(elem.value);
  3832. var sug_val = orig_val + step;
  3833. var increasing = sug_val >= orig_val;
  3834. if (step === 0) {return orig_val;}
  3835. if (orig_val >= 24) {
  3836. if (increasing) {
  3837. return Math.round(orig_val * 1.1);
  3838. }
  3839. return Math.round(orig_val / 1.1);
  3840. }
  3841. if (orig_val <= 1) {
  3842. if (increasing) {
  3843. return orig_val * 2;
  3844. }
  3845. return orig_val / 2;
  3846. }
  3847. return sug_val;
  3848. }
  3849. function stepZoom(elem, step) {
  3850. var orig_val = Number(elem.value);
  3851. if (orig_val === 0) {return 100;}
  3852. var sug_val = orig_val + step;
  3853. if (step === 0) {return orig_val;}
  3854. if (orig_val >= 100) {
  3855. return sug_val;
  3856. }
  3857. if (sug_val >= orig_val) {
  3858. return orig_val * 2;
  3859. }
  3860. return orig_val / 2;
  3861. }
  3862. // function setResolution(w, h, center) {
  3863. // updateCanvas();
  3864. // // w-=0; h-=0;
  3865. // // $('#svgcanvas').css( { 'width': w, 'height': h } );
  3866. // // $('#canvas_width').val(w);
  3867. // // $('#canvas_height').val(h);
  3868. // //
  3869. // // if (center) {
  3870. // // var w_area = workarea;
  3871. // // var scroll_y = h/2 - w_area.height()/2;
  3872. // // var scroll_x = w/2 - w_area.width()/2;
  3873. // // w_area[0].scrollTop = scroll_y;
  3874. // // w_area[0].scrollLeft = scroll_x;
  3875. // // }
  3876. // }
  3877. $('#resolution').change(function() {
  3878. var wh = $('#canvas_width,#canvas_height');
  3879. if (!this.selectedIndex) {
  3880. if ($('#canvas_width').val() == 'fit') {
  3881. wh.removeAttr('disabled').val(100);
  3882. }
  3883. } else if (this.value == 'content') {
  3884. wh.val('fit').attr('disabled', 'disabled');
  3885. } else {
  3886. var dims = this.value.split('x');
  3887. $('#canvas_width').val(dims[0]);
  3888. $('#canvas_height').val(dims[1]);
  3889. wh.removeAttr('disabled');
  3890. }
  3891. });
  3892. //Prevent browser from erroneously repopulating fields
  3893. $('input,select').attr('autocomplete', 'off');
  3894. // Associate all button actions as well as non-button keyboard shortcuts
  3895. Actions = (function() {
  3896. // sel:'selector', fn:function, evt:'event', key:[key, preventDefault, NoDisableInInput]
  3897. var tool_buttons = [
  3898. {sel: '#tool_select', fn: clickSelect, evt: 'click', key: ['V', true]},
  3899. {sel: '#tool_fhpath', fn: clickFHPath, evt: 'click', key: ['Q', true]},
  3900. {sel: '#tool_line', fn: clickLine, evt: 'click', key: ['L', true]},
  3901. {sel: '#tool_rect', fn: clickRect, evt: 'mouseup', key: ['R', true], parent: '#tools_rect', icon: 'rect'},
  3902. {sel: '#tool_square', fn: clickSquare, evt: 'mouseup', parent: '#tools_rect', icon: 'square'},
  3903. {sel: '#tool_fhrect', fn: clickFHRect, evt: 'mouseup', parent: '#tools_rect', icon: 'fh_rect'},
  3904. {sel: '#tool_ellipse', fn: clickEllipse, evt: 'mouseup', key: ['E', true], parent: '#tools_ellipse', icon: 'ellipse'},
  3905. {sel: '#tool_circle', fn: clickCircle, evt: 'mouseup', parent: '#tools_ellipse', icon: 'circle'},
  3906. {sel: '#tool_fhellipse', fn: clickFHEllipse, evt: 'mouseup', parent: '#tools_ellipse', icon: 'fh_ellipse'},
  3907. {sel: '#tool_path', fn: clickPath, evt: 'click', key: ['P', true]},
  3908. {sel: '#tool_text', fn: clickText, evt: 'click', key: ['T', true]},
  3909. {sel: '#tool_image', fn: clickImage, evt: 'mouseup'},
  3910. {sel: '#tool_zoom', fn: clickZoom, evt: 'mouseup', key: ['Z', true]},
  3911. {sel: '#tool_clear', fn: clickClear, evt: 'mouseup', key: ['N', true]},
  3912. {sel: '#tool_save', fn: function() {
  3913. if (editingsource) {
  3914. saveSourceEditor();
  3915. }
  3916. else {
  3917. clickSave();
  3918. }
  3919. }, evt: 'mouseup', key: ['S', true]},
  3920. {sel: '#tool_export', fn: clickExport, evt: 'mouseup'},
  3921. {sel: '#tool_open', fn: clickOpen, evt: 'mouseup', key: ['O', true]},
  3922. {sel: '#tool_import', fn: clickImport, evt: 'mouseup'},
  3923. {sel: '#tool_source', fn: showSourceEditor, evt: 'click', key: ['U', true]},
  3924. {sel: '#tool_wireframe', fn: clickWireframe, evt: 'click', key: ['F', true]},
  3925. {sel: '#tool_source_cancel,.overlay,#tool_docprops_cancel,#tool_prefs_cancel', fn: cancelOverlays, evt: 'click', key: ['esc', false, false], hidekey: true},
  3926. {sel: '#tool_source_save', fn: saveSourceEditor, evt: 'click'},
  3927. {sel: '#tool_docprops_save', fn: saveDocProperties, evt: 'click'},
  3928. {sel: '#tool_docprops', fn: showDocProperties, evt: 'mouseup'},
  3929. {sel: '#tool_prefs_save', fn: savePreferences, evt: 'click'},
  3930. {sel: '#tool_prefs_option', fn: function() {showPreferences(); return false;}, evt: 'mouseup'},
  3931. {sel: '#tool_delete,#tool_delete_multi', fn: deleteSelected, evt: 'click', key: ['del/backspace', true]},
  3932. {sel: '#tool_reorient', fn: reorientPath, evt: 'click'},
  3933. {sel: '#tool_node_link', fn: linkControlPoints, evt: 'click'},
  3934. {sel: '#tool_node_clone', fn: clonePathNode, evt: 'click'},
  3935. {sel: '#tool_node_delete', fn: deletePathNode, evt: 'click'},
  3936. {sel: '#tool_openclose_path', fn: opencloseSubPath, evt: 'click'},
  3937. {sel: '#tool_add_subpath', fn: addSubPath, evt: 'click'},
  3938. {sel: '#tool_move_top', fn: moveToTopSelected, evt: 'click', key: 'ctrl+shift+]'},
  3939. {sel: '#tool_move_bottom', fn: moveToBottomSelected, evt: 'click', key: 'ctrl+shift+['},
  3940. {sel: '#tool_topath', fn: convertToPath, evt: 'click'},
  3941. {sel: '#tool_make_link,#tool_make_link_multi', fn: makeHyperlink, evt: 'click'},
  3942. {sel: '#tool_undo', fn: clickUndo, evt: 'click', key: ['Z', true]},
  3943. {sel: '#tool_redo', fn: clickRedo, evt: 'click', key: ['Y', true]},
  3944. {sel: '#tool_clone,#tool_clone_multi', fn: clickClone, evt: 'click', key: ['D', true]},
  3945. {sel: '#tool_group_elements', fn: clickGroup, evt: 'click', key: ['G', true]},
  3946. {sel: '#tool_ungroup', fn: clickGroup, evt: 'click'},
  3947. {sel: '#tool_unlink_use', fn: clickGroup, evt: 'click'},
  3948. {sel: '[id^=tool_align]', fn: clickAlign, evt: 'click'},
  3949. // these two lines are required to make Opera work properly with the flyout mechanism
  3950. // {sel: '#tools_rect_show', fn: clickRect, evt: 'click'},
  3951. // {sel: '#tools_ellipse_show', fn: clickEllipse, evt: 'click'},
  3952. {sel: '#tool_bold', fn: clickBold, evt: 'mousedown'},
  3953. {sel: '#tool_italic', fn: clickItalic, evt: 'mousedown'},
  3954. {sel: '#sidepanel_handle', fn: toggleSidePanel, key: ['X']},
  3955. {sel: '#copy_save_done', fn: cancelOverlays, evt: 'click'},
  3956. // Shortcuts not associated with buttons
  3957. {key: 'ctrl+left', fn: function(){rotateSelected(0,1);}},
  3958. {key: 'ctrl+right', fn: function(){rotateSelected(1,1);}},
  3959. {key: 'ctrl+shift+left', fn: function(){rotateSelected(0,5);}},
  3960. {key: 'ctrl+shift+right', fn: function(){rotateSelected(1,5);}},
  3961. {key: 'shift+O', fn: selectPrev},
  3962. {key: 'shift+P', fn: selectNext},
  3963. {key: [modKey+'up', true], fn: function(){zoomImage(2);}},
  3964. {key: [modKey+'down', true], fn: function(){zoomImage(0.5);}},
  3965. {key: [modKey+']', true], fn: function(){moveUpDownSelected('Up');}},
  3966. {key: [modKey+'[', true], fn: function(){moveUpDownSelected('Down');}},
  3967. {key: ['up', true], fn: function(){moveSelected(0,-1);}},
  3968. {key: ['down', true], fn: function(){moveSelected(0,1);}},
  3969. {key: ['left', true], fn: function(){moveSelected(-1,0);}},
  3970. {key: ['right', true], fn: function(){moveSelected(1,0);}},
  3971. {key: 'shift+up', fn: function(){moveSelected(0,-10);}},
  3972. {key: 'shift+down', fn: function(){moveSelected(0,10);}},
  3973. {key: 'shift+left', fn: function(){moveSelected(-10,0);}},
  3974. {key: 'shift+right', fn: function(){moveSelected(10,0);}},
  3975. {key: ['alt+up', true], fn: function(){svgCanvas.cloneSelectedElements(0,-1);}},
  3976. {key: ['alt+down', true], fn: function(){svgCanvas.cloneSelectedElements(0,1);}},
  3977. {key: ['alt+left', true], fn: function(){svgCanvas.cloneSelectedElements(-1,0);}},
  3978. {key: ['alt+right', true], fn: function(){svgCanvas.cloneSelectedElements(1,0);}},
  3979. {key: ['alt+shift+up', true], fn: function(){svgCanvas.cloneSelectedElements(0,-10);}},
  3980. {key: ['alt+shift+down', true], fn: function(){svgCanvas.cloneSelectedElements(0,10);}},
  3981. {key: ['alt+shift+left', true], fn: function(){svgCanvas.cloneSelectedElements(-10,0);}},
  3982. {key: ['alt+shift+right', true], fn: function(){svgCanvas.cloneSelectedElements(10,0);}},
  3983. {key: 'A', fn: function(){svgCanvas.selectAllInCurrentLayer();}},
  3984. // Standard shortcuts
  3985. {key: modKey+'z', fn: clickUndo},
  3986. {key: modKey + 'shift+z', fn: clickRedo},
  3987. {key: modKey + 'y', fn: clickRedo},
  3988. {key: modKey+'x', fn: cutSelected},
  3989. {key: modKey+'c', fn: copySelected},
  3990. {key: modKey+'v', fn: pasteInCenter}
  3991. ];
  3992. // Tooltips not directly associated with a single function
  3993. var key_assocs = {
  3994. '4/Shift+4': '#tools_rect_show',
  3995. '5/Shift+5': '#tools_ellipse_show'
  3996. };
  3997. return {
  3998. setAll: function() {
  3999. var flyouts = {};
  4000. $.each(tool_buttons, function(i, opts) {
  4001. // Bind function to button
  4002. var btn;
  4003. if (opts.sel) {
  4004. btn = $(opts.sel);
  4005. if (btn.length == 0) {return true;} // Skip if markup does not exist
  4006. if (opts.evt) {
  4007. if (svgedit.browser.isTouch() && opts.evt === 'click') {
  4008. opts.evt = 'mousedown';
  4009. }
  4010. btn[opts.evt](opts.fn);
  4011. }
  4012. // Add to parent flyout menu, if able to be displayed
  4013. if (opts.parent && $(opts.parent + '_show').length != 0) {
  4014. var f_h = $(opts.parent);
  4015. if (!f_h.length) {
  4016. f_h = makeFlyoutHolder(opts.parent.substr(1));
  4017. }
  4018. f_h.append(btn);
  4019. if (!$.isArray(flyouts[opts.parent])) {
  4020. flyouts[opts.parent] = [];
  4021. }
  4022. flyouts[opts.parent].push(opts);
  4023. }
  4024. }
  4025. // Bind function to shortcut key
  4026. if (opts.key) {
  4027. // Set shortcut based on options
  4028. var keyval, disInInp = true, fn = opts.fn, pd = false;
  4029. if ($.isArray(opts.key)) {
  4030. keyval = opts.key[0];
  4031. if (opts.key.length > 1) {pd = opts.key[1];}
  4032. if (opts.key.length > 2) {disInInp = opts.key[2];}
  4033. } else {
  4034. keyval = opts.key;
  4035. }
  4036. keyval += '';
  4037. $.each(keyval.split('/'), function(i, key) {
  4038. $(document).bind('keydown', key, function(e) {
  4039. fn();
  4040. if (pd) {
  4041. e.preventDefault();
  4042. }
  4043. // Prevent default on ALL keys?
  4044. return false;
  4045. });
  4046. });
  4047. // Put shortcut in title
  4048. if (opts.sel && !opts.hidekey && btn.attr('title')) {
  4049. var newTitle = btn.attr('title').split('[')[0] + ' (' + keyval + ')';
  4050. key_assocs[keyval] = opts.sel;
  4051. // Disregard for menu items
  4052. if (!btn.parents('#main_menu').length) {
  4053. btn.attr('title', newTitle);
  4054. }
  4055. }
  4056. }
  4057. });
  4058. // Setup flyouts
  4059. setupFlyouts(flyouts);
  4060. // Misc additional actions
  4061. // Make 'return' keypress trigger the change event
  4062. $('.attr_changer, #image_url').bind('keydown', 'return',
  4063. function(evt) {$(this).change();evt.preventDefault();}
  4064. );
  4065. $(window).bind('keydown', 'tab', function(e) {
  4066. if (ui_context === 'canvas') {
  4067. e.preventDefault();
  4068. selectNext();
  4069. }
  4070. }).bind('keydown', 'shift+tab', function(e) {
  4071. if (ui_context === 'canvas') {
  4072. e.preventDefault();
  4073. selectPrev();
  4074. }
  4075. });
  4076. $('#tool_zoom').dblclick(dblclickZoom);
  4077. },
  4078. setTitles: function() {
  4079. $.each(key_assocs, function(keyval, sel) {
  4080. var menu = ($(sel).parents('#main_menu').length);
  4081. $(sel).each(function() {
  4082. var t;
  4083. if (menu) {
  4084. t = $(this).text().split(' [')[0];
  4085. } else {
  4086. t = this.title.split(' [')[0];
  4087. }
  4088. var key_str = '';
  4089. // Shift+Up
  4090. $.each(keyval.split('/'), function(i, key) {
  4091. var mod_bits = key.split('+'), mod = '';
  4092. if (mod_bits.length > 1) {
  4093. mod = mod_bits[0] + '+';
  4094. key = mod_bits[1];
  4095. }
  4096. key_str += (i?'/':'') + mod + (uiStrings['key_'+key] || key);
  4097. });
  4098. if (menu) {
  4099. this.lastChild.textContent = t +' ['+key_str+']';
  4100. } else {
  4101. this.title = t +' ['+key_str+']';
  4102. }
  4103. });
  4104. });
  4105. },
  4106. getButtonData: function(sel) {
  4107. var b;
  4108. $.each(tool_buttons, function(i, btn) {
  4109. if (btn.sel === sel) {b = btn;}
  4110. });
  4111. return b;
  4112. }
  4113. };
  4114. }());
  4115. Actions.setAll();
  4116. // Select given tool
  4117. editor.ready(function() {
  4118. var tool,
  4119. itool = curConfig.initTool,
  4120. container = $('#tools_left, #svg_editor .tools_flyout'),
  4121. pre_tool = container.find('#tool_' + itool),
  4122. reg_tool = container.find('#' + itool);
  4123. if (pre_tool.length) {
  4124. tool = pre_tool;
  4125. } else if (reg_tool.length) {
  4126. tool = reg_tool;
  4127. } else {
  4128. tool = $('#tool_select');
  4129. }
  4130. tool.click().mouseup();
  4131. if (curConfig.wireframe) {
  4132. $('#tool_wireframe').click();
  4133. }
  4134. if (curConfig.showlayers) {
  4135. toggleSidePanel();
  4136. }
  4137. $('#rulers').toggle(!!curConfig.showRulers);
  4138. if (curConfig.showRulers) {
  4139. $('#show_rulers')[0].checked = true;
  4140. }
  4141. if (curConfig.baseUnit) {
  4142. $('#base_unit').val(curConfig.baseUnit);
  4143. }
  4144. if (curConfig.gridSnapping) {
  4145. $('#grid_snapping_on')[0].checked = true;
  4146. }
  4147. if (curConfig.snappingStep) {
  4148. $('#grid_snapping_step').val(curConfig.snappingStep);
  4149. }
  4150. if (curConfig.gridColor) {
  4151. $('#grid_color').val(curConfig.gridColor);
  4152. }
  4153. });
  4154. // init SpinButtons
  4155. $('#rect_rx').SpinButton({ min: 0, max: 1000, callback: changeRectRadius });
  4156. $('#stroke_width').SpinButton({ min: 0, max: 99, smallStep: 0.1, callback: changeStrokeWidth });
  4157. $('#angle').SpinButton({ min: -180, max: 180, step: 5, callback: changeRotationAngle });
  4158. $('#font_size').SpinButton({ min: 0.001, stepfunc: stepFontSize, callback: changeFontSize });
  4159. $('#group_opacity').SpinButton({ min: 0, max: 100, step: 5, callback: changeOpacity });
  4160. $('#blur').SpinButton({ min: 0, max: 10, step: 0.1, callback: changeBlur });
  4161. $('#zoom').SpinButton({ min: 0.001, max: 10000, step: 50, stepfunc: stepZoom, callback: changeZoom })
  4162. // Set default zoom
  4163. .val(svgCanvas.getZoom() * 100);
  4164. $('#workarea').contextMenu({
  4165. menu: 'cmenu_canvas',
  4166. inSpeed: 0
  4167. },
  4168. function(action, el, pos) {
  4169. switch (action) {
  4170. case 'delete':
  4171. deleteSelected();
  4172. break;
  4173. case 'cut':
  4174. cutSelected();
  4175. break;
  4176. case 'copy':
  4177. copySelected();
  4178. break;
  4179. case 'paste':
  4180. svgCanvas.pasteElements();
  4181. break;
  4182. case 'paste_in_place':
  4183. svgCanvas.pasteElements('in_place');
  4184. break;
  4185. case 'group_elements':
  4186. svgCanvas.groupSelectedElements();
  4187. break;
  4188. case 'ungroup':
  4189. svgCanvas.ungroupSelectedElement();
  4190. break;
  4191. case 'move_front':
  4192. moveToTopSelected();
  4193. break;
  4194. case 'move_up':
  4195. moveUpDownSelected('Up');
  4196. break;
  4197. case 'move_down':
  4198. moveUpDownSelected('Down');
  4199. break;
  4200. case 'move_back':
  4201. moveToBottomSelected();
  4202. break;
  4203. default:
  4204. if (svgedit.contextmenu && svgedit.contextmenu.hasCustomHandler(action)) {
  4205. svgedit.contextmenu.getCustomHandler(action).call();
  4206. }
  4207. break;
  4208. }
  4209. if (svgCanvas.clipBoard.length) {
  4210. canv_menu.enableContextMenuItems('#paste,#paste_in_place');
  4211. }
  4212. }
  4213. );
  4214. var lmenu_func = function(action, el, pos) {
  4215. switch ( action ) {
  4216. case 'dupe':
  4217. cloneLayer();
  4218. break;
  4219. case 'delete':
  4220. deleteLayer();
  4221. break;
  4222. case 'merge_down':
  4223. mergeLayer();
  4224. break;
  4225. case 'merge_all':
  4226. svgCanvas.mergeAllLayers();
  4227. updateContextPanel();
  4228. populateLayers();
  4229. break;
  4230. }
  4231. };
  4232. $('#layerlist').contextMenu({
  4233. menu: 'cmenu_layers',
  4234. inSpeed: 0
  4235. },
  4236. lmenu_func
  4237. );
  4238. $('#layer_moreopts').contextMenu({
  4239. menu: 'cmenu_layers',
  4240. inSpeed: 0,
  4241. allowLeft: true
  4242. },
  4243. lmenu_func
  4244. );
  4245. $('.contextMenu li').mousedown(function(ev) {
  4246. ev.preventDefault();
  4247. });
  4248. $('#cmenu_canvas li').disableContextMenu();
  4249. canv_menu.enableContextMenuItems('#delete,#cut,#copy');
  4250. window.addEventListener('beforeunload', function(e) {
  4251. // Suppress warning if page is empty
  4252. if (undoMgr.getUndoStackSize() === 0) {
  4253. editor.showSaveWarning = false;
  4254. }
  4255. // showSaveWarning is set to 'false' when the page is saved.
  4256. if (!curConfig.no_save_warning && editor.showSaveWarning) {
  4257. // Browser already asks question about closing the page
  4258. e.returnValue = uiStrings.notification.unsavedChanges; // Firefox needs this when beforeunload set by addEventListener (even though message is not used)
  4259. return uiStrings.notification.unsavedChanges;
  4260. }
  4261. }, false);
  4262. editor.openPrep = function(func) {
  4263. $('#main_menu').hide();
  4264. if (undoMgr.getUndoStackSize() === 0) {
  4265. func(true);
  4266. } else {
  4267. $.confirm(uiStrings.notification.QwantToOpen, func);
  4268. }
  4269. };
  4270. function onDragEnter(e) {
  4271. e.stopPropagation();
  4272. e.preventDefault();
  4273. // and indicator should be displayed here, such as "drop files here"
  4274. }
  4275. function onDragOver(e) {
  4276. e.stopPropagation();
  4277. e.preventDefault();
  4278. }
  4279. function onDragLeave(e) {
  4280. e.stopPropagation();
  4281. e.preventDefault();
  4282. // hypothetical indicator should be removed here
  4283. }
  4284. // Use HTML5 File API: http://www.w3.org/TR/FileAPI/
  4285. // if browser has HTML5 File API support, then we will show the open menu item
  4286. // and provide a file input to click. When that change event fires, it will
  4287. // get the text contents of the file and send it to the canvas
  4288. if (window.FileReader) {
  4289. var importImage = function(e) {
  4290. $.process_cancel(uiStrings.notification.loadingImage);
  4291. e.stopPropagation();
  4292. e.preventDefault();
  4293. $('#workarea').removeAttr('style');
  4294. $('#main_menu').hide();
  4295. var file = (e.type == 'drop') ? e.dataTransfer.files[0] : this.files[0];
  4296. if (!file) {
  4297. return;
  4298. }
  4299. if (file.type.indexOf('image') != -1) {
  4300. // Detected an image
  4301. // svg handling
  4302. var reader;
  4303. if (file.type.indexOf('svg') != -1) {
  4304. reader = new FileReader();
  4305. reader.onloadend = function(e) {
  4306. svgCanvas.importSvgString(e.target.result, true);
  4307. svgCanvas.ungroupSelectedElement();
  4308. svgCanvas.ungroupSelectedElement();
  4309. svgCanvas.groupSelectedElements();
  4310. svgCanvas.alignSelectedElements('m', 'page');
  4311. svgCanvas.alignSelectedElements('c', 'page');
  4312. };
  4313. reader.readAsText(file);
  4314. } else {
  4315. //bitmap handling
  4316. reader = new FileReader();
  4317. reader.onloadend = function(e) {
  4318. // let's insert the new image until we know its dimensions
  4319. var insertNewImage = function(width, height) {
  4320. var newImage = svgCanvas.addSvgElementFromJson({
  4321. element: 'image',
  4322. attr: {
  4323. x: 0,
  4324. y: 0,
  4325. width: width,
  4326. height: height,
  4327. id: svgCanvas.getNextId(),
  4328. style: 'pointer-events:inherit'
  4329. }
  4330. });
  4331. svgCanvas.setHref(newImage, e.target.result);
  4332. svgCanvas.selectOnly([newImage]);
  4333. svgCanvas.alignSelectedElements('m', 'page');
  4334. svgCanvas.alignSelectedElements('c', 'page');
  4335. updateContextPanel();
  4336. };
  4337. // create dummy img so we know the default dimensions
  4338. var imgWidth = 100;
  4339. var imgHeight = 100;
  4340. var img = new Image();
  4341. img.src = e.target.result;
  4342. img.style.opacity = 0;
  4343. img.onload = function() {
  4344. imgWidth = img.offsetWidth;
  4345. imgHeight = img.offsetHeight;
  4346. insertNewImage(imgWidth, imgHeight);
  4347. };
  4348. };
  4349. reader.readAsDataURL(file);
  4350. }
  4351. }
  4352. };
  4353. workarea[0].addEventListener('dragenter', onDragEnter, false);
  4354. workarea[0].addEventListener('dragover', onDragOver, false);
  4355. workarea[0].addEventListener('dragleave', onDragLeave, false);
  4356. workarea[0].addEventListener('drop', importImage, false);
  4357. var open = $('<input type="file">').change(function() {
  4358. var f = this;
  4359. editor.openPrep(function(ok) {
  4360. if (!ok) {return;}
  4361. svgCanvas.clear();
  4362. if (f.files.length==1) {
  4363. $.process_cancel(uiStrings.notification.loadingImage);
  4364. var reader = new FileReader();
  4365. reader.onloadend = function(e) {
  4366. loadSvgString(e.target.result);
  4367. updateCanvas();
  4368. };
  4369. reader.readAsText(f.files[0]);
  4370. }
  4371. });
  4372. });
  4373. $('#tool_open').show().prepend(open);
  4374. var imgImport = $('<input type="file">').change(importImage);
  4375. $('#tool_import').show().prepend(imgImport);
  4376. }
  4377. // $(function() {
  4378. updateCanvas(true);
  4379. // });
  4380. // var revnums = "svg-editor.js ($Rev: 2672 $) ";
  4381. // revnums += svgCanvas.getVersion();
  4382. // $('#copyright')[0].setAttribute('title', revnums);
  4383. // Callback handler for embedapi.js
  4384. try {
  4385. window.addEventListener('message', function(e) {
  4386. // We accept and post strings for the sake of IE9 support
  4387. if (typeof e.data !== 'string' || e.data.charAt() === '|') {
  4388. return;
  4389. }
  4390. var data = JSON.parse(e.data);
  4391. if (!data || typeof data !== 'object' || data.namespace !== 'svgCanvas') {
  4392. return;
  4393. }
  4394. var cbid = data.id,
  4395. name = data.name,
  4396. args = data.args;
  4397. try {
  4398. e.source.postMessage(JSON.stringify({namespace: 'svg-edit', id: cbid, result: svgCanvas[name].apply(svgCanvas, args)}), '*');
  4399. } catch(err) {
  4400. e.source.postMessage(JSON.stringify({namespace: 'svg-edit', id: cbid, error: err.message}), '*');
  4401. }
  4402. }, false);
  4403. } catch(err) {
  4404. window.embed_error = err;
  4405. }
  4406. // For Compatibility with older extensions
  4407. $(function() {
  4408. window.svgCanvas = svgCanvas;
  4409. svgCanvas.ready = svgEditor.ready;
  4410. });
  4411. editor.setLang = function(lang, allStrings) {
  4412. editor.langChanged = true;
  4413. $.pref('lang', lang);
  4414. $('#lang_select').val(lang);
  4415. if (!allStrings) {
  4416. return;
  4417. }
  4418. // var notif = allStrings.notification; // Currently unused
  4419. // $.extend will only replace the given strings
  4420. var oldLayerName = $('#layerlist tr.layersel td.layername').text();
  4421. var rename_layer = (oldLayerName == uiStrings.common.layer + ' 1');
  4422. $.extend(uiStrings, allStrings);
  4423. svgCanvas.setUiStrings(allStrings);
  4424. Actions.setTitles();
  4425. if (rename_layer) {
  4426. svgCanvas.renameCurrentLayer(uiStrings.common.layer + ' 1');
  4427. populateLayers();
  4428. }
  4429. // In case extensions loaded before the locale, now we execute a callback on them
  4430. if (extsPreLang.length) {
  4431. while (extsPreLang.length) {
  4432. var ext = extsPreLang.shift();
  4433. ext.langReady({lang: lang, uiStrings: uiStrings});
  4434. }
  4435. }
  4436. else {
  4437. svgCanvas.runExtensions('langReady', {lang: lang, uiStrings: uiStrings});
  4438. }
  4439. svgCanvas.runExtensions('langChanged', lang);
  4440. // Update flyout tooltips
  4441. setFlyoutTitles();
  4442. // Copy title for certain tool elements
  4443. var elems = {
  4444. '#stroke_color': '#tool_stroke .icon_label, #tool_stroke .color_block',
  4445. '#fill_color': '#tool_fill label, #tool_fill .color_block',
  4446. '#linejoin_miter': '#cur_linejoin',
  4447. '#linecap_butt': '#cur_linecap'
  4448. };
  4449. $.each(elems, function(source, dest) {
  4450. $(dest).attr('title', $(source)[0].title);
  4451. });
  4452. // Copy alignment titles
  4453. $('#multiselected_panel div[id^=tool_align]').each(function() {
  4454. $('#tool_pos' + this.id.substr(10))[0].title = this.title;
  4455. });
  4456. };
  4457. };
  4458. editor.ready = function (cb) {
  4459. if (!isReady) {
  4460. callbacks.push(cb);
  4461. } else {
  4462. cb();
  4463. }
  4464. };
  4465. editor.runCallbacks = function () {
  4466. $.each(callbacks, function() {
  4467. this();
  4468. });
  4469. isReady = true;
  4470. };
  4471. editor.loadFromString = function (str) {
  4472. editor.ready(function() {
  4473. loadSvgString(str);
  4474. });
  4475. };
  4476. editor.disableUI = function (featList) {
  4477. // $(function() {
  4478. // $('#tool_wireframe, #tool_image, #main_button, #tool_source, #sidepanels').remove();
  4479. // $('#tools_top').css('left', 5);
  4480. // });
  4481. };
  4482. editor.loadFromURL = function (url, opts) {
  4483. if (!opts) {opts = {};}
  4484. var cache = opts.cache;
  4485. var cb = opts.callback;
  4486. editor.ready(function() {
  4487. $.ajax({
  4488. 'url': url,
  4489. 'dataType': 'text',
  4490. cache: !!cache,
  4491. beforeSend:function(){
  4492. $.process_cancel(uiStrings.notification.loadingImage);
  4493. },
  4494. success: function(str) {
  4495. loadSvgString(str, cb);
  4496. },
  4497. error: function(xhr, stat, err) {
  4498. if (xhr.status != 404 && xhr.responseText) {
  4499. loadSvgString(xhr.responseText, cb);
  4500. } else {
  4501. $.alert(uiStrings.notification.URLloadFail + ': \n' + err, cb);
  4502. }
  4503. },
  4504. complete:function(){
  4505. $('#dialog_box').hide();
  4506. }
  4507. });
  4508. });
  4509. };
  4510. editor.loadFromDataURI = function(str) {
  4511. editor.ready(function() {
  4512. var pre = 'data:image/svg+xml;base64,';
  4513. var src = str.substring(pre.length);
  4514. loadSvgString(svgedit.utilities.decode64(src));
  4515. });
  4516. };
  4517. editor.addExtension = function () {
  4518. var args = arguments;
  4519. // Note that we don't want this on editor.ready since some extensions
  4520. // may want to run before then (like server_opensave).
  4521. $(function() {
  4522. if (svgCanvas) {svgCanvas.addExtension.apply(this, args);}
  4523. });
  4524. };
  4525. return editor;
  4526. }(jQuery));
  4527. // Run init once DOM is loaded
  4528. $(svgEditor.init);
  4529. }());