Skip to content

Commit 307f0e6

Browse files
Add bruker nå controller/eventmanager og påbegynt backward fix
1 parent 498aa6d commit 307f0e6

File tree

8 files changed

+121
-55
lines changed

8 files changed

+121
-55
lines changed

Heap/js/EventManager.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ var EventManager = /** @class */ (function () {
2323
};
2424
// Executing the previous event
2525
EventManager.prototype.previous = function () {
26+
this.pause();
2627
if (this.previousEvents.length == 0)
2728
return;
2829
var event = this.previousEvents.pop();
2930
//this.delayTime = 0; //TODO: Should there be a delay when stepping backwards?
31+
console.log(event);
32+
console.log(this.previousEvents);
3033
event.previous();
3134
this.nextEvents.unshift(event);
3235
};

Heap/js/EventManager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ class EventManager {
2727

2828
// Executing the previous event
2929
previous() {
30+
this.pause();
3031
if (this.previousEvents.length == 0)
3132
return;
3233
var event: FrontendEvent = (<FrontendEvent>this.previousEvents.pop());
3334
//this.delayTime = 0; //TODO: Should there be a delay when stepping backwards?
35+
console.log(event);
36+
console.log(this.previousEvents);
37+
3438
event.previous();
3539
this.nextEvents.unshift(event);
3640
}

Heap/js/MaxHeap.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,9 @@ var MaxHeap = /** @class */ (function () {
111111
else {
112112
control.lockScreen(true);
113113
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);
114+
control.insertNewElem(this.currIndex++, a, Math.floor((this.currIndex - 2) / 2));
124115
}
116+
this.swim(this.currIndex - 1);
125117
control.lockScreen(false);
126118
};
127119
MaxHeap.prototype.remove = function () {

Heap/js/MaxHeap.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,9 @@ class MaxHeap implements IAlgorithm {
133133
} else {
134134
control.lockScreen(true);
135135
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);
136+
control.insertNewElem(this.currIndex++, a, Math.floor((this.currIndex - 2) / 2));
146137
}
138+
this.swim(this.currIndex - 1);
147139
control.lockScreen(false);
148140
}
149141

Heap/js/View.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ var View = /** @class */ (function () {
9797
highlightNode(index, color);
9898
};
9999
}(index, color);
100-
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
100+
var backward = function (index) {
101+
return function () {
102+
removeHighlight(index);
103+
};
104+
}(index);
105+
manager.addEvent(new FrontendEvent(forward, backward, this.animSpeed));
101106
};
102107
View.prototype.highlightThisSortElem = function (index, color) {
103108
var forward = function (index, color) {
@@ -108,11 +113,24 @@ var View = /** @class */ (function () {
108113
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
109114
};
110115
View.prototype.removeThisHighlight = function (index) {
116+
// Find the current color
117+
var color = "";
118+
var classList = document.getElementById('arrayElem' + index).className.split(/\s+/);
119+
for (var i = 0; i < classList.length; i++) {
120+
if (classList[i] === 'orange' || classList[i] === 'green') {
121+
color = classList[i];
122+
}
123+
}
111124
var forward = function (index) {
112125
return function () {
113126
removeHighlight(index);
114127
};
115128
}(index);
129+
var backward = function (index, color) {
130+
return function () {
131+
highlightNode(index, color);
132+
};
133+
}(index, color);
116134
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
117135
};
118136
View.prototype.setThisState = function (relationships, backendArray) {
@@ -122,7 +140,10 @@ var View = /** @class */ (function () {
122140
this.step("backward", twoDimRelationshipsJSON, backendArray);
123141
};
124142
View.prototype.stepForward = function (twoDimRelationshipsJSON, backendArray) {
125-
this.step("forward", twoDimRelationshipsJSON, backendArray);
143+
//this.step("forward", twoDimRelationshipsJSON, backendArray);
144+
manager.next();
145+
if (manager.nextEvents.length <= 0)
146+
manager.start();
126147
};
127148
View.prototype.step = function (dir, twoDimRelationshipsJSON, backendArray) {
128149
var relationships = JSON.parse(twoDimRelationshipsJSON);
@@ -161,7 +182,12 @@ var View = /** @class */ (function () {
161182
screenLock(lock);
162183
};
163184
}(locked);
164-
manager.addEvent(new FrontendEvent(lck, lck, this.animSpeed));
185+
var notLck = function (lock) {
186+
return function () {
187+
screenLock(!lock);
188+
};
189+
}(locked);
190+
manager.addEvent(new FrontendEvent(lck, notLck, this.animSpeed));
165191
};
166192
View.prototype.setSlow = function () {
167193
this.animSpeed = 250;
@@ -294,14 +320,20 @@ var View = /** @class */ (function () {
294320
View.prototype.insertNewElemThis = function (child, value, parent) {
295321
var forward = function (index, value, parent) {
296322
return function () {
297-
insertNewElem(index, value);
323+
setValueAtIndex(index, value);
324+
insertNewNode(index, value);
298325
insertNewElemConnect(index, parent);
326+
// If first node -> Position with a nice animation
327+
if (control.getAlgorithm().getArrayLength() == 1)
328+
positioningNodes(1500);
299329
};
300330
}(child, value, parent);
301331
var backward = function (index, parent) {
302332
return function () {
303333
allNodes[parent].removeChild(allNodes[index]);
304-
removeElem(index, true);
334+
//removeElem(index, true);
335+
setValueAtIndex(index, "");
336+
removeNode(index);
305337
};
306338
}(child, parent);
307339
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));

Heap/js/View.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ class View implements IView {
120120
}
121121
}(index, color);
122122

123-
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
123+
let backward = function (index: number) {
124+
return function () {
125+
removeHighlight(index);
126+
}
127+
}(index);
128+
129+
manager.addEvent(new FrontendEvent(forward, backward, this.animSpeed));
124130
}
125131

126132
highlightThisSortElem(index: number, color: string) {
@@ -135,12 +141,27 @@ class View implements IView {
135141

136142

137143
removeThisHighlight(index: number) {
144+
// Find the current color
145+
let color: string = "";
146+
var classList = document.getElementById('arrayElem' + index).className.split(/\s+/);
147+
for (var i = 0; i < classList.length; i++) {
148+
if (classList[i] === 'orange' || classList[i] === 'green') {
149+
color = classList[i];
150+
}
151+
}
152+
138153
let forward = function (index: number) {
139154
return function () {
140155
removeHighlight(index);
141156
}
142157
}(index);
143158

159+
let backward = function (index: number, color: string) {
160+
return function () {
161+
highlightNode(index, color);
162+
}
163+
}(index, color);
164+
144165
manager.addEvent(new FrontendEvent(forward, forward, this.animSpeed));
145166
}
146167

@@ -153,7 +174,10 @@ class View implements IView {
153174
}
154175

155176
stepForward(twoDimRelationshipsJSON: string, backendArray: string) {
156-
this.step("forward", twoDimRelationshipsJSON, backendArray);
177+
//this.step("forward", twoDimRelationshipsJSON, backendArray);
178+
manager.next();
179+
if (manager.nextEvents.length <= 0)
180+
manager.start();
157181
}
158182

159183
step(dir: string, twoDimRelationshipsJSON: string, backendArray: string) {
@@ -196,7 +220,12 @@ class View implements IView {
196220
}
197221
}(locked);
198222

199-
manager.addEvent(new FrontendEvent(lck, lck, this.animSpeed));
223+
let notLck = function (lock: boolean) {
224+
return function () {
225+
screenLock(!lock);
226+
}
227+
}(locked);
228+
manager.addEvent(new FrontendEvent(lck, notLck, this.animSpeed));
200229
}
201230

202231
setSlow() {
@@ -345,15 +374,22 @@ class View implements IView {
345374
insertNewElemThis(child: number, value: number, parent: number) {
346375
let forward = function (index, value, parent) {
347376
return function () {
348-
insertNewElem(index, value);
377+
setValueAtIndex(index, value);
378+
insertNewNode(index, value);
349379
insertNewElemConnect(index, parent);
380+
381+
// If first node -> Position with a nice animation
382+
if (control.getAlgorithm().getArrayLength() == 1)
383+
positioningNodes(1500);
350384
}
351385
}(child, value, parent);
352386

353387
let backward = function (index, parent) {
354388
return function () {
355389
allNodes[parent].removeChild(allNodes[index]);
356-
removeElem(index, true);
390+
//removeElem(index, true);
391+
setValueAtIndex(index, "");
392+
removeNode(index);
357393
}
358394
}(child, parent);
359395

Heap/js/methods.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,17 @@ function removeElem(i, delArray) {
153153
});
154154
if (delArray)
155155
$("#arrayElem" + i).remove();
156-
//allNodes[i].reset();
157-
$("#node" + i).fadeOut(2000, function () {
158-
$(this).remove();
159-
});
160-
allNodes[i].parent.removeChild(allNodes[i]);
161-
//allNodes[i].reset();
162-
allNodes.pop();
156+
removeNode(i);
163157
}, 1000);
164158
}
159+
function removeNode(i) {
160+
$("#node" + i).fadeOut(2000, function () {
161+
$(this).remove();
162+
});
163+
allNodes[i].parent.removeChild(allNodes[i]);
164+
//allNodes[i].reset();
165+
allNodes.pop();
166+
}
165167
// Swap position of two graphNodes
166168
function swapNodes(child, parent) {
167169
if (allNodes[parent] === undefined)
@@ -340,7 +342,8 @@ function stepBack() {
340342
firstSelected = -1;
341343
}
342344
else {
343-
viewer.stepBack(getGraphState(), getArrayState());
345+
//viewer.stepBack(getGraphState(), getArrayState());
346+
manager.previous();
344347
}
345348
}
346349
function setHeaderText(text) {

Heap/js/methods.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ function setArrow(index: number) {
114114
var $arrow = $("#arrow");
115115
if (index == -1) {
116116
$arrow.addClass("hidden");
117-
$arrow.animate({left: ($("#sortArrayElem0").position().left + 9) + "px"}, 0);
117+
$arrow.animate({ left: ($("#sortArrayElem0").position().left + 9) + "px" }, 0);
118118
return;
119119
}
120120
var left: number = $("#sortArrayElem" + index).position().left + 9;
121121
$arrow.removeClass("hidden");
122-
$arrow.animate({left: left + "px"}, 200);
122+
$arrow.animate({ left: left + "px" }, 200);
123123
}
124124

125125
// New value in arrayElem
@@ -141,7 +141,7 @@ function setValueAtSortIndex(i: number, value) {
141141
function insertNewElem(i: number, val: number): void {
142142
$("#arrayUL").append("<li id='arrayElem" + i + "'><div class='index'>" + i + "</div><div class='content' id='arrayContent" + i + "'>" + val + "</div></li>");
143143
var left = (i * 70) + "px";
144-
$("#arrayElem" + i).animate({left: left}, 0);
144+
$("#arrayElem" + i).animate({ left: left }, 0);
145145

146146
insertNewNode(i, val);
147147
}
@@ -182,17 +182,20 @@ function removeElem(i: number, delArray: boolean) {
182182
if (delArray)
183183
$("#arrayElem" + i).remove();
184184

185-
//allNodes[i].reset();
186-
$("#node" + i).fadeOut(2000, function () {
187-
$(this).remove();
188-
});
189-
190-
allNodes[i].parent.removeChild(allNodes[i]);
191-
//allNodes[i].reset();
192-
allNodes.pop();
185+
removeNode(i);
193186
}, 1000);
194187
}
195188

189+
function removeNode(i: number) {
190+
$("#node" + i).fadeOut(2000, function () {
191+
$(this).remove();
192+
});
193+
194+
allNodes[i].parent.removeChild(allNodes[i]);
195+
//allNodes[i].reset();
196+
allNodes.pop();
197+
}
198+
196199
// Swap position of two graphNodes
197200
function swapNodes(child: number, parent: number) {
198201
if (allNodes[parent] === undefined)
@@ -212,7 +215,7 @@ function swapNodes(child: number, parent: number) {
212215
}, 1000, function () {
213216
allNodes[parent].changeValue(allNodes[child].value);
214217
// Reset node position
215-
$("#node" + parent).css({'left': pLeft + 'px', 'top': pTop + 'px'});
218+
$("#node" + parent).css({ 'left': pLeft + 'px', 'top': pTop + 'px' });
216219
});
217220
$("#node" + child).animate({
218221
left: allNodes[parent].left + 'px',
@@ -222,7 +225,7 @@ function swapNodes(child: number, parent: number) {
222225
return;
223226
allNodes[child].changeValue(tmp);
224227
// Reset node position
225-
$("#node" + child).css({'left': cLeft + 'px', 'top': cTop + 'px'});
228+
$("#node" + child).css({ 'left': cLeft + 'px', 'top': cTop + 'px' });
226229

227230
});
228231
}
@@ -372,8 +375,8 @@ function setState(backendArrayJSON: string, twoDimRelationshipArrayJSON: string)
372375
function screenLock(lock: boolean) {
373376
locked = lock;
374377
if (lock) {
375-
$("#addElem").attr({"disabled": "true"});
376-
$("#removeElem").attr({"disabled": "true"});
378+
$("#addElem").attr({ "disabled": "true" });
379+
$("#removeElem").attr({ "disabled": "true" });
377380

378381
} else {
379382
$("#addElem").removeAttr("disabled");
@@ -383,7 +386,7 @@ function screenLock(lock: boolean) {
383386

384387
function lockPlay(lock: boolean) {
385388
if (lock) {
386-
$("#play").attr({"disabled": "true"});
389+
$("#play").attr({ "disabled": "true" });
387390
} else {
388391
$("#play").removeAttr("disabled");
389392
}
@@ -394,7 +397,8 @@ function stepBack() {
394397
selectIndex(firstSelected, false);
395398
firstSelected = -1;
396399
} else {
397-
viewer.stepBack(getGraphState(), getArrayState());
400+
//viewer.stepBack(getGraphState(), getArrayState());
401+
manager.previous();
398402
}
399403
}
400404

0 commit comments

Comments
 (0)