Several days ago I asked the following question on webapps.stackexchange.com:
https://webapps.stackexchange.com/questions/54130/is-there-a-way-to-remove-overlaying-events-in-google-calendar
I didn't get any good answers so far, so I've decided to write my own little script that will change width of the event based on the number of overlapping events.
I want to avoid overlapping of the boxes and want them to stack.
Here's the initial test:
Below is a basic script with description:
$('[class^="tg-col"]').each(function(){ // each day column
//(Mon, Tue, etc. has class tg-col or tg-col-weekend.
var ItemArray = [];
$(this).find(".chip").each(function(){ //each event box has chip class.
var top = $$(this).position().top; //Get top pixels
var bottom = $$(this).height() + top; //get end of the event.
var arr = ItemArray.push({
class: $$(this).attr("class").split(" ")[0],
start : top,
end:bottom
});
});
var result = getOverlaps(ItemArray); //get overlaps counts number of overlapping events.
$$.each(result, function(index, value){
var ec = result[index].eventCount;
var w = 100/result[index].eventCount-1 + "%";
var el = $$("."+result[index].class);
el.width(w);
//el.css("left",w);
});
});
function getOverlaps(events) {
// sort events
events.sort(function (a, b) {
return a.start - b.start;
});
var results = [];
for (var i = 0, l = events.length; i < l; i++) {
var oEvent = events[i];
var nOverlaps = 0;
for (var j = 0; j < l; j++) {
var oCompareEvent = events[j];
if (oCompareEvent.start <= oEvent.end && oCompareEvent.end > oEvent.start || oCompareEvent.end <= oEvent.start && oCompareEvent.start > oEvent.end) {
nOverlaps++;
}
}
if (nOverlaps > 1) {
results.push({
class: oEvent.class,
eventCount: nOverlaps
});
}
}
return results;
}
This solution works only on simple events (on the left):
For more complex overlapping we cannot simply count numbers of overlaps and diving 100% / number of overlaps. We have to take into considerations other dimensions.
Also, after each manipulation in google calendar it redraws events and script changes are lost. Is there easier way to solve this problem?
(Maybe it is possible to modify js received from google directly? (But it's minified :().