Skip to content

Commit d5114e3

Browse files
committed
Add layout feature
1 parent 8499a51 commit d5114e3

12 files changed

+158
-97
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "algorithm-visualizer",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "Visualization Library for JavaScript",
55
"keywords": [
66
"algorithm",

src/Array1DTracer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ import { Array2DTracer, ChartTracer } from './';
22

33
class Array1DTracer extends Array2DTracer {
44
set(array1d?: any[]): this {
5-
return this.addTrace('set', arguments);
5+
return this.command('set', arguments);
66
}
77

88
patch(x: number, v?: any): this {
9-
return this.addTrace('patch', arguments);
9+
return this.command('patch', arguments);
1010
}
1111

1212
depatch(x: number): this {
13-
return this.addTrace('depatch', arguments);
13+
return this.command('depatch', arguments);
1414
}
1515

1616
select(sx: number, ex?: number): this {
17-
return this.addTrace('select', arguments);
17+
return this.command('select', arguments);
1818
}
1919

2020
deselect(sx: number, ex?: number): this {
21-
return this.addTrace('deselect', arguments);
21+
return this.command('deselect', arguments);
2222
}
2323

2424
chart(chartTracer: ChartTracer): this {
25-
return this.addTrace('chart', arguments);
25+
return this.command('chart', arguments);
2626
}
2727
}
2828

src/Array2DTracer.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@ import { Tracer } from './';
22

33
class Array2DTracer extends Tracer {
44
set(array2d?: any[][]): this {
5-
return this.addTrace('set', arguments);
5+
return this.command('set', arguments);
66
}
77

88
patch(x: number, y: number, v?: any): this {
9-
return this.addTrace('patch', arguments);
9+
return this.command('patch', arguments);
1010
}
1111

1212
depatch(x: number, y: number): this {
13-
return this.addTrace('depatch', arguments);
13+
return this.command('depatch', arguments);
1414
}
1515

1616
select(sx: number, sy: number, ex?: number, ey?: number): this {
17-
return this.addTrace('select', arguments);
17+
return this.command('select', arguments);
1818
}
1919

2020
selectRow(x: number, sy: number, ey: number): this {
21-
return this.addTrace('selectRow', arguments);
21+
return this.command('selectRow', arguments);
2222
}
2323

2424
selectCol(y: number, sx: number, ex: number): this {
25-
return this.addTrace('selectCol', arguments);
25+
return this.command('selectCol', arguments);
2626
}
2727

2828
deselect(sx: number, sy: number, ex?: number, ey?: number): this {
29-
return this.addTrace('deselect', arguments);
29+
return this.command('deselect', arguments);
3030
}
3131

3232
deselectRow(x: number, sy: number, ey: number): this {
33-
return this.addTrace('deselectRow', arguments);
33+
return this.command('deselectRow', arguments);
3434
}
3535

3636
deselectCol(y: number, sx: number, ex: number): this {
37-
return this.addTrace('deselectCol', arguments);
37+
return this.command('deselectCol', arguments);
3838
}
3939
}
4040

src/Commander.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Randomize } from './';
2+
3+
const MAX_COMMANDS = 1000000;
4+
const MAX_OBJECTS = 100;
5+
6+
interface Command {
7+
key: string | null,
8+
method: string,
9+
args: Array<any>,
10+
}
11+
12+
class Commander {
13+
private static keyRandomizer = new Randomize.String(8, 'abcdefghijklmnopqrstuvwxyz0123456789');
14+
private static objectCount = 0;
15+
public static commands: Command[] = [];
16+
17+
static command(key: string | null, method: string, iArguments: IArguments): void {
18+
const args = Array.from(iArguments);
19+
this.commands.push({
20+
key,
21+
method,
22+
args: JSON.parse(JSON.stringify(args)),
23+
});
24+
if (this.commands.length > MAX_COMMANDS) throw new Error('Too Many Commands');
25+
if (this.objectCount > MAX_OBJECTS) throw new Error('Too Many Objects');
26+
}
27+
28+
static setRoot(child: Commander) {
29+
this.command(null, 'setRoot', arguments);
30+
}
31+
32+
static delay() {
33+
this.command(null, 'delay', arguments);
34+
}
35+
36+
private readonly key: string;
37+
38+
constructor(iArguments: IArguments) {
39+
const className = (<any>this).constructor.name;
40+
this.key = Commander.keyRandomizer.create();
41+
this.command(className, iArguments);
42+
}
43+
44+
destroy() {
45+
Commander.objectCount--;
46+
return this.command('destroy', arguments);
47+
}
48+
49+
command(method: string, iArguments: IArguments): this {
50+
Commander.command(this.key, method, iArguments);
51+
return this;
52+
}
53+
54+
delay(): this {
55+
Commander.delay();
56+
return this;
57+
}
58+
59+
toJSON() {
60+
return this.key;
61+
}
62+
}
63+
64+
const {ALGORITHM_VISUALIZER} = process.env;
65+
if (!ALGORITHM_VISUALIZER) {
66+
const axios = require('axios');
67+
const opn = require('opn');
68+
process.on('beforeExit', () => {
69+
axios.post('https://algorithm-visualizer.org/api/visualizations', {content: JSON.stringify(Commander.commands)})
70+
.then((response: any) => opn(response.data, {wait: false}))
71+
.catch(console.error)
72+
.finally(() => process.exit());
73+
});
74+
}
75+
76+
export default Commander;

