400.1.1
by mihirsoni-123
Added copyright header comments in all the pages |
1 |
/*
|
400.1.2
by mihirsoni-123
updated copyright year |
2 |
* Copyright (C) 2013-2014 Canonical Ltd
|
400.1.1
by mihirsoni-123
Added copyright header comments in all the pages |
3 |
*
|
4 |
* This file is part of Ubuntu Calendar App
|
|
5 |
*
|
|
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.
|
|
9 |
*
|
|
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.
|
|
14 |
*
|
|
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/>.
|
|
17 |
*/
|
|
238.1.17
by Kunal Parmar
moved duration calculation to Workerscript |
18 |
WorkerScript.onMessage = function(events) { |
238.1.7
by Kunal Parmar
Second attempt |
19 |
|
238.1.17
by Kunal Parmar
moved duration calculation to Workerscript |
20 |
//returns sorted array of schedules
|
21 |
//schedule is start time and duration
|
|
22 |
var allSchs = processEvents(events); |
|
238.1.7
by Kunal Parmar
Second attempt |
23 |
|
24 |
while( allSchs.length > 0) { |
|
238.1.14
by Kunal Parmar
splice to shift |
25 |
var sch = allSchs.shift(); |
238.1.7
by Kunal Parmar
Second attempt |
26 |
|
27 |
//finds all schedules overlapping with current schedule and remove from original array
|
|
28 |
var schs = findOverlappingSchedules(sch, allSchs); |
|
29 |
||
238.1.16
by Kunal Parmar
removed unnecessary sort |
30 |
//insert original schedule first, so array remain sorted
|
31 |
schs.unshift(sch); |
|
238.1.7
by Kunal Parmar
Second attempt |
32 |
|
33 |
//assign position to schedules with respest to their duration and start time
|
|
34 |
var array = []; |
|
35 |
var maxDepth = assignDepth(schs, array); |
|
36 |
WorkerScript.sendMessage({ 'schedules': array,"maxDepth":maxDepth}); |
|
37 |
}
|
|
38 |
}
|
|
39 |
||
238.1.17
by Kunal Parmar
moved duration calculation to Workerscript |
40 |
|
41 |
function getMinutes(time) { |
|
42 |
return time.getHours() * 60 + time.getMinutes(); |
|
43 |
}
|
|
44 |
||
45 |
function getDuration(event) { |
|
46 |
var start = getMinutes(event.startDateTime); |
|
47 |
var end = getMinutes(event.endDateTime); |
|
48 |
return end - start; |
|
49 |
}
|
|
50 |
||
51 |
function processEvents(events) { |
|
52 |
var array = []; |
|
53 |
for( var i = 0; i < events.length ; ++i) { |
|
54 |
var event = events[i] |
|
55 |
var sch = {}; |
|
56 |
sch["start"] = getMinutes(event.startDateTime); |
|
57 |
sch["duration"] = getDuration(event); |
|
58 |
sch["id"] = event.id; |
|
59 |
sch["depth"] = 0; |
|
60 |
sortedInsert(array,sch); |
|
61 |
}
|
|
62 |
return array; |
|
63 |
}
|
|
64 |
||
65 |
//insert in to array using insertion sort
|
|
66 |
function sortedInsert(array, sch) { |
|
67 |
for(var i = array.length-1; i >=0 ; --i) { |
|
68 |
var temp = array[i]; |
|
69 |
if( sch.duration < array[i].duration) { |
|
70 |
array.splice(i+1,0,sch); |
|
71 |
return; |
|
72 |
}
|
|
73 |
}
|
|
74 |
array.push(sch); |
|
75 |
}
|
|
76 |
||
238.1.7
by Kunal Parmar
Second attempt |
77 |
//find all overlapping schedules respect to provided schedule
|
78 |
function findOverlappingSchedules( sch, schs) { |
|
79 |
var array = []; |
|
80 |
var i = 0; |
|
81 |
while( i < schs.length && schs.length > 0) { |
|
82 |
var otherSch = schs[i]; |
|
83 |
if( doesOverlap(sch, otherSch) ) { |
|
84 |
schs.splice(i,1); |
|
85 |
array.push(otherSch); |
|
86 |
sch = mergeSchedules(sch,otherSch); |
|
87 |
} else { |
|
88 |
i++; |
|
89 |
}
|
|
90 |
}
|
|
91 |
return array; |
|
92 |
}
|
|
93 |
||
94 |
//merges tow schedules and creates a schedule which is long engouth to contain both the of them
|
|
95 |
function mergeSchedules(sch1,sch2) { |
|
96 |
var sch = {}; |
|
97 |
sch["start"] = Math.min(sch1.start,sch2.start); |
|
98 |
sch["duration"] = Math.max(sch1.duration,sch2.duration); |
|
99 |
sch["depth"] = 1; |
|
100 |
sch["id"] = "DUMMY"; |
|
101 |
return sch; |
|
102 |
}
|
|
103 |
||
104 |
||
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 ) { |
|
110 |
return true; |
|
111 |
}
|
|
112 |
||
113 |
if( sch2.start >= sch1.start && sch2.start < sch1.start + sch1.duration ) { |
|
114 |
return true; |
|
115 |
}
|
|
116 |
||
117 |
return false; |
|
118 |
}
|
|
119 |
||
238.1.11
by Kunal Parmar
typo removed |
120 |
//assign depth(position) of schedule with respect to other
|
238.1.7
by Kunal Parmar
Second attempt |
121 |
function assignDepth(schs, array) { |
122 |
var maxDepth = 0; |
|
123 |
while( schs.length > 0 ) { |
|
238.1.15
by Kunal Parmar
splice to shift |
124 |
var sch = schs.shift() |
238.1.7
by Kunal Parmar
Second attempt |
125 |
array.push(sch); |
126 |
var depth = findDepth(sch, schs); |
|
127 |
maxDepth = Math.max(maxDepth,depth); |
|
128 |
}
|
|
129 |
return maxDepth; |
|
130 |
}
|
|
131 |
||
132 |
function findDepth( sch, schs) { |
|
133 |
var maxDepth = 0; |
|
134 |
for( var i = 0; i < schs.length ; ++i) { |
|
135 |
var otherSch = schs[i]; |
|
136 |
if( doesOverlap(sch, otherSch) ) { |
|
137 |
otherSch.depth ++; |
|
138 |
maxDepth = Math.max(maxDepth,otherSch.depth); |
|
139 |
sch = mergeSchedules(sch,otherSch); |
|
140 |
}
|
|
141 |
}
|
|
142 |
return maxDepth; |
|
143 |
}
|