DateComponent.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. var DateComponent = FC.DateComponent = Component.extend({
  2. uid: null,
  3. childrenByUid: null,
  4. isRTL: false, // frequently accessed options
  5. nextDayThreshold: null, // "
  6. dateProfile: null, // hack
  7. eventRendererClass: null,
  8. helperRendererClass: null,
  9. businessHourRendererClass: null,
  10. fillRendererClass: null,
  11. eventRenderer: null,
  12. helperRenderer: null,
  13. businessHourRenderer: null,
  14. fillRenderer: null,
  15. hitsNeededDepth: 0, // necessary because multiple callers might need the same hits
  16. hasAllDayBusinessHours: false, // TODO: unify with largeUnit and isTimeScale?
  17. isDatesRendered: false,
  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. },
  37. addChild: function(child) {
  38. if (!this.childrenByUid[child.uid]) {
  39. this.childrenByUid[child.uid] = child;
  40. return true;
  41. }
  42. return false;
  43. },
  44. removeChild: function(child) {
  45. if (this.childrenByUid[child.uid]) {
  46. delete this.childrenByUid[child.uid];
  47. return true;
  48. }
  49. return false;
  50. },
  51. removeChildren: function() { // all
  52. var children = Object.values(this.childrenByUid); // because childrenByUid will mutate while iterating
  53. var i;
  54. for (i = 0; i < children.length; i++) {
  55. this.removeChild(children[i]);
  56. }
  57. },
  58. // TODO: only do if isInDom?
  59. // TODO: make part of Component, along with children/batch-render system?
  60. updateSize: function(totalHeight, isAuto, isResize) {
  61. this.callChildren('updateSize', arguments);
  62. },
  63. // Options
  64. // -----------------------------------------------------------------------------------------------------------------
  65. opt: function(name) {
  66. return this._getView().opt(name); // default implementation
  67. },
  68. publiclyTrigger: function(/**/) {
  69. var calendar = this._getCalendar();
  70. return calendar.publiclyTrigger.apply(calendar, arguments);
  71. },
  72. hasPublicHandlers: function(/**/) {
  73. var calendar = this._getCalendar();
  74. return calendar.hasPublicHandlers.apply(calendar, arguments);
  75. },
  76. // Date
  77. // -----------------------------------------------------------------------------------------------------------------
  78. executeDateRender: function(dateProfile) {
  79. this.dateProfile = dateProfile; // for rendering
  80. this.renderDates(dateProfile);
  81. this.isDatesRendered = true;
  82. this.callChildren('executeDateRender', arguments);
  83. },
  84. executeDateUnrender: function() { // wrapper
  85. this.callChildren('executeDateUnrender', arguments);
  86. this.dateProfile = null;
  87. this.unrenderDates();
  88. this.isDatesRendered = false;
  89. },
  90. // date-cell content only
  91. renderDates: function(dateProfile) {
  92. // subclasses should implement
  93. },
  94. // date-cell content only
  95. unrenderDates: function() {
  96. // subclasses should override
  97. },
  98. // Now-Indicator
  99. // -----------------------------------------------------------------------------------------------------------------
  100. // Returns a string unit, like 'second' or 'minute' that defined how often the current time indicator
  101. // should be refreshed. If something falsy is returned, no time indicator is rendered at all.
  102. getNowIndicatorUnit: function() {
  103. // subclasses should implement
  104. },
  105. // Renders a current time indicator at the given datetime
  106. renderNowIndicator: function(date) {
  107. this.callChildren('renderNowIndicator', arguments);
  108. },
  109. // Undoes the rendering actions from renderNowIndicator
  110. unrenderNowIndicator: function() {
  111. this.callChildren('unrenderNowIndicator', arguments);
  112. },
  113. // Business Hours
  114. // ---------------------------------------------------------------------------------------------------------------
  115. renderBusinessHours: function(businessHourGenerator) {
  116. if (this.businessHourRenderer) {
  117. this.businessHourRenderer.render(businessHourGenerator);
  118. }
  119. this.callChildren('renderBusinessHours', arguments);
  120. },
  121. // Unrenders previously-rendered business-hours
  122. unrenderBusinessHours: function() {
  123. this.callChildren('unrenderBusinessHours', arguments);
  124. if (this.businessHourRenderer) {
  125. this.businessHourRenderer.unrender();
  126. }
  127. },
  128. // Event Displaying
  129. // -----------------------------------------------------------------------------------------------------------------
  130. executeEventRender: function(eventsPayload) {
  131. if (this.eventRenderer) {
  132. this.eventRenderer.rangeUpdated(); // poorly named now
  133. this.eventRenderer.render(eventsPayload);
  134. }
  135. else if (this.renderEvents) { // legacy
  136. this.renderEvents(convertEventsPayloadToLegacyArray(eventsPayload));
  137. }
  138. this.callChildren('executeEventRender', arguments);
  139. },
  140. executeEventUnrender: function() {
  141. this.callChildren('executeEventUnrender', arguments);
  142. if (this.eventRenderer) {
  143. this.eventRenderer.unrender();
  144. }
  145. else if (this.destroyEvents) { // legacy
  146. this.destroyEvents();
  147. }
  148. },
  149. getBusinessHourSegs: function() { // recursive
  150. var segs = this.getOwnBusinessHourSegs();
  151. this.iterChildren(function(child) {
  152. segs.push.apply(segs, child.getBusinessHourSegs());
  153. });
  154. return segs;
  155. },
  156. getOwnBusinessHourSegs: function() {
  157. if (this.businessHourRenderer) {
  158. return this.businessHourRenderer.getSegs();
  159. }
  160. return [];
  161. },
  162. getEventSegs: function() { // recursive
  163. var segs = this.getOwnEventSegs();
  164. this.iterChildren(function(child) {
  165. segs.push.apply(segs, child.getEventSegs());
  166. });
  167. return segs;
  168. },
  169. getOwnEventSegs: function() { // just for itself
  170. if (this.eventRenderer) {
  171. return this.eventRenderer.getSegs();
  172. }
  173. return [];
  174. },
  175. // Event Rendering Triggering
  176. // -----------------------------------------------------------------------------------------------------------------
  177. triggerAfterEventsRendered: function() {
  178. this.triggerAfterEventSegsRendered(
  179. this.getEventSegs()
  180. );
  181. this.publiclyTrigger('eventAfterAllRender', {
  182. context: this,
  183. args: [ this ]
  184. });
  185. },
  186. triggerAfterEventSegsRendered: function(segs) {
  187. var _this = this;
  188. // an optimization, because getEventLegacy is expensive
  189. if (this.hasPublicHandlers('eventAfterRender')) {
  190. segs.forEach(function(seg) {
  191. var legacy;
  192. if (seg.el) { // necessary?
  193. legacy = seg.footprint.getEventLegacy();
  194. _this.publiclyTrigger('eventAfterRender', {
  195. context: legacy,
  196. args: [ legacy, seg.el, _this ]
  197. });
  198. }
  199. });
  200. }
  201. },
  202. triggerBeforeEventsDestroyed: function() {
  203. this.triggerBeforeEventSegsDestroyed(
  204. this.getEventSegs()
  205. );
  206. },
  207. triggerBeforeEventSegsDestroyed: function(segs) {
  208. var _this = this;
  209. if (this.hasPublicHandlers('eventDestroy')) {
  210. segs.forEach(function(seg) {
  211. var legacy;
  212. if (seg.el) { // necessary?
  213. legacy = seg.footprint.getEventLegacy();
  214. _this.publiclyTrigger('eventDestroy', {
  215. context: legacy,
  216. args: [ legacy, seg.el, _this ]
  217. });
  218. }
  219. });
  220. }
  221. },
  222. // Event Rendering Utils
  223. // -----------------------------------------------------------------------------------------------------------------
  224. // Hides all rendered event segments linked to the given event
  225. // RECURSIVE with subcomponents
  226. showEventsWithId: function(eventDefId) {
  227. this.getEventSegs().forEach(function(seg) {
  228. if (
  229. seg.footprint.eventDef.id === eventDefId &&
  230. seg.el // necessary?
  231. ) {
  232. seg.el.css('visibility', '');
  233. }
  234. });
  235. this.callChildren('showEventsWithId', arguments);
  236. },
  237. // Shows all rendered event segments linked to the given event
  238. // RECURSIVE with subcomponents
  239. hideEventsWithId: function(eventDefId) {
  240. this.getEventSegs().forEach(function(seg) {
  241. if (
  242. seg.footprint.eventDef.id === eventDefId &&
  243. seg.el // necessary?
  244. ) {
  245. seg.el.css('visibility', 'hidden');
  246. }
  247. });
  248. this.callChildren('hideEventsWithId', arguments);
  249. },
  250. // Drag-n-Drop Rendering (for both events and external elements)
  251. // ---------------------------------------------------------------------------------------------------------------
  252. // Renders a visual indication of a event or external-element drag over the given drop zone.
  253. // If an external-element, seg will be `null`.
  254. // Must return elements used for any mock events.
  255. renderDrag: function(eventFootprints, seg, isTouch) {
  256. var renderedHelper = false;
  257. this.iterChildren(function(child) {
  258. if (child.renderDrag(eventFootprints, seg, isTouch)) {
  259. renderedHelper = true;
  260. }
  261. });
  262. return renderedHelper;
  263. },
  264. // Unrenders a visual indication of an event or external-element being dragged.
  265. unrenderDrag: function() {
  266. this.callChildren('unrenderDrag', arguments);
  267. },
  268. // Event Resizing
  269. // ---------------------------------------------------------------------------------------------------------------
  270. // Renders a visual indication of an event being resized.
  271. renderEventResize: function(eventFootprints, seg, isTouch) {
  272. this.callChildren('renderEventResize', arguments);
  273. },
  274. // Unrenders a visual indication of an event being resized.
  275. unrenderEventResize: function() {
  276. this.callChildren('unrenderEventResize', arguments);
  277. },
  278. // Selection
  279. // ---------------------------------------------------------------------------------------------------------------
  280. // Renders a visual indication of the selection
  281. // TODO: rename to `renderSelection` after legacy is gone
  282. renderSelectionFootprint: function(componentFootprint) {
  283. this.renderHighlight(componentFootprint);
  284. this.callChildren('renderSelectionFootprint', arguments);
  285. },
  286. // Unrenders a visual indication of selection
  287. unrenderSelection: function() {
  288. this.unrenderHighlight();
  289. this.callChildren('unrenderSelection', arguments);
  290. },
  291. // Highlight
  292. // ---------------------------------------------------------------------------------------------------------------
  293. // Renders an emphasis on the given date range. Given a span (unzoned start/end and other misc data)
  294. renderHighlight: function(componentFootprint) {
  295. if (this.fillRenderer) {
  296. this.fillRenderer.renderFootprint(
  297. 'highlight',
  298. componentFootprint,
  299. {
  300. getClasses: function() {
  301. return [ 'fc-highlight' ];
  302. }
  303. }
  304. );
  305. }
  306. this.callChildren('renderHighlight', arguments);
  307. },
  308. // Unrenders the emphasis on a date range
  309. unrenderHighlight: function() {
  310. if (this.fillRenderer) {
  311. this.fillRenderer.unrender('highlight');
  312. }
  313. this.callChildren('unrenderHighlight', arguments);
  314. },
  315. // Hit Areas
  316. // ---------------------------------------------------------------------------------------------------------------
  317. // just because all DateComponents support this interface
  318. // doesn't mean they need to have their own internal coord system. they can defer to sub-components.
  319. hitsNeeded: function() {
  320. if (!(this.hitsNeededDepth++)) {
  321. this.prepareHits();
  322. }
  323. this.callChildren('hitsNeeded', arguments);
  324. },
  325. hitsNotNeeded: function() {
  326. if (this.hitsNeededDepth && !(--this.hitsNeededDepth)) {
  327. this.releaseHits();
  328. }
  329. this.callChildren('hitsNotNeeded', arguments);
  330. },
  331. prepareHits: function() {
  332. // subclasses can implement
  333. },
  334. releaseHits: function() {
  335. // subclasses can implement
  336. },
  337. // Given coordinates from the topleft of the document, return data about the date-related area underneath.
  338. // Can return an object with arbitrary properties (although top/right/left/bottom are encouraged).
  339. // Must have a `grid` property, a reference to this current grid. TODO: avoid this
  340. // The returned object will be processed by getHitFootprint and getHitEl.
  341. queryHit: function(leftOffset, topOffset) {
  342. var childrenByUid = this.childrenByUid;
  343. var uid;
  344. var hit;
  345. for (uid in childrenByUid) {
  346. hit = childrenByUid[uid].queryHit(leftOffset, topOffset);
  347. if (hit) {
  348. break;
  349. }
  350. }
  351. return hit;
  352. },
  353. getSafeHitFootprint: function(hit) {
  354. var footprint = this.getHitFootprint(hit);
  355. if (!this.dateProfile.activeUnzonedRange.containsRange(footprint.unzonedRange)) {
  356. return null;
  357. }
  358. return footprint;
  359. },
  360. getHitFootprint: function(hit) {
  361. },
  362. // Given position-level information about a date-related area within the grid,
  363. // should return a jQuery element that best represents it. passed to dayClick callback.
  364. getHitEl: function(hit) {
  365. },
  366. /* Converting eventRange -> eventFootprint
  367. ------------------------------------------------------------------------------------------------------------------*/
  368. eventRangesToEventFootprints: function(eventRanges) {
  369. var eventFootprints = [];
  370. var i;
  371. for (i = 0; i < eventRanges.length; i++) {
  372. eventFootprints.push.apply( // append
  373. eventFootprints,
  374. this.eventRangeToEventFootprints(eventRanges[i])
  375. );
  376. }
  377. return eventFootprints;
  378. },
  379. eventRangeToEventFootprints: function(eventRange) {
  380. return [ eventRangeToEventFootprint(eventRange) ];
  381. },
  382. /* Converting componentFootprint/eventFootprint -> segs
  383. ------------------------------------------------------------------------------------------------------------------*/
  384. eventFootprintsToSegs: function(eventFootprints) {
  385. var segs = [];
  386. var i;
  387. for (i = 0; i < eventFootprints.length; i++) {
  388. segs.push.apply(segs,
  389. this.eventFootprintToSegs(eventFootprints[i])
  390. );
  391. }
  392. return segs;
  393. },
  394. // Given an event's span (unzoned start/end and other misc data), and the event itself,
  395. // slices into segments and attaches event-derived properties to them.
  396. // eventSpan - { start, end, isStart, isEnd, otherthings... }
  397. // constraintRange allow additional clipping. optional. eventually remove this.
  398. eventFootprintToSegs: function(eventFootprint, constraintRange) {
  399. var unzonedRange = eventFootprint.componentFootprint.unzonedRange;
  400. var segs;
  401. var i, seg;
  402. if (constraintRange) {
  403. unzonedRange = unzonedRange.intersect(constraintRange);
  404. }
  405. segs = this.componentFootprintToSegs(eventFootprint.componentFootprint);
  406. for (i = 0; i < segs.length; i++) {
  407. seg = segs[i];
  408. if (!unzonedRange.isStart) {
  409. seg.isStart = false;
  410. }
  411. if (!unzonedRange.isEnd) {
  412. seg.isEnd = false;
  413. }
  414. seg.footprint = eventFootprint;
  415. // TODO: rename to seg.eventFootprint
  416. }
  417. return segs;
  418. },
  419. componentFootprintToSegs: function(componentFootprint) {
  420. return [];
  421. },
  422. // Utils
  423. // ---------------------------------------------------------------------------------------------------------------
  424. callChildren: function(methodName, args) {
  425. this.iterChildren(function(child) {
  426. child[methodName].apply(child, args);
  427. });
  428. },
  429. iterChildren: function(func) {
  430. var childrenByUid = this.childrenByUid;
  431. var uid;
  432. for (uid in childrenByUid) {
  433. func(childrenByUid[uid]);
  434. }
  435. },
  436. _getCalendar: function() { // TODO: strip out. move to generic parent.
  437. return this.calendar || this.view.calendar;
  438. },
  439. _getView: function() { // TODO: strip out. move to generic parent.
  440. return this.view;
  441. },
  442. _getDateProfile: function() {
  443. return this._getView().get('dateProfile');
  444. }
  445. });
  446. DateComponent.guid = 0; // TODO: better system for this?
  447. // legacy
  448. function convertEventsPayloadToLegacyArray(eventsPayload) {
  449. var eventDefId;
  450. var eventInstances;
  451. var legacyEvents = [];
  452. var i;
  453. for (eventDefId in eventsPayload) {
  454. eventInstances = eventsPayload[eventDefId].eventInstances;
  455. for (i = 0; i < eventInstances.length; i++) {
  456. legacyEvents.push(
  457. eventInstances[i].toLegacy()
  458. );
  459. }
  460. }
  461. return legacyEvents;
  462. }