DateComponent.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. var DateComponent = FC.DateComponent = Component.extend({
  2. uid: null,
  3. childrenByUid: null,
  4. isRTL: false, // frequently accessed options
  5. nextDayThreshold: null, // "
  6. eventRendererClass: null,
  7. helperRendererClass: null,
  8. businessHourRendererClass: null,
  9. fillRendererClass: null,
  10. eventRenderer: null,
  11. helperRenderer: null,
  12. businessHourRenderer: null,
  13. fillRenderer: null,
  14. hitsNeededDepth: 0, // necessary because multiple callers might need the same hits
  15. hasAllDayBusinessHours: false, // TODO: unify with largeUnit and isTimeScale?
  16. dateMessageAggregator: null,
  17. eventMessageAggregator: null,
  18. constructor: function() {
  19. Component.call(this);
  20. this.uid = String(DateComponent.guid++);
  21. this.childrenByUid = [];
  22. this.nextDayThreshold = moment.duration(this.opt('nextDayThreshold'));
  23. this.isRTL = this.opt('isRTL');
  24. if (this.fillRendererClass) {
  25. this.fillRenderer = new this.fillRendererClass(this);
  26. }
  27. if (this.eventRendererClass) { // fillRenderer is optional -----v
  28. this.eventRenderer = new this.eventRendererClass(this, this.fillRenderer);
  29. }
  30. if (this.helperRendererClass && this.eventRenderer) {
  31. this.helperRenderer = new this.helperRendererClass(this, this.eventRenderer);
  32. }
  33. if (this.businessHourRendererClass && this.fillRenderer) {
  34. this.businessHourRenderer = new this.businessHourRendererClass(this, this.fillRenderer);
  35. }
  36. this.dateMessageAggregator = buildMessageAggregator(this, 'dateRender', 'dateUnrender');
  37. this.eventMessageAggregator = buildMessageAggregator(this, 'eventRender', 'eventUnrender');
  38. },
  39. addChild: function(child) {
  40. if (!this.childrenByUid[child.uid]) {
  41. this.childrenByUid[child.uid] = child;
  42. this.dateMessageAggregator.addChild(child);
  43. this.eventMessageAggregator.addChild(child);
  44. }
  45. },
  46. removeChild: function(child) {
  47. if (this.childrenByUid[child.uid]) {
  48. delete this.childrenByUid[child.uid];
  49. this.dateMessageAggregator.removeChild(child);
  50. this.eventMessageAggregator.removeChild(child);
  51. }
  52. },
  53. removeChildren: function() { // all
  54. var children = Object.values(this.childrenByUid); // because childrenByUid will mutate while iterating
  55. var i;
  56. // aggregators can only do one at a time
  57. for (i = 0; i < children.length; i++) {
  58. this.removeChild(children[i]);
  59. }
  60. },
  61. requestRender: function(method, args, namespace, actionType) {
  62. var _this = this;
  63. this._getView().calendar.renderQueue.queue(function() {
  64. method.apply(_this, args);
  65. }, this.uid, namespace, actionType);
  66. },
  67. startBatchRender: function() {
  68. this._getView().calendar.startBatchRender();
  69. },
  70. stopBatchRender: function() {
  71. this._getView().calendar.stopBatchRender();
  72. },
  73. updateSize: function(totalHeight, isAuto, isResize) {
  74. this.callChildren('updateSize', arguments);
  75. },
  76. // Options
  77. // -----------------------------------------------------------------------------------------------------------------
  78. opt: function(name) {
  79. return this._getView().opt(name); // default implementation
  80. },
  81. publiclyTrigger: function(/**/) {
  82. var calendar = this._getCalendar();
  83. return calendar.publiclyTrigger.apply(calendar, arguments);
  84. },
  85. hasPublicHandlers: function(/**/) {
  86. var calendar = this._getCalendar();
  87. return calendar.hasPublicHandlers.apply(calendar, arguments);
  88. },
  89. // Date
  90. // -----------------------------------------------------------------------------------------------------------------
  91. handleDateProfileSet: function(dateProfile) {
  92. this.setDateProfileInChildren(dateProfile);
  93. },
  94. handleDateProfileUnset: function() {
  95. this.unsetDateProfileInChildren();
  96. },
  97. setDateProfileInChildren: function(dateProfile) {
  98. this.setInChildren('dateProfile', dateProfile);
  99. },
  100. unsetDateProfileInChildren: function() {
  101. this.unsetInChildren('dateProfile');
  102. },
  103. executeDateRender: function(dateProfile, skipScroll) { // wrapper
  104. this.renderDates(dateProfile);
  105. this.trigger('dateRender');
  106. },
  107. executeDateUnrender: function() { // wrapper
  108. this.trigger('before:dateUnrender');
  109. this.unrenderDates();
  110. },
  111. // date-cell content only
  112. renderDates: function(dateProfile) {
  113. // subclasses should implement
  114. },
  115. // date-cell content only
  116. unrenderDates: function() {
  117. // subclasses should override
  118. },
  119. // Now-Indicator
  120. // -----------------------------------------------------------------------------------------------------------------
  121. // Returns a string unit, like 'second' or 'minute' that defined how often the current time indicator
  122. // should be refreshed. If something falsy is returned, no time indicator is rendered at all.
  123. getNowIndicatorUnit: function() {
  124. // subclasses should implement
  125. },
  126. // Renders a current time indicator at the given datetime
  127. renderNowIndicator: function(date) {
  128. this.callChildren('renderNowIndicator', arguments);
  129. },
  130. // Undoes the rendering actions from renderNowIndicator
  131. unrenderNowIndicator: function() {
  132. this.callChildren('unrenderNowIndicator', arguments);
  133. },
  134. // Business Hours
  135. // ---------------------------------------------------------------------------------------------------------------
  136. setBusinessHoursInChildren: function(businessHours) {
  137. this.setInChildren('businessHours', businessHours);
  138. },
  139. unsetBusinessHoursInChildren: function() {
  140. this.unsetInChildren('businessHours');
  141. },
  142. // Renders business-hours onto the view. Assumes updateSize has already been called.
  143. renderBusinessHours: function(businessHourPayload) {
  144. var unzonedRange = this.get('dateProfile').activeUnzonedRange;
  145. var eventInstanceGroup = businessHourPayload[this.hasAllDayBusinessHours ? 'allDay' : 'timed'];
  146. var eventFootprints;
  147. if (eventInstanceGroup) {
  148. eventFootprints = this.eventRangesToEventFootprints(
  149. eventInstanceGroup.sliceRenderRanges(unzonedRange)
  150. );
  151. }
  152. else {
  153. eventFootprints = [];
  154. }
  155. this.renderBusinessHourEventFootprints(eventFootprints);
  156. },
  157. renderBusinessHourEventFootprints: function(eventFootprints) {
  158. if (this.businessHourRenderer) {
  159. this.businessHourRenderer.renderEventFootprints(eventFootprints);
  160. }
  161. },
  162. // Unrenders previously-rendered business-hours
  163. unrenderBusinessHours: function() {
  164. if (this.businessHourRenderer) {
  165. this.businessHourRenderer.unrender();
  166. }
  167. },
  168. // Events
  169. // -----------------------------------------------------------------------------------------------------------------
  170. // initial handling (to be called by initial data receipt)
  171. setEvents: function(eventsPayload) {
  172. this.set('currentEvents', eventsPayload);
  173. this.set('hasEvents', true);
  174. this.setEventsInChildren(eventsPayload);
  175. },
  176. unsetEvents: function() {
  177. this.unset('hasEvents');
  178. this.unset('currentEvents');
  179. this.unsetEventsInChildren();
  180. },
  181. // dynamic handling (to be called by post-binding updates)
  182. resetEvents: function(eventsPayload) {
  183. this.startBatchRender();
  184. this.unsetEvents();
  185. this.setEvents(eventsPayload);
  186. this.stopBatchRender();
  187. },
  188. addOrUpdateEvent: function(id, eventInstanceGroup) {
  189. var currentEvents = this.get('currentEvents');
  190. currentEvents[id] = eventInstanceGroup;
  191. this.set('currentEvents', currentEvents);
  192. this.addOrUpdateEventInChildren(id, eventInstanceGroup);
  193. if (this.has('displayingEvents')) {
  194. this.requestRender(this.renderEventAddOrUpdate, arguments, 'event', 'add');
  195. }
  196. },
  197. removeEvent: function(id) {
  198. var currentEvents = this.get('currentEvents');
  199. if (id in currentEvents) {
  200. delete currentEvents[id];
  201. this.set('currentEvents', currentEvents);
  202. this.removeEventInChildren(id);
  203. }
  204. if (this.has('displayingEvents')) {
  205. this.requestRender(this.renderEventRemove, arguments, 'event', 'remove');
  206. }
  207. },
  208. // for children
  209. setEventsInChildren: function(eventsPayload) {
  210. this.callChildren('setEvents', arguments);
  211. },
  212. unsetEventsInChildren: function() {
  213. this.callChildren('unsetEvents', arguments);
  214. },
  215. addOrUpdateEventInChildren: function(id, eventInstanceGroup) {
  216. this.callChildren('addOrUpdateEvent', arguments);
  217. },
  218. removeEventInChildren: function(id) {
  219. this.callChildren('removeEvent', arguments);
  220. },
  221. // rendering
  222. executeEventsRender: function(eventsPayload) { // wrapper
  223. this.renderEventsPayload(eventsPayload);
  224. this.trigger('eventRender');
  225. },
  226. executeEventsUnrender: function() { // wrapper
  227. this.trigger('before:eventUnrender');
  228. this.unrenderEvents();
  229. },
  230. // TODO: eventually rename to `renderEvents` once legacy is gone.
  231. renderEventsPayload: function(eventsPayload) {
  232. var dateProfile = this.get('dateProfile');
  233. var id, eventInstanceGroup;
  234. var eventRenderRanges;
  235. var eventFootprints;
  236. var bgFootprints = [];
  237. var fgFootprints = [];
  238. for (id in eventsPayload) {
  239. eventInstanceGroup = eventsPayload[id];
  240. eventRenderRanges = eventInstanceGroup.sliceRenderRanges(dateProfile.activeUnzonedRange);
  241. eventFootprints = this.eventRangesToEventFootprints(eventRenderRanges);
  242. if (eventInstanceGroup.getEventDef().hasBgRendering()) {
  243. bgFootprints.push.apply(bgFootprints, eventFootprints);
  244. }
  245. else {
  246. fgFootprints.push.apply(fgFootprints, eventFootprints);
  247. }
  248. }
  249. this.renderBgEventFootprints(bgFootprints);
  250. this.renderFgEventFootprints(fgFootprints);
  251. },
  252. // Unrenders all events currently rendered on the grid
  253. unrenderEvents: function() {
  254. this.unrenderFgEventFootprints();
  255. this.unrenderBgEventFootprints();
  256. // we DON'T need to call updateHeight() because
  257. // a renderEventsPayload() call always happens after this, which will eventually call updateHeight()
  258. },
  259. renderEventAddOrUpdate: function(id, eventInstanceGroup) {
  260. // by default, rerender all
  261. this.unrenderEvents();
  262. this.renderEventsPayload(this.get('currentEvents'));
  263. },
  264. renderEventRemove: function() {
  265. // by default, rerender all
  266. this.unrenderEvents();
  267. this.renderEventsPayload(this.get('currentEvents'));
  268. },
  269. // fg & bg delegation
  270. // NOTE: parents should never call these
  271. // TODO: make EventRenderer responsible for routing FG vs BG?
  272. renderFgEventFootprints: function(eventFootprints) {
  273. if (this.eventRenderer) {
  274. this.eventRenderer.renderFgFootprints(eventFootprints);
  275. }
  276. },
  277. renderBgEventFootprints: function(eventFootprints) {
  278. if (this.eventRenderer) {
  279. this.eventRenderer.renderBgFootprints(eventFootprints);
  280. }
  281. },
  282. // Removes event elements from the view.
  283. unrenderFgEventFootprints: function() {
  284. if (this.eventRenderer) {
  285. this.eventRenderer.unrenderFgFootprints();
  286. }
  287. },
  288. // Removes event elements from the view.
  289. unrenderBgEventFootprints: function() {
  290. if (this.eventRenderer) {
  291. this.eventRenderer.unrenderBgFootprints();
  292. }
  293. },
  294. // Retrieves all segment objects that are rendered in the view
  295. getEventSegs: function() {
  296. var segs = this.eventRenderer ? this.eventRenderer.getSegs() : [];
  297. var childrenByUid = this.childrenByUid;
  298. var uid;
  299. for (uid in childrenByUid) {
  300. segs.push.apply( // append
  301. segs,
  302. childrenByUid[uid].getEventSegs()
  303. );
  304. }
  305. return segs;
  306. },
  307. // Drag-n-Drop Rendering (for both events and external elements)
  308. // ---------------------------------------------------------------------------------------------------------------
  309. // Renders a visual indication of a event or external-element drag over the given drop zone.
  310. // If an external-element, seg will be `null`.
  311. // Must return elements used for any mock events.
  312. renderDrag: function(eventFootprints, seg, isTouch) {
  313. var childrenByUid = this.childrenByUid;
  314. var uid;
  315. var renderedHelper = false;
  316. for (uid in childrenByUid) {
  317. if (childrenByUid[uid].renderDrag(eventFootprints, seg, isTouch)) {
  318. renderedHelper = true;
  319. }
  320. }
  321. return renderedHelper;
  322. },
  323. // Unrenders a visual indication of an event or external-element being dragged.
  324. unrenderDrag: function() {
  325. this.callChildren('unrenderDrag', arguments);
  326. },
  327. // Event Resizing
  328. // ---------------------------------------------------------------------------------------------------------------
  329. // Renders a visual indication of an event being resized.
  330. renderEventResize: function(eventFootprints, seg, isTouch) {
  331. // subclasses must implement
  332. },
  333. // Unrenders a visual indication of an event being resized.
  334. unrenderEventResize: function() {
  335. // subclasses must implement
  336. },
  337. // Selection
  338. // ---------------------------------------------------------------------------------------------------------------
  339. // Renders a visual indication of the selection
  340. // TODO: rename to `renderSelection` after legacy is gone
  341. renderSelectionFootprint: function(componentFootprint) {
  342. this.renderHighlight(componentFootprint);
  343. this.callChildren('renderSelectionFootprint', arguments);
  344. },
  345. // Unrenders a visual indication of selection
  346. unrenderSelection: function() {
  347. this.unrenderHighlight();
  348. this.callChildren('unrenderSelection', arguments);
  349. },
  350. // Highlight
  351. // ---------------------------------------------------------------------------------------------------------------
  352. // Renders an emphasis on the given date range. Given a span (unzoned start/end and other misc data)
  353. renderHighlight: function(componentFootprint) {
  354. if (this.fillRenderer) {
  355. this.fillRenderer.renderFootprint(
  356. 'highlight',
  357. componentFootprint,
  358. {
  359. getClasses: function() {
  360. return [ 'fc-highlight' ];
  361. }
  362. }
  363. );
  364. }
  365. this.callChildren('renderHighlight', arguments);
  366. },
  367. // Unrenders the emphasis on a date range
  368. unrenderHighlight: function() {
  369. if (this.fillRenderer) {
  370. this.fillRenderer.unrender('highlight');
  371. }
  372. this.callChildren('unrenderHighlight', arguments);
  373. },
  374. // Hit Areas
  375. // ---------------------------------------------------------------------------------------------------------------
  376. // just because all DateComponents support this interface
  377. // doesn't mean they need to have their own internal coord system. they can defer to sub-components.
  378. hitsNeeded: function() {
  379. if (!(this.hitsNeededDepth++)) {
  380. this.prepareHits();
  381. }
  382. this.callChildren('hitsNeeded', arguments);
  383. },
  384. hitsNotNeeded: function() {
  385. if (this.hitsNeededDepth && !(--this.hitsNeededDepth)) {
  386. this.releaseHits();
  387. }
  388. this.callChildren('hitsNotNeeded', arguments);
  389. },
  390. prepareHits: function() {
  391. // subclasses can implement
  392. },
  393. releaseHits: function() {
  394. // subclasses can implement
  395. },
  396. // Given coordinates from the topleft of the document, return data about the date-related area underneath.
  397. // Can return an object with arbitrary properties (although top/right/left/bottom are encouraged).
  398. // Must have a `grid` property, a reference to this current grid. TODO: avoid this
  399. // The returned object will be processed by getHitFootprint and getHitEl.
  400. queryHit: function(leftOffset, topOffset) {
  401. var childrenByUid = this.childrenByUid;
  402. var uid;
  403. var hit;
  404. for (uid in childrenByUid) {
  405. hit = childrenByUid[uid].queryHit(leftOffset, topOffset);
  406. if (hit) {
  407. break;
  408. }
  409. }
  410. return hit;
  411. },
  412. getSafeHitFootprint: function(hit) {
  413. var dateProfile = this.get('dateProfile');
  414. var footprint = this.getHitFootprint(hit);
  415. if (!dateProfile.activeUnzonedRange.containsRange(footprint.unzonedRange)) {
  416. return null;
  417. }
  418. return footprint;
  419. },
  420. getHitFootprint: function(hit) {
  421. },
  422. // Given position-level information about a date-related area within the grid,
  423. // should return a jQuery element that best represents it. passed to dayClick callback.
  424. getHitEl: function(hit) {
  425. },
  426. /* Converting eventRange -> eventFootprint
  427. ------------------------------------------------------------------------------------------------------------------*/
  428. eventRangesToEventFootprints: function(eventRanges) {
  429. var eventFootprints = [];
  430. var i;
  431. for (i = 0; i < eventRanges.length; i++) {
  432. eventFootprints.push.apply(eventFootprints,
  433. this.eventRangeToEventFootprints(eventRanges[i])
  434. );
  435. }
  436. return eventFootprints;
  437. },
  438. // Given an event's unzoned date range, return an array of eventSpan objects.
  439. // eventSpan - { start, end, isStart, isEnd, otherthings... }
  440. // Subclasses can override.
  441. // Subclasses are obligated to forward eventRange.isStart/isEnd to the resulting spans.
  442. // TODO: somehow more DRY with Calendar::eventRangeToEventFootprints
  443. eventRangeToEventFootprints: function(eventRange) {
  444. return [
  445. new EventFootprint(
  446. new ComponentFootprint(
  447. eventRange.unzonedRange,
  448. eventRange.eventDef.isAllDay()
  449. ),
  450. eventRange.eventDef,
  451. eventRange.eventInstance // might not exist
  452. )
  453. ];
  454. },
  455. /* Converting componentFootprint/eventFootprint -> segs
  456. ------------------------------------------------------------------------------------------------------------------*/
  457. eventFootprintsToSegs: function(eventFootprints) {
  458. var segs = [];
  459. var i;
  460. for (i = 0; i < eventFootprints.length; i++) {
  461. segs.push.apply(segs,
  462. this.eventFootprintToSegs(eventFootprints[i])
  463. );
  464. }
  465. return segs;
  466. },
  467. // Given an event's span (unzoned start/end and other misc data), and the event itself,
  468. // slices into segments and attaches event-derived properties to them.
  469. // eventSpan - { start, end, isStart, isEnd, otherthings... }
  470. // constraintRange allow additional clipping. optional. eventually remove this.
  471. eventFootprintToSegs: function(eventFootprint, constraintRange) {
  472. var unzonedRange = eventFootprint.componentFootprint.unzonedRange;
  473. var segs;
  474. var i, seg;
  475. if (constraintRange) {
  476. unzonedRange = unzonedRange.intersect(constraintRange);
  477. }
  478. segs = this.componentFootprintToSegs(eventFootprint.componentFootprint);
  479. for (i = 0; i < segs.length; i++) {
  480. seg = segs[i];
  481. if (!unzonedRange.isStart) {
  482. seg.isStart = false;
  483. }
  484. if (!unzonedRange.isEnd) {
  485. seg.isEnd = false;
  486. }
  487. seg.footprint = eventFootprint;
  488. // TODO: rename to seg.eventFootprint
  489. }
  490. return segs;
  491. },
  492. componentFootprintToSegs: function(componentFootprint) {
  493. return [];
  494. },
  495. // Utils
  496. // ---------------------------------------------------------------------------------------------------------------
  497. callChildren: function(methodName, args) {
  498. var childrenByUid = this.childrenByUid;
  499. var uid;
  500. for (uid in childrenByUid) {
  501. childrenByUid[uid][methodName].apply(childrenByUid[uid], args);
  502. }
  503. },
  504. setInChildren: function(propName, propValue) {
  505. var childrenByUid = this.childrenByUid;
  506. var uid;
  507. for (uid in childrenByUid) {
  508. childrenByUid[uid].set(propName, propValue);
  509. }
  510. },
  511. unsetInChildren: function(propName) {
  512. var childrenByUid = this.childrenByUid;
  513. var uid;
  514. for (uid in childrenByUid) {
  515. childrenByUid[uid].unset(propName);
  516. }
  517. },
  518. _getCalendar: function() { // TODO: strip out. move to generic parent.
  519. return this.calendar || this.view.calendar;
  520. },
  521. _getView: function() { // TODO: strip out. move to generic parent.
  522. return this.view;
  523. }
  524. });
  525. DateComponent.guid = 0; // TODO: better system for this?
  526. DateComponent.watch('handleDateProfile', [ 'dateProfile' ], function(deps) {
  527. this.handleDateProfileSet(deps.dateProfile);
  528. }, function() {
  529. this.handleDateProfileUnset();
  530. });
  531. DateComponent.watch('businessHoursInChildren', [ 'businessHours' ], function(deps) {
  532. this.setBusinessHoursInChildren(deps.businessHours);
  533. }, function() {
  534. this.unsetBusinessHoursInChildren();
  535. });
  536. DateComponent.watch('displayingDates', [ 'dateProfile' ], function(deps) {
  537. this.requestRender(this.executeDateRender, [ deps.dateProfile ], 'date', 'init');
  538. }, function() {
  539. this.requestRender(this.executeDateUnrender, null, 'date', 'destroy');
  540. });
  541. DateComponent.watch('displayingBusinessHours', [ 'displayingDates', 'businessHours' ], function(deps) {
  542. this.requestRender(this.renderBusinessHours, [ deps.businessHours ], 'businessHours', 'init');
  543. }, function() {
  544. this.requestRender(this.unrenderBusinessHours, null, 'businessHours', 'destroy');
  545. });
  546. DateComponent.watch('displayingEvents', [ 'displayingDates', 'hasEvents' ], function() {
  547. // pass currentEvents in case there were event mutations after initialEvents
  548. this.requestRender(this.executeEventsRender, [ this.get('currentEvents') ], 'event', 'init');
  549. }, function() {
  550. this.requestRender(this.executeEventsUnrender, null, 'event', 'destroy');
  551. });