drag-drop.es5.js 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { normalizePassiveListenerOptions, _supportsShadowDom } from '@angular/cdk/platform';
  9. import { coerceBooleanProperty, coerceElement, coerceNumberProperty, coerceArray } from '@angular/cdk/coercion';
  10. import { Subscription, Subject, interval, animationFrameScheduler, Observable, merge } from 'rxjs';
  11. import { startWith, takeUntil, take, map, switchMap, tap } from 'rxjs/operators';
  12. import { Injectable, NgZone, Inject, InjectionToken, NgModule, ContentChildren, ElementRef, EventEmitter, forwardRef, Input, Output, Optional, Directive, ChangeDetectorRef, SkipSelf, ContentChild, ViewContainerRef, isDevMode, TemplateRef, ɵɵdefineInjectable, ɵɵinject } from '@angular/core';
  13. import { DOCUMENT } from '@angular/common';
  14. import { ViewportRuler } from '@angular/cdk/scrolling';
  15. import { Directionality } from '@angular/cdk/bidi';
  16. /**
  17. * @fileoverview added by tsickle
  18. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  19. */
  20. /**
  21. * Shallow-extends a stylesheet object with another stylesheet object.
  22. * \@docs-private
  23. * @param {?} dest
  24. * @param {?} source
  25. * @return {?}
  26. */
  27. function extendStyles(dest, source) {
  28. for (var key in source) {
  29. if (source.hasOwnProperty(key)) {
  30. dest[key] = (/** @type {?} */ (source[key]));
  31. }
  32. }
  33. return dest;
  34. }
  35. /**
  36. * Toggles whether the native drag interactions should be enabled for an element.
  37. * \@docs-private
  38. * @param {?} element Element on which to toggle the drag interactions.
  39. * @param {?} enable Whether the drag interactions should be enabled.
  40. * @return {?}
  41. */
  42. function toggleNativeDragInteractions(element, enable) {
  43. /** @type {?} */
  44. var userSelect = enable ? '' : 'none';
  45. extendStyles(element.style, {
  46. touchAction: enable ? '' : 'none',
  47. webkitUserDrag: enable ? '' : 'none',
  48. webkitTapHighlightColor: enable ? '' : 'transparent',
  49. userSelect: userSelect,
  50. msUserSelect: userSelect,
  51. webkitUserSelect: userSelect,
  52. MozUserSelect: userSelect
  53. });
  54. }
  55. /**
  56. * @fileoverview added by tsickle
  57. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  58. */
  59. /**
  60. * Parses a CSS time value to milliseconds.
  61. * @param {?} value
  62. * @return {?}
  63. */
  64. function parseCssTimeUnitsToMs(value) {
  65. // Some browsers will return it in seconds, whereas others will return milliseconds.
  66. /** @type {?} */
  67. var multiplier = value.toLowerCase().indexOf('ms') > -1 ? 1 : 1000;
  68. return parseFloat(value) * multiplier;
  69. }
  70. /**
  71. * Gets the transform transition duration, including the delay, of an element in milliseconds.
  72. * @param {?} element
  73. * @return {?}
  74. */
  75. function getTransformTransitionDurationInMs(element) {
  76. /** @type {?} */
  77. var computedStyle = getComputedStyle(element);
  78. /** @type {?} */
  79. var transitionedProperties = parseCssPropertyValue(computedStyle, 'transition-property');
  80. /** @type {?} */
  81. var property = transitionedProperties.find((/**
  82. * @param {?} prop
  83. * @return {?}
  84. */
  85. function (prop) { return prop === 'transform' || prop === 'all'; }));
  86. // If there's no transition for `all` or `transform`, we shouldn't do anything.
  87. if (!property) {
  88. return 0;
  89. }
  90. // Get the index of the property that we're interested in and match
  91. // it up to the same index in `transition-delay` and `transition-duration`.
  92. /** @type {?} */
  93. var propertyIndex = transitionedProperties.indexOf(property);
  94. /** @type {?} */
  95. var rawDurations = parseCssPropertyValue(computedStyle, 'transition-duration');
  96. /** @type {?} */
  97. var rawDelays = parseCssPropertyValue(computedStyle, 'transition-delay');
  98. return parseCssTimeUnitsToMs(rawDurations[propertyIndex]) +
  99. parseCssTimeUnitsToMs(rawDelays[propertyIndex]);
  100. }
  101. /**
  102. * Parses out multiple values from a computed style into an array.
  103. * @param {?} computedStyle
  104. * @param {?} name
  105. * @return {?}
  106. */
  107. function parseCssPropertyValue(computedStyle, name) {
  108. /** @type {?} */
  109. var value = computedStyle.getPropertyValue(name);
  110. return value.split(',').map((/**
  111. * @param {?} part
  112. * @return {?}
  113. */
  114. function (part) { return part.trim(); }));
  115. }
  116. /**
  117. * @fileoverview added by tsickle
  118. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  119. */
  120. /**
  121. * Options that can be used to bind a passive event listener.
  122. * @type {?}
  123. */
  124. var passiveEventListenerOptions = normalizePassiveListenerOptions({ passive: true });
  125. /**
  126. * Options that can be used to bind an active event listener.
  127. * @type {?}
  128. */
  129. var activeEventListenerOptions = normalizePassiveListenerOptions({ passive: false });
  130. /**
  131. * Time in milliseconds for which to ignore mouse events, after
  132. * receiving a touch event. Used to avoid doing double work for
  133. * touch devices where the browser fires fake mouse events, in
  134. * addition to touch events.
  135. * @type {?}
  136. */
  137. var MOUSE_EVENT_IGNORE_TIME = 800;
  138. /**
  139. * Reference to a draggable item. Used to manipulate or dispose of the item.
  140. * \@docs-private
  141. * @template T
  142. */
  143. var /**
  144. * Reference to a draggable item. Used to manipulate or dispose of the item.
  145. * \@docs-private
  146. * @template T
  147. */
  148. DragRef = /** @class */ (function () {
  149. function DragRef(element, _config, _document, _ngZone, _viewportRuler, _dragDropRegistry) {
  150. var _this = this;
  151. this._config = _config;
  152. this._document = _document;
  153. this._ngZone = _ngZone;
  154. this._viewportRuler = _viewportRuler;
  155. this._dragDropRegistry = _dragDropRegistry;
  156. /**
  157. * CSS `transform` applied to the element when it isn't being dragged. We need a
  158. * passive transform in order for the dragged element to retain its new position
  159. * after the user has stopped dragging and because we need to know the relative
  160. * position in case they start dragging again. This corresponds to `element.style.transform`.
  161. */
  162. this._passiveTransform = { x: 0, y: 0 };
  163. /**
  164. * CSS `transform` that is applied to the element while it's being dragged.
  165. */
  166. this._activeTransform = { x: 0, y: 0 };
  167. /**
  168. * Emits when the item is being moved.
  169. */
  170. this._moveEvents = new Subject();
  171. /**
  172. * Subscription to pointer movement events.
  173. */
  174. this._pointerMoveSubscription = Subscription.EMPTY;
  175. /**
  176. * Subscription to the event that is dispatched when the user lifts their pointer.
  177. */
  178. this._pointerUpSubscription = Subscription.EMPTY;
  179. /**
  180. * Subscription to the viewport being scrolled.
  181. */
  182. this._scrollSubscription = Subscription.EMPTY;
  183. /**
  184. * Subscription to the viewport being resized.
  185. */
  186. this._resizeSubscription = Subscription.EMPTY;
  187. /**
  188. * Cached reference to the boundary element.
  189. */
  190. this._boundaryElement = null;
  191. /**
  192. * Whether the native dragging interactions have been enabled on the root element.
  193. */
  194. this._nativeInteractionsEnabled = true;
  195. /**
  196. * Elements that can be used to drag the draggable item.
  197. */
  198. this._handles = [];
  199. /**
  200. * Registered handles that are currently disabled.
  201. */
  202. this._disabledHandles = new Set();
  203. /**
  204. * Layout direction of the item.
  205. */
  206. this._direction = 'ltr';
  207. /**
  208. * Amount of milliseconds to wait after the user has put their
  209. * pointer down before starting to drag the element.
  210. */
  211. this.dragStartDelay = 0;
  212. this._disabled = false;
  213. /**
  214. * Emits as the drag sequence is being prepared.
  215. */
  216. this.beforeStarted = new Subject();
  217. /**
  218. * Emits when the user starts dragging the item.
  219. */
  220. this.started = new Subject();
  221. /**
  222. * Emits when the user has released a drag item, before any animations have started.
  223. */
  224. this.released = new Subject();
  225. /**
  226. * Emits when the user stops dragging an item in the container.
  227. */
  228. this.ended = new Subject();
  229. /**
  230. * Emits when the user has moved the item into a new container.
  231. */
  232. this.entered = new Subject();
  233. /**
  234. * Emits when the user removes the item its container by dragging it into another container.
  235. */
  236. this.exited = new Subject();
  237. /**
  238. * Emits when the user drops the item inside a container.
  239. */
  240. this.dropped = new Subject();
  241. /**
  242. * Emits as the user is dragging the item. Use with caution,
  243. * because this event will fire for every pixel that the user has dragged.
  244. */
  245. this.moved = this._moveEvents.asObservable();
  246. /**
  247. * Handler for the `mousedown`/`touchstart` events.
  248. */
  249. this._pointerDown = (/**
  250. * @param {?} event
  251. * @return {?}
  252. */
  253. function (event) {
  254. _this.beforeStarted.next();
  255. // Delegate the event based on whether it started from a handle or the element itself.
  256. if (_this._handles.length) {
  257. /** @type {?} */
  258. var targetHandle = _this._handles.find((/**
  259. * @param {?} handle
  260. * @return {?}
  261. */
  262. function (handle) {
  263. /** @type {?} */
  264. var target = event.target;
  265. return !!target && (target === handle || handle.contains((/** @type {?} */ (target))));
  266. }));
  267. if (targetHandle && !_this._disabledHandles.has(targetHandle) && !_this.disabled) {
  268. _this._initializeDragSequence(targetHandle, event);
  269. }
  270. }
  271. else if (!_this.disabled) {
  272. _this._initializeDragSequence(_this._rootElement, event);
  273. }
  274. });
  275. /**
  276. * Handler that is invoked when the user moves their pointer after they've initiated a drag.
  277. */
  278. this._pointerMove = (/**
  279. * @param {?} event
  280. * @return {?}
  281. */
  282. function (event) {
  283. if (!_this._hasStartedDragging) {
  284. /** @type {?} */
  285. var pointerPosition = _this._getPointerPositionOnPage(event);
  286. /** @type {?} */
  287. var distanceX = Math.abs(pointerPosition.x - _this._pickupPositionOnPage.x);
  288. /** @type {?} */
  289. var distanceY = Math.abs(pointerPosition.y - _this._pickupPositionOnPage.y);
  290. /** @type {?} */
  291. var isOverThreshold = distanceX + distanceY >= _this._config.dragStartThreshold;
  292. // Only start dragging after the user has moved more than the minimum distance in either
  293. // direction. Note that this is preferrable over doing something like `skip(minimumDistance)`
  294. // in the `pointerMove` subscription, because we're not guaranteed to have one move event
  295. // per pixel of movement (e.g. if the user moves their pointer quickly).
  296. if (isOverThreshold) {
  297. /** @type {?} */
  298. var isDelayElapsed = Date.now() >= _this._dragStartTime + (_this.dragStartDelay || 0);
  299. if (!isDelayElapsed) {
  300. _this._endDragSequence(event);
  301. return;
  302. }
  303. // Prevent other drag sequences from starting while something in the container is still
  304. // being dragged. This can happen while we're waiting for the drop animation to finish
  305. // and can cause errors, because some elements might still be moving around.
  306. if (!_this._dropContainer || !_this._dropContainer.isDragging()) {
  307. _this._hasStartedDragging = true;
  308. _this._ngZone.run((/**
  309. * @return {?}
  310. */
  311. function () { return _this._startDragSequence(event); }));
  312. }
  313. }
  314. return;
  315. }
  316. // We only need the preview dimensions if we have a boundary element.
  317. if (_this._boundaryElement) {
  318. // Cache the preview element rect if we haven't cached it already or if
  319. // we cached it too early before the element dimensions were computed.
  320. if (!_this._previewRect || (!_this._previewRect.width && !_this._previewRect.height)) {
  321. _this._previewRect = (_this._preview || _this._rootElement).getBoundingClientRect();
  322. }
  323. }
  324. /** @type {?} */
  325. var constrainedPointerPosition = _this._getConstrainedPointerPosition(event);
  326. _this._hasMoved = true;
  327. event.preventDefault();
  328. _this._updatePointerDirectionDelta(constrainedPointerPosition);
  329. if (_this._dropContainer) {
  330. _this._updateActiveDropContainer(constrainedPointerPosition);
  331. }
  332. else {
  333. /** @type {?} */
  334. var activeTransform = _this._activeTransform;
  335. activeTransform.x =
  336. constrainedPointerPosition.x - _this._pickupPositionOnPage.x + _this._passiveTransform.x;
  337. activeTransform.y =
  338. constrainedPointerPosition.y - _this._pickupPositionOnPage.y + _this._passiveTransform.y;
  339. _this._applyRootElementTransform(activeTransform.x, activeTransform.y);
  340. // Apply transform as attribute if dragging and svg element to work for IE
  341. if (typeof SVGElement !== 'undefined' && _this._rootElement instanceof SVGElement) {
  342. /** @type {?} */
  343. var appliedTransform = "translate(" + activeTransform.x + " " + activeTransform.y + ")";
  344. _this._rootElement.setAttribute('transform', appliedTransform);
  345. }
  346. }
  347. // Since this event gets fired for every pixel while dragging, we only
  348. // want to fire it if the consumer opted into it. Also we have to
  349. // re-enter the zone because we run all of the events on the outside.
  350. if (_this._moveEvents.observers.length) {
  351. _this._ngZone.run((/**
  352. * @return {?}
  353. */
  354. function () {
  355. _this._moveEvents.next({
  356. source: _this,
  357. pointerPosition: constrainedPointerPosition,
  358. event: event,
  359. distance: _this._getDragDistance(constrainedPointerPosition),
  360. delta: _this._pointerDirectionDelta
  361. });
  362. }));
  363. }
  364. });
  365. /**
  366. * Handler that is invoked when the user lifts their pointer up, after initiating a drag.
  367. */
  368. this._pointerUp = (/**
  369. * @param {?} event
  370. * @return {?}
  371. */
  372. function (event) {
  373. _this._endDragSequence(event);
  374. });
  375. this.withRootElement(element);
  376. _dragDropRegistry.registerDragItem(this);
  377. }
  378. Object.defineProperty(DragRef.prototype, "disabled", {
  379. /** Whether starting to drag this element is disabled. */
  380. get: /**
  381. * Whether starting to drag this element is disabled.
  382. * @return {?}
  383. */
  384. function () {
  385. return this._disabled || !!(this._dropContainer && this._dropContainer.disabled);
  386. },
  387. set: /**
  388. * @param {?} value
  389. * @return {?}
  390. */
  391. function (value) {
  392. /** @type {?} */
  393. var newValue = coerceBooleanProperty(value);
  394. if (newValue !== this._disabled) {
  395. this._disabled = newValue;
  396. this._toggleNativeDragInteractions();
  397. }
  398. },
  399. enumerable: true,
  400. configurable: true
  401. });
  402. /**
  403. * Returns the element that is being used as a placeholder
  404. * while the current element is being dragged.
  405. */
  406. /**
  407. * Returns the element that is being used as a placeholder
  408. * while the current element is being dragged.
  409. * @return {?}
  410. */
  411. DragRef.prototype.getPlaceholderElement = /**
  412. * Returns the element that is being used as a placeholder
  413. * while the current element is being dragged.
  414. * @return {?}
  415. */
  416. function () {
  417. return this._placeholder;
  418. };
  419. /** Returns the root draggable element. */
  420. /**
  421. * Returns the root draggable element.
  422. * @return {?}
  423. */
  424. DragRef.prototype.getRootElement = /**
  425. * Returns the root draggable element.
  426. * @return {?}
  427. */
  428. function () {
  429. return this._rootElement;
  430. };
  431. /** Registers the handles that can be used to drag the element. */
  432. /**
  433. * Registers the handles that can be used to drag the element.
  434. * @template THIS
  435. * @this {THIS}
  436. * @param {?} handles
  437. * @return {THIS}
  438. */
  439. DragRef.prototype.withHandles = /**
  440. * Registers the handles that can be used to drag the element.
  441. * @template THIS
  442. * @this {THIS}
  443. * @param {?} handles
  444. * @return {THIS}
  445. */
  446. function (handles) {
  447. (/** @type {?} */ (this))._handles = handles.map((/**
  448. * @param {?} handle
  449. * @return {?}
  450. */
  451. function (handle) { return coerceElement(handle); }));
  452. (/** @type {?} */ (this))._handles.forEach((/**
  453. * @param {?} handle
  454. * @return {?}
  455. */
  456. function (handle) { return toggleNativeDragInteractions(handle, false); }));
  457. (/** @type {?} */ (this))._toggleNativeDragInteractions();
  458. return (/** @type {?} */ (this));
  459. };
  460. /**
  461. * Registers the template that should be used for the drag preview.
  462. * @param template Template that from which to stamp out the preview.
  463. */
  464. /**
  465. * Registers the template that should be used for the drag preview.
  466. * @template THIS
  467. * @this {THIS}
  468. * @param {?} template Template that from which to stamp out the preview.
  469. * @return {THIS}
  470. */
  471. DragRef.prototype.withPreviewTemplate = /**
  472. * Registers the template that should be used for the drag preview.
  473. * @template THIS
  474. * @this {THIS}
  475. * @param {?} template Template that from which to stamp out the preview.
  476. * @return {THIS}
  477. */
  478. function (template) {
  479. (/** @type {?} */ (this))._previewTemplate = template;
  480. return (/** @type {?} */ (this));
  481. };
  482. /**
  483. * Registers the template that should be used for the drag placeholder.
  484. * @param template Template that from which to stamp out the placeholder.
  485. */
  486. /**
  487. * Registers the template that should be used for the drag placeholder.
  488. * @template THIS
  489. * @this {THIS}
  490. * @param {?} template Template that from which to stamp out the placeholder.
  491. * @return {THIS}
  492. */
  493. DragRef.prototype.withPlaceholderTemplate = /**
  494. * Registers the template that should be used for the drag placeholder.
  495. * @template THIS
  496. * @this {THIS}
  497. * @param {?} template Template that from which to stamp out the placeholder.
  498. * @return {THIS}
  499. */
  500. function (template) {
  501. (/** @type {?} */ (this))._placeholderTemplate = template;
  502. return (/** @type {?} */ (this));
  503. };
  504. /**
  505. * Sets an alternate drag root element. The root element is the element that will be moved as
  506. * the user is dragging. Passing an alternate root element is useful when trying to enable
  507. * dragging on an element that you might not have access to.
  508. */
  509. /**
  510. * Sets an alternate drag root element. The root element is the element that will be moved as
  511. * the user is dragging. Passing an alternate root element is useful when trying to enable
  512. * dragging on an element that you might not have access to.
  513. * @template THIS
  514. * @this {THIS}
  515. * @param {?} rootElement
  516. * @return {THIS}
  517. */
  518. DragRef.prototype.withRootElement = /**
  519. * Sets an alternate drag root element. The root element is the element that will be moved as
  520. * the user is dragging. Passing an alternate root element is useful when trying to enable
  521. * dragging on an element that you might not have access to.
  522. * @template THIS
  523. * @this {THIS}
  524. * @param {?} rootElement
  525. * @return {THIS}
  526. */
  527. function (rootElement) {
  528. /** @type {?} */
  529. var element = coerceElement(rootElement);
  530. if (element !== (/** @type {?} */ (this))._rootElement) {
  531. if ((/** @type {?} */ (this))._rootElement) {
  532. (/** @type {?} */ (this))._removeRootElementListeners((/** @type {?} */ (this))._rootElement);
  533. }
  534. element.addEventListener('mousedown', (/** @type {?} */ (this))._pointerDown, activeEventListenerOptions);
  535. element.addEventListener('touchstart', (/** @type {?} */ (this))._pointerDown, passiveEventListenerOptions);
  536. (/** @type {?} */ (this))._initialTransform = undefined;
  537. (/** @type {?} */ (this))._rootElement = element;
  538. }
  539. return (/** @type {?} */ (this));
  540. };
  541. /**
  542. * Element to which the draggable's position will be constrained.
  543. */
  544. /**
  545. * Element to which the draggable's position will be constrained.
  546. * @template THIS
  547. * @this {THIS}
  548. * @param {?} boundaryElement
  549. * @return {THIS}
  550. */
  551. DragRef.prototype.withBoundaryElement = /**
  552. * Element to which the draggable's position will be constrained.
  553. * @template THIS
  554. * @this {THIS}
  555. * @param {?} boundaryElement
  556. * @return {THIS}
  557. */
  558. function (boundaryElement) {
  559. var _this = this;
  560. (/** @type {?} */ (this))._boundaryElement = boundaryElement ? coerceElement(boundaryElement) : null;
  561. (/** @type {?} */ (this))._resizeSubscription.unsubscribe();
  562. if (boundaryElement) {
  563. (/** @type {?} */ (this))._resizeSubscription = (/** @type {?} */ (this))._viewportRuler
  564. .change(10)
  565. .subscribe((/**
  566. * @return {?}
  567. */
  568. function () { return (/** @type {?} */ (_this))._containInsideBoundaryOnResize(); }));
  569. }
  570. return (/** @type {?} */ (this));
  571. };
  572. /** Removes the dragging functionality from the DOM element. */
  573. /**
  574. * Removes the dragging functionality from the DOM element.
  575. * @return {?}
  576. */
  577. DragRef.prototype.dispose = /**
  578. * Removes the dragging functionality from the DOM element.
  579. * @return {?}
  580. */
  581. function () {
  582. this._removeRootElementListeners(this._rootElement);
  583. // Do this check before removing from the registry since it'll
  584. // stop being considered as dragged once it is removed.
  585. if (this.isDragging()) {
  586. // Since we move out the element to the end of the body while it's being
  587. // dragged, we have to make sure that it's removed if it gets destroyed.
  588. removeElement(this._rootElement);
  589. }
  590. this._destroyPreview();
  591. this._destroyPlaceholder();
  592. this._dragDropRegistry.removeDragItem(this);
  593. this._removeSubscriptions();
  594. this.beforeStarted.complete();
  595. this.started.complete();
  596. this.released.complete();
  597. this.ended.complete();
  598. this.entered.complete();
  599. this.exited.complete();
  600. this.dropped.complete();
  601. this._moveEvents.complete();
  602. this._handles = [];
  603. this._disabledHandles.clear();
  604. this._dropContainer = undefined;
  605. this._boundaryElement = this._rootElement = this._placeholderTemplate =
  606. this._previewTemplate = this._nextSibling = (/** @type {?} */ (null));
  607. };
  608. /** Checks whether the element is currently being dragged. */
  609. /**
  610. * Checks whether the element is currently being dragged.
  611. * @return {?}
  612. */
  613. DragRef.prototype.isDragging = /**
  614. * Checks whether the element is currently being dragged.
  615. * @return {?}
  616. */
  617. function () {
  618. return this._hasStartedDragging && this._dragDropRegistry.isDragging(this);
  619. };
  620. /** Resets a standalone drag item to its initial position. */
  621. /**
  622. * Resets a standalone drag item to its initial position.
  623. * @return {?}
  624. */
  625. DragRef.prototype.reset = /**
  626. * Resets a standalone drag item to its initial position.
  627. * @return {?}
  628. */
  629. function () {
  630. this._rootElement.style.transform = this._initialTransform || '';
  631. this._activeTransform = { x: 0, y: 0 };
  632. this._passiveTransform = { x: 0, y: 0 };
  633. };
  634. /**
  635. * Sets a handle as disabled. While a handle is disabled, it'll capture and interrupt dragging.
  636. * @param handle Handle element that should be disabled.
  637. */
  638. /**
  639. * Sets a handle as disabled. While a handle is disabled, it'll capture and interrupt dragging.
  640. * @param {?} handle Handle element that should be disabled.
  641. * @return {?}
  642. */
  643. DragRef.prototype.disableHandle = /**
  644. * Sets a handle as disabled. While a handle is disabled, it'll capture and interrupt dragging.
  645. * @param {?} handle Handle element that should be disabled.
  646. * @return {?}
  647. */
  648. function (handle) {
  649. if (this._handles.indexOf(handle) > -1) {
  650. this._disabledHandles.add(handle);
  651. }
  652. };
  653. /**
  654. * Enables a handle, if it has been disabled.
  655. * @param handle Handle element to be enabled.
  656. */
  657. /**
  658. * Enables a handle, if it has been disabled.
  659. * @param {?} handle Handle element to be enabled.
  660. * @return {?}
  661. */
  662. DragRef.prototype.enableHandle = /**
  663. * Enables a handle, if it has been disabled.
  664. * @param {?} handle Handle element to be enabled.
  665. * @return {?}
  666. */
  667. function (handle) {
  668. this._disabledHandles.delete(handle);
  669. };
  670. /** Sets the layout direction of the draggable item. */
  671. /**
  672. * Sets the layout direction of the draggable item.
  673. * @template THIS
  674. * @this {THIS}
  675. * @param {?} direction
  676. * @return {THIS}
  677. */
  678. DragRef.prototype.withDirection = /**
  679. * Sets the layout direction of the draggable item.
  680. * @template THIS
  681. * @this {THIS}
  682. * @param {?} direction
  683. * @return {THIS}
  684. */
  685. function (direction) {
  686. (/** @type {?} */ (this))._direction = direction;
  687. return (/** @type {?} */ (this));
  688. };
  689. /** Sets the container that the item is part of. */
  690. /**
  691. * Sets the container that the item is part of.
  692. * @param {?} container
  693. * @return {?}
  694. */
  695. DragRef.prototype._withDropContainer = /**
  696. * Sets the container that the item is part of.
  697. * @param {?} container
  698. * @return {?}
  699. */
  700. function (container) {
  701. this._dropContainer = container;
  702. };
  703. /**
  704. * Gets the current position in pixels the draggable outside of a drop container.
  705. */
  706. /**
  707. * Gets the current position in pixels the draggable outside of a drop container.
  708. * @return {?}
  709. */
  710. DragRef.prototype.getFreeDragPosition = /**
  711. * Gets the current position in pixels the draggable outside of a drop container.
  712. * @return {?}
  713. */
  714. function () {
  715. /** @type {?} */
  716. var position = this.isDragging() ? this._activeTransform : this._passiveTransform;
  717. return { x: position.x, y: position.y };
  718. };
  719. /**
  720. * Sets the current position in pixels the draggable outside of a drop container.
  721. * @param value New position to be set.
  722. */
  723. /**
  724. * Sets the current position in pixels the draggable outside of a drop container.
  725. * @template THIS
  726. * @this {THIS}
  727. * @param {?} value New position to be set.
  728. * @return {THIS}
  729. */
  730. DragRef.prototype.setFreeDragPosition = /**
  731. * Sets the current position in pixels the draggable outside of a drop container.
  732. * @template THIS
  733. * @this {THIS}
  734. * @param {?} value New position to be set.
  735. * @return {THIS}
  736. */
  737. function (value) {
  738. (/** @type {?} */ (this))._activeTransform = { x: 0, y: 0 };
  739. (/** @type {?} */ (this))._passiveTransform.x = value.x;
  740. (/** @type {?} */ (this))._passiveTransform.y = value.y;
  741. if (!(/** @type {?} */ (this))._dropContainer) {
  742. (/** @type {?} */ (this))._applyRootElementTransform(value.x, value.y);
  743. }
  744. return (/** @type {?} */ (this));
  745. };
  746. /** Updates the item's sort order based on the last-known pointer position. */
  747. /**
  748. * Updates the item's sort order based on the last-known pointer position.
  749. * @return {?}
  750. */
  751. DragRef.prototype._sortFromLastPointerPosition = /**
  752. * Updates the item's sort order based on the last-known pointer position.
  753. * @return {?}
  754. */
  755. function () {
  756. /** @type {?} */
  757. var position = this._pointerPositionAtLastDirectionChange;
  758. if (position && this._dropContainer) {
  759. this._updateActiveDropContainer(position);
  760. }
  761. };
  762. /** Unsubscribes from the global subscriptions. */
  763. /**
  764. * Unsubscribes from the global subscriptions.
  765. * @private
  766. * @return {?}
  767. */
  768. DragRef.prototype._removeSubscriptions = /**
  769. * Unsubscribes from the global subscriptions.
  770. * @private
  771. * @return {?}
  772. */
  773. function () {
  774. this._pointerMoveSubscription.unsubscribe();
  775. this._pointerUpSubscription.unsubscribe();
  776. this._scrollSubscription.unsubscribe();
  777. };
  778. /** Destroys the preview element and its ViewRef. */
  779. /**
  780. * Destroys the preview element and its ViewRef.
  781. * @private
  782. * @return {?}
  783. */
  784. DragRef.prototype._destroyPreview = /**
  785. * Destroys the preview element and its ViewRef.
  786. * @private
  787. * @return {?}
  788. */
  789. function () {
  790. if (this._preview) {
  791. removeElement(this._preview);
  792. }
  793. if (this._previewRef) {
  794. this._previewRef.destroy();
  795. }
  796. this._preview = this._previewRef = (/** @type {?} */ (null));
  797. };
  798. /** Destroys the placeholder element and its ViewRef. */
  799. /**
  800. * Destroys the placeholder element and its ViewRef.
  801. * @private
  802. * @return {?}
  803. */
  804. DragRef.prototype._destroyPlaceholder = /**
  805. * Destroys the placeholder element and its ViewRef.
  806. * @private
  807. * @return {?}
  808. */
  809. function () {
  810. if (this._placeholder) {
  811. removeElement(this._placeholder);
  812. }
  813. if (this._placeholderRef) {
  814. this._placeholderRef.destroy();
  815. }
  816. this._placeholder = this._placeholderRef = (/** @type {?} */ (null));
  817. };
  818. /**
  819. * Clears subscriptions and stops the dragging sequence.
  820. * @param event Browser event object that ended the sequence.
  821. */
  822. /**
  823. * Clears subscriptions and stops the dragging sequence.
  824. * @private
  825. * @param {?} event Browser event object that ended the sequence.
  826. * @return {?}
  827. */
  828. DragRef.prototype._endDragSequence = /**
  829. * Clears subscriptions and stops the dragging sequence.
  830. * @private
  831. * @param {?} event Browser event object that ended the sequence.
  832. * @return {?}
  833. */
  834. function (event) {
  835. var _this = this;
  836. // Note that here we use `isDragging` from the service, rather than from `this`.
  837. // The difference is that the one from the service reflects whether a dragging sequence
  838. // has been initiated, whereas the one on `this` includes whether the user has passed
  839. // the minimum dragging threshold.
  840. if (!this._dragDropRegistry.isDragging(this)) {
  841. return;
  842. }
  843. this._removeSubscriptions();
  844. this._dragDropRegistry.stopDragging(this);
  845. this._toggleNativeDragInteractions();
  846. if (this._handles) {
  847. this._rootElement.style.webkitTapHighlightColor = this._rootElementTapHighlight;
  848. }
  849. if (!this._hasStartedDragging) {
  850. return;
  851. }
  852. this.released.next({ source: this });
  853. if (this._dropContainer) {
  854. // Stop scrolling immediately, instead of waiting for the animation to finish.
  855. this._dropContainer._stopScrolling();
  856. this._animatePreviewToPlaceholder().then((/**
  857. * @return {?}
  858. */
  859. function () {
  860. _this._cleanupDragArtifacts(event);
  861. _this._cleanupCachedDimensions();
  862. _this._dragDropRegistry.stopDragging(_this);
  863. }));
  864. }
  865. else {
  866. // Convert the active transform into a passive one. This means that next time
  867. // the user starts dragging the item, its position will be calculated relatively
  868. // to the new passive transform.
  869. this._passiveTransform.x = this._activeTransform.x;
  870. this._passiveTransform.y = this._activeTransform.y;
  871. this._ngZone.run((/**
  872. * @return {?}
  873. */
  874. function () {
  875. _this.ended.next({
  876. source: _this,
  877. distance: _this._getDragDistance(_this._getPointerPositionOnPage(event))
  878. });
  879. }));
  880. this._cleanupCachedDimensions();
  881. this._dragDropRegistry.stopDragging(this);
  882. }
  883. };
  884. /** Starts the dragging sequence. */
  885. /**
  886. * Starts the dragging sequence.
  887. * @private
  888. * @param {?} event
  889. * @return {?}
  890. */
  891. DragRef.prototype._startDragSequence = /**
  892. * Starts the dragging sequence.
  893. * @private
  894. * @param {?} event
  895. * @return {?}
  896. */
  897. function (event) {
  898. // Emit the event on the item before the one on the container.
  899. this.started.next({ source: this });
  900. if (isTouchEvent(event)) {
  901. this._lastTouchEventTime = Date.now();
  902. }
  903. this._toggleNativeDragInteractions();
  904. if (this._dropContainer) {
  905. /** @type {?} */
  906. var element = this._rootElement;
  907. // Grab the `nextSibling` before the preview and placeholder
  908. // have been created so we don't get the preview by accident.
  909. this._nextSibling = element.nextSibling;
  910. /** @type {?} */
  911. var preview = this._preview = this._createPreviewElement();
  912. /** @type {?} */
  913. var placeholder = this._placeholder = this._createPlaceholderElement();
  914. // We move the element out at the end of the body and we make it hidden, because keeping it in
  915. // place will throw off the consumer's `:last-child` selectors. We can't remove the element
  916. // from the DOM completely, because iOS will stop firing all subsequent events in the chain.
  917. element.style.display = 'none';
  918. this._document.body.appendChild((/** @type {?} */ (element.parentNode)).replaceChild(placeholder, element));
  919. getPreviewInsertionPoint(this._document).appendChild(preview);
  920. this._dropContainer.start();
  921. }
  922. };
  923. /**
  924. * Sets up the different variables and subscriptions
  925. * that will be necessary for the dragging sequence.
  926. * @param referenceElement Element that started the drag sequence.
  927. * @param event Browser event object that started the sequence.
  928. */
  929. /**
  930. * Sets up the different variables and subscriptions
  931. * that will be necessary for the dragging sequence.
  932. * @private
  933. * @param {?} referenceElement Element that started the drag sequence.
  934. * @param {?} event Browser event object that started the sequence.
  935. * @return {?}
  936. */
  937. DragRef.prototype._initializeDragSequence = /**
  938. * Sets up the different variables and subscriptions
  939. * that will be necessary for the dragging sequence.
  940. * @private
  941. * @param {?} referenceElement Element that started the drag sequence.
  942. * @param {?} event Browser event object that started the sequence.
  943. * @return {?}
  944. */
  945. function (referenceElement, event) {
  946. var _this = this;
  947. // Always stop propagation for the event that initializes
  948. // the dragging sequence, in order to prevent it from potentially
  949. // starting another sequence for a draggable parent somewhere up the DOM tree.
  950. event.stopPropagation();
  951. /** @type {?} */
  952. var isDragging = this.isDragging();
  953. /** @type {?} */
  954. var isTouchSequence = isTouchEvent(event);
  955. /** @type {?} */
  956. var isAuxiliaryMouseButton = !isTouchSequence && ((/** @type {?} */ (event))).button !== 0;
  957. /** @type {?} */
  958. var rootElement = this._rootElement;
  959. /** @type {?} */
  960. var isSyntheticEvent = !isTouchSequence && this._lastTouchEventTime &&
  961. this._lastTouchEventTime + MOUSE_EVENT_IGNORE_TIME > Date.now();
  962. // If the event started from an element with the native HTML drag&drop, it'll interfere
  963. // with our own dragging (e.g. `img` tags do it by default). Prevent the default action
  964. // to stop it from happening. Note that preventing on `dragstart` also seems to work, but
  965. // it's flaky and it fails if the user drags it away quickly. Also note that we only want
  966. // to do this for `mousedown` since doing the same for `touchstart` will stop any `click`
  967. // events from firing on touch devices.
  968. if (event.target && ((/** @type {?} */ (event.target))).draggable && event.type === 'mousedown') {
  969. event.preventDefault();
  970. }
  971. // Abort if the user is already dragging or is using a mouse button other than the primary one.
  972. if (isDragging || isAuxiliaryMouseButton || isSyntheticEvent) {
  973. return;
  974. }
  975. // If we've got handles, we need to disable the tap highlight on the entire root element,
  976. // otherwise iOS will still add it, even though all the drag interactions on the handle
  977. // are disabled.
  978. if (this._handles.length) {
  979. this._rootElementTapHighlight = rootElement.style.webkitTapHighlightColor;
  980. rootElement.style.webkitTapHighlightColor = 'transparent';
  981. }
  982. this._hasStartedDragging = this._hasMoved = false;
  983. this._initialContainer = (/** @type {?} */ (this._dropContainer));
  984. // Avoid multiple subscriptions and memory leaks when multi touch
  985. // (isDragging check above isn't enough because of possible temporal and/or dimensional delays)
  986. this._removeSubscriptions();
  987. this._pointerMoveSubscription = this._dragDropRegistry.pointerMove.subscribe(this._pointerMove);
  988. this._pointerUpSubscription = this._dragDropRegistry.pointerUp.subscribe(this._pointerUp);
  989. this._scrollSubscription = this._dragDropRegistry.scroll.pipe(startWith(null)).subscribe((/**
  990. * @return {?}
  991. */
  992. function () {
  993. _this._scrollPosition = _this._viewportRuler.getViewportScrollPosition();
  994. }));
  995. if (this._boundaryElement) {
  996. this._boundaryRect = this._boundaryElement.getBoundingClientRect();
  997. }
  998. // If we have a custom preview template, the element won't be visible anyway so we avoid the
  999. // extra `getBoundingClientRect` calls and just move the preview next to the cursor.
  1000. this._pickupPositionInElement = this._previewTemplate && this._previewTemplate.template ?
  1001. { x: 0, y: 0 } :
  1002. this._getPointerPositionInElement(referenceElement, event);
  1003. /** @type {?} */
  1004. var pointerPosition = this._pickupPositionOnPage = this._getPointerPositionOnPage(event);
  1005. this._pointerDirectionDelta = { x: 0, y: 0 };
  1006. this._pointerPositionAtLastDirectionChange = { x: pointerPosition.x, y: pointerPosition.y };
  1007. this._dragStartTime = Date.now();
  1008. this._dragDropRegistry.startDragging(this, event);
  1009. };
  1010. /** Cleans up the DOM artifacts that were added to facilitate the element being dragged. */
  1011. /**
  1012. * Cleans up the DOM artifacts that were added to facilitate the element being dragged.
  1013. * @private
  1014. * @param {?} event
  1015. * @return {?}
  1016. */
  1017. DragRef.prototype._cleanupDragArtifacts = /**
  1018. * Cleans up the DOM artifacts that were added to facilitate the element being dragged.
  1019. * @private
  1020. * @param {?} event
  1021. * @return {?}
  1022. */
  1023. function (event) {
  1024. var _this = this;
  1025. // Restore the element's visibility and insert it at its old position in the DOM.
  1026. // It's important that we maintain the position, because moving the element around in the DOM
  1027. // can throw off `NgFor` which does smart diffing and re-creates elements only when necessary,
  1028. // while moving the existing elements in all other cases.
  1029. this._rootElement.style.display = '';
  1030. if (this._nextSibling) {
  1031. (/** @type {?} */ (this._nextSibling.parentNode)).insertBefore(this._rootElement, this._nextSibling);
  1032. }
  1033. else {
  1034. coerceElement(this._initialContainer.element).appendChild(this._rootElement);
  1035. }
  1036. this._destroyPreview();
  1037. this._destroyPlaceholder();
  1038. this._boundaryRect = this._previewRect = undefined;
  1039. // Re-enter the NgZone since we bound `document` events on the outside.
  1040. this._ngZone.run((/**
  1041. * @return {?}
  1042. */
  1043. function () {
  1044. /** @type {?} */
  1045. var container = (/** @type {?} */ (_this._dropContainer));
  1046. /** @type {?} */
  1047. var currentIndex = container.getItemIndex(_this);
  1048. /** @type {?} */
  1049. var pointerPosition = _this._getPointerPositionOnPage(event);
  1050. /** @type {?} */
  1051. var distance = _this._getDragDistance(_this._getPointerPositionOnPage(event));
  1052. /** @type {?} */
  1053. var isPointerOverContainer = container._isOverContainer(pointerPosition.x, pointerPosition.y);
  1054. _this.ended.next({ source: _this, distance: distance });
  1055. _this.dropped.next({
  1056. item: _this,
  1057. currentIndex: currentIndex,
  1058. previousIndex: _this._initialContainer.getItemIndex(_this),
  1059. container: container,
  1060. previousContainer: _this._initialContainer,
  1061. isPointerOverContainer: isPointerOverContainer,
  1062. distance: distance
  1063. });
  1064. container.drop(_this, currentIndex, _this._initialContainer, isPointerOverContainer, distance);
  1065. _this._dropContainer = _this._initialContainer;
  1066. }));
  1067. };
  1068. /**
  1069. * Updates the item's position in its drop container, or moves it
  1070. * into a new one, depending on its current drag position.
  1071. */
  1072. /**
  1073. * Updates the item's position in its drop container, or moves it
  1074. * into a new one, depending on its current drag position.
  1075. * @private
  1076. * @param {?} __0
  1077. * @return {?}
  1078. */
  1079. DragRef.prototype._updateActiveDropContainer = /**
  1080. * Updates the item's position in its drop container, or moves it
  1081. * into a new one, depending on its current drag position.
  1082. * @private
  1083. * @param {?} __0
  1084. * @return {?}
  1085. */
  1086. function (_a) {
  1087. var _this = this;
  1088. var x = _a.x, y = _a.y;
  1089. // Drop container that draggable has been moved into.
  1090. /** @type {?} */
  1091. var newContainer = this._initialContainer._getSiblingContainerFromPosition(this, x, y);
  1092. // If we couldn't find a new container to move the item into, and the item has left its
  1093. // initial container, check whether the it's over the initial container. This handles the
  1094. // case where two containers are connected one way and the user tries to undo dragging an
  1095. // item into a new container.
  1096. if (!newContainer && this._dropContainer !== this._initialContainer &&
  1097. this._initialContainer._isOverContainer(x, y)) {
  1098. newContainer = this._initialContainer;
  1099. }
  1100. if (newContainer && newContainer !== this._dropContainer) {
  1101. this._ngZone.run((/**
  1102. * @return {?}
  1103. */
  1104. function () {
  1105. // Notify the old container that the item has left.
  1106. _this.exited.next({ item: _this, container: (/** @type {?} */ (_this._dropContainer)) });
  1107. (/** @type {?} */ (_this._dropContainer)).exit(_this);
  1108. // Notify the new container that the item has entered.
  1109. _this._dropContainer = (/** @type {?} */ (newContainer));
  1110. _this._dropContainer.enter(_this, x, y);
  1111. _this.entered.next({
  1112. item: _this,
  1113. container: (/** @type {?} */ (newContainer)),
  1114. currentIndex: (/** @type {?} */ (newContainer)).getItemIndex(_this)
  1115. });
  1116. }));
  1117. }
  1118. (/** @type {?} */ (this._dropContainer))._startScrollingIfNecessary(x, y);
  1119. (/** @type {?} */ (this._dropContainer))._sortItem(this, x, y, this._pointerDirectionDelta);
  1120. this._preview.style.transform =
  1121. getTransform(x - this._pickupPositionInElement.x, y - this._pickupPositionInElement.y);
  1122. };
  1123. /**
  1124. * Creates the element that will be rendered next to the user's pointer
  1125. * and will be used as a preview of the element that is being dragged.
  1126. */
  1127. /**
  1128. * Creates the element that will be rendered next to the user's pointer
  1129. * and will be used as a preview of the element that is being dragged.
  1130. * @private
  1131. * @return {?}
  1132. */
  1133. DragRef.prototype._createPreviewElement = /**
  1134. * Creates the element that will be rendered next to the user's pointer
  1135. * and will be used as a preview of the element that is being dragged.
  1136. * @private
  1137. * @return {?}
  1138. */
  1139. function () {
  1140. /** @type {?} */
  1141. var previewConfig = this._previewTemplate;
  1142. /** @type {?} */
  1143. var previewTemplate = previewConfig ? previewConfig.template : null;
  1144. /** @type {?} */
  1145. var preview;
  1146. if (previewTemplate) {
  1147. /** @type {?} */
  1148. var viewRef = (/** @type {?} */ (previewConfig)).viewContainer.createEmbeddedView(previewTemplate, (/** @type {?} */ (previewConfig)).context);
  1149. preview = getRootNode(viewRef, this._document);
  1150. this._previewRef = viewRef;
  1151. preview.style.transform =
  1152. getTransform(this._pickupPositionOnPage.x, this._pickupPositionOnPage.y);
  1153. }
  1154. else {
  1155. /** @type {?} */
  1156. var element = this._rootElement;
  1157. /** @type {?} */
  1158. var elementRect = element.getBoundingClientRect();
  1159. preview = deepCloneNode(element);
  1160. preview.style.width = elementRect.width + "px";
  1161. preview.style.height = elementRect.height + "px";
  1162. preview.style.transform = getTransform(elementRect.left, elementRect.top);
  1163. }
  1164. extendStyles(preview.style, {
  1165. // It's important that we disable the pointer events on the preview, because
  1166. // it can throw off the `document.elementFromPoint` calls in the `CdkDropList`.
  1167. pointerEvents: 'none',
  1168. // We have to reset the margin, because can throw off positioning relative to the viewport.
  1169. margin: '0',
  1170. position: 'fixed',
  1171. top: '0',
  1172. left: '0',
  1173. zIndex: '1000'
  1174. });
  1175. toggleNativeDragInteractions(preview, false);
  1176. preview.classList.add('cdk-drag-preview');
  1177. preview.setAttribute('dir', this._direction);
  1178. return preview;
  1179. };
  1180. /**
  1181. * Animates the preview element from its current position to the location of the drop placeholder.
  1182. * @returns Promise that resolves when the animation completes.
  1183. */
  1184. /**
  1185. * Animates the preview element from its current position to the location of the drop placeholder.
  1186. * @private
  1187. * @return {?} Promise that resolves when the animation completes.
  1188. */
  1189. DragRef.prototype._animatePreviewToPlaceholder = /**
  1190. * Animates the preview element from its current position to the location of the drop placeholder.
  1191. * @private
  1192. * @return {?} Promise that resolves when the animation completes.
  1193. */
  1194. function () {
  1195. var _this = this;
  1196. // If the user hasn't moved yet, the transitionend event won't fire.
  1197. if (!this._hasMoved) {
  1198. return Promise.resolve();
  1199. }
  1200. /** @type {?} */
  1201. var placeholderRect = this._placeholder.getBoundingClientRect();
  1202. // Apply the class that adds a transition to the preview.
  1203. this._preview.classList.add('cdk-drag-animating');
  1204. // Move the preview to the placeholder position.
  1205. this._preview.style.transform = getTransform(placeholderRect.left, placeholderRect.top);
  1206. // If the element doesn't have a `transition`, the `transitionend` event won't fire. Since
  1207. // we need to trigger a style recalculation in order for the `cdk-drag-animating` class to
  1208. // apply its style, we take advantage of the available info to figure out whether we need to
  1209. // bind the event in the first place.
  1210. /** @type {?} */
  1211. var duration = getTransformTransitionDurationInMs(this._preview);
  1212. if (duration === 0) {
  1213. return Promise.resolve();
  1214. }
  1215. return this._ngZone.runOutsideAngular((/**
  1216. * @return {?}
  1217. */
  1218. function () {
  1219. return new Promise((/**
  1220. * @param {?} resolve
  1221. * @return {?}
  1222. */
  1223. function (resolve) {
  1224. /** @type {?} */
  1225. var handler = (/** @type {?} */ (((/**
  1226. * @param {?} event
  1227. * @return {?}
  1228. */
  1229. function (event) {
  1230. if (!event || (event.target === _this._preview && event.propertyName === 'transform')) {
  1231. _this._preview.removeEventListener('transitionend', handler);
  1232. resolve();
  1233. clearTimeout(timeout);
  1234. }
  1235. }))));
  1236. // If a transition is short enough, the browser might not fire the `transitionend` event.
  1237. // Since we know how long it's supposed to take, add a timeout with a 50% buffer that'll
  1238. // fire if the transition hasn't completed when it was supposed to.
  1239. /** @type {?} */
  1240. var timeout = setTimeout((/** @type {?} */ (handler)), duration * 1.5);
  1241. _this._preview.addEventListener('transitionend', handler);
  1242. }));
  1243. }));
  1244. };
  1245. /** Creates an element that will be shown instead of the current element while dragging. */
  1246. /**
  1247. * Creates an element that will be shown instead of the current element while dragging.
  1248. * @private
  1249. * @return {?}
  1250. */
  1251. DragRef.prototype._createPlaceholderElement = /**
  1252. * Creates an element that will be shown instead of the current element while dragging.
  1253. * @private
  1254. * @return {?}
  1255. */
  1256. function () {
  1257. /** @type {?} */
  1258. var placeholderConfig = this._placeholderTemplate;
  1259. /** @type {?} */
  1260. var placeholderTemplate = placeholderConfig ? placeholderConfig.template : null;
  1261. /** @type {?} */
  1262. var placeholder;
  1263. if (placeholderTemplate) {
  1264. this._placeholderRef = (/** @type {?} */ (placeholderConfig)).viewContainer.createEmbeddedView(placeholderTemplate, (/** @type {?} */ (placeholderConfig)).context);
  1265. placeholder = getRootNode(this._placeholderRef, this._document);
  1266. }
  1267. else {
  1268. placeholder = deepCloneNode(this._rootElement);
  1269. }
  1270. placeholder.classList.add('cdk-drag-placeholder');
  1271. return placeholder;
  1272. };
  1273. /**
  1274. * Figures out the coordinates at which an element was picked up.
  1275. * @param referenceElement Element that initiated the dragging.
  1276. * @param event Event that initiated the dragging.
  1277. */
  1278. /**
  1279. * Figures out the coordinates at which an element was picked up.
  1280. * @private
  1281. * @param {?} referenceElement Element that initiated the dragging.
  1282. * @param {?} event Event that initiated the dragging.
  1283. * @return {?}
  1284. */
  1285. DragRef.prototype._getPointerPositionInElement = /**
  1286. * Figures out the coordinates at which an element was picked up.
  1287. * @private
  1288. * @param {?} referenceElement Element that initiated the dragging.
  1289. * @param {?} event Event that initiated the dragging.
  1290. * @return {?}
  1291. */
  1292. function (referenceElement, event) {
  1293. /** @type {?} */
  1294. var elementRect = this._rootElement.getBoundingClientRect();
  1295. /** @type {?} */
  1296. var handleElement = referenceElement === this._rootElement ? null : referenceElement;
  1297. /** @type {?} */
  1298. var referenceRect = handleElement ? handleElement.getBoundingClientRect() : elementRect;
  1299. /** @type {?} */
  1300. var point = isTouchEvent(event) ? event.targetTouches[0] : event;
  1301. /** @type {?} */
  1302. var x = point.pageX - referenceRect.left - this._scrollPosition.left;
  1303. /** @type {?} */
  1304. var y = point.pageY - referenceRect.top - this._scrollPosition.top;
  1305. return {
  1306. x: referenceRect.left - elementRect.left + x,
  1307. y: referenceRect.top - elementRect.top + y
  1308. };
  1309. };
  1310. /** Determines the point of the page that was touched by the user. */
  1311. /**
  1312. * Determines the point of the page that was touched by the user.
  1313. * @private
  1314. * @param {?} event
  1315. * @return {?}
  1316. */
  1317. DragRef.prototype._getPointerPositionOnPage = /**
  1318. * Determines the point of the page that was touched by the user.
  1319. * @private
  1320. * @param {?} event
  1321. * @return {?}
  1322. */
  1323. function (event) {
  1324. // `touches` will be empty for start/end events so we have to fall back to `changedTouches`.
  1325. /** @type {?} */
  1326. var point = isTouchEvent(event) ? (event.touches[0] || event.changedTouches[0]) : event;
  1327. return {
  1328. x: point.pageX - this._scrollPosition.left,
  1329. y: point.pageY - this._scrollPosition.top
  1330. };
  1331. };
  1332. /** Gets the pointer position on the page, accounting for any position constraints. */
  1333. /**
  1334. * Gets the pointer position on the page, accounting for any position constraints.
  1335. * @private
  1336. * @param {?} event
  1337. * @return {?}
  1338. */
  1339. DragRef.prototype._getConstrainedPointerPosition = /**
  1340. * Gets the pointer position on the page, accounting for any position constraints.
  1341. * @private
  1342. * @param {?} event
  1343. * @return {?}
  1344. */
  1345. function (event) {
  1346. /** @type {?} */
  1347. var point = this._getPointerPositionOnPage(event);
  1348. /** @type {?} */
  1349. var constrainedPoint = this.constrainPosition ? this.constrainPosition(point, this) : point;
  1350. /** @type {?} */
  1351. var dropContainerLock = this._dropContainer ? this._dropContainer.lockAxis : null;
  1352. if (this.lockAxis === 'x' || dropContainerLock === 'x') {
  1353. constrainedPoint.y = this._pickupPositionOnPage.y;
  1354. }
  1355. else if (this.lockAxis === 'y' || dropContainerLock === 'y') {
  1356. constrainedPoint.x = this._pickupPositionOnPage.x;
  1357. }
  1358. if (this._boundaryRect) {
  1359. var _a = this._pickupPositionInElement, pickupX = _a.x, pickupY = _a.y;
  1360. /** @type {?} */
  1361. var boundaryRect = this._boundaryRect;
  1362. /** @type {?} */
  1363. var previewRect = (/** @type {?} */ (this._previewRect));
  1364. /** @type {?} */
  1365. var minY = boundaryRect.top + pickupY;
  1366. /** @type {?} */
  1367. var maxY = boundaryRect.bottom - (previewRect.height - pickupY);
  1368. /** @type {?} */
  1369. var minX = boundaryRect.left + pickupX;
  1370. /** @type {?} */
  1371. var maxX = boundaryRect.right - (previewRect.width - pickupX);
  1372. constrainedPoint.x = clamp(constrainedPoint.x, minX, maxX);
  1373. constrainedPoint.y = clamp(constrainedPoint.y, minY, maxY);
  1374. }
  1375. return constrainedPoint;
  1376. };
  1377. /** Updates the current drag delta, based on the user's current pointer position on the page. */
  1378. /**
  1379. * Updates the current drag delta, based on the user's current pointer position on the page.
  1380. * @private
  1381. * @param {?} pointerPositionOnPage
  1382. * @return {?}
  1383. */
  1384. DragRef.prototype._updatePointerDirectionDelta = /**
  1385. * Updates the current drag delta, based on the user's current pointer position on the page.
  1386. * @private
  1387. * @param {?} pointerPositionOnPage
  1388. * @return {?}
  1389. */
  1390. function (pointerPositionOnPage) {
  1391. var x = pointerPositionOnPage.x, y = pointerPositionOnPage.y;
  1392. /** @type {?} */
  1393. var delta = this._pointerDirectionDelta;
  1394. /** @type {?} */
  1395. var positionSinceLastChange = this._pointerPositionAtLastDirectionChange;
  1396. // Amount of pixels the user has dragged since the last time the direction changed.
  1397. /** @type {?} */
  1398. var changeX = Math.abs(x - positionSinceLastChange.x);
  1399. /** @type {?} */
  1400. var changeY = Math.abs(y - positionSinceLastChange.y);
  1401. // Because we handle pointer events on a per-pixel basis, we don't want the delta
  1402. // to change for every pixel, otherwise anything that depends on it can look erratic.
  1403. // To make the delta more consistent, we track how much the user has moved since the last
  1404. // delta change and we only update it after it has reached a certain threshold.
  1405. if (changeX > this._config.pointerDirectionChangeThreshold) {
  1406. delta.x = x > positionSinceLastChange.x ? 1 : -1;
  1407. positionSinceLastChange.x = x;
  1408. }
  1409. if (changeY > this._config.pointerDirectionChangeThreshold) {
  1410. delta.y = y > positionSinceLastChange.y ? 1 : -1;
  1411. positionSinceLastChange.y = y;
  1412. }
  1413. return delta;
  1414. };
  1415. /** Toggles the native drag interactions, based on how many handles are registered. */
  1416. /**
  1417. * Toggles the native drag interactions, based on how many handles are registered.
  1418. * @private
  1419. * @return {?}
  1420. */
  1421. DragRef.prototype._toggleNativeDragInteractions = /**
  1422. * Toggles the native drag interactions, based on how many handles are registered.
  1423. * @private
  1424. * @return {?}
  1425. */
  1426. function () {
  1427. if (!this._rootElement || !this._handles) {
  1428. return;
  1429. }
  1430. /** @type {?} */
  1431. var shouldEnable = this._handles.length > 0 || !this.isDragging();
  1432. if (shouldEnable !== this._nativeInteractionsEnabled) {
  1433. this._nativeInteractionsEnabled = shouldEnable;
  1434. toggleNativeDragInteractions(this._rootElement, shouldEnable);
  1435. }
  1436. };
  1437. /** Removes the manually-added event listeners from the root element. */
  1438. /**
  1439. * Removes the manually-added event listeners from the root element.
  1440. * @private
  1441. * @param {?} element
  1442. * @return {?}
  1443. */
  1444. DragRef.prototype._removeRootElementListeners = /**
  1445. * Removes the manually-added event listeners from the root element.
  1446. * @private
  1447. * @param {?} element
  1448. * @return {?}
  1449. */
  1450. function (element) {
  1451. element.removeEventListener('mousedown', this._pointerDown, activeEventListenerOptions);
  1452. element.removeEventListener('touchstart', this._pointerDown, passiveEventListenerOptions);
  1453. };
  1454. /**
  1455. * Applies a `transform` to the root element, taking into account any existing transforms on it.
  1456. * @param x New transform value along the X axis.
  1457. * @param y New transform value along the Y axis.
  1458. */
  1459. /**
  1460. * Applies a `transform` to the root element, taking into account any existing transforms on it.
  1461. * @private
  1462. * @param {?} x New transform value along the X axis.
  1463. * @param {?} y New transform value along the Y axis.
  1464. * @return {?}
  1465. */
  1466. DragRef.prototype._applyRootElementTransform = /**
  1467. * Applies a `transform` to the root element, taking into account any existing transforms on it.
  1468. * @private
  1469. * @param {?} x New transform value along the X axis.
  1470. * @param {?} y New transform value along the Y axis.
  1471. * @return {?}
  1472. */
  1473. function (x, y) {
  1474. /** @type {?} */
  1475. var transform = getTransform(x, y);
  1476. // Cache the previous transform amount only after the first drag sequence, because
  1477. // we don't want our own transforms to stack on top of each other.
  1478. if (this._initialTransform == null) {
  1479. this._initialTransform = this._rootElement.style.transform || '';
  1480. }
  1481. // Preserve the previous `transform` value, if there was one. Note that we apply our own
  1482. // transform before the user's, because things like rotation can affect which direction
  1483. // the element will be translated towards.
  1484. this._rootElement.style.transform = this._initialTransform ?
  1485. transform + ' ' + this._initialTransform : transform;
  1486. };
  1487. /**
  1488. * Gets the distance that the user has dragged during the current drag sequence.
  1489. * @param currentPosition Current position of the user's pointer.
  1490. */
  1491. /**
  1492. * Gets the distance that the user has dragged during the current drag sequence.
  1493. * @private
  1494. * @param {?} currentPosition Current position of the user's pointer.
  1495. * @return {?}
  1496. */
  1497. DragRef.prototype._getDragDistance = /**
  1498. * Gets the distance that the user has dragged during the current drag sequence.
  1499. * @private
  1500. * @param {?} currentPosition Current position of the user's pointer.
  1501. * @return {?}
  1502. */
  1503. function (currentPosition) {
  1504. /** @type {?} */
  1505. var pickupPosition = this._pickupPositionOnPage;
  1506. if (pickupPosition) {
  1507. return { x: currentPosition.x - pickupPosition.x, y: currentPosition.y - pickupPosition.y };
  1508. }
  1509. return { x: 0, y: 0 };
  1510. };
  1511. /** Cleans up any cached element dimensions that we don't need after dragging has stopped. */
  1512. /**
  1513. * Cleans up any cached element dimensions that we don't need after dragging has stopped.
  1514. * @private
  1515. * @return {?}
  1516. */
  1517. DragRef.prototype._cleanupCachedDimensions = /**
  1518. * Cleans up any cached element dimensions that we don't need after dragging has stopped.
  1519. * @private
  1520. * @return {?}
  1521. */
  1522. function () {
  1523. this._boundaryRect = this._previewRect = undefined;
  1524. };
  1525. /**
  1526. * Checks whether the element is still inside its boundary after the viewport has been resized.
  1527. * If not, the position is adjusted so that the element fits again.
  1528. */
  1529. /**
  1530. * Checks whether the element is still inside its boundary after the viewport has been resized.
  1531. * If not, the position is adjusted so that the element fits again.
  1532. * @private
  1533. * @return {?}
  1534. */
  1535. DragRef.prototype._containInsideBoundaryOnResize = /**
  1536. * Checks whether the element is still inside its boundary after the viewport has been resized.
  1537. * If not, the position is adjusted so that the element fits again.
  1538. * @private
  1539. * @return {?}
  1540. */
  1541. function () {
  1542. var _a = this._passiveTransform, x = _a.x, y = _a.y;
  1543. if ((x === 0 && y === 0) || this.isDragging() || !this._boundaryElement) {
  1544. return;
  1545. }
  1546. /** @type {?} */
  1547. var boundaryRect = this._boundaryElement.getBoundingClientRect();
  1548. /** @type {?} */
  1549. var elementRect = this._rootElement.getBoundingClientRect();
  1550. /** @type {?} */
  1551. var leftOverflow = boundaryRect.left - elementRect.left;
  1552. /** @type {?} */
  1553. var rightOverflow = elementRect.right - boundaryRect.right;
  1554. /** @type {?} */
  1555. var topOverflow = boundaryRect.top - elementRect.top;
  1556. /** @type {?} */
  1557. var bottomOverflow = elementRect.bottom - boundaryRect.bottom;
  1558. // If the element has become wider than the boundary, we can't
  1559. // do much to make it fit so we just anchor it to the left.
  1560. if (boundaryRect.width > elementRect.width) {
  1561. if (leftOverflow > 0) {
  1562. x += leftOverflow;
  1563. }
  1564. if (rightOverflow > 0) {
  1565. x -= rightOverflow;
  1566. }
  1567. }
  1568. else {
  1569. x = 0;
  1570. }
  1571. // If the element has become taller than the boundary, we can't
  1572. // do much to make it fit so we just anchor it to the top.
  1573. if (boundaryRect.height > elementRect.height) {
  1574. if (topOverflow > 0) {
  1575. y += topOverflow;
  1576. }
  1577. if (bottomOverflow > 0) {
  1578. y -= bottomOverflow;
  1579. }
  1580. }
  1581. else {
  1582. y = 0;
  1583. }
  1584. if (x !== this._passiveTransform.x || y !== this._passiveTransform.y) {
  1585. this.setFreeDragPosition({ y: y, x: x });
  1586. }
  1587. };
  1588. return DragRef;
  1589. }());
  1590. /**
  1591. * Gets a 3d `transform` that can be applied to an element.
  1592. * @param {?} x Desired position of the element along the X axis.
  1593. * @param {?} y Desired position of the element along the Y axis.
  1594. * @return {?}
  1595. */
  1596. function getTransform(x, y) {
  1597. // Round the transforms since some browsers will
  1598. // blur the elements for sub-pixel transforms.
  1599. return "translate3d(" + Math.round(x) + "px, " + Math.round(y) + "px, 0)";
  1600. }
  1601. /**
  1602. * Creates a deep clone of an element.
  1603. * @param {?} node
  1604. * @return {?}
  1605. */
  1606. function deepCloneNode(node) {
  1607. /** @type {?} */
  1608. var clone = (/** @type {?} */ (node.cloneNode(true)));
  1609. /** @type {?} */
  1610. var descendantsWithId = clone.querySelectorAll('[id]');
  1611. /** @type {?} */
  1612. var descendantCanvases = node.querySelectorAll('canvas');
  1613. // Remove the `id` to avoid having multiple elements with the same id on the page.
  1614. clone.removeAttribute('id');
  1615. for (var i = 0; i < descendantsWithId.length; i++) {
  1616. descendantsWithId[i].removeAttribute('id');
  1617. }
  1618. // `cloneNode` won't transfer the content of `canvas` elements so we have to do it ourselves.
  1619. // We match up the cloned canvas to their sources using their index in the DOM.
  1620. if (descendantCanvases.length) {
  1621. /** @type {?} */
  1622. var cloneCanvases = clone.querySelectorAll('canvas');
  1623. for (var i = 0; i < descendantCanvases.length; i++) {
  1624. /** @type {?} */
  1625. var correspondingCloneContext = cloneCanvases[i].getContext('2d');
  1626. if (correspondingCloneContext) {
  1627. correspondingCloneContext.drawImage(descendantCanvases[i], 0, 0);
  1628. }
  1629. }
  1630. }
  1631. return clone;
  1632. }
  1633. /**
  1634. * Clamps a value between a minimum and a maximum.
  1635. * @param {?} value
  1636. * @param {?} min
  1637. * @param {?} max
  1638. * @return {?}
  1639. */
  1640. function clamp(value, min, max) {
  1641. return Math.max(min, Math.min(max, value));
  1642. }
  1643. /**
  1644. * Helper to remove an element from the DOM and to do all the necessary null checks.
  1645. * @param {?} element Element to be removed.
  1646. * @return {?}
  1647. */
  1648. function removeElement(element) {
  1649. if (element && element.parentNode) {
  1650. element.parentNode.removeChild(element);
  1651. }
  1652. }
  1653. /**
  1654. * Determines whether an event is a touch event.
  1655. * @param {?} event
  1656. * @return {?}
  1657. */
  1658. function isTouchEvent(event) {
  1659. // This function is called for every pixel that the user has dragged so we need it to be
  1660. // as fast as possible. Since we only bind mouse events and touch events, we can assume
  1661. // that if the event's name starts with `t`, it's a touch event.
  1662. return event.type[0] === 't';
  1663. }
  1664. /**
  1665. * Gets the element into which the drag preview should be inserted.
  1666. * @param {?} documentRef
  1667. * @return {?}
  1668. */
  1669. function getPreviewInsertionPoint(documentRef) {
  1670. // We can't use the body if the user is in fullscreen mode,
  1671. // because the preview will render under the fullscreen element.
  1672. // TODO(crisbeto): dedupe this with the `FullscreenOverlayContainer` eventually.
  1673. return documentRef.fullscreenElement ||
  1674. documentRef.webkitFullscreenElement ||
  1675. documentRef.mozFullScreenElement ||
  1676. documentRef.msFullscreenElement ||
  1677. documentRef.body;
  1678. }
  1679. /**
  1680. * Gets the root HTML element of an embedded view.
  1681. * If the root is not an HTML element it gets wrapped in one.
  1682. * @param {?} viewRef
  1683. * @param {?} _document
  1684. * @return {?}
  1685. */
  1686. function getRootNode(viewRef, _document) {
  1687. /** @type {?} */
  1688. var rootNode = viewRef.rootNodes[0];
  1689. if (rootNode.nodeType !== _document.ELEMENT_NODE) {
  1690. /** @type {?} */
  1691. var wrapper = _document.createElement('div');
  1692. wrapper.appendChild(rootNode);
  1693. return wrapper;
  1694. }
  1695. return (/** @type {?} */ (rootNode));
  1696. }
  1697. /**
  1698. * @fileoverview added by tsickle
  1699. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1700. */
  1701. /**
  1702. * Moves an item one index in an array to another.
  1703. * @template T
  1704. * @param {?} array Array in which to move the item.
  1705. * @param {?} fromIndex Starting index of the item.
  1706. * @param {?} toIndex Index to which the item should be moved.
  1707. * @return {?}
  1708. */
  1709. function moveItemInArray(array, fromIndex, toIndex) {
  1710. /** @type {?} */
  1711. var from = clamp$1(fromIndex, array.length - 1);
  1712. /** @type {?} */
  1713. var to = clamp$1(toIndex, array.length - 1);
  1714. if (from === to) {
  1715. return;
  1716. }
  1717. /** @type {?} */
  1718. var target = array[from];
  1719. /** @type {?} */
  1720. var delta = to < from ? -1 : 1;
  1721. for (var i = from; i !== to; i += delta) {
  1722. array[i] = array[i + delta];
  1723. }
  1724. array[to] = target;
  1725. }
  1726. /**
  1727. * Moves an item from one array to another.
  1728. * @template T
  1729. * @param {?} currentArray Array from which to transfer the item.
  1730. * @param {?} targetArray Array into which to put the item.
  1731. * @param {?} currentIndex Index of the item in its current array.
  1732. * @param {?} targetIndex Index at which to insert the item.
  1733. * @return {?}
  1734. */
  1735. function transferArrayItem(currentArray, targetArray, currentIndex, targetIndex) {
  1736. /** @type {?} */
  1737. var from = clamp$1(currentIndex, currentArray.length - 1);
  1738. /** @type {?} */
  1739. var to = clamp$1(targetIndex, targetArray.length);
  1740. if (currentArray.length) {
  1741. targetArray.splice(to, 0, currentArray.splice(from, 1)[0]);
  1742. }
  1743. }
  1744. /**
  1745. * Copies an item from one array to another, leaving it in its
  1746. * original position in current array.
  1747. * @template T
  1748. * @param {?} currentArray Array from which to copy the item.
  1749. * @param {?} targetArray Array into which is copy the item.
  1750. * @param {?} currentIndex Index of the item in its current array.
  1751. * @param {?} targetIndex Index at which to insert the item.
  1752. *
  1753. * @return {?}
  1754. */
  1755. function copyArrayItem(currentArray, targetArray, currentIndex, targetIndex) {
  1756. /** @type {?} */
  1757. var to = clamp$1(targetIndex, targetArray.length);
  1758. if (currentArray.length) {
  1759. targetArray.splice(to, 0, currentArray[currentIndex]);
  1760. }
  1761. }
  1762. /**
  1763. * Clamps a number between zero and a maximum.
  1764. * @param {?} value
  1765. * @param {?} max
  1766. * @return {?}
  1767. */
  1768. function clamp$1(value, max) {
  1769. return Math.max(0, Math.min(max, value));
  1770. }
  1771. /**
  1772. * @fileoverview added by tsickle
  1773. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1774. */
  1775. /**
  1776. * Counter used to generate unique ids for drop refs.
  1777. * @type {?}
  1778. */
  1779. var _uniqueIdCounter = 0;
  1780. /**
  1781. * Proximity, as a ratio to width/height, at which a
  1782. * dragged item will affect the drop container.
  1783. * @type {?}
  1784. */
  1785. var DROP_PROXIMITY_THRESHOLD = 0.05;
  1786. /**
  1787. * Proximity, as a ratio to width/height at which to start auto-scrolling the drop list or the
  1788. * viewport. The value comes from trying it out manually until it feels right.
  1789. * @type {?}
  1790. */
  1791. var SCROLL_PROXIMITY_THRESHOLD = 0.05;
  1792. /**
  1793. * Number of pixels to scroll for each frame when auto-scrolling an element.
  1794. * The value comes from trying it out manually until it feels right.
  1795. * @type {?}
  1796. */
  1797. var AUTO_SCROLL_STEP = 2;
  1798. /**
  1799. * Reference to a drop list. Used to manipulate or dispose of the container.
  1800. * \@docs-private
  1801. * @template T
  1802. */
  1803. var /**
  1804. * Reference to a drop list. Used to manipulate or dispose of the container.
  1805. * \@docs-private
  1806. * @template T
  1807. */
  1808. DropListRef = /** @class */ (function () {
  1809. function DropListRef(element, _dragDropRegistry, _document, _ngZone, _viewportRuler) {
  1810. var _this = this;
  1811. this._dragDropRegistry = _dragDropRegistry;
  1812. this._ngZone = _ngZone;
  1813. this._viewportRuler = _viewportRuler;
  1814. /**
  1815. * Unique ID for the drop list.
  1816. * @deprecated No longer being used. To be removed.
  1817. * \@breaking-change 8.0.0
  1818. */
  1819. this.id = "cdk-drop-list-ref-" + _uniqueIdCounter++;
  1820. /**
  1821. * Whether starting a dragging sequence from this container is disabled.
  1822. */
  1823. this.disabled = false;
  1824. /**
  1825. * Whether sorting items within the list is disabled.
  1826. */
  1827. this.sortingDisabled = false;
  1828. /**
  1829. * Whether auto-scrolling the view when the user
  1830. * moves their pointer close to the edges is disabled.
  1831. */
  1832. this.autoScrollDisabled = false;
  1833. /**
  1834. * Function that is used to determine whether an item
  1835. * is allowed to be moved into a drop container.
  1836. */
  1837. this.enterPredicate = (/**
  1838. * @return {?}
  1839. */
  1840. function () { return true; });
  1841. /**
  1842. * Emits right before dragging has started.
  1843. */
  1844. this.beforeStarted = new Subject();
  1845. /**
  1846. * Emits when the user has moved a new drag item into this container.
  1847. */
  1848. this.entered = new Subject();
  1849. /**
  1850. * Emits when the user removes an item from the container
  1851. * by dragging it into another container.
  1852. */
  1853. this.exited = new Subject();
  1854. /**
  1855. * Emits when the user drops an item inside the container.
  1856. */
  1857. this.dropped = new Subject();
  1858. /**
  1859. * Emits as the user is swapping items while actively dragging.
  1860. */
  1861. this.sorted = new Subject();
  1862. /**
  1863. * Whether an item in the list is being dragged.
  1864. */
  1865. this._isDragging = false;
  1866. /**
  1867. * Cache of the dimensions of all the items inside the container.
  1868. */
  1869. this._itemPositions = [];
  1870. /**
  1871. * Keeps track of the container's scroll position.
  1872. */
  1873. this._scrollPosition = { top: 0, left: 0 };
  1874. /**
  1875. * Keeps track of the scroll position of the viewport.
  1876. */
  1877. this._viewportScrollPosition = { top: 0, left: 0 };
  1878. /**
  1879. * Keeps track of the item that was last swapped with the dragged item, as
  1880. * well as what direction the pointer was moving in when the swap occured.
  1881. */
  1882. this._previousSwap = { drag: (/** @type {?} */ (null)), delta: 0 };
  1883. /**
  1884. * Drop lists that are connected to the current one.
  1885. */
  1886. this._siblings = [];
  1887. /**
  1888. * Direction in which the list is oriented.
  1889. */
  1890. this._orientation = 'vertical';
  1891. /**
  1892. * Connected siblings that currently have a dragged item.
  1893. */
  1894. this._activeSiblings = new Set();
  1895. /**
  1896. * Layout direction of the drop list.
  1897. */
  1898. this._direction = 'ltr';
  1899. /**
  1900. * Subscription to the window being scrolled.
  1901. */
  1902. this._viewportScrollSubscription = Subscription.EMPTY;
  1903. /**
  1904. * Vertical direction in which the list is currently scrolling.
  1905. */
  1906. this._verticalScrollDirection = 0 /* NONE */;
  1907. /**
  1908. * Horizontal direction in which the list is currently scrolling.
  1909. */
  1910. this._horizontalScrollDirection = 0 /* NONE */;
  1911. /**
  1912. * Used to signal to the current auto-scroll sequence when to stop.
  1913. */
  1914. this._stopScrollTimers = new Subject();
  1915. /**
  1916. * Handles the container being scrolled. Has to be an arrow function to preserve the context.
  1917. */
  1918. this._handleScroll = (/**
  1919. * @return {?}
  1920. */
  1921. function () {
  1922. if (!_this.isDragging()) {
  1923. return;
  1924. }
  1925. /** @type {?} */
  1926. var element = coerceElement(_this.element);
  1927. _this._updateAfterScroll(_this._scrollPosition, element.scrollTop, element.scrollLeft);
  1928. });
  1929. /**
  1930. * Starts the interval that'll auto-scroll the element.
  1931. */
  1932. this._startScrollInterval = (/**
  1933. * @return {?}
  1934. */
  1935. function () {
  1936. _this._stopScrolling();
  1937. interval(0, animationFrameScheduler)
  1938. .pipe(takeUntil(_this._stopScrollTimers))
  1939. .subscribe((/**
  1940. * @return {?}
  1941. */
  1942. function () {
  1943. /** @type {?} */
  1944. var node = _this._scrollNode;
  1945. if (_this._verticalScrollDirection === 1 /* UP */) {
  1946. incrementVerticalScroll(node, -AUTO_SCROLL_STEP);
  1947. }
  1948. else if (_this._verticalScrollDirection === 2 /* DOWN */) {
  1949. incrementVerticalScroll(node, AUTO_SCROLL_STEP);
  1950. }
  1951. if (_this._horizontalScrollDirection === 1 /* LEFT */) {
  1952. incrementHorizontalScroll(node, -AUTO_SCROLL_STEP);
  1953. }
  1954. else if (_this._horizontalScrollDirection === 2 /* RIGHT */) {
  1955. incrementHorizontalScroll(node, AUTO_SCROLL_STEP);
  1956. }
  1957. }));
  1958. });
  1959. /** @type {?} */
  1960. var nativeNode = this.element = coerceElement(element);
  1961. this._shadowRoot = getShadowRoot(nativeNode) || _document;
  1962. _dragDropRegistry.registerDropContainer(this);
  1963. }
  1964. /** Removes the drop list functionality from the DOM element. */
  1965. /**
  1966. * Removes the drop list functionality from the DOM element.
  1967. * @return {?}
  1968. */
  1969. DropListRef.prototype.dispose = /**
  1970. * Removes the drop list functionality from the DOM element.
  1971. * @return {?}
  1972. */
  1973. function () {
  1974. this._stopScrolling();
  1975. this._stopScrollTimers.complete();
  1976. this._removeListeners();
  1977. this.beforeStarted.complete();
  1978. this.entered.complete();
  1979. this.exited.complete();
  1980. this.dropped.complete();
  1981. this.sorted.complete();
  1982. this._activeSiblings.clear();
  1983. this._scrollNode = (/** @type {?} */ (null));
  1984. this._dragDropRegistry.removeDropContainer(this);
  1985. };
  1986. /** Whether an item from this list is currently being dragged. */
  1987. /**
  1988. * Whether an item from this list is currently being dragged.
  1989. * @return {?}
  1990. */
  1991. DropListRef.prototype.isDragging = /**
  1992. * Whether an item from this list is currently being dragged.
  1993. * @return {?}
  1994. */
  1995. function () {
  1996. return this._isDragging;
  1997. };
  1998. /** Starts dragging an item. */
  1999. /**
  2000. * Starts dragging an item.
  2001. * @return {?}
  2002. */
  2003. DropListRef.prototype.start = /**
  2004. * Starts dragging an item.
  2005. * @return {?}
  2006. */
  2007. function () {
  2008. var _this = this;
  2009. /** @type {?} */
  2010. var element = coerceElement(this.element);
  2011. this.beforeStarted.next();
  2012. this._isDragging = true;
  2013. this._cacheItems();
  2014. this._siblings.forEach((/**
  2015. * @param {?} sibling
  2016. * @return {?}
  2017. */
  2018. function (sibling) { return sibling._startReceiving(_this); }));
  2019. this._removeListeners();
  2020. // @breaking-change 9.0.0 Remove check for _ngZone once it's marked as a required param.
  2021. if (this._ngZone) {
  2022. this._ngZone.runOutsideAngular((/**
  2023. * @return {?}
  2024. */
  2025. function () { return element.addEventListener('scroll', _this._handleScroll); }));
  2026. }
  2027. else {
  2028. element.addEventListener('scroll', this._handleScroll);
  2029. }
  2030. // @breaking-change 9.0.0 Remove check for _viewportRuler once it's marked as a required param.
  2031. if (this._viewportRuler) {
  2032. this._listenToScrollEvents();
  2033. }
  2034. };
  2035. /**
  2036. * Emits an event to indicate that the user moved an item into the container.
  2037. * @param item Item that was moved into the container.
  2038. * @param pointerX Position of the item along the X axis.
  2039. * @param pointerY Position of the item along the Y axis.
  2040. */
  2041. /**
  2042. * Emits an event to indicate that the user moved an item into the container.
  2043. * @param {?} item Item that was moved into the container.
  2044. * @param {?} pointerX Position of the item along the X axis.
  2045. * @param {?} pointerY Position of the item along the Y axis.
  2046. * @return {?}
  2047. */
  2048. DropListRef.prototype.enter = /**
  2049. * Emits an event to indicate that the user moved an item into the container.
  2050. * @param {?} item Item that was moved into the container.
  2051. * @param {?} pointerX Position of the item along the X axis.
  2052. * @param {?} pointerY Position of the item along the Y axis.
  2053. * @return {?}
  2054. */
  2055. function (item, pointerX, pointerY) {
  2056. this.start();
  2057. // If sorting is disabled, we want the item to return to its starting
  2058. // position if the user is returning it to its initial container.
  2059. /** @type {?} */
  2060. var newIndex = this.sortingDisabled ? this._draggables.indexOf(item) : -1;
  2061. if (newIndex === -1) {
  2062. // We use the coordinates of where the item entered the drop
  2063. // zone to figure out at which index it should be inserted.
  2064. newIndex = this._getItemIndexFromPointerPosition(item, pointerX, pointerY);
  2065. }
  2066. /** @type {?} */
  2067. var activeDraggables = this._activeDraggables;
  2068. /** @type {?} */
  2069. var currentIndex = activeDraggables.indexOf(item);
  2070. /** @type {?} */
  2071. var placeholder = item.getPlaceholderElement();
  2072. /** @type {?} */
  2073. var newPositionReference = activeDraggables[newIndex];
  2074. // If the item at the new position is the same as the item that is being dragged,
  2075. // it means that we're trying to restore the item to its initial position. In this
  2076. // case we should use the next item from the list as the reference.
  2077. if (newPositionReference === item) {
  2078. newPositionReference = activeDraggables[newIndex + 1];
  2079. }
  2080. // Since the item may be in the `activeDraggables` already (e.g. if the user dragged it
  2081. // into another container and back again), we have to ensure that it isn't duplicated.
  2082. if (currentIndex > -1) {
  2083. activeDraggables.splice(currentIndex, 1);
  2084. }
  2085. // Don't use items that are being dragged as a reference, because
  2086. // their element has been moved down to the bottom of the body.
  2087. if (newPositionReference && !this._dragDropRegistry.isDragging(newPositionReference)) {
  2088. /** @type {?} */
  2089. var element = newPositionReference.getRootElement();
  2090. (/** @type {?} */ (element.parentElement)).insertBefore(placeholder, element);
  2091. activeDraggables.splice(newIndex, 0, item);
  2092. }
  2093. else {
  2094. coerceElement(this.element).appendChild(placeholder);
  2095. activeDraggables.push(item);
  2096. }
  2097. // The transform needs to be cleared so it doesn't throw off the measurements.
  2098. placeholder.style.transform = '';
  2099. // Note that the positions were already cached when we called `start` above,
  2100. // but we need to refresh them since the amount of items has changed.
  2101. this._cacheItemPositions();
  2102. this.entered.next({ item: item, container: this, currentIndex: this.getItemIndex(item) });
  2103. };
  2104. /**
  2105. * Removes an item from the container after it was dragged into another container by the user.
  2106. * @param item Item that was dragged out.
  2107. */
  2108. /**
  2109. * Removes an item from the container after it was dragged into another container by the user.
  2110. * @param {?} item Item that was dragged out.
  2111. * @return {?}
  2112. */
  2113. DropListRef.prototype.exit = /**
  2114. * Removes an item from the container after it was dragged into another container by the user.
  2115. * @param {?} item Item that was dragged out.
  2116. * @return {?}
  2117. */
  2118. function (item) {
  2119. this._reset();
  2120. this.exited.next({ item: item, container: this });
  2121. };
  2122. /**
  2123. * Drops an item into this container.
  2124. * @param item Item being dropped into the container.
  2125. * @param currentIndex Index at which the item should be inserted.
  2126. * @param previousContainer Container from which the item got dragged in.
  2127. * @param isPointerOverContainer Whether the user's pointer was over the
  2128. * container when the item was dropped.
  2129. * @param distance Distance the user has dragged since the start of the dragging sequence.
  2130. * @breaking-change 9.0.0 `distance` parameter to become required.
  2131. */
  2132. /**
  2133. * Drops an item into this container.
  2134. * \@breaking-change 9.0.0 `distance` parameter to become required.
  2135. * @param {?} item Item being dropped into the container.
  2136. * @param {?} currentIndex Index at which the item should be inserted.
  2137. * @param {?} previousContainer Container from which the item got dragged in.
  2138. * @param {?} isPointerOverContainer Whether the user's pointer was over the
  2139. * container when the item was dropped.
  2140. * @param {?=} distance Distance the user has dragged since the start of the dragging sequence.
  2141. * @return {?}
  2142. */
  2143. DropListRef.prototype.drop = /**
  2144. * Drops an item into this container.
  2145. * \@breaking-change 9.0.0 `distance` parameter to become required.
  2146. * @param {?} item Item being dropped into the container.
  2147. * @param {?} currentIndex Index at which the item should be inserted.
  2148. * @param {?} previousContainer Container from which the item got dragged in.
  2149. * @param {?} isPointerOverContainer Whether the user's pointer was over the
  2150. * container when the item was dropped.
  2151. * @param {?=} distance Distance the user has dragged since the start of the dragging sequence.
  2152. * @return {?}
  2153. */
  2154. function (item, currentIndex, previousContainer, isPointerOverContainer, distance) {
  2155. if (distance === void 0) { distance = { x: 0, y: 0 }; }
  2156. this._reset();
  2157. this.dropped.next({
  2158. item: item,
  2159. currentIndex: currentIndex,
  2160. previousIndex: previousContainer.getItemIndex(item),
  2161. container: this,
  2162. previousContainer: previousContainer,
  2163. isPointerOverContainer: isPointerOverContainer,
  2164. distance: distance
  2165. });
  2166. };
  2167. /**
  2168. * Sets the draggable items that are a part of this list.
  2169. * @param items Items that are a part of this list.
  2170. */
  2171. /**
  2172. * Sets the draggable items that are a part of this list.
  2173. * @template THIS
  2174. * @this {THIS}
  2175. * @param {?} items Items that are a part of this list.
  2176. * @return {THIS}
  2177. */
  2178. DropListRef.prototype.withItems = /**
  2179. * Sets the draggable items that are a part of this list.
  2180. * @template THIS
  2181. * @this {THIS}
  2182. * @param {?} items Items that are a part of this list.
  2183. * @return {THIS}
  2184. */
  2185. function (items) {
  2186. var _this = this;
  2187. (/** @type {?} */ (this))._draggables = items;
  2188. items.forEach((/**
  2189. * @param {?} item
  2190. * @return {?}
  2191. */
  2192. function (item) { return item._withDropContainer((/** @type {?} */ (_this))); }));
  2193. if ((/** @type {?} */ (this)).isDragging()) {
  2194. (/** @type {?} */ (this))._cacheItems();
  2195. }
  2196. return (/** @type {?} */ (this));
  2197. };
  2198. /** Sets the layout direction of the drop list. */
  2199. /**
  2200. * Sets the layout direction of the drop list.
  2201. * @template THIS
  2202. * @this {THIS}
  2203. * @param {?} direction
  2204. * @return {THIS}
  2205. */
  2206. DropListRef.prototype.withDirection = /**
  2207. * Sets the layout direction of the drop list.
  2208. * @template THIS
  2209. * @this {THIS}
  2210. * @param {?} direction
  2211. * @return {THIS}
  2212. */
  2213. function (direction) {
  2214. (/** @type {?} */ (this))._direction = direction;
  2215. return (/** @type {?} */ (this));
  2216. };
  2217. /**
  2218. * Sets the containers that are connected to this one. When two or more containers are
  2219. * connected, the user will be allowed to transfer items between them.
  2220. * @param connectedTo Other containers that the current containers should be connected to.
  2221. */
  2222. /**
  2223. * Sets the containers that are connected to this one. When two or more containers are
  2224. * connected, the user will be allowed to transfer items between them.
  2225. * @template THIS
  2226. * @this {THIS}
  2227. * @param {?} connectedTo Other containers that the current containers should be connected to.
  2228. * @return {THIS}
  2229. */
  2230. DropListRef.prototype.connectedTo = /**
  2231. * Sets the containers that are connected to this one. When two or more containers are
  2232. * connected, the user will be allowed to transfer items between them.
  2233. * @template THIS
  2234. * @this {THIS}
  2235. * @param {?} connectedTo Other containers that the current containers should be connected to.
  2236. * @return {THIS}
  2237. */
  2238. function (connectedTo) {
  2239. (/** @type {?} */ (this))._siblings = connectedTo.slice();
  2240. return (/** @type {?} */ (this));
  2241. };
  2242. /**
  2243. * Sets the orientation of the container.
  2244. * @param orientation New orientation for the container.
  2245. */
  2246. /**
  2247. * Sets the orientation of the container.
  2248. * @template THIS
  2249. * @this {THIS}
  2250. * @param {?} orientation New orientation for the container.
  2251. * @return {THIS}
  2252. */
  2253. DropListRef.prototype.withOrientation = /**
  2254. * Sets the orientation of the container.
  2255. * @template THIS
  2256. * @this {THIS}
  2257. * @param {?} orientation New orientation for the container.
  2258. * @return {THIS}
  2259. */
  2260. function (orientation) {
  2261. (/** @type {?} */ (this))._orientation = orientation;
  2262. return (/** @type {?} */ (this));
  2263. };
  2264. /**
  2265. * Figures out the index of an item in the container.
  2266. * @param item Item whose index should be determined.
  2267. */
  2268. /**
  2269. * Figures out the index of an item in the container.
  2270. * @param {?} item Item whose index should be determined.
  2271. * @return {?}
  2272. */
  2273. DropListRef.prototype.getItemIndex = /**
  2274. * Figures out the index of an item in the container.
  2275. * @param {?} item Item whose index should be determined.
  2276. * @return {?}
  2277. */
  2278. function (item) {
  2279. if (!this._isDragging) {
  2280. return this._draggables.indexOf(item);
  2281. }
  2282. // Items are sorted always by top/left in the cache, however they flow differently in RTL.
  2283. // The rest of the logic still stands no matter what orientation we're in, however
  2284. // we need to invert the array when determining the index.
  2285. /** @type {?} */
  2286. var items = this._orientation === 'horizontal' && this._direction === 'rtl' ?
  2287. this._itemPositions.slice().reverse() : this._itemPositions;
  2288. return findIndex(items, (/**
  2289. * @param {?} currentItem
  2290. * @return {?}
  2291. */
  2292. function (currentItem) { return currentItem.drag === item; }));
  2293. };
  2294. /**
  2295. * Whether the list is able to receive the item that
  2296. * is currently being dragged inside a connected drop list.
  2297. */
  2298. /**
  2299. * Whether the list is able to receive the item that
  2300. * is currently being dragged inside a connected drop list.
  2301. * @return {?}
  2302. */
  2303. DropListRef.prototype.isReceiving = /**
  2304. * Whether the list is able to receive the item that
  2305. * is currently being dragged inside a connected drop list.
  2306. * @return {?}
  2307. */
  2308. function () {
  2309. return this._activeSiblings.size > 0;
  2310. };
  2311. /**
  2312. * Sorts an item inside the container based on its position.
  2313. * @param item Item to be sorted.
  2314. * @param pointerX Position of the item along the X axis.
  2315. * @param pointerY Position of the item along the Y axis.
  2316. * @param pointerDelta Direction in which the pointer is moving along each axis.
  2317. */
  2318. /**
  2319. * Sorts an item inside the container based on its position.
  2320. * @param {?} item Item to be sorted.
  2321. * @param {?} pointerX Position of the item along the X axis.
  2322. * @param {?} pointerY Position of the item along the Y axis.
  2323. * @param {?} pointerDelta Direction in which the pointer is moving along each axis.
  2324. * @return {?}
  2325. */
  2326. DropListRef.prototype._sortItem = /**
  2327. * Sorts an item inside the container based on its position.
  2328. * @param {?} item Item to be sorted.
  2329. * @param {?} pointerX Position of the item along the X axis.
  2330. * @param {?} pointerY Position of the item along the Y axis.
  2331. * @param {?} pointerDelta Direction in which the pointer is moving along each axis.
  2332. * @return {?}
  2333. */
  2334. function (item, pointerX, pointerY, pointerDelta) {
  2335. // Don't sort the item if sorting is disabled or it's out of range.
  2336. if (this.sortingDisabled || !this._isPointerNearDropContainer(pointerX, pointerY)) {
  2337. return;
  2338. }
  2339. /** @type {?} */
  2340. var siblings = this._itemPositions;
  2341. /** @type {?} */
  2342. var newIndex = this._getItemIndexFromPointerPosition(item, pointerX, pointerY, pointerDelta);
  2343. if (newIndex === -1 && siblings.length > 0) {
  2344. return;
  2345. }
  2346. /** @type {?} */
  2347. var isHorizontal = this._orientation === 'horizontal';
  2348. /** @type {?} */
  2349. var currentIndex = findIndex(siblings, (/**
  2350. * @param {?} currentItem
  2351. * @return {?}
  2352. */
  2353. function (currentItem) { return currentItem.drag === item; }));
  2354. /** @type {?} */
  2355. var siblingAtNewPosition = siblings[newIndex];
  2356. /** @type {?} */
  2357. var currentPosition = siblings[currentIndex].clientRect;
  2358. /** @type {?} */
  2359. var newPosition = siblingAtNewPosition.clientRect;
  2360. /** @type {?} */
  2361. var delta = currentIndex > newIndex ? 1 : -1;
  2362. this._previousSwap.drag = siblingAtNewPosition.drag;
  2363. this._previousSwap.delta = isHorizontal ? pointerDelta.x : pointerDelta.y;
  2364. // How many pixels the item's placeholder should be offset.
  2365. /** @type {?} */
  2366. var itemOffset = this._getItemOffsetPx(currentPosition, newPosition, delta);
  2367. // How many pixels all the other items should be offset.
  2368. /** @type {?} */
  2369. var siblingOffset = this._getSiblingOffsetPx(currentIndex, siblings, delta);
  2370. // Save the previous order of the items before moving the item to its new index.
  2371. // We use this to check whether an item has been moved as a result of the sorting.
  2372. /** @type {?} */
  2373. var oldOrder = siblings.slice();
  2374. // Shuffle the array in place.
  2375. moveItemInArray(siblings, currentIndex, newIndex);
  2376. this.sorted.next({
  2377. previousIndex: currentIndex,
  2378. currentIndex: newIndex,
  2379. container: this,
  2380. item: item
  2381. });
  2382. siblings.forEach((/**
  2383. * @param {?} sibling
  2384. * @param {?} index
  2385. * @return {?}
  2386. */
  2387. function (sibling, index) {
  2388. // Don't do anything if the position hasn't changed.
  2389. if (oldOrder[index] === sibling) {
  2390. return;
  2391. }
  2392. /** @type {?} */
  2393. var isDraggedItem = sibling.drag === item;
  2394. /** @type {?} */
  2395. var offset = isDraggedItem ? itemOffset : siblingOffset;
  2396. /** @type {?} */
  2397. var elementToOffset = isDraggedItem ? item.getPlaceholderElement() :
  2398. sibling.drag.getRootElement();
  2399. // Update the offset to reflect the new position.
  2400. sibling.offset += offset;
  2401. // Since we're moving the items with a `transform`, we need to adjust their cached
  2402. // client rects to reflect their new position, as well as swap their positions in the cache.
  2403. // Note that we shouldn't use `getBoundingClientRect` here to update the cache, because the
  2404. // elements may be mid-animation which will give us a wrong result.
  2405. if (isHorizontal) {
  2406. // Round the transforms since some browsers will
  2407. // blur the elements, for sub-pixel transforms.
  2408. elementToOffset.style.transform = "translate3d(" + Math.round(sibling.offset) + "px, 0, 0)";
  2409. adjustClientRect(sibling.clientRect, 0, offset);
  2410. }
  2411. else {
  2412. elementToOffset.style.transform = "translate3d(0, " + Math.round(sibling.offset) + "px, 0)";
  2413. adjustClientRect(sibling.clientRect, offset, 0);
  2414. }
  2415. }));
  2416. };
  2417. /**
  2418. * Checks whether the user's pointer is close to the edges of either the
  2419. * viewport or the drop list and starts the auto-scroll sequence.
  2420. * @param pointerX User's pointer position along the x axis.
  2421. * @param pointerY User's pointer position along the y axis.
  2422. */
  2423. /**
  2424. * Checks whether the user's pointer is close to the edges of either the
  2425. * viewport or the drop list and starts the auto-scroll sequence.
  2426. * @param {?} pointerX User's pointer position along the x axis.
  2427. * @param {?} pointerY User's pointer position along the y axis.
  2428. * @return {?}
  2429. */
  2430. DropListRef.prototype._startScrollingIfNecessary = /**
  2431. * Checks whether the user's pointer is close to the edges of either the
  2432. * viewport or the drop list and starts the auto-scroll sequence.
  2433. * @param {?} pointerX User's pointer position along the x axis.
  2434. * @param {?} pointerY User's pointer position along the y axis.
  2435. * @return {?}
  2436. */
  2437. function (pointerX, pointerY) {
  2438. var _a;
  2439. if (this.autoScrollDisabled) {
  2440. return;
  2441. }
  2442. /** @type {?} */
  2443. var scrollNode;
  2444. /** @type {?} */
  2445. var verticalScrollDirection = 0 /* NONE */;
  2446. /** @type {?} */
  2447. var horizontalScrollDirection = 0 /* NONE */;
  2448. // Check whether we should start scrolling the container.
  2449. if (this._isPointerNearDropContainer(pointerX, pointerY)) {
  2450. /** @type {?} */
  2451. var element = coerceElement(this.element);
  2452. _a = getElementScrollDirections(element, this._clientRect, pointerX, pointerY), verticalScrollDirection = _a[0], horizontalScrollDirection = _a[1];
  2453. if (verticalScrollDirection || horizontalScrollDirection) {
  2454. scrollNode = element;
  2455. }
  2456. }
  2457. // @breaking-change 9.0.0 Remove null check for _viewportRuler once it's a required parameter.
  2458. // Otherwise check if we can start scrolling the viewport.
  2459. if (this._viewportRuler && !verticalScrollDirection && !horizontalScrollDirection) {
  2460. var _b = this._viewportRuler.getViewportSize(), width = _b.width, height = _b.height;
  2461. /** @type {?} */
  2462. var clientRect = { width: width, height: height, top: 0, right: width, bottom: height, left: 0 };
  2463. verticalScrollDirection = getVerticalScrollDirection(clientRect, pointerY);
  2464. horizontalScrollDirection = getHorizontalScrollDirection(clientRect, pointerX);
  2465. scrollNode = window;
  2466. }
  2467. if (scrollNode && (verticalScrollDirection !== this._verticalScrollDirection ||
  2468. horizontalScrollDirection !== this._horizontalScrollDirection ||
  2469. scrollNode !== this._scrollNode)) {
  2470. this._verticalScrollDirection = verticalScrollDirection;
  2471. this._horizontalScrollDirection = horizontalScrollDirection;
  2472. this._scrollNode = scrollNode;
  2473. if ((verticalScrollDirection || horizontalScrollDirection) && scrollNode) {
  2474. // @breaking-change 9.0.0 Remove null check for `_ngZone` once it is made required.
  2475. if (this._ngZone) {
  2476. this._ngZone.runOutsideAngular(this._startScrollInterval);
  2477. }
  2478. else {
  2479. this._startScrollInterval();
  2480. }
  2481. }
  2482. else {
  2483. this._stopScrolling();
  2484. }
  2485. }
  2486. };
  2487. /** Stops any currently-running auto-scroll sequences. */
  2488. /**
  2489. * Stops any currently-running auto-scroll sequences.
  2490. * @return {?}
  2491. */
  2492. DropListRef.prototype._stopScrolling = /**
  2493. * Stops any currently-running auto-scroll sequences.
  2494. * @return {?}
  2495. */
  2496. function () {
  2497. this._stopScrollTimers.next();
  2498. };
  2499. /** Caches the position of the drop list. */
  2500. /**
  2501. * Caches the position of the drop list.
  2502. * @private
  2503. * @return {?}
  2504. */
  2505. DropListRef.prototype._cacheOwnPosition = /**
  2506. * Caches the position of the drop list.
  2507. * @private
  2508. * @return {?}
  2509. */
  2510. function () {
  2511. /** @type {?} */
  2512. var element = coerceElement(this.element);
  2513. this._clientRect = getMutableClientRect(element);
  2514. this._scrollPosition = { top: element.scrollTop, left: element.scrollLeft };
  2515. };
  2516. /** Refreshes the position cache of the items and sibling containers. */
  2517. /**
  2518. * Refreshes the position cache of the items and sibling containers.
  2519. * @private
  2520. * @return {?}
  2521. */
  2522. DropListRef.prototype._cacheItemPositions = /**
  2523. * Refreshes the position cache of the items and sibling containers.
  2524. * @private
  2525. * @return {?}
  2526. */
  2527. function () {
  2528. var _this = this;
  2529. /** @type {?} */
  2530. var isHorizontal = this._orientation === 'horizontal';
  2531. this._itemPositions = this._activeDraggables.map((/**
  2532. * @param {?} drag
  2533. * @return {?}
  2534. */
  2535. function (drag) {
  2536. /** @type {?} */
  2537. var elementToMeasure = _this._dragDropRegistry.isDragging(drag) ?
  2538. // If the element is being dragged, we have to measure the
  2539. // placeholder, because the element is hidden.
  2540. drag.getPlaceholderElement() :
  2541. drag.getRootElement();
  2542. return { drag: drag, offset: 0, clientRect: getMutableClientRect(elementToMeasure) };
  2543. })).sort((/**
  2544. * @param {?} a
  2545. * @param {?} b
  2546. * @return {?}
  2547. */
  2548. function (a, b) {
  2549. return isHorizontal ? a.clientRect.left - b.clientRect.left :
  2550. a.clientRect.top - b.clientRect.top;
  2551. }));
  2552. };
  2553. /** Resets the container to its initial state. */
  2554. /**
  2555. * Resets the container to its initial state.
  2556. * @private
  2557. * @return {?}
  2558. */
  2559. DropListRef.prototype._reset = /**
  2560. * Resets the container to its initial state.
  2561. * @private
  2562. * @return {?}
  2563. */
  2564. function () {
  2565. var _this = this;
  2566. this._isDragging = false;
  2567. // TODO(crisbeto): may have to wait for the animations to finish.
  2568. this._activeDraggables.forEach((/**
  2569. * @param {?} item
  2570. * @return {?}
  2571. */
  2572. function (item) { return item.getRootElement().style.transform = ''; }));
  2573. this._siblings.forEach((/**
  2574. * @param {?} sibling
  2575. * @return {?}
  2576. */
  2577. function (sibling) { return sibling._stopReceiving(_this); }));
  2578. this._activeDraggables = [];
  2579. this._itemPositions = [];
  2580. this._previousSwap.drag = null;
  2581. this._previousSwap.delta = 0;
  2582. this._stopScrolling();
  2583. this._removeListeners();
  2584. };
  2585. /**
  2586. * Gets the offset in pixels by which the items that aren't being dragged should be moved.
  2587. * @param currentIndex Index of the item currently being dragged.
  2588. * @param siblings All of the items in the list.
  2589. * @param delta Direction in which the user is moving.
  2590. */
  2591. /**
  2592. * Gets the offset in pixels by which the items that aren't being dragged should be moved.
  2593. * @private
  2594. * @param {?} currentIndex Index of the item currently being dragged.
  2595. * @param {?} siblings All of the items in the list.
  2596. * @param {?} delta Direction in which the user is moving.
  2597. * @return {?}
  2598. */
  2599. DropListRef.prototype._getSiblingOffsetPx = /**
  2600. * Gets the offset in pixels by which the items that aren't being dragged should be moved.
  2601. * @private
  2602. * @param {?} currentIndex Index of the item currently being dragged.
  2603. * @param {?} siblings All of the items in the list.
  2604. * @param {?} delta Direction in which the user is moving.
  2605. * @return {?}
  2606. */
  2607. function (currentIndex, siblings, delta) {
  2608. /** @type {?} */
  2609. var isHorizontal = this._orientation === 'horizontal';
  2610. /** @type {?} */
  2611. var currentPosition = siblings[currentIndex].clientRect;
  2612. /** @type {?} */
  2613. var immediateSibling = siblings[currentIndex + delta * -1];
  2614. /** @type {?} */
  2615. var siblingOffset = currentPosition[isHorizontal ? 'width' : 'height'] * delta;
  2616. if (immediateSibling) {
  2617. /** @type {?} */
  2618. var start = isHorizontal ? 'left' : 'top';
  2619. /** @type {?} */
  2620. var end = isHorizontal ? 'right' : 'bottom';
  2621. // Get the spacing between the start of the current item and the end of the one immediately
  2622. // after it in the direction in which the user is dragging, or vice versa. We add it to the
  2623. // offset in order to push the element to where it will be when it's inline and is influenced
  2624. // by the `margin` of its siblings.
  2625. if (delta === -1) {
  2626. siblingOffset -= immediateSibling.clientRect[start] - currentPosition[end];
  2627. }
  2628. else {
  2629. siblingOffset += currentPosition[start] - immediateSibling.clientRect[end];
  2630. }
  2631. }
  2632. return siblingOffset;
  2633. };
  2634. /**
  2635. * Checks whether the pointer coordinates are close to the drop container.
  2636. * @param pointerX Coordinates along the X axis.
  2637. * @param pointerY Coordinates along the Y axis.
  2638. */
  2639. /**
  2640. * Checks whether the pointer coordinates are close to the drop container.
  2641. * @private
  2642. * @param {?} pointerX Coordinates along the X axis.
  2643. * @param {?} pointerY Coordinates along the Y axis.
  2644. * @return {?}
  2645. */
  2646. DropListRef.prototype._isPointerNearDropContainer = /**
  2647. * Checks whether the pointer coordinates are close to the drop container.
  2648. * @private
  2649. * @param {?} pointerX Coordinates along the X axis.
  2650. * @param {?} pointerY Coordinates along the Y axis.
  2651. * @return {?}
  2652. */
  2653. function (pointerX, pointerY) {
  2654. var _a = this._clientRect, top = _a.top, right = _a.right, bottom = _a.bottom, left = _a.left, width = _a.width, height = _a.height;
  2655. /** @type {?} */
  2656. var xThreshold = width * DROP_PROXIMITY_THRESHOLD;
  2657. /** @type {?} */
  2658. var yThreshold = height * DROP_PROXIMITY_THRESHOLD;
  2659. return pointerY > top - yThreshold && pointerY < bottom + yThreshold &&
  2660. pointerX > left - xThreshold && pointerX < right + xThreshold;
  2661. };
  2662. /**
  2663. * Gets the offset in pixels by which the item that is being dragged should be moved.
  2664. * @param currentPosition Current position of the item.
  2665. * @param newPosition Position of the item where the current item should be moved.
  2666. * @param delta Direction in which the user is moving.
  2667. */
  2668. /**
  2669. * Gets the offset in pixels by which the item that is being dragged should be moved.
  2670. * @private
  2671. * @param {?} currentPosition Current position of the item.
  2672. * @param {?} newPosition Position of the item where the current item should be moved.
  2673. * @param {?} delta Direction in which the user is moving.
  2674. * @return {?}
  2675. */
  2676. DropListRef.prototype._getItemOffsetPx = /**
  2677. * Gets the offset in pixels by which the item that is being dragged should be moved.
  2678. * @private
  2679. * @param {?} currentPosition Current position of the item.
  2680. * @param {?} newPosition Position of the item where the current item should be moved.
  2681. * @param {?} delta Direction in which the user is moving.
  2682. * @return {?}
  2683. */
  2684. function (currentPosition, newPosition, delta) {
  2685. /** @type {?} */
  2686. var isHorizontal = this._orientation === 'horizontal';
  2687. /** @type {?} */
  2688. var itemOffset = isHorizontal ? newPosition.left - currentPosition.left :
  2689. newPosition.top - currentPosition.top;
  2690. // Account for differences in the item width/height.
  2691. if (delta === -1) {
  2692. itemOffset += isHorizontal ? newPosition.width - currentPosition.width :
  2693. newPosition.height - currentPosition.height;
  2694. }
  2695. return itemOffset;
  2696. };
  2697. /**
  2698. * Gets the index of an item in the drop container, based on the position of the user's pointer.
  2699. * @param item Item that is being sorted.
  2700. * @param pointerX Position of the user's pointer along the X axis.
  2701. * @param pointerY Position of the user's pointer along the Y axis.
  2702. * @param delta Direction in which the user is moving their pointer.
  2703. */
  2704. /**
  2705. * Gets the index of an item in the drop container, based on the position of the user's pointer.
  2706. * @private
  2707. * @param {?} item Item that is being sorted.
  2708. * @param {?} pointerX Position of the user's pointer along the X axis.
  2709. * @param {?} pointerY Position of the user's pointer along the Y axis.
  2710. * @param {?=} delta Direction in which the user is moving their pointer.
  2711. * @return {?}
  2712. */
  2713. DropListRef.prototype._getItemIndexFromPointerPosition = /**
  2714. * Gets the index of an item in the drop container, based on the position of the user's pointer.
  2715. * @private
  2716. * @param {?} item Item that is being sorted.
  2717. * @param {?} pointerX Position of the user's pointer along the X axis.
  2718. * @param {?} pointerY Position of the user's pointer along the Y axis.
  2719. * @param {?=} delta Direction in which the user is moving their pointer.
  2720. * @return {?}
  2721. */
  2722. function (item, pointerX, pointerY, delta) {
  2723. var _this = this;
  2724. /** @type {?} */
  2725. var isHorizontal = this._orientation === 'horizontal';
  2726. return findIndex(this._itemPositions, (/**
  2727. * @param {?} __0
  2728. * @param {?} _
  2729. * @param {?} array
  2730. * @return {?}
  2731. */
  2732. function (_a, _, array) {
  2733. var drag = _a.drag, clientRect = _a.clientRect;
  2734. if (drag === item) {
  2735. // If there's only one item left in the container, it must be
  2736. // the dragged item itself so we use it as a reference.
  2737. return array.length < 2;
  2738. }
  2739. if (delta) {
  2740. /** @type {?} */
  2741. var direction = isHorizontal ? delta.x : delta.y;
  2742. // If the user is still hovering over the same item as last time, and they didn't change
  2743. // the direction in which they're dragging, we don't consider it a direction swap.
  2744. if (drag === _this._previousSwap.drag && direction === _this._previousSwap.delta) {
  2745. return false;
  2746. }
  2747. }
  2748. return isHorizontal ?
  2749. // Round these down since most browsers report client rects with
  2750. // sub-pixel precision, whereas the pointer coordinates are rounded to pixels.
  2751. pointerX >= Math.floor(clientRect.left) && pointerX <= Math.floor(clientRect.right) :
  2752. pointerY >= Math.floor(clientRect.top) && pointerY <= Math.floor(clientRect.bottom);
  2753. }));
  2754. };
  2755. /** Caches the current items in the list and their positions. */
  2756. /**
  2757. * Caches the current items in the list and their positions.
  2758. * @private
  2759. * @return {?}
  2760. */
  2761. DropListRef.prototype._cacheItems = /**
  2762. * Caches the current items in the list and their positions.
  2763. * @private
  2764. * @return {?}
  2765. */
  2766. function () {
  2767. this._activeDraggables = this._draggables.slice();
  2768. this._cacheItemPositions();
  2769. this._cacheOwnPosition();
  2770. };
  2771. /**
  2772. * Updates the internal state of the container after a scroll event has happened.
  2773. * @param scrollPosition Object that is keeping track of the scroll position.
  2774. * @param newTop New top scroll position.
  2775. * @param newLeft New left scroll position.
  2776. * @param extraClientRect Extra `ClientRect` object that should be updated, in addition to the
  2777. * ones of the drag items. Useful when the viewport has been scrolled and we also need to update
  2778. * the `ClientRect` of the list.
  2779. */
  2780. /**
  2781. * Updates the internal state of the container after a scroll event has happened.
  2782. * @private
  2783. * @param {?} scrollPosition Object that is keeping track of the scroll position.
  2784. * @param {?} newTop New top scroll position.
  2785. * @param {?} newLeft New left scroll position.
  2786. * @param {?=} extraClientRect Extra `ClientRect` object that should be updated, in addition to the
  2787. * ones of the drag items. Useful when the viewport has been scrolled and we also need to update
  2788. * the `ClientRect` of the list.
  2789. * @return {?}
  2790. */
  2791. DropListRef.prototype._updateAfterScroll = /**
  2792. * Updates the internal state of the container after a scroll event has happened.
  2793. * @private
  2794. * @param {?} scrollPosition Object that is keeping track of the scroll position.
  2795. * @param {?} newTop New top scroll position.
  2796. * @param {?} newLeft New left scroll position.
  2797. * @param {?=} extraClientRect Extra `ClientRect` object that should be updated, in addition to the
  2798. * ones of the drag items. Useful when the viewport has been scrolled and we also need to update
  2799. * the `ClientRect` of the list.
  2800. * @return {?}
  2801. */
  2802. function (scrollPosition, newTop, newLeft, extraClientRect) {
  2803. var _this = this;
  2804. /** @type {?} */
  2805. var topDifference = scrollPosition.top - newTop;
  2806. /** @type {?} */
  2807. var leftDifference = scrollPosition.left - newLeft;
  2808. if (extraClientRect) {
  2809. adjustClientRect(extraClientRect, topDifference, leftDifference);
  2810. }
  2811. // Since we know the amount that the user has scrolled we can shift all of the client rectangles
  2812. // ourselves. This is cheaper than re-measuring everything and we can avoid inconsistent
  2813. // behavior where we might be measuring the element before its position has changed.
  2814. this._itemPositions.forEach((/**
  2815. * @param {?} __0
  2816. * @return {?}
  2817. */
  2818. function (_a) {
  2819. var clientRect = _a.clientRect;
  2820. adjustClientRect(clientRect, topDifference, leftDifference);
  2821. }));
  2822. // We need two loops for this, because we want all of the cached
  2823. // positions to be up-to-date before we re-sort the item.
  2824. this._itemPositions.forEach((/**
  2825. * @param {?} __0
  2826. * @return {?}
  2827. */
  2828. function (_a) {
  2829. var drag = _a.drag;
  2830. if (_this._dragDropRegistry.isDragging(drag)) {
  2831. // We need to re-sort the item manually, because the pointer move
  2832. // events won't be dispatched while the user is scrolling.
  2833. drag._sortFromLastPointerPosition();
  2834. }
  2835. }));
  2836. scrollPosition.top = newTop;
  2837. scrollPosition.left = newLeft;
  2838. };
  2839. /** Removes the event listeners associated with this drop list. */
  2840. /**
  2841. * Removes the event listeners associated with this drop list.
  2842. * @private
  2843. * @return {?}
  2844. */
  2845. DropListRef.prototype._removeListeners = /**
  2846. * Removes the event listeners associated with this drop list.
  2847. * @private
  2848. * @return {?}
  2849. */
  2850. function () {
  2851. coerceElement(this.element).removeEventListener('scroll', this._handleScroll);
  2852. this._viewportScrollSubscription.unsubscribe();
  2853. };
  2854. /**
  2855. * Checks whether the user's pointer is positioned over the container.
  2856. * @param x Pointer position along the X axis.
  2857. * @param y Pointer position along the Y axis.
  2858. */
  2859. /**
  2860. * Checks whether the user's pointer is positioned over the container.
  2861. * @param {?} x Pointer position along the X axis.
  2862. * @param {?} y Pointer position along the Y axis.
  2863. * @return {?}
  2864. */
  2865. DropListRef.prototype._isOverContainer = /**
  2866. * Checks whether the user's pointer is positioned over the container.
  2867. * @param {?} x Pointer position along the X axis.
  2868. * @param {?} y Pointer position along the Y axis.
  2869. * @return {?}
  2870. */
  2871. function (x, y) {
  2872. return isInsideClientRect(this._clientRect, x, y);
  2873. };
  2874. /**
  2875. * Figures out whether an item should be moved into a sibling
  2876. * drop container, based on its current position.
  2877. * @param item Drag item that is being moved.
  2878. * @param x Position of the item along the X axis.
  2879. * @param y Position of the item along the Y axis.
  2880. */
  2881. /**
  2882. * Figures out whether an item should be moved into a sibling
  2883. * drop container, based on its current position.
  2884. * @param {?} item Drag item that is being moved.
  2885. * @param {?} x Position of the item along the X axis.
  2886. * @param {?} y Position of the item along the Y axis.
  2887. * @return {?}
  2888. */
  2889. DropListRef.prototype._getSiblingContainerFromPosition = /**
  2890. * Figures out whether an item should be moved into a sibling
  2891. * drop container, based on its current position.
  2892. * @param {?} item Drag item that is being moved.
  2893. * @param {?} x Position of the item along the X axis.
  2894. * @param {?} y Position of the item along the Y axis.
  2895. * @return {?}
  2896. */
  2897. function (item, x, y) {
  2898. return this._siblings.find((/**
  2899. * @param {?} sibling
  2900. * @return {?}
  2901. */
  2902. function (sibling) { return sibling._canReceive(item, x, y); }));
  2903. };
  2904. /**
  2905. * Checks whether the drop list can receive the passed-in item.
  2906. * @param item Item that is being dragged into the list.
  2907. * @param x Position of the item along the X axis.
  2908. * @param y Position of the item along the Y axis.
  2909. */
  2910. /**
  2911. * Checks whether the drop list can receive the passed-in item.
  2912. * @param {?} item Item that is being dragged into the list.
  2913. * @param {?} x Position of the item along the X axis.
  2914. * @param {?} y Position of the item along the Y axis.
  2915. * @return {?}
  2916. */
  2917. DropListRef.prototype._canReceive = /**
  2918. * Checks whether the drop list can receive the passed-in item.
  2919. * @param {?} item Item that is being dragged into the list.
  2920. * @param {?} x Position of the item along the X axis.
  2921. * @param {?} y Position of the item along the Y axis.
  2922. * @return {?}
  2923. */
  2924. function (item, x, y) {
  2925. if (!this.enterPredicate(item, this) || !isInsideClientRect(this._clientRect, x, y)) {
  2926. return false;
  2927. }
  2928. /** @type {?} */
  2929. var elementFromPoint = (/** @type {?} */ (this._shadowRoot.elementFromPoint(x, y)));
  2930. // If there's no element at the pointer position, then
  2931. // the client rect is probably scrolled out of the view.
  2932. if (!elementFromPoint) {
  2933. return false;
  2934. }
  2935. /** @type {?} */
  2936. var nativeElement = coerceElement(this.element);
  2937. // The `ClientRect`, that we're using to find the container over which the user is
  2938. // hovering, doesn't give us any information on whether the element has been scrolled
  2939. // out of the view or whether it's overlapping with other containers. This means that
  2940. // we could end up transferring the item into a container that's invisible or is positioned
  2941. // below another one. We use the result from `elementFromPoint` to get the top-most element
  2942. // at the pointer position and to find whether it's one of the intersecting drop containers.
  2943. return elementFromPoint === nativeElement || nativeElement.contains(elementFromPoint);
  2944. };
  2945. /**
  2946. * Called by one of the connected drop lists when a dragging sequence has started.
  2947. * @param sibling Sibling in which dragging has started.
  2948. */
  2949. /**
  2950. * Called by one of the connected drop lists when a dragging sequence has started.
  2951. * @param {?} sibling Sibling in which dragging has started.
  2952. * @return {?}
  2953. */
  2954. DropListRef.prototype._startReceiving = /**
  2955. * Called by one of the connected drop lists when a dragging sequence has started.
  2956. * @param {?} sibling Sibling in which dragging has started.
  2957. * @return {?}
  2958. */
  2959. function (sibling) {
  2960. /** @type {?} */
  2961. var activeSiblings = this._activeSiblings;
  2962. if (!activeSiblings.has(sibling)) {
  2963. activeSiblings.add(sibling);
  2964. this._cacheOwnPosition();
  2965. this._listenToScrollEvents();
  2966. }
  2967. };
  2968. /**
  2969. * Called by a connected drop list when dragging has stopped.
  2970. * @param sibling Sibling whose dragging has stopped.
  2971. */
  2972. /**
  2973. * Called by a connected drop list when dragging has stopped.
  2974. * @param {?} sibling Sibling whose dragging has stopped.
  2975. * @return {?}
  2976. */
  2977. DropListRef.prototype._stopReceiving = /**
  2978. * Called by a connected drop list when dragging has stopped.
  2979. * @param {?} sibling Sibling whose dragging has stopped.
  2980. * @return {?}
  2981. */
  2982. function (sibling) {
  2983. this._activeSiblings.delete(sibling);
  2984. this._viewportScrollSubscription.unsubscribe();
  2985. };
  2986. /**
  2987. * Starts listening to scroll events on the viewport.
  2988. * Used for updating the internal state of the list.
  2989. */
  2990. /**
  2991. * Starts listening to scroll events on the viewport.
  2992. * Used for updating the internal state of the list.
  2993. * @private
  2994. * @return {?}
  2995. */
  2996. DropListRef.prototype._listenToScrollEvents = /**
  2997. * Starts listening to scroll events on the viewport.
  2998. * Used for updating the internal state of the list.
  2999. * @private
  3000. * @return {?}
  3001. */
  3002. function () {
  3003. var _this = this;
  3004. this._viewportScrollPosition = (/** @type {?} */ (this._viewportRuler)).getViewportScrollPosition();
  3005. this._viewportScrollSubscription = this._dragDropRegistry.scroll.subscribe((/**
  3006. * @return {?}
  3007. */
  3008. function () {
  3009. if (_this.isDragging()) {
  3010. /** @type {?} */
  3011. var newPosition = (/** @type {?} */ (_this._viewportRuler)).getViewportScrollPosition();
  3012. _this._updateAfterScroll(_this._viewportScrollPosition, newPosition.top, newPosition.left, _this._clientRect);
  3013. }
  3014. else if (_this.isReceiving()) {
  3015. _this._cacheOwnPosition();
  3016. }
  3017. }));
  3018. };
  3019. return DropListRef;
  3020. }());
  3021. /**
  3022. * Updates the top/left positions of a `ClientRect`, as well as their bottom/right counterparts.
  3023. * @param {?} clientRect `ClientRect` that should be updated.
  3024. * @param {?} top Amount to add to the `top` position.
  3025. * @param {?} left Amount to add to the `left` position.
  3026. * @return {?}
  3027. */
  3028. function adjustClientRect(clientRect, top, left) {
  3029. clientRect.top += top;
  3030. clientRect.bottom = clientRect.top + clientRect.height;
  3031. clientRect.left += left;
  3032. clientRect.right = clientRect.left + clientRect.width;
  3033. }
  3034. /**
  3035. * Finds the index of an item that matches a predicate function. Used as an equivalent
  3036. * of `Array.prototype.findIndex` which isn't part of the standard Google typings.
  3037. * @template T
  3038. * @param {?} array Array in which to look for matches.
  3039. * @param {?} predicate Function used to determine whether an item is a match.
  3040. * @return {?}
  3041. */
  3042. function findIndex(array, predicate) {
  3043. for (var i = 0; i < array.length; i++) {
  3044. if (predicate(array[i], i, array)) {
  3045. return i;
  3046. }
  3047. }
  3048. return -1;
  3049. }
  3050. /**
  3051. * Checks whether some coordinates are within a `ClientRect`.
  3052. * @param {?} clientRect ClientRect that is being checked.
  3053. * @param {?} x Coordinates along the X axis.
  3054. * @param {?} y Coordinates along the Y axis.
  3055. * @return {?}
  3056. */
  3057. function isInsideClientRect(clientRect, x, y) {
  3058. var top = clientRect.top, bottom = clientRect.bottom, left = clientRect.left, right = clientRect.right;
  3059. return y >= top && y <= bottom && x >= left && x <= right;
  3060. }
  3061. /**
  3062. * Gets a mutable version of an element's bounding `ClientRect`.
  3063. * @param {?} element
  3064. * @return {?}
  3065. */
  3066. function getMutableClientRect(element) {
  3067. /** @type {?} */
  3068. var clientRect = element.getBoundingClientRect();
  3069. // We need to clone the `clientRect` here, because all the values on it are readonly
  3070. // and we need to be able to update them. Also we can't use a spread here, because
  3071. // the values on a `ClientRect` aren't own properties. See:
  3072. // https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect#Notes
  3073. return {
  3074. top: clientRect.top,
  3075. right: clientRect.right,
  3076. bottom: clientRect.bottom,
  3077. left: clientRect.left,
  3078. width: clientRect.width,
  3079. height: clientRect.height
  3080. };
  3081. }
  3082. /**
  3083. * Increments the vertical scroll position of a node.
  3084. * @param {?} node Node whose scroll position should change.
  3085. * @param {?} amount Amount of pixels that the `node` should be scrolled.
  3086. * @return {?}
  3087. */
  3088. function incrementVerticalScroll(node, amount) {
  3089. if (node === window) {
  3090. ((/** @type {?} */ (node))).scrollBy(0, amount);
  3091. }
  3092. else {
  3093. // Ideally we could use `Element.scrollBy` here as well, but IE and Edge don't support it.
  3094. ((/** @type {?} */ (node))).scrollTop += amount;
  3095. }
  3096. }
  3097. /**
  3098. * Increments the horizontal scroll position of a node.
  3099. * @param {?} node Node whose scroll position should change.
  3100. * @param {?} amount Amount of pixels that the `node` should be scrolled.
  3101. * @return {?}
  3102. */
  3103. function incrementHorizontalScroll(node, amount) {
  3104. if (node === window) {
  3105. ((/** @type {?} */ (node))).scrollBy(amount, 0);
  3106. }
  3107. else {
  3108. // Ideally we could use `Element.scrollBy` here as well, but IE and Edge don't support it.
  3109. ((/** @type {?} */ (node))).scrollLeft += amount;
  3110. }
  3111. }
  3112. /**
  3113. * Gets whether the vertical auto-scroll direction of a node.
  3114. * @param {?} clientRect Dimensions of the node.
  3115. * @param {?} pointerY Position of the user's pointer along the y axis.
  3116. * @return {?}
  3117. */
  3118. function getVerticalScrollDirection(clientRect, pointerY) {
  3119. var top = clientRect.top, bottom = clientRect.bottom, height = clientRect.height;
  3120. /** @type {?} */
  3121. var yThreshold = height * SCROLL_PROXIMITY_THRESHOLD;
  3122. if (pointerY >= top - yThreshold && pointerY <= top + yThreshold) {
  3123. return 1 /* UP */;
  3124. }
  3125. else if (pointerY >= bottom - yThreshold && pointerY <= bottom + yThreshold) {
  3126. return 2 /* DOWN */;
  3127. }
  3128. return 0 /* NONE */;
  3129. }
  3130. /**
  3131. * Gets whether the horizontal auto-scroll direction of a node.
  3132. * @param {?} clientRect Dimensions of the node.
  3133. * @param {?} pointerX Position of the user's pointer along the x axis.
  3134. * @return {?}
  3135. */
  3136. function getHorizontalScrollDirection(clientRect, pointerX) {
  3137. var left = clientRect.left, right = clientRect.right, width = clientRect.width;
  3138. /** @type {?} */
  3139. var xThreshold = width * SCROLL_PROXIMITY_THRESHOLD;
  3140. if (pointerX >= left - xThreshold && pointerX <= left + xThreshold) {
  3141. return 1 /* LEFT */;
  3142. }
  3143. else if (pointerX >= right - xThreshold && pointerX <= right + xThreshold) {
  3144. return 2 /* RIGHT */;
  3145. }
  3146. return 0 /* NONE */;
  3147. }
  3148. /**
  3149. * Gets the directions in which an element node should be scrolled,
  3150. * assuming that the user's pointer is already within it scrollable region.
  3151. * @param {?} element Element for which we should calculate the scroll direction.
  3152. * @param {?} clientRect Bounding client rectangle of the element.
  3153. * @param {?} pointerX Position of the user's pointer along the x axis.
  3154. * @param {?} pointerY Position of the user's pointer along the y axis.
  3155. * @return {?}
  3156. */
  3157. function getElementScrollDirections(element, clientRect, pointerX, pointerY) {
  3158. /** @type {?} */
  3159. var computedVertical = getVerticalScrollDirection(clientRect, pointerY);
  3160. /** @type {?} */
  3161. var computedHorizontal = getHorizontalScrollDirection(clientRect, pointerX);
  3162. /** @type {?} */
  3163. var verticalScrollDirection = 0 /* NONE */;
  3164. /** @type {?} */
  3165. var horizontalScrollDirection = 0 /* NONE */;
  3166. // Note that we here we do some extra checks for whether the element is actually scrollable in
  3167. // a certain direction and we only assign the scroll direction if it is. We do this so that we
  3168. // can allow other elements to be scrolled, if the current element can't be scrolled anymore.
  3169. // This allows us to handle cases where the scroll regions of two scrollable elements overlap.
  3170. if (computedVertical) {
  3171. /** @type {?} */
  3172. var scrollTop = element.scrollTop;
  3173. if (computedVertical === 1 /* UP */) {
  3174. if (scrollTop > 0) {
  3175. verticalScrollDirection = 1 /* UP */;
  3176. }
  3177. }
  3178. else if (element.scrollHeight - scrollTop > element.clientHeight) {
  3179. verticalScrollDirection = 2 /* DOWN */;
  3180. }
  3181. }
  3182. if (computedHorizontal) {
  3183. /** @type {?} */
  3184. var scrollLeft = element.scrollLeft;
  3185. if (computedHorizontal === 1 /* LEFT */) {
  3186. if (scrollLeft > 0) {
  3187. horizontalScrollDirection = 1 /* LEFT */;
  3188. }
  3189. }
  3190. else if (element.scrollWidth - scrollLeft > element.clientWidth) {
  3191. horizontalScrollDirection = 2 /* RIGHT */;
  3192. }
  3193. }
  3194. return [verticalScrollDirection, horizontalScrollDirection];
  3195. }
  3196. /**
  3197. * Gets the shadow root of an element, if any.
  3198. * @param {?} element
  3199. * @return {?}
  3200. */
  3201. function getShadowRoot(element) {
  3202. if (_supportsShadowDom()) {
  3203. /** @type {?} */
  3204. var rootNode = element.getRootNode ? element.getRootNode() : null;
  3205. if (rootNode instanceof ShadowRoot) {
  3206. return rootNode;
  3207. }
  3208. }
  3209. return null;
  3210. }
  3211. /**
  3212. * @fileoverview added by tsickle
  3213. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3214. */
  3215. /**
  3216. * Event options that can be used to bind an active, capturing event.
  3217. * @type {?}
  3218. */
  3219. var activeCapturingEventOptions = normalizePassiveListenerOptions({
  3220. passive: false,
  3221. capture: true
  3222. });
  3223. /**
  3224. * Service that keeps track of all the drag item and drop container
  3225. * instances, and manages global event listeners on the `document`.
  3226. * \@docs-private
  3227. * @template I, C
  3228. */
  3229. // Note: this class is generic, rather than referencing CdkDrag and CdkDropList directly, in order
  3230. // to avoid circular imports. If we were to reference them here, importing the registry into the
  3231. // classes that are registering themselves will introduce a circular import.
  3232. var DragDropRegistry = /** @class */ (function () {
  3233. function DragDropRegistry(_ngZone, _document) {
  3234. var _this = this;
  3235. this._ngZone = _ngZone;
  3236. /**
  3237. * Registered drop container instances.
  3238. */
  3239. this._dropInstances = new Set();
  3240. /**
  3241. * Registered drag item instances.
  3242. */
  3243. this._dragInstances = new Set();
  3244. /**
  3245. * Drag item instances that are currently being dragged.
  3246. */
  3247. this._activeDragInstances = new Set();
  3248. /**
  3249. * Keeps track of the event listeners that we've bound to the `document`.
  3250. */
  3251. this._globalListeners = new Map();
  3252. /**
  3253. * Emits the `touchmove` or `mousemove` events that are dispatched
  3254. * while the user is dragging a drag item instance.
  3255. */
  3256. this.pointerMove = new Subject();
  3257. /**
  3258. * Emits the `touchend` or `mouseup` events that are dispatched
  3259. * while the user is dragging a drag item instance.
  3260. */
  3261. this.pointerUp = new Subject();
  3262. /**
  3263. * Emits when the viewport has been scrolled while the user is dragging an item.
  3264. */
  3265. this.scroll = new Subject();
  3266. /**
  3267. * Event listener that will prevent the default browser action while the user is dragging.
  3268. * @param event Event whose default action should be prevented.
  3269. */
  3270. this._preventDefaultWhileDragging = (/**
  3271. * @param {?} event
  3272. * @return {?}
  3273. */
  3274. function (event) {
  3275. if (_this._activeDragInstances.size) {
  3276. event.preventDefault();
  3277. }
  3278. });
  3279. this._document = _document;
  3280. }
  3281. /** Adds a drop container to the registry. */
  3282. /**
  3283. * Adds a drop container to the registry.
  3284. * @param {?} drop
  3285. * @return {?}
  3286. */
  3287. DragDropRegistry.prototype.registerDropContainer = /**
  3288. * Adds a drop container to the registry.
  3289. * @param {?} drop
  3290. * @return {?}
  3291. */
  3292. function (drop) {
  3293. if (!this._dropInstances.has(drop)) {
  3294. if (this.getDropContainer(drop.id)) {
  3295. throw Error("Drop instance with id \"" + drop.id + "\" has already been registered.");
  3296. }
  3297. this._dropInstances.add(drop);
  3298. }
  3299. };
  3300. /** Adds a drag item instance to the registry. */
  3301. /**
  3302. * Adds a drag item instance to the registry.
  3303. * @param {?} drag
  3304. * @return {?}
  3305. */
  3306. DragDropRegistry.prototype.registerDragItem = /**
  3307. * Adds a drag item instance to the registry.
  3308. * @param {?} drag
  3309. * @return {?}
  3310. */
  3311. function (drag) {
  3312. var _this = this;
  3313. this._dragInstances.add(drag);
  3314. // The `touchmove` event gets bound once, ahead of time, because WebKit
  3315. // won't preventDefault on a dynamically-added `touchmove` listener.
  3316. // See https://bugs.webkit.org/show_bug.cgi?id=184250.
  3317. if (this._dragInstances.size === 1) {
  3318. this._ngZone.runOutsideAngular((/**
  3319. * @return {?}
  3320. */
  3321. function () {
  3322. // The event handler has to be explicitly active,
  3323. // because newer browsers make it passive by default.
  3324. _this._document.addEventListener('touchmove', _this._preventDefaultWhileDragging, activeCapturingEventOptions);
  3325. }));
  3326. }
  3327. };
  3328. /** Removes a drop container from the registry. */
  3329. /**
  3330. * Removes a drop container from the registry.
  3331. * @param {?} drop
  3332. * @return {?}
  3333. */
  3334. DragDropRegistry.prototype.removeDropContainer = /**
  3335. * Removes a drop container from the registry.
  3336. * @param {?} drop
  3337. * @return {?}
  3338. */
  3339. function (drop) {
  3340. this._dropInstances.delete(drop);
  3341. };
  3342. /** Removes a drag item instance from the registry. */
  3343. /**
  3344. * Removes a drag item instance from the registry.
  3345. * @param {?} drag
  3346. * @return {?}
  3347. */
  3348. DragDropRegistry.prototype.removeDragItem = /**
  3349. * Removes a drag item instance from the registry.
  3350. * @param {?} drag
  3351. * @return {?}
  3352. */
  3353. function (drag) {
  3354. this._dragInstances.delete(drag);
  3355. this.stopDragging(drag);
  3356. if (this._dragInstances.size === 0) {
  3357. this._document.removeEventListener('touchmove', this._preventDefaultWhileDragging, activeCapturingEventOptions);
  3358. }
  3359. };
  3360. /**
  3361. * Starts the dragging sequence for a drag instance.
  3362. * @param drag Drag instance which is being dragged.
  3363. * @param event Event that initiated the dragging.
  3364. */
  3365. /**
  3366. * Starts the dragging sequence for a drag instance.
  3367. * @param {?} drag Drag instance which is being dragged.
  3368. * @param {?} event Event that initiated the dragging.
  3369. * @return {?}
  3370. */
  3371. DragDropRegistry.prototype.startDragging = /**
  3372. * Starts the dragging sequence for a drag instance.
  3373. * @param {?} drag Drag instance which is being dragged.
  3374. * @param {?} event Event that initiated the dragging.
  3375. * @return {?}
  3376. */
  3377. function (drag, event) {
  3378. var _this = this;
  3379. // Do not process the same drag twice to avoid memory leaks and redundant listeners
  3380. if (this._activeDragInstances.has(drag)) {
  3381. return;
  3382. }
  3383. this._activeDragInstances.add(drag);
  3384. if (this._activeDragInstances.size === 1) {
  3385. /** @type {?} */
  3386. var isTouchEvent = event.type.startsWith('touch');
  3387. /** @type {?} */
  3388. var moveEvent = isTouchEvent ? 'touchmove' : 'mousemove';
  3389. /** @type {?} */
  3390. var upEvent = isTouchEvent ? 'touchend' : 'mouseup';
  3391. // We explicitly bind __active__ listeners here, because newer browsers will default to
  3392. // passive ones for `mousemove` and `touchmove`. The events need to be active, because we
  3393. // use `preventDefault` to prevent the page from scrolling while the user is dragging.
  3394. this._globalListeners
  3395. .set(moveEvent, {
  3396. handler: (/**
  3397. * @param {?} e
  3398. * @return {?}
  3399. */
  3400. function (e) { return _this.pointerMove.next((/** @type {?} */ (e))); }),
  3401. options: activeCapturingEventOptions
  3402. })
  3403. .set(upEvent, {
  3404. handler: (/**
  3405. * @param {?} e
  3406. * @return {?}
  3407. */
  3408. function (e) { return _this.pointerUp.next((/** @type {?} */ (e))); }),
  3409. options: true
  3410. })
  3411. .set('scroll', {
  3412. handler: (/**
  3413. * @param {?} e
  3414. * @return {?}
  3415. */
  3416. function (e) { return _this.scroll.next(e); }),
  3417. // Use capturing so that we pick up scroll changes in any scrollable nodes that aren't
  3418. // the document. See https://github.com/angular/components/issues/17144.
  3419. options: true
  3420. })
  3421. // Preventing the default action on `mousemove` isn't enough to disable text selection
  3422. // on Safari so we need to prevent the selection event as well. Alternatively this can
  3423. // be done by setting `user-select: none` on the `body`, however it has causes a style
  3424. // recalculation which can be expensive on pages with a lot of elements.
  3425. .set('selectstart', {
  3426. handler: this._preventDefaultWhileDragging,
  3427. options: activeCapturingEventOptions
  3428. });
  3429. this._ngZone.runOutsideAngular((/**
  3430. * @return {?}
  3431. */
  3432. function () {
  3433. _this._globalListeners.forEach((/**
  3434. * @param {?} config
  3435. * @param {?} name
  3436. * @return {?}
  3437. */
  3438. function (config, name) {
  3439. _this._document.addEventListener(name, config.handler, config.options);
  3440. }));
  3441. }));
  3442. }
  3443. };
  3444. /** Stops dragging a drag item instance. */
  3445. /**
  3446. * Stops dragging a drag item instance.
  3447. * @param {?} drag
  3448. * @return {?}
  3449. */
  3450. DragDropRegistry.prototype.stopDragging = /**
  3451. * Stops dragging a drag item instance.
  3452. * @param {?} drag
  3453. * @return {?}
  3454. */
  3455. function (drag) {
  3456. this._activeDragInstances.delete(drag);
  3457. if (this._activeDragInstances.size === 0) {
  3458. this._clearGlobalListeners();
  3459. }
  3460. };
  3461. /** Gets whether a drag item instance is currently being dragged. */
  3462. /**
  3463. * Gets whether a drag item instance is currently being dragged.
  3464. * @param {?} drag
  3465. * @return {?}
  3466. */
  3467. DragDropRegistry.prototype.isDragging = /**
  3468. * Gets whether a drag item instance is currently being dragged.
  3469. * @param {?} drag
  3470. * @return {?}
  3471. */
  3472. function (drag) {
  3473. return this._activeDragInstances.has(drag);
  3474. };
  3475. /**
  3476. * Gets a drop container by its id.
  3477. * @deprecated No longer being used. To be removed.
  3478. * @breaking-change 8.0.0
  3479. */
  3480. /**
  3481. * Gets a drop container by its id.
  3482. * @deprecated No longer being used. To be removed.
  3483. * \@breaking-change 8.0.0
  3484. * @param {?} id
  3485. * @return {?}
  3486. */
  3487. DragDropRegistry.prototype.getDropContainer = /**
  3488. * Gets a drop container by its id.
  3489. * @deprecated No longer being used. To be removed.
  3490. * \@breaking-change 8.0.0
  3491. * @param {?} id
  3492. * @return {?}
  3493. */
  3494. function (id) {
  3495. return Array.from(this._dropInstances).find((/**
  3496. * @param {?} instance
  3497. * @return {?}
  3498. */
  3499. function (instance) { return instance.id === id; }));
  3500. };
  3501. /**
  3502. * @return {?}
  3503. */
  3504. DragDropRegistry.prototype.ngOnDestroy = /**
  3505. * @return {?}
  3506. */
  3507. function () {
  3508. var _this = this;
  3509. this._dragInstances.forEach((/**
  3510. * @param {?} instance
  3511. * @return {?}
  3512. */
  3513. function (instance) { return _this.removeDragItem(instance); }));
  3514. this._dropInstances.forEach((/**
  3515. * @param {?} instance
  3516. * @return {?}
  3517. */
  3518. function (instance) { return _this.removeDropContainer(instance); }));
  3519. this._clearGlobalListeners();
  3520. this.pointerMove.complete();
  3521. this.pointerUp.complete();
  3522. };
  3523. /** Clears out the global event listeners from the `document`. */
  3524. /**
  3525. * Clears out the global event listeners from the `document`.
  3526. * @private
  3527. * @return {?}
  3528. */
  3529. DragDropRegistry.prototype._clearGlobalListeners = /**
  3530. * Clears out the global event listeners from the `document`.
  3531. * @private
  3532. * @return {?}
  3533. */
  3534. function () {
  3535. var _this = this;
  3536. this._globalListeners.forEach((/**
  3537. * @param {?} config
  3538. * @param {?} name
  3539. * @return {?}
  3540. */
  3541. function (config, name) {
  3542. _this._document.removeEventListener(name, config.handler, config.options);
  3543. }));
  3544. this._globalListeners.clear();
  3545. };
  3546. DragDropRegistry.decorators = [
  3547. { type: Injectable, args: [{ providedIn: 'root' },] },
  3548. ];
  3549. /** @nocollapse */
  3550. DragDropRegistry.ctorParameters = function () { return [
  3551. { type: NgZone },
  3552. { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] }
  3553. ]; };
  3554. /** @nocollapse */ DragDropRegistry.ngInjectableDef = ɵɵdefineInjectable({ factory: function DragDropRegistry_Factory() { return new DragDropRegistry(ɵɵinject(NgZone), ɵɵinject(DOCUMENT)); }, token: DragDropRegistry, providedIn: "root" });
  3555. return DragDropRegistry;
  3556. }());
  3557. /**
  3558. * @fileoverview added by tsickle
  3559. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3560. */
  3561. /**
  3562. * Default configuration to be used when creating a `DragRef`.
  3563. * @type {?}
  3564. */
  3565. var DEFAULT_CONFIG = {
  3566. dragStartThreshold: 5,
  3567. pointerDirectionChangeThreshold: 5
  3568. };
  3569. /**
  3570. * Service that allows for drag-and-drop functionality to be attached to DOM elements.
  3571. */
  3572. var DragDrop = /** @class */ (function () {
  3573. function DragDrop(_document, _ngZone, _viewportRuler, _dragDropRegistry) {
  3574. this._document = _document;
  3575. this._ngZone = _ngZone;
  3576. this._viewportRuler = _viewportRuler;
  3577. this._dragDropRegistry = _dragDropRegistry;
  3578. }
  3579. /**
  3580. * Turns an element into a draggable item.
  3581. * @param element Element to which to attach the dragging functionality.
  3582. * @param config Object used to configure the dragging behavior.
  3583. */
  3584. /**
  3585. * Turns an element into a draggable item.
  3586. * @template T
  3587. * @param {?} element Element to which to attach the dragging functionality.
  3588. * @param {?=} config Object used to configure the dragging behavior.
  3589. * @return {?}
  3590. */
  3591. DragDrop.prototype.createDrag = /**
  3592. * Turns an element into a draggable item.
  3593. * @template T
  3594. * @param {?} element Element to which to attach the dragging functionality.
  3595. * @param {?=} config Object used to configure the dragging behavior.
  3596. * @return {?}
  3597. */
  3598. function (element, config) {
  3599. if (config === void 0) { config = DEFAULT_CONFIG; }
  3600. return new DragRef(element, config, this._document, this._ngZone, this._viewportRuler, this._dragDropRegistry);
  3601. };
  3602. /**
  3603. * Turns an element into a drop list.
  3604. * @param element Element to which to attach the drop list functionality.
  3605. */
  3606. /**
  3607. * Turns an element into a drop list.
  3608. * @template T
  3609. * @param {?} element Element to which to attach the drop list functionality.
  3610. * @return {?}
  3611. */
  3612. DragDrop.prototype.createDropList = /**
  3613. * Turns an element into a drop list.
  3614. * @template T
  3615. * @param {?} element Element to which to attach the drop list functionality.
  3616. * @return {?}
  3617. */
  3618. function (element) {
  3619. return new DropListRef(element, this._dragDropRegistry, this._document, this._ngZone, this._viewportRuler);
  3620. };
  3621. DragDrop.decorators = [
  3622. { type: Injectable, args: [{ providedIn: 'root' },] },
  3623. ];
  3624. /** @nocollapse */
  3625. DragDrop.ctorParameters = function () { return [
  3626. { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] },
  3627. { type: NgZone },
  3628. { type: ViewportRuler },
  3629. { type: DragDropRegistry }
  3630. ]; };
  3631. /** @nocollapse */ DragDrop.ngInjectableDef = ɵɵdefineInjectable({ factory: function DragDrop_Factory() { return new DragDrop(ɵɵinject(DOCUMENT), ɵɵinject(NgZone), ɵɵinject(ViewportRuler), ɵɵinject(DragDropRegistry)); }, token: DragDrop, providedIn: "root" });
  3632. return DragDrop;
  3633. }());
  3634. /**
  3635. * @fileoverview added by tsickle
  3636. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3637. */
  3638. /**
  3639. * Injection token that is used to provide a CdkDropList instance to CdkDrag.
  3640. * Used for avoiding circular imports.
  3641. * @type {?}
  3642. */
  3643. var CDK_DROP_LIST = new InjectionToken('CDK_DROP_LIST');
  3644. /**
  3645. * Injection token that is used to provide a CdkDropList instance to CdkDrag.
  3646. * Used for avoiding circular imports.
  3647. * @deprecated Use `CDK_DROP_LIST` instead.
  3648. * \@breaking-change 8.0.0
  3649. * @type {?}
  3650. */
  3651. var CDK_DROP_LIST_CONTAINER = CDK_DROP_LIST;
  3652. /**
  3653. * @fileoverview added by tsickle
  3654. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3655. */
  3656. /**
  3657. * @fileoverview added by tsickle
  3658. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3659. */
  3660. /**
  3661. * Injection token that can be used for a `CdkDrag` to provide itself as a parent to the
  3662. * drag-specific child directive (`CdkDragHandle`, `CdkDragPreview` etc.). Used primarily
  3663. * to avoid circular imports.
  3664. * \@docs-private
  3665. * @type {?}
  3666. */
  3667. var CDK_DRAG_PARENT = new InjectionToken('CDK_DRAG_PARENT');
  3668. /**
  3669. * @fileoverview added by tsickle
  3670. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3671. */
  3672. /**
  3673. * Handle that can be used to drag and CdkDrag instance.
  3674. */
  3675. var CdkDragHandle = /** @class */ (function () {
  3676. function CdkDragHandle(element, parentDrag) {
  3677. this.element = element;
  3678. /**
  3679. * Emits when the state of the handle has changed.
  3680. */
  3681. this._stateChanges = new Subject();
  3682. this._disabled = false;
  3683. this._parentDrag = parentDrag;
  3684. toggleNativeDragInteractions(element.nativeElement, false);
  3685. }
  3686. Object.defineProperty(CdkDragHandle.prototype, "disabled", {
  3687. /** Whether starting to drag through this handle is disabled. */
  3688. get: /**
  3689. * Whether starting to drag through this handle is disabled.
  3690. * @return {?}
  3691. */
  3692. function () { return this._disabled; },
  3693. set: /**
  3694. * @param {?} value
  3695. * @return {?}
  3696. */
  3697. function (value) {
  3698. this._disabled = coerceBooleanProperty(value);
  3699. this._stateChanges.next(this);
  3700. },
  3701. enumerable: true,
  3702. configurable: true
  3703. });
  3704. /**
  3705. * @return {?}
  3706. */
  3707. CdkDragHandle.prototype.ngOnDestroy = /**
  3708. * @return {?}
  3709. */
  3710. function () {
  3711. this._stateChanges.complete();
  3712. };
  3713. CdkDragHandle.decorators = [
  3714. { type: Directive, args: [{
  3715. selector: '[cdkDragHandle]',
  3716. host: {
  3717. 'class': 'cdk-drag-handle'
  3718. }
  3719. },] },
  3720. ];
  3721. /** @nocollapse */
  3722. CdkDragHandle.ctorParameters = function () { return [
  3723. { type: ElementRef },
  3724. { type: undefined, decorators: [{ type: Inject, args: [CDK_DRAG_PARENT,] }, { type: Optional }] }
  3725. ]; };
  3726. CdkDragHandle.propDecorators = {
  3727. disabled: [{ type: Input, args: ['cdkDragHandleDisabled',] }]
  3728. };
  3729. return CdkDragHandle;
  3730. }());
  3731. /**
  3732. * @fileoverview added by tsickle
  3733. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3734. */
  3735. /**
  3736. * Element that will be used as a template for the placeholder of a CdkDrag when
  3737. * it is being dragged. The placeholder is displayed in place of the element being dragged.
  3738. * @template T
  3739. */
  3740. var CdkDragPlaceholder = /** @class */ (function () {
  3741. function CdkDragPlaceholder(templateRef) {
  3742. this.templateRef = templateRef;
  3743. }
  3744. CdkDragPlaceholder.decorators = [
  3745. { type: Directive, args: [{
  3746. selector: 'ng-template[cdkDragPlaceholder]'
  3747. },] },
  3748. ];
  3749. /** @nocollapse */
  3750. CdkDragPlaceholder.ctorParameters = function () { return [
  3751. { type: TemplateRef }
  3752. ]; };
  3753. CdkDragPlaceholder.propDecorators = {
  3754. data: [{ type: Input }]
  3755. };
  3756. return CdkDragPlaceholder;
  3757. }());
  3758. /**
  3759. * @fileoverview added by tsickle
  3760. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3761. */
  3762. /**
  3763. * Element that will be used as a template for the preview
  3764. * of a CdkDrag when it is being dragged.
  3765. * @template T
  3766. */
  3767. var CdkDragPreview = /** @class */ (function () {
  3768. function CdkDragPreview(templateRef) {
  3769. this.templateRef = templateRef;
  3770. }
  3771. CdkDragPreview.decorators = [
  3772. { type: Directive, args: [{
  3773. selector: 'ng-template[cdkDragPreview]'
  3774. },] },
  3775. ];
  3776. /** @nocollapse */
  3777. CdkDragPreview.ctorParameters = function () { return [
  3778. { type: TemplateRef }
  3779. ]; };
  3780. CdkDragPreview.propDecorators = {
  3781. data: [{ type: Input }]
  3782. };
  3783. return CdkDragPreview;
  3784. }());
  3785. /**
  3786. * @fileoverview added by tsickle
  3787. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  3788. */
  3789. /**
  3790. * Injection token that can be used to configure the behavior of `CdkDrag`.
  3791. * @type {?}
  3792. */
  3793. var CDK_DRAG_CONFIG = new InjectionToken('CDK_DRAG_CONFIG', {
  3794. providedIn: 'root',
  3795. factory: CDK_DRAG_CONFIG_FACTORY
  3796. });
  3797. /**
  3798. * \@docs-private
  3799. * @return {?}
  3800. */
  3801. function CDK_DRAG_CONFIG_FACTORY() {
  3802. return { dragStartThreshold: 5, pointerDirectionChangeThreshold: 5 };
  3803. }
  3804. /**
  3805. * Element that can be moved inside a CdkDropList container.
  3806. * @template T
  3807. */
  3808. var CdkDrag = /** @class */ (function () {
  3809. function CdkDrag(element, dropContainer, _document, _ngZone, _viewContainerRef, config, _dir, dragDrop, _changeDetectorRef) {
  3810. var _this = this;
  3811. this.element = element;
  3812. this.dropContainer = dropContainer;
  3813. this._document = _document;
  3814. this._ngZone = _ngZone;
  3815. this._viewContainerRef = _viewContainerRef;
  3816. this._dir = _dir;
  3817. this._changeDetectorRef = _changeDetectorRef;
  3818. this._destroyed = new Subject();
  3819. /**
  3820. * Amount of milliseconds to wait after the user has put their
  3821. * pointer down before starting to drag the element.
  3822. */
  3823. this.dragStartDelay = 0;
  3824. this._disabled = false;
  3825. /**
  3826. * Emits when the user starts dragging the item.
  3827. */
  3828. this.started = new EventEmitter();
  3829. /**
  3830. * Emits when the user has released a drag item, before any animations have started.
  3831. */
  3832. this.released = new EventEmitter();
  3833. /**
  3834. * Emits when the user stops dragging an item in the container.
  3835. */
  3836. this.ended = new EventEmitter();
  3837. /**
  3838. * Emits when the user has moved the item into a new container.
  3839. */
  3840. this.entered = new EventEmitter();
  3841. /**
  3842. * Emits when the user removes the item its container by dragging it into another container.
  3843. */
  3844. this.exited = new EventEmitter();
  3845. /**
  3846. * Emits when the user drops the item inside a container.
  3847. */
  3848. this.dropped = new EventEmitter();
  3849. /**
  3850. * Emits as the user is dragging the item. Use with caution,
  3851. * because this event will fire for every pixel that the user has dragged.
  3852. */
  3853. this.moved = new Observable((/**
  3854. * @param {?} observer
  3855. * @return {?}
  3856. */
  3857. function (observer) {
  3858. /** @type {?} */
  3859. var subscription = _this._dragRef.moved.pipe(map((/**
  3860. * @param {?} movedEvent
  3861. * @return {?}
  3862. */
  3863. function (movedEvent) { return ({
  3864. source: _this,
  3865. pointerPosition: movedEvent.pointerPosition,
  3866. event: movedEvent.event,
  3867. delta: movedEvent.delta,
  3868. distance: movedEvent.distance
  3869. }); }))).subscribe(observer);
  3870. return (/**
  3871. * @return {?}
  3872. */
  3873. function () {
  3874. subscription.unsubscribe();
  3875. });
  3876. }));
  3877. this._dragRef = dragDrop.createDrag(element, config);
  3878. this._dragRef.data = this;
  3879. this._syncInputs(this._dragRef);
  3880. this._handleEvents(this._dragRef);
  3881. }
  3882. Object.defineProperty(CdkDrag.prototype, "boundaryElementSelector", {
  3883. /**
  3884. * Selector that will be used to determine the element to which the draggable's position will
  3885. * be constrained. Matching starts from the element's parent and goes up the DOM until a matching
  3886. * element has been found
  3887. * @deprecated Use `boundaryElement` instead.
  3888. * @breaking-change 9.0.0
  3889. */
  3890. get: /**
  3891. * Selector that will be used to determine the element to which the draggable's position will
  3892. * be constrained. Matching starts from the element's parent and goes up the DOM until a matching
  3893. * element has been found
  3894. * @deprecated Use `boundaryElement` instead.
  3895. * \@breaking-change 9.0.0
  3896. * @return {?}
  3897. */
  3898. function () {
  3899. return typeof this.boundaryElement === 'string' ? this.boundaryElement : (/** @type {?} */ (undefined));
  3900. },
  3901. set: /**
  3902. * @param {?} selector
  3903. * @return {?}
  3904. */
  3905. function (selector) {
  3906. this.boundaryElement = selector;
  3907. },
  3908. enumerable: true,
  3909. configurable: true
  3910. });
  3911. Object.defineProperty(CdkDrag.prototype, "disabled", {
  3912. /** Whether starting to drag this element is disabled. */
  3913. get: /**
  3914. * Whether starting to drag this element is disabled.
  3915. * @return {?}
  3916. */
  3917. function () {
  3918. return this._disabled || (this.dropContainer && this.dropContainer.disabled);
  3919. },
  3920. set: /**
  3921. * @param {?} value
  3922. * @return {?}
  3923. */
  3924. function (value) {
  3925. this._disabled = coerceBooleanProperty(value);
  3926. this._dragRef.disabled = this._disabled;
  3927. },
  3928. enumerable: true,
  3929. configurable: true
  3930. });
  3931. /**
  3932. * Returns the element that is being used as a placeholder
  3933. * while the current element is being dragged.
  3934. */
  3935. /**
  3936. * Returns the element that is being used as a placeholder
  3937. * while the current element is being dragged.
  3938. * @return {?}
  3939. */
  3940. CdkDrag.prototype.getPlaceholderElement = /**
  3941. * Returns the element that is being used as a placeholder
  3942. * while the current element is being dragged.
  3943. * @return {?}
  3944. */
  3945. function () {
  3946. return this._dragRef.getPlaceholderElement();
  3947. };
  3948. /** Returns the root draggable element. */
  3949. /**
  3950. * Returns the root draggable element.
  3951. * @return {?}
  3952. */
  3953. CdkDrag.prototype.getRootElement = /**
  3954. * Returns the root draggable element.
  3955. * @return {?}
  3956. */
  3957. function () {
  3958. return this._dragRef.getRootElement();
  3959. };
  3960. /** Resets a standalone drag item to its initial position. */
  3961. /**
  3962. * Resets a standalone drag item to its initial position.
  3963. * @return {?}
  3964. */
  3965. CdkDrag.prototype.reset = /**
  3966. * Resets a standalone drag item to its initial position.
  3967. * @return {?}
  3968. */
  3969. function () {
  3970. this._dragRef.reset();
  3971. };
  3972. /**
  3973. * Gets the pixel coordinates of the draggable outside of a drop container.
  3974. */
  3975. /**
  3976. * Gets the pixel coordinates of the draggable outside of a drop container.
  3977. * @return {?}
  3978. */
  3979. CdkDrag.prototype.getFreeDragPosition = /**
  3980. * Gets the pixel coordinates of the draggable outside of a drop container.
  3981. * @return {?}
  3982. */
  3983. function () {
  3984. return this._dragRef.getFreeDragPosition();
  3985. };
  3986. /**
  3987. * @return {?}
  3988. */
  3989. CdkDrag.prototype.ngAfterViewInit = /**
  3990. * @return {?}
  3991. */
  3992. function () {
  3993. var _this = this;
  3994. // We need to wait for the zone to stabilize, in order for the reference
  3995. // element to be in the proper place in the DOM. This is mostly relevant
  3996. // for draggable elements inside portals since they get stamped out in
  3997. // their original DOM position and then they get transferred to the portal.
  3998. this._ngZone.onStable.asObservable()
  3999. .pipe(take(1), takeUntil(this._destroyed))
  4000. .subscribe((/**
  4001. * @return {?}
  4002. */
  4003. function () {
  4004. _this._updateRootElement();
  4005. // Listen for any newly-added handles.
  4006. _this._handles.changes.pipe(startWith(_this._handles),
  4007. // Sync the new handles with the DragRef.
  4008. tap((/**
  4009. * @param {?} handles
  4010. * @return {?}
  4011. */
  4012. function (handles) {
  4013. /** @type {?} */
  4014. var childHandleElements = handles
  4015. .filter((/**
  4016. * @param {?} handle
  4017. * @return {?}
  4018. */
  4019. function (handle) { return handle._parentDrag === _this; }))
  4020. .map((/**
  4021. * @param {?} handle
  4022. * @return {?}
  4023. */
  4024. function (handle) { return handle.element; }));
  4025. _this._dragRef.withHandles(childHandleElements);
  4026. })),
  4027. // Listen if the state of any of the handles changes.
  4028. switchMap((/**
  4029. * @param {?} handles
  4030. * @return {?}
  4031. */
  4032. function (handles) {
  4033. return merge.apply(void 0, handles.map((/**
  4034. * @param {?} item
  4035. * @return {?}
  4036. */
  4037. function (item) { return item._stateChanges; })));
  4038. })), takeUntil(_this._destroyed)).subscribe((/**
  4039. * @param {?} handleInstance
  4040. * @return {?}
  4041. */
  4042. function (handleInstance) {
  4043. // Enabled/disable the handle that changed in the DragRef.
  4044. /** @type {?} */
  4045. var dragRef = _this._dragRef;
  4046. /** @type {?} */
  4047. var handle = handleInstance.element.nativeElement;
  4048. handleInstance.disabled ? dragRef.disableHandle(handle) : dragRef.enableHandle(handle);
  4049. }));
  4050. if (_this.freeDragPosition) {
  4051. _this._dragRef.setFreeDragPosition(_this.freeDragPosition);
  4052. }
  4053. }));
  4054. };
  4055. /**
  4056. * @param {?} changes
  4057. * @return {?}
  4058. */
  4059. CdkDrag.prototype.ngOnChanges = /**
  4060. * @param {?} changes
  4061. * @return {?}
  4062. */
  4063. function (changes) {
  4064. /** @type {?} */
  4065. var rootSelectorChange = changes['rootElementSelector'];
  4066. /** @type {?} */
  4067. var positionChange = changes['freeDragPosition'];
  4068. // We don't have to react to the first change since it's being
  4069. // handled in `ngAfterViewInit` where it needs to be deferred.
  4070. if (rootSelectorChange && !rootSelectorChange.firstChange) {
  4071. this._updateRootElement();
  4072. }
  4073. // Skip the first change since it's being handled in `ngAfterViewInit`.
  4074. if (positionChange && !positionChange.firstChange && this.freeDragPosition) {
  4075. this._dragRef.setFreeDragPosition(this.freeDragPosition);
  4076. }
  4077. };
  4078. /**
  4079. * @return {?}
  4080. */
  4081. CdkDrag.prototype.ngOnDestroy = /**
  4082. * @return {?}
  4083. */
  4084. function () {
  4085. this._destroyed.next();
  4086. this._destroyed.complete();
  4087. this._dragRef.dispose();
  4088. };
  4089. /** Syncs the root element with the `DragRef`. */
  4090. /**
  4091. * Syncs the root element with the `DragRef`.
  4092. * @private
  4093. * @return {?}
  4094. */
  4095. CdkDrag.prototype._updateRootElement = /**
  4096. * Syncs the root element with the `DragRef`.
  4097. * @private
  4098. * @return {?}
  4099. */
  4100. function () {
  4101. /** @type {?} */
  4102. var element = this.element.nativeElement;
  4103. /** @type {?} */
  4104. var rootElement = this.rootElementSelector ?
  4105. getClosestMatchingAncestor(element, this.rootElementSelector) : element;
  4106. if (rootElement && rootElement.nodeType !== this._document.ELEMENT_NODE) {
  4107. throw Error("cdkDrag must be attached to an element node. " +
  4108. ("Currently attached to \"" + rootElement.nodeName + "\"."));
  4109. }
  4110. this._dragRef.withRootElement(rootElement || element);
  4111. };
  4112. /** Gets the boundary element, based on the `boundaryElement` value. */
  4113. /**
  4114. * Gets the boundary element, based on the `boundaryElement` value.
  4115. * @private
  4116. * @return {?}
  4117. */
  4118. CdkDrag.prototype._getBoundaryElement = /**
  4119. * Gets the boundary element, based on the `boundaryElement` value.
  4120. * @private
  4121. * @return {?}
  4122. */
  4123. function () {
  4124. /** @type {?} */
  4125. var boundary = this.boundaryElement;
  4126. if (!boundary) {
  4127. return null;
  4128. }
  4129. if (typeof boundary === 'string') {
  4130. return getClosestMatchingAncestor(this.element.nativeElement, boundary);
  4131. }
  4132. /** @type {?} */
  4133. var element = coerceElement(boundary);
  4134. if (isDevMode() && !element.contains(this.element.nativeElement)) {
  4135. throw Error('Draggable element is not inside of the node passed into cdkDragBoundary.');
  4136. }
  4137. return element;
  4138. };
  4139. /** Syncs the inputs of the CdkDrag with the options of the underlying DragRef. */
  4140. /**
  4141. * Syncs the inputs of the CdkDrag with the options of the underlying DragRef.
  4142. * @private
  4143. * @param {?} ref
  4144. * @return {?}
  4145. */
  4146. CdkDrag.prototype._syncInputs = /**
  4147. * Syncs the inputs of the CdkDrag with the options of the underlying DragRef.
  4148. * @private
  4149. * @param {?} ref
  4150. * @return {?}
  4151. */
  4152. function (ref) {
  4153. var _this = this;
  4154. ref.beforeStarted.subscribe((/**
  4155. * @return {?}
  4156. */
  4157. function () {
  4158. if (!ref.isDragging()) {
  4159. /** @type {?} */
  4160. var dir = _this._dir;
  4161. /** @type {?} */
  4162. var placeholder = _this._placeholderTemplate ? {
  4163. template: _this._placeholderTemplate.templateRef,
  4164. context: _this._placeholderTemplate.data,
  4165. viewContainer: _this._viewContainerRef
  4166. } : null;
  4167. /** @type {?} */
  4168. var preview = _this._previewTemplate ? {
  4169. template: _this._previewTemplate.templateRef,
  4170. context: _this._previewTemplate.data,
  4171. viewContainer: _this._viewContainerRef
  4172. } : null;
  4173. ref.disabled = _this.disabled;
  4174. ref.lockAxis = _this.lockAxis;
  4175. ref.dragStartDelay = coerceNumberProperty(_this.dragStartDelay);
  4176. ref.constrainPosition = _this.constrainPosition;
  4177. ref
  4178. .withBoundaryElement(_this._getBoundaryElement())
  4179. .withPlaceholderTemplate(placeholder)
  4180. .withPreviewTemplate(preview);
  4181. if (dir) {
  4182. ref.withDirection(dir.value);
  4183. }
  4184. }
  4185. }));
  4186. };
  4187. /** Handles the events from the underlying `DragRef`. */
  4188. /**
  4189. * Handles the events from the underlying `DragRef`.
  4190. * @private
  4191. * @param {?} ref
  4192. * @return {?}
  4193. */
  4194. CdkDrag.prototype._handleEvents = /**
  4195. * Handles the events from the underlying `DragRef`.
  4196. * @private
  4197. * @param {?} ref
  4198. * @return {?}
  4199. */
  4200. function (ref) {
  4201. var _this = this;
  4202. ref.started.subscribe((/**
  4203. * @return {?}
  4204. */
  4205. function () {
  4206. _this.started.emit({ source: _this });
  4207. // Since all of these events run outside of change detection,
  4208. // we need to ensure that everything is marked correctly.
  4209. _this._changeDetectorRef.markForCheck();
  4210. }));
  4211. ref.released.subscribe((/**
  4212. * @return {?}
  4213. */
  4214. function () {
  4215. _this.released.emit({ source: _this });
  4216. }));
  4217. ref.ended.subscribe((/**
  4218. * @param {?} event
  4219. * @return {?}
  4220. */
  4221. function (event) {
  4222. _this.ended.emit({ source: _this, distance: event.distance });
  4223. // Since all of these events run outside of change detection,
  4224. // we need to ensure that everything is marked correctly.
  4225. _this._changeDetectorRef.markForCheck();
  4226. }));
  4227. ref.entered.subscribe((/**
  4228. * @param {?} event
  4229. * @return {?}
  4230. */
  4231. function (event) {
  4232. _this.entered.emit({
  4233. container: event.container.data,
  4234. item: _this,
  4235. currentIndex: event.currentIndex
  4236. });
  4237. }));
  4238. ref.exited.subscribe((/**
  4239. * @param {?} event
  4240. * @return {?}
  4241. */
  4242. function (event) {
  4243. _this.exited.emit({
  4244. container: event.container.data,
  4245. item: _this
  4246. });
  4247. }));
  4248. ref.dropped.subscribe((/**
  4249. * @param {?} event
  4250. * @return {?}
  4251. */
  4252. function (event) {
  4253. _this.dropped.emit({
  4254. previousIndex: event.previousIndex,
  4255. currentIndex: event.currentIndex,
  4256. previousContainer: event.previousContainer.data,
  4257. container: event.container.data,
  4258. isPointerOverContainer: event.isPointerOverContainer,
  4259. item: _this,
  4260. distance: event.distance
  4261. });
  4262. }));
  4263. };
  4264. CdkDrag.decorators = [
  4265. { type: Directive, args: [{
  4266. selector: '[cdkDrag]',
  4267. exportAs: 'cdkDrag',
  4268. host: {
  4269. 'class': 'cdk-drag',
  4270. '[class.cdk-drag-disabled]': 'disabled',
  4271. '[class.cdk-drag-dragging]': '_dragRef.isDragging()',
  4272. },
  4273. providers: [{ provide: CDK_DRAG_PARENT, useExisting: CdkDrag }]
  4274. },] },
  4275. ];
  4276. /** @nocollapse */
  4277. CdkDrag.ctorParameters = function () { return [
  4278. { type: ElementRef },
  4279. { type: undefined, decorators: [{ type: Inject, args: [CDK_DROP_LIST,] }, { type: Optional }, { type: SkipSelf }] },
  4280. { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] },
  4281. { type: NgZone },
  4282. { type: ViewContainerRef },
  4283. { type: undefined, decorators: [{ type: Inject, args: [CDK_DRAG_CONFIG,] }] },
  4284. { type: Directionality, decorators: [{ type: Optional }] },
  4285. { type: DragDrop },
  4286. { type: ChangeDetectorRef }
  4287. ]; };
  4288. CdkDrag.propDecorators = {
  4289. _handles: [{ type: ContentChildren, args: [CdkDragHandle, { descendants: true },] }],
  4290. _previewTemplate: [{ type: ContentChild, args: [CdkDragPreview, { static: false },] }],
  4291. _placeholderTemplate: [{ type: ContentChild, args: [CdkDragPlaceholder, { static: false },] }],
  4292. data: [{ type: Input, args: ['cdkDragData',] }],
  4293. lockAxis: [{ type: Input, args: ['cdkDragLockAxis',] }],
  4294. rootElementSelector: [{ type: Input, args: ['cdkDragRootElement',] }],
  4295. boundaryElement: [{ type: Input, args: ['cdkDragBoundary',] }],
  4296. dragStartDelay: [{ type: Input, args: ['cdkDragStartDelay',] }],
  4297. freeDragPosition: [{ type: Input, args: ['cdkDragFreeDragPosition',] }],
  4298. disabled: [{ type: Input, args: ['cdkDragDisabled',] }],
  4299. constrainPosition: [{ type: Input, args: ['cdkDragConstrainPosition',] }],
  4300. started: [{ type: Output, args: ['cdkDragStarted',] }],
  4301. released: [{ type: Output, args: ['cdkDragReleased',] }],
  4302. ended: [{ type: Output, args: ['cdkDragEnded',] }],
  4303. entered: [{ type: Output, args: ['cdkDragEntered',] }],
  4304. exited: [{ type: Output, args: ['cdkDragExited',] }],
  4305. dropped: [{ type: Output, args: ['cdkDragDropped',] }],
  4306. moved: [{ type: Output, args: ['cdkDragMoved',] }]
  4307. };
  4308. return CdkDrag;
  4309. }());
  4310. /**
  4311. * Gets the closest ancestor of an element that matches a selector.
  4312. * @param {?} element
  4313. * @param {?} selector
  4314. * @return {?}
  4315. */
  4316. function getClosestMatchingAncestor(element, selector) {
  4317. /** @type {?} */
  4318. var currentElement = (/** @type {?} */ (element.parentElement));
  4319. while (currentElement) {
  4320. // IE doesn't support `matches` so we have to fall back to `msMatchesSelector`.
  4321. if (currentElement.matches ? currentElement.matches(selector) :
  4322. ((/** @type {?} */ (currentElement))).msMatchesSelector(selector)) {
  4323. return currentElement;
  4324. }
  4325. currentElement = currentElement.parentElement;
  4326. }
  4327. return null;
  4328. }
  4329. /**
  4330. * @fileoverview added by tsickle
  4331. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  4332. */
  4333. /**
  4334. * Declaratively connects sibling `cdkDropList` instances together. All of the `cdkDropList`
  4335. * elements that are placed inside a `cdkDropListGroup` will be connected to each other
  4336. * automatically. Can be used as an alternative to the `cdkDropListConnectedTo` input
  4337. * from `cdkDropList`.
  4338. * @template T
  4339. */
  4340. var CdkDropListGroup = /** @class */ (function () {
  4341. function CdkDropListGroup() {
  4342. /**
  4343. * Drop lists registered inside the group.
  4344. */
  4345. this._items = new Set();
  4346. this._disabled = false;
  4347. }
  4348. Object.defineProperty(CdkDropListGroup.prototype, "disabled", {
  4349. /** Whether starting a dragging sequence from inside this group is disabled. */
  4350. get: /**
  4351. * Whether starting a dragging sequence from inside this group is disabled.
  4352. * @return {?}
  4353. */
  4354. function () { return this._disabled; },
  4355. set: /**
  4356. * @param {?} value
  4357. * @return {?}
  4358. */
  4359. function (value) {
  4360. this._disabled = coerceBooleanProperty(value);
  4361. },
  4362. enumerable: true,
  4363. configurable: true
  4364. });
  4365. /**
  4366. * @return {?}
  4367. */
  4368. CdkDropListGroup.prototype.ngOnDestroy = /**
  4369. * @return {?}
  4370. */
  4371. function () {
  4372. this._items.clear();
  4373. };
  4374. CdkDropListGroup.decorators = [
  4375. { type: Directive, args: [{
  4376. selector: '[cdkDropListGroup]',
  4377. exportAs: 'cdkDropListGroup',
  4378. },] },
  4379. ];
  4380. CdkDropListGroup.propDecorators = {
  4381. disabled: [{ type: Input, args: ['cdkDropListGroupDisabled',] }]
  4382. };
  4383. return CdkDropListGroup;
  4384. }());
  4385. /**
  4386. * @fileoverview added by tsickle
  4387. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  4388. */
  4389. /**
  4390. * Counter used to generate unique ids for drop zones.
  4391. * @type {?}
  4392. */
  4393. var _uniqueIdCounter$1 = 0;
  4394. var ɵ0 = undefined;
  4395. // @breaking-change 8.0.0 `CdkDropList` implements `CdkDropListContainer` for backwards
  4396. // compatiblity. The implements clause, as well as all the methods that it enforces can
  4397. // be removed when `CdkDropListContainer` is deleted.
  4398. /**
  4399. * Container that wraps a set of draggable items.
  4400. * @template T
  4401. */
  4402. var CdkDropList = /** @class */ (function () {
  4403. function CdkDropList(element, dragDrop, _changeDetectorRef, _dir, _group) {
  4404. var _this = this;
  4405. this.element = element;
  4406. this._changeDetectorRef = _changeDetectorRef;
  4407. this._dir = _dir;
  4408. this._group = _group;
  4409. /**
  4410. * Emits when the list has been destroyed.
  4411. */
  4412. this._destroyed = new Subject();
  4413. /**
  4414. * Other draggable containers that this container is connected to and into which the
  4415. * container's items can be transferred. Can either be references to other drop containers,
  4416. * or their unique IDs.
  4417. */
  4418. this.connectedTo = [];
  4419. /**
  4420. * Direction in which the list is oriented.
  4421. */
  4422. this.orientation = 'vertical';
  4423. /**
  4424. * Unique ID for the drop zone. Can be used as a reference
  4425. * in the `connectedTo` of another `CdkDropList`.
  4426. */
  4427. this.id = "cdk-drop-list-" + _uniqueIdCounter$1++;
  4428. this._disabled = false;
  4429. this._sortingDisabled = false;
  4430. /**
  4431. * Function that is used to determine whether an item
  4432. * is allowed to be moved into a drop container.
  4433. */
  4434. this.enterPredicate = (/**
  4435. * @return {?}
  4436. */
  4437. function () { return true; });
  4438. /**
  4439. * Whether to auto-scroll the view when the user moves their pointer close to the edges.
  4440. */
  4441. this.autoScrollDisabled = false;
  4442. /**
  4443. * Emits when the user drops an item inside the container.
  4444. */
  4445. this.dropped = new EventEmitter();
  4446. /**
  4447. * Emits when the user has moved a new drag item into this container.
  4448. */
  4449. this.entered = new EventEmitter();
  4450. /**
  4451. * Emits when the user removes an item from the container
  4452. * by dragging it into another container.
  4453. */
  4454. this.exited = new EventEmitter();
  4455. /**
  4456. * Emits as the user is swapping items while actively dragging.
  4457. */
  4458. this.sorted = new EventEmitter();
  4459. this._dropListRef = dragDrop.createDropList(element);
  4460. this._dropListRef.data = this;
  4461. this._dropListRef.enterPredicate = (/**
  4462. * @param {?} drag
  4463. * @param {?} drop
  4464. * @return {?}
  4465. */
  4466. function (drag, drop) {
  4467. return _this.enterPredicate(drag.data, drop.data);
  4468. });
  4469. this._syncInputs(this._dropListRef);
  4470. this._handleEvents(this._dropListRef);
  4471. CdkDropList._dropLists.push(this);
  4472. if (_group) {
  4473. _group._items.add(this);
  4474. }
  4475. }
  4476. Object.defineProperty(CdkDropList.prototype, "disabled", {
  4477. /** Whether starting a dragging sequence from this container is disabled. */
  4478. get: /**
  4479. * Whether starting a dragging sequence from this container is disabled.
  4480. * @return {?}
  4481. */
  4482. function () {
  4483. return this._disabled || (!!this._group && this._group.disabled);
  4484. },
  4485. set: /**
  4486. * @param {?} value
  4487. * @return {?}
  4488. */
  4489. function (value) {
  4490. this._disabled = coerceBooleanProperty(value);
  4491. },
  4492. enumerable: true,
  4493. configurable: true
  4494. });
  4495. Object.defineProperty(CdkDropList.prototype, "sortingDisabled", {
  4496. /** Whether sorting within this drop list is disabled. */
  4497. get: /**
  4498. * Whether sorting within this drop list is disabled.
  4499. * @return {?}
  4500. */
  4501. function () { return this._sortingDisabled; },
  4502. set: /**
  4503. * @param {?} value
  4504. * @return {?}
  4505. */
  4506. function (value) {
  4507. this._sortingDisabled = coerceBooleanProperty(value);
  4508. },
  4509. enumerable: true,
  4510. configurable: true
  4511. });
  4512. /**
  4513. * @return {?}
  4514. */
  4515. CdkDropList.prototype.ngAfterContentInit = /**
  4516. * @return {?}
  4517. */
  4518. function () {
  4519. var _this = this;
  4520. this._draggables.changes
  4521. .pipe(startWith(this._draggables), takeUntil(this._destroyed))
  4522. .subscribe((/**
  4523. * @param {?} items
  4524. * @return {?}
  4525. */
  4526. function (items) {
  4527. _this._dropListRef.withItems(items.map((/**
  4528. * @param {?} drag
  4529. * @return {?}
  4530. */
  4531. function (drag) { return drag._dragRef; })));
  4532. }));
  4533. };
  4534. /**
  4535. * @return {?}
  4536. */
  4537. CdkDropList.prototype.ngOnDestroy = /**
  4538. * @return {?}
  4539. */
  4540. function () {
  4541. /** @type {?} */
  4542. var index = CdkDropList._dropLists.indexOf(this);
  4543. if (index > -1) {
  4544. CdkDropList._dropLists.splice(index, 1);
  4545. }
  4546. if (this._group) {
  4547. this._group._items.delete(this);
  4548. }
  4549. this._dropListRef.dispose();
  4550. this._destroyed.next();
  4551. this._destroyed.complete();
  4552. };
  4553. /** Starts dragging an item. */
  4554. /**
  4555. * Starts dragging an item.
  4556. * @return {?}
  4557. */
  4558. CdkDropList.prototype.start = /**
  4559. * Starts dragging an item.
  4560. * @return {?}
  4561. */
  4562. function () {
  4563. this._dropListRef.start();
  4564. };
  4565. /**
  4566. * Drops an item into this container.
  4567. * @param item Item being dropped into the container.
  4568. * @param currentIndex Index at which the item should be inserted.
  4569. * @param previousContainer Container from which the item got dragged in.
  4570. * @param isPointerOverContainer Whether the user's pointer was over the
  4571. * container when the item was dropped.
  4572. */
  4573. /**
  4574. * Drops an item into this container.
  4575. * @param {?} item Item being dropped into the container.
  4576. * @param {?} currentIndex Index at which the item should be inserted.
  4577. * @param {?} previousContainer Container from which the item got dragged in.
  4578. * @param {?} isPointerOverContainer Whether the user's pointer was over the
  4579. * container when the item was dropped.
  4580. * @return {?}
  4581. */
  4582. CdkDropList.prototype.drop = /**
  4583. * Drops an item into this container.
  4584. * @param {?} item Item being dropped into the container.
  4585. * @param {?} currentIndex Index at which the item should be inserted.
  4586. * @param {?} previousContainer Container from which the item got dragged in.
  4587. * @param {?} isPointerOverContainer Whether the user's pointer was over the
  4588. * container when the item was dropped.
  4589. * @return {?}
  4590. */
  4591. function (item, currentIndex, previousContainer, isPointerOverContainer) {
  4592. this._dropListRef.drop(item._dragRef, currentIndex, ((/** @type {?} */ (previousContainer)))._dropListRef, isPointerOverContainer);
  4593. };
  4594. /**
  4595. * Emits an event to indicate that the user moved an item into the container.
  4596. * @param item Item that was moved into the container.
  4597. * @param pointerX Position of the item along the X axis.
  4598. * @param pointerY Position of the item along the Y axis.
  4599. */
  4600. /**
  4601. * Emits an event to indicate that the user moved an item into the container.
  4602. * @param {?} item Item that was moved into the container.
  4603. * @param {?} pointerX Position of the item along the X axis.
  4604. * @param {?} pointerY Position of the item along the Y axis.
  4605. * @return {?}
  4606. */
  4607. CdkDropList.prototype.enter = /**
  4608. * Emits an event to indicate that the user moved an item into the container.
  4609. * @param {?} item Item that was moved into the container.
  4610. * @param {?} pointerX Position of the item along the X axis.
  4611. * @param {?} pointerY Position of the item along the Y axis.
  4612. * @return {?}
  4613. */
  4614. function (item, pointerX, pointerY) {
  4615. this._dropListRef.enter(item._dragRef, pointerX, pointerY);
  4616. };
  4617. /**
  4618. * Removes an item from the container after it was dragged into another container by the user.
  4619. * @param item Item that was dragged out.
  4620. */
  4621. /**
  4622. * Removes an item from the container after it was dragged into another container by the user.
  4623. * @param {?} item Item that was dragged out.
  4624. * @return {?}
  4625. */
  4626. CdkDropList.prototype.exit = /**
  4627. * Removes an item from the container after it was dragged into another container by the user.
  4628. * @param {?} item Item that was dragged out.
  4629. * @return {?}
  4630. */
  4631. function (item) {
  4632. this._dropListRef.exit(item._dragRef);
  4633. };
  4634. /**
  4635. * Figures out the index of an item in the container.
  4636. * @param item Item whose index should be determined.
  4637. */
  4638. /**
  4639. * Figures out the index of an item in the container.
  4640. * @param {?} item Item whose index should be determined.
  4641. * @return {?}
  4642. */
  4643. CdkDropList.prototype.getItemIndex = /**
  4644. * Figures out the index of an item in the container.
  4645. * @param {?} item Item whose index should be determined.
  4646. * @return {?}
  4647. */
  4648. function (item) {
  4649. return this._dropListRef.getItemIndex(item._dragRef);
  4650. };
  4651. /**
  4652. * Sorts an item inside the container based on its position.
  4653. * @param item Item to be sorted.
  4654. * @param pointerX Position of the item along the X axis.
  4655. * @param pointerY Position of the item along the Y axis.
  4656. * @param pointerDelta Direction in which the pointer is moving along each axis.
  4657. */
  4658. /**
  4659. * Sorts an item inside the container based on its position.
  4660. * @param {?} item Item to be sorted.
  4661. * @param {?} pointerX Position of the item along the X axis.
  4662. * @param {?} pointerY Position of the item along the Y axis.
  4663. * @param {?} pointerDelta Direction in which the pointer is moving along each axis.
  4664. * @return {?}
  4665. */
  4666. CdkDropList.prototype._sortItem = /**
  4667. * Sorts an item inside the container based on its position.
  4668. * @param {?} item Item to be sorted.
  4669. * @param {?} pointerX Position of the item along the X axis.
  4670. * @param {?} pointerY Position of the item along the Y axis.
  4671. * @param {?} pointerDelta Direction in which the pointer is moving along each axis.
  4672. * @return {?}
  4673. */
  4674. function (item, pointerX, pointerY, pointerDelta) {
  4675. return this._dropListRef._sortItem(item._dragRef, pointerX, pointerY, pointerDelta);
  4676. };
  4677. /**
  4678. * Figures out whether an item should be moved into a sibling
  4679. * drop container, based on its current position.
  4680. * @param item Drag item that is being moved.
  4681. * @param x Position of the item along the X axis.
  4682. * @param y Position of the item along the Y axis.
  4683. */
  4684. /**
  4685. * Figures out whether an item should be moved into a sibling
  4686. * drop container, based on its current position.
  4687. * @param {?} item Drag item that is being moved.
  4688. * @param {?} x Position of the item along the X axis.
  4689. * @param {?} y Position of the item along the Y axis.
  4690. * @return {?}
  4691. */
  4692. CdkDropList.prototype._getSiblingContainerFromPosition = /**
  4693. * Figures out whether an item should be moved into a sibling
  4694. * drop container, based on its current position.
  4695. * @param {?} item Drag item that is being moved.
  4696. * @param {?} x Position of the item along the X axis.
  4697. * @param {?} y Position of the item along the Y axis.
  4698. * @return {?}
  4699. */
  4700. function (item, x, y) {
  4701. /** @type {?} */
  4702. var result = this._dropListRef._getSiblingContainerFromPosition(item._dragRef, x, y);
  4703. return result ? result.data : null;
  4704. };
  4705. /**
  4706. * Checks whether the user's pointer is positioned over the container.
  4707. * @param x Pointer position along the X axis.
  4708. * @param y Pointer position along the Y axis.
  4709. */
  4710. /**
  4711. * Checks whether the user's pointer is positioned over the container.
  4712. * @param {?} x Pointer position along the X axis.
  4713. * @param {?} y Pointer position along the Y axis.
  4714. * @return {?}
  4715. */
  4716. CdkDropList.prototype._isOverContainer = /**
  4717. * Checks whether the user's pointer is positioned over the container.
  4718. * @param {?} x Pointer position along the X axis.
  4719. * @param {?} y Pointer position along the Y axis.
  4720. * @return {?}
  4721. */
  4722. function (x, y) {
  4723. return this._dropListRef._isOverContainer(x, y);
  4724. };
  4725. /** Syncs the inputs of the CdkDropList with the options of the underlying DropListRef. */
  4726. /**
  4727. * Syncs the inputs of the CdkDropList with the options of the underlying DropListRef.
  4728. * @private
  4729. * @param {?} ref
  4730. * @return {?}
  4731. */
  4732. CdkDropList.prototype._syncInputs = /**
  4733. * Syncs the inputs of the CdkDropList with the options of the underlying DropListRef.
  4734. * @private
  4735. * @param {?} ref
  4736. * @return {?}
  4737. */
  4738. function (ref) {
  4739. var _this = this;
  4740. if (this._dir) {
  4741. this._dir.change
  4742. .pipe(startWith(this._dir.value), takeUntil(this._destroyed))
  4743. .subscribe((/**
  4744. * @param {?} value
  4745. * @return {?}
  4746. */
  4747. function (value) { return ref.withDirection(value); }));
  4748. }
  4749. ref.beforeStarted.subscribe((/**
  4750. * @return {?}
  4751. */
  4752. function () {
  4753. /** @type {?} */
  4754. var siblings = coerceArray(_this.connectedTo).map((/**
  4755. * @param {?} drop
  4756. * @return {?}
  4757. */
  4758. function (drop) {
  4759. return typeof drop === 'string' ?
  4760. (/** @type {?} */ (CdkDropList._dropLists.find((/**
  4761. * @param {?} list
  4762. * @return {?}
  4763. */
  4764. function (list) { return list.id === drop; })))) : drop;
  4765. }));
  4766. if (_this._group) {
  4767. _this._group._items.forEach((/**
  4768. * @param {?} drop
  4769. * @return {?}
  4770. */
  4771. function (drop) {
  4772. if (siblings.indexOf(drop) === -1) {
  4773. siblings.push(drop);
  4774. }
  4775. }));
  4776. }
  4777. ref.disabled = _this.disabled;
  4778. ref.lockAxis = _this.lockAxis;
  4779. ref.sortingDisabled = _this.sortingDisabled;
  4780. ref.autoScrollDisabled = _this.autoScrollDisabled;
  4781. ref
  4782. .connectedTo(siblings.filter((/**
  4783. * @param {?} drop
  4784. * @return {?}
  4785. */
  4786. function (drop) { return drop && drop !== _this; })).map((/**
  4787. * @param {?} list
  4788. * @return {?}
  4789. */
  4790. function (list) { return list._dropListRef; })))
  4791. .withOrientation(_this.orientation);
  4792. }));
  4793. };
  4794. /** Handles events from the underlying DropListRef. */
  4795. /**
  4796. * Handles events from the underlying DropListRef.
  4797. * @private
  4798. * @param {?} ref
  4799. * @return {?}
  4800. */
  4801. CdkDropList.prototype._handleEvents = /**
  4802. * Handles events from the underlying DropListRef.
  4803. * @private
  4804. * @param {?} ref
  4805. * @return {?}
  4806. */
  4807. function (ref) {
  4808. var _this = this;
  4809. ref.beforeStarted.subscribe((/**
  4810. * @return {?}
  4811. */
  4812. function () {
  4813. _this._changeDetectorRef.markForCheck();
  4814. }));
  4815. ref.entered.subscribe((/**
  4816. * @param {?} event
  4817. * @return {?}
  4818. */
  4819. function (event) {
  4820. _this.entered.emit({
  4821. container: _this,
  4822. item: event.item.data,
  4823. currentIndex: event.currentIndex
  4824. });
  4825. }));
  4826. ref.exited.subscribe((/**
  4827. * @param {?} event
  4828. * @return {?}
  4829. */
  4830. function (event) {
  4831. _this.exited.emit({
  4832. container: _this,
  4833. item: event.item.data
  4834. });
  4835. _this._changeDetectorRef.markForCheck();
  4836. }));
  4837. ref.sorted.subscribe((/**
  4838. * @param {?} event
  4839. * @return {?}
  4840. */
  4841. function (event) {
  4842. _this.sorted.emit({
  4843. previousIndex: event.previousIndex,
  4844. currentIndex: event.currentIndex,
  4845. container: _this,
  4846. item: event.item.data
  4847. });
  4848. }));
  4849. ref.dropped.subscribe((/**
  4850. * @param {?} event
  4851. * @return {?}
  4852. */
  4853. function (event) {
  4854. _this.dropped.emit({
  4855. previousIndex: event.previousIndex,
  4856. currentIndex: event.currentIndex,
  4857. previousContainer: event.previousContainer.data,
  4858. container: event.container.data,
  4859. item: event.item.data,
  4860. isPointerOverContainer: event.isPointerOverContainer,
  4861. distance: event.distance
  4862. });
  4863. // Mark for check since all of these events run outside of change
  4864. // detection and we're not guaranteed for something else to have triggered it.
  4865. _this._changeDetectorRef.markForCheck();
  4866. }));
  4867. };
  4868. /**
  4869. * Keeps track of the drop lists that are currently on the page.
  4870. */
  4871. CdkDropList._dropLists = [];
  4872. CdkDropList.decorators = [
  4873. { type: Directive, args: [{
  4874. selector: '[cdkDropList], cdk-drop-list',
  4875. exportAs: 'cdkDropList',
  4876. providers: [
  4877. // Prevent child drop lists from picking up the same group as their parent.
  4878. { provide: CdkDropListGroup, useValue: ɵ0 },
  4879. { provide: CDK_DROP_LIST_CONTAINER, useExisting: CdkDropList },
  4880. ],
  4881. host: {
  4882. 'class': 'cdk-drop-list',
  4883. '[id]': 'id',
  4884. '[class.cdk-drop-list-disabled]': 'disabled',
  4885. '[class.cdk-drop-list-dragging]': '_dropListRef.isDragging()',
  4886. '[class.cdk-drop-list-receiving]': '_dropListRef.isReceiving()',
  4887. }
  4888. },] },
  4889. ];
  4890. /** @nocollapse */
  4891. CdkDropList.ctorParameters = function () { return [
  4892. { type: ElementRef },
  4893. { type: DragDrop },
  4894. { type: ChangeDetectorRef },
  4895. { type: Directionality, decorators: [{ type: Optional }] },
  4896. { type: CdkDropListGroup, decorators: [{ type: Optional }, { type: SkipSelf }] }
  4897. ]; };
  4898. CdkDropList.propDecorators = {
  4899. _draggables: [{ type: ContentChildren, args: [forwardRef((/**
  4900. * @return {?}
  4901. */
  4902. function () { return CdkDrag; })), {
  4903. // Explicitly set to false since some of the logic below makes assumptions about it.
  4904. // The `.withItems` call below should be updated if we ever need to switch this to `true`.
  4905. descendants: false
  4906. },] }],
  4907. connectedTo: [{ type: Input, args: ['cdkDropListConnectedTo',] }],
  4908. data: [{ type: Input, args: ['cdkDropListData',] }],
  4909. orientation: [{ type: Input, args: ['cdkDropListOrientation',] }],
  4910. id: [{ type: Input }],
  4911. lockAxis: [{ type: Input, args: ['cdkDropListLockAxis',] }],
  4912. disabled: [{ type: Input, args: ['cdkDropListDisabled',] }],
  4913. sortingDisabled: [{ type: Input, args: ['cdkDropListSortingDisabled',] }],
  4914. enterPredicate: [{ type: Input, args: ['cdkDropListEnterPredicate',] }],
  4915. autoScrollDisabled: [{ type: Input, args: ['cdkDropListAutoScrollDisabled',] }],
  4916. dropped: [{ type: Output, args: ['cdkDropListDropped',] }],
  4917. entered: [{ type: Output, args: ['cdkDropListEntered',] }],
  4918. exited: [{ type: Output, args: ['cdkDropListExited',] }],
  4919. sorted: [{ type: Output, args: ['cdkDropListSorted',] }]
  4920. };
  4921. return CdkDropList;
  4922. }());
  4923. /**
  4924. * @fileoverview added by tsickle
  4925. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  4926. */
  4927. var DragDropModule = /** @class */ (function () {
  4928. function DragDropModule() {
  4929. }
  4930. DragDropModule.decorators = [
  4931. { type: NgModule, args: [{
  4932. declarations: [
  4933. CdkDropList,
  4934. CdkDropListGroup,
  4935. CdkDrag,
  4936. CdkDragHandle,
  4937. CdkDragPreview,
  4938. CdkDragPlaceholder,
  4939. ],
  4940. exports: [
  4941. CdkDropList,
  4942. CdkDropListGroup,
  4943. CdkDrag,
  4944. CdkDragHandle,
  4945. CdkDragPreview,
  4946. CdkDragPlaceholder,
  4947. ],
  4948. providers: [
  4949. DragDrop,
  4950. ]
  4951. },] },
  4952. ];
  4953. return DragDropModule;
  4954. }());
  4955. /**
  4956. * @fileoverview added by tsickle
  4957. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  4958. */
  4959. /**
  4960. * @fileoverview added by tsickle
  4961. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  4962. */
  4963. export { DragDrop, DragRef, DropListRef, CdkDropList, CDK_DROP_LIST, CDK_DROP_LIST_CONTAINER, moveItemInArray, transferArrayItem, copyArrayItem, DragDropModule, DragDropRegistry, CdkDropListGroup, CDK_DRAG_CONFIG_FACTORY, CDK_DRAG_CONFIG, CdkDrag, CdkDragHandle, CdkDragPreview, CdkDragPlaceholder, CDK_DRAG_PARENT as ɵb };
  4964. //# sourceMappingURL=drag-drop.es5.js.map