Skip to content

Commit 78d6b4a

Browse files
committed
Merge branch 'master' of https://github.com/kennidenni/INF219
2 parents a112328 + e911f7a commit 78d6b4a

17 files changed

+237
-621
lines changed

Heap/js/Controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ var Controller = /** @class */ (function () {
3939
Controller.prototype.setSelectedIndex = function (index, select) {
4040
viewer.selectThisIndex(index, select);
4141
};
42-
Controller.prototype.setValueAtIndex = function (i, bValue) {
43-
viewer.setValueAtThisIndex(i, bValue);
42+
Controller.prototype.setValueAtIndex = function (i, bValue, oldVal) {
43+
viewer.setValueAtThisIndex(i, bValue, oldVal);
4444
};
4545
Controller.prototype.setValueAtSortIndex = function (i, bValue) {
4646
viewer.setValueAtThisSortIndex(i, bValue);

Heap/js/Controller.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
///<reference path="MaxHeapFree.ts"/>
1010

1111
class Controller {
12-
1312
private algorithm: IAlgorithm;
1413
private speed: number;
1514

@@ -46,8 +45,8 @@ class Controller {
4645
viewer.selectThisIndex(index, select);
4746
}
4847

49-
setValueAtIndex(i: number, bValue) {
50-
viewer.setValueAtThisIndex(i, bValue);
48+
setValueAtIndex(i: number, bValue : any, oldVal : any) {
49+
viewer.setValueAtThisIndex(i, bValue, oldVal);
5150
}
5251

5352
setValueAtSortIndex(i: number, bValue) {

Heap/js/EventManager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Created by knutandersstokke on 16.10.2016.
3-
*
3+
* Modified by Øyvind Skeie Liland
44
*/
55
/** Manager for events stored in queue. Manager is also responsible for executing events automatically */
66
var EventManager = /** @class */ (function () {
@@ -11,6 +11,7 @@ var EventManager = /** @class */ (function () {
1111
}
1212
// Executing the next event in the queue, adding it to 'previous'
1313
EventManager.prototype.next = function () {
14+
viewer.playButtonState();
1415
if (this.nextEvents.length == 0) {
1516
return;
1617
}

Heap/js/EventManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Created by knutandersstokke on 16.10.2016.
3-
*
3+
* Modified by Øyvind Skeie Liland
44
*/
55

66
declare var $: any;
@@ -14,6 +14,7 @@ class EventManager {
1414

1515
// Executing the next event in the queue, adding it to 'previous'
1616
next() {
17+
viewer.playButtonState();
1718
if (this.nextEvents.length == 0) {
1819
return;
1920
}
@@ -31,6 +32,7 @@ class EventManager {
3132
return;
3233
var event: FrontendEvent = (<FrontendEvent>this.previousEvents.pop());
3334
//this.delayTime = 0; //TODO: Should there be a delay when stepping backwards?
35+
3436
event.previous();
3537
this.nextEvents.unshift(event);
3638
}

Heap/js/HeapSort.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var HeapSort = /** @class */ (function (_super) {
3030
HeapSort.prototype.remove = function () {
3131
// Remove root element, set last element to root and start frontendevents
3232
this.currIndex--;
33+
var oldVal = this.array[0];
3334
control.setSelectedIndex(0, true);
3435
control.highlightNode(0, "orange");
3536
control.highlightSortElem(this.sortIndex, "orange");
@@ -38,7 +39,7 @@ var HeapSort = /** @class */ (function (_super) {
3839
this.exch(0, this.currIndex);
3940
control.swapNode(this.currIndex, 0);
4041
control.removeElem(this.currIndex, false);
41-
control.setValueAtIndex(this.currIndex, " ");
42+
control.setValueAtIndex(this.currIndex, " ", oldVal);
4243
this.sink(0, this.currIndex - 1);
4344
control.highlightSortElem(this.sortIndex--, "green");
4445
control.saveState(this.array);

Heap/js/HeapSort.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class HeapSort extends MaxHeap {
2424
remove(): void {
2525
// Remove root element, set last element to root and start frontendevents
2626
this.currIndex--
27+
let oldVal = this.array[0];
2728
control.setSelectedIndex(0, true);
2829
control.highlightNode(0, "orange");
2930
control.highlightSortElem(this.sortIndex, "orange");
@@ -32,7 +33,7 @@ class HeapSort extends MaxHeap {
3233
this.exch(0, this.currIndex);
3334
control.swapNode(this.currIndex, 0);
3435
control.removeElem(this.currIndex, false);
35-
control.setValueAtIndex(this.currIndex, " ");
36+
control.setValueAtIndex(this.currIndex, " ", oldVal);
3637
this.sink(0, this.currIndex - 1);
3738
control.highlightSortElem(this.sortIndex--, "green");
3839
control.saveState(this.array);

Heap/js/IView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface IView {
1212

1313
setThisArrow(index: number): void;
1414

15-
setValueAtThisIndex(i: number, bValue: number): void;
15+
setValueAtThisIndex(i: number, bValue: any, oldVal:any): void;
1616

1717
connectThisNodes(child: number, parent: number): void;
1818

Heap/js/MaxHeap.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ var MaxHeap = /** @class */ (function () {
9898
control.saveState(this.array);
9999
var tmp = this.array[number];
100100
this.array[number] = this.array[number2];
101-
control.setValueAtIndex(number, this.array[number]);
101+
control.setValueAtIndex(number, this.array[number], tmp);
102102
this.array[number2] = tmp;
103-
control.setValueAtIndex(number2, this.array[number2]);
103+
control.setValueAtIndex(number2, this.array[number2], this.array[number]);
104+
//console.log("fst: " + this.array[number] + ", second: " + this.array[number2]);
105+
//control.swapArrayElems(number, this.array[number], number2, this.array[number2]);
104106
};
105107
MaxHeap.prototype.add = function (a) {
106108
control.saveState(this.array);
@@ -111,28 +113,21 @@ var MaxHeap = /** @class */ (function () {
111113
else {
112114
control.lockScreen(true);
113115
this.array[this.currIndex] = a;
114-
setValueAtIndex(this.currIndex, a);
115-
insertNewNode(this.currIndex++, a);
116-
}
117-
// Swim to te correct index and start frontendevents
118-
if (this.currIndex == 1) {
119-
positioningNodes(1000);
120-
}
121-
else {
122-
insertNewElemConnect(this.currIndex - 1, Math.floor((this.currIndex - 2) / 2));
123-
this.swim(this.currIndex - 1);
116+
control.insertNewElem(this.currIndex++, a, Math.floor((this.currIndex - 2) / 2));
124117
}
118+
this.swim(this.currIndex - 1);
125119
control.lockScreen(false);
126120
};
127121
MaxHeap.prototype.remove = function () {
128122
control.saveState(this.array);
129123
control.lockScreen(true);
130124
// Remove root element, set last element to root and start frontendevents
131125
this.currIndex--;
126+
var oldVal = this.array[0];
132127
this.exch(0, this.currIndex);
133128
control.swapNode(this.currIndex, 0);
134129
control.removeElem(this.currIndex, false);
135-
control.setValueAtIndex(this.currIndex, " ");
130+
control.setValueAtIndex(this.currIndex, " ", oldVal);
136131
this.sink(0, this.currIndex - 1);
137132
control.saveState(this.array);
138133
control.lockScreen(false);

Heap/js/MaxHeap.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ class MaxHeap implements IAlgorithm {
119119

120120
let tmp = this.array[number];
121121
this.array[number] = this.array[number2];
122-
control.setValueAtIndex(number, this.array[number]);
122+
control.setValueAtIndex(number, this.array[number], tmp);
123123
this.array[number2] = tmp;
124-
control.setValueAtIndex(number2, this.array[number2]);
124+
control.setValueAtIndex(number2, this.array[number2], this.array[number]);
125+
//console.log("fst: " + this.array[number] + ", second: " + this.array[number2]);
126+
127+
//control.swapArrayElems(number, this.array[number], number2, this.array[number2]);
125128
}
126129

127130
add(a: number): void {
@@ -133,17 +136,9 @@ class MaxHeap implements IAlgorithm {
133136
} else {
134137
control.lockScreen(true);
135138
this.array[this.currIndex] = a;
136-
setValueAtIndex(this.currIndex, a);
137-
insertNewNode(this.currIndex++, a);
138-
}
139-
140-
// Swim to te correct index and start frontendevents
141-
if (this.currIndex == 1) {
142-
positioningNodes(1000);
143-
} else {
144-
insertNewElemConnect(this.currIndex - 1, Math.floor((this.currIndex - 2) / 2));
145-
this.swim(this.currIndex - 1);
139+
control.insertNewElem(this.currIndex++, a, Math.floor((this.currIndex - 2) / 2));
146140
}
141+
this.swim(this.currIndex - 1);
147142
control.lockScreen(false);
148143
}
149144

@@ -153,10 +148,11 @@ class MaxHeap implements IAlgorithm {
153148
control.lockScreen(true);
154149
// Remove root element, set last element to root and start frontendevents
155150
this.currIndex--;
151+
let oldVal = this.array[0];
156152
this.exch(0, this.currIndex);
157153
control.swapNode(this.currIndex, 0);
158154
control.removeElem(this.currIndex, false);
159-
control.setValueAtIndex(this.currIndex, " ");
155+
control.setValueAtIndex(this.currIndex, " ", oldVal);
160156
this.sink(0, this.currIndex - 1);
161157
control.saveState(this.array);
162158
control.lockScreen(false);

Heap/js/View.js

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ var View = /** @class */ (function () {
5151
}(index);
5252
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
5353
};
54-
View.prototype.setValueAtThisIndex = function (i, bValue) {
55-
var val = $("#arrayElem" + i).text();
54+
View.prototype.setValueAtThisIndex = function (i, bValue, oldVal) {
5655
var forwardSteps = function (i, bValue) {
5756
return function () {
5857
setValueAtIndex(i, bValue);
@@ -62,7 +61,7 @@ var View = /** @class */ (function () {
6261
return function () {
6362
setValueAtIndex(i, oldVal);
6463
};
65-
}(i, val);
64+
}(i, oldVal);
6665
manager.addEvent(new FrontendEvent(forwardSteps, backwardSteps, this.animSpeed));
6766
};
6867
View.prototype.setValueAtThisSortIndex = function (i, bValue) {
@@ -73,7 +72,7 @@ var View = /** @class */ (function () {
7372
}(i, bValue);
7473
var backwardSteps = function (i, bValue) {
7574
return function () {
76-
setValueAtSortIndex(i, i);
75+
setValueAtSortIndex(i, "");
7776
};
7877
}(i, bValue);
7978
manager.addEvent(new FrontendEvent(forwardSteps, backwardSteps, this.animSpeed));
@@ -97,22 +96,45 @@ var View = /** @class */ (function () {
9796
highlightNode(index, color);
9897
};
9998
}(index, color);
100-
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
99+
var backward = function (index) {
100+
return function () {
101+
removeHighlight(index);
102+
};
103+
}(index);
104+
manager.addEvent(new FrontendEvent(forward, backward, this.animSpeed));
101105
};
102106
View.prototype.highlightThisSortElem = function (index, color) {
103107
var forward = function (index, color) {
104108
return function () {
105109
sortHighlightElem(index, color);
106110
};
107111
}(index, color);
108-
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
112+
var backward = function (index) {
113+
return function () {
114+
removeSortHighlight(index);
115+
};
116+
}(index);
117+
manager.addEvent(new FrontendEvent(forward, backward, this.animSpeed));
109118
};
110119
View.prototype.removeThisHighlight = function (index) {
120+
// Find the current color
121+
var color = "";
122+
var classList = document.getElementById('arrayElem' + index).className.split(/\s+/);
123+
for (var i = 0; i < classList.length; i++) {
124+
if (classList[i] === 'orange' || classList[i] === 'green') {
125+
color = classList[i];
126+
}
127+
}
111128
var forward = function (index) {
112129
return function () {
113130
removeHighlight(index);
114131
};
115132
}(index);
133+
var backward = function (index, color) {
134+
return function () {
135+
highlightNode(index, color);
136+
};
137+
}(index, color);
116138
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
117139
};
118140
View.prototype.setThisState = function (relationships, backendArray) {
@@ -122,7 +144,10 @@ var View = /** @class */ (function () {
122144
this.step("backward", twoDimRelationshipsJSON, backendArray);
123145
};
124146
View.prototype.stepForward = function (twoDimRelationshipsJSON, backendArray) {
125-
this.step("forward", twoDimRelationshipsJSON, backendArray);
147+
//this.step("forward", twoDimRelationshipsJSON, backendArray);
148+
manager.next();
149+
if (manager.nextEvents.length <= 0)
150+
manager.start();
126151
};
127152
View.prototype.step = function (dir, twoDimRelationshipsJSON, backendArray) {
128153
var relationships = JSON.parse(twoDimRelationshipsJSON);
@@ -161,7 +186,12 @@ var View = /** @class */ (function () {
161186
screenLock(lock);
162187
};
163188
}(locked);
164-
manager.addEvent(new FrontendEvent(lck, lck, this.animSpeed));
189+
var notLck = function (lock) {
190+
return function () {
191+
screenLock(!lock);
192+
};
193+
}(locked);
194+
manager.addEvent(new FrontendEvent(lck, notLck, this.animSpeed));
165195
};
166196
View.prototype.setSlow = function () {
167197
this.animSpeed = 250;
@@ -257,11 +287,16 @@ var View = /** @class */ (function () {
257287
removeElem(index, removeArr);
258288
};
259289
}(i, removeArr);
260-
var backward = function (index, val) {
290+
var backward = function (index, value, parent) {
261291
return function () {
262-
insertNewElem(index, val);
292+
setValueAtIndex(index, value);
293+
insertNewNode(index, value);
294+
insertNewElemConnect(index, parent);
295+
// If first node -> Position with a nice animation
296+
if (control.getAlgorithm().getArrayLength() == 1)
297+
positioningNodes(1500);
263298
};
264-
}(i, val);
299+
}(i, val, Math.floor((i - 1) / 2));
265300
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));
266301
};
267302
View.prototype.play = function () {
@@ -271,37 +306,63 @@ var View = /** @class */ (function () {
271306
this.paused = true;
272307
this.playing = true;
273308
$("#play").text("Pause");
309+
lockBackForward(true);
274310
}
275311
else if (algo === "HeapSort" && !this.paused && !this.playing) {
276312
control.getAlgorithm().sort();
277313
this.paused = true;
278314
this.playing = true;
279315
$("#play").text("Pause");
316+
lockBackForward(true);
280317
}
281318
else {
282319
if (this.playing) {
283320
manager.pause();
284321
$("#play").text("Resume");
285322
this.playing = false;
323+
lockBackForward(false);
286324
}
287325
else {
288326
this.playing = true;
289327
manager.start();
290328
$("#play").text("Pause");
329+
lockBackForward(true);
291330
}
292331
}
293332
};
333+
// Used in eventmanager for freemode and predefined
334+
View.prototype.playButtonState = function () {
335+
var algo = control.getAlgorithm().getName();
336+
if (!(algo === "MaxHeap" || algo === "MaxHeapFree" || this.playing))
337+
return;
338+
if (manager.nextEvents.length > 0) {
339+
this.playing = true;
340+
lockPlay(false);
341+
lockBackForward(true);
342+
$("#play").text("Pause");
343+
}
344+
else {
345+
lockPlay(true);
346+
lockBackForward(false);
347+
}
348+
};
294349
View.prototype.insertNewElemThis = function (child, value, parent) {
295350
var forward = function (index, value, parent) {
296351
return function () {
297-
insertNewElem(index, value);
352+
setValueAtIndex(index, value);
353+
insertNewNode(index, value);
298354
insertNewElemConnect(index, parent);
355+
// If first node -> Position with a nice animation
356+
if (control.getAlgorithm().getArrayLength() == 1)
357+
positioningNodes(1500);
299358
};
300359
}(child, value, parent);
301360
var backward = function (index, parent) {
302361
return function () {
303362
allNodes[parent].removeChild(allNodes[index]);
304-
removeElem(index, true);
363+
//removeElem(index, true);
364+
setValueAtIndex(index, "");
365+
removeNode(index);
305366
};
306367
}(child, parent);
307368
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));

0 commit comments

Comments
 (0)