Skip to content

Commit e17bd72

Browse files
Fkset disable/enable bug med forward
1 parent 78d6b4a commit e17bd72

File tree

2 files changed

+116
-93
lines changed

2 files changed

+116
-93
lines changed

Heap/js/View.js

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ var View = /** @class */ (function () {
1313
function View() {
1414
this.colors = ["#7FFF00", "not used", "#FFB366"];
1515
this.k = 0;
16-
this.paused = false;
16+
this.started = false;
1717
this.playing = false;
1818
this.animSpeed = 500;
19+
this.clickedPlay = true;
1920
}
2021
View.prototype.displayThisArray = function (array) {
2122
displayArray(JSON.stringify(array));
@@ -145,9 +146,12 @@ var View = /** @class */ (function () {
145146
};
146147
View.prototype.stepForward = function (twoDimRelationshipsJSON, backendArray) {
147148
//this.step("forward", twoDimRelationshipsJSON, backendArray);
149+
this.clickedPlay = false;
148150
manager.next();
149-
if (manager.nextEvents.length <= 0)
151+
if (manager.nextEvents.length <= 0) {
152+
this.playing = true;
150153
manager.start();
154+
}
151155
};
152156
View.prototype.step = function (dir, twoDimRelationshipsJSON, backendArray) {
153157
var relationships = JSON.parse(twoDimRelationshipsJSON);
@@ -158,7 +162,7 @@ var View = /** @class */ (function () {
158162
stepper.stepBack(relationships, backendArr);
159163
};
160164
View.prototype.resetAll = function () {
161-
this.paused = false;
165+
this.started = false;
162166
this.playing = false;
163167
$("#play").text("Play");
164168
manager.pause();
@@ -299,74 +303,82 @@ var View = /** @class */ (function () {
299303
}(i, val, Math.floor((i - 1) / 2));
300304
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));
301305
};
306+
View.prototype.insertNewElemThis = function (child, value, parent) {
307+
var forward = function (index, value, parent) {
308+
return function () {
309+
setValueAtIndex(index, value);
310+
insertNewNode(index, value);
311+
insertNewElemConnect(index, parent);
312+
// If first node -> Position with a nice animation
313+
if (control.getAlgorithm().getArrayLength() == 1)
314+
positioningNodes(1500);
315+
};
316+
}(child, value, parent);
317+
var backward = function (index, parent) {
318+
return function () {
319+
allNodes[parent].removeChild(allNodes[index]);
320+
//removeElem(index, true);
321+
setValueAtIndex(index, "");
322+
removeNode(index);
323+
};
324+
}(child, parent);
325+
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));
326+
};
302327
View.prototype.play = function () {
328+
this.clickedPlay = true;
303329
var algo = control.getAlgorithm().getName();
304-
if (algo === "BuildHeap" && !this.paused && !this.playing) {
330+
if (algo === "BuildHeap" && !this.started && !this.playing) {
305331
control.getAlgorithm().build();
306-
this.paused = true;
307-
this.playing = true;
308-
$("#play").text("Pause");
309-
lockBackForward(true);
332+
this.started = true;
333+
this.setPause(false);
310334
}
311-
else if (algo === "HeapSort" && !this.paused && !this.playing) {
335+
else if (algo === "HeapSort" && !this.started && !this.playing) {
312336
control.getAlgorithm().sort();
313-
this.paused = true;
314-
this.playing = true;
315-
$("#play").text("Pause");
316-
lockBackForward(true);
337+
this.started = true;
338+
this.setPause(false);
317339
}
318340
else {
319341
if (this.playing) {
320-
manager.pause();
321-
$("#play").text("Resume");
322-
this.playing = false;
323-
lockBackForward(false);
342+
this.setPause(true);
324343
}
325344
else {
326-
this.playing = true;
327-
manager.start();
328-
$("#play").text("Pause");
329-
lockBackForward(true);
345+
this.setPause(false);
330346
}
331347
}
332348
};
349+
View.prototype.setPause = function (bool) {
350+
if (bool) {
351+
this.playing = false;
352+
manager.pause();
353+
$("#play").text("Resume");
354+
lockBackForward(false);
355+
}
356+
else {
357+
this.playing = true;
358+
manager.start();
359+
$("#play").text("Pause");
360+
lockBackForward(true);
361+
}
362+
};
333363
// Used in eventmanager for freemode and predefined
334364
View.prototype.playButtonState = function () {
335365
var algo = control.getAlgorithm().getName();
336-
if (!(algo === "MaxHeap" || algo === "MaxHeapFree" || this.playing))
366+
if (!(algo === "MaxHeap" || algo === "MaxHeapFree"))
337367
return;
338-
if (manager.nextEvents.length > 0) {
368+
if (manager.nextEvents.length > 0 && this.clickedPlay) {
339369
this.playing = true;
340370
lockPlay(false);
341371
lockBackForward(true);
342372
$("#play").text("Pause");
343373
}
374+
else if (manager.nextEvents.length > 0) {
375+
return;
376+
}
344377
else {
345378
lockPlay(true);
346379
lockBackForward(false);
347380
}
348381
};
349-
View.prototype.insertNewElemThis = function (child, value, parent) {
350-
var forward = function (index, value, parent) {
351-
return function () {
352-
setValueAtIndex(index, value);
353-
insertNewNode(index, value);
354-
insertNewElemConnect(index, parent);
355-
// If first node -> Position with a nice animation
356-
if (control.getAlgorithm().getArrayLength() == 1)
357-
positioningNodes(1500);
358-
};
359-
}(child, value, parent);
360-
var backward = function (index, parent) {
361-
return function () {
362-
allNodes[parent].removeChild(allNodes[index]);
363-
//removeElem(index, true);
364-
setValueAtIndex(index, "");
365-
removeNode(index);
366-
};
367-
}(child, parent);
368-
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));
369-
};
370382
return View;
371383
}());
372384
var viewer = new View();

Heap/js/View.ts

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class View implements IView {
1818

1919
colors: string[] = ["#7FFF00", "not used", "#FFB366"];
2020
k: number = 0;
21-
paused: boolean = false;
21+
started: boolean = false;
2222
playing: boolean = false;
2323
animSpeed: number = 500;
2424

@@ -180,9 +180,12 @@ class View implements IView {
180180

181181
stepForward(twoDimRelationshipsJSON: string, backendArray: string) {
182182
//this.step("forward", twoDimRelationshipsJSON, backendArray);
183+
this.clickedPlay = false;
183184
manager.next();
184-
if (manager.nextEvents.length <= 0)
185+
if (manager.nextEvents.length <= 0) {
186+
this.playing = true;
185187
manager.start();
188+
}
186189
}
187190

188191
step(dir: string, twoDimRelationshipsJSON: string, backendArray: string) {
@@ -195,7 +198,7 @@ class View implements IView {
195198
}
196199

197200
resetAll() {
198-
this.paused = false;
201+
this.started = false;
199202
this.playing = false;
200203
$("#play").text("Play");
201204
manager.pause();
@@ -356,52 +359,6 @@ class View implements IView {
356359
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));
357360
}
358361

359-
play() {
360-
let algo = control.getAlgorithm().getName();
361-
if (algo === "BuildHeap" && !this.paused && !this.playing) {
362-
control.getAlgorithm().build();
363-
this.paused = true;
364-
this.playing = true;
365-
$("#play").text("Pause");
366-
lockBackForward(true);
367-
} else if (algo === "HeapSort" && !this.paused && !this.playing) {
368-
(<HeapSort>control.getAlgorithm()).sort();
369-
this.paused = true;
370-
this.playing = true;
371-
$("#play").text("Pause");
372-
lockBackForward(true);
373-
} else {
374-
if (this.playing) {
375-
manager.pause();
376-
$("#play").text("Resume");
377-
this.playing = false;
378-
lockBackForward(false);
379-
} else {
380-
this.playing = true;
381-
manager.start();
382-
$("#play").text("Pause");
383-
lockBackForward(true);
384-
}
385-
}
386-
}
387-
388-
// Used in eventmanager for freemode and predefined
389-
playButtonState() {
390-
let algo = control.getAlgorithm().getName();
391-
if (!(algo === "MaxHeap" || algo === "MaxHeapFree" || this.playing))
392-
return;
393-
394-
if (manager.nextEvents.length > 0) {
395-
this.playing = true;
396-
lockPlay(false);
397-
lockBackForward(true);
398-
$("#play").text("Pause");
399-
} else {
400-
lockPlay(true);
401-
lockBackForward(false);
402-
}
403-
}
404-
405362
insertNewElemThis(child: number, value: number, parent: number) {
406363
let forward = function (index, value, parent) {
407364
return function () {
@@ -428,6 +385,60 @@ class View implements IView {
428385
manager.addEvent(new FrontendEvent(forward, backward, manager.delayTime));
429386

430387
}
388+
389+
clickedPlay = true;
390+
play() {
391+
this.clickedPlay = true;
392+
let algo = control.getAlgorithm().getName();
393+
if (algo === "BuildHeap" && !this.started && !this.playing) {
394+
control.getAlgorithm().build();
395+
this.started = true;
396+
this.setPause(false);
397+
} else if (algo === "HeapSort" && !this.started && !this.playing) {
398+
(<HeapSort>control.getAlgorithm()).sort();
399+
this.started = true;
400+
this.setPause(false);
401+
} else {
402+
if (this.playing) {
403+
this.setPause(true);
404+
} else {
405+
this.setPause(false);
406+
}
407+
}
408+
}
409+
410+
setPause(bool: boolean) {
411+
if (bool) {
412+
this.playing = false;
413+
manager.pause();
414+
$("#play").text("Resume");
415+
lockBackForward(false);
416+
} else {
417+
this.playing = true;
418+
manager.start();
419+
$("#play").text("Pause");
420+
lockBackForward(true);
421+
}
422+
}
423+
424+
// Used in eventmanager for freemode and predefined
425+
playButtonState() {
426+
let algo = control.getAlgorithm().getName();
427+
if (!(algo === "MaxHeap" || algo === "MaxHeapFree"))
428+
return;
429+
430+
if (manager.nextEvents.length > 0 && this.clickedPlay) {
431+
this.playing = true;
432+
lockPlay(false);
433+
lockBackForward(true);
434+
$("#play").text("Pause");
435+
} else if (manager.nextEvents.length > 0) {
436+
return;
437+
} else {
438+
lockPlay(true);
439+
lockBackForward(false);
440+
}
441+
}
431442
}
432443

433444
var viewer: View = new View();

0 commit comments

Comments
 (0)