diff --git a/src/NzbDrone.Api/Albums/AlbumResource.cs b/src/NzbDrone.Api/Albums/AlbumResource.cs index 6c92ceaa6..1bab9d342 100644 --- a/src/NzbDrone.Api/Albums/AlbumResource.cs +++ b/src/NzbDrone.Api/Albums/AlbumResource.cs @@ -13,12 +13,13 @@ namespace NzbDrone.Api.Albums { public string Title { get; set; } + public int ArtistId { get; set; } public string Label { get; set; } public bool Monitored { get; set; } public string Path { get; set; } public int ProfileId { get; set; } public Ratings Ratings { get; set; } - public DateTime ReleaseDate { get; set; } + public DateTime? ReleaseDate { get; set; } public List Genres { get; set; } public ArtistResource Artist { get; set; } public List Images { get; set; } @@ -34,6 +35,7 @@ namespace NzbDrone.Api.Albums return new AlbumResource { Id = model.Id, + ArtistId = model.ArtistId, Label = model.Label, Path = model.Path, ProfileId = model.ProfileId, @@ -55,6 +57,7 @@ namespace NzbDrone.Api.Albums return new Core.Music.Album { Id = resource.Id, + ArtistId = resource.ArtistId, Label = resource.Label, Path = resource.Path, Monitored = resource.Monitored, diff --git a/src/NzbDrone.Api/Calendar/CalendarFeedModule.cs b/src/NzbDrone.Api/Calendar/CalendarFeedModule.cs index 69a9225e4..e65a42833 100644 --- a/src/NzbDrone.Api/Calendar/CalendarFeedModule.cs +++ b/src/NzbDrone.Api/Calendar/CalendarFeedModule.cs @@ -8,6 +8,7 @@ using Ical.Net.Interfaces.Serialization; using Ical.Net.Serialization; using Ical.Net.Serialization.iCalendar.Factory; using NzbDrone.Core.Tv; +using NzbDrone.Core.Music; using Nancy.Responses; using NzbDrone.Core.Tags; using NzbDrone.Common.Extensions; @@ -16,13 +17,13 @@ namespace NzbDrone.Api.Calendar { public class CalendarFeedModule : NzbDroneFeedModule { - private readonly IEpisodeService _episodeService; + private readonly IAlbumService _albumService; private readonly ITagService _tagService; - public CalendarFeedModule(IEpisodeService episodeService, ITagService tagService) + public CalendarFeedModule(IAlbumService albumService, ITagService tagService) : base("calendar") { - _episodeService = episodeService; + _albumService = albumService; _tagService = tagService; Get["/NzbDrone.ics"] = options => GetCalendarFeed(); @@ -86,7 +87,7 @@ namespace NzbDrone.Api.Calendar tags.AddRange(tagInput.Split(',').Select(_tagService.GetTag).Select(t => t.Id)); } - var episodes = _episodeService.EpisodesBetweenDates(start, end, unmonitored); + var albums = _albumService.AlbumsBetweenDates(start, end, unmonitored); var calendar = new Ical.Net.Calendar { // This will need to point to the hosted web site @@ -96,43 +97,28 @@ namespace NzbDrone.Api.Calendar - foreach (var episode in episodes.OrderBy(v => v.AirDateUtc.Value)) + foreach (var album in albums.OrderBy(v => v.ReleaseDate)) { - if (premiersOnly && (episode.SeasonNumber == 0 || episode.EpisodeNumber != 1)) - { - continue; - } + //if (premiersOnly && (album.SeasonNumber == 0 || album.EpisodeNumber != 1)) + //{ + // continue; + //} - if (tags.Any() && tags.None(episode.Series.Tags.Contains)) + if (tags.Any() && tags.None(album.Artist.Tags.Contains)) { continue; } var occurrence = calendar.Create(); - occurrence.Uid = "NzbDrone_album_" + episode.Id; - occurrence.Status = episode.HasFile ? EventStatus.Confirmed : EventStatus.Tentative; - occurrence.Description = episode.Overview; - occurrence.Categories = new List() { episode.Series.Network }; + occurrence.Uid = "NzbDrone_album_" + album.Id; + //occurrence.Status = album.HasFile ? EventStatus.Confirmed : EventStatus.Tentative; + //occurrence.Description = album.Overview; + //occurrence.Categories = new List() { album.Artist. }; - if (asAllDay) - { - occurrence.Start = new CalDateTime(episode.AirDateUtc.Value) { HasTime = false }; - } - else - { - occurrence.Start = new CalDateTime(episode.AirDateUtc.Value) { HasTime = true }; - occurrence.End = new CalDateTime(episode.AirDateUtc.Value.AddMinutes(episode.Series.Runtime)) { HasTime = true }; - } - - switch (episode.Series.SeriesType) - { - case SeriesTypes.Daily: - occurrence.Summary = $"{episode.Series.Title} - {episode.Title}"; - break; - default: - occurrence.Summary =$"{episode.Series.Title} - {episode.SeasonNumber}x{episode.EpisodeNumber:00} - {episode.Title}"; - break; - } + occurrence.Start = new CalDateTime(album.ReleaseDate.Value) { HasTime = false }; + + occurrence.Summary =$"{album.Artist.Name} - {album.Title}"; + } var serializer = (IStringSerializer) new SerializerFactory().Build(calendar.GetType(), new SerializationContext()); diff --git a/src/NzbDrone.Api/Calendar/CalendarModule.cs b/src/NzbDrone.Api/Calendar/CalendarModule.cs index f403b79c7..3f4a0c80d 100644 --- a/src/NzbDrone.Api/Calendar/CalendarModule.cs +++ b/src/NzbDrone.Api/Calendar/CalendarModule.cs @@ -2,24 +2,26 @@ using System.Collections.Generic; using System.Linq; using NzbDrone.Api.Episodes; +using NzbDrone.Api.Albums; using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.Tv; +using NzbDrone.Core.Music; using NzbDrone.SignalR; namespace NzbDrone.Api.Calendar { - public class CalendarModule : EpisodeModuleWithSignalR + public class CalendarModule : AlbumModuleWithSignalR { - public CalendarModule(IEpisodeService episodeService, - ISeriesService seriesService, + public CalendarModule(IAlbumService albumService, + IArtistService artistService, IQualityUpgradableSpecification qualityUpgradableSpecification, IBroadcastSignalRMessage signalRBroadcaster) - : base(episodeService, seriesService, qualityUpgradableSpecification, signalRBroadcaster, "calendar") + : base(albumService, artistService, qualityUpgradableSpecification, signalRBroadcaster, "calendar") { GetResourceAll = GetCalendar; } - private List GetCalendar() + private List GetCalendar() { var start = DateTime.Today; var end = DateTime.Today.AddDays(2); @@ -33,9 +35,9 @@ namespace NzbDrone.Api.Calendar if (queryEnd.HasValue) end = DateTime.Parse(queryEnd.Value); if (queryIncludeUnmonitored.HasValue) includeUnmonitored = Convert.ToBoolean(queryIncludeUnmonitored.Value); - var resources = MapToResource(_episodeService.EpisodesBetweenDates(start, end, includeUnmonitored), true, true); + var resources = MapToResource(_albumService.AlbumsBetweenDates(start, end, includeUnmonitored), true); - return resources.OrderBy(e => e.AirDateUtc).ToList(); + return resources.OrderBy(e => e.ReleaseDate).ToList(); } } } diff --git a/src/NzbDrone.Core/Music/Album.cs b/src/NzbDrone.Core/Music/Album.cs index b6993cb29..c199396e1 100644 --- a/src/NzbDrone.Core/Music/Album.cs +++ b/src/NzbDrone.Core/Music/Album.cs @@ -15,11 +15,13 @@ namespace NzbDrone.Core.Music Images = new List(); } + public const string RELEASE_DATE_FORMAT = "yyyy-MM-dd"; + public string ForeignAlbumId { get; set; } public int ArtistId { get; set; } public string Title { get; set; } public string CleanTitle { get; set; } - public DateTime ReleaseDate { get; set; } + public DateTime? ReleaseDate { get; set; } public string Label { get; set; } //public int TrackCount { get; set; } public string Path { get; set; } diff --git a/src/NzbDrone.Core/Music/AlbumRepository.cs b/src/NzbDrone.Core/Music/AlbumRepository.cs index aba0b1945..805c40160 100644 --- a/src/NzbDrone.Core/Music/AlbumRepository.cs +++ b/src/NzbDrone.Core/Music/AlbumRepository.cs @@ -1,4 +1,6 @@ -using System.Linq; +using System; +using System.Linq; +using Marr.Data.QGen; using NzbDrone.Core.Datastore; using System.Collections.Generic; using NzbDrone.Core.Messaging.Events; @@ -11,6 +13,7 @@ namespace NzbDrone.Core.Music List GetAlbums(int artistId); Album FindByName(string cleanTitle); Album FindById(string spotifyId); + List AlbumsBetweenDates(DateTime startDate, DateTime endDate, bool includeUnmonitored); void SetMonitoredFlat(Album album, bool monitored); } @@ -36,6 +39,22 @@ namespace NzbDrone.Core.Music return Query.Where(s => s.ForeignAlbumId == foreignAlbumId).SingleOrDefault(); } + public List AlbumsBetweenDates(DateTime startDate, DateTime endDate, bool includeUnmonitored) + { + var query = Query.Join(JoinType.Inner, e => e.Artist, (e, s) => e.ArtistId == s.Id) + .Where(e => e.ReleaseDate >= startDate) + .AndWhere(e => e.ReleaseDate <= endDate); + + + if (!includeUnmonitored) + { + query.AndWhere(e => e.Monitored) + .AndWhere(e => e.Artist.Monitored); + } + + return query.ToList(); + } + public void SetMonitoredFlat(Album album, bool monitored) { album.Monitored = monitored; diff --git a/src/NzbDrone.Core/Music/AlbumService.cs b/src/NzbDrone.Core/Music/AlbumService.cs index f957fe765..0d9f0551f 100644 --- a/src/NzbDrone.Core/Music/AlbumService.cs +++ b/src/NzbDrone.Core/Music/AlbumService.cs @@ -25,6 +25,7 @@ namespace NzbDrone.Core.Music Album UpdateAlbum(Album album); List UpdateAlbums(List album); void SetAlbumMonitored(int albumId, bool monitored); + List AlbumsBetweenDates(DateTime start, DateTime end, bool includeUnmonitored); void InsertMany(List albums); void UpdateMany(List albums); void DeleteMany(List albums); @@ -110,6 +111,13 @@ namespace NzbDrone.Core.Music _albumRepository.SetFields(album, s => s.AddOptions); } + public List AlbumsBetweenDates(DateTime start, DateTime end, bool includeUnmonitored) + { + var albums = _albumRepository.AlbumsBetweenDates(start.ToUniversalTime(), end.ToUniversalTime(), includeUnmonitored); + + return albums; + } + public void InsertMany(List albums) { _albumRepository.InsertMany(albums); diff --git a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs index dd0bf3497..c67e23e48 100644 --- a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs +++ b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs @@ -403,7 +403,14 @@ namespace NzbDrone.Core.Organizer { tokenHandlers["{Album Title}"] = m => album.Title; tokenHandlers["{Album CleanTitle}"] = m => CleanTitle(album.Title); - tokenHandlers["{Release Year}"] = m => album.ReleaseDate.Year.ToString(); + if (album.ReleaseDate.HasValue) + { + tokenHandlers["{Release Year}"] = m => album.ReleaseDate.Value.Year.ToString(); + } + else + { + tokenHandlers["{Release Year}"] = m => "Unknown"; + } } private string AddSeasonEpisodeNumberingTokens(string pattern, Dictionary> tokenHandlers, List episodes, NamingConfig namingConfig) diff --git a/src/UI/Album/AlbumDetailsLayout.js b/src/UI/Album/AlbumDetailsLayout.js new file mode 100644 index 000000000..53bcf941a --- /dev/null +++ b/src/UI/Album/AlbumDetailsLayout.js @@ -0,0 +1,133 @@ +var Marionette = require('marionette'); +var SummaryLayout = require('./Summary/AlbumSummaryLayout'); +var SearchLayout = require('./Search/AlbumSearchLayout'); +var AlbumHistoryLayout = require('./History/AlbumHistoryLayout'); +var ArtistCollection = require('../Artist/ArtistCollection'); +var Messenger = require('../Shared/Messenger'); + +module.exports = Marionette.Layout.extend({ + className : 'modal-lg', + template : 'Album/AlbumDetailsLayoutTemplate', + + regions : { + summary : '#album-summary', + history : '#album-history', + search : '#album-search' + }, + + ui : { + summary : '.x-album-summary', + history : '.x-album-history', + search : '.x-album-search', + monitored : '.x-album-monitored' + }, + + events : { + + 'click .x-album-summary' : '_showSummary', + 'click .x-album-history' : '_showHistory', + 'click .x-album-search' : '_showSearch', + 'click .x-album-monitored' : '_toggleMonitored' + }, + + templateHelpers : {}, + + initialize : function(options) { + + this.templateHelpers.hideArtistLink = options.hideArtistLink; + + + this.artist = ArtistCollection.get(this.model.get('artistId')); + + this.templateHelpers.artist = this.artist.toJSON(); + this.openingTab = options.openingTab || 'summary'; + + this.listenTo(this.model, 'sync', this._setMonitoredState); + }, + + onShow : function() { + this.searchLayout = new SearchLayout({ model : this.model }); + + if (this.openingTab === 'search') { + this.searchLayout.startManualSearch = true; + this._showSearch(); + } + + else { + this._showSummary(); + } + + this._setMonitoredState(); + + if (this.artist.get('monitored')) { + this.$el.removeClass('artist-not-monitored'); + } + + else { + this.$el.addClass('artist-not-monitored'); + } + }, + + _showSummary : function(e) { + if (e) { + e.preventDefault(); + } + + this.ui.summary.tab('show'); + this.summary.show(new SummaryLayout({ + model : this.model, + artist : this.artist + })); + }, + + _showHistory : function(e) { + if (e) { + e.preventDefault(); + } + + this.ui.history.tab('show'); + this.history.show(new AlbumHistoryLayout({ + model : this.model, + artist : this.artist + })); + }, + + _showSearch : function(e) { + if (e) { + e.preventDefault(); + } + + this.ui.search.tab('show'); + this.search.show(this.searchLayout); + }, + + _toggleMonitored : function() { + if (!this.series.get('monitored')) { + + Messenger.show({ + message : 'Unable to change monitored state when artist is not monitored', + type : 'error' + }); + + return; + } + + var name = 'monitored'; + this.model.set(name, !this.model.get(name), { silent : true }); + + this.ui.monitored.addClass('icon-lidarr-spinner fa-spin'); + this.model.save(); + }, + + _setMonitoredState : function() { + this.ui.monitored.removeClass('fa-spin icon-lidarr-spinner'); + + if (this.model.get('monitored')) { + this.ui.monitored.addClass('icon-lidarr-monitored'); + this.ui.monitored.removeClass('icon-lidarr-unmonitored'); + } else { + this.ui.monitored.addClass('icon-lidarr-unmonitored'); + this.ui.monitored.removeClass('icon-lidarr-monitored'); + } + } +}); \ No newline at end of file diff --git a/src/UI/Album/AlbumDetailsLayoutTemplate.hbs b/src/UI/Album/AlbumDetailsLayoutTemplate.hbs new file mode 100644 index 000000000..f76caa790 --- /dev/null +++ b/src/UI/Album/AlbumDetailsLayoutTemplate.hbs @@ -0,0 +1,35 @@ + -
- - -
-
-
-
-
- - -
-
-
-
diff --git a/src/UI/Calendar/CalendarLayoutTemplate.hbs b/src/UI/Calendar/CalendarLayoutTemplate.hbs index db8c097be..508545565 100644 --- a/src/UI/Calendar/CalendarLayoutTemplate.hbs +++ b/src/UI/Calendar/CalendarLayoutTemplate.hbs @@ -10,13 +10,11 @@
    -
  • Unaired Premiere
  • -
  • Unaired
  • -
  • On Air
  • -
  • Downloading
  • -
  • Missing
  • -
  • Downloaded
  • -
  • Unmonitored
  • +
  • Unreleased
  • +
  • Downloading
  • +
  • Missing
  • +
  • Downloaded
  • +
  • Unmonitored
diff --git a/src/UI/Calendar/CalendarView.js b/src/UI/Calendar/CalendarView.js index 3fcc30334..058a19e61 100644 --- a/src/UI/Calendar/CalendarView.js +++ b/src/UI/Calendar/CalendarView.js @@ -89,7 +89,7 @@ module.exports = Marionette.ItemView.extend({ }); element.find('.chart').tooltip({ - title : 'Episode is downloading - {0}% {1}'.format(progress.toFixed(1), releaseTitle), + title : 'Album is downloading - {0}% {1}'.format(progress.toFixed(1), releaseTitle), container : '.fc' }); } @@ -99,9 +99,6 @@ module.exports = Marionette.ItemView.extend({ this._addStatusIcon(element, 'icon-lidarr-form-warning', 'Scene number hasn\'t been verified yet.'); } - else if (event.model.get('series').seriesType === 'anime' && event.model.get('seasonNumber') > 0 && !event.model.has('absoluteEpisodeNumber')) { - this._addStatusIcon(element, 'icon-lidarr-form-warning', 'Episode does not have an absolute episode number'); - } }, _eventAfterAllRender : function () { @@ -148,20 +145,21 @@ module.exports = Marionette.ItemView.extend({ var self = this; collection.each(function(model) { - var seriesTitle = model.get('series').title; - var start = model.get('airDateUtc'); - var runtime = model.get('series').runtime; + var albumTitle = model.get('title'); + var artistName = model.get('artist').name; + var start = model.get('releaseDate'); + var runtime = '30'; var end = moment(start).add('minutes', runtime).toISOString(); var event = { - title : seriesTitle, + title : artistName + " - " + albumTitle, start : moment(start), end : moment(end), - allDay : false, + allDay : true, statusLevel : self._getStatusLevel(model, end), downloading : QueueCollection.findEpisode(model.get('id')), model : model, - sortOrder : (model.get('seasonNumber') === 0 ? 1000000 : model.get('seasonNumber') * 10000) + model.get('episodeNumber') + sortOrder : 0 }; events.push(event); @@ -174,9 +172,9 @@ module.exports = Marionette.ItemView.extend({ var hasFile = element.get('hasFile'); var downloading = QueueCollection.findEpisode(element.get('id')) || element.get('grabbed'); var currentTime = moment(); - var start = moment(element.get('airDateUtc')); + var start = moment(element.get('releaseDate')); var end = moment(endTime); - var monitored = element.get('series').monitored && element.get('monitored'); + var monitored = element.get('artist').monitored && element.get('monitored'); var statusLevel = 'primary'; @@ -218,7 +216,7 @@ module.exports = Marionette.ItemView.extend({ _getOptions : function() { var options = { - allDayDefault : false, + allDayDefault : true, weekMode : 'variable', firstDay : UiSettings.get('firstDayOfWeek'), timeFormat : 'h(:mm)t', @@ -227,7 +225,7 @@ module.exports = Marionette.ItemView.extend({ eventAfterAllRender : this._eventAfterAllRender.bind(this), windowResize : this._windowResize.bind(this), eventClick : function(event) { - vent.trigger(vent.Commands.ShowEpisodeDetails, { episode : event.model }); + vent.trigger(vent.Commands.ShowAlbumDetails, { album : event.model }); } }; @@ -237,7 +235,7 @@ module.exports = Marionette.ItemView.extend({ options.header = { left : 'prev,next today', center : 'title', - right : 'basicWeek,basicDay' + right : 'basicWeek,listYear' }; } @@ -247,20 +245,21 @@ module.exports = Marionette.ItemView.extend({ options.header = { left : 'prev,next today', center : 'title', - right : 'month,basicWeek,basicDay' + right : 'month,basicWeek,listYear' }; } - options.titleFormat = { - month : 'MMMM YYYY', - week : UiSettings.get('shortDateFormat'), - day : UiSettings.get('longDateFormat') - }; + options.views = { + month: { + titleFormat: 'MMMM YYYY', + columnFormat: 'ddd' + }, + week: { + titleFormat: UiSettings.get('shortDateFormat'), + columnFormat: UiSettings.get('calendarWeekColumnHeader') + } + - options.columnFormat = { - month : 'ddd', - week : UiSettings.get('calendarWeekColumnHeader'), - day : 'dddd' }; options.timeFormat = UiSettings.get('timeFormat'); diff --git a/src/UI/Calendar/UpcomingCollection.js b/src/UI/Calendar/UpcomingCollection.js index 5c0e9542e..7409459a4 100644 --- a/src/UI/Calendar/UpcomingCollection.js +++ b/src/UI/Calendar/UpcomingCollection.js @@ -1,17 +1,17 @@ var Backbone = require('backbone'); var moment = require('moment'); -var EpisodeModel = require('../Series/EpisodeModel'); +var AlbumModel = require('../Artist/AlbumModel'); module.exports = Backbone.Collection.extend({ url : window.NzbDrone.ApiRoot + '/calendar', - model : EpisodeModel, + model : AlbumModel, comparator : function(model1, model2) { - var airDate1 = model1.get('airDateUtc'); + var airDate1 = model1.get('releaseDate'); var date1 = moment(airDate1); var time1 = date1.unix(); - var airDate2 = model2.get('airDateUtc'); + var airDate2 = model2.get('releaseDate'); var date2 = moment(airDate2); var time2 = date2.unix(); diff --git a/src/UI/Calendar/UpcomingCollectionView.js b/src/UI/Calendar/UpcomingCollectionView.js index 9a8944f3d..c4b40e989 100644 --- a/src/UI/Calendar/UpcomingCollectionView.js +++ b/src/UI/Calendar/UpcomingCollectionView.js @@ -9,7 +9,9 @@ module.exports = Marionette.CollectionView.extend({ itemView : UpcomingItemView, initialize : function() { + this.showUnmonitored = Config.getValue('calendar.show', 'monitored') === 'all'; + this.collection = new UpcomingCollection().bindSignalR({ updateOnly : true }); this._fetchCollection(); diff --git a/src/UI/Calendar/UpcomingItemView.js b/src/UI/Calendar/UpcomingItemView.js index f0b8eb18c..9a47a26dc 100644 --- a/src/UI/Calendar/UpcomingItemView.js +++ b/src/UI/Calendar/UpcomingItemView.js @@ -7,12 +7,12 @@ module.exports = Marionette.ItemView.extend({ tagName : 'div', events : { - 'click .x-episode-title' : '_showEpisodeDetails' + 'click .x-album-title' : '_showAlbumDetails' }, initialize : function() { - var start = this.model.get('airDateUtc'); - var runtime = this.model.get('series').runtime; + var start = this.model.get('releaseDate'); + var runtime = '30'; var end = moment(start).add('minutes', runtime); this.model.set({ @@ -22,7 +22,7 @@ module.exports = Marionette.ItemView.extend({ this.listenTo(this.model, 'change', this.render); }, - _showEpisodeDetails : function() { - vent.trigger(vent.Commands.ShowEpisodeDetails, { episode : this.model }); + _showAlbumDetails : function() { + vent.trigger(vent.Commands.ShowAlbumDetails, { album : this.model }); } }); \ No newline at end of file diff --git a/src/UI/Calendar/UpcomingItemViewTemplate.hbs b/src/UI/Calendar/UpcomingItemViewTemplate.hbs index eae2491bd..6c42e0bba 100644 --- a/src/UI/Calendar/UpcomingItemViewTemplate.hbs +++ b/src/UI/Calendar/UpcomingItemViewTemplate.hbs @@ -1,18 +1,18 @@
-

{{Day airDateUtc}}

-

{{Month airDateUtc}}

+

{{Day releaseDate}}

+

{{Month releaseDate}}

- {{#with series}} + {{#with artist}} -

{{title}}

+

{{name}}

{{/with}} -

{{StartTime airDateUtc}} {{#unless_today airDateUtc}}{{ShortDate airDateUtc}}{{/unless_today}}

+

{{#unless_today releaseDate}}{{ShortDate releaseDate}}{{/unless_today}}

- + {{title}} - {{seasonNumber}}x{{Pad2 episodeNumber}} + x

diff --git a/src/UI/Calendar/calendar.less b/src/UI/Calendar/calendar.less index d836c6720..6214b4617 100644 --- a/src/UI/Calendar/calendar.less +++ b/src/UI/Calendar/calendar.less @@ -43,6 +43,10 @@ .past { opacity : 0.8; } + + .fc-title { + white-space: normal; + } } .event { @@ -124,7 +128,7 @@ border-color : grey; } - .episode-title { + .album-title { .btn-link; .text-overflow; color : @link-color; diff --git a/src/UI/Cells/TrackTitleCell.js b/src/UI/Cells/TrackTitleCell.js index 66706c473..ab2777e52 100644 --- a/src/UI/Cells/TrackTitleCell.js +++ b/src/UI/Cells/TrackTitleCell.js @@ -5,7 +5,7 @@ module.exports = NzbDroneCell.extend({ className : 'track-title-cell', events : { - 'click' : '_showDetails' + //'click' : '_showDetails' }, render : function() { @@ -21,9 +21,9 @@ module.exports = NzbDroneCell.extend({ _showDetails : function() { var hideArtistLink = this.column.get('hideArtistLink'); - vent.trigger(vent.Commands.ShowTrackDetails, { - track : this.cellValue, - hideArtistLink : hideArtistLink - }); + //vent.trigger(vent.Commands.ShowTrackDetails, { //TODO Impelement Track search and screen as well as album? + // track : this.cellValue, + // hideArtistLink : hideArtistLink + //}); } }); \ No newline at end of file diff --git a/src/UI/Cells/cells.less b/src/UI/Cells/cells.less index 0b398dd15..eeffdcf38 100644 --- a/src/UI/Cells/cells.less +++ b/src/UI/Cells/cells.less @@ -40,6 +40,31 @@ } } +.album-title-cell { + .text-overflow(); + + color: #428bca; + text-decoration: none; + + &:focus, &:hover { + color: #2a6496; + text-decoration: underline; + cursor: pointer; + } + + @media @lg { + max-width: 350px; + } + + @media @md { + max-width: 250px; + } + + @media @sm { + max-width: 200px; + } +} + .air-date-cell { width : 120px; cursor: default; diff --git a/src/UI/Content/fullcalendar.css b/src/UI/Content/fullcalendar.css index 4e5e4eb61..1022ff39c 100644 --- a/src/UI/Content/fullcalendar.css +++ b/src/UI/Content/fullcalendar.css @@ -1,7 +1,7 @@ /*! - * FullCalendar v2.3.2 Stylesheet - * Docs & License: http://fullcalendar.io/ - * (c) 2015 Adam Shaw + * FullCalendar v3.4.0 Stylesheet + * Docs & License: https://fullcalendar.io/ + * (c) 2017 Adam Shaw */ @@ -28,7 +28,10 @@ body .fc { /* extra precedence to overcome jqui */ .fc-unthemed tbody, .fc-unthemed .fc-divider, .fc-unthemed .fc-row, -.fc-unthemed .fc-popover { +.fc-unthemed .fc-content, /* for gutter border */ +.fc-unthemed .fc-popover, +.fc-unthemed .fc-list-view, +.fc-unthemed .fc-list-heading td { border-color: #ddd; } @@ -37,7 +40,8 @@ body .fc { /* extra precedence to overcome jqui */ } .fc-unthemed .fc-divider, -.fc-unthemed .fc-popover .fc-header { +.fc-unthemed .fc-popover .fc-header, +.fc-unthemed .fc-list-heading td { background: #eee; } @@ -45,20 +49,18 @@ body .fc { /* extra precedence to overcome jqui */ color: #666; } -.fc-unthemed .fc-today { +.fc-unthemed td.fc-today { background: #fcf8e3; } .fc-highlight { /* when user is selecting cells */ background: #bce8f1; opacity: .3; - filter: alpha(opacity=30); /* for IE */ } .fc-bgevent { /* default look for background events */ background: rgb(143, 223, 130); opacity: .3; - filter: alpha(opacity=30); /* for IE */ } .fc-nonbusiness { /* default look for non-business-hours areas */ @@ -66,13 +68,21 @@ body .fc { /* extra precedence to overcome jqui */ background: #d7d7d7; } +.fc-unthemed .fc-disabled-day { + background: #d7d7d7; + opacity: .3; +} + +.ui-widget .fc-disabled-day { /* themed */ + background-image: none; +} + /* Icons (inline elements with styled text that mock arrow icons) --------------------------------------------------------------------------------------------------*/ .fc-icon { display: inline-block; - width: 1em; height: 1em; line-height: 1em; font-size: 1em; @@ -99,7 +109,6 @@ NOTE: use percentage font sizes or else old IE chokes .fc-icon:after { position: relative; - margin: 0 -1em; /* ensures character will be centered, regardless of width */ } .fc-icon-left-single-arrow:after { @@ -107,7 +116,6 @@ NOTE: use percentage font sizes or else old IE chokes font-weight: bold; font-size: 200%; top: -7%; - left: 3%; } .fc-icon-right-single-arrow:after { @@ -115,7 +123,6 @@ NOTE: use percentage font sizes or else old IE chokes font-weight: bold; font-size: 200%; top: -7%; - left: -3%; } .fc-icon-left-double-arrow:after { @@ -134,14 +141,12 @@ NOTE: use percentage font sizes or else old IE chokes content: "\25C4"; font-size: 125%; top: 3%; - left: -2%; } .fc-icon-right-triangle:after { content: "\25BA"; font-size: 125%; top: 3%; - left: 2%; } .fc-icon-down-triangle:after { @@ -252,7 +257,6 @@ NOTE: use percentage font sizes or else old IE chokes cursor: default; background-image: none; opacity: 0.65; - filter: alpha(opacity=65); box-shadow: none; } @@ -372,6 +376,7 @@ hr.fc-divider { .fc table { width: 100%; + box-sizing: border-box; /* fix scrollbar issue in firefox */ table-layout: fixed; border-collapse: collapse; border-spacing: 0; @@ -395,6 +400,18 @@ hr.fc-divider { } +/* Internal Nav Links +--------------------------------------------------------------------------------------------------*/ + +a[data-goto] { + cursor: pointer; +} + +a[data-goto]:hover { + text-decoration: underline; +} + + /* Fake Table Rows --------------------------------------------------------------------------------------------------*/ @@ -491,15 +508,15 @@ temporary rendered events). /* Scrolling Container --------------------------------------------------------------------------------------------------*/ -.fc-scroller { /* this class goes on elements for guaranteed vertical scrollbars */ - overflow-y: scroll; - overflow-x: hidden; +.fc-scroller { + -webkit-overflow-scrolling: touch; } -.fc-scroller > * { /* we expect an immediate inner element */ +/* TODO: move to agenda/basic */ +.fc-scroller > .fc-day-grid, +.fc-scroller > .fc-time-grid { position: relative; /* re-scope all positions */ width: 100%; /* hack to force re-sizing this inner element when scrollbars appear/disappear */ - overflow: hidden; /* don't let negative margins or absolute positioning create further scroll */ } @@ -513,10 +530,14 @@ temporary rendered events). line-height: 1.3; border-radius: 3px; border: 1px solid #3a87ad; /* default BORDER color */ - background-color: #3a87ad; /* default BACKGROUND color */ font-weight: normal; /* undo jqui's ui-widget-header bold */ } +.fc-event, +.fc-event-dot { + background-color: #3a87ad; /* default BACKGROUND color */ +} + /* overpower some of bootstrap's and jqui's styles on tags */ .fc-event, .fc-event:hover, @@ -539,7 +560,6 @@ temporary rendered events). z-index: 1; background: #fff; opacity: .25; - filter: alpha(opacity=25); /* for IE */ } .fc-event .fc-content { @@ -547,15 +567,68 @@ temporary rendered events). z-index: 2; } +/* resizer (cursor AND touch devices) */ + .fc-event .fc-resizer { position: absolute; - z-index: 3; + z-index: 4; +} + +/* resizer (touch devices) */ + +.fc-event .fc-resizer { + display: none; +} + +.fc-event.fc-allow-mouse-resize .fc-resizer, +.fc-event.fc-selected .fc-resizer { + /* only show when hovering or selected (with touch) */ + display: block; +} + +/* hit area */ + +.fc-event.fc-selected .fc-resizer:before { + /* 40x40 touch area */ + content: ""; + position: absolute; + z-index: 9999; /* user of this util can scope within a lower z-index */ + top: 50%; + left: 50%; + width: 40px; + height: 40px; + margin-left: -20px; + margin-top: -20px; +} + + +/* Event Selection (only for touch devices) +--------------------------------------------------------------------------------------------------*/ + +.fc-event.fc-selected { + z-index: 9999 !important; /* overcomes inline z-index */ + box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); +} + +.fc-event.fc-selected.fc-dragging { + box-shadow: 0 2px 7px rgba(0, 0, 0, 0.3); } /* Horizontal Events --------------------------------------------------------------------------------------------------*/ +/* bigger touch area when selected */ +.fc-h-event.fc-selected:before { + content: ""; + position: absolute; + z-index: 3; /* below resizers */ + top: -10px; + bottom: -10px; + left: 0; + right: 0; +} + /* events that are continuing to/from another week. kill rounded corners and butt up against edge */ .fc-ltr .fc-h-event.fc-not-start, @@ -576,36 +649,56 @@ temporary rendered events). border-bottom-right-radius: 0; } -/* resizer */ - -.fc-h-event .fc-resizer { /* positioned it to overcome the event's borders */ - top: -1px; - bottom: -1px; - left: -1px; - right: -1px; - width: 5px; -} +/* resizer (cursor AND touch devices) */ /* left resizer */ .fc-ltr .fc-h-event .fc-start-resizer, -.fc-ltr .fc-h-event .fc-start-resizer:before, -.fc-ltr .fc-h-event .fc-start-resizer:after, -.fc-rtl .fc-h-event .fc-end-resizer, -.fc-rtl .fc-h-event .fc-end-resizer:before, -.fc-rtl .fc-h-event .fc-end-resizer:after { - right: auto; /* ignore the right and only use the left */ +.fc-rtl .fc-h-event .fc-end-resizer { cursor: w-resize; + left: -1px; /* overcome border */ } /* right resizer */ .fc-ltr .fc-h-event .fc-end-resizer, -.fc-ltr .fc-h-event .fc-end-resizer:before, -.fc-ltr .fc-h-event .fc-end-resizer:after, -.fc-rtl .fc-h-event .fc-start-resizer, -.fc-rtl .fc-h-event .fc-start-resizer:before, -.fc-rtl .fc-h-event .fc-start-resizer:after { - left: auto; /* ignore the left and only use the right */ +.fc-rtl .fc-h-event .fc-start-resizer { cursor: e-resize; + right: -1px; /* overcome border */ +} + +/* resizer (mouse devices) */ + +.fc-h-event.fc-allow-mouse-resize .fc-resizer { + width: 7px; + top: -1px; /* overcome top border */ + bottom: -1px; /* overcome bottom border */ +} + +/* resizer (touch devices) */ + +.fc-h-event.fc-selected .fc-resizer { + /* 8x8 little dot */ + border-radius: 4px; + border-width: 1px; + width: 6px; + height: 6px; + border-style: solid; + border-color: inherit; + background: #fff; + /* vertically center */ + top: 50%; + margin-top: -4px; +} + +/* left resizer */ +.fc-ltr .fc-h-event.fc-selected .fc-start-resizer, +.fc-rtl .fc-h-event.fc-selected .fc-end-resizer { + margin-left: -4px; /* centers the 8x8 dot on the left edge */ +} + +/* right resizer */ +.fc-ltr .fc-h-event.fc-selected .fc-end-resizer, +.fc-rtl .fc-h-event.fc-selected .fc-start-resizer { + margin-right: -4px; /* centers the 8x8 dot on the right edge */ } @@ -620,6 +713,23 @@ be a descendant of the grid when it is being dragged. padding: 0 1px; } +tr:first-child > td > .fc-day-grid-event { + margin-top: 2px; /* a little bit more space before the first event */ +} + +.fc-day-grid-event.fc-selected:after { + content: ""; + position: absolute; + z-index: 1; /* same z-index as fc-bg, behind text */ + /* overcome the borders */ + top: -1px; + right: -1px; + bottom: -1px; + left: -1px; + /* darkening effect */ + background: #000; + opacity: .25; +} .fc-day-grid-event .fc-content { /* force events to be one-line tall */ white-space: nowrap; @@ -630,10 +740,18 @@ be a descendant of the grid when it is being dragged. font-weight: bold; } -.fc-day-grid-event .fc-resizer { /* enlarge the default hit area */ - left: -3px; - right: -3px; - width: 7px; +/* resizer (cursor devices) */ + +/* left resizer */ +.fc-ltr .fc-day-grid-event.fc-allow-mouse-resize .fc-start-resizer, +.fc-rtl .fc-day-grid-event.fc-allow-mouse-resize .fc-end-resizer { + margin-left: -2px; /* to the day cell's edge */ +} + +/* right resizer */ +.fc-ltr .fc-day-grid-event.fc-allow-mouse-resize .fc-end-resizer, +.fc-rtl .fc-day-grid-event.fc-allow-mouse-resize .fc-start-resizer { + margin-right: -2px; /* to the day cell's edge */ } @@ -672,14 +790,46 @@ a.fc-more:hover { padding: 10px; } + +/* Now Indicator +--------------------------------------------------------------------------------------------------*/ + +.fc-now-indicator { + position: absolute; + border: 0 solid red; +} + + +/* Utilities +--------------------------------------------------------------------------------------------------*/ + +.fc-unselectable { + -webkit-user-select: none; + -khtml-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + -webkit-touch-callout: none; + -webkit-tap-highlight-color: rgba(0, 0, 0, 0); +} + + + /* Toolbar --------------------------------------------------------------------------------------------------*/ .fc-toolbar { text-align: center; +} + +.fc-toolbar.fc-header-toolbar { margin-bottom: 1em; } +.fc-toolbar.fc-footer-toolbar { + margin-top: 1em; +} + .fc-toolbar .fc-left { float: left; } @@ -753,6 +903,8 @@ a.fc-more:hover { z-index: 1; } + + /* BasicView --------------------------------------------------------------------------------------------------*/ @@ -760,8 +912,7 @@ a.fc-more:hover { .fc-basicWeek-view .fc-content-skeleton, .fc-basicDay-view .fc-content-skeleton { - /* we are sure there are no day numbers in these views, so... */ - padding-top: 1px; /* add a pixel to make sure there are 2px padding above events */ + /* there may be week numbers in these views, so no padding-top */ padding-bottom: 1em; /* ensure a space at bottom of cell for user selecting/clicking */ } @@ -784,42 +935,45 @@ a.fc-more:hover { /* week and day number styling */ +.fc-day-top.fc-other-month { + opacity: 0.3; +} + .fc-basic-view .fc-week-number, .fc-basic-view .fc-day-number { - padding: 0 2px; + padding: 2px; } -.fc-basic-view td.fc-week-number span, -.fc-basic-view td.fc-day-number { - padding-top: 2px; - padding-bottom: 2px; +.fc-basic-view th.fc-week-number, +.fc-basic-view th.fc-day-number { + padding: 0 2px; /* column headers can't have as much v space */ } -.fc-basic-view .fc-week-number { +.fc-ltr .fc-basic-view .fc-day-top .fc-day-number { float: right; } +.fc-rtl .fc-basic-view .fc-day-top .fc-day-number { float: left; } + +.fc-ltr .fc-basic-view .fc-day-top .fc-week-number { float: left; border-radius: 0 0 3px 0; } +.fc-rtl .fc-basic-view .fc-day-top .fc-week-number { float: right; border-radius: 0 0 0 3px; } + +.fc-basic-view .fc-day-top .fc-week-number { + min-width: 1.5em; + text-align: center; + background-color: #f2f2f2; + color: #808080; +} + +/* when week/day number have own column */ + +.fc-basic-view td.fc-week-number { text-align: center; } -.fc-basic-view .fc-week-number span { +.fc-basic-view td.fc-week-number > * { /* work around the way we do column resizing and ensure a minimum width */ display: inline-block; min-width: 1.25em; } -.fc-ltr .fc-basic-view .fc-day-number { - text-align: right; -} - -.fc-rtl .fc-basic-view .fc-day-number { - text-align: left; -} - -.fc-day-number.fc-other-month { - opacity: 0.3; - filter: alpha(opacity=30); /* for IE */ - /* opacity with small font can sometimes look too faded - might want to set the 'color' property instead - making day-numbers bold also fixes the problem */ -} /* AgendaView all-day area --------------------------------------------------------------------------------------------------*/ @@ -834,7 +988,6 @@ a.fc-more:hover { } .fc-agenda-view .fc-day-grid .fc-row .fc-content-skeleton { - padding-top: 1px; /* add a pixel to make sure there are 2px padding above events */ padding-bottom: 1em; /* give space underneath events for clicking/selecting days */ } @@ -888,27 +1041,46 @@ a.fc-more:hover { z-index: 2; } -.fc-time-grid .fc-bgevent-skeleton, +.fc-time-grid .fc-content-col { + position: relative; /* because now-indicator lives directly inside */ +} + .fc-time-grid .fc-content-skeleton { position: absolute; + z-index: 3; top: 0; left: 0; right: 0; } -.fc-time-grid .fc-bgevent-skeleton { +/* divs within a cell within the fc-content-skeleton */ + +.fc-time-grid .fc-business-container { + position: relative; + z-index: 1; +} + +.fc-time-grid .fc-bgevent-container { + position: relative; + z-index: 2; +} + +.fc-time-grid .fc-highlight-container { + position: relative; z-index: 3; } -.fc-time-grid .fc-highlight-skeleton { +.fc-time-grid .fc-event-container { + position: relative; z-index: 4; } -.fc-time-grid .fc-content-skeleton { +.fc-time-grid .fc-now-indicator-line { z-index: 5; } -.fc-time-grid .fc-helper-skeleton { +.fc-time-grid .fc-helper-container { /* also is fc-event-container */ + position: relative; z-index: 6; } @@ -948,11 +1120,6 @@ a.fc-more:hover { /* TimeGrid Event Containment --------------------------------------------------------------------------------------------------*/ -.fc-time-grid .fc-event-container, /* a div within a cell within the fc-content-skeleton */ -.fc-time-grid .fc-bgevent-container { /* a div within a cell within the fc-bgevent-skeleton */ - position: relative; -} - .fc-ltr .fc-time-grid .fc-event-container { /* space on the sides of events for LTR (default) */ margin: 0 2.5% 0 2px; } @@ -1008,6 +1175,20 @@ be a descendant of the grid when it is being dragged. overflow: hidden; /* don't let the bg flow over rounded corners */ } +.fc-time-grid-event.fc-selected { + /* need to allow touch resizers to extend outside event's bounding box */ + /* common fc-selected styles hide the fc-bg, so don't need this anyway */ + overflow: visible; +} + +.fc-time-grid-event.fc-selected .fc-bg { + display: none; /* hide semi-white background, to appear darker */ +} + +.fc-time-grid-event .fc-content { + overflow: hidden; /* for when .fc-selected */ +} + .fc-time-grid-event .fc-time, .fc-time-grid-event .fc-title { padding: 0 1px; @@ -1049,9 +1230,9 @@ be a descendant of the grid when it is being dragged. padding: 0; /* undo padding from above */ } -/* resizer */ +/* resizer (cursor device) */ -.fc-time-grid-event .fc-resizer { +.fc-time-grid-event.fc-allow-mouse-resize .fc-resizer { left: 0; right: 0; bottom: 0; @@ -1064,6 +1245,169 @@ be a descendant of the grid when it is being dragged. cursor: s-resize; } -.fc-time-grid-event .fc-resizer:after { +.fc-time-grid-event.fc-allow-mouse-resize .fc-resizer:after { content: "="; } + +/* resizer (touch device) */ + +.fc-time-grid-event.fc-selected .fc-resizer { + /* 10x10 dot */ + border-radius: 5px; + border-width: 1px; + width: 8px; + height: 8px; + border-style: solid; + border-color: inherit; + background: #fff; + /* horizontally center */ + left: 50%; + margin-left: -5px; + /* center on the bottom edge */ + bottom: -5px; +} + + +/* Now Indicator +--------------------------------------------------------------------------------------------------*/ + +.fc-time-grid .fc-now-indicator-line { + border-top-width: 1px; + left: 0; + right: 0; +} + +/* arrow on axis */ + +.fc-time-grid .fc-now-indicator-arrow { + margin-top: -5px; /* vertically center on top coordinate */ +} + +.fc-ltr .fc-time-grid .fc-now-indicator-arrow { + left: 0; + /* triangle pointing right... */ + border-width: 5px 0 5px 6px; + border-top-color: transparent; + border-bottom-color: transparent; +} + +.fc-rtl .fc-time-grid .fc-now-indicator-arrow { + right: 0; + /* triangle pointing left... */ + border-width: 5px 6px 5px 0; + border-top-color: transparent; + border-bottom-color: transparent; +} + + + +/* List View +--------------------------------------------------------------------------------------------------*/ + +/* possibly reusable */ + +.fc-event-dot { + display: inline-block; + width: 10px; + height: 10px; + border-radius: 5px; +} + +/* view wrapper */ + +.fc-rtl .fc-list-view { + direction: rtl; /* unlike core views, leverage browser RTL */ +} + +.fc-list-view { + border-width: 1px; + border-style: solid; +} + +/* table resets */ + +.fc .fc-list-table { + table-layout: auto; /* for shrinkwrapping cell content */ +} + +.fc-list-table td { + border-width: 1px 0 0; + padding: 8px 14px; +} + +.fc-list-table tr:first-child td { + border-top-width: 0; +} + +/* day headings with the list */ + +.fc-list-heading { + border-bottom-width: 1px; +} + +.fc-list-heading td { + font-weight: bold; +} + +.fc-ltr .fc-list-heading-main { float: left; } +.fc-ltr .fc-list-heading-alt { float: right; } + +.fc-rtl .fc-list-heading-main { float: right; } +.fc-rtl .fc-list-heading-alt { float: left; } + +/* event list items */ + +.fc-list-item.fc-has-url { + cursor: pointer; /* whole row will be clickable */ +} + +.fc-list-item:hover td { + background-color: #f5f5f5; +} + +.fc-list-item-marker, +.fc-list-item-time { + white-space: nowrap; + width: 1px; +} + +/* make the dot closer to the event title */ +.fc-ltr .fc-list-item-marker { padding-right: 0; } +.fc-rtl .fc-list-item-marker { padding-left: 0; } + +.fc-list-item-title a { + /* every event title cell has an tag */ + text-decoration: none; + color: inherit; +} + +.fc-list-item-title a[href]:hover { + /* hover effect only on titles with hrefs */ + text-decoration: underline; +} + +/* message when no events */ + +.fc-list-empty-wrap2 { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; +} + +.fc-list-empty-wrap1 { + width: 100%; + height: 100%; + display: table; +} + +.fc-list-empty { + display: table-cell; + vertical-align: middle; + text-align: center; +} + +.fc-unthemed .fc-list-empty { /* theme will provide own background */ + background-color: #eee; +} diff --git a/src/UI/Handlebars/Helpers/Album.js b/src/UI/Handlebars/Helpers/Album.js index 309d1e267..4e4c85372 100644 --- a/src/UI/Handlebars/Helpers/Album.js +++ b/src/UI/Handlebars/Helpers/Album.js @@ -20,7 +20,36 @@ Handlebars.registerHelper('cover', function() { return new Handlebars.SafeString(''.format(placeholder)); }); +Handlebars.registerHelper('StatusLevel', function() { + var hasFile = false; //this.hasFile; #TODO Refactor for Albums + var downloading = false; //require('../../Activity/Queue/QueueCollection').findEpisode(this.id) || this.downloading; #TODO Queue Refactor for Albums + var currentTime = moment(); + var start = moment(this.releaseDate); + var end = moment(this.end); + var monitored = this.artist.monitored && this.monitored; + if (hasFile) { + return 'success'; + } + + if (downloading) { + return 'purple'; + } + + else if (!monitored) { + return 'unmonitored'; + } + + if (currentTime.isAfter(start) && currentTime.isBefore(end)) { + return 'warning'; + } + + if (start.isBefore(currentTime) && !hasFile) { + return 'danger'; + } + + return 'primary'; +}); Handlebars.registerHelper('MBAlbumUrl', function() { return 'https://musicbrainz.org/release-group/' + this.mbId; diff --git a/src/UI/Handlebars/Helpers/Episode.js b/src/UI/Handlebars/Helpers/Episode.js index 154236489..3bd821b66 100644 --- a/src/UI/Handlebars/Helpers/Episode.js +++ b/src/UI/Handlebars/Helpers/Episode.js @@ -14,40 +14,7 @@ Handlebars.registerHelper('EpisodeNumber', function() { } }); -Handlebars.registerHelper('StatusLevel', function() { - var hasFile = this.hasFile; - var downloading = require('../../Activity/Queue/QueueCollection').findEpisode(this.id) || this.downloading; - var currentTime = moment(); - var start = moment(this.airDateUtc); - var end = moment(this.end); - var monitored = this.series.monitored && this.monitored; - if (hasFile) { - return 'success'; - } - - if (downloading) { - return 'purple'; - } - - else if (!monitored) { - return 'unmonitored'; - } - - if (this.episodeNumber === 1) { - return 'premiere'; - } - - if (currentTime.isAfter(start) && currentTime.isBefore(end)) { - return 'warning'; - } - - if (start.isBefore(currentTime) && !hasFile) { - return 'danger'; - } - - return 'primary'; -}); Handlebars.registerHelper('EpisodeProgressClass', function() { if (this.episodeFileCount === this.episodeCount) { diff --git a/src/UI/JsLibraries/fullcalendar.js b/src/UI/JsLibraries/fullcalendar.js index 7cd7aca2e..d4c97f0dc 100644 --- a/src/UI/JsLibraries/fullcalendar.js +++ b/src/UI/JsLibraries/fullcalendar.js @@ -1,7 +1,7 @@ /*! - * FullCalendar v2.3.2 - * Docs & License: http://fullcalendar.io/ - * (c) 2015 Adam Shaw + * FullCalendar v3.4.0 + * Docs & License: https://fullcalendar.io/ + * (c) 2017 Adam Shaw */ (function(factory) { @@ -18,8 +18,14 @@ ;; -var fc = $.fullCalendar = { version: "2.3.2" }; -var fcViews = fc.views = {}; +var FC = $.fullCalendar = { + version: "3.4.0", + // When introducing internal API incompatibilities (where fullcalendar plugins would break), + // the minor version of the calendar should be upped (ex: 2.7.2 -> 2.8.0) + // and the below integer should be incremented. + internalApiVersion: 9 +}; +var fcViews = FC.views = {}; $.fn.fullCalendar = function(options) { @@ -50,13 +56,14 @@ $.fn.fullCalendar = function(options) { calendar.render(); } }); - + return res; }; var complexOptions = [ // names of options that are objects whose properties should be combined 'header', + 'footer', 'buttonText', 'buttonIcons', 'themeButtonIcons' @@ -68,67 +75,17 @@ function mergeOptions(optionObjs) { return mergeProps(optionObjs, complexOptions); } - -// Given options specified for the calendar's constructor, massages any legacy options into a non-legacy form. -// Converts View-Option-Hashes into the View-Specific-Options format. -function massageOverrides(input) { - var overrides = { views: input.views || {} }; // the output. ensure a `views` hash - var subObj; - - // iterate through all option override properties (except `views`) - $.each(input, function(name, val) { - if (name != 'views') { - - // could the value be a legacy View-Option-Hash? - if ( - $.isPlainObject(val) && - !/(time|duration|interval)$/i.test(name) && // exclude duration options. might be given as objects - $.inArray(name, complexOptions) == -1 // complex options aren't allowed to be View-Option-Hashes - ) { - subObj = null; - - // iterate through the properties of this possible View-Option-Hash value - $.each(val, function(subName, subVal) { - - // is the property targeting a view? - if (/^(month|week|day|default|basic(Week|Day)?|agenda(Week|Day)?)$/.test(subName)) { - if (!overrides.views[subName]) { // ensure the view-target entry exists - overrides.views[subName] = {}; - } - overrides.views[subName][name] = subVal; // record the value in the `views` object - } - else { // a non-View-Option-Hash property - if (!subObj) { - subObj = {}; - } - subObj[subName] = subVal; // accumulate these unrelated values for later - } - }); - - if (subObj) { // non-View-Option-Hash properties? transfer them as-is - overrides[name] = subObj; - } - } - else { - overrides[name] = val; // transfer normal options as-is - } - } - }); - - return overrides; -} - ;; // exports -fc.intersectionToSeg = intersectionToSeg; -fc.applyAll = applyAll; -fc.debounce = debounce; -fc.isInt = isInt; -fc.htmlEscape = htmlEscape; -fc.cssToStr = cssToStr; -fc.proxy = proxy; -fc.capitaliseFirstLetter = capitaliseFirstLetter; +FC.intersectRanges = intersectRanges; +FC.applyAll = applyAll; +FC.debounce = debounce; +FC.isInt = isInt; +FC.htmlEscape = htmlEscape; +FC.cssToStr = cssToStr; +FC.proxy = proxy; +FC.capitaliseFirstLetter = capitaliseFirstLetter; /* FullCalendar-specific DOM Utilities @@ -259,34 +216,31 @@ function matchCellWidths(els) { } -// Turns a container element into a scroller if its contents is taller than the allotted height. -// Returns true if the element is now a scroller, false otherwise. -// NOTE: this method is best because it takes weird zooming dimensions into account -function setPotentialScroller(containerEl, height) { - containerEl.height(height).addClass('fc-scroller'); +// Given one element that resides inside another, +// Subtracts the height of the inner element from the outer element. +function subtractInnerElHeight(outerEl, innerEl) { + var both = outerEl.add(innerEl); + var diff; - // are scrollbars needed? - if (containerEl[0].scrollHeight - 1 > containerEl[0].clientHeight) { // !!! -1 because IE is often off-by-one :( - return true; - } + // effin' IE8/9/10/11 sometimes returns 0 for dimensions. this weird hack was the only thing that worked + both.css({ + position: 'relative', // cause a reflow, which will force fresh dimension recalculation + left: -1 // ensure reflow in case the el was already relative. negative is less likely to cause new scroll + }); + diff = outerEl.outerHeight() - innerEl.outerHeight(); // grab the dimensions + both.css({ position: '', left: '' }); // undo hack - unsetScroller(containerEl); // undo - return false; + return diff; } -// Takes an element that might have been a scroller, and turns it back into a normal element. -function unsetScroller(containerEl) { - containerEl.height('').removeClass('fc-scroller'); -} - - -/* General DOM Utilities +/* Element Geom Utilities ----------------------------------------------------------------------------------------------------------------------*/ -fc.getClientRect = getClientRect; -fc.getContentRect = getContentRect; -fc.getScrollbarWidths = getScrollbarWidths; +FC.getOuterRect = getOuterRect; +FC.getClientRect = getClientRect; +FC.getContentRect = getContentRect; +FC.getScrollbarWidths = getScrollbarWidths; // borrowed from https://github.com/jquery/jquery-ui/blob/1.11.0/ui/core.js#L51 @@ -305,26 +259,31 @@ function getScrollParent(el) { // Queries the outer bounding area of a jQuery element. // Returns a rectangle with absolute coordinates: left, right (exclusive), top, bottom (exclusive). -function getOuterRect(el) { +// Origin is optional. +function getOuterRect(el, origin) { var offset = el.offset(); + var left = offset.left - (origin ? origin.left : 0); + var top = offset.top - (origin ? origin.top : 0); return { - left: offset.left, - right: offset.left + el.outerWidth(), - top: offset.top, - bottom: offset.top + el.outerHeight() + left: left, + right: left + el.outerWidth(), + top: top, + bottom: top + el.outerHeight() }; } // Queries the area within the margin/border/scrollbars of a jQuery element. Does not go within the padding. // Returns a rectangle with absolute coordinates: left, right (exclusive), top, bottom (exclusive). +// Origin is optional. +// WARNING: given element can't have borders // NOTE: should use clientLeft/clientTop, but very unreliable cross-browser. -function getClientRect(el) { +function getClientRect(el, origin) { var offset = el.offset(); var scrollbarWidths = getScrollbarWidths(el); - var left = offset.left + getCssFloat(el, 'border-left-width') + scrollbarWidths.left; - var top = offset.top + getCssFloat(el, 'border-top-width') + scrollbarWidths.top; + var left = offset.left + getCssFloat(el, 'border-left-width') + scrollbarWidths.left - (origin ? origin.left : 0); + var top = offset.top + getCssFloat(el, 'border-top-width') + scrollbarWidths.top - (origin ? origin.top : 0); return { left: left, @@ -337,10 +296,13 @@ function getClientRect(el) { // Queries the area within the margin/border/padding of a jQuery element. Assumed not to have scrollbars. // Returns a rectangle with absolute coordinates: left, right (exclusive), top, bottom (exclusive). -function getContentRect(el) { +// Origin is optional. +function getContentRect(el, origin) { var offset = el.offset(); // just outside of border, margin not included - var left = offset.left + getCssFloat(el, 'border-left-width') + getCssFloat(el, 'padding-left'); - var top = offset.top + getCssFloat(el, 'border-top-width') + getCssFloat(el, 'padding-top'); + var left = offset.left + getCssFloat(el, 'border-left-width') + getCssFloat(el, 'padding-left') - + (origin ? origin.left : 0); + var top = offset.top + getCssFloat(el, 'border-top-width') + getCssFloat(el, 'padding-top') - + (origin ? origin.top : 0); return { left: left, @@ -352,15 +314,17 @@ function getContentRect(el) { // Returns the computed left/right/top/bottom scrollbar widths for the given jQuery element. +// WARNING: given element can't have borders (which will cause offsetWidth/offsetHeight to be larger). // NOTE: should use clientLeft/clientTop, but very unreliable cross-browser. function getScrollbarWidths(el) { - var leftRightWidth = el.innerWidth() - el[0].clientWidth; // the paddings cancel out, leaving the scrollbars - var widths = { - left: 0, - right: 0, - top: 0, - bottom: el.innerHeight() - el[0].clientHeight // the paddings cancel out, leaving the bottom scrollbar - }; + var leftRightWidth = el[0].offsetWidth - el[0].clientWidth; + var bottomWidth = el[0].offsetHeight - el[0].clientHeight; + var widths; + + leftRightWidth = sanitizeScrollbarWidth(leftRightWidth); + bottomWidth = sanitizeScrollbarWidth(bottomWidth); + + widths = { left: 0, right: 0, top: 0, bottom: bottomWidth }; if (getIsLeftRtlScrollbars() && el.css('direction') == 'rtl') { // is the scrollbar on the left side? widths.left = leftRightWidth; @@ -373,6 +337,15 @@ function getScrollbarWidths(el) { } +// The scrollbar width computations in getScrollbarWidths are sometimes flawed when it comes to +// retina displays, rounding, and IE11. Massage them into a usable value. +function sanitizeScrollbarWidth(width) { + width = Math.max(0, width); // no negatives + width = Math.round(width); + return width; +} + + // Logic for determining if, when the element is right-to-left, the scrollbar appears on the left side var _isLeftRtlScrollbars = null; @@ -410,15 +383,71 @@ function getCssFloat(el, prop) { } +/* Mouse / Touch Utilities +----------------------------------------------------------------------------------------------------------------------*/ + +FC.preventDefault = preventDefault; + + // Returns a boolean whether this was a left mouse click and no ctrl key (which means right click on Mac) function isPrimaryMouseButton(ev) { return ev.which == 1 && !ev.ctrlKey; } -/* Geometry +function getEvX(ev) { + var touches = ev.originalEvent.touches; + + // on mobile FF, pageX for touch events is present, but incorrect, + // so, look at touch coordinates first. + if (touches && touches.length) { + return touches[0].pageX; + } + + return ev.pageX; +} + + +function getEvY(ev) { + var touches = ev.originalEvent.touches; + + // on mobile FF, pageX for touch events is present, but incorrect, + // so, look at touch coordinates first. + if (touches && touches.length) { + return touches[0].pageY; + } + + return ev.pageY; +} + + +function getEvIsTouch(ev) { + return /^touch/.test(ev.type); +} + + +function preventSelection(el) { + el.addClass('fc-unselectable') + .on('selectstart', preventDefault); +} + + +function allowSelection(el) { + el.removeClass('fc-unselectable') + .off('selectstart', preventDefault); +} + + +// Stops a mouse/touch event from doing it's native browser action +function preventDefault(ev) { + ev.preventDefault(); +} + + +/* General Geometry Utils ----------------------------------------------------------------------------------------------------------------------*/ +FC.intersectRects = intersectRects; // Returns a new rectangle that is the intersection of the two rectangles. If they don't intersect, returns false function intersectRects(rect1, rect2) { @@ -463,14 +492,99 @@ function diffPoints(point1, point2) { } +/* Object Ordering by Field +----------------------------------------------------------------------------------------------------------------------*/ + +FC.parseFieldSpecs = parseFieldSpecs; +FC.compareByFieldSpecs = compareByFieldSpecs; +FC.compareByFieldSpec = compareByFieldSpec; +FC.flexibleCompare = flexibleCompare; + + +function parseFieldSpecs(input) { + var specs = []; + var tokens = []; + var i, token; + + if (typeof input === 'string') { + tokens = input.split(/\s*,\s*/); + } + else if (typeof input === 'function') { + tokens = [ input ]; + } + else if ($.isArray(input)) { + tokens = input; + } + + for (i = 0; i < tokens.length; i++) { + token = tokens[i]; + + if (typeof token === 'string') { + specs.push( + token.charAt(0) == '-' ? + { field: token.substring(1), order: -1 } : + { field: token, order: 1 } + ); + } + else if (typeof token === 'function') { + specs.push({ func: token }); + } + } + + return specs; +} + + +function compareByFieldSpecs(obj1, obj2, fieldSpecs) { + var i; + var cmp; + + for (i = 0; i < fieldSpecs.length; i++) { + cmp = compareByFieldSpec(obj1, obj2, fieldSpecs[i]); + if (cmp) { + return cmp; + } + } + + return 0; +} + + +function compareByFieldSpec(obj1, obj2, fieldSpec) { + if (fieldSpec.func) { + return fieldSpec.func(obj1, obj2); + } + return flexibleCompare(obj1[fieldSpec.field], obj2[fieldSpec.field]) * + (fieldSpec.order || 1); +} + + +function flexibleCompare(a, b) { + if (!a && !b) { + return 0; + } + if (b == null) { + return -1; + } + if (a == null) { + return 1; + } + if ($.type(a) === 'string' || $.type(b) === 'string') { + return String(a).localeCompare(String(b)); + } + return a - b; +} + + /* FullCalendar-specific Misc Utilities ----------------------------------------------------------------------------------------------------------------------*/ -// Creates a basic segment with the intersection of the two ranges. Returns undefined if no intersection. +// Computes the intersection of the two ranges. Will return fresh date clones in a range. +// Returns undefined if no intersection. // Expects all dates to be normalized to the same timezone beforehand. // TODO: move to date section? -function intersectionToSeg(subjectRange, constraintRange) { +function intersectRanges(subjectRange, constraintRange) { var subjectStart = subjectRange.start; var subjectEnd = subjectRange.end; var constraintStart = constraintRange.start; @@ -511,11 +625,14 @@ function intersectionToSeg(subjectRange, constraintRange) { /* Date Utilities ----------------------------------------------------------------------------------------------------------------------*/ -fc.computeIntervalUnit = computeIntervalUnit; -fc.durationHasTime = durationHasTime; +FC.computeGreatestUnit = computeGreatestUnit; +FC.divideRangeByDuration = divideRangeByDuration; +FC.divideDurationByDuration = divideDurationByDuration; +FC.multiplyDuration = multiplyDuration; +FC.durationHasTime = durationHasTime; var dayIDs = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ]; -var intervalUnits = [ 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond' ]; +var unitsDesc = [ 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond' ]; // descending // Diffs the two moments into a Duration where full-days are recorded first, then the remaining time. @@ -548,12 +665,12 @@ function diffByUnit(a, b, unit) { // Computes the unit name of the largest whole-unit period of time. // For example, 48 hours will be "days" whereas 49 hours will be "hours". // Accepts start/end, a range object, or an original duration object. -function computeIntervalUnit(start, end) { +function computeGreatestUnit(start, end) { var i, unit; var val; - for (i = 0; i < intervalUnits.length; i++) { - unit = intervalUnits[i]; + for (i = 0; i < unitsDesc.length; i++) { + unit = unitsDesc[i]; val = computeRangeAs(unit, start, end); if (val >= 1 && isInt(val)) { @@ -565,6 +682,19 @@ function computeIntervalUnit(start, end) { } +// like computeGreatestUnit, but has special abilities to interpret the source input for clues +function computeDurationGreatestUnit(duration, durationInput) { + var unit = computeGreatestUnit(duration); + + // prevent days:7 from being interpreted as a week + if (unit === 'week' && typeof durationInput === 'object' && durationInput.days) { + unit = 'day'; + } + + return unit; +} + + // Computes the number of units (like "hours") in the given range. // Range can be a {start,end} object, separate start/end args, or a Duration. // Results are based on Moment's .as() and .diff() methods, so results can depend on internal handling @@ -583,6 +713,137 @@ function computeRangeAs(unit, start, end) { } +// Intelligently divides a range (specified by a start/end params) by a duration +function divideRangeByDuration(start, end, dur) { + var months; + + if (durationHasTime(dur)) { + return (end - start) / dur; + } + months = dur.asMonths(); + if (Math.abs(months) >= 1 && isInt(months)) { + return end.diff(start, 'months', true) / months; + } + return end.diff(start, 'days', true) / dur.asDays(); +} + + +// Intelligently divides one duration by another +function divideDurationByDuration(dur1, dur2) { + var months1, months2; + + if (durationHasTime(dur1) || durationHasTime(dur2)) { + return dur1 / dur2; + } + months1 = dur1.asMonths(); + months2 = dur2.asMonths(); + if ( + Math.abs(months1) >= 1 && isInt(months1) && + Math.abs(months2) >= 1 && isInt(months2) + ) { + return months1 / months2; + } + return dur1.asDays() / dur2.asDays(); +} + + +// Intelligently multiplies a duration by a number +function multiplyDuration(dur, n) { + var months; + + if (durationHasTime(dur)) { + return moment.duration(dur * n); + } + months = dur.asMonths(); + if (Math.abs(months) >= 1 && isInt(months)) { + return moment.duration({ months: months * n }); + } + return moment.duration({ days: dur.asDays() * n }); +} + + +function cloneRange(range) { + return { + start: range.start.clone(), + end: range.end.clone() + }; +} + + +// Trims the beginning and end of inner range to be completely within outerRange. +// Returns a new range object. +function constrainRange(innerRange, outerRange) { + innerRange = cloneRange(innerRange); + + if (outerRange.start) { + // needs to be inclusively before outerRange's end + innerRange.start = constrainDate(innerRange.start, outerRange); + } + + if (outerRange.end) { + innerRange.end = minMoment(innerRange.end, outerRange.end); + } + + return innerRange; +} + + +// If the given date is not within the given range, move it inside. +// (If it's past the end, make it one millisecond before the end). +// Always returns a new moment. +function constrainDate(date, range) { + date = date.clone(); + + if (range.start) { + date = maxMoment(date, range.start); + } + + if (range.end && date >= range.end) { + date = range.end.clone().subtract(1); + } + + return date; +} + + +function isDateWithinRange(date, range) { + return (!range.start || date >= range.start) && + (!range.end || date < range.end); +} + + +// TODO: deal with repeat code in intersectRanges +// constraintRange can have unspecified start/end, an open-ended range. +function doRangesIntersect(subjectRange, constraintRange) { + return (!constraintRange.start || subjectRange.end >= constraintRange.start) && + (!constraintRange.end || subjectRange.start < constraintRange.end); +} + + +function isRangeWithinRange(innerRange, outerRange) { + return (!outerRange.start || innerRange.start >= outerRange.start) && + (!outerRange.end || innerRange.end <= outerRange.end); +} + + +function isRangesEqual(range0, range1) { + return ((range0.start && range1.start && range0.start.isSame(range1.start)) || (!range0.start && !range1.start)) && + ((range0.end && range1.end && range0.end.isSame(range1.end)) || (!range0.end && !range1.end)); +} + + +// Returns the moment that's earlier in time. Always a copy. +function minMoment(mom1, mom2) { + return (mom1.isBefore(mom2) ? mom1 : mom2).clone(); +} + + +// Returns the moment that's later in time. Always a copy. +function maxMoment(mom1, mom2) { + return (mom1.isAfter(mom2) ? mom1 : mom2).clone(); +} + + // Returns a boolean about whether the given duration has any time parts (hours/minutes/seconds/ms) function durationHasTime(dur) { return Boolean(dur.hours() || dur.minutes() || dur.seconds() || dur.milliseconds()); @@ -600,6 +861,29 @@ function isTimeString(str) { } +/* Logging and Debug +----------------------------------------------------------------------------------------------------------------------*/ + +FC.log = function() { + var console = window.console; + + if (console && console.log) { + return console.log.apply(console, arguments); + } +}; + +FC.warn = function() { + var console = window.console; + + if (console && console.warn) { + return console.warn.apply(console, arguments); + } + else { + return FC.log.apply(FC, arguments); + } +}; + + /* General Utilities ----------------------------------------------------------------------------------------------------------------------*/ @@ -661,6 +945,7 @@ function createObject(proto) { f.prototype = proto; return new f(); } +FC.createObject = createObject; function copyOwnProps(src, dest) { @@ -672,22 +957,6 @@ function copyOwnProps(src, dest) { } -// Copies over certain methods with the same names as Object.prototype methods. Overcomes an IE<=8 bug: -// https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug -function copyNativeMethods(src, dest) { - var names = [ 'constructor', 'toString', 'valueOf' ]; - var i, name; - - for (i = 0; i < names.length; i++) { - name = names[i]; - - if (src[name] !== Object.prototype[name]) { - dest[name] = src[name]; - } - } -} - - function hasOwnProp(obj, name) { return hasOwnPropMethod.call(obj, name); } @@ -753,6 +1022,21 @@ function cssToStr(cssProps) { } +// Given an object hash of HTML attribute names to values, +// generates a string that can be injected between < > in HTML +function attrsToStr(attrs) { + var parts = []; + + $.each(attrs, function(name, val) { + if (val != null) { + parts.push(name + '="' + htmlEscape(val) + '"'); + } + }); + + return parts.join(' '); +} + + function capitaliseFirstLetter(str) { return str.charAt(0).toUpperCase() + str.slice(1); } @@ -782,22 +1066,21 @@ function proxy(obj, methodName) { // Returns a function, that, as long as it continues to be invoked, will not // be triggered. The function will be called after it stops being called for -// N milliseconds. +// N milliseconds. If `immediate` is passed, trigger the function on the +// leading edge, instead of the trailing. // https://github.com/jashkenas/underscore/blob/1.6.0/underscore.js#L714 -function debounce(func, wait) { - var timeoutId; - var args; - var context; - var timestamp; // of most recent call +function debounce(func, wait, immediate) { + var timeout, args, context, timestamp, result; + var later = function() { var last = +new Date() - timestamp; - if (last < wait && last > 0) { - timeoutId = setTimeout(later, wait - last); + if (last < wait) { + timeout = setTimeout(later, wait - last); } else { - timeoutId = null; - func.apply(context, args); - if (!timeoutId) { + timeout = null; + if (!immediate) { + result = func.apply(context, args); context = args = null; } } @@ -807,22 +1090,38 @@ function debounce(func, wait) { context = this; args = arguments; timestamp = +new Date(); - if (!timeoutId) { - timeoutId = setTimeout(later, wait); + var callNow = immediate && !timeout; + if (!timeout) { + timeout = setTimeout(later, wait); } + if (callNow) { + result = func.apply(context, args); + context = args = null; + } + return result; }; } ;; +/* +GENERAL NOTE on moments throughout the *entire rest* of the codebase: +All moments are assumed to be ambiguously-zoned unless otherwise noted, +with the NOTABLE EXCEOPTION of start/end dates that live on *Event Objects*. +Ambiguously-TIMED moments are assumed to be ambiguously-zoned by nature. +*/ + var ambigDateOfMonthRegex = /^\s*\d{4}-\d\d$/; var ambigTimeOrZoneRegex = /^\s*\d{4}-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d+)?)?)?)?)?$/; var newMomentProto = moment.fn; // where we will attach our new methods var oldMomentProto = $.extend({}, newMomentProto); // copy of original moment methods -var allowValueOptimization; -var setUTCValues; // function defined below -var setLocalValues; // function defined below + +// tell momentjs to transfer these properties upon clone +var momentProperties = moment.momentProperties; +momentProperties.push('_fullCalendar'); +momentProperties.push('_ambigTime'); +momentProperties.push('_ambigZone'); // Creating @@ -832,12 +1131,12 @@ var setLocalValues; // function defined below // extra features (ambiguous time, enhanced formatting). When given an existing moment, // it will function as a clone (and retain the zone of the moment). Anything else will // result in a moment in the local zone. -fc.moment = function() { +FC.moment = function() { return makeMoment(arguments); }; -// Sames as fc.moment, but forces the resulting moment to be in the UTC timezone. -fc.moment.utc = function() { +// Sames as FC.moment, but forces the resulting moment to be in the UTC timezone. +FC.moment.utc = function() { var mom = makeMoment(arguments, true); // Force it into UTC because makeMoment doesn't guarantee it @@ -849,9 +1148,9 @@ fc.moment.utc = function() { return mom; }; -// Same as fc.moment, but when given an ISO8601 string, the timezone offset is preserved. +// Same as FC.moment, but when given an ISO8601 string, the timezone offset is preserved. // ISO8601 strings with no timezone offset will become ambiguously zoned. -fc.moment.parseZone = function() { +FC.moment.parseZone = function() { return makeMoment(arguments, true, true); }; @@ -868,12 +1167,8 @@ function makeMoment(args, parseAsUTC, parseZone) { var ambigMatch; var mom; - if (moment.isMoment(input)) { - mom = moment.apply(null, args); // clone it - transferAmbigs(input, mom); // the ambig flags weren't transfered with the clone - } - else if (isNativeDate(input) || input === undefined) { - mom = moment.apply(null, args); // will be local + if (moment.isMoment(input) || isNativeDate(input) || input === undefined) { + mom = moment.apply(null, args); } else { // "parsing" is required isAmbigTime = false; @@ -914,12 +1209,7 @@ function makeMoment(args, parseAsUTC, parseZone) { mom._ambigZone = true; } else if (isSingleString) { - if (mom.utcOffset) { - mom.utcOffset(input); // if not a valid zone, will assign UTC - } - else { - mom.zone(input); // for moment-pre-2.9 - } + mom.utcOffset(input); // if not a valid zone, will assign UTC } } } @@ -930,21 +1220,6 @@ function makeMoment(args, parseAsUTC, parseZone) { } -// A clone method that works with the flags related to our enhanced functionality. -// In the future, use moment.momentProperties -newMomentProto.clone = function() { - var mom = oldMomentProto.clone.apply(this, arguments); - - // these flags weren't transfered with the clone - transferAmbigs(this, mom); - if (this._fullCalendar) { - mom._fullCalendar = true; - } - - return mom; -}; - - // Week Number // ------------------------------------------------------------------------------------------------- @@ -952,8 +1227,7 @@ newMomentProto.clone = function() { // Returns the week number, considering the locale's custom week number calcuation // `weeks` is an alias for `week` newMomentProto.week = newMomentProto.weeks = function(input) { - var weekCalc = (this._locale || this._lang) // works pre-moment-2.8 - ._fullCalendar_weekCalc; + var weekCalc = this._locale._fullCalendar_weekCalc; if (input == null && typeof weekCalc === 'function') { // custom function only works for getter return weekCalc(this); @@ -1020,19 +1294,21 @@ newMomentProto.time = function(time) { // but preserving its YMD. A moment with a stripped time will display no time // nor timezone offset when .format() is called. newMomentProto.stripTime = function() { - var a; if (!this._ambigTime) { - // get the values before any conversion happens - a = this.toArray(); // array of y/m/d/h/m/s/ms + this.utc(true); // keepLocalTime=true (for keeping *date* value) - // TODO: use keepLocalTime in the future - this.utc(); // set the internal UTC flag (will clear the ambig flags) - setUTCValues(this, a.slice(0, 3)); // set the year/month/date. time will be zero + // set time to zero + this.set({ + hours: 0, + minutes: 0, + seconds: 0, + ms: 0 + }); // Mark the time as ambiguous. This needs to happen after the .utc() call, which might call .utcOffset(), - // which clears all ambig flags. Same with setUTCValues with moment-timezone. + // which clears all ambig flags. this._ambigTime = true; this._ambigZone = true; // if ambiguous time, also ambiguous timezone offset } @@ -1052,24 +1328,20 @@ newMomentProto.hasTime = function() { // Converts the moment to UTC, stripping out its timezone offset, but preserving its // YMD and time-of-day. A moment with a stripped timezone offset will display no // timezone offset when .format() is called. -// TODO: look into Moment's keepLocalTime functionality newMomentProto.stripZone = function() { - var a, wasAmbigTime; + var wasAmbigTime; if (!this._ambigZone) { - // get the values before any conversion happens - a = this.toArray(); // array of y/m/d/h/m/s/ms wasAmbigTime = this._ambigTime; - this.utc(); // set the internal UTC flag (might clear the ambig flags, depending on Moment internals) - setUTCValues(this, a); // will set the year/month/date/hours/minutes/seconds/ms + this.utc(true); // keepLocalTime=true (for keeping date and time values) // the above call to .utc()/.utcOffset() unfortunately might clear the ambig flags, so restore this._ambigTime = wasAmbigTime || false; // Mark the zone as ambiguous. This needs to happen after the .utc() call, which might call .utcOffset(), - // which clears the ambig flags. Same with setUTCValues with moment-timezone. + // which clears the ambig flags. this._ambigZone = true; } @@ -1082,32 +1354,26 @@ newMomentProto.hasZone = function() { }; -// this method implicitly marks a zone -newMomentProto.local = function() { - var a = this.toArray(); // year,month,date,hours,minutes,seconds,ms as an array - var wasAmbigZone = this._ambigZone; +// implicitly marks a zone +newMomentProto.local = function(keepLocalTime) { - oldMomentProto.local.apply(this, arguments); + // for when converting from ambiguously-zoned to local, + // keep the time values when converting from UTC -> local + oldMomentProto.local.call(this, this._ambigZone || keepLocalTime); // ensure non-ambiguous // this probably already happened via local() -> utcOffset(), but don't rely on Moment's internals this._ambigTime = false; this._ambigZone = false; - if (wasAmbigZone) { - // If the moment was ambiguously zoned, the date fields were stored as UTC. - // We want to preserve these, but in local time. - // TODO: look into Moment's keepLocalTime functionality - setLocalValues(this, a); - } - return this; // for chaining }; // implicitly marks a zone -newMomentProto.utc = function() { - oldMomentProto.utc.apply(this, arguments); +newMomentProto.utc = function(keepLocalTime) { + + oldMomentProto.utc.call(this, keepLocalTime); // ensure non-ambiguous // this probably already happened via utc() -> utcOffset(), but don't rely on Moment's internals @@ -1118,239 +1384,103 @@ newMomentProto.utc = function() { }; -// methods for arbitrarily manipulating timezone offset. -// should clear time/zone ambiguity when called. -$.each([ - 'zone', // only in moment-pre-2.9. deprecated afterwards - 'utcOffset' -], function(i, name) { - if (oldMomentProto[name]) { // original method exists? +// implicitly marks a zone (will probably get called upon .utc() and .local()) +newMomentProto.utcOffset = function(tzo) { - // this method implicitly marks a zone (will probably get called upon .utc() and .local()) - newMomentProto[name] = function(tzo) { - - if (tzo != null) { // setter - // these assignments needs to happen before the original zone method is called. - // I forget why, something to do with a browser crash. - this._ambigTime = false; - this._ambigZone = false; - } - - return oldMomentProto[name].apply(this, arguments); - }; + if (tzo != null) { // setter + // these assignments needs to happen before the original zone method is called. + // I forget why, something to do with a browser crash. + this._ambigTime = false; + this._ambigZone = false; } -}); + + return oldMomentProto.utcOffset.apply(this, arguments); +}; // Formatting // ------------------------------------------------------------------------------------------------- newMomentProto.format = function() { + if (this._fullCalendar && arguments[0]) { // an enhanced moment? and a format string provided? return formatDate(this, arguments[0]); // our extended formatting } if (this._ambigTime) { - return oldMomentFormat(this, 'YYYY-MM-DD'); + return oldMomentFormat(englishMoment(this), 'YYYY-MM-DD'); } if (this._ambigZone) { - return oldMomentFormat(this, 'YYYY-MM-DD[T]HH:mm:ss'); + return oldMomentFormat(englishMoment(this), 'YYYY-MM-DD[T]HH:mm:ss'); } + if (this._fullCalendar) { // enhanced non-ambig moment? + // moment.format() doesn't ensure english, but we want to. + return oldMomentFormat(englishMoment(this)); + } + return oldMomentProto.format.apply(this, arguments); }; newMomentProto.toISOString = function() { + if (this._ambigTime) { - return oldMomentFormat(this, 'YYYY-MM-DD'); + return oldMomentFormat(englishMoment(this), 'YYYY-MM-DD'); } if (this._ambigZone) { - return oldMomentFormat(this, 'YYYY-MM-DD[T]HH:mm:ss'); + return oldMomentFormat(englishMoment(this), 'YYYY-MM-DD[T]HH:mm:ss'); } + if (this._fullCalendar) { // enhanced non-ambig moment? + // depending on browser, moment might not output english. ensure english. + // https://github.com/moment/moment/blob/2.18.1/src/lib/moment/format.js#L22 + return oldMomentProto.toISOString.apply(englishMoment(this), arguments); + } + return oldMomentProto.toISOString.apply(this, arguments); }; - -// Querying -// ------------------------------------------------------------------------------------------------- - -// Is the moment within the specified range? `end` is exclusive. -// FYI, this method is not a standard Moment method, so always do our enhanced logic. -newMomentProto.isWithin = function(start, end) { - var a = commonlyAmbiguate([ this, start, end ]); - return a[0] >= a[1] && a[0] < a[2]; -}; - -// When isSame is called with units, timezone ambiguity is normalized before the comparison happens. -// If no units specified, the two moments must be identically the same, with matching ambig flags. -newMomentProto.isSame = function(input, units) { - var a; - - // only do custom logic if this is an enhanced moment - if (!this._fullCalendar) { - return oldMomentProto.isSame.apply(this, arguments); +function englishMoment(mom) { + if (mom.locale() !== 'en') { + return mom.clone().locale('en'); } - - if (units) { - a = commonlyAmbiguate([ this, input ], true); // normalize timezones but don't erase times - return oldMomentProto.isSame.call(a[0], a[1], units); - } - else { - input = fc.moment.parseZone(input); // normalize input - return oldMomentProto.isSame.call(this, input) && - Boolean(this._ambigTime) === Boolean(input._ambigTime) && - Boolean(this._ambigZone) === Boolean(input._ambigZone); - } -}; - -// Make these query methods work with ambiguous moments -$.each([ - 'isBefore', - 'isAfter' -], function(i, methodName) { - newMomentProto[methodName] = function(input, units) { - var a; - - // only do custom logic if this is an enhanced moment - if (!this._fullCalendar) { - return oldMomentProto[methodName].apply(this, arguments); - } - - a = commonlyAmbiguate([ this, input ]); - return oldMomentProto[methodName].call(a[0], a[1], units); - }; -}); - - -// Misc Internals -// ------------------------------------------------------------------------------------------------- - -// given an array of moment-like inputs, return a parallel array w/ moments similarly ambiguated. -// for example, of one moment has ambig time, but not others, all moments will have their time stripped. -// set `preserveTime` to `true` to keep times, but only normalize zone ambiguity. -// returns the original moments if no modifications are necessary. -function commonlyAmbiguate(inputs, preserveTime) { - var anyAmbigTime = false; - var anyAmbigZone = false; - var len = inputs.length; - var moms = []; - var i, mom; - - // parse inputs into real moments and query their ambig flags - for (i = 0; i < len; i++) { - mom = inputs[i]; - if (!moment.isMoment(mom)) { - mom = fc.moment.parseZone(mom); - } - anyAmbigTime = anyAmbigTime || mom._ambigTime; - anyAmbigZone = anyAmbigZone || mom._ambigZone; - moms.push(mom); - } - - // strip each moment down to lowest common ambiguity - // use clones to avoid modifying the original moments - for (i = 0; i < len; i++) { - mom = moms[i]; - if (!preserveTime && anyAmbigTime && !mom._ambigTime) { - moms[i] = mom.clone().stripTime(); - } - else if (anyAmbigZone && !mom._ambigZone) { - moms[i] = mom.clone().stripZone(); - } - } - - return moms; + return mom; } -// Transfers all the flags related to ambiguous time/zone from the `src` moment to the `dest` moment -// TODO: look into moment.momentProperties for this. -function transferAmbigs(src, dest) { - if (src._ambigTime) { - dest._ambigTime = true; - } - else if (dest._ambigTime) { - dest._ambigTime = false; - } - - if (src._ambigZone) { - dest._ambigZone = true; - } - else if (dest._ambigZone) { - dest._ambigZone = false; - } -} - - -// Sets the year/month/date/etc values of the moment from the given array. -// Inefficient because it calls each individual setter. -function setMomentValues(mom, a) { - mom.year(a[0] || 0) - .month(a[1] || 0) - .date(a[2] || 0) - .hours(a[3] || 0) - .minutes(a[4] || 0) - .seconds(a[5] || 0) - .milliseconds(a[6] || 0); -} - -// Can we set the moment's internal date directly? -allowValueOptimization = '_d' in moment() && 'updateOffset' in moment; - -// Utility function. Accepts a moment and an array of the UTC year/month/date/etc values to set. -// Assumes the given moment is already in UTC mode. -setUTCValues = allowValueOptimization ? function(mom, a) { - // simlate what moment's accessors do - mom._d.setTime(Date.UTC.apply(Date, a)); - moment.updateOffset(mom, false); // keepTime=false -} : setMomentValues; - -// Utility function. Accepts a moment and an array of the local year/month/date/etc values to set. -// Assumes the given moment is already in local mode. -setLocalValues = allowValueOptimization ? function(mom, a) { - // simlate what moment's accessors do - mom._d.setTime(+new Date( // FYI, there is now way to apply an array of args to a constructor - a[0] || 0, - a[1] || 0, - a[2] || 0, - a[3] || 0, - a[4] || 0, - a[5] || 0, - a[6] || 0 - )); - moment.updateOffset(mom, false); // keepTime=false -} : setMomentValues; - ;; +(function() { -// Single Date Formatting -// ------------------------------------------------------------------------------------------------- +// exports +FC.formatDate = formatDate; +FC.formatRange = formatRange; +FC.oldMomentFormat = oldMomentFormat; +FC.queryMostGranularFormatUnit = queryMostGranularFormatUnit; -// call this if you want Moment's original format method to be used -function oldMomentFormat(mom, formatStr) { - return oldMomentProto.format.call(mom, formatStr); // oldMomentProto defined in moment-ext.js -} +// Config +// --------------------------------------------------------------------------------------------------------------------- +/* +Inserted between chunks in the fake ("intermediate") formatting string. +Important that it passes as whitespace (\s) because moment often identifies non-standalone months +via a regexp with an \s. +*/ +var PART_SEPARATOR = '\u000b'; // vertical tab -// Formats `date` with a Moment formatting string, but allow our non-zero areas and -// additional token. -function formatDate(date, formatStr) { - return formatDateWithChunks(date, getFormatStringChunks(formatStr)); -} +/* +Inserted as the first character of a literal-text chunk to indicate that the literal text is not actually literal text, +but rather, a "special" token that has custom rendering (see specialTokens map). +*/ +var SPECIAL_TOKEN_MARKER = '\u001f'; // information separator 1 +/* +Inserted at the beginning and end of a span of text that must have non-zero numeric characters. +Handling of these markers is done in a post-processing step at the very end of text rendering. +*/ +var MAYBE_MARKER = '\u001e'; // information separator 2 +var MAYBE_REGEXP = new RegExp(MAYBE_MARKER + '([^' + MAYBE_MARKER + ']*)' + MAYBE_MARKER, 'g'); // must be global -function formatDateWithChunks(date, chunks) { - var s = ''; - var i; - - for (i=0; i "MMMM D YYYY" - formatStr = localeData.longDateFormat(formatStr) || formatStr; + // Expand localized format strings, like "LL" -> "MMMM D YYYY". // BTW, this is not important for `formatDate` because it is impossible to put custom tokens // or non-zero areas in Moment's localized format strings. + formatStr = localeData.longDateFormat(formatStr) || formatStr; - separator = separator || ' - '; - - return formatRangeWithChunks( + return renderParsedFormat( + getParsedFormatString(formatStr), date1, date2, - getFormatStringChunks(formatStr), - separator, + separator || ' - ', isRTL ); } -fc.formatRange = formatRange; // expose +/* +Renders a range with an already-parsed format string. +*/ +function renderParsedFormat(parsedFormat, date1, date2, separator, isRTL) { + var sameUnits = parsedFormat.sameUnits; + var unzonedDate1 = date1.clone().stripZone(); // for same-unit comparisons + var unzonedDate2 = date2.clone().stripZone(); // " + + var renderedParts1 = renderFakeFormatStringParts(parsedFormat.fakeFormatString, date1); + var renderedParts2 = renderFakeFormatStringParts(parsedFormat.fakeFormatString, date2); -function formatRangeWithChunks(date1, date2, chunks, separator, isRTL) { - var chunkStr; // the rendering of the chunk var leftI; var leftStr = ''; var rightI; @@ -1431,28 +1579,35 @@ function formatRangeWithChunks(date1, date2, chunks, separator, isRTL) { // Start at the leftmost side of the formatting string and continue until you hit a token // that is not the same between dates. - for (leftI=0; leftIleftI; rightI--) { - chunkStr = formatSimilarChunk(date1, date2, chunks[rightI]); - if (chunkStr === false) { + for ( + rightI = sameUnits.length - 1; + rightI > leftI && (!sameUnits[rightI] || unzonedDate1.isSame(unzonedDate2, sameUnits[rightI])); + rightI-- + ) { + // If current chunk is on the boundary of unique date-content, and is a special-case + // date-formatting postfix character, then don't consume it. Consider it unique date-content. + // TODO: make configurable + if (rightI - 1 === leftI && renderedParts1[rightI] === '.') { break; } - rightStr = chunkStr + rightStr; + + rightStr = renderedParts1[rightI] + rightStr; } // The area in the middle is different for both of the dates. // Collect them distinctly so we can jam them together later. - for (middleI=leftI; middleI<=rightI; middleI++) { - middleStr1 += formatDateWithChunk(date1, chunks[middleI]); - middleStr2 += formatDateWithChunk(date2, chunks[middleI]); + for (middleI = leftI; middleI <= rightI; middleI++) { + middleStr1 += renderedParts1[middleI]; + middleStr2 += renderedParts2[middleI]; } if (middleStr1 || middleStr2) { @@ -1464,75 +1619,59 @@ function formatRangeWithChunks(date1, date2, chunks, separator, isRTL) { } } - return leftStr + middleStr + rightStr; + return processMaybeMarkers( + leftStr + middleStr + rightStr + ); } -var similarUnitMap = { - Y: 'year', - M: 'month', - D: 'day', // day of month - d: 'day', // day of week - // prevents a separator between anything time-related... - A: 'second', // AM/PM - a: 'second', // am/pm - T: 'second', // A/P - t: 'second', // a/p - H: 'second', // hour (24) - h: 'second', // hour (12) - m: 'second', // minute - s: 'second' // second -}; -// TODO: week maybe? +// Format String Parsing +// --------------------------------------------------------------------------------------------------------------------- +var parsedFormatStrCache = {}; -// Given a formatting chunk, and given that both dates are similar in the regard the -// formatting chunk is concerned, format date1 against `chunk`. Otherwise, return `false`. -function formatSimilarChunk(date1, date2, chunk) { - var token; - var unit; - - if (typeof chunk === 'string') { // a literal string - return chunk; - } - else if ((token = chunk.token)) { - unit = similarUnitMap[token.charAt(0)]; - // are the dates the same for this unit of measurement? - if (unit && date1.isSame(date2, unit)) { - return oldMomentFormat(date1, token); // would be the same if we used `date2` - // BTW, don't support custom tokens - } - } - - return false; // the chunk is NOT the same for the two dates - // BTW, don't support splitting on non-zero areas +/* +Returns a parsed format string, leveraging a cache. +*/ +function getParsedFormatString(formatStr) { + return parsedFormatStrCache[formatStr] || + (parsedFormatStrCache[formatStr] = parseFormatString(formatStr)); } - -// Chunking Utils -// ------------------------------------------------------------------------------------------------- - - -var formatStringChunkCache = {}; - - -function getFormatStringChunks(formatStr) { - if (formatStr in formatStringChunkCache) { - return formatStringChunkCache[formatStr]; - } - return (formatStringChunkCache[formatStr] = chunkFormatString(formatStr)); +/* +Parses a format string into the following: +- fakeFormatString: a momentJS formatting string, littered with special control characters that get post-processed. +- sameUnits: for every part in fakeFormatString, if the part is a token, the value will be a unit string (like "day"), + that indicates how similar a range's start & end must be in order to share the same formatted text. + If not a token, then the value is null. + Always a flat array (not nested liked "chunks"). +*/ +function parseFormatString(formatStr) { + var chunks = chunkFormatString(formatStr); + + return { + fakeFormatString: buildFakeFormatString(chunks), + sameUnits: buildSameUnits(chunks) + }; } - -// Break the formatting string into an array of chunks +/* +Break the formatting string into an array of chunks. +A 'maybe' chunk will have nested chunks. +*/ function chunkFormatString(formatStr) { var chunks = []; - var chunker = /\[([^\]]*)\]|\(([^\)]*)\)|(LTS|LT|(\w)\4*o?)|([^\w\[\(]+)/g; // TODO: more descrimination var match; + // TODO: more descrimination + // \4 is a backreference to the first character of a multi-character set. + var chunker = /\[([^\]]*)\]|\(([^\)]*)\)|(LTS|LT|(\w)\4*o?)|([^\w\[\(]+)/g; + while ((match = chunker.exec(formatStr))) { if (match[1]) { // a literal string inside [ ... ] - chunks.push(match[1]); + chunks.push.apply(chunks, // append + splitStringLiteral(match[1]) + ); } else if (match[2]) { // non-zero formatting inside ( ... ) chunks.push({ maybe: chunkFormatString(match[2]) }); @@ -1541,26 +1680,223 @@ function chunkFormatString(formatStr) { chunks.push({ token: match[3] }); } else if (match[5]) { // an unenclosed literal string - chunks.push(match[5]); + chunks.push.apply(chunks, // append + splitStringLiteral(match[5]) + ); } } return chunks; } +/* +Potentially splits a literal-text string into multiple parts. For special cases. +*/ +function splitStringLiteral(s) { + if (s === '. ') { + return [ '.', ' ' ]; // for locales with periods bound to the end of each year/month/date + } + else { + return [ s ]; + } +} + +/* +Given chunks parsed from a real format string, generate a fake (aka "intermediate") format string with special control +characters that will eventually be given to moment for formatting, and then post-processed. +*/ +function buildFakeFormatString(chunks) { + var parts = []; + var i, chunk; + + for (i = 0; i < chunks.length; i++) { + chunk = chunks[i]; + + if (typeof chunk === 'string') { + parts.push('[' + chunk + ']'); + } + else if (chunk.token) { + if (chunk.token in specialTokens) { + parts.push( + SPECIAL_TOKEN_MARKER + // useful during post-processing + '[' + chunk.token + ']' // preserve as literal text + ); + } + else { + parts.push(chunk.token); // unprotected text implies a format string + } + } + else if (chunk.maybe) { + parts.push( + MAYBE_MARKER + // useful during post-processing + buildFakeFormatString(chunk.maybe) + + MAYBE_MARKER + ); + } + } + + return parts.join(PART_SEPARATOR); +} + +/* +Given parsed chunks from a real formatting string, generates an array of unit strings (like "day") that indicate +in which regard two dates must be similar in order to share range formatting text. +The `chunks` can be nested (because of "maybe" chunks), however, the returned array will be flat. +*/ +function buildSameUnits(chunks) { + var units = []; + var i, chunk; + var tokenInfo; + + for (i = 0; i < chunks.length; i++) { + chunk = chunks[i]; + + if (chunk.token) { + tokenInfo = largeTokenMap[chunk.token.charAt(0)]; + units.push(tokenInfo ? tokenInfo.unit : 'second'); // default to a very strict same-second + } + else if (chunk.maybe) { + units.push.apply(units, // append + buildSameUnits(chunk.maybe) + ); + } + else { + units.push(null); + } + } + + return units; +} + + +// Rendering to text +// --------------------------------------------------------------------------------------------------------------------- + +/* +Formats a date with a fake format string, post-processes the control characters, then returns. +*/ +function renderFakeFormatString(fakeFormatString, date) { + return processMaybeMarkers( + renderFakeFormatStringParts(fakeFormatString, date).join('') + ); +} + +/* +Formats a date into parts that will have been post-processed, EXCEPT for the "maybe" markers. +*/ +function renderFakeFormatStringParts(fakeFormatString, date) { + var parts = []; + var fakeRender = oldMomentFormat(date, fakeFormatString); + var fakeParts = fakeRender.split(PART_SEPARATOR); + var i, fakePart; + + for (i = 0; i < fakeParts.length; i++) { + fakePart = fakeParts[i]; + + if (fakePart.charAt(0) === SPECIAL_TOKEN_MARKER) { + parts.push( + // the literal string IS the token's name. + // call special token's registered function. + specialTokens[fakePart.substring(1)](date) + ); + } + else { + parts.push(fakePart); + } + } + + return parts; +} + +/* +Accepts an almost-finally-formatted string and processes the "maybe" control characters, returning a new string. +*/ +function processMaybeMarkers(s) { + return s.replace(MAYBE_REGEXP, function(m0, m1) { // regex assumed to have 'g' flag + if (m1.match(/[1-9]/)) { // any non-zero numeric characters? + return m1; + } + else { + return ''; + } + }); +} + + +// Misc Utils +// ------------------------------------------------------------------------------------------------- + +/* +Returns a unit string, either 'year', 'month', 'day', or null for the most granular formatting token in the string. +*/ +function queryMostGranularFormatUnit(formatStr) { + var chunks = chunkFormatString(formatStr); + var i, chunk; + var candidate; + var best; + + for (i = 0; i < chunks.length; i++) { + chunk = chunks[i]; + + if (chunk.token) { + candidate = largeTokenMap[chunk.token.charAt(0)]; + if (candidate) { + if (!best || candidate.value > best.value) { + best = candidate; + } + } + } + } + + if (best) { + return best.unit; + } + + return null; +}; + +})(); + +// quick local references +var formatDate = FC.formatDate; +var formatRange = FC.formatRange; +var oldMomentFormat = FC.oldMomentFormat; + ;; -fc.Class = Class; // export +FC.Class = Class; // export -// class that all other classes will inherit from +// Class that all other classes will inherit from function Class() { } -// called upon a class to create a subclass -Class.extend = function(members) { - var superClass = this; - var subClass; - members = members || {}; +// Called on a class to create a subclass. +// Last argument contains instance methods. Any argument before the last are considered mixins. +Class.extend = function() { + var len = arguments.length; + var i; + var members; + + for (i = 0; i < len; i++) { + members = arguments[i]; + if (i < len - 1) { // not the last argument? + mixIntoClass(this, members); + } + } + + return extendClass(this, members || {}); // members will be undefined if no arguments +}; + + +// Adds new member variables/methods to the class's prototype. +// Can be called with another class, or a plain object hash containing new members. +Class.mixin = function(members) { + mixIntoClass(this, members); +}; + + +function extendClass(superClass, members) { + var subClass; // ensure a constructor for the subclass, forwarding all arguments to the super-constructor if it doesn't exist if (hasOwnProp(members, 'constructor')) { @@ -1577,19 +1913,795 @@ Class.extend = function(members) { // copy each member variable/method onto the the subclass's prototype copyOwnProps(members, subClass.prototype); - copyNativeMethods(members, subClass.prototype); // hack for IE8 // copy over all class variables/methods to the subclass, such as `extend` and `mixin` copyOwnProps(superClass, subClass); return subClass; +} + + +function mixIntoClass(theClass, members) { + copyOwnProps(members, theClass.prototype); +} +;; + +var Model = Class.extend(EmitterMixin, ListenerMixin, { + + _props: null, + _watchers: null, + _globalWatchArgs: null, + + constructor: function() { + this._watchers = {}; + this._props = {}; + this.applyGlobalWatchers(); + }, + + applyGlobalWatchers: function() { + var argSets = this._globalWatchArgs || []; + var i; + + for (i = 0; i < argSets.length; i++) { + this.watch.apply(this, argSets[i]); + } + }, + + has: function(name) { + return name in this._props; + }, + + get: function(name) { + if (name === undefined) { + return this._props; + } + + return this._props[name]; + }, + + set: function(name, val) { + var newProps; + + if (typeof name === 'string') { + newProps = {}; + newProps[name] = val === undefined ? null : val; + } + else { + newProps = name; + } + + this.setProps(newProps); + }, + + reset: function(newProps) { + var oldProps = this._props; + var changeset = {}; // will have undefined's to signal unsets + var name; + + for (name in oldProps) { + changeset[name] = undefined; + } + + for (name in newProps) { + changeset[name] = newProps[name]; + } + + this.setProps(changeset); + }, + + unset: function(name) { // accepts a string or array of strings + var newProps = {}; + var names; + var i; + + if (typeof name === 'string') { + names = [ name ]; + } + else { + names = name; + } + + for (i = 0; i < names.length; i++) { + newProps[names[i]] = undefined; + } + + this.setProps(newProps); + }, + + setProps: function(newProps) { + var changedProps = {}; + var changedCnt = 0; + var name, val; + + for (name in newProps) { + val = newProps[name]; + + // a change in value? + // if an object, don't check equality, because might have been mutated internally. + // TODO: eventually enforce immutability. + if ( + typeof val === 'object' || + val !== this._props[name] + ) { + changedProps[name] = val; + changedCnt++; + } + } + + if (changedCnt) { + + this.trigger('before:batchChange', changedProps); + + for (name in changedProps) { + val = changedProps[name]; + + this.trigger('before:change', name, val); + this.trigger('before:change:' + name, val); + } + + for (name in changedProps) { + val = changedProps[name]; + + if (val === undefined) { + delete this._props[name]; + } + else { + this._props[name] = val; + } + + this.trigger('change:' + name, val); + this.trigger('change', name, val); + } + + this.trigger('batchChange', changedProps); + } + }, + + watch: function(name, depList, startFunc, stopFunc) { + var _this = this; + + this.unwatch(name); + + this._watchers[name] = this._watchDeps(depList, function(deps) { + var res = startFunc.call(_this, deps); + + if (res && res.then) { + _this.unset(name); // put in an unset state while resolving + res.then(function(val) { + _this.set(name, val); + }); + } + else { + _this.set(name, res); + } + }, function() { + _this.unset(name); + + if (stopFunc) { + stopFunc.call(_this); + } + }); + }, + + unwatch: function(name) { + var watcher = this._watchers[name]; + + if (watcher) { + delete this._watchers[name]; + watcher.teardown(); + } + }, + + _watchDeps: function(depList, startFunc, stopFunc) { + var _this = this; + var queuedChangeCnt = 0; + var depCnt = depList.length; + var satisfyCnt = 0; + var values = {}; // what's passed as the `deps` arguments + var bindTuples = []; // array of [ eventName, handlerFunc ] arrays + var isCallingStop = false; + + function onBeforeDepChange(depName, val, isOptional) { + queuedChangeCnt++; + if (queuedChangeCnt === 1) { // first change to cause a "stop" ? + if (satisfyCnt === depCnt) { // all deps previously satisfied? + isCallingStop = true; + stopFunc(); + isCallingStop = false; + } + } + } + + function onDepChange(depName, val, isOptional) { + + if (val === undefined) { // unsetting a value? + + // required dependency that was previously set? + if (!isOptional && values[depName] !== undefined) { + satisfyCnt--; + } + + delete values[depName]; + } + else { // setting a value? + + // required dependency that was previously unset? + if (!isOptional && values[depName] === undefined) { + satisfyCnt++; + } + + values[depName] = val; + } + + queuedChangeCnt--; + if (!queuedChangeCnt) { // last change to cause a "start"? + + // now finally satisfied or satisfied all along? + if (satisfyCnt === depCnt) { + + // if the stopFunc initiated another value change, ignore it. + // it will be processed by another change event anyway. + if (!isCallingStop) { + startFunc(values); + } + } + } + } + + // intercept for .on() that remembers handlers + function bind(eventName, handler) { + _this.on(eventName, handler); + bindTuples.push([ eventName, handler ]); + } + + // listen to dependency changes + depList.forEach(function(depName) { + var isOptional = false; + + if (depName.charAt(0) === '?') { // TODO: more DRY + depName = depName.substring(1); + isOptional = true; + } + + bind('before:change:' + depName, function(val) { + onBeforeDepChange(depName, val, isOptional); + }); + + bind('change:' + depName, function(val) { + onDepChange(depName, val, isOptional); + }); + }); + + // process current dependency values + depList.forEach(function(depName) { + var isOptional = false; + + if (depName.charAt(0) === '?') { // TODO: more DRY + depName = depName.substring(1); + isOptional = true; + } + + if (_this.has(depName)) { + values[depName] = _this.get(depName); + satisfyCnt++; + } + else if (isOptional) { + satisfyCnt++; + } + }); + + // initially satisfied + if (satisfyCnt === depCnt) { + startFunc(values); + } + + return { + teardown: function() { + // remove all handlers + for (var i = 0; i < bindTuples.length; i++) { + _this.off(bindTuples[i][0], bindTuples[i][1]); + } + bindTuples = null; + + // was satisfied, so call stopFunc + if (satisfyCnt === depCnt) { + stopFunc(); + } + }, + flash: function() { + if (satisfyCnt === depCnt) { + stopFunc(); + startFunc(values); + } + } + }; + }, + + flash: function(name) { + var watcher = this._watchers[name]; + + if (watcher) { + watcher.flash(); + } + } + +}); + + +Model.watch = function(/* same arguments as this.watch() */) { + var proto = this.prototype; + + if (!proto._globalWatchArgs) { + proto._globalWatchArgs = []; + } + + proto._globalWatchArgs.push(arguments); }; -// adds new member variables/methods to the class's prototype. -// can be called with another class, or a plain object hash containing new members. -Class.mixin = function(members) { - copyOwnProps(members.prototype || members, this.prototype); // TODO: copyNativeMethods? + +FC.Model = Model; + + +;; + +var Promise = { + + construct: function(executor) { + var deferred = $.Deferred(); + var promise = deferred.promise(); + + if (typeof executor === 'function') { + executor( + function(val) { // resolve + deferred.resolve(val); + attachImmediatelyResolvingThen(promise, val); + }, + function() { // reject + deferred.reject(); + attachImmediatelyRejectingThen(promise); + } + ); + } + + return promise; + }, + + resolve: function(val) { + var deferred = $.Deferred().resolve(val); + var promise = deferred.promise(); + + attachImmediatelyResolvingThen(promise, val); + + return promise; + }, + + reject: function() { + var deferred = $.Deferred().reject(); + var promise = deferred.promise(); + + attachImmediatelyRejectingThen(promise); + + return promise; + } + }; + + +function attachImmediatelyResolvingThen(promise, val) { + promise.then = function(onResolve) { + if (typeof onResolve === 'function') { + onResolve(val); + } + return promise; // for chaining + }; +} + + +function attachImmediatelyRejectingThen(promise) { + promise.then = function(onResolve, onReject) { + if (typeof onReject === 'function') { + onReject(); + } + return promise; // for chaining + }; +} + + +FC.Promise = Promise; + +;; + +var TaskQueue = Class.extend(EmitterMixin, { + + q: null, + isPaused: false, + isRunning: false, + + + constructor: function() { + this.q = []; + }, + + + queue: function(/* taskFunc, taskFunc... */) { + this.q.push.apply(this.q, arguments); // append + this.tryStart(); + }, + + + pause: function() { + this.isPaused = true; + }, + + + resume: function() { + this.isPaused = false; + this.tryStart(); + }, + + + tryStart: function() { + if (!this.isRunning && this.canRunNext()) { + this.isRunning = true; + this.trigger('start'); + this.runNext(); + } + }, + + + canRunNext: function() { + return !this.isPaused && this.q.length; + }, + + + runNext: function() { // does not check canRunNext + this.runTask(this.q.shift()); + }, + + + runTask: function(task) { + this.runTaskFunc(task); + }, + + + runTaskFunc: function(taskFunc) { + var _this = this; + var res = taskFunc(); + + if (res && res.then) { + res.then(done); + } + else { + done(); + } + + function done() { + if (_this.canRunNext()) { + _this.runNext(); + } + else { + _this.isRunning = false; + _this.trigger('stop'); + } + } + } + +}); + +FC.TaskQueue = TaskQueue; + +;; + +var RenderQueue = TaskQueue.extend({ + + waitsByNamespace: null, + waitNamespace: null, + waitId: null, + + + constructor: function(waitsByNamespace) { + TaskQueue.call(this); // super-constructor + + this.waitsByNamespace = waitsByNamespace || {}; + }, + + + queue: function(taskFunc, namespace, type) { + var task = { + func: taskFunc, + namespace: namespace, + type: type + }; + var waitMs; + + if (namespace) { + waitMs = this.waitsByNamespace[namespace]; + } + + if (this.waitNamespace) { + if (namespace === this.waitNamespace && waitMs != null) { + this.delayWait(waitMs); + } + else { + this.clearWait(); + this.tryStart(); + } + } + + if (this.compoundTask(task)) { // appended to queue? + + if (!this.waitNamespace && waitMs != null) { + this.startWait(namespace, waitMs); + } + else { + this.tryStart(); + } + } + }, + + + startWait: function(namespace, waitMs) { + this.waitNamespace = namespace; + this.spawnWait(waitMs); + }, + + + delayWait: function(waitMs) { + clearTimeout(this.waitId); + this.spawnWait(waitMs); + }, + + + spawnWait: function(waitMs) { + var _this = this; + + this.waitId = setTimeout(function() { + _this.waitNamespace = null; + _this.tryStart(); + }, waitMs); + }, + + + clearWait: function() { + if (this.waitNamespace) { + clearTimeout(this.waitId); + this.waitId = null; + this.waitNamespace = null; + } + }, + + + canRunNext: function() { + if (!TaskQueue.prototype.canRunNext.apply(this, arguments)) { + return false; + } + + // waiting for a certain namespace to stop receiving tasks? + if (this.waitNamespace) { + + // if there was a different namespace task in the meantime, + // that forces all previously-waiting tasks to suddenly execute. + // TODO: find a way to do this in constant time. + for (var q = this.q, i = 0; i < q.length; i++) { + if (q[i].namespace !== this.waitNamespace) { + return true; // allow execution + } + } + + return false; + } + + return true; + }, + + + runTask: function(task) { + this.runTaskFunc(task.func); + }, + + + compoundTask: function(newTask) { + var q = this.q; + var shouldAppend = true; + var i, task; + + if (newTask.namespace) { + + if (newTask.type === 'destroy' || newTask.type === 'init') { + + // remove all add/remove ops with same namespace, regardless of order + for (i = q.length - 1; i >= 0; i--) { + task = q[i]; + + if ( + task.namespace === newTask.namespace && + (task.type === 'add' || task.type === 'remove') + ) { + q.splice(i, 1); // remove task + } + } + + if (newTask.type === 'destroy') { + // eat away final init/destroy operation + if (q.length) { + task = q[q.length - 1]; // last task + + if (task.namespace === newTask.namespace) { + + // the init and our destroy cancel each other out + if (task.type === 'init') { + shouldAppend = false; + q.pop(); + } + // prefer to use the destroy operation that's already present + else if (task.type === 'destroy') { + shouldAppend = false; + } + } + } + } + else if (newTask.type === 'init') { + // eat away final init operation + if (q.length) { + task = q[q.length - 1]; // last task + + if ( + task.namespace === newTask.namespace && + task.type === 'init' + ) { + // our init operation takes precedence + q.pop(); + } + } + } + } + } + + if (shouldAppend) { + q.push(newTask); + } + + return shouldAppend; + } + +}); + +FC.RenderQueue = RenderQueue; + +;; + +var EmitterMixin = FC.EmitterMixin = { + + // jQuery-ification via $(this) allows a non-DOM object to have + // the same event handling capabilities (including namespaces). + + + on: function(types, handler) { + $(this).on(types, this._prepareIntercept(handler)); + return this; // for chaining + }, + + + one: function(types, handler) { + $(this).one(types, this._prepareIntercept(handler)); + return this; // for chaining + }, + + + _prepareIntercept: function(handler) { + // handlers are always called with an "event" object as their first param. + // sneak the `this` context and arguments into the extra parameter object + // and forward them on to the original handler. + var intercept = function(ev, extra) { + return handler.apply( + extra.context || this, + extra.args || [] + ); + }; + + // mimick jQuery's internal "proxy" system (risky, I know) + // causing all functions with the same .guid to appear to be the same. + // https://github.com/jquery/jquery/blob/2.2.4/src/core.js#L448 + // this is needed for calling .off with the original non-intercept handler. + if (!handler.guid) { + handler.guid = $.guid++; + } + intercept.guid = handler.guid; + + return intercept; + }, + + + off: function(types, handler) { + $(this).off(types, handler); + + return this; // for chaining + }, + + + trigger: function(types) { + var args = Array.prototype.slice.call(arguments, 1); // arguments after the first + + // pass in "extra" info to the intercept + $(this).triggerHandler(types, { args: args }); + + return this; // for chaining + }, + + + triggerWith: function(types, context, args) { + + // `triggerHandler` is less reliant on the DOM compared to `trigger`. + // pass in "extra" info to the intercept. + $(this).triggerHandler(types, { context: context, args: args }); + + return this; // for chaining + } + +}; + +;; + +/* +Utility methods for easily listening to events on another object, +and more importantly, easily unlistening from them. +*/ +var ListenerMixin = FC.ListenerMixin = (function() { + var guid = 0; + var ListenerMixin = { + + listenerId: null, + + /* + Given an `other` object that has on/off methods, bind the given `callback` to an event by the given name. + The `callback` will be called with the `this` context of the object that .listenTo is being called on. + Can be called: + .listenTo(other, eventName, callback) + OR + .listenTo(other, { + eventName1: callback1, + eventName2: callback2 + }) + */ + listenTo: function(other, arg, callback) { + if (typeof arg === 'object') { // given dictionary of callbacks + for (var eventName in arg) { + if (arg.hasOwnProperty(eventName)) { + this.listenTo(other, eventName, arg[eventName]); + } + } + } + else if (typeof arg === 'string') { + other.on( + arg + '.' + this.getListenerNamespace(), // use event namespacing to identify this object + $.proxy(callback, this) // always use `this` context + // the usually-undesired jQuery guid behavior doesn't matter, + // because we always unbind via namespace + ); + } + }, + + /* + Causes the current object to stop listening to events on the `other` object. + `eventName` is optional. If omitted, will stop listening to ALL events on `other`. + */ + stopListeningTo: function(other, eventName) { + other.off((eventName || '') + '.' + this.getListenerNamespace()); + }, + + /* + Returns a string, unique to this object, to be used for event namespacing + */ + getListenerNamespace: function() { + if (this.listenerId == null) { + this.listenerId = guid++; + } + return '_listener' + this.listenerId; + } + + }; + return ListenerMixin; +})(); ;; /* A rectangular panel that is absolutely positioned over other content @@ -1606,12 +2718,11 @@ Options: - hide (callback) */ -var Popover = Class.extend({ +var Popover = Class.extend(ListenerMixin, { isHidden: true, options: null, el: null, // the container element for the popover. generated by this object - documentMousedownProxy: null, // document mousedown handler bound to `this` margin: 10, // the space required between the popover and the edges of the scroll container @@ -1665,7 +2776,7 @@ var Popover = Class.extend({ }); if (options.autoHide) { - $(document).on('mousedown', this.documentMousedownProxy = proxy(this, 'documentMousedown')); + this.listenTo($(document), 'mousedown', this.documentMousedown); } }, @@ -1688,7 +2799,7 @@ var Popover = Class.extend({ this.el = null; } - $(document).off('mousedown', this.documentMousedownProxy); + this.stopListeningTo($(document), 'mousedown'); }, @@ -1761,165 +2872,258 @@ var Popover = Class.extend({ ;; -/* A "coordinate map" converts pixel coordinates into an associated cell, which has an associated date ------------------------------------------------------------------------------------------------------------------------- -Common interface: - - CoordMap.prototype = { - build: function() {}, - getCell: function(x, y) {} - }; +/* +A cache for the left/right/top/bottom/width/height values for one or more elements. +Works with both offset (from topleft document) and position (from offsetParent). +options: +- els +- isHorizontal +- isVertical */ +var CoordCache = FC.CoordCache = Class.extend({ -/* Coordinate map for a grid component -----------------------------------------------------------------------------------------------------------------------*/ + els: null, // jQuery set (assumed to be siblings) + forcedOffsetParentEl: null, // options can override the natural offsetParent + origin: null, // {left,top} position of offsetParent of els + boundingRect: null, // constrain cordinates to this rectangle. {left,right,top,bottom} or null + isHorizontal: false, // whether to query for left/right/width + isVertical: false, // whether to query for top/bottom/height -var GridCoordMap = Class.extend({ - - grid: null, // reference to the Grid - rowCoords: null, // array of {top,bottom} objects - colCoords: null, // array of {left,right} objects - - containerEl: null, // container element that all coordinates are constrained to. optionally assigned - bounds: null, + // arrays of coordinates (offsets from topleft of document) + lefts: null, + rights: null, + tops: null, + bottoms: null, - constructor: function(grid) { - this.grid = grid; + constructor: function(options) { + this.els = $(options.els); + this.isHorizontal = options.isHorizontal; + this.isVertical = options.isVertical; + this.forcedOffsetParentEl = options.offsetParent ? $(options.offsetParent) : null; }, - // Queries the grid for the coordinates of all the cells + // Queries the els for coordinates and stores them. + // Call this method before using and of the get* methods below. build: function() { - this.grid.build(); - this.rowCoords = this.grid.computeRowCoords(); - this.colCoords = this.grid.computeColCoords(); - this.computeBounds(); + var offsetParentEl = this.forcedOffsetParentEl; + if (!offsetParentEl && this.els.length > 0) { + offsetParentEl = this.els.eq(0).offsetParent(); + } + + this.origin = offsetParentEl ? + offsetParentEl.offset() : + null; + + this.boundingRect = this.queryBoundingRect(); + + if (this.isHorizontal) { + this.buildElHorizontals(); + } + if (this.isVertical) { + this.buildElVerticals(); + } }, - // Clears the coordinates data to free up memory + // Destroys all internal data about coordinates, freeing memory clear: function() { - this.grid.clear(); - this.rowCoords = null; - this.colCoords = null; + this.origin = null; + this.boundingRect = null; + this.lefts = null; + this.rights = null; + this.tops = null; + this.bottoms = null; }, - // Given a coordinate of the document, gets the associated cell. If no cell is underneath, returns null - getCell: function(x, y) { - var rowCoords = this.rowCoords; - var rowCnt = rowCoords.length; - var colCoords = this.colCoords; - var colCnt = colCoords.length; - var hitRow = null; - var hitCol = null; - var i, coords; - var cell; + // When called, if coord caches aren't built, builds them + ensureBuilt: function() { + if (!this.origin) { + this.build(); + } + }, - if (this.inBounds(x, y)) { - for (i = 0; i < rowCnt; i++) { - coords = rowCoords[i]; - if (y >= coords.top && y < coords.bottom) { - hitRow = i; - break; - } + // Populates the left/right internal coordinate arrays + buildElHorizontals: function() { + var lefts = []; + var rights = []; + + this.els.each(function(i, node) { + var el = $(node); + var left = el.offset().left; + var width = el.outerWidth(); + + lefts.push(left); + rights.push(left + width); + }); + + this.lefts = lefts; + this.rights = rights; + }, + + + // Populates the top/bottom internal coordinate arrays + buildElVerticals: function() { + var tops = []; + var bottoms = []; + + this.els.each(function(i, node) { + var el = $(node); + var top = el.offset().top; + var height = el.outerHeight(); + + tops.push(top); + bottoms.push(top + height); + }); + + this.tops = tops; + this.bottoms = bottoms; + }, + + + // Given a left offset (from document left), returns the index of the el that it horizontally intersects. + // If no intersection is made, returns undefined. + getHorizontalIndex: function(leftOffset) { + this.ensureBuilt(); + + var lefts = this.lefts; + var rights = this.rights; + var len = lefts.length; + var i; + + for (i = 0; i < len; i++) { + if (leftOffset >= lefts[i] && leftOffset < rights[i]) { + return i; } + } + }, - for (i = 0; i < colCnt; i++) { - coords = colCoords[i]; - if (x >= coords.left && x < coords.right) { - hitCol = i; - break; - } + + // Given a top offset (from document top), returns the index of the el that it vertically intersects. + // If no intersection is made, returns undefined. + getVerticalIndex: function(topOffset) { + this.ensureBuilt(); + + var tops = this.tops; + var bottoms = this.bottoms; + var len = tops.length; + var i; + + for (i = 0; i < len; i++) { + if (topOffset >= tops[i] && topOffset < bottoms[i]) { + return i; } + } + }, - if (hitRow !== null && hitCol !== null) { - cell = this.grid.getCell(hitRow, hitCol); // expected to return a fresh object we can modify - cell.grid = this.grid; // for CellDragListener's isCellsEqual. dragging between grids + // Gets the left offset (from document left) of the element at the given index + getLeftOffset: function(leftIndex) { + this.ensureBuilt(); + return this.lefts[leftIndex]; + }, - // make the coordinates available on the cell object - $.extend(cell, rowCoords[hitRow], colCoords[hitCol]); - return cell; + // Gets the left position (from offsetParent left) of the element at the given index + getLeftPosition: function(leftIndex) { + this.ensureBuilt(); + return this.lefts[leftIndex] - this.origin.left; + }, + + + // Gets the right offset (from document left) of the element at the given index. + // This value is NOT relative to the document's right edge, like the CSS concept of "right" would be. + getRightOffset: function(leftIndex) { + this.ensureBuilt(); + return this.rights[leftIndex]; + }, + + + // Gets the right position (from offsetParent left) of the element at the given index. + // This value is NOT relative to the offsetParent's right edge, like the CSS concept of "right" would be. + getRightPosition: function(leftIndex) { + this.ensureBuilt(); + return this.rights[leftIndex] - this.origin.left; + }, + + + // Gets the width of the element at the given index + getWidth: function(leftIndex) { + this.ensureBuilt(); + return this.rights[leftIndex] - this.lefts[leftIndex]; + }, + + + // Gets the top offset (from document top) of the element at the given index + getTopOffset: function(topIndex) { + this.ensureBuilt(); + return this.tops[topIndex]; + }, + + + // Gets the top position (from offsetParent top) of the element at the given position + getTopPosition: function(topIndex) { + this.ensureBuilt(); + return this.tops[topIndex] - this.origin.top; + }, + + // Gets the bottom offset (from the document top) of the element at the given index. + // This value is NOT relative to the offsetParent's bottom edge, like the CSS concept of "bottom" would be. + getBottomOffset: function(topIndex) { + this.ensureBuilt(); + return this.bottoms[topIndex]; + }, + + + // Gets the bottom position (from the offsetParent top) of the element at the given index. + // This value is NOT relative to the offsetParent's bottom edge, like the CSS concept of "bottom" would be. + getBottomPosition: function(topIndex) { + this.ensureBuilt(); + return this.bottoms[topIndex] - this.origin.top; + }, + + + // Gets the height of the element at the given index + getHeight: function(topIndex) { + this.ensureBuilt(); + return this.bottoms[topIndex] - this.tops[topIndex]; + }, + + + // Bounding Rect + // TODO: decouple this from CoordCache + + // Compute and return what the elements' bounding rectangle is, from the user's perspective. + // Right now, only returns a rectangle if constrained by an overflow:scroll element. + // Returns null if there are no elements + queryBoundingRect: function() { + var scrollParentEl; + + if (this.els.length > 0) { + scrollParentEl = getScrollParent(this.els.eq(0)); + + if (!scrollParentEl.is(document)) { + return getClientRect(scrollParentEl); } } return null; }, - - // If there is a containerEl, compute the bounds into min/max values - computeBounds: function() { - this.bounds = this.containerEl ? - getClientRect(this.containerEl) : // area within scrollbars - null; + isPointInBounds: function(leftOffset, topOffset) { + return this.isLeftInBounds(leftOffset) && this.isTopInBounds(topOffset); }, - - // Determines if the given coordinates are in bounds. If no `containerEl`, always true - inBounds: function(x, y) { - var bounds = this.bounds; - - if (bounds) { - return x >= bounds.left && x < bounds.right && y >= bounds.top && y < bounds.bottom; - } - - return true; - } - -}); - - -/* Coordinate map that is a combination of multiple other coordinate maps -----------------------------------------------------------------------------------------------------------------------*/ - -var ComboCoordMap = Class.extend({ - - coordMaps: null, // an array of CoordMaps - - - constructor: function(coordMaps) { - this.coordMaps = coordMaps; + isLeftInBounds: function(leftOffset) { + return !this.boundingRect || (leftOffset >= this.boundingRect.left && leftOffset < this.boundingRect.right); }, - - // Builds all coordMaps - build: function() { - var coordMaps = this.coordMaps; - var i; - - for (i = 0; i < coordMaps.length; i++) { - coordMaps[i].build(); - } - }, - - - // Queries all coordMaps for the cell underneath the given coordinates, returning the first result - getCell: function(x, y) { - var coordMaps = this.coordMaps; - var cell = null; - var i; - - for (i = 0; i < coordMaps.length && !cell; i++) { - cell = coordMaps[i].getCell(x, y); - } - - return cell; - }, - - - // Clears all coordMaps - clear: function() { - var coordMaps = this.coordMaps; - var i; - - for (i = 0; i < coordMaps.length; i++) { - coordMaps[i].clear(); - } + isTopInBounds: function(topOffset) { + return !this.boundingRect || (topOffset >= this.boundingRect.top && topOffset < this.boundingRect.bottom); } }); @@ -1928,258 +3132,370 @@ var ComboCoordMap = Class.extend({ /* Tracks a drag's mouse movement, firing various handlers ----------------------------------------------------------------------------------------------------------------------*/ +// TODO: use Emitter -var DragListener = fc.DragListener = Class.extend({ +var DragListener = FC.DragListener = Class.extend(ListenerMixin, { options: null, - - isListening: false, - isDragging: false, + subjectEl: null, // coordinates of the initial mousedown originX: null, originY: null, - // handler attached to the document, bound to the DragListener's `this` - mousemoveProxy: null, - mouseupProxy: null, - - // for IE8 bug-fighting behavior, for now - subjectEl: null, // the element being draged. optional - subjectHref: null, - + // the wrapping element that scrolls, or MIGHT scroll if there's overflow. + // TODO: do this for wrappers that have overflow:hidden as well. scrollEl: null, - scrollBounds: null, // { top, bottom, left, right } - scrollTopVel: null, // pixels per second - scrollLeftVel: null, // pixels per second - scrollIntervalId: null, // ID of setTimeout for scrolling animation loop - scrollHandlerProxy: null, // this-scoped function for handling when scrollEl is scrolled - scrollSensitivity: 30, // pixels from edge for scrolling to start - scrollSpeed: 200, // pixels per second, at maximum speed - scrollIntervalMs: 50, // millisecond wait between scroll increment + isInteracting: false, + isDistanceSurpassed: false, + isDelayEnded: false, + isDragging: false, + isTouch: false, + isGeneric: false, // initiated by 'dragstart' (jqui) + + delay: null, + delayTimeoutId: null, + minDistance: null, + + shouldCancelTouchScroll: true, + scrollAlwaysKills: false, constructor: function(options) { - options = options || {}; - this.options = options; - this.subjectEl = options.subjectEl; + this.options = options || {}; }, - // Call this when the user does a mousedown. Will probably lead to startListening - mousedown: function(ev) { - if (isPrimaryMouseButton(ev)) { + // Interaction (high-level) + // ----------------------------------------------------------------------------------------------------------------- - ev.preventDefault(); // prevents native selection in most browsers - this.startListening(ev); + startInteraction: function(ev, extraOptions) { - // start the drag immediately if there is no minimum distance for a drag start - if (!this.options.distance) { - this.startDrag(ev); + if (ev.type === 'mousedown') { + if (GlobalEmitter.get().shouldIgnoreMouse()) { + return; } - } - }, - - - // Call this to start tracking mouse movements - startListening: function(ev) { - var scrollParent; - - if (!this.isListening) { - - // grab scroll container and attach handler - if (ev && this.options.scroll) { - scrollParent = getScrollParent($(ev.target)); - if (!scrollParent.is(window) && !scrollParent.is(document)) { - this.scrollEl = scrollParent; - - // scope to `this`, and use `debounce` to make sure rapid calls don't happen - this.scrollHandlerProxy = debounce(proxy(this, 'scrollHandler'), 100); - this.scrollEl.on('scroll', this.scrollHandlerProxy); - } - } - - $(document) - .on('mousemove', this.mousemoveProxy = proxy(this, 'mousemove')) - .on('mouseup', this.mouseupProxy = proxy(this, 'mouseup')) - .on('selectstart', this.preventDefault); // prevents native selection in IE<=8 - - if (ev) { - this.originX = ev.pageX; - this.originY = ev.pageY; + else if (!isPrimaryMouseButton(ev)) { + return; } else { - // if no starting information was given, origin will be the topleft corner of the screen. - // if so, dx/dy in the future will be the absolute coordinates. - this.originX = 0; - this.originY = 0; + ev.preventDefault(); // prevents native selection in most browsers } + } - this.isListening = true; - this.listenStart(ev); + if (!this.isInteracting) { + + // process options + extraOptions = extraOptions || {}; + this.delay = firstDefined(extraOptions.delay, this.options.delay, 0); + this.minDistance = firstDefined(extraOptions.distance, this.options.distance, 0); + this.subjectEl = this.options.subjectEl; + + preventSelection($('body')); + + this.isInteracting = true; + this.isTouch = getEvIsTouch(ev); + this.isGeneric = ev.type === 'dragstart'; + this.isDelayEnded = false; + this.isDistanceSurpassed = false; + + this.originX = getEvX(ev); + this.originY = getEvY(ev); + this.scrollEl = getScrollParent($(ev.target)); + + this.bindHandlers(); + this.initAutoScroll(); + this.handleInteractionStart(ev); + this.startDelay(ev); + + if (!this.minDistance) { + this.handleDistanceSurpassed(ev); + } } }, - // Called when drag listening has started (but a real drag has not necessarily began) - listenStart: function(ev) { - this.trigger('listenStart', ev); + handleInteractionStart: function(ev) { + this.trigger('interactionStart', ev); }, - // Called when the user moves the mouse - mousemove: function(ev) { - var dx = ev.pageX - this.originX; - var dy = ev.pageY - this.originY; - var minDistance; + endInteraction: function(ev, isCancelled) { + if (this.isInteracting) { + this.endDrag(ev); + + if (this.delayTimeoutId) { + clearTimeout(this.delayTimeoutId); + this.delayTimeoutId = null; + } + + this.destroyAutoScroll(); + this.unbindHandlers(); + + this.isInteracting = false; + this.handleInteractionEnd(ev, isCancelled); + + allowSelection($('body')); + } + }, + + + handleInteractionEnd: function(ev, isCancelled) { + this.trigger('interactionEnd', ev, isCancelled || false); + }, + + + // Binding To DOM + // ----------------------------------------------------------------------------------------------------------------- + + + bindHandlers: function() { + // some browsers (Safari in iOS 10) don't allow preventDefault on touch events that are bound after touchstart, + // so listen to the GlobalEmitter singleton, which is always bound, instead of the document directly. + var globalEmitter = GlobalEmitter.get(); + + if (this.isGeneric) { + this.listenTo($(document), { // might only work on iOS because of GlobalEmitter's bind :( + drag: this.handleMove, + dragstop: this.endInteraction + }); + } + else if (this.isTouch) { + this.listenTo(globalEmitter, { + touchmove: this.handleTouchMove, + touchend: this.endInteraction, + scroll: this.handleTouchScroll + }); + } + else { + this.listenTo(globalEmitter, { + mousemove: this.handleMouseMove, + mouseup: this.endInteraction + }); + } + + this.listenTo(globalEmitter, { + selectstart: preventDefault, // don't allow selection while dragging + contextmenu: preventDefault // long taps would open menu on Chrome dev tools + }); + }, + + + unbindHandlers: function() { + this.stopListeningTo(GlobalEmitter.get()); + this.stopListeningTo($(document)); // for isGeneric + }, + + + // Drag (high-level) + // ----------------------------------------------------------------------------------------------------------------- + + + // extraOptions ignored if drag already started + startDrag: function(ev, extraOptions) { + this.startInteraction(ev, extraOptions); // ensure interaction began + + if (!this.isDragging) { + this.isDragging = true; + this.handleDragStart(ev); + } + }, + + + handleDragStart: function(ev) { + this.trigger('dragStart', ev); + }, + + + handleMove: function(ev) { + var dx = getEvX(ev) - this.originX; + var dy = getEvY(ev) - this.originY; + var minDistance = this.minDistance; var distanceSq; // current distance from the origin, squared - if (!this.isDragging) { // if not already dragging... - // then start the drag if the minimum distance criteria is met - minDistance = this.options.distance || 1; + if (!this.isDistanceSurpassed) { distanceSq = dx * dx + dy * dy; if (distanceSq >= minDistance * minDistance) { // use pythagorean theorem - this.startDrag(ev); + this.handleDistanceSurpassed(ev); } } if (this.isDragging) { - this.drag(dx, dy, ev); // report a drag, even if this mousemove initiated the drag - } - }, - - - // Call this to initiate a legitimate drag. - // This function is called internally from this class, but can also be called explicitly from outside - startDrag: function(ev) { - - if (!this.isListening) { // startDrag must have manually initiated - this.startListening(); - } - - if (!this.isDragging) { - this.isDragging = true; - this.dragStart(ev); - } - }, - - - // Called when the actual drag has started (went beyond minDistance) - dragStart: function(ev) { - var subjectEl = this.subjectEl; - - this.trigger('dragStart', ev); - - // remove a mousedown'd 's href so it is not visited (IE8 bug) - if ((this.subjectHref = subjectEl ? subjectEl.attr('href') : null)) { - subjectEl.removeAttr('href'); + this.handleDrag(dx, dy, ev); } }, // Called while the mouse is being moved and when we know a legitimate drag is taking place - drag: function(dx, dy, ev) { + handleDrag: function(dx, dy, ev) { this.trigger('drag', dx, dy, ev); - this.updateScroll(ev); // will possibly cause scrolling + this.updateAutoScroll(ev); // will possibly cause scrolling }, - // Called when the user does a mouseup - mouseup: function(ev) { - this.stopListening(ev); - }, - - - // Called when the drag is over. Will not cause listening to stop however. - // A concluding 'cellOut' event will NOT be triggered. - stopDrag: function(ev) { + endDrag: function(ev) { if (this.isDragging) { - this.stopScrolling(); - this.dragStop(ev); this.isDragging = false; + this.handleDragEnd(ev); } }, - // Called when dragging has been stopped - dragStop: function(ev) { + handleDragEnd: function(ev) { + this.trigger('dragEnd', ev); + }, + + + // Delay + // ----------------------------------------------------------------------------------------------------------------- + + + startDelay: function(initialEv) { var _this = this; - this.trigger('dragStop', ev); - - // restore a mousedown'd 's href (for IE8 bug) - setTimeout(function() { // must be outside of the click's execution - if (_this.subjectHref) { - _this.subjectEl.attr('href', _this.subjectHref); - } - }, 0); - }, - - - // Call this to stop listening to the user's mouse events - stopListening: function(ev) { - this.stopDrag(ev); // if there's a current drag, kill it - - if (this.isListening) { - - // remove the scroll handler if there is a scrollEl - if (this.scrollEl) { - this.scrollEl.off('scroll', this.scrollHandlerProxy); - this.scrollHandlerProxy = null; - } - - $(document) - .off('mousemove', this.mousemoveProxy) - .off('mouseup', this.mouseupProxy) - .off('selectstart', this.preventDefault); - - this.mousemoveProxy = null; - this.mouseupProxy = null; - - this.isListening = false; - this.listenStop(ev); + if (this.delay) { + this.delayTimeoutId = setTimeout(function() { + _this.handleDelayEnd(initialEv); + }, this.delay); + } + else { + this.handleDelayEnd(initialEv); } }, - // Called when drag listening has stopped - listenStop: function(ev) { - this.trigger('listenStop', ev); + handleDelayEnd: function(initialEv) { + this.isDelayEnded = true; + + if (this.isDistanceSurpassed) { + this.startDrag(initialEv); + } }, + // Distance + // ----------------------------------------------------------------------------------------------------------------- + + + handleDistanceSurpassed: function(ev) { + this.isDistanceSurpassed = true; + + if (this.isDelayEnded) { + this.startDrag(ev); + } + }, + + + // Mouse / Touch + // ----------------------------------------------------------------------------------------------------------------- + + + handleTouchMove: function(ev) { + + // prevent inertia and touchmove-scrolling while dragging + if (this.isDragging && this.shouldCancelTouchScroll) { + ev.preventDefault(); + } + + this.handleMove(ev); + }, + + + handleMouseMove: function(ev) { + this.handleMove(ev); + }, + + + // Scrolling (unrelated to auto-scroll) + // ----------------------------------------------------------------------------------------------------------------- + + + handleTouchScroll: function(ev) { + // if the drag is being initiated by touch, but a scroll happens before + // the drag-initiating delay is over, cancel the drag + if (!this.isDragging || this.scrollAlwaysKills) { + this.endInteraction(ev, true); // isCancelled=true + } + }, + + + // Utils + // ----------------------------------------------------------------------------------------------------------------- + + // Triggers a callback. Calls a function in the option hash of the same name. // Arguments beyond the first `name` are forwarded on. trigger: function(name) { if (this.options[name]) { this.options[name].apply(this, Array.prototype.slice.call(arguments, 1)); } + // makes _methods callable by event name. TODO: kill this + if (this['_' + name]) { + this['_' + name].apply(this, Array.prototype.slice.call(arguments, 1)); + } + } + + +}); + +;; +/* +this.scrollEl is set in DragListener +*/ +DragListener.mixin({ + + isAutoScroll: false, + + scrollBounds: null, // { top, bottom, left, right } + scrollTopVel: null, // pixels per second + scrollLeftVel: null, // pixels per second + scrollIntervalId: null, // ID of setTimeout for scrolling animation loop + + // defaults + scrollSensitivity: 30, // pixels from edge for scrolling to start + scrollSpeed: 200, // pixels per second, at maximum speed + scrollIntervalMs: 50, // millisecond wait between scroll increment + + + initAutoScroll: function() { + var scrollEl = this.scrollEl; + + this.isAutoScroll = + this.options.scroll && + scrollEl && + !scrollEl.is(window) && + !scrollEl.is(document); + + if (this.isAutoScroll) { + // debounce makes sure rapid calls don't happen + this.listenTo(scrollEl, 'scroll', debounce(this.handleDebouncedScroll, 100)); + } }, - // Stops a given mouse event from doing it's native browser action. In our case, text selection. - preventDefault: function(ev) { - ev.preventDefault(); + destroyAutoScroll: function() { + this.endAutoScroll(); // kill any animation loop + + // remove the scroll handler if there is a scrollEl + if (this.isAutoScroll) { + this.stopListeningTo(this.scrollEl, 'scroll'); // will probably get removed by unbindHandlers too :( + } }, - /* Scrolling - ------------------------------------------------------------------------------------------------------------------*/ - - // Computes and stores the bounding rectangle of scrollEl computeScrollBounds: function() { - var el = this.scrollEl; - - this.scrollBounds = el ? getOuterRect(el) : null; + if (this.isAutoScroll) { + this.scrollBounds = getOuterRect(this.scrollEl); // TODO: use getClientRect in future. but prevents auto scrolling when on top of scrollbars + } }, // Called when the dragging is in progress and scrolling should be updated - updateScroll: function(ev) { + updateAutoScroll: function(ev) { var sensitivity = this.scrollSensitivity; var bounds = this.scrollBounds; var topCloseness, bottomCloseness; @@ -2190,10 +3506,10 @@ var DragListener = fc.DragListener = Class.extend({ if (bounds) { // only scroll if scrollEl exists // compute closeness to edges. valid range is from 0.0 - 1.0 - topCloseness = (sensitivity - (ev.pageY - bounds.top)) / sensitivity; - bottomCloseness = (sensitivity - (bounds.bottom - ev.pageY)) / sensitivity; - leftCloseness = (sensitivity - (ev.pageX - bounds.left)) / sensitivity; - rightCloseness = (sensitivity - (bounds.right - ev.pageX)) / sensitivity; + topCloseness = (sensitivity - (getEvY(ev) - bounds.top)) / sensitivity; + bottomCloseness = (sensitivity - (bounds.bottom - getEvY(ev))) / sensitivity; + leftCloseness = (sensitivity - (getEvX(ev) - bounds.left)) / sensitivity; + rightCloseness = (sensitivity - (bounds.right - getEvX(ev))) / sensitivity; // translate vertical closeness into velocity. // mouse must be completely in bounds for velocity to happen. @@ -2280,76 +3596,75 @@ var DragListener = fc.DragListener = Class.extend({ // if scrolled all the way, which causes the vels to be zero, stop the animation loop if (!this.scrollTopVel && !this.scrollLeftVel) { - this.stopScrolling(); + this.endAutoScroll(); } }, // Kills any existing scrolling animation loop - stopScrolling: function() { + endAutoScroll: function() { if (this.scrollIntervalId) { clearInterval(this.scrollIntervalId); this.scrollIntervalId = null; - // when all done with scrolling, recompute positions since they probably changed - this.scrollStop(); + this.handleScrollEnd(); } }, // Get called when the scrollEl is scrolled (NOTE: this is delayed via debounce) - scrollHandler: function() { + handleDebouncedScroll: function() { // recompute all coordinates, but *only* if this is *not* part of our scrolling animation if (!this.scrollIntervalId) { - this.scrollStop(); + this.handleScrollEnd(); } }, // Called when scrolling has stopped, whether through auto scroll, or the user scrolling - scrollStop: function() { + handleScrollEnd: function() { } }); - ;; -/* Tracks mouse movements over a CoordMap and raises events about which cell the mouse is over. +/* Tracks mouse movements over a component and raises events about which hit the mouse is over. ------------------------------------------------------------------------------------------------------------------------ options: - subjectEl - subjectCenter */ -var CellDragListener = DragListener.extend({ +var HitDragListener = DragListener.extend({ - coordMap: null, // converts coordinates to date cells - origCell: null, // the cell the mouse was over when listening started - cell: null, // the cell the mouse is over + component: null, // converts coordinates to hits + // methods: hitsNeeded, hitsNotNeeded, queryHit + + origHit: null, // the hit the mouse was over when listening started + hit: null, // the hit the mouse is over coordAdjust: null, // delta that will be added to the mouse coordinates when computing collisions - constructor: function(coordMap, options) { - DragListener.prototype.constructor.call(this, options); // call the super-constructor + constructor: function(component, options) { + DragListener.call(this, options); // call the super-constructor - this.coordMap = coordMap; + this.component = component; }, // Called when drag listening starts (but a real drag has not necessarily began). // ev might be undefined if dragging was started manually. - listenStart: function(ev) { + handleInteractionStart: function(ev) { var subjectEl = this.subjectEl; var subjectRect; var origPoint; var point; - DragListener.prototype.listenStart.apply(this, arguments); // call the super-method - - this.computeCoords(); + this.component.hitsNeeded(); + this.computeScrollBounds(); // for autoscroll if (ev) { - origPoint = { left: ev.pageX, top: ev.pageY }; + origPoint = { left: getEvX(ev), top: getEvY(ev) }; point = origPoint; // constrain the point to bounds of the element being dragged @@ -2358,14 +3673,15 @@ var CellDragListener = DragListener.extend({ point = constrainPoint(point, subjectRect); } - this.origCell = this.getCell(point.left, point.top); + this.origHit = this.queryHit(point.left, point.top); // treat the center of the subject as the collision point? if (subjectEl && this.options.subjectCenter) { - // only consider the area the subject overlaps the cell. best for large subjects - if (this.origCell) { - subjectRect = intersectRects(this.origCell, subjectRect) || + // only consider the area the subject overlaps the hit. best for large subjects. + // TODO: skip this if hit didn't supply left/right/top/bottom + if (this.origHit) { + subjectRect = intersectRects(this.origHit, subjectRect) || subjectRect; // in case there is no intersection } @@ -2375,141 +3691,385 @@ var CellDragListener = DragListener.extend({ this.coordAdjust = diffPoints(point, origPoint); // point - origPoint } else { - this.origCell = null; + this.origHit = null; this.coordAdjust = null; } - }, - - // Recomputes the drag-critical positions of elements - computeCoords: function() { - this.coordMap.build(); - this.computeScrollBounds(); + // call the super-method. do it after origHit has been computed + DragListener.prototype.handleInteractionStart.apply(this, arguments); }, // Called when the actual drag has started - dragStart: function(ev) { - var cell; + handleDragStart: function(ev) { + var hit; - DragListener.prototype.dragStart.apply(this, arguments); // call the super-method + DragListener.prototype.handleDragStart.apply(this, arguments); // call the super-method - cell = this.getCell(ev.pageX, ev.pageY); // might be different from this.origCell if the min-distance is large + // might be different from this.origHit if the min-distance is large + hit = this.queryHit(getEvX(ev), getEvY(ev)); - // report the initial cell the mouse is over + // report the initial hit the mouse is over // especially important if no min-distance and drag starts immediately - if (cell) { - this.cellOver(cell); + if (hit) { + this.handleHitOver(hit); } }, // Called when the drag moves - drag: function(dx, dy, ev) { - var cell; + handleDrag: function(dx, dy, ev) { + var hit; - DragListener.prototype.drag.apply(this, arguments); // call the super-method + DragListener.prototype.handleDrag.apply(this, arguments); // call the super-method - cell = this.getCell(ev.pageX, ev.pageY); + hit = this.queryHit(getEvX(ev), getEvY(ev)); - if (!isCellsEqual(cell, this.cell)) { // a different cell than before? - if (this.cell) { - this.cellOut(); + if (!isHitsEqual(hit, this.hit)) { // a different hit than before? + if (this.hit) { + this.handleHitOut(); } - if (cell) { - this.cellOver(cell); + if (hit) { + this.handleHitOver(hit); } } }, // Called when dragging has been stopped - dragStop: function() { - this.cellDone(); - DragListener.prototype.dragStop.apply(this, arguments); // call the super-method + handleDragEnd: function() { + this.handleHitDone(); + DragListener.prototype.handleDragEnd.apply(this, arguments); // call the super-method }, - // Called when a the mouse has just moved over a new cell - cellOver: function(cell) { - this.cell = cell; - this.trigger('cellOver', cell, isCellsEqual(cell, this.origCell), this.origCell); + // Called when a the mouse has just moved over a new hit + handleHitOver: function(hit) { + var isOrig = isHitsEqual(hit, this.origHit); + + this.hit = hit; + + this.trigger('hitOver', this.hit, isOrig, this.origHit); }, - // Called when the mouse has just moved out of a cell - cellOut: function() { - if (this.cell) { - this.trigger('cellOut', this.cell); - this.cellDone(); - this.cell = null; + // Called when the mouse has just moved out of a hit + handleHitOut: function() { + if (this.hit) { + this.trigger('hitOut', this.hit); + this.handleHitDone(); + this.hit = null; } }, - // Called after a cellOut. Also called before a dragStop - cellDone: function() { - if (this.cell) { - this.trigger('cellDone', this.cell); + // Called after a hitOut. Also called before a dragStop + handleHitDone: function() { + if (this.hit) { + this.trigger('hitDone', this.hit); } }, - // Called when drag listening has stopped - listenStop: function() { - DragListener.prototype.listenStop.apply(this, arguments); // call the super-method + // Called when the interaction ends, whether there was a real drag or not + handleInteractionEnd: function() { + DragListener.prototype.handleInteractionEnd.apply(this, arguments); // call the super-method - this.origCell = this.cell = null; - this.coordMap.clear(); + this.origHit = null; + this.hit = null; + + this.component.hitsNotNeeded(); }, // Called when scrolling has stopped, whether through auto scroll, or the user scrolling - scrollStop: function() { - DragListener.prototype.scrollStop.apply(this, arguments); // call the super-method + handleScrollEnd: function() { + DragListener.prototype.handleScrollEnd.apply(this, arguments); // call the super-method - this.computeCoords(); // cells' absolute positions will be in new places. recompute + // hits' absolute positions will be in new places after a user's scroll. + // HACK for recomputing. + if (this.isDragging) { + this.component.releaseHits(); + this.component.prepareHits(); + } }, - // Gets the cell underneath the coordinates for the given mouse event - getCell: function(left, top) { + // Gets the hit underneath the coordinates for the given mouse event + queryHit: function(left, top) { if (this.coordAdjust) { left += this.coordAdjust.left; top += this.coordAdjust.top; } - return this.coordMap.getCell(left, top); + return this.component.queryHit(left, top); } }); -// Returns `true` if the cells are identically equal. `false` otherwise. -// They must have the same row, col, and be from the same grid. -// Two null values will be considered equal, as two "out of the grid" states are the same. -function isCellsEqual(cell1, cell2) { +// Returns `true` if the hits are identically equal. `false` otherwise. Must be from the same component. +// Two null values will be considered equal, as two "out of the component" states are the same. +function isHitsEqual(hit0, hit1) { - if (!cell1 && !cell2) { + if (!hit0 && !hit1) { return true; } - if (cell1 && cell2) { - return cell1.grid === cell2.grid && - cell1.row === cell2.row && - cell1.col === cell2.col; + if (hit0 && hit1) { + return hit0.component === hit1.component && + isHitPropsWithin(hit0, hit1) && + isHitPropsWithin(hit1, hit0); // ensures all props are identical } return false; } + +// Returns true if all of subHit's non-standard properties are within superHit +function isHitPropsWithin(subHit, superHit) { + for (var propName in subHit) { + if (!/^(component|left|right|top|bottom)$/.test(propName)) { + if (subHit[propName] !== superHit[propName]) { + return false; + } + } + } + return true; +} + +;; + +/* +Listens to document and window-level user-interaction events, like touch events and mouse events, +and fires these events as-is to whoever is observing a GlobalEmitter. +Best when used as a singleton via GlobalEmitter.get() + +Normalizes mouse/touch events. For examples: +- ignores the the simulated mouse events that happen after a quick tap: mousemove+mousedown+mouseup+click +- compensates for various buggy scenarios where a touchend does not fire +*/ + +FC.touchMouseIgnoreWait = 500; + +var GlobalEmitter = Class.extend(ListenerMixin, EmitterMixin, { + + isTouching: false, + mouseIgnoreDepth: 0, + handleScrollProxy: null, + + + bind: function() { + var _this = this; + + this.listenTo($(document), { + touchstart: this.handleTouchStart, + touchcancel: this.handleTouchCancel, + touchend: this.handleTouchEnd, + mousedown: this.handleMouseDown, + mousemove: this.handleMouseMove, + mouseup: this.handleMouseUp, + click: this.handleClick, + selectstart: this.handleSelectStart, + contextmenu: this.handleContextMenu + }); + + // because we need to call preventDefault + // because https://www.chromestatus.com/features/5093566007214080 + // TODO: investigate performance because this is a global handler + window.addEventListener( + 'touchmove', + this.handleTouchMoveProxy = function(ev) { + _this.handleTouchMove($.Event(ev)); + }, + { passive: false } // allows preventDefault() + ); + + // attach a handler to get called when ANY scroll action happens on the page. + // this was impossible to do with normal on/off because 'scroll' doesn't bubble. + // http://stackoverflow.com/a/32954565/96342 + window.addEventListener( + 'scroll', + this.handleScrollProxy = function(ev) { + _this.handleScroll($.Event(ev)); + }, + true // useCapture + ); + }, + + unbind: function() { + this.stopListeningTo($(document)); + + window.removeEventListener( + 'touchmove', + this.handleTouchMoveProxy + ); + + window.removeEventListener( + 'scroll', + this.handleScrollProxy, + true // useCapture + ); + }, + + + // Touch Handlers + // ----------------------------------------------------------------------------------------------------------------- + + handleTouchStart: function(ev) { + + // if a previous touch interaction never ended with a touchend, then implicitly end it, + // but since a new touch interaction is about to begin, don't start the mouse ignore period. + this.stopTouch(ev, true); // skipMouseIgnore=true + + this.isTouching = true; + this.trigger('touchstart', ev); + }, + + handleTouchMove: function(ev) { + if (this.isTouching) { + this.trigger('touchmove', ev); + } + }, + + handleTouchCancel: function(ev) { + if (this.isTouching) { + this.trigger('touchcancel', ev); + + // Have touchcancel fire an artificial touchend. That way, handlers won't need to listen to both. + // If touchend fires later, it won't have any effect b/c isTouching will be false. + this.stopTouch(ev); + } + }, + + handleTouchEnd: function(ev) { + this.stopTouch(ev); + }, + + + // Mouse Handlers + // ----------------------------------------------------------------------------------------------------------------- + + handleMouseDown: function(ev) { + if (!this.shouldIgnoreMouse()) { + this.trigger('mousedown', ev); + } + }, + + handleMouseMove: function(ev) { + if (!this.shouldIgnoreMouse()) { + this.trigger('mousemove', ev); + } + }, + + handleMouseUp: function(ev) { + if (!this.shouldIgnoreMouse()) { + this.trigger('mouseup', ev); + } + }, + + handleClick: function(ev) { + if (!this.shouldIgnoreMouse()) { + this.trigger('click', ev); + } + }, + + + // Misc Handlers + // ----------------------------------------------------------------------------------------------------------------- + + handleSelectStart: function(ev) { + this.trigger('selectstart', ev); + }, + + handleContextMenu: function(ev) { + this.trigger('contextmenu', ev); + }, + + handleScroll: function(ev) { + this.trigger('scroll', ev); + }, + + + // Utils + // ----------------------------------------------------------------------------------------------------------------- + + stopTouch: function(ev, skipMouseIgnore) { + if (this.isTouching) { + this.isTouching = false; + this.trigger('touchend', ev); + + if (!skipMouseIgnore) { + this.startTouchMouseIgnore(); + } + } + }, + + startTouchMouseIgnore: function() { + var _this = this; + var wait = FC.touchMouseIgnoreWait; + + if (wait) { + this.mouseIgnoreDepth++; + setTimeout(function() { + _this.mouseIgnoreDepth--; + }, wait); + } + }, + + shouldIgnoreMouse: function() { + return this.isTouching || Boolean(this.mouseIgnoreDepth); + } + +}); + + +// Singleton +// --------------------------------------------------------------------------------------------------------------------- + +(function() { + var globalEmitter = null; + var neededCount = 0; + + + // gets the singleton + GlobalEmitter.get = function() { + + if (!globalEmitter) { + globalEmitter = new GlobalEmitter(); + globalEmitter.bind(); + } + + return globalEmitter; + }; + + + // called when an object knows it will need a GlobalEmitter in the near future. + GlobalEmitter.needed = function() { + GlobalEmitter.get(); // ensures globalEmitter + neededCount++; + }; + + + // called when the object that originally called needed() doesn't need a GlobalEmitter anymore. + GlobalEmitter.unneeded = function() { + neededCount--; + + if (!neededCount) { // nobody else needs it + globalEmitter.unbind(); + globalEmitter = null; + } + }; + +})(); + ;; /* Creates a clone of an element and lets it track the mouse as it moves ----------------------------------------------------------------------------------------------------------------------*/ -var MouseFollower = Class.extend({ +var MouseFollower = Class.extend(ListenerMixin, { options: null, @@ -2521,16 +4081,14 @@ var MouseFollower = Class.extend({ top0: null, left0: null, - // the initial position of the mouse - mouseY0: null, - mouseX0: null, + // the absolute coordinates of the initiating touch/mouse action + y0: null, + x0: null, // the number of pixels the mouse has moved from its initial position topDelta: null, leftDelta: null, - mousemoveProxy: null, // document mousemove handler, bound to the MouseFollower's `this` - isFollowing: false, isHidden: false, isAnimating: false, // doing the revert animation? @@ -2547,8 +4105,8 @@ var MouseFollower = Class.extend({ if (!this.isFollowing) { this.isFollowing = true; - this.mouseY0 = ev.pageY; - this.mouseX0 = ev.pageX; + this.y0 = getEvY(ev); + this.x0 = getEvX(ev); this.topDelta = 0; this.leftDelta = 0; @@ -2556,7 +4114,12 @@ var MouseFollower = Class.extend({ this.updatePosition(); } - $(document).on('mousemove', this.mousemoveProxy = proxy(this, 'mousemove')); + if (getEvIsTouch(ev)) { + this.listenTo($(document), 'touchmove', this.handleMove); + } + else { + this.listenTo($(document), 'mousemove', this.handleMove); + } } }, @@ -2567,11 +4130,11 @@ var MouseFollower = Class.extend({ var _this = this; var revertDuration = this.options.revertDuration; - function complete() { - this.isAnimating = false; + function complete() { // might be called by .animate(), which might change `this` context + _this.isAnimating = false; _this.removeElement(); - this.top0 = this.left0 = null; // reset state for future updatePosition calls + _this.top0 = _this.left0 = null; // reset state for future updatePosition calls if (callback) { callback(); @@ -2581,7 +4144,7 @@ var MouseFollower = Class.extend({ if (this.isFollowing && !this.isAnimating) { // disallow more than one stop animation at a time this.isFollowing = false; - $(document).off('mousemove', this.mousemoveProxy); + this.stopListeningTo($(document)); if (shouldRevert && revertDuration && !this.isHidden) { // do a revert animation? this.isAnimating = true; @@ -2605,8 +4168,8 @@ var MouseFollower = Class.extend({ var el = this.el; if (!el) { - this.sourceEl.width(); // hack to force IE8 to compute correct bounding box el = this.el = this.sourceEl.clone() + .addClass(this.options.additionalClass || '') .css({ position: 'absolute', visibility: '', // in case original element was hidden (commonly through hideEvents()) @@ -2618,8 +4181,13 @@ var MouseFollower = Class.extend({ height: this.sourceEl.height(), // explicit width in case there was a 'bottom' value opacity: this.options.opacity || '', zIndex: this.options.zIndex - }) - .appendTo(this.parentEl); + }); + + // we don't want long taps or any mouse interaction causing selection/menus. + // would use preventSelection(), but that prevents selectstart, causing problems. + el.addClass('fc-unselectable'); + + el.appendTo(this.parentEl); } return el; @@ -2644,7 +4212,6 @@ var MouseFollower = Class.extend({ // make sure origin info was computed if (this.top0 === null) { - this.sourceEl.width(); // hack to force IE8 to compute correct bounding box sourceOffset = this.sourceEl.offset(); origin = this.el.offsetParent().offset(); this.top0 = sourceOffset.top - origin.top; @@ -2659,9 +4226,9 @@ var MouseFollower = Class.extend({ // Gets called when the user moves the mouse - mousemove: function(ev) { - this.topDelta = ev.pageY - this.mouseY0; - this.leftDelta = ev.pageX - this.mouseX0; + handleMove: function(ev) { + this.topDelta = getEvY(ev) - this.y0; + this.leftDelta = getEvX(ev) - this.x0; if (!this.isHidden) { this.updatePosition(); @@ -2693,148 +4260,49 @@ var MouseFollower = Class.extend({ ;; -/* A utility class for rendering rows. +/* An abstract class comprised of a "grid" of areas that each represent a specific datetime ----------------------------------------------------------------------------------------------------------------------*/ -// It leverages methods of the subclass and the View to determine custom rendering behavior for each row "type" -// (such as highlight rows, day rows, helper rows, etc). -var RowRenderer = Class.extend({ +var Grid = FC.Grid = Class.extend(ListenerMixin, { + + // self-config, overridable by subclasses + hasDayInteractions: true, // can user click/select ranges of time? view: null, // a View object isRTL: null, // shortcut to the view's isRTL option - cellHtml: '', // plain default HTML used for a cell when no other is available + + start: null, + end: null, + + el: null, // the containing element + elsByFill: null, // a hash of jQuery element sets used for rendering each fill. Keyed by fill name. + + // derived from options + eventTimeFormat: null, + displayEventTime: null, + displayEventEnd: null, + + minResizeDuration: null, // TODO: hack. set by subclasses. minumum event resize duration + + // if defined, holds the unit identified (ex: "year" or "month") that determines the level of granularity + // of the date areas. if not defined, assumes to be day and time granularity. + // TODO: port isTimeScale into same system? + largeUnit: null, + + dayClickListener: null, + daySelectListener: null, + segDragListener: null, + segResizeListener: null, + externalDragListener: null, constructor: function(view) { this.view = view; this.isRTL = view.opt('isRTL'); - }, - - - // Renders the HTML for a row, leveraging custom cell-HTML-renderers based on the `rowType`. - // Also applies the "intro" and "outro" cells, which are specified by the subclass and views. - // `row` is an optional row number. - rowHtml: function(rowType, row) { - var renderCell = this.getHtmlRenderer('cell', rowType); - var rowCellHtml = ''; - var col; - var cell; - - row = row || 0; - - for (col = 0; col < this.colCnt; col++) { - cell = this.getCell(row, col); - rowCellHtml += renderCell(cell); - } - - rowCellHtml = this.bookendCells(rowCellHtml, rowType, row); // apply intro and outro - - return '' + rowCellHtml + ''; - }, - - - // Applies the "intro" and "outro" HTML to the given cells. - // Intro means the leftmost cell when the calendar is LTR and the rightmost cell when RTL. Vice-versa for outro. - // `cells` can be an HTML string of 's or a jQuery element - // `row` is an optional row number. - bookendCells: function(cells, rowType, row) { - var intro = this.getHtmlRenderer('intro', rowType)(row || 0); - var outro = this.getHtmlRenderer('outro', rowType)(row || 0); - var prependHtml = this.isRTL ? outro : intro; - var appendHtml = this.isRTL ? intro : outro; - - if (typeof cells === 'string') { - return prependHtml + cells + appendHtml; - } - else { // a jQuery element - return cells.prepend(prependHtml).append(appendHtml); - } - }, - - - // Returns an HTML-rendering function given a specific `rendererName` (like cell, intro, or outro) and a specific - // `rowType` (like day, eventSkeleton, helperSkeleton), which is optional. - // If a renderer for the specific rowType doesn't exist, it will fall back to a generic renderer. - // We will query the View object first for any custom rendering functions, then the methods of the subclass. - getHtmlRenderer: function(rendererName, rowType) { - var view = this.view; - var generalName; // like "cellHtml" - var specificName; // like "dayCellHtml". based on rowType - var provider; // either the View or the RowRenderer subclass, whichever provided the method - var renderer; - - generalName = rendererName + 'Html'; - if (rowType) { - specificName = rowType + capitaliseFirstLetter(rendererName) + 'Html'; - } - - if (specificName && (renderer = view[specificName])) { - provider = view; - } - else if (specificName && (renderer = this[specificName])) { - provider = this; - } - else if ((renderer = view[generalName])) { - provider = view; - } - else if ((renderer = this[generalName])) { - provider = this; - } - - if (typeof renderer === 'function') { - return function() { - return renderer.apply(provider, arguments) || ''; // use correct `this` and always return a string - }; - } - - // the rendered can be a plain string as well. if not specified, always an empty string. - return function() { - return renderer || ''; - }; - } - -}); - -;; - -/* An abstract class comprised of a "grid" of cells that each represent a specific datetime -----------------------------------------------------------------------------------------------------------------------*/ - -var Grid = fc.Grid = RowRenderer.extend({ - - start: null, // the date of the first cell - end: null, // the date after the last cell - - rowCnt: 0, // number of rows - colCnt: 0, // number of cols - - el: null, // the containing element - coordMap: null, // a GridCoordMap that converts pixel values to datetimes - elsByFill: null, // a hash of jQuery element sets used for rendering each fill. Keyed by fill name. - - externalDragStartProxy: null, // binds the Grid's scope to externalDragStart (in DayGrid.events) - - // derived from options - colHeadFormat: null, // TODO: move to another class. not applicable to all Grids - eventTimeFormat: null, - displayEventTime: null, - displayEventEnd: null, - - // if all cells are the same length of time, the duration they all share. optional. - // when defined, allows the computeCellRange shortcut, as well as improved resizing behavior. - cellDuration: null, - - // if defined, holds the unit identified (ex: "year" or "month") that determines the level of granularity - // of the date cells. if not defined, assumes to be day and time granularity. - largeUnit: null, - - - constructor: function() { - RowRenderer.apply(this, arguments); // call the super-constructor - - this.coordMap = new GridCoordMap(this); this.elsByFill = {}; - this.externalDragStartProxy = proxy(this, 'externalDragStart'); + + this.dayClickListener = this.buildDayClickListener(); + this.daySelectListener = this.buildDaySelectListener(); }, @@ -2842,13 +4310,6 @@ var Grid = fc.Grid = RowRenderer.extend({ ------------------------------------------------------------------------------------------------------------------*/ - // Generates the format string used for the text in column headers, if not explicitly defined by 'columnFormat' - // TODO: move to another class. not applicable to all Grids - computeColHeadFormat: function() { - // subclasses must implement if they want to use headHtml() - }, - - // Generates the format string used for event time text, if not explicitly defined by 'timeFormat' computeEventTimeFormat: function() { return this.view.opt('smallTimeFormat'); @@ -2873,7 +4334,7 @@ var Grid = fc.Grid = RowRenderer.extend({ // Tells the grid about what period of time to display. - // Any date-related cell system internal data should be generated. + // Any date-related internal data should be generated. setRange: function(range) { this.start = range.start.clone(); this.end = range.end.clone(); @@ -2894,9 +4355,6 @@ var Grid = fc.Grid = RowRenderer.extend({ var displayEventTime; var displayEventEnd; - // Populate option-derived settings. Look for override first, then compute if necessary. - this.colHeadFormat = view.opt('columnFormat') || this.computeColHeadFormat(); - this.eventTimeFormat = view.opt('eventTimeFormat') || view.opt('timeFormat') || // deprecated @@ -2917,25 +4375,15 @@ var Grid = fc.Grid = RowRenderer.extend({ }, - // Called before the grid's coordinates will need to be queried for cells. - // Any non-date-related cell system internal data should be built. - build: function() { - }, - - - // Called after the grid's coordinates are done being relied upon. - // Any non-date-related cell system internal data should be cleared. - clear: function() { - }, - - - // Converts a range with an inclusive `start` and an exclusive `end` into an array of segment objects - rangeToSegs: function(range) { + // Converts a span (has unzoned start/end and any other grid-specific location information) + // into an array of segments (pieces of events whose format is decided by the grid). + spanToSegs: function(span) { // subclasses must implement }, // Diffs the two dates, returning a duration, based on granularity of the grid + // TODO: port isTimeScale into this system? diffDates: function(a, b) { if (this.largeUnit) { return diffByUnit(a, b, this.largeUnit); @@ -2946,126 +4394,63 @@ var Grid = fc.Grid = RowRenderer.extend({ }, - /* Cells - ------------------------------------------------------------------------------------------------------------------*/ - // NOTE: columns are ordered left-to-right - - - // Gets an object containing row/col number, misc data, and range information about the cell. - // Accepts row/col values, an object with row/col properties, or a single-number offset from the first cell. - getCell: function(row, col) { - var cell; - - if (col == null) { - if (typeof row === 'number') { // a single-number offset - col = row % this.colCnt; - row = Math.floor(row / this.colCnt); - } - else { // an object with row/col properties - col = row.col; - row = row.row; - } - } - - cell = { row: row, col: col }; - - $.extend(cell, this.getRowData(row), this.getColData(col)); - $.extend(cell, this.computeCellRange(cell)); - - return cell; - }, - - - // Given a cell object with index and misc data, generates a range object - // If the grid is leveraging cellDuration, this doesn't need to be defined. Only computeCellDate does. - // If being overridden, should return a range with reference-free date copies. - computeCellRange: function(cell) { - var date = this.computeCellDate(cell); - - return { - start: date, - end: date.clone().add(this.cellDuration) - }; - }, - - - // Given a cell, returns its start date. Should return a reference-free date copy. - computeCellDate: function(cell) { - // subclasses can implement - }, - - - // Retrieves misc data about the given row - getRowData: function(row) { - return {}; - }, - - - // Retrieves misc data baout the given column - getColData: function(col) { - return {}; - }, - - - // Retrieves the element representing the given row - getRowEl: function(row) { - // subclasses should implement if leveraging the default getCellDayEl() or computeRowCoords() - }, - - - // Retrieves the element representing the given column - getColEl: function(col) { - // subclasses should implement if leveraging the default getCellDayEl() or computeColCoords() - }, - - - // Given a cell object, returns the element that represents the cell's whole-day - getCellDayEl: function(cell) { - return this.getColEl(cell.col) || this.getRowEl(cell.row); - }, - - - /* Cell Coordinates + /* Hit Area ------------------------------------------------------------------------------------------------------------------*/ + hitsNeededDepth: 0, // necessary because multiple callers might need the same hits - // Computes the top/bottom coordinates of all rows. - // By default, queries the dimensions of the element provided by getRowEl(). - computeRowCoords: function() { - var items = []; - var i, el; - var top; - - for (i = 0; i < this.rowCnt; i++) { - el = this.getRowEl(i); - top = el.offset().top; - items.push({ - top: top, - bottom: top + el.outerHeight() - }); + hitsNeeded: function() { + if (!(this.hitsNeededDepth++)) { + this.prepareHits(); } + }, - return items; + hitsNotNeeded: function() { + if (this.hitsNeededDepth && !(--this.hitsNeededDepth)) { + this.releaseHits(); + } }, - // Computes the left/right coordinates of all rows. - // By default, queries the dimensions of the element provided by getColEl(). Columns can be LTR or RTL. - computeColCoords: function() { - var items = []; - var i, el; - var left; + // Called before one or more queryHit calls might happen. Should prepare any cached coordinates for queryHit + prepareHits: function() { + }, - for (i = 0; i < this.colCnt; i++) { - el = this.getColEl(i); - left = el.offset().left; - items.push({ - left: left, - right: left + el.outerWidth() - }); + + // Called when queryHit calls have subsided. Good place to clear any coordinate caches. + releaseHits: function() { + }, + + + // Given coordinates from the topleft of the document, return data about the date-related area underneath. + // Can return an object with arbitrary properties (although top/right/left/bottom are encouraged). + // Must have a `grid` property, a reference to this current grid. TODO: avoid this + // The returned object will be processed by getHitSpan and getHitEl. + queryHit: function(leftOffset, topOffset) { + }, + + + // like getHitSpan, but returns null if the resulting span's range is invalid + getSafeHitSpan: function(hit) { + var hitSpan = this.getHitSpan(hit); + + if (!isRangeWithinRange(hitSpan, this.view.activeRange)) { + return null; } - return items; + return hitSpan; + }, + + + // Given position-level information about a date-related area within the grid, + // should return an object with at least a start/end date. Can provide other information as well. + getHitSpan: function(hit) { + }, + + + // Given position-level information about a date-related area within the grid, + // should return a jQuery element that best represents it. passed to dayClick callback. + getHitEl: function(hit) { }, @@ -3076,20 +4461,14 @@ var Grid = fc.Grid = RowRenderer.extend({ // Sets the container element that the grid should render inside of. // Does other DOM-related initializations. setElement: function(el) { - var _this = this; - this.el = el; - // attach a handler to the grid's root element. - // jQuery will take care of unregistering them when removeElement gets called. - el.on('mousedown', function(ev) { - if ( - !$(ev.target).is('.fc-event-container *, .fc-more') && // not an an event element, or "more.." link - !$(ev.target).closest('.fc-popover').length // not on a popover (like the "more.." events one) - ) { - _this.dayMousedown(ev); - } - }); + if (this.hasDayInteractions) { + preventSelection(el); + + this.bindDayHandler('touchstart', this.dayTouchStart); + this.bindDayHandler('mousedown', this.dayMousedown); + } // attach event-element-related handlers. in Grid.events // same garbage collection note as above. @@ -3099,10 +4478,31 @@ var Grid = fc.Grid = RowRenderer.extend({ }, + bindDayHandler: function(name, handler) { + var _this = this; + + // attach a handler to the grid's root element. + // jQuery will take care of unregistering them when removeElement gets called. + this.el.on(name, function(ev) { + if ( + !$(ev.target).is( + _this.segSelector + ',' + // directly on an event element + _this.segSelector + ' *,' + // within an event element + '.fc-more,' + // a "more.." link + 'a[data-goto]' // a clickable nav link + ) + ) { + return handler.call(_this, ev); + } + }); + }, + + // Removes the grid's container element from the DOM. Undoes any other DOM-related attachments. // DOES NOT remove any content beforehand (doesn't clear events or call unrenderDates), unlike View removeElement: function() { this.unbindGlobalHandlers(); + this.clearDragListeners(); this.el.remove(); @@ -3116,7 +4516,7 @@ var Grid = fc.Grid = RowRenderer.extend({ }, - // Renders the grid's date-related content (like cells that represent days/times). + // Renders the grid's date-related content (like areas that represent days/times). // Assumes setRange has already been called and the skeleton has already been rendered. renderDates: function() { // subclasses should implement @@ -3135,66 +4535,182 @@ var Grid = fc.Grid = RowRenderer.extend({ // Binds DOM handlers to elements that reside outside the grid, such as the document bindGlobalHandlers: function() { - $(document).on('dragstart sortstart', this.externalDragStartProxy); // jqui + this.listenTo($(document), { + dragstart: this.externalDragStart, // jqui + sortstart: this.externalDragStart // jqui + }); }, // Unbinds DOM handlers from elements that reside outside the grid unbindGlobalHandlers: function() { - $(document).off('dragstart sortstart', this.externalDragStartProxy); // jqui + this.stopListeningTo($(document)); }, // Process a mousedown on an element that represents a day. For day clicking and selecting. dayMousedown: function(ev) { + var view = this.view; + + // HACK + // This will still work even though bindDayHandler doesn't use GlobalEmitter. + if (GlobalEmitter.get().shouldIgnoreMouse()) { + return; + } + + this.dayClickListener.startInteraction(ev); + + if (view.opt('selectable')) { + this.daySelectListener.startInteraction(ev, { + distance: view.opt('selectMinDistance') + }); + } + }, + + + dayTouchStart: function(ev) { + var view = this.view; + var selectLongPressDelay; + + // On iOS (and Android?) when a new selection is initiated overtop another selection, + // the touchend never fires because the elements gets removed mid-touch-interaction (my theory). + // HACK: simply don't allow this to happen. + // ALSO: prevent selection when an *event* is already raised. + if (view.isSelected || view.selectedEvent) { + return; + } + + selectLongPressDelay = view.opt('selectLongPressDelay'); + if (selectLongPressDelay == null) { + selectLongPressDelay = view.opt('longPressDelay'); // fallback + } + + this.dayClickListener.startInteraction(ev); + + if (view.opt('selectable')) { + this.daySelectListener.startInteraction(ev, { + delay: selectLongPressDelay + }); + } + }, + + + // Creates a listener that tracks the user's drag across day elements, for day clicking. + buildDayClickListener: function() { var _this = this; var view = this.view; - var isSelectable = view.opt('selectable'); - var dayClickCell; // null if invalid dayClick - var selectionRange; // null if invalid selection + var dayClickHit; // null if invalid dayClick - // this listener tracks a mousedown on a day element, and a subsequent drag. - // if the drag ends on the same day, it is a 'dayClick'. - // if 'selectable' is enabled, this listener also detects selections. - var dragListener = new CellDragListener(this.coordMap, { - //distance: 5, // needs more work if we want dayClick to fire correctly + var dragListener = new HitDragListener(this, { scroll: view.opt('dragScroll'), - dragStart: function() { - view.unselect(); // since we could be rendering a new selection, we want to clear any old one + interactionStart: function() { + dayClickHit = dragListener.origHit; }, - cellOver: function(cell, isOrig, origCell) { - if (origCell) { // click needs to have started on a cell - dayClickCell = isOrig ? cell : null; // single-cell selection is a day click - if (isSelectable) { - selectionRange = _this.computeSelection(origCell, cell); - if (selectionRange) { - _this.renderSelection(selectionRange); - } - else { - disableCursor(); - } + hitOver: function(hit, isOrig, origHit) { + // if user dragged to another cell at any point, it can no longer be a dayClick + if (!isOrig) { + dayClickHit = null; + } + }, + hitOut: function() { // called before mouse moves to a different hit OR moved out of all hits + dayClickHit = null; + }, + interactionEnd: function(ev, isCancelled) { + var hitSpan; + + if (!isCancelled && dayClickHit) { + hitSpan = _this.getSafeHitSpan(dayClickHit); + + if (hitSpan) { + view.triggerDayClick(hitSpan, _this.getHitEl(dayClickHit), ev); } } - }, - cellOut: function(cell) { - dayClickCell = null; - selectionRange = null; - _this.unrenderSelection(); - enableCursor(); - }, - listenStop: function(ev) { - if (dayClickCell) { - view.triggerDayClick(dayClickCell, _this.getCellDayEl(dayClickCell), ev); - } - if (selectionRange) { - // the selection will already have been rendered. just report it - view.reportSelection(selectionRange, ev); - } - enableCursor(); } }); - dragListener.mousedown(ev); // start listening, which will eventually initiate a dragStart + // because dayClickListener won't be called with any time delay, "dragging" will begin immediately, + // which will kill any touchmoving/scrolling. Prevent this. + dragListener.shouldCancelTouchScroll = false; + + dragListener.scrollAlwaysKills = true; + + return dragListener; + }, + + + // Creates a listener that tracks the user's drag across day elements, for day selecting. + buildDaySelectListener: function() { + var _this = this; + var view = this.view; + var selectionSpan; // null if invalid selection + + var dragListener = new HitDragListener(this, { + scroll: view.opt('dragScroll'), + interactionStart: function() { + selectionSpan = null; + }, + dragStart: function() { + view.unselect(); // since we could be rendering a new selection, we want to clear any old one + }, + hitOver: function(hit, isOrig, origHit) { + var origHitSpan; + var hitSpan; + + if (origHit) { // click needs to have started on a hit + + origHitSpan = _this.getSafeHitSpan(origHit); + hitSpan = _this.getSafeHitSpan(hit); + + if (origHitSpan && hitSpan) { + selectionSpan = _this.computeSelection(origHitSpan, hitSpan); + } + else { + selectionSpan = null; + } + + if (selectionSpan) { + _this.renderSelection(selectionSpan); + } + else if (selectionSpan === false) { + disableCursor(); + } + } + }, + hitOut: function() { // called before mouse moves to a different hit OR moved out of all hits + selectionSpan = null; + _this.unrenderSelection(); + }, + hitDone: function() { // called after a hitOut OR before a dragEnd + enableCursor(); + }, + interactionEnd: function(ev, isCancelled) { + if (!isCancelled && selectionSpan) { + // the selection will already have been rendered. just report it + view.reportSelection(selectionSpan, ev); + } + } + }); + + return dragListener; + }, + + + // Kills all in-progress dragging. + // Useful for when public API methods that result in re-rendering are invoked during a drag. + // Also useful for when touch devices misbehave and don't fire their touchend. + clearDragListeners: function() { + this.dayClickListener.endInteraction(); + this.daySelectListener.endInteraction(); + + if (this.segDragListener) { + this.segDragListener.endInteraction(); // will clear this.segDragListener + } + if (this.segResizeListener) { + this.segResizeListener.endInteraction(); // will clear this.segResizeListener + } + if (this.externalDragListener) { + this.externalDragListener.endInteraction(); // will clear this.externalDragListener + } }, @@ -3203,24 +4719,25 @@ var Grid = fc.Grid = RowRenderer.extend({ // TODO: should probably move this to Grid.events, like we did event dragging / resizing - // Renders a mock event over the given range - renderRangeHelper: function(range, sourceSeg) { - var fakeEvent = this.fabricateHelperEvent(range, sourceSeg); + // Renders a mock event at the given event location, which contains zoned start/end properties. + // Returns all mock event elements. + renderEventLocationHelper: function(eventLocation, sourceSeg) { + var fakeEvent = this.fabricateHelperEvent(eventLocation, sourceSeg); - this.renderHelper(fakeEvent, sourceSeg); // do the actual rendering + return this.renderHelper(fakeEvent, sourceSeg); // do the actual rendering }, - // Builds a fake event given a date range it should cover, and a segment is should be inspired from. + // Builds a fake event given zoned event date properties and a segment is should be inspired from. // The range's end can be null, in which case the mock event that is rendered will have a null end time. // `sourceSeg` is the internal segment object involved in the drag. If null, something external is dragging. - fabricateHelperEvent: function(range, sourceSeg) { + fabricateHelperEvent: function(eventLocation, sourceSeg) { var fakeEvent = sourceSeg ? createObject(sourceSeg.event) : {}; // mask the original event object if possible - fakeEvent.start = range.start.clone(); - fakeEvent.end = range.end ? range.end.clone() : null; - fakeEvent.allDay = null; // force it to be freshly computed by normalizeEventRange - this.view.calendar.normalizeEventRange(fakeEvent); + fakeEvent.start = eventLocation.start.clone(); + fakeEvent.end = eventLocation.end ? eventLocation.end.clone() : null; + fakeEvent.allDay = null; // force it to be freshly computed by normalizeEventDates + this.view.calendar.normalizeEventDates(fakeEvent); // this extra className will be useful for differentiating real events from mock events in CSS fakeEvent.className = (fakeEvent.className || []).concat('fc-helper'); @@ -3234,8 +4751,9 @@ var Grid = fc.Grid = RowRenderer.extend({ }, - // Renders a mock event - renderHelper: function(event, sourceSeg) { + // Renders a mock event. Given zoned event date properties. + // Must return all mock event elements. + renderHelper: function(eventLocation, sourceSeg) { // subclasses must implement }, @@ -3251,8 +4769,9 @@ var Grid = fc.Grid = RowRenderer.extend({ // Renders a visual indication of a selection. Will highlight by default but can be overridden by subclasses. - renderSelection: function(range) { - this.renderHighlight(this.selectionRangeToSegs(range)); + // Given a span (unzoned start/end and other misc data) + renderSelection: function(span) { + this.renderHighlight(span); }, @@ -3262,35 +4781,29 @@ var Grid = fc.Grid = RowRenderer.extend({ }, - // Given the first and last cells of a selection, returns a range object. - // Will return something falsy if the selection is invalid (when outside of selectionConstraint for example). - // Subclasses can override and provide additional data in the range object. Will be passed to renderSelection(). - computeSelection: function(firstCell, lastCell) { - var dates = [ - firstCell.start, - firstCell.end, - lastCell.start, - lastCell.end - ]; - var range; + // Given the first and last date-spans of a selection, returns another date-span object. + // Subclasses can override and provide additional data in the span object. Will be passed to renderSelection(). + // Will return false if the selection is invalid and this should be indicated to the user. + // Will return null/undefined if a selection invalid but no error should be reported. + computeSelection: function(span0, span1) { + var span = this.computeSelectionSpan(span0, span1); - dates.sort(compareNumbers); // sorts chronologically. works with Moments - - range = { - start: dates[0].clone(), - end: dates[3].clone() - }; - - if (!this.view.calendar.isSelectionRangeAllowed(range)) { - return null; + if (span && !this.view.calendar.isSelectionSpanAllowed(span)) { + return false; } - return range; + return span; }, - selectionRangeToSegs: function(range) { - return this.rangeToSegs(range); + // Given two spans, must return the combination of the two. + // TODO: do this separation of concerns (combining VS validation) for event dnd/resize too. + computeSelectionSpan: function(span0, span1) { + var dates = [ span0.start, span0.end, span1.start, span1.end ]; + + dates.sort(compareNumbers); // sorts chronologically. works with Moments + + return { start: dates[0].clone(), end: dates[3].clone() }; }, @@ -3298,9 +4811,9 @@ var Grid = fc.Grid = RowRenderer.extend({ ------------------------------------------------------------------------------------------------------------------*/ - // Renders an emphasis on the given date range. Given an array of segments. - renderHighlight: function(segs) { - this.renderFill('highlight', segs); + // Renders an emphasis on the given date range. Given a span (unzoned start/end and other misc data) + renderHighlight: function(span) { + this.renderFill('highlight', this.spanToSegs(span)); }, @@ -3316,10 +4829,40 @@ var Grid = fc.Grid = RowRenderer.extend({ }, - /* Fill System (highlight, background events, business hours) + /* Business Hours ------------------------------------------------------------------------------------------------------------------*/ + renderBusinessHours: function() { + }, + + + unrenderBusinessHours: function() { + }, + + + /* Now Indicator + ------------------------------------------------------------------------------------------------------------------*/ + + + getNowIndicatorUnit: function() { + }, + + + renderNowIndicator: function(date) { + }, + + + unrenderNowIndicator: function() { + }, + + + /* Fill System (highlight, background events, business hours) + -------------------------------------------------------------------------------------------------------------------- + TODO: remove this system. like we did in TimeGrid + */ + + // Renders a set of rectangles over the given segments of time. // MUST RETURN a subset of segs, the segs that were actually rendered. // Responsible for populating this.elsByFill. TODO: better API for expressing this requirement @@ -3387,7 +4930,7 @@ var Grid = fc.Grid = RowRenderer.extend({ fillSegTag: 'div', // subclasses can override - // Builds the HTML needed for one fill segment. Generic enought o work with different types. + // Builds the HTML needed for one fill segment. Generic enough to work with different types. fillSegHtml: function(type, seg) { // custom hooks per-type @@ -3404,75 +4947,45 @@ var Grid = fc.Grid = RowRenderer.extend({ }, + /* Generic rendering utilities for subclasses ------------------------------------------------------------------------------------------------------------------*/ - // Renders a day-of-week header row. - // TODO: move to another class. not applicable to all Grids - headHtml: function() { - return '' + - '
' + - '' + - '' + - this.rowHtml('head') + // leverages RowRenderer - '' + - '
' + - '
'; - }, - - - // Used by the `headHtml` method, via RowRenderer, for rendering the HTML of a day-of-week header cell - // TODO: move to another class. not applicable to all Grids - headCellHtml: function(cell) { + // Computes HTML classNames for a single-day element + getDayClasses: function(date, noThemeHighlight) { var view = this.view; - var date = cell.start; + var classes = []; + var today; - return '' + - '' + - htmlEscape(date.format(this.colHeadFormat)) + - ''; - }, - - - // Renders the HTML for a single-day background cell - bgCellHtml: function(cell) { - var view = this.view; - var date = cell.start; - var classes = this.getDayClasses(date); - - classes.unshift('fc-day', view.widgetContentClass); - - return ''; - }, - - - // Computes HTML classNames for a single-day cell - getDayClasses: function(date) { - var view = this.view; - var today = view.calendar.getNow().stripTime(); - var classes = [ 'fc-' + dayIDs[date.day()] ]; - - if ( - view.intervalDuration.as('months') == 1 && - date.month() != view.intervalStart.month() - ) { - classes.push('fc-other-month'); - } - - if (date.isSame(today, 'day')) { - classes.push( - 'fc-today', - view.highlightStateClass - ); - } - else if (date < today) { - classes.push('fc-past'); + if (!isDateWithinRange(date, view.activeRange)) { + classes.push('fc-disabled-day'); // TODO: jQuery UI theme? } else { - classes.push('fc-future'); + classes.push('fc-' + dayIDs[date.day()]); + + if ( + view.currentRangeAs('months') == 1 && // TODO: somehow get into MonthView + date.month() != view.currentRange.start.month() + ) { + classes.push('fc-other-month'); + } + + today = view.calendar.getNow(); + + if (date.isSame(today, 'day')) { + classes.push('fc-today'); + + if (noThemeHighlight !== true) { + classes.push(view.highlightStateClass); + } + } + else if (date < today) { + classes.push('fc-past'); + } + else { + classes.push('fc-future'); + } } return classes; @@ -3483,47 +4996,67 @@ var Grid = fc.Grid = RowRenderer.extend({ ;; /* Event-rendering and event-interaction methods for the abstract Grid class -----------------------------------------------------------------------------------------------------------------------*/ +---------------------------------------------------------------------------------------------------------------------- + +Data Types: + event - { title, id, start, (end), whatever } + location - { start, (end), allDay } + rawEventRange - { start, end } + eventRange - { start, end, isStart, isEnd } + eventSpan - { start, end, isStart, isEnd, whatever } + eventSeg - { event, whatever } + seg - { whatever } +*/ Grid.mixin({ + // self-config, overridable by subclasses + segSelector: '.fc-event-container > *', // what constitutes an event element? + mousedOverSeg: null, // the segment object the user's mouse is over. null if over nothing isDraggingSeg: false, // is a segment being dragged? boolean isResizingSeg: false, // is a segment being resized? boolean isDraggingExternal: false, // jqui-dragging an external element? boolean - segs: null, // the event segments currently rendered in the grid + segs: null, // the *event* segments currently rendered in the grid. TODO: rename to `eventSegs` // Renders the given events onto the grid renderEvents: function(events) { - var segs = this.eventsToSegs(events); - var bgSegs = []; - var fgSegs = []; - var i, seg; + var bgEvents = []; + var fgEvents = []; + var i; - for (i = 0; i < segs.length; i++) { - seg = segs[i]; - - if (isBgEvent(seg.event)) { - bgSegs.push(seg); - } - else { - fgSegs.push(seg); - } + for (i = 0; i < events.length; i++) { + (isBgEvent(events[i]) ? bgEvents : fgEvents).push(events[i]); } - // Render each different type of segment. - // Each function may return a subset of the segs, segs that were actually rendered. - bgSegs = this.renderBgSegs(bgSegs) || bgSegs; - fgSegs = this.renderFgSegs(fgSegs) || fgSegs; + this.segs = [].concat( // record all segs + this.renderBgEvents(bgEvents), + this.renderFgEvents(fgEvents) + ); + }, - this.segs = bgSegs.concat(fgSegs); + + renderBgEvents: function(events) { + var segs = this.eventsToSegs(events); + + // renderBgSegs might return a subset of segs, segs that were actually rendered + return this.renderBgSegs(segs) || segs; + }, + + + renderFgEvents: function(events) { + var segs = this.eventsToSegs(events); + + // renderFgSegs might return a subset of segs, segs that were actually rendered + return this.renderFgSegs(segs) || segs; }, // Unrenders all events currently rendered on the grid unrenderEvents: function() { - this.triggerSegMouseout(); // trigger an eventMouseout if user's mouse is over an event + this.handleSegMouseout(); // trigger an eventMouseout if user's mouse is over an event + this.clearDragListeners(); this.unrenderFgSegs(); this.unrenderBgSegs(); @@ -3618,7 +5151,7 @@ Grid.mixin({ // Generates an array of classNames to be used for the default rendering of a background event. - // Called by the fill system. + // Called by fillSegHtml. bgEventSegClasses: function(seg) { var event = seg.event; var source = event.source || {}; @@ -3631,151 +5164,286 @@ Grid.mixin({ // Generates a semicolon-separated CSS string to be used for the default rendering of a background event. - // Called by the fill system. - // TODO: consolidate with getEventSkinCss? + // Called by fillSegHtml. bgEventSegCss: function(seg) { - var view = this.view; - var event = seg.event; - var source = event.source || {}; - return { - 'background-color': - event.backgroundColor || - event.color || - source.backgroundColor || - source.color || - view.opt('eventBackgroundColor') || - view.opt('eventColor') + 'background-color': this.getSegSkinCss(seg)['background-color'] }; }, // Generates an array of classNames to be used for the rendering business hours overlay. Called by the fill system. + // Called by fillSegHtml. businessHoursSegClasses: function(seg) { return [ 'fc-nonbusiness', 'fc-bgevent' ]; }, + /* Business Hours + ------------------------------------------------------------------------------------------------------------------*/ + + + // Compute business hour segs for the grid's current date range. + // Caller must ask if whole-day business hours are needed. + // If no `businessHours` configuration value is specified, assumes the calendar default. + buildBusinessHourSegs: function(wholeDay, businessHours) { + return this.eventsToSegs( + this.buildBusinessHourEvents(wholeDay, businessHours) + ); + }, + + + // Compute business hour *events* for the grid's current date range. + // Caller must ask if whole-day business hours are needed. + // If no `businessHours` configuration value is specified, assumes the calendar default. + buildBusinessHourEvents: function(wholeDay, businessHours) { + var calendar = this.view.calendar; + var events; + + if (businessHours == null) { + // fallback + // access from calendawr. don't access from view. doesn't update with dynamic options. + businessHours = calendar.opt('businessHours'); + } + + events = calendar.computeBusinessHourEvents(wholeDay, businessHours); + + // HACK. Eventually refactor business hours "events" system. + // If no events are given, but businessHours is activated, this means the entire visible range should be + // marked as *not* business-hours, via inverse-background rendering. + if (!events.length && businessHours) { + events = [ + $.extend({}, BUSINESS_HOUR_EVENT_DEFAULTS, { + start: this.view.activeRange.end, // guaranteed out-of-range + end: this.view.activeRange.end, // " + dow: null + }) + ]; + } + + return events; + }, + + /* Handlers ------------------------------------------------------------------------------------------------------------------*/ - // Attaches event-element-related handlers to the container element and leverage bubbling + // Attaches event-element-related handlers for *all* rendered event segments of the view. bindSegHandlers: function() { + this.bindSegHandlersToEl(this.el); + }, + + + // Attaches event-element-related handlers to an arbitrary container element. leverages bubbling. + bindSegHandlersToEl: function(el) { + this.bindSegHandlerToEl(el, 'touchstart', this.handleSegTouchStart); + this.bindSegHandlerToEl(el, 'mouseenter', this.handleSegMouseover); + this.bindSegHandlerToEl(el, 'mouseleave', this.handleSegMouseout); + this.bindSegHandlerToEl(el, 'mousedown', this.handleSegMousedown); + this.bindSegHandlerToEl(el, 'click', this.handleSegClick); + }, + + + // Executes a handler for any a user-interaction on a segment. + // Handler gets called with (seg, ev), and with the `this` context of the Grid + bindSegHandlerToEl: function(el, name, handler) { var _this = this; - var view = this.view; - $.each( - { - mouseenter: function(seg, ev) { - _this.triggerSegMouseover(seg, ev); - }, - mouseleave: function(seg, ev) { - _this.triggerSegMouseout(seg, ev); - }, - click: function(seg, ev) { - return view.trigger('eventClick', this, seg.event, ev); // can return `false` to cancel - }, - mousedown: function(seg, ev) { - if ($(ev.target).is('.fc-resizer') && view.isEventResizable(seg.event)) { - _this.segResizeMousedown(seg, ev, $(ev.target).is('.fc-start-resizer')); - } - else if (view.isEventDraggable(seg.event)) { - _this.segDragMousedown(seg, ev); - } - } - }, - function(name, func) { - // attach the handler to the container element and only listen for real event elements via bubbling - _this.el.on(name, '.fc-event-container > *', function(ev) { - var seg = $(this).data('fc-seg'); // grab segment data. put there by View::renderEvents + el.on(name, this.segSelector, function(ev) { + var seg = $(this).data('fc-seg'); // grab segment data. put there by View::renderEvents - // only call the handlers if there is not a drag/resize in progress - if (seg && !_this.isDraggingSeg && !_this.isResizingSeg) { - return func.call(this, seg, ev); // `this` will be the event element - } - }); + // only call the handlers if there is not a drag/resize in progress + if (seg && !_this.isDraggingSeg && !_this.isResizingSeg) { + return handler.call(_this, seg, ev); // context will be the Grid } - ); + }); + }, + + + handleSegClick: function(seg, ev) { + var res = this.view.publiclyTrigger('eventClick', seg.el[0], seg.event, ev); // can return `false` to cancel + if (res === false) { + ev.preventDefault(); + } }, // Updates internal state and triggers handlers for when an event element is moused over - triggerSegMouseover: function(seg, ev) { - if (!this.mousedOverSeg) { + handleSegMouseover: function(seg, ev) { + if ( + !GlobalEmitter.get().shouldIgnoreMouse() && + !this.mousedOverSeg + ) { this.mousedOverSeg = seg; - this.view.trigger('eventMouseover', seg.el[0], seg.event, ev); + if (this.view.isEventResizable(seg.event)) { + seg.el.addClass('fc-allow-mouse-resize'); + } + this.view.publiclyTrigger('eventMouseover', seg.el[0], seg.event, ev); } }, // Updates internal state and triggers handlers for when an event element is moused out. // Can be given no arguments, in which case it will mouseout the segment that was previously moused over. - triggerSegMouseout: function(seg, ev) { + handleSegMouseout: function(seg, ev) { ev = ev || {}; // if given no args, make a mock mouse event if (this.mousedOverSeg) { seg = seg || this.mousedOverSeg; // if given no args, use the currently moused-over segment this.mousedOverSeg = null; - this.view.trigger('eventMouseout', seg.el[0], seg.event, ev); + if (this.view.isEventResizable(seg.event)) { + seg.el.removeClass('fc-allow-mouse-resize'); + } + this.view.publiclyTrigger('eventMouseout', seg.el[0], seg.event, ev); } }, + handleSegMousedown: function(seg, ev) { + var isResizing = this.startSegResize(seg, ev, { distance: 5 }); + + if (!isResizing && this.view.isEventDraggable(seg.event)) { + this.buildSegDragListener(seg) + .startInteraction(ev, { + distance: 5 + }); + } + }, + + + handleSegTouchStart: function(seg, ev) { + var view = this.view; + var event = seg.event; + var isSelected = view.isEventSelected(event); + var isDraggable = view.isEventDraggable(event); + var isResizable = view.isEventResizable(event); + var isResizing = false; + var dragListener; + var eventLongPressDelay; + + if (isSelected && isResizable) { + // only allow resizing of the event is selected + isResizing = this.startSegResize(seg, ev); + } + + if (!isResizing && (isDraggable || isResizable)) { // allowed to be selected? + + eventLongPressDelay = view.opt('eventLongPressDelay'); + if (eventLongPressDelay == null) { + eventLongPressDelay = view.opt('longPressDelay'); // fallback + } + + dragListener = isDraggable ? + this.buildSegDragListener(seg) : + this.buildSegSelectListener(seg); // seg isn't draggable, but still needs to be selected + + dragListener.startInteraction(ev, { // won't start if already started + delay: isSelected ? 0 : eventLongPressDelay // do delay if not already selected + }); + } + }, + + + // returns boolean whether resizing actually started or not. + // assumes the seg allows resizing. + // `dragOptions` are optional. + startSegResize: function(seg, ev, dragOptions) { + if ($(ev.target).is('.fc-resizer')) { + this.buildSegResizeListener(seg, $(ev.target).is('.fc-start-resizer')) + .startInteraction(ev, dragOptions); + return true; + } + return false; + }, + + + /* Event Dragging ------------------------------------------------------------------------------------------------------------------*/ - // Called when the user does a mousedown on an event, which might lead to dragging. + // Builds a listener that will track user-dragging on an event segment. // Generic enough to work with any type of Grid. - segDragMousedown: function(seg, ev) { + // Has side effect of setting/unsetting `segDragListener` + buildSegDragListener: function(seg) { var _this = this; var view = this.view; - var calendar = view.calendar; var el = seg.el; var event = seg.event; - var dropLocation; + var isDragging; + var mouseFollower; // A clone of the original element that will move with the mouse + var dropLocation; // zoned event date properties - // A clone of the original element that will move with the mouse - var mouseFollower = new MouseFollower(seg.el, { - parentEl: view.el, - opacity: view.opt('dragOpacity'), - revertDuration: view.opt('dragRevertDuration'), - zIndex: 2 // one above the .fc-view - }); + if (this.segDragListener) { + return this.segDragListener; + } // Tracks mouse movement over the *view's* coordinate map. Allows dragging and dropping between subcomponents // of the view. - var dragListener = new CellDragListener(view.coordMap, { - distance: 5, + var dragListener = this.segDragListener = new HitDragListener(view, { scroll: view.opt('dragScroll'), subjectEl: el, subjectCenter: true, - listenStart: function(ev) { + interactionStart: function(ev) { + seg.component = _this; // for renderDrag + isDragging = false; + mouseFollower = new MouseFollower(seg.el, { + additionalClass: 'fc-dragging', + parentEl: view.el, + opacity: dragListener.isTouch ? null : view.opt('dragOpacity'), + revertDuration: view.opt('dragRevertDuration'), + zIndex: 2 // one above the .fc-view + }); mouseFollower.hide(); // don't show until we know this is a real drag mouseFollower.start(ev); }, dragStart: function(ev) { - _this.triggerSegMouseout(seg, ev); // ensure a mouseout on the manipulated event has been reported + if (dragListener.isTouch && !view.isEventSelected(event)) { + // if not previously selected, will fire after a delay. then, select the event + view.selectEvent(event); + } + isDragging = true; + _this.handleSegMouseout(seg, ev); // ensure a mouseout on the manipulated event has been reported _this.segDragStart(seg, ev); view.hideEvent(event); // hide all event segments. our mouseFollower will take over }, - cellOver: function(cell, isOrig, origCell) { + hitOver: function(hit, isOrig, origHit) { + var isAllowed = true; + var origHitSpan; + var hitSpan; + var dragHelperEls; - // starting cell could be forced (DayGrid.limit) - if (seg.cell) { - origCell = seg.cell; + // starting hit could be forced (DayGrid.limit) + if (seg.hit) { + origHit = seg.hit; } - dropLocation = _this.computeEventDrop(origCell, cell, event); + // hit might not belong to this grid, so query origin grid + origHitSpan = origHit.component.getSafeHitSpan(origHit); + hitSpan = hit.component.getSafeHitSpan(hit); - if (dropLocation && !calendar.isEventRangeAllowed(dropLocation, event)) { - disableCursor(); + if (origHitSpan && hitSpan) { + dropLocation = _this.computeEventDrop(origHitSpan, hitSpan, event); + isAllowed = dropLocation && _this.isEventLocationAllowed(dropLocation, event); + } + else { + isAllowed = false; + } + + if (!isAllowed) { dropLocation = null; + disableCursor(); } // if a valid drop location, have the subclass render a visual indication - if (dropLocation && view.renderDrag(dropLocation, seg)) { + if (dropLocation && (dragHelperEls = view.renderDrag(dropLocation, seg))) { + + dragHelperEls.addClass('fc-dragging'); + if (!dragListener.isTouch) { + _this.applyDragOpacity(dragHelperEls); + } + mouseFollower.hide(); // if the subclass is already using a mock event "helper", hide our own } else { @@ -3783,61 +5451,95 @@ Grid.mixin({ } if (isOrig) { - dropLocation = null; // needs to have moved cells to be a valid drop + dropLocation = null; // needs to have moved hits to be a valid drop } }, - cellOut: function() { // called before mouse moves to a different cell OR moved out of all cells + hitOut: function() { // called before mouse moves to a different hit OR moved out of all hits view.unrenderDrag(); // unrender whatever was done in renderDrag - mouseFollower.show(); // show in case we are moving out of all cells + mouseFollower.show(); // show in case we are moving out of all hits dropLocation = null; }, - cellDone: function() { // Called after a cellOut OR before a dragStop + hitDone: function() { // Called after a hitOut OR before a dragEnd enableCursor(); }, - dragStop: function(ev) { + interactionEnd: function(ev) { + delete seg.component; // prevent side effects + // do revert animation if hasn't changed. calls a callback when finished (whether animation or not) mouseFollower.stop(!dropLocation, function() { - view.unrenderDrag(); - view.showEvent(event); - _this.segDragStop(seg, ev); + if (isDragging) { + view.unrenderDrag(); + _this.segDragStop(seg, ev); + } if (dropLocation) { - view.reportEventDrop(event, dropLocation, this.largeUnit, el, ev); + // no need to re-show original, will rerender all anyways. esp important if eventRenderWait + view.reportSegDrop(seg, dropLocation, _this.largeUnit, el, ev); + } + else { + view.showEvent(event); } }); - }, - listenStop: function() { - mouseFollower.stop(); // put in listenStop in case there was a mousedown but the drag never started + _this.segDragListener = null; } }); - dragListener.mousedown(ev); // start listening, which will eventually lead to a dragStart + return dragListener; + }, + + + // seg isn't draggable, but let's use a generic DragListener + // simply for the delay, so it can be selected. + // Has side effect of setting/unsetting `segDragListener` + buildSegSelectListener: function(seg) { + var _this = this; + var view = this.view; + var event = seg.event; + + if (this.segDragListener) { + return this.segDragListener; + } + + var dragListener = this.segDragListener = new DragListener({ + dragStart: function(ev) { + if (dragListener.isTouch && !view.isEventSelected(event)) { + // if not previously selected, will fire after a delay. then, select the event + view.selectEvent(event); + } + }, + interactionEnd: function(ev) { + _this.segDragListener = null; + } + }); + + return dragListener; }, // Called before event segment dragging starts segDragStart: function(seg, ev) { this.isDraggingSeg = true; - this.view.trigger('eventDragStart', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy + this.view.publiclyTrigger('eventDragStart', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy }, // Called after event segment dragging stops segDragStop: function(seg, ev) { this.isDraggingSeg = false; - this.view.trigger('eventDragStop', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy + this.view.publiclyTrigger('eventDragStop', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy }, - // Given the cell an event drag began, and the cell event was dropped, calculates the new start/end/allDay + // Given the spans an event drag began, and the span event was dropped, calculates the new zoned start/end/allDay // values for the event. Subclasses may override and set additional properties to be used by renderDrag. // A falsy returned value indicates an invalid drop. - computeEventDrop: function(startCell, endCell, event) { + // DOES NOT consider overlap/constraint. + computeEventDrop: function(startSpan, endSpan, event) { var calendar = this.view.calendar; - var dragStart = startCell.start; - var dragEnd = endCell.start; + var dragStart = startSpan.start; + var dragEnd = endSpan.start; var delta; - var dropLocation; + var dropLocation; // zoned event date properties if (dragStart.hasTime() === dragEnd.hasTime()) { delta = this.diffDates(dragEnd, dragStart); @@ -3848,17 +5550,13 @@ Grid.mixin({ dropLocation = { start: event.start.clone(), end: calendar.getEventEnd(event), // will be an ambig day - allDay: false // for normalizeEventRangeTimes + allDay: false // for normalizeEventTimes }; - calendar.normalizeEventRangeTimes(dropLocation); + calendar.normalizeEventTimes(dropLocation); } // othewise, work off existing values else { - dropLocation = { - start: event.start.clone(), - end: event.end ? event.end.clone() : null, - allDay: event.allDay // keep it the same - }; + dropLocation = pluckEventDateProps(event); } dropLocation.start.add(delta); @@ -3884,11 +5582,7 @@ Grid.mixin({ var opacity = this.view.opt('dragOpacity'); if (opacity != null) { - els.each(function(i, node) { - // Don't use jQuery (will set an IE filter), do it the old fashioned way. - // In IE8, a helper element will disappears if there's a filter. - node.style.opacity = opacity; - }); + els.css('opacity', opacity); } }, @@ -3918,42 +5612,52 @@ Grid.mixin({ }, - // Called when a jQuery UI drag starts and it needs to be monitored for cell dropping + // Called when a jQuery UI drag starts and it needs to be monitored for dropping listenToExternalDrag: function(el, ev, ui) { var _this = this; + var view = this.view; var meta = getDraggedElMeta(el); // extra data about event drop, including possible event to create - var dragListener; var dropLocation; // a null value signals an unsuccessful drag // listener that tracks mouse movement over date-associated pixel regions - dragListener = new CellDragListener(this.coordMap, { - listenStart: function() { + var dragListener = _this.externalDragListener = new HitDragListener(this, { + interactionStart: function() { _this.isDraggingExternal = true; }, - cellOver: function(cell) { - dropLocation = _this.computeExternalDrop(cell, meta); + hitOver: function(hit) { + var isAllowed = true; + var hitSpan = hit.component.getSafeHitSpan(hit); // hit might not belong to this grid + + if (hitSpan) { + dropLocation = _this.computeExternalDrop(hitSpan, meta); + isAllowed = dropLocation && _this.isExternalLocationAllowed(dropLocation, meta.eventProps); + } + else { + isAllowed = false; + } + + if (!isAllowed) { + dropLocation = null; + disableCursor(); + } + if (dropLocation) { _this.renderDrag(dropLocation); // called without a seg parameter } - else { // invalid drop cell - disableCursor(); - } }, - cellOut: function() { + hitOut: function() { dropLocation = null; // signal unsuccessful - _this.unrenderDrag(); - enableCursor(); }, - dragStop: function() { - _this.unrenderDrag(); + hitDone: function() { // Called after a hitOut OR before a dragEnd enableCursor(); - - if (dropLocation) { // element was dropped on a valid date/time cell - _this.view.reportExternalDrop(meta, dropLocation, el, ev, ui); + _this.unrenderDrag(); + }, + interactionEnd: function(ev) { + if (dropLocation) { // element was dropped on a valid hit + view.reportExternalDrop(meta, dropLocation, el, ev, ui); } - }, - listenStop: function() { _this.isDraggingExternal = false; + _this.externalDragListener = null; } }); @@ -3961,16 +5665,18 @@ Grid.mixin({ }, - // Given a cell to be dropped upon, and misc data associated with the jqui drag (guaranteed to be a plain object), - // returns start/end dates for the event that would result from the hypothetical drop. end might be null. - // Returning a null value signals an invalid drop cell. - computeExternalDrop: function(cell, meta) { + // Given a hit to be dropped upon, and misc data associated with the jqui drag (guaranteed to be a plain object), + // returns the zoned start/end dates for the event that would result from the hypothetical drop. end might be null. + // Returning a null value signals an invalid drop hit. + // DOES NOT consider overlap/constraint. + computeExternalDrop: function(span, meta) { + var calendar = this.view.calendar; var dropLocation = { - start: cell.start.clone(), + start: calendar.applyTimezone(span.start), // simulate a zoned event start date end: null }; - // if dropped on an all-day cell, and element's metadata specified a time, set it + // if dropped on an all-day span, and element's metadata specified a time, set it if (meta.startTime && !dropLocation.start.hasTime()) { dropLocation.start.time(meta.startTime); } @@ -3979,10 +5685,6 @@ Grid.mixin({ dropLocation.end = dropLocation.start.clone().add(meta.duration); } - if (!this.view.calendar.isExternalDropRangeAllowed(dropLocation, meta.eventProps)) { - return null; - } - return dropLocation; }, @@ -3996,6 +5698,7 @@ Grid.mixin({ // `dropLocation` contains hypothetical start/end/allDay values the event would have if dropped. end can be null. // `seg` is the internal segment object that is being dragged. If dragging an external element, `seg` is null. // A truthy returned value indicates this method has rendered a helper element. + // Must return elements used for any mock events. renderDrag: function(dropLocation, seg) { // subclasses must implement }, @@ -4011,39 +5714,56 @@ Grid.mixin({ ------------------------------------------------------------------------------------------------------------------*/ - // Called when the user does a mousedown on an event's resizer, which might lead to resizing. + // Creates a listener that tracks the user as they resize an event segment. // Generic enough to work with any type of Grid. - segResizeMousedown: function(seg, ev, isStart) { + buildSegResizeListener: function(seg, isStart) { var _this = this; var view = this.view; var calendar = view.calendar; var el = seg.el; var event = seg.event; var eventEnd = calendar.getEventEnd(event); - var dragListener; - var resizeLocation; // falsy if invalid resize + var isDragging; + var resizeLocation; // zoned event date properties. falsy if invalid resize // Tracks mouse movement over the *grid's* coordinate map - dragListener = new CellDragListener(this.coordMap, { - distance: 5, + var dragListener = this.segResizeListener = new HitDragListener(this, { scroll: view.opt('dragScroll'), subjectEl: el, + interactionStart: function() { + isDragging = false; + }, dragStart: function(ev) { - _this.triggerSegMouseout(seg, ev); // ensure a mouseout on the manipulated event has been reported + isDragging = true; + _this.handleSegMouseout(seg, ev); // ensure a mouseout on the manipulated event has been reported _this.segResizeStart(seg, ev); }, - cellOver: function(cell, isOrig, origCell) { - resizeLocation = isStart ? - _this.computeEventStartResize(origCell, cell, event) : - _this.computeEventEndResize(origCell, cell, event); + hitOver: function(hit, isOrig, origHit) { + var isAllowed = true; + var origHitSpan = _this.getSafeHitSpan(origHit); + var hitSpan = _this.getSafeHitSpan(hit); - if (resizeLocation) { - if (!calendar.isEventRangeAllowed(resizeLocation, event)) { - disableCursor(); - resizeLocation = null; - } - // no change? (TODO: how does this work with timezones?) - else if (resizeLocation.start.isSame(event.start) && resizeLocation.end.isSame(eventEnd)) { + if (origHitSpan && hitSpan) { + resizeLocation = isStart ? + _this.computeEventStartResize(origHitSpan, hitSpan, event) : + _this.computeEventEndResize(origHitSpan, hitSpan, event); + + isAllowed = resizeLocation && _this.isEventLocationAllowed(resizeLocation, event); + } + else { + isAllowed = false; + } + + if (!isAllowed) { + resizeLocation = null; + disableCursor(); + } + else { + if ( + resizeLocation.start.isSame(event.start.clone().stripZone()) && + resizeLocation.end.isSame(eventEnd.clone().stripZone()) + ) { + // no change. (FYI, event dates might have zones) resizeLocation = null; } } @@ -4053,103 +5773,108 @@ Grid.mixin({ _this.renderEventResize(resizeLocation, seg); } }, - cellOut: function() { // called before mouse moves to a different cell OR moved out of all cells + hitOut: function() { // called before mouse moves to a different hit OR moved out of all hits resizeLocation = null; + view.showEvent(event); // for when out-of-bounds. show original }, - cellDone: function() { // resets the rendering to show the original event + hitDone: function() { // resets the rendering to show the original event _this.unrenderEventResize(); - view.showEvent(event); enableCursor(); }, - dragStop: function(ev) { - _this.segResizeStop(seg, ev); + interactionEnd: function(ev) { + if (isDragging) { + _this.segResizeStop(seg, ev); + } if (resizeLocation) { // valid date to resize to? - view.reportEventResize(event, resizeLocation, this.largeUnit, el, ev); + // no need to re-show original, will rerender all anyways. esp important if eventRenderWait + view.reportSegResize(seg, resizeLocation, _this.largeUnit, el, ev); } + else { + view.showEvent(event); + } + _this.segResizeListener = null; } }); - dragListener.mousedown(ev); // start listening, which will eventually lead to a dragStart + return dragListener; }, // Called before event segment resizing starts segResizeStart: function(seg, ev) { this.isResizingSeg = true; - this.view.trigger('eventResizeStart', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy + this.view.publiclyTrigger('eventResizeStart', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy }, // Called after event segment resizing stops segResizeStop: function(seg, ev) { this.isResizingSeg = false; - this.view.trigger('eventResizeStop', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy + this.view.publiclyTrigger('eventResizeStop', seg.el[0], seg.event, ev, {}); // last argument is jqui dummy }, // Returns new date-information for an event segment being resized from its start - computeEventStartResize: function(startCell, endCell, event) { - return this.computeEventResize('start', startCell, endCell, event); + computeEventStartResize: function(startSpan, endSpan, event) { + return this.computeEventResize('start', startSpan, endSpan, event); }, // Returns new date-information for an event segment being resized from its end - computeEventEndResize: function(startCell, endCell, event) { - return this.computeEventResize('end', startCell, endCell, event); + computeEventEndResize: function(startSpan, endSpan, event) { + return this.computeEventResize('end', startSpan, endSpan, event); }, - // Returns new date-information for an event segment being resized from its start OR end - // `type` is either 'start' or 'end' - computeEventResize: function(type, startCell, endCell, event) { + // Returns new zoned date information for an event segment being resized from its start OR end + // `type` is either 'start' or 'end'. + // DOES NOT consider overlap/constraint. + computeEventResize: function(type, startSpan, endSpan, event) { var calendar = this.view.calendar; - var delta = this.diffDates(endCell[type], startCell[type]); - var range; + var delta = this.diffDates(endSpan[type], startSpan[type]); + var resizeLocation; // zoned event date properties var defaultDuration; // build original values to work from, guaranteeing a start and end - range = { + resizeLocation = { start: event.start.clone(), end: calendar.getEventEnd(event), allDay: event.allDay }; // if an all-day event was in a timed area and was resized to a time, adjust start/end to have times - if (range.allDay && durationHasTime(delta)) { - range.allDay = false; - calendar.normalizeEventRangeTimes(range); + if (resizeLocation.allDay && durationHasTime(delta)) { + resizeLocation.allDay = false; + calendar.normalizeEventTimes(resizeLocation); } - range[type].add(delta); // apply delta to start or end + resizeLocation[type].add(delta); // apply delta to start or end // if the event was compressed too small, find a new reasonable duration for it - if (!range.start.isBefore(range.end)) { + if (!resizeLocation.start.isBefore(resizeLocation.end)) { - defaultDuration = event.allDay ? - calendar.defaultAllDayEventDuration : - calendar.defaultTimedEventDuration; - - // between the cell's duration and the event's default duration, use the smaller of the two. - // example: if year-length slots, and compressed to one slot, we don't want the event to be a year long - if (this.cellDuration && this.cellDuration < defaultDuration) { - defaultDuration = this.cellDuration; - } + defaultDuration = + this.minResizeDuration || // TODO: hack + (event.allDay ? + calendar.defaultAllDayEventDuration : + calendar.defaultTimedEventDuration); if (type == 'start') { // resizing the start? - range.start = range.end.clone().subtract(defaultDuration); + resizeLocation.start = resizeLocation.end.clone().subtract(defaultDuration); } else { // resizing the end? - range.end = range.start.clone().add(defaultDuration); + resizeLocation.end = resizeLocation.start.clone().add(defaultDuration); } } - return range; + return resizeLocation; }, // Renders a visual indication of an event being resized. // `range` has the updated dates of the event. `seg` is the original segment object involved in the drag. + // Must return elements used for any mock events. renderEventResize: function(range, seg) { // subclasses must implement }, @@ -4195,15 +5920,12 @@ Grid.mixin({ // Generic utility for generating the HTML classNames for an event segment's element getSegClasses: function(seg, isDraggable, isResizable) { - var event = seg.event; + var view = this.view; var classes = [ 'fc-event', seg.isStart ? 'fc-start' : 'fc-not-start', seg.isEnd ? 'fc-end' : 'fc-not-end' - ].concat( - event.className, - event.source ? event.source.className : [] - ); + ].concat(this.getSegCustomClasses(seg)); if (isDraggable) { classes.push('fc-draggable'); @@ -4212,57 +5934,276 @@ Grid.mixin({ classes.push('fc-resizable'); } + // event is currently selected? attach a className. + if (view.isEventSelected(seg.event)) { + classes.push('fc-selected'); + } + return classes; }, - // Utility for generating event skin-related CSS properties - getEventSkinCss: function(event) { - var view = this.view; - var source = event.source || {}; - var eventColor = event.color; - var sourceColor = source.color; - var optionColor = view.opt('eventColor'); + // List of classes that were defined by the caller of the API in some way + getSegCustomClasses: function(seg) { + var event = seg.event; + return [].concat( + event.className, // guaranteed to be an array + event.source ? event.source.className : [] + ); + }, + + + // Utility for generating event skin-related CSS properties + getSegSkinCss: function(seg) { return { - 'background-color': - event.backgroundColor || - eventColor || - source.backgroundColor || - sourceColor || - view.opt('eventBackgroundColor') || - optionColor, - 'border-color': - event.borderColor || - eventColor || - source.borderColor || - sourceColor || - view.opt('eventBorderColor') || - optionColor, - color: - event.textColor || - source.textColor || - view.opt('eventTextColor') + 'background-color': this.getSegBackgroundColor(seg), + 'border-color': this.getSegBorderColor(seg), + color: this.getSegTextColor(seg) }; }, - /* Converting events -> ranges -> segs + // Queries for caller-specified color, then falls back to default + getSegBackgroundColor: function(seg) { + return seg.event.backgroundColor || + seg.event.color || + this.getSegDefaultBackgroundColor(seg); + }, + + + getSegDefaultBackgroundColor: function(seg) { + var source = seg.event.source || {}; + + return source.backgroundColor || + source.color || + this.view.opt('eventBackgroundColor') || + this.view.opt('eventColor'); + }, + + + // Queries for caller-specified color, then falls back to default + getSegBorderColor: function(seg) { + return seg.event.borderColor || + seg.event.color || + this.getSegDefaultBorderColor(seg); + }, + + + getSegDefaultBorderColor: function(seg) { + var source = seg.event.source || {}; + + return source.borderColor || + source.color || + this.view.opt('eventBorderColor') || + this.view.opt('eventColor'); + }, + + + // Queries for caller-specified color, then falls back to default + getSegTextColor: function(seg) { + return seg.event.textColor || + this.getSegDefaultTextColor(seg); + }, + + + getSegDefaultTextColor: function(seg) { + var source = seg.event.source || {}; + + return source.textColor || + this.view.opt('eventTextColor'); + }, + + + /* Event Location Validation ------------------------------------------------------------------------------------------------------------------*/ + isEventLocationAllowed: function(eventLocation, event) { + if (this.isEventLocationInRange(eventLocation)) { + var calendar = this.view.calendar; + var eventSpans = this.eventToSpans(eventLocation); + var i; + + if (eventSpans.length) { + for (i = 0; i < eventSpans.length; i++) { + if (!calendar.isEventSpanAllowed(eventSpans[i], event)) { + return false; + } + } + + return true; + } + } + + return false; + }, + + + isExternalLocationAllowed: function(eventLocation, metaProps) { // FOR the external element + if (this.isEventLocationInRange(eventLocation)) { + var calendar = this.view.calendar; + var eventSpans = this.eventToSpans(eventLocation); + var i; + + if (eventSpans.length) { + for (i = 0; i < eventSpans.length; i++) { + if (!calendar.isExternalSpanAllowed(eventSpans[i], eventLocation, metaProps)) { + return false; + } + } + + return true; + } + } + + return false; + }, + + + isEventLocationInRange: function(eventLocation) { + return isRangeWithinRange( + this.eventToRawRange(eventLocation), + this.view.validRange + ); + }, + + + /* Converting events -> eventRange -> eventSpan -> eventSegs + ------------------------------------------------------------------------------------------------------------------*/ + + + // Generates an array of segments for the given single event + // Can accept an event "location" as well (which only has start/end and no allDay) + eventToSegs: function(event) { + return this.eventsToSegs([ event ]); + }, + + + // Generates spans (always unzoned) for the given event. + // Does not do any inverting for inverse-background events. + // Can accept an event "location" as well (which only has start/end and no allDay) + eventToSpans: function(event) { + var eventRange = this.eventToRange(event); // { start, end, isStart, isEnd } + + if (eventRange) { + return this.eventRangeToSpans(eventRange, event); + } + else { // out of view's valid range + return []; + } + }, + + + // Converts an array of event objects into an array of event segment objects. - // A custom `rangeToSegsFunc` may be given for arbitrarily slicing up events. + // A custom `segSliceFunc` may be given for arbitrarily slicing up events. // Doesn't guarantee an order for the resulting array. - eventsToSegs: function(events, rangeToSegsFunc) { - var eventRanges = this.eventsToRanges(events); + eventsToSegs: function(allEvents, segSliceFunc) { + var _this = this; + var eventsById = groupEventsById(allEvents); + var segs = []; + + $.each(eventsById, function(id, events) { + var visibleEvents = []; + var eventRanges = []; + var eventRange; // { start, end, isStart, isEnd } + var i; + + for (i = 0; i < events.length; i++) { + eventRange = _this.eventToRange(events[i]); // might be null if completely out of range + + if (eventRange) { + eventRanges.push(eventRange); + visibleEvents.push(events[i]); + } + } + + // inverse-background events (utilize only the first event in calculations) + if (isInverseBgEvent(events[0])) { + eventRanges = _this.invertRanges(eventRanges); // will lose isStart/isEnd + + for (i = 0; i < eventRanges.length; i++) { + segs.push.apply(segs, // append to + _this.eventRangeToSegs(eventRanges[i], events[0], segSliceFunc) + ); + } + } + // normal event ranges + else { + for (i = 0; i < eventRanges.length; i++) { + segs.push.apply(segs, // append to + _this.eventRangeToSegs(eventRanges[i], visibleEvents[i], segSliceFunc) + ); + } + } + }); + + return segs; + }, + + + // Generates the unzoned start/end dates an event appears to occupy + // Can accept an event "location" as well (which only has start/end and no allDay) + // returns { start, end, isStart, isEnd } + // If the event is completely outside of the grid's valid range, will return undefined. + eventToRange: function(event) { + return this.refineRawEventRange( + this.eventToRawRange(event) + ); + }, + + + // Ensures the given range is within the view's activeRange and is correctly localized. + // Always returns a result + refineRawEventRange: function(rawRange) { + var view = this.view; + var calendar = view.calendar; + var range = intersectRanges(rawRange, view.activeRange); + + if (range) { // otherwise, event doesn't have valid range + + // hack: dynamic locale change forgets to upate stored event localed + calendar.localizeMoment(range.start); + calendar.localizeMoment(range.end); + + return range; + } + }, + + + // not constrained to valid dates + // not given localizeMoment hack + eventToRawRange: function(event) { + var calendar = this.view.calendar; + var start = event.start.clone().stripZone(); + var end = ( + event.end ? + event.end.clone() : + // derive the end from the start and allDay. compute allDay if necessary + calendar.getDefaultEventEnd( + event.allDay != null ? + event.allDay : + !event.start.hasTime(), + event.start + ) + ).stripZone(); + + return { start: start, end: end }; + }, + + + // Given an event's range (unzoned start/end), and the event itself, + // slice into segments (using the segSliceFunc function if specified) + // eventRange - { start, end, isStart, isEnd } + eventRangeToSegs: function(eventRange, event, segSliceFunc) { + var eventSpans = this.eventRangeToSpans(eventRange, event); var segs = []; var i; - for (i = 0; i < eventRanges.length; i++) { - segs.push.apply( - segs, - this.eventRangeToSegs(eventRanges[i], rangeToSegsFunc) + for (i = 0; i < eventSpans.length; i++) { + segs.push.apply(segs, // append to + this.eventSpanToSegs(eventSpans[i], event, segSliceFunc) ); } @@ -4270,93 +6211,74 @@ Grid.mixin({ }, - // Converts an array of events into an array of "range" objects. - // A "range" object is a plain object with start/end properties denoting the time it covers. Also an event property. - // For "normal" events, this will be identical to the event's start/end, but for "inverse-background" events, - // will create an array of ranges that span the time *not* covered by the given event. - // Doesn't guarantee an order for the resulting array. - eventsToRanges: function(events) { - var _this = this; - var eventsById = groupEventsById(events); - var ranges = []; - - // group by ID so that related inverse-background events can be rendered together - $.each(eventsById, function(id, eventGroup) { - if (eventGroup.length) { - ranges.push.apply( - ranges, - isInverseBgEvent(eventGroup[0]) ? - _this.eventsToInverseRanges(eventGroup) : - _this.eventsToNormalRanges(eventGroup) - ); - } - }); - - return ranges; + // Given an event's unzoned date range, return an array of eventSpan objects. + // eventSpan - { start, end, isStart, isEnd, otherthings... } + // Subclasses can override. + // Subclasses are obligated to forward eventRange.isStart/isEnd to the resulting spans. + eventRangeToSpans: function(eventRange, event) { + return [ $.extend({}, eventRange) ]; // copy into a single-item array }, - // Converts an array of "normal" events (not inverted rendering) into a parallel array of ranges - eventsToNormalRanges: function(events) { - var calendar = this.view.calendar; - var ranges = []; - var i, event; - var eventStart, eventEnd; + // Given an event's span (unzoned start/end and other misc data), and the event itself, + // slices into segments and attaches event-derived properties to them. + // eventSpan - { start, end, isStart, isEnd, otherthings... } + eventSpanToSegs: function(eventSpan, event, segSliceFunc) { + var segs = segSliceFunc ? segSliceFunc(eventSpan) : this.spanToSegs(eventSpan); + var i, seg; - for (i = 0; i < events.length; i++) { - event = events[i]; + for (i = 0; i < segs.length; i++) { + seg = segs[i]; - // make copies and normalize by stripping timezone - eventStart = event.start.clone().stripZone(); - eventEnd = calendar.getEventEnd(event).stripZone(); + // the eventSpan's isStart/isEnd takes precedence over the seg's + if (!eventSpan.isStart) { + seg.isStart = false; + } + if (!eventSpan.isEnd) { + seg.isEnd = false; + } - ranges.push({ - event: event, - start: eventStart, - end: eventEnd, - eventStartMS: +eventStart, - eventDurationMS: eventEnd - eventStart - }); + seg.event = event; + seg.eventStartMS = +eventSpan.start; // TODO: not the best name after making spans unzoned + seg.eventDurationMS = eventSpan.end - eventSpan.start; } - return ranges; + return segs; }, - // Converts an array of events, with inverse-background rendering, into an array of range objects. - // The range objects will cover all the time NOT covered by the events. - eventsToInverseRanges: function(events) { + // Produces a new array of range objects that will cover all the time NOT covered by the given ranges. + // SIDE EFFECT: will mutate the given array and will use its date references. + invertRanges: function(ranges) { var view = this.view; - var viewStart = view.start.clone().stripZone(); // normalize timezone - var viewEnd = view.end.clone().stripZone(); // normalize timezone - var normalRanges = this.eventsToNormalRanges(events); // will give us normalized dates we can use w/o copies + var viewStart = view.activeRange.start.clone(); // need a copy + var viewEnd = view.activeRange.end.clone(); // need a copy var inverseRanges = []; - var event0 = events[0]; // assign this to each range's `.event` var start = viewStart; // the end of the previous range. the start of the new range - var i, normalRange; + var i, range; // ranges need to be in order. required for our date-walking algorithm - normalRanges.sort(compareNormalRanges); + ranges.sort(compareRanges); - for (i = 0; i < normalRanges.length; i++) { - normalRange = normalRanges[i]; + for (i = 0; i < ranges.length; i++) { + range = ranges[i]; // add the span of time before the event (if there is any) - if (normalRange.start > start) { // compare millisecond time (skip any ambig logic) + if (range.start > start) { // compare millisecond time (skip any ambig logic) inverseRanges.push({ - event: event0, start: start, - end: normalRange.start + end: range.start }); } - start = normalRange.end; + if (range.end > start) { + start = range.end; + } } // add the span of time after the last event (if there is any) if (start < viewEnd) { // compare millisecond time (skip any ambig logic) inverseRanges.push({ - event: event0, start: start, end: viewEnd }); @@ -4366,29 +6288,17 @@ Grid.mixin({ }, - // Slices the given event range into one or more segment objects. - // A `rangeToSegsFunc` custom slicing function can be given. - eventRangeToSegs: function(eventRange, rangeToSegsFunc) { - var segs; - var i, seg; + sortEventSegs: function(segs) { + segs.sort(proxy(this, 'compareEventSegs')); + }, - eventRange = this.view.calendar.ensureVisibleEventRange(eventRange); - if (rangeToSegsFunc) { - segs = rangeToSegsFunc(eventRange); - } - else { - segs = this.rangeToSegs(eventRange); // defined by the subclass - } - - for (i = 0; i < segs.length; i++) { - seg = segs[i]; - seg.event = eventRange.event; - seg.eventStartMS = eventRange.eventStartMS; - seg.eventDurationMS = eventRange.eventDurationMS; - } - - return segs; + // A cmp function for determining which segments should take visual priority + compareEventSegs: function(seg1, seg2) { + return seg1.eventStartMS - seg2.eventStartMS || // earlier events go first + seg2.eventDurationMS - seg1.eventDurationMS || // tie? longer events go first + seg2.event.allDay - seg1.event.allDay || // tie? put all-day events first (booleans cast to 0/1) + compareByFieldSpecs(seg1.event, seg2.event, this.view.eventOrderSpecs); } }); @@ -4398,10 +6308,21 @@ Grid.mixin({ ----------------------------------------------------------------------------------------------------------------------*/ +function pluckEventDateProps(event) { + return { + start: event.start.clone(), + end: event.end ? event.end.clone() : null, + allDay: event.allDay // keep it the same + }; +} +FC.pluckEventDateProps = pluckEventDateProps; + + function isBgEvent(event) { // returns true if background OR inverse-background var rendering = getEventRendering(event); return rendering === 'background' || rendering === 'inverse-background'; } +FC.isBgEvent = isBgEvent; // export function isInverseBgEvent(event) { @@ -4428,36 +6349,23 @@ function groupEventsById(events) { // A cmp function for determining which non-inverted "ranges" (see above) happen earlier -function compareNormalRanges(range1, range2) { - return range1.eventStartMS - range2.eventStartMS; // earlier ranges go first +function compareRanges(range1, range2) { + return range1.start - range2.start; // earlier ranges go first } -// A cmp function for determining which segments should take visual priority -// DOES NOT WORK ON INVERTED BACKGROUND EVENTS because they have no eventStartMS/eventDurationMS -function compareSegs(seg1, seg2) { - return seg1.eventStartMS - seg2.eventStartMS || // earlier events go first - seg2.eventDurationMS - seg1.eventDurationMS || // tie? longer events go first - seg2.event.allDay - seg1.event.allDay || // tie? put all-day events first (booleans cast to 0/1) - (seg1.event.title || '').localeCompare(seg2.event.title) || // tie? alphabetically by title - seg1.event.sortOrder - seg2.event.sortOrder; // tie? use sortOrder -} - -fc.compareSegs = compareSegs; // export - - /* External-Dragging-Element Data ----------------------------------------------------------------------------------------------------------------------*/ // Require all HTML5 data-* attributes used by FullCalendar to have this prefix. // A value of '' will query attributes like data-event. A value of 'fc' will query attributes like data-fc-event. -fc.dataAttrPrefix = ''; +FC.dataAttrPrefix = ''; // Given a jQuery element that might represent a dragged FullCalendar event, returns an intermediate data structure // to be used for Event Object creation. // A defined `.eventProps`, even when empty, indicates that an event should be created. function getDraggedElMeta(el) { - var prefix = fc.dataAttrPrefix; + var prefix = FC.dataAttrPrefix; var eventProps; // properties for creating the event, not related to date/time var startTime; // a Duration var duration; @@ -4500,30 +6408,448 @@ function getDraggedElMeta(el) { } +;; + +/* +A set of rendering and date-related methods for a visual component comprised of one or more rows of day columns. +Prerequisite: the object being mixed into needs to be a *Grid* +*/ +var DayTableMixin = FC.DayTableMixin = { + + breakOnWeeks: false, // should create a new row for each week? + dayDates: null, // whole-day dates for each column. left to right + dayIndices: null, // for each day from start, the offset + daysPerRow: null, + rowCnt: null, + colCnt: null, + colHeadFormat: null, + + + // Populates internal variables used for date calculation and rendering + updateDayTable: function() { + var view = this.view; + var date = this.start.clone(); + var dayIndex = -1; + var dayIndices = []; + var dayDates = []; + var daysPerRow; + var firstDay; + var rowCnt; + + while (date.isBefore(this.end)) { // loop each day from start to end + if (view.isHiddenDay(date)) { + dayIndices.push(dayIndex + 0.5); // mark that it's between indices + } + else { + dayIndex++; + dayIndices.push(dayIndex); + dayDates.push(date.clone()); + } + date.add(1, 'days'); + } + + if (this.breakOnWeeks) { + // count columns until the day-of-week repeats + firstDay = dayDates[0].day(); + for (daysPerRow = 1; daysPerRow < dayDates.length; daysPerRow++) { + if (dayDates[daysPerRow].day() == firstDay) { + break; + } + } + rowCnt = Math.ceil(dayDates.length / daysPerRow); + } + else { + rowCnt = 1; + daysPerRow = dayDates.length; + } + + this.dayDates = dayDates; + this.dayIndices = dayIndices; + this.daysPerRow = daysPerRow; + this.rowCnt = rowCnt; + + this.updateDayTableCols(); + }, + + + // Computes and assigned the colCnt property and updates any options that may be computed from it + updateDayTableCols: function() { + this.colCnt = this.computeColCnt(); + this.colHeadFormat = this.view.opt('columnFormat') || this.computeColHeadFormat(); + }, + + + // Determines how many columns there should be in the table + computeColCnt: function() { + return this.daysPerRow; + }, + + + // Computes the ambiguously-timed moment for the given cell + getCellDate: function(row, col) { + return this.dayDates[ + this.getCellDayIndex(row, col) + ].clone(); + }, + + + // Computes the ambiguously-timed date range for the given cell + getCellRange: function(row, col) { + var start = this.getCellDate(row, col); + var end = start.clone().add(1, 'days'); + + return { start: start, end: end }; + }, + + + // Returns the number of day cells, chronologically, from the first of the grid (0-based) + getCellDayIndex: function(row, col) { + return row * this.daysPerRow + this.getColDayIndex(col); + }, + + + // Returns the numner of day cells, chronologically, from the first cell in *any given row* + getColDayIndex: function(col) { + if (this.isRTL) { + return this.colCnt - 1 - col; + } + else { + return col; + } + }, + + + // Given a date, returns its chronolocial cell-index from the first cell of the grid. + // If the date lies between cells (because of hiddenDays), returns a floating-point value between offsets. + // If before the first offset, returns a negative number. + // If after the last offset, returns an offset past the last cell offset. + // Only works for *start* dates of cells. Will not work for exclusive end dates for cells. + getDateDayIndex: function(date) { + var dayIndices = this.dayIndices; + var dayOffset = date.diff(this.start, 'days'); + + if (dayOffset < 0) { + return dayIndices[0] - 1; + } + else if (dayOffset >= dayIndices.length) { + return dayIndices[dayIndices.length - 1] + 1; + } + else { + return dayIndices[dayOffset]; + } + }, + + + /* Options + ------------------------------------------------------------------------------------------------------------------*/ + + + // Computes a default column header formatting string if `colFormat` is not explicitly defined + computeColHeadFormat: function() { + // if more than one week row, or if there are a lot of columns with not much space, + // put just the day numbers will be in each cell + if (this.rowCnt > 1 || this.colCnt > 10) { + return 'ddd'; // "Sat" + } + // multiple days, so full single date string WON'T be in title text + else if (this.colCnt > 1) { + return this.view.opt('dayOfMonthFormat'); // "Sat 12/10" + } + // single day, so full single date string will probably be in title text + else { + return 'dddd'; // "Saturday" + } + }, + + + /* Slicing + ------------------------------------------------------------------------------------------------------------------*/ + + + // Slices up a date range into a segment for every week-row it intersects with + sliceRangeByRow: function(range) { + var daysPerRow = this.daysPerRow; + var normalRange = this.view.computeDayRange(range); // make whole-day range, considering nextDayThreshold + var rangeFirst = this.getDateDayIndex(normalRange.start); // inclusive first index + var rangeLast = this.getDateDayIndex(normalRange.end.clone().subtract(1, 'days')); // inclusive last index + var segs = []; + var row; + var rowFirst, rowLast; // inclusive day-index range for current row + var segFirst, segLast; // inclusive day-index range for segment + + for (row = 0; row < this.rowCnt; row++) { + rowFirst = row * daysPerRow; + rowLast = rowFirst + daysPerRow - 1; + + // intersect segment's offset range with the row's + segFirst = Math.max(rangeFirst, rowFirst); + segLast = Math.min(rangeLast, rowLast); + + // deal with in-between indices + segFirst = Math.ceil(segFirst); // in-between starts round to next cell + segLast = Math.floor(segLast); // in-between ends round to prev cell + + if (segFirst <= segLast) { // was there any intersection with the current row? + segs.push({ + row: row, + + // normalize to start of row + firstRowDayIndex: segFirst - rowFirst, + lastRowDayIndex: segLast - rowFirst, + + // must be matching integers to be the segment's start/end + isStart: segFirst === rangeFirst, + isEnd: segLast === rangeLast + }); + } + } + + return segs; + }, + + + // Slices up a date range into a segment for every day-cell it intersects with. + // TODO: make more DRY with sliceRangeByRow somehow. + sliceRangeByDay: function(range) { + var daysPerRow = this.daysPerRow; + var normalRange = this.view.computeDayRange(range); // make whole-day range, considering nextDayThreshold + var rangeFirst = this.getDateDayIndex(normalRange.start); // inclusive first index + var rangeLast = this.getDateDayIndex(normalRange.end.clone().subtract(1, 'days')); // inclusive last index + var segs = []; + var row; + var rowFirst, rowLast; // inclusive day-index range for current row + var i; + var segFirst, segLast; // inclusive day-index range for segment + + for (row = 0; row < this.rowCnt; row++) { + rowFirst = row * daysPerRow; + rowLast = rowFirst + daysPerRow - 1; + + for (i = rowFirst; i <= rowLast; i++) { + + // intersect segment's offset range with the row's + segFirst = Math.max(rangeFirst, i); + segLast = Math.min(rangeLast, i); + + // deal with in-between indices + segFirst = Math.ceil(segFirst); // in-between starts round to next cell + segLast = Math.floor(segLast); // in-between ends round to prev cell + + if (segFirst <= segLast) { // was there any intersection with the current row? + segs.push({ + row: row, + + // normalize to start of row + firstRowDayIndex: segFirst - rowFirst, + lastRowDayIndex: segLast - rowFirst, + + // must be matching integers to be the segment's start/end + isStart: segFirst === rangeFirst, + isEnd: segLast === rangeLast + }); + } + } + } + + return segs; + }, + + + /* Header Rendering + ------------------------------------------------------------------------------------------------------------------*/ + + + renderHeadHtml: function() { + var view = this.view; + + return '' + + '
' + + '' + + '' + + this.renderHeadTrHtml() + + '' + + '
' + + '
'; + }, + + + renderHeadIntroHtml: function() { + return this.renderIntroHtml(); // fall back to generic + }, + + + renderHeadTrHtml: function() { + return '' + + '' + + (this.isRTL ? '' : this.renderHeadIntroHtml()) + + this.renderHeadDateCellsHtml() + + (this.isRTL ? this.renderHeadIntroHtml() : '') + + ''; + }, + + + renderHeadDateCellsHtml: function() { + var htmls = []; + var col, date; + + for (col = 0; col < this.colCnt; col++) { + date = this.getCellDate(0, col); + htmls.push(this.renderHeadDateCellHtml(date)); + } + + return htmls.join(''); + }, + + + // TODO: when internalApiVersion, accept an object for HTML attributes + // (colspan should be no different) + renderHeadDateCellHtml: function(date, colspan, otherAttrs) { + var view = this.view; + var isDateValid = isDateWithinRange(date, view.activeRange); // TODO: called too frequently. cache somehow. + var classNames = [ + 'fc-day-header', + view.widgetHeaderClass + ]; + var innerHtml = htmlEscape(date.format(this.colHeadFormat)); + + // if only one row of days, the classNames on the header can represent the specific days beneath + if (this.rowCnt === 1) { + classNames = classNames.concat( + // includes the day-of-week class + // noThemeHighlight=true (don't highlight the header) + this.getDayClasses(date, true) + ); + } + else { + classNames.push('fc-' + dayIDs[date.day()]); // only add the day-of-week class + } + + return '' + + ' 1 ? + ' colspan="' + colspan + '"' : + '') + + (otherAttrs ? + ' ' + otherAttrs : + '') + + '>' + + (isDateValid ? + // don't make a link if the heading could represent multiple days, or if there's only one day (forceOff) + view.buildGotoAnchorHtml( + { date: date, forceOff: this.rowCnt > 1 || this.colCnt === 1 }, + innerHtml + ) : + // if not valid, display text, but no link + innerHtml + ) + + ''; + }, + + + /* Background Rendering + ------------------------------------------------------------------------------------------------------------------*/ + + + renderBgTrHtml: function(row) { + return '' + + '' + + (this.isRTL ? '' : this.renderBgIntroHtml(row)) + + this.renderBgCellsHtml(row) + + (this.isRTL ? this.renderBgIntroHtml(row) : '') + + ''; + }, + + + renderBgIntroHtml: function(row) { + return this.renderIntroHtml(); // fall back to generic + }, + + + renderBgCellsHtml: function(row) { + var htmls = []; + var col, date; + + for (col = 0; col < this.colCnt; col++) { + date = this.getCellDate(row, col); + htmls.push(this.renderBgCellHtml(date)); + } + + return htmls.join(''); + }, + + + renderBgCellHtml: function(date, otherAttrs) { + var view = this.view; + var isDateValid = isDateWithinRange(date, view.activeRange); // TODO: called too frequently. cache somehow. + var classes = this.getDayClasses(date); + + classes.unshift('fc-day', view.widgetContentClass); + + return ''; + }, + + + /* Generic + ------------------------------------------------------------------------------------------------------------------*/ + + + // Generates the default HTML intro for any row. User classes should override + renderIntroHtml: function() { + }, + + + // TODO: a generic method for dealing with , RTL, intro + // when increment internalApiVersion + // wrapTr (scheduler) + + + /* Utils + ------------------------------------------------------------------------------------------------------------------*/ + + + // Applies the generic "intro" and "outro" HTML to the given cells. + // Intro means the leftmost cell when the calendar is LTR and the rightmost cell when RTL. Vice-versa for outro. + bookendCells: function(trEl) { + var introHtml = this.renderIntroHtml(); + + if (introHtml) { + if (this.isRTL) { + trEl.append(introHtml); + } + else { + trEl.prepend(introHtml); + } + } + } + +}; + ;; /* A component that renders a grid of whole-days that runs horizontally. There can be multiple rows, one per week. ----------------------------------------------------------------------------------------------------------------------*/ -var DayGrid = Grid.extend({ +var DayGrid = FC.DayGrid = Grid.extend(DayTableMixin, { numbersVisible: false, // should render a row for day/week numbers? set by outside view. TODO: make internal bottomCoordPadding: 0, // hack for extending the hit area for the last row of the coordinate grid - breakOnWeeks: null, // should create a new row for each week? set by outside view - - cellDates: null, // flat chronological array of each cell's dates - dayToCellOffsets: null, // maps days offsets from grid's start date, to cell offsets rowEls: null, // set of fake row elements - dayEls: null, // set of whole-day elements comprising the row's background + cellEls: null, // set of whole-day elements comprising the row's background helperEls: null, // set of cell skeleton elements for rendering the mock event "helper" - - constructor: function() { - Grid.apply(this, arguments); - - this.cellDuration = moment.duration(1, 'day'); // for Grid system - }, + rowCoordCache: null, + colCoordCache: null, // Renders the rows and columns into the component's `this.el`, which should already be assigned. @@ -4533,23 +6859,37 @@ var DayGrid = Grid.extend({ var view = this.view; var rowCnt = this.rowCnt; var colCnt = this.colCnt; - var cellCnt = rowCnt * colCnt; var html = ''; var row; - var i, cell; + var col; for (row = 0; row < rowCnt; row++) { - html += this.dayRowHtml(row, isRigid); + html += this.renderDayRowHtml(row, isRigid); } this.el.html(html); this.rowEls = this.el.find('.fc-row'); - this.dayEls = this.el.find('.fc-day'); + this.cellEls = this.el.find('.fc-day, .fc-disabled-day'); + + this.rowCoordCache = new CoordCache({ + els: this.rowEls, + isVertical: true + }); + this.colCoordCache = new CoordCache({ + els: this.cellEls.slice(0, this.colCnt), // only the first row + isHorizontal: true + }); // trigger dayRender with each cell's element - for (i = 0; i < cellCnt; i++) { - cell = this.getCell(i); - view.trigger('dayRender', null, cell.start, this.dayEls.eq(i)); + for (row = 0; row < rowCnt; row++) { + for (col = 0; col < colCnt; col++) { + view.publiclyTrigger( + 'dayRender', + null, + this.getCellDate(row, col), + this.getCellEl(row, col) + ); + } } }, @@ -4560,15 +6900,19 @@ var DayGrid = Grid.extend({ renderBusinessHours: function() { - var events = this.view.calendar.getBusinessHoursEvents(true); // wholeDay=true - var segs = this.eventsToSegs(events); - + var segs = this.buildBusinessHourSegs(true); // wholeDay=true this.renderFill('businessHours', segs, 'bgevent'); }, - // Generates the HTML for a single row. `row` is the row number. - dayRowHtml: function(row, isRigid) { + unrenderBusinessHours: function() { + this.unrenderFill('businessHours'); + }, + + + // Generates the HTML for a single row, which is a div that wraps a table. + // `row` is the row number. + renderDayRowHtml: function(row, isRigid) { var view = this.view; var classes = [ 'fc-row', 'fc-week', view.widgetContentClass ]; @@ -4580,14 +6924,14 @@ var DayGrid = Grid.extend({ '
' + '
' + '' + - this.rowHtml('day', row) + // leverages RowRenderer. calls dayCellHtml() + this.renderBgTrHtml(row) + '
' + '
' + '
' + '' + (this.numbersVisible ? '' + - this.rowHtml('number', row) + // leverages RowRenderer. View will define render method + this.renderNumberTrHtml(row) + '' : '' ) + @@ -4597,11 +6941,96 @@ var DayGrid = Grid.extend({ }, - // Renders the HTML for a whole-day cell. Will eventually end up in the day-row's background. - // We go through a 'day' row type instead of just doing a 'bg' row type so that the View can do custom rendering - // specifically for whole-day rows, whereas a 'bg' might also be used for other purposes (TimeGrid bg for example). - dayCellHtml: function(cell) { - return this.bgCellHtml(cell); + /* Grid Number Rendering + ------------------------------------------------------------------------------------------------------------------*/ + + + renderNumberTrHtml: function(row) { + return '' + + '' + + (this.isRTL ? '' : this.renderNumberIntroHtml(row)) + + this.renderNumberCellsHtml(row) + + (this.isRTL ? this.renderNumberIntroHtml(row) : '') + + ''; + }, + + + renderNumberIntroHtml: function(row) { + return this.renderIntroHtml(); + }, + + + renderNumberCellsHtml: function(row) { + var htmls = []; + var col, date; + + for (col = 0; col < this.colCnt; col++) { + date = this.getCellDate(row, col); + htmls.push(this.renderNumberCellHtml(date)); + } + + return htmls.join(''); + }, + + + // Generates the HTML for the '; + + return html; }, @@ -4609,20 +7038,6 @@ var DayGrid = Grid.extend({ ------------------------------------------------------------------------------------------------------------------*/ - // Computes a default column header formatting string if `colFormat` is not explicitly defined - computeColHeadFormat: function() { - if (this.rowCnt > 1) { // more than one week row. day numbers will be in each cell - return 'ddd'; // "Sat" - } - else if (this.colCnt > 1) { // multiple days, so full single date string WON'T be in title text - return this.view.opt('dayOfMonthFormat'); // "Sat 12/10" - } - else { // single day, so full single date string will probably be in title text - return 'dddd'; // "Saturday" - } - }, - - // Computes a default event time formatting string if `timeFormat` is not explicitly defined computeEventTimeFormat: function() { return this.view.opt('extraSmallTimeFormat'); // like "6p" or "6:30p" @@ -4635,155 +7050,29 @@ var DayGrid = Grid.extend({ }, - /* Cell System - ------------------------------------------------------------------------------------------------------------------*/ - - - rangeUpdated: function() { - var cellDates; - var firstDay; - var rowCnt; - var colCnt; - - this.updateCellDates(); // populates cellDates and dayToCellOffsets - cellDates = this.cellDates; - - if (this.breakOnWeeks) { - // count columns until the day-of-week repeats - firstDay = cellDates[0].day(); - for (colCnt = 1; colCnt < cellDates.length; colCnt++) { - if (cellDates[colCnt].day() == firstDay) { - break; - } - } - rowCnt = Math.ceil(cellDates.length / colCnt); - } - else { - rowCnt = 1; - colCnt = cellDates.length; - } - - this.rowCnt = rowCnt; - this.colCnt = colCnt; - }, - - - // Populates cellDates and dayToCellOffsets - updateCellDates: function() { - var view = this.view; - var date = this.start.clone(); - var dates = []; - var offset = -1; - var offsets = []; - - while (date.isBefore(this.end)) { // loop each day from start to end - if (view.isHiddenDay(date)) { - offsets.push(offset + 0.5); // mark that it's between offsets - } - else { - offset++; - offsets.push(offset); - dates.push(date.clone()); - } - date.add(1, 'days'); - } - - this.cellDates = dates; - this.dayToCellOffsets = offsets; - }, - - - // Given a cell object, generates its start date. Returns a reference-free copy. - computeCellDate: function(cell) { - var colCnt = this.colCnt; - var index = cell.row * colCnt + (this.isRTL ? colCnt - cell.col - 1 : cell.col); - - return this.cellDates[index].clone(); - }, - - - // Retrieves the element representing the given row - getRowEl: function(row) { - return this.rowEls.eq(row); - }, - - - // Retrieves the element representing the given column - getColEl: function(col) { - return this.dayEls.eq(col); - }, - - - // Gets the whole-day element associated with the cell - getCellDayEl: function(cell) { - return this.dayEls.eq(cell.row * this.colCnt + cell.col); - }, - - - // Overrides Grid's method for when row coordinates are computed - computeRowCoords: function() { - var rowCoords = Grid.prototype.computeRowCoords.call(this); // call the super-method - - // hack for extending last row (used by AgendaView) - rowCoords[rowCoords.length - 1].bottom += this.bottomCoordPadding; - - return rowCoords; - }, - - /* Dates ------------------------------------------------------------------------------------------------------------------*/ - // Slices up a date range by row into an array of segments - rangeToSegs: function(range) { - var isRTL = this.isRTL; - var rowCnt = this.rowCnt; - var colCnt = this.colCnt; - var segs = []; - var first, last; // inclusive cell-offset range for given range - var row; - var rowFirst, rowLast; // inclusive cell-offset range for current row - var isStart, isEnd; - var segFirst, segLast; // inclusive cell-offset range for segment - var seg; + rangeUpdated: function() { + this.updateDayTable(); + }, - range = this.view.computeDayRange(range); // make whole-day range, considering nextDayThreshold - first = this.dateToCellOffset(range.start); - last = this.dateToCellOffset(range.end.subtract(1, 'days')); // offset of inclusive end date - for (row = 0; row < rowCnt; row++) { - rowFirst = row * colCnt; - rowLast = rowFirst + colCnt - 1; + // Slices up the given span (unzoned start/end with other misc data) into an array of segments + spanToSegs: function(span) { + var segs = this.sliceRangeByRow(span); + var i, seg; - // intersect segment's offset range with the row's - segFirst = Math.max(rowFirst, first); - segLast = Math.min(rowLast, last); - - // deal with in-between indices - segFirst = Math.ceil(segFirst); // in-between starts round to next cell - segLast = Math.floor(segLast); // in-between ends round to prev cell - - if (segFirst <= segLast) { // was there any intersection with the current row? - - // must be matching integers to be the segment's start/end - isStart = segFirst === first; - isEnd = segLast === last; - - // translate offsets to be relative to start-of-row - segFirst -= rowFirst; - segLast -= rowFirst; - - seg = { row: row, isStart: isStart, isEnd: isEnd }; - if (isRTL) { - seg.leftCol = colCnt - segLast - 1; - seg.rightCol = colCnt - segFirst - 1; - } - else { - seg.leftCol = segFirst; - seg.rightCol = segLast; - } - segs.push(seg); + for (i = 0; i < segs.length; i++) { + seg = segs[i]; + if (this.isRTL) { + seg.leftCol = this.daysPerRow - 1 - seg.lastRowDayIndex; + seg.rightCol = this.daysPerRow - 1 - seg.firstRowDayIndex; + } + else { + seg.leftCol = seg.firstRowDayIndex; + seg.rightCol = seg.lastRowDayIndex; } } @@ -4791,46 +7080,87 @@ var DayGrid = Grid.extend({ }, - // Given a date, returns its chronolocial cell-offset from the first cell of the grid. - // If the date lies between cells (because of hiddenDays), returns a floating-point value between offsets. - // If before the first offset, returns a negative number. - // If after the last offset, returns an offset past the last cell offset. - // Only works for *start* dates of cells. Will not work for exclusive end dates for cells. - dateToCellOffset: function(date) { - var offsets = this.dayToCellOffsets; - var day = date.diff(this.start, 'days'); + /* Hit System + ------------------------------------------------------------------------------------------------------------------*/ - if (day < 0) { - return offsets[0] - 1; - } - else if (day >= offsets.length) { - return offsets[offsets.length - 1] + 1; - } - else { - return offsets[day]; + + prepareHits: function() { + this.colCoordCache.build(); + this.rowCoordCache.build(); + this.rowCoordCache.bottoms[this.rowCnt - 1] += this.bottomCoordPadding; // hack + }, + + + releaseHits: function() { + this.colCoordCache.clear(); + this.rowCoordCache.clear(); + }, + + + queryHit: function(leftOffset, topOffset) { + if (this.colCoordCache.isLeftInBounds(leftOffset) && this.rowCoordCache.isTopInBounds(topOffset)) { + var col = this.colCoordCache.getHorizontalIndex(leftOffset); + var row = this.rowCoordCache.getVerticalIndex(topOffset); + + if (row != null && col != null) { + return this.getCellHit(row, col); + } } }, + getHitSpan: function(hit) { + return this.getCellRange(hit.row, hit.col); + }, + + + getHitEl: function(hit) { + return this.getCellEl(hit.row, hit.col); + }, + + + /* Cell System + ------------------------------------------------------------------------------------------------------------------*/ + // FYI: the first column is the leftmost column, regardless of date + + + getCellHit: function(row, col) { + return { + row: row, + col: col, + component: this, // needed unfortunately :( + left: this.colCoordCache.getLeftOffset(col), + right: this.colCoordCache.getRightOffset(col), + top: this.rowCoordCache.getTopOffset(row), + bottom: this.rowCoordCache.getBottomOffset(row) + }; + }, + + + getCellEl: function(row, col) { + return this.cellEls.eq(row * this.colCnt + col); + }, + + /* Event Drag Visualization ------------------------------------------------------------------------------------------------------------------*/ // TODO: move to DayGrid.event, similar to what we did with Grid's drag methods // Renders a visual indication of an event or external element being dragged. - // The dropLocation's end can be null. seg can be null. See Grid::renderDrag for more info. - renderDrag: function(dropLocation, seg) { + // `eventLocation` has zoned start and end (optional) + renderDrag: function(eventLocation, seg) { + var eventSpans = this.eventToSpans(eventLocation); + var i; // always render a highlight underneath - this.renderHighlight(this.eventRangeToSegs(dropLocation)); + for (i = 0; i < eventSpans.length; i++) { + this.renderHighlight(eventSpans[i]); + } // if a segment from the same calendar but another component is being dragged, render a helper event - if (seg && !seg.el.closest(this.el).length) { - - this.renderRangeHelper(dropLocation, seg); - this.applyDragOpacity(this.helperEls); - - return true; // a helper has been rendered + if (seg && seg.component !== this) { + return this.renderEventLocationHelper(eventLocation, seg); // returns mock event elements } }, @@ -4847,9 +7177,15 @@ var DayGrid = Grid.extend({ // Renders a visual indication of an event being resized - renderEventResize: function(range, seg) { - this.renderHighlight(this.eventRangeToSegs(range)); - this.renderRangeHelper(range, seg); + renderEventResize: function(eventLocation, seg) { + var eventSpans = this.eventToSpans(eventLocation); + var i; + + for (i = 0; i < eventSpans.length; i++) { + this.renderHighlight(eventSpans[i]); + } + + return this.renderEventLocationHelper(eventLocation, seg); // returns mock event elements }, @@ -4867,7 +7203,7 @@ var DayGrid = Grid.extend({ // Renders a mock "helper" event. `sourceSeg` is the associated internal segment object. It can be null. renderHelper: function(event, sourceSeg) { var helperNodes = []; - var segs = this.eventsToSegs([ event ]); + var segs = this.eventToSegs(event); var rowStructs; segs = this.renderFgSegEls(segs); // assigns each seg's el and returns a subset of segs that were rendered @@ -4895,7 +7231,9 @@ var DayGrid = Grid.extend({ helperNodes.push(skeletonEl[0]); }); - this.helperEls = $(helperNodes); // array -> jQuery set + return ( // must return the elements rendered + this.helperEls = $(helperNodes) // array -> jQuery set + ); }, @@ -4966,7 +7304,7 @@ var DayGrid = Grid.extend({ trEl.append(' and segment '; html += - '' + + '' + (!isRTL ? axisHtml : '') + ''; + } + + skeletonEl = $( + '
' + + '
s of the "number" row in the DayGrid's content skeleton. + // The number row will only exist if either day numbers or week numbers are turned on. + renderNumberCellHtml: function(date) { + var view = this.view; + var html = ''; + var isDateValid = isDateWithinRange(date, view.activeRange); // TODO: called too frequently. cache somehow. + var isDayNumberVisible = view.dayNumbersVisible && isDateValid; + var classes; + var weekCalcFirstDoW; + + if (!isDayNumberVisible && !view.cellWeekNumbersVisible) { + // no numbers in day cell (week number must be along the side) + return ''; // will create an empty space above events :( + } + + classes = this.getDayClasses(date); + classes.unshift('fc-day-top'); + + if (view.cellWeekNumbersVisible) { + // To determine the day of week number change under ISO, we cannot + // rely on moment.js methods such as firstDayOfWeek() or weekday(), + // because they rely on the locale's dow (possibly overridden by + // our firstDay option), which may not be Monday. We cannot change + // dow, because that would affect the calendar start day as well. + if (date._locale._fullCalendar_weekCalc === 'ISO') { + weekCalcFirstDoW = 1; // Monday by ISO 8601 definition + } + else { + weekCalcFirstDoW = date._locale.firstDayOfWeek(); + } + } + + html += ''; + + if (view.cellWeekNumbersVisible && (date.day() == weekCalcFirstDoW)) { + html += view.buildGotoAnchorHtml( + { date: date, type: 'week' }, + { 'class': 'fc-week-number' }, + date.format('w') // inner HTML + ); + } + + if (isDayNumberVisible) { + html += view.buildGotoAnchorHtml( + date, + { 'class': 'fc-day-number' }, + date.date() // inner HTML + ); + } + + html += ''); } - this.bookendCells(trEl, type); + this.bookendCells(trEl); return skeletonEl; } @@ -5074,7 +7412,7 @@ DayGrid.mixin({ var isResizableFromEnd = !disableResizing && event.allDay && seg.isEnd && view.isEventResizableFromEnd(event); var classes = this.getSegClasses(seg, isDraggable, isResizableFromStart || isResizableFromEnd); - var skinCss = cssToStr(this.getEventSkinCss(event)); + var skinCss = cssToStr(this.getSegSkinCss(seg)); var timeHtml = ''; var timeText; var titleHtml; @@ -5197,7 +7535,7 @@ DayGrid.mixin({ } emptyCellsUntil(colCnt); // finish off the row - this.bookendCells(tr, 'eventSkeleton'); + this.bookendCells(tr); tbody.append(tr); } @@ -5221,7 +7559,7 @@ DayGrid.mixin({ // Give preference to elements with certain criteria, so they have // a chance to be closer to the top. - segs.sort(compareSegs); + this.sortEventSegs(segs); for (i = 0; i < segs.length; i++) { seg = segs[i]; @@ -5377,7 +7715,6 @@ DayGrid.mixin({ var rowStruct = this.rowStructs[row]; var moreNodes = []; // array of "more" links and DOM nodes var col = 0; // col #, left-to-right (not chronologically) - var cell; var levelSegs; // array of segment objects in the last allowable level, ordered left-to-right var cellMatrix; // a matrix (by level, then column) of all jQuery elements in the row var limitedNodes; // array of temporarily hidden level
DOM nodes @@ -5393,11 +7730,10 @@ DayGrid.mixin({ // Iterates through empty level cells and places "more" links inside if need be function emptyCellsUntil(endCol) { // goes from current `col` to `endCol` while (col < endCol) { - cell = _this.getCell(row, col); - segsBelow = _this.getCellSegs(cell, levelLimit); + segsBelow = _this.getCellSegs(row, col, levelLimit); if (segsBelow.length) { td = cellMatrix[levelLimit - 1][col]; - moreLink = _this.renderMoreLink(cell, segsBelow); + moreLink = _this.renderMoreLink(row, col, segsBelow); moreWrap = $('
').append(moreLink); td.append(moreWrap); moreNodes.push(moreWrap[0]); @@ -5422,8 +7758,7 @@ DayGrid.mixin({ colSegsBelow = []; totalSegsBelow = 0; while (col <= seg.rightCol) { - cell = this.getCell(row, col); - segsBelow = this.getCellSegs(cell, levelLimit); + segsBelow = this.getCellSegs(row, col, levelLimit); colSegsBelow.push(segsBelow); totalSegsBelow += segsBelow.length; col++; @@ -5438,8 +7773,11 @@ DayGrid.mixin({ for (j = 0; j < colSegsBelow.length; j++) { moreTd = $('
').attr('rowspan', rowspan); segsBelow = colSegsBelow[j]; - cell = this.getCell(row, seg.leftCol + j); - moreLink = this.renderMoreLink(cell, [ seg ].concat(segsBelow)); // count seg as hidden too + moreLink = this.renderMoreLink( + row, + seg.leftCol + j, + [ seg ].concat(segsBelow) // count seg as hidden too + ); moreWrap = $('
').append(moreLink); moreTd.append(moreWrap); segMoreNodes.push(moreTd[0]); @@ -5477,7 +7815,7 @@ DayGrid.mixin({ // Renders an element that represents hidden event element for a cell. // Responsible for attaching click handler as well. - renderMoreLink: function(cell, hiddenSegs) { + renderMoreLink: function(row, col, hiddenSegs) { var _this = this; var view = this.view; @@ -5487,10 +7825,10 @@ DayGrid.mixin({ ) .on('click', function(ev) { var clickOption = view.opt('eventLimitClick'); - var date = cell.start; + var date = _this.getCellDate(row, col); var moreEl = $(this); - var dayEl = _this.getCellDayEl(cell); - var allSegs = _this.getCellSegs(cell); + var dayEl = _this.getCellEl(row, col); + var allSegs = _this.getCellSegs(row, col); // rescope the segments to be within the cell's date var reslicedAllSegs = _this.resliceDaySegs(allSegs, date); @@ -5498,7 +7836,7 @@ DayGrid.mixin({ if (typeof clickOption === 'function') { // the returned value can be an atomic option - clickOption = view.trigger('eventLimitClick', null, { + clickOption = view.publiclyTrigger('eventLimitClick', null, { date: date, dayEl: dayEl, moreEl: moreEl, @@ -5508,7 +7846,7 @@ DayGrid.mixin({ } if (clickOption === 'popover') { - _this.showSegPopover(cell, moreEl, reslicedAllSegs); + _this.showSegPopover(row, col, moreEl, reslicedAllSegs); } else if (typeof clickOption === 'string') { // a view name view.calendar.zoomTo(date, clickOption); @@ -5518,7 +7856,7 @@ DayGrid.mixin({ // Reveals the popover that displays all events within a cell - showSegPopover: function(cell, moreLink, segs) { + showSegPopover: function(row, col, moreLink, segs) { var _this = this; var view = this.view; var moreWrap = moreLink.parent(); // the
wrapper around the @@ -5529,18 +7867,26 @@ DayGrid.mixin({ topEl = view.el; // will cause the popover to cover any sort of header } else { - topEl = this.rowEls.eq(cell.row); // will align with top of row + topEl = this.rowEls.eq(row); // will align with top of row } options = { className: 'fc-more-popover', - content: this.renderSegPopoverContent(cell, segs), - parentEl: this.el, + content: this.renderSegPopoverContent(row, col, segs), + parentEl: this.view.el, // attach to root of view. guarantees outside of scrollbars. top: topEl.offset().top, autoHide: true, // when the user clicks elsewhere, hide the popover viewportConstrain: view.opt('popoverViewportConstrain'), hide: function() { // kill everything when the popover is hidden + // notify events to be removed + if (_this.popoverSegs) { + var seg; + for (var i = 0; i < _this.popoverSegs.length; ++i) { + seg = _this.popoverSegs[i]; + view.publiclyTrigger('eventDestroy', seg.event, seg.event, seg.el); + } + } _this.segPopover.removeElement(); _this.segPopover = null; _this.popoverSegs = null; @@ -5558,14 +7904,18 @@ DayGrid.mixin({ this.segPopover = new Popover(options); this.segPopover.show(); + + // the popover doesn't live within the grid's container element, and thus won't get the event + // delegated-handlers for free. attach event-related handlers to the popover. + this.bindSegHandlersToEl(this.segPopover.el); }, // Builds the inner DOM contents of the segment popover - renderSegPopoverContent: function(cell, segs) { + renderSegPopoverContent: function(row, col, segs) { var view = this.view; var isTheme = view.opt('theme'); - var title = cell.start.format(view.opt('dayPopoverFormat')); + var title = this.getCellDate(row, col).format(view.opt('dayPopoverFormat')); var content = $( '
' + '' + '' + - this.rowHtml('slotBg') + // leverages RowRenderer, which will call slotBgCellHtml + this.renderBgTrHtml(0) + // row=0 '
' + '
' + '
' + '' + - this.slatRowHtml() + + this.renderSlatRowHtml() + '
' + '
'; }, - // Renders the HTML for a vertical background cell behind the slots. - // This method is distinct from 'bg' because we wanted a new `rowType` so the View could customize the rendering. - slotBgCellHtml: function(cell) { - return this.bgCellHtml(cell); - }, - - // Generates the HTML for the horizontal "slats" that run width-wise. Has a time axis on a side. Depends on RTL. - slatRowHtml: function() { + renderSlatRowHtml: function() { var view = this.view; var isRTL = this.isRTL; var html = ''; - var slotNormal = this.slotDuration.asMinutes() % 15 === 0; - var slotTime = moment.duration(+this.minTime); // wish there was .clone() for durations + var slotTime = moment.duration(+this.view.minTime); // wish there was .clone() for durations var slotDate; // will be on the view's first day, but we only care about its time - var minutes; + var isLabeled; var axisHtml; // Calculate the time for each slot - while (slotTime < this.maxTime) { - slotDate = this.start.clone().time(slotTime); // will be in UTC but that's good. to avoid DST issues - minutes = slotDate.minutes(); + while (slotTime < this.view.maxTime) { + slotDate = this.start.clone().time(slotTime); + isLabeled = isInt(divideDurationByDuration(slotTime, this.labelInterval)); axisHtml = '
' + - ((!slotNormal || !minutes) ? // if irregular slot duration, or on the hour, then display the time + (isLabeled ? '' + // for matchCellWidths - htmlEscape(slotDate.format(this.axisFormat)) + + htmlEscape(slotDate.format(this.labelFormat)) + '' : '' ) + '
' + (isRTL ? axisHtml : '') + @@ -5779,29 +8131,51 @@ var TimeGrid = Grid.extend({ var view = this.view; var slotDuration = view.opt('slotDuration'); var snapDuration = view.opt('snapDuration'); + var input; slotDuration = moment.duration(slotDuration); snapDuration = snapDuration ? moment.duration(snapDuration) : slotDuration; this.slotDuration = slotDuration; this.snapDuration = snapDuration; - this.cellDuration = snapDuration; // for Grid system + this.snapsPerSlot = slotDuration / snapDuration; // TODO: ensure an integer multiple? - this.minTime = moment.duration(view.opt('minTime')); - this.maxTime = moment.duration(view.opt('maxTime')); + this.minResizeDuration = snapDuration; // hack - this.axisFormat = view.opt('axisFormat') || view.opt('smallTimeFormat'); + // might be an array value (for TimelineView). + // if so, getting the most granular entry (the last one probably). + input = view.opt('slotLabelFormat'); + if ($.isArray(input)) { + input = input[input.length - 1]; + } + + this.labelFormat = + input || + view.opt('smallTimeFormat'); // the computed default + + input = view.opt('slotLabelInterval'); + this.labelInterval = input ? + moment.duration(input) : + this.computeLabelInterval(slotDuration); }, - // Computes a default column header formatting string if `colFormat` is not explicitly defined - computeColHeadFormat: function() { - if (this.colCnt > 1) { // multiple days, so full single date string WON'T be in title text - return this.view.opt('dayOfMonthFormat'); // "Sat 12/10" - } - else { // single day, so full single date string will probably be in title text - return 'dddd'; // "Saturday" + // Computes an automatic value for slotLabelInterval + computeLabelInterval: function(slotDuration) { + var i; + var labelInterval; + var slotsPerLabel; + + // find the smallest stock label interval that results in more than one slots-per-label + for (i = AGENDA_STOCK_SUB_DURATIONS.length - 1; i >= 0; i--) { + labelInterval = moment.duration(AGENDA_STOCK_SUB_DURATIONS[i]); + slotsPerLabel = divideDurationByDuration(labelInterval, slotDuration); + if (isInt(slotsPerLabel) && slotsPerLabel > 1) { + return labelInterval; + } } + + return moment.duration(slotDuration); // fall back. clone }, @@ -5817,47 +8191,68 @@ var TimeGrid = Grid.extend({ }, - /* Cell System + /* Hit System ------------------------------------------------------------------------------------------------------------------*/ - rangeUpdated: function() { - var view = this.view; - var colDates = []; - var date; - - date = this.start.clone(); - while (date.isBefore(this.end)) { - colDates.push(date.clone()); - date.add(1, 'day'); - date = view.skipHiddenDays(date); - } - - if (this.isRTL) { - colDates.reverse(); - } - - this.colDates = colDates; - this.colCnt = colDates.length; - this.rowCnt = Math.ceil((this.maxTime - this.minTime) / this.snapDuration); // # of vertical snaps + prepareHits: function() { + this.colCoordCache.build(); + this.slatCoordCache.build(); }, - // Given a cell object, generates its start date. Returns a reference-free copy. - computeCellDate: function(cell) { - var date = this.colDates[cell.col]; - var time = this.computeSnapTime(cell.row); - - date = this.view.calendar.rezoneDate(date); // give it a 00:00 time - date.time(time); - - return date; + releaseHits: function() { + this.colCoordCache.clear(); + // NOTE: don't clear slatCoordCache because we rely on it for computeTimeTop }, - // Retrieves the element representing the given column - getColEl: function(col) { - return this.dayEls.eq(col); + queryHit: function(leftOffset, topOffset) { + var snapsPerSlot = this.snapsPerSlot; + var colCoordCache = this.colCoordCache; + var slatCoordCache = this.slatCoordCache; + + if (colCoordCache.isLeftInBounds(leftOffset) && slatCoordCache.isTopInBounds(topOffset)) { + var colIndex = colCoordCache.getHorizontalIndex(leftOffset); + var slatIndex = slatCoordCache.getVerticalIndex(topOffset); + + if (colIndex != null && slatIndex != null) { + var slatTop = slatCoordCache.getTopOffset(slatIndex); + var slatHeight = slatCoordCache.getHeight(slatIndex); + var partial = (topOffset - slatTop) / slatHeight; // floating point number between 0 and 1 + var localSnapIndex = Math.floor(partial * snapsPerSlot); // the snap # relative to start of slat + var snapIndex = slatIndex * snapsPerSlot + localSnapIndex; + var snapTop = slatTop + (localSnapIndex / snapsPerSlot) * slatHeight; + var snapBottom = slatTop + ((localSnapIndex + 1) / snapsPerSlot) * slatHeight; + + return { + col: colIndex, + snap: snapIndex, + component: this, // needed unfortunately :( + left: colCoordCache.getLeftOffset(colIndex), + right: colCoordCache.getRightOffset(colIndex), + top: snapTop, + bottom: snapBottom + }; + } + } + }, + + + getHitSpan: function(hit) { + var start = this.getCellDate(0, hit.col); // row=0 + var time = this.computeSnapTime(hit.snap); // pass in the snap-index + var end; + + start.time(time); + end = start.clone().add(this.snapDuration); + + return { start: start, end: end }; + }, + + + getHitEl: function(hit) { + return this.colEls.eq(hit.col); }, @@ -5865,36 +8260,51 @@ var TimeGrid = Grid.extend({ ------------------------------------------------------------------------------------------------------------------*/ - // Given a row number of the grid, representing a "snap", returns a time (Duration) from its start-of-day - computeSnapTime: function(row) { - return moment.duration(this.minTime + this.snapDuration * row); + rangeUpdated: function() { + this.updateDayTable(); }, - // Slices up a date range by column into an array of segments - rangeToSegs: function(range) { - var colCnt = this.colCnt; + // Given a row number of the grid, representing a "snap", returns a time (Duration) from its start-of-day + computeSnapTime: function(snapIndex) { + return moment.duration(this.view.minTime + this.snapDuration * snapIndex); + }, + + + // Slices up the given span (unzoned start/end with other misc data) into an array of segments + spanToSegs: function(span) { + var segs = this.sliceRangeByTimes(span); + var i; + + for (i = 0; i < segs.length; i++) { + if (this.isRTL) { + segs[i].col = this.daysPerRow - 1 - segs[i].dayIndex; + } + else { + segs[i].col = segs[i].dayIndex; + } + } + + return segs; + }, + + + sliceRangeByTimes: function(range) { var segs = []; var seg; - var col; - var colDate; - var colRange; + var dayIndex; + var dayDate; + var dayRange; - // normalize :( - range = { - start: range.start.clone().stripZone(), - end: range.end.clone().stripZone() - }; - - for (col = 0; col < colCnt; col++) { - colDate = this.colDates[col]; // will be ambig time/timezone - colRange = { - start: colDate.clone().time(this.minTime), - end: colDate.clone().time(this.maxTime) + for (dayIndex = 0; dayIndex < this.daysPerRow; dayIndex++) { + dayDate = this.dayDates[dayIndex].clone().time(0); // TODO: better API for this? + dayRange = { + start: dayDate.clone().add(this.view.minTime), // don't use .time() because it sux with negatives + end: dayDate.clone().add(this.view.maxTime) }; - seg = intersectionToSeg(range, colRange); // both will be ambig timezone + seg = intersectRanges(range, dayRange); // both will be ambig timezone if (seg) { - seg.col = col; + seg.dayIndex = dayIndex; segs.push(seg); } } @@ -5908,33 +8318,18 @@ var TimeGrid = Grid.extend({ updateSize: function(isResize) { // NOT a standard Grid method - this.computeSlatTops(); + this.slatCoordCache.build(); if (isResize) { - this.updateSegVerticals(); + this.updateSegVerticals( + [].concat(this.fgSegs || [], this.bgSegs || [], this.businessSegs || []) + ); } }, - // Computes the top/bottom coordinates of each "snap" rows - computeRowCoords: function() { - var originTop = this.el.offset().top; - var items = []; - var i; - var item; - - for (i = 0; i < this.rowCnt; i++) { - item = { - top: originTop + this.computeTimeTop(this.computeSnapTime(i)) - }; - if (i > 0) { - items[i - 1].bottom = item.top; - } - items.push(item); - } - item.bottom = item.top + this.computeTimeTop(this.computeSnapTime(i)); - - return items; + getTotalSlatHeight: function() { + return this.slatContainerEl.outerHeight(); }, @@ -5943,7 +8338,7 @@ var TimeGrid = Grid.extend({ computeDateTop: function(date, startOfDayDate) { return this.computeTimeTop( moment.duration( - date.clone().stripZone() - startOfDayDate.clone().stripTime() + date - startOfDayDate.clone().stripTime() ) ); }, @@ -5951,65 +8346,54 @@ var TimeGrid = Grid.extend({ // Computes the top coordinate, relative to the bounds of the grid, of the given time (a Duration). computeTimeTop: function(time) { - var slatCoverage = (time - this.minTime) / this.slotDuration; // floating-point value of # of slots covered + var len = this.slatEls.length; + var slatCoverage = (time - this.view.minTime) / this.slotDuration; // floating-point value of # of slots covered var slatIndex; var slatRemainder; - var slatTop; - var slatBottom; - // constrain. because minTime/maxTime might be customized + // compute a floating-point number for how many slats should be progressed through. + // from 0 to number of slats (inclusive) + // constrained because minTime/maxTime might be customized. slatCoverage = Math.max(0, slatCoverage); - slatCoverage = Math.min(this.slatEls.length, slatCoverage); + slatCoverage = Math.min(len, slatCoverage); - slatIndex = Math.floor(slatCoverage); // an integer index of the furthest whole slot + // an integer index of the furthest whole slat + // from 0 to number slats (*exclusive*, so len-1) + slatIndex = Math.floor(slatCoverage); + slatIndex = Math.min(slatIndex, len - 1); + + // how much further through the slatIndex slat (from 0.0-1.0) must be covered in addition. + // could be 1.0 if slatCoverage is covering *all* the slots slatRemainder = slatCoverage - slatIndex; - slatTop = this.slatTops[slatIndex]; // the top position of the furthest whole slot - if (slatRemainder) { // time spans part-way into the slot - slatBottom = this.slatTops[slatIndex + 1]; - return slatTop + (slatBottom - slatTop) * slatRemainder; // part-way between slots - } - else { - return slatTop; - } + return this.slatCoordCache.getTopPosition(slatIndex) + + this.slatCoordCache.getHeight(slatIndex) * slatRemainder; }, - // Queries each `slatEl` for its position relative to the grid's container and stores it in `slatTops`. - // Includes the the bottom of the last slat as the last item in the array. - computeSlatTops: function() { - var tops = []; - var top; - - this.slatEls.each(function(i, node) { - top = $(node).position().top; - tops.push(top); - }); - - tops.push(top + this.slatEls.last().outerHeight()); // bottom of the last slat - - this.slatTops = tops; - }, - /* Event Drag Visualization ------------------------------------------------------------------------------------------------------------------*/ // Renders a visual indication of an event being dragged over the specified date(s). - // dropLocation's end might be null, as well as `seg`. See Grid::renderDrag for more info. // A returned value of `true` signals that a mock "helper" event has been rendered. - renderDrag: function(dropLocation, seg) { + renderDrag: function(eventLocation, seg) { + var eventSpans; + var i; if (seg) { // if there is event information for this drag, render a helper event - this.renderRangeHelper(dropLocation, seg); - this.applyDragOpacity(this.helperEl); - return true; // signal that a helper has been rendered + // returns mock event elements + // signal that a helper has been rendered + return this.renderEventLocationHelper(eventLocation, seg); } - else { - // otherwise, just render a highlight - this.renderHighlight(this.eventRangeToSegs(dropLocation)); + else { // otherwise, just render a highlight + eventSpans = this.eventToSpans(eventLocation); + + for (i = 0; i < eventSpans.length; i++) { + this.renderHighlight(eventSpans[i]); + } } }, @@ -6026,8 +8410,8 @@ var TimeGrid = Grid.extend({ // Renders a visual indication of an event being resized - renderEventResize: function(range, seg) { - this.renderRangeHelper(range, seg); + renderEventResize: function(eventLocation, seg) { + return this.renderEventLocationHelper(eventLocation, seg); // returns mock event elements }, @@ -6043,13 +8427,204 @@ var TimeGrid = Grid.extend({ // Renders a mock "helper" event. `sourceSeg` is the original segment object and might be null (an external drag) renderHelper: function(event, sourceSeg) { - var segs = this.eventsToSegs([ event ]); - var tableEl; + return this.renderHelperSegs(this.eventToSegs(event), sourceSeg); // returns mock event elements + }, + + + // Unrenders any mock helper event + unrenderHelper: function() { + this.unrenderHelperSegs(); + }, + + + /* Business Hours + ------------------------------------------------------------------------------------------------------------------*/ + + + renderBusinessHours: function() { + this.renderBusinessSegs( + this.buildBusinessHourSegs() + ); + }, + + + unrenderBusinessHours: function() { + this.unrenderBusinessSegs(); + }, + + + /* Now Indicator + ------------------------------------------------------------------------------------------------------------------*/ + + + getNowIndicatorUnit: function() { + return 'minute'; // will refresh on the minute + }, + + + renderNowIndicator: function(date) { + // seg system might be overkill, but it handles scenario where line needs to be rendered + // more than once because of columns with the same date (resources columns for example) + var segs = this.spanToSegs({ start: date, end: date }); + var top = this.computeDateTop(date, date); + var nodes = []; + var i; + + // render lines within the columns + for (i = 0; i < segs.length; i++) { + nodes.push($('
') + .css('top', top) + .appendTo(this.colContainerEls.eq(segs[i].col))[0]); + } + + // render an arrow over the axis + if (segs.length > 0) { // is the current time in view? + nodes.push($('
') + .css('top', top) + .appendTo(this.el.find('.fc-content-skeleton'))[0]); + } + + this.nowIndicatorEls = $(nodes); + }, + + + unrenderNowIndicator: function() { + if (this.nowIndicatorEls) { + this.nowIndicatorEls.remove(); + this.nowIndicatorEls = null; + } + }, + + + /* Selection + ------------------------------------------------------------------------------------------------------------------*/ + + + // Renders a visual indication of a selection. Overrides the default, which was to simply render a highlight. + renderSelection: function(span) { + if (this.view.opt('selectHelper')) { // this setting signals that a mock helper event should be rendered + + // normally acceps an eventLocation, span has a start/end, which is good enough + this.renderEventLocationHelper(span); + } + else { + this.renderHighlight(span); + } + }, + + + // Unrenders any visual indication of a selection + unrenderSelection: function() { + this.unrenderHelper(); + this.unrenderHighlight(); + }, + + + /* Highlight + ------------------------------------------------------------------------------------------------------------------*/ + + + renderHighlight: function(span) { + this.renderHighlightSegs(this.spanToSegs(span)); + }, + + + unrenderHighlight: function() { + this.unrenderHighlightSegs(); + } + +}); + +;; + +/* Methods for rendering SEGMENTS, pieces of content that live on the view + ( this file is no longer just for events ) +----------------------------------------------------------------------------------------------------------------------*/ + +TimeGrid.mixin({ + + colContainerEls: null, // containers for each column + + // inner-containers for each column where different types of segs live + fgContainerEls: null, + bgContainerEls: null, + helperContainerEls: null, + highlightContainerEls: null, + businessContainerEls: null, + + // arrays of different types of displayed segments + fgSegs: null, + bgSegs: null, + helperSegs: null, + highlightSegs: null, + businessSegs: null, + + + // Renders the DOM that the view's content will live in + renderContentSkeleton: function() { + var cellHtml = ''; + var i; + var skeletonEl; + + for (i = 0; i < this.colCnt; i++) { + cellHtml += + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '' + cellHtml + '' + + '
' + + '
' + ); + + this.colContainerEls = skeletonEl.find('.fc-content-col'); + this.helperContainerEls = skeletonEl.find('.fc-helper-container'); + this.fgContainerEls = skeletonEl.find('.fc-event-container:not(.fc-helper-container)'); + this.bgContainerEls = skeletonEl.find('.fc-bgevent-container'); + this.highlightContainerEls = skeletonEl.find('.fc-highlight-container'); + this.businessContainerEls = skeletonEl.find('.fc-business-container'); + + this.bookendCells(skeletonEl.find('tr')); // TODO: do this on string level + this.el.append(skeletonEl); + }, + + + /* Foreground Events + ------------------------------------------------------------------------------------------------------------------*/ + + + renderFgSegs: function(segs) { + segs = this.renderFgSegsIntoContainers(segs, this.fgContainerEls); + this.fgSegs = segs; + return segs; // needed for Grid::renderEvents + }, + + + unrenderFgSegs: function() { + this.unrenderNamedSegs('fgSegs'); + }, + + + /* Foreground Helper Events + ------------------------------------------------------------------------------------------------------------------*/ + + + renderHelperSegs: function(segs, sourceSeg) { + var helperEls = []; var i, seg; var sourceEl; - segs = this.renderFgSegEls(segs); // assigns each seg's el and returns a subset of segs that were rendered - tableEl = this.renderSegTable(segs); + segs = this.renderFgSegsIntoContainers(segs, this.helperContainerEls); // Try to make the segment that is in the same row as sourceSeg look the same for (i = 0; i < segs.length; i++) { @@ -6063,207 +8638,149 @@ var TimeGrid = Grid.extend({ 'margin-right': sourceEl.css('margin-right') }); } + helperEls.push(seg.el[0]); } - this.helperEl = $('
') - .append(tableEl) - .appendTo(this.el); + this.helperSegs = segs; + + return $(helperEls); // must return rendered helpers }, - // Unrenders any mock helper event - unrenderHelper: function() { - if (this.helperEl) { - this.helperEl.remove(); - this.helperEl = null; - } + unrenderHelperSegs: function() { + this.unrenderNamedSegs('helperSegs'); }, - /* Selection + /* Background Events ------------------------------------------------------------------------------------------------------------------*/ - // Renders a visual indication of a selection. Overrides the default, which was to simply render a highlight. - renderSelection: function(range) { - if (this.view.opt('selectHelper')) { // this setting signals that a mock helper event should be rendered - this.renderRangeHelper(range); - } - else { - this.renderHighlight(this.selectionRangeToSegs(range)); - } + renderBgSegs: function(segs) { + segs = this.renderFillSegEls('bgEvent', segs); // TODO: old fill system + this.updateSegVerticals(segs); + this.attachSegsByCol(this.groupSegsByCol(segs), this.bgContainerEls); + this.bgSegs = segs; + return segs; // needed for Grid::renderEvents }, - // Unrenders any visual indication of a selection - unrenderSelection: function() { - this.unrenderHelper(); - this.unrenderHighlight(); + unrenderBgSegs: function() { + this.unrenderNamedSegs('bgSegs'); }, - /* Fill System (highlight, background events, business hours) + /* Highlight ------------------------------------------------------------------------------------------------------------------*/ - // Renders a set of rectangles over the given time segments. - // Only returns segments that successfully rendered. - renderFill: function(type, segs, className) { - var segCols; - var skeletonEl; - var trEl; - var col, colSegs; - var tdEl; - var containerEl; - var dayDate; - var i, seg; - - if (segs.length) { - - segs = this.renderFillSegEls(type, segs); // assignes `.el` to each seg. returns successfully rendered segs - segCols = this.groupSegCols(segs); // group into sub-arrays, and assigns 'col' to each seg - - className = className || type.toLowerCase(); - skeletonEl = $( - '
' + - '
' + - '
' - ); - trEl = skeletonEl.find('tr'); - - for (col = 0; col < segCols.length; col++) { - colSegs = segCols[col]; - tdEl = $('').appendTo(trEl); - - if (colSegs.length) { - containerEl = $('
').appendTo(tdEl); - dayDate = this.colDates[col]; - - for (i = 0; i < colSegs.length; i++) { - seg = colSegs[i]; - containerEl.append( - seg.el.css({ - top: this.computeDateTop(seg.start, dayDate), - bottom: -this.computeDateTop(seg.end, dayDate) // the y position of the bottom edge - }) - ); - } - } - } - - this.bookendCells(trEl, type); - - this.el.append(skeletonEl); - this.elsByFill[type] = skeletonEl; - } - - return segs; - } - -}); - -;; - -/* Event-rendering methods for the TimeGrid class -----------------------------------------------------------------------------------------------------------------------*/ - -TimeGrid.mixin({ - - eventSkeletonEl: null, // has cells with event-containers, which contain absolutely positioned event elements - - - // Renders the given foreground event segments onto the grid - renderFgSegs: function(segs) { - segs = this.renderFgSegEls(segs); // returns a subset of the segs. segs that were actually rendered - - this.el.append( - this.eventSkeletonEl = $('
') - .append(this.renderSegTable(segs)) - ); - - return segs; // return only the segs that were actually rendered + renderHighlightSegs: function(segs) { + segs = this.renderFillSegEls('highlight', segs); // TODO: old fill system + this.updateSegVerticals(segs); + this.attachSegsByCol(this.groupSegsByCol(segs), this.highlightContainerEls); + this.highlightSegs = segs; }, - // Unrenders all currently rendered foreground event segments - unrenderFgSegs: function(segs) { - if (this.eventSkeletonEl) { - this.eventSkeletonEl.remove(); - this.eventSkeletonEl = null; - } + unrenderHighlightSegs: function() { + this.unrenderNamedSegs('highlightSegs'); }, - // Renders and returns the portion of the event-skeleton. - // Returns an object with properties 'tbodyEl' and 'segs'. - renderSegTable: function(segs) { - var tableEl = $('
'); - var trEl = tableEl.find('tr'); - var segCols; - var i, seg; - var col, colSegs; - var containerEl; + /* Business Hours + ------------------------------------------------------------------------------------------------------------------*/ - segCols = this.groupSegCols(segs); // group into sub-arrays, and assigns 'col' to each seg - this.computeSegVerticals(segs); // compute and assign top/bottom - - for (col = 0; col < segCols.length; col++) { // iterate each column grouping - colSegs = segCols[col]; - placeSlotSegs(colSegs); // compute horizontal coordinates, z-index's, and reorder the array - - containerEl = $('
'); - - // assign positioning CSS and insert into container - for (i = 0; i < colSegs.length; i++) { - seg = colSegs[i]; - seg.el.css(this.generateSegPositionCss(seg)); - - // if the height is short, add a className for alternate styling - if (seg.bottom - seg.top < 30) { - seg.el.addClass('fc-short'); - } - - containerEl.append(seg.el); - } - - trEl.append($('').append(containerEl)); - } - - this.bookendCells(trEl, 'eventSkeleton'); - - return tableEl; + renderBusinessSegs: function(segs) { + segs = this.renderFillSegEls('businessHours', segs); // TODO: old fill system + this.updateSegVerticals(segs); + this.attachSegsByCol(this.groupSegsByCol(segs), this.businessContainerEls); + this.businessSegs = segs; }, - // Refreshes the CSS top/bottom coordinates for each segment element. Probably after a window resize/zoom. - // Repositions business hours segs too, so not just for events. Maybe shouldn't be here. - updateSegVerticals: function() { - var allSegs = (this.segs || []).concat(this.businessHourSegs || []); + unrenderBusinessSegs: function() { + this.unrenderNamedSegs('businessSegs'); + }, + + + /* Seg Rendering Utils + ------------------------------------------------------------------------------------------------------------------*/ + + + // Given a flat array of segments, return an array of sub-arrays, grouped by each segment's col + groupSegsByCol: function(segs) { + var segsByCol = []; var i; - this.computeSegVerticals(allSegs); + for (i = 0; i < this.colCnt; i++) { + segsByCol.push([]); + } - for (i = 0; i < allSegs.length; i++) { - allSegs[i].el.css( - this.generateSegVerticalCss(allSegs[i]) - ); + for (i = 0; i < segs.length; i++) { + segsByCol[segs[i].col].push(segs[i]); + } + + return segsByCol; + }, + + + // Given segments grouped by column, insert the segments' elements into a parallel array of container + // elements, each living within a column. + attachSegsByCol: function(segsByCol, containerEls) { + var col; + var segs; + var i; + + for (col = 0; col < this.colCnt; col++) { // iterate each column grouping + segs = segsByCol[col]; + + for (i = 0; i < segs.length; i++) { + containerEls.eq(col).append(segs[i].el); + } } }, - // For each segment in an array, computes and assigns its top and bottom properties - computeSegVerticals: function(segs) { - var i, seg; + // Given the name of a property of `this` object, assumed to be an array of segments, + // loops through each segment and removes from DOM. Will null-out the property afterwards. + unrenderNamedSegs: function(propName) { + var segs = this[propName]; + var i; - for (i = 0; i < segs.length; i++) { - seg = segs[i]; - seg.top = this.computeDateTop(seg.start, seg.start); - seg.bottom = this.computeDateTop(seg.end, seg.start); + if (segs) { + for (i = 0; i < segs.length; i++) { + segs[i].el.remove(); + } + this[propName] = null; } }, + + /* Foreground Event Rendering Utils + ------------------------------------------------------------------------------------------------------------------*/ + + + // Given an array of foreground segments, render a DOM element for each, computes position, + // and attaches to the column inner-container elements. + renderFgSegsIntoContainers: function(segs, containerEls) { + var segsByCol; + var col; + + segs = this.renderFgSegEls(segs); // will call fgSegHtml + segsByCol = this.groupSegsByCol(segs); + + for (col = 0; col < this.colCnt; col++) { + this.updateFgSegCoords(segsByCol[col]); + } + + this.attachSegsByCol(segsByCol, containerEls); + + return segs; + }, + + // Renders the HTML for a single event segment's default rendering fgSegHtml: function(seg, disableResizing) { var view = this.view; @@ -6272,7 +8789,7 @@ TimeGrid.mixin({ var isResizableFromStart = !disableResizing && seg.isStart && view.isEventResizableFromStart(event); var isResizableFromEnd = !disableResizing && seg.isEnd && view.isEventResizableFromEnd(event); var classes = this.getSegClasses(seg, isDraggable, isResizableFromStart || isResizableFromEnd); - var skinCss = cssToStr(this.getEventSkinCss(event)); + var skinCss = cssToStr(this.getSegSkinCss(seg)); var timeText; var fullTimeText; // more verbose time text. for the print stylesheet var startTimeText; // just the start time text @@ -6337,9 +8854,172 @@ TimeGrid.mixin({ }, + /* Seg Position Utils + ------------------------------------------------------------------------------------------------------------------*/ + + + // Refreshes the CSS top/bottom coordinates for each segment element. + // Works when called after initial render, after a window resize/zoom for example. + updateSegVerticals: function(segs) { + this.computeSegVerticals(segs); + this.assignSegVerticals(segs); + }, + + + // For each segment in an array, computes and assigns its top and bottom properties + computeSegVerticals: function(segs) { + var i, seg; + var dayDate; + + for (i = 0; i < segs.length; i++) { + seg = segs[i]; + dayDate = this.dayDates[seg.dayIndex]; + + seg.top = this.computeDateTop(seg.start, dayDate); + seg.bottom = this.computeDateTop(seg.end, dayDate); + } + }, + + + // Given segments that already have their top/bottom properties computed, applies those values to + // the segments' elements. + assignSegVerticals: function(segs) { + var i, seg; + + for (i = 0; i < segs.length; i++) { + seg = segs[i]; + seg.el.css(this.generateSegVerticalCss(seg)); + } + }, + + + // Generates an object with CSS properties for the top/bottom coordinates of a segment element + generateSegVerticalCss: function(seg) { + return { + top: seg.top, + bottom: -seg.bottom // flipped because needs to be space beyond bottom edge of event container + }; + }, + + + /* Foreground Event Positioning Utils + ------------------------------------------------------------------------------------------------------------------*/ + + + // Given segments that are assumed to all live in the *same column*, + // compute their verical/horizontal coordinates and assign to their elements. + updateFgSegCoords: function(segs) { + this.computeSegVerticals(segs); // horizontals relies on this + this.computeFgSegHorizontals(segs); // compute horizontal coordinates, z-index's, and reorder the array + this.assignSegVerticals(segs); + this.assignFgSegHorizontals(segs); + }, + + + // Given an array of segments that are all in the same column, sets the backwardCoord and forwardCoord on each. + // NOTE: Also reorders the given array by date! + computeFgSegHorizontals: function(segs) { + var levels; + var level0; + var i; + + this.sortEventSegs(segs); // order by certain criteria + levels = buildSlotSegLevels(segs); + computeForwardSlotSegs(levels); + + if ((level0 = levels[0])) { + + for (i = 0; i < level0.length; i++) { + computeSlotSegPressures(level0[i]); + } + + for (i = 0; i < level0.length; i++) { + this.computeFgSegForwardBack(level0[i], 0, 0); + } + } + }, + + + // Calculate seg.forwardCoord and seg.backwardCoord for the segment, where both values range + // from 0 to 1. If the calendar is left-to-right, the seg.backwardCoord maps to "left" and + // seg.forwardCoord maps to "right" (via percentage). Vice-versa if the calendar is right-to-left. + // + // The segment might be part of a "series", which means consecutive segments with the same pressure + // who's width is unknown until an edge has been hit. `seriesBackwardPressure` is the number of + // segments behind this one in the current series, and `seriesBackwardCoord` is the starting + // coordinate of the first segment in the series. + computeFgSegForwardBack: function(seg, seriesBackwardPressure, seriesBackwardCoord) { + var forwardSegs = seg.forwardSegs; + var i; + + if (seg.forwardCoord === undefined) { // not already computed + + if (!forwardSegs.length) { + + // if there are no forward segments, this segment should butt up against the edge + seg.forwardCoord = 1; + } + else { + + // sort highest pressure first + this.sortForwardSegs(forwardSegs); + + // this segment's forwardCoord will be calculated from the backwardCoord of the + // highest-pressure forward segment. + this.computeFgSegForwardBack(forwardSegs[0], seriesBackwardPressure + 1, seriesBackwardCoord); + seg.forwardCoord = forwardSegs[0].backwardCoord; + } + + // calculate the backwardCoord from the forwardCoord. consider the series + seg.backwardCoord = seg.forwardCoord - + (seg.forwardCoord - seriesBackwardCoord) / // available width for series + (seriesBackwardPressure + 1); // # of segments in the series + + // use this segment's coordinates to computed the coordinates of the less-pressurized + // forward segments + for (i=0; i seg2.top && seg1.top < seg2.bottom; } - -// A cmp function for determining which forward segment to rely on more when computing coordinates. -function compareForwardSlotSegs(seg1, seg2) { - // put higher-pressure first - return seg2.forwardPressure - seg1.forwardPressure || - // put segments that are closer to initial edge first (and favor ones with no coords yet) - (seg1.backwardCoord || 0) - (seg2.backwardCoord || 0) || - // do normal sorting... - compareSegs(seg1, seg2); -} - ;; /* An abstract class from which other views inherit from ----------------------------------------------------------------------------------------------------------------------*/ -var View = fc.View = Class.extend({ +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 - coordMap: null, // a CoordMap object for converting pixel regions to dates el: null, // the view's containing element. set by Calendar - displaying: null, // a promise representing the state of rendering. null if no render requested - isSkeletonRendered: false, + renderQueue: null, + batchRenderDepth: 0, + isDatesRendered: false, isEventsRendered: false, + isBaseRendered: false, // related to viewRender/viewDestroy triggers - // 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" + queuedScroll: null, isRTL: false, isSelected: false, // boolean whether a range of time is user-selected or not + selectedEvent: null, - // subclasses can optionally use a scroll container - scrollerEl: null, // the element that will most likely scroll when content is too tall - scrollTop: null, // cached vertical scroll value + eventOrderSpecs: null, // criteria for ordering events when they have same date/time // classNames styled by jqui themes widgetHeaderClass: null, @@ -6624,28 +9191,88 @@ var View = fc.View = Class.extend({ nextDayThreshold: null, isHiddenDayHash: null, - // document handlers, bound to `this` object - documentMousedownProxy: null, // TODO: doesn't work with touch + // 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) { + constructor: function(calendar, viewSpec) { + Model.prototype.constructor.call(this); this.calendar = calendar; - this.type = this.name = type; // .name is deprecated - this.options = options; - this.intervalDuration = intervalDuration || moment.duration(1, 'day'); + 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.documentMousedownProxy = proxy(this, 'documentMousedown'); + this.eventOrderSpecs = parseFieldSpecs(this.opt('eventOrder')); + + this.renderQueue = this.buildRenderQueue(); + this.initAutoBatchRender(); this.initialize(); }, + buildRenderQueue: function() { + var _this = this; + var renderQueue = new RenderQueue({ + event: this.opt('eventRenderWait') + }); + + renderQueue.on('start', function() { + _this.freezeHeight(); + _this.addScroll(_this.queryScroll()); + }); + + renderQueue.on('stop', function() { + _this.thawHeight(); + _this.popScroll(); + }); + + return renderQueue; + }, + + + initAutoBatchRender: function() { + var _this = this; + + this.on('before:change', function() { + _this.startBatchRender(); + }); + + this.on('change', function() { + _this.stopBatchRender(); + }); + }, + + + startBatchRender: function() { + if (!(this.batchRenderDepth++)) { + this.renderQueue.pause(); + } + }, + + + stopBatchRender: function() { + if (!(--this.batchRenderDepth)) { + this.renderQueue.resume(); + } + }, + + // A good place for subclasses to initialize member variables initialize: function() { // subclasses can implement @@ -6659,10 +9286,10 @@ var View = fc.View = Class.extend({ // Triggers handlers that are view-related. Modifies args before passing to calendar. - trigger: function(name, thisObj) { // arguments beyond thisObj are passed along + publiclyTrigger: function(name, thisObj) { // arguments beyond thisObj are passed along var calendar = this.calendar; - return calendar.trigger.apply( + return calendar.publiclyTrigger.apply( calendar, [name, thisObj || this].concat( Array.prototype.slice.call(arguments, 2), // arguments beyond thisObj @@ -6672,92 +9299,6 @@ var View = fc.View = Class.extend({ }, - /* Dates - ------------------------------------------------------------------------------------------------------------------*/ - - - // Updates all internal dates to center around the given current date - setDate: function(date) { - this.setRange(this.computeRange(date)); - }, - - - // Updates all internal dates for displaying the given range. - // Expects all values to be normalized (like what computeRange does). - setRange: function(range) { - $.extend(this, range); - this.updateTitle(); - }, - - - // Given a single current 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.rezoneDate(intervalStart); // convert to current timezone, with 00:00 - } - if (!intervalEnd.hasTime()) { - intervalEnd = this.calendar.rezoneDate(intervalEnd); // convert to current timezone, with 00:00 - } - } - - 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 ------------------------------------------------------------------------------------------------------------------*/ @@ -6765,13 +9306,28 @@ var View = fc.View = Class.extend({ // 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( - { start: this.intervalStart, end: this.intervalEnd }, + { + // 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') ); @@ -6781,13 +9337,13 @@ var View = fc.View = Class.extend({ // 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') { + if (this.currentRangeUnit == 'year') { return 'YYYY'; } - else if (this.intervalUnit == 'month') { + else if (this.currentRangeUnit == 'month') { return this.opt('monthYearFormat'); // like "September 2014" } - else if (this.intervalDuration.as('days') > 1) { + else if (this.currentRangeAs('days') > 1) { return 'll'; // multi-day range. shorter, like "Sep 9 - 10 2014" } else { @@ -6798,6 +9354,7 @@ var View = fc.View = Class.extend({ // 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; @@ -6809,114 +9366,92 @@ var View = fc.View = Class.extend({ }, - /* Rendering + getAllDayHtml: function() { + return this.opt('allDayHtml') || htmlEscape(this.opt('allDayText')); + }, + + + /* Navigation ------------------------------------------------------------------------------------------------------------------*/ - // Sets the container element that the view should render inside of. - // Does other DOM-related initializations. + // Generates HTML for an anchor to another view into the calendar. + // Will either generate an tag or a non-clickable 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 '' + + innerHtml + + ''; + } + else { + return '' + + innerHtml + + ''; + } + }, + + + // 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.clear(); // clears all content - - // clean up the skeleton - if (this.isSkeletonRendered) { - this.unrenderSkeleton(); - this.isSkeletonRendered = false; - } + 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. }, - // Does everything necessary to display the view centered around the given date. - // Does every type of rendering EXCEPT rendering events. - // Is asychronous and returns a promise. - display: function(date) { - var _this = this; - var scrollState = null; - - if (this.displaying) { - scrollState = this.queryScroll(); - } - - return this.clear().then(function() { // clear the content first (async) - return ( - _this.displaying = - $.when(_this.displayView(date)) // displayView might return a promise - .then(function() { - _this.forceScroll(_this.computeInitialScroll(scrollState)); - _this.triggerRender(); - }) - ); - }); - }, - - - // Does everything necessary to clear the content of the view. - // Clears dates and events. Does not clear the skeleton. - // Is asychronous and returns a promise. - clear: function() { - var _this = this; - var displaying = this.displaying; - - if (displaying) { // previously displayed, or in the process of being displayed? - return displaying.then(function() { // wait for the display to finish - _this.displaying = null; - _this.clearEvents(); - return _this.clearView(); // might return a promise. chain it - }); - } - else { - return $.when(); // an immediately-resolved promise - } - }, - - - // Displays the view's non-event content, such as date-related content or anything required by events. - // Renders the view's non-content skeleton if necessary. - // Can be asynchronous and return a promise. - displayView: function(date) { - if (!this.isSkeletonRendered) { - this.renderSkeleton(); - this.isSkeletonRendered = true; - } - this.setDate(date); - if (this.render) { - this.render(); // TODO: deprecate - } - this.renderDates(); - this.updateSize(); - this.renderBusinessHours(); // might need coordinates, so should go after updateSize() - }, - - - // Unrenders the view content that was rendered in displayView. - // Can be asynchronous and return a promise. - clearView: function() { - this.unselect(); - this.triggerUnrender(); - this.unrenderBusinessHours(); - this.unrenderDates(); - if (this.destroy) { - this.destroy(); // TODO: deprecate - } - }, - - // Renders the basic structure of the view before any content is rendered renderSkeleton: function() { // subclasses should implement @@ -6929,19 +9464,246 @@ var View = fc.View = Class.extend({ }, - // Renders the view's date-related content (like cells that represent days/times). - // Assumes setRange has already been called and the skeleton has already been rendered. + // Date Setting/Unsetting + // ----------------------------------------------------------------------------------------------------------------- + + + setDate: function(date) { + var currentDateProfile = this.get('dateProfile'); + var newDateProfile = this.buildDateProfile(date, null, true); // forceToValid=true + + if ( + !currentDateProfile || + !isRangesEqual(currentDateProfile.activeRange, newDateProfile.activeRange) + ) { + this.set('dateProfile', newDateProfile); + } + + return newDateProfile.date; + }, + + + unsetDate: function() { + this.unset('dateProfile'); + }, + + + // Date Rendering + // ----------------------------------------------------------------------------------------------------------------- + + + requestDateRender: function(dateProfile) { + var _this = this; + + this.renderQueue.queue(function() { + _this.executeDateRender(dateProfile); + }, 'date', 'init'); + }, + + + requestDateUnrender: function() { + var _this = this; + + this.renderQueue.queue(function() { + _this.executeDateUnrender(); + }, 'date', 'destroy'); + }, + + + // Event Data + // ----------------------------------------------------------------------------------------------------------------- + + + fetchInitialEvents: function(dateProfile) { + return this.calendar.requestEvents( + dateProfile.activeRange.start, + dateProfile.activeRange.end + ); + }, + + + bindEventChanges: function() { + this.listenTo(this.calendar, 'eventsReset', this.resetEvents); + }, + + + unbindEventChanges: function() { + this.stopListeningTo(this.calendar, 'eventsReset'); + }, + + + setEvents: function(events) { + this.set('currentEvents', events); + this.set('hasEvents', true); + }, + + + unsetEvents: function() { + this.unset('currentEvents'); + this.unset('hasEvents'); + }, + + + resetEvents: function(events) { + this.startBatchRender(); + this.unsetEvents(); + this.setEvents(events); + this.stopBatchRender(); + }, + + + // Event Rendering + // ----------------------------------------------------------------------------------------------------------------- + + + requestEventsRender: function(events) { + var _this = this; + + this.renderQueue.queue(function() { + _this.executeEventsRender(events); + }, 'event', 'init'); + }, + + + requestEventsUnrender: function() { + var _this = this; + + this.renderQueue.queue(function() { + _this.executeEventsUnrender(); + }, 'event', 'destroy'); + }, + + + // Date High-level Rendering + // ----------------------------------------------------------------------------------------------------------------- + + + // if dateProfile not specified, uses current + executeDateRender: function(dateProfile, skipScroll) { + + this.setDateProfileForRendering(dateProfile); + this.updateTitle(); + this.calendar.updateToolbarButtons(); + + if (this.render) { + this.render(); // TODO: deprecate + } + + this.renderDates(); + this.updateSize(); + this.renderBusinessHours(); // might need coordinates, so should go after updateSize() + this.startNowIndicator(); + + if (!skipScroll) { + this.addScroll(this.computeInitialDateScroll()); + } + + this.isDatesRendered = true; + this.trigger('datesRendered'); + }, + + + executeDateUnrender: function() { + + this.unselect(); + this.stopNowIndicator(); + + this.trigger('before:datesUnrendered'); + + this.unrenderBusinessHours(); + this.unrenderDates(); + + if (this.destroy) { + this.destroy(); // TODO: deprecate + } + + this.isDatesRendered = false; + }, + + + // Date Low-level Rendering + // ----------------------------------------------------------------------------------------------------------------- + + + // date-cell content only renderDates: function() { // subclasses should implement }, - // Unrenders the view's date-related content + // 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.applyScreenState(); + this.publiclyTrigger('viewRender', this, this, this.el); + }, + + + onBeforeBaseUnrender: function() { + this.applyScreenState(); + 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 @@ -6954,37 +9716,91 @@ var View = fc.View = Class.extend({ }, - // Signals that the view's content has been rendered - triggerRender: function() { - this.trigger('viewRender', this, this, this.el); + /* 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); + } + } }, - // Signals that the view's content is about to be unrendered - triggerUnrender: function() { - this.trigger('viewDestroy', this, this, this.el); + // 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 + ); + } }, - // Binds DOM handlers to elements that reside outside the view container, such as the document - bindGlobalHandlers: function() { - $(document).on('mousedown', this.documentMousedownProxy); + // 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; + } }, - // Unbinds DOM handlers from elements that reside outside the view container - unbindGlobalHandlers: function() { - $(document).off('mousedown', this.documentMousedownProxy); + // 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 }, - // Initializes internal variables related to theming - initThemingProps: function() { - var tm = this.opt('theme') ? 'ui' : 'fc'; + // Renders a current time indicator at the given datetime + renderNowIndicator: function(date) { + // subclasses should implement + }, - this.widgetHeaderClass = tm + '-widget-header'; - this.widgetContentClass = tm + '-widget-content'; - this.highlightStateClass = tm + '-state-highlight'; + + // Undoes the rendering actions from renderNowIndicator + unrenderNowIndicator: function() { + // subclasses should implement }, @@ -6994,17 +9810,18 @@ var View = fc.View = Class.extend({ // Refreshes anything dependant upon sizing of the container element of the grid updateSize: function(isResize) { - var scrollState; + var scroll; if (isResize) { - scrollState = this.queryScroll(); + scroll = this.queryScroll(); } this.updateHeight(isResize); this.updateWidth(isResize); + this.updateNowIndicator(); if (isResize) { - this.setScroll(scrollState); + this.applyScroll(scroll); } }, @@ -7037,90 +9854,142 @@ var View = fc.View = Class.extend({ ------------------------------------------------------------------------------------------------------------------*/ - // Given the total height of the view, return the number of pixels that should be used for the scroller. - // Utility for subclasses. - computeScrollerHeight: function(totalHeight) { - var scrollerEl = this.scrollerEl; - var both; - var otherHeight; // cumulative height of everything that is not the scrollerEl in the view (header+borders) - - both = this.el.add(scrollerEl); - - // fuckin IE8/9/10/11 sometimes returns 0 for dimensions. this weird hack was the only thing that worked - both.css({ - position: 'relative', // cause a reflow, which will force fresh dimension recalculation - left: -1 // ensure reflow in case the el was already relative. negative is less likely to cause new scroll - }); - otherHeight = this.el.outerHeight() - scrollerEl.height(); // grab the dimensions - both.css({ position: '', left: '' }); // undo hack - - return totalHeight - otherHeight; + addForcedScroll: function(scroll) { + this.addScroll( + $.extend(scroll, { isForced: true }) + ); }, - // Computes the initial pre-configured scroll state prior to allowing the user to change it. - // Given the scroll state from the previous rendering. If first time rendering, given null. - computeInitialScroll: function(previousScrollState) { - return 0; + addScroll: function(scroll) { + var queuedScroll = this.queuedScroll || (this.queuedScroll = {}); + + if (!queuedScroll.isForced) { + $.extend(queuedScroll, scroll); + } + }, + + + popScroll: function() { + this.applyQueuedScroll(); + this.queuedScroll = null; + }, + + + applyQueuedScroll: function() { + if (this.queuedScroll) { + this.applyScroll(this.queuedScroll); + } }, - // Retrieves the view's current natural scroll state. Can return an arbitrary format. queryScroll: function() { - if (this.scrollerEl) { - return this.scrollerEl.scrollTop(); // operates on scrollerEl by default + var scroll = {}; + + if (this.isDatesRendered) { + $.extend(scroll, this.queryDateScroll()); + } + + return scroll; + }, + + + applyScroll: function(scroll) { + if (this.isDatesRendered) { + this.applyDateScroll(scroll); } }, - // Sets the view's scroll state. Will accept the same format computeInitialScroll and queryScroll produce. - setScroll: function(scrollState) { - if (this.scrollerEl) { - return this.scrollerEl.scrollTop(scrollState); // operates on scrollerEl by default - } + computeInitialDateScroll: function() { + return {}; // subclasses must implement }, - // Sets the scroll state, making sure to overcome any predefined scroll value the browser has in mind - forceScroll: function(scrollState) { - var _this = this; - - this.setScroll(scrollState); - setTimeout(function() { - _this.setScroll(scrollState); - }, 0); + queryDateScroll: function() { + return {}; // subclasses must implement }, - /* Event Elements / Segments + applyDateScroll: function(scroll) { + ; // subclasses must implement + }, + + + /* Height Freezing ------------------------------------------------------------------------------------------------------------------*/ - // Does everything necessary to display the given events onto the current view - displayEvents: function(events) { - var scrollState = this.queryScroll(); + freezeHeight: function() { + this.calendar.freezeContentHeight(); + }, - this.clearEvents(); + + thawHeight: function() { + this.calendar.thawContentHeight(); + }, + + + // Event High-level Rendering + // ----------------------------------------------------------------------------------------------------------------- + + + executeEventsRender: function(events) { this.renderEvents(events); this.isEventsRendered = true; - this.setScroll(scrollState); - this.triggerEventRender(); + + this.onEventsRender(); }, - // Does everything necessary to clear the view's currently-rendered events - clearEvents: function() { - if (this.isEventsRendered) { - this.triggerEventUnrender(); - if (this.destroyEvents) { - this.destroyEvents(); // TODO: deprecate - } - this.unrenderEvents(); - this.isEventsRendered = false; + executeEventsUnrender: function() { + this.onBeforeEventsUnrender(); + + if (this.destroyEvents) { + this.destroyEvents(); // TODO: deprecate } + + this.unrenderEvents(); + this.isEventsRendered = false; }, + // Event Rendering Triggers + // ----------------------------------------------------------------------------------------------------------------- + + + // Signals that all events have been rendered + onEventsRender: function() { + this.applyScreenState(); + + 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.applyScreenState(); + + this.renderedEventSegEach(function(seg) { + this.publiclyTrigger('eventDestroy', seg.event, seg.event, seg.el); + }); + }, + + + applyScreenState: function() { + this.thawHeight(); + this.freezeHeight(); + this.applyQueuedScroll(); + }, + + + // Event Low-level Rendering + // ----------------------------------------------------------------------------------------------------------------- + + // Renders the events onto the view. renderEvents: function(events) { // subclasses should implement @@ -7133,27 +10002,14 @@ var View = fc.View = Class.extend({ }, - // 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); - }); - }, + // 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.trigger('eventRender', event, event, el); + var custom = this.publiclyTrigger('eventRender', event, event, el); if (custom === false) { // means don't render at all el = null; @@ -7212,37 +10068,47 @@ var View = fc.View = Class.extend({ // Computes if the given event is allowed to be dragged by the user isEventDraggable: function(event) { - var source = event.source || {}; + return this.isEventStartEditable(event); + }, + + isEventStartEditable: function(event) { return firstDefined( event.startEditable, - source.startEditable, + (event.source || {}).startEditable, this.opt('eventStartEditable'), + this.isEventGenerallyEditable(event) + ); + }, + + + isEventGenerallyEditable: function(event) { + return firstDefined( event.editable, - source.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 start/end/allDay values for the event. - reportEventDrop: function(event, dropLocation, largeUnit, el, ev) { + // `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.mutateEvent(event, dropLocation, largeUnit); + var mutateResult = calendar.mutateSeg(seg, dropLocation, largeUnit); var undoFunc = function() { mutateResult.undo(); calendar.reportEventChange(); }; - this.triggerEventDrop(event, mutateResult.dateDelta, undoFunc, el, ev); + 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.trigger('eventDrop', el[0], event, dateDelta, undoFunc, ev, {}); // {} = jqui dummy + this.publiclyTrigger('eventDrop', el[0], event, dateDelta, undoFunc, ev, {}); // {} = jqui dummy }, @@ -7252,7 +10118,7 @@ var View = fc.View = Class.extend({ // 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 start/end/allDay values for the 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; @@ -7272,10 +10138,10 @@ var View = fc.View = Class.extend({ 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); + this.publiclyTrigger('drop', el[0], dropLocation.start, ev, ui); if (event) { - this.trigger('eventReceive', null, event); // signal an external event landed + this.publiclyTrigger('eventReceive', null, event); // signal an external event landed } }, @@ -7285,7 +10151,8 @@ var View = fc.View = Class.extend({ // Renders a visual indication of a event or external-element drag over the given drop zone. - // If an external-element, seg will be `null` + // If an external-element, seg will be `null`. + // Must return elements used for any mock events. renderDrag: function(dropLocation, seg) { // subclasses must implement }, @@ -7329,54 +10196,60 @@ var View = fc.View = Class.extend({ // Must be called when an event in the view has been resized to a new length - reportEventResize: function(event, resizeLocation, largeUnit, el, ev) { + reportSegResize: function(seg, resizeLocation, largeUnit, el, ev) { var calendar = this.calendar; - var mutateResult = calendar.mutateEvent(event, resizeLocation, largeUnit); + var mutateResult = calendar.mutateSeg(seg, resizeLocation, largeUnit); var undoFunc = function() { mutateResult.undo(); calendar.reportEventChange(); }; - this.triggerEventResize(event, mutateResult.durationDelta, undoFunc, el, ev); + 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.trigger('eventResize', el[0], event, durationDelta, undoFunc, ev, {}); // {} = jqui dummy + this.publiclyTrigger('eventResize', el[0], event, durationDelta, undoFunc, ev, {}); // {} = jqui dummy }, - /* Selection + /* Selection (time range) ------------------------------------------------------------------------------------------------------------------*/ - // Selects a date range on the view. `start` and `end` are both Moments. + // 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(range, ev) { + select: function(span, ev) { this.unselect(ev); - this.renderSelection(range); - this.reportSelection(range, ev); + this.renderSelection(span); + this.reportSelection(span, ev); }, // Renders a visual indication of the selection - renderSelection: function(range) { + renderSelection: function(span) { // subclasses should implement }, // Called when a new selection is made. Updates internal state and triggers handlers. - reportSelection: function(range, ev) { + reportSelection: function(span, ev) { this.isSelected = true; - this.triggerSelect(range, ev); + this.triggerSelect(span, ev); }, // Triggers handlers to 'select' - triggerSelect: function(range, ev) { - this.trigger('select', null, range.start, range.end, ev); + 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 + ); }, @@ -7389,7 +10262,7 @@ var View = fc.View = Class.extend({ this.destroySelection(); // TODO: deprecate } this.unrenderSelection(); - this.trigger('unselect', null, ev); + this.publiclyTrigger('unselect', null, ev); } }, @@ -7400,13 +10273,62 @@ var View = fc.View = Class.extend({ }, - // Handler for unselecting when the user clicks something and the 'unselectAuto' setting is on - documentMousedown: function(ev) { + /* 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 selection, and has the user made a proper left click? - if (this.isSelected && this.opt('unselectAuto') && isPrimaryMouseButton(ev)) { - + // 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) { @@ -7416,13 +10338,28 @@ var View = fc.View = Class.extend({ }, + processEventUnselect: function(ev) { + if (this.selectedEvent) { + if (!$(ev.target).closest('.fc-selected').length) { + this.unselectEvent(); + } + } + }, + + /* Day Click ------------------------------------------------------------------------------------------------------------------*/ // Triggers handlers to 'dayClick' - triggerDayClick: function(cell, dayEl, ev) { - this.trigger('dayClick', dayEl, cell.start, ev); + // 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 + ); }, @@ -7430,59 +10367,6 @@ var View = fc.View = Class.extend({ ------------------------------------------------------------------------------------------------------------------*/ - // 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) { @@ -7522,1232 +10406,610 @@ var View = fc.View = Class.extend({ }); + +View.watch('displayingDates', [ 'dateProfile' ], function(deps) { + this.requestDateRender(deps.dateProfile); +}, function() { + this.requestDateUnrender(); +}); + + +View.watch('initialEvents', [ 'dateProfile' ], function(deps) { + return this.fetchInitialEvents(deps.dateProfile); +}); + + +View.watch('bindingEvents', [ 'initialEvents' ], function(deps) { + this.setEvents(deps.initialEvents); + this.bindEventChanges(); +}, function() { + this.unbindEventChanges(); + this.unsetEvents(); +}); + + +View.watch('displayingEvents', [ 'displayingDates', 'hasEvents' ], function() { + this.requestEventsRender(this.get('currentEvents')); // if there were event mutations after initialEvents +}, function() { + this.requestEventsUnrender(); +}); + ;; -var Calendar = fc.Calendar = Class.extend({ +View.mixin({ - dirDefaults: null, // option defaults related to LTR or RTL - langDefaults: null, // option defaults related to current locale - overrides: null, // option overrides given to the fullCalendar constructor - options: null, // all defaults combined with overrides - viewSpecCache: null, // cache of view definitions - view: null, // current View object - header: null, - loadingLevel: 0, // number of simultaneous loading tasks + // range the view is formally responsible for. + // for example, a month view might have 1st-31st, excluding padded dates + currentRange: null, + currentRangeUnit: null, // name of largest unit being displayed, like "month" or "week" + + // date range with a rendered skeleton + // includes not-active days that need some sort of DOM + renderRange: null, + + // dates that display events and accept drag-n-drop + activeRange: null, + + // constraint for where prev/next operations can go and where events can be dragged/resized to. + // an object with optional start and end properties. + validRange: null, + + // how far the current date will move for a prev/next operation + dateIncrement: null, + + minTime: null, // Duration object that denotes the first visible time of any given day + maxTime: null, // Duration object that denotes the exclusive visible end time of any given day + usesMinMaxTime: false, // whether minTime/maxTime will affect the activeRange. Views must opt-in. + + // DEPRECATED + start: null, // use activeRange.start + end: null, // use activeRange.end + intervalStart: null, // use currentRange.start + intervalEnd: null, // use currentRange.end - // a lot of this class' OOP logic is scoped within this constructor function, - // but in the future, write individual methods on the prototype. - constructor: Calendar_constructor, + /* Date Range Computation + ------------------------------------------------------------------------------------------------------------------*/ - // Subclasses can override this for initialization logic after the constructor has been called - initialize: function() { + setDateProfileForRendering: function(dateProfile) { + this.currentRange = dateProfile.currentRange; + this.currentRangeUnit = dateProfile.currentRangeUnit; + this.renderRange = dateProfile.renderRange; + this.activeRange = dateProfile.activeRange; + this.validRange = dateProfile.validRange; + this.dateIncrement = dateProfile.dateIncrement; + this.minTime = dateProfile.minTime; + this.maxTime = dateProfile.maxTime; + + // DEPRECATED, but we need to keep it updated + this.start = dateProfile.activeRange.start; + this.end = dateProfile.activeRange.end; + this.intervalStart = dateProfile.currentRange.start; + this.intervalEnd = dateProfile.currentRange.end; }, - // Initializes `this.options` and other important options-related objects - initOptions: function(overrides) { - var lang, langDefaults; - var isRTL, dirDefaults; + // Builds a structure with info about what the dates/ranges will be for the "prev" view. + buildPrevDateProfile: function(date) { + var prevDate = date.clone().startOf(this.currentRangeUnit).subtract(this.dateIncrement); - // converts legacy options into non-legacy ones. - // in the future, when this is removed, don't use `overrides` reference. make a copy. - overrides = massageOverrides(overrides); + return this.buildDateProfile(prevDate, -1); + }, - lang = overrides.lang; - langDefaults = langOptionHash[lang]; - if (!langDefaults) { - lang = Calendar.defaults.lang; - langDefaults = langOptionHash[lang] || {}; + + // Builds a structure with info about what the dates/ranges will be for the "next" view. + buildNextDateProfile: function(date) { + var nextDate = date.clone().startOf(this.currentRangeUnit).add(this.dateIncrement); + + return this.buildDateProfile(nextDate, 1); + }, + + + // Builds a structure holding dates/ranges for rendering around the given date. + // Optional direction param indicates whether the date is being incremented/decremented + // from its previous value. decremented = -1, incremented = 1 (default). + buildDateProfile: function(date, direction, forceToValid) { + var validRange = this.buildValidRange(); + var minTime = null; + var maxTime = null; + var currentInfo; + var renderRange; + var activeRange; + var isValid; + + if (forceToValid) { + date = constrainDate(date, validRange); } - isRTL = firstDefined( - overrides.isRTL, - langDefaults.isRTL, - Calendar.defaults.isRTL - ); - dirDefaults = isRTL ? Calendar.rtlDefaults : {}; + currentInfo = this.buildCurrentRangeInfo(date, direction); + renderRange = this.buildRenderRange(currentInfo.range, currentInfo.unit); + activeRange = cloneRange(renderRange); - this.dirDefaults = dirDefaults; - this.langDefaults = langDefaults; - this.overrides = overrides; - this.options = mergeOptions([ // merge defaults and overrides. lowest to highest precedence - Calendar.defaults, // global defaults - dirDefaults, - langDefaults, - overrides - ]); - populateInstanceComputableOptions(this.options); - - this.viewSpecCache = {}; // somewhat unrelated - }, - - - // Gets information about how to create a view. Will use a cache. - getViewSpec: function(viewType) { - var cache = this.viewSpecCache; - - return cache[viewType] || (cache[viewType] = this.buildViewSpec(viewType)); - }, - - - // Given a duration singular unit, like "week" or "day", finds a matching view spec. - // Preference is given to views that have corresponding buttons. - getUnitViewSpec: function(unit) { - var viewTypes; - var i; - var spec; - - if ($.inArray(unit, intervalUnits) != -1) { - - // put views that have buttons first. there will be duplicates, but oh well - viewTypes = this.header.getViewsWithButtons(); - $.each(fc.views, function(viewType) { // all views - viewTypes.push(viewType); - }); - - for (i = 0; i < viewTypes.length; i++) { - spec = this.getViewSpec(viewTypes[i]); - if (spec) { - if (spec.singleUnit == unit) { - return spec; - } - } - } - } - }, - - - // Builds an object with information on how to create a given view - buildViewSpec: function(requestedViewType) { - var viewOverrides = this.overrides.views || {}; - var specChain = []; // for the view. lowest to highest priority - var defaultsChain = []; // for the view. lowest to highest priority - var overridesChain = []; // for the view. lowest to highest priority - var viewType = requestedViewType; - var spec; // for the view - var overrides; // for the view - var duration; - var unit; - - // iterate from the specific view definition to a more general one until we hit an actual View class - while (viewType) { - spec = fcViews[viewType]; - overrides = viewOverrides[viewType]; - viewType = null; // clear. might repopulate for another iteration - - if (typeof spec === 'function') { // TODO: deprecate - spec = { 'class': spec }; - } - - if (spec) { - specChain.unshift(spec); - defaultsChain.unshift(spec.defaults || {}); - duration = duration || spec.duration; - viewType = viewType || spec.type; - } - - if (overrides) { - overridesChain.unshift(overrides); // view-specific option hashes have options at zero-level - duration = duration || overrides.duration; - viewType = viewType || overrides.type; - } + if (!this.opt('showNonCurrentDates')) { + activeRange = constrainRange(activeRange, currentInfo.range); } - spec = mergeProps(specChain); - spec.type = requestedViewType; - if (!spec['class']) { - return false; + minTime = moment.duration(this.opt('minTime')); + maxTime = moment.duration(this.opt('maxTime')); + this.adjustActiveRange(activeRange, minTime, maxTime); + + activeRange = constrainRange(activeRange, validRange); + date = constrainDate(date, activeRange); + + // it's invalid if the originally requested date is not contained, + // or if the range is completely outside of the valid range. + isValid = doRangesIntersect(currentInfo.range, validRange); + + return { + validRange: validRange, + currentRange: currentInfo.range, + currentRangeUnit: currentInfo.unit, + activeRange: activeRange, + renderRange: renderRange, + minTime: minTime, + maxTime: maxTime, + isValid: isValid, + date: date, + dateIncrement: this.buildDateIncrement(currentInfo.duration) + // pass a fallback (might be null) ^ + }; + }, + + + // Builds an object with optional start/end properties. + // Indicates the minimum/maximum dates to display. + buildValidRange: function() { + return this.getRangeOption('validRange', this.calendar.getNow()) || {}; + }, + + + // Builds a structure with info about the "current" range, the range that is + // highlighted as being the current month for example. + // See buildDateProfile for a description of `direction`. + // Guaranteed to have `range` and `unit` properties. `duration` is optional. + buildCurrentRangeInfo: function(date, direction) { + var duration = null; + var unit = null; + var range = null; + var dayCount; + + if (this.viewSpec.duration) { + duration = this.viewSpec.duration; + unit = this.viewSpec.durationUnit; + range = this.buildRangeFromDuration(date, direction, duration, unit); } - - if (duration) { - duration = moment.duration(duration); - if (duration.valueOf()) { // valid? - spec.duration = duration; - unit = computeIntervalUnit(duration); - - // view is a single-unit duration, like "week" or "day" - // incorporate options for this. lowest priority - if (duration.as(unit) === 1) { - spec.singleUnit = unit; - overridesChain.unshift(viewOverrides[unit] || {}); - } - } + else if ((dayCount = this.opt('dayCount'))) { + unit = 'day'; + range = this.buildRangeFromDayCount(date, direction, dayCount); } - - spec.defaults = mergeOptions(defaultsChain); - spec.overrides = mergeOptions(overridesChain); - - this.buildViewSpecOptions(spec); - this.buildViewSpecButtonText(spec, requestedViewType); - - return spec; - }, - - - // Builds and assigns a view spec's options object from its already-assigned defaults and overrides - buildViewSpecOptions: function(spec) { - spec.options = mergeOptions([ // lowest to highest priority - Calendar.defaults, // global defaults - spec.defaults, // view's defaults (from ViewSubclass.defaults) - this.dirDefaults, - this.langDefaults, // locale and dir take precedence over view's defaults! - this.overrides, // calendar's overrides (options given to constructor) - spec.overrides // view's overrides (view-specific options) - ]); - populateInstanceComputableOptions(spec.options); - }, - - - // Computes and assigns a view spec's buttonText-related options - buildViewSpecButtonText: function(spec, requestedViewType) { - - // given an options object with a possible `buttonText` hash, lookup the buttonText for the - // requested view, falling back to a generic unit entry like "week" or "day" - function queryButtonText(options) { - var buttonText = options.buttonText || {}; - return buttonText[requestedViewType] || - (spec.singleUnit ? buttonText[spec.singleUnit] : null); - } - - // highest to lowest priority - spec.buttonTextOverride = - queryButtonText(this.overrides) || // constructor-specified buttonText lookup hash takes precedence - spec.overrides.buttonText; // `buttonText` for view-specific options is a string - - // highest to lowest priority. mirrors buildViewSpecOptions - spec.buttonTextDefault = - queryButtonText(this.langDefaults) || - queryButtonText(this.dirDefaults) || - spec.defaults.buttonText || // a single string. from ViewSubclass.defaults - queryButtonText(Calendar.defaults) || - (spec.duration ? this.humanizeDuration(spec.duration) : null) || // like "3 days" - requestedViewType; // fall back to given view name - }, - - - // Given a view name for a custom view or a standard view, creates a ready-to-go View object - instantiateView: function(viewType) { - var spec = this.getViewSpec(viewType); - - return new spec['class'](this, viewType, spec.options, spec.duration); - }, - - - // Returns a boolean about whether the view is okay to instantiate at some point - isValidViewType: function(viewType) { - return Boolean(this.getViewSpec(viewType)); - }, - - - // Should be called when any type of async data fetching begins - pushLoading: function() { - if (!(this.loadingLevel++)) { - this.trigger('loading', null, true, this.view); - } - }, - - - // Should be called when any type of async data fetching completes - popLoading: function() { - if (!(--this.loadingLevel)) { - this.trigger('loading', null, false, this.view); - } - }, - - - // Given arguments to the select method in the API, returns a range - buildSelectRange: function(start, end) { - - start = this.moment(start); - if (end) { - end = this.moment(end); - } - else if (start.hasTime()) { - end = start.clone().add(this.defaultTimedEventDuration); + else if ((range = this.buildCustomVisibleRange(date))) { + unit = computeGreatestUnit(range.start, range.end); } else { - end = start.clone().add(this.defaultAllDayEventDuration); + duration = this.getFallbackDuration(); + unit = computeGreatestUnit(duration); + range = this.buildRangeFromDuration(date, direction, duration, unit); } + this.normalizeCurrentRange(range, unit); // modifies in-place + + return { duration: duration, unit: unit, range: range }; + }, + + + getFallbackDuration: function() { + return moment.duration({ days: 1 }); + }, + + + // If the range has day units or larger, remove times. Otherwise, ensure times. + normalizeCurrentRange: function(range, unit) { + + if (/^(year|month|week|day)$/.test(unit)) { // whole-days? + range.start.stripTime(); + range.end.stripTime(); + } + else { // needs to have a time? + if (!range.start.hasTime()) { + range.start.time(0); // give 00:00 time + } + if (!range.end.hasTime()) { + range.end.time(0); // give 00:00 time + } + } + }, + + + // Mutates the given activeRange to have time values (un-ambiguate) + // if the minTime or maxTime causes the range to expand. + // TODO: eventually activeRange should *always* have times. + adjustActiveRange: function(range, minTime, maxTime) { + var hasSpecialTimes = false; + + if (this.usesMinMaxTime) { + + if (minTime < 0) { + range.start.time(0).add(minTime); + hasSpecialTimes = true; + } + + if (maxTime > 24 * 60 * 60 * 1000) { // beyond 24 hours? + range.end.time(maxTime - (24 * 60 * 60 * 1000)); + hasSpecialTimes = true; + } + + if (hasSpecialTimes) { + if (!range.start.hasTime()) { + range.start.time(0); + } + if (!range.end.hasTime()) { + range.end.time(0); + } + } + } + }, + + + // Builds the "current" range when it is specified as an explicit duration. + // `unit` is the already-computed computeGreatestUnit value of duration. + buildRangeFromDuration: function(date, direction, duration, unit) { + var alignment = this.opt('dateAlignment'); + var start = date.clone(); + var end; + var dateIncrementInput; + var dateIncrementDuration; + + // if the view displays a single day or smaller + if (duration.as('days') <= 1) { + if (this.isHiddenDay(start)) { + start = this.skipHiddenDays(start, direction); + start.startOf('day'); + } + } + + // compute what the alignment should be + if (!alignment) { + dateIncrementInput = this.opt('dateIncrement'); + + if (dateIncrementInput) { + dateIncrementDuration = moment.duration(dateIncrementInput); + + // use the smaller of the two units + if (dateIncrementDuration < duration) { + alignment = computeDurationGreatestUnit(dateIncrementDuration, dateIncrementInput); + } + else { + alignment = unit; + } + } + else { + alignment = unit; + } + } + + start.startOf(alignment); + end = start.clone().add(duration); + return { start: start, end: end }; + }, + + + // Builds the "current" range when a dayCount is specified. + buildRangeFromDayCount: function(date, direction, dayCount) { + var customAlignment = this.opt('dateAlignment'); + var runningCount = 0; + var start = date.clone(); + var end; + + if (customAlignment) { + start.startOf(customAlignment); + } + + start.startOf('day'); + start = this.skipHiddenDays(start, direction); + + end = start.clone(); + do { + end.add(1, 'day'); + if (!this.isHiddenDay(end)) { + runningCount++; + } + } while (runningCount < dayCount); + + return { start: start, end: end }; + }, + + + // Builds a normalized range object for the "visible" range, + // which is a way to define the currentRange and activeRange at the same time. + buildCustomVisibleRange: function(date) { + var visibleRange = this.getRangeOption( + 'visibleRange', + this.calendar.moment(date) // correct zone. also generates new obj that avoids mutations + ); + + if (visibleRange && (!visibleRange.start || !visibleRange.end)) { + return null; + } + + return visibleRange; + }, + + + // Computes the range that will represent the element/cells for *rendering*, + // but which may have voided days/times. + buildRenderRange: function(currentRange, currentRangeUnit) { + // cut off days in the currentRange that are hidden + return this.trimHiddenDays(currentRange); + }, + + + // Compute the duration value that should be added/substracted to the current date + // when a prev/next operation happens. + buildDateIncrement: function(fallback) { + var dateIncrementInput = this.opt('dateIncrement'); + var customAlignment; + + if (dateIncrementInput) { + return moment.duration(dateIncrementInput); + } + else if ((customAlignment = this.opt('dateAlignment'))) { + return moment.duration(1, customAlignment); + } + else if (fallback) { + return fallback; + } + else { + return moment.duration({ days: 1 }); + } + }, + + + // Remove days from the beginning and end of the range that are computed as hidden. + trimHiddenDays: function(inputRange) { + return { + start: this.skipHiddenDays(inputRange.start), + end: this.skipHiddenDays(inputRange.end, -1, true) // exclusively move backwards + }; + }, + + + // Compute the number of the give units in the "current" range. + // Will return a floating-point number. Won't round. + currentRangeAs: function(unit) { + var currentRange = this.currentRange; + return currentRange.end.diff(currentRange.start, unit, true); + }, + + + // Arguments after name will be forwarded to a hypothetical function value + // WARNING: passed-in arguments will be given to generator functions as-is and can cause side-effects. + // Always clone your objects if you fear mutation. + getRangeOption: function(name) { + var val = this.opt(name); + + if (typeof val === 'function') { + val = val.apply( + null, + Array.prototype.slice.call(arguments, 1) + ); + } + + if (val) { + return this.calendar.parseRange(val); + } + }, + + + /* Hidden Days + ------------------------------------------------------------------------------------------------------------------*/ + + + // 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. + // DOES NOT CONSIDER validRange! + // 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; } }); +;; -function Calendar_constructor(element, overrides) { - var t = this; +/* +Embodies a div that has potential scrollbars +*/ +var Scroller = FC.Scroller = Class.extend({ + el: null, // the guaranteed outer element + scrollEl: null, // the element with the scrollbars + overflowX: null, + overflowY: null, - t.initOptions(overrides || {}); - var options = this.options; - - // Exports - // ----------------------------------------------------------------------------------- + constructor: function(options) { + options = options || {}; + this.overflowX = options.overflowX || options.overflow || 'auto'; + this.overflowY = options.overflowY || options.overflow || 'auto'; + }, - t.render = render; - t.destroy = destroy; - t.refetchEvents = refetchEvents; - t.reportEvents = reportEvents; - t.reportEventChange = reportEventChange; - t.rerenderEvents = renderEvents; // `renderEvents` serves as a rerender. an API method - t.changeView = renderView; // `renderView` will switch to another view - t.select = select; - t.unselect = unselect; - t.prev = prev; - t.next = next; - t.prevYear = prevYear; - t.nextYear = nextYear; - t.today = today; - t.gotoDate = gotoDate; - t.incrementDate = incrementDate; - t.zoomTo = zoomTo; - t.getDate = getDate; - t.getCalendar = getCalendar; - t.getView = getView; - t.option = option; - t.trigger = trigger; + render: function() { + this.el = this.renderEl(); + this.applyOverflow(); + }, - // Language-data Internals - // ----------------------------------------------------------------------------------- - // Apply overrides to the current language's data + renderEl: function() { + return (this.scrollEl = $('
')); + }, - var localeData = createObject( // make a cheap copy - getMomentLocaleData(options.lang) // will fall back to en - ); + // sets to natural height, unlocks overflow + clear: function() { + this.setHeight('auto'); + this.applyOverflow(); + }, - if (options.monthNames) { - localeData._months = options.monthNames; - } - if (options.monthNamesShort) { - localeData._monthsShort = options.monthNamesShort; - } - if (options.dayNames) { - localeData._weekdays = options.dayNames; - } - if (options.dayNamesShort) { - localeData._weekdaysShort = options.dayNamesShort; - } - if (options.firstDay != null) { - var _week = createObject(localeData._week); // _week: { dow: # } - _week.dow = options.firstDay; - localeData._week = _week; - } - // assign a normalized value, to be used by our .week() moment extension - localeData._fullCalendar_weekCalc = (function(weekCalc) { - if (typeof weekCalc === 'function') { - return weekCalc; - } - else if (weekCalc === 'local') { - return weekCalc; - } - else if (weekCalc === 'iso' || weekCalc === 'ISO') { - return 'ISO'; - } - })(options.weekNumberCalculation); + destroy: function() { + this.el.remove(); + }, + // Overflow + // ----------------------------------------------------------------------------------------------------------------- - // Calendar-specific Date Utilities - // ----------------------------------------------------------------------------------- - - t.defaultAllDayEventDuration = moment.duration(options.defaultAllDayEventDuration); - t.defaultTimedEventDuration = moment.duration(options.defaultTimedEventDuration); - - - // Builds a moment using the settings of the current calendar: timezone and language. - // Accepts anything the vanilla moment() constructor accepts. - t.moment = function() { - var mom; - - if (options.timezone === 'local') { - mom = fc.moment.apply(null, arguments); - - // Force the moment to be local, because fc.moment doesn't guarantee it. - if (mom.hasTime()) { // don't give ambiguously-timed moments a local zone - mom.local(); - } - } - else if (options.timezone === 'UTC') { - mom = fc.moment.utc.apply(null, arguments); // process as UTC - } - else { - mom = fc.moment.parseZone.apply(null, arguments); // let the input decide the zone - } - - if ('_locale' in mom) { // moment 2.8 and above - mom._locale = localeData; - } - else { // pre-moment-2.8 - mom._lang = localeData; - } - - return mom; - }; - - - // Returns a boolean about whether or not the calendar knows how to calculate - // the timezone offset of arbitrary dates in the current timezone. - t.getIsAmbigTimezone = function() { - return options.timezone !== 'local' && options.timezone !== 'UTC'; - }; - - - // Returns a copy of the given date in the current timezone of it is ambiguously zoned. - // This will also give the date an unambiguous time. - t.rezoneDate = function(date) { - return t.moment(date.toArray()); - }; - - - // Returns a moment for the current date, as defined by the client's computer, - // or overridden by the `now` option. - t.getNow = function() { - var now = options.now; - if (typeof now === 'function') { - now = now(); - } - return t.moment(now); - }; - - - // Get an event's normalized end date. If not present, calculate it from the defaults. - t.getEventEnd = function(event) { - if (event.end) { - return event.end.clone(); - } - else { - return t.getDefaultEventEnd(event.allDay, event.start); - } - }; - - - // Given an event's allDay status and start date, return swhat its fallback end date should be. - t.getDefaultEventEnd = function(allDay, start) { // TODO: rename to computeDefaultEventEnd - var end = start.clone(); - - if (allDay) { - end.stripTime().add(t.defaultAllDayEventDuration); - } - else { - end.add(t.defaultTimedEventDuration); - } - - if (t.getIsAmbigTimezone()) { - end.stripZone(); // we don't know what the tzo should be - } - - return end; - }; - - - // Produces a human-readable string for the given duration. - // Side-effect: changes the locale of the given duration. - t.humanizeDuration = function(duration) { - return (duration.locale || duration.lang).call(duration, options.lang) // works moment-pre-2.8 - .humanize(); - }; - - - - // Imports - // ----------------------------------------------------------------------------------- - - - EventManager.call(t, options); - var isFetchNeeded = t.isFetchNeeded; - var fetchEvents = t.fetchEvents; - - - - // Locals - // ----------------------------------------------------------------------------------- - - - var _element = element[0]; - var header; - var headerElement; - var content; - var tm; // for making theme classes - var currentView; // NOTE: keep this in sync with this.view - var viewsByType = {}; // holds all instantiated view instances, current or not - var suggestedViewHeight; - var windowResizeProxy; // wraps the windowResize function - var ignoreWindowResize = 0; - var date; - var events = []; - - - - // Main Rendering - // ----------------------------------------------------------------------------------- - - - if (options.defaultDate != null) { - date = t.moment(options.defaultDate); - } - else { - date = t.getNow(); - } - - - function render() { - if (!content) { - initialRender(); - } - else if (elementVisible()) { - // mainly for the public API - calcSize(); - renderView(); - } - } - - - function initialRender() { - tm = options.theme ? 'ui' : 'fc'; - element.addClass('fc'); - - if (options.isRTL) { - element.addClass('fc-rtl'); - } - else { - element.addClass('fc-ltr'); - } - - if (options.theme) { - element.addClass('ui-widget'); - } - else { - element.addClass('fc-unthemed'); - } - - content = $("
").prependTo(element); - - header = t.header = new Header(t, options); - headerElement = header.render(); - if (headerElement) { - element.prepend(headerElement); - } - - renderView(options.defaultView); - - if (options.handleWindowResize) { - windowResizeProxy = debounce(windowResize, options.windowResizeDelay); // prevents rapid calls - $(window).resize(windowResizeProxy); - } - } - - - function destroy() { - - if (currentView) { - currentView.removeElement(); - - // NOTE: don't null-out currentView/t.view in case API methods are called after destroy. - // It is still the "current" view, just not rendered. - } - - header.removeElement(); - content.remove(); - element.removeClass('fc fc-ltr fc-rtl fc-unthemed ui-widget'); - - if (windowResizeProxy) { - $(window).unbind('resize', windowResizeProxy); - } - } - - - function elementVisible() { - return element.is(':visible'); - } - - - - // View Rendering - // ----------------------------------------------------------------------------------- - - - // Renders a view because of a date change, view-type change, or for the first time. - // If not given a viewType, keep the current view but render different dates. - function renderView(viewType) { - ignoreWindowResize++; - - // if viewType is changing, remove the old view's rendering - if (currentView && viewType && currentView.type !== viewType) { - header.deactivateButton(currentView.type); - freezeContentHeight(); // prevent a scroll jump when view element is removed - currentView.removeElement(); - currentView = t.view = null; - } - - // if viewType changed, or the view was never created, create a fresh view - if (!currentView && viewType) { - currentView = t.view = - viewsByType[viewType] || - (viewsByType[viewType] = t.instantiateView(viewType)); - - currentView.setElement( - $("
").appendTo(content) - ); - header.activateButton(viewType); - } - - if (currentView) { - - // in case the view should render a period of time that is completely hidden - date = currentView.massageCurrentDate(date); - - // render or rerender the view - if ( - !currentView.displaying || - !date.isWithin(currentView.intervalStart, currentView.intervalEnd) // implicit date window change - ) { - if (elementVisible()) { - - freezeContentHeight(); - currentView.display(date); - unfreezeContentHeight(); // immediately unfreeze regardless of whether display is async - - // need to do this after View::render, so dates are calculated - updateHeaderTitle(); - updateTodayButton(); - - getAndRenderEvents(); - } - } - } - - unfreezeContentHeight(); // undo any lone freezeContentHeight calls - ignoreWindowResize--; - } - - - - // Resizing - // ----------------------------------------------------------------------------------- - - - t.getSuggestedViewHeight = function() { - if (suggestedViewHeight === undefined) { - calcSize(); - } - return suggestedViewHeight; - }; - - - t.isHeightAuto = function() { - return options.contentHeight === 'auto' || options.height === 'auto'; - }; - - - function updateSize(shouldRecalc) { - if (elementVisible()) { - - if (shouldRecalc) { - _calcSize(); - } - - ignoreWindowResize++; - currentView.updateSize(true); // isResize=true. will poll getSuggestedViewHeight() and isHeightAuto() - ignoreWindowResize--; - - return true; // signal success - } - } - - - function calcSize() { - if (elementVisible()) { - _calcSize(); - } - } - - - function _calcSize() { // assumes elementVisible - if (typeof options.contentHeight === 'number') { // exists and not 'auto' - suggestedViewHeight = options.contentHeight; - } - else if (typeof options.height === 'number') { // exists and not 'auto' - suggestedViewHeight = options.height - (headerElement ? headerElement.outerHeight(true) : 0); - } - else { - suggestedViewHeight = Math.round(content.width() / Math.max(options.aspectRatio, .5)); - } - } - - - function windowResize(ev) { - if ( - !ignoreWindowResize && - ev.target === window && // so we don't process jqui "resize" events that have bubbled up - currentView.start // view has already been rendered - ) { - if (updateSize(true)) { - currentView.trigger('windowResize', _element); - } - } - } - - - - /* Event Fetching/Rendering - -----------------------------------------------------------------------------*/ - // TODO: going forward, most of this stuff should be directly handled by the view - - - function refetchEvents() { // can be called as an API method - destroyEvents(); // so that events are cleared before user starts waiting for AJAX - fetchAndRenderEvents(); - } - - - function renderEvents() { // destroys old events if previously rendered - if (elementVisible()) { - freezeContentHeight(); - currentView.displayEvents(events); - unfreezeContentHeight(); - } - } - - - function destroyEvents() { - freezeContentHeight(); - currentView.clearEvents(); - unfreezeContentHeight(); - } - - - function getAndRenderEvents() { - if (!options.lazyFetching || isFetchNeeded(currentView.start, currentView.end)) { - fetchAndRenderEvents(); - } - else { - renderEvents(); - } - } - - - function fetchAndRenderEvents() { - fetchEvents(currentView.start, currentView.end); - // ... will call reportEvents - // ... which will call renderEvents - } - - - // called when event data arrives - function reportEvents(_events) { - events = _events; - renderEvents(); - } - - - // called when a single event's data has been changed - function reportEventChange() { - renderEvents(); - } - - - - /* Header Updating - -----------------------------------------------------------------------------*/ - - - function updateHeaderTitle() { - header.updateTitle(currentView.title); - } - - - function updateTodayButton() { - var now = t.getNow(); - if (now.isWithin(currentView.intervalStart, currentView.intervalEnd)) { - header.disableButton('today'); - } - else { - header.enableButton('today'); - } - } - - - - /* Selection - -----------------------------------------------------------------------------*/ - - - function select(start, end) { - currentView.select( - t.buildSelectRange.apply(t, arguments) - ); - } - - - function unselect() { // safe to be called before renderView - if (currentView) { - currentView.unselect(); - } - } - - - - /* Date - -----------------------------------------------------------------------------*/ - - - function prev() { - date = currentView.computePrevDate(date); - renderView(); - } - - - function next() { - date = currentView.computeNextDate(date); - renderView(); - } - - - function prevYear() { - date.add(-1, 'years'); - renderView(); - } - - - function nextYear() { - date.add(1, 'years'); - renderView(); - } - - - function today() { - date = t.getNow(); - renderView(); - } - - - function gotoDate(dateInput) { - date = t.moment(dateInput); - renderView(); - } - - - function incrementDate(delta) { - date.add(moment.duration(delta)); - renderView(); - } - - - // Forces navigation to a view for the given date. - // `viewType` can be a specific view name or a generic one like "week" or "day". - function zoomTo(newDate, viewType) { - var spec; - - viewType = viewType || 'day'; // day is default zoom - spec = t.getViewSpec(viewType) || t.getUnitViewSpec(viewType); - - date = newDate; - renderView(spec ? spec.type : null); - } - - - function getDate() { - return date.clone(); - } - - - - /* Height "Freezing" - -----------------------------------------------------------------------------*/ - // TODO: move this into the view - - - function freezeContentHeight() { - content.css({ - width: '100%', - height: content.height(), - overflow: 'hidden' + applyOverflow: function() { + this.scrollEl.css({ + 'overflow-x': this.overflowX, + 'overflow-y': this.overflowY }); - } + }, - function unfreezeContentHeight() { - content.css({ - width: '', - height: '', - overflow: '' - }); - } - - - - /* Misc - -----------------------------------------------------------------------------*/ - + // Causes any 'auto' overflow values to resolves to 'scroll' or 'hidden'. + // Useful for preserving scrollbar widths regardless of future resizes. + // Can pass in scrollbarWidths for optimization. + lockOverflow: function(scrollbarWidths) { + var overflowX = this.overflowX; + var overflowY = this.overflowY; - function getCalendar() { - return t; - } + scrollbarWidths = scrollbarWidths || this.getScrollbarWidths(); - - function getView() { - return currentView; - } - - - function option(name, value) { - if (value === undefined) { - return options[name]; + if (overflowX === 'auto') { + overflowX = ( + scrollbarWidths.top || scrollbarWidths.bottom || // horizontal scrollbars? + // OR scrolling pane with massless scrollbars? + this.scrollEl[0].scrollWidth - 1 > this.scrollEl[0].clientWidth + // subtract 1 because of IE off-by-one issue + ) ? 'scroll' : 'hidden'; } - if (name == 'height' || name == 'contentHeight' || name == 'aspectRatio') { - options[name] = value; - updateSize(true); // true = allow recalculation of height - } - } - - - function trigger(name, thisObj) { - if (options[name]) { - return options[name].apply( - thisObj || _element, - Array.prototype.slice.call(arguments, 2) - ); + + if (overflowY === 'auto') { + overflowY = ( + scrollbarWidths.left || scrollbarWidths.right || // vertical scrollbars? + // OR scrolling pane with massless scrollbars? + this.scrollEl[0].scrollHeight - 1 > this.scrollEl[0].clientHeight + // subtract 1 because of IE off-by-one issue + ) ? 'scroll' : 'hidden'; } + + this.scrollEl.css({ 'overflow-x': overflowX, 'overflow-y': overflowY }); + }, + + + // Getters / Setters + // ----------------------------------------------------------------------------------------------------------------- + + + setHeight: function(height) { + this.scrollEl.height(height); + }, + + + getScrollTop: function() { + return this.scrollEl.scrollTop(); + }, + + + setScrollTop: function(top) { + this.scrollEl.scrollTop(top); + }, + + + getClientWidth: function() { + return this.scrollEl[0].clientWidth; + }, + + + getClientHeight: function() { + return this.scrollEl[0].clientHeight; + }, + + + getScrollbarWidths: function() { + return getScrollbarWidths(this.scrollEl); } - t.initialize(); -} +}); ;; - -Calendar.defaults = { - - titleRangeSeparator: ' \u2014 ', // emphasized dash - monthYearFormat: 'MMMM YYYY', // required for en. other languages rely on datepicker computable option - - defaultTimedEventDuration: '02:00:00', - defaultAllDayEventDuration: { days: 1 }, - forceEventDuration: false, - nextDayThreshold: '09:00:00', // 9am - - // display - defaultView: 'month', - aspectRatio: 1.35, - header: { - left: 'title', - center: '', - right: 'today prev,next' - }, - weekends: true, - weekNumbers: false, - - weekNumberTitle: 'W', - weekNumberCalculation: 'local', - - //editable: false, - - scrollTime: '06:00:00', - - // event ajax - lazyFetching: true, - startParam: 'start', - endParam: 'end', - timezoneParam: 'timezone', - - timezone: false, - - //allDayDefault: undefined, - - // locale - isRTL: false, - buttonText: { - prev: "prev", - next: "next", - prevYear: "prev year", - nextYear: "next year", - year: 'year', // TODO: locale files need to specify this - today: 'today', - month: 'month', - week: 'week', - day: 'day' - }, - - buttonIcons: { - prev: 'left-single-arrow', - next: 'right-single-arrow', - prevYear: 'left-double-arrow', - nextYear: 'right-double-arrow' - }, - - // jquery-ui theming - theme: false, - themeButtonIcons: { - prev: 'circle-triangle-w', - next: 'circle-triangle-e', - prevYear: 'seek-prev', - nextYear: 'seek-next' - }, - - //eventResizableFromStart: false, - dragOpacity: .75, - dragRevertDuration: 500, - dragScroll: true, - - //selectable: false, - unselectAuto: true, - - dropAccept: '*', - - eventLimit: false, - eventLimitText: 'more', - eventLimitClick: 'popover', - dayPopoverFormat: 'LL', - - handleWindowResize: true, - windowResizeDelay: 200 // milliseconds before an updateSize happens - -}; +function Iterator(items) { + this.items = items || []; +} -Calendar.englishDefaults = { // used by lang.js - dayPopoverFormat: 'dddd, MMMM D' -}; +/* Calls a method on every item passing the arguments through */ +Iterator.prototype.proxyCall = function(methodName) { + var args = Array.prototype.slice.call(arguments, 1); + var results = []; + this.items.forEach(function(item) { + results.push(item[methodName].apply(item, args)); + }); -Calendar.rtlDefaults = { // right-to-left defaults - header: { // TODO: smarter solution (first/center/last ?) - left: 'next,prev today', - center: '', - right: 'title' - }, - buttonIcons: { - prev: 'right-single-arrow', - next: 'left-single-arrow', - prevYear: 'right-double-arrow', - nextYear: 'left-double-arrow' - }, - themeButtonIcons: { - prev: 'circle-triangle-e', - next: 'circle-triangle-w', - nextYear: 'seek-prev', - prevYear: 'seek-next' - } + return results; }; ;; -var langOptionHash = fc.langs = {}; // initialize and expose - - -// TODO: document the structure and ordering of a FullCalendar lang file -// TODO: rename everything "lang" to "locale", like what the moment project did - - -// Initialize jQuery UI datepicker translations while using some of the translations -// Will set this as the default language for datepicker. -fc.datepickerLang = function(langCode, dpLangCode, dpOptions) { - - // get the FullCalendar internal option hash for this language. create if necessary - var fcOptions = langOptionHash[langCode] || (langOptionHash[langCode] = {}); - - // transfer some simple options from datepicker to fc - fcOptions.isRTL = dpOptions.isRTL; - fcOptions.weekNumberTitle = dpOptions.weekHeader; - - // compute some more complex options from datepicker - $.each(dpComputableOptions, function(name, func) { - fcOptions[name] = func(dpOptions); - }); - - // is jQuery UI Datepicker is on the page? - if ($.datepicker) { - - // Register the language data. - // FullCalendar and MomentJS use language codes like "pt-br" but Datepicker - // does it like "pt-BR" or if it doesn't have the language, maybe just "pt". - // Make an alias so the language can be referenced either way. - $.datepicker.regional[dpLangCode] = - $.datepicker.regional[langCode] = // alias - dpOptions; - - // Alias 'en' to the default language data. Do this every time. - $.datepicker.regional.en = $.datepicker.regional['']; - - // Set as Datepicker's global defaults. - $.datepicker.setDefaults(dpOptions); - } -}; - - -// Sets FullCalendar-specific translations. Will set the language as the global default. -fc.lang = function(langCode, newFcOptions) { - var fcOptions; - var momOptions; - - // get the FullCalendar internal option hash for this language. create if necessary - fcOptions = langOptionHash[langCode] || (langOptionHash[langCode] = {}); - - // provided new options for this language? merge them in - if (newFcOptions) { - fcOptions = langOptionHash[langCode] = mergeOptions([ fcOptions, newFcOptions ]); - } - - // compute language options that weren't defined. - // always do this. newFcOptions can be undefined when initializing from i18n file, - // so no way to tell if this is an initialization or a default-setting. - momOptions = getMomentLocaleData(langCode); // will fall back to en - $.each(momComputableOptions, function(name, func) { - if (fcOptions[name] == null) { - fcOptions[name] = func(momOptions, fcOptions); - } - }); - - // set it as the default language for FullCalendar - Calendar.defaults.lang = langCode; -}; - - -// NOTE: can't guarantee any of these computations will run because not every language has datepicker -// configs, so make sure there are English fallbacks for these in the defaults file. -var dpComputableOptions = { - - buttonText: function(dpOptions) { - return { - // the translations sometimes wrongly contain HTML entities - prev: stripHtmlEntities(dpOptions.prevText), - next: stripHtmlEntities(dpOptions.nextText), - today: stripHtmlEntities(dpOptions.currentText) - }; - }, - - // Produces format strings like "MMMM YYYY" -> "September 2014" - monthYearFormat: function(dpOptions) { - return dpOptions.showMonthAfterYear ? - 'YYYY[' + dpOptions.yearSuffix + '] MMMM' : - 'MMMM YYYY[' + dpOptions.yearSuffix + ']'; - } - -}; - -var momComputableOptions = { - - // Produces format strings like "ddd M/D" -> "Fri 9/15" - dayOfMonthFormat: function(momOptions, fcOptions) { - var format = momOptions.longDateFormat('l'); // for the format like "M/D/YYYY" - - // strip the year off the edge, as well as other misc non-whitespace chars - format = format.replace(/^Y+[^\w\s]*|[^\w\s]*Y+$/g, ''); - - if (fcOptions.isRTL) { - format += ' ddd'; // for RTL, add day-of-week to end - } - else { - format = 'ddd ' + format; // for LTR, add day-of-week to beginning - } - return format; - }, - - // Produces format strings like "h:mma" -> "6:00pm" - mediumTimeFormat: function(momOptions) { // can't be called `timeFormat` because collides with option - return momOptions.longDateFormat('LT') - .replace(/\s*a$/i, 'a'); // convert AM/PM/am/pm to lowercase. remove any spaces beforehand - }, - - // Produces format strings like "h(:mm)a" -> "6pm" / "6:30pm" - smallTimeFormat: function(momOptions) { - return momOptions.longDateFormat('LT') - .replace(':mm', '(:mm)') - .replace(/(\Wmm)$/, '($1)') // like above, but for foreign langs - .replace(/\s*a$/i, 'a'); // convert AM/PM/am/pm to lowercase. remove any spaces beforehand - }, - - // Produces format strings like "h(:mm)t" -> "6p" / "6:30p" - extraSmallTimeFormat: function(momOptions) { - return momOptions.longDateFormat('LT') - .replace(':mm', '(:mm)') - .replace(/(\Wmm)$/, '($1)') // like above, but for foreign langs - .replace(/\s*a$/i, 't'); // convert to AM/PM/am/pm to lowercase one-letter. remove any spaces beforehand - }, - - // Produces format strings like "ha" / "H" -> "6pm" / "18" - hourFormat: function(momOptions) { - return momOptions.longDateFormat('LT') - .replace(':mm', '') - .replace(/(\Wmm)$/, '') // like above, but for foreign langs - .replace(/\s*a$/i, 'a'); // convert AM/PM/am/pm to lowercase. remove any spaces beforehand - }, - - // Produces format strings like "h:mm" -> "6:30" (with no AM/PM) - noMeridiemTimeFormat: function(momOptions) { - return momOptions.longDateFormat('LT') - .replace(/\s*a$/i, ''); // remove trailing AM/PM - } - -}; - - -// options that should be computed off live calendar options (considers override options) -var instanceComputableOptions = { // TODO: best place for this? related to lang? - - // Produces format strings for results like "Mo 16" - smallDayDateFormat: function(options) { - return options.isRTL ? - 'D dd' : - 'dd D'; - }, - - // Produces format strings for results like "Wk 5" - weekFormat: function(options) { - return options.isRTL ? - 'w[ ' + options.weekNumberTitle + ']' : - '[' + options.weekNumberTitle + ' ]w'; - }, - - // Produces format strings for results like "Wk5" - smallWeekFormat: function(options) { - return options.isRTL ? - 'w[' + options.weekNumberTitle + ']' : - '[' + options.weekNumberTitle + ']w'; - } - -}; - -function populateInstanceComputableOptions(options) { - $.each(instanceComputableOptions, function(name, func) { - if (options[name] == null) { - options[name] = func(options); - } - }); -} - - -// Returns moment's internal locale data. If doesn't exist, returns English. -// Works with moment-pre-2.8 -function getMomentLocaleData(langCode) { - var func = moment.localeData || moment.langData; - return func.call(moment, langCode) || - func.call(moment, 'en'); // the newer localData could return null, so fall back to en -} - - -// Initialize English by forcing computation of moment-derived options. -// Also, sets it as the default. -fc.lang('en', Calendar.englishDefaults); - -;; - -/* Top toolbar area with buttons and title +/* Toolbar with buttons and title ----------------------------------------------------------------------------------------------------------------------*/ -// TODO: rename all header-related things to "toolbar" -function Header(calendar, options) { +function Toolbar(calendar, toolbarOptions) { var t = this; - + // exports + t.setToolbarOptions = setToolbarOptions; t.render = render; t.removeElement = removeElement; t.updateTitle = updateTitle; @@ -8756,39 +11018,55 @@ function Header(calendar, options) { t.disableButton = disableButton; t.enableButton = enableButton; t.getViewsWithButtons = getViewsWithButtons; - + t.el = null; // mirrors local `el` + // locals - var el = $(); + var el; var viewsWithButtons = []; var tm; + // method to update toolbar-specific options, not calendar-wide options + function setToolbarOptions(newToolbarOptions) { + toolbarOptions = newToolbarOptions; + } + // can be called repeatedly and will rerender function render() { - var sections = options.header; + var sections = toolbarOptions.layout; - tm = options.theme ? 'ui' : 'fc'; + tm = calendar.opt('theme') ? 'ui' : 'fc'; if (sections) { - el = $("
") - .append(renderSection('left')) + if (!el) { + el = this.el = $("
"); + } + else { + el.empty(); + } + el.append(renderSection('left')) .append(renderSection('right')) .append(renderSection('center')) .append('
'); - - return el; + } + else { + removeElement(); } } - - + + function removeElement() { - el.remove(); - el = $(); + if (el) { + el.remove(); + el = t.el = null; + } } - - + + function renderSection(position) { var sectionEl = $('
'); - var buttonStr = options.header[position]; + var buttonStr = toolbarOptions.layout[position]; + var calendarCustomButtons = calendar.opt('customButtons') || {}; + var calendarButtonText = calendar.opt('buttonText') || {}; if (buttonStr) { $.each(buttonStr.split(' '), function(i) { @@ -8797,6 +11075,7 @@ function Header(calendar, options) { var groupEl; $.each(this.split(','), function(j, buttonName) { + var customButtonProps; var viewSpec; var buttonClick; var overrideText; // text explicitly set by calendar's constructor options. overcomes icons @@ -8805,16 +11084,23 @@ function Header(calendar, options) { var normalIcon; var innerHtml; var classes; - var button; + var button; // the element if (buttonName == 'title') { groupChildren = groupChildren.add($('

 

')); // we always want it to take up height isOnlyButtons = false; } else { - viewSpec = calendar.getViewSpec(buttonName); - - if (viewSpec) { + if ((customButtonProps = calendarCustomButtons[buttonName])) { + buttonClick = function(ev) { + if (customButtonProps.click) { + customButtonProps.click.call(button[0], ev); + } + }; + overrideText = ''; // icons will override text + defaultText = customButtonProps.text; + } + else if ((viewSpec = calendar.getViewSpec(buttonName))) { buttonClick = function() { calendar.changeView(buttonName); }; @@ -8827,21 +11113,28 @@ function Header(calendar, options) { calendar[buttonName](); }; overrideText = (calendar.overrides.buttonText || {})[buttonName]; - defaultText = options.buttonText[buttonName]; // everything else is considered default + defaultText = calendarButtonText[buttonName]; // everything else is considered default } if (buttonClick) { - themeIcon = options.themeButtonIcons[buttonName]; - normalIcon = options.buttonIcons[buttonName]; + themeIcon = + customButtonProps ? + customButtonProps.themeIcon : + calendar.opt('themeButtonIcons')[buttonName]; + + normalIcon = + customButtonProps ? + customButtonProps.icon : + calendar.opt('buttonIcons')[buttonName]; if (overrideText) { innerHtml = htmlEscape(overrideText); } - else if (themeIcon && options.theme) { + else if (themeIcon && calendar.opt('theme')) { innerHtml = ""; } - else if (normalIcon && !options.theme) { + else if (normalIcon && !calendar.opt('theme')) { innerHtml = ""; } else { @@ -8859,11 +11152,11 @@ function Header(calendar, options) { innerHtml + '' ) - .click(function() { + .click(function(ev) { // don't process clicks for disabled buttons if (!button.hasClass(tm + '-state-disabled')) { - buttonClick(); + buttonClick(ev); // after the click action, if the button becomes the "active" tab, or disabled, // it should never have a hover class, so remove it now. @@ -8931,36 +11224,46 @@ function Header(calendar, options) { return sectionEl; } - - + + function updateTitle(text) { - el.find('h2').text(text); + if (el) { + el.find('h2').text(text); + } } - - + + function activateButton(buttonName) { - el.find('.fc-' + buttonName + '-button') - .addClass(tm + '-state-active'); + if (el) { + el.find('.fc-' + buttonName + '-button') + .addClass(tm + '-state-active'); + } } - - + + function deactivateButton(buttonName) { - el.find('.fc-' + buttonName + '-button') - .removeClass(tm + '-state-active'); + if (el) { + el.find('.fc-' + buttonName + '-button') + .removeClass(tm + '-state-active'); + } } - - + + function disableButton(buttonName) { - el.find('.fc-' + buttonName + '-button') - .attr('disabled', 'disabled') - .addClass(tm + '-state-disabled'); + if (el) { + el.find('.fc-' + buttonName + '-button') + .prop('disabled', true) + .addClass(tm + '-state-disabled'); + } } - - + + function enableButton(buttonName) { - el.find('.fc-' + buttonName + '-button') - .removeAttr('disabled') - .removeClass(tm + '-state-disabled'); + if (el) { + el.find('.fc-' + buttonName + '-button') + .prop('disabled', false) + .removeClass(tm + '-state-disabled'); + } } @@ -8972,8 +11275,1514 @@ function Header(calendar, options) { ;; -fc.sourceNormalizers = []; -fc.sourceFetchers = []; +var Calendar = FC.Calendar = Class.extend(EmitterMixin, { + + view: null, // current View object + viewsByType: null, // holds all instantiated view instances, current or not + currentDate: null, // unzoned moment. private (public API should use getDate instead) + loadingLevel: 0, // number of simultaneous loading tasks + + + constructor: function(el, overrides) { + + // declare the current calendar instance relies on GlobalEmitter. needed for garbage collection. + // unneeded() is called in destroy. + GlobalEmitter.needed(); + + this.el = el; + this.viewsByType = {}; + this.viewSpecCache = {}; + + this.initOptionsInternals(overrides); + this.initMomentInternals(); // needs to happen after options hash initialized + this.initCurrentDate(); + + EventManager.call(this); // needs options immediately + this.initialize(); + }, + + + // Subclasses can override this for initialization logic after the constructor has been called + initialize: function() { + }, + + + // Public API + // ----------------------------------------------------------------------------------------------------------------- + + + getCalendar: function() { + return this; + }, + + + getView: function() { + return this.view; + }, + + + publiclyTrigger: function(name, thisObj) { + var args = Array.prototype.slice.call(arguments, 2); + var optHandler = this.opt(name); + + thisObj = thisObj || this.el[0]; + this.triggerWith(name, thisObj, args); // Emitter's method + + if (optHandler) { + return optHandler.apply(thisObj, args); + } + }, + + + // View + // ----------------------------------------------------------------------------------------------------------------- + + + // Given a view name for a custom view or a standard view, creates a ready-to-go View object + instantiateView: function(viewType) { + var spec = this.getViewSpec(viewType); + + return new spec['class'](this, spec); + }, + + + // Returns a boolean about whether the view is okay to instantiate at some point + isValidViewType: function(viewType) { + return Boolean(this.getViewSpec(viewType)); + }, + + + changeView: function(viewName, dateOrRange) { + + if (dateOrRange) { + + if (dateOrRange.start && dateOrRange.end) { // a range + this.recordOptionOverrides({ // will not rerender + visibleRange: dateOrRange + }); + } + else { // a date + this.currentDate = this.moment(dateOrRange).stripZone(); // just like gotoDate + } + } + + this.renderView(viewName); + }, + + + // Forces navigation to a view for the given date. + // `viewType` can be a specific view name or a generic one like "week" or "day". + zoomTo: function(newDate, viewType) { + var spec; + + viewType = viewType || 'day'; // day is default zoom + spec = this.getViewSpec(viewType) || this.getUnitViewSpec(viewType); + + this.currentDate = newDate.clone(); + this.renderView(spec ? spec.type : null); + }, + + + // Current Date + // ----------------------------------------------------------------------------------------------------------------- + + + initCurrentDate: function() { + var defaultDateInput = this.opt('defaultDate'); + + // compute the initial ambig-timezone date + if (defaultDateInput != null) { + this.currentDate = this.moment(defaultDateInput).stripZone(); + } + else { + this.currentDate = this.getNow(); // getNow already returns unzoned + } + }, + + + prev: function() { + var prevInfo = this.view.buildPrevDateProfile(this.currentDate); + + if (prevInfo.isValid) { + this.currentDate = prevInfo.date; + this.renderView(); + } + }, + + + next: function() { + var nextInfo = this.view.buildNextDateProfile(this.currentDate); + + if (nextInfo.isValid) { + this.currentDate = nextInfo.date; + this.renderView(); + } + }, + + + prevYear: function() { + this.currentDate.add(-1, 'years'); + this.renderView(); + }, + + + nextYear: function() { + this.currentDate.add(1, 'years'); + this.renderView(); + }, + + + today: function() { + this.currentDate = this.getNow(); // should deny like prev/next? + this.renderView(); + }, + + + gotoDate: function(zonedDateInput) { + this.currentDate = this.moment(zonedDateInput).stripZone(); + this.renderView(); + }, + + + incrementDate: function(delta) { + this.currentDate.add(moment.duration(delta)); + this.renderView(); + }, + + + // for external API + getDate: function() { + return this.applyTimezone(this.currentDate); // infuse the calendar's timezone + }, + + + // Loading Triggering + // ----------------------------------------------------------------------------------------------------------------- + + + // Should be called when any type of async data fetching begins + pushLoading: function() { + if (!(this.loadingLevel++)) { + this.publiclyTrigger('loading', null, true, this.view); + } + }, + + + // Should be called when any type of async data fetching completes + popLoading: function() { + if (!(--this.loadingLevel)) { + this.publiclyTrigger('loading', null, false, this.view); + } + }, + + + // Selection + // ----------------------------------------------------------------------------------------------------------------- + + + // this public method receives start/end dates in any format, with any timezone + select: function(zonedStartInput, zonedEndInput) { + this.view.select( + this.buildSelectSpan.apply(this, arguments) + ); + }, + + + unselect: function() { // safe to be called before renderView + if (this.view) { + this.view.unselect(); + } + }, + + + // Given arguments to the select method in the API, returns a span (unzoned start/end and other info) + buildSelectSpan: function(zonedStartInput, zonedEndInput) { + var start = this.moment(zonedStartInput).stripZone(); + var end; + + if (zonedEndInput) { + end = this.moment(zonedEndInput).stripZone(); + } + else if (start.hasTime()) { + end = start.clone().add(this.defaultTimedEventDuration); + } + else { + end = start.clone().add(this.defaultAllDayEventDuration); + } + + return { start: start, end: end }; + }, + + + // Misc + // ----------------------------------------------------------------------------------------------------------------- + + + // will return `null` if invalid range + parseRange: function(rangeInput) { + var start = null; + var end = null; + + if (rangeInput.start) { + start = this.moment(rangeInput.start).stripZone(); + } + + if (rangeInput.end) { + end = this.moment(rangeInput.end).stripZone(); + } + + if (!start && !end) { + return null; + } + + if (start && end && end.isBefore(start)) { + return null; + } + + return { start: start, end: end }; + }, + + + rerenderEvents: function() { // API method. destroys old events if previously rendered. + if (this.elementVisible()) { + this.reportEventChange(); // will re-trasmit events to the view, causing a rerender + } + } + +}); + +;; +/* +Options binding/triggering system. +*/ +Calendar.mixin({ + + dirDefaults: null, // option defaults related to LTR or RTL + localeDefaults: null, // option defaults related to current locale + overrides: null, // option overrides given to the fullCalendar constructor + dynamicOverrides: null, // options set with dynamic setter method. higher precedence than view overrides. + optionsModel: null, // all defaults combined with overrides + + + initOptionsInternals: function(overrides) { + this.overrides = $.extend({}, overrides); // make a copy + this.dynamicOverrides = {}; + this.optionsModel = new Model(); + + this.populateOptionsHash(); + }, + + + // public getter/setter + option: function(name, value) { + var newOptionHash; + + if (typeof name === 'string') { + if (value === undefined) { // getter + return this.optionsModel.get(name); + } + else { // setter for individual option + newOptionHash = {}; + newOptionHash[name] = value; + this.setOptions(newOptionHash); + } + } + else if (typeof name === 'object') { // compound setter with object input + this.setOptions(name); + } + }, + + + // private getter + opt: function(name) { + return this.optionsModel.get(name); + }, + + + setOptions: function(newOptionHash) { + var optionCnt = 0; + var optionName; + + this.recordOptionOverrides(newOptionHash); + + for (optionName in newOptionHash) { + optionCnt++; + } + + // special-case handling of single option change. + // if only one option change, `optionName` will be its name. + if (optionCnt === 1) { + if (optionName === 'height' || optionName === 'contentHeight' || optionName === 'aspectRatio') { + this.updateSize(true); // true = allow recalculation of height + return; + } + else if (optionName === 'defaultDate') { + return; // can't change date this way. use gotoDate instead + } + else if (optionName === 'businessHours') { + if (this.view) { + this.view.unrenderBusinessHours(); + this.view.renderBusinessHours(); + } + return; + } + else if (optionName === 'timezone') { + this.rezoneArrayEventSources(); + this.refetchEvents(); + return; + } + } + + // catch-all. rerender the header and footer and rebuild/rerender the current view + this.renderHeader(); + this.renderFooter(); + + // even non-current views will be affected by this option change. do before rerender + // TODO: detangle + this.viewsByType = {}; + + this.reinitView(); + }, + + + // Computes the flattened options hash for the calendar and assigns to `this.options`. + // Assumes this.overrides and this.dynamicOverrides have already been initialized. + populateOptionsHash: function() { + var locale, localeDefaults; + var isRTL, dirDefaults; + var rawOptions; + + locale = firstDefined( // explicit locale option given? + this.dynamicOverrides.locale, + this.overrides.locale + ); + localeDefaults = localeOptionHash[locale]; + if (!localeDefaults) { // explicit locale option not given or invalid? + locale = Calendar.defaults.locale; + localeDefaults = localeOptionHash[locale] || {}; + } + + isRTL = firstDefined( // based on options computed so far, is direction RTL? + this.dynamicOverrides.isRTL, + this.overrides.isRTL, + localeDefaults.isRTL, + Calendar.defaults.isRTL + ); + dirDefaults = isRTL ? Calendar.rtlDefaults : {}; + + this.dirDefaults = dirDefaults; + this.localeDefaults = localeDefaults; + + rawOptions = mergeOptions([ // merge defaults and overrides. lowest to highest precedence + Calendar.defaults, // global defaults + dirDefaults, + localeDefaults, + this.overrides, + this.dynamicOverrides + ]); + populateInstanceComputableOptions(rawOptions); // fill in gaps with computed options + + this.optionsModel.reset(rawOptions); + }, + + + // stores the new options internally, but does not rerender anything. + recordOptionOverrides: function(newOptionHash) { + var optionName; + + for (optionName in newOptionHash) { + this.dynamicOverrides[optionName] = newOptionHash[optionName]; + } + + this.viewSpecCache = {}; // the dynamic override invalidates the options in this cache, so just clear it + this.populateOptionsHash(); // this.options needs to be recomputed after the dynamic override + } + +}); + +;; + +Calendar.mixin({ + + defaultAllDayEventDuration: null, + defaultTimedEventDuration: null, + localeData: null, + + + initMomentInternals: function() { + var _this = this; + + this.defaultAllDayEventDuration = moment.duration(this.opt('defaultAllDayEventDuration')); + this.defaultTimedEventDuration = moment.duration(this.opt('defaultTimedEventDuration')); + + // Called immediately, and when any of the options change. + // Happens before any internal objects rebuild or rerender, because this is very core. + this.optionsModel.watch('buildingMomentLocale', [ + '?locale', '?monthNames', '?monthNamesShort', '?dayNames', '?dayNamesShort', + '?firstDay', '?weekNumberCalculation' + ], function(opts) { + var weekNumberCalculation = opts.weekNumberCalculation; + var firstDay = opts.firstDay; + var _week; + + // normalize + if (weekNumberCalculation === 'iso') { + weekNumberCalculation = 'ISO'; // normalize + } + + var localeData = createObject( // make a cheap copy + getMomentLocaleData(opts.locale) // will fall back to en + ); + + if (opts.monthNames) { + localeData._months = opts.monthNames; + } + if (opts.monthNamesShort) { + localeData._monthsShort = opts.monthNamesShort; + } + if (opts.dayNames) { + localeData._weekdays = opts.dayNames; + } + if (opts.dayNamesShort) { + localeData._weekdaysShort = opts.dayNamesShort; + } + + if (firstDay == null && weekNumberCalculation === 'ISO') { + firstDay = 1; + } + if (firstDay != null) { + _week = createObject(localeData._week); // _week: { dow: # } + _week.dow = firstDay; + localeData._week = _week; + } + + if ( // whitelist certain kinds of input + weekNumberCalculation === 'ISO' || + weekNumberCalculation === 'local' || + typeof weekNumberCalculation === 'function' + ) { + localeData._fullCalendar_weekCalc = weekNumberCalculation; // moment-ext will know what to do with it + } + + _this.localeData = localeData; + + // If the internal current date object already exists, move to new locale. + // We do NOT need to do this technique for event dates, because this happens when converting to "segments". + if (_this.currentDate) { + _this.localizeMoment(_this.currentDate); // sets to localeData + } + }); + }, + + + // Builds a moment using the settings of the current calendar: timezone and locale. + // Accepts anything the vanilla moment() constructor accepts. + moment: function() { + var mom; + + if (this.opt('timezone') === 'local') { + mom = FC.moment.apply(null, arguments); + + // Force the moment to be local, because FC.moment doesn't guarantee it. + if (mom.hasTime()) { // don't give ambiguously-timed moments a local zone + mom.local(); + } + } + else if (this.opt('timezone') === 'UTC') { + mom = FC.moment.utc.apply(null, arguments); // process as UTC + } + else { + mom = FC.moment.parseZone.apply(null, arguments); // let the input decide the zone + } + + this.localizeMoment(mom); // TODO + + return mom; + }, + + + // Updates the given moment's locale settings to the current calendar locale settings. + localizeMoment: function(mom) { + mom._locale = this.localeData; + }, + + + // Returns a boolean about whether or not the calendar knows how to calculate + // the timezone offset of arbitrary dates in the current timezone. + getIsAmbigTimezone: function() { + return this.opt('timezone') !== 'local' && this.opt('timezone') !== 'UTC'; + }, + + + // Returns a copy of the given date in the current timezone. Has no effect on dates without times. + applyTimezone: function(date) { + if (!date.hasTime()) { + return date.clone(); + } + + var zonedDate = this.moment(date.toArray()); + var timeAdjust = date.time() - zonedDate.time(); + var adjustedZonedDate; + + // Safari sometimes has problems with this coersion when near DST. Adjust if necessary. (bug #2396) + if (timeAdjust) { // is the time result different than expected? + adjustedZonedDate = zonedDate.clone().add(timeAdjust); // add milliseconds + if (date.time() - adjustedZonedDate.time() === 0) { // does it match perfectly now? + zonedDate = adjustedZonedDate; + } + } + + return zonedDate; + }, + + + // Returns a moment for the current date, as defined by the client's computer or from the `now` option. + // Will return an moment with an ambiguous timezone. + getNow: function() { + var now = this.opt('now'); + if (typeof now === 'function') { + now = now(); + } + return this.moment(now).stripZone(); + }, + + + // Produces a human-readable string for the given duration. + // Side-effect: changes the locale of the given duration. + humanizeDuration: function(duration) { + return duration.locale(this.opt('locale')).humanize(); + }, + + + + // Event-Specific Date Utilities. TODO: move + // ----------------------------------------------------------------------------------------------------------------- + + + // Get an event's normalized end date. If not present, calculate it from the defaults. + getEventEnd: function(event) { + if (event.end) { + return event.end.clone(); + } + else { + return this.getDefaultEventEnd(event.allDay, event.start); + } + }, + + + // Given an event's allDay status and start date, return what its fallback end date should be. + // TODO: rename to computeDefaultEventEnd + getDefaultEventEnd: function(allDay, zonedStart) { + var end = zonedStart.clone(); + + if (allDay) { + end.stripTime().add(this.defaultAllDayEventDuration); + } + else { + end.add(this.defaultTimedEventDuration); + } + + if (this.getIsAmbigTimezone()) { + end.stripZone(); // we don't know what the tzo should be + } + + return end; + } + +}); + +;; + +Calendar.mixin({ + + viewSpecCache: null, // cache of view definitions (initialized in Calendar.js) + + + // Gets information about how to create a view. Will use a cache. + getViewSpec: function(viewType) { + var cache = this.viewSpecCache; + + return cache[viewType] || (cache[viewType] = this.buildViewSpec(viewType)); + }, + + + // Given a duration singular unit, like "week" or "day", finds a matching view spec. + // Preference is given to views that have corresponding buttons. + getUnitViewSpec: function(unit) { + var viewTypes; + var i; + var spec; + + if ($.inArray(unit, unitsDesc) != -1) { + + // put views that have buttons first. there will be duplicates, but oh well + viewTypes = this.header.getViewsWithButtons(); // TODO: include footer as well? + $.each(FC.views, function(viewType) { // all views + viewTypes.push(viewType); + }); + + for (i = 0; i < viewTypes.length; i++) { + spec = this.getViewSpec(viewTypes[i]); + if (spec) { + if (spec.singleUnit == unit) { + return spec; + } + } + } + } + }, + + + // Builds an object with information on how to create a given view + buildViewSpec: function(requestedViewType) { + var viewOverrides = this.overrides.views || {}; + var specChain = []; // for the view. lowest to highest priority + var defaultsChain = []; // for the view. lowest to highest priority + var overridesChain = []; // for the view. lowest to highest priority + var viewType = requestedViewType; + var spec; // for the view + var overrides; // for the view + var durationInput; + var duration; + var unit; + + // iterate from the specific view definition to a more general one until we hit an actual View class + while (viewType) { + spec = fcViews[viewType]; + overrides = viewOverrides[viewType]; + viewType = null; // clear. might repopulate for another iteration + + if (typeof spec === 'function') { // TODO: deprecate + spec = { 'class': spec }; + } + + if (spec) { + specChain.unshift(spec); + defaultsChain.unshift(spec.defaults || {}); + durationInput = durationInput || spec.duration; + viewType = viewType || spec.type; + } + + if (overrides) { + overridesChain.unshift(overrides); // view-specific option hashes have options at zero-level + durationInput = durationInput || overrides.duration; + viewType = viewType || overrides.type; + } + } + + spec = mergeProps(specChain); + spec.type = requestedViewType; + if (!spec['class']) { + return false; + } + + // fall back to top-level `duration` option + durationInput = durationInput || + this.dynamicOverrides.duration || + this.overrides.duration; + + if (durationInput) { + duration = moment.duration(durationInput); + + if (duration.valueOf()) { // valid? + + unit = computeDurationGreatestUnit(duration, durationInput); + + spec.duration = duration; + spec.durationUnit = unit; + + // view is a single-unit duration, like "week" or "day" + // incorporate options for this. lowest priority + if (duration.as(unit) === 1) { + spec.singleUnit = unit; + overridesChain.unshift(viewOverrides[unit] || {}); + } + } + } + + spec.defaults = mergeOptions(defaultsChain); + spec.overrides = mergeOptions(overridesChain); + + this.buildViewSpecOptions(spec); + this.buildViewSpecButtonText(spec, requestedViewType); + + return spec; + }, + + + // Builds and assigns a view spec's options object from its already-assigned defaults and overrides + buildViewSpecOptions: function(spec) { + spec.options = mergeOptions([ // lowest to highest priority + Calendar.defaults, // global defaults + spec.defaults, // view's defaults (from ViewSubclass.defaults) + this.dirDefaults, + this.localeDefaults, // locale and dir take precedence over view's defaults! + this.overrides, // calendar's overrides (options given to constructor) + spec.overrides, // view's overrides (view-specific options) + this.dynamicOverrides // dynamically set via setter. highest precedence + ]); + populateInstanceComputableOptions(spec.options); + }, + + + // Computes and assigns a view spec's buttonText-related options + buildViewSpecButtonText: function(spec, requestedViewType) { + + // given an options object with a possible `buttonText` hash, lookup the buttonText for the + // requested view, falling back to a generic unit entry like "week" or "day" + function queryButtonText(options) { + var buttonText = options.buttonText || {}; + return buttonText[requestedViewType] || + // view can decide to look up a certain key + (spec.buttonTextKey ? buttonText[spec.buttonTextKey] : null) || + // a key like "month" + (spec.singleUnit ? buttonText[spec.singleUnit] : null); + } + + // highest to lowest priority + spec.buttonTextOverride = + queryButtonText(this.dynamicOverrides) || + queryButtonText(this.overrides) || // constructor-specified buttonText lookup hash takes precedence + spec.overrides.buttonText; // `buttonText` for view-specific options is a string + + // highest to lowest priority. mirrors buildViewSpecOptions + spec.buttonTextDefault = + queryButtonText(this.localeDefaults) || + queryButtonText(this.dirDefaults) || + spec.defaults.buttonText || // a single string. from ViewSubclass.defaults + queryButtonText(Calendar.defaults) || + (spec.duration ? this.humanizeDuration(spec.duration) : null) || // like "3 days" + requestedViewType; // fall back to given view name + } + +}); + +;; + +Calendar.mixin({ + + el: null, + contentEl: null, + suggestedViewHeight: null, + windowResizeProxy: null, + ignoreWindowResize: 0, + + + render: function() { + if (!this.contentEl) { + this.initialRender(); + } + else if (this.elementVisible()) { + // mainly for the public API + this.calcSize(); + this.renderView(); + } + }, + + + initialRender: function() { + var _this = this; + var el = this.el; + + el.addClass('fc'); + + // event delegation for nav links + el.on('click.fc', 'a[data-goto]', function(ev) { + var anchorEl = $(this); + var gotoOptions = anchorEl.data('goto'); // will automatically parse JSON + var date = _this.moment(gotoOptions.date); + var viewType = gotoOptions.type; + + // property like "navLinkDayClick". might be a string or a function + var customAction = _this.view.opt('navLink' + capitaliseFirstLetter(viewType) + 'Click'); + + if (typeof customAction === 'function') { + customAction(date, ev); + } + else { + if (typeof customAction === 'string') { + viewType = customAction; + } + _this.zoomTo(date, viewType); + } + }); + + // called immediately, and upon option change + this.optionsModel.watch('applyingThemeClasses', [ '?theme' ], function(opts) { + el.toggleClass('ui-widget', opts.theme); + el.toggleClass('fc-unthemed', !opts.theme); + }); + + // called immediately, and upon option change. + // HACK: locale often affects isRTL, so we explicitly listen to that too. + this.optionsModel.watch('applyingDirClasses', [ '?isRTL', '?locale' ], function(opts) { + el.toggleClass('fc-ltr', !opts.isRTL); + el.toggleClass('fc-rtl', opts.isRTL); + }); + + this.contentEl = $("
").prependTo(el); + + this.initToolbars(); + this.renderHeader(); + this.renderFooter(); + this.renderView(this.opt('defaultView')); + + if (this.opt('handleWindowResize')) { + $(window).resize( + this.windowResizeProxy = debounce( // prevents rapid calls + this.windowResize.bind(this), + this.opt('windowResizeDelay') + ) + ); + } + }, + + + destroy: function() { + + if (this.view) { + this.view.removeElement(); + + // NOTE: don't null-out this.view in case API methods are called after destroy. + // It is still the "current" view, just not rendered. + } + + this.toolbarsManager.proxyCall('removeElement'); + this.contentEl.remove(); + this.el.removeClass('fc fc-ltr fc-rtl fc-unthemed ui-widget'); + + this.el.off('.fc'); // unbind nav link handlers + + if (this.windowResizeProxy) { + $(window).unbind('resize', this.windowResizeProxy); + this.windowResizeProxy = null; + } + + GlobalEmitter.unneeded(); + }, + + + elementVisible: function() { + return this.el.is(':visible'); + }, + + + + // View Rendering + // ----------------------------------------------------------------------------------- + + + // Renders a view because of a date change, view-type change, or for the first time. + // If not given a viewType, keep the current view but render different dates. + // Accepts an optional scroll state to restore to. + renderView: function(viewType, forcedScroll) { + + this.ignoreWindowResize++; + + var needsClearView = this.view && viewType && this.view.type !== viewType; + + // if viewType is changing, remove the old view's rendering + if (needsClearView) { + this.freezeContentHeight(); // prevent a scroll jump when view element is removed + this.clearView(); + } + + // if viewType changed, or the view was never created, create a fresh view + if (!this.view && viewType) { + this.view = + this.viewsByType[viewType] || + (this.viewsByType[viewType] = this.instantiateView(viewType)); + + this.view.setElement( + $("
").appendTo(this.contentEl) + ); + this.toolbarsManager.proxyCall('activateButton', viewType); + } + + if (this.view) { + + if (forcedScroll) { + this.view.addForcedScroll(forcedScroll); + } + + if (this.elementVisible()) { + this.currentDate = this.view.setDate(this.currentDate); + } + } + + if (needsClearView) { + this.thawContentHeight(); + } + + this.ignoreWindowResize--; + }, + + + // Unrenders the current view and reflects this change in the Header. + // Unregsiters the `view`, but does not remove from viewByType hash. + clearView: function() { + this.toolbarsManager.proxyCall('deactivateButton', this.view.type); + this.view.removeElement(); + this.view = null; + }, + + + // Destroys the view, including the view object. Then, re-instantiates it and renders it. + // Maintains the same scroll state. + // TODO: maintain any other user-manipulated state. + reinitView: function() { + this.ignoreWindowResize++; + this.freezeContentHeight(); + + var viewType = this.view.type; + var scrollState = this.view.queryScroll(); + this.clearView(); + this.calcSize(); + this.renderView(viewType, scrollState); + + this.thawContentHeight(); + this.ignoreWindowResize--; + }, + + + // Resizing + // ----------------------------------------------------------------------------------- + + + getSuggestedViewHeight: function() { + if (this.suggestedViewHeight === null) { + this.calcSize(); + } + return this.suggestedViewHeight; + }, + + + isHeightAuto: function() { + return this.opt('contentHeight') === 'auto' || this.opt('height') === 'auto'; + }, + + + updateSize: function(shouldRecalc) { + if (this.elementVisible()) { + + if (shouldRecalc) { + this._calcSize(); + } + + this.ignoreWindowResize++; + this.view.updateSize(true); // isResize=true. will poll getSuggestedViewHeight() and isHeightAuto() + this.ignoreWindowResize--; + + return true; // signal success + } + }, + + + calcSize: function() { + if (this.elementVisible()) { + this._calcSize(); + } + }, + + + _calcSize: function() { // assumes elementVisible + var contentHeightInput = this.opt('contentHeight'); + var heightInput = this.opt('height'); + + if (typeof contentHeightInput === 'number') { // exists and not 'auto' + this.suggestedViewHeight = contentHeightInput; + } + else if (typeof contentHeightInput === 'function') { // exists and is a function + this.suggestedViewHeight = contentHeightInput(); + } + else if (typeof heightInput === 'number') { // exists and not 'auto' + this.suggestedViewHeight = heightInput - this.queryToolbarsHeight(); + } + else if (typeof heightInput === 'function') { // exists and is a function + this.suggestedViewHeight = heightInput() - this.queryToolbarsHeight(); + } + else if (heightInput === 'parent') { // set to height of parent element + this.suggestedViewHeight = this.el.parent().height() - this.queryToolbarsHeight(); + } + else { + this.suggestedViewHeight = Math.round( + this.contentEl.width() / + Math.max(this.opt('aspectRatio'), .5) + ); + } + }, + + + windowResize: function(ev) { + if ( + !this.ignoreWindowResize && + ev.target === window && // so we don't process jqui "resize" events that have bubbled up + this.view.renderRange // view has already been rendered + ) { + if (this.updateSize(true)) { + this.view.publiclyTrigger('windowResize', this.el[0]); + } + } + }, + + + /* Height "Freezing" + -----------------------------------------------------------------------------*/ + + + freezeContentHeight: function() { + this.contentEl.css({ + width: '100%', + height: this.contentEl.height(), + overflow: 'hidden' + }); + }, + + + thawContentHeight: function() { + this.contentEl.css({ + width: '', + height: '', + overflow: '' + }); + } + +}); + +;; + +Calendar.mixin({ + + header: null, + footer: null, + toolbarsManager: null, + + + initToolbars: function() { + this.header = new Toolbar(this, this.computeHeaderOptions()); + this.footer = new Toolbar(this, this.computeFooterOptions()); + this.toolbarsManager = new Iterator([ this.header, this.footer ]); + }, + + + computeHeaderOptions: function() { + return { + extraClasses: 'fc-header-toolbar', + layout: this.opt('header') + }; + }, + + + computeFooterOptions: function() { + return { + extraClasses: 'fc-footer-toolbar', + layout: this.opt('footer') + }; + }, + + + // can be called repeatedly and Header will rerender + renderHeader: function() { + var header = this.header; + + header.setToolbarOptions(this.computeHeaderOptions()); + header.render(); + + if (header.el) { + this.el.prepend(header.el); + } + }, + + + // can be called repeatedly and Footer will rerender + renderFooter: function() { + var footer = this.footer; + + footer.setToolbarOptions(this.computeFooterOptions()); + footer.render(); + + if (footer.el) { + this.el.append(footer.el); + } + }, + + + setToolbarsTitle: function(title) { + this.toolbarsManager.proxyCall('updateTitle', title); + }, + + + updateToolbarButtons: function() { + var now = this.getNow(); + var view = this.view; + var todayInfo = view.buildDateProfile(now); + var prevInfo = view.buildPrevDateProfile(this.currentDate); + var nextInfo = view.buildNextDateProfile(this.currentDate); + + this.toolbarsManager.proxyCall( + (todayInfo.isValid && !isDateWithinRange(now, view.currentRange)) ? + 'enableButton' : + 'disableButton', + 'today' + ); + + this.toolbarsManager.proxyCall( + prevInfo.isValid ? + 'enableButton' : + 'disableButton', + 'prev' + ); + + this.toolbarsManager.proxyCall( + nextInfo.isValid ? + 'enableButton' : + 'disableButton', + 'next' + ); + }, + + + queryToolbarsHeight: function() { + return this.toolbarsManager.items.reduce(function(accumulator, toolbar) { + var toolbarHeight = toolbar.el ? toolbar.el.outerHeight(true) : 0; // includes margin + return accumulator + toolbarHeight; + }, 0); + } + +}); + +;; + +Calendar.defaults = { + + titleRangeSeparator: ' \u2013 ', // en dash + monthYearFormat: 'MMMM YYYY', // required for en. other locales rely on datepicker computable option + + defaultTimedEventDuration: '02:00:00', + defaultAllDayEventDuration: { days: 1 }, + forceEventDuration: false, + nextDayThreshold: '09:00:00', // 9am + + // display + defaultView: 'month', + aspectRatio: 1.35, + header: { + left: 'title', + center: '', + right: 'today prev,next' + }, + weekends: true, + weekNumbers: false, + + weekNumberTitle: 'W', + weekNumberCalculation: 'local', + + //editable: false, + + //nowIndicator: false, + + scrollTime: '06:00:00', + minTime: '00:00:00', + maxTime: '24:00:00', + showNonCurrentDates: true, + + // event ajax + lazyFetching: true, + startParam: 'start', + endParam: 'end', + timezoneParam: 'timezone', + + timezone: false, + + //allDayDefault: undefined, + + // locale + isRTL: false, + buttonText: { + prev: "prev", + next: "next", + prevYear: "prev year", + nextYear: "next year", + year: 'year', // TODO: locale files need to specify this + today: 'today', + month: 'month', + week: 'week', + day: 'day' + }, + + buttonIcons: { + prev: 'left-single-arrow', + next: 'right-single-arrow', + prevYear: 'left-double-arrow', + nextYear: 'right-double-arrow' + }, + + allDayText: 'all-day', + + // jquery-ui theming + theme: false, + themeButtonIcons: { + prev: 'circle-triangle-w', + next: 'circle-triangle-e', + prevYear: 'seek-prev', + nextYear: 'seek-next' + }, + + //eventResizableFromStart: false, + dragOpacity: .75, + dragRevertDuration: 500, + dragScroll: true, + + //selectable: false, + unselectAuto: true, + //selectMinDistance: 0, + + dropAccept: '*', + + eventOrder: 'title', + //eventRenderWait: null, + + eventLimit: false, + eventLimitText: 'more', + eventLimitClick: 'popover', + dayPopoverFormat: 'LL', + + handleWindowResize: true, + windowResizeDelay: 100, // milliseconds before an updateSize happens + + longPressDelay: 1000 + +}; + + +Calendar.englishDefaults = { // used by locale.js + dayPopoverFormat: 'dddd, MMMM D' +}; + + +Calendar.rtlDefaults = { // right-to-left defaults + header: { // TODO: smarter solution (first/center/last ?) + left: 'next,prev today', + center: '', + right: 'title' + }, + buttonIcons: { + prev: 'right-single-arrow', + next: 'left-single-arrow', + prevYear: 'right-double-arrow', + nextYear: 'left-double-arrow' + }, + themeButtonIcons: { + prev: 'circle-triangle-e', + next: 'circle-triangle-w', + nextYear: 'seek-prev', + prevYear: 'seek-next' + } +}; + +;; + +var localeOptionHash = FC.locales = {}; // initialize and expose + + +// TODO: document the structure and ordering of a FullCalendar locale file + + +// Initialize jQuery UI datepicker translations while using some of the translations +// Will set this as the default locales for datepicker. +FC.datepickerLocale = function(localeCode, dpLocaleCode, dpOptions) { + + // get the FullCalendar internal option hash for this locale. create if necessary + var fcOptions = localeOptionHash[localeCode] || (localeOptionHash[localeCode] = {}); + + // transfer some simple options from datepicker to fc + fcOptions.isRTL = dpOptions.isRTL; + fcOptions.weekNumberTitle = dpOptions.weekHeader; + + // compute some more complex options from datepicker + $.each(dpComputableOptions, function(name, func) { + fcOptions[name] = func(dpOptions); + }); + + // is jQuery UI Datepicker is on the page? + if ($.datepicker) { + + // Register the locale data. + // FullCalendar and MomentJS use locale codes like "pt-br" but Datepicker + // does it like "pt-BR" or if it doesn't have the locale, maybe just "pt". + // Make an alias so the locale can be referenced either way. + $.datepicker.regional[dpLocaleCode] = + $.datepicker.regional[localeCode] = // alias + dpOptions; + + // Alias 'en' to the default locale data. Do this every time. + $.datepicker.regional.en = $.datepicker.regional['']; + + // Set as Datepicker's global defaults. + $.datepicker.setDefaults(dpOptions); + } +}; + + +// Sets FullCalendar-specific translations. Will set the locales as the global default. +FC.locale = function(localeCode, newFcOptions) { + var fcOptions; + var momOptions; + + // get the FullCalendar internal option hash for this locale. create if necessary + fcOptions = localeOptionHash[localeCode] || (localeOptionHash[localeCode] = {}); + + // provided new options for this locales? merge them in + if (newFcOptions) { + fcOptions = localeOptionHash[localeCode] = mergeOptions([ fcOptions, newFcOptions ]); + } + + // compute locale options that weren't defined. + // always do this. newFcOptions can be undefined when initializing from i18n file, + // so no way to tell if this is an initialization or a default-setting. + momOptions = getMomentLocaleData(localeCode); // will fall back to en + $.each(momComputableOptions, function(name, func) { + if (fcOptions[name] == null) { + fcOptions[name] = func(momOptions, fcOptions); + } + }); + + // set it as the default locale for FullCalendar + Calendar.defaults.locale = localeCode; +}; + + +// NOTE: can't guarantee any of these computations will run because not every locale has datepicker +// configs, so make sure there are English fallbacks for these in the defaults file. +var dpComputableOptions = { + + buttonText: function(dpOptions) { + return { + // the translations sometimes wrongly contain HTML entities + prev: stripHtmlEntities(dpOptions.prevText), + next: stripHtmlEntities(dpOptions.nextText), + today: stripHtmlEntities(dpOptions.currentText) + }; + }, + + // Produces format strings like "MMMM YYYY" -> "September 2014" + monthYearFormat: function(dpOptions) { + return dpOptions.showMonthAfterYear ? + 'YYYY[' + dpOptions.yearSuffix + '] MMMM' : + 'MMMM YYYY[' + dpOptions.yearSuffix + ']'; + } + +}; + +var momComputableOptions = { + + // Produces format strings like "ddd M/D" -> "Fri 9/15" + dayOfMonthFormat: function(momOptions, fcOptions) { + var format = momOptions.longDateFormat('l'); // for the format like "M/D/YYYY" + + // strip the year off the edge, as well as other misc non-whitespace chars + format = format.replace(/^Y+[^\w\s]*|[^\w\s]*Y+$/g, ''); + + if (fcOptions.isRTL) { + format += ' ddd'; // for RTL, add day-of-week to end + } + else { + format = 'ddd ' + format; // for LTR, add day-of-week to beginning + } + return format; + }, + + // Produces format strings like "h:mma" -> "6:00pm" + mediumTimeFormat: function(momOptions) { // can't be called `timeFormat` because collides with option + return momOptions.longDateFormat('LT') + .replace(/\s*a$/i, 'a'); // convert AM/PM/am/pm to lowercase. remove any spaces beforehand + }, + + // Produces format strings like "h(:mm)a" -> "6pm" / "6:30pm" + smallTimeFormat: function(momOptions) { + return momOptions.longDateFormat('LT') + .replace(':mm', '(:mm)') + .replace(/(\Wmm)$/, '($1)') // like above, but for foreign locales + .replace(/\s*a$/i, 'a'); // convert AM/PM/am/pm to lowercase. remove any spaces beforehand + }, + + // Produces format strings like "h(:mm)t" -> "6p" / "6:30p" + extraSmallTimeFormat: function(momOptions) { + return momOptions.longDateFormat('LT') + .replace(':mm', '(:mm)') + .replace(/(\Wmm)$/, '($1)') // like above, but for foreign locales + .replace(/\s*a$/i, 't'); // convert to AM/PM/am/pm to lowercase one-letter. remove any spaces beforehand + }, + + // Produces format strings like "ha" / "H" -> "6pm" / "18" + hourFormat: function(momOptions) { + return momOptions.longDateFormat('LT') + .replace(':mm', '') + .replace(/(\Wmm)$/, '') // like above, but for foreign locales + .replace(/\s*a$/i, 'a'); // convert AM/PM/am/pm to lowercase. remove any spaces beforehand + }, + + // Produces format strings like "h:mm" -> "6:30" (with no AM/PM) + noMeridiemTimeFormat: function(momOptions) { + return momOptions.longDateFormat('LT') + .replace(/\s*a$/i, ''); // remove trailing AM/PM + } + +}; + + +// options that should be computed off live calendar options (considers override options) +// TODO: best place for this? related to locale? +// TODO: flipping text based on isRTL is a bad idea because the CSS `direction` might want to handle it +var instanceComputableOptions = { + + // Produces format strings for results like "Mo 16" + smallDayDateFormat: function(options) { + return options.isRTL ? + 'D dd' : + 'dd D'; + }, + + // Produces format strings for results like "Wk 5" + weekFormat: function(options) { + return options.isRTL ? + 'w[ ' + options.weekNumberTitle + ']' : + '[' + options.weekNumberTitle + ' ]w'; + }, + + // Produces format strings for results like "Wk5" + smallWeekFormat: function(options) { + return options.isRTL ? + 'w[' + options.weekNumberTitle + ']' : + '[' + options.weekNumberTitle + ']w'; + } + +}; + +// TODO: make these computable properties in optionsModel +function populateInstanceComputableOptions(options) { + $.each(instanceComputableOptions, function(name, func) { + if (options[name] == null) { + options[name] = func(options); + } + }); +} + + +// Returns moment's internal locale data. If doesn't exist, returns English. +function getMomentLocaleData(localeCode) { + return moment.localeData(localeCode) || moment.localeData('en'); +} + + +// Initialize English by forcing computation of moment-derived options. +// Also, sets it as the default. +FC.locale('en', Calendar.englishDefaults); + +;; + +FC.sourceNormalizers = []; +FC.sourceFetchers = []; var ajaxDefaults = { dataType: 'json', @@ -8983,40 +12792,45 @@ var ajaxDefaults = { var eventGUID = 1; -function EventManager(options) { // assumed to be a calendar +function EventManager() { // assumed to be a calendar var t = this; - - + + // exports + t.requestEvents = requestEvents; + t.reportEventChange = reportEventChange; t.isFetchNeeded = isFetchNeeded; t.fetchEvents = fetchEvents; + t.fetchEventSources = fetchEventSources; + t.refetchEvents = refetchEvents; + t.refetchEventSources = refetchEventSources; + t.getEventSources = getEventSources; + t.getEventSourceById = getEventSourceById; t.addEventSource = addEventSource; t.removeEventSource = removeEventSource; + t.removeEventSources = removeEventSources; t.updateEvent = updateEvent; + t.updateEvents = updateEvents; t.renderEvent = renderEvent; + t.renderEvents = renderEvents; t.removeEvents = removeEvents; t.clientEvents = clientEvents; t.mutateEvent = mutateEvent; - t.normalizeEventRange = normalizeEventRange; - t.normalizeEventRangeTimes = normalizeEventRangeTimes; - t.ensureVisibleEventRange = ensureVisibleEventRange; - - - // imports - var reportEvents = t.reportEvents; - - + t.normalizeEventDates = normalizeEventDates; + t.normalizeEventTimes = normalizeEventTimes; + + // locals var stickySource = { events: [] }; var sources = [ stickySource ]; var rangeStart, rangeEnd; - var currentFetchID = 0; - var pendingSourceCnt = 0; + var pendingSourceCnt = 0; // outstanding fetch requests, max one per source var cache = []; // holds events that have already been expanded + var prunedCache; // like cache, but only events that intersect with rangeStart/rangeEnd $.each( - (options.events ? [ options.events ] : []).concat(options.eventSources || []), + (t.opt('events') ? [ t.opt('events') ] : []).concat(t.opt('eventSources') || []), function(i, sourceInput) { var source = buildEventSource(sourceInput); if (source) { @@ -9024,41 +12838,136 @@ function EventManager(options) { // assumed to be a calendar } } ); - - - + + + + function requestEvents(start, end) { + if (!t.opt('lazyFetching') || isFetchNeeded(start, end)) { + return fetchEvents(start, end); + } + else { + return Promise.resolve(prunedCache); + } + } + + + function reportEventChange() { + prunedCache = filterEventsWithinRange(cache); + t.trigger('eventsReset', prunedCache); + } + + + function filterEventsWithinRange(events) { + var filteredEvents = []; + var i, event; + + for (i = 0; i < events.length; i++) { + event = events[i]; + + if ( + event.start.clone().stripZone() < rangeEnd && + t.getEventEnd(event).stripZone() > rangeStart + ) { + filteredEvents.push(event); + } + } + + return filteredEvents; + } + + + t.getEventCache = function() { + return cache; + }; + + + /* Fetching -----------------------------------------------------------------------------*/ - - + + + // start and end are assumed to be unzoned function isFetchNeeded(start, end) { return !rangeStart || // nothing has been fetched yet? - // or, a part of the new range is outside of the old range? (after normalizing) - start.clone().stripZone() < rangeStart.clone().stripZone() || - end.clone().stripZone() > rangeEnd.clone().stripZone(); + start < rangeStart || end > rangeEnd; // is part of the new range outside of the old range? } - - + + function fetchEvents(start, end) { rangeStart = start; rangeEnd = end; - cache = []; - var fetchID = ++currentFetchID; - var len = sources.length; - pendingSourceCnt = len; - for (var i=0; i eventRange system - function ensureVisibleEventRange(range) { - var allDay; - - if (!range.end) { - - allDay = range.allDay; // range might be more event-ish than we think - if (allDay == null) { - allDay = !range.start.hasTime(); - } - - range = $.extend({}, range); // make a copy, copying over other misc properties - range.end = t.getDefaultEventEnd(allDay, range.start); - } - return range; - } - - // If the given event is a recurring event, break it down into an array of individual instances. // If not a recurring event, return an array with the single original event. // If given a falsy input (probably because of a failed buildEventFromInput call), returns an empty array. @@ -9637,6 +13712,7 @@ function EventManager(options) { // assumed to be a calendar return events; } + t.expandEvent = expandEvent; @@ -9685,7 +13761,7 @@ function EventManager(options) { // assumed to be a calendar if (newProps.allDay == null) { // is null or undefined? newProps.allDay = event.allDay; } - normalizeEventRange(newProps); + normalizeEventDates(newProps); // create normalized versions of the original props to compare against // need a real end value, for diffing @@ -9694,7 +13770,7 @@ function EventManager(options) { // assumed to be a calendar end: event._end ? event._end.clone() : t.getDefaultEventEnd(event._allDay, event._start), allDay: newProps.allDay // normalize the dates in the same regard as the new properties }; - normalizeEventRange(oldProps); + normalizeEventDates(oldProps); // need to clear the end date if explicitly changed to null clearEnd = event._end !== null && newProps.end === null; @@ -9779,7 +13855,7 @@ function EventManager(options) { // assumed to be a calendar end: event._end, allDay: allDay // normalize the dates in the same regard as the new properties }; - normalizeEventRange(newProps); // massages start/end/allDay + normalizeEventDates(newProps); // massages start/end/allDay // strip or ensure the end date if (clearEnd) { @@ -9829,233 +13905,34 @@ function EventManager(options) { // assumed to be a calendar }; } - - /* Business Hours - -----------------------------------------------------------------------------------------*/ - - t.getBusinessHoursEvents = getBusinessHoursEvents; - - - // Returns an array of events as to when the business hours occur in the given view. - // Abuse of our event system :( - function getBusinessHoursEvents(wholeDay) { - var optionVal = options.businessHours; - var defaultVal = { - className: 'fc-nonbusiness', - start: '09:00', - end: '17:00', - dow: [ 1, 2, 3, 4, 5 ], // monday - friday - rendering: 'inverse-background' - }; - var view = t.getView(); - var eventInput; - - if (optionVal) { // `true` (which means "use the defaults") or an override object - eventInput = $.extend( - {}, // copy to a new object in either case - defaultVal, - typeof optionVal === 'object' ? optionVal : {} // override the defaults - ); - } - - if (eventInput) { - - // if a whole-day series is requested, clear the start/end times - if (wholeDay) { - eventInput.start = null; - eventInput.end = null; - } - - return expandEvent( - buildEventFromInput(eventInput), - view.start, - view.end - ); - } - - return []; - } - - - /* Overlapping / Constraining - -----------------------------------------------------------------------------------------*/ - - t.isEventRangeAllowed = isEventRangeAllowed; - t.isSelectionRangeAllowed = isSelectionRangeAllowed; - t.isExternalDropRangeAllowed = isExternalDropRangeAllowed; - - - function isEventRangeAllowed(range, event) { - var source = event.source || {}; - var constraint = firstDefined( - event.constraint, - source.constraint, - options.eventConstraint - ); - var overlap = firstDefined( - event.overlap, - source.overlap, - options.eventOverlap - ); - - range = ensureVisibleEventRange(range); // ensure a proper range with an end for isRangeAllowed - - return isRangeAllowed(range, constraint, overlap, event); - } - - - function isSelectionRangeAllowed(range) { - return isRangeAllowed(range, options.selectConstraint, options.selectOverlap); - } - - - // when `eventProps` is defined, consider this an event. - // `eventProps` can contain misc non-date-related info about the event. - function isExternalDropRangeAllowed(range, eventProps) { - var eventInput; - var event; - - // note: very similar logic is in View's reportExternalDrop - if (eventProps) { - eventInput = $.extend({}, eventProps, range); - event = expandEvent(buildEventFromInput(eventInput))[0]; - } - - if (event) { - return isEventRangeAllowed(range, event); - } - else { // treat it as a selection - - range = ensureVisibleEventRange(range); // ensure a proper range with an end for isSelectionRangeAllowed - - return isSelectionRangeAllowed(range); - } - } - - - // Returns true if the given range (caused by an event drop/resize or a selection) is allowed to exist - // according to the constraint/overlap settings. - // `event` is not required if checking a selection. - function isRangeAllowed(range, constraint, overlap, event) { - var constraintEvents; - var anyContainment; - var peerEvents; - var i, peerEvent; - var peerOverlap; - - // normalize. fyi, we're normalizing in too many places :( - range = $.extend({}, range); // copy all properties in case there are misc non-date properties - range.start = range.start.clone().stripZone(); - range.end = range.end.clone().stripZone(); - - // the range must be fully contained by at least one of produced constraint events - if (constraint != null) { - - // not treated as an event! intermediate data structure - // TODO: use ranges in the future - constraintEvents = constraintToEvents(constraint); - - anyContainment = false; - for (i = 0; i < constraintEvents.length; i++) { - if (eventContainsRange(constraintEvents[i], range)) { - anyContainment = true; - break; - } - } - - if (!anyContainment) { - return false; - } - } - - peerEvents = t.getPeerEvents(event, range); - - for (i = 0; i < peerEvents.length; i++) { - peerEvent = peerEvents[i]; - - // there needs to be an actual intersection before disallowing anything - if (eventIntersectsRange(peerEvent, range)) { - - // evaluate overlap for the given range and short-circuit if necessary - if (overlap === false) { - return false; - } - // if the event's overlap is a test function, pass the peer event in question as the first param - else if (typeof overlap === 'function' && !overlap(peerEvent, event)) { - return false; - } - - // if we are computing if the given range is allowable for an event, consider the other event's - // EventObject-specific or Source-specific `overlap` property - if (event) { - peerOverlap = firstDefined( - peerEvent.overlap, - (peerEvent.source || {}).overlap - // we already considered the global `eventOverlap` - ); - if (peerOverlap === false) { - return false; - } - // if the peer event's overlap is a test function, pass the subject event as the first param - if (typeof peerOverlap === 'function' && !peerOverlap(event, peerEvent)) { - return false; - } - } - } - } - - return true; - } - - - // Given an event input from the API, produces an array of event objects. Possible event inputs: - // 'businessHours' - // An event ID (number or string) - // An object with specific start/end dates or a recurring event (like what businessHours accepts) - function constraintToEvents(constraintInput) { - - if (constraintInput === 'businessHours') { - return getBusinessHoursEvents(); - } - - if (typeof constraintInput === 'object') { - return expandEvent(buildEventFromInput(constraintInput)); - } - - return clientEvents(constraintInput); // probably an ID - } - - - // Does the event's date range fully contain the given range? - // start/end already assumed to have stripped zones :( - function eventContainsRange(event, range) { - var eventStart = event.start.clone().stripZone(); - var eventEnd = t.getEventEnd(event).stripZone(); - - return range.start >= eventStart && range.end <= eventEnd; - } - - - // Does the event's date range intersect with the given range? - // start/end already assumed to have stripped zones :( - function eventIntersectsRange(event, range) { - var eventStart = event.start.clone().stripZone(); - var eventEnd = t.getEventEnd(event).stripZone(); - - return range.start < eventEnd && range.end > eventStart; - } - - - t.getEventCache = function() { - return cache; - }; - } +// returns an undo function +Calendar.prototype.mutateSeg = function(seg, newProps) { + return this.mutateEvent(seg.event, newProps); +}; + + +// hook for external libs to manipulate event properties upon creation. +// should manipulate the event in-place. +Calendar.prototype.normalizeEvent = function(event) { +}; + + +// Does the given span (start, end, and other location information) +// fully contain the other? +Calendar.prototype.spanContainsSpan = function(outerSpan, innerSpan) { + var eventStart = outerSpan.start.clone().stripZone(); + var eventEnd = this.getEventEnd(outerSpan).stripZone(); + + return innerSpan.start >= eventStart && innerSpan.end <= eventEnd; +}; + + // Returns a list of events that the given event should be compared against when being considered for a move to -// the specified range. Attached to the Calendar's prototype because EventManager is a mixin for a Calendar. -Calendar.prototype.getPeerEvents = function(event, range) { +// the specified span. Attached to the Calendar's prototype because EventManager is a mixin for a Calendar. +Calendar.prototype.getPeerEvents = function(span, event) { var cache = this.getEventCache(); var peerEvents = []; var i, otherEvent; @@ -10081,6 +13958,239 @@ function backupEventDates(event) { event._end = event.end ? event.end.clone() : null; } + +/* Overlapping / Constraining +-----------------------------------------------------------------------------------------*/ + + +// Determines if the given event can be relocated to the given span (unzoned start/end with other misc data) +Calendar.prototype.isEventSpanAllowed = function(span, event) { + var source = event.source || {}; + var eventAllowFunc = this.opt('eventAllow'); + + var constraint = firstDefined( + event.constraint, + source.constraint, + this.opt('eventConstraint') + ); + + var overlap = firstDefined( + event.overlap, + source.overlap, + this.opt('eventOverlap') + ); + + return this.isSpanAllowed(span, constraint, overlap, event) && + (!eventAllowFunc || eventAllowFunc(span, event) !== false); +}; + + +// Determines if an external event can be relocated to the given span (unzoned start/end with other misc data) +Calendar.prototype.isExternalSpanAllowed = function(eventSpan, eventLocation, eventProps) { + var eventInput; + var event; + + // note: very similar logic is in View's reportExternalDrop + if (eventProps) { + eventInput = $.extend({}, eventProps, eventLocation); + event = this.expandEvent( + this.buildEventFromInput(eventInput) + )[0]; + } + + if (event) { + return this.isEventSpanAllowed(eventSpan, event); + } + else { // treat it as a selection + + return this.isSelectionSpanAllowed(eventSpan); + } +}; + + +// Determines the given span (unzoned start/end with other misc data) can be selected. +Calendar.prototype.isSelectionSpanAllowed = function(span) { + var selectAllowFunc = this.opt('selectAllow'); + + return this.isSpanAllowed(span, this.opt('selectConstraint'), this.opt('selectOverlap')) && + (!selectAllowFunc || selectAllowFunc(span) !== false); +}; + + +// Returns true if the given span (caused by an event drop/resize or a selection) is allowed to exist +// according to the constraint/overlap settings. +// `event` is not required if checking a selection. +Calendar.prototype.isSpanAllowed = function(span, constraint, overlap, event) { + var constraintEvents; + var anyContainment; + var peerEvents; + var i, peerEvent; + var peerOverlap; + + // the range must be fully contained by at least one of produced constraint events + if (constraint != null) { + + // not treated as an event! intermediate data structure + // TODO: use ranges in the future + constraintEvents = this.constraintToEvents(constraint); + if (constraintEvents) { // not invalid + + anyContainment = false; + for (i = 0; i < constraintEvents.length; i++) { + if (this.spanContainsSpan(constraintEvents[i], span)) { + anyContainment = true; + break; + } + } + + if (!anyContainment) { + return false; + } + } + } + + peerEvents = this.getPeerEvents(span, event); + + for (i = 0; i < peerEvents.length; i++) { + peerEvent = peerEvents[i]; + + // there needs to be an actual intersection before disallowing anything + if (this.eventIntersectsRange(peerEvent, span)) { + + // evaluate overlap for the given range and short-circuit if necessary + if (overlap === false) { + return false; + } + // if the event's overlap is a test function, pass the peer event in question as the first param + else if (typeof overlap === 'function' && !overlap(peerEvent, event)) { + return false; + } + + // if we are computing if the given range is allowable for an event, consider the other event's + // EventObject-specific or Source-specific `overlap` property + if (event) { + peerOverlap = firstDefined( + peerEvent.overlap, + (peerEvent.source || {}).overlap + // we already considered the global `eventOverlap` + ); + if (peerOverlap === false) { + return false; + } + // if the peer event's overlap is a test function, pass the subject event as the first param + if (typeof peerOverlap === 'function' && !peerOverlap(event, peerEvent)) { + return false; + } + } + } + } + + return true; +}; + + +// Given an event input from the API, produces an array of event objects. Possible event inputs: +// 'businessHours' +// An event ID (number or string) +// An object with specific start/end dates or a recurring event (like what businessHours accepts) +Calendar.prototype.constraintToEvents = function(constraintInput) { + + if (constraintInput === 'businessHours') { + return this.getCurrentBusinessHourEvents(); + } + + if (typeof constraintInput === 'object') { + if (constraintInput.start != null) { // needs to be event-like input + return this.expandEvent(this.buildEventFromInput(constraintInput)); + } + else { + return null; // invalid + } + } + + return this.clientEvents(constraintInput); // probably an ID +}; + + +// Does the event's date range intersect with the given range? +// start/end already assumed to have stripped zones :( +Calendar.prototype.eventIntersectsRange = function(event, range) { + var eventStart = event.start.clone().stripZone(); + var eventEnd = this.getEventEnd(event).stripZone(); + + return range.start < eventEnd && range.end > eventStart; +}; + + +/* Business Hours +-----------------------------------------------------------------------------------------*/ + +var BUSINESS_HOUR_EVENT_DEFAULTS = { + id: '_fcBusinessHours', // will relate events from different calls to expandEvent + start: '09:00', + end: '17:00', + dow: [ 1, 2, 3, 4, 5 ], // monday - friday + rendering: 'inverse-background' + // classNames are defined in businessHoursSegClasses +}; + +// Return events objects for business hours within the current view. +// Abuse of our event system :( +Calendar.prototype.getCurrentBusinessHourEvents = function(wholeDay) { + return this.computeBusinessHourEvents(wholeDay, this.opt('businessHours')); +}; + +// Given a raw input value from options, return events objects for business hours within the current view. +Calendar.prototype.computeBusinessHourEvents = function(wholeDay, input) { + if (input === true) { + return this.expandBusinessHourEvents(wholeDay, [ {} ]); + } + else if ($.isPlainObject(input)) { + return this.expandBusinessHourEvents(wholeDay, [ input ]); + } + else if ($.isArray(input)) { + return this.expandBusinessHourEvents(wholeDay, input, true); + } + else { + return []; + } +}; + +// inputs expected to be an array of objects. +// if ignoreNoDow is true, will ignore entries that don't specify a day-of-week (dow) key. +Calendar.prototype.expandBusinessHourEvents = function(wholeDay, inputs, ignoreNoDow) { + var view = this.getView(); + var events = []; + var i, input; + + for (i = 0; i < inputs.length; i++) { + input = inputs[i]; + + if (ignoreNoDow && !input.dow) { + continue; + } + + // give defaults. will make a copy + input = $.extend({}, BUSINESS_HOUR_EVENT_DEFAULTS, input); + + // if a whole-day series is requested, clear the start/end times + if (wholeDay) { + input.start = null; + input.end = null; + } + + events.push.apply(events, // append + this.expandEvent( + this.buildEventFromInput(input), + view.activeRange.start, + view.activeRange.end + ) + ); + } + + return events; +}; + ;; /* An abstract class for the "basic" views, as well as month view. Renders one or more rows of day cells. @@ -10088,77 +14198,109 @@ function backupEventDates(event) { // It is a manager for a DayGrid subcomponent, which does most of the heavy lifting. // It is responsible for managing width/height. -var BasicView = View.extend({ +var BasicView = FC.BasicView = View.extend({ + scroller: null, + + dayGridClass: DayGrid, // class the dayGrid will be instantiated from (overridable by subclasses) dayGrid: null, // the main subcomponent that does most of the heavy lifting dayNumbersVisible: false, // display day numbers on each day cell? - weekNumbersVisible: false, // display week numbers along the side? + colWeekNumbersVisible: false, // display week numbers along the side? + cellWeekNumbersVisible: false, // display week numbers in day cell? weekNumberWidth: null, // width of all the week-number cells running down the side + headContainerEl: null, // div that hold's the dayGrid's rendered date header headRowEl: null, // the fake row element of the day-of-week header initialize: function() { - this.dayGrid = new DayGrid(this); - this.coordMap = this.dayGrid.coordMap; // the view's date-to-cell mapping is identical to the subcomponent's + this.dayGrid = this.instantiateDayGrid(); + + this.scroller = new Scroller({ + overflowX: 'hidden', + overflowY: 'auto' + }); }, - // Sets the display range and computes all necessary dates - setRange: function(range) { - View.prototype.setRange.call(this, range); // call the super-method + // Generates the DayGrid object this view needs. Draws from this.dayGridClass + instantiateDayGrid: function() { + // generate a subclass on the fly with BasicView-specific behavior + // TODO: cache this subclass + var subclass = this.dayGridClass.extend(basicDayGridMethods); - this.dayGrid.breakOnWeeks = /year|month|week/.test(this.intervalUnit); // do before setRange - this.dayGrid.setRange(range); + return new subclass(this); }, - // Compute the value to feed into setRange. Overrides superclass. - computeRange: function(date) { - var range = View.prototype.computeRange.call(this, date); // get value from the super-method + // Computes the date range that will be rendered. + buildRenderRange: function(currentRange, currentRangeUnit) { + var renderRange = View.prototype.buildRenderRange.apply(this, arguments); // year and month views should be aligned with weeks. this is already done for week - if (/year|month/.test(range.intervalUnit)) { - range.start.startOf('week'); - range.start = this.skipHiddenDays(range.start); + if (/^(year|month)$/.test(currentRangeUnit)) { + renderRange.start.startOf('week'); // make end-of-week if not already - if (range.end.weekday()) { - range.end.add(1, 'week').startOf('week'); - range.end = this.skipHiddenDays(range.end, -1, true); // exclusively move backwards + if (renderRange.end.weekday()) { + renderRange.end.add(1, 'week').startOf('week'); // exclusively move backwards } } - return range; + return this.trimHiddenDays(renderRange); }, // Renders the view into `this.el`, which should already be assigned renderDates: function() { + this.dayGrid.breakOnWeeks = /year|month|week/.test(this.currentRangeUnit); // do before Grid::setRange + this.dayGrid.setRange(this.renderRange); + this.dayNumbersVisible = this.dayGrid.rowCnt > 1; // TODO: make grid responsible - this.weekNumbersVisible = this.opt('weekNumbers'); - this.dayGrid.numbersVisible = this.dayNumbersVisible || this.weekNumbersVisible; + if (this.opt('weekNumbers')) { + if (this.opt('weekNumbersWithinDays')) { + this.cellWeekNumbersVisible = true; + this.colWeekNumbersVisible = false; + } + else { + this.cellWeekNumbersVisible = false; + this.colWeekNumbersVisible = true; + }; + } + this.dayGrid.numbersVisible = this.dayNumbersVisible || + this.cellWeekNumbersVisible || this.colWeekNumbersVisible; - this.el.addClass('fc-basic-view').html(this.renderHtml()); + this.el.addClass('fc-basic-view').html(this.renderSkeletonHtml()); + this.renderHead(); - this.headRowEl = this.el.find('thead .fc-row'); + this.scroller.render(); + var dayGridContainerEl = this.scroller.el.addClass('fc-day-grid-container'); + var dayGridEl = $('
').appendTo(dayGridContainerEl); + this.el.find('.fc-body > tr > td').append(dayGridContainerEl); - this.scrollerEl = this.el.find('.fc-day-grid-container'); - this.dayGrid.coordMap.containerEl = this.scrollerEl; // constrain clicks/etc to the dimensions of the scroller - - this.dayGrid.setElement(this.el.find('.fc-day-grid')); + this.dayGrid.setElement(dayGridEl); this.dayGrid.renderDates(this.hasRigidRows()); }, + // render the day-of-week headers + renderHead: function() { + this.headContainerEl = + this.el.find('.fc-head-container') + .html(this.dayGrid.renderHeadHtml()); + this.headRowEl = this.headContainerEl.find('.fc-row'); + }, + + // Unrenders the content of the view. Since we haven't separated skeleton rendering from date rendering, // always completely kill the dayGrid's rendering. unrenderDates: function() { this.dayGrid.unrenderDates(); this.dayGrid.removeElement(); + this.scroller.destroy(); }, @@ -10167,98 +14309,30 @@ var BasicView = View.extend({ }, + unrenderBusinessHours: function() { + this.dayGrid.unrenderBusinessHours(); + }, + + // Builds the HTML skeleton for the view. // The day-grid component will render inside of a container defined by this HTML. - renderHtml: function() { + renderSkeletonHtml: function() { return '' + '' + '' + '' + - '' + + '' + '' + '' + '' + '' + - '' + + '' + '' + '' + '
' + - this.dayGrid.headHtml() + // render the day-of-week headers - '
' + - '
' + - '
' + - '
' + - '
'; }, - // Generates the HTML that will go before the day-of week header cells. - // Queried by the DayGrid subcomponent when generating rows. Ordering depends on isRTL. - headIntroHtml: function() { - if (this.weekNumbersVisible) { - return '' + - '' + - '' + // needed for matchCellWidths - htmlEscape(this.opt('weekNumberTitle')) + - '' + - ''; - } - }, - - - // Generates the HTML that will go before content-skeleton cells that display the day/week numbers. - // Queried by the DayGrid subcomponent. Ordering depends on isRTL. - numberIntroHtml: function(row) { - if (this.weekNumbersVisible) { - return '' + - '' + - '' + // needed for matchCellWidths - this.dayGrid.getCell(row, 0).start.format('w') + - '' + - ''; - } - }, - - - // Generates the HTML that goes before the day bg cells for each day-row. - // Queried by the DayGrid subcomponent. Ordering depends on isRTL. - dayIntroHtml: function() { - if (this.weekNumbersVisible) { - return ''; - } - }, - - - // Generates the HTML that goes before every other type of row generated by DayGrid. Ordering depends on isRTL. - // Affects helper-skeleton and highlight-skeleton rows. - introHtml: function() { - if (this.weekNumbersVisible) { - return ''; - } - }, - - - // Generates the HTML for the s of the "number" row in the DayGrid's content skeleton. - // The number row will only exist if either day numbers or week numbers are turned on. - numberCellHtml: function(cell) { - var date = cell.start; - var classes; - - if (!this.dayNumbersVisible) { // if there are week numbers but not day numbers - return ''; // will create an empty space above events :( - } - - classes = this.dayGrid.getDayClasses(date); - classes.unshift('fc-day-number'); - - return '' + - '' + - date.date() + - ''; - }, - - // Generates an HTML attribute string for setting the width of the week number column, if it is known weekNumberStyleAttr: function() { if (this.weekNumberWidth !== null) { @@ -10281,7 +14355,7 @@ var BasicView = View.extend({ // Refreshes the horizontal dimensions of the view updateWidth: function() { - if (this.weekNumbersVisible) { + if (this.colWeekNumbersVisible) { // Make sure all week number cells running down the side have the same width. // Record the width for cells created later. this.weekNumberWidth = matchCellWidths( @@ -10295,9 +14369,10 @@ var BasicView = View.extend({ setHeight: function(totalHeight, isAuto) { var eventLimit = this.opt('eventLimit'); var scrollerHeight; + var scrollbarWidths; // reset all heights to be natural - unsetScroller(this.scrollerEl); + this.scroller.clear(); uncompensateScroll(this.headRowEl); this.dayGrid.removeSegPopover(); // kill the "more" popover if displayed @@ -10307,6 +14382,8 @@ var BasicView = View.extend({ this.dayGrid.limitRows(eventLimit); // limit the levels first so the height can redistribute after } + // distribute the height to the rows + // (totalHeight is a "recommended" value if isAuto) scrollerHeight = this.computeScrollerHeight(totalHeight); this.setGridHeight(scrollerHeight, isAuto); @@ -10315,17 +14392,33 @@ var BasicView = View.extend({ this.dayGrid.limitRows(eventLimit); // limit the levels after the grid's row heights have been set } - if (!isAuto && setPotentialScroller(this.scrollerEl, scrollerHeight)) { // using scrollbars? + if (!isAuto) { // should we force dimensions of the scroll container? - compensateScroll(this.headRowEl, getScrollbarWidths(this.scrollerEl)); + this.scroller.setHeight(scrollerHeight); + scrollbarWidths = this.scroller.getScrollbarWidths(); - // doing the scrollbar compensation might have created text overflow which created more height. redo - scrollerHeight = this.computeScrollerHeight(totalHeight); - this.scrollerEl.height(scrollerHeight); + if (scrollbarWidths.left || scrollbarWidths.right) { // using scrollbars? + + compensateScroll(this.headRowEl, scrollbarWidths); + + // doing the scrollbar compensation might have created text overflow which created more height. redo + scrollerHeight = this.computeScrollerHeight(totalHeight); + this.scroller.setHeight(scrollerHeight); + } + + // guarantees the same scrollbar widths + this.scroller.lockOverflow(scrollbarWidths); } }, + // given a desired total height of the view, returns what the height of the scroller should be + computeScrollerHeight: function(totalHeight) { + return totalHeight - + subtractInnerElHeight(this.el, this.scroller.el); // everything that's NOT the scroller + }, + + // Sets the height of just the DayGrid component in this view setGridHeight: function(height, isAuto) { if (isAuto) { @@ -10337,6 +14430,67 @@ var BasicView = View.extend({ }, + /* Scroll + ------------------------------------------------------------------------------------------------------------------*/ + + + computeInitialDateScroll: function() { + return { top: 0 }; + }, + + + queryDateScroll: function() { + return { top: this.scroller.getScrollTop() }; + }, + + + applyDateScroll: function(scroll) { + if (scroll.top !== undefined) { + this.scroller.setScrollTop(scroll.top); + } + }, + + + /* Hit Areas + ------------------------------------------------------------------------------------------------------------------*/ + // forward all hit-related method calls to dayGrid + + + hitsNeeded: function() { + this.dayGrid.hitsNeeded(); + }, + + + hitsNotNeeded: function() { + this.dayGrid.hitsNotNeeded(); + }, + + + prepareHits: function() { + this.dayGrid.prepareHits(); + }, + + + releaseHits: function() { + this.dayGrid.releaseHits(); + }, + + + queryHit: function(left, top) { + return this.dayGrid.queryHit(left, top); + }, + + + getHitSpan: function(hit) { + return this.dayGrid.getHitSpan(hit); + }, + + + getHitEl: function(hit) { + return this.dayGrid.getHitEl(hit); + }, + + /* Events ------------------------------------------------------------------------------------------------------------------*/ @@ -10359,9 +14513,8 @@ var BasicView = View.extend({ unrenderEvents: function() { this.dayGrid.unrenderEvents(); - // we DON'T need to call updateHeight() because: - // A) a renderEvents() call always happens after this, which will eventually call updateHeight() - // B) in IE8, this causes a flash whenever events are rerendered + // we DON'T need to call updateHeight() because + // a renderEvents() call always happens after this, which will eventually call updateHeight() }, @@ -10385,8 +14538,8 @@ var BasicView = View.extend({ // Renders a visual indication of a selection - renderSelection: function(range) { - this.dayGrid.renderSelection(range); + renderSelection: function(span) { + this.dayGrid.renderSelection(span); }, @@ -10397,33 +14550,102 @@ var BasicView = View.extend({ }); + +// Methods that will customize the rendering behavior of the BasicView's dayGrid +var basicDayGridMethods = { + + + // Generates the HTML that will go before the day-of week header cells + renderHeadIntroHtml: function() { + var view = this.view; + + if (view.colWeekNumbersVisible) { + return '' + + '' + + '' + // needed for matchCellWidths + htmlEscape(view.opt('weekNumberTitle')) + + '' + + ''; + } + + return ''; + }, + + + // Generates the HTML that will go before content-skeleton cells that display the day/week numbers + renderNumberIntroHtml: function(row) { + var view = this.view; + var weekStart = this.getCellDate(row, 0); + + if (view.colWeekNumbersVisible) { + return '' + + '' + + view.buildGotoAnchorHtml( // aside from link, important for matchCellWidths + { date: weekStart, type: 'week', forceOff: this.colCnt === 1 }, + weekStart.format('w') // inner HTML + ) + + ''; + } + + return ''; + }, + + + // Generates the HTML that goes before the day bg cells for each day-row + renderBgIntroHtml: function() { + var view = this.view; + + if (view.colWeekNumbersVisible) { + return ''; + } + + return ''; + }, + + + // Generates the HTML that goes before every other type of row generated by DayGrid. + // Affects helper-skeleton and highlight-skeleton rows. + renderIntroHtml: function() { + var view = this.view; + + if (view.colWeekNumbersVisible) { + return ''; + } + + return ''; + } + +}; + ;; /* A month view with day cells running in rows (one-per-week) and columns ----------------------------------------------------------------------------------------------------------------------*/ -var MonthView = BasicView.extend({ +var MonthView = FC.MonthView = BasicView.extend({ - // Produces information about what range to display - computeRange: function(date) { - var range = BasicView.prototype.computeRange.call(this, date); // get value from super-method + + // Computes the date range that will be rendered. + buildRenderRange: function() { + var renderRange = BasicView.prototype.buildRenderRange.apply(this, arguments); var rowCnt; // ensure 6 weeks if (this.isFixedWeeks()) { - rowCnt = Math.ceil(range.end.diff(range.start, 'weeks', true)); // could be partial weeks due to hiddenDays - range.end.add(6 - rowCnt, 'weeks'); + rowCnt = Math.ceil( // could be partial weeks due to hiddenDays + renderRange.end.diff(renderRange.start, 'weeks', true) // dontRound=true + ); + renderRange.end.add(6 - rowCnt, 'weeks'); } - return range; + return renderRange; }, // Overrides the default BasicView behavior to have special multi-week auto-height logic setGridHeight: function(height, isAuto) { - isAuto = isAuto || this.opt('weekMode') === 'variable'; // LEGACY: weekMode is deprecated - // if auto, make the height of each row the height that it would be if there were 6 weeks if (isAuto) { height *= this.rowCnt / 6; @@ -10434,11 +14656,6 @@ var MonthView = BasicView.extend({ isFixedWeeks: function() { - var weekMode = this.opt('weekMode'); // LEGACY: weekMode is deprecated - if (weekMode) { - return weekMode === 'fixed'; // if any other type of weekMode, assume NOT fixed - } - return this.opt('fixedWeekCount'); } @@ -10474,35 +14691,55 @@ fcViews.month = { // Is a manager for the TimeGrid subcomponent and possibly the DayGrid subcomponent (if allDaySlot is on). // Responsible for managing width/height. -var AgendaView = View.extend({ +var AgendaView = FC.AgendaView = View.extend({ + scroller: null, + + timeGridClass: TimeGrid, // class used to instantiate the timeGrid. subclasses can override timeGrid: null, // the main time-grid subcomponent of this view + + dayGridClass: DayGrid, // class used to instantiate the dayGrid. subclasses can override dayGrid: null, // the "all-day" subcomponent. if all-day is turned off, this will be null axisWidth: null, // the width of the time axis running down the side - noScrollRowEls: null, // set of fake row elements that must compensate when scrollerEl has scrollbars + headContainerEl: null, // div that hold's the timeGrid's rendered date header + noScrollRowEls: null, // set of fake row elements that must compensate when scroller has scrollbars // when the time-grid isn't tall enough to occupy the given height, we render an
underneath bottomRuleEl: null, - bottomRuleHeight: null, + + // indicates that minTime/maxTime affects rendering + usesMinMaxTime: true, initialize: function() { - this.timeGrid = new TimeGrid(this); + this.timeGrid = this.instantiateTimeGrid(); if (this.opt('allDaySlot')) { // should we display the "all-day" area? - this.dayGrid = new DayGrid(this); // the all-day subcomponent of this view + this.dayGrid = this.instantiateDayGrid(); // the all-day subcomponent of this view + } - // the coordinate grid will be a combination of both subcomponents' grids - this.coordMap = new ComboCoordMap([ - this.dayGrid.coordMap, - this.timeGrid.coordMap - ]); - } - else { - this.coordMap = this.timeGrid.coordMap; - } + this.scroller = new Scroller({ + overflowX: 'hidden', + overflowY: 'auto' + }); + }, + + + // Instantiates the TimeGrid object this view needs. Draws from this.timeGridClass + instantiateTimeGrid: function() { + var subclass = this.timeGridClass.extend(agendaTimeGridMethods); + + return new subclass(this); + }, + + + // Instantiates the DayGrid object this view might need. Draws from this.dayGridClass + instantiateDayGrid: function() { + var subclass = this.dayGridClass.extend(agendaDayGridMethods); + + return new subclass(this); }, @@ -10510,27 +14747,24 @@ var AgendaView = View.extend({ ------------------------------------------------------------------------------------------------------------------*/ - // Sets the display range and computes all necessary dates - setRange: function(range) { - View.prototype.setRange.call(this, range); // call the super-method - - this.timeGrid.setRange(range); - if (this.dayGrid) { - this.dayGrid.setRange(range); - } - }, - - // Renders the view into `this.el`, which has already been assigned renderDates: function() { - this.el.addClass('fc-agenda-view').html(this.renderHtml()); + this.timeGrid.setRange(this.renderRange); - // the element that wraps the time-grid that will probably scroll - this.scrollerEl = this.el.find('.fc-time-grid-container'); - this.timeGrid.coordMap.containerEl = this.scrollerEl; // don't accept clicks/etc outside of this + if (this.dayGrid) { + this.dayGrid.setRange(this.renderRange); + } - this.timeGrid.setElement(this.el.find('.fc-time-grid')); + this.el.addClass('fc-agenda-view').html(this.renderSkeletonHtml()); + this.renderHead(); + + this.scroller.render(); + var timeGridWrapEl = this.scroller.el.addClass('fc-time-grid-container'); + var timeGridEl = $('
').appendTo(timeGridWrapEl); + this.el.find('.fc-body > tr > td').append(timeGridWrapEl); + + this.timeGrid.setElement(timeGridEl); this.timeGrid.renderDates(); // the
that sometimes displays under the time-grid @@ -10549,6 +14783,14 @@ var AgendaView = View.extend({ }, + // render the day-of-week headers + renderHead: function() { + this.headContainerEl = + this.el.find('.fc-head-container') + .html(this.timeGrid.renderHeadHtml()); + }, + + // Unrenders the content of the view. Since we haven't separated skeleton rendering from date rendering, // always completely kill each grid's rendering. unrenderDates: function() { @@ -10559,9 +14801,49 @@ var AgendaView = View.extend({ this.dayGrid.unrenderDates(); this.dayGrid.removeElement(); } + + this.scroller.destroy(); }, + // Builds the HTML skeleton for the view. + // The day-grid and time-grid components will render inside containers defined by this HTML. + renderSkeletonHtml: function() { + return '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + (this.dayGrid ? + '
' + + '
' : + '' + ) + + '
'; + }, + + + // Generates an HTML attribute string for setting the width of the axis, if it is known + axisStyleAttr: function() { + if (this.axisWidth !== null) { + return 'style="width:' + this.axisWidth + 'px"'; + } + return ''; + }, + + + /* Business Hours + ------------------------------------------------------------------------------------------------------------------*/ + + renderBusinessHours: function() { this.timeGrid.renderBusinessHours(); @@ -10571,91 +14853,31 @@ var AgendaView = View.extend({ }, - // Builds the HTML skeleton for the view. - // The day-grid and time-grid components will render inside containers defined by this HTML. - renderHtml: function() { - return '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '
' + - this.timeGrid.headHtml() + // render the day-of-week headers - '
' + - (this.dayGrid ? - '
' + - '
' : - '' - ) + - '
' + - '
' + - '
' + - '
'; - }, + unrenderBusinessHours: function() { + this.timeGrid.unrenderBusinessHours(); - - // Generates the HTML that will go before the day-of week header cells. - // Queried by the TimeGrid subcomponent when generating rows. Ordering depends on isRTL. - headIntroHtml: function() { - var date; - var weekText; - - if (this.opt('weekNumbers')) { - date = this.timeGrid.getCell(0).start; - weekText = date.format(this.opt('smallWeekFormat')); - - return '' + - '' + - '' + // needed for matchCellWidths - htmlEscape(weekText) + - '' + - ''; - } - else { - return ''; + if (this.dayGrid) { + this.dayGrid.unrenderBusinessHours(); } }, - // Generates the HTML that goes before the all-day cells. - // Queried by the DayGrid subcomponent when generating rows. Ordering depends on isRTL. - dayIntroHtml: function() { - return '' + - '' + - '' + // needed for matchCellWidths - (this.opt('allDayHtml') || htmlEscape(this.opt('allDayText'))) + - '' + - ''; + /* Now Indicator + ------------------------------------------------------------------------------------------------------------------*/ + + + getNowIndicatorUnit: function() { + return this.timeGrid.getNowIndicatorUnit(); }, - // Generates the HTML that goes before the bg of the TimeGrid slot area. Long vertical column. - slotBgIntroHtml: function() { - return ''; + renderNowIndicator: function(date) { + this.timeGrid.renderNowIndicator(date); }, - // Generates the HTML that goes before all other types of cells. - // Affects content-skeleton, helper-skeleton, highlight-skeleton for both the time-grid and day-grid. - // Queried by the TimeGrid and DayGrid subcomponents when generating rows. Ordering depends on isRTL. - introHtml: function() { - return ''; - }, - - - // Generates an HTML attribute string for setting the width of the axis, if it is known - axisStyleAttr: function() { - if (this.axisWidth !== null) { - return 'style="width:' + this.axisWidth + 'px"'; - } - return ''; + unrenderNowIndicator: function() { + this.timeGrid.unrenderNowIndicator(); }, @@ -10681,16 +14903,11 @@ var AgendaView = View.extend({ setHeight: function(totalHeight, isAuto) { var eventLimit; var scrollerHeight; - - if (this.bottomRuleHeight === null) { - // calculate the height of the rule the very first time - this.bottomRuleHeight = this.bottomRuleEl.outerHeight(); - } - this.bottomRuleEl.hide(); // .show() will be called later if this
is necessary + var scrollbarWidths; // reset all dimensions back to the original state - this.scrollerEl.css('overflow', ''); - unsetScroller(this.scrollerEl); + this.bottomRuleEl.hide(); // .show() will be called later if this
is necessary + this.scroller.clear(); // sets height to 'auto' and clears overflow uncompensateScroll(this.noScrollRowEls); // limit number of events in the all-day area @@ -10706,30 +14923,48 @@ var AgendaView = View.extend({ } } - if (!isAuto) { // should we force dimensions of the scroll container, or let the contents be natural height? + if (!isAuto) { // should we force dimensions of the scroll container? scrollerHeight = this.computeScrollerHeight(totalHeight); - if (setPotentialScroller(this.scrollerEl, scrollerHeight)) { // using scrollbars? + this.scroller.setHeight(scrollerHeight); + scrollbarWidths = this.scroller.getScrollbarWidths(); + + if (scrollbarWidths.left || scrollbarWidths.right) { // using scrollbars? // make the all-day and header rows lines up - compensateScroll(this.noScrollRowEls, getScrollbarWidths(this.scrollerEl)); + compensateScroll(this.noScrollRowEls, scrollbarWidths); // the scrollbar compensation might have changed text flow, which might affect height, so recalculate // and reapply the desired height to the scroller. scrollerHeight = this.computeScrollerHeight(totalHeight); - this.scrollerEl.height(scrollerHeight); + this.scroller.setHeight(scrollerHeight); } - else { // no scrollbars - // still, force a height and display the bottom rule (marks the end of day) - this.scrollerEl.height(scrollerHeight).css('overflow', 'hidden'); // in case
goes outside + + // guarantees the same scrollbar widths + this.scroller.lockOverflow(scrollbarWidths); + + // if there's any space below the slats, show the horizontal rule. + // this won't cause any new overflow, because lockOverflow already called. + if (this.timeGrid.getTotalSlatHeight() < scrollerHeight) { this.bottomRuleEl.show(); } } }, + // given a desired total height of the view, returns what the height of the scroller should be + computeScrollerHeight: function(totalHeight) { + return totalHeight - + subtractInnerElHeight(this.el, this.scroller.el); // everything that's NOT the scroller + }, + + + /* Scroll + ------------------------------------------------------------------------------------------------------------------*/ + + // Computes the initial pre-configured scroll state prior to allowing the user to change it - computeInitialScroll: function() { + computeInitialDateScroll: function() { var scrollTime = moment.duration(this.opt('scrollTime')); var top = this.timeGrid.computeTimeTop(scrollTime); @@ -10740,7 +14975,79 @@ var AgendaView = View.extend({ top++; // to overcome top border that slots beyond the first have. looks better } - return top; + return { top: top }; + }, + + + queryDateScroll: function() { + return { top: this.scroller.getScrollTop() }; + }, + + + applyDateScroll: function(scroll) { + if (scroll.top !== undefined) { + this.scroller.setScrollTop(scroll.top); + } + }, + + + /* Hit Areas + ------------------------------------------------------------------------------------------------------------------*/ + // forward all hit-related method calls to the grids (dayGrid might not be defined) + + + hitsNeeded: function() { + this.timeGrid.hitsNeeded(); + if (this.dayGrid) { + this.dayGrid.hitsNeeded(); + } + }, + + + hitsNotNeeded: function() { + this.timeGrid.hitsNotNeeded(); + if (this.dayGrid) { + this.dayGrid.hitsNotNeeded(); + } + }, + + + prepareHits: function() { + this.timeGrid.prepareHits(); + if (this.dayGrid) { + this.dayGrid.prepareHits(); + } + }, + + + releaseHits: function() { + this.timeGrid.releaseHits(); + if (this.dayGrid) { + this.dayGrid.releaseHits(); + } + }, + + + queryHit: function(left, top) { + var hit = this.timeGrid.queryHit(left, top); + + if (!hit && this.dayGrid) { + hit = this.dayGrid.queryHit(left, top); + } + + return hit; + }, + + + getHitSpan: function(hit) { + // TODO: hit.component is set as a hack to identify where the hit came from + return hit.component.getHitSpan(hit); + }, + + + getHitEl: function(hit) { + // TODO: hit.component is set as a hack to identify where the hit came from + return hit.component.getHitEl(hit); }, @@ -10794,9 +15101,8 @@ var AgendaView = View.extend({ this.dayGrid.unrenderEvents(); } - // we DON'T need to call updateHeight() because: - // A) a renderEvents() call always happens after this, which will eventually call updateHeight() - // B) in IE8, this causes a flash whenever events are rerendered + // we DON'T need to call updateHeight() because + // a renderEvents() call always happens after this, which will eventually call updateHeight() }, @@ -10828,12 +15134,12 @@ var AgendaView = View.extend({ // Renders a visual indication of a selection - renderSelection: function(range) { - if (range.start.hasTime() || range.end.hasTime()) { - this.timeGrid.renderSelection(range); + renderSelection: function(span) { + if (span.start.hasTime() || span.end.hasTime()) { + this.timeGrid.renderSelection(span); } else if (this.dayGrid) { - this.dayGrid.renderSelection(range); + this.dayGrid.renderSelection(span); } }, @@ -10848,18 +15154,99 @@ var AgendaView = View.extend({ }); + +// Methods that will customize the rendering behavior of the AgendaView's timeGrid +// TODO: move into TimeGrid +var agendaTimeGridMethods = { + + + // Generates the HTML that will go before the day-of week header cells + renderHeadIntroHtml: function() { + var view = this.view; + var weekText; + + if (view.opt('weekNumbers')) { + weekText = this.start.format(view.opt('smallWeekFormat')); + + return '' + + '' + + view.buildGotoAnchorHtml( // aside from link, important for matchCellWidths + { date: this.start, type: 'week', forceOff: this.colCnt > 1 }, + htmlEscape(weekText) // inner HTML + ) + + ''; + } + else { + return ''; + } + }, + + + // Generates the HTML that goes before the bg of the TimeGrid slot area. Long vertical column. + renderBgIntroHtml: function() { + var view = this.view; + + return ''; + }, + + + // Generates the HTML that goes before all other types of cells. + // Affects content-skeleton, helper-skeleton, highlight-skeleton for both the time-grid and day-grid. + renderIntroHtml: function() { + var view = this.view; + + return ''; + } + +}; + + +// Methods that will customize the rendering behavior of the AgendaView's dayGrid +var agendaDayGridMethods = { + + + // Generates the HTML that goes before the all-day cells + renderBgIntroHtml: function() { + var view = this.view; + + return '' + + '' + + '' + // needed for matchCellWidths + view.getAllDayHtml() + + '' + + ''; + }, + + + // Generates the HTML that goes before all other types of cells. + // Affects content-skeleton, helper-skeleton, highlight-skeleton for both the time-grid and day-grid. + renderIntroHtml: function() { + var view = this.view; + + return ''; + } + +}; + ;; var AGENDA_ALL_DAY_EVENT_LIMIT = 5; +// potential nice values for the slot-duration and interval-duration +// from largest to smallest +var AGENDA_STOCK_SUB_DURATIONS = [ + { hours: 1 }, + { minutes: 30 }, + { minutes: 15 }, + { seconds: 30 }, + { seconds: 15 } +]; + fcViews.agenda = { 'class': AgendaView, defaults: { allDaySlot: true, - allDayText: 'all-day', slotDuration: '00:30:00', - minTime: '00:00:00', - maxTime: '24:00:00', slotEventOverlap: true // a bad name. confused with overlap/constraint system } }; @@ -10875,5 +15262,330 @@ fcViews.agendaWeek = { }; ;; -return fc; // export for Node/CommonJS +/* +Responsible for the scroller, and forwarding event-related actions into the "grid" +*/ +var ListView = View.extend({ + + grid: null, + scroller: null, + + initialize: function() { + this.grid = new ListViewGrid(this); + this.scroller = new Scroller({ + overflowX: 'hidden', + overflowY: 'auto' + }); + }, + + renderSkeleton: function() { + this.el.addClass( + 'fc-list-view ' + + this.widgetContentClass + ); + + this.scroller.render(); + this.scroller.el.appendTo(this.el); + + this.grid.setElement(this.scroller.scrollEl); + }, + + unrenderSkeleton: function() { + this.scroller.destroy(); // will remove the Grid too + }, + + setHeight: function(totalHeight, isAuto) { + this.scroller.setHeight(this.computeScrollerHeight(totalHeight)); + }, + + computeScrollerHeight: function(totalHeight) { + return totalHeight - + subtractInnerElHeight(this.el, this.scroller.el); // everything that's NOT the scroller + }, + + renderDates: function() { + this.grid.setRange(this.renderRange); // needs to process range-related options + }, + + renderEvents: function(events) { + this.grid.renderEvents(events); + }, + + unrenderEvents: function() { + this.grid.unrenderEvents(); + }, + + isEventResizable: function(event) { + return false; + }, + + isEventDraggable: function(event) { + return false; + } + +}); + +/* +Responsible for event rendering and user-interaction. +Its "el" is the inner-content of the above view's scroller. +*/ +var ListViewGrid = Grid.extend({ + + segSelector: '.fc-list-item', // which elements accept event actions + hasDayInteractions: false, // no day selection or day clicking + + // slices by day + spanToSegs: function(span) { + var view = this.view; + var dayStart = view.renderRange.start.clone().time(0); // timed, so segs get times! + var dayIndex = 0; + var seg; + var segs = []; + + while (dayStart < view.renderRange.end) { + + seg = intersectRanges(span, { + start: dayStart, + end: dayStart.clone().add(1, 'day') + }); + + if (seg) { + seg.dayIndex = dayIndex; + segs.push(seg); + } + + dayStart.add(1, 'day'); + dayIndex++; + + // detect when span won't go fully into the next day, + // and mutate the latest seg to the be the end. + if ( + seg && !seg.isEnd && span.end.hasTime() && + span.end < dayStart.clone().add(this.view.nextDayThreshold) + ) { + seg.end = span.end.clone(); + seg.isEnd = true; + break; + } + } + + return segs; + }, + + // like "4:00am" + computeEventTimeFormat: function() { + return this.view.opt('mediumTimeFormat'); + }, + + // for events with a url, the whole should be clickable, + // but it's impossible to wrap with an tag. simulate this. + handleSegClick: function(seg, ev) { + var url; + + Grid.prototype.handleSegClick.apply(this, arguments); // super. might prevent the default action + + // not clicking on or within an with an href + if (!$(ev.target).closest('a[href]').length) { + url = seg.event.url; + if (url && !ev.isDefaultPrevented()) { // jsEvent not cancelled in handler + window.location.href = url; // simulate link click + } + } + }, + + // returns list of foreground segs that were actually rendered + renderFgSegs: function(segs) { + segs = this.renderFgSegEls(segs); // might filter away hidden events + + if (!segs.length) { + this.renderEmptyMessage(); + } + else { + this.renderSegList(segs); + } + + return segs; + }, + + renderEmptyMessage: function() { + this.el.html( + '
' + // TODO: try less wraps + '
' + + '
' + + htmlEscape(this.view.opt('noEventsMessage')) + + '
' + + '
' + + '
' + ); + }, + + // render the event segments in the view + renderSegList: function(allSegs) { + var segsByDay = this.groupSegsByDay(allSegs); // sparse array + var dayIndex; + var daySegs; + var i; + var tableEl = $('
'); + var tbodyEl = tableEl.find('tbody'); + + for (dayIndex = 0; dayIndex < segsByDay.length; dayIndex++) { + daySegs = segsByDay[dayIndex]; + if (daySegs) { // sparse array, so might be undefined + + // append a day header + tbodyEl.append(this.dayHeaderHtml( + this.view.renderRange.start.clone().add(dayIndex, 'days') + )); + + this.sortEventSegs(daySegs); + + for (i = 0; i < daySegs.length; i++) { + tbodyEl.append(daySegs[i].el); // append event row + } + } + } + + this.el.empty().append(tableEl); + }, + + // Returns a sparse array of arrays, segs grouped by their dayIndex + groupSegsByDay: function(segs) { + var segsByDay = []; // sparse array + var i, seg; + + for (i = 0; i < segs.length; i++) { + seg = segs[i]; + (segsByDay[seg.dayIndex] || (segsByDay[seg.dayIndex] = [])) + .push(seg); + } + + return segsByDay; + }, + + // generates the HTML for the day headers that live amongst the event rows + dayHeaderHtml: function(dayDate) { + var view = this.view; + var mainFormat = view.opt('listDayFormat'); + var altFormat = view.opt('listDayAltFormat'); + + return '' + + '' + + (mainFormat ? + view.buildGotoAnchorHtml( + dayDate, + { 'class': 'fc-list-heading-main' }, + htmlEscape(dayDate.format(mainFormat)) // inner HTML + ) : + '') + + (altFormat ? + view.buildGotoAnchorHtml( + dayDate, + { 'class': 'fc-list-heading-alt' }, + htmlEscape(dayDate.format(altFormat)) // inner HTML + ) : + '') + + '' + + ''; + }, + + // generates the HTML for a single event row + fgSegHtml: function(seg) { + var view = this.view; + var classes = [ 'fc-list-item' ].concat(this.getSegCustomClasses(seg)); + var bgColor = this.getSegBackgroundColor(seg); + var event = seg.event; + var url = event.url; + var timeHtml; + + if (event.allDay) { + timeHtml = view.getAllDayHtml(); + } + else if (view.isMultiDayEvent(event)) { // if the event appears to span more than one day + if (seg.isStart || seg.isEnd) { // outer segment that probably lasts part of the day + timeHtml = htmlEscape(this.getEventTimeText(seg)); + } + else { // inner segment that lasts the whole day + timeHtml = view.getAllDayHtml(); + } + } + else { + // Display the normal time text for the *event's* times + timeHtml = htmlEscape(this.getEventTimeText(event)); + } + + if (url) { + classes.push('fc-has-url'); + } + + return '' + + (this.displayEventTime ? + '' + + (timeHtml || '') + + '' : + '') + + '' + + '' + + '' + + '' + + '' + + htmlEscape(seg.event.title || '') + + '
' + + '' + + ''; + } + +}); + +;; + +fcViews.list = { + 'class': ListView, + buttonTextKey: 'list', // what to lookup in locale files + defaults: { + buttonText: 'list', // text to display for English + listDayFormat: 'LL', // like "January 1, 2016" + noEventsMessage: 'No events to display' + } +}; + +fcViews.listDay = { + type: 'list', + duration: { days: 1 }, + defaults: { + listDayFormat: 'dddd' // day-of-week is all we need. full date is probably in header + } +}; + +fcViews.listWeek = { + type: 'list', + duration: { weeks: 1 }, + defaults: { + listDayFormat: 'dddd', // day-of-week is more important + listDayAltFormat: 'LL' + } +}; + +fcViews.listMonth = { + type: 'list', + duration: { month: 1 }, + defaults: { + listDayAltFormat: 'dddd' // day-of-week is nice-to-have + } +}; + +fcViews.listYear = { + type: 'list', + duration: { year: 1 }, + defaults: { + listDayAltFormat: 'dddd' // day-of-week is nice-to-have + } +}; + +;; + +return FC; // export for Node/CommonJS }); \ No newline at end of file diff --git a/src/UI/Shared/Modal/ModalController.js b/src/UI/Shared/Modal/ModalController.js index f373f92d9..173399ade 100644 --- a/src/UI/Shared/Modal/ModalController.js +++ b/src/UI/Shared/Modal/ModalController.js @@ -4,6 +4,7 @@ var Marionette = require('marionette'); var EditArtistView = require('../../Artist/Edit/EditArtistView'); var DeleteArtistView = require('../../Artist/Delete/DeleteArtistView'); var EpisodeDetailsLayout = require('../../Episode/EpisodeDetailsLayout'); +var AlbumDetailsLayout = require('../../Album/AlbumDetailsLayout'); var HistoryDetailsLayout = require('../../Activity/History/Details/HistoryDetailsLayout'); var LogDetailsView = require('../../System/Logs/Table/Details/LogDetailsView'); var RenamePreviewLayout = require('../../Rename/RenamePreviewLayout'); @@ -19,6 +20,7 @@ module.exports = Marionette.AppRouter.extend({ vent.on(vent.Commands.EditArtistCommand, this._editArtist, this); vent.on(vent.Commands.DeleteArtistCommand, this._deleteArtist, this); vent.on(vent.Commands.ShowEpisodeDetails, this._showEpisode, this); + vent.on(vent.Commands.ShowAlbumDetails, this._showAlbum, this); vent.on(vent.Commands.ShowHistoryDetails, this._showHistory, this); vent.on(vent.Commands.ShowLogDetails, this._showLogDetails, this); vent.on(vent.Commands.ShowRenamePreview, this._showRenamePreview, this); @@ -62,6 +64,13 @@ module.exports = Marionette.AppRouter.extend({ AppLayout.modalRegion.show(view); }, + _showAlbum : function(options) { + var view = new AlbumDetailsLayout({ + model : options.album + }); + AppLayout.modalRegion.show(view); + }, + _showHistory : function(options) { var view = new HistoryDetailsLayout({ model : options.model }); AppLayout.modalRegion.show(view); diff --git a/src/UI/vent.js b/src/UI/vent.js index 7603be871..7b5eaaef1 100644 --- a/src/UI/vent.js +++ b/src/UI/vent.js @@ -20,6 +20,7 @@ vent.Commands = { OpenModal2Command : 'OpenModal2Command', CloseModal2Command : 'CloseModal2Command', ShowEpisodeDetails : 'ShowEpisodeDetails', + ShowAlbumDetails : 'ShowAlbumDetails', ShowHistoryDetails : 'ShowHistoryDetails', ShowLogDetails : 'ShowLogDetails', SaveSettings : 'saveSettings',