-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.js
208 lines (183 loc) · 6.92 KB
/
assignment.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
let aEvents, aEvents1;
aEvents = [
{id: 111, start: 50, end: 190},
{id: 222, start: 360, end: 420},
{id: 333, start: 380, end: 440},
{id: 444, start: 430, end: 490},
{id: 555, start: 500, end: 590},
{id: 666, start: 435, end: 590},
];
aEvents1 = [
{
"id": 1,
"start": 23,
"end": 113
},
{
"id": 2,
"start": 81,
"end": 455
},
{
"id": 3,
"start": 339,
"end": 597
},
{
"id": 4,
"start": 387,
"end": 686
},
{
"id": 5,
"start": 454,
"end": 673
}
];
const containerWidth = 600;
function createElements(aRes) {
let result = [];
for (let i = 0; i < aRes.length; ++i) {
const innerElementAsString = "<div class=\"tileTitle\">Sample Item - " +
aRes[i].id + "</div>" +
"<div class=\"tileTitle tileSubTitle\">Sample Location</div>";
let newElement = document.createElement('div');
newElement.className = "tile";
newElement.innerHTML = innerElementAsString.trim();
const width = aRes[i].hasOwnProperty("width") ? aRes[i].width : 880;
const height = aRes[i].end - aRes[i].start;
const left = aRes[i].left || 0;
const strAttr = "top:" + aRes[i].start + "px;" + "left:" + left + "px;" + "width:" + width + "px;" + "height:" + height + "px;";
newElement.setAttribute("style", strAttr);
result.push(newElement);
}
return result;
}
function _findElementByID(aEvents, id) {
if (aEvents && aEvents.length > 0) {
return aEvents.find(e => e.id === id);
}
}
function _increaseNextTilesDivider(eventArray, nextCollisions, divider) {
if (nextCollisions && eventArray) {
nextCollisions.forEach(e => {
let event = _findElementByID(eventArray, e);
if (event && event.divider) {
event.divider = Math.max(event.divider, divider);
}
});
}
}
function _increasePrevTilesDividerRec(eventArray, prevCollisions, location) {
if (prevCollisions && prevCollisions.length > 0) {
prevCollisions.forEach((e) => {
let elem = _findElementByID(eventArray, e);
if (elem && elem.prevCollisions) {
elem.divider = Math.max(location, elem.divider);
_increasePrevTilesDividerRec(eventArray, elem.prevCollisions, location);
} else if (elem) {
elem.divider = Math.max(location, elem.divider);
}
});
}
}
function _locationProcess(eventArray) {
for (let i = 0; i < eventArray.length; ++i) {
if (eventArray[i].root) {
eventArray[i].location = 1;
} else {
eventArray[i].location = eventArray[i].hasOwnProperty("prevCollisions") ? eventArray[i].prevCollisions.length + 1 : 1;
if (eventArray[i].location > eventArray[i].divider) {
console.log("This is where you dance!! " + eventArray[i].id);
eventArray[i].divider = eventArray[i].location;
_increaseNextTilesDivider(eventArray, eventArray[i].nextCollisions, eventArray[i].location);
_increasePrevTilesDividerRec(eventArray, eventArray[i].prevCollisions, eventArray[i].location);
}
}
}
}
function _rearangeRoots(eventArray) {
eventArray.forEach((event, i) => {
if (event.hasOwnProperty("nextCollisions") || event.hasOwnProperty("prevCollisions")) {
if (event.root) {
event.location = 0;
if (event.nextCollisions && !event.prevCollisions) {
event.divider = event.nextCollisions.length + 1;
event.nextCollisions.forEach((e) => {
const nextE = _findElementByID(eventArray, e);
event.divider = nextE.divider = Math.max(event.divider, nextE.divider);
});
} else if (event.nextCollisions && event.prevCollisions) {
event.prevCollisions.forEach((e) => {
const prevE = _findElementByID(eventArray, e);
event.divider = prevE.divider = Math.max(event.divider, prevE.divider);
});
event.nextCollisions.forEach((e) => {
const nextE = _findElementByID(eventArray, e);
event.divider = nextE.divider = Math.max(event.divider, nextE.divider);
});
} else if (event.prevCollisions) {
let element = _findElementByID(eventArray, event.prevCollisions[event.prevCollisions.length - 1]);
event.divider = element.divider;
}
if (event.nextCollisions && eventArray.length - 1 > i + event.nextCollisions.length) {
eventArray[i + 1 + event.nextCollisions.length].root = true;
}
} else {
if (event.nextCollisions && event.nextCollisions.length > 1 && _findElementByID(eventArray, event.nextCollisions[0]).root) {
}
}
}
});
}
function _checkAllCollisions(eventArray) {
eventArray[0].root = true;
for (let i = 0; i < eventArray.length; ++i) {
eventArray[i].location = 0;
eventArray[i].divider = 1;
for (let j = i + 1; j < eventArray.length; j++) {
if (eventArray[i].end > eventArray[j].start) {
eventArray[i].hasOwnProperty("nextCollisions") ? eventArray[i].nextCollisions.push(eventArray[j].id) : eventArray[i].nextCollisions = [eventArray[j].id];
eventArray[j].hasOwnProperty("prevCollisions") ? eventArray[j].prevCollisions.push(eventArray[i].id) : eventArray[j].prevCollisions = [eventArray[i].id];
eventArray[j].root = false;
} else {
eventArray[j].root = true;
}
}
}
_rearangeRoots(eventArray);
}
function _processAllCollidedEvents(aEvents) {
if (!aEvents || aEvents.length === 0) {
return;
} else {
aEvents.forEach((e) => {
e.width = containerWidth / e.divider;
e.left = (e.location - 1) * e.width;
});
}
}
function sortEvents(aEvents) {
aEvents = aEvents.filter(a => a.start < a.end);
aEvents.sort((a, b) => a.start - b.start);
aEvents[0].top = aEvents[0].start;
aEvents[0].left = 0;
_checkAllCollisions(aEvents);
_rearangeRoots(aEvents);
_locationProcess(aEvents);
_processAllCollidedEvents(aEvents);
return aEvents;
}
function appendChildren(elem, tiles) {
for (let i = 0; i < tiles.length; i++) {
elem.appendChild(tiles[i]);
}
}
document.addEventListener('DOMContentLoaded', function () {
const aRes = sortEvents(aEvents1);
console.log(aEvents);
const tiles = createElements(aRes);
const elem = document.getElementById("container");
appendChildren(elem, tiles);
console.log(elem + "GOT IT ")
}, false);