-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfullcalendar.viewmore.js
289 lines (242 loc) · 10.7 KB
/
fullcalendar.viewmore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright (c) 2011 Lyconic, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//todo should attach reset to changeview method in certain situations
(function ($, undefined) {
$.fn.limitEvents = function(opts){
if (opts.constructor === Number) opts = { maxEvents: opts };
return this.each(function(){
var limit = new $.fn.limitEvents.constructor($(this));
$.extend({ maxEvents: 4 }, opts); //defaults
$(this).fullCalendar('limitEvents', opts);
});
};
$.fn.limitEvents.constructor = function(calendar){
if (!(this instanceof arguments.callee)) return new arguments.callee(calendar);
var self = this;
self.calendar = calendar;
self.calendar.data('fullCalendar').limitEvents = function(opts){
self.opts = opts;
self.observers();
self.increaseHeight(25);
self.extendCallbacks();
}
};
})(jQuery);
(function ($, undefined) {
this.observers = function(){
var self = this;
$(document).mouseup(function(e){ //deselect when clicking outside of calendar or formbubble
var $target = $(e.target),
isFormBubble = $target.parents('.form-bubble').length || $target.hasClass('form-bubble'),
isInsideOfCalendar = $target.parents('.fc-content').length || $target.hasClass('fc-content');
if (!isInsideOfCalendar && !isFormBubble) self.calendar.fullCalendar('unselect');
});
self.calendar.delegate('.fc-event','mousedown', function(){ //close currently open form bubbles when user clicks an existing event
$.fn.formBubble.close();
});
self.calendar.delegate('.fc-button-prev, .fc-button-next', 'click', function(){
resetEventsRangeCounts();
});
};
this.increaseHeight = function(height, windowResized, td){
var cal = this.calendar,
cells = td || cal.find('.fc-view-month tbody tr td'),
fcDayContent = cells.find('.fc-day-content'),
cellHeight, fcDayContentHeight;
if (windowResized) fcDayContent.height(1);
cellHeight = cells.eq(0).height(),
fcDayContentHeight = cellHeight - cells.eq(0).find('.fc-day-number').height();
fcDayContent.height(fcDayContentHeight);
};
this.extendCallbacks = function(){
var self = this,
opt = self.calendar.fullCalendar('getView').calendar.options,
_eventRender = opt.eventRender,
_eventDrop = opt.eventDrop,
_eventResize = opt.eventResize,
_viewDisplay = opt.viewDisplay,
_events = opt.events,
_windowResize = opt.windowResize;
$.extend(opt, {
eventRender: function(event, element){
var currentView = self.calendar.fullCalendar('getView').name,
dateFormat = (event.allDay) ? 'MM/dd/yyyy' : 'hh:mmtt',
startDateLink = $.fullCalendar.formatDate(event.start, dateFormat),
endDateLink = $.fullCalendar.formatDate(event.end, dateFormat),
maxEvents = self.opts.maxEvents,
allEvents = self.calendar.fullCalendar('clientEvents'),
eventDate = $.fullCalendar.formatDate(event.end || event.start,'MM/dd/yy'),
td, viewMoreButton;
event.element = element;
event.startDateLink = startDateLink;
event.endDateLink = endDateLink;
if (currentView === 'month') {
doEventsRangeCount(event, self.calendar); //add event quantity to range for event and day
td = getCellFromDate(eventDate, self.calendar);
if (td.data('apptCount') > maxEvents) {
if (!td.find('.events-view-more').length) {
viewMoreButton = $('<div class="events-view-more"><a href="#view-more"><span>View More</span></a></div>')
.appendTo(td)
.click(function () {
var viewMoreClick = self.opts.viewMoreClick;
if (viewMoreClick && $.isFunction(viewMoreClick)) self.opts.viewMoreClick();
else viewMore(td, self.calendar); //show events in formBubble overlay
return false;
});
self.increaseHeight(25, false, td);
}
if ($.isFunction(_eventRender)) _eventRender(event, element);
return false; //prevents event from being rendered
}
}
if ($.isFunction(_eventRender)) _eventRender(event, element);
return true; //renders event
},
eventDrop: function (event, dayDelta, minuteDelta, allDay, revertFunc) {
resetEventsRangeCounts();
if ($.isFunction(_eventDrop)) _eventDrop(event, dayDelta, minuteDelta, allDay, revertFunc);
},
eventResize: function(event){
resetEventsRangeCounts();
if ($.isFunction(_eventResize)) _eventResize(event);
},
viewDisplay: function(view){
$.fn.formBubble.close();
if (view.name !== 'month') resetEventsRangeCounts();
if ($.isFunction(_viewDisplay)) _viewDisplay(view);
},
events: function(start, end, callback) {
resetEventsRangeCounts();
if ($.isFunction(_events)) _events(start, end, callback);
},
windowResize: function(view){ //fired AFTER events are rendered
if ($.isFunction(_windowResize)) _windowResize(view);
self.increaseHeight(25, true);
resetEventsRangeCounts();
self.calendar.fullCalendar('render'); //manually render to avoid layout bug
}
});
};
function doEventsRangeCount(event, calInstance){
var eventStart = event._start,
eventEnd = event._end || event._start,
dateRange = expandDateRange(eventStart, eventEnd),
eventElement = event.element;
$(dateRange).each(function(i){
var td = getCellFromDate($.fullCalendar.formatDate(dateRange[i],'MM/dd/yy'), calInstance),
currentCount = (td.data('apptCount') || 0) + 1;
td.data('apptCount', currentCount);
if (td.data().appointments === undefined) td.data().appointments = [event];
else td.data().appointments.push(event);
});
}
function expandDateRange(start, end){
var value = new Date(start.getFullYear(), start.getMonth(), start.getDate()),
values = [];
end = new Date(end.getFullYear(), end.getMonth(), end.getDate());
if (value > end) throw "InvalidRange";
while (value <= end) {
values.push(value);
value = new Date(value.getFullYear(), value.getMonth(), value.getDate() + 1);
}
return values;
}
function resetEventsRangeCounts(){
$('.fc-view-month td').each(function(i){
$(this).find('.events-view-more').remove();
$.removeData(this, "apptCount");
$.removeData(this, "appointments");
});
}
function viewMore(day, calInstance){
var appointments = day.data('appointments'),
elemWidth = day.outerWidth() + 1,
self = this;
day.formBubble({
graphics: {
close: true,
pointer: false
},
offset: {
x: -elemWidth,
y: 0
},
animation: {
slide: false,
speed: 0
},
callbacks: {
onOpen: function(){
var bubble = $.fn.formBubble.bubbleObject;
bubble.addClass('overlay');
},
onClose: function(){
calInstance.fullCalendar('unselect');
}
},
content: function(){
var startDate = getDateFromCell(day, calInstance),
startDateLabel = startDate.toString("MMM dS"),
dayValue = parseInt(day.find('.fc-day-number').text()),
eventList=$('<ul></ul>').prepend('<li><h5>' + startDateLabel + '</h5></li>');
elemWidth = elemWidth - 30;
$(appointments).each(function(){
var apptStartDay = parseInt($.fullCalendar.formatDate(this.start,'d')), //should be comparing date instead of day (bug with gray dates) <-- fix
apptEndDay = parseInt($.fullCalendar.formatDate(this.end,'d')),
event = this.element.clone(false).attr('style', '').css('width', elemWidth);
if (apptStartDay < dayValue) $(event).addClass('arrow-left');
if (apptEndDay > dayValue) $(event).addClass('arrow-right');
event.appendTo(eventList).wrap('<li>');
});
eventList.wrap('<div>');
return eventList.parent('div').html();
}
});
}
function getCellFromDate(thisDate, calInstance){ //ties events to actual table cells, and also differentiates between "gray" dates and "black" dates
var start = calInstance.fullCalendar('getView').start,
end = calInstance.fullCalendar('getView').end,
td;
thisDate = Date.parse(thisDate);
td = $('.fc-day-number').filter(function(){
return $(this).text()===$.fullCalendar.formatDate(thisDate,'d')
}).parent('td');
if (thisDate < start){ //date is in last month
td = td.filter(':first');
}else if (thisDate >= end){ //date is in next month
td = td.filter(':last');
}else{ //date is in this month
td = td.filter(function(){
return $(this).hasClass('fc-other-month')===false;
});
}
return td;
}
function getDateFromCell(td, calInstance){
var cellPos = {
row: td.parent().parent().children().index(td.parent()),
col: td.parent().children().index(td)
};
return calInstance.fullCalendar('getView').cellDate(cellPos);
}
}).call(jQuery.fn.limitEvents.constructor.prototype, jQuery);