src/GraphTracer.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,71 @@ import { LogTracer, Tracer } from './';
22

33
class GraphTracer extends Tracer {
44
set(array2d?: any[][]): this {
5-
return this.addTrace('set', arguments);
5+
return this.command('set', arguments);
66
}
77

88
directed(isDirected?: boolean): this {
9-
return this.addTrace('directed', arguments);
9+
return this.command('directed', arguments);
1010
}
1111

1212
weighted(isWeighted?: boolean): this {
13-
return this.addTrace('weighted', arguments);
13+
return this.command('weighted', arguments);
1414
}
1515

1616
addNode(id: any, weight?: any, x?: number, y?: number, visitedCount?: number, selectedCount?: number): this {
17-
return this.addTrace('addNode', arguments);
17+
return this.command('addNode', arguments);
1818
}
1919

2020
updateNode(id: any, weight?: any, x?: number, y?: number, visitedCount?: number, selectedCount?: number): this {
21-
return this.addTrace('updateNode', arguments);
21+
return this.command('updateNode', arguments);
2222
}
2323

2424
removeNode(id: any): this {
25-
return this.addTrace('removeNode', arguments);
25+
return this.command('removeNode', arguments);
2626
}
2727

2828
addEdge(source: any, target: any, weight?: any, visitedCount?: number, selectedCount?: number): this {
29-
return this.addTrace('addEdge', arguments);
29+
return this.command('addEdge', arguments);
3030
}
3131

3232
updateEdge(source: any, target: any, weight?: any, visitedCount?: number, selectedCount?: number): this {
33-
return this.addTrace('updateEdge', arguments);
33+
return this.command('updateEdge', arguments);
3434
}
3535

3636
removeEdge(source: any, target: any): this {
37-
return this.addTrace('removeEdge', arguments);
37+
return this.command('removeEdge', arguments);
3838
}
3939

4040
layoutCircle() {
41-
return this.addTrace('layoutCircle', arguments);
41+
return this.command('layoutCircle', arguments);
4242
}
4343

4444
layoutTree(root?: any, sorted?: boolean) {
45-
return this.addTrace('layoutTree', arguments);
45+
return this.command('layoutTree', arguments);
4646
}
4747

4848
layoutRandom() {
49-
return this.addTrace('layoutRandom', arguments);
49+
return this.command('layoutRandom', arguments);
5050
}
5151

5252
visit(target: any, source?: any, weight?: any) {
53-
return this.addTrace('visit', arguments);
53+
return this.command('visit', arguments);
5454
}
5555

5656
leave(target: any, source?: any, weight?: any) {
57-
return this.addTrace('leave', arguments);
57+
return this.command('leave', arguments);
5858
}
5959

6060
select(target: any, source?: any) {
61-
return this.addTrace('select', arguments);
61+
return this.command('select', arguments);
6262
}
6363

6464
deselect(target: any, source?: any) {
65-
return this.addTrace('deselect', arguments);
65+
return this.command('deselect', arguments);
6666
}
6767

6868
log(logTracer: LogTracer) {
69-
return this.addTrace('log', arguments);
69+
return this.command('log', arguments);
7070
}
7171
}
7272

