| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273 |
- /* An abstract class from which other views inherit from
- ----------------------------------------------------------------------------------------------------------------------*/
- var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
- type: null, // subclass' view name (string)
- name: null, // deprecated. use `type` instead
- title: null, // the text that will be displayed in the header's title
- calendar: null, // owner Calendar object
- options: null, // hash containing all options. already merged with view-specific-options
- el: null, // the view's containing element. set by Calendar
- isDateSet: false,
- dateSetQueue: null,
- displayingEvents: null, // a promise
- isEventsSet: false,
- isEventsBounds: false,
- eventRenderQueue: null,
- // range the view is actually displaying (moments)
- start: null,
- end: null, // exclusive
- // range the view is formally responsible for (moments)
- // may be different from start/end. for example, a month view might have 1st-31st, excluding padded dates
- intervalStart: null,
- intervalEnd: null, // exclusive
- intervalDuration: null,
- intervalUnit: null, // name of largest unit being displayed, like "month" or "week"
- isRTL: false,
- isSelected: false, // boolean whether a range of time is user-selected or not
- selectedEvent: null,
- eventOrderSpecs: null, // criteria for ordering events when they have same date/time
- // classNames styled by jqui themes
- widgetHeaderClass: null,
- widgetContentClass: null,
- highlightStateClass: null,
- // for date utils, computed from options
- nextDayThreshold: null,
- isHiddenDayHash: null,
- // now indicator
- isNowIndicatorRendered: null,
- initialNowDate: null, // result first getNow call
- initialNowQueriedMs: null, // ms time the getNow was called
- nowIndicatorTimeoutID: null, // for refresh timing of now indicator
- nowIndicatorIntervalID: null, // "
- constructor: function(calendar, type, options, intervalDuration) {
- this.calendar = calendar;
- this.type = this.name = type; // .name is deprecated
- this.options = options;
- this.intervalDuration = intervalDuration || moment.duration(1, 'day');
- this.nextDayThreshold = moment.duration(this.opt('nextDayThreshold'));
- this.initThemingProps();
- this.initHiddenDays();
- this.isRTL = this.opt('isRTL');
- this.eventOrderSpecs = parseFieldSpecs(this.opt('eventOrder'));
- this.dateSetQueue = new RunQueue();
- this.eventRenderQueue = new RunQueue();
- this.initialize();
- },
- // A good place for subclasses to initialize member variables
- initialize: function() {
- // subclasses can implement
- },
- // Retrieves an option with the given name
- opt: function(name) {
- return this.options[name];
- },
- // Triggers handlers that are view-related. Modifies args before passing to calendar.
- trigger: function(name, thisObj) { // arguments beyond thisObj are passed along
- var calendar = this.calendar;
- return calendar.trigger.apply(
- calendar,
- [name, thisObj || this].concat(
- Array.prototype.slice.call(arguments, 2), // arguments beyond thisObj
- [ this ] // always make the last argument a reference to the view. TODO: deprecate
- )
- );
- },
- /* Date Computation
- ------------------------------------------------------------------------------------------------------------------*/
- // Updates all internal dates for displaying the given unzoned range.
- setRange: function(range) {
- $.extend(this, range); // assigns every property to this object's member variables
- this.updateTitle();
- },
- // Given a single current unzoned date, produce information about what range to display.
- // Subclasses can override. Must return all properties.
- computeRange: function(date) {
- var intervalUnit = computeIntervalUnit(this.intervalDuration);
- var intervalStart = date.clone().startOf(intervalUnit);
- var intervalEnd = intervalStart.clone().add(this.intervalDuration);
- var start, end;
- // normalize the range's time-ambiguity
- if (/year|month|week|day/.test(intervalUnit)) { // whole-days?
- intervalStart.stripTime();
- intervalEnd.stripTime();
- }
- else { // needs to have a time?
- if (!intervalStart.hasTime()) {
- intervalStart = this.calendar.time(0); // give 00:00 time
- }
- if (!intervalEnd.hasTime()) {
- intervalEnd = this.calendar.time(0); // give 00:00 time
- }
- }
- start = intervalStart.clone();
- start = this.skipHiddenDays(start);
- end = intervalEnd.clone();
- end = this.skipHiddenDays(end, -1, true); // exclusively move backwards
- return {
- intervalUnit: intervalUnit,
- intervalStart: intervalStart,
- intervalEnd: intervalEnd,
- start: start,
- end: end
- };
- },
- // Computes the new date when the user hits the prev button, given the current date
- computePrevDate: function(date) {
- return this.massageCurrentDate(
- date.clone().startOf(this.intervalUnit).subtract(this.intervalDuration), -1
- );
- },
- // Computes the new date when the user hits the next button, given the current date
- computeNextDate: function(date) {
- return this.massageCurrentDate(
- date.clone().startOf(this.intervalUnit).add(this.intervalDuration)
- );
- },
- // Given an arbitrarily calculated current date of the calendar, returns a date that is ensured to be completely
- // visible. `direction` is optional and indicates which direction the current date was being
- // incremented or decremented (1 or -1).
- massageCurrentDate: function(date, direction) {
- if (this.intervalDuration.as('days') <= 1) { // if the view displays a single day or smaller
- if (this.isHiddenDay(date)) {
- date = this.skipHiddenDays(date, direction);
- date.startOf('day');
- }
- }
- return date;
- },
- /* Title and Date Formatting
- ------------------------------------------------------------------------------------------------------------------*/
- // Sets the view's title property to the most updated computed value
- updateTitle: function() {
- this.title = this.computeTitle();
- this.calendar.setToolbarsTitle(this.title);
- },
- // Computes what the title at the top of the calendar should be for this view
- computeTitle: function() {
- return this.formatRange(
- {
- // in case intervalStart/End has a time, make sure timezone is correct
- start: this.calendar.applyTimezone(this.intervalStart),
- end: this.calendar.applyTimezone(this.intervalEnd)
- },
- this.opt('titleFormat') || this.computeTitleFormat(),
- this.opt('titleRangeSeparator')
- );
- },
- // Generates the format string that should be used to generate the title for the current date range.
- // Attempts to compute the most appropriate format if not explicitly specified with `titleFormat`.
- computeTitleFormat: function() {
- if (this.intervalUnit == 'year') {
- return 'YYYY';
- }
- else if (this.intervalUnit == 'month') {
- return this.opt('monthYearFormat'); // like "September 2014"
- }
- else if (this.intervalDuration.as('days') > 1) {
- return 'll'; // multi-day range. shorter, like "Sep 9 - 10 2014"
- }
- else {
- return 'LL'; // one day. longer, like "September 9 2014"
- }
- },
- // Utility for formatting a range. Accepts a range object, formatting string, and optional separator.
- // Displays all-day ranges naturally, with an inclusive end. Takes the current isRTL into account.
- // The timezones of the dates within `range` will be respected.
- formatRange: function(range, formatStr, separator) {
- var end = range.end;
- if (!end.hasTime()) { // all-day?
- end = end.clone().subtract(1); // convert to inclusive. last ms of previous day
- }
- return formatRange(range.start, end, formatStr, separator, this.opt('isRTL'));
- },
- getAllDayHtml: function() {
- return this.opt('allDayHtml') || htmlEscape(this.opt('allDayText'));
- },
- /* Navigation
- ------------------------------------------------------------------------------------------------------------------*/
- // Generates HTML for an anchor to another view into the calendar.
- // Will either generate an <a> tag or a non-clickable <span> tag, depending on enabled settings.
- // `gotoOptions` can either be a moment input, or an object with the form:
- // { date, type, forceOff }
- // `type` is a view-type like "day" or "week". default value is "day".
- // `attrs` and `innerHtml` are use to generate the rest of the HTML tag.
- buildGotoAnchorHtml: function(gotoOptions, attrs, innerHtml) {
- var date, type, forceOff;
- var finalOptions;
- if ($.isPlainObject(gotoOptions)) {
- date = gotoOptions.date;
- type = gotoOptions.type;
- forceOff = gotoOptions.forceOff;
- }
- else {
- date = gotoOptions; // a single moment input
- }
- date = FC.moment(date); // if a string, parse it
- finalOptions = { // for serialization into the link
- date: date.format('YYYY-MM-DD'),
- type: type || 'day'
- };
- if (typeof attrs === 'string') {
- innerHtml = attrs;
- attrs = null;
- }
- attrs = attrs ? ' ' + attrsToStr(attrs) : ''; // will have a leading space
- innerHtml = innerHtml || '';
- if (!forceOff && this.opt('navLinks')) {
- return '<a' + attrs +
- ' data-goto="' + htmlEscape(JSON.stringify(finalOptions)) + '">' +
- innerHtml +
- '</a>';
- }
- else {
- return '<span' + attrs + '>' +
- innerHtml +
- '</span>';
- }
- },
- /* Rendering
- ------------------------------------------------------------------------------------------------------------------*/
- // Non-date-related content, like the view's skeleton
- // --------------------------------------------------
- // Sets the container element that the view should render inside of, does global DOM-related initializations,
- // and renders all the non-date-related content inside.
- setElement: function(el) {
- this.el = el;
- this.bindGlobalHandlers();
- this.renderSkeleton();
- },
- // Removes the view's container element from the DOM, clearing any content beforehand.
- // Undoes any other DOM-related attachments.
- removeElement: function() {
- this.unbindGlobalHandlers();
- this.unsetDate();
- this.unrenderSkeleton();
- this.el.remove();
- // NOTE: don't null-out this.el in case the View was destroyed within an API callback.
- // We don't null-out the View's other jQuery element references upon destroy,
- // so we shouldn't kill this.el either.
- },
- // Renders the basic structure of the view before any content is rendered
- renderSkeleton: function() {
- // subclasses should implement
- },
- // Unrenders the basic structure of the view
- unrenderSkeleton: function() {
- // subclasses should implement
- },
- // Date-related-content, like date cells, date indicators, and events
- // ------------------------------------------------------------------
- // Renders ALL date related content, including events. Guaranteed to redraw content.
- // async
- setDate: function(date, forcedScroll) {
- var _this = this;
- // do this before unsetDate, which is destructive
- this.captureScroll();
- this.calendar.freezeContentHeight();
- this.unsetDate();
- this.isDateSet = true;
- return this.dateSetQueue.push(function() {
- _this.setRange(_this.computeRange(date));
- _this.displayDateVisuals();
- _this.calendar.unfreezeContentHeight();
- _this.releaseScroll(true, forcedScroll); // isInitial=true
- _this.triggerDateVisualsRendered();
- }).then(function() {
- return _this.displayEvents();
- }, function() {
- // failure. TODO: implement in RunQueue
- _this.calendar.unfreezeContentHeight();
- _this.discardScroll();
- });
- },
- // sync
- unsetDate: function() {
- if (this.isDateSet) {
- this.isDateSet = false; // important to do first
- this.dateSetQueue.clear();
- this.stopDisplayingEvents();
- this.stopDisplayingDateVisuals();
- }
- },
- // sync
- displayDateVisuals: function() {
- this.stopDisplayingDateVisuals();
- this.isDisplayingDateVisuals = true;
- if (this.render) {
- this.render(); // TODO: deprecate
- }
- this.renderDates();
- this.updateSize();
- this.renderBusinessHours(); // might need coordinates, so should go after updateSize()
- this.startNowIndicator();
- },
- // sync
- stopDisplayingDateVisuals: function() {
- if (this.isDisplayingDateVisuals) {
- this.isDisplayingDateVisuals = false;
- this.unselect();
- this.stopNowIndicator();
- this.triggerUnrender();
- this.unrenderBusinessHours();
- this.unrenderDates();
- if (this.destroy) {
- this.destroy(); // TODO: deprecate
- }
- }
- },
- // Renders the view's date-related content.
- // Assumes setRange has already been called and the skeleton has already been rendered.
- renderDates: function() {
- // subclasses should implement
- },
- // Unrenders the view's date-related content
- unrenderDates: function() {
- // subclasses should override
- },
- // Misc rendering utils
- // --------------------
- // Can be extended to rely on other things
- triggerDateVisualsRendered: function() {
- this.triggerRender();
- },
- // Signals that the view's content has been rendered
- triggerRender: function() {
- this.trigger('viewRender', this, this, this.el);
- },
- // Signals that the view's content is about to be unrendered
- triggerUnrender: function() {
- this.trigger('viewDestroy', this, this, this.el);
- },
- // Binds DOM handlers to elements that reside outside the view container, such as the document
- bindGlobalHandlers: function() {
- this.listenTo($(document), 'mousedown', this.handleDocumentMousedown);
- this.listenTo($(document), 'touchstart', this.processUnselect);
- },
- // Unbinds DOM handlers from elements that reside outside the view container
- unbindGlobalHandlers: function() {
- this.stopListeningTo($(document));
- },
- // Initializes internal variables related to theming
- initThemingProps: function() {
- var tm = this.opt('theme') ? 'ui' : 'fc';
- this.widgetHeaderClass = tm + '-widget-header';
- this.widgetContentClass = tm + '-widget-content';
- this.highlightStateClass = tm + '-state-highlight';
- },
- /* Business Hours
- ------------------------------------------------------------------------------------------------------------------*/
- // Renders business-hours onto the view. Assumes updateSize has already been called.
- renderBusinessHours: function() {
- // subclasses should implement
- },
- // Unrenders previously-rendered business-hours
- unrenderBusinessHours: function() {
- // subclasses should implement
- },
- /* Now Indicator
- ------------------------------------------------------------------------------------------------------------------*/
- // Immediately render the current time indicator and begins re-rendering it at an interval,
- // which is defined by this.getNowIndicatorUnit().
- // TODO: somehow do this for the current whole day's background too
- startNowIndicator: function() {
- var _this = this;
- var unit;
- var update;
- var delay; // ms wait value
- if (this.opt('nowIndicator')) {
- unit = this.getNowIndicatorUnit();
- if (unit) {
- update = proxy(this, 'updateNowIndicator'); // bind to `this`
- this.initialNowDate = this.calendar.getNow();
- this.initialNowQueriedMs = +new Date();
- this.renderNowIndicator(this.initialNowDate);
- this.isNowIndicatorRendered = true;
- // wait until the beginning of the next interval
- delay = this.initialNowDate.clone().startOf(unit).add(1, unit) - this.initialNowDate;
- this.nowIndicatorTimeoutID = setTimeout(function() {
- _this.nowIndicatorTimeoutID = null;
- update();
- delay = +moment.duration(1, unit);
- delay = Math.max(100, delay); // prevent too frequent
- _this.nowIndicatorIntervalID = setInterval(update, delay); // update every interval
- }, delay);
- }
- }
- },
- // rerenders the now indicator, computing the new current time from the amount of time that has passed
- // since the initial getNow call.
- updateNowIndicator: function() {
- if (this.isNowIndicatorRendered) {
- this.unrenderNowIndicator();
- this.renderNowIndicator(
- this.initialNowDate.clone().add(new Date() - this.initialNowQueriedMs) // add ms
- );
- }
- },
- // Immediately unrenders the view's current time indicator and stops any re-rendering timers.
- // Won't cause side effects if indicator isn't rendered.
- stopNowIndicator: function() {
- if (this.isNowIndicatorRendered) {
- if (this.nowIndicatorTimeoutID) {
- clearTimeout(this.nowIndicatorTimeoutID);
- this.nowIndicatorTimeoutID = null;
- }
- if (this.nowIndicatorIntervalID) {
- clearTimeout(this.nowIndicatorIntervalID);
- this.nowIndicatorIntervalID = null;
- }
- this.unrenderNowIndicator();
- this.isNowIndicatorRendered = false;
- }
- },
- // Returns a string unit, like 'second' or 'minute' that defined how often the current time indicator
- // should be refreshed. If something falsy is returned, no time indicator is rendered at all.
- getNowIndicatorUnit: function() {
- // subclasses should implement
- },
- // Renders a current time indicator at the given datetime
- renderNowIndicator: function(date) {
- // subclasses should implement
- },
- // Undoes the rendering actions from renderNowIndicator
- unrenderNowIndicator: function() {
- // subclasses should implement
- },
- /* Dimensions
- ------------------------------------------------------------------------------------------------------------------*/
- // Refreshes anything dependant upon sizing of the container element of the grid
- updateSize: function(isResize) {
- var scrollState;
- if (isResize) {
- this.capturedScroll();
- }
- this.updateHeight(isResize);
- this.updateWidth(isResize);
- this.updateNowIndicator();
- if (isResize) {
- this.releaseScroll();
- }
- },
- // Refreshes the horizontal dimensions of the calendar
- updateWidth: function(isResize) {
- // subclasses should implement
- },
- // Refreshes the vertical dimensions of the calendar
- updateHeight: function(isResize) {
- var calendar = this.calendar; // we poll the calendar for height information
- this.setHeight(
- calendar.getSuggestedViewHeight(),
- calendar.isHeightAuto()
- );
- },
- // Updates the vertical dimensions of the calendar to the specified height.
- // if `isAuto` is set to true, height becomes merely a suggestion and the view should use its "natural" height.
- setHeight: function(height, isAuto) {
- // subclasses should implement
- },
- /* Scroller
- ------------------------------------------------------------------------------------------------------------------*/
- capturedScrollDepth: 0,
- capturedScroll: null,
- captureScroll: function() {
- if (!(this.capturedScrollDepth++)) {
- this.capturedScroll = this.isDisplayingDateVisuals ? this.queryScroll() : {};
- }
- },
- releaseScroll: function(isInitial, forcedScroll) {
- var _this = this;
- var scroll;
- var exec;
- if (!(--this.capturedScrollDepth)) {
- scroll = forcedScroll || this.capturedScroll;
- this.capturedScroll = null;
- if (isInitial) {
- $.extend(scroll, this.computeInitialScroll());
- }
- exec = function() { _this.setScroll(scroll); };
- exec();
- if (isInitial) {
- setTimeout(exec, 0);
- }
- }
- },
- discardScroll: function() {
- if (!(--this.capturedScrollDepth)) {
- this.capturedScroll = null;
- }
- },
- computeInitialScroll: function() {
- return {};
- },
- queryScroll: function() {
- return {};
- },
- setScroll: function(scroll) {
- },
- /* Event Elements / Segments
- ------------------------------------------------------------------------------------------------------------------*/
- // Does everything necessary to display the given events onto the current view. Guaranteed to redraw content.
- // async. promise might not resolve if rendering cancelled.
- // Assumes date visuals already displayed.
- displayEvents: function() {
- var _this = this;
- return this.displayingEvents = this.requestEvents().then(function(events) {
- _this.bindEvents(); // listen to changes. do this before the setEvents, because might trigger a reset itself
- return _this.setEvents(events);
- });
- },
- // Does everything necessary to clear the view's currently-rendered events.
- // sync
- stopDisplayingEvents: function() {
- this.displayingEvents = null;
- this.unbindEvents();
- this.unsetEvents();
- },
- requestEvents: function() {
- return this.calendar.requestEvents(this.start, this.end);
- },
- bindEvents: function() {
- if (!this.isEventsBounds) {
- this.listenTo(this.calendar, 'resetEvents', this.resetEvents);
- this.isEventsBounds = true;
- }
- },
- unbindEvents: function() {
- if (this.isEventsBounds) {
- this.stopListeningTo(this.calendar, 'resetEvents');
- this.isEventsBounds = false;
- }
- },
- // async
- resetEvents: function(events) {
- var _this = this;
- // do this before unsetEvents, a destructive action
- this.captureScroll();
- this.calendar.freezeContentHeight();
- this.unsetEvents();
- return this.setEvents(events).then(function() {
- _this.releaseScroll();
- _this.calendar.freezeContentHeight();
- });
- },
- // async
- setEvents: function(events) {
- var _this = this;
- if (this.isEventsSet) {
- return this.resetEvents(events);
- }
- else {
- this.isEventsSet = true;
- return this.eventRenderQueue.push(function() {
- _this.captureScroll();
- _this.calendar.freezeContentHeight();
- _this.renderEvents(events);
- _this.calendar.unfreezeContentHeight();
- _this.releaseScroll();
- _this.triggerEventRender();
- });
- }
- },
- // sync
- unsetEvents: function() {
- var _this = this;
- if (this.isEventsSet) {
- this.isEventsSet = false; // must go first. so triggers don't reinvoke unsetEvents
- this.eventRenderQueue.clear(); // kill in-progress renders
- this.triggerEventUnrender();
- if (this.destroyEvents) {
- this.destroyEvents(); // TODO: deprecate
- }
- this.unrenderEvents();
- }
- },
- // Renders the events onto the view.
- renderEvents: function(events) {
- // subclasses should implement
- },
- // Removes event elements from the view.
- unrenderEvents: function() {
- // subclasses should implement
- },
- // Signals that all events have been rendered
- triggerEventRender: function() {
- this.renderedEventSegEach(function(seg) {
- this.trigger('eventAfterRender', seg.event, seg.event, seg.el);
- });
- this.trigger('eventAfterAllRender');
- },
- // Signals that all event elements are about to be removed
- triggerEventUnrender: function() {
- this.renderedEventSegEach(function(seg) {
- this.trigger('eventDestroy', seg.event, seg.event, seg.el);
- });
- },
- // Given an event and the default element used for rendering, returns the element that should actually be used.
- // Basically runs events and elements through the eventRender hook.
- resolveEventEl: function(event, el) {
- var custom = this.trigger('eventRender', event, event, el);
- if (custom === false) { // means don't render at all
- el = null;
- }
- else if (custom && custom !== true) {
- el = $(custom);
- }
- return el;
- },
- // Hides all rendered event segments linked to the given event
- showEvent: function(event) {
- this.renderedEventSegEach(function(seg) {
- seg.el.css('visibility', '');
- }, event);
- },
- // Shows all rendered event segments linked to the given event
- hideEvent: function(event) {
- this.renderedEventSegEach(function(seg) {
- seg.el.css('visibility', 'hidden');
- }, event);
- },
- // Iterates through event segments that have been rendered (have an el). Goes through all by default.
- // If the optional `event` argument is specified, only iterates through segments linked to that event.
- // The `this` value of the callback function will be the view.
- renderedEventSegEach: function(func, event) {
- var segs = this.getEventSegs();
- var i;
- for (i = 0; i < segs.length; i++) {
- if (!event || segs[i].event._id === event._id) {
- if (segs[i].el) {
- func.call(this, segs[i]);
- }
- }
- }
- },
- // Retrieves all the rendered segment objects for the view
- getEventSegs: function() {
- // subclasses must implement
- return [];
- },
- /* Event Drag-n-Drop
- ------------------------------------------------------------------------------------------------------------------*/
- // Computes if the given event is allowed to be dragged by the user
- isEventDraggable: function(event) {
- return this.isEventStartEditable(event);
- },
- isEventStartEditable: function(event) {
- return firstDefined(
- event.startEditable,
- (event.source || {}).startEditable,
- this.opt('eventStartEditable'),
- this.isEventGenerallyEditable(event)
- );
- },
- isEventGenerallyEditable: function(event) {
- return firstDefined(
- event.editable,
- (event.source || {}).editable,
- this.opt('editable')
- );
- },
- // Must be called when an event in the view is dropped onto new location.
- // `dropLocation` is an object that contains the new zoned start/end/allDay values for the event.
- reportEventDrop: function(event, dropLocation, largeUnit, el, ev) {
- var calendar = this.calendar;
- var mutateResult = calendar.mutateEvent(event, dropLocation, largeUnit);
- var undoFunc = function() {
- mutateResult.undo();
- calendar.reportEventChange();
- };
- this.triggerEventDrop(event, mutateResult.dateDelta, undoFunc, el, ev);
- calendar.reportEventChange(); // will rerender events
- },
- // Triggers event-drop handlers that have subscribed via the API
- triggerEventDrop: function(event, dateDelta, undoFunc, el, ev) {
- this.trigger('eventDrop', el[0], event, dateDelta, undoFunc, ev, {}); // {} = jqui dummy
- },
- /* External Element Drag-n-Drop
- ------------------------------------------------------------------------------------------------------------------*/
- // Must be called when an external element, via jQuery UI, has been dropped onto the calendar.
- // `meta` is the parsed data that has been embedded into the dragging event.
- // `dropLocation` is an object that contains the new zoned start/end/allDay values for the event.
- reportExternalDrop: function(meta, dropLocation, el, ev, ui) {
- var eventProps = meta.eventProps;
- var eventInput;
- var event;
- // Try to build an event object and render it. TODO: decouple the two
- if (eventProps) {
- eventInput = $.extend({}, eventProps, dropLocation);
- event = this.calendar.renderEvent(eventInput, meta.stick)[0]; // renderEvent returns an array
- }
- this.triggerExternalDrop(event, dropLocation, el, ev, ui);
- },
- // Triggers external-drop handlers that have subscribed via the API
- triggerExternalDrop: function(event, dropLocation, el, ev, ui) {
- // trigger 'drop' regardless of whether element represents an event
- this.trigger('drop', el[0], dropLocation.start, ev, ui);
- if (event) {
- this.trigger('eventReceive', null, event); // signal an external event landed
- }
- },
- /* Drag-n-Drop Rendering (for both events and external elements)
- ------------------------------------------------------------------------------------------------------------------*/
- // Renders a visual indication of a event or external-element drag over the given drop zone.
- // If an external-element, seg will be `null`.
- // Must return elements used for any mock events.
- renderDrag: function(dropLocation, seg) {
- // subclasses must implement
- },
- // Unrenders a visual indication of an event or external-element being dragged.
- unrenderDrag: function() {
- // subclasses must implement
- },
- /* Event Resizing
- ------------------------------------------------------------------------------------------------------------------*/
- // Computes if the given event is allowed to be resized from its starting edge
- isEventResizableFromStart: function(event) {
- return this.opt('eventResizableFromStart') && this.isEventResizable(event);
- },
- // Computes if the given event is allowed to be resized from its ending edge
- isEventResizableFromEnd: function(event) {
- return this.isEventResizable(event);
- },
- // Computes if the given event is allowed to be resized by the user at all
- isEventResizable: function(event) {
- var source = event.source || {};
- return firstDefined(
- event.durationEditable,
- source.durationEditable,
- this.opt('eventDurationEditable'),
- event.editable,
- source.editable,
- this.opt('editable')
- );
- },
- // Must be called when an event in the view has been resized to a new length
- reportEventResize: function(event, resizeLocation, largeUnit, el, ev) {
- var calendar = this.calendar;
- var mutateResult = calendar.mutateEvent(event, resizeLocation, largeUnit);
- var undoFunc = function() {
- mutateResult.undo();
- calendar.reportEventChange();
- };
- this.triggerEventResize(event, mutateResult.durationDelta, undoFunc, el, ev);
- calendar.reportEventChange(); // will rerender events
- },
- // Triggers event-resize handlers that have subscribed via the API
- triggerEventResize: function(event, durationDelta, undoFunc, el, ev) {
- this.trigger('eventResize', el[0], event, durationDelta, undoFunc, ev, {}); // {} = jqui dummy
- },
- /* Selection (time range)
- ------------------------------------------------------------------------------------------------------------------*/
- // Selects a date span on the view. `start` and `end` are both Moments.
- // `ev` is the native mouse event that begin the interaction.
- select: function(span, ev) {
- this.unselect(ev);
- this.renderSelection(span);
- this.reportSelection(span, ev);
- },
- // Renders a visual indication of the selection
- renderSelection: function(span) {
- // subclasses should implement
- },
- // Called when a new selection is made. Updates internal state and triggers handlers.
- reportSelection: function(span, ev) {
- this.isSelected = true;
- this.triggerSelect(span, ev);
- },
- // Triggers handlers to 'select'
- triggerSelect: function(span, ev) {
- this.trigger(
- 'select',
- null,
- this.calendar.applyTimezone(span.start), // convert to calendar's tz for external API
- this.calendar.applyTimezone(span.end), // "
- ev
- );
- },
- // Undoes a selection. updates in the internal state and triggers handlers.
- // `ev` is the native mouse event that began the interaction.
- unselect: function(ev) {
- if (this.isSelected) {
- this.isSelected = false;
- if (this.destroySelection) {
- this.destroySelection(); // TODO: deprecate
- }
- this.unrenderSelection();
- this.trigger('unselect', null, ev);
- }
- },
- // Unrenders a visual indication of selection
- unrenderSelection: function() {
- // subclasses should implement
- },
- /* Event Selection
- ------------------------------------------------------------------------------------------------------------------*/
- selectEvent: function(event) {
- if (!this.selectedEvent || this.selectedEvent !== event) {
- this.unselectEvent();
- this.renderedEventSegEach(function(seg) {
- seg.el.addClass('fc-selected');
- }, event);
- this.selectedEvent = event;
- }
- },
- unselectEvent: function() {
- if (this.selectedEvent) {
- this.renderedEventSegEach(function(seg) {
- seg.el.removeClass('fc-selected');
- }, this.selectedEvent);
- this.selectedEvent = null;
- }
- },
- isEventSelected: function(event) {
- // event references might change on refetchEvents(), while selectedEvent doesn't,
- // so compare IDs
- return this.selectedEvent && this.selectedEvent._id === event._id;
- },
- /* Mouse / Touch Unselecting (time range & event unselection)
- ------------------------------------------------------------------------------------------------------------------*/
- // TODO: move consistently to down/start or up/end?
- // TODO: don't kill previous selection if touch scrolling
- handleDocumentMousedown: function(ev) {
- if (isPrimaryMouseButton(ev)) {
- this.processUnselect(ev);
- }
- },
- processUnselect: function(ev) {
- this.processRangeUnselect(ev);
- this.processEventUnselect(ev);
- },
- processRangeUnselect: function(ev) {
- var ignore;
- // is there a time-range selection?
- if (this.isSelected && this.opt('unselectAuto')) {
- // only unselect if the clicked element is not identical to or inside of an 'unselectCancel' element
- ignore = this.opt('unselectCancel');
- if (!ignore || !$(ev.target).closest(ignore).length) {
- this.unselect(ev);
- }
- }
- },
- processEventUnselect: function(ev) {
- if (this.selectedEvent) {
- if (!$(ev.target).closest('.fc-selected').length) {
- this.unselectEvent();
- }
- }
- },
- /* Day Click
- ------------------------------------------------------------------------------------------------------------------*/
- // Triggers handlers to 'dayClick'
- // Span has start/end of the clicked area. Only the start is useful.
- triggerDayClick: function(span, dayEl, ev) {
- this.trigger(
- 'dayClick',
- dayEl,
- this.calendar.applyTimezone(span.start), // convert to calendar's timezone for external API
- ev
- );
- },
- /* Date Utils
- ------------------------------------------------------------------------------------------------------------------*/
- // Initializes internal variables related to calculating hidden days-of-week
- initHiddenDays: function() {
- var hiddenDays = this.opt('hiddenDays') || []; // array of day-of-week indices that are hidden
- var isHiddenDayHash = []; // is the day-of-week hidden? (hash with day-of-week-index -> bool)
- var dayCnt = 0;
- var i;
- if (this.opt('weekends') === false) {
- hiddenDays.push(0, 6); // 0=sunday, 6=saturday
- }
- for (i = 0; i < 7; i++) {
- if (
- !(isHiddenDayHash[i] = $.inArray(i, hiddenDays) !== -1)
- ) {
- dayCnt++;
- }
- }
- if (!dayCnt) {
- throw 'invalid hiddenDays'; // all days were hidden? bad.
- }
- this.isHiddenDayHash = isHiddenDayHash;
- },
- // Is the current day hidden?
- // `day` is a day-of-week index (0-6), or a Moment
- isHiddenDay: function(day) {
- if (moment.isMoment(day)) {
- day = day.day();
- }
- return this.isHiddenDayHash[day];
- },
- // Incrementing the current day until it is no longer a hidden day, returning a copy.
- // If the initial value of `date` is not a hidden day, don't do anything.
- // Pass `isExclusive` as `true` if you are dealing with an end date.
- // `inc` defaults to `1` (increment one day forward each time)
- skipHiddenDays: function(date, inc, isExclusive) {
- var out = date.clone();
- inc = inc || 1;
- while (
- this.isHiddenDayHash[(out.day() + (isExclusive ? inc : 0) + 7) % 7]
- ) {
- out.add(inc, 'days');
- }
- return out;
- },
- // Returns the date range of the full days the given range visually appears to occupy.
- // Returns a new range object.
- computeDayRange: function(range) {
- var startDay = range.start.clone().stripTime(); // the beginning of the day the range starts
- var end = range.end;
- var endDay = null;
- var endTimeMS;
- if (end) {
- endDay = end.clone().stripTime(); // the beginning of the day the range exclusively ends
- endTimeMS = +end.time(); // # of milliseconds into `endDay`
- // If the end time is actually inclusively part of the next day and is equal to or
- // beyond the next day threshold, adjust the end to be the exclusive end of `endDay`.
- // Otherwise, leaving it as inclusive will cause it to exclude `endDay`.
- if (endTimeMS && endTimeMS >= this.nextDayThreshold) {
- endDay.add(1, 'days');
- }
- }
- // If no end was specified, or if it is within `startDay` but not past nextDayThreshold,
- // assign the default duration of one day.
- if (!end || endDay <= startDay) {
- endDay = startDay.clone().add(1, 'days');
- }
- return { start: startDay, end: endDay };
- },
- // Does the given event visually appear to occupy more than one day?
- isMultiDayEvent: function(event) {
- var range = this.computeDayRange(event); // event is range-ish
- return range.end.diff(range.start, 'days') > 1;
- }
- });
|