filepond.js 427 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729
  1. /*!
  2. * FilePond 4.30.4
  3. * Licensed under MIT, https://opensource.org/licenses/MIT/
  4. * Please visit https://pqina.nl/filepond/ for details.
  5. */
  6. /* eslint-disable */
  7. (function(global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined'
  9. ? factory(exports)
  10. : typeof define === 'function' && define.amd
  11. ? define(['exports'], factory)
  12. : ((global = global || self), factory((global.FilePond = {})));
  13. })(this, function(exports) {
  14. 'use strict';
  15. var isNode = function isNode(value) {
  16. return value instanceof HTMLElement;
  17. };
  18. var createStore = function createStore(initialState) {
  19. var queries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  20. var actions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  21. // internal state
  22. var state = Object.assign({}, initialState);
  23. // contains all actions for next frame, is clear when actions are requested
  24. var actionQueue = [];
  25. var dispatchQueue = [];
  26. // returns a duplicate of the current state
  27. var getState = function getState() {
  28. return Object.assign({}, state);
  29. };
  30. // returns a duplicate of the actions array and clears the actions array
  31. var processActionQueue = function processActionQueue() {
  32. // create copy of actions queue
  33. var queue = [].concat(actionQueue);
  34. // clear actions queue (we don't want no double actions)
  35. actionQueue.length = 0;
  36. return queue;
  37. };
  38. // processes actions that might block the main UI thread
  39. var processDispatchQueue = function processDispatchQueue() {
  40. // create copy of actions queue
  41. var queue = [].concat(dispatchQueue);
  42. // clear actions queue (we don't want no double actions)
  43. dispatchQueue.length = 0;
  44. // now dispatch these actions
  45. queue.forEach(function(_ref) {
  46. var type = _ref.type,
  47. data = _ref.data;
  48. dispatch(type, data);
  49. });
  50. };
  51. // adds a new action, calls its handler and
  52. var dispatch = function dispatch(type, data, isBlocking) {
  53. // is blocking action (should never block if document is hidden)
  54. if (isBlocking && !document.hidden) {
  55. dispatchQueue.push({ type: type, data: data });
  56. return;
  57. }
  58. // if this action has a handler, handle the action
  59. if (actionHandlers[type]) {
  60. actionHandlers[type](data);
  61. }
  62. // now add action
  63. actionQueue.push({
  64. type: type,
  65. data: data,
  66. });
  67. };
  68. var query = function query(str) {
  69. var _queryHandles;
  70. for (
  71. var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1;
  72. _key < _len;
  73. _key++
  74. ) {
  75. args[_key - 1] = arguments[_key];
  76. }
  77. return queryHandles[str]
  78. ? (_queryHandles = queryHandles)[str].apply(_queryHandles, args)
  79. : null;
  80. };
  81. var api = {
  82. getState: getState,
  83. processActionQueue: processActionQueue,
  84. processDispatchQueue: processDispatchQueue,
  85. dispatch: dispatch,
  86. query: query,
  87. };
  88. var queryHandles = {};
  89. queries.forEach(function(query) {
  90. queryHandles = Object.assign({}, query(state), {}, queryHandles);
  91. });
  92. var actionHandlers = {};
  93. actions.forEach(function(action) {
  94. actionHandlers = Object.assign({}, action(dispatch, query, state), {}, actionHandlers);
  95. });
  96. return api;
  97. };
  98. var defineProperty = function defineProperty(obj, property, definition) {
  99. if (typeof definition === 'function') {
  100. obj[property] = definition;
  101. return;
  102. }
  103. Object.defineProperty(obj, property, Object.assign({}, definition));
  104. };
  105. var forin = function forin(obj, cb) {
  106. for (var key in obj) {
  107. if (!obj.hasOwnProperty(key)) {
  108. continue;
  109. }
  110. cb(key, obj[key]);
  111. }
  112. };
  113. var createObject = function createObject(definition) {
  114. var obj = {};
  115. forin(definition, function(property) {
  116. defineProperty(obj, property, definition[property]);
  117. });
  118. return obj;
  119. };
  120. var attr = function attr(node, name) {
  121. var value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  122. if (value === null) {
  123. return node.getAttribute(name) || node.hasAttribute(name);
  124. }
  125. node.setAttribute(name, value);
  126. };
  127. var ns = 'http://www.w3.org/2000/svg';
  128. var svgElements = ['svg', 'path']; // only svg elements used
  129. var isSVGElement = function isSVGElement(tag) {
  130. return svgElements.includes(tag);
  131. };
  132. var createElement = function createElement(tag, className) {
  133. var attributes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  134. if (typeof className === 'object') {
  135. attributes = className;
  136. className = null;
  137. }
  138. var element = isSVGElement(tag)
  139. ? document.createElementNS(ns, tag)
  140. : document.createElement(tag);
  141. if (className) {
  142. if (isSVGElement(tag)) {
  143. attr(element, 'class', className);
  144. } else {
  145. element.className = className;
  146. }
  147. }
  148. forin(attributes, function(name, value) {
  149. attr(element, name, value);
  150. });
  151. return element;
  152. };
  153. var appendChild = function appendChild(parent) {
  154. return function(child, index) {
  155. if (typeof index !== 'undefined' && parent.children[index]) {
  156. parent.insertBefore(child, parent.children[index]);
  157. } else {
  158. parent.appendChild(child);
  159. }
  160. };
  161. };
  162. var appendChildView = function appendChildView(parent, childViews) {
  163. return function(view, index) {
  164. if (typeof index !== 'undefined') {
  165. childViews.splice(index, 0, view);
  166. } else {
  167. childViews.push(view);
  168. }
  169. return view;
  170. };
  171. };
  172. var removeChildView = function removeChildView(parent, childViews) {
  173. return function(view) {
  174. // remove from child views
  175. childViews.splice(childViews.indexOf(view), 1);
  176. // remove the element
  177. if (view.element.parentNode) {
  178. parent.removeChild(view.element);
  179. }
  180. return view;
  181. };
  182. };
  183. var IS_BROWSER = (function() {
  184. return typeof window !== 'undefined' && typeof window.document !== 'undefined';
  185. })();
  186. var isBrowser = function isBrowser() {
  187. return IS_BROWSER;
  188. };
  189. var testElement = isBrowser() ? createElement('svg') : {};
  190. var getChildCount =
  191. 'children' in testElement
  192. ? function(el) {
  193. return el.children.length;
  194. }
  195. : function(el) {
  196. return el.childNodes.length;
  197. };
  198. var getViewRect = function getViewRect(elementRect, childViews, offset, scale) {
  199. var left = offset[0] || elementRect.left;
  200. var top = offset[1] || elementRect.top;
  201. var right = left + elementRect.width;
  202. var bottom = top + elementRect.height * (scale[1] || 1);
  203. var rect = {
  204. // the rectangle of the element itself
  205. element: Object.assign({}, elementRect),
  206. // the rectangle of the element expanded to contain its children, does not include any margins
  207. inner: {
  208. left: elementRect.left,
  209. top: elementRect.top,
  210. right: elementRect.right,
  211. bottom: elementRect.bottom,
  212. },
  213. // the rectangle of the element expanded to contain its children including own margin and child margins
  214. // margins will be added after we've recalculated the size
  215. outer: {
  216. left: left,
  217. top: top,
  218. right: right,
  219. bottom: bottom,
  220. },
  221. };
  222. // expand rect to fit all child rectangles
  223. childViews
  224. .filter(function(childView) {
  225. return !childView.isRectIgnored();
  226. })
  227. .map(function(childView) {
  228. return childView.rect;
  229. })
  230. .forEach(function(childViewRect) {
  231. expandRect(rect.inner, Object.assign({}, childViewRect.inner));
  232. expandRect(rect.outer, Object.assign({}, childViewRect.outer));
  233. });
  234. // calculate inner width and height
  235. calculateRectSize(rect.inner);
  236. // append additional margin (top and left margins are included in top and left automatically)
  237. rect.outer.bottom += rect.element.marginBottom;
  238. rect.outer.right += rect.element.marginRight;
  239. // calculate outer width and height
  240. calculateRectSize(rect.outer);
  241. return rect;
  242. };
  243. var expandRect = function expandRect(parent, child) {
  244. // adjust for parent offset
  245. child.top += parent.top;
  246. child.right += parent.left;
  247. child.bottom += parent.top;
  248. child.left += parent.left;
  249. if (child.bottom > parent.bottom) {
  250. parent.bottom = child.bottom;
  251. }
  252. if (child.right > parent.right) {
  253. parent.right = child.right;
  254. }
  255. };
  256. var calculateRectSize = function calculateRectSize(rect) {
  257. rect.width = rect.right - rect.left;
  258. rect.height = rect.bottom - rect.top;
  259. };
  260. var isNumber = function isNumber(value) {
  261. return typeof value === 'number';
  262. };
  263. /**
  264. * Determines if position is at destination
  265. * @param position
  266. * @param destination
  267. * @param velocity
  268. * @param errorMargin
  269. * @returns {boolean}
  270. */
  271. var thereYet = function thereYet(position, destination, velocity) {
  272. var errorMargin = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0.001;
  273. return Math.abs(position - destination) < errorMargin && Math.abs(velocity) < errorMargin;
  274. };
  275. /**
  276. * Spring animation
  277. */
  278. var spring =
  279. // default options
  280. function spring() // method definition
  281. {
  282. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  283. _ref$stiffness = _ref.stiffness,
  284. stiffness = _ref$stiffness === void 0 ? 0.5 : _ref$stiffness,
  285. _ref$damping = _ref.damping,
  286. damping = _ref$damping === void 0 ? 0.75 : _ref$damping,
  287. _ref$mass = _ref.mass,
  288. mass = _ref$mass === void 0 ? 10 : _ref$mass;
  289. var target = null;
  290. var position = null;
  291. var velocity = 0;
  292. var resting = false;
  293. // updates spring state
  294. var interpolate = function interpolate(ts, skipToEndState) {
  295. // in rest, don't animate
  296. if (resting) return;
  297. // need at least a target or position to do springy things
  298. if (!(isNumber(target) && isNumber(position))) {
  299. resting = true;
  300. velocity = 0;
  301. return;
  302. }
  303. // calculate spring force
  304. var f = -(position - target) * stiffness;
  305. // update velocity by adding force based on mass
  306. velocity += f / mass;
  307. // update position by adding velocity
  308. position += velocity;
  309. // slow down based on amount of damping
  310. velocity *= damping;
  311. // we've arrived if we're near target and our velocity is near zero
  312. if (thereYet(position, target, velocity) || skipToEndState) {
  313. position = target;
  314. velocity = 0;
  315. resting = true;
  316. // we done
  317. api.onupdate(position);
  318. api.oncomplete(position);
  319. } else {
  320. // progress update
  321. api.onupdate(position);
  322. }
  323. };
  324. /**
  325. * Set new target value
  326. * @param value
  327. */
  328. var setTarget = function setTarget(value) {
  329. // if currently has no position, set target and position to this value
  330. if (isNumber(value) && !isNumber(position)) {
  331. position = value;
  332. }
  333. // next target value will not be animated to
  334. if (target === null) {
  335. target = value;
  336. position = value;
  337. }
  338. // let start moving to target
  339. target = value;
  340. // already at target
  341. if (position === target || typeof target === 'undefined') {
  342. // now resting as target is current position, stop moving
  343. resting = true;
  344. velocity = 0;
  345. // done!
  346. api.onupdate(position);
  347. api.oncomplete(position);
  348. return;
  349. }
  350. resting = false;
  351. };
  352. // need 'api' to call onupdate callback
  353. var api = createObject({
  354. interpolate: interpolate,
  355. target: {
  356. set: setTarget,
  357. get: function get() {
  358. return target;
  359. },
  360. },
  361. resting: {
  362. get: function get() {
  363. return resting;
  364. },
  365. },
  366. onupdate: function onupdate(value) {},
  367. oncomplete: function oncomplete(value) {},
  368. });
  369. return api;
  370. };
  371. var easeLinear = function easeLinear(t) {
  372. return t;
  373. };
  374. var easeInOutQuad = function easeInOutQuad(t) {
  375. return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
  376. };
  377. var tween =
  378. // default values
  379. function tween() // method definition
  380. {
  381. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  382. _ref$duration = _ref.duration,
  383. duration = _ref$duration === void 0 ? 500 : _ref$duration,
  384. _ref$easing = _ref.easing,
  385. easing = _ref$easing === void 0 ? easeInOutQuad : _ref$easing,
  386. _ref$delay = _ref.delay,
  387. delay = _ref$delay === void 0 ? 0 : _ref$delay;
  388. var start = null;
  389. var t;
  390. var p;
  391. var resting = true;
  392. var reverse = false;
  393. var target = null;
  394. var interpolate = function interpolate(ts, skipToEndState) {
  395. if (resting || target === null) return;
  396. if (start === null) {
  397. start = ts;
  398. }
  399. if (ts - start < delay) return;
  400. t = ts - start - delay;
  401. if (t >= duration || skipToEndState) {
  402. t = 1;
  403. p = reverse ? 0 : 1;
  404. api.onupdate(p * target);
  405. api.oncomplete(p * target);
  406. resting = true;
  407. } else {
  408. p = t / duration;
  409. api.onupdate((t >= 0 ? easing(reverse ? 1 - p : p) : 0) * target);
  410. }
  411. };
  412. // need 'api' to call onupdate callback
  413. var api = createObject({
  414. interpolate: interpolate,
  415. target: {
  416. get: function get() {
  417. return reverse ? 0 : target;
  418. },
  419. set: function set(value) {
  420. // is initial value
  421. if (target === null) {
  422. target = value;
  423. api.onupdate(value);
  424. api.oncomplete(value);
  425. return;
  426. }
  427. // want to tween to a smaller value and have a current value
  428. if (value < target) {
  429. target = 1;
  430. reverse = true;
  431. } else {
  432. // not tweening to a smaller value
  433. reverse = false;
  434. target = value;
  435. }
  436. // let's go!
  437. resting = false;
  438. start = null;
  439. },
  440. },
  441. resting: {
  442. get: function get() {
  443. return resting;
  444. },
  445. },
  446. onupdate: function onupdate(value) {},
  447. oncomplete: function oncomplete(value) {},
  448. });
  449. return api;
  450. };
  451. var animator = {
  452. spring: spring,
  453. tween: tween,
  454. };
  455. /*
  456. { type: 'spring', stiffness: .5, damping: .75, mass: 10 };
  457. { translation: { type: 'spring', ... }, ... }
  458. { translation: { x: { type: 'spring', ... } } }
  459. */
  460. var createAnimator = function createAnimator(definition, category, property) {
  461. // default is single definition
  462. // we check if transform is set, if so, we check if property is set
  463. var def =
  464. definition[category] && typeof definition[category][property] === 'object'
  465. ? definition[category][property]
  466. : definition[category] || definition;
  467. var type = typeof def === 'string' ? def : def.type;
  468. var props = typeof def === 'object' ? Object.assign({}, def) : {};
  469. return animator[type] ? animator[type](props) : null;
  470. };
  471. var addGetSet = function addGetSet(keys, obj, props) {
  472. var overwrite = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  473. obj = Array.isArray(obj) ? obj : [obj];
  474. obj.forEach(function(o) {
  475. keys.forEach(function(key) {
  476. var name = key;
  477. var getter = function getter() {
  478. return props[key];
  479. };
  480. var setter = function setter(value) {
  481. return (props[key] = value);
  482. };
  483. if (typeof key === 'object') {
  484. name = key.key;
  485. getter = key.getter || getter;
  486. setter = key.setter || setter;
  487. }
  488. if (o[name] && !overwrite) {
  489. return;
  490. }
  491. o[name] = {
  492. get: getter,
  493. set: setter,
  494. };
  495. });
  496. });
  497. };
  498. // add to state,
  499. // add getters and setters to internal and external api (if not set)
  500. // setup animators
  501. var animations = function animations(_ref) {
  502. var mixinConfig = _ref.mixinConfig,
  503. viewProps = _ref.viewProps,
  504. viewInternalAPI = _ref.viewInternalAPI,
  505. viewExternalAPI = _ref.viewExternalAPI;
  506. // initial properties
  507. var initialProps = Object.assign({}, viewProps);
  508. // list of all active animations
  509. var animations = [];
  510. // setup animators
  511. forin(mixinConfig, function(property, animation) {
  512. var animator = createAnimator(animation);
  513. if (!animator) {
  514. return;
  515. }
  516. // when the animator updates, update the view state value
  517. animator.onupdate = function(value) {
  518. viewProps[property] = value;
  519. };
  520. // set animator target
  521. animator.target = initialProps[property];
  522. // when value is set, set the animator target value
  523. var prop = {
  524. key: property,
  525. setter: function setter(value) {
  526. // if already at target, we done!
  527. if (animator.target === value) {
  528. return;
  529. }
  530. animator.target = value;
  531. },
  532. getter: function getter() {
  533. return viewProps[property];
  534. },
  535. };
  536. // add getters and setters
  537. addGetSet([prop], [viewInternalAPI, viewExternalAPI], viewProps, true);
  538. // add it to the list for easy updating from the _write method
  539. animations.push(animator);
  540. });
  541. // expose internal write api
  542. return {
  543. write: function write(ts) {
  544. var skipToEndState = document.hidden;
  545. var resting = true;
  546. animations.forEach(function(animation) {
  547. if (!animation.resting) resting = false;
  548. animation.interpolate(ts, skipToEndState);
  549. });
  550. return resting;
  551. },
  552. destroy: function destroy() {},
  553. };
  554. };
  555. var addEvent = function addEvent(element) {
  556. return function(type, fn) {
  557. element.addEventListener(type, fn);
  558. };
  559. };
  560. var removeEvent = function removeEvent(element) {
  561. return function(type, fn) {
  562. element.removeEventListener(type, fn);
  563. };
  564. };
  565. // mixin
  566. var listeners = function listeners(_ref) {
  567. var mixinConfig = _ref.mixinConfig,
  568. viewProps = _ref.viewProps,
  569. viewInternalAPI = _ref.viewInternalAPI,
  570. viewExternalAPI = _ref.viewExternalAPI,
  571. viewState = _ref.viewState,
  572. view = _ref.view;
  573. var events = [];
  574. var add = addEvent(view.element);
  575. var remove = removeEvent(view.element);
  576. viewExternalAPI.on = function(type, fn) {
  577. events.push({
  578. type: type,
  579. fn: fn,
  580. });
  581. add(type, fn);
  582. };
  583. viewExternalAPI.off = function(type, fn) {
  584. events.splice(
  585. events.findIndex(function(event) {
  586. return event.type === type && event.fn === fn;
  587. }),
  588. 1
  589. );
  590. remove(type, fn);
  591. };
  592. return {
  593. write: function write() {
  594. // not busy
  595. return true;
  596. },
  597. destroy: function destroy() {
  598. events.forEach(function(event) {
  599. remove(event.type, event.fn);
  600. });
  601. },
  602. };
  603. };
  604. // add to external api and link to props
  605. var apis = function apis(_ref) {
  606. var mixinConfig = _ref.mixinConfig,
  607. viewProps = _ref.viewProps,
  608. viewExternalAPI = _ref.viewExternalAPI;
  609. addGetSet(mixinConfig, viewExternalAPI, viewProps);
  610. };
  611. var isDefined = function isDefined(value) {
  612. return value != null;
  613. };
  614. // add to state,
  615. // add getters and setters to internal and external api (if not set)
  616. // set initial state based on props in viewProps
  617. // apply as transforms each frame
  618. var defaults = {
  619. opacity: 1,
  620. scaleX: 1,
  621. scaleY: 1,
  622. translateX: 0,
  623. translateY: 0,
  624. rotateX: 0,
  625. rotateY: 0,
  626. rotateZ: 0,
  627. originX: 0,
  628. originY: 0,
  629. };
  630. var styles = function styles(_ref) {
  631. var mixinConfig = _ref.mixinConfig,
  632. viewProps = _ref.viewProps,
  633. viewInternalAPI = _ref.viewInternalAPI,
  634. viewExternalAPI = _ref.viewExternalAPI,
  635. view = _ref.view;
  636. // initial props
  637. var initialProps = Object.assign({}, viewProps);
  638. // current props
  639. var currentProps = {};
  640. // we will add those properties to the external API and link them to the viewState
  641. addGetSet(mixinConfig, [viewInternalAPI, viewExternalAPI], viewProps);
  642. // override rect on internal and external rect getter so it takes in account transforms
  643. var getOffset = function getOffset() {
  644. return [viewProps['translateX'] || 0, viewProps['translateY'] || 0];
  645. };
  646. var getScale = function getScale() {
  647. return [viewProps['scaleX'] || 0, viewProps['scaleY'] || 0];
  648. };
  649. var getRect = function getRect() {
  650. return view.rect
  651. ? getViewRect(view.rect, view.childViews, getOffset(), getScale())
  652. : null;
  653. };
  654. viewInternalAPI.rect = { get: getRect };
  655. viewExternalAPI.rect = { get: getRect };
  656. // apply view props
  657. mixinConfig.forEach(function(key) {
  658. viewProps[key] =
  659. typeof initialProps[key] === 'undefined' ? defaults[key] : initialProps[key];
  660. });
  661. // expose api
  662. return {
  663. write: function write() {
  664. // see if props have changed
  665. if (!propsHaveChanged(currentProps, viewProps)) {
  666. return;
  667. }
  668. // moves element to correct position on screen
  669. applyStyles(view.element, viewProps);
  670. // store new transforms
  671. Object.assign(currentProps, Object.assign({}, viewProps));
  672. // no longer busy
  673. return true;
  674. },
  675. destroy: function destroy() {},
  676. };
  677. };
  678. var propsHaveChanged = function propsHaveChanged(currentProps, newProps) {
  679. // different amount of keys
  680. if (Object.keys(currentProps).length !== Object.keys(newProps).length) {
  681. return true;
  682. }
  683. // lets analyze the individual props
  684. for (var prop in newProps) {
  685. if (newProps[prop] !== currentProps[prop]) {
  686. return true;
  687. }
  688. }
  689. return false;
  690. };
  691. var applyStyles = function applyStyles(element, _ref2) {
  692. var opacity = _ref2.opacity,
  693. perspective = _ref2.perspective,
  694. translateX = _ref2.translateX,
  695. translateY = _ref2.translateY,
  696. scaleX = _ref2.scaleX,
  697. scaleY = _ref2.scaleY,
  698. rotateX = _ref2.rotateX,
  699. rotateY = _ref2.rotateY,
  700. rotateZ = _ref2.rotateZ,
  701. originX = _ref2.originX,
  702. originY = _ref2.originY,
  703. width = _ref2.width,
  704. height = _ref2.height;
  705. var transforms = '';
  706. var styles = '';
  707. // handle transform origin
  708. if (isDefined(originX) || isDefined(originY)) {
  709. styles += 'transform-origin: ' + (originX || 0) + 'px ' + (originY || 0) + 'px;';
  710. }
  711. // transform order is relevant
  712. // 0. perspective
  713. if (isDefined(perspective)) {
  714. transforms += 'perspective(' + perspective + 'px) ';
  715. }
  716. // 1. translate
  717. if (isDefined(translateX) || isDefined(translateY)) {
  718. transforms +=
  719. 'translate3d(' + (translateX || 0) + 'px, ' + (translateY || 0) + 'px, 0) ';
  720. }
  721. // 2. scale
  722. if (isDefined(scaleX) || isDefined(scaleY)) {
  723. transforms +=
  724. 'scale3d(' +
  725. (isDefined(scaleX) ? scaleX : 1) +
  726. ', ' +
  727. (isDefined(scaleY) ? scaleY : 1) +
  728. ', 1) ';
  729. }
  730. // 3. rotate
  731. if (isDefined(rotateZ)) {
  732. transforms += 'rotateZ(' + rotateZ + 'rad) ';
  733. }
  734. if (isDefined(rotateX)) {
  735. transforms += 'rotateX(' + rotateX + 'rad) ';
  736. }
  737. if (isDefined(rotateY)) {
  738. transforms += 'rotateY(' + rotateY + 'rad) ';
  739. }
  740. // add transforms
  741. if (transforms.length) {
  742. styles += 'transform:' + transforms + ';';
  743. }
  744. // add opacity
  745. if (isDefined(opacity)) {
  746. styles += 'opacity:' + opacity + ';';
  747. // if we reach zero, we make the element inaccessible
  748. if (opacity === 0) {
  749. styles += 'visibility:hidden;';
  750. }
  751. // if we're below 100% opacity this element can't be clicked
  752. if (opacity < 1) {
  753. styles += 'pointer-events:none;';
  754. }
  755. }
  756. // add height
  757. if (isDefined(height)) {
  758. styles += 'height:' + height + 'px;';
  759. }
  760. // add width
  761. if (isDefined(width)) {
  762. styles += 'width:' + width + 'px;';
  763. }
  764. // apply styles
  765. var elementCurrentStyle = element.elementCurrentStyle || '';
  766. // if new styles does not match current styles, lets update!
  767. if (styles.length !== elementCurrentStyle.length || styles !== elementCurrentStyle) {
  768. element.style.cssText = styles;
  769. // store current styles so we can compare them to new styles later on
  770. // _not_ getting the style value is faster
  771. element.elementCurrentStyle = styles;
  772. }
  773. };
  774. var Mixins = {
  775. styles: styles,
  776. listeners: listeners,
  777. animations: animations,
  778. apis: apis,
  779. };
  780. var updateRect = function updateRect() {
  781. var rect = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  782. var element = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  783. var style = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  784. if (!element.layoutCalculated) {
  785. rect.paddingTop = parseInt(style.paddingTop, 10) || 0;
  786. rect.marginTop = parseInt(style.marginTop, 10) || 0;
  787. rect.marginRight = parseInt(style.marginRight, 10) || 0;
  788. rect.marginBottom = parseInt(style.marginBottom, 10) || 0;
  789. rect.marginLeft = parseInt(style.marginLeft, 10) || 0;
  790. element.layoutCalculated = true;
  791. }
  792. rect.left = element.offsetLeft || 0;
  793. rect.top = element.offsetTop || 0;
  794. rect.width = element.offsetWidth || 0;
  795. rect.height = element.offsetHeight || 0;
  796. rect.right = rect.left + rect.width;
  797. rect.bottom = rect.top + rect.height;
  798. rect.scrollTop = element.scrollTop;
  799. rect.hidden = element.offsetParent === null;
  800. return rect;
  801. };
  802. var createView =
  803. // default view definition
  804. function createView() {
  805. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  806. _ref$tag = _ref.tag,
  807. tag = _ref$tag === void 0 ? 'div' : _ref$tag,
  808. _ref$name = _ref.name,
  809. name = _ref$name === void 0 ? null : _ref$name,
  810. _ref$attributes = _ref.attributes,
  811. attributes = _ref$attributes === void 0 ? {} : _ref$attributes,
  812. _ref$read = _ref.read,
  813. read = _ref$read === void 0 ? function() {} : _ref$read,
  814. _ref$write = _ref.write,
  815. write = _ref$write === void 0 ? function() {} : _ref$write,
  816. _ref$create = _ref.create,
  817. create = _ref$create === void 0 ? function() {} : _ref$create,
  818. _ref$destroy = _ref.destroy,
  819. destroy = _ref$destroy === void 0 ? function() {} : _ref$destroy,
  820. _ref$filterFrameActio = _ref.filterFrameActionsForChild,
  821. filterFrameActionsForChild =
  822. _ref$filterFrameActio === void 0
  823. ? function(child, actions) {
  824. return actions;
  825. }
  826. : _ref$filterFrameActio,
  827. _ref$didCreateView = _ref.didCreateView,
  828. didCreateView = _ref$didCreateView === void 0 ? function() {} : _ref$didCreateView,
  829. _ref$didWriteView = _ref.didWriteView,
  830. didWriteView = _ref$didWriteView === void 0 ? function() {} : _ref$didWriteView,
  831. _ref$ignoreRect = _ref.ignoreRect,
  832. ignoreRect = _ref$ignoreRect === void 0 ? false : _ref$ignoreRect,
  833. _ref$ignoreRectUpdate = _ref.ignoreRectUpdate,
  834. ignoreRectUpdate = _ref$ignoreRectUpdate === void 0 ? false : _ref$ignoreRectUpdate,
  835. _ref$mixins = _ref.mixins,
  836. mixins = _ref$mixins === void 0 ? [] : _ref$mixins;
  837. return function(
  838. // each view requires reference to store
  839. store
  840. ) {
  841. var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  842. // root element should not be changed
  843. var element = createElement(tag, 'filepond--' + name, attributes);
  844. // style reference should also not be changed
  845. var style = window.getComputedStyle(element, null);
  846. // element rectangle
  847. var rect = updateRect();
  848. var frameRect = null;
  849. // rest state
  850. var isResting = false;
  851. // pretty self explanatory
  852. var childViews = [];
  853. // loaded mixins
  854. var activeMixins = [];
  855. // references to created children
  856. var ref = {};
  857. // state used for each instance
  858. var state = {};
  859. // list of writers that will be called to update this view
  860. var writers = [
  861. write, // default writer
  862. ];
  863. var readers = [
  864. read, // default reader
  865. ];
  866. var destroyers = [
  867. destroy, // default destroy
  868. ];
  869. // core view methods
  870. var getElement = function getElement() {
  871. return element;
  872. };
  873. var getChildViews = function getChildViews() {
  874. return childViews.concat();
  875. };
  876. var getReference = function getReference() {
  877. return ref;
  878. };
  879. var createChildView = function createChildView(store) {
  880. return function(view, props) {
  881. return view(store, props);
  882. };
  883. };
  884. var getRect = function getRect() {
  885. if (frameRect) {
  886. return frameRect;
  887. }
  888. frameRect = getViewRect(rect, childViews, [0, 0], [1, 1]);
  889. return frameRect;
  890. };
  891. var getStyle = function getStyle() {
  892. return style;
  893. };
  894. /**
  895. * Read data from DOM
  896. * @private
  897. */
  898. var _read = function _read() {
  899. frameRect = null;
  900. // read child views
  901. childViews.forEach(function(child) {
  902. return child._read();
  903. });
  904. var shouldUpdate = !(ignoreRectUpdate && rect.width && rect.height);
  905. if (shouldUpdate) {
  906. updateRect(rect, element, style);
  907. }
  908. // readers
  909. var api = { root: internalAPI, props: props, rect: rect };
  910. readers.forEach(function(reader) {
  911. return reader(api);
  912. });
  913. };
  914. /**
  915. * Write data to DOM
  916. * @private
  917. */
  918. var _write = function _write(ts, frameActions, shouldOptimize) {
  919. // if no actions, we assume that the view is resting
  920. var resting = frameActions.length === 0;
  921. // writers
  922. writers.forEach(function(writer) {
  923. var writerResting = writer({
  924. props: props,
  925. root: internalAPI,
  926. actions: frameActions,
  927. timestamp: ts,
  928. shouldOptimize: shouldOptimize,
  929. });
  930. if (writerResting === false) {
  931. resting = false;
  932. }
  933. });
  934. // run mixins
  935. activeMixins.forEach(function(mixin) {
  936. // if one of the mixins is still busy after write operation, we are not resting
  937. var mixinResting = mixin.write(ts);
  938. if (mixinResting === false) {
  939. resting = false;
  940. }
  941. });
  942. // updates child views that are currently attached to the DOM
  943. childViews
  944. .filter(function(child) {
  945. return !!child.element.parentNode;
  946. })
  947. .forEach(function(child) {
  948. // if a child view is not resting, we are not resting
  949. var childResting = child._write(
  950. ts,
  951. filterFrameActionsForChild(child, frameActions),
  952. shouldOptimize
  953. );
  954. if (!childResting) {
  955. resting = false;
  956. }
  957. });
  958. // append new elements to DOM and update those
  959. childViews
  960. //.filter(child => !child.element.parentNode)
  961. .forEach(function(child, index) {
  962. // skip
  963. if (child.element.parentNode) {
  964. return;
  965. }
  966. // append to DOM
  967. internalAPI.appendChild(child.element, index);
  968. // call read (need to know the size of these elements)
  969. child._read();
  970. // re-call write
  971. child._write(
  972. ts,
  973. filterFrameActionsForChild(child, frameActions),
  974. shouldOptimize
  975. );
  976. // we just added somthing to the dom, no rest
  977. resting = false;
  978. });
  979. // update resting state
  980. isResting = resting;
  981. didWriteView({
  982. props: props,
  983. root: internalAPI,
  984. actions: frameActions,
  985. timestamp: ts,
  986. });
  987. // let parent know if we are resting
  988. return resting;
  989. };
  990. var _destroy = function _destroy() {
  991. activeMixins.forEach(function(mixin) {
  992. return mixin.destroy();
  993. });
  994. destroyers.forEach(function(destroyer) {
  995. destroyer({ root: internalAPI, props: props });
  996. });
  997. childViews.forEach(function(child) {
  998. return child._destroy();
  999. });
  1000. };
  1001. // sharedAPI
  1002. var sharedAPIDefinition = {
  1003. element: {
  1004. get: getElement,
  1005. },
  1006. style: {
  1007. get: getStyle,
  1008. },
  1009. childViews: {
  1010. get: getChildViews,
  1011. },
  1012. };
  1013. // private API definition
  1014. var internalAPIDefinition = Object.assign({}, sharedAPIDefinition, {
  1015. rect: {
  1016. get: getRect,
  1017. },
  1018. // access to custom children references
  1019. ref: {
  1020. get: getReference,
  1021. },
  1022. // dom modifiers
  1023. is: function is(needle) {
  1024. return name === needle;
  1025. },
  1026. appendChild: appendChild(element),
  1027. createChildView: createChildView(store),
  1028. linkView: function linkView(view) {
  1029. childViews.push(view);
  1030. return view;
  1031. },
  1032. unlinkView: function unlinkView(view) {
  1033. childViews.splice(childViews.indexOf(view), 1);
  1034. },
  1035. appendChildView: appendChildView(element, childViews),
  1036. removeChildView: removeChildView(element, childViews),
  1037. registerWriter: function registerWriter(writer) {
  1038. return writers.push(writer);
  1039. },
  1040. registerReader: function registerReader(reader) {
  1041. return readers.push(reader);
  1042. },
  1043. registerDestroyer: function registerDestroyer(destroyer) {
  1044. return destroyers.push(destroyer);
  1045. },
  1046. invalidateLayout: function invalidateLayout() {
  1047. return (element.layoutCalculated = false);
  1048. },
  1049. // access to data store
  1050. dispatch: store.dispatch,
  1051. query: store.query,
  1052. });
  1053. // public view API methods
  1054. var externalAPIDefinition = {
  1055. element: {
  1056. get: getElement,
  1057. },
  1058. childViews: {
  1059. get: getChildViews,
  1060. },
  1061. rect: {
  1062. get: getRect,
  1063. },
  1064. resting: {
  1065. get: function get() {
  1066. return isResting;
  1067. },
  1068. },
  1069. isRectIgnored: function isRectIgnored() {
  1070. return ignoreRect;
  1071. },
  1072. _read: _read,
  1073. _write: _write,
  1074. _destroy: _destroy,
  1075. };
  1076. // mixin API methods
  1077. var mixinAPIDefinition = Object.assign({}, sharedAPIDefinition, {
  1078. rect: {
  1079. get: function get() {
  1080. return rect;
  1081. },
  1082. },
  1083. });
  1084. // add mixin functionality
  1085. Object.keys(mixins)
  1086. .sort(function(a, b) {
  1087. // move styles to the back of the mixin list (so adjustments of other mixins are applied to the props correctly)
  1088. if (a === 'styles') {
  1089. return 1;
  1090. } else if (b === 'styles') {
  1091. return -1;
  1092. }
  1093. return 0;
  1094. })
  1095. .forEach(function(key) {
  1096. var mixinAPI = Mixins[key]({
  1097. mixinConfig: mixins[key],
  1098. viewProps: props,
  1099. viewState: state,
  1100. viewInternalAPI: internalAPIDefinition,
  1101. viewExternalAPI: externalAPIDefinition,
  1102. view: createObject(mixinAPIDefinition),
  1103. });
  1104. if (mixinAPI) {
  1105. activeMixins.push(mixinAPI);
  1106. }
  1107. });
  1108. // construct private api
  1109. var internalAPI = createObject(internalAPIDefinition);
  1110. // create the view
  1111. create({
  1112. root: internalAPI,
  1113. props: props,
  1114. });
  1115. // append created child views to root node
  1116. var childCount = getChildCount(element); // need to know the current child count so appending happens in correct order
  1117. childViews.forEach(function(child, index) {
  1118. internalAPI.appendChild(child.element, childCount + index);
  1119. });
  1120. // call did create
  1121. didCreateView(internalAPI);
  1122. // expose public api
  1123. return createObject(externalAPIDefinition);
  1124. };
  1125. };
  1126. var createPainter = function createPainter(read, write) {
  1127. var fps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 60;
  1128. var name = '__framePainter';
  1129. // set global painter
  1130. if (window[name]) {
  1131. window[name].readers.push(read);
  1132. window[name].writers.push(write);
  1133. return;
  1134. }
  1135. window[name] = {
  1136. readers: [read],
  1137. writers: [write],
  1138. };
  1139. var painter = window[name];
  1140. var interval = 1000 / fps;
  1141. var last = null;
  1142. var id = null;
  1143. var requestTick = null;
  1144. var cancelTick = null;
  1145. var setTimerType = function setTimerType() {
  1146. if (document.hidden) {
  1147. requestTick = function requestTick() {
  1148. return window.setTimeout(function() {
  1149. return tick(performance.now());
  1150. }, interval);
  1151. };
  1152. cancelTick = function cancelTick() {
  1153. return window.clearTimeout(id);
  1154. };
  1155. } else {
  1156. requestTick = function requestTick() {
  1157. return window.requestAnimationFrame(tick);
  1158. };
  1159. cancelTick = function cancelTick() {
  1160. return window.cancelAnimationFrame(id);
  1161. };
  1162. }
  1163. };
  1164. document.addEventListener('visibilitychange', function() {
  1165. if (cancelTick) cancelTick();
  1166. setTimerType();
  1167. tick(performance.now());
  1168. });
  1169. var tick = function tick(ts) {
  1170. // queue next tick
  1171. id = requestTick(tick);
  1172. // limit fps
  1173. if (!last) {
  1174. last = ts;
  1175. }
  1176. var delta = ts - last;
  1177. if (delta <= interval) {
  1178. // skip frame
  1179. return;
  1180. }
  1181. // align next frame
  1182. last = ts - (delta % interval);
  1183. // update view
  1184. painter.readers.forEach(function(read) {
  1185. return read();
  1186. });
  1187. painter.writers.forEach(function(write) {
  1188. return write(ts);
  1189. });
  1190. };
  1191. setTimerType();
  1192. tick(performance.now());
  1193. return {
  1194. pause: function pause() {
  1195. cancelTick(id);
  1196. },
  1197. };
  1198. };
  1199. var createRoute = function createRoute(routes, fn) {
  1200. return function(_ref) {
  1201. var root = _ref.root,
  1202. props = _ref.props,
  1203. _ref$actions = _ref.actions,
  1204. actions = _ref$actions === void 0 ? [] : _ref$actions,
  1205. timestamp = _ref.timestamp,
  1206. shouldOptimize = _ref.shouldOptimize;
  1207. actions
  1208. .filter(function(action) {
  1209. return routes[action.type];
  1210. })
  1211. .forEach(function(action) {
  1212. return routes[action.type]({
  1213. root: root,
  1214. props: props,
  1215. action: action.data,
  1216. timestamp: timestamp,
  1217. shouldOptimize: shouldOptimize,
  1218. });
  1219. });
  1220. if (fn) {
  1221. fn({
  1222. root: root,
  1223. props: props,
  1224. actions: actions,
  1225. timestamp: timestamp,
  1226. shouldOptimize: shouldOptimize,
  1227. });
  1228. }
  1229. };
  1230. };
  1231. var insertBefore = function insertBefore(newNode, referenceNode) {
  1232. return referenceNode.parentNode.insertBefore(newNode, referenceNode);
  1233. };
  1234. var insertAfter = function insertAfter(newNode, referenceNode) {
  1235. return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  1236. };
  1237. var isArray = function isArray(value) {
  1238. return Array.isArray(value);
  1239. };
  1240. var isEmpty = function isEmpty(value) {
  1241. return value == null;
  1242. };
  1243. var trim = function trim(str) {
  1244. return str.trim();
  1245. };
  1246. var toString = function toString(value) {
  1247. return '' + value;
  1248. };
  1249. var toArray = function toArray(value) {
  1250. var splitter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';
  1251. if (isEmpty(value)) {
  1252. return [];
  1253. }
  1254. if (isArray(value)) {
  1255. return value;
  1256. }
  1257. return toString(value)
  1258. .split(splitter)
  1259. .map(trim)
  1260. .filter(function(str) {
  1261. return str.length;
  1262. });
  1263. };
  1264. var isBoolean = function isBoolean(value) {
  1265. return typeof value === 'boolean';
  1266. };
  1267. var toBoolean = function toBoolean(value) {
  1268. return isBoolean(value) ? value : value === 'true';
  1269. };
  1270. var isString = function isString(value) {
  1271. return typeof value === 'string';
  1272. };
  1273. var toNumber = function toNumber(value) {
  1274. return isNumber(value)
  1275. ? value
  1276. : isString(value)
  1277. ? toString(value).replace(/[a-z]+/gi, '')
  1278. : 0;
  1279. };
  1280. var toInt = function toInt(value) {
  1281. return parseInt(toNumber(value), 10);
  1282. };
  1283. var toFloat = function toFloat(value) {
  1284. return parseFloat(toNumber(value));
  1285. };
  1286. var isInt = function isInt(value) {
  1287. return isNumber(value) && isFinite(value) && Math.floor(value) === value;
  1288. };
  1289. var toBytes = function toBytes(value) {
  1290. var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
  1291. // is in bytes
  1292. if (isInt(value)) {
  1293. return value;
  1294. }
  1295. // is natural file size
  1296. var naturalFileSize = toString(value).trim();
  1297. // if is value in megabytes
  1298. if (/MB$/i.test(naturalFileSize)) {
  1299. naturalFileSize = naturalFileSize.replace(/MB$i/, '').trim();
  1300. return toInt(naturalFileSize) * base * base;
  1301. }
  1302. // if is value in kilobytes
  1303. if (/KB/i.test(naturalFileSize)) {
  1304. naturalFileSize = naturalFileSize.replace(/KB$i/, '').trim();
  1305. return toInt(naturalFileSize) * base;
  1306. }
  1307. return toInt(naturalFileSize);
  1308. };
  1309. var isFunction = function isFunction(value) {
  1310. return typeof value === 'function';
  1311. };
  1312. var toFunctionReference = function toFunctionReference(string) {
  1313. var ref = self;
  1314. var levels = string.split('.');
  1315. var level = null;
  1316. while ((level = levels.shift())) {
  1317. ref = ref[level];
  1318. if (!ref) {
  1319. return null;
  1320. }
  1321. }
  1322. return ref;
  1323. };
  1324. var methods = {
  1325. process: 'POST',
  1326. patch: 'PATCH',
  1327. revert: 'DELETE',
  1328. fetch: 'GET',
  1329. restore: 'GET',
  1330. load: 'GET',
  1331. };
  1332. var createServerAPI = function createServerAPI(outline) {
  1333. var api = {};
  1334. api.url = isString(outline) ? outline : outline.url || '';
  1335. api.timeout = outline.timeout ? parseInt(outline.timeout, 10) : 0;
  1336. api.headers = outline.headers ? outline.headers : {};
  1337. forin(methods, function(key) {
  1338. api[key] = createAction(key, outline[key], methods[key], api.timeout, api.headers);
  1339. });
  1340. // remove process if no url or process on outline
  1341. api.process = outline.process || isString(outline) || outline.url ? api.process : null;
  1342. // special treatment for remove
  1343. api.remove = outline.remove || null;
  1344. // remove generic headers from api object
  1345. delete api.headers;
  1346. return api;
  1347. };
  1348. var createAction = function createAction(name, outline, method, timeout, headers) {
  1349. // is explicitely set to null so disable
  1350. if (outline === null) {
  1351. return null;
  1352. }
  1353. // if is custom function, done! Dev handles everything.
  1354. if (typeof outline === 'function') {
  1355. return outline;
  1356. }
  1357. // build action object
  1358. var action = {
  1359. url: method === 'GET' || method === 'PATCH' ? '?' + name + '=' : '',
  1360. method: method,
  1361. headers: headers,
  1362. withCredentials: false,
  1363. timeout: timeout,
  1364. onload: null,
  1365. ondata: null,
  1366. onerror: null,
  1367. };
  1368. // is a single url
  1369. if (isString(outline)) {
  1370. action.url = outline;
  1371. return action;
  1372. }
  1373. // overwrite
  1374. Object.assign(action, outline);
  1375. // see if should reformat headers;
  1376. if (isString(action.headers)) {
  1377. var parts = action.headers.split(/:(.+)/);
  1378. action.headers = {
  1379. header: parts[0],
  1380. value: parts[1],
  1381. };
  1382. }
  1383. // if is bool withCredentials
  1384. action.withCredentials = toBoolean(action.withCredentials);
  1385. return action;
  1386. };
  1387. var toServerAPI = function toServerAPI(value) {
  1388. return createServerAPI(value);
  1389. };
  1390. var isNull = function isNull(value) {
  1391. return value === null;
  1392. };
  1393. var isObject = function isObject(value) {
  1394. return typeof value === 'object' && value !== null;
  1395. };
  1396. var isAPI = function isAPI(value) {
  1397. return (
  1398. isObject(value) &&
  1399. isString(value.url) &&
  1400. isObject(value.process) &&
  1401. isObject(value.revert) &&
  1402. isObject(value.restore) &&
  1403. isObject(value.fetch)
  1404. );
  1405. };
  1406. var getType = function getType(value) {
  1407. if (isArray(value)) {
  1408. return 'array';
  1409. }
  1410. if (isNull(value)) {
  1411. return 'null';
  1412. }
  1413. if (isInt(value)) {
  1414. return 'int';
  1415. }
  1416. if (/^[0-9]+ ?(?:GB|MB|KB)$/gi.test(value)) {
  1417. return 'bytes';
  1418. }
  1419. if (isAPI(value)) {
  1420. return 'api';
  1421. }
  1422. return typeof value;
  1423. };
  1424. var replaceSingleQuotes = function replaceSingleQuotes(str) {
  1425. return str
  1426. .replace(/{\s*'/g, '{"')
  1427. .replace(/'\s*}/g, '"}')
  1428. .replace(/'\s*:/g, '":')
  1429. .replace(/:\s*'/g, ':"')
  1430. .replace(/,\s*'/g, ',"')
  1431. .replace(/'\s*,/g, '",');
  1432. };
  1433. var conversionTable = {
  1434. array: toArray,
  1435. boolean: toBoolean,
  1436. int: function int(value) {
  1437. return getType(value) === 'bytes' ? toBytes(value) : toInt(value);
  1438. },
  1439. number: toFloat,
  1440. float: toFloat,
  1441. bytes: toBytes,
  1442. string: function string(value) {
  1443. return isFunction(value) ? value : toString(value);
  1444. },
  1445. function: function _function(value) {
  1446. return toFunctionReference(value);
  1447. },
  1448. serverapi: toServerAPI,
  1449. object: function object(value) {
  1450. try {
  1451. return JSON.parse(replaceSingleQuotes(value));
  1452. } catch (e) {
  1453. return null;
  1454. }
  1455. },
  1456. };
  1457. var convertTo = function convertTo(value, type) {
  1458. return conversionTable[type](value);
  1459. };
  1460. var getValueByType = function getValueByType(newValue, defaultValue, valueType) {
  1461. // can always assign default value
  1462. if (newValue === defaultValue) {
  1463. return newValue;
  1464. }
  1465. // get the type of the new value
  1466. var newValueType = getType(newValue);
  1467. // is valid type?
  1468. if (newValueType !== valueType) {
  1469. // is string input, let's attempt to convert
  1470. var convertedValue = convertTo(newValue, valueType);
  1471. // what is the type now
  1472. newValueType = getType(convertedValue);
  1473. // no valid conversions found
  1474. if (convertedValue === null) {
  1475. throw 'Trying to assign value with incorrect type to "' +
  1476. option +
  1477. '", allowed type: "' +
  1478. valueType +
  1479. '"';
  1480. } else {
  1481. newValue = convertedValue;
  1482. }
  1483. }
  1484. // assign new value
  1485. return newValue;
  1486. };
  1487. var createOption = function createOption(defaultValue, valueType) {
  1488. var currentValue = defaultValue;
  1489. return {
  1490. enumerable: true,
  1491. get: function get() {
  1492. return currentValue;
  1493. },
  1494. set: function set(newValue) {
  1495. currentValue = getValueByType(newValue, defaultValue, valueType);
  1496. },
  1497. };
  1498. };
  1499. var createOptions = function createOptions(options) {
  1500. var obj = {};
  1501. forin(options, function(prop) {
  1502. var optionDefinition = options[prop];
  1503. obj[prop] = createOption(optionDefinition[0], optionDefinition[1]);
  1504. });
  1505. return createObject(obj);
  1506. };
  1507. var createInitialState = function createInitialState(options) {
  1508. return {
  1509. // model
  1510. items: [],
  1511. // timeout used for calling update items
  1512. listUpdateTimeout: null,
  1513. // timeout used for stacking metadata updates
  1514. itemUpdateTimeout: null,
  1515. // queue of items waiting to be processed
  1516. processingQueue: [],
  1517. // options
  1518. options: createOptions(options),
  1519. };
  1520. };
  1521. var fromCamels = function fromCamels(string) {
  1522. var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '-';
  1523. return string
  1524. .split(/(?=[A-Z])/)
  1525. .map(function(part) {
  1526. return part.toLowerCase();
  1527. })
  1528. .join(separator);
  1529. };
  1530. var createOptionAPI = function createOptionAPI(store, options) {
  1531. var obj = {};
  1532. forin(options, function(key) {
  1533. obj[key] = {
  1534. get: function get() {
  1535. return store.getState().options[key];
  1536. },
  1537. set: function set(value) {
  1538. store.dispatch('SET_' + fromCamels(key, '_').toUpperCase(), {
  1539. value: value,
  1540. });
  1541. },
  1542. };
  1543. });
  1544. return obj;
  1545. };
  1546. var createOptionActions = function createOptionActions(options) {
  1547. return function(dispatch, query, state) {
  1548. var obj = {};
  1549. forin(options, function(key) {
  1550. var name = fromCamels(key, '_').toUpperCase();
  1551. obj['SET_' + name] = function(action) {
  1552. try {
  1553. state.options[key] = action.value;
  1554. } catch (e) {} // nope, failed
  1555. // we successfully set the value of this option
  1556. dispatch('DID_SET_' + name, { value: state.options[key] });
  1557. };
  1558. });
  1559. return obj;
  1560. };
  1561. };
  1562. var createOptionQueries = function createOptionQueries(options) {
  1563. return function(state) {
  1564. var obj = {};
  1565. forin(options, function(key) {
  1566. obj['GET_' + fromCamels(key, '_').toUpperCase()] = function(action) {
  1567. return state.options[key];
  1568. };
  1569. });
  1570. return obj;
  1571. };
  1572. };
  1573. var InteractionMethod = {
  1574. API: 1,
  1575. DROP: 2,
  1576. BROWSE: 3,
  1577. PASTE: 4,
  1578. NONE: 5,
  1579. };
  1580. var getUniqueId = function getUniqueId() {
  1581. return Math.random()
  1582. .toString(36)
  1583. .substring(2, 11);
  1584. };
  1585. function _typeof(obj) {
  1586. if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
  1587. _typeof = function(obj) {
  1588. return typeof obj;
  1589. };
  1590. } else {
  1591. _typeof = function(obj) {
  1592. return obj &&
  1593. typeof Symbol === 'function' &&
  1594. obj.constructor === Symbol &&
  1595. obj !== Symbol.prototype
  1596. ? 'symbol'
  1597. : typeof obj;
  1598. };
  1599. }
  1600. return _typeof(obj);
  1601. }
  1602. var REACT_ELEMENT_TYPE;
  1603. function _jsx(type, props, key, children) {
  1604. if (!REACT_ELEMENT_TYPE) {
  1605. REACT_ELEMENT_TYPE =
  1606. (typeof Symbol === 'function' && Symbol['for'] && Symbol['for']('react.element')) ||
  1607. 0xeac7;
  1608. }
  1609. var defaultProps = type && type.defaultProps;
  1610. var childrenLength = arguments.length - 3;
  1611. if (!props && childrenLength !== 0) {
  1612. props = {
  1613. children: void 0,
  1614. };
  1615. }
  1616. if (props && defaultProps) {
  1617. for (var propName in defaultProps) {
  1618. if (props[propName] === void 0) {
  1619. props[propName] = defaultProps[propName];
  1620. }
  1621. }
  1622. } else if (!props) {
  1623. props = defaultProps || {};
  1624. }
  1625. if (childrenLength === 1) {
  1626. props.children = children;
  1627. } else if (childrenLength > 1) {
  1628. var childArray = new Array(childrenLength);
  1629. for (var i = 0; i < childrenLength; i++) {
  1630. childArray[i] = arguments[i + 3];
  1631. }
  1632. props.children = childArray;
  1633. }
  1634. return {
  1635. $$typeof: REACT_ELEMENT_TYPE,
  1636. type: type,
  1637. key: key === undefined ? null : '' + key,
  1638. ref: null,
  1639. props: props,
  1640. _owner: null,
  1641. };
  1642. }
  1643. function _asyncIterator(iterable) {
  1644. var method;
  1645. if (typeof Symbol !== 'undefined') {
  1646. if (Symbol.asyncIterator) {
  1647. method = iterable[Symbol.asyncIterator];
  1648. if (method != null) return method.call(iterable);
  1649. }
  1650. if (Symbol.iterator) {
  1651. method = iterable[Symbol.iterator];
  1652. if (method != null) return method.call(iterable);
  1653. }
  1654. }
  1655. throw new TypeError('Object is not async iterable');
  1656. }
  1657. function _AwaitValue(value) {
  1658. this.wrapped = value;
  1659. }
  1660. function _AsyncGenerator(gen) {
  1661. var front, back;
  1662. function send(key, arg) {
  1663. return new Promise(function(resolve, reject) {
  1664. var request = {
  1665. key: key,
  1666. arg: arg,
  1667. resolve: resolve,
  1668. reject: reject,
  1669. next: null,
  1670. };
  1671. if (back) {
  1672. back = back.next = request;
  1673. } else {
  1674. front = back = request;
  1675. resume(key, arg);
  1676. }
  1677. });
  1678. }
  1679. function resume(key, arg) {
  1680. try {
  1681. var result = gen[key](arg);
  1682. var value = result.value;
  1683. var wrappedAwait = value instanceof _AwaitValue;
  1684. Promise.resolve(wrappedAwait ? value.wrapped : value).then(
  1685. function(arg) {
  1686. if (wrappedAwait) {
  1687. resume('next', arg);
  1688. return;
  1689. }
  1690. settle(result.done ? 'return' : 'normal', arg);
  1691. },
  1692. function(err) {
  1693. resume('throw', err);
  1694. }
  1695. );
  1696. } catch (err) {
  1697. settle('throw', err);
  1698. }
  1699. }
  1700. function settle(type, value) {
  1701. switch (type) {
  1702. case 'return':
  1703. front.resolve({
  1704. value: value,
  1705. done: true,
  1706. });
  1707. break;
  1708. case 'throw':
  1709. front.reject(value);
  1710. break;
  1711. default:
  1712. front.resolve({
  1713. value: value,
  1714. done: false,
  1715. });
  1716. break;
  1717. }
  1718. front = front.next;
  1719. if (front) {
  1720. resume(front.key, front.arg);
  1721. } else {
  1722. back = null;
  1723. }
  1724. }
  1725. this._invoke = send;
  1726. if (typeof gen.return !== 'function') {
  1727. this.return = undefined;
  1728. }
  1729. }
  1730. if (typeof Symbol === 'function' && Symbol.asyncIterator) {
  1731. _AsyncGenerator.prototype[Symbol.asyncIterator] = function() {
  1732. return this;
  1733. };
  1734. }
  1735. _AsyncGenerator.prototype.next = function(arg) {
  1736. return this._invoke('next', arg);
  1737. };
  1738. _AsyncGenerator.prototype.throw = function(arg) {
  1739. return this._invoke('throw', arg);
  1740. };
  1741. _AsyncGenerator.prototype.return = function(arg) {
  1742. return this._invoke('return', arg);
  1743. };
  1744. function _wrapAsyncGenerator(fn) {
  1745. return function() {
  1746. return new _AsyncGenerator(fn.apply(this, arguments));
  1747. };
  1748. }
  1749. function _awaitAsyncGenerator(value) {
  1750. return new _AwaitValue(value);
  1751. }
  1752. function _asyncGeneratorDelegate(inner, awaitWrap) {
  1753. var iter = {},
  1754. waiting = false;
  1755. function pump(key, value) {
  1756. waiting = true;
  1757. value = new Promise(function(resolve) {
  1758. resolve(inner[key](value));
  1759. });
  1760. return {
  1761. done: false,
  1762. value: awaitWrap(value),
  1763. };
  1764. }
  1765. if (typeof Symbol === 'function' && Symbol.iterator) {
  1766. iter[Symbol.iterator] = function() {
  1767. return this;
  1768. };
  1769. }
  1770. iter.next = function(value) {
  1771. if (waiting) {
  1772. waiting = false;
  1773. return value;
  1774. }
  1775. return pump('next', value);
  1776. };
  1777. if (typeof inner.throw === 'function') {
  1778. iter.throw = function(value) {
  1779. if (waiting) {
  1780. waiting = false;
  1781. throw value;
  1782. }
  1783. return pump('throw', value);
  1784. };
  1785. }
  1786. if (typeof inner.return === 'function') {
  1787. iter.return = function(value) {
  1788. return pump('return', value);
  1789. };
  1790. }
  1791. return iter;
  1792. }
  1793. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  1794. try {
  1795. var info = gen[key](arg);
  1796. var value = info.value;
  1797. } catch (error) {
  1798. reject(error);
  1799. return;
  1800. }
  1801. if (info.done) {
  1802. resolve(value);
  1803. } else {
  1804. Promise.resolve(value).then(_next, _throw);
  1805. }
  1806. }
  1807. function _asyncToGenerator(fn) {
  1808. return function() {
  1809. var self = this,
  1810. args = arguments;
  1811. return new Promise(function(resolve, reject) {
  1812. var gen = fn.apply(self, args);
  1813. function _next(value) {
  1814. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'next', value);
  1815. }
  1816. function _throw(err) {
  1817. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'throw', err);
  1818. }
  1819. _next(undefined);
  1820. });
  1821. };
  1822. }
  1823. function _classCallCheck(instance, Constructor) {
  1824. if (!(instance instanceof Constructor)) {
  1825. throw new TypeError('Cannot call a class as a function');
  1826. }
  1827. }
  1828. function _defineProperties(target, props) {
  1829. for (var i = 0; i < props.length; i++) {
  1830. var descriptor = props[i];
  1831. descriptor.enumerable = descriptor.enumerable || false;
  1832. descriptor.configurable = true;
  1833. if ('value' in descriptor) descriptor.writable = true;
  1834. Object.defineProperty(target, descriptor.key, descriptor);
  1835. }
  1836. }
  1837. function _createClass(Constructor, protoProps, staticProps) {
  1838. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  1839. if (staticProps) _defineProperties(Constructor, staticProps);
  1840. return Constructor;
  1841. }
  1842. function _defineEnumerableProperties(obj, descs) {
  1843. for (var key in descs) {
  1844. var desc = descs[key];
  1845. desc.configurable = desc.enumerable = true;
  1846. if ('value' in desc) desc.writable = true;
  1847. Object.defineProperty(obj, key, desc);
  1848. }
  1849. if (Object.getOwnPropertySymbols) {
  1850. var objectSymbols = Object.getOwnPropertySymbols(descs);
  1851. for (var i = 0; i < objectSymbols.length; i++) {
  1852. var sym = objectSymbols[i];
  1853. var desc = descs[sym];
  1854. desc.configurable = desc.enumerable = true;
  1855. if ('value' in desc) desc.writable = true;
  1856. Object.defineProperty(obj, sym, desc);
  1857. }
  1858. }
  1859. return obj;
  1860. }
  1861. function _defaults(obj, defaults) {
  1862. var keys = Object.getOwnPropertyNames(defaults);
  1863. for (var i = 0; i < keys.length; i++) {
  1864. var key = keys[i];
  1865. var value = Object.getOwnPropertyDescriptor(defaults, key);
  1866. if (value && value.configurable && obj[key] === undefined) {
  1867. Object.defineProperty(obj, key, value);
  1868. }
  1869. }
  1870. return obj;
  1871. }
  1872. function _defineProperty(obj, key, value) {
  1873. if (key in obj) {
  1874. Object.defineProperty(obj, key, {
  1875. value: value,
  1876. enumerable: true,
  1877. configurable: true,
  1878. writable: true,
  1879. });
  1880. } else {
  1881. obj[key] = value;
  1882. }
  1883. return obj;
  1884. }
  1885. function _extends() {
  1886. _extends =
  1887. Object.assign ||
  1888. function(target) {
  1889. for (var i = 1; i < arguments.length; i++) {
  1890. var source = arguments[i];
  1891. for (var key in source) {
  1892. if (Object.prototype.hasOwnProperty.call(source, key)) {
  1893. target[key] = source[key];
  1894. }
  1895. }
  1896. }
  1897. return target;
  1898. };
  1899. return _extends.apply(this, arguments);
  1900. }
  1901. function _objectSpread(target) {
  1902. for (var i = 1; i < arguments.length; i++) {
  1903. var source = arguments[i] != null ? arguments[i] : {};
  1904. var ownKeys = Object.keys(source);
  1905. if (typeof Object.getOwnPropertySymbols === 'function') {
  1906. ownKeys = ownKeys.concat(
  1907. Object.getOwnPropertySymbols(source).filter(function(sym) {
  1908. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  1909. })
  1910. );
  1911. }
  1912. ownKeys.forEach(function(key) {
  1913. _defineProperty(target, key, source[key]);
  1914. });
  1915. }
  1916. return target;
  1917. }
  1918. function ownKeys(object, enumerableOnly) {
  1919. var keys = Object.keys(object);
  1920. if (Object.getOwnPropertySymbols) {
  1921. var symbols = Object.getOwnPropertySymbols(object);
  1922. if (enumerableOnly)
  1923. symbols = symbols.filter(function(sym) {
  1924. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  1925. });
  1926. keys.push.apply(keys, symbols);
  1927. }
  1928. return keys;
  1929. }
  1930. function _objectSpread2(target) {
  1931. for (var i = 1; i < arguments.length; i++) {
  1932. var source = arguments[i] != null ? arguments[i] : {};
  1933. if (i % 2) {
  1934. ownKeys(source, true).forEach(function(key) {
  1935. _defineProperty(target, key, source[key]);
  1936. });
  1937. } else if (Object.getOwnPropertyDescriptors) {
  1938. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  1939. } else {
  1940. ownKeys(source).forEach(function(key) {
  1941. Object.defineProperty(
  1942. target,
  1943. key,
  1944. Object.getOwnPropertyDescriptor(source, key)
  1945. );
  1946. });
  1947. }
  1948. }
  1949. return target;
  1950. }
  1951. function _inherits(subClass, superClass) {
  1952. if (typeof superClass !== 'function' && superClass !== null) {
  1953. throw new TypeError('Super expression must either be null or a function');
  1954. }
  1955. subClass.prototype = Object.create(superClass && superClass.prototype, {
  1956. constructor: {
  1957. value: subClass,
  1958. writable: true,
  1959. configurable: true,
  1960. },
  1961. });
  1962. if (superClass) _setPrototypeOf(subClass, superClass);
  1963. }
  1964. function _inheritsLoose(subClass, superClass) {
  1965. subClass.prototype = Object.create(superClass.prototype);
  1966. subClass.prototype.constructor = subClass;
  1967. subClass.__proto__ = superClass;
  1968. }
  1969. function _getPrototypeOf(o) {
  1970. _getPrototypeOf = Object.setPrototypeOf
  1971. ? Object.getPrototypeOf
  1972. : function _getPrototypeOf(o) {
  1973. return o.__proto__ || Object.getPrototypeOf(o);
  1974. };
  1975. return _getPrototypeOf(o);
  1976. }
  1977. function _setPrototypeOf(o, p) {
  1978. _setPrototypeOf =
  1979. Object.setPrototypeOf ||
  1980. function _setPrototypeOf(o, p) {
  1981. o.__proto__ = p;
  1982. return o;
  1983. };
  1984. return _setPrototypeOf(o, p);
  1985. }
  1986. function isNativeReflectConstruct() {
  1987. if (typeof Reflect === 'undefined' || !Reflect.construct) return false;
  1988. if (Reflect.construct.sham) return false;
  1989. if (typeof Proxy === 'function') return true;
  1990. try {
  1991. Date.prototype.toString.call(Reflect.construct(Date, [], function() {}));
  1992. return true;
  1993. } catch (e) {
  1994. return false;
  1995. }
  1996. }
  1997. function _construct(Parent, args, Class) {
  1998. if (isNativeReflectConstruct()) {
  1999. _construct = Reflect.construct;
  2000. } else {
  2001. _construct = function _construct(Parent, args, Class) {
  2002. var a = [null];
  2003. a.push.apply(a, args);
  2004. var Constructor = Function.bind.apply(Parent, a);
  2005. var instance = new Constructor();
  2006. if (Class) _setPrototypeOf(instance, Class.prototype);
  2007. return instance;
  2008. };
  2009. }
  2010. return _construct.apply(null, arguments);
  2011. }
  2012. function _isNativeFunction(fn) {
  2013. return Function.toString.call(fn).indexOf('[native code]') !== -1;
  2014. }
  2015. function _wrapNativeSuper(Class) {
  2016. var _cache = typeof Map === 'function' ? new Map() : undefined;
  2017. _wrapNativeSuper = function _wrapNativeSuper(Class) {
  2018. if (Class === null || !_isNativeFunction(Class)) return Class;
  2019. if (typeof Class !== 'function') {
  2020. throw new TypeError('Super expression must either be null or a function');
  2021. }
  2022. if (typeof _cache !== 'undefined') {
  2023. if (_cache.has(Class)) return _cache.get(Class);
  2024. _cache.set(Class, Wrapper);
  2025. }
  2026. function Wrapper() {
  2027. return _construct(Class, arguments, _getPrototypeOf(this).constructor);
  2028. }
  2029. Wrapper.prototype = Object.create(Class.prototype, {
  2030. constructor: {
  2031. value: Wrapper,
  2032. enumerable: false,
  2033. writable: true,
  2034. configurable: true,
  2035. },
  2036. });
  2037. return _setPrototypeOf(Wrapper, Class);
  2038. };
  2039. return _wrapNativeSuper(Class);
  2040. }
  2041. function _instanceof(left, right) {
  2042. if (right != null && typeof Symbol !== 'undefined' && right[Symbol.hasInstance]) {
  2043. return !!right[Symbol.hasInstance](left);
  2044. } else {
  2045. return left instanceof right;
  2046. }
  2047. }
  2048. function _interopRequireDefault(obj) {
  2049. return obj && obj.__esModule
  2050. ? obj
  2051. : {
  2052. default: obj,
  2053. };
  2054. }
  2055. function _interopRequireWildcard(obj) {
  2056. if (obj && obj.__esModule) {
  2057. return obj;
  2058. } else {
  2059. var newObj = {};
  2060. if (obj != null) {
  2061. for (var key in obj) {
  2062. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  2063. var desc =
  2064. Object.defineProperty && Object.getOwnPropertyDescriptor
  2065. ? Object.getOwnPropertyDescriptor(obj, key)
  2066. : {};
  2067. if (desc.get || desc.set) {
  2068. Object.defineProperty(newObj, key, desc);
  2069. } else {
  2070. newObj[key] = obj[key];
  2071. }
  2072. }
  2073. }
  2074. }
  2075. newObj.default = obj;
  2076. return newObj;
  2077. }
  2078. }
  2079. function _newArrowCheck(innerThis, boundThis) {
  2080. if (innerThis !== boundThis) {
  2081. throw new TypeError('Cannot instantiate an arrow function');
  2082. }
  2083. }
  2084. function _objectDestructuringEmpty(obj) {
  2085. if (obj == null) throw new TypeError('Cannot destructure undefined');
  2086. }
  2087. function _objectWithoutPropertiesLoose(source, excluded) {
  2088. if (source == null) return {};
  2089. var target = {};
  2090. var sourceKeys = Object.keys(source);
  2091. var key, i;
  2092. for (i = 0; i < sourceKeys.length; i++) {
  2093. key = sourceKeys[i];
  2094. if (excluded.indexOf(key) >= 0) continue;
  2095. target[key] = source[key];
  2096. }
  2097. return target;
  2098. }
  2099. function _objectWithoutProperties(source, excluded) {
  2100. if (source == null) return {};
  2101. var target = _objectWithoutPropertiesLoose(source, excluded);
  2102. var key, i;
  2103. if (Object.getOwnPropertySymbols) {
  2104. var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
  2105. for (i = 0; i < sourceSymbolKeys.length; i++) {
  2106. key = sourceSymbolKeys[i];
  2107. if (excluded.indexOf(key) >= 0) continue;
  2108. if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
  2109. target[key] = source[key];
  2110. }
  2111. }
  2112. return target;
  2113. }
  2114. function _assertThisInitialized(self) {
  2115. if (self === void 0) {
  2116. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  2117. }
  2118. return self;
  2119. }
  2120. function _possibleConstructorReturn(self, call) {
  2121. if (call && (typeof call === 'object' || typeof call === 'function')) {
  2122. return call;
  2123. }
  2124. return _assertThisInitialized(self);
  2125. }
  2126. function _superPropBase(object, property) {
  2127. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  2128. object = _getPrototypeOf(object);
  2129. if (object === null) break;
  2130. }
  2131. return object;
  2132. }
  2133. function _get(target, property, receiver) {
  2134. if (typeof Reflect !== 'undefined' && Reflect.get) {
  2135. _get = Reflect.get;
  2136. } else {
  2137. _get = function _get(target, property, receiver) {
  2138. var base = _superPropBase(target, property);
  2139. if (!base) return;
  2140. var desc = Object.getOwnPropertyDescriptor(base, property);
  2141. if (desc.get) {
  2142. return desc.get.call(receiver);
  2143. }
  2144. return desc.value;
  2145. };
  2146. }
  2147. return _get(target, property, receiver || target);
  2148. }
  2149. function set(target, property, value, receiver) {
  2150. if (typeof Reflect !== 'undefined' && Reflect.set) {
  2151. set = Reflect.set;
  2152. } else {
  2153. set = function set(target, property, value, receiver) {
  2154. var base = _superPropBase(target, property);
  2155. var desc;
  2156. if (base) {
  2157. desc = Object.getOwnPropertyDescriptor(base, property);
  2158. if (desc.set) {
  2159. desc.set.call(receiver, value);
  2160. return true;
  2161. } else if (!desc.writable) {
  2162. return false;
  2163. }
  2164. }
  2165. desc = Object.getOwnPropertyDescriptor(receiver, property);
  2166. if (desc) {
  2167. if (!desc.writable) {
  2168. return false;
  2169. }
  2170. desc.value = value;
  2171. Object.defineProperty(receiver, property, desc);
  2172. } else {
  2173. _defineProperty(receiver, property, value);
  2174. }
  2175. return true;
  2176. };
  2177. }
  2178. return set(target, property, value, receiver);
  2179. }
  2180. function _set(target, property, value, receiver, isStrict) {
  2181. var s = set(target, property, value, receiver || target);
  2182. if (!s && isStrict) {
  2183. throw new Error('failed to set property');
  2184. }
  2185. return value;
  2186. }
  2187. function _taggedTemplateLiteral(strings, raw) {
  2188. if (!raw) {
  2189. raw = strings.slice(0);
  2190. }
  2191. return Object.freeze(
  2192. Object.defineProperties(strings, {
  2193. raw: {
  2194. value: Object.freeze(raw),
  2195. },
  2196. })
  2197. );
  2198. }
  2199. function _taggedTemplateLiteralLoose(strings, raw) {
  2200. if (!raw) {
  2201. raw = strings.slice(0);
  2202. }
  2203. strings.raw = raw;
  2204. return strings;
  2205. }
  2206. function _temporalRef(val, name) {
  2207. if (val === _temporalUndefined) {
  2208. throw new ReferenceError(name + ' is not defined - temporal dead zone');
  2209. } else {
  2210. return val;
  2211. }
  2212. }
  2213. function _readOnlyError(name) {
  2214. throw new Error('"' + name + '" is read-only');
  2215. }
  2216. function _classNameTDZError(name) {
  2217. throw new Error('Class "' + name + '" cannot be referenced in computed property keys.');
  2218. }
  2219. var _temporalUndefined = {};
  2220. function _slicedToArray(arr, i) {
  2221. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
  2222. }
  2223. function _slicedToArrayLoose(arr, i) {
  2224. return _arrayWithHoles(arr) || _iterableToArrayLimitLoose(arr, i) || _nonIterableRest();
  2225. }
  2226. function _toArray(arr) {
  2227. return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest();
  2228. }
  2229. function _toConsumableArray(arr) {
  2230. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
  2231. }
  2232. function _arrayWithoutHoles(arr) {
  2233. if (Array.isArray(arr)) {
  2234. for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  2235. return arr2;
  2236. }
  2237. }
  2238. function _arrayWithHoles(arr) {
  2239. if (Array.isArray(arr)) return arr;
  2240. }
  2241. function _iterableToArray(iter) {
  2242. if (
  2243. Symbol.iterator in Object(iter) ||
  2244. Object.prototype.toString.call(iter) === '[object Arguments]'
  2245. )
  2246. return Array.from(iter);
  2247. }
  2248. function _iterableToArrayLimit(arr, i) {
  2249. var _arr = [];
  2250. var _n = true;
  2251. var _d = false;
  2252. var _e = undefined;
  2253. try {
  2254. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  2255. _arr.push(_s.value);
  2256. if (i && _arr.length === i) break;
  2257. }
  2258. } catch (err) {
  2259. _d = true;
  2260. _e = err;
  2261. } finally {
  2262. try {
  2263. if (!_n && _i['return'] != null) _i['return']();
  2264. } finally {
  2265. if (_d) throw _e;
  2266. }
  2267. }
  2268. return _arr;
  2269. }
  2270. function _iterableToArrayLimitLoose(arr, i) {
  2271. var _arr = [];
  2272. for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done; ) {
  2273. _arr.push(_step.value);
  2274. if (i && _arr.length === i) break;
  2275. }
  2276. return _arr;
  2277. }
  2278. function _nonIterableSpread() {
  2279. throw new TypeError('Invalid attempt to spread non-iterable instance');
  2280. }
  2281. function _nonIterableRest() {
  2282. throw new TypeError('Invalid attempt to destructure non-iterable instance');
  2283. }
  2284. function _skipFirstGeneratorNext(fn) {
  2285. return function() {
  2286. var it = fn.apply(this, arguments);
  2287. it.next();
  2288. return it;
  2289. };
  2290. }
  2291. function _toPrimitive(input, hint) {
  2292. if (typeof input !== 'object' || input === null) return input;
  2293. var prim = input[Symbol.toPrimitive];
  2294. if (prim !== undefined) {
  2295. var res = prim.call(input, hint || 'default');
  2296. if (typeof res !== 'object') return res;
  2297. throw new TypeError('@@toPrimitive must return a primitive value.');
  2298. }
  2299. return (hint === 'string' ? String : Number)(input);
  2300. }
  2301. function _toPropertyKey(arg) {
  2302. var key = _toPrimitive(arg, 'string');
  2303. return typeof key === 'symbol' ? key : String(key);
  2304. }
  2305. function _initializerWarningHelper(descriptor, context) {
  2306. throw new Error(
  2307. 'Decorating class property failed. Please ensure that ' +
  2308. 'proposal-class-properties is enabled and set to use loose mode. ' +
  2309. 'To use proposal-class-properties in spec mode with decorators, wait for ' +
  2310. 'the next major version of decorators in stage 2.'
  2311. );
  2312. }
  2313. function _initializerDefineProperty(target, property, descriptor, context) {
  2314. if (!descriptor) return;
  2315. Object.defineProperty(target, property, {
  2316. enumerable: descriptor.enumerable,
  2317. configurable: descriptor.configurable,
  2318. writable: descriptor.writable,
  2319. value: descriptor.initializer ? descriptor.initializer.call(context) : void 0,
  2320. });
  2321. }
  2322. function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  2323. var desc = {};
  2324. Object.keys(descriptor).forEach(function(key) {
  2325. desc[key] = descriptor[key];
  2326. });
  2327. desc.enumerable = !!desc.enumerable;
  2328. desc.configurable = !!desc.configurable;
  2329. if ('value' in desc || desc.initializer) {
  2330. desc.writable = true;
  2331. }
  2332. desc = decorators
  2333. .slice()
  2334. .reverse()
  2335. .reduce(function(desc, decorator) {
  2336. return decorator(target, property, desc) || desc;
  2337. }, desc);
  2338. if (context && desc.initializer !== void 0) {
  2339. desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
  2340. desc.initializer = undefined;
  2341. }
  2342. if (desc.initializer === void 0) {
  2343. Object.defineProperty(target, property, desc);
  2344. desc = null;
  2345. }
  2346. return desc;
  2347. }
  2348. var id = 0;
  2349. function _classPrivateFieldLooseKey(name) {
  2350. return '__private_' + id++ + '_' + name;
  2351. }
  2352. function _classPrivateFieldLooseBase(receiver, privateKey) {
  2353. if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
  2354. throw new TypeError('attempted to use private field on non-instance');
  2355. }
  2356. return receiver;
  2357. }
  2358. function _classPrivateFieldGet(receiver, privateMap) {
  2359. var descriptor = privateMap.get(receiver);
  2360. if (!descriptor) {
  2361. throw new TypeError('attempted to get private field on non-instance');
  2362. }
  2363. if (descriptor.get) {
  2364. return descriptor.get.call(receiver);
  2365. }
  2366. return descriptor.value;
  2367. }
  2368. function _classPrivateFieldSet(receiver, privateMap, value) {
  2369. var descriptor = privateMap.get(receiver);
  2370. if (!descriptor) {
  2371. throw new TypeError('attempted to set private field on non-instance');
  2372. }
  2373. if (descriptor.set) {
  2374. descriptor.set.call(receiver, value);
  2375. } else {
  2376. if (!descriptor.writable) {
  2377. throw new TypeError('attempted to set read only private field');
  2378. }
  2379. descriptor.value = value;
  2380. }
  2381. return value;
  2382. }
  2383. function _classPrivateFieldDestructureSet(receiver, privateMap) {
  2384. if (!privateMap.has(receiver)) {
  2385. throw new TypeError('attempted to set private field on non-instance');
  2386. }
  2387. var descriptor = privateMap.get(receiver);
  2388. if (descriptor.set) {
  2389. if (!('__destrObj' in descriptor)) {
  2390. descriptor.__destrObj = {
  2391. set value(v) {
  2392. descriptor.set.call(receiver, v);
  2393. },
  2394. };
  2395. }
  2396. return descriptor.__destrObj;
  2397. } else {
  2398. if (!descriptor.writable) {
  2399. throw new TypeError('attempted to set read only private field');
  2400. }
  2401. return descriptor;
  2402. }
  2403. }
  2404. function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) {
  2405. if (receiver !== classConstructor) {
  2406. throw new TypeError('Private static access of wrong provenance');
  2407. }
  2408. return descriptor.value;
  2409. }
  2410. function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) {
  2411. if (receiver !== classConstructor) {
  2412. throw new TypeError('Private static access of wrong provenance');
  2413. }
  2414. if (!descriptor.writable) {
  2415. throw new TypeError('attempted to set read only private field');
  2416. }
  2417. descriptor.value = value;
  2418. return value;
  2419. }
  2420. function _classStaticPrivateMethodGet(receiver, classConstructor, method) {
  2421. if (receiver !== classConstructor) {
  2422. throw new TypeError('Private static access of wrong provenance');
  2423. }
  2424. return method;
  2425. }
  2426. function _classStaticPrivateMethodSet() {
  2427. throw new TypeError('attempted to set read only static private field');
  2428. }
  2429. function _decorate(decorators, factory, superClass, mixins) {
  2430. var api = _getDecoratorsApi();
  2431. if (mixins) {
  2432. for (var i = 0; i < mixins.length; i++) {
  2433. api = mixins[i](api);
  2434. }
  2435. }
  2436. var r = factory(function initialize(O) {
  2437. api.initializeInstanceElements(O, decorated.elements);
  2438. }, superClass);
  2439. var decorated = api.decorateClass(
  2440. _coalesceClassElements(r.d.map(_createElementDescriptor)),
  2441. decorators
  2442. );
  2443. api.initializeClassElements(r.F, decorated.elements);
  2444. return api.runClassFinishers(r.F, decorated.finishers);
  2445. }
  2446. function _getDecoratorsApi() {
  2447. _getDecoratorsApi = function() {
  2448. return api;
  2449. };
  2450. var api = {
  2451. elementsDefinitionOrder: [['method'], ['field']],
  2452. initializeInstanceElements: function(O, elements) {
  2453. ['method', 'field'].forEach(function(kind) {
  2454. elements.forEach(function(element) {
  2455. if (element.kind === kind && element.placement === 'own') {
  2456. this.defineClassElement(O, element);
  2457. }
  2458. }, this);
  2459. }, this);
  2460. },
  2461. initializeClassElements: function(F, elements) {
  2462. var proto = F.prototype;
  2463. ['method', 'field'].forEach(function(kind) {
  2464. elements.forEach(function(element) {
  2465. var placement = element.placement;
  2466. if (
  2467. element.kind === kind &&
  2468. (placement === 'static' || placement === 'prototype')
  2469. ) {
  2470. var receiver = placement === 'static' ? F : proto;
  2471. this.defineClassElement(receiver, element);
  2472. }
  2473. }, this);
  2474. }, this);
  2475. },
  2476. defineClassElement: function(receiver, element) {
  2477. var descriptor = element.descriptor;
  2478. if (element.kind === 'field') {
  2479. var initializer = element.initializer;
  2480. descriptor = {
  2481. enumerable: descriptor.enumerable,
  2482. writable: descriptor.writable,
  2483. configurable: descriptor.configurable,
  2484. value: initializer === void 0 ? void 0 : initializer.call(receiver),
  2485. };
  2486. }
  2487. Object.defineProperty(receiver, element.key, descriptor);
  2488. },
  2489. decorateClass: function(elements, decorators) {
  2490. var newElements = [];
  2491. var finishers = [];
  2492. var placements = {
  2493. static: [],
  2494. prototype: [],
  2495. own: [],
  2496. };
  2497. elements.forEach(function(element) {
  2498. this.addElementPlacement(element, placements);
  2499. }, this);
  2500. elements.forEach(function(element) {
  2501. if (!_hasDecorators(element)) return newElements.push(element);
  2502. var elementFinishersExtras = this.decorateElement(element, placements);
  2503. newElements.push(elementFinishersExtras.element);
  2504. newElements.push.apply(newElements, elementFinishersExtras.extras);
  2505. finishers.push.apply(finishers, elementFinishersExtras.finishers);
  2506. }, this);
  2507. if (!decorators) {
  2508. return {
  2509. elements: newElements,
  2510. finishers: finishers,
  2511. };
  2512. }
  2513. var result = this.decorateConstructor(newElements, decorators);
  2514. finishers.push.apply(finishers, result.finishers);
  2515. result.finishers = finishers;
  2516. return result;
  2517. },
  2518. addElementPlacement: function(element, placements, silent) {
  2519. var keys = placements[element.placement];
  2520. if (!silent && keys.indexOf(element.key) !== -1) {
  2521. throw new TypeError('Duplicated element (' + element.key + ')');
  2522. }
  2523. keys.push(element.key);
  2524. },
  2525. decorateElement: function(element, placements) {
  2526. var extras = [];
  2527. var finishers = [];
  2528. for (var decorators = element.decorators, i = decorators.length - 1; i >= 0; i--) {
  2529. var keys = placements[element.placement];
  2530. keys.splice(keys.indexOf(element.key), 1);
  2531. var elementObject = this.fromElementDescriptor(element);
  2532. var elementFinisherExtras = this.toElementFinisherExtras(
  2533. (0, decorators[i])(elementObject) || elementObject
  2534. );
  2535. element = elementFinisherExtras.element;
  2536. this.addElementPlacement(element, placements);
  2537. if (elementFinisherExtras.finisher) {
  2538. finishers.push(elementFinisherExtras.finisher);
  2539. }
  2540. var newExtras = elementFinisherExtras.extras;
  2541. if (newExtras) {
  2542. for (var j = 0; j < newExtras.length; j++) {
  2543. this.addElementPlacement(newExtras[j], placements);
  2544. }
  2545. extras.push.apply(extras, newExtras);
  2546. }
  2547. }
  2548. return {
  2549. element: element,
  2550. finishers: finishers,
  2551. extras: extras,
  2552. };
  2553. },
  2554. decorateConstructor: function(elements, decorators) {
  2555. var finishers = [];
  2556. for (var i = decorators.length - 1; i >= 0; i--) {
  2557. var obj = this.fromClassDescriptor(elements);
  2558. var elementsAndFinisher = this.toClassDescriptor(
  2559. (0, decorators[i])(obj) || obj
  2560. );
  2561. if (elementsAndFinisher.finisher !== undefined) {
  2562. finishers.push(elementsAndFinisher.finisher);
  2563. }
  2564. if (elementsAndFinisher.elements !== undefined) {
  2565. elements = elementsAndFinisher.elements;
  2566. for (var j = 0; j < elements.length - 1; j++) {
  2567. for (var k = j + 1; k < elements.length; k++) {
  2568. if (
  2569. elements[j].key === elements[k].key &&
  2570. elements[j].placement === elements[k].placement
  2571. ) {
  2572. throw new TypeError(
  2573. 'Duplicated element (' + elements[j].key + ')'
  2574. );
  2575. }
  2576. }
  2577. }
  2578. }
  2579. }
  2580. return {
  2581. elements: elements,
  2582. finishers: finishers,
  2583. };
  2584. },
  2585. fromElementDescriptor: function(element) {
  2586. var obj = {
  2587. kind: element.kind,
  2588. key: element.key,
  2589. placement: element.placement,
  2590. descriptor: element.descriptor,
  2591. };
  2592. var desc = {
  2593. value: 'Descriptor',
  2594. configurable: true,
  2595. };
  2596. Object.defineProperty(obj, Symbol.toStringTag, desc);
  2597. if (element.kind === 'field') obj.initializer = element.initializer;
  2598. return obj;
  2599. },
  2600. toElementDescriptors: function(elementObjects) {
  2601. if (elementObjects === undefined) return;
  2602. return _toArray(elementObjects).map(function(elementObject) {
  2603. var element = this.toElementDescriptor(elementObject);
  2604. this.disallowProperty(elementObject, 'finisher', 'An element descriptor');
  2605. this.disallowProperty(elementObject, 'extras', 'An element descriptor');
  2606. return element;
  2607. }, this);
  2608. },
  2609. toElementDescriptor: function(elementObject) {
  2610. var kind = String(elementObject.kind);
  2611. if (kind !== 'method' && kind !== 'field') {
  2612. throw new TypeError(
  2613. 'An element descriptor\'s .kind property must be either "method" or' +
  2614. ' "field", but a decorator created an element descriptor with' +
  2615. ' .kind "' +
  2616. kind +
  2617. '"'
  2618. );
  2619. }
  2620. var key = _toPropertyKey(elementObject.key);
  2621. var placement = String(elementObject.placement);
  2622. if (placement !== 'static' && placement !== 'prototype' && placement !== 'own') {
  2623. throw new TypeError(
  2624. 'An element descriptor\'s .placement property must be one of "static",' +
  2625. ' "prototype" or "own", but a decorator created an element descriptor' +
  2626. ' with .placement "' +
  2627. placement +
  2628. '"'
  2629. );
  2630. }
  2631. var descriptor = elementObject.descriptor;
  2632. this.disallowProperty(elementObject, 'elements', 'An element descriptor');
  2633. var element = {
  2634. kind: kind,
  2635. key: key,
  2636. placement: placement,
  2637. descriptor: Object.assign({}, descriptor),
  2638. };
  2639. if (kind !== 'field') {
  2640. this.disallowProperty(elementObject, 'initializer', 'A method descriptor');
  2641. } else {
  2642. this.disallowProperty(
  2643. descriptor,
  2644. 'get',
  2645. 'The property descriptor of a field descriptor'
  2646. );
  2647. this.disallowProperty(
  2648. descriptor,
  2649. 'set',
  2650. 'The property descriptor of a field descriptor'
  2651. );
  2652. this.disallowProperty(
  2653. descriptor,
  2654. 'value',
  2655. 'The property descriptor of a field descriptor'
  2656. );
  2657. element.initializer = elementObject.initializer;
  2658. }
  2659. return element;
  2660. },
  2661. toElementFinisherExtras: function(elementObject) {
  2662. var element = this.toElementDescriptor(elementObject);
  2663. var finisher = _optionalCallableProperty(elementObject, 'finisher');
  2664. var extras = this.toElementDescriptors(elementObject.extras);
  2665. return {
  2666. element: element,
  2667. finisher: finisher,
  2668. extras: extras,
  2669. };
  2670. },
  2671. fromClassDescriptor: function(elements) {
  2672. var obj = {
  2673. kind: 'class',
  2674. elements: elements.map(this.fromElementDescriptor, this),
  2675. };
  2676. var desc = {
  2677. value: 'Descriptor',
  2678. configurable: true,
  2679. };
  2680. Object.defineProperty(obj, Symbol.toStringTag, desc);
  2681. return obj;
  2682. },
  2683. toClassDescriptor: function(obj) {
  2684. var kind = String(obj.kind);
  2685. if (kind !== 'class') {
  2686. throw new TypeError(
  2687. 'A class descriptor\'s .kind property must be "class", but a decorator' +
  2688. ' created a class descriptor with .kind "' +
  2689. kind +
  2690. '"'
  2691. );
  2692. }
  2693. this.disallowProperty(obj, 'key', 'A class descriptor');
  2694. this.disallowProperty(obj, 'placement', 'A class descriptor');
  2695. this.disallowProperty(obj, 'descriptor', 'A class descriptor');
  2696. this.disallowProperty(obj, 'initializer', 'A class descriptor');
  2697. this.disallowProperty(obj, 'extras', 'A class descriptor');
  2698. var finisher = _optionalCallableProperty(obj, 'finisher');
  2699. var elements = this.toElementDescriptors(obj.elements);
  2700. return {
  2701. elements: elements,
  2702. finisher: finisher,
  2703. };
  2704. },
  2705. runClassFinishers: function(constructor, finishers) {
  2706. for (var i = 0; i < finishers.length; i++) {
  2707. var newConstructor = (0, finishers[i])(constructor);
  2708. if (newConstructor !== undefined) {
  2709. if (typeof newConstructor !== 'function') {
  2710. throw new TypeError('Finishers must return a constructor.');
  2711. }
  2712. constructor = newConstructor;
  2713. }
  2714. }
  2715. return constructor;
  2716. },
  2717. disallowProperty: function(obj, name, objectType) {
  2718. if (obj[name] !== undefined) {
  2719. throw new TypeError(objectType + " can't have a ." + name + ' property.');
  2720. }
  2721. },
  2722. };
  2723. return api;
  2724. }
  2725. function _createElementDescriptor(def) {
  2726. var key = _toPropertyKey(def.key);
  2727. var descriptor;
  2728. if (def.kind === 'method') {
  2729. descriptor = {
  2730. value: def.value,
  2731. writable: true,
  2732. configurable: true,
  2733. enumerable: false,
  2734. };
  2735. } else if (def.kind === 'get') {
  2736. descriptor = {
  2737. get: def.value,
  2738. configurable: true,
  2739. enumerable: false,
  2740. };
  2741. } else if (def.kind === 'set') {
  2742. descriptor = {
  2743. set: def.value,
  2744. configurable: true,
  2745. enumerable: false,
  2746. };
  2747. } else if (def.kind === 'field') {
  2748. descriptor = {
  2749. configurable: true,
  2750. writable: true,
  2751. enumerable: true,
  2752. };
  2753. }
  2754. var element = {
  2755. kind: def.kind === 'field' ? 'field' : 'method',
  2756. key: key,
  2757. placement: def.static ? 'static' : def.kind === 'field' ? 'own' : 'prototype',
  2758. descriptor: descriptor,
  2759. };
  2760. if (def.decorators) element.decorators = def.decorators;
  2761. if (def.kind === 'field') element.initializer = def.value;
  2762. return element;
  2763. }
  2764. function _coalesceGetterSetter(element, other) {
  2765. if (element.descriptor.get !== undefined) {
  2766. other.descriptor.get = element.descriptor.get;
  2767. } else {
  2768. other.descriptor.set = element.descriptor.set;
  2769. }
  2770. }
  2771. function _coalesceClassElements(elements) {
  2772. var newElements = [];
  2773. var isSameElement = function(other) {
  2774. return (
  2775. other.kind === 'method' &&
  2776. other.key === element.key &&
  2777. other.placement === element.placement
  2778. );
  2779. };
  2780. for (var i = 0; i < elements.length; i++) {
  2781. var element = elements[i];
  2782. var other;
  2783. if (element.kind === 'method' && (other = newElements.find(isSameElement))) {
  2784. if (_isDataDescriptor(element.descriptor) || _isDataDescriptor(other.descriptor)) {
  2785. if (_hasDecorators(element) || _hasDecorators(other)) {
  2786. throw new ReferenceError(
  2787. 'Duplicated methods (' + element.key + ") can't be decorated."
  2788. );
  2789. }
  2790. other.descriptor = element.descriptor;
  2791. } else {
  2792. if (_hasDecorators(element)) {
  2793. if (_hasDecorators(other)) {
  2794. throw new ReferenceError(
  2795. "Decorators can't be placed on different accessors with for " +
  2796. 'the same property (' +
  2797. element.key +
  2798. ').'
  2799. );
  2800. }
  2801. other.decorators = element.decorators;
  2802. }
  2803. _coalesceGetterSetter(element, other);
  2804. }
  2805. } else {
  2806. newElements.push(element);
  2807. }
  2808. }
  2809. return newElements;
  2810. }
  2811. function _hasDecorators(element) {
  2812. return element.decorators && element.decorators.length;
  2813. }
  2814. function _isDataDescriptor(desc) {
  2815. return desc !== undefined && !(desc.value === undefined && desc.writable === undefined);
  2816. }
  2817. function _optionalCallableProperty(obj, name) {
  2818. var value = obj[name];
  2819. if (value !== undefined && typeof value !== 'function') {
  2820. throw new TypeError("Expected '" + name + "' to be a function");
  2821. }
  2822. return value;
  2823. }
  2824. function _classPrivateMethodGet(receiver, privateSet, fn) {
  2825. if (!privateSet.has(receiver)) {
  2826. throw new TypeError('attempted to get private field on non-instance');
  2827. }
  2828. return fn;
  2829. }
  2830. function _classPrivateMethodSet() {
  2831. throw new TypeError('attempted to reassign private method');
  2832. }
  2833. function _wrapRegExp(re, groups) {
  2834. _wrapRegExp = function(re, groups) {
  2835. return new BabelRegExp(re, groups);
  2836. };
  2837. var _RegExp = _wrapNativeSuper(RegExp);
  2838. var _super = RegExp.prototype;
  2839. var _groups = new WeakMap();
  2840. function BabelRegExp(re, groups) {
  2841. var _this = _RegExp.call(this, re);
  2842. _groups.set(_this, groups);
  2843. return _this;
  2844. }
  2845. _inherits(BabelRegExp, _RegExp);
  2846. BabelRegExp.prototype.exec = function(str) {
  2847. var result = _super.exec.call(this, str);
  2848. if (result) result.groups = buildGroups(result, this);
  2849. return result;
  2850. };
  2851. BabelRegExp.prototype[Symbol.replace] = function(str, substitution) {
  2852. if (typeof substitution === 'string') {
  2853. var groups = _groups.get(this);
  2854. return _super[Symbol.replace].call(
  2855. this,
  2856. str,
  2857. substitution.replace(/\$<([^>]+)>/g, function(_, name) {
  2858. return '$' + groups[name];
  2859. })
  2860. );
  2861. } else if (typeof substitution === 'function') {
  2862. var _this = this;
  2863. return _super[Symbol.replace].call(this, str, function() {
  2864. var args = [];
  2865. args.push.apply(args, arguments);
  2866. if (typeof args[args.length - 1] !== 'object') {
  2867. args.push(buildGroups(args, _this));
  2868. }
  2869. return substitution.apply(this, args);
  2870. });
  2871. } else {
  2872. return _super[Symbol.replace].call(this, str, substitution);
  2873. }
  2874. };
  2875. function buildGroups(result, re) {
  2876. var g = _groups.get(re);
  2877. return Object.keys(g).reduce(function(groups, name) {
  2878. groups[name] = result[g[name]];
  2879. return groups;
  2880. }, Object.create(null));
  2881. }
  2882. return _wrapRegExp.apply(this, arguments);
  2883. }
  2884. var arrayRemove = function arrayRemove(arr, index) {
  2885. return arr.splice(index, 1);
  2886. };
  2887. var run = function run(cb, sync) {
  2888. if (sync) {
  2889. cb();
  2890. } else if (document.hidden) {
  2891. Promise.resolve(1).then(cb);
  2892. } else {
  2893. setTimeout(cb, 0);
  2894. }
  2895. };
  2896. var on = function on() {
  2897. var listeners = [];
  2898. var off = function off(event, cb) {
  2899. arrayRemove(
  2900. listeners,
  2901. listeners.findIndex(function(listener) {
  2902. return listener.event === event && (listener.cb === cb || !cb);
  2903. })
  2904. );
  2905. };
  2906. var _fire = function fire(event, args, sync) {
  2907. listeners
  2908. .filter(function(listener) {
  2909. return listener.event === event;
  2910. })
  2911. .map(function(listener) {
  2912. return listener.cb;
  2913. })
  2914. .forEach(function(cb) {
  2915. return run(function() {
  2916. return cb.apply(void 0, _toConsumableArray(args));
  2917. }, sync);
  2918. });
  2919. };
  2920. return {
  2921. fireSync: function fireSync(event) {
  2922. for (
  2923. var _len = arguments.length,
  2924. args = new Array(_len > 1 ? _len - 1 : 0),
  2925. _key = 1;
  2926. _key < _len;
  2927. _key++
  2928. ) {
  2929. args[_key - 1] = arguments[_key];
  2930. }
  2931. _fire(event, args, true);
  2932. },
  2933. fire: function fire(event) {
  2934. for (
  2935. var _len2 = arguments.length,
  2936. args = new Array(_len2 > 1 ? _len2 - 1 : 0),
  2937. _key2 = 1;
  2938. _key2 < _len2;
  2939. _key2++
  2940. ) {
  2941. args[_key2 - 1] = arguments[_key2];
  2942. }
  2943. _fire(event, args, false);
  2944. },
  2945. on: function on(event, cb) {
  2946. listeners.push({ event: event, cb: cb });
  2947. },
  2948. onOnce: function onOnce(event, _cb) {
  2949. listeners.push({
  2950. event: event,
  2951. cb: function cb() {
  2952. off(event, _cb);
  2953. _cb.apply(void 0, arguments);
  2954. },
  2955. });
  2956. },
  2957. off: off,
  2958. };
  2959. };
  2960. var copyObjectPropertiesToObject = function copyObjectPropertiesToObject(
  2961. src,
  2962. target,
  2963. excluded
  2964. ) {
  2965. Object.getOwnPropertyNames(src)
  2966. .filter(function(property) {
  2967. return !excluded.includes(property);
  2968. })
  2969. .forEach(function(key) {
  2970. return Object.defineProperty(
  2971. target,
  2972. key,
  2973. Object.getOwnPropertyDescriptor(src, key)
  2974. );
  2975. });
  2976. };
  2977. var PRIVATE = [
  2978. 'fire',
  2979. 'process',
  2980. 'revert',
  2981. 'load',
  2982. 'on',
  2983. 'off',
  2984. 'onOnce',
  2985. 'retryLoad',
  2986. 'extend',
  2987. 'archive',
  2988. 'archived',
  2989. 'release',
  2990. 'released',
  2991. 'requestProcessing',
  2992. 'freeze',
  2993. ];
  2994. var createItemAPI = function createItemAPI(item) {
  2995. var api = {};
  2996. copyObjectPropertiesToObject(item, api, PRIVATE);
  2997. return api;
  2998. };
  2999. var removeReleasedItems = function removeReleasedItems(items) {
  3000. items.forEach(function(item, index) {
  3001. if (item.released) {
  3002. arrayRemove(items, index);
  3003. }
  3004. });
  3005. };
  3006. var ItemStatus = {
  3007. INIT: 1,
  3008. IDLE: 2,
  3009. PROCESSING_QUEUED: 9,
  3010. PROCESSING: 3,
  3011. PROCESSING_COMPLETE: 5,
  3012. PROCESSING_ERROR: 6,
  3013. PROCESSING_REVERT_ERROR: 10,
  3014. LOADING: 7,
  3015. LOAD_ERROR: 8,
  3016. };
  3017. var FileOrigin = {
  3018. INPUT: 1,
  3019. LIMBO: 2,
  3020. LOCAL: 3,
  3021. };
  3022. var getNonNumeric = function getNonNumeric(str) {
  3023. return /[^0-9]+/.exec(str);
  3024. };
  3025. var getDecimalSeparator = function getDecimalSeparator() {
  3026. return getNonNumeric((1.1).toLocaleString())[0];
  3027. };
  3028. var getThousandsSeparator = function getThousandsSeparator() {
  3029. // Added for browsers that do not return the thousands separator (happend on native browser Android 4.4.4)
  3030. // We check against the normal toString output and if they're the same return a comma when decimal separator is a dot
  3031. var decimalSeparator = getDecimalSeparator();
  3032. var thousandsStringWithSeparator = (1000.0).toLocaleString();
  3033. var thousandsStringWithoutSeparator = (1000.0).toString();
  3034. if (thousandsStringWithSeparator !== thousandsStringWithoutSeparator) {
  3035. return getNonNumeric(thousandsStringWithSeparator)[0];
  3036. }
  3037. return decimalSeparator === '.' ? ',' : '.';
  3038. };
  3039. var Type = {
  3040. BOOLEAN: 'boolean',
  3041. INT: 'int',
  3042. NUMBER: 'number',
  3043. STRING: 'string',
  3044. ARRAY: 'array',
  3045. OBJECT: 'object',
  3046. FUNCTION: 'function',
  3047. ACTION: 'action',
  3048. SERVER_API: 'serverapi',
  3049. REGEX: 'regex',
  3050. };
  3051. // all registered filters
  3052. var filters = [];
  3053. // loops over matching filters and passes options to each filter, returning the mapped results
  3054. var applyFilterChain = function applyFilterChain(key, value, utils) {
  3055. return new Promise(function(resolve, reject) {
  3056. // find matching filters for this key
  3057. var matchingFilters = filters
  3058. .filter(function(f) {
  3059. return f.key === key;
  3060. })
  3061. .map(function(f) {
  3062. return f.cb;
  3063. });
  3064. // resolve now
  3065. if (matchingFilters.length === 0) {
  3066. resolve(value);
  3067. return;
  3068. }
  3069. // first filter to kick things of
  3070. var initialFilter = matchingFilters.shift();
  3071. // chain filters
  3072. matchingFilters
  3073. .reduce(
  3074. // loop over promises passing value to next promise
  3075. function(current, next) {
  3076. return current.then(function(value) {
  3077. return next(value, utils);
  3078. });
  3079. },
  3080. // call initial filter, will return a promise
  3081. initialFilter(value, utils)
  3082. // all executed
  3083. )
  3084. .then(function(value) {
  3085. return resolve(value);
  3086. })
  3087. .catch(function(error) {
  3088. return reject(error);
  3089. });
  3090. });
  3091. };
  3092. var applyFilters = function applyFilters(key, value, utils) {
  3093. return filters
  3094. .filter(function(f) {
  3095. return f.key === key;
  3096. })
  3097. .map(function(f) {
  3098. return f.cb(value, utils);
  3099. });
  3100. };
  3101. // adds a new filter to the list
  3102. var addFilter = function addFilter(key, cb) {
  3103. return filters.push({ key: key, cb: cb });
  3104. };
  3105. var extendDefaultOptions = function extendDefaultOptions(additionalOptions) {
  3106. return Object.assign(defaultOptions, additionalOptions);
  3107. };
  3108. var getOptions = function getOptions() {
  3109. return Object.assign({}, defaultOptions);
  3110. };
  3111. var setOptions = function setOptions(opts) {
  3112. forin(opts, function(key, value) {
  3113. // key does not exist, so this option cannot be set
  3114. if (!defaultOptions[key]) {
  3115. return;
  3116. }
  3117. defaultOptions[key][0] = getValueByType(
  3118. value,
  3119. defaultOptions[key][0],
  3120. defaultOptions[key][1]
  3121. );
  3122. });
  3123. };
  3124. // default options on app
  3125. var defaultOptions = {
  3126. // the id to add to the root element
  3127. id: [null, Type.STRING],
  3128. // input field name to use
  3129. name: ['filepond', Type.STRING],
  3130. // disable the field
  3131. disabled: [false, Type.BOOLEAN],
  3132. // classname to put on wrapper
  3133. className: [null, Type.STRING],
  3134. // is the field required
  3135. required: [false, Type.BOOLEAN],
  3136. // Allow media capture when value is set
  3137. captureMethod: [null, Type.STRING],
  3138. // - "camera", "microphone" or "camcorder",
  3139. // - Does not work with multiple on apple devices
  3140. // - If set, acceptedFileTypes must be made to match with media wildcard "image/*", "audio/*" or "video/*"
  3141. // sync `acceptedFileTypes` property with `accept` attribute
  3142. allowSyncAcceptAttribute: [true, Type.BOOLEAN],
  3143. // Feature toggles
  3144. allowDrop: [true, Type.BOOLEAN], // Allow dropping of files
  3145. allowBrowse: [true, Type.BOOLEAN], // Allow browsing the file system
  3146. allowPaste: [true, Type.BOOLEAN], // Allow pasting files
  3147. allowMultiple: [false, Type.BOOLEAN], // Allow multiple files (disabled by default, as multiple attribute is also required on input to allow multiple)
  3148. allowReplace: [true, Type.BOOLEAN], // Allow dropping a file on other file to replace it (only works when multiple is set to false)
  3149. allowRevert: [true, Type.BOOLEAN], // Allows user to revert file upload
  3150. allowRemove: [true, Type.BOOLEAN], // Allow user to remove a file
  3151. allowProcess: [true, Type.BOOLEAN], // Allows user to process a file, when set to false, this removes the file upload button
  3152. allowReorder: [false, Type.BOOLEAN], // Allow reordering of files
  3153. allowDirectoriesOnly: [false, Type.BOOLEAN], // Allow only selecting directories with browse (no support for filtering dnd at this point)
  3154. // Try store file if `server` not set
  3155. storeAsFile: [false, Type.BOOLEAN],
  3156. // Revert mode
  3157. forceRevert: [false, Type.BOOLEAN], // Set to 'force' to require the file to be reverted before removal
  3158. // Input requirements
  3159. maxFiles: [null, Type.INT], // Max number of files
  3160. checkValidity: [false, Type.BOOLEAN], // Enables custom validity messages
  3161. // Where to put file
  3162. itemInsertLocationFreedom: [true, Type.BOOLEAN], // Set to false to always add items to begin or end of list
  3163. itemInsertLocation: ['before', Type.STRING], // Default index in list to add items that have been dropped at the top of the list
  3164. itemInsertInterval: [75, Type.INT],
  3165. // Drag 'n Drop related
  3166. dropOnPage: [false, Type.BOOLEAN], // Allow dropping of files anywhere on page (prevents browser from opening file if dropped outside of Up)
  3167. dropOnElement: [true, Type.BOOLEAN], // Drop needs to happen on element (set to false to also load drops outside of Up)
  3168. dropValidation: [false, Type.BOOLEAN], // Enable or disable validating files on drop
  3169. ignoredFiles: [['.ds_store', 'thumbs.db', 'desktop.ini'], Type.ARRAY],
  3170. // Upload related
  3171. instantUpload: [true, Type.BOOLEAN], // Should upload files immediately on drop
  3172. maxParallelUploads: [2, Type.INT], // Maximum files to upload in parallel
  3173. allowMinimumUploadDuration: [true, Type.BOOLEAN], // if true uploads take at least 750 ms, this ensures the user sees the upload progress giving trust the upload actually happened
  3174. // Chunks
  3175. chunkUploads: [false, Type.BOOLEAN], // Enable chunked uploads
  3176. chunkForce: [false, Type.BOOLEAN], // Force use of chunk uploads even for files smaller than chunk size
  3177. chunkSize: [5000000, Type.INT], // Size of chunks (5MB default)
  3178. chunkRetryDelays: [[500, 1000, 3000], Type.ARRAY], // Amount of times to retry upload of a chunk when it fails
  3179. // The server api end points to use for uploading (see docs)
  3180. server: [null, Type.SERVER_API],
  3181. // File size calculations, can set to 1024, this is only used for display, properties use file size base 1000
  3182. fileSizeBase: [1000, Type.INT],
  3183. // Labels and status messages
  3184. labelFileSizeBytes: ['bytes', Type.STRING],
  3185. labelFileSizeKilobytes: ['KB', Type.STRING],
  3186. labelFileSizeMegabytes: ['MB', Type.STRING],
  3187. labelFileSizeGigabytes: ['GB', Type.STRING],
  3188. labelDecimalSeparator: [getDecimalSeparator(), Type.STRING], // Default is locale separator
  3189. labelThousandsSeparator: [getThousandsSeparator(), Type.STRING], // Default is locale separator
  3190. labelIdle: [
  3191. 'Drag & Drop your files or <span class="filepond--label-action">Browse</span>',
  3192. Type.STRING,
  3193. ],
  3194. labelInvalidField: ['Field contains invalid files', Type.STRING],
  3195. labelFileWaitingForSize: ['Waiting for size', Type.STRING],
  3196. labelFileSizeNotAvailable: ['Size not available', Type.STRING],
  3197. labelFileCountSingular: ['file in list', Type.STRING],
  3198. labelFileCountPlural: ['files in list', Type.STRING],
  3199. labelFileLoading: ['Loading', Type.STRING],
  3200. labelFileAdded: ['Added', Type.STRING], // assistive only
  3201. labelFileLoadError: ['Error during load', Type.STRING],
  3202. labelFileRemoved: ['Removed', Type.STRING], // assistive only
  3203. labelFileRemoveError: ['Error during remove', Type.STRING],
  3204. labelFileProcessing: ['Uploading', Type.STRING],
  3205. labelFileProcessingComplete: ['Upload complete', Type.STRING],
  3206. labelFileProcessingAborted: ['Upload cancelled', Type.STRING],
  3207. labelFileProcessingError: ['Error during upload', Type.STRING],
  3208. labelFileProcessingRevertError: ['Error during revert', Type.STRING],
  3209. labelTapToCancel: ['tap to cancel', Type.STRING],
  3210. labelTapToRetry: ['tap to retry', Type.STRING],
  3211. labelTapToUndo: ['tap to undo', Type.STRING],
  3212. labelButtonRemoveItem: ['Remove', Type.STRING],
  3213. labelButtonAbortItemLoad: ['Abort', Type.STRING],
  3214. labelButtonRetryItemLoad: ['Retry', Type.STRING],
  3215. labelButtonAbortItemProcessing: ['Cancel', Type.STRING],
  3216. labelButtonUndoItemProcessing: ['Undo', Type.STRING],
  3217. labelButtonRetryItemProcessing: ['Retry', Type.STRING],
  3218. labelButtonProcessItem: ['Upload', Type.STRING],
  3219. // make sure width and height plus viewpox are even numbers so icons are nicely centered
  3220. iconRemove: [
  3221. '<svg width="26" height="26" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="M11.586 13l-2.293 2.293a1 1 0 0 0 1.414 1.414L13 14.414l2.293 2.293a1 1 0 0 0 1.414-1.414L14.414 13l2.293-2.293a1 1 0 0 0-1.414-1.414L13 11.586l-2.293-2.293a1 1 0 0 0-1.414 1.414L11.586 13z" fill="currentColor" fill-rule="nonzero"/></svg>',
  3222. Type.STRING,
  3223. ],
  3224. iconProcess: [
  3225. '<svg width="26" height="26" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="M14 10.414v3.585a1 1 0 0 1-2 0v-3.585l-1.293 1.293a1 1 0 0 1-1.414-1.415l3-3a1 1 0 0 1 1.414 0l3 3a1 1 0 0 1-1.414 1.415L14 10.414zM9 18a1 1 0 0 1 0-2h8a1 1 0 0 1 0 2H9z" fill="currentColor" fill-rule="evenodd"/></svg>',
  3226. Type.STRING,
  3227. ],
  3228. iconRetry: [
  3229. '<svg width="26" height="26" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="M10.81 9.185l-.038.02A4.997 4.997 0 0 0 8 13.683a5 5 0 0 0 5 5 5 5 0 0 0 5-5 1 1 0 0 1 2 0A7 7 0 1 1 9.722 7.496l-.842-.21a.999.999 0 1 1 .484-1.94l3.23.806c.535.133.86.675.73 1.21l-.804 3.233a.997.997 0 0 1-1.21.73.997.997 0 0 1-.73-1.21l.23-.928v-.002z" fill="currentColor" fill-rule="nonzero"/></svg>',
  3230. Type.STRING,
  3231. ],
  3232. iconUndo: [
  3233. '<svg width="26" height="26" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="M9.185 10.81l.02-.038A4.997 4.997 0 0 1 13.683 8a5 5 0 0 1 5 5 5 5 0 0 1-5 5 1 1 0 0 0 0 2A7 7 0 1 0 7.496 9.722l-.21-.842a.999.999 0 1 0-1.94.484l.806 3.23c.133.535.675.86 1.21.73l3.233-.803a.997.997 0 0 0 .73-1.21.997.997 0 0 0-1.21-.73l-.928.23-.002-.001z" fill="currentColor" fill-rule="nonzero"/></svg>',
  3234. Type.STRING,
  3235. ],
  3236. iconDone: [
  3237. '<svg width="26" height="26" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="M18.293 9.293a1 1 0 0 1 1.414 1.414l-7.002 7a1 1 0 0 1-1.414 0l-3.998-4a1 1 0 1 1 1.414-1.414L12 15.586l6.294-6.293z" fill="currentColor" fill-rule="nonzero"/></svg>',
  3238. Type.STRING,
  3239. ],
  3240. // event handlers
  3241. oninit: [null, Type.FUNCTION],
  3242. onwarning: [null, Type.FUNCTION],
  3243. onerror: [null, Type.FUNCTION],
  3244. onactivatefile: [null, Type.FUNCTION],
  3245. oninitfile: [null, Type.FUNCTION],
  3246. onaddfilestart: [null, Type.FUNCTION],
  3247. onaddfileprogress: [null, Type.FUNCTION],
  3248. onaddfile: [null, Type.FUNCTION],
  3249. onprocessfilestart: [null, Type.FUNCTION],
  3250. onprocessfileprogress: [null, Type.FUNCTION],
  3251. onprocessfileabort: [null, Type.FUNCTION],
  3252. onprocessfilerevert: [null, Type.FUNCTION],
  3253. onprocessfile: [null, Type.FUNCTION],
  3254. onprocessfiles: [null, Type.FUNCTION],
  3255. onremovefile: [null, Type.FUNCTION],
  3256. onpreparefile: [null, Type.FUNCTION],
  3257. onupdatefiles: [null, Type.FUNCTION],
  3258. onreorderfiles: [null, Type.FUNCTION],
  3259. // hooks
  3260. beforeDropFile: [null, Type.FUNCTION],
  3261. beforeAddFile: [null, Type.FUNCTION],
  3262. beforeRemoveFile: [null, Type.FUNCTION],
  3263. beforePrepareFile: [null, Type.FUNCTION],
  3264. // styles
  3265. stylePanelLayout: [null, Type.STRING], // null 'integrated', 'compact', 'circle'
  3266. stylePanelAspectRatio: [null, Type.STRING], // null or '3:2' or 1
  3267. styleItemPanelAspectRatio: [null, Type.STRING],
  3268. styleButtonRemoveItemPosition: ['left', Type.STRING],
  3269. styleButtonProcessItemPosition: ['right', Type.STRING],
  3270. styleLoadIndicatorPosition: ['right', Type.STRING],
  3271. styleProgressIndicatorPosition: ['right', Type.STRING],
  3272. styleButtonRemoveItemAlign: [false, Type.BOOLEAN],
  3273. // custom initial files array
  3274. files: [[], Type.ARRAY],
  3275. // show support by displaying credits
  3276. credits: [['https://pqina.nl/', 'Powered by PQINA'], Type.ARRAY],
  3277. };
  3278. var getItemByQuery = function getItemByQuery(items, query) {
  3279. // just return first index
  3280. if (isEmpty(query)) {
  3281. return items[0] || null;
  3282. }
  3283. // query is index
  3284. if (isInt(query)) {
  3285. return items[query] || null;
  3286. }
  3287. // if query is item, get the id
  3288. if (typeof query === 'object') {
  3289. query = query.id;
  3290. }
  3291. // assume query is a string and return item by id
  3292. return (
  3293. items.find(function(item) {
  3294. return item.id === query;
  3295. }) || null
  3296. );
  3297. };
  3298. var getNumericAspectRatioFromString = function getNumericAspectRatioFromString(aspectRatio) {
  3299. if (isEmpty(aspectRatio)) {
  3300. return aspectRatio;
  3301. }
  3302. if (/:/.test(aspectRatio)) {
  3303. var parts = aspectRatio.split(':');
  3304. return parts[1] / parts[0];
  3305. }
  3306. return parseFloat(aspectRatio);
  3307. };
  3308. var getActiveItems = function getActiveItems(items) {
  3309. return items.filter(function(item) {
  3310. return !item.archived;
  3311. });
  3312. };
  3313. var Status = {
  3314. EMPTY: 0,
  3315. IDLE: 1, // waiting
  3316. ERROR: 2, // a file is in error state
  3317. BUSY: 3, // busy processing or loading
  3318. READY: 4, // all files uploaded
  3319. };
  3320. var res = null;
  3321. var canUpdateFileInput = function canUpdateFileInput() {
  3322. if (res === null) {
  3323. try {
  3324. var dataTransfer = new DataTransfer();
  3325. dataTransfer.items.add(new File(['hello world'], 'This_Works.txt'));
  3326. var el = document.createElement('input');
  3327. el.setAttribute('type', 'file');
  3328. el.files = dataTransfer.files;
  3329. res = el.files.length === 1;
  3330. } catch (err) {
  3331. res = false;
  3332. }
  3333. }
  3334. return res;
  3335. };
  3336. var ITEM_ERROR = [
  3337. ItemStatus.LOAD_ERROR,
  3338. ItemStatus.PROCESSING_ERROR,
  3339. ItemStatus.PROCESSING_REVERT_ERROR,
  3340. ];
  3341. var ITEM_BUSY = [
  3342. ItemStatus.LOADING,
  3343. ItemStatus.PROCESSING,
  3344. ItemStatus.PROCESSING_QUEUED,
  3345. ItemStatus.INIT,
  3346. ];
  3347. var ITEM_READY = [ItemStatus.PROCESSING_COMPLETE];
  3348. var isItemInErrorState = function isItemInErrorState(item) {
  3349. return ITEM_ERROR.includes(item.status);
  3350. };
  3351. var isItemInBusyState = function isItemInBusyState(item) {
  3352. return ITEM_BUSY.includes(item.status);
  3353. };
  3354. var isItemInReadyState = function isItemInReadyState(item) {
  3355. return ITEM_READY.includes(item.status);
  3356. };
  3357. var isAsync = function isAsync(state) {
  3358. return (
  3359. isObject(state.options.server) &&
  3360. (isObject(state.options.server.process) || isFunction(state.options.server.process))
  3361. );
  3362. };
  3363. var queries = function queries(state) {
  3364. return {
  3365. GET_STATUS: function GET_STATUS() {
  3366. var items = getActiveItems(state.items);
  3367. var EMPTY = Status.EMPTY,
  3368. ERROR = Status.ERROR,
  3369. BUSY = Status.BUSY,
  3370. IDLE = Status.IDLE,
  3371. READY = Status.READY;
  3372. if (items.length === 0) return EMPTY;
  3373. if (items.some(isItemInErrorState)) return ERROR;
  3374. if (items.some(isItemInBusyState)) return BUSY;
  3375. if (items.some(isItemInReadyState)) return READY;
  3376. return IDLE;
  3377. },
  3378. GET_ITEM: function GET_ITEM(query) {
  3379. return getItemByQuery(state.items, query);
  3380. },
  3381. GET_ACTIVE_ITEM: function GET_ACTIVE_ITEM(query) {
  3382. return getItemByQuery(getActiveItems(state.items), query);
  3383. },
  3384. GET_ACTIVE_ITEMS: function GET_ACTIVE_ITEMS() {
  3385. return getActiveItems(state.items);
  3386. },
  3387. GET_ITEMS: function GET_ITEMS() {
  3388. return state.items;
  3389. },
  3390. GET_ITEM_NAME: function GET_ITEM_NAME(query) {
  3391. var item = getItemByQuery(state.items, query);
  3392. return item ? item.filename : null;
  3393. },
  3394. GET_ITEM_SIZE: function GET_ITEM_SIZE(query) {
  3395. var item = getItemByQuery(state.items, query);
  3396. return item ? item.fileSize : null;
  3397. },
  3398. GET_STYLES: function GET_STYLES() {
  3399. return Object.keys(state.options)
  3400. .filter(function(key) {
  3401. return /^style/.test(key);
  3402. })
  3403. .map(function(option) {
  3404. return {
  3405. name: option,
  3406. value: state.options[option],
  3407. };
  3408. });
  3409. },
  3410. GET_PANEL_ASPECT_RATIO: function GET_PANEL_ASPECT_RATIO() {
  3411. var isShapeCircle = /circle/.test(state.options.stylePanelLayout);
  3412. var aspectRatio = isShapeCircle
  3413. ? 1
  3414. : getNumericAspectRatioFromString(state.options.stylePanelAspectRatio);
  3415. return aspectRatio;
  3416. },
  3417. GET_ITEM_PANEL_ASPECT_RATIO: function GET_ITEM_PANEL_ASPECT_RATIO() {
  3418. return state.options.styleItemPanelAspectRatio;
  3419. },
  3420. GET_ITEMS_BY_STATUS: function GET_ITEMS_BY_STATUS(status) {
  3421. return getActiveItems(state.items).filter(function(item) {
  3422. return item.status === status;
  3423. });
  3424. },
  3425. GET_TOTAL_ITEMS: function GET_TOTAL_ITEMS() {
  3426. return getActiveItems(state.items).length;
  3427. },
  3428. SHOULD_UPDATE_FILE_INPUT: function SHOULD_UPDATE_FILE_INPUT() {
  3429. return state.options.storeAsFile && canUpdateFileInput() && !isAsync(state);
  3430. },
  3431. IS_ASYNC: function IS_ASYNC() {
  3432. return isAsync(state);
  3433. },
  3434. GET_FILE_SIZE_LABELS: function GET_FILE_SIZE_LABELS(query) {
  3435. return {
  3436. labelBytes: query('GET_LABEL_FILE_SIZE_BYTES') || undefined,
  3437. labelKilobytes: query('GET_LABEL_FILE_SIZE_KILOBYTES') || undefined,
  3438. labelMegabytes: query('GET_LABEL_FILE_SIZE_MEGABYTES') || undefined,
  3439. labelGigabytes: query('GET_LABEL_FILE_SIZE_GIGABYTES') || undefined,
  3440. };
  3441. },
  3442. };
  3443. };
  3444. var hasRoomForItem = function hasRoomForItem(state) {
  3445. var count = getActiveItems(state.items).length;
  3446. // if cannot have multiple items, to add one item it should currently not contain items
  3447. if (!state.options.allowMultiple) {
  3448. return count === 0;
  3449. }
  3450. // if allows multiple items, we check if a max item count has been set, if not, there's no limit
  3451. var maxFileCount = state.options.maxFiles;
  3452. if (maxFileCount === null) {
  3453. return true;
  3454. }
  3455. // we check if the current count is smaller than the max count, if so, another file can still be added
  3456. if (count < maxFileCount) {
  3457. return true;
  3458. }
  3459. // no more room for another file
  3460. return false;
  3461. };
  3462. var limit = function limit(value, min, max) {
  3463. return Math.max(Math.min(max, value), min);
  3464. };
  3465. var arrayInsert = function arrayInsert(arr, index, item) {
  3466. return arr.splice(index, 0, item);
  3467. };
  3468. var insertItem = function insertItem(items, item, index) {
  3469. if (isEmpty(item)) {
  3470. return null;
  3471. }
  3472. // if index is undefined, append
  3473. if (typeof index === 'undefined') {
  3474. items.push(item);
  3475. return item;
  3476. }
  3477. // limit the index to the size of the items array
  3478. index = limit(index, 0, items.length);
  3479. // add item to array
  3480. arrayInsert(items, index, item);
  3481. // expose
  3482. return item;
  3483. };
  3484. var isBase64DataURI = function isBase64DataURI(str) {
  3485. return /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*)\s*$/i.test(
  3486. str
  3487. );
  3488. };
  3489. var getFilenameFromURL = function getFilenameFromURL(url) {
  3490. return url
  3491. .split('/')
  3492. .pop()
  3493. .split('?')
  3494. .shift();
  3495. };
  3496. var getExtensionFromFilename = function getExtensionFromFilename(name) {
  3497. return name.split('.').pop();
  3498. };
  3499. var guesstimateExtension = function guesstimateExtension(type) {
  3500. // if no extension supplied, exit here
  3501. if (typeof type !== 'string') {
  3502. return '';
  3503. }
  3504. // get subtype
  3505. var subtype = type.split('/').pop();
  3506. // is svg subtype
  3507. if (/svg/.test(subtype)) {
  3508. return 'svg';
  3509. }
  3510. if (/zip|compressed/.test(subtype)) {
  3511. return 'zip';
  3512. }
  3513. if (/plain/.test(subtype)) {
  3514. return 'txt';
  3515. }
  3516. if (/msword/.test(subtype)) {
  3517. return 'doc';
  3518. }
  3519. // if is valid subtype
  3520. if (/[a-z]+/.test(subtype)) {
  3521. // always use jpg extension
  3522. if (subtype === 'jpeg') {
  3523. return 'jpg';
  3524. }
  3525. // return subtype
  3526. return subtype;
  3527. }
  3528. return '';
  3529. };
  3530. var leftPad = function leftPad(value) {
  3531. var padding = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  3532. return (padding + value).slice(-padding.length);
  3533. };
  3534. var getDateString = function getDateString() {
  3535. var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date();
  3536. return (
  3537. date.getFullYear() +
  3538. '-' +
  3539. leftPad(date.getMonth() + 1, '00') +
  3540. '-' +
  3541. leftPad(date.getDate(), '00') +
  3542. '_' +
  3543. leftPad(date.getHours(), '00') +
  3544. '-' +
  3545. leftPad(date.getMinutes(), '00') +
  3546. '-' +
  3547. leftPad(date.getSeconds(), '00')
  3548. );
  3549. };
  3550. var getFileFromBlob = function getFileFromBlob(blob, filename) {
  3551. var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  3552. var extension = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
  3553. var file =
  3554. typeof type === 'string'
  3555. ? blob.slice(0, blob.size, type)
  3556. : blob.slice(0, blob.size, blob.type);
  3557. file.lastModifiedDate = new Date();
  3558. // copy relative path
  3559. if (blob._relativePath) file._relativePath = blob._relativePath;
  3560. // if blob has name property, use as filename if no filename supplied
  3561. if (!isString(filename)) {
  3562. filename = getDateString();
  3563. }
  3564. // if filename supplied but no extension and filename has extension
  3565. if (filename && extension === null && getExtensionFromFilename(filename)) {
  3566. file.name = filename;
  3567. } else {
  3568. extension = extension || guesstimateExtension(file.type);
  3569. file.name = filename + (extension ? '.' + extension : '');
  3570. }
  3571. return file;
  3572. };
  3573. var getBlobBuilder = function getBlobBuilder() {
  3574. return (window.BlobBuilder =
  3575. window.BlobBuilder ||
  3576. window.WebKitBlobBuilder ||
  3577. window.MozBlobBuilder ||
  3578. window.MSBlobBuilder);
  3579. };
  3580. var createBlob = function createBlob(arrayBuffer, mimeType) {
  3581. var BB = getBlobBuilder();
  3582. if (BB) {
  3583. var bb = new BB();
  3584. bb.append(arrayBuffer);
  3585. return bb.getBlob(mimeType);
  3586. }
  3587. return new Blob([arrayBuffer], {
  3588. type: mimeType,
  3589. });
  3590. };
  3591. var getBlobFromByteStringWithMimeType = function getBlobFromByteStringWithMimeType(
  3592. byteString,
  3593. mimeType
  3594. ) {
  3595. var ab = new ArrayBuffer(byteString.length);
  3596. var ia = new Uint8Array(ab);
  3597. for (var i = 0; i < byteString.length; i++) {
  3598. ia[i] = byteString.charCodeAt(i);
  3599. }
  3600. return createBlob(ab, mimeType);
  3601. };
  3602. var getMimeTypeFromBase64DataURI = function getMimeTypeFromBase64DataURI(dataURI) {
  3603. return (/^data:(.+);/.exec(dataURI) || [])[1] || null;
  3604. };
  3605. var getBase64DataFromBase64DataURI = function getBase64DataFromBase64DataURI(dataURI) {
  3606. // get data part of string (remove data:image/jpeg...,)
  3607. var data = dataURI.split(',')[1];
  3608. // remove any whitespace as that causes InvalidCharacterError in IE
  3609. return data.replace(/\s/g, '');
  3610. };
  3611. var getByteStringFromBase64DataURI = function getByteStringFromBase64DataURI(dataURI) {
  3612. return atob(getBase64DataFromBase64DataURI(dataURI));
  3613. };
  3614. var getBlobFromBase64DataURI = function getBlobFromBase64DataURI(dataURI) {
  3615. var mimeType = getMimeTypeFromBase64DataURI(dataURI);
  3616. var byteString = getByteStringFromBase64DataURI(dataURI);
  3617. return getBlobFromByteStringWithMimeType(byteString, mimeType);
  3618. };
  3619. var getFileFromBase64DataURI = function getFileFromBase64DataURI(dataURI, filename, extension) {
  3620. return getFileFromBlob(getBlobFromBase64DataURI(dataURI), filename, null, extension);
  3621. };
  3622. var getFileNameFromHeader = function getFileNameFromHeader(header) {
  3623. // test if is content disposition header, if not exit
  3624. if (!/^content-disposition:/i.test(header)) return null;
  3625. // get filename parts
  3626. var matches = header
  3627. .split(/filename=|filename\*=.+''/)
  3628. .splice(1)
  3629. .map(function(name) {
  3630. return name.trim().replace(/^["']|[;"']{0,2}$/g, '');
  3631. })
  3632. .filter(function(name) {
  3633. return name.length;
  3634. });
  3635. return matches.length ? decodeURI(matches[matches.length - 1]) : null;
  3636. };
  3637. var getFileSizeFromHeader = function getFileSizeFromHeader(header) {
  3638. if (/content-length:/i.test(header)) {
  3639. var size = header.match(/[0-9]+/)[0];
  3640. return size ? parseInt(size, 10) : null;
  3641. }
  3642. return null;
  3643. };
  3644. var getTranfserIdFromHeader = function getTranfserIdFromHeader(header) {
  3645. if (/x-content-transfer-id:/i.test(header)) {
  3646. var id = (header.split(':')[1] || '').trim();
  3647. return id || null;
  3648. }
  3649. return null;
  3650. };
  3651. var getFileInfoFromHeaders = function getFileInfoFromHeaders(headers) {
  3652. var info = {
  3653. source: null,
  3654. name: null,
  3655. size: null,
  3656. };
  3657. var rows = headers.split('\n');
  3658. var _iteratorNormalCompletion = true;
  3659. var _didIteratorError = false;
  3660. var _iteratorError = undefined;
  3661. try {
  3662. for (
  3663. var _iterator = rows[Symbol.iterator](), _step;
  3664. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  3665. _iteratorNormalCompletion = true
  3666. ) {
  3667. var header = _step.value;
  3668. var name = getFileNameFromHeader(header);
  3669. if (name) {
  3670. info.name = name;
  3671. continue;
  3672. }
  3673. var size = getFileSizeFromHeader(header);
  3674. if (size) {
  3675. info.size = size;
  3676. continue;
  3677. }
  3678. var source = getTranfserIdFromHeader(header);
  3679. if (source) {
  3680. info.source = source;
  3681. continue;
  3682. }
  3683. }
  3684. } catch (err) {
  3685. _didIteratorError = true;
  3686. _iteratorError = err;
  3687. } finally {
  3688. try {
  3689. if (!_iteratorNormalCompletion && _iterator.return != null) {
  3690. _iterator.return();
  3691. }
  3692. } finally {
  3693. if (_didIteratorError) {
  3694. throw _iteratorError;
  3695. }
  3696. }
  3697. }
  3698. return info;
  3699. };
  3700. var createFileLoader = function createFileLoader(fetchFn) {
  3701. var state = {
  3702. source: null,
  3703. complete: false,
  3704. progress: 0,
  3705. size: null,
  3706. timestamp: null,
  3707. duration: 0,
  3708. request: null,
  3709. };
  3710. var getProgress = function getProgress() {
  3711. return state.progress;
  3712. };
  3713. var abort = function abort() {
  3714. if (state.request && state.request.abort) {
  3715. state.request.abort();
  3716. }
  3717. };
  3718. // load source
  3719. var load = function load() {
  3720. // get quick reference
  3721. var source = state.source;
  3722. api.fire('init', source);
  3723. // Load Files
  3724. if (source instanceof File) {
  3725. api.fire('load', source);
  3726. } else if (source instanceof Blob) {
  3727. // Load blobs, set default name to current date
  3728. api.fire('load', getFileFromBlob(source, source.name));
  3729. } else if (isBase64DataURI(source)) {
  3730. // Load base 64, set default name to current date
  3731. api.fire('load', getFileFromBase64DataURI(source));
  3732. } else {
  3733. // Deal as if is external URL, let's load it!
  3734. loadURL(source);
  3735. }
  3736. };
  3737. // loads a url
  3738. var loadURL = function loadURL(url) {
  3739. // is remote url and no fetch method supplied
  3740. if (!fetchFn) {
  3741. api.fire('error', {
  3742. type: 'error',
  3743. body: "Can't load URL",
  3744. code: 400,
  3745. });
  3746. return;
  3747. }
  3748. // set request start
  3749. state.timestamp = Date.now();
  3750. // load file
  3751. state.request = fetchFn(
  3752. url,
  3753. function(response) {
  3754. // update duration
  3755. state.duration = Date.now() - state.timestamp;
  3756. // done!
  3757. state.complete = true;
  3758. // turn blob response into a file
  3759. if (response instanceof Blob) {
  3760. response = getFileFromBlob(
  3761. response,
  3762. response.name || getFilenameFromURL(url)
  3763. );
  3764. }
  3765. api.fire(
  3766. 'load',
  3767. // if has received blob, we go with blob, if no response, we return null
  3768. response instanceof Blob ? response : response ? response.body : null
  3769. );
  3770. },
  3771. function(error) {
  3772. api.fire(
  3773. 'error',
  3774. typeof error === 'string'
  3775. ? {
  3776. type: 'error',
  3777. code: 0,
  3778. body: error,
  3779. }
  3780. : error
  3781. );
  3782. },
  3783. function(computable, current, total) {
  3784. // collected some meta data already
  3785. if (total) {
  3786. state.size = total;
  3787. }
  3788. // update duration
  3789. state.duration = Date.now() - state.timestamp;
  3790. // if we can't compute progress, we're not going to fire progress events
  3791. if (!computable) {
  3792. state.progress = null;
  3793. return;
  3794. }
  3795. // update progress percentage
  3796. state.progress = current / total;
  3797. // expose
  3798. api.fire('progress', state.progress);
  3799. },
  3800. function() {
  3801. api.fire('abort');
  3802. },
  3803. function(response) {
  3804. var fileinfo = getFileInfoFromHeaders(
  3805. typeof response === 'string' ? response : response.headers
  3806. );
  3807. api.fire('meta', {
  3808. size: state.size || fileinfo.size,
  3809. filename: fileinfo.name,
  3810. source: fileinfo.source,
  3811. });
  3812. }
  3813. );
  3814. };
  3815. var api = Object.assign({}, on(), {
  3816. setSource: function setSource(source) {
  3817. return (state.source = source);
  3818. },
  3819. getProgress: getProgress, // file load progress
  3820. abort: abort, // abort file load
  3821. load: load, // start load
  3822. });
  3823. return api;
  3824. };
  3825. var isGet = function isGet(method) {
  3826. return /GET|HEAD/.test(method);
  3827. };
  3828. var sendRequest = function sendRequest(data, url, options) {
  3829. var api = {
  3830. onheaders: function onheaders() {},
  3831. onprogress: function onprogress() {},
  3832. onload: function onload() {},
  3833. ontimeout: function ontimeout() {},
  3834. onerror: function onerror() {},
  3835. onabort: function onabort() {},
  3836. abort: function abort() {
  3837. aborted = true;
  3838. xhr.abort();
  3839. },
  3840. };
  3841. // timeout identifier, only used when timeout is defined
  3842. var aborted = false;
  3843. var headersReceived = false;
  3844. // set default options
  3845. options = Object.assign(
  3846. {
  3847. method: 'POST',
  3848. headers: {},
  3849. withCredentials: false,
  3850. },
  3851. options
  3852. );
  3853. // encode url
  3854. url = encodeURI(url);
  3855. // if method is GET, add any received data to url
  3856. if (isGet(options.method) && data) {
  3857. url =
  3858. '' +
  3859. url +
  3860. encodeURIComponent(typeof data === 'string' ? data : JSON.stringify(data));
  3861. }
  3862. // create request
  3863. var xhr = new XMLHttpRequest();
  3864. // progress of load
  3865. var process = isGet(options.method) ? xhr : xhr.upload;
  3866. process.onprogress = function(e) {
  3867. // no progress event when aborted ( onprogress is called once after abort() )
  3868. if (aborted) {
  3869. return;
  3870. }
  3871. api.onprogress(e.lengthComputable, e.loaded, e.total);
  3872. };
  3873. // tries to get header info to the app as fast as possible
  3874. xhr.onreadystatechange = function() {
  3875. // not interesting in these states ('unsent' and 'openend' as they don't give us any additional info)
  3876. if (xhr.readyState < 2) {
  3877. return;
  3878. }
  3879. // no server response
  3880. if (xhr.readyState === 4 && xhr.status === 0) {
  3881. return;
  3882. }
  3883. if (headersReceived) {
  3884. return;
  3885. }
  3886. headersReceived = true;
  3887. // we've probably received some useful data in response headers
  3888. api.onheaders(xhr);
  3889. };
  3890. // load successful
  3891. xhr.onload = function() {
  3892. // is classified as valid response
  3893. if (xhr.status >= 200 && xhr.status < 300) {
  3894. api.onload(xhr);
  3895. } else {
  3896. api.onerror(xhr);
  3897. }
  3898. };
  3899. // error during load
  3900. xhr.onerror = function() {
  3901. return api.onerror(xhr);
  3902. };
  3903. // request aborted
  3904. xhr.onabort = function() {
  3905. aborted = true;
  3906. api.onabort();
  3907. };
  3908. // request timeout
  3909. xhr.ontimeout = function() {
  3910. return api.ontimeout(xhr);
  3911. };
  3912. // open up open up!
  3913. xhr.open(options.method, url, true);
  3914. // set timeout if defined (do it after open so IE11 plays ball)
  3915. if (isInt(options.timeout)) {
  3916. xhr.timeout = options.timeout;
  3917. }
  3918. // add headers
  3919. Object.keys(options.headers).forEach(function(key) {
  3920. var value = unescape(encodeURIComponent(options.headers[key]));
  3921. xhr.setRequestHeader(key, value);
  3922. });
  3923. // set type of response
  3924. if (options.responseType) {
  3925. xhr.responseType = options.responseType;
  3926. }
  3927. // set credentials
  3928. if (options.withCredentials) {
  3929. xhr.withCredentials = true;
  3930. }
  3931. // let's send our data
  3932. xhr.send(data);
  3933. return api;
  3934. };
  3935. var createResponse = function createResponse(type, code, body, headers) {
  3936. return {
  3937. type: type,
  3938. code: code,
  3939. body: body,
  3940. headers: headers,
  3941. };
  3942. };
  3943. var createTimeoutResponse = function createTimeoutResponse(cb) {
  3944. return function(xhr) {
  3945. cb(createResponse('error', 0, 'Timeout', xhr.getAllResponseHeaders()));
  3946. };
  3947. };
  3948. var hasQS = function hasQS(str) {
  3949. return /\?/.test(str);
  3950. };
  3951. var buildURL = function buildURL() {
  3952. var url = '';
  3953. for (var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++) {
  3954. parts[_key] = arguments[_key];
  3955. }
  3956. parts.forEach(function(part) {
  3957. url += hasQS(url) && hasQS(part) ? part.replace(/\?/, '&') : part;
  3958. });
  3959. return url;
  3960. };
  3961. var createFetchFunction = function createFetchFunction() {
  3962. var apiUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  3963. var action = arguments.length > 1 ? arguments[1] : undefined;
  3964. // custom handler (should also handle file, load, error, progress and abort)
  3965. if (typeof action === 'function') {
  3966. return action;
  3967. }
  3968. // no action supplied
  3969. if (!action || !isString(action.url)) {
  3970. return null;
  3971. }
  3972. // set onload hanlder
  3973. var onload =
  3974. action.onload ||
  3975. function(res) {
  3976. return res;
  3977. };
  3978. var onerror =
  3979. action.onerror ||
  3980. function(res) {
  3981. return null;
  3982. };
  3983. // internal handler
  3984. return function(url, load, error, progress, abort, headers) {
  3985. // do local or remote request based on if the url is external
  3986. var request = sendRequest(
  3987. url,
  3988. buildURL(apiUrl, action.url),
  3989. Object.assign({}, action, {
  3990. responseType: 'blob',
  3991. })
  3992. );
  3993. request.onload = function(xhr) {
  3994. // get headers
  3995. var headers = xhr.getAllResponseHeaders();
  3996. // get filename
  3997. var filename = getFileInfoFromHeaders(headers).name || getFilenameFromURL(url);
  3998. // create response
  3999. load(
  4000. createResponse(
  4001. 'load',
  4002. xhr.status,
  4003. action.method === 'HEAD'
  4004. ? null
  4005. : getFileFromBlob(onload(xhr.response), filename),
  4006. headers
  4007. )
  4008. );
  4009. };
  4010. request.onerror = function(xhr) {
  4011. error(
  4012. createResponse(
  4013. 'error',
  4014. xhr.status,
  4015. onerror(xhr.response) || xhr.statusText,
  4016. xhr.getAllResponseHeaders()
  4017. )
  4018. );
  4019. };
  4020. request.onheaders = function(xhr) {
  4021. headers(createResponse('headers', xhr.status, null, xhr.getAllResponseHeaders()));
  4022. };
  4023. request.ontimeout = createTimeoutResponse(error);
  4024. request.onprogress = progress;
  4025. request.onabort = abort;
  4026. // should return request
  4027. return request;
  4028. };
  4029. };
  4030. var ChunkStatus = {
  4031. QUEUED: 0,
  4032. COMPLETE: 1,
  4033. PROCESSING: 2,
  4034. ERROR: 3,
  4035. WAITING: 4,
  4036. };
  4037. /*
  4038. function signature:
  4039. (file, metadata, load, error, progress, abort, transfer, options) => {
  4040. return {
  4041. abort:() => {}
  4042. }
  4043. }
  4044. */
  4045. // apiUrl, action, name, file, metadata, load, error, progress, abort, transfer, options
  4046. var processFileChunked = function processFileChunked(
  4047. apiUrl,
  4048. action,
  4049. name,
  4050. file,
  4051. metadata,
  4052. load,
  4053. error,
  4054. progress,
  4055. abort,
  4056. transfer,
  4057. options
  4058. ) {
  4059. // all chunks
  4060. var chunks = [];
  4061. var chunkTransferId = options.chunkTransferId,
  4062. chunkServer = options.chunkServer,
  4063. chunkSize = options.chunkSize,
  4064. chunkRetryDelays = options.chunkRetryDelays;
  4065. // default state
  4066. var state = {
  4067. serverId: chunkTransferId,
  4068. aborted: false,
  4069. };
  4070. // set onload handlers
  4071. var ondata =
  4072. action.ondata ||
  4073. function(fd) {
  4074. return fd;
  4075. };
  4076. var onload =
  4077. action.onload ||
  4078. function(xhr, method) {
  4079. return method === 'HEAD' ? xhr.getResponseHeader('Upload-Offset') : xhr.response;
  4080. };
  4081. var onerror =
  4082. action.onerror ||
  4083. function(res) {
  4084. return null;
  4085. };
  4086. // create server hook
  4087. var requestTransferId = function requestTransferId(cb) {
  4088. var formData = new FormData();
  4089. // add metadata under same name
  4090. if (isObject(metadata)) formData.append(name, JSON.stringify(metadata));
  4091. var headers =
  4092. typeof action.headers === 'function'
  4093. ? action.headers(file, metadata)
  4094. : Object.assign({}, action.headers, {
  4095. 'Upload-Length': file.size,
  4096. });
  4097. var requestParams = Object.assign({}, action, {
  4098. headers: headers,
  4099. });
  4100. // send request object
  4101. var request = sendRequest(
  4102. ondata(formData),
  4103. buildURL(apiUrl, action.url),
  4104. requestParams
  4105. );
  4106. request.onload = function(xhr) {
  4107. return cb(onload(xhr, requestParams.method));
  4108. };
  4109. request.onerror = function(xhr) {
  4110. return error(
  4111. createResponse(
  4112. 'error',
  4113. xhr.status,
  4114. onerror(xhr.response) || xhr.statusText,
  4115. xhr.getAllResponseHeaders()
  4116. )
  4117. );
  4118. };
  4119. request.ontimeout = createTimeoutResponse(error);
  4120. };
  4121. var requestTransferOffset = function requestTransferOffset(cb) {
  4122. var requestUrl = buildURL(apiUrl, chunkServer.url, state.serverId);
  4123. var headers =
  4124. typeof action.headers === 'function'
  4125. ? action.headers(state.serverId)
  4126. : Object.assign({}, action.headers);
  4127. var requestParams = {
  4128. headers: headers,
  4129. method: 'HEAD',
  4130. };
  4131. var request = sendRequest(null, requestUrl, requestParams);
  4132. request.onload = function(xhr) {
  4133. return cb(onload(xhr, requestParams.method));
  4134. };
  4135. request.onerror = function(xhr) {
  4136. return error(
  4137. createResponse(
  4138. 'error',
  4139. xhr.status,
  4140. onerror(xhr.response) || xhr.statusText,
  4141. xhr.getAllResponseHeaders()
  4142. )
  4143. );
  4144. };
  4145. request.ontimeout = createTimeoutResponse(error);
  4146. };
  4147. // create chunks
  4148. var lastChunkIndex = Math.floor(file.size / chunkSize);
  4149. for (var i = 0; i <= lastChunkIndex; i++) {
  4150. var offset = i * chunkSize;
  4151. var data = file.slice(offset, offset + chunkSize, 'application/offset+octet-stream');
  4152. chunks[i] = {
  4153. index: i,
  4154. size: data.size,
  4155. offset: offset,
  4156. data: data,
  4157. file: file,
  4158. progress: 0,
  4159. retries: _toConsumableArray(chunkRetryDelays),
  4160. status: ChunkStatus.QUEUED,
  4161. error: null,
  4162. request: null,
  4163. timeout: null,
  4164. };
  4165. }
  4166. var completeProcessingChunks = function completeProcessingChunks() {
  4167. return load(state.serverId);
  4168. };
  4169. var canProcessChunk = function canProcessChunk(chunk) {
  4170. return chunk.status === ChunkStatus.QUEUED || chunk.status === ChunkStatus.ERROR;
  4171. };
  4172. var processChunk = function processChunk(chunk) {
  4173. // processing is paused, wait here
  4174. if (state.aborted) return;
  4175. // get next chunk to process
  4176. chunk = chunk || chunks.find(canProcessChunk);
  4177. // no more chunks to process
  4178. if (!chunk) {
  4179. // all done?
  4180. if (
  4181. chunks.every(function(chunk) {
  4182. return chunk.status === ChunkStatus.COMPLETE;
  4183. })
  4184. ) {
  4185. completeProcessingChunks();
  4186. }
  4187. // no chunk to handle
  4188. return;
  4189. }
  4190. // now processing this chunk
  4191. chunk.status = ChunkStatus.PROCESSING;
  4192. chunk.progress = null;
  4193. // allow parsing of formdata
  4194. var ondata =
  4195. chunkServer.ondata ||
  4196. function(fd) {
  4197. return fd;
  4198. };
  4199. var onerror =
  4200. chunkServer.onerror ||
  4201. function(res) {
  4202. return null;
  4203. };
  4204. // send request object
  4205. var requestUrl = buildURL(apiUrl, chunkServer.url, state.serverId);
  4206. var headers =
  4207. typeof chunkServer.headers === 'function'
  4208. ? chunkServer.headers(chunk)
  4209. : Object.assign({}, chunkServer.headers, {
  4210. 'Content-Type': 'application/offset+octet-stream',
  4211. 'Upload-Offset': chunk.offset,
  4212. 'Upload-Length': file.size,
  4213. 'Upload-Name': file.name,
  4214. });
  4215. var request = (chunk.request = sendRequest(
  4216. ondata(chunk.data),
  4217. requestUrl,
  4218. Object.assign({}, chunkServer, {
  4219. headers: headers,
  4220. })
  4221. ));
  4222. request.onload = function() {
  4223. // done!
  4224. chunk.status = ChunkStatus.COMPLETE;
  4225. // remove request reference
  4226. chunk.request = null;
  4227. // start processing more chunks
  4228. processChunks();
  4229. };
  4230. request.onprogress = function(lengthComputable, loaded, total) {
  4231. chunk.progress = lengthComputable ? loaded : null;
  4232. updateTotalProgress();
  4233. };
  4234. request.onerror = function(xhr) {
  4235. chunk.status = ChunkStatus.ERROR;
  4236. chunk.request = null;
  4237. chunk.error = onerror(xhr.response) || xhr.statusText;
  4238. if (!retryProcessChunk(chunk)) {
  4239. error(
  4240. createResponse(
  4241. 'error',
  4242. xhr.status,
  4243. onerror(xhr.response) || xhr.statusText,
  4244. xhr.getAllResponseHeaders()
  4245. )
  4246. );
  4247. }
  4248. };
  4249. request.ontimeout = function(xhr) {
  4250. chunk.status = ChunkStatus.ERROR;
  4251. chunk.request = null;
  4252. if (!retryProcessChunk(chunk)) {
  4253. createTimeoutResponse(error)(xhr);
  4254. }
  4255. };
  4256. request.onabort = function() {
  4257. chunk.status = ChunkStatus.QUEUED;
  4258. chunk.request = null;
  4259. abort();
  4260. };
  4261. };
  4262. var retryProcessChunk = function retryProcessChunk(chunk) {
  4263. // no more retries left
  4264. if (chunk.retries.length === 0) return false;
  4265. // new retry
  4266. chunk.status = ChunkStatus.WAITING;
  4267. clearTimeout(chunk.timeout);
  4268. chunk.timeout = setTimeout(function() {
  4269. processChunk(chunk);
  4270. }, chunk.retries.shift());
  4271. // we're going to retry
  4272. return true;
  4273. };
  4274. var updateTotalProgress = function updateTotalProgress() {
  4275. // calculate total progress fraction
  4276. var totalBytesTransfered = chunks.reduce(function(p, chunk) {
  4277. if (p === null || chunk.progress === null) return null;
  4278. return p + chunk.progress;
  4279. }, 0);
  4280. // can't compute progress
  4281. if (totalBytesTransfered === null) return progress(false, 0, 0);
  4282. // calculate progress values
  4283. var totalSize = chunks.reduce(function(total, chunk) {
  4284. return total + chunk.size;
  4285. }, 0);
  4286. // can update progress indicator
  4287. progress(true, totalBytesTransfered, totalSize);
  4288. };
  4289. // process new chunks
  4290. var processChunks = function processChunks() {
  4291. var totalProcessing = chunks.filter(function(chunk) {
  4292. return chunk.status === ChunkStatus.PROCESSING;
  4293. }).length;
  4294. if (totalProcessing >= 1) return;
  4295. processChunk();
  4296. };
  4297. var abortChunks = function abortChunks() {
  4298. chunks.forEach(function(chunk) {
  4299. clearTimeout(chunk.timeout);
  4300. if (chunk.request) {
  4301. chunk.request.abort();
  4302. }
  4303. });
  4304. };
  4305. // let's go!
  4306. if (!state.serverId) {
  4307. requestTransferId(function(serverId) {
  4308. // stop here if aborted, might have happened in between request and callback
  4309. if (state.aborted) return;
  4310. // pass back to item so we can use it if something goes wrong
  4311. transfer(serverId);
  4312. // store internally
  4313. state.serverId = serverId;
  4314. processChunks();
  4315. });
  4316. } else {
  4317. requestTransferOffset(function(offset) {
  4318. // stop here if aborted, might have happened in between request and callback
  4319. if (state.aborted) return;
  4320. // mark chunks with lower offset as complete
  4321. chunks
  4322. .filter(function(chunk) {
  4323. return chunk.offset < offset;
  4324. })
  4325. .forEach(function(chunk) {
  4326. chunk.status = ChunkStatus.COMPLETE;
  4327. chunk.progress = chunk.size;
  4328. });
  4329. // continue processing
  4330. processChunks();
  4331. });
  4332. }
  4333. return {
  4334. abort: function abort() {
  4335. state.aborted = true;
  4336. abortChunks();
  4337. },
  4338. };
  4339. };
  4340. /*
  4341. function signature:
  4342. (file, metadata, load, error, progress, abort) => {
  4343. return {
  4344. abort:() => {}
  4345. }
  4346. }
  4347. */
  4348. var createFileProcessorFunction = function createFileProcessorFunction(
  4349. apiUrl,
  4350. action,
  4351. name,
  4352. options
  4353. ) {
  4354. return function(file, metadata, load, error, progress, abort, transfer) {
  4355. // no file received
  4356. if (!file) return;
  4357. // if was passed a file, and we can chunk it, exit here
  4358. var canChunkUpload = options.chunkUploads;
  4359. var shouldChunkUpload = canChunkUpload && file.size > options.chunkSize;
  4360. var willChunkUpload = canChunkUpload && (shouldChunkUpload || options.chunkForce);
  4361. if (file instanceof Blob && willChunkUpload)
  4362. return processFileChunked(
  4363. apiUrl,
  4364. action,
  4365. name,
  4366. file,
  4367. metadata,
  4368. load,
  4369. error,
  4370. progress,
  4371. abort,
  4372. transfer,
  4373. options
  4374. );
  4375. // set handlers
  4376. var ondata =
  4377. action.ondata ||
  4378. function(fd) {
  4379. return fd;
  4380. };
  4381. var onload =
  4382. action.onload ||
  4383. function(res) {
  4384. return res;
  4385. };
  4386. var onerror =
  4387. action.onerror ||
  4388. function(res) {
  4389. return null;
  4390. };
  4391. var headers =
  4392. typeof action.headers === 'function'
  4393. ? action.headers(file, metadata) || {}
  4394. : Object.assign(
  4395. {},
  4396. action.headers
  4397. );
  4398. var requestParams = Object.assign({}, action, {
  4399. headers: headers,
  4400. });
  4401. // create formdata object
  4402. var formData = new FormData();
  4403. // add metadata under same name
  4404. if (isObject(metadata)) {
  4405. formData.append(name, JSON.stringify(metadata));
  4406. }
  4407. // Turn into an array of objects so no matter what the input, we can handle it the same way
  4408. (file instanceof Blob ? [{ name: null, file: file }] : file).forEach(function(item) {
  4409. formData.append(
  4410. name,
  4411. item.file,
  4412. item.name === null ? item.file.name : '' + item.name + item.file.name
  4413. );
  4414. });
  4415. // send request object
  4416. var request = sendRequest(
  4417. ondata(formData),
  4418. buildURL(apiUrl, action.url),
  4419. requestParams
  4420. );
  4421. request.onload = function(xhr) {
  4422. load(
  4423. createResponse(
  4424. 'load',
  4425. xhr.status,
  4426. onload(xhr.response),
  4427. xhr.getAllResponseHeaders()
  4428. )
  4429. );
  4430. };
  4431. request.onerror = function(xhr) {
  4432. error(
  4433. createResponse(
  4434. 'error',
  4435. xhr.status,
  4436. onerror(xhr.response) || xhr.statusText,
  4437. xhr.getAllResponseHeaders()
  4438. )
  4439. );
  4440. };
  4441. request.ontimeout = createTimeoutResponse(error);
  4442. request.onprogress = progress;
  4443. request.onabort = abort;
  4444. // should return request
  4445. return request;
  4446. };
  4447. };
  4448. var createProcessorFunction = function createProcessorFunction() {
  4449. var apiUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  4450. var action = arguments.length > 1 ? arguments[1] : undefined;
  4451. var name = arguments.length > 2 ? arguments[2] : undefined;
  4452. var options = arguments.length > 3 ? arguments[3] : undefined;
  4453. // custom handler (should also handle file, load, error, progress and abort)
  4454. if (typeof action === 'function')
  4455. return function() {
  4456. for (
  4457. var _len = arguments.length, params = new Array(_len), _key = 0;
  4458. _key < _len;
  4459. _key++
  4460. ) {
  4461. params[_key] = arguments[_key];
  4462. }
  4463. return action.apply(void 0, [name].concat(params, [options]));
  4464. };
  4465. // no action supplied
  4466. if (!action || !isString(action.url)) return null;
  4467. // internal handler
  4468. return createFileProcessorFunction(apiUrl, action, name, options);
  4469. };
  4470. /*
  4471. function signature:
  4472. (uniqueFileId, load, error) => { }
  4473. */
  4474. var createRevertFunction = function createRevertFunction() {
  4475. var apiUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  4476. var action = arguments.length > 1 ? arguments[1] : undefined;
  4477. // is custom implementation
  4478. if (typeof action === 'function') {
  4479. return action;
  4480. }
  4481. // no action supplied, return stub function, interface will work, but file won't be removed
  4482. if (!action || !isString(action.url)) {
  4483. return function(uniqueFileId, load) {
  4484. return load();
  4485. };
  4486. }
  4487. // set onload hanlder
  4488. var onload =
  4489. action.onload ||
  4490. function(res) {
  4491. return res;
  4492. };
  4493. var onerror =
  4494. action.onerror ||
  4495. function(res) {
  4496. return null;
  4497. };
  4498. // internal implementation
  4499. return function(uniqueFileId, load, error) {
  4500. var request = sendRequest(
  4501. uniqueFileId,
  4502. apiUrl + action.url,
  4503. action // contains method, headers and withCredentials properties
  4504. );
  4505. request.onload = function(xhr) {
  4506. load(
  4507. createResponse(
  4508. 'load',
  4509. xhr.status,
  4510. onload(xhr.response),
  4511. xhr.getAllResponseHeaders()
  4512. )
  4513. );
  4514. };
  4515. request.onerror = function(xhr) {
  4516. error(
  4517. createResponse(
  4518. 'error',
  4519. xhr.status,
  4520. onerror(xhr.response) || xhr.statusText,
  4521. xhr.getAllResponseHeaders()
  4522. )
  4523. );
  4524. };
  4525. request.ontimeout = createTimeoutResponse(error);
  4526. return request;
  4527. };
  4528. };
  4529. var getRandomNumber = function getRandomNumber() {
  4530. var min = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  4531. var max = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  4532. return min + Math.random() * (max - min);
  4533. };
  4534. var createPerceivedPerformanceUpdater = function createPerceivedPerformanceUpdater(cb) {
  4535. var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
  4536. var offset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  4537. var tickMin = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 25;
  4538. var tickMax = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 250;
  4539. var timeout = null;
  4540. var start = Date.now();
  4541. var tick = function tick() {
  4542. var runtime = Date.now() - start;
  4543. var delay = getRandomNumber(tickMin, tickMax);
  4544. if (runtime + delay > duration) {
  4545. delay = runtime + delay - duration;
  4546. }
  4547. var progress = runtime / duration;
  4548. if (progress >= 1 || document.hidden) {
  4549. cb(1);
  4550. return;
  4551. }
  4552. cb(progress);
  4553. timeout = setTimeout(tick, delay);
  4554. };
  4555. if (duration > 0) tick();
  4556. return {
  4557. clear: function clear() {
  4558. clearTimeout(timeout);
  4559. },
  4560. };
  4561. };
  4562. var createFileProcessor = function createFileProcessor(processFn, options) {
  4563. var state = {
  4564. complete: false,
  4565. perceivedProgress: 0,
  4566. perceivedPerformanceUpdater: null,
  4567. progress: null,
  4568. timestamp: null,
  4569. perceivedDuration: 0,
  4570. duration: 0,
  4571. request: null,
  4572. response: null,
  4573. };
  4574. var allowMinimumUploadDuration = options.allowMinimumUploadDuration;
  4575. var process = function process(file, metadata) {
  4576. var progressFn = function progressFn() {
  4577. // we've not yet started the real download, stop here
  4578. // the request might not go through, for instance, there might be some server trouble
  4579. // if state.progress is null, the server does not allow computing progress and we show the spinner instead
  4580. if (state.duration === 0 || state.progress === null) return;
  4581. // as we're now processing, fire the progress event
  4582. api.fire('progress', api.getProgress());
  4583. };
  4584. var completeFn = function completeFn() {
  4585. state.complete = true;
  4586. api.fire('load-perceived', state.response.body);
  4587. };
  4588. // let's start processing
  4589. api.fire('start');
  4590. // set request start
  4591. state.timestamp = Date.now();
  4592. // create perceived performance progress indicator
  4593. state.perceivedPerformanceUpdater = createPerceivedPerformanceUpdater(
  4594. function(progress) {
  4595. state.perceivedProgress = progress;
  4596. state.perceivedDuration = Date.now() - state.timestamp;
  4597. progressFn();
  4598. // if fake progress is done, and a response has been received,
  4599. // and we've not yet called the complete method
  4600. if (state.response && state.perceivedProgress === 1 && !state.complete) {
  4601. // we done!
  4602. completeFn();
  4603. }
  4604. },
  4605. // random delay as in a list of files you start noticing
  4606. // files uploading at the exact same speed
  4607. allowMinimumUploadDuration ? getRandomNumber(750, 1500) : 0
  4608. );
  4609. // remember request so we can abort it later
  4610. state.request = processFn(
  4611. // the file to process
  4612. file,
  4613. // the metadata to send along
  4614. metadata,
  4615. // callbacks (load, error, progress, abort, transfer)
  4616. // load expects the body to be a server id if
  4617. // you want to make use of revert
  4618. function(response) {
  4619. // we put the response in state so we can access
  4620. // it outside of this method
  4621. state.response = isObject(response)
  4622. ? response
  4623. : {
  4624. type: 'load',
  4625. code: 200,
  4626. body: '' + response,
  4627. headers: {},
  4628. };
  4629. // update duration
  4630. state.duration = Date.now() - state.timestamp;
  4631. // force progress to 1 as we're now done
  4632. state.progress = 1;
  4633. // actual load is done let's share results
  4634. api.fire('load', state.response.body);
  4635. // we are really done
  4636. // if perceived progress is 1 ( wait for perceived progress to complete )
  4637. // or if server does not support progress ( null )
  4638. if (
  4639. !allowMinimumUploadDuration ||
  4640. (allowMinimumUploadDuration && state.perceivedProgress === 1)
  4641. ) {
  4642. completeFn();
  4643. }
  4644. },
  4645. // error is expected to be an object with type, code, body
  4646. function(error) {
  4647. // cancel updater
  4648. state.perceivedPerformanceUpdater.clear();
  4649. // update others about this error
  4650. api.fire(
  4651. 'error',
  4652. isObject(error)
  4653. ? error
  4654. : {
  4655. type: 'error',
  4656. code: 0,
  4657. body: '' + error,
  4658. }
  4659. );
  4660. },
  4661. // actual processing progress
  4662. function(computable, current, total) {
  4663. // update actual duration
  4664. state.duration = Date.now() - state.timestamp;
  4665. // update actual progress
  4666. state.progress = computable ? current / total : null;
  4667. progressFn();
  4668. },
  4669. // abort does not expect a value
  4670. function() {
  4671. // stop updater
  4672. state.perceivedPerformanceUpdater.clear();
  4673. // fire the abort event so we can switch visuals
  4674. api.fire('abort', state.response ? state.response.body : null);
  4675. },
  4676. // register the id for this transfer
  4677. function(transferId) {
  4678. api.fire('transfer', transferId);
  4679. }
  4680. );
  4681. };
  4682. var abort = function abort() {
  4683. // no request running, can't abort
  4684. if (!state.request) return;
  4685. // stop updater
  4686. state.perceivedPerformanceUpdater.clear();
  4687. // abort actual request
  4688. if (state.request.abort) state.request.abort();
  4689. // if has response object, we've completed the request
  4690. state.complete = true;
  4691. };
  4692. var reset = function reset() {
  4693. abort();
  4694. state.complete = false;
  4695. state.perceivedProgress = 0;
  4696. state.progress = 0;
  4697. state.timestamp = null;
  4698. state.perceivedDuration = 0;
  4699. state.duration = 0;
  4700. state.request = null;
  4701. state.response = null;
  4702. };
  4703. var getProgress = allowMinimumUploadDuration
  4704. ? function() {
  4705. return state.progress ? Math.min(state.progress, state.perceivedProgress) : null;
  4706. }
  4707. : function() {
  4708. return state.progress || null;
  4709. };
  4710. var getDuration = allowMinimumUploadDuration
  4711. ? function() {
  4712. return Math.min(state.duration, state.perceivedDuration);
  4713. }
  4714. : function() {
  4715. return state.duration;
  4716. };
  4717. var api = Object.assign({}, on(), {
  4718. process: process, // start processing file
  4719. abort: abort, // abort active process request
  4720. getProgress: getProgress,
  4721. getDuration: getDuration,
  4722. reset: reset,
  4723. });
  4724. return api;
  4725. };
  4726. var getFilenameWithoutExtension = function getFilenameWithoutExtension(name) {
  4727. return name.substring(0, name.lastIndexOf('.')) || name;
  4728. };
  4729. var createFileStub = function createFileStub(source) {
  4730. var data = [source.name, source.size, source.type];
  4731. // is blob or base64, then we need to set the name
  4732. if (source instanceof Blob || isBase64DataURI(source)) {
  4733. data[0] = source.name || getDateString();
  4734. } else if (isBase64DataURI(source)) {
  4735. // if is base64 data uri we need to determine the average size and type
  4736. data[1] = source.length;
  4737. data[2] = getMimeTypeFromBase64DataURI(source);
  4738. } else if (isString(source)) {
  4739. // url
  4740. data[0] = getFilenameFromURL(source);
  4741. data[1] = 0;
  4742. data[2] = 'application/octet-stream';
  4743. }
  4744. return {
  4745. name: data[0],
  4746. size: data[1],
  4747. type: data[2],
  4748. };
  4749. };
  4750. var isFile = function isFile(value) {
  4751. return !!(value instanceof File || (value instanceof Blob && value.name));
  4752. };
  4753. var deepCloneObject = function deepCloneObject(src) {
  4754. if (!isObject(src)) return src;
  4755. var target = isArray(src) ? [] : {};
  4756. for (var key in src) {
  4757. if (!src.hasOwnProperty(key)) continue;
  4758. var v = src[key];
  4759. target[key] = v && isObject(v) ? deepCloneObject(v) : v;
  4760. }
  4761. return target;
  4762. };
  4763. var createItem = function createItem() {
  4764. var origin = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  4765. var serverFileReference =
  4766. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  4767. var file = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  4768. // unique id for this item, is used to identify the item across views
  4769. var id = getUniqueId();
  4770. /**
  4771. * Internal item state
  4772. */
  4773. var state = {
  4774. // is archived
  4775. archived: false,
  4776. // if is frozen, no longer fires events
  4777. frozen: false,
  4778. // removed from view
  4779. released: false,
  4780. // original source
  4781. source: null,
  4782. // file model reference
  4783. file: file,
  4784. // id of file on server
  4785. serverFileReference: serverFileReference,
  4786. // id of file transfer on server
  4787. transferId: null,
  4788. // is aborted
  4789. processingAborted: false,
  4790. // current item status
  4791. status: serverFileReference ? ItemStatus.PROCESSING_COMPLETE : ItemStatus.INIT,
  4792. // active processes
  4793. activeLoader: null,
  4794. activeProcessor: null,
  4795. };
  4796. // callback used when abort processing is called to link back to the resolve method
  4797. var abortProcessingRequestComplete = null;
  4798. /**
  4799. * Externally added item metadata
  4800. */
  4801. var metadata = {};
  4802. // item data
  4803. var setStatus = function setStatus(status) {
  4804. return (state.status = status);
  4805. };
  4806. // fire event unless the item has been archived
  4807. var fire = function fire(event) {
  4808. if (state.released || state.frozen) return;
  4809. for (
  4810. var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1;
  4811. _key < _len;
  4812. _key++
  4813. ) {
  4814. params[_key - 1] = arguments[_key];
  4815. }
  4816. api.fire.apply(api, [event].concat(params));
  4817. };
  4818. // file data
  4819. var getFileExtension = function getFileExtension() {
  4820. return getExtensionFromFilename(state.file.name);
  4821. };
  4822. var getFileType = function getFileType() {
  4823. return state.file.type;
  4824. };
  4825. var getFileSize = function getFileSize() {
  4826. return state.file.size;
  4827. };
  4828. var getFile = function getFile() {
  4829. return state.file;
  4830. };
  4831. //
  4832. // logic to load a file
  4833. //
  4834. var load = function load(source, loader, onload) {
  4835. // remember the original item source
  4836. state.source = source;
  4837. // source is known
  4838. api.fireSync('init');
  4839. // file stub is already there
  4840. if (state.file) {
  4841. api.fireSync('load-skip');
  4842. return;
  4843. }
  4844. // set a stub file object while loading the actual data
  4845. state.file = createFileStub(source);
  4846. // starts loading
  4847. loader.on('init', function() {
  4848. fire('load-init');
  4849. });
  4850. // we'eve received a size indication, let's update the stub
  4851. loader.on('meta', function(meta) {
  4852. // set size of file stub
  4853. state.file.size = meta.size;
  4854. // set name of file stub
  4855. state.file.filename = meta.filename;
  4856. // if has received source, we done
  4857. if (meta.source) {
  4858. origin = FileOrigin.LIMBO;
  4859. state.serverFileReference = meta.source;
  4860. state.status = ItemStatus.PROCESSING_COMPLETE;
  4861. }
  4862. // size has been updated
  4863. fire('load-meta');
  4864. });
  4865. // the file is now loading we need to update the progress indicators
  4866. loader.on('progress', function(progress) {
  4867. setStatus(ItemStatus.LOADING);
  4868. fire('load-progress', progress);
  4869. });
  4870. // an error was thrown while loading the file, we need to switch to error state
  4871. loader.on('error', function(error) {
  4872. setStatus(ItemStatus.LOAD_ERROR);
  4873. fire('load-request-error', error);
  4874. });
  4875. // user or another process aborted the file load (cannot retry)
  4876. loader.on('abort', function() {
  4877. setStatus(ItemStatus.INIT);
  4878. fire('load-abort');
  4879. });
  4880. // done loading
  4881. loader.on('load', function(file) {
  4882. // as we've now loaded the file the loader is no longer required
  4883. state.activeLoader = null;
  4884. // called when file has loaded succesfully
  4885. var success = function success(result) {
  4886. // set (possibly) transformed file
  4887. state.file = isFile(result) ? result : state.file;
  4888. // file received
  4889. if (origin === FileOrigin.LIMBO && state.serverFileReference) {
  4890. setStatus(ItemStatus.PROCESSING_COMPLETE);
  4891. } else {
  4892. setStatus(ItemStatus.IDLE);
  4893. }
  4894. fire('load');
  4895. };
  4896. var error = function error(result) {
  4897. // set original file
  4898. state.file = file;
  4899. fire('load-meta');
  4900. setStatus(ItemStatus.LOAD_ERROR);
  4901. fire('load-file-error', result);
  4902. };
  4903. // if we already have a server file reference, we don't need to call the onload method
  4904. if (state.serverFileReference) {
  4905. success(file);
  4906. return;
  4907. }
  4908. // no server id, let's give this file the full treatment
  4909. onload(file, success, error);
  4910. });
  4911. // set loader source data
  4912. loader.setSource(source);
  4913. // set as active loader
  4914. state.activeLoader = loader;
  4915. // load the source data
  4916. loader.load();
  4917. };
  4918. var retryLoad = function retryLoad() {
  4919. if (!state.activeLoader) {
  4920. return;
  4921. }
  4922. state.activeLoader.load();
  4923. };
  4924. var abortLoad = function abortLoad() {
  4925. if (state.activeLoader) {
  4926. state.activeLoader.abort();
  4927. return;
  4928. }
  4929. setStatus(ItemStatus.INIT);
  4930. fire('load-abort');
  4931. };
  4932. //
  4933. // logic to process a file
  4934. //
  4935. var process = function process(processor, onprocess) {
  4936. // processing was aborted
  4937. if (state.processingAborted) {
  4938. state.processingAborted = false;
  4939. return;
  4940. }
  4941. // now processing
  4942. setStatus(ItemStatus.PROCESSING);
  4943. // reset abort callback
  4944. abortProcessingRequestComplete = null;
  4945. // if no file loaded we'll wait for the load event
  4946. if (!(state.file instanceof Blob)) {
  4947. api.on('load', function() {
  4948. process(processor, onprocess);
  4949. });
  4950. return;
  4951. }
  4952. // setup processor
  4953. processor.on('load', function(serverFileReference) {
  4954. // need this id to be able to revert the upload
  4955. state.transferId = null;
  4956. state.serverFileReference = serverFileReference;
  4957. });
  4958. // register transfer id
  4959. processor.on('transfer', function(transferId) {
  4960. // need this id to be able to revert the upload
  4961. state.transferId = transferId;
  4962. });
  4963. processor.on('load-perceived', function(serverFileReference) {
  4964. // no longer required
  4965. state.activeProcessor = null;
  4966. // need this id to be able to rever the upload
  4967. state.transferId = null;
  4968. state.serverFileReference = serverFileReference;
  4969. setStatus(ItemStatus.PROCESSING_COMPLETE);
  4970. fire('process-complete', serverFileReference);
  4971. });
  4972. processor.on('start', function() {
  4973. fire('process-start');
  4974. });
  4975. processor.on('error', function(error) {
  4976. state.activeProcessor = null;
  4977. setStatus(ItemStatus.PROCESSING_ERROR);
  4978. fire('process-error', error);
  4979. });
  4980. processor.on('abort', function(serverFileReference) {
  4981. state.activeProcessor = null;
  4982. // if file was uploaded but processing was cancelled during perceived processor time store file reference
  4983. state.serverFileReference = serverFileReference;
  4984. setStatus(ItemStatus.IDLE);
  4985. fire('process-abort');
  4986. // has timeout so doesn't interfere with remove action
  4987. if (abortProcessingRequestComplete) {
  4988. abortProcessingRequestComplete();
  4989. }
  4990. });
  4991. processor.on('progress', function(progress) {
  4992. fire('process-progress', progress);
  4993. });
  4994. // when successfully transformed
  4995. var success = function success(file) {
  4996. // if was archived in the mean time, don't process
  4997. if (state.archived) return;
  4998. // process file!
  4999. processor.process(file, Object.assign({}, metadata));
  5000. };
  5001. // something went wrong during transform phase
  5002. var error = console.error;
  5003. // start processing the file
  5004. onprocess(state.file, success, error);
  5005. // set as active processor
  5006. state.activeProcessor = processor;
  5007. };
  5008. var requestProcessing = function requestProcessing() {
  5009. state.processingAborted = false;
  5010. setStatus(ItemStatus.PROCESSING_QUEUED);
  5011. };
  5012. var abortProcessing = function abortProcessing() {
  5013. return new Promise(function(resolve) {
  5014. if (!state.activeProcessor) {
  5015. state.processingAborted = true;
  5016. setStatus(ItemStatus.IDLE);
  5017. fire('process-abort');
  5018. resolve();
  5019. return;
  5020. }
  5021. abortProcessingRequestComplete = function abortProcessingRequestComplete() {
  5022. resolve();
  5023. };
  5024. state.activeProcessor.abort();
  5025. });
  5026. };
  5027. //
  5028. // logic to revert a processed file
  5029. //
  5030. var revert = function revert(revertFileUpload, forceRevert) {
  5031. return new Promise(function(resolve, reject) {
  5032. // a completed upload will have a serverFileReference, a failed chunked upload where
  5033. // getting a serverId succeeded but >=0 chunks have been uploaded will have transferId set
  5034. var serverTransferId =
  5035. state.serverFileReference !== null
  5036. ? state.serverFileReference
  5037. : state.transferId;
  5038. // cannot revert without a server id for this process
  5039. if (serverTransferId === null) {
  5040. resolve();
  5041. return;
  5042. }
  5043. // revert the upload (fire and forget)
  5044. revertFileUpload(
  5045. serverTransferId,
  5046. function() {
  5047. // reset file server id and transfer id as now it's not available on the server
  5048. state.serverFileReference = null;
  5049. state.transferId = null;
  5050. resolve();
  5051. },
  5052. function(error) {
  5053. // don't set error state when reverting is optional, it will always resolve
  5054. if (!forceRevert) {
  5055. resolve();
  5056. return;
  5057. }
  5058. // oh no errors
  5059. setStatus(ItemStatus.PROCESSING_REVERT_ERROR);
  5060. fire('process-revert-error');
  5061. reject(error);
  5062. }
  5063. );
  5064. // fire event
  5065. setStatus(ItemStatus.IDLE);
  5066. fire('process-revert');
  5067. });
  5068. };
  5069. // exposed methods
  5070. var _setMetadata = function setMetadata(key, value, silent) {
  5071. var keys = key.split('.');
  5072. var root = keys[0];
  5073. var last = keys.pop();
  5074. var data = metadata;
  5075. keys.forEach(function(key) {
  5076. return (data = data[key]);
  5077. });
  5078. // compare old value against new value, if they're the same, we're not updating
  5079. if (JSON.stringify(data[last]) === JSON.stringify(value)) return;
  5080. // update value
  5081. data[last] = value;
  5082. // fire update
  5083. fire('metadata-update', {
  5084. key: root,
  5085. value: metadata[root],
  5086. silent: silent,
  5087. });
  5088. };
  5089. var getMetadata = function getMetadata(key) {
  5090. return deepCloneObject(key ? metadata[key] : metadata);
  5091. };
  5092. var api = Object.assign(
  5093. {
  5094. id: {
  5095. get: function get() {
  5096. return id;
  5097. },
  5098. },
  5099. origin: {
  5100. get: function get() {
  5101. return origin;
  5102. },
  5103. set: function set(value) {
  5104. return (origin = value);
  5105. },
  5106. },
  5107. serverId: {
  5108. get: function get() {
  5109. return state.serverFileReference;
  5110. },
  5111. },
  5112. transferId: {
  5113. get: function get() {
  5114. return state.transferId;
  5115. },
  5116. },
  5117. status: {
  5118. get: function get() {
  5119. return state.status;
  5120. },
  5121. },
  5122. filename: {
  5123. get: function get() {
  5124. return state.file.name;
  5125. },
  5126. },
  5127. filenameWithoutExtension: {
  5128. get: function get() {
  5129. return getFilenameWithoutExtension(state.file.name);
  5130. },
  5131. },
  5132. fileExtension: { get: getFileExtension },
  5133. fileType: { get: getFileType },
  5134. fileSize: { get: getFileSize },
  5135. file: { get: getFile },
  5136. relativePath: {
  5137. get: function get() {
  5138. return state.file._relativePath;
  5139. },
  5140. },
  5141. source: {
  5142. get: function get() {
  5143. return state.source;
  5144. },
  5145. },
  5146. getMetadata: getMetadata,
  5147. setMetadata: function setMetadata(key, value, silent) {
  5148. if (isObject(key)) {
  5149. var data = key;
  5150. Object.keys(data).forEach(function(key) {
  5151. _setMetadata(key, data[key], value);
  5152. });
  5153. return key;
  5154. }
  5155. _setMetadata(key, value, silent);
  5156. return value;
  5157. },
  5158. extend: function extend(name, handler) {
  5159. return (itemAPI[name] = handler);
  5160. },
  5161. abortLoad: abortLoad,
  5162. retryLoad: retryLoad,
  5163. requestProcessing: requestProcessing,
  5164. abortProcessing: abortProcessing,
  5165. load: load,
  5166. process: process,
  5167. revert: revert,
  5168. },
  5169. on(),
  5170. {
  5171. freeze: function freeze() {
  5172. return (state.frozen = true);
  5173. },
  5174. release: function release() {
  5175. return (state.released = true);
  5176. },
  5177. released: {
  5178. get: function get() {
  5179. return state.released;
  5180. },
  5181. },
  5182. archive: function archive() {
  5183. return (state.archived = true);
  5184. },
  5185. archived: {
  5186. get: function get() {
  5187. return state.archived;
  5188. },
  5189. },
  5190. }
  5191. );
  5192. // create it here instead of returning it instantly so we can extend it later
  5193. var itemAPI = createObject(api);
  5194. return itemAPI;
  5195. };
  5196. var getItemIndexByQuery = function getItemIndexByQuery(items, query) {
  5197. // just return first index
  5198. if (isEmpty(query)) {
  5199. return 0;
  5200. }
  5201. // invalid queries
  5202. if (!isString(query)) {
  5203. return -1;
  5204. }
  5205. // return item by id (or -1 if not found)
  5206. return items.findIndex(function(item) {
  5207. return item.id === query;
  5208. });
  5209. };
  5210. var getItemById = function getItemById(items, itemId) {
  5211. var index = getItemIndexByQuery(items, itemId);
  5212. if (index < 0) {
  5213. return;
  5214. }
  5215. return items[index] || null;
  5216. };
  5217. var fetchBlob = function fetchBlob(url, load, error, progress, abort, headers) {
  5218. var request = sendRequest(null, url, {
  5219. method: 'GET',
  5220. responseType: 'blob',
  5221. });
  5222. request.onload = function(xhr) {
  5223. // get headers
  5224. var headers = xhr.getAllResponseHeaders();
  5225. // get filename
  5226. var filename = getFileInfoFromHeaders(headers).name || getFilenameFromURL(url);
  5227. // create response
  5228. load(
  5229. createResponse('load', xhr.status, getFileFromBlob(xhr.response, filename), headers)
  5230. );
  5231. };
  5232. request.onerror = function(xhr) {
  5233. error(createResponse('error', xhr.status, xhr.statusText, xhr.getAllResponseHeaders()));
  5234. };
  5235. request.onheaders = function(xhr) {
  5236. headers(createResponse('headers', xhr.status, null, xhr.getAllResponseHeaders()));
  5237. };
  5238. request.ontimeout = createTimeoutResponse(error);
  5239. request.onprogress = progress;
  5240. request.onabort = abort;
  5241. // should return request
  5242. return request;
  5243. };
  5244. var getDomainFromURL = function getDomainFromURL(url) {
  5245. if (url.indexOf('//') === 0) {
  5246. url = location.protocol + url;
  5247. }
  5248. return url
  5249. .toLowerCase()
  5250. .replace('blob:', '')
  5251. .replace(/([a-z])?:\/\//, '$1')
  5252. .split('/')[0];
  5253. };
  5254. var isExternalURL = function isExternalURL(url) {
  5255. return (
  5256. (url.indexOf(':') > -1 || url.indexOf('//') > -1) &&
  5257. getDomainFromURL(location.href) !== getDomainFromURL(url)
  5258. );
  5259. };
  5260. var dynamicLabel = function dynamicLabel(label) {
  5261. return function() {
  5262. return isFunction(label) ? label.apply(void 0, arguments) : label;
  5263. };
  5264. };
  5265. var isMockItem = function isMockItem(item) {
  5266. return !isFile(item.file);
  5267. };
  5268. var listUpdated = function listUpdated(dispatch, state) {
  5269. clearTimeout(state.listUpdateTimeout);
  5270. state.listUpdateTimeout = setTimeout(function() {
  5271. dispatch('DID_UPDATE_ITEMS', { items: getActiveItems(state.items) });
  5272. }, 0);
  5273. };
  5274. var optionalPromise = function optionalPromise(fn) {
  5275. for (
  5276. var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1;
  5277. _key < _len;
  5278. _key++
  5279. ) {
  5280. params[_key - 1] = arguments[_key];
  5281. }
  5282. return new Promise(function(resolve) {
  5283. if (!fn) {
  5284. return resolve(true);
  5285. }
  5286. var result = fn.apply(void 0, params);
  5287. if (result == null) {
  5288. return resolve(true);
  5289. }
  5290. if (typeof result === 'boolean') {
  5291. return resolve(result);
  5292. }
  5293. if (typeof result.then === 'function') {
  5294. result.then(resolve);
  5295. }
  5296. });
  5297. };
  5298. var sortItems = function sortItems(state, compare) {
  5299. state.items.sort(function(a, b) {
  5300. return compare(createItemAPI(a), createItemAPI(b));
  5301. });
  5302. };
  5303. // returns item based on state
  5304. var getItemByQueryFromState = function getItemByQueryFromState(state, itemHandler) {
  5305. return function() {
  5306. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  5307. var query = _ref.query,
  5308. _ref$success = _ref.success,
  5309. success = _ref$success === void 0 ? function() {} : _ref$success,
  5310. _ref$failure = _ref.failure,
  5311. failure = _ref$failure === void 0 ? function() {} : _ref$failure,
  5312. options = _objectWithoutProperties(_ref, ['query', 'success', 'failure']);
  5313. var item = getItemByQuery(state.items, query);
  5314. if (!item) {
  5315. failure({
  5316. error: createResponse('error', 0, 'Item not found'),
  5317. file: null,
  5318. });
  5319. return;
  5320. }
  5321. itemHandler(item, success, failure, options || {});
  5322. };
  5323. };
  5324. var actions = function actions(dispatch, query, state) {
  5325. return {
  5326. /**
  5327. * Aborts all ongoing processes
  5328. */
  5329. ABORT_ALL: function ABORT_ALL() {
  5330. getActiveItems(state.items).forEach(function(item) {
  5331. item.freeze();
  5332. item.abortLoad();
  5333. item.abortProcessing();
  5334. });
  5335. },
  5336. /**
  5337. * Sets initial files
  5338. */
  5339. DID_SET_FILES: function DID_SET_FILES(_ref2) {
  5340. var _ref2$value = _ref2.value,
  5341. value = _ref2$value === void 0 ? [] : _ref2$value;
  5342. // map values to file objects
  5343. var files = value.map(function(file) {
  5344. return {
  5345. source: file.source ? file.source : file,
  5346. options: file.options,
  5347. };
  5348. });
  5349. // loop over files, if file is in list, leave it be, if not, remove
  5350. // test if items should be moved
  5351. var activeItems = getActiveItems(state.items);
  5352. activeItems.forEach(function(item) {
  5353. // if item not is in new value, remove
  5354. if (
  5355. !files.find(function(file) {
  5356. return file.source === item.source || file.source === item.file;
  5357. })
  5358. ) {
  5359. dispatch('REMOVE_ITEM', { query: item, remove: false });
  5360. }
  5361. });
  5362. // add new files
  5363. activeItems = getActiveItems(state.items);
  5364. files.forEach(function(file, index) {
  5365. // if file is already in list
  5366. if (
  5367. activeItems.find(function(item) {
  5368. return item.source === file.source || item.file === file.source;
  5369. })
  5370. )
  5371. return;
  5372. // not in list, add
  5373. dispatch(
  5374. 'ADD_ITEM',
  5375. Object.assign({}, file, {
  5376. interactionMethod: InteractionMethod.NONE,
  5377. index: index,
  5378. })
  5379. );
  5380. });
  5381. },
  5382. DID_UPDATE_ITEM_METADATA: function DID_UPDATE_ITEM_METADATA(_ref3) {
  5383. var id = _ref3.id,
  5384. action = _ref3.action,
  5385. change = _ref3.change;
  5386. // don't do anything
  5387. if (change.silent) return;
  5388. // if is called multiple times in close succession we combined all calls together to save resources
  5389. clearTimeout(state.itemUpdateTimeout);
  5390. state.itemUpdateTimeout = setTimeout(function() {
  5391. var item = getItemById(state.items, id);
  5392. // only revert and attempt to upload when we're uploading to a server
  5393. if (!query('IS_ASYNC')) {
  5394. // should we update the output data
  5395. applyFilterChain('SHOULD_PREPARE_OUTPUT', false, {
  5396. item: item,
  5397. query: query,
  5398. action: action,
  5399. change: change,
  5400. }).then(function(shouldPrepareOutput) {
  5401. // plugins determined the output data should be prepared (or not), can be adjusted with beforePrepareOutput hook
  5402. var beforePrepareFile = query('GET_BEFORE_PREPARE_FILE');
  5403. if (beforePrepareFile)
  5404. shouldPrepareOutput = beforePrepareFile(item, shouldPrepareOutput);
  5405. if (!shouldPrepareOutput) return;
  5406. dispatch(
  5407. 'REQUEST_PREPARE_OUTPUT',
  5408. {
  5409. query: id,
  5410. item: item,
  5411. success: function success(file) {
  5412. dispatch('DID_PREPARE_OUTPUT', { id: id, file: file });
  5413. },
  5414. },
  5415. true
  5416. );
  5417. });
  5418. return;
  5419. }
  5420. // if is local item we need to enable upload button so change can be propagated to server
  5421. if (item.origin === FileOrigin.LOCAL) {
  5422. dispatch('DID_LOAD_ITEM', {
  5423. id: item.id,
  5424. error: null,
  5425. serverFileReference: item.source,
  5426. });
  5427. }
  5428. // for async scenarios
  5429. var upload = function upload() {
  5430. // we push this forward a bit so the interface is updated correctly
  5431. setTimeout(function() {
  5432. dispatch('REQUEST_ITEM_PROCESSING', { query: id });
  5433. }, 32);
  5434. };
  5435. var revert = function revert(doUpload) {
  5436. item.revert(
  5437. createRevertFunction(
  5438. state.options.server.url,
  5439. state.options.server.revert
  5440. ),
  5441. query('GET_FORCE_REVERT')
  5442. )
  5443. .then(doUpload ? upload : function() {})
  5444. .catch(function() {});
  5445. };
  5446. var abort = function abort(doUpload) {
  5447. item.abortProcessing().then(doUpload ? upload : function() {});
  5448. };
  5449. // if we should re-upload the file immediately
  5450. if (item.status === ItemStatus.PROCESSING_COMPLETE) {
  5451. return revert(state.options.instantUpload);
  5452. }
  5453. // if currently uploading, cancel upload
  5454. if (item.status === ItemStatus.PROCESSING) {
  5455. return abort(state.options.instantUpload);
  5456. }
  5457. if (state.options.instantUpload) {
  5458. upload();
  5459. }
  5460. }, 0);
  5461. },
  5462. MOVE_ITEM: function MOVE_ITEM(_ref4) {
  5463. var query = _ref4.query,
  5464. index = _ref4.index;
  5465. var item = getItemByQuery(state.items, query);
  5466. if (!item) return;
  5467. var currentIndex = state.items.indexOf(item);
  5468. index = limit(index, 0, state.items.length - 1);
  5469. if (currentIndex === index) return;
  5470. state.items.splice(index, 0, state.items.splice(currentIndex, 1)[0]);
  5471. },
  5472. SORT: function SORT(_ref5) {
  5473. var compare = _ref5.compare;
  5474. sortItems(state, compare);
  5475. dispatch('DID_SORT_ITEMS', {
  5476. items: query('GET_ACTIVE_ITEMS'),
  5477. });
  5478. },
  5479. ADD_ITEMS: function ADD_ITEMS(_ref6) {
  5480. var items = _ref6.items,
  5481. index = _ref6.index,
  5482. interactionMethod = _ref6.interactionMethod,
  5483. _ref6$success = _ref6.success,
  5484. success = _ref6$success === void 0 ? function() {} : _ref6$success,
  5485. _ref6$failure = _ref6.failure,
  5486. failure = _ref6$failure === void 0 ? function() {} : _ref6$failure;
  5487. var currentIndex = index;
  5488. if (index === -1 || typeof index === 'undefined') {
  5489. var insertLocation = query('GET_ITEM_INSERT_LOCATION');
  5490. var totalItems = query('GET_TOTAL_ITEMS');
  5491. currentIndex = insertLocation === 'before' ? 0 : totalItems;
  5492. }
  5493. var ignoredFiles = query('GET_IGNORED_FILES');
  5494. var isValidFile = function isValidFile(source) {
  5495. return isFile(source)
  5496. ? !ignoredFiles.includes(source.name.toLowerCase())
  5497. : !isEmpty(source);
  5498. };
  5499. var validItems = items.filter(isValidFile);
  5500. var promises = validItems.map(function(source) {
  5501. return new Promise(function(resolve, reject) {
  5502. dispatch('ADD_ITEM', {
  5503. interactionMethod: interactionMethod,
  5504. source: source.source || source,
  5505. success: resolve,
  5506. failure: reject,
  5507. index: currentIndex++,
  5508. options: source.options || {},
  5509. });
  5510. });
  5511. });
  5512. Promise.all(promises)
  5513. .then(success)
  5514. .catch(failure);
  5515. },
  5516. /**
  5517. * @param source
  5518. * @param index
  5519. * @param interactionMethod
  5520. */
  5521. ADD_ITEM: function ADD_ITEM(_ref7) {
  5522. var source = _ref7.source,
  5523. _ref7$index = _ref7.index,
  5524. index = _ref7$index === void 0 ? -1 : _ref7$index,
  5525. interactionMethod = _ref7.interactionMethod,
  5526. _ref7$success = _ref7.success,
  5527. success = _ref7$success === void 0 ? function() {} : _ref7$success,
  5528. _ref7$failure = _ref7.failure,
  5529. failure = _ref7$failure === void 0 ? function() {} : _ref7$failure,
  5530. _ref7$options = _ref7.options,
  5531. options = _ref7$options === void 0 ? {} : _ref7$options;
  5532. // if no source supplied
  5533. if (isEmpty(source)) {
  5534. failure({
  5535. error: createResponse('error', 0, 'No source'),
  5536. file: null,
  5537. });
  5538. return;
  5539. }
  5540. // filter out invalid file items, used to filter dropped directory contents
  5541. if (
  5542. isFile(source) &&
  5543. state.options.ignoredFiles.includes(source.name.toLowerCase())
  5544. ) {
  5545. // fail silently
  5546. return;
  5547. }
  5548. // test if there's still room in the list of files
  5549. if (!hasRoomForItem(state)) {
  5550. // if multiple allowed, we can't replace
  5551. // or if only a single item is allowed but we're not allowed to replace it we exit
  5552. if (
  5553. state.options.allowMultiple ||
  5554. (!state.options.allowMultiple && !state.options.allowReplace)
  5555. ) {
  5556. var error = createResponse('warning', 0, 'Max files');
  5557. dispatch('DID_THROW_MAX_FILES', {
  5558. source: source,
  5559. error: error,
  5560. });
  5561. failure({ error: error, file: null });
  5562. return;
  5563. }
  5564. // let's replace the item
  5565. // id of first item we're about to remove
  5566. var _item = getActiveItems(state.items)[0];
  5567. // if has been processed remove it from the server as well
  5568. if (
  5569. _item.status === ItemStatus.PROCESSING_COMPLETE ||
  5570. _item.status === ItemStatus.PROCESSING_REVERT_ERROR
  5571. ) {
  5572. var forceRevert = query('GET_FORCE_REVERT');
  5573. _item
  5574. .revert(
  5575. createRevertFunction(
  5576. state.options.server.url,
  5577. state.options.server.revert
  5578. ),
  5579. forceRevert
  5580. )
  5581. .then(function() {
  5582. if (!forceRevert) return;
  5583. // try to add now
  5584. dispatch('ADD_ITEM', {
  5585. source: source,
  5586. index: index,
  5587. interactionMethod: interactionMethod,
  5588. success: success,
  5589. failure: failure,
  5590. options: options,
  5591. });
  5592. })
  5593. .catch(function() {}); // no need to handle this catch state for now
  5594. if (forceRevert) return;
  5595. }
  5596. // remove first item as it will be replaced by this item
  5597. dispatch('REMOVE_ITEM', { query: _item.id });
  5598. }
  5599. // where did the file originate
  5600. var origin =
  5601. options.type === 'local'
  5602. ? FileOrigin.LOCAL
  5603. : options.type === 'limbo'
  5604. ? FileOrigin.LIMBO
  5605. : FileOrigin.INPUT;
  5606. // create a new blank item
  5607. var item = createItem(
  5608. // where did this file come from
  5609. origin,
  5610. // an input file never has a server file reference
  5611. origin === FileOrigin.INPUT ? null : source,
  5612. // file mock data, if defined
  5613. options.file
  5614. );
  5615. // set initial meta data
  5616. Object.keys(options.metadata || {}).forEach(function(key) {
  5617. item.setMetadata(key, options.metadata[key]);
  5618. });
  5619. // created the item, let plugins add methods
  5620. applyFilters('DID_CREATE_ITEM', item, { query: query, dispatch: dispatch });
  5621. // where to insert new items
  5622. var itemInsertLocation = query('GET_ITEM_INSERT_LOCATION');
  5623. // adjust index if is not allowed to pick location
  5624. if (!state.options.itemInsertLocationFreedom) {
  5625. index = itemInsertLocation === 'before' ? -1 : state.items.length;
  5626. }
  5627. // add item to list
  5628. insertItem(state.items, item, index);
  5629. // sort items in list
  5630. if (isFunction(itemInsertLocation) && source) {
  5631. sortItems(state, itemInsertLocation);
  5632. }
  5633. // get a quick reference to the item id
  5634. var id = item.id;
  5635. // observe item events
  5636. item.on('init', function() {
  5637. dispatch('DID_INIT_ITEM', { id: id });
  5638. });
  5639. item.on('load-init', function() {
  5640. dispatch('DID_START_ITEM_LOAD', { id: id });
  5641. });
  5642. item.on('load-meta', function() {
  5643. dispatch('DID_UPDATE_ITEM_META', { id: id });
  5644. });
  5645. item.on('load-progress', function(progress) {
  5646. dispatch('DID_UPDATE_ITEM_LOAD_PROGRESS', { id: id, progress: progress });
  5647. });
  5648. item.on('load-request-error', function(error) {
  5649. var mainStatus = dynamicLabel(state.options.labelFileLoadError)(error);
  5650. // is client error, no way to recover
  5651. if (error.code >= 400 && error.code < 500) {
  5652. dispatch('DID_THROW_ITEM_INVALID', {
  5653. id: id,
  5654. error: error,
  5655. status: {
  5656. main: mainStatus,
  5657. sub: error.code + ' (' + error.body + ')',
  5658. },
  5659. });
  5660. // reject the file so can be dealt with through API
  5661. failure({ error: error, file: createItemAPI(item) });
  5662. return;
  5663. }
  5664. // is possible server error, so might be possible to retry
  5665. dispatch('DID_THROW_ITEM_LOAD_ERROR', {
  5666. id: id,
  5667. error: error,
  5668. status: {
  5669. main: mainStatus,
  5670. sub: state.options.labelTapToRetry,
  5671. },
  5672. });
  5673. });
  5674. item.on('load-file-error', function(error) {
  5675. dispatch('DID_THROW_ITEM_INVALID', {
  5676. id: id,
  5677. error: error.status,
  5678. status: error.status,
  5679. });
  5680. failure({ error: error.status, file: createItemAPI(item) });
  5681. });
  5682. item.on('load-abort', function() {
  5683. dispatch('REMOVE_ITEM', { query: id });
  5684. });
  5685. item.on('load-skip', function() {
  5686. dispatch('COMPLETE_LOAD_ITEM', {
  5687. query: id,
  5688. item: item,
  5689. data: {
  5690. source: source,
  5691. success: success,
  5692. },
  5693. });
  5694. });
  5695. item.on('load', function() {
  5696. var handleAdd = function handleAdd(shouldAdd) {
  5697. // no should not add this file
  5698. if (!shouldAdd) {
  5699. dispatch('REMOVE_ITEM', {
  5700. query: id,
  5701. });
  5702. return;
  5703. }
  5704. // now interested in metadata updates
  5705. item.on('metadata-update', function(change) {
  5706. dispatch('DID_UPDATE_ITEM_METADATA', { id: id, change: change });
  5707. });
  5708. // let plugins decide if the output data should be prepared at this point
  5709. // means we'll do this and wait for idle state
  5710. applyFilterChain('SHOULD_PREPARE_OUTPUT', false, {
  5711. item: item,
  5712. query: query,
  5713. }).then(function(shouldPrepareOutput) {
  5714. // plugins determined the output data should be prepared (or not), can be adjusted with beforePrepareOutput hook
  5715. var beforePrepareFile = query('GET_BEFORE_PREPARE_FILE');
  5716. if (beforePrepareFile)
  5717. shouldPrepareOutput = beforePrepareFile(item, shouldPrepareOutput);
  5718. var loadComplete = function loadComplete() {
  5719. dispatch('COMPLETE_LOAD_ITEM', {
  5720. query: id,
  5721. item: item,
  5722. data: {
  5723. source: source,
  5724. success: success,
  5725. },
  5726. });
  5727. listUpdated(dispatch, state);
  5728. };
  5729. // exit
  5730. if (shouldPrepareOutput) {
  5731. // wait for idle state and then run PREPARE_OUTPUT
  5732. dispatch(
  5733. 'REQUEST_PREPARE_OUTPUT',
  5734. {
  5735. query: id,
  5736. item: item,
  5737. success: function success(file) {
  5738. dispatch('DID_PREPARE_OUTPUT', { id: id, file: file });
  5739. loadComplete();
  5740. },
  5741. },
  5742. true
  5743. );
  5744. return;
  5745. }
  5746. loadComplete();
  5747. });
  5748. };
  5749. // item loaded, allow plugins to
  5750. // - read data (quickly)
  5751. // - add metadata
  5752. applyFilterChain('DID_LOAD_ITEM', item, { query: query, dispatch: dispatch })
  5753. .then(function() {
  5754. optionalPromise(query('GET_BEFORE_ADD_FILE'), createItemAPI(item)).then(
  5755. handleAdd
  5756. );
  5757. })
  5758. .catch(function(e) {
  5759. if (!e || !e.error || !e.status) return handleAdd(false);
  5760. dispatch('DID_THROW_ITEM_INVALID', {
  5761. id: id,
  5762. error: e.error,
  5763. status: e.status,
  5764. });
  5765. });
  5766. });
  5767. item.on('process-start', function() {
  5768. dispatch('DID_START_ITEM_PROCESSING', { id: id });
  5769. });
  5770. item.on('process-progress', function(progress) {
  5771. dispatch('DID_UPDATE_ITEM_PROCESS_PROGRESS', { id: id, progress: progress });
  5772. });
  5773. item.on('process-error', function(error) {
  5774. dispatch('DID_THROW_ITEM_PROCESSING_ERROR', {
  5775. id: id,
  5776. error: error,
  5777. status: {
  5778. main: dynamicLabel(state.options.labelFileProcessingError)(error),
  5779. sub: state.options.labelTapToRetry,
  5780. },
  5781. });
  5782. });
  5783. item.on('process-revert-error', function(error) {
  5784. dispatch('DID_THROW_ITEM_PROCESSING_REVERT_ERROR', {
  5785. id: id,
  5786. error: error,
  5787. status: {
  5788. main: dynamicLabel(state.options.labelFileProcessingRevertError)(error),
  5789. sub: state.options.labelTapToRetry,
  5790. },
  5791. });
  5792. });
  5793. item.on('process-complete', function(serverFileReference) {
  5794. dispatch('DID_COMPLETE_ITEM_PROCESSING', {
  5795. id: id,
  5796. error: null,
  5797. serverFileReference: serverFileReference,
  5798. });
  5799. dispatch('DID_DEFINE_VALUE', { id: id, value: serverFileReference });
  5800. });
  5801. item.on('process-abort', function() {
  5802. dispatch('DID_ABORT_ITEM_PROCESSING', { id: id });
  5803. });
  5804. item.on('process-revert', function() {
  5805. dispatch('DID_REVERT_ITEM_PROCESSING', { id: id });
  5806. dispatch('DID_DEFINE_VALUE', { id: id, value: null });
  5807. });
  5808. // let view know the item has been inserted
  5809. dispatch('DID_ADD_ITEM', {
  5810. id: id,
  5811. index: index,
  5812. interactionMethod: interactionMethod,
  5813. });
  5814. listUpdated(dispatch, state);
  5815. // start loading the source
  5816. var _ref8 = state.options.server || {},
  5817. url = _ref8.url,
  5818. load = _ref8.load,
  5819. restore = _ref8.restore,
  5820. fetch = _ref8.fetch;
  5821. item.load(
  5822. source,
  5823. // this creates a function that loads the file based on the type of file (string, base64, blob, file) and location of file (local, remote, limbo)
  5824. createFileLoader(
  5825. origin === FileOrigin.INPUT
  5826. ? // input, if is remote, see if should use custom fetch, else use default fetchBlob
  5827. isString(source) && isExternalURL(source)
  5828. ? fetch
  5829. ? createFetchFunction(url, fetch)
  5830. : fetchBlob // remote url
  5831. : fetchBlob // try to fetch url
  5832. : // limbo or local
  5833. origin === FileOrigin.LIMBO
  5834. ? createFetchFunction(url, restore) // limbo
  5835. : createFetchFunction(url, load) // local
  5836. ),
  5837. // called when the file is loaded so it can be piped through the filters
  5838. function(file, success, error) {
  5839. // let's process the file
  5840. applyFilterChain('LOAD_FILE', file, { query: query })
  5841. .then(success)
  5842. .catch(error);
  5843. }
  5844. );
  5845. },
  5846. REQUEST_PREPARE_OUTPUT: function REQUEST_PREPARE_OUTPUT(_ref9) {
  5847. var item = _ref9.item,
  5848. success = _ref9.success,
  5849. _ref9$failure = _ref9.failure,
  5850. failure = _ref9$failure === void 0 ? function() {} : _ref9$failure;
  5851. // error response if item archived
  5852. var err = {
  5853. error: createResponse('error', 0, 'Item not found'),
  5854. file: null,
  5855. };
  5856. // don't handle archived items, an item could have been archived (load aborted) while waiting to be prepared
  5857. if (item.archived) return failure(err);
  5858. // allow plugins to alter the file data
  5859. applyFilterChain('PREPARE_OUTPUT', item.file, { query: query, item: item }).then(
  5860. function(result) {
  5861. applyFilterChain('COMPLETE_PREPARE_OUTPUT', result, {
  5862. query: query,
  5863. item: item,
  5864. }).then(function(result) {
  5865. // don't handle archived items, an item could have been archived (load aborted) while being prepared
  5866. if (item.archived) return failure(err);
  5867. // we done!
  5868. success(result);
  5869. });
  5870. }
  5871. );
  5872. },
  5873. COMPLETE_LOAD_ITEM: function COMPLETE_LOAD_ITEM(_ref10) {
  5874. var item = _ref10.item,
  5875. data = _ref10.data;
  5876. var success = data.success,
  5877. source = data.source;
  5878. // sort items in list
  5879. var itemInsertLocation = query('GET_ITEM_INSERT_LOCATION');
  5880. if (isFunction(itemInsertLocation) && source) {
  5881. sortItems(state, itemInsertLocation);
  5882. }
  5883. // let interface know the item has loaded
  5884. dispatch('DID_LOAD_ITEM', {
  5885. id: item.id,
  5886. error: null,
  5887. serverFileReference: item.origin === FileOrigin.INPUT ? null : source,
  5888. });
  5889. // item has been successfully loaded and added to the
  5890. // list of items so can now be safely returned for use
  5891. success(createItemAPI(item));
  5892. // if this is a local server file we need to show a different state
  5893. if (item.origin === FileOrigin.LOCAL) {
  5894. dispatch('DID_LOAD_LOCAL_ITEM', { id: item.id });
  5895. return;
  5896. }
  5897. // if is a temp server file we prevent async upload call here (as the file is already on the server)
  5898. if (item.origin === FileOrigin.LIMBO) {
  5899. dispatch('DID_COMPLETE_ITEM_PROCESSING', {
  5900. id: item.id,
  5901. error: null,
  5902. serverFileReference: source,
  5903. });
  5904. dispatch('DID_DEFINE_VALUE', {
  5905. id: item.id,
  5906. value: item.serverId || source,
  5907. });
  5908. return;
  5909. }
  5910. // id we are allowed to upload the file immediately, lets do it
  5911. if (query('IS_ASYNC') && state.options.instantUpload) {
  5912. dispatch('REQUEST_ITEM_PROCESSING', { query: item.id });
  5913. }
  5914. },
  5915. RETRY_ITEM_LOAD: getItemByQueryFromState(state, function(item) {
  5916. // try loading the source one more time
  5917. item.retryLoad();
  5918. }),
  5919. REQUEST_ITEM_PREPARE: getItemByQueryFromState(state, function(item, _success, failure) {
  5920. dispatch(
  5921. 'REQUEST_PREPARE_OUTPUT',
  5922. {
  5923. query: item.id,
  5924. item: item,
  5925. success: function success(file) {
  5926. dispatch('DID_PREPARE_OUTPUT', { id: item.id, file: file });
  5927. _success({
  5928. file: item,
  5929. output: file,
  5930. });
  5931. },
  5932. failure: failure,
  5933. },
  5934. true
  5935. );
  5936. }),
  5937. REQUEST_ITEM_PROCESSING: getItemByQueryFromState(state, function(
  5938. item,
  5939. success,
  5940. failure
  5941. ) {
  5942. // cannot be queued (or is already queued)
  5943. var itemCanBeQueuedForProcessing =
  5944. // waiting for something
  5945. item.status === ItemStatus.IDLE ||
  5946. // processing went wrong earlier
  5947. item.status === ItemStatus.PROCESSING_ERROR;
  5948. // not ready to be processed
  5949. if (!itemCanBeQueuedForProcessing) {
  5950. var processNow = function processNow() {
  5951. return dispatch('REQUEST_ITEM_PROCESSING', {
  5952. query: item,
  5953. success: success,
  5954. failure: failure,
  5955. });
  5956. };
  5957. var process = function process() {
  5958. return document.hidden ? processNow() : setTimeout(processNow, 32);
  5959. };
  5960. // if already done processing or tried to revert but didn't work, try again
  5961. if (
  5962. item.status === ItemStatus.PROCESSING_COMPLETE ||
  5963. item.status === ItemStatus.PROCESSING_REVERT_ERROR
  5964. ) {
  5965. item.revert(
  5966. createRevertFunction(
  5967. state.options.server.url,
  5968. state.options.server.revert
  5969. ),
  5970. query('GET_FORCE_REVERT')
  5971. )
  5972. .then(process)
  5973. .catch(function() {}); // don't continue with processing if something went wrong
  5974. } else if (item.status === ItemStatus.PROCESSING) {
  5975. item.abortProcessing().then(process);
  5976. }
  5977. return;
  5978. }
  5979. // already queued for processing
  5980. if (item.status === ItemStatus.PROCESSING_QUEUED) return;
  5981. item.requestProcessing();
  5982. dispatch('DID_REQUEST_ITEM_PROCESSING', { id: item.id });
  5983. dispatch('PROCESS_ITEM', { query: item, success: success, failure: failure }, true);
  5984. }),
  5985. PROCESS_ITEM: getItemByQueryFromState(state, function(item, success, failure) {
  5986. var maxParallelUploads = query('GET_MAX_PARALLEL_UPLOADS');
  5987. var totalCurrentUploads = query('GET_ITEMS_BY_STATUS', ItemStatus.PROCESSING)
  5988. .length;
  5989. // queue and wait till queue is freed up
  5990. if (totalCurrentUploads === maxParallelUploads) {
  5991. // queue for later processing
  5992. state.processingQueue.push({
  5993. id: item.id,
  5994. success: success,
  5995. failure: failure,
  5996. });
  5997. // stop it!
  5998. return;
  5999. }
  6000. // if was not queued or is already processing exit here
  6001. if (item.status === ItemStatus.PROCESSING) return;
  6002. var processNext = function processNext() {
  6003. // process queueud items
  6004. var queueEntry = state.processingQueue.shift();
  6005. // no items left
  6006. if (!queueEntry) return;
  6007. // get item reference
  6008. var id = queueEntry.id,
  6009. success = queueEntry.success,
  6010. failure = queueEntry.failure;
  6011. var itemReference = getItemByQuery(state.items, id);
  6012. // if item was archived while in queue, jump to next
  6013. if (!itemReference || itemReference.archived) {
  6014. processNext();
  6015. return;
  6016. }
  6017. // process queued item
  6018. dispatch(
  6019. 'PROCESS_ITEM',
  6020. { query: id, success: success, failure: failure },
  6021. true
  6022. );
  6023. };
  6024. // we done function
  6025. item.onOnce('process-complete', function() {
  6026. success(createItemAPI(item));
  6027. processNext();
  6028. // if origin is local, and we're instant uploading, trigger remove of original
  6029. // as revert will remove file from list
  6030. var server = state.options.server;
  6031. var instantUpload = state.options.instantUpload;
  6032. if (
  6033. instantUpload &&
  6034. item.origin === FileOrigin.LOCAL &&
  6035. isFunction(server.remove)
  6036. ) {
  6037. var noop = function noop() {};
  6038. item.origin = FileOrigin.LIMBO;
  6039. state.options.server.remove(item.source, noop, noop);
  6040. }
  6041. // All items processed? No errors?
  6042. var allItemsProcessed =
  6043. query('GET_ITEMS_BY_STATUS', ItemStatus.PROCESSING_COMPLETE).length ===
  6044. state.items.length;
  6045. if (allItemsProcessed) {
  6046. dispatch('DID_COMPLETE_ITEM_PROCESSING_ALL');
  6047. }
  6048. });
  6049. // we error function
  6050. item.onOnce('process-error', function(error) {
  6051. failure({ error: error, file: createItemAPI(item) });
  6052. processNext();
  6053. });
  6054. // start file processing
  6055. var options = state.options;
  6056. item.process(
  6057. createFileProcessor(
  6058. createProcessorFunction(
  6059. options.server.url,
  6060. options.server.process,
  6061. options.name,
  6062. {
  6063. chunkTransferId: item.transferId,
  6064. chunkServer: options.server.patch,
  6065. chunkUploads: options.chunkUploads,
  6066. chunkForce: options.chunkForce,
  6067. chunkSize: options.chunkSize,
  6068. chunkRetryDelays: options.chunkRetryDelays,
  6069. }
  6070. ),
  6071. {
  6072. allowMinimumUploadDuration: query('GET_ALLOW_MINIMUM_UPLOAD_DURATION'),
  6073. }
  6074. ),
  6075. // called when the file is about to be processed so it can be piped through the transform filters
  6076. function(file, success, error) {
  6077. // allow plugins to alter the file data
  6078. applyFilterChain('PREPARE_OUTPUT', file, { query: query, item: item })
  6079. .then(function(file) {
  6080. dispatch('DID_PREPARE_OUTPUT', { id: item.id, file: file });
  6081. success(file);
  6082. })
  6083. .catch(error);
  6084. }
  6085. );
  6086. }),
  6087. RETRY_ITEM_PROCESSING: getItemByQueryFromState(state, function(item) {
  6088. dispatch('REQUEST_ITEM_PROCESSING', { query: item });
  6089. }),
  6090. REQUEST_REMOVE_ITEM: getItemByQueryFromState(state, function(item) {
  6091. optionalPromise(query('GET_BEFORE_REMOVE_FILE'), createItemAPI(item)).then(function(
  6092. shouldRemove
  6093. ) {
  6094. if (!shouldRemove) {
  6095. return;
  6096. }
  6097. dispatch('REMOVE_ITEM', { query: item });
  6098. });
  6099. }),
  6100. RELEASE_ITEM: getItemByQueryFromState(state, function(item) {
  6101. item.release();
  6102. }),
  6103. REMOVE_ITEM: getItemByQueryFromState(state, function(item, success, failure, options) {
  6104. var removeFromView = function removeFromView() {
  6105. // get id reference
  6106. var id = item.id;
  6107. // archive the item, this does not remove it from the list
  6108. getItemById(state.items, id).archive();
  6109. // tell the view the item has been removed
  6110. dispatch('DID_REMOVE_ITEM', { error: null, id: id, item: item });
  6111. // now the list has been modified
  6112. listUpdated(dispatch, state);
  6113. // correctly removed
  6114. success(createItemAPI(item));
  6115. };
  6116. // if this is a local file and the `server.remove` function has been configured,
  6117. // send source there so dev can remove file from server
  6118. var server = state.options.server;
  6119. if (
  6120. item.origin === FileOrigin.LOCAL &&
  6121. server &&
  6122. isFunction(server.remove) &&
  6123. options.remove !== false
  6124. ) {
  6125. dispatch('DID_START_ITEM_REMOVE', { id: item.id });
  6126. server.remove(
  6127. item.source,
  6128. function() {
  6129. return removeFromView();
  6130. },
  6131. function(status) {
  6132. dispatch('DID_THROW_ITEM_REMOVE_ERROR', {
  6133. id: item.id,
  6134. error: createResponse('error', 0, status, null),
  6135. status: {
  6136. main: dynamicLabel(state.options.labelFileRemoveError)(status),
  6137. sub: state.options.labelTapToRetry,
  6138. },
  6139. });
  6140. }
  6141. );
  6142. } else {
  6143. // if is requesting revert and can revert need to call revert handler (not calling request_ because that would also trigger beforeRemoveHook)
  6144. if (
  6145. (options.revert &&
  6146. item.origin !== FileOrigin.LOCAL &&
  6147. item.serverId !== null) ||
  6148. // if chunked uploads are enabled and we're uploading in chunks for this specific file
  6149. // or if the file isn't big enough for chunked uploads but chunkForce is set then call
  6150. // revert before removing from the view...
  6151. (state.options.chunkUploads && item.file.size > state.options.chunkSize) ||
  6152. (state.options.chunkUploads && state.options.chunkForce)
  6153. ) {
  6154. item.revert(
  6155. createRevertFunction(
  6156. state.options.server.url,
  6157. state.options.server.revert
  6158. ),
  6159. query('GET_FORCE_REVERT')
  6160. );
  6161. }
  6162. // can now safely remove from view
  6163. removeFromView();
  6164. }
  6165. }),
  6166. ABORT_ITEM_LOAD: getItemByQueryFromState(state, function(item) {
  6167. item.abortLoad();
  6168. }),
  6169. ABORT_ITEM_PROCESSING: getItemByQueryFromState(state, function(item) {
  6170. // test if is already processed
  6171. if (item.serverId) {
  6172. dispatch('REVERT_ITEM_PROCESSING', { id: item.id });
  6173. return;
  6174. }
  6175. // abort
  6176. item.abortProcessing().then(function() {
  6177. var shouldRemove = state.options.instantUpload;
  6178. if (shouldRemove) {
  6179. dispatch('REMOVE_ITEM', { query: item.id });
  6180. }
  6181. });
  6182. }),
  6183. REQUEST_REVERT_ITEM_PROCESSING: getItemByQueryFromState(state, function(item) {
  6184. // not instant uploading, revert immediately
  6185. if (!state.options.instantUpload) {
  6186. dispatch('REVERT_ITEM_PROCESSING', { query: item });
  6187. return;
  6188. }
  6189. // if we're instant uploading the file will also be removed if we revert,
  6190. // so if a before remove file hook is defined we need to run it now
  6191. var handleRevert = function handleRevert(shouldRevert) {
  6192. if (!shouldRevert) return;
  6193. dispatch('REVERT_ITEM_PROCESSING', { query: item });
  6194. };
  6195. var fn = query('GET_BEFORE_REMOVE_FILE');
  6196. if (!fn) {
  6197. return handleRevert(true);
  6198. }
  6199. var requestRemoveResult = fn(createItemAPI(item));
  6200. if (requestRemoveResult == null) {
  6201. // undefined or null
  6202. return handleRevert(true);
  6203. }
  6204. if (typeof requestRemoveResult === 'boolean') {
  6205. return handleRevert(requestRemoveResult);
  6206. }
  6207. if (typeof requestRemoveResult.then === 'function') {
  6208. requestRemoveResult.then(handleRevert);
  6209. }
  6210. }),
  6211. REVERT_ITEM_PROCESSING: getItemByQueryFromState(state, function(item) {
  6212. item.revert(
  6213. createRevertFunction(state.options.server.url, state.options.server.revert),
  6214. query('GET_FORCE_REVERT')
  6215. )
  6216. .then(function() {
  6217. var shouldRemove = state.options.instantUpload || isMockItem(item);
  6218. if (shouldRemove) {
  6219. dispatch('REMOVE_ITEM', { query: item.id });
  6220. }
  6221. })
  6222. .catch(function() {});
  6223. }),
  6224. SET_OPTIONS: function SET_OPTIONS(_ref11) {
  6225. var options = _ref11.options;
  6226. // get all keys passed
  6227. var optionKeys = Object.keys(options);
  6228. // get prioritized keyed to include (remove once not in options object)
  6229. var prioritizedOptionKeys = PrioritizedOptions.filter(function(key) {
  6230. return optionKeys.includes(key);
  6231. });
  6232. // order the keys, prioritized first, then rest
  6233. var orderedOptionKeys = [].concat(
  6234. _toConsumableArray(prioritizedOptionKeys),
  6235. _toConsumableArray(
  6236. Object.keys(options).filter(function(key) {
  6237. return !prioritizedOptionKeys.includes(key);
  6238. })
  6239. )
  6240. );
  6241. // dispatch set event for each option
  6242. orderedOptionKeys.forEach(function(key) {
  6243. dispatch('SET_' + fromCamels(key, '_').toUpperCase(), {
  6244. value: options[key],
  6245. });
  6246. });
  6247. },
  6248. };
  6249. };
  6250. var PrioritizedOptions = ['server'];
  6251. var formatFilename = function formatFilename(name) {
  6252. return name;
  6253. };
  6254. var createElement$1 = function createElement(tagName) {
  6255. return document.createElement(tagName);
  6256. };
  6257. var text = function text(node, value) {
  6258. var textNode = node.childNodes[0];
  6259. if (!textNode) {
  6260. textNode = document.createTextNode(value);
  6261. node.appendChild(textNode);
  6262. } else if (value !== textNode.nodeValue) {
  6263. textNode.nodeValue = value;
  6264. }
  6265. };
  6266. var polarToCartesian = function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  6267. var angleInRadians = (((angleInDegrees % 360) - 90) * Math.PI) / 180.0;
  6268. return {
  6269. x: centerX + radius * Math.cos(angleInRadians),
  6270. y: centerY + radius * Math.sin(angleInRadians),
  6271. };
  6272. };
  6273. var describeArc = function describeArc(x, y, radius, startAngle, endAngle, arcSweep) {
  6274. var start = polarToCartesian(x, y, radius, endAngle);
  6275. var end = polarToCartesian(x, y, radius, startAngle);
  6276. return ['M', start.x, start.y, 'A', radius, radius, 0, arcSweep, 0, end.x, end.y].join(' ');
  6277. };
  6278. var percentageArc = function percentageArc(x, y, radius, from, to) {
  6279. var arcSweep = 1;
  6280. if (to > from && to - from <= 0.5) {
  6281. arcSweep = 0;
  6282. }
  6283. if (from > to && from - to >= 0.5) {
  6284. arcSweep = 0;
  6285. }
  6286. return describeArc(
  6287. x,
  6288. y,
  6289. radius,
  6290. Math.min(0.9999, from) * 360,
  6291. Math.min(0.9999, to) * 360,
  6292. arcSweep
  6293. );
  6294. };
  6295. var create = function create(_ref) {
  6296. var root = _ref.root,
  6297. props = _ref.props;
  6298. // start at 0
  6299. props.spin = false;
  6300. props.progress = 0;
  6301. props.opacity = 0;
  6302. // svg
  6303. var svg = createElement('svg');
  6304. root.ref.path = createElement('path', {
  6305. 'stroke-width': 2,
  6306. 'stroke-linecap': 'round',
  6307. });
  6308. svg.appendChild(root.ref.path);
  6309. root.ref.svg = svg;
  6310. root.appendChild(svg);
  6311. };
  6312. var write = function write(_ref2) {
  6313. var root = _ref2.root,
  6314. props = _ref2.props;
  6315. if (props.opacity === 0) {
  6316. return;
  6317. }
  6318. if (props.align) {
  6319. root.element.dataset.align = props.align;
  6320. }
  6321. // get width of stroke
  6322. var ringStrokeWidth = parseInt(attr(root.ref.path, 'stroke-width'), 10);
  6323. // calculate size of ring
  6324. var size = root.rect.element.width * 0.5;
  6325. // ring state
  6326. var ringFrom = 0;
  6327. var ringTo = 0;
  6328. // now in busy mode
  6329. if (props.spin) {
  6330. ringFrom = 0;
  6331. ringTo = 0.5;
  6332. } else {
  6333. ringFrom = 0;
  6334. ringTo = props.progress;
  6335. }
  6336. // get arc path
  6337. var coordinates = percentageArc(size, size, size - ringStrokeWidth, ringFrom, ringTo);
  6338. // update progress bar
  6339. attr(root.ref.path, 'd', coordinates);
  6340. // hide while contains 0 value
  6341. attr(root.ref.path, 'stroke-opacity', props.spin || props.progress > 0 ? 1 : 0);
  6342. };
  6343. var progressIndicator = createView({
  6344. tag: 'div',
  6345. name: 'progress-indicator',
  6346. ignoreRectUpdate: true,
  6347. ignoreRect: true,
  6348. create: create,
  6349. write: write,
  6350. mixins: {
  6351. apis: ['progress', 'spin', 'align'],
  6352. styles: ['opacity'],
  6353. animations: {
  6354. opacity: { type: 'tween', duration: 500 },
  6355. progress: {
  6356. type: 'spring',
  6357. stiffness: 0.95,
  6358. damping: 0.65,
  6359. mass: 10,
  6360. },
  6361. },
  6362. },
  6363. });
  6364. var create$1 = function create(_ref) {
  6365. var root = _ref.root,
  6366. props = _ref.props;
  6367. root.element.innerHTML = (props.icon || '') + ('<span>' + props.label + '</span>');
  6368. props.isDisabled = false;
  6369. };
  6370. var write$1 = function write(_ref2) {
  6371. var root = _ref2.root,
  6372. props = _ref2.props;
  6373. var isDisabled = props.isDisabled;
  6374. var shouldDisable = root.query('GET_DISABLED') || props.opacity === 0;
  6375. if (shouldDisable && !isDisabled) {
  6376. props.isDisabled = true;
  6377. attr(root.element, 'disabled', 'disabled');
  6378. } else if (!shouldDisable && isDisabled) {
  6379. props.isDisabled = false;
  6380. root.element.removeAttribute('disabled');
  6381. }
  6382. };
  6383. var fileActionButton = createView({
  6384. tag: 'button',
  6385. attributes: {
  6386. type: 'button',
  6387. },
  6388. ignoreRect: true,
  6389. ignoreRectUpdate: true,
  6390. name: 'file-action-button',
  6391. mixins: {
  6392. apis: ['label'],
  6393. styles: ['translateX', 'translateY', 'scaleX', 'scaleY', 'opacity'],
  6394. animations: {
  6395. scaleX: 'spring',
  6396. scaleY: 'spring',
  6397. translateX: 'spring',
  6398. translateY: 'spring',
  6399. opacity: { type: 'tween', duration: 250 },
  6400. },
  6401. listeners: true,
  6402. },
  6403. create: create$1,
  6404. write: write$1,
  6405. });
  6406. var toNaturalFileSize = function toNaturalFileSize(bytes) {
  6407. var decimalSeparator =
  6408. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.';
  6409. var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1000;
  6410. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  6411. var _options$labelBytes = options.labelBytes,
  6412. labelBytes = _options$labelBytes === void 0 ? 'bytes' : _options$labelBytes,
  6413. _options$labelKilobyt = options.labelKilobytes,
  6414. labelKilobytes = _options$labelKilobyt === void 0 ? 'KB' : _options$labelKilobyt,
  6415. _options$labelMegabyt = options.labelMegabytes,
  6416. labelMegabytes = _options$labelMegabyt === void 0 ? 'MB' : _options$labelMegabyt,
  6417. _options$labelGigabyt = options.labelGigabytes,
  6418. labelGigabytes = _options$labelGigabyt === void 0 ? 'GB' : _options$labelGigabyt;
  6419. // no negative byte sizes
  6420. bytes = Math.round(Math.abs(bytes));
  6421. var KB = base;
  6422. var MB = base * base;
  6423. var GB = base * base * base;
  6424. // just bytes
  6425. if (bytes < KB) {
  6426. return bytes + ' ' + labelBytes;
  6427. }
  6428. // kilobytes
  6429. if (bytes < MB) {
  6430. return Math.floor(bytes / KB) + ' ' + labelKilobytes;
  6431. }
  6432. // megabytes
  6433. if (bytes < GB) {
  6434. return removeDecimalsWhenZero(bytes / MB, 1, decimalSeparator) + ' ' + labelMegabytes;
  6435. }
  6436. // gigabytes
  6437. return removeDecimalsWhenZero(bytes / GB, 2, decimalSeparator) + ' ' + labelGigabytes;
  6438. };
  6439. var removeDecimalsWhenZero = function removeDecimalsWhenZero(value, decimalCount, separator) {
  6440. return value
  6441. .toFixed(decimalCount)
  6442. .split('.')
  6443. .filter(function(part) {
  6444. return part !== '0';
  6445. })
  6446. .join(separator);
  6447. };
  6448. var create$2 = function create(_ref) {
  6449. var root = _ref.root,
  6450. props = _ref.props;
  6451. // filename
  6452. var fileName = createElement$1('span');
  6453. fileName.className = 'filepond--file-info-main';
  6454. // hide for screenreaders
  6455. // the file is contained in a fieldset with legend that contains the filename
  6456. // no need to read it twice
  6457. attr(fileName, 'aria-hidden', 'true');
  6458. root.appendChild(fileName);
  6459. root.ref.fileName = fileName;
  6460. // filesize
  6461. var fileSize = createElement$1('span');
  6462. fileSize.className = 'filepond--file-info-sub';
  6463. root.appendChild(fileSize);
  6464. root.ref.fileSize = fileSize;
  6465. // set initial values
  6466. text(fileSize, root.query('GET_LABEL_FILE_WAITING_FOR_SIZE'));
  6467. text(fileName, formatFilename(root.query('GET_ITEM_NAME', props.id)));
  6468. };
  6469. var updateFile = function updateFile(_ref2) {
  6470. var root = _ref2.root,
  6471. props = _ref2.props;
  6472. text(
  6473. root.ref.fileSize,
  6474. toNaturalFileSize(
  6475. root.query('GET_ITEM_SIZE', props.id),
  6476. '.',
  6477. root.query('GET_FILE_SIZE_BASE'),
  6478. root.query('GET_FILE_SIZE_LABELS', root.query)
  6479. )
  6480. );
  6481. text(root.ref.fileName, formatFilename(root.query('GET_ITEM_NAME', props.id)));
  6482. };
  6483. var updateFileSizeOnError = function updateFileSizeOnError(_ref3) {
  6484. var root = _ref3.root,
  6485. props = _ref3.props;
  6486. // if size is available don't fallback to unknown size message
  6487. if (isInt(root.query('GET_ITEM_SIZE', props.id))) {
  6488. updateFile({ root: root, props: props });
  6489. return;
  6490. }
  6491. text(root.ref.fileSize, root.query('GET_LABEL_FILE_SIZE_NOT_AVAILABLE'));
  6492. };
  6493. var fileInfo = createView({
  6494. name: 'file-info',
  6495. ignoreRect: true,
  6496. ignoreRectUpdate: true,
  6497. write: createRoute({
  6498. DID_LOAD_ITEM: updateFile,
  6499. DID_UPDATE_ITEM_META: updateFile,
  6500. DID_THROW_ITEM_LOAD_ERROR: updateFileSizeOnError,
  6501. DID_THROW_ITEM_INVALID: updateFileSizeOnError,
  6502. }),
  6503. didCreateView: function didCreateView(root) {
  6504. applyFilters('CREATE_VIEW', Object.assign({}, root, { view: root }));
  6505. },
  6506. create: create$2,
  6507. mixins: {
  6508. styles: ['translateX', 'translateY'],
  6509. animations: {
  6510. translateX: 'spring',
  6511. translateY: 'spring',
  6512. },
  6513. },
  6514. });
  6515. var toPercentage = function toPercentage(value) {
  6516. return Math.round(value * 100);
  6517. };
  6518. var create$3 = function create(_ref) {
  6519. var root = _ref.root;
  6520. // main status
  6521. var main = createElement$1('span');
  6522. main.className = 'filepond--file-status-main';
  6523. root.appendChild(main);
  6524. root.ref.main = main;
  6525. // sub status
  6526. var sub = createElement$1('span');
  6527. sub.className = 'filepond--file-status-sub';
  6528. root.appendChild(sub);
  6529. root.ref.sub = sub;
  6530. didSetItemLoadProgress({ root: root, action: { progress: null } });
  6531. };
  6532. var didSetItemLoadProgress = function didSetItemLoadProgress(_ref2) {
  6533. var root = _ref2.root,
  6534. action = _ref2.action;
  6535. var title =
  6536. action.progress === null
  6537. ? root.query('GET_LABEL_FILE_LOADING')
  6538. : root.query('GET_LABEL_FILE_LOADING') + ' ' + toPercentage(action.progress) + '%';
  6539. text(root.ref.main, title);
  6540. text(root.ref.sub, root.query('GET_LABEL_TAP_TO_CANCEL'));
  6541. };
  6542. var didSetItemProcessProgress = function didSetItemProcessProgress(_ref3) {
  6543. var root = _ref3.root,
  6544. action = _ref3.action;
  6545. var title =
  6546. action.progress === null
  6547. ? root.query('GET_LABEL_FILE_PROCESSING')
  6548. : root.query('GET_LABEL_FILE_PROCESSING') +
  6549. ' ' +
  6550. toPercentage(action.progress) +
  6551. '%';
  6552. text(root.ref.main, title);
  6553. text(root.ref.sub, root.query('GET_LABEL_TAP_TO_CANCEL'));
  6554. };
  6555. var didRequestItemProcessing = function didRequestItemProcessing(_ref4) {
  6556. var root = _ref4.root;
  6557. text(root.ref.main, root.query('GET_LABEL_FILE_PROCESSING'));
  6558. text(root.ref.sub, root.query('GET_LABEL_TAP_TO_CANCEL'));
  6559. };
  6560. var didAbortItemProcessing = function didAbortItemProcessing(_ref5) {
  6561. var root = _ref5.root;
  6562. text(root.ref.main, root.query('GET_LABEL_FILE_PROCESSING_ABORTED'));
  6563. text(root.ref.sub, root.query('GET_LABEL_TAP_TO_RETRY'));
  6564. };
  6565. var didCompleteItemProcessing = function didCompleteItemProcessing(_ref6) {
  6566. var root = _ref6.root;
  6567. text(root.ref.main, root.query('GET_LABEL_FILE_PROCESSING_COMPLETE'));
  6568. text(root.ref.sub, root.query('GET_LABEL_TAP_TO_UNDO'));
  6569. };
  6570. var clear = function clear(_ref7) {
  6571. var root = _ref7.root;
  6572. text(root.ref.main, '');
  6573. text(root.ref.sub, '');
  6574. };
  6575. var error = function error(_ref8) {
  6576. var root = _ref8.root,
  6577. action = _ref8.action;
  6578. text(root.ref.main, action.status.main);
  6579. text(root.ref.sub, action.status.sub);
  6580. };
  6581. var fileStatus = createView({
  6582. name: 'file-status',
  6583. ignoreRect: true,
  6584. ignoreRectUpdate: true,
  6585. write: createRoute({
  6586. DID_LOAD_ITEM: clear,
  6587. DID_REVERT_ITEM_PROCESSING: clear,
  6588. DID_REQUEST_ITEM_PROCESSING: didRequestItemProcessing,
  6589. DID_ABORT_ITEM_PROCESSING: didAbortItemProcessing,
  6590. DID_COMPLETE_ITEM_PROCESSING: didCompleteItemProcessing,
  6591. DID_UPDATE_ITEM_PROCESS_PROGRESS: didSetItemProcessProgress,
  6592. DID_UPDATE_ITEM_LOAD_PROGRESS: didSetItemLoadProgress,
  6593. DID_THROW_ITEM_LOAD_ERROR: error,
  6594. DID_THROW_ITEM_INVALID: error,
  6595. DID_THROW_ITEM_PROCESSING_ERROR: error,
  6596. DID_THROW_ITEM_PROCESSING_REVERT_ERROR: error,
  6597. DID_THROW_ITEM_REMOVE_ERROR: error,
  6598. }),
  6599. didCreateView: function didCreateView(root) {
  6600. applyFilters('CREATE_VIEW', Object.assign({}, root, { view: root }));
  6601. },
  6602. create: create$3,
  6603. mixins: {
  6604. styles: ['translateX', 'translateY', 'opacity'],
  6605. animations: {
  6606. opacity: { type: 'tween', duration: 250 },
  6607. translateX: 'spring',
  6608. translateY: 'spring',
  6609. },
  6610. },
  6611. });
  6612. /**
  6613. * Button definitions for the file view
  6614. */
  6615. var Buttons = {
  6616. AbortItemLoad: {
  6617. label: 'GET_LABEL_BUTTON_ABORT_ITEM_LOAD',
  6618. action: 'ABORT_ITEM_LOAD',
  6619. className: 'filepond--action-abort-item-load',
  6620. align: 'LOAD_INDICATOR_POSITION', // right
  6621. },
  6622. RetryItemLoad: {
  6623. label: 'GET_LABEL_BUTTON_RETRY_ITEM_LOAD',
  6624. action: 'RETRY_ITEM_LOAD',
  6625. icon: 'GET_ICON_RETRY',
  6626. className: 'filepond--action-retry-item-load',
  6627. align: 'BUTTON_PROCESS_ITEM_POSITION', // right
  6628. },
  6629. RemoveItem: {
  6630. label: 'GET_LABEL_BUTTON_REMOVE_ITEM',
  6631. action: 'REQUEST_REMOVE_ITEM',
  6632. icon: 'GET_ICON_REMOVE',
  6633. className: 'filepond--action-remove-item',
  6634. align: 'BUTTON_REMOVE_ITEM_POSITION', // left
  6635. },
  6636. ProcessItem: {
  6637. label: 'GET_LABEL_BUTTON_PROCESS_ITEM',
  6638. action: 'REQUEST_ITEM_PROCESSING',
  6639. icon: 'GET_ICON_PROCESS',
  6640. className: 'filepond--action-process-item',
  6641. align: 'BUTTON_PROCESS_ITEM_POSITION', // right
  6642. },
  6643. AbortItemProcessing: {
  6644. label: 'GET_LABEL_BUTTON_ABORT_ITEM_PROCESSING',
  6645. action: 'ABORT_ITEM_PROCESSING',
  6646. className: 'filepond--action-abort-item-processing',
  6647. align: 'BUTTON_PROCESS_ITEM_POSITION', // right
  6648. },
  6649. RetryItemProcessing: {
  6650. label: 'GET_LABEL_BUTTON_RETRY_ITEM_PROCESSING',
  6651. action: 'RETRY_ITEM_PROCESSING',
  6652. icon: 'GET_ICON_RETRY',
  6653. className: 'filepond--action-retry-item-processing',
  6654. align: 'BUTTON_PROCESS_ITEM_POSITION', // right
  6655. },
  6656. RevertItemProcessing: {
  6657. label: 'GET_LABEL_BUTTON_UNDO_ITEM_PROCESSING',
  6658. action: 'REQUEST_REVERT_ITEM_PROCESSING',
  6659. icon: 'GET_ICON_UNDO',
  6660. className: 'filepond--action-revert-item-processing',
  6661. align: 'BUTTON_PROCESS_ITEM_POSITION', // right
  6662. },
  6663. };
  6664. // make a list of buttons, we can then remove buttons from this list if they're disabled
  6665. var ButtonKeys = [];
  6666. forin(Buttons, function(key) {
  6667. ButtonKeys.push(key);
  6668. });
  6669. var calculateFileInfoOffset = function calculateFileInfoOffset(root) {
  6670. if (getRemoveIndicatorAligment(root) === 'right') return 0;
  6671. var buttonRect = root.ref.buttonRemoveItem.rect.element;
  6672. return buttonRect.hidden ? null : buttonRect.width + buttonRect.left;
  6673. };
  6674. var calculateButtonWidth = function calculateButtonWidth(root) {
  6675. var buttonRect = root.ref.buttonAbortItemLoad.rect.element;
  6676. return buttonRect.width;
  6677. };
  6678. // Force on full pixels so text stays crips
  6679. var calculateFileVerticalCenterOffset = function calculateFileVerticalCenterOffset(root) {
  6680. return Math.floor(root.ref.buttonRemoveItem.rect.element.height / 4);
  6681. };
  6682. var calculateFileHorizontalCenterOffset = function calculateFileHorizontalCenterOffset(root) {
  6683. return Math.floor(root.ref.buttonRemoveItem.rect.element.left / 2);
  6684. };
  6685. var getLoadIndicatorAlignment = function getLoadIndicatorAlignment(root) {
  6686. return root.query('GET_STYLE_LOAD_INDICATOR_POSITION');
  6687. };
  6688. var getProcessIndicatorAlignment = function getProcessIndicatorAlignment(root) {
  6689. return root.query('GET_STYLE_PROGRESS_INDICATOR_POSITION');
  6690. };
  6691. var getRemoveIndicatorAligment = function getRemoveIndicatorAligment(root) {
  6692. return root.query('GET_STYLE_BUTTON_REMOVE_ITEM_POSITION');
  6693. };
  6694. var DefaultStyle = {
  6695. buttonAbortItemLoad: { opacity: 0 },
  6696. buttonRetryItemLoad: { opacity: 0 },
  6697. buttonRemoveItem: { opacity: 0 },
  6698. buttonProcessItem: { opacity: 0 },
  6699. buttonAbortItemProcessing: { opacity: 0 },
  6700. buttonRetryItemProcessing: { opacity: 0 },
  6701. buttonRevertItemProcessing: { opacity: 0 },
  6702. loadProgressIndicator: { opacity: 0, align: getLoadIndicatorAlignment },
  6703. processProgressIndicator: { opacity: 0, align: getProcessIndicatorAlignment },
  6704. processingCompleteIndicator: { opacity: 0, scaleX: 0.75, scaleY: 0.75 },
  6705. info: { translateX: 0, translateY: 0, opacity: 0 },
  6706. status: { translateX: 0, translateY: 0, opacity: 0 },
  6707. };
  6708. var IdleStyle = {
  6709. buttonRemoveItem: { opacity: 1 },
  6710. buttonProcessItem: { opacity: 1 },
  6711. info: { translateX: calculateFileInfoOffset },
  6712. status: { translateX: calculateFileInfoOffset },
  6713. };
  6714. var ProcessingStyle = {
  6715. buttonAbortItemProcessing: { opacity: 1 },
  6716. processProgressIndicator: { opacity: 1 },
  6717. status: { opacity: 1 },
  6718. };
  6719. var StyleMap = {
  6720. DID_THROW_ITEM_INVALID: {
  6721. buttonRemoveItem: { opacity: 1 },
  6722. info: { translateX: calculateFileInfoOffset },
  6723. status: { translateX: calculateFileInfoOffset, opacity: 1 },
  6724. },
  6725. DID_START_ITEM_LOAD: {
  6726. buttonAbortItemLoad: { opacity: 1 },
  6727. loadProgressIndicator: { opacity: 1 },
  6728. status: { opacity: 1 },
  6729. },
  6730. DID_THROW_ITEM_LOAD_ERROR: {
  6731. buttonRetryItemLoad: { opacity: 1 },
  6732. buttonRemoveItem: { opacity: 1 },
  6733. info: { translateX: calculateFileInfoOffset },
  6734. status: { opacity: 1 },
  6735. },
  6736. DID_START_ITEM_REMOVE: {
  6737. processProgressIndicator: { opacity: 1, align: getRemoveIndicatorAligment },
  6738. info: { translateX: calculateFileInfoOffset },
  6739. status: { opacity: 0 },
  6740. },
  6741. DID_THROW_ITEM_REMOVE_ERROR: {
  6742. processProgressIndicator: { opacity: 0, align: getRemoveIndicatorAligment },
  6743. buttonRemoveItem: { opacity: 1 },
  6744. info: { translateX: calculateFileInfoOffset },
  6745. status: { opacity: 1, translateX: calculateFileInfoOffset },
  6746. },
  6747. DID_LOAD_ITEM: IdleStyle,
  6748. DID_LOAD_LOCAL_ITEM: {
  6749. buttonRemoveItem: { opacity: 1 },
  6750. info: { translateX: calculateFileInfoOffset },
  6751. status: { translateX: calculateFileInfoOffset },
  6752. },
  6753. DID_START_ITEM_PROCESSING: ProcessingStyle,
  6754. DID_REQUEST_ITEM_PROCESSING: ProcessingStyle,
  6755. DID_UPDATE_ITEM_PROCESS_PROGRESS: ProcessingStyle,
  6756. DID_COMPLETE_ITEM_PROCESSING: {
  6757. buttonRevertItemProcessing: { opacity: 1 },
  6758. info: { opacity: 1 },
  6759. status: { opacity: 1 },
  6760. },
  6761. DID_THROW_ITEM_PROCESSING_ERROR: {
  6762. buttonRemoveItem: { opacity: 1 },
  6763. buttonRetryItemProcessing: { opacity: 1 },
  6764. status: { opacity: 1 },
  6765. info: { translateX: calculateFileInfoOffset },
  6766. },
  6767. DID_THROW_ITEM_PROCESSING_REVERT_ERROR: {
  6768. buttonRevertItemProcessing: { opacity: 1 },
  6769. status: { opacity: 1 },
  6770. info: { opacity: 1 },
  6771. },
  6772. DID_ABORT_ITEM_PROCESSING: {
  6773. buttonRemoveItem: { opacity: 1 },
  6774. buttonProcessItem: { opacity: 1 },
  6775. info: { translateX: calculateFileInfoOffset },
  6776. status: { opacity: 1 },
  6777. },
  6778. DID_REVERT_ITEM_PROCESSING: IdleStyle,
  6779. };
  6780. // complete indicator view
  6781. var processingCompleteIndicatorView = createView({
  6782. create: function create(_ref) {
  6783. var root = _ref.root;
  6784. root.element.innerHTML = root.query('GET_ICON_DONE');
  6785. },
  6786. name: 'processing-complete-indicator',
  6787. ignoreRect: true,
  6788. mixins: {
  6789. styles: ['scaleX', 'scaleY', 'opacity'],
  6790. animations: {
  6791. scaleX: 'spring',
  6792. scaleY: 'spring',
  6793. opacity: { type: 'tween', duration: 250 },
  6794. },
  6795. },
  6796. });
  6797. /**
  6798. * Creates the file view
  6799. */
  6800. var create$4 = function create(_ref2) {
  6801. var root = _ref2.root,
  6802. props = _ref2.props;
  6803. // copy Buttons object
  6804. var LocalButtons = Object.keys(Buttons).reduce(function(prev, curr) {
  6805. prev[curr] = Object.assign({}, Buttons[curr]);
  6806. return prev;
  6807. }, {});
  6808. var id = props.id;
  6809. // allow reverting upload
  6810. var allowRevert = root.query('GET_ALLOW_REVERT');
  6811. // allow remove file
  6812. var allowRemove = root.query('GET_ALLOW_REMOVE');
  6813. // allow processing upload
  6814. var allowProcess = root.query('GET_ALLOW_PROCESS');
  6815. // is instant uploading, need this to determine the icon of the undo button
  6816. var instantUpload = root.query('GET_INSTANT_UPLOAD');
  6817. // is async set up
  6818. var isAsync = root.query('IS_ASYNC');
  6819. // should align remove item buttons
  6820. var alignRemoveItemButton = root.query('GET_STYLE_BUTTON_REMOVE_ITEM_ALIGN');
  6821. // enabled buttons array
  6822. var buttonFilter;
  6823. if (isAsync) {
  6824. if (allowProcess && !allowRevert) {
  6825. // only remove revert button
  6826. buttonFilter = function buttonFilter(key) {
  6827. return !/RevertItemProcessing/.test(key);
  6828. };
  6829. } else if (!allowProcess && allowRevert) {
  6830. // only remove process button
  6831. buttonFilter = function buttonFilter(key) {
  6832. return !/ProcessItem|RetryItemProcessing|AbortItemProcessing/.test(key);
  6833. };
  6834. } else if (!allowProcess && !allowRevert) {
  6835. // remove all process buttons
  6836. buttonFilter = function buttonFilter(key) {
  6837. return !/Process/.test(key);
  6838. };
  6839. }
  6840. } else {
  6841. // no process controls available
  6842. buttonFilter = function buttonFilter(key) {
  6843. return !/Process/.test(key);
  6844. };
  6845. }
  6846. var enabledButtons = buttonFilter ? ButtonKeys.filter(buttonFilter) : ButtonKeys.concat();
  6847. // update icon and label for revert button when instant uploading
  6848. if (instantUpload && allowRevert) {
  6849. LocalButtons['RevertItemProcessing'].label = 'GET_LABEL_BUTTON_REMOVE_ITEM';
  6850. LocalButtons['RevertItemProcessing'].icon = 'GET_ICON_REMOVE';
  6851. }
  6852. // remove last button (revert) if not allowed
  6853. if (isAsync && !allowRevert) {
  6854. var map = StyleMap['DID_COMPLETE_ITEM_PROCESSING'];
  6855. map.info.translateX = calculateFileHorizontalCenterOffset;
  6856. map.info.translateY = calculateFileVerticalCenterOffset;
  6857. map.status.translateY = calculateFileVerticalCenterOffset;
  6858. map.processingCompleteIndicator = { opacity: 1, scaleX: 1, scaleY: 1 };
  6859. }
  6860. // should align center
  6861. if (isAsync && !allowProcess) {
  6862. [
  6863. 'DID_START_ITEM_PROCESSING',
  6864. 'DID_REQUEST_ITEM_PROCESSING',
  6865. 'DID_UPDATE_ITEM_PROCESS_PROGRESS',
  6866. 'DID_THROW_ITEM_PROCESSING_ERROR',
  6867. ].forEach(function(key) {
  6868. StyleMap[key].status.translateY = calculateFileVerticalCenterOffset;
  6869. });
  6870. StyleMap['DID_THROW_ITEM_PROCESSING_ERROR'].status.translateX = calculateButtonWidth;
  6871. }
  6872. // move remove button to right
  6873. if (alignRemoveItemButton && allowRevert) {
  6874. LocalButtons['RevertItemProcessing'].align = 'BUTTON_REMOVE_ITEM_POSITION';
  6875. var _map = StyleMap['DID_COMPLETE_ITEM_PROCESSING'];
  6876. _map.info.translateX = calculateFileInfoOffset;
  6877. _map.status.translateY = calculateFileVerticalCenterOffset;
  6878. _map.processingCompleteIndicator = { opacity: 1, scaleX: 1, scaleY: 1 };
  6879. }
  6880. // show/hide RemoveItem button
  6881. if (!allowRemove) {
  6882. LocalButtons['RemoveItem'].disabled = true;
  6883. }
  6884. // create the button views
  6885. forin(LocalButtons, function(key, definition) {
  6886. // create button
  6887. var buttonView = root.createChildView(fileActionButton, {
  6888. label: root.query(definition.label),
  6889. icon: root.query(definition.icon),
  6890. opacity: 0,
  6891. });
  6892. // should be appended?
  6893. if (enabledButtons.includes(key)) {
  6894. root.appendChildView(buttonView);
  6895. }
  6896. // toggle
  6897. if (definition.disabled) {
  6898. buttonView.element.setAttribute('disabled', 'disabled');
  6899. buttonView.element.setAttribute('hidden', 'hidden');
  6900. }
  6901. // add position attribute
  6902. buttonView.element.dataset.align = root.query('GET_STYLE_' + definition.align);
  6903. // add class
  6904. buttonView.element.classList.add(definition.className);
  6905. // handle interactions
  6906. buttonView.on('click', function(e) {
  6907. e.stopPropagation();
  6908. if (definition.disabled) return;
  6909. root.dispatch(definition.action, { query: id });
  6910. });
  6911. // set reference
  6912. root.ref['button' + key] = buttonView;
  6913. });
  6914. // checkmark
  6915. root.ref.processingCompleteIndicator = root.appendChildView(
  6916. root.createChildView(processingCompleteIndicatorView)
  6917. );
  6918. root.ref.processingCompleteIndicator.element.dataset.align = root.query(
  6919. 'GET_STYLE_BUTTON_PROCESS_ITEM_POSITION'
  6920. );
  6921. // create file info view
  6922. root.ref.info = root.appendChildView(root.createChildView(fileInfo, { id: id }));
  6923. // create file status view
  6924. root.ref.status = root.appendChildView(root.createChildView(fileStatus, { id: id }));
  6925. // add progress indicators
  6926. var loadIndicatorView = root.appendChildView(
  6927. root.createChildView(progressIndicator, {
  6928. opacity: 0,
  6929. align: root.query('GET_STYLE_LOAD_INDICATOR_POSITION'),
  6930. })
  6931. );
  6932. loadIndicatorView.element.classList.add('filepond--load-indicator');
  6933. root.ref.loadProgressIndicator = loadIndicatorView;
  6934. var progressIndicatorView = root.appendChildView(
  6935. root.createChildView(progressIndicator, {
  6936. opacity: 0,
  6937. align: root.query('GET_STYLE_PROGRESS_INDICATOR_POSITION'),
  6938. })
  6939. );
  6940. progressIndicatorView.element.classList.add('filepond--process-indicator');
  6941. root.ref.processProgressIndicator = progressIndicatorView;
  6942. // current active styles
  6943. root.ref.activeStyles = [];
  6944. };
  6945. var write$2 = function write(_ref3) {
  6946. var root = _ref3.root,
  6947. actions = _ref3.actions,
  6948. props = _ref3.props;
  6949. // route actions
  6950. route({ root: root, actions: actions, props: props });
  6951. // select last state change action
  6952. var action = actions
  6953. .concat()
  6954. .filter(function(action) {
  6955. return /^DID_/.test(action.type);
  6956. })
  6957. .reverse()
  6958. .find(function(action) {
  6959. return StyleMap[action.type];
  6960. });
  6961. // a new action happened, let's get the matching styles
  6962. if (action) {
  6963. // define new active styles
  6964. root.ref.activeStyles = [];
  6965. var stylesToApply = StyleMap[action.type];
  6966. forin(DefaultStyle, function(name, defaultStyles) {
  6967. // get reference to control
  6968. var control = root.ref[name];
  6969. // loop over all styles for this control
  6970. forin(defaultStyles, function(key, defaultValue) {
  6971. var value =
  6972. stylesToApply[name] && typeof stylesToApply[name][key] !== 'undefined'
  6973. ? stylesToApply[name][key]
  6974. : defaultValue;
  6975. root.ref.activeStyles.push({ control: control, key: key, value: value });
  6976. });
  6977. });
  6978. }
  6979. // apply active styles to element
  6980. root.ref.activeStyles.forEach(function(_ref4) {
  6981. var control = _ref4.control,
  6982. key = _ref4.key,
  6983. value = _ref4.value;
  6984. control[key] = typeof value === 'function' ? value(root) : value;
  6985. });
  6986. };
  6987. var route = createRoute({
  6988. DID_SET_LABEL_BUTTON_ABORT_ITEM_PROCESSING: function DID_SET_LABEL_BUTTON_ABORT_ITEM_PROCESSING(
  6989. _ref5
  6990. ) {
  6991. var root = _ref5.root,
  6992. action = _ref5.action;
  6993. root.ref.buttonAbortItemProcessing.label = action.value;
  6994. },
  6995. DID_SET_LABEL_BUTTON_ABORT_ITEM_LOAD: function DID_SET_LABEL_BUTTON_ABORT_ITEM_LOAD(_ref6) {
  6996. var root = _ref6.root,
  6997. action = _ref6.action;
  6998. root.ref.buttonAbortItemLoad.label = action.value;
  6999. },
  7000. DID_SET_LABEL_BUTTON_ABORT_ITEM_REMOVAL: function DID_SET_LABEL_BUTTON_ABORT_ITEM_REMOVAL(
  7001. _ref7
  7002. ) {
  7003. var root = _ref7.root,
  7004. action = _ref7.action;
  7005. root.ref.buttonAbortItemRemoval.label = action.value;
  7006. },
  7007. DID_REQUEST_ITEM_PROCESSING: function DID_REQUEST_ITEM_PROCESSING(_ref8) {
  7008. var root = _ref8.root;
  7009. root.ref.processProgressIndicator.spin = true;
  7010. root.ref.processProgressIndicator.progress = 0;
  7011. },
  7012. DID_START_ITEM_LOAD: function DID_START_ITEM_LOAD(_ref9) {
  7013. var root = _ref9.root;
  7014. root.ref.loadProgressIndicator.spin = true;
  7015. root.ref.loadProgressIndicator.progress = 0;
  7016. },
  7017. DID_START_ITEM_REMOVE: function DID_START_ITEM_REMOVE(_ref10) {
  7018. var root = _ref10.root;
  7019. root.ref.processProgressIndicator.spin = true;
  7020. root.ref.processProgressIndicator.progress = 0;
  7021. },
  7022. DID_UPDATE_ITEM_LOAD_PROGRESS: function DID_UPDATE_ITEM_LOAD_PROGRESS(_ref11) {
  7023. var root = _ref11.root,
  7024. action = _ref11.action;
  7025. root.ref.loadProgressIndicator.spin = false;
  7026. root.ref.loadProgressIndicator.progress = action.progress;
  7027. },
  7028. DID_UPDATE_ITEM_PROCESS_PROGRESS: function DID_UPDATE_ITEM_PROCESS_PROGRESS(_ref12) {
  7029. var root = _ref12.root,
  7030. action = _ref12.action;
  7031. root.ref.processProgressIndicator.spin = false;
  7032. root.ref.processProgressIndicator.progress = action.progress;
  7033. },
  7034. });
  7035. var file = createView({
  7036. create: create$4,
  7037. write: write$2,
  7038. didCreateView: function didCreateView(root) {
  7039. applyFilters('CREATE_VIEW', Object.assign({}, root, { view: root }));
  7040. },
  7041. name: 'file',
  7042. });
  7043. /**
  7044. * Creates the file view
  7045. */
  7046. var create$5 = function create(_ref) {
  7047. var root = _ref.root,
  7048. props = _ref.props;
  7049. // filename
  7050. root.ref.fileName = createElement$1('legend');
  7051. root.appendChild(root.ref.fileName);
  7052. // file appended
  7053. root.ref.file = root.appendChildView(root.createChildView(file, { id: props.id }));
  7054. // data has moved to data.js
  7055. root.ref.data = false;
  7056. };
  7057. /**
  7058. * Data storage
  7059. */
  7060. var didLoadItem = function didLoadItem(_ref2) {
  7061. var root = _ref2.root,
  7062. props = _ref2.props;
  7063. // updates the legend of the fieldset so screenreaders can better group buttons
  7064. text(root.ref.fileName, formatFilename(root.query('GET_ITEM_NAME', props.id)));
  7065. };
  7066. var fileWrapper = createView({
  7067. create: create$5,
  7068. ignoreRect: true,
  7069. write: createRoute({
  7070. DID_LOAD_ITEM: didLoadItem,
  7071. }),
  7072. didCreateView: function didCreateView(root) {
  7073. applyFilters('CREATE_VIEW', Object.assign({}, root, { view: root }));
  7074. },
  7075. tag: 'fieldset',
  7076. name: 'file-wrapper',
  7077. });
  7078. var PANEL_SPRING_PROPS = { type: 'spring', damping: 0.6, mass: 7 };
  7079. var create$6 = function create(_ref) {
  7080. var root = _ref.root,
  7081. props = _ref.props;
  7082. [
  7083. {
  7084. name: 'top',
  7085. },
  7086. {
  7087. name: 'center',
  7088. props: {
  7089. translateY: null,
  7090. scaleY: null,
  7091. },
  7092. mixins: {
  7093. animations: {
  7094. scaleY: PANEL_SPRING_PROPS,
  7095. },
  7096. styles: ['translateY', 'scaleY'],
  7097. },
  7098. },
  7099. {
  7100. name: 'bottom',
  7101. props: {
  7102. translateY: null,
  7103. },
  7104. mixins: {
  7105. animations: {
  7106. translateY: PANEL_SPRING_PROPS,
  7107. },
  7108. styles: ['translateY'],
  7109. },
  7110. },
  7111. ].forEach(function(section) {
  7112. createSection(root, section, props.name);
  7113. });
  7114. root.element.classList.add('filepond--' + props.name);
  7115. root.ref.scalable = null;
  7116. };
  7117. var createSection = function createSection(root, section, className) {
  7118. var viewConstructor = createView({
  7119. name: 'panel-' + section.name + ' filepond--' + className,
  7120. mixins: section.mixins,
  7121. ignoreRectUpdate: true,
  7122. });
  7123. var view = root.createChildView(viewConstructor, section.props);
  7124. root.ref[section.name] = root.appendChildView(view);
  7125. };
  7126. var write$3 = function write(_ref2) {
  7127. var root = _ref2.root,
  7128. props = _ref2.props;
  7129. // update scalable state
  7130. if (root.ref.scalable === null || props.scalable !== root.ref.scalable) {
  7131. root.ref.scalable = isBoolean(props.scalable) ? props.scalable : true;
  7132. root.element.dataset.scalable = root.ref.scalable;
  7133. }
  7134. // no height, can't set
  7135. if (!props.height) return;
  7136. // get child rects
  7137. var topRect = root.ref.top.rect.element;
  7138. var bottomRect = root.ref.bottom.rect.element;
  7139. // make sure height never is smaller than bottom and top seciton heights combined (will probably never happen, but who knows)
  7140. var height = Math.max(topRect.height + bottomRect.height, props.height);
  7141. // offset center part
  7142. root.ref.center.translateY = topRect.height;
  7143. // scale center part
  7144. // use math ceil to prevent transparent lines because of rounding errors
  7145. root.ref.center.scaleY = (height - topRect.height - bottomRect.height) / 100;
  7146. // offset bottom part
  7147. root.ref.bottom.translateY = height - bottomRect.height;
  7148. };
  7149. var panel = createView({
  7150. name: 'panel',
  7151. read: function read(_ref3) {
  7152. var root = _ref3.root,
  7153. props = _ref3.props;
  7154. return (props.heightCurrent = root.ref.bottom.translateY);
  7155. },
  7156. write: write$3,
  7157. create: create$6,
  7158. ignoreRect: true,
  7159. mixins: {
  7160. apis: ['height', 'heightCurrent', 'scalable'],
  7161. },
  7162. });
  7163. var createDragHelper = function createDragHelper(items) {
  7164. var itemIds = items.map(function(item) {
  7165. return item.id;
  7166. });
  7167. var prevIndex = undefined;
  7168. return {
  7169. setIndex: function setIndex(index) {
  7170. prevIndex = index;
  7171. },
  7172. getIndex: function getIndex() {
  7173. return prevIndex;
  7174. },
  7175. getItemIndex: function getItemIndex(item) {
  7176. return itemIds.indexOf(item.id);
  7177. },
  7178. };
  7179. };
  7180. var ITEM_TRANSLATE_SPRING = {
  7181. type: 'spring',
  7182. stiffness: 0.75,
  7183. damping: 0.45,
  7184. mass: 10,
  7185. };
  7186. var ITEM_SCALE_SPRING = 'spring';
  7187. var StateMap = {
  7188. DID_START_ITEM_LOAD: 'busy',
  7189. DID_UPDATE_ITEM_LOAD_PROGRESS: 'loading',
  7190. DID_THROW_ITEM_INVALID: 'load-invalid',
  7191. DID_THROW_ITEM_LOAD_ERROR: 'load-error',
  7192. DID_LOAD_ITEM: 'idle',
  7193. DID_THROW_ITEM_REMOVE_ERROR: 'remove-error',
  7194. DID_START_ITEM_REMOVE: 'busy',
  7195. DID_START_ITEM_PROCESSING: 'busy processing',
  7196. DID_REQUEST_ITEM_PROCESSING: 'busy processing',
  7197. DID_UPDATE_ITEM_PROCESS_PROGRESS: 'processing',
  7198. DID_COMPLETE_ITEM_PROCESSING: 'processing-complete',
  7199. DID_THROW_ITEM_PROCESSING_ERROR: 'processing-error',
  7200. DID_THROW_ITEM_PROCESSING_REVERT_ERROR: 'processing-revert-error',
  7201. DID_ABORT_ITEM_PROCESSING: 'cancelled',
  7202. DID_REVERT_ITEM_PROCESSING: 'idle',
  7203. };
  7204. /**
  7205. * Creates the file view
  7206. */
  7207. var create$7 = function create(_ref) {
  7208. var root = _ref.root,
  7209. props = _ref.props;
  7210. // select
  7211. root.ref.handleClick = function(e) {
  7212. return root.dispatch('DID_ACTIVATE_ITEM', { id: props.id });
  7213. };
  7214. // set id
  7215. root.element.id = 'filepond--item-' + props.id;
  7216. root.element.addEventListener('click', root.ref.handleClick);
  7217. // file view
  7218. root.ref.container = root.appendChildView(
  7219. root.createChildView(fileWrapper, { id: props.id })
  7220. );
  7221. // file panel
  7222. root.ref.panel = root.appendChildView(root.createChildView(panel, { name: 'item-panel' }));
  7223. // default start height
  7224. root.ref.panel.height = null;
  7225. // by default not marked for removal
  7226. props.markedForRemoval = false;
  7227. // if not allowed to reorder file items, exit here
  7228. if (!root.query('GET_ALLOW_REORDER')) return;
  7229. // set to idle so shows grab cursor
  7230. root.element.dataset.dragState = 'idle';
  7231. var grab = function grab(e) {
  7232. if (!e.isPrimary) return;
  7233. var removedActivateListener = false;
  7234. var origin = {
  7235. x: e.pageX,
  7236. y: e.pageY,
  7237. };
  7238. props.dragOrigin = {
  7239. x: root.translateX,
  7240. y: root.translateY,
  7241. };
  7242. props.dragCenter = {
  7243. x: e.offsetX,
  7244. y: e.offsetY,
  7245. };
  7246. var dragState = createDragHelper(root.query('GET_ACTIVE_ITEMS'));
  7247. root.dispatch('DID_GRAB_ITEM', { id: props.id, dragState: dragState });
  7248. var drag = function drag(e) {
  7249. if (!e.isPrimary) return;
  7250. e.stopPropagation();
  7251. e.preventDefault();
  7252. props.dragOffset = {
  7253. x: e.pageX - origin.x,
  7254. y: e.pageY - origin.y,
  7255. };
  7256. // if dragged stop listening to clicks, will re-add when done dragging
  7257. var dist =
  7258. props.dragOffset.x * props.dragOffset.x +
  7259. props.dragOffset.y * props.dragOffset.y;
  7260. if (dist > 16 && !removedActivateListener) {
  7261. removedActivateListener = true;
  7262. root.element.removeEventListener('click', root.ref.handleClick);
  7263. }
  7264. root.dispatch('DID_DRAG_ITEM', { id: props.id, dragState: dragState });
  7265. };
  7266. var drop = function drop(e) {
  7267. if (!e.isPrimary) return;
  7268. document.removeEventListener('pointermove', drag);
  7269. document.removeEventListener('pointerup', drop);
  7270. props.dragOffset = {
  7271. x: e.pageX - origin.x,
  7272. y: e.pageY - origin.y,
  7273. };
  7274. root.dispatch('DID_DROP_ITEM', { id: props.id, dragState: dragState });
  7275. // start listening to clicks again
  7276. if (removedActivateListener) {
  7277. setTimeout(function() {
  7278. return root.element.addEventListener('click', root.ref.handleClick);
  7279. }, 0);
  7280. }
  7281. };
  7282. document.addEventListener('pointermove', drag);
  7283. document.addEventListener('pointerup', drop);
  7284. };
  7285. root.element.addEventListener('pointerdown', grab);
  7286. };
  7287. var route$1 = createRoute({
  7288. DID_UPDATE_PANEL_HEIGHT: function DID_UPDATE_PANEL_HEIGHT(_ref2) {
  7289. var root = _ref2.root,
  7290. action = _ref2.action;
  7291. root.height = action.height;
  7292. },
  7293. });
  7294. var write$4 = createRoute(
  7295. {
  7296. DID_GRAB_ITEM: function DID_GRAB_ITEM(_ref3) {
  7297. var root = _ref3.root,
  7298. props = _ref3.props;
  7299. props.dragOrigin = {
  7300. x: root.translateX,
  7301. y: root.translateY,
  7302. };
  7303. },
  7304. DID_DRAG_ITEM: function DID_DRAG_ITEM(_ref4) {
  7305. var root = _ref4.root;
  7306. root.element.dataset.dragState = 'drag';
  7307. },
  7308. DID_DROP_ITEM: function DID_DROP_ITEM(_ref5) {
  7309. var root = _ref5.root,
  7310. props = _ref5.props;
  7311. props.dragOffset = null;
  7312. props.dragOrigin = null;
  7313. root.element.dataset.dragState = 'drop';
  7314. },
  7315. },
  7316. function(_ref6) {
  7317. var root = _ref6.root,
  7318. actions = _ref6.actions,
  7319. props = _ref6.props,
  7320. shouldOptimize = _ref6.shouldOptimize;
  7321. if (root.element.dataset.dragState === 'drop') {
  7322. if (root.scaleX <= 1) {
  7323. root.element.dataset.dragState = 'idle';
  7324. }
  7325. }
  7326. // select last state change action
  7327. var action = actions
  7328. .concat()
  7329. .filter(function(action) {
  7330. return /^DID_/.test(action.type);
  7331. })
  7332. .reverse()
  7333. .find(function(action) {
  7334. return StateMap[action.type];
  7335. });
  7336. // no need to set same state twice
  7337. if (action && action.type !== props.currentState) {
  7338. // set current state
  7339. props.currentState = action.type;
  7340. // set state
  7341. root.element.dataset.filepondItemState = StateMap[props.currentState] || '';
  7342. }
  7343. // route actions
  7344. var aspectRatio =
  7345. root.query('GET_ITEM_PANEL_ASPECT_RATIO') || root.query('GET_PANEL_ASPECT_RATIO');
  7346. if (!aspectRatio) {
  7347. route$1({ root: root, actions: actions, props: props });
  7348. if (!root.height && root.ref.container.rect.element.height > 0) {
  7349. root.height = root.ref.container.rect.element.height;
  7350. }
  7351. } else if (!shouldOptimize) {
  7352. root.height = root.rect.element.width * aspectRatio;
  7353. }
  7354. // sync panel height with item height
  7355. if (shouldOptimize) {
  7356. root.ref.panel.height = null;
  7357. }
  7358. root.ref.panel.height = root.height;
  7359. }
  7360. );
  7361. var item = createView({
  7362. create: create$7,
  7363. write: write$4,
  7364. destroy: function destroy(_ref7) {
  7365. var root = _ref7.root,
  7366. props = _ref7.props;
  7367. root.element.removeEventListener('click', root.ref.handleClick);
  7368. root.dispatch('RELEASE_ITEM', { query: props.id });
  7369. },
  7370. tag: 'li',
  7371. name: 'item',
  7372. mixins: {
  7373. apis: [
  7374. 'id',
  7375. 'interactionMethod',
  7376. 'markedForRemoval',
  7377. 'spawnDate',
  7378. 'dragCenter',
  7379. 'dragOrigin',
  7380. 'dragOffset',
  7381. ],
  7382. styles: ['translateX', 'translateY', 'scaleX', 'scaleY', 'opacity', 'height'],
  7383. animations: {
  7384. scaleX: ITEM_SCALE_SPRING,
  7385. scaleY: ITEM_SCALE_SPRING,
  7386. translateX: ITEM_TRANSLATE_SPRING,
  7387. translateY: ITEM_TRANSLATE_SPRING,
  7388. opacity: { type: 'tween', duration: 150 },
  7389. },
  7390. },
  7391. });
  7392. var getItemsPerRow = function(horizontalSpace, itemWidth) {
  7393. // add one pixel leeway, when using percentages for item width total items can be 1.99 per row
  7394. return Math.max(1, Math.floor((horizontalSpace + 1) / itemWidth));
  7395. };
  7396. var getItemIndexByPosition = function getItemIndexByPosition(view, children, positionInView) {
  7397. if (!positionInView) return;
  7398. var horizontalSpace = view.rect.element.width;
  7399. // const children = view.childViews;
  7400. var l = children.length;
  7401. var last = null;
  7402. // -1, don't move items to accomodate (either add to top or bottom)
  7403. if (l === 0 || positionInView.top < children[0].rect.element.top) return -1;
  7404. // let's get the item width
  7405. var item = children[0];
  7406. var itemRect = item.rect.element;
  7407. var itemHorizontalMargin = itemRect.marginLeft + itemRect.marginRight;
  7408. var itemWidth = itemRect.width + itemHorizontalMargin;
  7409. var itemsPerRow = getItemsPerRow(horizontalSpace, itemWidth);
  7410. // stack
  7411. if (itemsPerRow === 1) {
  7412. for (var index = 0; index < l; index++) {
  7413. var child = children[index];
  7414. var childMid = child.rect.outer.top + child.rect.element.height * 0.5;
  7415. if (positionInView.top < childMid) {
  7416. return index;
  7417. }
  7418. }
  7419. return l;
  7420. }
  7421. // grid
  7422. var itemVerticalMargin = itemRect.marginTop + itemRect.marginBottom;
  7423. var itemHeight = itemRect.height + itemVerticalMargin;
  7424. for (var _index = 0; _index < l; _index++) {
  7425. var indexX = _index % itemsPerRow;
  7426. var indexY = Math.floor(_index / itemsPerRow);
  7427. var offsetX = indexX * itemWidth;
  7428. var offsetY = indexY * itemHeight;
  7429. var itemTop = offsetY - itemRect.marginTop;
  7430. var itemRight = offsetX + itemWidth;
  7431. var itemBottom = offsetY + itemHeight + itemRect.marginBottom;
  7432. if (positionInView.top < itemBottom && positionInView.top > itemTop) {
  7433. if (positionInView.left < itemRight) {
  7434. return _index;
  7435. } else if (_index !== l - 1) {
  7436. last = _index;
  7437. } else {
  7438. last = null;
  7439. }
  7440. }
  7441. }
  7442. if (last !== null) {
  7443. return last;
  7444. }
  7445. return l;
  7446. };
  7447. var dropAreaDimensions = {
  7448. height: 0,
  7449. width: 0,
  7450. get getHeight() {
  7451. return this.height;
  7452. },
  7453. set setHeight(val) {
  7454. if (this.height === 0 || val === 0) this.height = val;
  7455. },
  7456. get getWidth() {
  7457. return this.width;
  7458. },
  7459. set setWidth(val) {
  7460. if (this.width === 0 || val === 0) this.width = val;
  7461. },
  7462. setDimensions: function setDimensions(height, width) {
  7463. if (this.height === 0 || height === 0) this.height = height;
  7464. if (this.width === 0 || width === 0) this.width = width;
  7465. },
  7466. };
  7467. var create$8 = function create(_ref) {
  7468. var root = _ref.root;
  7469. // need to set role to list as otherwise it won't be read as a list by VoiceOver
  7470. attr(root.element, 'role', 'list');
  7471. root.ref.lastItemSpanwDate = Date.now();
  7472. };
  7473. /**
  7474. * Inserts a new item
  7475. * @param root
  7476. * @param action
  7477. */
  7478. var addItemView = function addItemView(_ref2) {
  7479. var root = _ref2.root,
  7480. action = _ref2.action;
  7481. var id = action.id,
  7482. index = action.index,
  7483. interactionMethod = action.interactionMethod;
  7484. root.ref.addIndex = index;
  7485. var now = Date.now();
  7486. var spawnDate = now;
  7487. var opacity = 1;
  7488. if (interactionMethod !== InteractionMethod.NONE) {
  7489. opacity = 0;
  7490. var cooldown = root.query('GET_ITEM_INSERT_INTERVAL');
  7491. var dist = now - root.ref.lastItemSpanwDate;
  7492. spawnDate = dist < cooldown ? now + (cooldown - dist) : now;
  7493. }
  7494. root.ref.lastItemSpanwDate = spawnDate;
  7495. root.appendChildView(
  7496. root.createChildView(
  7497. // view type
  7498. item,
  7499. // props
  7500. {
  7501. spawnDate: spawnDate,
  7502. id: id,
  7503. opacity: opacity,
  7504. interactionMethod: interactionMethod,
  7505. }
  7506. ),
  7507. index
  7508. );
  7509. };
  7510. var moveItem = function moveItem(item, x, y) {
  7511. var vx = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  7512. var vy = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  7513. // set to null to remove animation while dragging
  7514. if (item.dragOffset) {
  7515. item.translateX = null;
  7516. item.translateY = null;
  7517. item.translateX = item.dragOrigin.x + item.dragOffset.x;
  7518. item.translateY = item.dragOrigin.y + item.dragOffset.y;
  7519. item.scaleX = 1.025;
  7520. item.scaleY = 1.025;
  7521. } else {
  7522. item.translateX = x;
  7523. item.translateY = y;
  7524. if (Date.now() > item.spawnDate) {
  7525. // reveal element
  7526. if (item.opacity === 0) {
  7527. introItemView(item, x, y, vx, vy);
  7528. }
  7529. // make sure is default scale every frame
  7530. item.scaleX = 1;
  7531. item.scaleY = 1;
  7532. item.opacity = 1;
  7533. }
  7534. }
  7535. };
  7536. var introItemView = function introItemView(item, x, y, vx, vy) {
  7537. if (item.interactionMethod === InteractionMethod.NONE) {
  7538. item.translateX = null;
  7539. item.translateX = x;
  7540. item.translateY = null;
  7541. item.translateY = y;
  7542. } else if (item.interactionMethod === InteractionMethod.DROP) {
  7543. item.translateX = null;
  7544. item.translateX = x - vx * 20;
  7545. item.translateY = null;
  7546. item.translateY = y - vy * 10;
  7547. item.scaleX = 0.8;
  7548. item.scaleY = 0.8;
  7549. } else if (item.interactionMethod === InteractionMethod.BROWSE) {
  7550. item.translateY = null;
  7551. item.translateY = y - 30;
  7552. } else if (item.interactionMethod === InteractionMethod.API) {
  7553. item.translateX = null;
  7554. item.translateX = x - 30;
  7555. item.translateY = null;
  7556. }
  7557. };
  7558. /**
  7559. * Removes an existing item
  7560. * @param root
  7561. * @param action
  7562. */
  7563. var removeItemView = function removeItemView(_ref3) {
  7564. var root = _ref3.root,
  7565. action = _ref3.action;
  7566. var id = action.id;
  7567. // get the view matching the given id
  7568. var view = root.childViews.find(function(child) {
  7569. return child.id === id;
  7570. });
  7571. // if no view found, exit
  7572. if (!view) {
  7573. return;
  7574. }
  7575. // animate view out of view
  7576. view.scaleX = 0.9;
  7577. view.scaleY = 0.9;
  7578. view.opacity = 0;
  7579. // mark for removal
  7580. view.markedForRemoval = true;
  7581. };
  7582. var getItemHeight = function getItemHeight(child) {
  7583. return (
  7584. child.rect.element.height +
  7585. child.rect.element.marginBottom * 0.5 +
  7586. child.rect.element.marginTop * 0.5
  7587. );
  7588. };
  7589. var getItemWidth = function getItemWidth(child) {
  7590. return (
  7591. child.rect.element.width +
  7592. child.rect.element.marginLeft * 0.5 +
  7593. child.rect.element.marginRight * 0.5
  7594. );
  7595. };
  7596. var dragItem = function dragItem(_ref4) {
  7597. var root = _ref4.root,
  7598. action = _ref4.action;
  7599. var id = action.id,
  7600. dragState = action.dragState;
  7601. // reference to item
  7602. var item = root.query('GET_ITEM', { id: id });
  7603. // get the view matching the given id
  7604. var view = root.childViews.find(function(child) {
  7605. return child.id === id;
  7606. });
  7607. var numItems = root.childViews.length;
  7608. var oldIndex = dragState.getItemIndex(item);
  7609. // if no view found, exit
  7610. if (!view) return;
  7611. var dragPosition = {
  7612. x: view.dragOrigin.x + view.dragOffset.x + view.dragCenter.x,
  7613. y: view.dragOrigin.y + view.dragOffset.y + view.dragCenter.y,
  7614. };
  7615. // get drag area dimensions
  7616. var dragHeight = getItemHeight(view);
  7617. var dragWidth = getItemWidth(view);
  7618. // get rows and columns (There will always be at least one row and one column if a file is present)
  7619. var cols = Math.floor(root.rect.outer.width / dragWidth);
  7620. if (cols > numItems) cols = numItems;
  7621. // rows are used to find when we have left the preview area bounding box
  7622. var rows = Math.floor(numItems / cols + 1);
  7623. dropAreaDimensions.setHeight = dragHeight * rows;
  7624. dropAreaDimensions.setWidth = dragWidth * cols;
  7625. // get new index of dragged item
  7626. var location = {
  7627. y: Math.floor(dragPosition.y / dragHeight),
  7628. x: Math.floor(dragPosition.x / dragWidth),
  7629. getGridIndex: function getGridIndex() {
  7630. if (
  7631. dragPosition.y > dropAreaDimensions.getHeight ||
  7632. dragPosition.y < 0 ||
  7633. dragPosition.x > dropAreaDimensions.getWidth ||
  7634. dragPosition.x < 0
  7635. )
  7636. return oldIndex;
  7637. return this.y * cols + this.x;
  7638. },
  7639. getColIndex: function getColIndex() {
  7640. var items = root.query('GET_ACTIVE_ITEMS');
  7641. var visibleChildren = root.childViews.filter(function(child) {
  7642. return child.rect.element.height;
  7643. });
  7644. var children = items.map(function(item) {
  7645. return visibleChildren.find(function(childView) {
  7646. return childView.id === item.id;
  7647. });
  7648. });
  7649. var currentIndex = children.findIndex(function(child) {
  7650. return child === view;
  7651. });
  7652. var dragHeight = getItemHeight(view);
  7653. var l = children.length;
  7654. var idx = l;
  7655. var childHeight = 0;
  7656. var childBottom = 0;
  7657. var childTop = 0;
  7658. for (var i = 0; i < l; i++) {
  7659. childHeight = getItemHeight(children[i]);
  7660. childTop = childBottom;
  7661. childBottom = childTop + childHeight;
  7662. if (dragPosition.y < childBottom) {
  7663. if (currentIndex > i) {
  7664. if (dragPosition.y < childTop + dragHeight) {
  7665. idx = i;
  7666. break;
  7667. }
  7668. continue;
  7669. }
  7670. idx = i;
  7671. break;
  7672. }
  7673. }
  7674. return idx;
  7675. },
  7676. };
  7677. // get new index
  7678. var index = cols > 1 ? location.getGridIndex() : location.getColIndex();
  7679. root.dispatch('MOVE_ITEM', { query: view, index: index });
  7680. // if the index of the item changed, dispatch reorder action
  7681. var currentIndex = dragState.getIndex();
  7682. if (currentIndex === undefined || currentIndex !== index) {
  7683. dragState.setIndex(index);
  7684. if (currentIndex === undefined) return;
  7685. root.dispatch('DID_REORDER_ITEMS', {
  7686. items: root.query('GET_ACTIVE_ITEMS'),
  7687. origin: oldIndex,
  7688. target: index,
  7689. });
  7690. }
  7691. };
  7692. /**
  7693. * Setup action routes
  7694. */
  7695. var route$2 = createRoute({
  7696. DID_ADD_ITEM: addItemView,
  7697. DID_REMOVE_ITEM: removeItemView,
  7698. DID_DRAG_ITEM: dragItem,
  7699. });
  7700. /**
  7701. * Write to view
  7702. * @param root
  7703. * @param actions
  7704. * @param props
  7705. */
  7706. var write$5 = function write(_ref5) {
  7707. var root = _ref5.root,
  7708. props = _ref5.props,
  7709. actions = _ref5.actions,
  7710. shouldOptimize = _ref5.shouldOptimize;
  7711. // route actions
  7712. route$2({ root: root, props: props, actions: actions });
  7713. var dragCoordinates = props.dragCoordinates;
  7714. // available space on horizontal axis
  7715. var horizontalSpace = root.rect.element.width;
  7716. // only draw children that have dimensions
  7717. var visibleChildren = root.childViews.filter(function(child) {
  7718. return child.rect.element.height;
  7719. });
  7720. // sort based on current active items
  7721. var children = root
  7722. .query('GET_ACTIVE_ITEMS')
  7723. .map(function(item) {
  7724. return visibleChildren.find(function(child) {
  7725. return child.id === item.id;
  7726. });
  7727. })
  7728. .filter(function(item) {
  7729. return item;
  7730. });
  7731. // get index
  7732. var dragIndex = dragCoordinates
  7733. ? getItemIndexByPosition(root, children, dragCoordinates)
  7734. : null;
  7735. // add index is used to reserve the dropped/added item index till the actual item is rendered
  7736. var addIndex = root.ref.addIndex || null;
  7737. // add index no longer needed till possibly next draw
  7738. root.ref.addIndex = null;
  7739. var dragIndexOffset = 0;
  7740. var removeIndexOffset = 0;
  7741. var addIndexOffset = 0;
  7742. if (children.length === 0) return;
  7743. var childRect = children[0].rect.element;
  7744. var itemVerticalMargin = childRect.marginTop + childRect.marginBottom;
  7745. var itemHorizontalMargin = childRect.marginLeft + childRect.marginRight;
  7746. var itemWidth = childRect.width + itemHorizontalMargin;
  7747. var itemHeight = childRect.height + itemVerticalMargin;
  7748. var itemsPerRow = getItemsPerRow(horizontalSpace, itemWidth);
  7749. // stack
  7750. if (itemsPerRow === 1) {
  7751. var offsetY = 0;
  7752. var dragOffset = 0;
  7753. children.forEach(function(child, index) {
  7754. if (dragIndex) {
  7755. var dist = index - dragIndex;
  7756. if (dist === -2) {
  7757. dragOffset = -itemVerticalMargin * 0.25;
  7758. } else if (dist === -1) {
  7759. dragOffset = -itemVerticalMargin * 0.75;
  7760. } else if (dist === 0) {
  7761. dragOffset = itemVerticalMargin * 0.75;
  7762. } else if (dist === 1) {
  7763. dragOffset = itemVerticalMargin * 0.25;
  7764. } else {
  7765. dragOffset = 0;
  7766. }
  7767. }
  7768. if (shouldOptimize) {
  7769. child.translateX = null;
  7770. child.translateY = null;
  7771. }
  7772. if (!child.markedForRemoval) {
  7773. moveItem(child, 0, offsetY + dragOffset);
  7774. }
  7775. var itemHeight = child.rect.element.height + itemVerticalMargin;
  7776. var visualHeight = itemHeight * (child.markedForRemoval ? child.opacity : 1);
  7777. offsetY += visualHeight;
  7778. });
  7779. }
  7780. // grid
  7781. else {
  7782. var prevX = 0;
  7783. var prevY = 0;
  7784. children.forEach(function(child, index) {
  7785. if (index === dragIndex) {
  7786. dragIndexOffset = 1;
  7787. }
  7788. if (index === addIndex) {
  7789. addIndexOffset += 1;
  7790. }
  7791. if (child.markedForRemoval && child.opacity < 0.5) {
  7792. removeIndexOffset -= 1;
  7793. }
  7794. var visualIndex = index + addIndexOffset + dragIndexOffset + removeIndexOffset;
  7795. var indexX = visualIndex % itemsPerRow;
  7796. var indexY = Math.floor(visualIndex / itemsPerRow);
  7797. var offsetX = indexX * itemWidth;
  7798. var offsetY = indexY * itemHeight;
  7799. var vectorX = Math.sign(offsetX - prevX);
  7800. var vectorY = Math.sign(offsetY - prevY);
  7801. prevX = offsetX;
  7802. prevY = offsetY;
  7803. if (child.markedForRemoval) return;
  7804. if (shouldOptimize) {
  7805. child.translateX = null;
  7806. child.translateY = null;
  7807. }
  7808. moveItem(child, offsetX, offsetY, vectorX, vectorY);
  7809. });
  7810. }
  7811. };
  7812. /**
  7813. * Filters actions that are meant specifically for a certain child of the list
  7814. * @param child
  7815. * @param actions
  7816. */
  7817. var filterSetItemActions = function filterSetItemActions(child, actions) {
  7818. return actions.filter(function(action) {
  7819. // if action has an id, filter out actions that don't have this child id
  7820. if (action.data && action.data.id) {
  7821. return child.id === action.data.id;
  7822. }
  7823. // allow all other actions
  7824. return true;
  7825. });
  7826. };
  7827. var list = createView({
  7828. create: create$8,
  7829. write: write$5,
  7830. tag: 'ul',
  7831. name: 'list',
  7832. didWriteView: function didWriteView(_ref6) {
  7833. var root = _ref6.root;
  7834. root.childViews
  7835. .filter(function(view) {
  7836. return view.markedForRemoval && view.opacity === 0 && view.resting;
  7837. })
  7838. .forEach(function(view) {
  7839. view._destroy();
  7840. root.removeChildView(view);
  7841. });
  7842. },
  7843. filterFrameActionsForChild: filterSetItemActions,
  7844. mixins: {
  7845. apis: ['dragCoordinates'],
  7846. },
  7847. });
  7848. var create$9 = function create(_ref) {
  7849. var root = _ref.root,
  7850. props = _ref.props;
  7851. root.ref.list = root.appendChildView(root.createChildView(list));
  7852. props.dragCoordinates = null;
  7853. props.overflowing = false;
  7854. };
  7855. var storeDragCoordinates = function storeDragCoordinates(_ref2) {
  7856. var root = _ref2.root,
  7857. props = _ref2.props,
  7858. action = _ref2.action;
  7859. if (!root.query('GET_ITEM_INSERT_LOCATION_FREEDOM')) return;
  7860. props.dragCoordinates = {
  7861. left: action.position.scopeLeft - root.ref.list.rect.element.left,
  7862. top:
  7863. action.position.scopeTop -
  7864. (root.rect.outer.top + root.rect.element.marginTop + root.rect.element.scrollTop),
  7865. };
  7866. };
  7867. var clearDragCoordinates = function clearDragCoordinates(_ref3) {
  7868. var props = _ref3.props;
  7869. props.dragCoordinates = null;
  7870. };
  7871. var route$3 = createRoute({
  7872. DID_DRAG: storeDragCoordinates,
  7873. DID_END_DRAG: clearDragCoordinates,
  7874. });
  7875. var write$6 = function write(_ref4) {
  7876. var root = _ref4.root,
  7877. props = _ref4.props,
  7878. actions = _ref4.actions;
  7879. // route actions
  7880. route$3({ root: root, props: props, actions: actions });
  7881. // current drag position
  7882. root.ref.list.dragCoordinates = props.dragCoordinates;
  7883. // if currently overflowing but no longer received overflow
  7884. if (props.overflowing && !props.overflow) {
  7885. props.overflowing = false;
  7886. // reset overflow state
  7887. root.element.dataset.state = '';
  7888. root.height = null;
  7889. }
  7890. // if is not overflowing currently but does receive overflow value
  7891. if (props.overflow) {
  7892. var newHeight = Math.round(props.overflow);
  7893. if (newHeight !== root.height) {
  7894. props.overflowing = true;
  7895. root.element.dataset.state = 'overflow';
  7896. root.height = newHeight;
  7897. }
  7898. }
  7899. };
  7900. var listScroller = createView({
  7901. create: create$9,
  7902. write: write$6,
  7903. name: 'list-scroller',
  7904. mixins: {
  7905. apis: ['overflow', 'dragCoordinates'],
  7906. styles: ['height', 'translateY'],
  7907. animations: {
  7908. translateY: 'spring',
  7909. },
  7910. },
  7911. });
  7912. var attrToggle = function attrToggle(element, name, state) {
  7913. var enabledValue = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
  7914. if (state) {
  7915. attr(element, name, enabledValue);
  7916. } else {
  7917. element.removeAttribute(name);
  7918. }
  7919. };
  7920. var resetFileInput = function resetFileInput(input) {
  7921. // no value, no need to reset
  7922. if (!input || input.value === '') {
  7923. return;
  7924. }
  7925. try {
  7926. // for modern browsers
  7927. input.value = '';
  7928. } catch (err) {}
  7929. // for IE10
  7930. if (input.value) {
  7931. // quickly append input to temp form and reset form
  7932. var form = createElement$1('form');
  7933. var parentNode = input.parentNode;
  7934. var ref = input.nextSibling;
  7935. form.appendChild(input);
  7936. form.reset();
  7937. // re-inject input where it originally was
  7938. if (ref) {
  7939. parentNode.insertBefore(input, ref);
  7940. } else {
  7941. parentNode.appendChild(input);
  7942. }
  7943. }
  7944. };
  7945. var create$a = function create(_ref) {
  7946. var root = _ref.root,
  7947. props = _ref.props;
  7948. // set id so can be referenced from outside labels
  7949. root.element.id = 'filepond--browser-' + props.id;
  7950. // set name of element (is removed when a value is set)
  7951. attr(root.element, 'name', root.query('GET_NAME'));
  7952. // we have to link this element to the status element
  7953. attr(root.element, 'aria-controls', 'filepond--assistant-' + props.id);
  7954. // set label, we use labelled by as otherwise the screenreader does not read the "browse" text in the label (as it has tabindex: 0)
  7955. attr(root.element, 'aria-labelledby', 'filepond--drop-label-' + props.id);
  7956. // set configurable props
  7957. setAcceptedFileTypes({
  7958. root: root,
  7959. action: { value: root.query('GET_ACCEPTED_FILE_TYPES') },
  7960. });
  7961. toggleAllowMultiple({ root: root, action: { value: root.query('GET_ALLOW_MULTIPLE') } });
  7962. toggleDirectoryFilter({
  7963. root: root,
  7964. action: { value: root.query('GET_ALLOW_DIRECTORIES_ONLY') },
  7965. });
  7966. toggleDisabled({ root: root });
  7967. toggleRequired({ root: root, action: { value: root.query('GET_REQUIRED') } });
  7968. setCaptureMethod({ root: root, action: { value: root.query('GET_CAPTURE_METHOD') } });
  7969. // handle changes to the input field
  7970. root.ref.handleChange = function(e) {
  7971. if (!root.element.value) {
  7972. return;
  7973. }
  7974. // extract files and move value of webkitRelativePath path to _relativePath
  7975. var files = Array.from(root.element.files).map(function(file) {
  7976. file._relativePath = file.webkitRelativePath;
  7977. return file;
  7978. });
  7979. // we add a little delay so the OS file select window can move out of the way before we add our file
  7980. setTimeout(function() {
  7981. // load files
  7982. props.onload(files);
  7983. // reset input, it's just for exposing a method to drop files, should not retain any state
  7984. resetFileInput(root.element);
  7985. }, 250);
  7986. };
  7987. root.element.addEventListener('change', root.ref.handleChange);
  7988. };
  7989. var setAcceptedFileTypes = function setAcceptedFileTypes(_ref2) {
  7990. var root = _ref2.root,
  7991. action = _ref2.action;
  7992. if (!root.query('GET_ALLOW_SYNC_ACCEPT_ATTRIBUTE')) return;
  7993. attrToggle(
  7994. root.element,
  7995. 'accept',
  7996. !!action.value,
  7997. action.value ? action.value.join(',') : ''
  7998. );
  7999. };
  8000. var toggleAllowMultiple = function toggleAllowMultiple(_ref3) {
  8001. var root = _ref3.root,
  8002. action = _ref3.action;
  8003. attrToggle(root.element, 'multiple', action.value);
  8004. };
  8005. var toggleDirectoryFilter = function toggleDirectoryFilter(_ref4) {
  8006. var root = _ref4.root,
  8007. action = _ref4.action;
  8008. attrToggle(root.element, 'webkitdirectory', action.value);
  8009. };
  8010. var toggleDisabled = function toggleDisabled(_ref5) {
  8011. var root = _ref5.root;
  8012. var isDisabled = root.query('GET_DISABLED');
  8013. var doesAllowBrowse = root.query('GET_ALLOW_BROWSE');
  8014. var disableField = isDisabled || !doesAllowBrowse;
  8015. attrToggle(root.element, 'disabled', disableField);
  8016. };
  8017. var toggleRequired = function toggleRequired(_ref6) {
  8018. var root = _ref6.root,
  8019. action = _ref6.action;
  8020. // want to remove required, always possible
  8021. if (!action.value) {
  8022. attrToggle(root.element, 'required', false);
  8023. }
  8024. // if want to make required, only possible when zero items
  8025. else if (root.query('GET_TOTAL_ITEMS') === 0) {
  8026. attrToggle(root.element, 'required', true);
  8027. }
  8028. };
  8029. var setCaptureMethod = function setCaptureMethod(_ref7) {
  8030. var root = _ref7.root,
  8031. action = _ref7.action;
  8032. attrToggle(
  8033. root.element,
  8034. 'capture',
  8035. !!action.value,
  8036. action.value === true ? '' : action.value
  8037. );
  8038. };
  8039. var updateRequiredStatus = function updateRequiredStatus(_ref8) {
  8040. var root = _ref8.root;
  8041. var element = root.element;
  8042. // always remove the required attribute when more than zero items
  8043. if (root.query('GET_TOTAL_ITEMS') > 0) {
  8044. attrToggle(element, 'required', false);
  8045. attrToggle(element, 'name', false);
  8046. } else {
  8047. // add name attribute
  8048. attrToggle(element, 'name', true, root.query('GET_NAME'));
  8049. // remove any validation messages
  8050. var shouldCheckValidity = root.query('GET_CHECK_VALIDITY');
  8051. if (shouldCheckValidity) {
  8052. element.setCustomValidity('');
  8053. }
  8054. // we only add required if the field has been deemed required
  8055. if (root.query('GET_REQUIRED')) {
  8056. attrToggle(element, 'required', true);
  8057. }
  8058. }
  8059. };
  8060. var updateFieldValidityStatus = function updateFieldValidityStatus(_ref9) {
  8061. var root = _ref9.root;
  8062. var shouldCheckValidity = root.query('GET_CHECK_VALIDITY');
  8063. if (!shouldCheckValidity) return;
  8064. root.element.setCustomValidity(root.query('GET_LABEL_INVALID_FIELD'));
  8065. };
  8066. var browser = createView({
  8067. tag: 'input',
  8068. name: 'browser',
  8069. ignoreRect: true,
  8070. ignoreRectUpdate: true,
  8071. attributes: {
  8072. type: 'file',
  8073. },
  8074. create: create$a,
  8075. destroy: function destroy(_ref10) {
  8076. var root = _ref10.root;
  8077. root.element.removeEventListener('change', root.ref.handleChange);
  8078. },
  8079. write: createRoute({
  8080. DID_LOAD_ITEM: updateRequiredStatus,
  8081. DID_REMOVE_ITEM: updateRequiredStatus,
  8082. DID_THROW_ITEM_INVALID: updateFieldValidityStatus,
  8083. DID_SET_DISABLED: toggleDisabled,
  8084. DID_SET_ALLOW_BROWSE: toggleDisabled,
  8085. DID_SET_ALLOW_DIRECTORIES_ONLY: toggleDirectoryFilter,
  8086. DID_SET_ALLOW_MULTIPLE: toggleAllowMultiple,
  8087. DID_SET_ACCEPTED_FILE_TYPES: setAcceptedFileTypes,
  8088. DID_SET_CAPTURE_METHOD: setCaptureMethod,
  8089. DID_SET_REQUIRED: toggleRequired,
  8090. }),
  8091. });
  8092. var Key = {
  8093. ENTER: 13,
  8094. SPACE: 32,
  8095. };
  8096. var create$b = function create(_ref) {
  8097. var root = _ref.root,
  8098. props = _ref.props;
  8099. // create the label and link it to the file browser
  8100. var label = createElement$1('label');
  8101. attr(label, 'for', 'filepond--browser-' + props.id);
  8102. // use for labeling file input (aria-labelledby on file input)
  8103. attr(label, 'id', 'filepond--drop-label-' + props.id);
  8104. // hide the label for screenreaders, the input element will read the contents of the label when it's focussed. If we don't set aria-hidden the screenreader will also navigate the contents of the label separately from the input.
  8105. attr(label, 'aria-hidden', 'true');
  8106. // handle keys
  8107. root.ref.handleKeyDown = function(e) {
  8108. var isActivationKey = e.keyCode === Key.ENTER || e.keyCode === Key.SPACE;
  8109. if (!isActivationKey) return;
  8110. // stops from triggering the element a second time
  8111. e.preventDefault();
  8112. // click link (will then in turn activate file input)
  8113. root.ref.label.click();
  8114. };
  8115. root.ref.handleClick = function(e) {
  8116. var isLabelClick = e.target === label || label.contains(e.target);
  8117. // don't want to click twice
  8118. if (isLabelClick) return;
  8119. // click link (will then in turn activate file input)
  8120. root.ref.label.click();
  8121. };
  8122. // attach events
  8123. label.addEventListener('keydown', root.ref.handleKeyDown);
  8124. root.element.addEventListener('click', root.ref.handleClick);
  8125. // update
  8126. updateLabelValue(label, props.caption);
  8127. // add!
  8128. root.appendChild(label);
  8129. root.ref.label = label;
  8130. };
  8131. var updateLabelValue = function updateLabelValue(label, value) {
  8132. label.innerHTML = value;
  8133. var clickable = label.querySelector('.filepond--label-action');
  8134. if (clickable) {
  8135. attr(clickable, 'tabindex', '0');
  8136. }
  8137. return value;
  8138. };
  8139. var dropLabel = createView({
  8140. name: 'drop-label',
  8141. ignoreRect: true,
  8142. create: create$b,
  8143. destroy: function destroy(_ref2) {
  8144. var root = _ref2.root;
  8145. root.ref.label.addEventListener('keydown', root.ref.handleKeyDown);
  8146. root.element.removeEventListener('click', root.ref.handleClick);
  8147. },
  8148. write: createRoute({
  8149. DID_SET_LABEL_IDLE: function DID_SET_LABEL_IDLE(_ref3) {
  8150. var root = _ref3.root,
  8151. action = _ref3.action;
  8152. updateLabelValue(root.ref.label, action.value);
  8153. },
  8154. }),
  8155. mixins: {
  8156. styles: ['opacity', 'translateX', 'translateY'],
  8157. animations: {
  8158. opacity: { type: 'tween', duration: 150 },
  8159. translateX: 'spring',
  8160. translateY: 'spring',
  8161. },
  8162. },
  8163. });
  8164. var blob = createView({
  8165. name: 'drip-blob',
  8166. ignoreRect: true,
  8167. mixins: {
  8168. styles: ['translateX', 'translateY', 'scaleX', 'scaleY', 'opacity'],
  8169. animations: {
  8170. scaleX: 'spring',
  8171. scaleY: 'spring',
  8172. translateX: 'spring',
  8173. translateY: 'spring',
  8174. opacity: { type: 'tween', duration: 250 },
  8175. },
  8176. },
  8177. });
  8178. var addBlob = function addBlob(_ref) {
  8179. var root = _ref.root;
  8180. var centerX = root.rect.element.width * 0.5;
  8181. var centerY = root.rect.element.height * 0.5;
  8182. root.ref.blob = root.appendChildView(
  8183. root.createChildView(blob, {
  8184. opacity: 0,
  8185. scaleX: 2.5,
  8186. scaleY: 2.5,
  8187. translateX: centerX,
  8188. translateY: centerY,
  8189. })
  8190. );
  8191. };
  8192. var moveBlob = function moveBlob(_ref2) {
  8193. var root = _ref2.root,
  8194. action = _ref2.action;
  8195. if (!root.ref.blob) {
  8196. addBlob({ root: root });
  8197. return;
  8198. }
  8199. root.ref.blob.translateX = action.position.scopeLeft;
  8200. root.ref.blob.translateY = action.position.scopeTop;
  8201. root.ref.blob.scaleX = 1;
  8202. root.ref.blob.scaleY = 1;
  8203. root.ref.blob.opacity = 1;
  8204. };
  8205. var hideBlob = function hideBlob(_ref3) {
  8206. var root = _ref3.root;
  8207. if (!root.ref.blob) {
  8208. return;
  8209. }
  8210. root.ref.blob.opacity = 0;
  8211. };
  8212. var explodeBlob = function explodeBlob(_ref4) {
  8213. var root = _ref4.root;
  8214. if (!root.ref.blob) {
  8215. return;
  8216. }
  8217. root.ref.blob.scaleX = 2.5;
  8218. root.ref.blob.scaleY = 2.5;
  8219. root.ref.blob.opacity = 0;
  8220. };
  8221. var write$7 = function write(_ref5) {
  8222. var root = _ref5.root,
  8223. props = _ref5.props,
  8224. actions = _ref5.actions;
  8225. route$4({ root: root, props: props, actions: actions });
  8226. var blob = root.ref.blob;
  8227. if (actions.length === 0 && blob && blob.opacity === 0) {
  8228. root.removeChildView(blob);
  8229. root.ref.blob = null;
  8230. }
  8231. };
  8232. var route$4 = createRoute({
  8233. DID_DRAG: moveBlob,
  8234. DID_DROP: explodeBlob,
  8235. DID_END_DRAG: hideBlob,
  8236. });
  8237. var drip = createView({
  8238. ignoreRect: true,
  8239. ignoreRectUpdate: true,
  8240. name: 'drip',
  8241. write: write$7,
  8242. });
  8243. var setInputFiles = function setInputFiles(element, files) {
  8244. try {
  8245. // Create a DataTransfer instance and add a newly created file
  8246. var dataTransfer = new DataTransfer();
  8247. files.forEach(function(file) {
  8248. if (file instanceof File) {
  8249. dataTransfer.items.add(file);
  8250. } else {
  8251. dataTransfer.items.add(
  8252. new File([file], file.name, {
  8253. type: file.type,
  8254. })
  8255. );
  8256. }
  8257. });
  8258. // Assign the DataTransfer files list to the file input
  8259. element.files = dataTransfer.files;
  8260. } catch (err) {
  8261. return false;
  8262. }
  8263. return true;
  8264. };
  8265. var create$c = function create(_ref) {
  8266. var root = _ref.root;
  8267. return (root.ref.fields = {});
  8268. };
  8269. var getField = function getField(root, id) {
  8270. return root.ref.fields[id];
  8271. };
  8272. var syncFieldPositionsWithItems = function syncFieldPositionsWithItems(root) {
  8273. root.query('GET_ACTIVE_ITEMS').forEach(function(item) {
  8274. if (!root.ref.fields[item.id]) return;
  8275. root.element.appendChild(root.ref.fields[item.id]);
  8276. });
  8277. };
  8278. var didReorderItems = function didReorderItems(_ref2) {
  8279. var root = _ref2.root;
  8280. return syncFieldPositionsWithItems(root);
  8281. };
  8282. var didAddItem = function didAddItem(_ref3) {
  8283. var root = _ref3.root,
  8284. action = _ref3.action;
  8285. var fileItem = root.query('GET_ITEM', action.id);
  8286. var isLocalFile = fileItem.origin === FileOrigin.LOCAL;
  8287. var shouldUseFileInput = !isLocalFile && root.query('SHOULD_UPDATE_FILE_INPUT');
  8288. var dataContainer = createElement$1('input');
  8289. dataContainer.type = shouldUseFileInput ? 'file' : 'hidden';
  8290. dataContainer.name = root.query('GET_NAME');
  8291. dataContainer.disabled = root.query('GET_DISABLED');
  8292. root.ref.fields[action.id] = dataContainer;
  8293. syncFieldPositionsWithItems(root);
  8294. };
  8295. var didLoadItem$1 = function didLoadItem(_ref4) {
  8296. var root = _ref4.root,
  8297. action = _ref4.action;
  8298. var field = getField(root, action.id);
  8299. if (!field) return;
  8300. // store server ref in hidden input
  8301. if (action.serverFileReference !== null) field.value = action.serverFileReference;
  8302. // store file item in file input
  8303. if (!root.query('SHOULD_UPDATE_FILE_INPUT')) return;
  8304. var fileItem = root.query('GET_ITEM', action.id);
  8305. setInputFiles(field, [fileItem.file]);
  8306. };
  8307. var didPrepareOutput = function didPrepareOutput(_ref5) {
  8308. var root = _ref5.root,
  8309. action = _ref5.action;
  8310. // this timeout pushes the handler after 'load'
  8311. if (!root.query('SHOULD_UPDATE_FILE_INPUT')) return;
  8312. setTimeout(function() {
  8313. var field = getField(root, action.id);
  8314. if (!field) return;
  8315. setInputFiles(field, [action.file]);
  8316. }, 0);
  8317. };
  8318. var didSetDisabled = function didSetDisabled(_ref6) {
  8319. var root = _ref6.root;
  8320. root.element.disabled = root.query('GET_DISABLED');
  8321. };
  8322. var didRemoveItem = function didRemoveItem(_ref7) {
  8323. var root = _ref7.root,
  8324. action = _ref7.action;
  8325. var field = getField(root, action.id);
  8326. if (!field) return;
  8327. if (field.parentNode) field.parentNode.removeChild(field);
  8328. delete root.ref.fields[action.id];
  8329. };
  8330. // only runs for server files (so doesn't deal with file input)
  8331. var didDefineValue = function didDefineValue(_ref8) {
  8332. var root = _ref8.root,
  8333. action = _ref8.action;
  8334. var field = getField(root, action.id);
  8335. if (!field) return;
  8336. if (action.value === null) {
  8337. // clear field value
  8338. field.removeAttribute('value');
  8339. } else {
  8340. // set field value
  8341. field.value = action.value;
  8342. }
  8343. syncFieldPositionsWithItems(root);
  8344. };
  8345. var write$8 = createRoute({
  8346. DID_SET_DISABLED: didSetDisabled,
  8347. DID_ADD_ITEM: didAddItem,
  8348. DID_LOAD_ITEM: didLoadItem$1,
  8349. DID_REMOVE_ITEM: didRemoveItem,
  8350. DID_DEFINE_VALUE: didDefineValue,
  8351. DID_PREPARE_OUTPUT: didPrepareOutput,
  8352. DID_REORDER_ITEMS: didReorderItems,
  8353. DID_SORT_ITEMS: didReorderItems,
  8354. });
  8355. var data = createView({
  8356. tag: 'fieldset',
  8357. name: 'data',
  8358. create: create$c,
  8359. write: write$8,
  8360. ignoreRect: true,
  8361. });
  8362. var getRootNode = function getRootNode(element) {
  8363. return 'getRootNode' in element ? element.getRootNode() : document;
  8364. };
  8365. var images = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg', 'tiff'];
  8366. var text$1 = ['css', 'csv', 'html', 'txt'];
  8367. var map = {
  8368. zip: 'zip|compressed',
  8369. epub: 'application/epub+zip',
  8370. };
  8371. var guesstimateMimeType = function guesstimateMimeType() {
  8372. var extension = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  8373. extension = extension.toLowerCase();
  8374. if (images.includes(extension)) {
  8375. return (
  8376. 'image/' +
  8377. (extension === 'jpg' ? 'jpeg' : extension === 'svg' ? 'svg+xml' : extension)
  8378. );
  8379. }
  8380. if (text$1.includes(extension)) {
  8381. return 'text/' + extension;
  8382. }
  8383. return map[extension] || '';
  8384. };
  8385. var requestDataTransferItems = function requestDataTransferItems(dataTransfer) {
  8386. return new Promise(function(resolve, reject) {
  8387. // try to get links from transfer, if found we'll exit immediately (unless a file is in the dataTransfer as well, this is because Firefox could represent the file as a URL and a file object at the same time)
  8388. var links = getLinks(dataTransfer);
  8389. if (links.length && !hasFiles(dataTransfer)) {
  8390. return resolve(links);
  8391. }
  8392. // try to get files from the transfer
  8393. getFiles(dataTransfer).then(resolve);
  8394. });
  8395. };
  8396. /**
  8397. * Test if datatransfer has files
  8398. */
  8399. var hasFiles = function hasFiles(dataTransfer) {
  8400. if (dataTransfer.files) return dataTransfer.files.length > 0;
  8401. return false;
  8402. };
  8403. /**
  8404. * Extracts files from a DataTransfer object
  8405. */
  8406. var getFiles = function getFiles(dataTransfer) {
  8407. return new Promise(function(resolve, reject) {
  8408. // get the transfer items as promises
  8409. var promisedFiles = (dataTransfer.items ? Array.from(dataTransfer.items) : [])
  8410. // only keep file system items (files and directories)
  8411. .filter(function(item) {
  8412. return isFileSystemItem(item);
  8413. })
  8414. // map each item to promise
  8415. .map(function(item) {
  8416. return getFilesFromItem(item);
  8417. });
  8418. // if is empty, see if we can extract some info from the files property as a fallback
  8419. if (!promisedFiles.length) {
  8420. // TODO: test for directories (should not be allowed)
  8421. // Use FileReader, problem is that the files property gets lost in the process
  8422. resolve(dataTransfer.files ? Array.from(dataTransfer.files) : []);
  8423. return;
  8424. }
  8425. // done!
  8426. Promise.all(promisedFiles)
  8427. .then(function(returnedFileGroups) {
  8428. // flatten groups
  8429. var files = [];
  8430. returnedFileGroups.forEach(function(group) {
  8431. files.push.apply(files, group);
  8432. });
  8433. // done (filter out empty files)!
  8434. resolve(
  8435. files
  8436. .filter(function(file) {
  8437. return file;
  8438. })
  8439. .map(function(file) {
  8440. if (!file._relativePath)
  8441. file._relativePath = file.webkitRelativePath;
  8442. return file;
  8443. })
  8444. );
  8445. })
  8446. .catch(console.error);
  8447. });
  8448. };
  8449. var isFileSystemItem = function isFileSystemItem(item) {
  8450. if (isEntry(item)) {
  8451. var entry = getAsEntry(item);
  8452. if (entry) {
  8453. return entry.isFile || entry.isDirectory;
  8454. }
  8455. }
  8456. return item.kind === 'file';
  8457. };
  8458. var getFilesFromItem = function getFilesFromItem(item) {
  8459. return new Promise(function(resolve, reject) {
  8460. if (isDirectoryEntry(item)) {
  8461. getFilesInDirectory(getAsEntry(item))
  8462. .then(resolve)
  8463. .catch(reject);
  8464. return;
  8465. }
  8466. resolve([item.getAsFile()]);
  8467. });
  8468. };
  8469. var getFilesInDirectory = function getFilesInDirectory(entry) {
  8470. return new Promise(function(resolve, reject) {
  8471. var files = [];
  8472. // the total entries to read
  8473. var dirCounter = 0;
  8474. var fileCounter = 0;
  8475. var resolveIfDone = function resolveIfDone() {
  8476. if (fileCounter === 0 && dirCounter === 0) {
  8477. resolve(files);
  8478. }
  8479. };
  8480. // the recursive function
  8481. var readEntries = function readEntries(dirEntry) {
  8482. dirCounter++;
  8483. var directoryReader = dirEntry.createReader();
  8484. // directories are returned in batches, we need to process all batches before we're done
  8485. var readBatch = function readBatch() {
  8486. directoryReader.readEntries(function(entries) {
  8487. if (entries.length === 0) {
  8488. dirCounter--;
  8489. resolveIfDone();
  8490. return;
  8491. }
  8492. entries.forEach(function(entry) {
  8493. // recursively read more directories
  8494. if (entry.isDirectory) {
  8495. readEntries(entry);
  8496. } else {
  8497. // read as file
  8498. fileCounter++;
  8499. entry.file(function(file) {
  8500. var correctedFile = correctMissingFileType(file);
  8501. if (entry.fullPath)
  8502. correctedFile._relativePath = entry.fullPath;
  8503. files.push(correctedFile);
  8504. fileCounter--;
  8505. resolveIfDone();
  8506. });
  8507. }
  8508. });
  8509. // try to get next batch of files
  8510. readBatch();
  8511. }, reject);
  8512. };
  8513. // read first batch of files
  8514. readBatch();
  8515. };
  8516. // go!
  8517. readEntries(entry);
  8518. });
  8519. };
  8520. var correctMissingFileType = function correctMissingFileType(file) {
  8521. if (file.type.length) return file;
  8522. var date = file.lastModifiedDate;
  8523. var name = file.name;
  8524. var type = guesstimateMimeType(getExtensionFromFilename(file.name));
  8525. if (!type.length) return file;
  8526. file = file.slice(0, file.size, type);
  8527. file.name = name;
  8528. file.lastModifiedDate = date;
  8529. return file;
  8530. };
  8531. var isDirectoryEntry = function isDirectoryEntry(item) {
  8532. return isEntry(item) && (getAsEntry(item) || {}).isDirectory;
  8533. };
  8534. var isEntry = function isEntry(item) {
  8535. return 'webkitGetAsEntry' in item;
  8536. };
  8537. var getAsEntry = function getAsEntry(item) {
  8538. return item.webkitGetAsEntry();
  8539. };
  8540. /**
  8541. * Extracts links from a DataTransfer object
  8542. */
  8543. var getLinks = function getLinks(dataTransfer) {
  8544. var links = [];
  8545. try {
  8546. // look in meta data property
  8547. links = getLinksFromTransferMetaData(dataTransfer);
  8548. if (links.length) {
  8549. return links;
  8550. }
  8551. links = getLinksFromTransferURLData(dataTransfer);
  8552. } catch (e) {
  8553. // nope nope nope (probably IE trouble)
  8554. }
  8555. return links;
  8556. };
  8557. var getLinksFromTransferURLData = function getLinksFromTransferURLData(dataTransfer) {
  8558. var data = dataTransfer.getData('url');
  8559. if (typeof data === 'string' && data.length) {
  8560. return [data];
  8561. }
  8562. return [];
  8563. };
  8564. var getLinksFromTransferMetaData = function getLinksFromTransferMetaData(dataTransfer) {
  8565. var data = dataTransfer.getData('text/html');
  8566. if (typeof data === 'string' && data.length) {
  8567. var matches = data.match(/src\s*=\s*"(.+?)"/);
  8568. if (matches) {
  8569. return [matches[1]];
  8570. }
  8571. }
  8572. return [];
  8573. };
  8574. var dragNDropObservers = [];
  8575. var eventPosition = function eventPosition(e) {
  8576. return {
  8577. pageLeft: e.pageX,
  8578. pageTop: e.pageY,
  8579. scopeLeft: e.offsetX || e.layerX,
  8580. scopeTop: e.offsetY || e.layerY,
  8581. };
  8582. };
  8583. var createDragNDropClient = function createDragNDropClient(
  8584. element,
  8585. scopeToObserve,
  8586. filterElement
  8587. ) {
  8588. var observer = getDragNDropObserver(scopeToObserve);
  8589. var client = {
  8590. element: element,
  8591. filterElement: filterElement,
  8592. state: null,
  8593. ondrop: function ondrop() {},
  8594. onenter: function onenter() {},
  8595. ondrag: function ondrag() {},
  8596. onexit: function onexit() {},
  8597. onload: function onload() {},
  8598. allowdrop: function allowdrop() {},
  8599. };
  8600. client.destroy = observer.addListener(client);
  8601. return client;
  8602. };
  8603. var getDragNDropObserver = function getDragNDropObserver(element) {
  8604. // see if already exists, if so, return
  8605. var observer = dragNDropObservers.find(function(item) {
  8606. return item.element === element;
  8607. });
  8608. if (observer) {
  8609. return observer;
  8610. }
  8611. // create new observer, does not yet exist for this element
  8612. var newObserver = createDragNDropObserver(element);
  8613. dragNDropObservers.push(newObserver);
  8614. return newObserver;
  8615. };
  8616. var createDragNDropObserver = function createDragNDropObserver(element) {
  8617. var clients = [];
  8618. var routes = {
  8619. dragenter: dragenter,
  8620. dragover: dragover,
  8621. dragleave: dragleave,
  8622. drop: drop,
  8623. };
  8624. var handlers = {};
  8625. forin(routes, function(event, createHandler) {
  8626. handlers[event] = createHandler(element, clients);
  8627. element.addEventListener(event, handlers[event], false);
  8628. });
  8629. var observer = {
  8630. element: element,
  8631. addListener: function addListener(client) {
  8632. // add as client
  8633. clients.push(client);
  8634. // return removeListener function
  8635. return function() {
  8636. // remove client
  8637. clients.splice(clients.indexOf(client), 1);
  8638. // if no more clients, clean up observer
  8639. if (clients.length === 0) {
  8640. dragNDropObservers.splice(dragNDropObservers.indexOf(observer), 1);
  8641. forin(routes, function(event) {
  8642. element.removeEventListener(event, handlers[event], false);
  8643. });
  8644. }
  8645. };
  8646. },
  8647. };
  8648. return observer;
  8649. };
  8650. var elementFromPoint = function elementFromPoint(root, point) {
  8651. if (!('elementFromPoint' in root)) {
  8652. root = document;
  8653. }
  8654. return root.elementFromPoint(point.x, point.y);
  8655. };
  8656. var isEventTarget = function isEventTarget(e, target) {
  8657. // get root
  8658. var root = getRootNode(target);
  8659. // get element at position
  8660. // if root is not actual shadow DOM and does not have elementFromPoint method, use the one on document
  8661. var elementAtPosition = elementFromPoint(root, {
  8662. x: e.pageX - window.pageXOffset,
  8663. y: e.pageY - window.pageYOffset,
  8664. });
  8665. // test if target is the element or if one of its children is
  8666. return elementAtPosition === target || target.contains(elementAtPosition);
  8667. };
  8668. var initialTarget = null;
  8669. var setDropEffect = function setDropEffect(dataTransfer, effect) {
  8670. // is in try catch as IE11 will throw error if not
  8671. try {
  8672. dataTransfer.dropEffect = effect;
  8673. } catch (e) {}
  8674. };
  8675. var dragenter = function dragenter(root, clients) {
  8676. return function(e) {
  8677. e.preventDefault();
  8678. initialTarget = e.target;
  8679. clients.forEach(function(client) {
  8680. var element = client.element,
  8681. onenter = client.onenter;
  8682. if (isEventTarget(e, element)) {
  8683. client.state = 'enter';
  8684. // fire enter event
  8685. onenter(eventPosition(e));
  8686. }
  8687. });
  8688. };
  8689. };
  8690. var dragover = function dragover(root, clients) {
  8691. return function(e) {
  8692. e.preventDefault();
  8693. var dataTransfer = e.dataTransfer;
  8694. requestDataTransferItems(dataTransfer).then(function(items) {
  8695. var overDropTarget = false;
  8696. clients.some(function(client) {
  8697. var filterElement = client.filterElement,
  8698. element = client.element,
  8699. onenter = client.onenter,
  8700. onexit = client.onexit,
  8701. ondrag = client.ondrag,
  8702. allowdrop = client.allowdrop;
  8703. // by default we can drop
  8704. setDropEffect(dataTransfer, 'copy');
  8705. // allow transfer of these items
  8706. var allowsTransfer = allowdrop(items);
  8707. // only used when can be dropped on page
  8708. if (!allowsTransfer) {
  8709. setDropEffect(dataTransfer, 'none');
  8710. return;
  8711. }
  8712. // targetting this client
  8713. if (isEventTarget(e, element)) {
  8714. overDropTarget = true;
  8715. // had no previous state, means we are entering this client
  8716. if (client.state === null) {
  8717. client.state = 'enter';
  8718. onenter(eventPosition(e));
  8719. return;
  8720. }
  8721. // now over element (no matter if it allows the drop or not)
  8722. client.state = 'over';
  8723. // needs to allow transfer
  8724. if (filterElement && !allowsTransfer) {
  8725. setDropEffect(dataTransfer, 'none');
  8726. return;
  8727. }
  8728. // dragging
  8729. ondrag(eventPosition(e));
  8730. } else {
  8731. // should be over an element to drop
  8732. if (filterElement && !overDropTarget) {
  8733. setDropEffect(dataTransfer, 'none');
  8734. }
  8735. // might have just left this client?
  8736. if (client.state) {
  8737. client.state = null;
  8738. onexit(eventPosition(e));
  8739. }
  8740. }
  8741. });
  8742. });
  8743. };
  8744. };
  8745. var drop = function drop(root, clients) {
  8746. return function(e) {
  8747. e.preventDefault();
  8748. var dataTransfer = e.dataTransfer;
  8749. requestDataTransferItems(dataTransfer).then(function(items) {
  8750. clients.forEach(function(client) {
  8751. var filterElement = client.filterElement,
  8752. element = client.element,
  8753. ondrop = client.ondrop,
  8754. onexit = client.onexit,
  8755. allowdrop = client.allowdrop;
  8756. client.state = null;
  8757. // if we're filtering on element we need to be over the element to drop
  8758. if (filterElement && !isEventTarget(e, element)) return;
  8759. // no transfer for this client
  8760. if (!allowdrop(items)) return onexit(eventPosition(e));
  8761. // we can drop these items on this client
  8762. ondrop(eventPosition(e), items);
  8763. });
  8764. });
  8765. };
  8766. };
  8767. var dragleave = function dragleave(root, clients) {
  8768. return function(e) {
  8769. if (initialTarget !== e.target) {
  8770. return;
  8771. }
  8772. clients.forEach(function(client) {
  8773. var onexit = client.onexit;
  8774. client.state = null;
  8775. onexit(eventPosition(e));
  8776. });
  8777. };
  8778. };
  8779. var createHopper = function createHopper(scope, validateItems, options) {
  8780. // is now hopper scope
  8781. scope.classList.add('filepond--hopper');
  8782. // shortcuts
  8783. var catchesDropsOnPage = options.catchesDropsOnPage,
  8784. requiresDropOnElement = options.requiresDropOnElement,
  8785. _options$filterItems = options.filterItems,
  8786. filterItems =
  8787. _options$filterItems === void 0
  8788. ? function(items) {
  8789. return items;
  8790. }
  8791. : _options$filterItems;
  8792. // create a dnd client
  8793. var client = createDragNDropClient(
  8794. scope,
  8795. catchesDropsOnPage ? document.documentElement : scope,
  8796. requiresDropOnElement
  8797. );
  8798. // current client state
  8799. var lastState = '';
  8800. var currentState = '';
  8801. // determines if a file may be dropped
  8802. client.allowdrop = function(items) {
  8803. // TODO: if we can, throw error to indicate the items cannot by dropped
  8804. return validateItems(filterItems(items));
  8805. };
  8806. client.ondrop = function(position, items) {
  8807. var filteredItems = filterItems(items);
  8808. if (!validateItems(filteredItems)) {
  8809. api.ondragend(position);
  8810. return;
  8811. }
  8812. currentState = 'drag-drop';
  8813. api.onload(filteredItems, position);
  8814. };
  8815. client.ondrag = function(position) {
  8816. api.ondrag(position);
  8817. };
  8818. client.onenter = function(position) {
  8819. currentState = 'drag-over';
  8820. api.ondragstart(position);
  8821. };
  8822. client.onexit = function(position) {
  8823. currentState = 'drag-exit';
  8824. api.ondragend(position);
  8825. };
  8826. var api = {
  8827. updateHopperState: function updateHopperState() {
  8828. if (lastState !== currentState) {
  8829. scope.dataset.hopperState = currentState;
  8830. lastState = currentState;
  8831. }
  8832. },
  8833. onload: function onload() {},
  8834. ondragstart: function ondragstart() {},
  8835. ondrag: function ondrag() {},
  8836. ondragend: function ondragend() {},
  8837. destroy: function destroy() {
  8838. // destroy client
  8839. client.destroy();
  8840. },
  8841. };
  8842. return api;
  8843. };
  8844. var listening = false;
  8845. var listeners$1 = [];
  8846. var handlePaste = function handlePaste(e) {
  8847. // if is pasting in input or textarea and the target is outside of a filepond scope, ignore
  8848. var activeEl = document.activeElement;
  8849. if (activeEl && /textarea|input/i.test(activeEl.nodeName)) {
  8850. // test textarea or input is contained in filepond root
  8851. var inScope = false;
  8852. var element = activeEl;
  8853. while (element !== document.body) {
  8854. if (element.classList.contains('filepond--root')) {
  8855. inScope = true;
  8856. break;
  8857. }
  8858. element = element.parentNode;
  8859. }
  8860. if (!inScope) return;
  8861. }
  8862. requestDataTransferItems(e.clipboardData).then(function(files) {
  8863. // no files received
  8864. if (!files.length) {
  8865. return;
  8866. }
  8867. // notify listeners of received files
  8868. listeners$1.forEach(function(listener) {
  8869. return listener(files);
  8870. });
  8871. });
  8872. };
  8873. var listen = function listen(cb) {
  8874. // can't add twice
  8875. if (listeners$1.includes(cb)) {
  8876. return;
  8877. }
  8878. // add initial listener
  8879. listeners$1.push(cb);
  8880. // setup paste listener for entire page
  8881. if (listening) {
  8882. return;
  8883. }
  8884. listening = true;
  8885. document.addEventListener('paste', handlePaste);
  8886. };
  8887. var unlisten = function unlisten(listener) {
  8888. arrayRemove(listeners$1, listeners$1.indexOf(listener));
  8889. // clean up
  8890. if (listeners$1.length === 0) {
  8891. document.removeEventListener('paste', handlePaste);
  8892. listening = false;
  8893. }
  8894. };
  8895. var createPaster = function createPaster() {
  8896. var cb = function cb(files) {
  8897. api.onload(files);
  8898. };
  8899. var api = {
  8900. destroy: function destroy() {
  8901. unlisten(cb);
  8902. },
  8903. onload: function onload() {},
  8904. };
  8905. listen(cb);
  8906. return api;
  8907. };
  8908. /**
  8909. * Creates the file view
  8910. */
  8911. var create$d = function create(_ref) {
  8912. var root = _ref.root,
  8913. props = _ref.props;
  8914. root.element.id = 'filepond--assistant-' + props.id;
  8915. attr(root.element, 'role', 'status');
  8916. attr(root.element, 'aria-live', 'polite');
  8917. attr(root.element, 'aria-relevant', 'additions');
  8918. };
  8919. var addFilesNotificationTimeout = null;
  8920. var notificationClearTimeout = null;
  8921. var filenames = [];
  8922. var assist = function assist(root, message) {
  8923. root.element.textContent = message;
  8924. };
  8925. var clear$1 = function clear(root) {
  8926. root.element.textContent = '';
  8927. };
  8928. var listModified = function listModified(root, filename, label) {
  8929. var total = root.query('GET_TOTAL_ITEMS');
  8930. assist(
  8931. root,
  8932. label +
  8933. ' ' +
  8934. filename +
  8935. ', ' +
  8936. total +
  8937. ' ' +
  8938. (total === 1
  8939. ? root.query('GET_LABEL_FILE_COUNT_SINGULAR')
  8940. : root.query('GET_LABEL_FILE_COUNT_PLURAL'))
  8941. );
  8942. // clear group after set amount of time so the status is not read twice
  8943. clearTimeout(notificationClearTimeout);
  8944. notificationClearTimeout = setTimeout(function() {
  8945. clear$1(root);
  8946. }, 1500);
  8947. };
  8948. var isUsingFilePond = function isUsingFilePond(root) {
  8949. return root.element.parentNode.contains(document.activeElement);
  8950. };
  8951. var itemAdded = function itemAdded(_ref2) {
  8952. var root = _ref2.root,
  8953. action = _ref2.action;
  8954. if (!isUsingFilePond(root)) {
  8955. return;
  8956. }
  8957. root.element.textContent = '';
  8958. var item = root.query('GET_ITEM', action.id);
  8959. filenames.push(item.filename);
  8960. clearTimeout(addFilesNotificationTimeout);
  8961. addFilesNotificationTimeout = setTimeout(function() {
  8962. listModified(root, filenames.join(', '), root.query('GET_LABEL_FILE_ADDED'));
  8963. filenames.length = 0;
  8964. }, 750);
  8965. };
  8966. var itemRemoved = function itemRemoved(_ref3) {
  8967. var root = _ref3.root,
  8968. action = _ref3.action;
  8969. if (!isUsingFilePond(root)) {
  8970. return;
  8971. }
  8972. var item = action.item;
  8973. listModified(root, item.filename, root.query('GET_LABEL_FILE_REMOVED'));
  8974. };
  8975. var itemProcessed = function itemProcessed(_ref4) {
  8976. var root = _ref4.root,
  8977. action = _ref4.action;
  8978. // will also notify the user when FilePond is not being used, as the user might be occupied with other activities while uploading a file
  8979. var item = root.query('GET_ITEM', action.id);
  8980. var filename = item.filename;
  8981. var label = root.query('GET_LABEL_FILE_PROCESSING_COMPLETE');
  8982. assist(root, filename + ' ' + label);
  8983. };
  8984. var itemProcessedUndo = function itemProcessedUndo(_ref5) {
  8985. var root = _ref5.root,
  8986. action = _ref5.action;
  8987. var item = root.query('GET_ITEM', action.id);
  8988. var filename = item.filename;
  8989. var label = root.query('GET_LABEL_FILE_PROCESSING_ABORTED');
  8990. assist(root, filename + ' ' + label);
  8991. };
  8992. var itemError = function itemError(_ref6) {
  8993. var root = _ref6.root,
  8994. action = _ref6.action;
  8995. var item = root.query('GET_ITEM', action.id);
  8996. var filename = item.filename;
  8997. // will also notify the user when FilePond is not being used, as the user might be occupied with other activities while uploading a file
  8998. assist(root, action.status.main + ' ' + filename + ' ' + action.status.sub);
  8999. };
  9000. var assistant = createView({
  9001. create: create$d,
  9002. ignoreRect: true,
  9003. ignoreRectUpdate: true,
  9004. write: createRoute({
  9005. DID_LOAD_ITEM: itemAdded,
  9006. DID_REMOVE_ITEM: itemRemoved,
  9007. DID_COMPLETE_ITEM_PROCESSING: itemProcessed,
  9008. DID_ABORT_ITEM_PROCESSING: itemProcessedUndo,
  9009. DID_REVERT_ITEM_PROCESSING: itemProcessedUndo,
  9010. DID_THROW_ITEM_REMOVE_ERROR: itemError,
  9011. DID_THROW_ITEM_LOAD_ERROR: itemError,
  9012. DID_THROW_ITEM_INVALID: itemError,
  9013. DID_THROW_ITEM_PROCESSING_ERROR: itemError,
  9014. }),
  9015. tag: 'span',
  9016. name: 'assistant',
  9017. });
  9018. var toCamels = function toCamels(string) {
  9019. var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '-';
  9020. return string.replace(new RegExp(separator + '.', 'g'), function(sub) {
  9021. return sub.charAt(1).toUpperCase();
  9022. });
  9023. };
  9024. var debounce = function debounce(func) {
  9025. var interval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 16;
  9026. var immidiateOnly =
  9027. arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  9028. var last = Date.now();
  9029. var timeout = null;
  9030. return function() {
  9031. for (
  9032. var _len = arguments.length, args = new Array(_len), _key = 0;
  9033. _key < _len;
  9034. _key++
  9035. ) {
  9036. args[_key] = arguments[_key];
  9037. }
  9038. clearTimeout(timeout);
  9039. var dist = Date.now() - last;
  9040. var fn = function fn() {
  9041. last = Date.now();
  9042. func.apply(void 0, args);
  9043. };
  9044. if (dist < interval) {
  9045. // we need to delay by the difference between interval and dist
  9046. // for example: if distance is 10 ms and interval is 16 ms,
  9047. // we need to wait an additional 6ms before calling the function)
  9048. if (!immidiateOnly) {
  9049. timeout = setTimeout(fn, interval - dist);
  9050. }
  9051. } else {
  9052. // go!
  9053. fn();
  9054. }
  9055. };
  9056. };
  9057. var MAX_FILES_LIMIT = 1000000;
  9058. var prevent = function prevent(e) {
  9059. return e.preventDefault();
  9060. };
  9061. var create$e = function create(_ref) {
  9062. var root = _ref.root,
  9063. props = _ref.props;
  9064. // Add id
  9065. var id = root.query('GET_ID');
  9066. if (id) {
  9067. root.element.id = id;
  9068. }
  9069. // Add className
  9070. var className = root.query('GET_CLASS_NAME');
  9071. if (className) {
  9072. className
  9073. .split(' ')
  9074. .filter(function(name) {
  9075. return name.length;
  9076. })
  9077. .forEach(function(name) {
  9078. root.element.classList.add(name);
  9079. });
  9080. }
  9081. // Field label
  9082. root.ref.label = root.appendChildView(
  9083. root.createChildView(
  9084. dropLabel,
  9085. Object.assign({}, props, {
  9086. translateY: null,
  9087. caption: root.query('GET_LABEL_IDLE'),
  9088. })
  9089. )
  9090. );
  9091. // List of items
  9092. root.ref.list = root.appendChildView(
  9093. root.createChildView(listScroller, { translateY: null })
  9094. );
  9095. // Background panel
  9096. root.ref.panel = root.appendChildView(root.createChildView(panel, { name: 'panel-root' }));
  9097. // Assistant notifies assistive tech when content changes
  9098. root.ref.assistant = root.appendChildView(
  9099. root.createChildView(assistant, Object.assign({}, props))
  9100. );
  9101. // Data
  9102. root.ref.data = root.appendChildView(root.createChildView(data, Object.assign({}, props)));
  9103. // Measure (tests if fixed height was set)
  9104. // DOCTYPE needs to be set for this to work
  9105. root.ref.measure = createElement$1('div');
  9106. root.ref.measure.style.height = '100%';
  9107. root.element.appendChild(root.ref.measure);
  9108. // information on the root height or fixed height status
  9109. root.ref.bounds = null;
  9110. // apply initial style properties
  9111. root.query('GET_STYLES')
  9112. .filter(function(style) {
  9113. return !isEmpty(style.value);
  9114. })
  9115. .map(function(_ref2) {
  9116. var name = _ref2.name,
  9117. value = _ref2.value;
  9118. root.element.dataset[name] = value;
  9119. });
  9120. // determine if width changed
  9121. root.ref.widthPrevious = null;
  9122. root.ref.widthUpdated = debounce(function() {
  9123. root.ref.updateHistory = [];
  9124. root.dispatch('DID_RESIZE_ROOT');
  9125. }, 250);
  9126. // history of updates
  9127. root.ref.previousAspectRatio = null;
  9128. root.ref.updateHistory = [];
  9129. // prevent scrolling and zooming on iOS (only if supports pointer events, for then we can enable reorder)
  9130. var canHover = window.matchMedia('(pointer: fine) and (hover: hover)').matches;
  9131. var hasPointerEvents = 'PointerEvent' in window;
  9132. if (root.query('GET_ALLOW_REORDER') && hasPointerEvents && !canHover) {
  9133. root.element.addEventListener('touchmove', prevent, { passive: false });
  9134. root.element.addEventListener('gesturestart', prevent);
  9135. }
  9136. // add credits
  9137. var credits = root.query('GET_CREDITS');
  9138. var hasCredits = credits.length === 2;
  9139. if (hasCredits) {
  9140. var frag = document.createElement('a');
  9141. frag.className = 'filepond--credits';
  9142. frag.setAttribute('aria-hidden', 'true');
  9143. frag.href = credits[0];
  9144. frag.tabindex = -1;
  9145. frag.target = '_blank';
  9146. frag.rel = 'noopener noreferrer';
  9147. frag.textContent = credits[1];
  9148. root.element.appendChild(frag);
  9149. root.ref.credits = frag;
  9150. }
  9151. };
  9152. var write$9 = function write(_ref3) {
  9153. var root = _ref3.root,
  9154. props = _ref3.props,
  9155. actions = _ref3.actions;
  9156. // route actions
  9157. route$5({ root: root, props: props, actions: actions });
  9158. // apply style properties
  9159. actions
  9160. .filter(function(action) {
  9161. return /^DID_SET_STYLE_/.test(action.type);
  9162. })
  9163. .filter(function(action) {
  9164. return !isEmpty(action.data.value);
  9165. })
  9166. .map(function(_ref4) {
  9167. var type = _ref4.type,
  9168. data = _ref4.data;
  9169. var name = toCamels(type.substring(8).toLowerCase(), '_');
  9170. root.element.dataset[name] = data.value;
  9171. root.invalidateLayout();
  9172. });
  9173. if (root.rect.element.hidden) return;
  9174. if (root.rect.element.width !== root.ref.widthPrevious) {
  9175. root.ref.widthPrevious = root.rect.element.width;
  9176. root.ref.widthUpdated();
  9177. }
  9178. // get box bounds, we do this only once
  9179. var bounds = root.ref.bounds;
  9180. if (!bounds) {
  9181. bounds = root.ref.bounds = calculateRootBoundingBoxHeight(root);
  9182. // destroy measure element
  9183. root.element.removeChild(root.ref.measure);
  9184. root.ref.measure = null;
  9185. }
  9186. // get quick references to various high level parts of the upload tool
  9187. var _root$ref = root.ref,
  9188. hopper = _root$ref.hopper,
  9189. label = _root$ref.label,
  9190. list = _root$ref.list,
  9191. panel = _root$ref.panel;
  9192. // sets correct state to hopper scope
  9193. if (hopper) {
  9194. hopper.updateHopperState();
  9195. }
  9196. // bool to indicate if we're full or not
  9197. var aspectRatio = root.query('GET_PANEL_ASPECT_RATIO');
  9198. var isMultiItem = root.query('GET_ALLOW_MULTIPLE');
  9199. var totalItems = root.query('GET_TOTAL_ITEMS');
  9200. var maxItems = isMultiItem ? root.query('GET_MAX_FILES') || MAX_FILES_LIMIT : 1;
  9201. var atMaxCapacity = totalItems === maxItems;
  9202. // action used to add item
  9203. var addAction = actions.find(function(action) {
  9204. return action.type === 'DID_ADD_ITEM';
  9205. });
  9206. // if reached max capacity and we've just reached it
  9207. if (atMaxCapacity && addAction) {
  9208. // get interaction type
  9209. var interactionMethod = addAction.data.interactionMethod;
  9210. // hide label
  9211. label.opacity = 0;
  9212. if (isMultiItem) {
  9213. label.translateY = -40;
  9214. } else {
  9215. if (interactionMethod === InteractionMethod.API) {
  9216. label.translateX = 40;
  9217. } else if (interactionMethod === InteractionMethod.BROWSE) {
  9218. label.translateY = 40;
  9219. } else {
  9220. label.translateY = 30;
  9221. }
  9222. }
  9223. } else if (!atMaxCapacity) {
  9224. label.opacity = 1;
  9225. label.translateX = 0;
  9226. label.translateY = 0;
  9227. }
  9228. var listItemMargin = calculateListItemMargin(root);
  9229. var listHeight = calculateListHeight(root);
  9230. var labelHeight = label.rect.element.height;
  9231. var currentLabelHeight = !isMultiItem || atMaxCapacity ? 0 : labelHeight;
  9232. var listMarginTop = atMaxCapacity ? list.rect.element.marginTop : 0;
  9233. var listMarginBottom = totalItems === 0 ? 0 : list.rect.element.marginBottom;
  9234. var visualHeight =
  9235. currentLabelHeight + listMarginTop + listHeight.visual + listMarginBottom;
  9236. var boundsHeight =
  9237. currentLabelHeight + listMarginTop + listHeight.bounds + listMarginBottom;
  9238. // link list to label bottom position
  9239. list.translateY =
  9240. Math.max(0, currentLabelHeight - list.rect.element.marginTop) - listItemMargin.top;
  9241. if (aspectRatio) {
  9242. // fixed aspect ratio
  9243. // calculate height based on width
  9244. var width = root.rect.element.width;
  9245. var height = width * aspectRatio;
  9246. // clear history if aspect ratio has changed
  9247. if (aspectRatio !== root.ref.previousAspectRatio) {
  9248. root.ref.previousAspectRatio = aspectRatio;
  9249. root.ref.updateHistory = [];
  9250. }
  9251. // remember this width
  9252. var history = root.ref.updateHistory;
  9253. history.push(width);
  9254. var MAX_BOUNCES = 2;
  9255. if (history.length > MAX_BOUNCES * 2) {
  9256. var l = history.length;
  9257. var bottom = l - 10;
  9258. var bounces = 0;
  9259. for (var i = l; i >= bottom; i--) {
  9260. if (history[i] === history[i - 2]) {
  9261. bounces++;
  9262. }
  9263. if (bounces >= MAX_BOUNCES) {
  9264. // dont adjust height
  9265. return;
  9266. }
  9267. }
  9268. }
  9269. // fix height of panel so it adheres to aspect ratio
  9270. panel.scalable = false;
  9271. panel.height = height;
  9272. // available height for list
  9273. var listAvailableHeight =
  9274. // the height of the panel minus the label height
  9275. height -
  9276. currentLabelHeight -
  9277. // the room we leave open between the end of the list and the panel bottom
  9278. (listMarginBottom - listItemMargin.bottom) -
  9279. // if we're full we need to leave some room between the top of the panel and the list
  9280. (atMaxCapacity ? listMarginTop : 0);
  9281. if (listHeight.visual > listAvailableHeight) {
  9282. list.overflow = listAvailableHeight;
  9283. } else {
  9284. list.overflow = null;
  9285. }
  9286. // set container bounds (so pushes siblings downwards)
  9287. root.height = height;
  9288. } else if (bounds.fixedHeight) {
  9289. // fixed height
  9290. // fix height of panel
  9291. panel.scalable = false;
  9292. // available height for list
  9293. var _listAvailableHeight =
  9294. // the height of the panel minus the label height
  9295. bounds.fixedHeight -
  9296. currentLabelHeight -
  9297. // the room we leave open between the end of the list and the panel bottom
  9298. (listMarginBottom - listItemMargin.bottom) -
  9299. // if we're full we need to leave some room between the top of the panel and the list
  9300. (atMaxCapacity ? listMarginTop : 0);
  9301. // set list height
  9302. if (listHeight.visual > _listAvailableHeight) {
  9303. list.overflow = _listAvailableHeight;
  9304. } else {
  9305. list.overflow = null;
  9306. }
  9307. // no need to set container bounds as these are handles by CSS fixed height
  9308. } else if (bounds.cappedHeight) {
  9309. // max-height
  9310. // not a fixed height panel
  9311. var isCappedHeight = visualHeight >= bounds.cappedHeight;
  9312. var panelHeight = Math.min(bounds.cappedHeight, visualHeight);
  9313. panel.scalable = true;
  9314. panel.height = isCappedHeight
  9315. ? panelHeight
  9316. : panelHeight - listItemMargin.top - listItemMargin.bottom;
  9317. // available height for list
  9318. var _listAvailableHeight2 =
  9319. // the height of the panel minus the label height
  9320. panelHeight -
  9321. currentLabelHeight -
  9322. // the room we leave open between the end of the list and the panel bottom
  9323. (listMarginBottom - listItemMargin.bottom) -
  9324. // if we're full we need to leave some room between the top of the panel and the list
  9325. (atMaxCapacity ? listMarginTop : 0);
  9326. // set list height (if is overflowing)
  9327. if (visualHeight > bounds.cappedHeight && listHeight.visual > _listAvailableHeight2) {
  9328. list.overflow = _listAvailableHeight2;
  9329. } else {
  9330. list.overflow = null;
  9331. }
  9332. // set container bounds (so pushes siblings downwards)
  9333. root.height = Math.min(
  9334. bounds.cappedHeight,
  9335. boundsHeight - listItemMargin.top - listItemMargin.bottom
  9336. );
  9337. } else {
  9338. // flexible height
  9339. // not a fixed height panel
  9340. var itemMargin = totalItems > 0 ? listItemMargin.top + listItemMargin.bottom : 0;
  9341. panel.scalable = true;
  9342. panel.height = Math.max(labelHeight, visualHeight - itemMargin);
  9343. // set container bounds (so pushes siblings downwards)
  9344. root.height = Math.max(labelHeight, boundsHeight - itemMargin);
  9345. }
  9346. // move credits to bottom
  9347. if (root.ref.credits && panel.heightCurrent)
  9348. root.ref.credits.style.transform = 'translateY(' + panel.heightCurrent + 'px)';
  9349. };
  9350. var calculateListItemMargin = function calculateListItemMargin(root) {
  9351. var item = root.ref.list.childViews[0].childViews[0];
  9352. return item
  9353. ? {
  9354. top: item.rect.element.marginTop,
  9355. bottom: item.rect.element.marginBottom,
  9356. }
  9357. : {
  9358. top: 0,
  9359. bottom: 0,
  9360. };
  9361. };
  9362. var calculateListHeight = function calculateListHeight(root) {
  9363. var visual = 0;
  9364. var bounds = 0;
  9365. // get file list reference
  9366. var scrollList = root.ref.list;
  9367. var itemList = scrollList.childViews[0];
  9368. var visibleChildren = itemList.childViews.filter(function(child) {
  9369. return child.rect.element.height;
  9370. });
  9371. var children = root
  9372. .query('GET_ACTIVE_ITEMS')
  9373. .map(function(item) {
  9374. return visibleChildren.find(function(child) {
  9375. return child.id === item.id;
  9376. });
  9377. })
  9378. .filter(function(item) {
  9379. return item;
  9380. });
  9381. // no children, done!
  9382. if (children.length === 0) return { visual: visual, bounds: bounds };
  9383. var horizontalSpace = itemList.rect.element.width;
  9384. var dragIndex = getItemIndexByPosition(itemList, children, scrollList.dragCoordinates);
  9385. var childRect = children[0].rect.element;
  9386. var itemVerticalMargin = childRect.marginTop + childRect.marginBottom;
  9387. var itemHorizontalMargin = childRect.marginLeft + childRect.marginRight;
  9388. var itemWidth = childRect.width + itemHorizontalMargin;
  9389. var itemHeight = childRect.height + itemVerticalMargin;
  9390. var newItem = typeof dragIndex !== 'undefined' && dragIndex >= 0 ? 1 : 0;
  9391. var removedItem = children.find(function(child) {
  9392. return child.markedForRemoval && child.opacity < 0.45;
  9393. })
  9394. ? -1
  9395. : 0;
  9396. var verticalItemCount = children.length + newItem + removedItem;
  9397. var itemsPerRow = getItemsPerRow(horizontalSpace, itemWidth);
  9398. // stack
  9399. if (itemsPerRow === 1) {
  9400. children.forEach(function(item) {
  9401. var height = item.rect.element.height + itemVerticalMargin;
  9402. bounds += height;
  9403. visual += height * item.opacity;
  9404. });
  9405. }
  9406. // grid
  9407. else {
  9408. bounds = Math.ceil(verticalItemCount / itemsPerRow) * itemHeight;
  9409. visual = bounds;
  9410. }
  9411. return { visual: visual, bounds: bounds };
  9412. };
  9413. var calculateRootBoundingBoxHeight = function calculateRootBoundingBoxHeight(root) {
  9414. var height = root.ref.measureHeight || null;
  9415. var cappedHeight = parseInt(root.style.maxHeight, 10) || null;
  9416. var fixedHeight = height === 0 ? null : height;
  9417. return {
  9418. cappedHeight: cappedHeight,
  9419. fixedHeight: fixedHeight,
  9420. };
  9421. };
  9422. var exceedsMaxFiles = function exceedsMaxFiles(root, items) {
  9423. var allowReplace = root.query('GET_ALLOW_REPLACE');
  9424. var allowMultiple = root.query('GET_ALLOW_MULTIPLE');
  9425. var totalItems = root.query('GET_TOTAL_ITEMS');
  9426. var maxItems = root.query('GET_MAX_FILES');
  9427. // total amount of items being dragged
  9428. var totalBrowseItems = items.length;
  9429. // if does not allow multiple items and dragging more than one item
  9430. if (!allowMultiple && totalBrowseItems > 1) {
  9431. root.dispatch('DID_THROW_MAX_FILES', {
  9432. source: items,
  9433. error: createResponse('warning', 0, 'Max files'),
  9434. });
  9435. return true;
  9436. }
  9437. // limit max items to one if not allowed to drop multiple items
  9438. maxItems = allowMultiple ? maxItems : 1;
  9439. if (!allowMultiple && allowReplace) {
  9440. // There is only one item, so there is room to replace or add an item
  9441. return false;
  9442. }
  9443. // no more room?
  9444. var hasMaxItems = isInt(maxItems);
  9445. if (hasMaxItems && totalItems + totalBrowseItems > maxItems) {
  9446. root.dispatch('DID_THROW_MAX_FILES', {
  9447. source: items,
  9448. error: createResponse('warning', 0, 'Max files'),
  9449. });
  9450. return true;
  9451. }
  9452. return false;
  9453. };
  9454. var getDragIndex = function getDragIndex(list, children, position) {
  9455. var itemList = list.childViews[0];
  9456. return getItemIndexByPosition(itemList, children, {
  9457. left: position.scopeLeft - itemList.rect.element.left,
  9458. top:
  9459. position.scopeTop -
  9460. (list.rect.outer.top + list.rect.element.marginTop + list.rect.element.scrollTop),
  9461. });
  9462. };
  9463. /**
  9464. * Enable or disable file drop functionality
  9465. */
  9466. var toggleDrop = function toggleDrop(root) {
  9467. var isAllowed = root.query('GET_ALLOW_DROP');
  9468. var isDisabled = root.query('GET_DISABLED');
  9469. var enabled = isAllowed && !isDisabled;
  9470. if (enabled && !root.ref.hopper) {
  9471. var hopper = createHopper(
  9472. root.element,
  9473. function(items) {
  9474. // allow quick validation of dropped items
  9475. var beforeDropFile =
  9476. root.query('GET_BEFORE_DROP_FILE') ||
  9477. function() {
  9478. return true;
  9479. };
  9480. // all items should be validated by all filters as valid
  9481. var dropValidation = root.query('GET_DROP_VALIDATION');
  9482. return dropValidation
  9483. ? items.every(function(item) {
  9484. return (
  9485. applyFilters('ALLOW_HOPPER_ITEM', item, {
  9486. query: root.query,
  9487. }).every(function(result) {
  9488. return result === true;
  9489. }) && beforeDropFile(item)
  9490. );
  9491. })
  9492. : true;
  9493. },
  9494. {
  9495. filterItems: function filterItems(items) {
  9496. var ignoredFiles = root.query('GET_IGNORED_FILES');
  9497. return items.filter(function(item) {
  9498. if (isFile(item)) {
  9499. return !ignoredFiles.includes(item.name.toLowerCase());
  9500. }
  9501. return true;
  9502. });
  9503. },
  9504. catchesDropsOnPage: root.query('GET_DROP_ON_PAGE'),
  9505. requiresDropOnElement: root.query('GET_DROP_ON_ELEMENT'),
  9506. }
  9507. );
  9508. hopper.onload = function(items, position) {
  9509. // get item children elements and sort based on list sort
  9510. var list = root.ref.list.childViews[0];
  9511. var visibleChildren = list.childViews.filter(function(child) {
  9512. return child.rect.element.height;
  9513. });
  9514. var children = root
  9515. .query('GET_ACTIVE_ITEMS')
  9516. .map(function(item) {
  9517. return visibleChildren.find(function(child) {
  9518. return child.id === item.id;
  9519. });
  9520. })
  9521. .filter(function(item) {
  9522. return item;
  9523. });
  9524. applyFilterChain('ADD_ITEMS', items, { dispatch: root.dispatch }).then(function(
  9525. queue
  9526. ) {
  9527. // these files don't fit so stop here
  9528. if (exceedsMaxFiles(root, queue)) return false;
  9529. // go
  9530. root.dispatch('ADD_ITEMS', {
  9531. items: queue,
  9532. index: getDragIndex(root.ref.list, children, position),
  9533. interactionMethod: InteractionMethod.DROP,
  9534. });
  9535. });
  9536. root.dispatch('DID_DROP', { position: position });
  9537. root.dispatch('DID_END_DRAG', { position: position });
  9538. };
  9539. hopper.ondragstart = function(position) {
  9540. root.dispatch('DID_START_DRAG', { position: position });
  9541. };
  9542. hopper.ondrag = debounce(function(position) {
  9543. root.dispatch('DID_DRAG', { position: position });
  9544. });
  9545. hopper.ondragend = function(position) {
  9546. root.dispatch('DID_END_DRAG', { position: position });
  9547. };
  9548. root.ref.hopper = hopper;
  9549. root.ref.drip = root.appendChildView(root.createChildView(drip));
  9550. } else if (!enabled && root.ref.hopper) {
  9551. root.ref.hopper.destroy();
  9552. root.ref.hopper = null;
  9553. root.removeChildView(root.ref.drip);
  9554. }
  9555. };
  9556. /**
  9557. * Enable or disable browse functionality
  9558. */
  9559. var toggleBrowse = function toggleBrowse(root, props) {
  9560. var isAllowed = root.query('GET_ALLOW_BROWSE');
  9561. var isDisabled = root.query('GET_DISABLED');
  9562. var enabled = isAllowed && !isDisabled;
  9563. if (enabled && !root.ref.browser) {
  9564. root.ref.browser = root.appendChildView(
  9565. root.createChildView(
  9566. browser,
  9567. Object.assign({}, props, {
  9568. onload: function onload(items) {
  9569. applyFilterChain('ADD_ITEMS', items, {
  9570. dispatch: root.dispatch,
  9571. }).then(function(queue) {
  9572. // these files don't fit so stop here
  9573. if (exceedsMaxFiles(root, queue)) return false;
  9574. // add items!
  9575. root.dispatch('ADD_ITEMS', {
  9576. items: queue,
  9577. index: -1,
  9578. interactionMethod: InteractionMethod.BROWSE,
  9579. });
  9580. });
  9581. },
  9582. })
  9583. ),
  9584. 0
  9585. );
  9586. } else if (!enabled && root.ref.browser) {
  9587. root.removeChildView(root.ref.browser);
  9588. root.ref.browser = null;
  9589. }
  9590. };
  9591. /**
  9592. * Enable or disable paste functionality
  9593. */
  9594. var togglePaste = function togglePaste(root) {
  9595. var isAllowed = root.query('GET_ALLOW_PASTE');
  9596. var isDisabled = root.query('GET_DISABLED');
  9597. var enabled = isAllowed && !isDisabled;
  9598. if (enabled && !root.ref.paster) {
  9599. root.ref.paster = createPaster();
  9600. root.ref.paster.onload = function(items) {
  9601. applyFilterChain('ADD_ITEMS', items, { dispatch: root.dispatch }).then(function(
  9602. queue
  9603. ) {
  9604. // these files don't fit so stop here
  9605. if (exceedsMaxFiles(root, queue)) return false;
  9606. // add items!
  9607. root.dispatch('ADD_ITEMS', {
  9608. items: queue,
  9609. index: -1,
  9610. interactionMethod: InteractionMethod.PASTE,
  9611. });
  9612. });
  9613. };
  9614. } else if (!enabled && root.ref.paster) {
  9615. root.ref.paster.destroy();
  9616. root.ref.paster = null;
  9617. }
  9618. };
  9619. /**
  9620. * Route actions
  9621. */
  9622. var route$5 = createRoute({
  9623. DID_SET_ALLOW_BROWSE: function DID_SET_ALLOW_BROWSE(_ref5) {
  9624. var root = _ref5.root,
  9625. props = _ref5.props;
  9626. toggleBrowse(root, props);
  9627. },
  9628. DID_SET_ALLOW_DROP: function DID_SET_ALLOW_DROP(_ref6) {
  9629. var root = _ref6.root;
  9630. toggleDrop(root);
  9631. },
  9632. DID_SET_ALLOW_PASTE: function DID_SET_ALLOW_PASTE(_ref7) {
  9633. var root = _ref7.root;
  9634. togglePaste(root);
  9635. },
  9636. DID_SET_DISABLED: function DID_SET_DISABLED(_ref8) {
  9637. var root = _ref8.root,
  9638. props = _ref8.props;
  9639. toggleDrop(root);
  9640. togglePaste(root);
  9641. toggleBrowse(root, props);
  9642. var isDisabled = root.query('GET_DISABLED');
  9643. if (isDisabled) {
  9644. root.element.dataset.disabled = 'disabled';
  9645. } else {
  9646. // delete root.element.dataset.disabled; <= this does not work on iOS 10
  9647. root.element.removeAttribute('data-disabled');
  9648. }
  9649. },
  9650. });
  9651. var root = createView({
  9652. name: 'root',
  9653. read: function read(_ref9) {
  9654. var root = _ref9.root;
  9655. if (root.ref.measure) {
  9656. root.ref.measureHeight = root.ref.measure.offsetHeight;
  9657. }
  9658. },
  9659. create: create$e,
  9660. write: write$9,
  9661. destroy: function destroy(_ref10) {
  9662. var root = _ref10.root;
  9663. if (root.ref.paster) {
  9664. root.ref.paster.destroy();
  9665. }
  9666. if (root.ref.hopper) {
  9667. root.ref.hopper.destroy();
  9668. }
  9669. root.element.removeEventListener('touchmove', prevent);
  9670. root.element.removeEventListener('gesturestart', prevent);
  9671. },
  9672. mixins: {
  9673. styles: ['height'],
  9674. },
  9675. });
  9676. // creates the app
  9677. var createApp = function createApp() {
  9678. var initialOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  9679. // let element
  9680. var originalElement = null;
  9681. // get default options
  9682. var defaultOptions = getOptions();
  9683. // create the data store, this will contain all our app info
  9684. var store = createStore(
  9685. // initial state (should be serializable)
  9686. createInitialState(defaultOptions),
  9687. // queries
  9688. [queries, createOptionQueries(defaultOptions)],
  9689. // action handlers
  9690. [actions, createOptionActions(defaultOptions)]
  9691. );
  9692. // set initial options
  9693. store.dispatch('SET_OPTIONS', { options: initialOptions });
  9694. // kick thread if visibility changes
  9695. var visibilityHandler = function visibilityHandler() {
  9696. if (document.hidden) return;
  9697. store.dispatch('KICK');
  9698. };
  9699. document.addEventListener('visibilitychange', visibilityHandler);
  9700. // re-render on window resize start and finish
  9701. var resizeDoneTimer = null;
  9702. var isResizing = false;
  9703. var isResizingHorizontally = false;
  9704. var initialWindowWidth = null;
  9705. var currentWindowWidth = null;
  9706. var resizeHandler = function resizeHandler() {
  9707. if (!isResizing) {
  9708. isResizing = true;
  9709. }
  9710. clearTimeout(resizeDoneTimer);
  9711. resizeDoneTimer = setTimeout(function() {
  9712. isResizing = false;
  9713. initialWindowWidth = null;
  9714. currentWindowWidth = null;
  9715. if (isResizingHorizontally) {
  9716. isResizingHorizontally = false;
  9717. store.dispatch('DID_STOP_RESIZE');
  9718. }
  9719. }, 500);
  9720. };
  9721. window.addEventListener('resize', resizeHandler);
  9722. // render initial view
  9723. var view = root(store, { id: getUniqueId() });
  9724. //
  9725. // PRIVATE API -------------------------------------------------------------------------------------
  9726. //
  9727. var isResting = false;
  9728. var isHidden = false;
  9729. var readWriteApi = {
  9730. // necessary for update loop
  9731. /**
  9732. * Reads from dom (never call manually)
  9733. * @private
  9734. */
  9735. _read: function _read() {
  9736. // test if we're resizing horizontally
  9737. // TODO: see if we can optimize this by measuring root rect
  9738. if (isResizing) {
  9739. currentWindowWidth = window.innerWidth;
  9740. if (!initialWindowWidth) {
  9741. initialWindowWidth = currentWindowWidth;
  9742. }
  9743. if (!isResizingHorizontally && currentWindowWidth !== initialWindowWidth) {
  9744. store.dispatch('DID_START_RESIZE');
  9745. isResizingHorizontally = true;
  9746. }
  9747. }
  9748. if (isHidden && isResting) {
  9749. // test if is no longer hidden
  9750. isResting = view.element.offsetParent === null;
  9751. }
  9752. // if resting, no need to read as numbers will still all be correct
  9753. if (isResting) return;
  9754. // read view data
  9755. view._read();
  9756. // if is hidden we need to know so we exit rest mode when revealed
  9757. isHidden = view.rect.element.hidden;
  9758. },
  9759. /**
  9760. * Writes to dom (never call manually)
  9761. * @private
  9762. */
  9763. _write: function _write(ts) {
  9764. // get all actions from store
  9765. var actions = store
  9766. .processActionQueue()
  9767. // filter out set actions (these will automatically trigger DID_SET)
  9768. .filter(function(action) {
  9769. return !/^SET_/.test(action.type);
  9770. });
  9771. // if was idling and no actions stop here
  9772. if (isResting && !actions.length) return;
  9773. // some actions might trigger events
  9774. routeActionsToEvents(actions);
  9775. // update the view
  9776. isResting = view._write(ts, actions, isResizingHorizontally);
  9777. // will clean up all archived items
  9778. removeReleasedItems(store.query('GET_ITEMS'));
  9779. // now idling
  9780. if (isResting) {
  9781. store.processDispatchQueue();
  9782. }
  9783. },
  9784. };
  9785. //
  9786. // EXPOSE EVENTS -------------------------------------------------------------------------------------
  9787. //
  9788. var createEvent = function createEvent(name) {
  9789. return function(data) {
  9790. // create default event
  9791. var event = {
  9792. type: name,
  9793. };
  9794. // no data to add
  9795. if (!data) {
  9796. return event;
  9797. }
  9798. // copy relevant props
  9799. if (data.hasOwnProperty('error')) {
  9800. event.error = data.error ? Object.assign({}, data.error) : null;
  9801. }
  9802. if (data.status) {
  9803. event.status = Object.assign({}, data.status);
  9804. }
  9805. if (data.file) {
  9806. event.output = data.file;
  9807. }
  9808. // only source is available, else add item if possible
  9809. if (data.source) {
  9810. event.file = data.source;
  9811. } else if (data.item || data.id) {
  9812. var item = data.item ? data.item : store.query('GET_ITEM', data.id);
  9813. event.file = item ? createItemAPI(item) : null;
  9814. }
  9815. // map all items in a possible items array
  9816. if (data.items) {
  9817. event.items = data.items.map(createItemAPI);
  9818. }
  9819. // if this is a progress event add the progress amount
  9820. if (/progress/.test(name)) {
  9821. event.progress = data.progress;
  9822. }
  9823. // copy relevant props
  9824. if (data.hasOwnProperty('origin') && data.hasOwnProperty('target')) {
  9825. event.origin = data.origin;
  9826. event.target = data.target;
  9827. }
  9828. return event;
  9829. };
  9830. };
  9831. var eventRoutes = {
  9832. DID_DESTROY: createEvent('destroy'),
  9833. DID_INIT: createEvent('init'),
  9834. DID_THROW_MAX_FILES: createEvent('warning'),
  9835. DID_INIT_ITEM: createEvent('initfile'),
  9836. DID_START_ITEM_LOAD: createEvent('addfilestart'),
  9837. DID_UPDATE_ITEM_LOAD_PROGRESS: createEvent('addfileprogress'),
  9838. DID_LOAD_ITEM: createEvent('addfile'),
  9839. DID_THROW_ITEM_INVALID: [createEvent('error'), createEvent('addfile')],
  9840. DID_THROW_ITEM_LOAD_ERROR: [createEvent('error'), createEvent('addfile')],
  9841. DID_THROW_ITEM_REMOVE_ERROR: [createEvent('error'), createEvent('removefile')],
  9842. DID_PREPARE_OUTPUT: createEvent('preparefile'),
  9843. DID_START_ITEM_PROCESSING: createEvent('processfilestart'),
  9844. DID_UPDATE_ITEM_PROCESS_PROGRESS: createEvent('processfileprogress'),
  9845. DID_ABORT_ITEM_PROCESSING: createEvent('processfileabort'),
  9846. DID_COMPLETE_ITEM_PROCESSING: createEvent('processfile'),
  9847. DID_COMPLETE_ITEM_PROCESSING_ALL: createEvent('processfiles'),
  9848. DID_REVERT_ITEM_PROCESSING: createEvent('processfilerevert'),
  9849. DID_THROW_ITEM_PROCESSING_ERROR: [createEvent('error'), createEvent('processfile')],
  9850. DID_REMOVE_ITEM: createEvent('removefile'),
  9851. DID_UPDATE_ITEMS: createEvent('updatefiles'),
  9852. DID_ACTIVATE_ITEM: createEvent('activatefile'),
  9853. DID_REORDER_ITEMS: createEvent('reorderfiles'),
  9854. };
  9855. var exposeEvent = function exposeEvent(event) {
  9856. // create event object to be dispatched
  9857. var detail = Object.assign({ pond: exports }, event);
  9858. delete detail.type;
  9859. view.element.dispatchEvent(
  9860. new CustomEvent('FilePond:' + event.type, {
  9861. // event info
  9862. detail: detail,
  9863. // event behaviour
  9864. bubbles: true,
  9865. cancelable: true,
  9866. composed: true, // triggers listeners outside of shadow root
  9867. })
  9868. );
  9869. // event object to params used for `on()` event handlers and callbacks `oninit()`
  9870. var params = [];
  9871. // if is possible error event, make it the first param
  9872. if (event.hasOwnProperty('error')) {
  9873. params.push(event.error);
  9874. }
  9875. // file is always section
  9876. if (event.hasOwnProperty('file')) {
  9877. params.push(event.file);
  9878. }
  9879. // append other props
  9880. var filtered = ['type', 'error', 'file'];
  9881. Object.keys(event)
  9882. .filter(function(key) {
  9883. return !filtered.includes(key);
  9884. })
  9885. .forEach(function(key) {
  9886. return params.push(event[key]);
  9887. });
  9888. // on(type, () => { })
  9889. exports.fire.apply(exports, [event.type].concat(params));
  9890. // oninit = () => {}
  9891. var handler = store.query('GET_ON' + event.type.toUpperCase());
  9892. if (handler) {
  9893. handler.apply(void 0, params);
  9894. }
  9895. };
  9896. var routeActionsToEvents = function routeActionsToEvents(actions) {
  9897. if (!actions.length) return;
  9898. actions
  9899. .filter(function(action) {
  9900. return eventRoutes[action.type];
  9901. })
  9902. .forEach(function(action) {
  9903. var routes = eventRoutes[action.type];
  9904. (Array.isArray(routes) ? routes : [routes]).forEach(function(route) {
  9905. // this isn't fantastic, but because of the stacking of settimeouts plugins can handle the did_load before the did_init
  9906. if (action.type === 'DID_INIT_ITEM') {
  9907. exposeEvent(route(action.data));
  9908. } else {
  9909. setTimeout(function() {
  9910. exposeEvent(route(action.data));
  9911. }, 0);
  9912. }
  9913. });
  9914. });
  9915. };
  9916. //
  9917. // PUBLIC API -------------------------------------------------------------------------------------
  9918. //
  9919. var setOptions = function setOptions(options) {
  9920. return store.dispatch('SET_OPTIONS', { options: options });
  9921. };
  9922. var getFile = function getFile(query) {
  9923. return store.query('GET_ACTIVE_ITEM', query);
  9924. };
  9925. var prepareFile = function prepareFile(query) {
  9926. return new Promise(function(resolve, reject) {
  9927. store.dispatch('REQUEST_ITEM_PREPARE', {
  9928. query: query,
  9929. success: function success(item) {
  9930. resolve(item);
  9931. },
  9932. failure: function failure(error) {
  9933. reject(error);
  9934. },
  9935. });
  9936. });
  9937. };
  9938. var addFile = function addFile(source) {
  9939. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  9940. return new Promise(function(resolve, reject) {
  9941. addFiles([{ source: source, options: options }], { index: options.index })
  9942. .then(function(items) {
  9943. return resolve(items && items[0]);
  9944. })
  9945. .catch(reject);
  9946. });
  9947. };
  9948. var isFilePondFile = function isFilePondFile(obj) {
  9949. return obj.file && obj.id;
  9950. };
  9951. var removeFile = function removeFile(query, options) {
  9952. // if only passed options
  9953. if (typeof query === 'object' && !isFilePondFile(query) && !options) {
  9954. options = query;
  9955. query = undefined;
  9956. }
  9957. // request item removal
  9958. store.dispatch('REMOVE_ITEM', Object.assign({}, options, { query: query }));
  9959. // see if item has been removed
  9960. return store.query('GET_ACTIVE_ITEM', query) === null;
  9961. };
  9962. var addFiles = function addFiles() {
  9963. for (
  9964. var _len = arguments.length, args = new Array(_len), _key = 0;
  9965. _key < _len;
  9966. _key++
  9967. ) {
  9968. args[_key] = arguments[_key];
  9969. }
  9970. return new Promise(function(resolve, reject) {
  9971. var sources = [];
  9972. var options = {};
  9973. // user passed a sources array
  9974. if (isArray(args[0])) {
  9975. sources.push.apply(sources, args[0]);
  9976. Object.assign(options, args[1] || {});
  9977. } else {
  9978. // user passed sources as arguments, last one might be options object
  9979. var lastArgument = args[args.length - 1];
  9980. if (typeof lastArgument === 'object' && !(lastArgument instanceof Blob)) {
  9981. Object.assign(options, args.pop());
  9982. }
  9983. // add rest to sources
  9984. sources.push.apply(sources, args);
  9985. }
  9986. store.dispatch('ADD_ITEMS', {
  9987. items: sources,
  9988. index: options.index,
  9989. interactionMethod: InteractionMethod.API,
  9990. success: resolve,
  9991. failure: reject,
  9992. });
  9993. });
  9994. };
  9995. var getFiles = function getFiles() {
  9996. return store.query('GET_ACTIVE_ITEMS');
  9997. };
  9998. var processFile = function processFile(query) {
  9999. return new Promise(function(resolve, reject) {
  10000. store.dispatch('REQUEST_ITEM_PROCESSING', {
  10001. query: query,
  10002. success: function success(item) {
  10003. resolve(item);
  10004. },
  10005. failure: function failure(error) {
  10006. reject(error);
  10007. },
  10008. });
  10009. });
  10010. };
  10011. var prepareFiles = function prepareFiles() {
  10012. for (
  10013. var _len2 = arguments.length, args = new Array(_len2), _key2 = 0;
  10014. _key2 < _len2;
  10015. _key2++
  10016. ) {
  10017. args[_key2] = arguments[_key2];
  10018. }
  10019. var queries = Array.isArray(args[0]) ? args[0] : args;
  10020. var items = queries.length ? queries : getFiles();
  10021. return Promise.all(items.map(prepareFile));
  10022. };
  10023. var processFiles = function processFiles() {
  10024. for (
  10025. var _len3 = arguments.length, args = new Array(_len3), _key3 = 0;
  10026. _key3 < _len3;
  10027. _key3++
  10028. ) {
  10029. args[_key3] = arguments[_key3];
  10030. }
  10031. var queries = Array.isArray(args[0]) ? args[0] : args;
  10032. if (!queries.length) {
  10033. var files = getFiles().filter(function(item) {
  10034. return (
  10035. !(item.status === ItemStatus.IDLE && item.origin === FileOrigin.LOCAL) &&
  10036. item.status !== ItemStatus.PROCESSING &&
  10037. item.status !== ItemStatus.PROCESSING_COMPLETE &&
  10038. item.status !== ItemStatus.PROCESSING_REVERT_ERROR
  10039. );
  10040. });
  10041. return Promise.all(files.map(processFile));
  10042. }
  10043. return Promise.all(queries.map(processFile));
  10044. };
  10045. var removeFiles = function removeFiles() {
  10046. for (
  10047. var _len4 = arguments.length, args = new Array(_len4), _key4 = 0;
  10048. _key4 < _len4;
  10049. _key4++
  10050. ) {
  10051. args[_key4] = arguments[_key4];
  10052. }
  10053. var queries = Array.isArray(args[0]) ? args[0] : args;
  10054. var options;
  10055. if (typeof queries[queries.length - 1] === 'object') {
  10056. options = queries.pop();
  10057. } else if (Array.isArray(args[0])) {
  10058. options = args[1];
  10059. }
  10060. var files = getFiles();
  10061. if (!queries.length)
  10062. return Promise.all(
  10063. files.map(function(file) {
  10064. return removeFile(file, options);
  10065. })
  10066. );
  10067. // when removing by index the indexes shift after each file removal so we need to convert indexes to ids
  10068. var mappedQueries = queries
  10069. .map(function(query) {
  10070. return isNumber(query) ? (files[query] ? files[query].id : null) : query;
  10071. })
  10072. .filter(function(query) {
  10073. return query;
  10074. });
  10075. return mappedQueries.map(function(q) {
  10076. return removeFile(q, options);
  10077. });
  10078. };
  10079. var exports = Object.assign(
  10080. {},
  10081. on(),
  10082. {},
  10083. readWriteApi,
  10084. {},
  10085. createOptionAPI(store, defaultOptions),
  10086. {
  10087. /**
  10088. * Override options defined in options object
  10089. * @param options
  10090. */
  10091. setOptions: setOptions,
  10092. /**
  10093. * Load the given file
  10094. * @param source - the source of the file (either a File, base64 data uri or url)
  10095. * @param options - object, { index: 0 }
  10096. */
  10097. addFile: addFile,
  10098. /**
  10099. * Load the given files
  10100. * @param sources - the sources of the files to load
  10101. * @param options - object, { index: 0 }
  10102. */
  10103. addFiles: addFiles,
  10104. /**
  10105. * Returns the file objects matching the given query
  10106. * @param query { string, number, null }
  10107. */
  10108. getFile: getFile,
  10109. /**
  10110. * Upload file with given name
  10111. * @param query { string, number, null }
  10112. */
  10113. processFile: processFile,
  10114. /**
  10115. * Request prepare output for file with given name
  10116. * @param query { string, number, null }
  10117. */
  10118. prepareFile: prepareFile,
  10119. /**
  10120. * Removes a file by its name
  10121. * @param query { string, number, null }
  10122. */
  10123. removeFile: removeFile,
  10124. /**
  10125. * Moves a file to a new location in the files list
  10126. */
  10127. moveFile: function moveFile(query, index) {
  10128. return store.dispatch('MOVE_ITEM', { query: query, index: index });
  10129. },
  10130. /**
  10131. * Returns all files (wrapped in public api)
  10132. */
  10133. getFiles: getFiles,
  10134. /**
  10135. * Starts uploading all files
  10136. */
  10137. processFiles: processFiles,
  10138. /**
  10139. * Clears all files from the files list
  10140. */
  10141. removeFiles: removeFiles,
  10142. /**
  10143. * Starts preparing output of all files
  10144. */
  10145. prepareFiles: prepareFiles,
  10146. /**
  10147. * Sort list of files
  10148. */
  10149. sort: function sort(compare) {
  10150. return store.dispatch('SORT', { compare: compare });
  10151. },
  10152. /**
  10153. * Browse the file system for a file
  10154. */
  10155. browse: function browse() {
  10156. // needs to be trigger directly as user action needs to be traceable (is not traceable in requestAnimationFrame)
  10157. var input = view.element.querySelector('input[type=file]');
  10158. if (input) {
  10159. input.click();
  10160. }
  10161. },
  10162. /**
  10163. * Destroys the app
  10164. */
  10165. destroy: function destroy() {
  10166. // request destruction
  10167. exports.fire('destroy', view.element);
  10168. // stop active processes (file uploads, fetches, stuff like that)
  10169. // loop over items and depending on states call abort for ongoing processes
  10170. store.dispatch('ABORT_ALL');
  10171. // destroy view
  10172. view._destroy();
  10173. // stop listening to resize
  10174. window.removeEventListener('resize', resizeHandler);
  10175. // stop listening to the visiblitychange event
  10176. document.removeEventListener('visibilitychange', visibilityHandler);
  10177. // dispatch destroy
  10178. store.dispatch('DID_DESTROY');
  10179. },
  10180. /**
  10181. * Inserts the plugin before the target element
  10182. */
  10183. insertBefore: function insertBefore$1(element) {
  10184. return insertBefore(view.element, element);
  10185. },
  10186. /**
  10187. * Inserts the plugin after the target element
  10188. */
  10189. insertAfter: function insertAfter$1(element) {
  10190. return insertAfter(view.element, element);
  10191. },
  10192. /**
  10193. * Appends the plugin to the target element
  10194. */
  10195. appendTo: function appendTo(element) {
  10196. return element.appendChild(view.element);
  10197. },
  10198. /**
  10199. * Replaces an element with the app
  10200. */
  10201. replaceElement: function replaceElement(element) {
  10202. // insert the app before the element
  10203. insertBefore(view.element, element);
  10204. // remove the original element
  10205. element.parentNode.removeChild(element);
  10206. // remember original element
  10207. originalElement = element;
  10208. },
  10209. /**
  10210. * Restores the original element
  10211. */
  10212. restoreElement: function restoreElement() {
  10213. if (!originalElement) {
  10214. return; // no element to restore
  10215. }
  10216. // restore original element
  10217. insertAfter(originalElement, view.element);
  10218. // remove our element
  10219. view.element.parentNode.removeChild(view.element);
  10220. // remove reference
  10221. originalElement = null;
  10222. },
  10223. /**
  10224. * Returns true if the app root is attached to given element
  10225. * @param element
  10226. */
  10227. isAttachedTo: function isAttachedTo(element) {
  10228. return view.element === element || originalElement === element;
  10229. },
  10230. /**
  10231. * Returns the root element
  10232. */
  10233. element: {
  10234. get: function get() {
  10235. return view.element;
  10236. },
  10237. },
  10238. /**
  10239. * Returns the current pond status
  10240. */
  10241. status: {
  10242. get: function get() {
  10243. return store.query('GET_STATUS');
  10244. },
  10245. },
  10246. }
  10247. );
  10248. // Done!
  10249. store.dispatch('DID_INIT');
  10250. // create actual api object
  10251. return createObject(exports);
  10252. };
  10253. var createAppObject = function createAppObject() {
  10254. var customOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  10255. // default options
  10256. var defaultOptions = {};
  10257. forin(getOptions(), function(key, value) {
  10258. defaultOptions[key] = value[0];
  10259. });
  10260. // set app options
  10261. var app = createApp(
  10262. Object.assign(
  10263. {},
  10264. defaultOptions,
  10265. {},
  10266. customOptions
  10267. )
  10268. );
  10269. // return the plugin instance
  10270. return app;
  10271. };
  10272. var lowerCaseFirstLetter = function lowerCaseFirstLetter(string) {
  10273. return string.charAt(0).toLowerCase() + string.slice(1);
  10274. };
  10275. var attributeNameToPropertyName = function attributeNameToPropertyName(attributeName) {
  10276. return toCamels(attributeName.replace(/^data-/, ''));
  10277. };
  10278. var mapObject = function mapObject(object, propertyMap) {
  10279. // remove unwanted
  10280. forin(propertyMap, function(selector, mapping) {
  10281. forin(object, function(property, value) {
  10282. // create regexp shortcut
  10283. var selectorRegExp = new RegExp(selector);
  10284. // tests if
  10285. var matches = selectorRegExp.test(property);
  10286. // no match, skip
  10287. if (!matches) {
  10288. return;
  10289. }
  10290. // if there's a mapping, the original property is always removed
  10291. delete object[property];
  10292. // should only remove, we done!
  10293. if (mapping === false) {
  10294. return;
  10295. }
  10296. // move value to new property
  10297. if (isString(mapping)) {
  10298. object[mapping] = value;
  10299. return;
  10300. }
  10301. // move to group
  10302. var group = mapping.group;
  10303. if (isObject(mapping) && !object[group]) {
  10304. object[group] = {};
  10305. }
  10306. object[group][lowerCaseFirstLetter(property.replace(selectorRegExp, ''))] = value;
  10307. });
  10308. // do submapping
  10309. if (mapping.mapping) {
  10310. mapObject(object[mapping.group], mapping.mapping);
  10311. }
  10312. });
  10313. };
  10314. var getAttributesAsObject = function getAttributesAsObject(node) {
  10315. var attributeMapping =
  10316. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  10317. // turn attributes into object
  10318. var attributes = [];
  10319. forin(node.attributes, function(index) {
  10320. attributes.push(node.attributes[index]);
  10321. });
  10322. var output = attributes
  10323. .filter(function(attribute) {
  10324. return attribute.name;
  10325. })
  10326. .reduce(function(obj, attribute) {
  10327. var value = attr(node, attribute.name);
  10328. obj[attributeNameToPropertyName(attribute.name)] =
  10329. value === attribute.name ? true : value;
  10330. return obj;
  10331. }, {});
  10332. // do mapping of object properties
  10333. mapObject(output, attributeMapping);
  10334. return output;
  10335. };
  10336. var createAppAtElement = function createAppAtElement(element) {
  10337. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  10338. // how attributes of the input element are mapped to the options for the plugin
  10339. var attributeMapping = {
  10340. // translate to other name
  10341. '^class$': 'className',
  10342. '^multiple$': 'allowMultiple',
  10343. '^capture$': 'captureMethod',
  10344. '^webkitdirectory$': 'allowDirectoriesOnly',
  10345. // group under single property
  10346. '^server': {
  10347. group: 'server',
  10348. mapping: {
  10349. '^process': {
  10350. group: 'process',
  10351. },
  10352. '^revert': {
  10353. group: 'revert',
  10354. },
  10355. '^fetch': {
  10356. group: 'fetch',
  10357. },
  10358. '^restore': {
  10359. group: 'restore',
  10360. },
  10361. '^load': {
  10362. group: 'load',
  10363. },
  10364. },
  10365. },
  10366. // don't include in object
  10367. '^type$': false,
  10368. '^files$': false,
  10369. };
  10370. // add additional option translators
  10371. applyFilters('SET_ATTRIBUTE_TO_OPTION_MAP', attributeMapping);
  10372. // create final options object by setting options object and then overriding options supplied on element
  10373. var mergedOptions = Object.assign({}, options);
  10374. var attributeOptions = getAttributesAsObject(
  10375. element.nodeName === 'FIELDSET' ? element.querySelector('input[type=file]') : element,
  10376. attributeMapping
  10377. );
  10378. // merge with options object
  10379. Object.keys(attributeOptions).forEach(function(key) {
  10380. if (isObject(attributeOptions[key])) {
  10381. if (!isObject(mergedOptions[key])) {
  10382. mergedOptions[key] = {};
  10383. }
  10384. Object.assign(mergedOptions[key], attributeOptions[key]);
  10385. } else {
  10386. mergedOptions[key] = attributeOptions[key];
  10387. }
  10388. });
  10389. // if parent is a fieldset, get files from parent by selecting all input fields that are not file upload fields
  10390. // these will then be automatically set to the initial files
  10391. mergedOptions.files = (options.files || []).concat(
  10392. Array.from(element.querySelectorAll('input:not([type=file])')).map(function(input) {
  10393. return {
  10394. source: input.value,
  10395. options: {
  10396. type: input.dataset.type,
  10397. },
  10398. };
  10399. })
  10400. );
  10401. // build plugin
  10402. var app = createAppObject(mergedOptions);
  10403. // add already selected files
  10404. if (element.files) {
  10405. Array.from(element.files).forEach(function(file) {
  10406. app.addFile(file);
  10407. });
  10408. }
  10409. // replace the target element
  10410. app.replaceElement(element);
  10411. // expose
  10412. return app;
  10413. };
  10414. // if an element is passed, we create the instance at that element, if not, we just create an up object
  10415. var createApp$1 = function createApp() {
  10416. return isNode(arguments.length <= 0 ? undefined : arguments[0])
  10417. ? createAppAtElement.apply(void 0, arguments)
  10418. : createAppObject.apply(void 0, arguments);
  10419. };
  10420. var PRIVATE_METHODS = ['fire', '_read', '_write'];
  10421. var createAppAPI = function createAppAPI(app) {
  10422. var api = {};
  10423. copyObjectPropertiesToObject(app, api, PRIVATE_METHODS);
  10424. return api;
  10425. };
  10426. /**
  10427. * Replaces placeholders in given string with replacements
  10428. * @param string - "Foo {bar}""
  10429. * @param replacements - { "bar": 10 }
  10430. */
  10431. var replaceInString = function replaceInString(string, replacements) {
  10432. return string.replace(/(?:{([a-zA-Z]+)})/g, function(match, group) {
  10433. return replacements[group];
  10434. });
  10435. };
  10436. var createWorker = function createWorker(fn) {
  10437. var workerBlob = new Blob(['(', fn.toString(), ')()'], {
  10438. type: 'application/javascript',
  10439. });
  10440. var workerURL = URL.createObjectURL(workerBlob);
  10441. var worker = new Worker(workerURL);
  10442. return {
  10443. transfer: function transfer(message, cb) {},
  10444. post: function post(message, cb, transferList) {
  10445. var id = getUniqueId();
  10446. worker.onmessage = function(e) {
  10447. if (e.data.id === id) {
  10448. cb(e.data.message);
  10449. }
  10450. };
  10451. worker.postMessage(
  10452. {
  10453. id: id,
  10454. message: message,
  10455. },
  10456. transferList
  10457. );
  10458. },
  10459. terminate: function terminate() {
  10460. worker.terminate();
  10461. URL.revokeObjectURL(workerURL);
  10462. },
  10463. };
  10464. };
  10465. var loadImage = function loadImage(url) {
  10466. return new Promise(function(resolve, reject) {
  10467. var img = new Image();
  10468. img.onload = function() {
  10469. resolve(img);
  10470. };
  10471. img.onerror = function(e) {
  10472. reject(e);
  10473. };
  10474. img.src = url;
  10475. });
  10476. };
  10477. var renameFile = function renameFile(file, name) {
  10478. var renamedFile = file.slice(0, file.size, file.type);
  10479. renamedFile.lastModifiedDate = file.lastModifiedDate;
  10480. renamedFile.name = name;
  10481. return renamedFile;
  10482. };
  10483. var copyFile = function copyFile(file) {
  10484. return renameFile(file, file.name);
  10485. };
  10486. // already registered plugins (can't register twice)
  10487. var registeredPlugins = [];
  10488. // pass utils to plugin
  10489. var createAppPlugin = function createAppPlugin(plugin) {
  10490. // already registered
  10491. if (registeredPlugins.includes(plugin)) {
  10492. return;
  10493. }
  10494. // remember this plugin
  10495. registeredPlugins.push(plugin);
  10496. // setup!
  10497. var pluginOutline = plugin({
  10498. addFilter: addFilter,
  10499. utils: {
  10500. Type: Type,
  10501. forin: forin,
  10502. isString: isString,
  10503. isFile: isFile,
  10504. toNaturalFileSize: toNaturalFileSize,
  10505. replaceInString: replaceInString,
  10506. getExtensionFromFilename: getExtensionFromFilename,
  10507. getFilenameWithoutExtension: getFilenameWithoutExtension,
  10508. guesstimateMimeType: guesstimateMimeType,
  10509. getFileFromBlob: getFileFromBlob,
  10510. getFilenameFromURL: getFilenameFromURL,
  10511. createRoute: createRoute,
  10512. createWorker: createWorker,
  10513. createView: createView,
  10514. createItemAPI: createItemAPI,
  10515. loadImage: loadImage,
  10516. copyFile: copyFile,
  10517. renameFile: renameFile,
  10518. createBlob: createBlob,
  10519. applyFilterChain: applyFilterChain,
  10520. text: text,
  10521. getNumericAspectRatioFromString: getNumericAspectRatioFromString,
  10522. },
  10523. views: {
  10524. fileActionButton: fileActionButton,
  10525. },
  10526. });
  10527. // add plugin options to default options
  10528. extendDefaultOptions(pluginOutline.options);
  10529. };
  10530. // feature detection used by supported() method
  10531. var isOperaMini = function isOperaMini() {
  10532. return Object.prototype.toString.call(window.operamini) === '[object OperaMini]';
  10533. };
  10534. var hasPromises = function hasPromises() {
  10535. return 'Promise' in window;
  10536. };
  10537. var hasBlobSlice = function hasBlobSlice() {
  10538. return 'slice' in Blob.prototype;
  10539. };
  10540. var hasCreateObjectURL = function hasCreateObjectURL() {
  10541. return 'URL' in window && 'createObjectURL' in window.URL;
  10542. };
  10543. var hasVisibility = function hasVisibility() {
  10544. return 'visibilityState' in document;
  10545. };
  10546. var hasTiming = function hasTiming() {
  10547. return 'performance' in window;
  10548. }; // iOS 8.x
  10549. var hasCSSSupports = function hasCSSSupports() {
  10550. return 'supports' in (window.CSS || {});
  10551. }; // use to detect Safari 9+
  10552. var isIE11 = function isIE11() {
  10553. return /MSIE|Trident/.test(window.navigator.userAgent);
  10554. };
  10555. var supported = (function() {
  10556. // Runs immediately and then remembers result for subsequent calls
  10557. var isSupported =
  10558. // Has to be a browser
  10559. isBrowser() &&
  10560. // Can't run on Opera Mini due to lack of everything
  10561. !isOperaMini() &&
  10562. // Require these APIs to feature detect a modern browser
  10563. hasVisibility() &&
  10564. hasPromises() &&
  10565. hasBlobSlice() &&
  10566. hasCreateObjectURL() &&
  10567. hasTiming() &&
  10568. // doesn't need CSSSupports but is a good way to detect Safari 9+ (we do want to support IE11 though)
  10569. (hasCSSSupports() || isIE11());
  10570. return function() {
  10571. return isSupported;
  10572. };
  10573. })();
  10574. /**
  10575. * Plugin internal state (over all instances)
  10576. */
  10577. var state = {
  10578. // active app instances, used to redraw the apps and to find the later
  10579. apps: [],
  10580. };
  10581. // plugin name
  10582. var name = 'filepond';
  10583. /**
  10584. * Public Plugin methods
  10585. */
  10586. var fn = function fn() {};
  10587. exports.Status = {};
  10588. exports.FileStatus = {};
  10589. exports.FileOrigin = {};
  10590. exports.OptionTypes = {};
  10591. exports.create = fn;
  10592. exports.destroy = fn;
  10593. exports.parse = fn;
  10594. exports.find = fn;
  10595. exports.registerPlugin = fn;
  10596. exports.getOptions = fn;
  10597. exports.setOptions = fn;
  10598. // if not supported, no API
  10599. if (supported()) {
  10600. // start painter and fire load event
  10601. createPainter(
  10602. function() {
  10603. state.apps.forEach(function(app) {
  10604. return app._read();
  10605. });
  10606. },
  10607. function(ts) {
  10608. state.apps.forEach(function(app) {
  10609. return app._write(ts);
  10610. });
  10611. }
  10612. );
  10613. // fire loaded event so we know when FilePond is available
  10614. var dispatch = function dispatch() {
  10615. // let others know we have area ready
  10616. document.dispatchEvent(
  10617. new CustomEvent('FilePond:loaded', {
  10618. detail: {
  10619. supported: supported,
  10620. create: exports.create,
  10621. destroy: exports.destroy,
  10622. parse: exports.parse,
  10623. find: exports.find,
  10624. registerPlugin: exports.registerPlugin,
  10625. setOptions: exports.setOptions,
  10626. },
  10627. })
  10628. );
  10629. // clean up event
  10630. document.removeEventListener('DOMContentLoaded', dispatch);
  10631. };
  10632. if (document.readyState !== 'loading') {
  10633. // move to back of execution queue, FilePond should have been exported by then
  10634. setTimeout(function() {
  10635. return dispatch();
  10636. }, 0);
  10637. } else {
  10638. document.addEventListener('DOMContentLoaded', dispatch);
  10639. }
  10640. // updates the OptionTypes object based on the current options
  10641. var updateOptionTypes = function updateOptionTypes() {
  10642. return forin(getOptions(), function(key, value) {
  10643. exports.OptionTypes[key] = value[1];
  10644. });
  10645. };
  10646. exports.Status = Object.assign({}, Status);
  10647. exports.FileOrigin = Object.assign({}, FileOrigin);
  10648. exports.FileStatus = Object.assign({}, ItemStatus);
  10649. exports.OptionTypes = {};
  10650. updateOptionTypes();
  10651. // create method, creates apps and adds them to the app array
  10652. exports.create = function create() {
  10653. var app = createApp$1.apply(void 0, arguments);
  10654. app.on('destroy', exports.destroy);
  10655. state.apps.push(app);
  10656. return createAppAPI(app);
  10657. };
  10658. // destroys apps and removes them from the app array
  10659. exports.destroy = function destroy(hook) {
  10660. // returns true if the app was destroyed successfully
  10661. var indexToRemove = state.apps.findIndex(function(app) {
  10662. return app.isAttachedTo(hook);
  10663. });
  10664. if (indexToRemove >= 0) {
  10665. // remove from apps
  10666. var app = state.apps.splice(indexToRemove, 1)[0];
  10667. // restore original dom element
  10668. app.restoreElement();
  10669. return true;
  10670. }
  10671. return false;
  10672. };
  10673. // parses the given context for plugins (does not include the context element itself)
  10674. exports.parse = function parse(context) {
  10675. // get all possible hooks
  10676. var matchedHooks = Array.from(context.querySelectorAll('.' + name));
  10677. // filter out already active hooks
  10678. var newHooks = matchedHooks.filter(function(newHook) {
  10679. return !state.apps.find(function(app) {
  10680. return app.isAttachedTo(newHook);
  10681. });
  10682. });
  10683. // create new instance for each hook
  10684. return newHooks.map(function(hook) {
  10685. return exports.create(hook);
  10686. });
  10687. };
  10688. // returns an app based on the given element hook
  10689. exports.find = function find(hook) {
  10690. var app = state.apps.find(function(app) {
  10691. return app.isAttachedTo(hook);
  10692. });
  10693. if (!app) {
  10694. return null;
  10695. }
  10696. return createAppAPI(app);
  10697. };
  10698. // adds a plugin extension
  10699. exports.registerPlugin = function registerPlugin() {
  10700. for (
  10701. var _len = arguments.length, plugins = new Array(_len), _key = 0;
  10702. _key < _len;
  10703. _key++
  10704. ) {
  10705. plugins[_key] = arguments[_key];
  10706. }
  10707. // register plugins
  10708. plugins.forEach(createAppPlugin);
  10709. // update OptionTypes, each plugin might have extended the default options
  10710. updateOptionTypes();
  10711. };
  10712. exports.getOptions = function getOptions$1() {
  10713. var opts = {};
  10714. forin(getOptions(), function(key, value) {
  10715. opts[key] = value[0];
  10716. });
  10717. return opts;
  10718. };
  10719. exports.setOptions = function setOptions$1(opts) {
  10720. if (isObject(opts)) {
  10721. // update existing plugins
  10722. state.apps.forEach(function(app) {
  10723. app.setOptions(opts);
  10724. });
  10725. // override defaults
  10726. setOptions(opts);
  10727. }
  10728. // return new options
  10729. return exports.getOptions();
  10730. };
  10731. }
  10732. exports.supported = supported;
  10733. Object.defineProperty(exports, '__esModule', { value: true });
  10734. });