2
* Copyright (C) 2013-2014 Canonical Ltd
4
* This file is part of Ubuntu Calendar App
6
* Ubuntu Calendar App is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 3 as
8
* published by the Free Software Foundation.
10
* Ubuntu Calendar App is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
WorkerScript.onMessage = function(events) {
20
//returns sorted array of schedules
21
//schedule is start time and duration
22
var allSchs = processEvents(events);
24
while( allSchs.length > 0) {
25
var sch = allSchs.shift();
27
//finds all schedules overlapping with current schedule and remove from original array
28
var schs = findOverlappingSchedules(sch, allSchs);
30
//insert original schedule first, so array remain sorted
33
//assign position to schedules with respest to their duration and start time
35
var maxDepth = assignDepth(schs, array);
36
WorkerScript.sendMessage({ 'schedules': array,"maxDepth":maxDepth});
41
function getMinutes(time) {
42
return time.getHours() * 60 + time.getMinutes();
45
function getDuration(event) {
46
var start = getMinutes(event.startDateTime);
47
var end = getMinutes(event.endDateTime);
51
function processEvents(events) {
53
for( var i = 0; i < events.length ; ++i) {
56
sch["start"] = getMinutes(event.startDateTime);
57
sch["duration"] = getDuration(event);
60
sortedInsert(array,sch);
65
//insert in to array using insertion sort
66
function sortedInsert(array, sch) {
67
for(var i = array.length-1; i >=0 ; --i) {
69
if( sch.duration < array[i].duration) {
70
array.splice(i+1,0,sch);
77
//find all overlapping schedules respect to provided schedule
78
function findOverlappingSchedules( sch, schs) {
81
while( i < schs.length && schs.length > 0) {
82
var otherSch = schs[i];
83
if( doesOverlap(sch, otherSch) ) {
86
sch = mergeSchedules(sch,otherSch);
94
//merges tow schedules and creates a schedule which is long engouth to contain both the of them
95
function mergeSchedules(sch1,sch2) {
97
sch["start"] = Math.min(sch1.start,sch2.start);
98
sch["duration"] = Math.max(sch1.duration,sch2.duration);
105
//check if two schedules overlap each other
106
//is start time of one schedule is between
107
//start and end time of other schedule then it's considered as overlap
108
function doesOverlap( sch1, sch2) {
109
if( sch1.start >= sch2.start && sch1.start < sch2.start + sch2.duration ) {
113
if( sch2.start >= sch1.start && sch2.start < sch1.start + sch1.duration ) {
120
//assign depth(position) of schedule with respect to other
121
function assignDepth(schs, array) {
123
while( schs.length > 0 ) {
124
var sch = schs.shift()
126
var depth = findDepth(sch, schs);
127
maxDepth = Math.max(maxDepth,depth);
132
function findDepth( sch, schs) {
134
for( var i = 0; i < schs.length ; ++i) {
135
var otherSch = schs[i];
136
if( doesOverlap(sch, otherSch) ) {
138
maxDepth = Math.max(maxDepth,otherSch.depth);
139
sch = mergeSchedules(sch,otherSch);