| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209 |
- /* An abstract class from which other views inherit from
- ----------------------------------------------------------------------------------------------------------------------*/
- var View = FC.View = Model.extend({
- 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
- viewSpec: null,
- options: null, // hash containing all options. already merged with view-specific-options
- el: null, // the view's containing element. set by Calendar
- renderQueue: null,
- isDatesRendered: false,
- isEventsRendered: false,
- isBaseRendered: false, // related to viewRender/viewDestroy triggers
- 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, viewSpec) {
- Model.prototype.constructor.call(this);
- this.calendar = calendar;
- this.viewSpec = viewSpec;
- // shortcuts
- this.type = viewSpec.type;
- this.options = viewSpec.options;
- // .name is deprecated
- this.name = this.type;
- this.nextDayThreshold = moment.duration(this.opt('nextDayThreshold'));
- this.initThemingProps();
- this.initHiddenDays();
- this.isRTL = this.opt('isRTL');
- this.eventOrderSpecs = parseFieldSpecs(this.opt('eventOrder'));
- // TODO: differentiate eventRenderWait from date rendering
- this.renderQueue = new TaskQueue(this.opt('eventRenderWait'));
- 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.
- publiclyTrigger: function(name, thisObj) { // arguments beyond thisObj are passed along
- var calendar = this.calendar;
- return calendar.publiclyTrigger.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
- )
- );
- },
- /* 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() {
- var range;
- // for views that span a large unit of time, show the proper interval, ignoring stray days before and after
- if (/^(year|month)$/.test(this.currentRangeUnit)) {
- range = this.currentRange;
- }
- else { // for day units or smaller, use the actual day range
- range = this.activeRange;
- }
- return this.formatRange(
- {
- // in case currentRange has a time, make sure timezone is correct
- start: this.calendar.applyTimezone(range.start),
- end: this.calendar.applyTimezone(range.end)
- },
- 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.currentRangeUnit == 'year') {
- return 'YYYY';
- }
- else if (this.currentRangeUnit == 'month') {
- return this.opt('monthYearFormat'); // like "September 2014"
- }
- else if (this.currentRangeAs('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
- // -----------------------------------------------------------------------------------------------------------------
- // 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.bindBaseRenderHandlers();
- this.renderSkeleton();
- },
- // Removes the view's container element from the DOM, clearing any content beforehand.
- // Undoes any other DOM-related attachments.
- removeElement: function() {
- this.unsetDate();
- this.unrenderSkeleton();
- this.unbindGlobalHandlers();
- this.unbindBaseRenderHandlers();
- 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 Setting/Unsetting
- // -----------------------------------------------------------------------------------------------------------------
- setDate: function(date, forcedScroll) {
- var currentDateProfile = this.get('dateProfile');
- var newDateProfile = this.buildDateProfile(date, null, true); // forceToValid=true
- if (
- !currentDateProfile ||
- !isRangesEqual(currentDateProfile.activeRange, newDateProfile.activeRange)
- ) {
- this.setDateProfile(newDateProfile, forcedScroll);
- }
- return newDateProfile.date;
- },
- unsetDate: function() {
- if (this.has('dateProfile')) {
- this.unsetDateProfile();
- }
- },
- setDateProfile: function(dateProfile, forcedScroll) {
- var _this = this;
- // render first, so that dependants of dateProfile know rendering already happened
- this.renderQueue.add(function() {
- _this.executeDateRender(dateProfile, forcedScroll);
- });
- this.set('dateProfile', dateProfile); // for watchers
- },
- unsetDateProfile: function() {
- var _this = this;
- this.unset('dateProfile'); // for watchers. let them react before unrendering.
- this.renderQueue.add(function() {
- _this.executeDateUnrender();
- });
- },
- // Event Data
- // -----------------------------------------------------------------------------------------------------------------
- fetchInitialEvents: function(dateProfile) {
- return this.calendar.requestEvents(
- dateProfile.activeRange.start,
- dateProfile.activeRange.end
- );
- },
- bindEventChanges: function() {
- var _this = this;
- this.listenTo(this.calendar, 'eventsReset', function(events) {
- _this.setEvents(events);
- });
- },
- unbindEventChanges: function() {
- this.stopListeningTo(this.calendar, 'eventsReset');
- this.unsetEvents();
- },
- setEvents: function(events) {
- var _this = this;
- // render first, so that dependants of bindingEvents/currentEvents know rendering already happened
- this.renderQueue.add(function() {
- _this.executeEventsRender(events);
- });
- this.set('currentEvents', events); // for watchers
- },
- unsetEvents: function() {
- var _this = this;
- this.unset('currentEvents'); // for watchers. let them react before unrendering.
- this.renderQueue.add(function() {
- _this.executeEventsUnrender();
- });
- },
- // Date High-level Rendering
- // -----------------------------------------------------------------------------------------------------------------
- // if dateProfile not specified, uses current
- executeDateRender: function(dateProfile, forcedScroll) {
- var scroll;
- this.setDateProfileForRendering(dateProfile);
- this.updateTitle();
- this.calendar.updateToolbarButtons();
- scroll = forcedScroll || this.queryScroll();
- this.freezeHeight();
- this.executeDateUnrender(true);
- if (this.render) {
- this.render(); // TODO: deprecate
- }
- this.renderDates();
- this.updateSize();
- this.renderBusinessHours(); // might need coordinates, so should go after updateSize()
- this.startNowIndicator();
- this.thawHeight();
- if (!forcedScroll) {
- $.extend(scroll, this.computeInitialDateScroll());
- }
- this.applyScroll(scroll);
- this.isDatesRendered = true;
- this.trigger('datesRendered');
- },
- executeDateUnrender: function(willRender) {
- var scroll;
- if (this.isDatesRendered) {
- if (!willRender) {
- scroll = this.queryScroll();
- this.freezeHeight();
- }
- this.unselect();
- this.stopNowIndicator();
- this.trigger('before:datesUnrendered');
- this.unrenderBusinessHours();
- this.unrenderDates();
- if (this.destroy) {
- this.destroy(); // TODO: deprecate
- }
- if (!willRender) {
- this.thawHeight();
- this.applyScroll(scroll);
- }
- this.isDatesRendered = false;
- }
- },
- // Date Low-level Rendering
- // -----------------------------------------------------------------------------------------------------------------
- // date-cell content only
- renderDates: function() {
- // subclasses should implement
- },
- // date-cell content only
- unrenderDates: function() {
- // subclasses should override
- },
- // Determing when the "meat" of the view is rendered (aka the base)
- // -----------------------------------------------------------------------------------------------------------------
- bindBaseRenderHandlers: function() {
- var _this = this;
- this.on('datesRendered.baseHandler', function() {
- _this.onBaseRender();
- });
- this.on('before:datesUnrendered.baseHandler', function() {
- _this.onBeforeBaseUnrender();
- });
- },
- unbindBaseRenderHandlers: function() {
- this.off('.baseHandler');
- },
- onBaseRender: function() {
- this.publiclyTrigger('viewRender', this, this, this.el);
- },
- onBeforeBaseUnrender: function() {
- this.publiclyTrigger('viewDestroy', this, this, this.el);
- },
- // Misc view rendering utils
- // -----------------------------------------------------------------------------------------------------------------
- // Binds DOM handlers to elements that reside outside the view container, such as the document
- bindGlobalHandlers: function() {
- this.listenTo(GlobalEmitter.get(), {
- touchstart: this.processUnselect,
- mousedown: this.handleDocumentMousedown
- });
- },
- // Unbinds DOM handlers from elements that reside outside the view container
- unbindGlobalHandlers: function() {
- this.stopListeningTo(GlobalEmitter.get());
- },
- // 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 scroll;
- if (isResize) {
- scroll = this.queryScroll();
- }
- this.updateHeight(isResize);
- this.updateWidth(isResize);
- this.updateNowIndicator();
- if (isResize) {
- this.applyScroll(scroll);
- }
- },
- // 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
- ------------------------------------------------------------------------------------------------------------------*/
- queryScroll: function() {
- var scroll = {};
- if (this.isDatesRendered) {
- $.extend(scroll, this.queryDateScroll());
- }
- return scroll;
- },
- applyScroll: function(scroll) {
- this.applyDateScroll(scroll);
- },
- computeInitialDateScroll: function() {
- return {}; // subclasses must implement
- },
- queryDateScroll: function() {
- return {}; // subclasses must implement
- },
- applyDateScroll: function(scroll) {
- ; // subclasses must implement
- },
- /* Height Freezing
- ------------------------------------------------------------------------------------------------------------------*/
- freezeHeight: function() {
- this.calendar.freezeContentHeight();
- },
- thawHeight: function() {
- this.calendar.thawContentHeight();
- },
- // Event High-level Rendering
- // -----------------------------------------------------------------------------------------------------------------
- executeEventsRender: function(events) {
- var scroll;
- scroll = this.queryScroll();
- this.freezeHeight();
- this.executeEventsUnrender(true);
- this.renderEvents(events);
- this.thawHeight();
- this.applyScroll(scroll);
- this.isEventsRendered = true;
- this.onEventsRender();
- },
- executeEventsUnrender: function(willRender) {
- var scroll;
- if (this.isEventsRendered) {
- this.onBeforeEventsUnrender();
- if (!willRender) {
- scroll = this.queryScroll();
- this.freezeHeight();
- }
- if (this.destroyEvents) {
- this.destroyEvents(); // TODO: deprecate
- }
- this.unrenderEvents();
- if (!willRender) {
- this.thawHeight();
- this.applyScroll(scroll);
- }
- this.isEventsRendered = false;
- }
- },
- // Event Rendering Triggers
- // -----------------------------------------------------------------------------------------------------------------
- // Signals that all events have been rendered
- onEventsRender: function() {
- this.renderedEventSegEach(function(seg) {
- this.publiclyTrigger('eventAfterRender', seg.event, seg.event, seg.el);
- });
- this.publiclyTrigger('eventAfterAllRender');
- },
- // Signals that all event elements are about to be removed
- onBeforeEventsUnrender: function() {
- this.renderedEventSegEach(function(seg) {
- this.publiclyTrigger('eventDestroy', seg.event, seg.event, seg.el);
- });
- },
- // Event Low-level Rendering
- // -----------------------------------------------------------------------------------------------------------------
- // Renders the events onto the view.
- renderEvents: function(events) {
- // subclasses should implement
- },
- // Removes event elements from the view.
- unrenderEvents: function() {
- // subclasses should implement
- },
- // Event Rendering Utils
- // -----------------------------------------------------------------------------------------------------------------
- // 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.publiclyTrigger('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.
- reportSegDrop: function(seg, dropLocation, largeUnit, el, ev) {
- var calendar = this.calendar;
- var mutateResult = calendar.mutateSeg(seg, dropLocation, largeUnit);
- var undoFunc = function() {
- mutateResult.undo();
- calendar.reportEventChange();
- };
- this.triggerEventDrop(seg.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.publiclyTrigger('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.publiclyTrigger('drop', el[0], dropLocation.start, ev, ui);
- if (event) {
- this.publiclyTrigger('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
- reportSegResize: function(seg, resizeLocation, largeUnit, el, ev) {
- var calendar = this.calendar;
- var mutateResult = calendar.mutateSeg(seg, resizeLocation, largeUnit);
- var undoFunc = function() {
- mutateResult.undo();
- calendar.reportEventChange();
- };
- this.triggerEventResize(seg.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.publiclyTrigger('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.publiclyTrigger(
- '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.publiclyTrigger('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.publiclyTrigger(
- 'dayClick',
- dayEl,
- this.calendar.applyTimezone(span.start), // convert to calendar's timezone for external API
- ev
- );
- },
- /* Date Utils
- ------------------------------------------------------------------------------------------------------------------*/
- // 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;
- }
- });
- View.watch('initialEvents', [ 'dateProfile' ], function(deps) {
- return this.fetchInitialEvents(deps.dateProfile);
- });
- View.watch('bindingEvents', [ 'initialEvents' ], function(deps) {
- this.bindEventChanges();
- this.setEvents(deps.initialEvents);
- }, function() {
- this.unbindEventChanges();
- this.unsetEvents();
- });
|