svg-editor-copy-ok.js 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120
  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. }());