index.cjs 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var index_cjs = require('@fullcalendar/core/index.cjs');
  4. var internal_cjs = require('@fullcalendar/core/internal.cjs');
  5. internal_cjs.config.touchMouseIgnoreWait = 500;
  6. let ignoreMouseDepth = 0;
  7. let listenerCnt = 0;
  8. let isWindowTouchMoveCancelled = false;
  9. /*
  10. Uses a "pointer" abstraction, which monitors UI events for both mouse and touch.
  11. Tracks when the pointer "drags" on a certain element, meaning down+move+up.
  12. Also, tracks if there was touch-scrolling.
  13. Also, can prevent touch-scrolling from happening.
  14. Also, can fire pointermove events when scrolling happens underneath, even when no real pointer movement.
  15. emits:
  16. - pointerdown
  17. - pointermove
  18. - pointerup
  19. */
  20. class PointerDragging {
  21. constructor(containerEl) {
  22. this.subjectEl = null;
  23. // options that can be directly assigned by caller
  24. this.selector = ''; // will cause subjectEl in all emitted events to be this element
  25. this.handleSelector = '';
  26. this.shouldIgnoreMove = false;
  27. this.shouldWatchScroll = true; // for simulating pointermove on scroll
  28. // internal states
  29. this.isDragging = false;
  30. this.isTouchDragging = false;
  31. this.wasTouchScroll = false;
  32. // Mouse
  33. // ----------------------------------------------------------------------------------------------------
  34. this.handleMouseDown = (ev) => {
  35. if (!this.shouldIgnoreMouse() &&
  36. isPrimaryMouseButton(ev) &&
  37. this.tryStart(ev)) {
  38. let pev = this.createEventFromMouse(ev, true);
  39. this.emitter.trigger('pointerdown', pev);
  40. this.initScrollWatch(pev);
  41. if (!this.shouldIgnoreMove) {
  42. document.addEventListener('mousemove', this.handleMouseMove);
  43. }
  44. document.addEventListener('mouseup', this.handleMouseUp);
  45. }
  46. };
  47. this.handleMouseMove = (ev) => {
  48. let pev = this.createEventFromMouse(ev);
  49. this.recordCoords(pev);
  50. this.emitter.trigger('pointermove', pev);
  51. };
  52. this.handleMouseUp = (ev) => {
  53. document.removeEventListener('mousemove', this.handleMouseMove);
  54. document.removeEventListener('mouseup', this.handleMouseUp);
  55. this.emitter.trigger('pointerup', this.createEventFromMouse(ev));
  56. this.cleanup(); // call last so that pointerup has access to props
  57. };
  58. // Touch
  59. // ----------------------------------------------------------------------------------------------------
  60. this.handleTouchStart = (ev) => {
  61. if (this.tryStart(ev)) {
  62. this.isTouchDragging = true;
  63. let pev = this.createEventFromTouch(ev, true);
  64. this.emitter.trigger('pointerdown', pev);
  65. this.initScrollWatch(pev);
  66. // unlike mouse, need to attach to target, not document
  67. // https://stackoverflow.com/a/45760014
  68. let targetEl = ev.target;
  69. if (!this.shouldIgnoreMove) {
  70. targetEl.addEventListener('touchmove', this.handleTouchMove);
  71. }
  72. targetEl.addEventListener('touchend', this.handleTouchEnd);
  73. targetEl.addEventListener('touchcancel', this.handleTouchEnd); // treat it as a touch end
  74. // attach a handler to get called when ANY scroll action happens on the page.
  75. // this was impossible to do with normal on/off because 'scroll' doesn't bubble.
  76. // http://stackoverflow.com/a/32954565/96342
  77. window.addEventListener('scroll', this.handleTouchScroll, true);
  78. }
  79. };
  80. this.handleTouchMove = (ev) => {
  81. let pev = this.createEventFromTouch(ev);
  82. this.recordCoords(pev);
  83. this.emitter.trigger('pointermove', pev);
  84. };
  85. this.handleTouchEnd = (ev) => {
  86. if (this.isDragging) { // done to guard against touchend followed by touchcancel
  87. let targetEl = ev.target;
  88. targetEl.removeEventListener('touchmove', this.handleTouchMove);
  89. targetEl.removeEventListener('touchend', this.handleTouchEnd);
  90. targetEl.removeEventListener('touchcancel', this.handleTouchEnd);
  91. window.removeEventListener('scroll', this.handleTouchScroll, true); // useCaptured=true
  92. this.emitter.trigger('pointerup', this.createEventFromTouch(ev));
  93. this.cleanup(); // call last so that pointerup has access to props
  94. this.isTouchDragging = false;
  95. startIgnoringMouse();
  96. }
  97. };
  98. this.handleTouchScroll = () => {
  99. this.wasTouchScroll = true;
  100. };
  101. this.handleScroll = (ev) => {
  102. if (!this.shouldIgnoreMove) {
  103. let pageX = (window.scrollX - this.prevScrollX) + this.prevPageX;
  104. let pageY = (window.scrollY - this.prevScrollY) + this.prevPageY;
  105. this.emitter.trigger('pointermove', {
  106. origEvent: ev,
  107. isTouch: this.isTouchDragging,
  108. subjectEl: this.subjectEl,
  109. pageX,
  110. pageY,
  111. deltaX: pageX - this.origPageX,
  112. deltaY: pageY - this.origPageY,
  113. });
  114. }
  115. };
  116. this.containerEl = containerEl;
  117. this.emitter = new internal_cjs.Emitter();
  118. containerEl.addEventListener('mousedown', this.handleMouseDown);
  119. containerEl.addEventListener('touchstart', this.handleTouchStart, { passive: true });
  120. listenerCreated();
  121. }
  122. destroy() {
  123. this.containerEl.removeEventListener('mousedown', this.handleMouseDown);
  124. this.containerEl.removeEventListener('touchstart', this.handleTouchStart, { passive: true });
  125. listenerDestroyed();
  126. }
  127. tryStart(ev) {
  128. let subjectEl = this.querySubjectEl(ev);
  129. let downEl = ev.target;
  130. if (subjectEl &&
  131. (!this.handleSelector || internal_cjs.elementClosest(downEl, this.handleSelector))) {
  132. this.subjectEl = subjectEl;
  133. this.isDragging = true; // do this first so cancelTouchScroll will work
  134. this.wasTouchScroll = false;
  135. return true;
  136. }
  137. return false;
  138. }
  139. cleanup() {
  140. isWindowTouchMoveCancelled = false;
  141. this.isDragging = false;
  142. this.subjectEl = null;
  143. // keep wasTouchScroll around for later access
  144. this.destroyScrollWatch();
  145. }
  146. querySubjectEl(ev) {
  147. if (this.selector) {
  148. return internal_cjs.elementClosest(ev.target, this.selector);
  149. }
  150. return this.containerEl;
  151. }
  152. shouldIgnoreMouse() {
  153. return ignoreMouseDepth || this.isTouchDragging;
  154. }
  155. // can be called by user of this class, to cancel touch-based scrolling for the current drag
  156. cancelTouchScroll() {
  157. if (this.isDragging) {
  158. isWindowTouchMoveCancelled = true;
  159. }
  160. }
  161. // Scrolling that simulates pointermoves
  162. // ----------------------------------------------------------------------------------------------------
  163. initScrollWatch(ev) {
  164. if (this.shouldWatchScroll) {
  165. this.recordCoords(ev);
  166. window.addEventListener('scroll', this.handleScroll, true); // useCapture=true
  167. }
  168. }
  169. recordCoords(ev) {
  170. if (this.shouldWatchScroll) {
  171. this.prevPageX = ev.pageX;
  172. this.prevPageY = ev.pageY;
  173. this.prevScrollX = window.scrollX;
  174. this.prevScrollY = window.scrollY;
  175. }
  176. }
  177. destroyScrollWatch() {
  178. if (this.shouldWatchScroll) {
  179. window.removeEventListener('scroll', this.handleScroll, true); // useCaptured=true
  180. }
  181. }
  182. // Event Normalization
  183. // ----------------------------------------------------------------------------------------------------
  184. createEventFromMouse(ev, isFirst) {
  185. let deltaX = 0;
  186. let deltaY = 0;
  187. // TODO: repeat code
  188. if (isFirst) {
  189. this.origPageX = ev.pageX;
  190. this.origPageY = ev.pageY;
  191. }
  192. else {
  193. deltaX = ev.pageX - this.origPageX;
  194. deltaY = ev.pageY - this.origPageY;
  195. }
  196. return {
  197. origEvent: ev,
  198. isTouch: false,
  199. subjectEl: this.subjectEl,
  200. pageX: ev.pageX,
  201. pageY: ev.pageY,
  202. deltaX,
  203. deltaY,
  204. };
  205. }
  206. createEventFromTouch(ev, isFirst) {
  207. let touches = ev.touches;
  208. let pageX;
  209. let pageY;
  210. let deltaX = 0;
  211. let deltaY = 0;
  212. // if touch coords available, prefer,
  213. // because FF would give bad ev.pageX ev.pageY
  214. if (touches && touches.length) {
  215. pageX = touches[0].pageX;
  216. pageY = touches[0].pageY;
  217. }
  218. else {
  219. pageX = ev.pageX;
  220. pageY = ev.pageY;
  221. }
  222. // TODO: repeat code
  223. if (isFirst) {
  224. this.origPageX = pageX;
  225. this.origPageY = pageY;
  226. }
  227. else {
  228. deltaX = pageX - this.origPageX;
  229. deltaY = pageY - this.origPageY;
  230. }
  231. return {
  232. origEvent: ev,
  233. isTouch: true,
  234. subjectEl: this.subjectEl,
  235. pageX,
  236. pageY,
  237. deltaX,
  238. deltaY,
  239. };
  240. }
  241. }
  242. // Returns a boolean whether this was a left mouse click and no ctrl key (which means right click on Mac)
  243. function isPrimaryMouseButton(ev) {
  244. return ev.button === 0 && !ev.ctrlKey;
  245. }
  246. // Ignoring fake mouse events generated by touch
  247. // ----------------------------------------------------------------------------------------------------
  248. function startIgnoringMouse() {
  249. ignoreMouseDepth += 1;
  250. setTimeout(() => {
  251. ignoreMouseDepth -= 1;
  252. }, internal_cjs.config.touchMouseIgnoreWait);
  253. }
  254. // We want to attach touchmove as early as possible for Safari
  255. // ----------------------------------------------------------------------------------------------------
  256. function listenerCreated() {
  257. listenerCnt += 1;
  258. if (listenerCnt === 1) {
  259. window.addEventListener('touchmove', onWindowTouchMove, { passive: false });
  260. }
  261. }
  262. function listenerDestroyed() {
  263. listenerCnt -= 1;
  264. if (!listenerCnt) {
  265. window.removeEventListener('touchmove', onWindowTouchMove, { passive: false });
  266. }
  267. }
  268. function onWindowTouchMove(ev) {
  269. if (isWindowTouchMoveCancelled) {
  270. ev.preventDefault();
  271. }
  272. }
  273. /*
  274. An effect in which an element follows the movement of a pointer across the screen.
  275. The moving element is a clone of some other element.
  276. Must call start + handleMove + stop.
  277. */
  278. class ElementMirror {
  279. constructor() {
  280. this.isVisible = false; // must be explicitly enabled
  281. this.sourceEl = null;
  282. this.mirrorEl = null;
  283. this.sourceElRect = null; // screen coords relative to viewport
  284. // options that can be set directly by caller
  285. this.parentNode = document.body; // HIGHLY SUGGESTED to set this to sidestep ShadowDOM issues
  286. this.zIndex = 9999;
  287. this.revertDuration = 0;
  288. }
  289. start(sourceEl, pageX, pageY) {
  290. this.sourceEl = sourceEl;
  291. this.sourceElRect = this.sourceEl.getBoundingClientRect();
  292. this.origScreenX = pageX - window.scrollX;
  293. this.origScreenY = pageY - window.scrollY;
  294. this.deltaX = 0;
  295. this.deltaY = 0;
  296. this.updateElPosition();
  297. }
  298. handleMove(pageX, pageY) {
  299. this.deltaX = (pageX - window.scrollX) - this.origScreenX;
  300. this.deltaY = (pageY - window.scrollY) - this.origScreenY;
  301. this.updateElPosition();
  302. }
  303. // can be called before start
  304. setIsVisible(bool) {
  305. if (bool) {
  306. if (!this.isVisible) {
  307. if (this.mirrorEl) {
  308. this.mirrorEl.style.display = '';
  309. }
  310. this.isVisible = bool; // needs to happen before updateElPosition
  311. this.updateElPosition(); // because was not updating the position while invisible
  312. }
  313. }
  314. else if (this.isVisible) {
  315. if (this.mirrorEl) {
  316. this.mirrorEl.style.display = 'none';
  317. }
  318. this.isVisible = bool;
  319. }
  320. }
  321. // always async
  322. stop(needsRevertAnimation, callback) {
  323. let done = () => {
  324. this.cleanup();
  325. callback();
  326. };
  327. if (needsRevertAnimation &&
  328. this.mirrorEl &&
  329. this.isVisible &&
  330. this.revertDuration && // if 0, transition won't work
  331. (this.deltaX || this.deltaY) // if same coords, transition won't work
  332. ) {
  333. this.doRevertAnimation(done, this.revertDuration);
  334. }
  335. else {
  336. setTimeout(done, 0);
  337. }
  338. }
  339. doRevertAnimation(callback, revertDuration) {
  340. let mirrorEl = this.mirrorEl;
  341. let finalSourceElRect = this.sourceEl.getBoundingClientRect(); // because autoscrolling might have happened
  342. mirrorEl.style.transition =
  343. 'top ' + revertDuration + 'ms,' +
  344. 'left ' + revertDuration + 'ms';
  345. internal_cjs.applyStyle(mirrorEl, {
  346. left: finalSourceElRect.left,
  347. top: finalSourceElRect.top,
  348. });
  349. internal_cjs.whenTransitionDone(mirrorEl, () => {
  350. mirrorEl.style.transition = '';
  351. callback();
  352. });
  353. }
  354. cleanup() {
  355. if (this.mirrorEl) {
  356. internal_cjs.removeElement(this.mirrorEl);
  357. this.mirrorEl = null;
  358. }
  359. this.sourceEl = null;
  360. }
  361. updateElPosition() {
  362. if (this.sourceEl && this.isVisible) {
  363. internal_cjs.applyStyle(this.getMirrorEl(), {
  364. left: this.sourceElRect.left + this.deltaX,
  365. top: this.sourceElRect.top + this.deltaY,
  366. });
  367. }
  368. }
  369. getMirrorEl() {
  370. let sourceElRect = this.sourceElRect;
  371. let mirrorEl = this.mirrorEl;
  372. if (!mirrorEl) {
  373. mirrorEl = this.mirrorEl = this.sourceEl.cloneNode(true); // cloneChildren=true
  374. // we don't want long taps or any mouse interaction causing selection/menus.
  375. // would use preventSelection(), but that prevents selectstart, causing problems.
  376. mirrorEl.style.userSelect = 'none';
  377. mirrorEl.style.webkitUserSelect = 'none';
  378. mirrorEl.style.pointerEvents = 'none';
  379. mirrorEl.classList.add('fc-event-dragging');
  380. internal_cjs.applyStyle(mirrorEl, {
  381. position: 'fixed',
  382. zIndex: this.zIndex,
  383. visibility: '',
  384. boxSizing: 'border-box',
  385. width: sourceElRect.right - sourceElRect.left,
  386. height: sourceElRect.bottom - sourceElRect.top,
  387. right: 'auto',
  388. bottom: 'auto',
  389. margin: 0,
  390. });
  391. this.parentNode.appendChild(mirrorEl);
  392. }
  393. return mirrorEl;
  394. }
  395. }
  396. /*
  397. Is a cache for a given element's scroll information (all the info that ScrollController stores)
  398. in addition the "client rectangle" of the element.. the area within the scrollbars.
  399. The cache can be in one of two modes:
  400. - doesListening:false - ignores when the container is scrolled by someone else
  401. - doesListening:true - watch for scrolling and update the cache
  402. */
  403. class ScrollGeomCache extends internal_cjs.ScrollController {
  404. constructor(scrollController, doesListening) {
  405. super();
  406. this.handleScroll = () => {
  407. this.scrollTop = this.scrollController.getScrollTop();
  408. this.scrollLeft = this.scrollController.getScrollLeft();
  409. this.handleScrollChange();
  410. };
  411. this.scrollController = scrollController;
  412. this.doesListening = doesListening;
  413. this.scrollTop = this.origScrollTop = scrollController.getScrollTop();
  414. this.scrollLeft = this.origScrollLeft = scrollController.getScrollLeft();
  415. this.scrollWidth = scrollController.getScrollWidth();
  416. this.scrollHeight = scrollController.getScrollHeight();
  417. this.clientWidth = scrollController.getClientWidth();
  418. this.clientHeight = scrollController.getClientHeight();
  419. this.clientRect = this.computeClientRect(); // do last in case it needs cached values
  420. if (this.doesListening) {
  421. this.getEventTarget().addEventListener('scroll', this.handleScroll);
  422. }
  423. }
  424. destroy() {
  425. if (this.doesListening) {
  426. this.getEventTarget().removeEventListener('scroll', this.handleScroll);
  427. }
  428. }
  429. getScrollTop() {
  430. return this.scrollTop;
  431. }
  432. getScrollLeft() {
  433. return this.scrollLeft;
  434. }
  435. setScrollTop(top) {
  436. this.scrollController.setScrollTop(top);
  437. if (!this.doesListening) {
  438. // we are not relying on the element to normalize out-of-bounds scroll values
  439. // so we need to sanitize ourselves
  440. this.scrollTop = Math.max(Math.min(top, this.getMaxScrollTop()), 0);
  441. this.handleScrollChange();
  442. }
  443. }
  444. setScrollLeft(top) {
  445. this.scrollController.setScrollLeft(top);
  446. if (!this.doesListening) {
  447. // we are not relying on the element to normalize out-of-bounds scroll values
  448. // so we need to sanitize ourselves
  449. this.scrollLeft = Math.max(Math.min(top, this.getMaxScrollLeft()), 0);
  450. this.handleScrollChange();
  451. }
  452. }
  453. getClientWidth() {
  454. return this.clientWidth;
  455. }
  456. getClientHeight() {
  457. return this.clientHeight;
  458. }
  459. getScrollWidth() {
  460. return this.scrollWidth;
  461. }
  462. getScrollHeight() {
  463. return this.scrollHeight;
  464. }
  465. handleScrollChange() {
  466. }
  467. }
  468. class ElementScrollGeomCache extends ScrollGeomCache {
  469. constructor(el, doesListening) {
  470. super(new internal_cjs.ElementScrollController(el), doesListening);
  471. }
  472. getEventTarget() {
  473. return this.scrollController.el;
  474. }
  475. computeClientRect() {
  476. return internal_cjs.computeInnerRect(this.scrollController.el);
  477. }
  478. }
  479. class WindowScrollGeomCache extends ScrollGeomCache {
  480. constructor(doesListening) {
  481. super(new internal_cjs.WindowScrollController(), doesListening);
  482. }
  483. getEventTarget() {
  484. return window;
  485. }
  486. computeClientRect() {
  487. return {
  488. left: this.scrollLeft,
  489. right: this.scrollLeft + this.clientWidth,
  490. top: this.scrollTop,
  491. bottom: this.scrollTop + this.clientHeight,
  492. };
  493. }
  494. // the window is the only scroll object that changes it's rectangle relative
  495. // to the document's topleft as it scrolls
  496. handleScrollChange() {
  497. this.clientRect = this.computeClientRect();
  498. }
  499. }
  500. // If available we are using native "performance" API instead of "Date"
  501. // Read more about it on MDN:
  502. // https://developer.mozilla.org/en-US/docs/Web/API/Performance
  503. const getTime = typeof performance === 'function' ? performance.now : Date.now;
  504. /*
  505. For a pointer interaction, automatically scrolls certain scroll containers when the pointer
  506. approaches the edge.
  507. The caller must call start + handleMove + stop.
  508. */
  509. class AutoScroller {
  510. constructor() {
  511. // options that can be set by caller
  512. this.isEnabled = true;
  513. this.scrollQuery = [window, '.fc-scroller'];
  514. this.edgeThreshold = 50; // pixels
  515. this.maxVelocity = 300; // pixels per second
  516. // internal state
  517. this.pointerScreenX = null;
  518. this.pointerScreenY = null;
  519. this.isAnimating = false;
  520. this.scrollCaches = null;
  521. // protect against the initial pointerdown being too close to an edge and starting the scroll
  522. this.everMovedUp = false;
  523. this.everMovedDown = false;
  524. this.everMovedLeft = false;
  525. this.everMovedRight = false;
  526. this.animate = () => {
  527. if (this.isAnimating) { // wasn't cancelled between animation calls
  528. let edge = this.computeBestEdge(this.pointerScreenX + window.scrollX, this.pointerScreenY + window.scrollY);
  529. if (edge) {
  530. let now = getTime();
  531. this.handleSide(edge, (now - this.msSinceRequest) / 1000);
  532. this.requestAnimation(now);
  533. }
  534. else {
  535. this.isAnimating = false; // will stop animation
  536. }
  537. }
  538. };
  539. }
  540. start(pageX, pageY, scrollStartEl) {
  541. if (this.isEnabled) {
  542. this.scrollCaches = this.buildCaches(scrollStartEl);
  543. this.pointerScreenX = null;
  544. this.pointerScreenY = null;
  545. this.everMovedUp = false;
  546. this.everMovedDown = false;
  547. this.everMovedLeft = false;
  548. this.everMovedRight = false;
  549. this.handleMove(pageX, pageY);
  550. }
  551. }
  552. handleMove(pageX, pageY) {
  553. if (this.isEnabled) {
  554. let pointerScreenX = pageX - window.scrollX;
  555. let pointerScreenY = pageY - window.scrollY;
  556. let yDelta = this.pointerScreenY === null ? 0 : pointerScreenY - this.pointerScreenY;
  557. let xDelta = this.pointerScreenX === null ? 0 : pointerScreenX - this.pointerScreenX;
  558. if (yDelta < 0) {
  559. this.everMovedUp = true;
  560. }
  561. else if (yDelta > 0) {
  562. this.everMovedDown = true;
  563. }
  564. if (xDelta < 0) {
  565. this.everMovedLeft = true;
  566. }
  567. else if (xDelta > 0) {
  568. this.everMovedRight = true;
  569. }
  570. this.pointerScreenX = pointerScreenX;
  571. this.pointerScreenY = pointerScreenY;
  572. if (!this.isAnimating) {
  573. this.isAnimating = true;
  574. this.requestAnimation(getTime());
  575. }
  576. }
  577. }
  578. stop() {
  579. if (this.isEnabled) {
  580. this.isAnimating = false; // will stop animation
  581. for (let scrollCache of this.scrollCaches) {
  582. scrollCache.destroy();
  583. }
  584. this.scrollCaches = null;
  585. }
  586. }
  587. requestAnimation(now) {
  588. this.msSinceRequest = now;
  589. requestAnimationFrame(this.animate);
  590. }
  591. handleSide(edge, seconds) {
  592. let { scrollCache } = edge;
  593. let { edgeThreshold } = this;
  594. let invDistance = edgeThreshold - edge.distance;
  595. let velocity = // the closer to the edge, the faster we scroll
  596. ((invDistance * invDistance) / (edgeThreshold * edgeThreshold)) * // quadratic
  597. this.maxVelocity * seconds;
  598. let sign = 1;
  599. switch (edge.name) {
  600. case 'left':
  601. sign = -1;
  602. // falls through
  603. case 'right':
  604. scrollCache.setScrollLeft(scrollCache.getScrollLeft() + velocity * sign);
  605. break;
  606. case 'top':
  607. sign = -1;
  608. // falls through
  609. case 'bottom':
  610. scrollCache.setScrollTop(scrollCache.getScrollTop() + velocity * sign);
  611. break;
  612. }
  613. }
  614. // left/top are relative to document topleft
  615. computeBestEdge(left, top) {
  616. let { edgeThreshold } = this;
  617. let bestSide = null;
  618. let scrollCaches = this.scrollCaches || [];
  619. for (let scrollCache of scrollCaches) {
  620. let rect = scrollCache.clientRect;
  621. let leftDist = left - rect.left;
  622. let rightDist = rect.right - left;
  623. let topDist = top - rect.top;
  624. let bottomDist = rect.bottom - top;
  625. // completely within the rect?
  626. if (leftDist >= 0 && rightDist >= 0 && topDist >= 0 && bottomDist >= 0) {
  627. if (topDist <= edgeThreshold && this.everMovedUp && scrollCache.canScrollUp() &&
  628. (!bestSide || bestSide.distance > topDist)) {
  629. bestSide = { scrollCache, name: 'top', distance: topDist };
  630. }
  631. if (bottomDist <= edgeThreshold && this.everMovedDown && scrollCache.canScrollDown() &&
  632. (!bestSide || bestSide.distance > bottomDist)) {
  633. bestSide = { scrollCache, name: 'bottom', distance: bottomDist };
  634. }
  635. /*
  636. TODO: fix broken RTL scrolling. canScrollLeft always returning false
  637. https://github.com/fullcalendar/fullcalendar/issues/4837
  638. */
  639. if (leftDist <= edgeThreshold && this.everMovedLeft && scrollCache.canScrollLeft() &&
  640. (!bestSide || bestSide.distance > leftDist)) {
  641. bestSide = { scrollCache, name: 'left', distance: leftDist };
  642. }
  643. if (rightDist <= edgeThreshold && this.everMovedRight && scrollCache.canScrollRight() &&
  644. (!bestSide || bestSide.distance > rightDist)) {
  645. bestSide = { scrollCache, name: 'right', distance: rightDist };
  646. }
  647. }
  648. }
  649. return bestSide;
  650. }
  651. buildCaches(scrollStartEl) {
  652. return this.queryScrollEls(scrollStartEl).map((el) => {
  653. if (el === window) {
  654. return new WindowScrollGeomCache(false); // false = don't listen to user-generated scrolls
  655. }
  656. return new ElementScrollGeomCache(el, false); // false = don't listen to user-generated scrolls
  657. });
  658. }
  659. queryScrollEls(scrollStartEl) {
  660. let els = [];
  661. for (let query of this.scrollQuery) {
  662. if (typeof query === 'object') {
  663. els.push(query);
  664. }
  665. else {
  666. /*
  667. TODO: in the future, always have auto-scroll happen on element where current Hit came from
  668. Ticket: https://github.com/fullcalendar/fullcalendar/issues/4593
  669. */
  670. els.push(...Array.prototype.slice.call(scrollStartEl.getRootNode().querySelectorAll(query)));
  671. }
  672. }
  673. return els;
  674. }
  675. }
  676. /*
  677. Monitors dragging on an element. Has a number of high-level features:
  678. - minimum distance required before dragging
  679. - minimum wait time ("delay") before dragging
  680. - a mirror element that follows the pointer
  681. */
  682. class FeaturefulElementDragging extends internal_cjs.ElementDragging {
  683. constructor(containerEl, selector) {
  684. super(containerEl);
  685. this.containerEl = containerEl;
  686. // options that can be directly set by caller
  687. // the caller can also set the PointerDragging's options as well
  688. this.delay = null;
  689. this.minDistance = 0;
  690. this.touchScrollAllowed = true; // prevents drag from starting and blocks scrolling during drag
  691. this.mirrorNeedsRevert = false;
  692. this.isInteracting = false; // is the user validly moving the pointer? lasts until pointerup
  693. this.isDragging = false; // is it INTENTFULLY dragging? lasts until after revert animation
  694. this.isDelayEnded = false;
  695. this.isDistanceSurpassed = false;
  696. this.delayTimeoutId = null;
  697. this.onPointerDown = (ev) => {
  698. if (!this.isDragging) { // so new drag doesn't happen while revert animation is going
  699. this.isInteracting = true;
  700. this.isDelayEnded = false;
  701. this.isDistanceSurpassed = false;
  702. internal_cjs.preventSelection(document.body);
  703. internal_cjs.preventContextMenu(document.body);
  704. // prevent links from being visited if there's an eventual drag.
  705. // also prevents selection in older browsers (maybe?).
  706. // not necessary for touch, besides, browser would complain about passiveness.
  707. if (!ev.isTouch) {
  708. ev.origEvent.preventDefault();
  709. }
  710. this.emitter.trigger('pointerdown', ev);
  711. if (this.isInteracting && // not destroyed via pointerdown handler
  712. !this.pointer.shouldIgnoreMove) {
  713. // actions related to initiating dragstart+dragmove+dragend...
  714. this.mirror.setIsVisible(false); // reset. caller must set-visible
  715. this.mirror.start(ev.subjectEl, ev.pageX, ev.pageY); // must happen on first pointer down
  716. this.startDelay(ev);
  717. if (!this.minDistance) {
  718. this.handleDistanceSurpassed(ev);
  719. }
  720. }
  721. }
  722. };
  723. this.onPointerMove = (ev) => {
  724. if (this.isInteracting) {
  725. this.emitter.trigger('pointermove', ev);
  726. if (!this.isDistanceSurpassed) {
  727. let minDistance = this.minDistance;
  728. let distanceSq; // current distance from the origin, squared
  729. let { deltaX, deltaY } = ev;
  730. distanceSq = deltaX * deltaX + deltaY * deltaY;
  731. if (distanceSq >= minDistance * minDistance) { // use pythagorean theorem
  732. this.handleDistanceSurpassed(ev);
  733. }
  734. }
  735. if (this.isDragging) {
  736. // a real pointer move? (not one simulated by scrolling)
  737. if (ev.origEvent.type !== 'scroll') {
  738. this.mirror.handleMove(ev.pageX, ev.pageY);
  739. this.autoScroller.handleMove(ev.pageX, ev.pageY);
  740. }
  741. this.emitter.trigger('dragmove', ev);
  742. }
  743. }
  744. };
  745. this.onPointerUp = (ev) => {
  746. if (this.isInteracting) {
  747. this.isInteracting = false;
  748. internal_cjs.allowSelection(document.body);
  749. internal_cjs.allowContextMenu(document.body);
  750. this.emitter.trigger('pointerup', ev); // can potentially set mirrorNeedsRevert
  751. if (this.isDragging) {
  752. this.autoScroller.stop();
  753. this.tryStopDrag(ev); // which will stop the mirror
  754. }
  755. if (this.delayTimeoutId) {
  756. clearTimeout(this.delayTimeoutId);
  757. this.delayTimeoutId = null;
  758. }
  759. }
  760. };
  761. let pointer = this.pointer = new PointerDragging(containerEl);
  762. pointer.emitter.on('pointerdown', this.onPointerDown);
  763. pointer.emitter.on('pointermove', this.onPointerMove);
  764. pointer.emitter.on('pointerup', this.onPointerUp);
  765. if (selector) {
  766. pointer.selector = selector;
  767. }
  768. this.mirror = new ElementMirror();
  769. this.autoScroller = new AutoScroller();
  770. }
  771. destroy() {
  772. this.pointer.destroy();
  773. // HACK: simulate a pointer-up to end the current drag
  774. // TODO: fire 'dragend' directly and stop interaction. discourage use of pointerup event (b/c might not fire)
  775. this.onPointerUp({});
  776. }
  777. startDelay(ev) {
  778. if (typeof this.delay === 'number') {
  779. this.delayTimeoutId = setTimeout(() => {
  780. this.delayTimeoutId = null;
  781. this.handleDelayEnd(ev);
  782. }, this.delay); // not assignable to number!
  783. }
  784. else {
  785. this.handleDelayEnd(ev);
  786. }
  787. }
  788. handleDelayEnd(ev) {
  789. this.isDelayEnded = true;
  790. this.tryStartDrag(ev);
  791. }
  792. handleDistanceSurpassed(ev) {
  793. this.isDistanceSurpassed = true;
  794. this.tryStartDrag(ev);
  795. }
  796. tryStartDrag(ev) {
  797. if (this.isDelayEnded && this.isDistanceSurpassed) {
  798. if (!this.pointer.wasTouchScroll || this.touchScrollAllowed) {
  799. this.isDragging = true;
  800. this.mirrorNeedsRevert = false;
  801. this.autoScroller.start(ev.pageX, ev.pageY, this.containerEl);
  802. this.emitter.trigger('dragstart', ev);
  803. if (this.touchScrollAllowed === false) {
  804. this.pointer.cancelTouchScroll();
  805. }
  806. }
  807. }
  808. }
  809. tryStopDrag(ev) {
  810. // .stop() is ALWAYS asynchronous, which we NEED because we want all pointerup events
  811. // that come from the document to fire beforehand. much more convenient this way.
  812. this.mirror.stop(this.mirrorNeedsRevert, this.stopDrag.bind(this, ev));
  813. }
  814. stopDrag(ev) {
  815. this.isDragging = false;
  816. this.emitter.trigger('dragend', ev);
  817. }
  818. // fill in the implementations...
  819. setIgnoreMove(bool) {
  820. this.pointer.shouldIgnoreMove = bool;
  821. }
  822. setMirrorIsVisible(bool) {
  823. this.mirror.setIsVisible(bool);
  824. }
  825. setMirrorNeedsRevert(bool) {
  826. this.mirrorNeedsRevert = bool;
  827. }
  828. setAutoScrollEnabled(bool) {
  829. this.autoScroller.isEnabled = bool;
  830. }
  831. }
  832. /*
  833. When this class is instantiated, it records the offset of an element (relative to the document topleft),
  834. and continues to monitor scrolling, updating the cached coordinates if it needs to.
  835. Does not access the DOM after instantiation, so highly performant.
  836. Also keeps track of all scrolling/overflow:hidden containers that are parents of the given element
  837. and an determine if a given point is inside the combined clipping rectangle.
  838. */
  839. class OffsetTracker {
  840. constructor(el) {
  841. this.el = el;
  842. this.origRect = internal_cjs.computeRect(el);
  843. // will work fine for divs that have overflow:hidden
  844. this.scrollCaches = internal_cjs.getClippingParents(el).map((scrollEl) => new ElementScrollGeomCache(scrollEl, true));
  845. }
  846. destroy() {
  847. for (let scrollCache of this.scrollCaches) {
  848. scrollCache.destroy();
  849. }
  850. }
  851. computeLeft() {
  852. let left = this.origRect.left;
  853. for (let scrollCache of this.scrollCaches) {
  854. left += scrollCache.origScrollLeft - scrollCache.getScrollLeft();
  855. }
  856. return left;
  857. }
  858. computeTop() {
  859. let top = this.origRect.top;
  860. for (let scrollCache of this.scrollCaches) {
  861. top += scrollCache.origScrollTop - scrollCache.getScrollTop();
  862. }
  863. return top;
  864. }
  865. isWithinClipping(pageX, pageY) {
  866. let point = { left: pageX, top: pageY };
  867. for (let scrollCache of this.scrollCaches) {
  868. if (!isIgnoredClipping(scrollCache.getEventTarget()) &&
  869. !internal_cjs.pointInsideRect(point, scrollCache.clientRect)) {
  870. return false;
  871. }
  872. }
  873. return true;
  874. }
  875. }
  876. // certain clipping containers should never constrain interactions, like <html> and <body>
  877. // https://github.com/fullcalendar/fullcalendar/issues/3615
  878. function isIgnoredClipping(node) {
  879. let tagName = node.tagName;
  880. return tagName === 'HTML' || tagName === 'BODY';
  881. }
  882. /*
  883. Tracks movement over multiple droppable areas (aka "hits")
  884. that exist in one or more DateComponents.
  885. Relies on an existing draggable.
  886. emits:
  887. - pointerdown
  888. - dragstart
  889. - hitchange - fires initially, even if not over a hit
  890. - pointerup
  891. - (hitchange - again, to null, if ended over a hit)
  892. - dragend
  893. */
  894. class HitDragging {
  895. constructor(dragging, droppableStore) {
  896. // options that can be set by caller
  897. this.useSubjectCenter = false;
  898. this.requireInitial = true; // if doesn't start out on a hit, won't emit any events
  899. this.disablePointCheck = false;
  900. this.initialHit = null;
  901. this.movingHit = null;
  902. this.finalHit = null; // won't ever be populated if shouldIgnoreMove
  903. this.handlePointerDown = (ev) => {
  904. let { dragging } = this;
  905. this.initialHit = null;
  906. this.movingHit = null;
  907. this.finalHit = null;
  908. this.prepareHits();
  909. this.processFirstCoord(ev);
  910. if (this.initialHit || !this.requireInitial) {
  911. dragging.setIgnoreMove(false);
  912. // TODO: fire this before computing processFirstCoord, so listeners can cancel. this gets fired by almost every handler :(
  913. this.emitter.trigger('pointerdown', ev);
  914. }
  915. else {
  916. dragging.setIgnoreMove(true);
  917. }
  918. };
  919. this.handleDragStart = (ev) => {
  920. this.emitter.trigger('dragstart', ev);
  921. this.handleMove(ev, true); // force = fire even if initially null
  922. };
  923. this.handleDragMove = (ev) => {
  924. this.emitter.trigger('dragmove', ev);
  925. this.handleMove(ev);
  926. };
  927. this.handlePointerUp = (ev) => {
  928. this.releaseHits();
  929. this.emitter.trigger('pointerup', ev);
  930. };
  931. this.handleDragEnd = (ev) => {
  932. if (this.movingHit) {
  933. this.emitter.trigger('hitupdate', null, true, ev);
  934. }
  935. this.finalHit = this.movingHit;
  936. this.movingHit = null;
  937. this.emitter.trigger('dragend', ev);
  938. };
  939. this.droppableStore = droppableStore;
  940. dragging.emitter.on('pointerdown', this.handlePointerDown);
  941. dragging.emitter.on('dragstart', this.handleDragStart);
  942. dragging.emitter.on('dragmove', this.handleDragMove);
  943. dragging.emitter.on('pointerup', this.handlePointerUp);
  944. dragging.emitter.on('dragend', this.handleDragEnd);
  945. this.dragging = dragging;
  946. this.emitter = new internal_cjs.Emitter();
  947. }
  948. // sets initialHit
  949. // sets coordAdjust
  950. processFirstCoord(ev) {
  951. let origPoint = { left: ev.pageX, top: ev.pageY };
  952. let adjustedPoint = origPoint;
  953. let subjectEl = ev.subjectEl;
  954. let subjectRect;
  955. if (subjectEl instanceof HTMLElement) { // i.e. not a Document/ShadowRoot
  956. subjectRect = internal_cjs.computeRect(subjectEl);
  957. adjustedPoint = internal_cjs.constrainPoint(adjustedPoint, subjectRect);
  958. }
  959. let initialHit = this.initialHit = this.queryHitForOffset(adjustedPoint.left, adjustedPoint.top);
  960. if (initialHit) {
  961. if (this.useSubjectCenter && subjectRect) {
  962. let slicedSubjectRect = internal_cjs.intersectRects(subjectRect, initialHit.rect);
  963. if (slicedSubjectRect) {
  964. adjustedPoint = internal_cjs.getRectCenter(slicedSubjectRect);
  965. }
  966. }
  967. this.coordAdjust = internal_cjs.diffPoints(adjustedPoint, origPoint);
  968. }
  969. else {
  970. this.coordAdjust = { left: 0, top: 0 };
  971. }
  972. }
  973. handleMove(ev, forceHandle) {
  974. let hit = this.queryHitForOffset(ev.pageX + this.coordAdjust.left, ev.pageY + this.coordAdjust.top);
  975. if (forceHandle || !isHitsEqual(this.movingHit, hit)) {
  976. this.movingHit = hit;
  977. this.emitter.trigger('hitupdate', hit, false, ev);
  978. }
  979. }
  980. prepareHits() {
  981. this.offsetTrackers = internal_cjs.mapHash(this.droppableStore, (interactionSettings) => {
  982. interactionSettings.component.prepareHits();
  983. return new OffsetTracker(interactionSettings.el);
  984. });
  985. }
  986. releaseHits() {
  987. let { offsetTrackers } = this;
  988. for (let id in offsetTrackers) {
  989. offsetTrackers[id].destroy();
  990. }
  991. this.offsetTrackers = {};
  992. }
  993. queryHitForOffset(offsetLeft, offsetTop) {
  994. let { droppableStore, offsetTrackers } = this;
  995. let bestHit = null;
  996. for (let id in droppableStore) {
  997. let component = droppableStore[id].component;
  998. let offsetTracker = offsetTrackers[id];
  999. if (offsetTracker && // wasn't destroyed mid-drag
  1000. offsetTracker.isWithinClipping(offsetLeft, offsetTop)) {
  1001. let originLeft = offsetTracker.computeLeft();
  1002. let originTop = offsetTracker.computeTop();
  1003. let positionLeft = offsetLeft - originLeft;
  1004. let positionTop = offsetTop - originTop;
  1005. let { origRect } = offsetTracker;
  1006. let width = origRect.right - origRect.left;
  1007. let height = origRect.bottom - origRect.top;
  1008. if (
  1009. // must be within the element's bounds
  1010. positionLeft >= 0 && positionLeft < width &&
  1011. positionTop >= 0 && positionTop < height) {
  1012. let hit = component.queryHit(positionLeft, positionTop, width, height);
  1013. if (hit && (
  1014. // make sure the hit is within activeRange, meaning it's not a dead cell
  1015. internal_cjs.rangeContainsRange(hit.dateProfile.activeRange, hit.dateSpan.range)) &&
  1016. // Ensure the component we are querying for the hit is accessibly my the pointer
  1017. // Prevents obscured calendars (ex: under a modal dialog) from accepting hit
  1018. // https://github.com/fullcalendar/fullcalendar/issues/5026
  1019. (this.disablePointCheck ||
  1020. offsetTracker.el.contains(document.elementFromPoint(
  1021. // add-back origins to get coordinate relative to top-left of window viewport
  1022. positionLeft + originLeft - window.scrollX, positionTop + originTop - window.scrollY))) &&
  1023. (!bestHit || hit.layer > bestHit.layer)) {
  1024. hit.componentId = id;
  1025. hit.context = component.context;
  1026. // TODO: better way to re-orient rectangle
  1027. hit.rect.left += originLeft;
  1028. hit.rect.right += originLeft;
  1029. hit.rect.top += originTop;
  1030. hit.rect.bottom += originTop;
  1031. bestHit = hit;
  1032. }
  1033. }
  1034. }
  1035. }
  1036. return bestHit;
  1037. }
  1038. }
  1039. function isHitsEqual(hit0, hit1) {
  1040. if (!hit0 && !hit1) {
  1041. return true;
  1042. }
  1043. if (Boolean(hit0) !== Boolean(hit1)) {
  1044. return false;
  1045. }
  1046. return internal_cjs.isDateSpansEqual(hit0.dateSpan, hit1.dateSpan);
  1047. }
  1048. function buildDatePointApiWithContext(dateSpan, context) {
  1049. let props = {};
  1050. for (let transform of context.pluginHooks.datePointTransforms) {
  1051. Object.assign(props, transform(dateSpan, context));
  1052. }
  1053. Object.assign(props, buildDatePointApi(dateSpan, context.dateEnv));
  1054. return props;
  1055. }
  1056. function buildDatePointApi(span, dateEnv) {
  1057. return {
  1058. date: dateEnv.toDate(span.range.start),
  1059. dateStr: dateEnv.formatIso(span.range.start, { omitTime: span.allDay }),
  1060. allDay: span.allDay,
  1061. };
  1062. }
  1063. /*
  1064. Monitors when the user clicks on a specific date/time of a component.
  1065. A pointerdown+pointerup on the same "hit" constitutes a click.
  1066. */
  1067. class DateClicking extends internal_cjs.Interaction {
  1068. constructor(settings) {
  1069. super(settings);
  1070. this.handlePointerDown = (pev) => {
  1071. let { dragging } = this;
  1072. let downEl = pev.origEvent.target;
  1073. // do this in pointerdown (not dragend) because DOM might be mutated by the time dragend is fired
  1074. dragging.setIgnoreMove(!this.component.isValidDateDownEl(downEl));
  1075. };
  1076. // won't even fire if moving was ignored
  1077. this.handleDragEnd = (ev) => {
  1078. let { component } = this;
  1079. let { pointer } = this.dragging;
  1080. if (!pointer.wasTouchScroll) {
  1081. let { initialHit, finalHit } = this.hitDragging;
  1082. if (initialHit && finalHit && isHitsEqual(initialHit, finalHit)) {
  1083. let { context } = component;
  1084. let arg = Object.assign(Object.assign({}, buildDatePointApiWithContext(initialHit.dateSpan, context)), { dayEl: initialHit.dayEl, jsEvent: ev.origEvent, view: context.viewApi || context.calendarApi.view });
  1085. context.emitter.trigger('dateClick', arg);
  1086. }
  1087. }
  1088. };
  1089. // we DO want to watch pointer moves because otherwise finalHit won't get populated
  1090. this.dragging = new FeaturefulElementDragging(settings.el);
  1091. this.dragging.autoScroller.isEnabled = false;
  1092. let hitDragging = this.hitDragging = new HitDragging(this.dragging, internal_cjs.interactionSettingsToStore(settings));
  1093. hitDragging.emitter.on('pointerdown', this.handlePointerDown);
  1094. hitDragging.emitter.on('dragend', this.handleDragEnd);
  1095. }
  1096. destroy() {
  1097. this.dragging.destroy();
  1098. }
  1099. }
  1100. /*
  1101. Tracks when the user selects a portion of time of a component,
  1102. constituted by a drag over date cells, with a possible delay at the beginning of the drag.
  1103. */
  1104. class DateSelecting extends internal_cjs.Interaction {
  1105. constructor(settings) {
  1106. super(settings);
  1107. this.dragSelection = null;
  1108. this.handlePointerDown = (ev) => {
  1109. let { component, dragging } = this;
  1110. let { options } = component.context;
  1111. let canSelect = options.selectable &&
  1112. component.isValidDateDownEl(ev.origEvent.target);
  1113. // don't bother to watch expensive moves if component won't do selection
  1114. dragging.setIgnoreMove(!canSelect);
  1115. // if touch, require user to hold down
  1116. dragging.delay = ev.isTouch ? getComponentTouchDelay$1(component) : null;
  1117. };
  1118. this.handleDragStart = (ev) => {
  1119. this.component.context.calendarApi.unselect(ev); // unselect previous selections
  1120. };
  1121. this.handleHitUpdate = (hit, isFinal) => {
  1122. let { context } = this.component;
  1123. let dragSelection = null;
  1124. let isInvalid = false;
  1125. if (hit) {
  1126. let initialHit = this.hitDragging.initialHit;
  1127. let disallowed = hit.componentId === initialHit.componentId
  1128. && this.isHitComboAllowed
  1129. && !this.isHitComboAllowed(initialHit, hit);
  1130. if (!disallowed) {
  1131. dragSelection = joinHitsIntoSelection(initialHit, hit, context.pluginHooks.dateSelectionTransformers);
  1132. }
  1133. if (!dragSelection || !internal_cjs.isDateSelectionValid(dragSelection, hit.dateProfile, context)) {
  1134. isInvalid = true;
  1135. dragSelection = null;
  1136. }
  1137. }
  1138. if (dragSelection) {
  1139. context.dispatch({ type: 'SELECT_DATES', selection: dragSelection });
  1140. }
  1141. else if (!isFinal) { // only unselect if moved away while dragging
  1142. context.dispatch({ type: 'UNSELECT_DATES' });
  1143. }
  1144. if (!isInvalid) {
  1145. internal_cjs.enableCursor();
  1146. }
  1147. else {
  1148. internal_cjs.disableCursor();
  1149. }
  1150. if (!isFinal) {
  1151. this.dragSelection = dragSelection; // only clear if moved away from all hits while dragging
  1152. }
  1153. };
  1154. this.handlePointerUp = (pev) => {
  1155. if (this.dragSelection) {
  1156. // selection is already rendered, so just need to report selection
  1157. internal_cjs.triggerDateSelect(this.dragSelection, pev, this.component.context);
  1158. this.dragSelection = null;
  1159. }
  1160. };
  1161. let { component } = settings;
  1162. let { options } = component.context;
  1163. let dragging = this.dragging = new FeaturefulElementDragging(settings.el);
  1164. dragging.touchScrollAllowed = false;
  1165. dragging.minDistance = options.selectMinDistance || 0;
  1166. dragging.autoScroller.isEnabled = options.dragScroll;
  1167. let hitDragging = this.hitDragging = new HitDragging(this.dragging, internal_cjs.interactionSettingsToStore(settings));
  1168. hitDragging.emitter.on('pointerdown', this.handlePointerDown);
  1169. hitDragging.emitter.on('dragstart', this.handleDragStart);
  1170. hitDragging.emitter.on('hitupdate', this.handleHitUpdate);
  1171. hitDragging.emitter.on('pointerup', this.handlePointerUp);
  1172. }
  1173. destroy() {
  1174. this.dragging.destroy();
  1175. }
  1176. }
  1177. function getComponentTouchDelay$1(component) {
  1178. let { options } = component.context;
  1179. let delay = options.selectLongPressDelay;
  1180. if (delay == null) {
  1181. delay = options.longPressDelay;
  1182. }
  1183. return delay;
  1184. }
  1185. function joinHitsIntoSelection(hit0, hit1, dateSelectionTransformers) {
  1186. let dateSpan0 = hit0.dateSpan;
  1187. let dateSpan1 = hit1.dateSpan;
  1188. let ms = [
  1189. dateSpan0.range.start,
  1190. dateSpan0.range.end,
  1191. dateSpan1.range.start,
  1192. dateSpan1.range.end,
  1193. ];
  1194. ms.sort(internal_cjs.compareNumbers);
  1195. let props = {};
  1196. for (let transformer of dateSelectionTransformers) {
  1197. let res = transformer(hit0, hit1);
  1198. if (res === false) {
  1199. return null;
  1200. }
  1201. if (res) {
  1202. Object.assign(props, res);
  1203. }
  1204. }
  1205. props.range = { start: ms[0], end: ms[3] };
  1206. props.allDay = dateSpan0.allDay;
  1207. return props;
  1208. }
  1209. class EventDragging extends internal_cjs.Interaction {
  1210. constructor(settings) {
  1211. super(settings);
  1212. // internal state
  1213. this.subjectEl = null;
  1214. this.subjectSeg = null; // the seg being selected/dragged
  1215. this.isDragging = false;
  1216. this.eventRange = null;
  1217. this.relevantEvents = null; // the events being dragged
  1218. this.receivingContext = null;
  1219. this.validMutation = null;
  1220. this.mutatedRelevantEvents = null;
  1221. this.handlePointerDown = (ev) => {
  1222. let origTarget = ev.origEvent.target;
  1223. let { component, dragging } = this;
  1224. let { mirror } = dragging;
  1225. let { options } = component.context;
  1226. let initialContext = component.context;
  1227. this.subjectEl = ev.subjectEl;
  1228. let subjectSeg = this.subjectSeg = internal_cjs.getElSeg(ev.subjectEl);
  1229. let eventRange = this.eventRange = subjectSeg.eventRange;
  1230. let eventInstanceId = eventRange.instance.instanceId;
  1231. this.relevantEvents = internal_cjs.getRelevantEvents(initialContext.getCurrentData().eventStore, eventInstanceId);
  1232. dragging.minDistance = ev.isTouch ? 0 : options.eventDragMinDistance;
  1233. dragging.delay =
  1234. // only do a touch delay if touch and this event hasn't been selected yet
  1235. (ev.isTouch && eventInstanceId !== component.props.eventSelection) ?
  1236. getComponentTouchDelay(component) :
  1237. null;
  1238. if (options.fixedMirrorParent) {
  1239. mirror.parentNode = options.fixedMirrorParent;
  1240. }
  1241. else {
  1242. mirror.parentNode = internal_cjs.elementClosest(origTarget, '.fc');
  1243. }
  1244. mirror.revertDuration = options.dragRevertDuration;
  1245. let isValid = component.isValidSegDownEl(origTarget) &&
  1246. !internal_cjs.elementClosest(origTarget, '.fc-event-resizer'); // NOT on a resizer
  1247. dragging.setIgnoreMove(!isValid);
  1248. // disable dragging for elements that are resizable (ie, selectable)
  1249. // but are not draggable
  1250. this.isDragging = isValid &&
  1251. ev.subjectEl.classList.contains('fc-event-draggable');
  1252. };
  1253. this.handleDragStart = (ev) => {
  1254. let initialContext = this.component.context;
  1255. let eventRange = this.eventRange;
  1256. let eventInstanceId = eventRange.instance.instanceId;
  1257. if (ev.isTouch) {
  1258. // need to select a different event?
  1259. if (eventInstanceId !== this.component.props.eventSelection) {
  1260. initialContext.dispatch({ type: 'SELECT_EVENT', eventInstanceId });
  1261. }
  1262. }
  1263. else {
  1264. // if now using mouse, but was previous touch interaction, clear selected event
  1265. initialContext.dispatch({ type: 'UNSELECT_EVENT' });
  1266. }
  1267. if (this.isDragging) {
  1268. initialContext.calendarApi.unselect(ev); // unselect *date* selection
  1269. initialContext.emitter.trigger('eventDragStart', {
  1270. el: this.subjectEl,
  1271. event: new internal_cjs.EventImpl(initialContext, eventRange.def, eventRange.instance),
  1272. jsEvent: ev.origEvent,
  1273. view: initialContext.viewApi,
  1274. });
  1275. }
  1276. };
  1277. this.handleHitUpdate = (hit, isFinal) => {
  1278. if (!this.isDragging) {
  1279. return;
  1280. }
  1281. let relevantEvents = this.relevantEvents;
  1282. let initialHit = this.hitDragging.initialHit;
  1283. let initialContext = this.component.context;
  1284. // states based on new hit
  1285. let receivingContext = null;
  1286. let mutation = null;
  1287. let mutatedRelevantEvents = null;
  1288. let isInvalid = false;
  1289. let interaction = {
  1290. affectedEvents: relevantEvents,
  1291. mutatedEvents: internal_cjs.createEmptyEventStore(),
  1292. isEvent: true,
  1293. };
  1294. if (hit) {
  1295. receivingContext = hit.context;
  1296. let receivingOptions = receivingContext.options;
  1297. if (initialContext === receivingContext ||
  1298. (receivingOptions.editable && receivingOptions.droppable)) {
  1299. mutation = computeEventMutation(initialHit, hit, this.eventRange.instance.range.start, receivingContext.getCurrentData().pluginHooks.eventDragMutationMassagers);
  1300. if (mutation) {
  1301. mutatedRelevantEvents = internal_cjs.applyMutationToEventStore(relevantEvents, receivingContext.getCurrentData().eventUiBases, mutation, receivingContext);
  1302. interaction.mutatedEvents = mutatedRelevantEvents;
  1303. if (!internal_cjs.isInteractionValid(interaction, hit.dateProfile, receivingContext)) {
  1304. isInvalid = true;
  1305. mutation = null;
  1306. mutatedRelevantEvents = null;
  1307. interaction.mutatedEvents = internal_cjs.createEmptyEventStore();
  1308. }
  1309. }
  1310. }
  1311. else {
  1312. receivingContext = null;
  1313. }
  1314. }
  1315. this.displayDrag(receivingContext, interaction);
  1316. if (!isInvalid) {
  1317. internal_cjs.enableCursor();
  1318. }
  1319. else {
  1320. internal_cjs.disableCursor();
  1321. }
  1322. if (!isFinal) {
  1323. if (initialContext === receivingContext && // TODO: write test for this
  1324. isHitsEqual(initialHit, hit)) {
  1325. mutation = null;
  1326. }
  1327. this.dragging.setMirrorNeedsRevert(!mutation);
  1328. // render the mirror if no already-rendered mirror
  1329. // TODO: wish we could somehow wait for dispatch to guarantee render
  1330. this.dragging.setMirrorIsVisible(!hit || !this.subjectEl.getRootNode().querySelector('.fc-event-mirror'));
  1331. // assign states based on new hit
  1332. this.receivingContext = receivingContext;
  1333. this.validMutation = mutation;
  1334. this.mutatedRelevantEvents = mutatedRelevantEvents;
  1335. }
  1336. };
  1337. this.handlePointerUp = () => {
  1338. if (!this.isDragging) {
  1339. this.cleanup(); // because handleDragEnd won't fire
  1340. }
  1341. };
  1342. this.handleDragEnd = (ev) => {
  1343. if (this.isDragging) {
  1344. let initialContext = this.component.context;
  1345. let initialView = initialContext.viewApi;
  1346. let { receivingContext, validMutation } = this;
  1347. let eventDef = this.eventRange.def;
  1348. let eventInstance = this.eventRange.instance;
  1349. let eventApi = new internal_cjs.EventImpl(initialContext, eventDef, eventInstance);
  1350. let relevantEvents = this.relevantEvents;
  1351. let mutatedRelevantEvents = this.mutatedRelevantEvents;
  1352. let { finalHit } = this.hitDragging;
  1353. this.clearDrag(); // must happen after revert animation
  1354. initialContext.emitter.trigger('eventDragStop', {
  1355. el: this.subjectEl,
  1356. event: eventApi,
  1357. jsEvent: ev.origEvent,
  1358. view: initialView,
  1359. });
  1360. if (validMutation) {
  1361. // dropped within same calendar
  1362. if (receivingContext === initialContext) {
  1363. let updatedEventApi = new internal_cjs.EventImpl(initialContext, mutatedRelevantEvents.defs[eventDef.defId], eventInstance ? mutatedRelevantEvents.instances[eventInstance.instanceId] : null);
  1364. initialContext.dispatch({
  1365. type: 'MERGE_EVENTS',
  1366. eventStore: mutatedRelevantEvents,
  1367. });
  1368. let eventChangeArg = {
  1369. oldEvent: eventApi,
  1370. event: updatedEventApi,
  1371. relatedEvents: internal_cjs.buildEventApis(mutatedRelevantEvents, initialContext, eventInstance),
  1372. revert() {
  1373. initialContext.dispatch({
  1374. type: 'MERGE_EVENTS',
  1375. eventStore: relevantEvents, // the pre-change data
  1376. });
  1377. },
  1378. };
  1379. let transformed = {};
  1380. for (let transformer of initialContext.getCurrentData().pluginHooks.eventDropTransformers) {
  1381. Object.assign(transformed, transformer(validMutation, initialContext));
  1382. }
  1383. initialContext.emitter.trigger('eventDrop', Object.assign(Object.assign(Object.assign({}, eventChangeArg), transformed), { el: ev.subjectEl, delta: validMutation.datesDelta, jsEvent: ev.origEvent, view: initialView }));
  1384. initialContext.emitter.trigger('eventChange', eventChangeArg);
  1385. // dropped in different calendar
  1386. }
  1387. else if (receivingContext) {
  1388. let eventRemoveArg = {
  1389. event: eventApi,
  1390. relatedEvents: internal_cjs.buildEventApis(relevantEvents, initialContext, eventInstance),
  1391. revert() {
  1392. initialContext.dispatch({
  1393. type: 'MERGE_EVENTS',
  1394. eventStore: relevantEvents,
  1395. });
  1396. },
  1397. };
  1398. initialContext.emitter.trigger('eventLeave', Object.assign(Object.assign({}, eventRemoveArg), { draggedEl: ev.subjectEl, view: initialView }));
  1399. initialContext.dispatch({
  1400. type: 'REMOVE_EVENTS',
  1401. eventStore: relevantEvents,
  1402. });
  1403. initialContext.emitter.trigger('eventRemove', eventRemoveArg);
  1404. let addedEventDef = mutatedRelevantEvents.defs[eventDef.defId];
  1405. let addedEventInstance = mutatedRelevantEvents.instances[eventInstance.instanceId];
  1406. let addedEventApi = new internal_cjs.EventImpl(receivingContext, addedEventDef, addedEventInstance);
  1407. receivingContext.dispatch({
  1408. type: 'MERGE_EVENTS',
  1409. eventStore: mutatedRelevantEvents,
  1410. });
  1411. let eventAddArg = {
  1412. event: addedEventApi,
  1413. relatedEvents: internal_cjs.buildEventApis(mutatedRelevantEvents, receivingContext, addedEventInstance),
  1414. revert() {
  1415. receivingContext.dispatch({
  1416. type: 'REMOVE_EVENTS',
  1417. eventStore: mutatedRelevantEvents,
  1418. });
  1419. },
  1420. };
  1421. receivingContext.emitter.trigger('eventAdd', eventAddArg);
  1422. if (ev.isTouch) {
  1423. receivingContext.dispatch({
  1424. type: 'SELECT_EVENT',
  1425. eventInstanceId: eventInstance.instanceId,
  1426. });
  1427. }
  1428. receivingContext.emitter.trigger('drop', Object.assign(Object.assign({}, buildDatePointApiWithContext(finalHit.dateSpan, receivingContext)), { draggedEl: ev.subjectEl, jsEvent: ev.origEvent, view: finalHit.context.viewApi }));
  1429. receivingContext.emitter.trigger('eventReceive', Object.assign(Object.assign({}, eventAddArg), { draggedEl: ev.subjectEl, view: finalHit.context.viewApi }));
  1430. }
  1431. }
  1432. else {
  1433. initialContext.emitter.trigger('_noEventDrop');
  1434. }
  1435. }
  1436. this.cleanup();
  1437. };
  1438. let { component } = this;
  1439. let { options } = component.context;
  1440. let dragging = this.dragging = new FeaturefulElementDragging(settings.el);
  1441. dragging.pointer.selector = EventDragging.SELECTOR;
  1442. dragging.touchScrollAllowed = false;
  1443. dragging.autoScroller.isEnabled = options.dragScroll;
  1444. let hitDragging = this.hitDragging = new HitDragging(this.dragging, internal_cjs.interactionSettingsStore);
  1445. hitDragging.useSubjectCenter = settings.useEventCenter;
  1446. hitDragging.emitter.on('pointerdown', this.handlePointerDown);
  1447. hitDragging.emitter.on('dragstart', this.handleDragStart);
  1448. hitDragging.emitter.on('hitupdate', this.handleHitUpdate);
  1449. hitDragging.emitter.on('pointerup', this.handlePointerUp);
  1450. hitDragging.emitter.on('dragend', this.handleDragEnd);
  1451. }
  1452. destroy() {
  1453. this.dragging.destroy();
  1454. }
  1455. // render a drag state on the next receivingCalendar
  1456. displayDrag(nextContext, state) {
  1457. let initialContext = this.component.context;
  1458. let prevContext = this.receivingContext;
  1459. // does the previous calendar need to be cleared?
  1460. if (prevContext && prevContext !== nextContext) {
  1461. // does the initial calendar need to be cleared?
  1462. // if so, don't clear all the way. we still need to to hide the affectedEvents
  1463. if (prevContext === initialContext) {
  1464. prevContext.dispatch({
  1465. type: 'SET_EVENT_DRAG',
  1466. state: {
  1467. affectedEvents: state.affectedEvents,
  1468. mutatedEvents: internal_cjs.createEmptyEventStore(),
  1469. isEvent: true,
  1470. },
  1471. });
  1472. // completely clear the old calendar if it wasn't the initial
  1473. }
  1474. else {
  1475. prevContext.dispatch({ type: 'UNSET_EVENT_DRAG' });
  1476. }
  1477. }
  1478. if (nextContext) {
  1479. nextContext.dispatch({ type: 'SET_EVENT_DRAG', state });
  1480. }
  1481. }
  1482. clearDrag() {
  1483. let initialCalendar = this.component.context;
  1484. let { receivingContext } = this;
  1485. if (receivingContext) {
  1486. receivingContext.dispatch({ type: 'UNSET_EVENT_DRAG' });
  1487. }
  1488. // the initial calendar might have an dummy drag state from displayDrag
  1489. if (initialCalendar !== receivingContext) {
  1490. initialCalendar.dispatch({ type: 'UNSET_EVENT_DRAG' });
  1491. }
  1492. }
  1493. cleanup() {
  1494. this.subjectSeg = null;
  1495. this.isDragging = false;
  1496. this.eventRange = null;
  1497. this.relevantEvents = null;
  1498. this.receivingContext = null;
  1499. this.validMutation = null;
  1500. this.mutatedRelevantEvents = null;
  1501. }
  1502. }
  1503. // TODO: test this in IE11
  1504. // QUESTION: why do we need it on the resizable???
  1505. EventDragging.SELECTOR = '.fc-event-draggable, .fc-event-resizable';
  1506. function computeEventMutation(hit0, hit1, eventInstanceStart, massagers) {
  1507. let dateSpan0 = hit0.dateSpan;
  1508. let dateSpan1 = hit1.dateSpan;
  1509. let date0 = dateSpan0.range.start;
  1510. let date1 = dateSpan1.range.start;
  1511. let standardProps = {};
  1512. if (dateSpan0.allDay !== dateSpan1.allDay) {
  1513. standardProps.allDay = dateSpan1.allDay;
  1514. standardProps.hasEnd = hit1.context.options.allDayMaintainDuration;
  1515. if (dateSpan1.allDay) {
  1516. // means date1 is already start-of-day,
  1517. // but date0 needs to be converted
  1518. date0 = internal_cjs.startOfDay(eventInstanceStart);
  1519. }
  1520. else {
  1521. // Moving from allDate->timed
  1522. // Doesn't matter where on the event the drag began, mutate the event's start-date to date1
  1523. date0 = eventInstanceStart;
  1524. }
  1525. }
  1526. let delta = internal_cjs.diffDates(date0, date1, hit0.context.dateEnv, hit0.componentId === hit1.componentId ?
  1527. hit0.largeUnit :
  1528. null);
  1529. if (delta.milliseconds) { // has hours/minutes/seconds
  1530. standardProps.allDay = false;
  1531. }
  1532. let mutation = {
  1533. datesDelta: delta,
  1534. standardProps,
  1535. };
  1536. for (let massager of massagers) {
  1537. massager(mutation, hit0, hit1);
  1538. }
  1539. return mutation;
  1540. }
  1541. function getComponentTouchDelay(component) {
  1542. let { options } = component.context;
  1543. let delay = options.eventLongPressDelay;
  1544. if (delay == null) {
  1545. delay = options.longPressDelay;
  1546. }
  1547. return delay;
  1548. }
  1549. class EventResizing extends internal_cjs.Interaction {
  1550. constructor(settings) {
  1551. super(settings);
  1552. // internal state
  1553. this.draggingSegEl = null;
  1554. this.draggingSeg = null; // TODO: rename to resizingSeg? subjectSeg?
  1555. this.eventRange = null;
  1556. this.relevantEvents = null;
  1557. this.validMutation = null;
  1558. this.mutatedRelevantEvents = null;
  1559. this.handlePointerDown = (ev) => {
  1560. let { component } = this;
  1561. let segEl = this.querySegEl(ev);
  1562. let seg = internal_cjs.getElSeg(segEl);
  1563. let eventRange = this.eventRange = seg.eventRange;
  1564. this.dragging.minDistance = component.context.options.eventDragMinDistance;
  1565. // if touch, need to be working with a selected event
  1566. this.dragging.setIgnoreMove(!this.component.isValidSegDownEl(ev.origEvent.target) ||
  1567. (ev.isTouch && this.component.props.eventSelection !== eventRange.instance.instanceId));
  1568. };
  1569. this.handleDragStart = (ev) => {
  1570. let { context } = this.component;
  1571. let eventRange = this.eventRange;
  1572. this.relevantEvents = internal_cjs.getRelevantEvents(context.getCurrentData().eventStore, this.eventRange.instance.instanceId);
  1573. let segEl = this.querySegEl(ev);
  1574. this.draggingSegEl = segEl;
  1575. this.draggingSeg = internal_cjs.getElSeg(segEl);
  1576. context.calendarApi.unselect();
  1577. context.emitter.trigger('eventResizeStart', {
  1578. el: segEl,
  1579. event: new internal_cjs.EventImpl(context, eventRange.def, eventRange.instance),
  1580. jsEvent: ev.origEvent,
  1581. view: context.viewApi,
  1582. });
  1583. };
  1584. this.handleHitUpdate = (hit, isFinal, ev) => {
  1585. let { context } = this.component;
  1586. let relevantEvents = this.relevantEvents;
  1587. let initialHit = this.hitDragging.initialHit;
  1588. let eventInstance = this.eventRange.instance;
  1589. let mutation = null;
  1590. let mutatedRelevantEvents = null;
  1591. let isInvalid = false;
  1592. let interaction = {
  1593. affectedEvents: relevantEvents,
  1594. mutatedEvents: internal_cjs.createEmptyEventStore(),
  1595. isEvent: true,
  1596. };
  1597. if (hit) {
  1598. let disallowed = hit.componentId === initialHit.componentId
  1599. && this.isHitComboAllowed
  1600. && !this.isHitComboAllowed(initialHit, hit);
  1601. if (!disallowed) {
  1602. mutation = computeMutation(initialHit, hit, ev.subjectEl.classList.contains('fc-event-resizer-start'), eventInstance.range);
  1603. }
  1604. }
  1605. if (mutation) {
  1606. mutatedRelevantEvents = internal_cjs.applyMutationToEventStore(relevantEvents, context.getCurrentData().eventUiBases, mutation, context);
  1607. interaction.mutatedEvents = mutatedRelevantEvents;
  1608. if (!internal_cjs.isInteractionValid(interaction, hit.dateProfile, context)) {
  1609. isInvalid = true;
  1610. mutation = null;
  1611. mutatedRelevantEvents = null;
  1612. interaction.mutatedEvents = null;
  1613. }
  1614. }
  1615. if (mutatedRelevantEvents) {
  1616. context.dispatch({
  1617. type: 'SET_EVENT_RESIZE',
  1618. state: interaction,
  1619. });
  1620. }
  1621. else {
  1622. context.dispatch({ type: 'UNSET_EVENT_RESIZE' });
  1623. }
  1624. if (!isInvalid) {
  1625. internal_cjs.enableCursor();
  1626. }
  1627. else {
  1628. internal_cjs.disableCursor();
  1629. }
  1630. if (!isFinal) {
  1631. if (mutation && isHitsEqual(initialHit, hit)) {
  1632. mutation = null;
  1633. }
  1634. this.validMutation = mutation;
  1635. this.mutatedRelevantEvents = mutatedRelevantEvents;
  1636. }
  1637. };
  1638. this.handleDragEnd = (ev) => {
  1639. let { context } = this.component;
  1640. let eventDef = this.eventRange.def;
  1641. let eventInstance = this.eventRange.instance;
  1642. let eventApi = new internal_cjs.EventImpl(context, eventDef, eventInstance);
  1643. let relevantEvents = this.relevantEvents;
  1644. let mutatedRelevantEvents = this.mutatedRelevantEvents;
  1645. context.emitter.trigger('eventResizeStop', {
  1646. el: this.draggingSegEl,
  1647. event: eventApi,
  1648. jsEvent: ev.origEvent,
  1649. view: context.viewApi,
  1650. });
  1651. if (this.validMutation) {
  1652. let updatedEventApi = new internal_cjs.EventImpl(context, mutatedRelevantEvents.defs[eventDef.defId], eventInstance ? mutatedRelevantEvents.instances[eventInstance.instanceId] : null);
  1653. context.dispatch({
  1654. type: 'MERGE_EVENTS',
  1655. eventStore: mutatedRelevantEvents,
  1656. });
  1657. let eventChangeArg = {
  1658. oldEvent: eventApi,
  1659. event: updatedEventApi,
  1660. relatedEvents: internal_cjs.buildEventApis(mutatedRelevantEvents, context, eventInstance),
  1661. revert() {
  1662. context.dispatch({
  1663. type: 'MERGE_EVENTS',
  1664. eventStore: relevantEvents, // the pre-change events
  1665. });
  1666. },
  1667. };
  1668. context.emitter.trigger('eventResize', Object.assign(Object.assign({}, eventChangeArg), { el: this.draggingSegEl, startDelta: this.validMutation.startDelta || internal_cjs.createDuration(0), endDelta: this.validMutation.endDelta || internal_cjs.createDuration(0), jsEvent: ev.origEvent, view: context.viewApi }));
  1669. context.emitter.trigger('eventChange', eventChangeArg);
  1670. }
  1671. else {
  1672. context.emitter.trigger('_noEventResize');
  1673. }
  1674. // reset all internal state
  1675. this.draggingSeg = null;
  1676. this.relevantEvents = null;
  1677. this.validMutation = null;
  1678. // okay to keep eventInstance around. useful to set it in handlePointerDown
  1679. };
  1680. let { component } = settings;
  1681. let dragging = this.dragging = new FeaturefulElementDragging(settings.el);
  1682. dragging.pointer.selector = '.fc-event-resizer';
  1683. dragging.touchScrollAllowed = false;
  1684. dragging.autoScroller.isEnabled = component.context.options.dragScroll;
  1685. let hitDragging = this.hitDragging = new HitDragging(this.dragging, internal_cjs.interactionSettingsToStore(settings));
  1686. hitDragging.emitter.on('pointerdown', this.handlePointerDown);
  1687. hitDragging.emitter.on('dragstart', this.handleDragStart);
  1688. hitDragging.emitter.on('hitupdate', this.handleHitUpdate);
  1689. hitDragging.emitter.on('dragend', this.handleDragEnd);
  1690. }
  1691. destroy() {
  1692. this.dragging.destroy();
  1693. }
  1694. querySegEl(ev) {
  1695. return internal_cjs.elementClosest(ev.subjectEl, '.fc-event');
  1696. }
  1697. }
  1698. function computeMutation(hit0, hit1, isFromStart, instanceRange) {
  1699. let dateEnv = hit0.context.dateEnv;
  1700. let date0 = hit0.dateSpan.range.start;
  1701. let date1 = hit1.dateSpan.range.start;
  1702. let delta = internal_cjs.diffDates(date0, date1, dateEnv, hit0.largeUnit);
  1703. if (isFromStart) {
  1704. if (dateEnv.add(instanceRange.start, delta) < instanceRange.end) {
  1705. return { startDelta: delta };
  1706. }
  1707. }
  1708. else if (dateEnv.add(instanceRange.end, delta) > instanceRange.start) {
  1709. return { endDelta: delta };
  1710. }
  1711. return null;
  1712. }
  1713. class UnselectAuto {
  1714. constructor(context) {
  1715. this.context = context;
  1716. this.isRecentPointerDateSelect = false; // wish we could use a selector to detect date selection, but uses hit system
  1717. this.matchesCancel = false;
  1718. this.matchesEvent = false;
  1719. this.onSelect = (selectInfo) => {
  1720. if (selectInfo.jsEvent) {
  1721. this.isRecentPointerDateSelect = true;
  1722. }
  1723. };
  1724. this.onDocumentPointerDown = (pev) => {
  1725. let unselectCancel = this.context.options.unselectCancel;
  1726. let downEl = internal_cjs.getEventTargetViaRoot(pev.origEvent);
  1727. this.matchesCancel = !!internal_cjs.elementClosest(downEl, unselectCancel);
  1728. this.matchesEvent = !!internal_cjs.elementClosest(downEl, EventDragging.SELECTOR); // interaction started on an event?
  1729. };
  1730. this.onDocumentPointerUp = (pev) => {
  1731. let { context } = this;
  1732. let { documentPointer } = this;
  1733. let calendarState = context.getCurrentData();
  1734. // touch-scrolling should never unfocus any type of selection
  1735. if (!documentPointer.wasTouchScroll) {
  1736. if (calendarState.dateSelection && // an existing date selection?
  1737. !this.isRecentPointerDateSelect // a new pointer-initiated date selection since last onDocumentPointerUp?
  1738. ) {
  1739. let unselectAuto = context.options.unselectAuto;
  1740. if (unselectAuto && (!unselectAuto || !this.matchesCancel)) {
  1741. context.calendarApi.unselect(pev);
  1742. }
  1743. }
  1744. if (calendarState.eventSelection && // an existing event selected?
  1745. !this.matchesEvent // interaction DIDN'T start on an event
  1746. ) {
  1747. context.dispatch({ type: 'UNSELECT_EVENT' });
  1748. }
  1749. }
  1750. this.isRecentPointerDateSelect = false;
  1751. };
  1752. let documentPointer = this.documentPointer = new PointerDragging(document);
  1753. documentPointer.shouldIgnoreMove = true;
  1754. documentPointer.shouldWatchScroll = false;
  1755. documentPointer.emitter.on('pointerdown', this.onDocumentPointerDown);
  1756. documentPointer.emitter.on('pointerup', this.onDocumentPointerUp);
  1757. /*
  1758. TODO: better way to know about whether there was a selection with the pointer
  1759. */
  1760. context.emitter.on('select', this.onSelect);
  1761. }
  1762. destroy() {
  1763. this.context.emitter.off('select', this.onSelect);
  1764. this.documentPointer.destroy();
  1765. }
  1766. }
  1767. const OPTION_REFINERS = {
  1768. fixedMirrorParent: internal_cjs.identity,
  1769. };
  1770. const LISTENER_REFINERS = {
  1771. dateClick: internal_cjs.identity,
  1772. eventDragStart: internal_cjs.identity,
  1773. eventDragStop: internal_cjs.identity,
  1774. eventDrop: internal_cjs.identity,
  1775. eventResizeStart: internal_cjs.identity,
  1776. eventResizeStop: internal_cjs.identity,
  1777. eventResize: internal_cjs.identity,
  1778. drop: internal_cjs.identity,
  1779. eventReceive: internal_cjs.identity,
  1780. eventLeave: internal_cjs.identity,
  1781. };
  1782. /*
  1783. Given an already instantiated draggable object for one-or-more elements,
  1784. Interprets any dragging as an attempt to drag an events that lives outside
  1785. of a calendar onto a calendar.
  1786. */
  1787. class ExternalElementDragging {
  1788. constructor(dragging, suppliedDragMeta) {
  1789. this.receivingContext = null;
  1790. this.droppableEvent = null; // will exist for all drags, even if create:false
  1791. this.suppliedDragMeta = null;
  1792. this.dragMeta = null;
  1793. this.handleDragStart = (ev) => {
  1794. this.dragMeta = this.buildDragMeta(ev.subjectEl);
  1795. };
  1796. this.handleHitUpdate = (hit, isFinal, ev) => {
  1797. let { dragging } = this.hitDragging;
  1798. let receivingContext = null;
  1799. let droppableEvent = null;
  1800. let isInvalid = false;
  1801. let interaction = {
  1802. affectedEvents: internal_cjs.createEmptyEventStore(),
  1803. mutatedEvents: internal_cjs.createEmptyEventStore(),
  1804. isEvent: this.dragMeta.create,
  1805. };
  1806. if (hit) {
  1807. receivingContext = hit.context;
  1808. if (this.canDropElOnCalendar(ev.subjectEl, receivingContext)) {
  1809. droppableEvent = computeEventForDateSpan(hit.dateSpan, this.dragMeta, receivingContext);
  1810. interaction.mutatedEvents = internal_cjs.eventTupleToStore(droppableEvent);
  1811. isInvalid = !internal_cjs.isInteractionValid(interaction, hit.dateProfile, receivingContext);
  1812. if (isInvalid) {
  1813. interaction.mutatedEvents = internal_cjs.createEmptyEventStore();
  1814. droppableEvent = null;
  1815. }
  1816. }
  1817. }
  1818. this.displayDrag(receivingContext, interaction);
  1819. // show mirror if no already-rendered mirror element OR if we are shutting down the mirror (?)
  1820. // TODO: wish we could somehow wait for dispatch to guarantee render
  1821. dragging.setMirrorIsVisible(isFinal || !droppableEvent || !document.querySelector('.fc-event-mirror'));
  1822. if (!isInvalid) {
  1823. internal_cjs.enableCursor();
  1824. }
  1825. else {
  1826. internal_cjs.disableCursor();
  1827. }
  1828. if (!isFinal) {
  1829. dragging.setMirrorNeedsRevert(!droppableEvent);
  1830. this.receivingContext = receivingContext;
  1831. this.droppableEvent = droppableEvent;
  1832. }
  1833. };
  1834. this.handleDragEnd = (pev) => {
  1835. let { receivingContext, droppableEvent } = this;
  1836. this.clearDrag();
  1837. if (receivingContext && droppableEvent) {
  1838. let finalHit = this.hitDragging.finalHit;
  1839. let finalView = finalHit.context.viewApi;
  1840. let dragMeta = this.dragMeta;
  1841. receivingContext.emitter.trigger('drop', Object.assign(Object.assign({}, buildDatePointApiWithContext(finalHit.dateSpan, receivingContext)), { draggedEl: pev.subjectEl, jsEvent: pev.origEvent, view: finalView }));
  1842. if (dragMeta.create) {
  1843. let addingEvents = internal_cjs.eventTupleToStore(droppableEvent);
  1844. receivingContext.dispatch({
  1845. type: 'MERGE_EVENTS',
  1846. eventStore: addingEvents,
  1847. });
  1848. if (pev.isTouch) {
  1849. receivingContext.dispatch({
  1850. type: 'SELECT_EVENT',
  1851. eventInstanceId: droppableEvent.instance.instanceId,
  1852. });
  1853. }
  1854. // signal that an external event landed
  1855. receivingContext.emitter.trigger('eventReceive', {
  1856. event: new internal_cjs.EventImpl(receivingContext, droppableEvent.def, droppableEvent.instance),
  1857. relatedEvents: [],
  1858. revert() {
  1859. receivingContext.dispatch({
  1860. type: 'REMOVE_EVENTS',
  1861. eventStore: addingEvents,
  1862. });
  1863. },
  1864. draggedEl: pev.subjectEl,
  1865. view: finalView,
  1866. });
  1867. }
  1868. }
  1869. this.receivingContext = null;
  1870. this.droppableEvent = null;
  1871. };
  1872. let hitDragging = this.hitDragging = new HitDragging(dragging, internal_cjs.interactionSettingsStore);
  1873. hitDragging.requireInitial = false; // will start outside of a component
  1874. hitDragging.emitter.on('dragstart', this.handleDragStart);
  1875. hitDragging.emitter.on('hitupdate', this.handleHitUpdate);
  1876. hitDragging.emitter.on('dragend', this.handleDragEnd);
  1877. this.suppliedDragMeta = suppliedDragMeta;
  1878. }
  1879. buildDragMeta(subjectEl) {
  1880. if (typeof this.suppliedDragMeta === 'object') {
  1881. return internal_cjs.parseDragMeta(this.suppliedDragMeta);
  1882. }
  1883. if (typeof this.suppliedDragMeta === 'function') {
  1884. return internal_cjs.parseDragMeta(this.suppliedDragMeta(subjectEl));
  1885. }
  1886. return getDragMetaFromEl(subjectEl);
  1887. }
  1888. displayDrag(nextContext, state) {
  1889. let prevContext = this.receivingContext;
  1890. if (prevContext && prevContext !== nextContext) {
  1891. prevContext.dispatch({ type: 'UNSET_EVENT_DRAG' });
  1892. }
  1893. if (nextContext) {
  1894. nextContext.dispatch({ type: 'SET_EVENT_DRAG', state });
  1895. }
  1896. }
  1897. clearDrag() {
  1898. if (this.receivingContext) {
  1899. this.receivingContext.dispatch({ type: 'UNSET_EVENT_DRAG' });
  1900. }
  1901. }
  1902. canDropElOnCalendar(el, receivingContext) {
  1903. let dropAccept = receivingContext.options.dropAccept;
  1904. if (typeof dropAccept === 'function') {
  1905. return dropAccept.call(receivingContext.calendarApi, el);
  1906. }
  1907. if (typeof dropAccept === 'string' && dropAccept) {
  1908. return Boolean(internal_cjs.elementMatches(el, dropAccept));
  1909. }
  1910. return true;
  1911. }
  1912. }
  1913. // Utils for computing event store from the DragMeta
  1914. // ----------------------------------------------------------------------------------------------------
  1915. function computeEventForDateSpan(dateSpan, dragMeta, context) {
  1916. let defProps = Object.assign({}, dragMeta.leftoverProps);
  1917. for (let transform of context.pluginHooks.externalDefTransforms) {
  1918. Object.assign(defProps, transform(dateSpan, dragMeta));
  1919. }
  1920. let { refined, extra } = internal_cjs.refineEventDef(defProps, context);
  1921. let def = internal_cjs.parseEventDef(refined, extra, dragMeta.sourceId, dateSpan.allDay, context.options.forceEventDuration || Boolean(dragMeta.duration), // hasEnd
  1922. context);
  1923. let start = dateSpan.range.start;
  1924. // only rely on time info if drop zone is all-day,
  1925. // otherwise, we already know the time
  1926. if (dateSpan.allDay && dragMeta.startTime) {
  1927. start = context.dateEnv.add(start, dragMeta.startTime);
  1928. }
  1929. let end = dragMeta.duration ?
  1930. context.dateEnv.add(start, dragMeta.duration) :
  1931. internal_cjs.getDefaultEventEnd(dateSpan.allDay, start, context);
  1932. let instance = internal_cjs.createEventInstance(def.defId, { start, end });
  1933. return { def, instance };
  1934. }
  1935. // Utils for extracting data from element
  1936. // ----------------------------------------------------------------------------------------------------
  1937. function getDragMetaFromEl(el) {
  1938. let str = getEmbeddedElData(el, 'event');
  1939. let obj = str ?
  1940. JSON.parse(str) :
  1941. { create: false }; // if no embedded data, assume no event creation
  1942. return internal_cjs.parseDragMeta(obj);
  1943. }
  1944. internal_cjs.config.dataAttrPrefix = '';
  1945. function getEmbeddedElData(el, name) {
  1946. let prefix = internal_cjs.config.dataAttrPrefix;
  1947. let prefixedName = (prefix ? prefix + '-' : '') + name;
  1948. return el.getAttribute('data-' + prefixedName) || '';
  1949. }
  1950. /*
  1951. Makes an element (that is *external* to any calendar) draggable.
  1952. Can pass in data that determines how an event will be created when dropped onto a calendar.
  1953. Leverages FullCalendar's internal drag-n-drop functionality WITHOUT a third-party drag system.
  1954. */
  1955. class ExternalDraggable {
  1956. constructor(el, settings = {}) {
  1957. this.handlePointerDown = (ev) => {
  1958. let { dragging } = this;
  1959. let { minDistance, longPressDelay } = this.settings;
  1960. dragging.minDistance =
  1961. minDistance != null ?
  1962. minDistance :
  1963. (ev.isTouch ? 0 : internal_cjs.BASE_OPTION_DEFAULTS.eventDragMinDistance);
  1964. dragging.delay =
  1965. ev.isTouch ? // TODO: eventually read eventLongPressDelay instead vvv
  1966. (longPressDelay != null ? longPressDelay : internal_cjs.BASE_OPTION_DEFAULTS.longPressDelay) :
  1967. 0;
  1968. };
  1969. this.handleDragStart = (ev) => {
  1970. if (ev.isTouch &&
  1971. this.dragging.delay &&
  1972. ev.subjectEl.classList.contains('fc-event')) {
  1973. this.dragging.mirror.getMirrorEl().classList.add('fc-event-selected');
  1974. }
  1975. };
  1976. this.settings = settings;
  1977. let dragging = this.dragging = new FeaturefulElementDragging(el);
  1978. dragging.touchScrollAllowed = false;
  1979. if (settings.itemSelector != null) {
  1980. dragging.pointer.selector = settings.itemSelector;
  1981. }
  1982. if (settings.appendTo != null) {
  1983. dragging.mirror.parentNode = settings.appendTo; // TODO: write tests
  1984. }
  1985. dragging.emitter.on('pointerdown', this.handlePointerDown);
  1986. dragging.emitter.on('dragstart', this.handleDragStart);
  1987. new ExternalElementDragging(dragging, settings.eventData); // eslint-disable-line no-new
  1988. }
  1989. destroy() {
  1990. this.dragging.destroy();
  1991. }
  1992. }
  1993. /*
  1994. Detects when a *THIRD-PARTY* drag-n-drop system interacts with elements.
  1995. The third-party system is responsible for drawing the visuals effects of the drag.
  1996. This class simply monitors for pointer movements and fires events.
  1997. It also has the ability to hide the moving element (the "mirror") during the drag.
  1998. */
  1999. class InferredElementDragging extends internal_cjs.ElementDragging {
  2000. constructor(containerEl) {
  2001. super(containerEl);
  2002. this.shouldIgnoreMove = false;
  2003. this.mirrorSelector = '';
  2004. this.currentMirrorEl = null;
  2005. this.handlePointerDown = (ev) => {
  2006. this.emitter.trigger('pointerdown', ev);
  2007. if (!this.shouldIgnoreMove) {
  2008. // fire dragstart right away. does not support delay or min-distance
  2009. this.emitter.trigger('dragstart', ev);
  2010. }
  2011. };
  2012. this.handlePointerMove = (ev) => {
  2013. if (!this.shouldIgnoreMove) {
  2014. this.emitter.trigger('dragmove', ev);
  2015. }
  2016. };
  2017. this.handlePointerUp = (ev) => {
  2018. this.emitter.trigger('pointerup', ev);
  2019. if (!this.shouldIgnoreMove) {
  2020. // fire dragend right away. does not support a revert animation
  2021. this.emitter.trigger('dragend', ev);
  2022. }
  2023. };
  2024. let pointer = this.pointer = new PointerDragging(containerEl);
  2025. pointer.emitter.on('pointerdown', this.handlePointerDown);
  2026. pointer.emitter.on('pointermove', this.handlePointerMove);
  2027. pointer.emitter.on('pointerup', this.handlePointerUp);
  2028. }
  2029. destroy() {
  2030. this.pointer.destroy();
  2031. }
  2032. setIgnoreMove(bool) {
  2033. this.shouldIgnoreMove = bool;
  2034. }
  2035. setMirrorIsVisible(bool) {
  2036. if (bool) {
  2037. // restore a previously hidden element.
  2038. // use the reference in case the selector class has already been removed.
  2039. if (this.currentMirrorEl) {
  2040. this.currentMirrorEl.style.visibility = '';
  2041. this.currentMirrorEl = null;
  2042. }
  2043. }
  2044. else {
  2045. let mirrorEl = this.mirrorSelector
  2046. // TODO: somehow query FullCalendars WITHIN shadow-roots
  2047. ? document.querySelector(this.mirrorSelector)
  2048. : null;
  2049. if (mirrorEl) {
  2050. this.currentMirrorEl = mirrorEl;
  2051. mirrorEl.style.visibility = 'hidden';
  2052. }
  2053. }
  2054. }
  2055. }
  2056. /*
  2057. Bridges third-party drag-n-drop systems with FullCalendar.
  2058. Must be instantiated and destroyed by caller.
  2059. */
  2060. class ThirdPartyDraggable {
  2061. constructor(containerOrSettings, settings) {
  2062. let containerEl = document;
  2063. if (
  2064. // wish we could just test instanceof EventTarget, but doesn't work in IE11
  2065. containerOrSettings === document ||
  2066. containerOrSettings instanceof Element) {
  2067. containerEl = containerOrSettings;
  2068. settings = settings || {};
  2069. }
  2070. else {
  2071. settings = (containerOrSettings || {});
  2072. }
  2073. let dragging = this.dragging = new InferredElementDragging(containerEl);
  2074. if (typeof settings.itemSelector === 'string') {
  2075. dragging.pointer.selector = settings.itemSelector;
  2076. }
  2077. else if (containerEl === document) {
  2078. dragging.pointer.selector = '[data-event]';
  2079. }
  2080. if (typeof settings.mirrorSelector === 'string') {
  2081. dragging.mirrorSelector = settings.mirrorSelector;
  2082. }
  2083. let externalDragging = new ExternalElementDragging(dragging, settings.eventData);
  2084. // The hit-detection system requires that the dnd-mirror-element be pointer-events:none,
  2085. // but this can't be guaranteed for third-party draggables, so disable
  2086. externalDragging.hitDragging.disablePointCheck = true;
  2087. }
  2088. destroy() {
  2089. this.dragging.destroy();
  2090. }
  2091. }
  2092. var index = index_cjs.createPlugin({
  2093. name: '@fullcalendar/interaction',
  2094. componentInteractions: [DateClicking, DateSelecting, EventDragging, EventResizing],
  2095. calendarInteractions: [UnselectAuto],
  2096. elementDraggingImpl: FeaturefulElementDragging,
  2097. optionRefiners: OPTION_REFINERS,
  2098. listenerRefiners: LISTENER_REFINERS,
  2099. });
  2100. exports.Draggable = ExternalDraggable;
  2101. exports.ThirdPartyDraggable = ThirdPartyDraggable;
  2102. exports["default"] = index;