src/HorizontalLayout.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Layout } from './';
2+
3+
class HorizontalLayout extends Layout {
4+
}
5+
6+
export default HorizontalLayout;

src/Layout.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Commander } from './';
2+
3+
class Layout extends Commander {
4+
constructor(children: [Commander]) {
5+
super(arguments);
6+
}
7+
8+
add(child: Commander, index?: Number): this {
9+
return this.command('add', arguments);
10+
}
11+
12+
remove(child: Commander): this {
13+
return this.command('remove', arguments);
14+
}
15+
16+
removeAll(): this {
17+
return this.command('removeAll', arguments);
18+
}
19+
}
20+
21+
export default Layout;

src/LogTracer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import { Tracer } from './';
22

33
class LogTracer extends Tracer {
44
set(log?: string): this {
5-
return this.addTrace('set', arguments);
5+
return this.command('set', arguments);
66
}
77

88
print(message: any): this {
9-
return this.addTrace('print', arguments);
9+
return this.command('print', arguments);
1010
}
1111

1212
println(message: any): this {
13-
return this.addTrace('println', arguments);
13+
return this.command('println', arguments);
1414
}
1515

1616
printf(format: string, ...args: any[]): this {
17-
return this.addTrace('printf', arguments);
17+
return this.command('printf', arguments);
1818
}
1919
}
2020

src/Tracer.ts

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,17 @@
1-
const MAX_TRACES = 1000000;
2-
const MAX_TRACERS = 100;
3-
4-
interface Trace {
5-
tracerKey: string,
6-
method: string,
7-
args: Array<any>,
8-
}
9-
10-
class Tracer {
11-
private static tracerCount: number = 0;
12-
public static traces: Trace[] = [];
13-
private readonly key: string;
14-
15-
static addTracer(className: string, title: string): string {
16-
const key = `${this.tracerCount++}-${className}-${title}`;
17-
const method = 'construct';
18-
const args = [className, title];
19-
this.addTrace(key, method, args);
20-
return key;
21-
}
22-
23-
static addTrace(tracerKey: string, method: string, args: any[]): void {
24-
this.traces.push({
25-
tracerKey,
26-
method,
27-
args: JSON.parse(JSON.stringify(args)),
28-
});
29-
if (this.traces.length > MAX_TRACES) throw new Error('Traces Limit Exceeded');
30-
if (this.tracerCount > MAX_TRACERS) throw new Error('Tracers Limit Exceeded');
31-
}
1+
import { Commander } from './';
322

3+
class Tracer extends Commander {
334
constructor(title?: string) {
34-
const className: string = (<any>this).constructor.name;
35-
if (title === undefined) title = className;
36-
this.key = Tracer.addTracer(className, title);
37-
}
38-
39-
addTrace(method: string, iArguments: IArguments): this {
40-
const args = Array.from(iArguments).map(arg => arg instanceof Tracer ? arg.key : arg);
41-
Tracer.addTrace(this.key, method, args);
42-
return this;
5+
super(arguments);
436
}
447

458
set(): this {
46-
return this.addTrace('set', arguments);
9+
return this.command('set', arguments);
4710
}
4811

4912
reset(): this {
50-
return this.addTrace('reset', arguments);
51-
}
52-
53-
delay(): this {
54-
return this.addTrace('delay', arguments);
13+
return this.command('reset', arguments);
5514
}
5615
}
5716

58-
const {ALGORITHM_VISUALIZER} = process.env;
59-
if (!ALGORITHM_VISUALIZER) {
60-
const axios = require('axios');
61-
const opn = require('opn');
62-
process.on('beforeExit', () => {
63-
axios.post('https://algorithm-visualizer.org/api/visualizations', {content: JSON.stringify(Tracer.traces)})
64-
.then((response: any) => opn(response.data, {wait: false}))
65-
.catch(console.error)
66-
.finally(() => process.exit());
67-
});
68-
}
69-
7017
export default Tracer;

src/VerticalLayout.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Layout } from './';
2+
3+
class VerticalLayout extends Layout {
4+
}
5+
6+
export default VerticalLayout;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy