forked from sysoevss/LOQC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlo_designer.html
791 lines (731 loc) · 30.3 KB
/
lo_designer.html
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Linear Optics Designer">
<meta name="author" content="Sergey Sysoev">
<link href="/css/bootstrap.min.css" rel="stylesheet">
<style>
hr {margin: 10px 0px 0px 0px}
body {margin: 10px}
a.loqc_button:link, a.loqc_button:visited {
background-color: rgba(0, 100, 180, 0.7);
color: white;
padding: 6px 25px;
margin: 1px 1px 1px 1px;
text-align: center;
text-decoration: none;
display: inline-block;
width: 120px;
}
a.loqc_button:hover, a.loqc_button:active {
background-color: rgba(0, 100, 180, 0.5);
}
a.loqc_button_2:link, a.loqc_button_2:visited {
background-color: rgba(0, 140, 160, 0.7);
color: white;
padding: 7px 25px;
margin: 1px 1px 1px 1px;
text-align: center;
text-decoration: none;
display: inline-block;
width: 120px;
}
a.loqc_button_2:hover, a.loqc_button_2:active {
background-color: rgba(0, 140, 160, 0.5);
}
a.loqc_button_2_inactive:link, a.loqc_button_2_inactive:visited {
background-color: rgba(0, 140, 160, 0.3);
color: white;
padding: 7px 25px;
margin: 1px 1px 1px 1px;
text-align: center;
text-decoration: none;
display: inline-block;
width: 120px;
}
a.loqc_button_2_inactive:hover, a.loqc_button_2_inactive:active {
background-color: rgba(0, 140, 160, 0.3);
}
a.loqc_button_alert:link, a.loqc_button_alert:visited {
background-color: rgba(255, 140, 60, 0.7);
color: white;
padding: 7px 25px;
margin: 1px 1px 1px 1px;
text-align: center;
text-decoration: none;
display: inline-block;
width: 120px;
}
a.loqc_button_alert:hover, a.loqc_button_alert:active {
background-color: rgba(255, 140, 60, 0.5);
}
body {
height: 100%;
background-color: #fff;
}
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<script type="text/javascript" src="/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/js/jquery.form.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui.min.js"></script>
<script type="text/javascript" src='/js/pdfmake.min.js'></script>
<script src="/js/draw2d.js"></script>
<script type="text/javascript">
var canvas;
var cur_figure;
// Function to download data to a file
// Thanks to: https://stackoverflow.com/questions/13405129/create-and-save-a-file-with-javascript
//
function download(data, filename) {
var file = new Blob([data], {type: 'text/plain'});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
// Thanks to https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
var show_port = function(){this.setVisible(true);};
var hide_port = function(){this.setVisible(false);};
InputFigure = draw2d.shape.basic.Polygon.extend({
init : function(attr)
{
this._super($.extend({bgColor:{red: 100, green: 170, blue: 255, alpha: 0.5},color:"#1B1B1B"},attr));
this.resetVertices();
var box = this.getBoundingBox();
this.addVertex(0, 0);
this.addVertex(box.w, box.h/2);
this.addVertex(0, box.h);
this.installEditPolicy(new draw2d.policy.figure.RectangleSelectionFeedbackPolicy());
this.setPosition(box.getTopLeft());
}
});
OutputFigure = draw2d.shape.basic.Polygon.extend({
init : function(attr)
{
this._super($.extend({bgColor:{red: 100, green: 170, blue: 255, alpha: 0.5},color:"#1B1B1B"},attr));
this.resetVertices();
var box = this.getBoundingBox();
this.addVertex(0, box.h/2);
this.addVertex(box.w, 0);
this.addVertex(box.w, box.h);
this.installEditPolicy(new draw2d.policy.figure.RectangleSelectionFeedbackPolicy());
this.setPosition(box.getTopLeft());
}
});
document.addEventListener("DOMContentLoaded",function () {
canvas = new draw2d.Canvas("gfx_holder");
$("svg").css('left', '');
$("svg").css('top', '');
canvas.installEditPolicy(new draw2d.policy.canvas.ShowGridEditPolicy());
canvas.installEditPolicy(new draw2d.policy.canvas.SnapToGeometryEditPolicy());
canvas.installEditPolicy(new draw2d.policy.canvas.SnapToInBetweenEditPolicy());
canvas.installEditPolicy(new draw2d.policy.canvas.SnapToCenterEditPolicy());
// this is to fix wrong coordinates on canvas scrolling
// https://stackoverflow.com/questions/22073377/canvas-within-a-scrollable-browser-window-grab-position
canvas.setScrollArea($('#gfx_holder'));
canvas.fromDocumentToCanvasCoordinate = $.proxy(function(x, y) {
return new draw2d.geo.Point(
(x - this.getAbsoluteX() + $(document).scrollLeft())*this.zoomFactor/$('svg')[0].currentScale,
(y - this.getAbsoluteY() + $(document).scrollTop())*this.zoomFactor/$('svg')[0].currentScale);
},canvas);
canvas.fromCanvasToDocumentCoordinate = $.proxy(function(x,y) {
return new draw2d.geo.Point(
((x*(1/(this.zoomFactor/$('svg')[0].currentScale))) + this.getAbsoluteX() - $(document).scrollLeft()),
((y*(1/(this.zoomFactor/$('svg')[0].currentScale))) + this.getAbsoluteY() - $(document).scrollTop()));
},canvas);
// https://stackoverflow.com/questions/14883066/resize-listener-for-draw2d-touch
canvas.getCommandStack().addEventListener(function(e){
if(e.isPostChangeEvent()){
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
}
});
// add a very simple CnavasPolicy just to catch the onClick method
//
canvas.installEditPolicy( new draw2d.policy.canvas.CanvasPolicy({
onClick: function(figure){
cur_figure = null;
if(figure!=null) {
var fig;
var figure_types = ["BS", "PS", "IN", "OUT"];
if (figure_types.includes(figure["type"])) {
fig = figure;
} else {
if (figure.parent) {
if (figure_types.includes(figure.parent["type"]))
fig = figure.parent;
}
}
if (fig) {
cur_figure = fig;
$("#controls").show();
if (fig["type"] == "BS") {
$("#theta_input").val(parseFloat(fig["theta"]));
$("#theta").show();
} else {
$("#theta").hide();
}
if (fig["type"] == "BS" || fig["type"] == "PS") {
$("#phi_input").val(parseFloat(fig["phi"]));
$("#phi").show();
} else {
$("#phi").hide();
}
if (fig["type"] == "IN" || fig["type"] == "OUT") {
if (fig["N"] == -1) {
$("#n_input").val("");
} else {
$("#n_input").val(parseInt(fig["N"]));
}
$("#n").show();
} else {
$("#n").hide();
}
} else {
$("#controls").hide();
cur_figure = null;
}
}
}
}));
canvas.on("unselect", function(emitter,event){
$("#controls").hide();
cur_figure = null;
});
// Override the default connection type. This is used during drag&drop operations of ports.
//
var createConnection=function(sourcePort, targetPort){
// return my special kind of connection
var con = new draw2d.Connection();
con.setRouter(new draw2d.layout.connection.ManhattanConnectionRouter());
return con;
};
// Install a special policy into the canvas to use my own implementation of connection
// if we drag&drop a port
//
canvas.installEditPolicy( new draw2d.policy.connection.DragConnectionCreatePolicy({
createConnection: createConnection
}));
var MyInputPortLocatorTop = draw2d.layout.locator.PortLocator.extend({
init:function( ){
this._super();
},
relocate:function(index, figure){
this.applyConsiderRotation(figure, 10, 0);
}
});
var MyInputPortLocatorBottom = draw2d.layout.locator.PortLocator.extend({
init:function( ){
this._super();
},
relocate:function(index, figure){
var p = figure.getParent();
this.applyConsiderRotation(figure, 10, p.getHeight());
}
});
var MyOutputPortLocatorTop = draw2d.layout.locator.PortLocator.extend({
init:function( ){
this._super();
},
relocate:function(index, figure){
var p = figure.getParent();
this.applyConsiderRotation(figure, p.getWidth() - 10, 0);
}
});
var MyOutputPortLocatorBottom = draw2d.layout.locator.PortLocator.extend({
init:function( ){
this._super();
},
relocate:function(index, figure){
var p = figure.getParent();
this.applyConsiderRotation(figure, p.getWidth() - 10, p.getHeight());
}
});
function add_bs(x, y, theta, phi, id) {
var d = new draw2d.shape.basic.Rectangle({width:60, height:60, x:x, y:y});
d.setBackgroundColor({red: 200, green: 200, blue: 250, alpha: 0.5})
d["type"] = "BS";
d["theta"] = theta;
d["phi"] = phi;
if (id) {
d["id"] = id;
}
d.label = new draw2d.shape.basic.Label({text:Number(theta).toFixed(1) + "\n" + Number(phi).toFixed(1), fontColor:"#0d0d0d", stroke:0});
d.add(d.label, new draw2d.layout.locator.CenterLocator(d));
port1 = d.createPort("hybrid",new MyInputPortLocatorTop());
port2 = d.createPort("hybrid",new MyOutputPortLocatorTop());
port3 = d.createPort("hybrid",new MyInputPortLocatorBottom());
port4 = d.createPort("hybrid",new MyOutputPortLocatorBottom());
port1.on("connect", hide_port, port1);
port1.on("disconnect", show_port, port1);
port2.on("connect", hide_port, port2);
port2.on("disconnect", show_port, port2);
port3.on("connect", hide_port, port3);
port3.on("disconnect", show_port, port3);
port4.on("connect", hide_port, port4);
port4.on("disconnect", show_port, port4);
canvas.add( d);
}
$("#add_bs").click(function(e) {
e.preventDefault();
add_bs(100, 100, 45.0, 0.0, null);
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
});
function add_ps(x, y, phi, id) {
var d = new draw2d.shape.basic.Rectangle({width:35, height:60, x:x, y:y});
d.setBackgroundColor({red: 200, green: 200, blue: 200, alpha: 0.5})
d["type"] = "PS";
d["phi"] = phi;
if (id) {
d["id"] = id;
}
d.label = new draw2d.shape.basic.Label({text:Number(phi).toFixed(1), fontColor:"#0d0d0d", stroke:0});
d.add(d.label, new draw2d.layout.locator.CenterLocator(d));
var inputLocator = new draw2d.layout.locator.InputPortLocator({width:10, height:10});
var outputLocator = new draw2d.layout.locator.OutputPortLocator();
port1_in = d.createPort("hybrid",inputLocator);
port1_out = d.createPort("hybrid",outputLocator);
port1_in.on("connect", hide_port, port1_in);
port1_in.on("disconnect", show_port, port1_in);
port1_out.on("connect", hide_port, port1_out);
port1_out.on("disconnect", show_port, port1_out);
canvas.add( d);
}
$("#add_ps").click(function(e) {
e.preventDefault();
add_ps(100, 100, 180.0, null);
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
});
function add_input(x, y, N, id, input_type = 0) {
var d = new InputFigure({x: x, y:y, width:40, height:40});
if (input_type == 1) {
d.setBackgroundColor({red: 100, green: 100, blue: 100, alpha: 0.5});
}
if (input_type == 2) {
d.setBackgroundColor({red: 100, green: 250, blue: 100, alpha: 0.5});
}
d["type"] = "IN";
d["N"] = N;
d["input_type"] = input_type;
if (id) {
d["id"] = id;
}
label_text = N;
if (N == -1) label_text = "-";
d.label = new draw2d.shape.basic.Label({text:label_text, fontColor:"#0d0d0d", stroke:0});
d.add(d.label, new draw2d.layout.locator.LeftLocator(d));
var outputLocator = new draw2d.layout.locator.OutputPortLocator();
port1_out = d.createPort("hybrid",outputLocator);
port1_out.on("connect", hide_port, port1_out);
port1_out.on("disconnect", show_port, port1_out);
canvas.add( d);
}
$("#add_input").click(function(e) {
e.preventDefault();
add_input(100, 100, "-", null, 0);
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
});
$("#add_ancilla_input").click(function(e) {
e.preventDefault();
add_input(100, 100, "-", null, 1);
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
});
$("#add_control_input").click(function(e) {
e.preventDefault();
add_input(100, 100, "-", null, 2);
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
});
function add_output(x, y, N, id) {
var d = new OutputFigure({x: x, y:y, width:40, height:40});
d["type"] = "OUT";
d["N"] = N;
if (id) {
d["id"] = id;
}
label_text = N;
if (N == -1) label_text = "-";
d.label = new draw2d.shape.basic.Label({text:label_text, fontColor:"#0d0d0d", stroke:0});
if (N >= 0) {
d.setBackgroundColor({red: 100, green: 100, blue: 100, alpha: 0.5});
}
d.add(d.label, new draw2d.layout.locator.RightLocator(d));
var inputLocator = new draw2d.layout.locator.InputPortLocator();
port1_out = d.createPort("hybrid",inputLocator);
port1_out.on("connect", hide_port, port1_out);
port1_out.on("disconnect", show_port, port1_out);
canvas.add( d);
}
$("#add_output").click(function(e) {
e.preventDefault();
add_output(100, 100, "-", null);
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
});
function serialize_object(el, suffix = "") {
return {id: el.id + suffix,
type: el.type,
theta: String(el.theta),
phi: String(el.phi),
n: String(el.N),
input_type: String(el.input_type),
x: Math.round(el.x),
y: Math.round(el.y),
project_key: el.project_key};
}
function serialize_connection(el, suffix = "") {
return [{
type: "draw2d.Connection",
id: el.id + suffix,
alpha: 1,
selectable: true,
draggable: true,
angle: 0,
userData: {},
cssClass: "draw2d_Connection",
stroke: 2,
color: "rgba(18,156,228,1)",
outlineStroke: 0,
outlineColor: "rgba(0,0,0,0)",
policy: "draw2d.policy.line.VertexSelectionFeedbackPolicy",
vertex: el.vertices.data,
router: "draw2d.layout.connection.ManhattanConnectionRouter",
radius: 3,
source: {
node: el.getSource().parent.id + suffix,
port: el.sourcePort.name
},
target: {
"node": el.getTarget().parent.id + suffix,
"port": el.targetPort.name
}
}];
}
$("#save").click(function(e) {
e.preventDefault();
$.get('/clear_design/', {id: '{{ project_key }}' }, function(data) {
canvas.figures.data.forEach(el => {
$.post('/add_cycle_object/', {parent_key: '{{ project_key }}', object_type: "LODevice",
data: JSON.stringify(serialize_object(el)),}, function(data) {});
});
canvas.lines.data.forEach(el => {
var line_json = JSON.stringify(serialize_connection(el));
$.post('/add_cycle_object/', {parent_key: '{{ project_key }}', object_type: "LOConnection",
data: JSON.stringify({line_json: line_json})}, function(data) {}
);
});
$("#save").removeClass("loqc_button_alert");
$("#save").addClass("loqc_button_2_inactive");
//$("#activate").click();
});
});
$("#export").click(function(e) {
e.preventDefault();
exp_objects = [];
exp_connections = [];
random_suffix = makeid(16);
canvas.figures.data.forEach(el => {
exp_objects.push(serialize_object(el, random_suffix));
});
canvas.lines.data.forEach(el => {
exp_connections.push(serialize_connection(el, random_suffix));
});
data_to_save = JSON.stringify({objects: exp_objects, connections: exp_connections});
download(data_to_save, "loqc_tex_.txt");
//$("#activate").click();
});
$("#import").click(function(e) {
e.preventDefault();
// thanks to https://stackoverflow.com/questions/16215771/how-to-open-select-file-dialog-via-js
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
// getting a hold of the file reference
var file = e.target.files[0];
// setting up the reader
var reader = new FileReader();
reader.readAsText(file,'UTF-8');
// here we tell the reader what to do when it's done reading...
reader.onload = readerEvent => {
var content = readerEvent.target.result; // this is the content!
var reader_json = new draw2d.io.json.Reader();
data = JSON.parse(content);
data.objects.forEach(el => { load_object([0, el]) });
data.connections.forEach(el => {
reader_json.unmarshal(canvas, [el[0]]);
});
}
}
input.click();
//$("#activate").click();
});
$("#publish").click(function(e) {
e.preventDefault();
PublushProject(canvas);
//$("#activate").click();
});
$("#set").click(function(e) {
e.preventDefault();
//$("#activate").click();
if (cur_figure) {
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
if (cur_figure["type"] == "BS") {
theta = parseFloat($("#theta_input").val());
phi = parseFloat($("#phi_input").val());
cur_figure["theta"] = $("#theta_input").val();
cur_figure["phi"] = $("#phi_input").val();
cur_figure.label.text = Number((theta).toFixed(1)) + "\n" + Number((phi).toFixed(1));
cur_figure.label.repaint();
return;
}
if (cur_figure["type"] == "PS") {
phi = parseFloat($("#phi_input").val());
cur_figure["phi"] = $("#phi_input").val();
cur_figure.label.text = Number((phi).toFixed(1));
cur_figure.label.repaint();
return;
}
if (cur_figure["type"] == "IN" || cur_figure["type"] == "OUT") {
n_val = $("#n_input").val();
if (n_val) {
n = parseInt(n_val);
cur_figure["N"] = n_val;
cur_figure.label.text = n;
} else {
cur_figure["N"] = -1;
cur_figure.label.text = "-";
}
cur_figure.label.repaint();
return;
}
}
});
$("#scale_minus").click(function(e) {
e.preventDefault();
canvas.setZoom(canvas.getZoom()*1.1,true);
});
$("#scale_plus").click(function(e) {
e.preventDefault();
canvas.setZoom(canvas.getZoom()*0.91,true);
});
$("#delete_selection").click(function(e) {
e.preventDefault();
let to_del = canvas.getSelection().getAll().data;
for (let i = to_del.length - 1; i >= 0 ; i--) {
var command= new draw2d.command.CommandDelete(to_del[i]);
canvas.getCommandStack().execute(command);
}
$("#save").removeClass("loqc_button_2_inactive");
$("#save").addClass("loqc_button_alert");
});
$("#undo").click(function(e) {
e.preventDefault();
canvas.getCommandStack().undo();
});
$("#redo").click(function(e) {
e.preventDefault();
canvas.getCommandStack().redo();
});
// For some reason after this iframe starts to accept keyboard events
$("#activate").click(function(e) {
e.preventDefault();
//$("#activate").removeClass("loqc_button_2");
//$("#activate").addClass("loqc_button_2_inactive");
});
function load_object(el) {
// figures
if (el[1].type == "BS") {
add_bs(el[1].x, el[1].y, el[1].theta, el[1].phi, el[1].id);
}
if (el[1].type == "PS") {
add_ps(el[1].x, el[1].y, el[1].phi, el[1].id);
}
if (el[1].type == "IN") {
add_input(el[1].x, el[1].y, el[1].n, el[1].id, el[1].input_type);
}
if (el[1].type == "OUT") {
add_output(el[1].x, el[1].y, el[1].n, el[1].id);
}
if (el[1].type == "User Project") {
add_user_project(el[1].project_key, el[1].id, el[1].x, el[1].y);
}
}
function load_design() {
user_projects_to_load = [];
// Project params
$.get('/get_object_by_key/', {object_type: "Project", key: '{{ project_key }}'}, function(json) {
data = JSON.parse(json);
if (data["published"]) {
$("#save").hide();
$("#publish").html("Unpublish");
}
});
$.get('/cycle_objects/', {object_type: "LODevice", parent_key: '{{ project_key }}' }, function(json) {
data = JSON.parse(json);
data.forEach(el => { load_object(el) });
// lines
need_lines = true;
$(document).ajaxStop(function () {
if (!need_lines) return;
need_lines = false;
$.get('/cycle_objects/', {object_type: "LOConnection", parent_key: '{{ project_key }}' }, function(json) {
var reader = new draw2d.io.json.Reader();
data = JSON.parse(json);
data.forEach(el => {
reader.unmarshal(canvas, el[1]["line_json"]);
});
});
});
});
}
load_design();
});
var need_lines = false;
function PublushProject(canvas){
// convert the canvas into a PNG image source string
//
var xCoords = [];
var yCoords = [];
canvas.getFigures().each(function(i,f){
var b = f.getBoundingBox();
xCoords.push(b.x, b.x+b.w);
yCoords.push(b.y, b.y+b.h);
});
var minX = Math.min.apply(Math, xCoords);
var minY = Math.min.apply(Math, yCoords);
var width = Math.max.apply(Math, xCoords)-minX;
var height = Math.max.apply(Math, yCoords)-minY;
var writer = new draw2d.io.png.Writer();
writer.marshal(canvas,function(png){
$.post('/publish_project/', {key: '{{ project_key }}', png: png}, function(data) {
if (data != "OK") {
alert(data);
} else {
if ($("#publish").html() == "Unpublish") {
$("#publish").html("Publish");
$("#save").show();
} else {
$("#publish").html("Unpublish");
$("#save").hide();
}
//location.reload();
}
});
}, new draw2d.geo.Rectangle(minX,minY,width,height));
}
function add_user_project(key, id, x, y) {
$.get('/cycle_objects/', {object_type: "LOCircuit", parent_key: key}, function(json) {
data = JSON.parse(json);
if (data.length == 0) {
alert("Can't use this circuit. You need to execute it separately first to build its matrix");
return;
}
modes = parseInt(data[0][1].modes);
if (modes > 0) {
var d = new draw2d.shape.basic.Rectangle({width:60, height: 30*modes, x:x, y:y});
d.setBackgroundColor({red: 200, green: 200, blue: 250, alpha: 0.5})
d["type"] = "User Project";
d["project_key"] = key;
if (id) {
d["id"] = id;
}
d.label = new draw2d.shape.basic.Label({text:data[0][1].name, fontColor:"#0d0d0d", stroke:0});
d.add(d.label, new draw2d.layout.locator.CenterLocator(d));
for (i = 0; i < modes; i++) {
port1 = d.createPort("hybrid",new draw2d.layout.locator.InputPortLocator({width:10, height:10}));
port2 = d.createPort("hybrid",new draw2d.layout.locator.OutputPortLocator());
port1.on("connect", hide_port, port1);
port1.on("disconnect", show_port, port1);
port2.on("connect", hide_port, port2);
port2.on("disconnect", show_port, port2);
}
canvas.add(d);
} else {
alert("Can't use this circuit. You need to execute it separately first to build its matrix");
}
});
}
</script>
</head>
<body>
<table><tr>
<td style="width:180px;vertical-align: top;">
<a class="loqc_button_2_inactive" href="#" id='save' >Save</a>
<a class="loqc_button_2" href="#" id='delete_selection' title="Delete selection">Delete</a>
<a class="loqc_button_2" href="#" style="width:32px;" id='undo' title="Undo"><i class="icon-arrow-left"></i></a>
<a class="loqc_button_2" href="#" style="width:32px;" id='redo' title="Redo"><i class="icon-arrow-right"></i></a>
<a class="loqc_button_2" href="#" style="width:32px;" id='scale_minus' title="Zoom out"><i class="icon-zoom-out"></i></a>
<a class="loqc_button_2" href="#" style="width:32px;" id='scale_plus' title="Zoom-in"><i class="icon-zoom-in"></i></a>
<a class="loqc_button_2 tooltip" href="#" id='activate' style="display:none;">Unlock<span class="tooltiptext">Press me when 'Delete' doesn't work!</span></a>
<a class="loqc_button_2" href="#" id='publish' >Publish</a>
<a class="loqc_button_2" href="#" style="width:32px;" id='export' >Export</a>
<a class="loqc_button_2" href="#" style="width:32px;" id='import' >Import</a>
<hr></hr>
<a class="loqc_button" href="#" id='add_bs' >Beam Splitter</a>
<a class="loqc_button" href="#" id='add_ps' >Phase Shifter</a>
<a class="loqc_button" href="#" id='add_input' >Input</a>
<a class="loqc_button" href="#" id='add_ancilla_input' >Ancilla Input</a>
<a class="loqc_button" href="#" id='add_control_input' >Control Input</a>
<a class="loqc_button" href="#" id='add_output' >Photon Detection<!--<span class="tooltiptext">Single photon detection</span>--></a>
<table id="controls" style="display:none;">
<tr id="theta"><td style="width:20px;"><b>θ</b></td><td><input type="number" id="theta_input" value="" style="min-height:15px;width:130px;"/></td></tr>
<tr id="phi"><td style="width:20px;"><b>φ</b></td><td><input type="number" id="phi_input" value="" style="min-height:15px;width:130px;"/></td></tr>
<tr id="n"><td style="width:20px;"><b>n</b></td><td><input type="number" id="n_input" value="" style="min-height:15px;width:130px;"/></td></tr>
<tr><td colspan="2"><a class="loqc_button_2" href="#" id='set' >Set</a></td></tr>
</table>
<hr></hr>
{% for p in projects %}
<a class="loqc_button" href="#" onclick='event.preventDefault();$("#save").removeClass("loqc_button_2_inactive");$("#save").addClass("loqc_button_alert");add_user_project("{{p.key}}", null, 100, 100);' >{{p.name}}</a>
{% endfor %}
</td>
<td>
<div onselectstart="javascript:/*IE8 hack*/return false" id="gfx_holder" style="width:2000px; height:2000px;display: block;"></div>
</td>
</tr></table>
</body>
</html>