Drag revert working - need to do same for resize & tidy up

This commit is contained in:
sean kenny
2014-08-29 16:58:15 +01:00
parent 331a242281
commit 4d489a41ea
5 changed files with 241 additions and 9 deletions
+177 -1
View File
@@ -3,7 +3,7 @@ function ResourceManager(options) {
// exports
t.fetchResources = fetchResources;
t.setResources = setResources;
t.mutateResourceEvent = mutateResourceEvent;
// locals
var resourceSources = [];
var cache;
@@ -134,4 +134,180 @@ function ResourceManager(options) {
normalizers[i](source);
}
}
/* Event Modification Math
-----------------------------------------------------------------------------------------*/
// Modify the date(s) of an event and make this change propagate to all other events with
// the same ID (related repeating events).
//
// If `newStart`/`newEnd` are not specified, the "new" dates are assumed to be `event.start` and `event.end`.
// The "old" dates to be compare against are always `event._start` and `event._end` (set by EventManager).
//
// Returns an object with delta information and a function to undo all operations.
//
function mutateResourceEvent(event, newResources, newStart, newEnd) {
var oldAllDay = event._allDay;
var oldStart = event._start;
var oldEnd = event._end;
var clearEnd = false;
var newAllDay;
var dateDelta;
var durationDelta;
var undoFunc;
// if no new dates were passed in, compare against the event's existing dates
if (!newStart && !newEnd) {
newStart = event.start;
newEnd = event.end;
}
// NOTE: throughout this function, the initial values of `newStart` and `newEnd` are
// preserved. These values may be undefined.
// detect new allDay
if (event.allDay != oldAllDay) { // if value has changed, use it
newAllDay = event.allDay;
}
else { // otherwise, see if any of the new dates are allDay
newAllDay = !(newStart || newEnd).hasTime();
}
// normalize the new dates based on allDay
if (newAllDay) {
if (newStart) {
newStart = newStart.clone().stripTime();
}
if (newEnd) {
newEnd = newEnd.clone().stripTime();
}
}
// compute dateDelta
if (newStart) {
if (newAllDay) {
dateDelta = dayishDiff(newStart, oldStart.clone().stripTime()); // treat oldStart as allDay
}
else {
dateDelta = dayishDiff(newStart, oldStart);
}
}
if (newAllDay != oldAllDay) {
// if allDay has changed, always throw away the end
clearEnd = true;
}
else if (newEnd) {
durationDelta = dayishDiff(
// new duration
newEnd || t.getDefaultEventEnd(newAllDay, newStart || oldStart),
newStart || oldStart
).subtract(dayishDiff(
// subtract old duration
oldEnd || t.getDefaultEventEnd(oldAllDay, oldStart),
oldStart
));
}
undoFunc = mutateResourceEvents(
t.clientEvents(event._id), // get events with this ID
clearEnd,
newAllDay,
dateDelta,
durationDelta,
newResources
);
return {
dateDelta: dateDelta,
durationDelta: durationDelta,
undo: undoFunc
};
}
// Modifies an array of events in the following ways (operations are in order):
// - clear the event's `end`
// - convert the event to allDay
// - add `dateDelta` to the start and end
// - add `durationDelta` to the event's duration
//
// Returns a function that can be called to undo all the operations.
//
function mutateResourceEvents(events, clearEnd, forceAllDay, dateDelta, durationDelta, newResources) {
var isAmbigTimezone = t.getIsAmbigTimezone();
var undoFunctions = [];
$.each(events, function(i, event) {
var oldResources = event.resources;
var oldAllDay = event._allDay;
var oldStart = event._start;
var oldEnd = event._end;
var newAllDay = forceAllDay != null ? forceAllDay : oldAllDay;
var newStart = oldStart.clone();
var newEnd = (!clearEnd && oldEnd) ? oldEnd.clone() : null;
// NOTE: this function is responsible for transforming `newStart` and `newEnd`,
// which were initialized to the OLD values first. `newEnd` may be null.
// normlize newStart/newEnd to be consistent with newAllDay
if (newAllDay) {
newStart.stripTime();
if (newEnd) {
newEnd.stripTime();
}
}
else {
if (!newStart.hasTime()) {
newStart = t.rezoneDate(newStart);
}
if (newEnd && !newEnd.hasTime()) {
newEnd = t.rezoneDate(newEnd);
}
}
// ensure we have an end date if necessary
if (!newEnd && (options.forceEventDuration || +durationDelta)) {
newEnd = t.getDefaultEventEnd(newAllDay, newStart);
}
// translate the dates
newStart.add(dateDelta);
if (newEnd) {
newEnd.add(dateDelta).add(durationDelta);
}
// if the dates have changed, and we know it is impossible to recompute the
// timezone offsets, strip the zone.
if (isAmbigTimezone) {
if (+dateDelta || +durationDelta) {
newStart.stripZone();
if (newEnd) {
newEnd.stripZone();
}
}
}
event.allDay = newAllDay;
event.start = newStart;
event.end = newEnd;
event.resources = newResources;
backupEventDates(event);
undoFunctions.push(function() {
event.allDay = oldAllDay;
event.start = oldStart;
event.end = oldEnd;
event.resources = oldResources;
backupEventDates(event);
});
});
return function() {
for (var i=0; i<undoFunctions.length; i++) {
undoFunctions[i]();
}
};
}
}
-1
View File
@@ -9,7 +9,6 @@ function ResourceDayView(element, calendar) { // TODO: make a DayView mixin
t.incrementDate = incrementDate;
t.render = render;
// imports
ResourceView.call(t, element, calendar, 'resourceDay');
var getResources = t.getResources;
+12 -4
View File
@@ -489,9 +489,14 @@ function ResourceEventRenderer() {
else { // changed!
// calculate column delta
var newCol = Math.round((eventElement.offset().left - getSlotContainer().offset().left) / colWidth);
// if (newCol !== origCol){
// event.resources = [ resources()[newCol].id ];
// }
var newResources = event.resources;
if (newCol !== origCol){
event.resources = [ resources()[newCol].id ];
newResources = [ resources()[newCol].id ];
}
var eventStart = event.start.clone(); // already assumed to have a stripped time
var snapTime;
var snapIndex;
@@ -504,6 +509,7 @@ function ResourceEventRenderer() {
eventDrop(
eventElement[0],
event,
newResources,
eventStart,
ev,
ui
@@ -637,12 +643,14 @@ function ResourceEventRenderer() {
trigger('eventDragStop', eventElement[0], event, ev, ui);
if (isInBounds && (isAllDay || resourceDelta || snapDelta)) { // changed!
if (resourceDelta){
event.resources = [ resources()[origCell.col + resourceDelta].id ];
}
var resources = event.resources;
if (resourceDelta){
resources = [ resources()[origCell.col + resourceDelta].id ];
}
eventDrop(
eventElement[0],
event,
resources,
eventStart,
ev,
ui
+49 -3
View File
@@ -62,9 +62,12 @@ function ResourceView(element, calendar, viewName) {
t.dragStart = dragStart;
t.dragStop = dragStop;
t.getResources = calendar.fetchResources;
// imports
View.call(t, element, calendar, viewName);
t.eventDrop = eventDrop;
t.eventResize = eventResize;
// imports
// View.call(t, element, calendar, viewName);
OverlayManager.call(t);
SelectionManager.call(t);
ResourceEventRenderer.call(t);
@@ -81,7 +84,7 @@ function ResourceView(element, calendar, viewName) {
var rangeToSegments = t.rangeToSegments;
var formatDate = calendar.formatDate;
var calculateWeekNumber = calendar.calculateWeekNumber;
var reportEventChange = calendar.reportEventChange;
// locals
@@ -908,7 +911,50 @@ function ResourceView(element, calendar, viewName) {
trigger('dayClick', dayBodyCells[dateToCell(date).col], date, ev);
}
/* Event Modification Reporting
---------------------------------------------------------------------------------*/
function eventDrop(el, event, newResources, newStart, ev, ui) {
//var mutateResult = calendar.mutateEvent(event, newStart, null);
var mutateResult = calendar.mutateResourceEvent(event, newResources, newStart, null);
trigger(
'eventDrop',
el,
event,
mutateResult.dateDelta,
function() {
mutateResult.undo();
reportEventChange(event._id);
},
ev,
ui
);
reportEventChange(event._id);
}
function eventResize(el, event, newEnd, ev, ui) {
var mutateResult = calendar.mutateResourceEvent(event, null, newEnd);
trigger(
'eventResize',
el,
event,
mutateResult.durationDelta,
function() {
mutateResult.undo();
reportEventChange(event._id);
},
ev,
ui
);
reportEventChange(event._id);
}
/* External Dragging
--------------------------------------------------------------------------------*/
+3
View File
@@ -103,6 +103,9 @@
selectable: true,
selectHelper: true,
allDaySlot: true,
eventDrop: function (event, delta, revertFunc) {
revertFunc();
},
// weekends: false,
header: {
left: 'prev,next today',