-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauto-generated-widget.html
More file actions
5515 lines (5059 loc) · 233 KB
/
auto-generated-widget.html
File metadata and controls
5515 lines (5059 loc) · 233 KB
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Widget / PCB Board v1.0</title>
<!-- ChiliPeppr is based on bootstrap CSS. -->
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<!-- Customized version of require.js for ChiliPeppr. Please see require.js docs for how
ChiliPeppr's dependency system works as it's based on require.js. -->
<script type='text/javascript' src="//i2dcui.appspot.com/js/require.js"></script>
<style type='text/css'>
/* CSS for ChiliPeppr Widget */
#com-chilipeppr-widget-pcb {
/* width: 200px; */
}
#com-chilipeppr-widget-pcb input {
padding: 0 0 0 6px;
}
#com-chilipeppr-widget-pcb .pcb-advanced {
padding: 0 10px 10px 10px;
border: 1px gray dashed;
}
#com-chilipeppr-widget-pcb .pcb-advanced .checkbox {
margin-bottom: 4px;
}
#com-chilipeppr-widget-pcb .enclosed-group {
border:1px dashed black;
padding:0 10px;
border-radius:6px;
margin-bottom: 20px;
}
#com-chilipeppr-widget-pcb .slidecontainer {
width: 100%;
margin-bottom: 10px;
}
#com-chilipeppr-widget-pcb .input-group-addon {
padding: 5px;
}
#com-chilipeppr-widget-pcb .descriptive-text {
font-size: 10px;
padding-top: 5px;
}
#com-chilipeppr-widget-pcb h2 {
font-size: 17px;
margin-bottom: 0px;
}
#com-chilipeppr-widget-pcb #pcb-gcode {
/* padding:0; */
}
#com-chilipeppr-widget-pcb .com-chilipeppr-widget-pcb-gcode,
#com-chilipeppr-widget-pcb .reg-holes-gcode{
width: 100%;
height: 100%;
min-height: 150px;
margin: 0;
}
.com-chilipeppr-widget-pcb-info {
padding-bottom: 0;
margin-bottom: 0;
font-size: 10px;
position: absolute;
width: 200px;
}
.com-chilipeppr-widget-pcb-info-signal {
padding-bottom: 0;
margin-bottom: 0;
font-size: 10px;
position: absolute;
width: 200px;
}
#com-chilipeppr-widget-pcb #pcb-canvas {
width: 100%;
}
#com-chilipeppr-widget-pcb .nav-tabs>li>a {
padding: 2px 6px;
}
#com-chilipeppr-widget-pcb .tab-pane {
padding: 10px 16px 20px 16px;
overflow: auto;
max-height: 260px;
}
#com-chilipeppr-widget-pcb .tab-pane-container {
padding: 0px 0px 0px 0px;
overflow: visible;
max-height: 295px;
}
#com-chilipeppr-widget-pcb .panel-body {
padding: 3px;
}
#com-chilipeppr-widget-pcb .panel-footer {
font-size: 11px;
padding: 0;
margin: 0;
}
#com-chilipeppr-widget-pcb .process-list {
list-style-type: none;
margin: 0;
padding: 0;
width: 60%;
}
#com-chilipeppr-widget-pcb .process-list li {
margin: 0 3px 3px 3px;
padding: 0.4em;
padding-left: 1.5em;
font-size: 1.4em;
height: 18px;
}
#com-chilipeppr-widget-pcb .process-list li span {
position: absolute;
margin-left: -1.3em;
}
.nav-tabs-2ndpos {
margin-left:10px;
margin-right:10px;
}
.nav-tabs-2ndpos li {
font-size:12px;
}
.nav-tabs.nav-tabs-2ndpos li.active a {
border-style: dashed;
}
#com-chilipeppr-widget-pcb .edit-popup {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
#com-chilipeppr-widget-pcb .edit-popup-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 300px;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
#com-chilipeppr-widget-pcb .edit-popup-close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
#com-chilipeppr-widget-pcb .edit-popup-close:hover,
#com-chilipeppr-widget-pcb .edit-popup-close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
#com-chilipeppr-widget-pcb .edit-popup-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
border-radius: 3px;
}
#com-chilipeppr-widget-pcb .edit-popup-body {padding: 2px 16px;}
#com-chilipeppr-widget-pcb .edit-popup-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
</style>
<script type='text/javascript'>
//<![CDATA[
/* global requirejs cprequire cpdefine chilipeppr THREE */
// Defining the globals above helps Cloud9 not show warnings for those variables
// ChiliPeppr Widget/Element Javascript
requirejs.config({
/*
Dependencies can be defined here. ChiliPeppr uses require.js so
please refer to http://requirejs.org/docs/api.html for info.
Most widgets will not need to define Javascript dependencies.
Make sure all URLs are https and http accessible. Try to use URLs
that start with // rather than http:// or https:// so they simply
use whatever method the main page uses.
Also, please make sure you are not loading dependencies from different
URLs that other widgets may already load like jquery, bootstrap,
three.js, etc.
You may slingshot content through ChiliPeppr's proxy URL if you desire
to enable SSL for non-SSL URL's. ChiliPeppr's SSL URL is
https://i2dcui.appspot.com which is the SSL equivalent for
http://chilipeppr.com
*/
paths: {
Three: "https://cdnjs.cloudflare.com/ajax/libs/three.js/r76/three",
ThreeTextGeometry: "//i2dcui.appspot.com/js/three/TextGeometry",
ThreeFontUtils: "//i2dcui.appspot.com/js/three/FontUtils",
ThreeHelvetiker: "//i2dcui.appspot.com/js/three/threehelvetiker",
Clipper: "//i2dcui.appspot.com/js/clipper/clipper_unminified"
},
shim: {
ThreeTextGeometry: ["Three"],
ThreeFontUtils: ["Three", "ThreeTextGeometry"],
ThreeHelvetiker: ["Three", "ThreeTextGeometry", "ThreeFontUtils"],
}
});
cprequire_test(["inline:com-chilipeppr-widget-pcb"], function(pcbw) {
// Test this element. This code is auto-removed by the chilipeppr.load()
// when using this widget in production. So use the cpquire_test to do things
// you only want to have happen during testing, like loading other widgets or
// doing unit tests. Don't remove end_test at the end or auto-remove will fail.
console.info("PCBW", "test running of " + pcbw.id);
//ew.init();
$("#com-chilipeppr-widget-pcb").css("position", "relative");
$("#com-chilipeppr-widget-pcb").css("background", "none");
$("#com-chilipeppr-widget-pcb").css("width", "320px");
$("body").prepend("<div id=\"3dviewer\"></div>");
chilipeppr.load(
"#3dviewer",
"http://raw.githubusercontent.com/ameennihad/widget-3dviewer/master/auto-generated-widget.html",
function() {
cprequire(["inline:com-chilipeppr-widget-3dviewer"], function(threed) {
threed.init({
doMyOwnDragDrop: false
});
// only init pcb widget once 3d is loaded
// set doMyOwnDragDrop
pcbw.init(true);
});
});
$("body").prepend("<div id=\"test-drag-drop\"></div>");
chilipeppr.load("#test-drag-drop", "http://raw.githubusercontent.com/chilipeppr/elem-dragdrop/master/auto-generated-widget.html",
function() {
cprequire(
["inline:com-chilipeppr-elem-dragdrop"],
function(dd) {
dd.init();
dd.bind("body", null);
});
});
$("body").prepend("<div id=\"com-chilipeppr-flash\"></div>");
chilipeppr.load("#com-chilipeppr-flash",
"http://raw.githubusercontent.com/chilipeppr/element-flash/master/auto-generated-widget.html",
function() {
cprequire(["inline:com-chilipeppr-elem-flashmsg"], function(fm) {
fm.init();
});
});
// test before and after render
chilipeppr.subscribe("/" + pcbw.id + "/beforeToolPathRender", this, function(pcbWidget) {});
chilipeppr.subscribe("/" + pcbw.id + "/afterToolPathRender", this, function(pcbWidget) {});
// // test before and after render
// chilipeppr.subscribe("/" + pcbw.id + "/beforeLayerGenerate", this, function(pcbWidget) {
// });
// chilipeppr.subscribe("/" + pcbw.id + "/afterLayerGenerate", this, function(pcbWidget) {
// });
} /*end_test*/ );
// This is the main definition of your widget. Give it a unique name.
cpdefine("inline:com-chilipeppr-widget-pcb", ["chilipeppr_ready", "Clipper", "jqueryuiWidget"], function() {
return {
/**
* The ID of the widget. You must define this and make it unique.
*/
id: "com-chilipeppr-widget-pcb", // Make the id the same as the cpdefine id
name: "Widget / PCB Board v1.0", // The descriptive name of your widget.
desc: "This widget lets you drag in a PCB board file to mill.", // A description of what your widget does
url: "http://raw.githubusercontent.com/ameennihad/widget-pcb/master/auto-generated-widget.html", // The final URL of the working widget as a single HTML file with CSS and Javascript inlined. You can let runme.js auto fill this if you are using Cloud9.
fiddleurl: "http://ide.c9.io/ameennihad/widget-pcb", // The edit URL. This can be auto-filled by runme.js in Cloud9 if you'd like, or just define it on your own to help people know where they can edit/fork your widget
githuburl: "http://github.com/ameennihad/widget-pcb", // The backing github repo
testurl: "http://widget-pcb-ameennihad.c9users.io/widget.html", // The standalone working widget so can view it working by itself
/**
* Define pubsub signals below. These are basically ChiliPeppr's event system.
* ChiliPeppr uses amplify.js's pubsub system so please refer to docs at
* http://amplifyjs.com/api/pubsub/
*/
/**
* Define the publish signals that this widget/element owns or defines so that
* other widgets know how to subscribe to them and what they do.
*/
publish: {
// Define a key:value pair here as strings to document what signals you publish.
"/onExampleGenerate": "Example: Publish this signal when we go to generate gcode."
},
/**
* Define the subscribe signals that this widget/element owns or defines so that
* other widgets know how to subscribe to them and what they do.
*/
subscribe: {
// Define a key:value pair here as strings to document what signals you subscribe to
// so other widgets can publish to this widget to have it do something.
// "/onExampleConsume": "Example: This widget subscribe to this signal so other widgets can send to us and we'll do something with it."
},
/**
* Document the foreign publish signals, i.e. signals owned by other widgets
* or elements, that this widget/element publishes to.
*/
foreignPublish: {
// Define a key:value pair here as strings to document what signals you publish to
// that are owned by foreign/other widgets.
// "/jsonSend": "Example: We send Gcode to the serial port widget to do stuff with the CNC controller."
},
/**
* Document the foreign subscribe signals, i.e. signals owned by other widgets
* or elements, that this widget/element subscribes to.
*/
foreignSubscribe: {
// Define a key:value pair here as strings to document what signals you subscribe to
// that are owned by foreign/other widgets.
// "/com-chilipeppr-elem-dragdrop/ondropped": "Example: We subscribe to this signal at a higher priority to intercept the signal. We do not let it propagate by returning false."
},
/**
* All widgets should have an init method. It should be run by the
* instantiating code like a workspace or a different widget.
*/
init: function(doMyOwnDragDrop) {
// the workspace may want to handle the drag drop
// but when in dev mode it makes sense for us to do our own
if (doMyOwnDragDrop) {
this.setupDragDrop();
}
else {
// the workspace is doing the drag/drop. this is important
// because this code base for this widget is huge and thus
// the workspace should handle dragging in BRD files
// and once it sees one, it should then load this widget
// so that users who don't use ChiliPeppr for BRD files
// don't have to load all this insane code
}
//TODO: Remove this line & onFileInputChanged event when drag/drop is implemented
$(this.getElementId("pcbw-file-input")).change(this.onFileInputChanged.bind(this));
this.readFr4Values();
this.readRegHoleGcodeValues();
this.readRegHoleValues();
this.setupUiFromLocalStorage();
this.btnSetup();
this.forkSetup();
this.init3d();
//this.open();
this.setupMouseOver();
},
onFileInputChanged: function(e){
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
var that = this;
reader.onload = function(e) {
var contents = e.target.result;
var info = {
name: e.target.fileName,
lastModified: new Date()
};
that.open(contents, info);
};
reader.readAsText(file, "UTF-8");
},
getElementId: function(id) {
return "#" + this.id + " #" + id;
},
getElementClass: function(id) {
return "#" + this.id + " ." + id;
},
/**
* Try to get a reference to the 3D viewer.
*/
init3d: function() {
this.get3dObj();
if (this.obj3d === null) {
console.info("PCBW", "loading 3d scene failed, try again in 1 second");
var attempts = 1;
var that = this;
setTimeout(function() {
that.get3dObj();
if (that.obj3d === null) {
attempts++;
setTimeout(function() {
that.get3dObj();
if (that.obj3d === null) {
console.info("PCBW", "giving up on trying to get 3d");
}
else {
console.info("PCBW", "succeeded on getting 3d after attempts:", attempts);
that.onInit3dSuccess();
}
}, 5000);
}
else {
console.info("PCBW", "succeeded on getting 3d after attempts:", attempts);
that.onInit3dSuccess();
}
}, 1000);
}
else {
this.onInit3dSuccess();
}
},
onInit3dSuccess: function() {
console.info("PCBW", "onInit3dSuccess. That means we finally got an object back.");
//this.clear3dViewer();
// open the last file
this.open();
},
obj3d: null, // gets the 3dviewer obj stored in here on callback
obj3dmeta: null, // gets metadata for 3dviewer
userCallbackForGet3dObj: null,
get3dObj: function(callback) {
this.userCallbackForGet3dObj = callback;
chilipeppr.subscribe("/com-chilipeppr-widget-3dviewer/recv3dObject", this, this.get3dObjCallback);
chilipeppr.publish("/com-chilipeppr-widget-3dviewer/request3dObject", "");
chilipeppr.unsubscribe("/com-chilipeppr-widget-3dviewer/recv3dObject", this.get3dObjCallback);
},
get3dObjCallback: function(data, meta) {
console.info("PCBW", "got 3d obj:", data, meta);
this.obj3d = data;
this.obj3dmeta = meta;
if (this.userCallbackForGet3dObj) {
this.userCallbackForGet3dObj();
this.userCallbackForGet3dObj = null;
}
},
is3dViewerReady: false,
clear3dViewer: function() {
console.info("PCBW", "clearing 3d viewer");
chilipeppr.publish("/com-chilipeppr-widget-3dviewer/sceneclear");
this.is3dViewerReady = true;
},
setupDragDrop: function() {
// subscribe to events
chilipeppr.subscribe("/com-chilipeppr-elem-dragdrop/ondragover", this, this.onDragOver);
chilipeppr.subscribe("/com-chilipeppr-elem-dragdrop/ondragleave", this, this.onDragLeave);
// /com-chilipeppr-elem-dragdrop/ondropped
chilipeppr.subscribe("/com-chilipeppr-elem-dragdrop/ondropped", this, this.onDropped, 8); // default is 10, we do 9 to be higher priority
},
/**
* Call this method from init to setup all the buttons when this widget
* is first loaded. This basically attaches click events to your
* buttons. It also turns on all the bootstrap popovers by scanning
* the entire DOM of the widget.
*/
btnSetup: function() {
// Chevron hide/show body
var that = this;
$("#" + this.id + " .hidebody").click(function(evt) {
console.info("PCBW", "hide/unhide body");
if ($("#" + that.id + " .panel-body").hasClass("hidden")) {
// it's hidden, unhide
that.showBody(evt);
}
else {
// hide
that.hideBody(evt);
}
});
// Ask bootstrap to scan all the buttons in the widget to turn
// on popover menus
$("#" + this.id + " .btn").popover({
delay: 1000,
animation: true,
placement: "auto",
trigger: "hover",
container: "body"
});
},
/**
* User options are available in this property for reference by your
* methods. If any change is made on these options, please call
* saveOptionsLocalStorage()
*/
options: null,
/**
* Call this method on init to setup the UI by reading the user's
* stored settings from localStorage and then adjust the UI to reflect
* what the user wants.
*/
setupUiFromLocalStorage: function() {
//TODO: SETUP WIDGET OPTIONS
// Read vals from localStorage. Make sure to use a unique
// key specific to this widget so as not to overwrite other
// widgets' options. By using this.id as the prefix of the
// key we're safe that this will be unique.
// Feel free to add your own keys inside the options
// object for your own items
var options = localStorage.getItem(this.id + "-options");
if (options) {
options = $.parseJSON(options);
console.info("PCBW", "just evaled options: ", options);
}
else {
options = {
showBody: true,
tabShowing: 1,
customParam1: null,
customParam2: 1.0
};
}
this.options = options;
console.info("PCBW", "options:", options);
// show/hide body
if (options.showBody) {
this.showBody();
}
else {
this.hideBody();
}
},
/**
* When a user changes a value that is stored as an option setting, you
* should call this method immediately so that on next load the value
* is correctly set.
*/
saveOptionsLocalStorage: function() {
// You can add your own values to this.options to store them
// along with some of the normal stuff like showBody
var options = this.options;
var optionsStr = JSON.stringify(options);
console.info("PCBW", "saving options:", options, "json.stringify:", optionsStr);
// store settings to localStorage
localStorage.setItem(this.id + "-options", optionsStr);
},
/**
* Show the body of the panel.
* @param {jquery_event} evt - If you pass the event parameter in, we
* know it was clicked by the user and thus we store it for the next
* load so we can reset the user's preference. If you don't pass this
* value in we don't store the preference because it was likely code
* that sent in the param.
*/
showBody: function(evt) {
$("#" + this.id + " .panel-body").removeClass("hidden");
$("#" + this.id + " .panel-footer").removeClass("hidden");
$("#" + this.id + " .hidebody span").addClass("glyphicon-chevron-up");
$("#" + this.id + " .hidebody span").removeClass("glyphicon-chevron-down");
if (evt !== null) {
this.options.showBody = true;
this.saveOptionsLocalStorage();
}
// this will send an artificial event letting other widgets know to resize
// themselves since this widget is now taking up more room since it's showing
$(window).trigger("resize");
},
/**
* Hide the body of the panel.
* @param {jquery_event} evt - If you pass the event parameter in, we
* know it was clicked by the user and thus we store it for the next
* load so we can reset the user's preference. If you don't pass this
* value in we don't store the preference because it was likely code
* that sent in the param.
*/
hideBody: function(evt) {
$("#" + this.id + " .panel-body").addClass("hidden");
$("#" + this.id + " .panel-footer").addClass("hidden");
$("#" + this.id + " .hidebody span").removeClass("glyphicon-chevron-up");
$("#" + this.id + " .hidebody span").addClass("glyphicon-chevron-down");
if (evt !== null) {
this.options.showBody = false;
this.saveOptionsLocalStorage();
}
// this will send an artificial event letting other widgets know to resize
// themselves since this widget is now taking up less room since it's hiding
$(window).trigger("resize");
},
/**
* This method loads the pubsubviewer widget which attaches to our
* upper right corner triangle menu and generates 3 menu items like
* Pubsub Viewer, View Standalone, and Fork Widget. It also enables
* the modal dialog that shows the documentation for this widget.
*
* By using chilipeppr.load() we can ensure that the pubsubviewer widget
* is only loaded and inlined once into the final ChiliPeppr workspace.
* We are given back a reference to the instantiated singleton so its
* not instantiated more than once. Then we call it's attachTo method
* which creates the full pulldown menu for us and attaches the click
* events.
*/
forkSetup: function() {
var topCssSelector = "#" + this.id;
$(topCssSelector + " .panel-title").popover({
title: this.name,
content: this.desc,
html: true,
delay: 1000,
animation: true,
trigger: "hover",
placement: "auto"
});
var that = this;
chilipeppr.load("http://raw.githubusercontent.com/chilipeppr/widget-pubsubviewer/master/auto-generated-widget.html", function() {
require(["inline:com-chilipeppr-elem-pubsubviewer"], function(pubsubviewer) {
pubsubviewer.attachTo($(topCssSelector + " .panel-heading .dropdown-menu"), that);
});
});
},
statusEl: null, // cache the status element in DOM
status: function(txt) {
console.info("PCBW", "status. txt:", txt);
if (this.statusEl === null) this.statusEl = $("#" + this.id + "-status");
var len = this.statusEl.val().length;
if (len > 30000) {
console.info("PCBW", "truncating status area text");
this.statusEl.val(this.statusEl.val().substring(len - 5000));
}
this.statusEl.val(this.statusEl.val() + txt + "\n");
this.statusEl.scrollTop(
this.statusEl[0].scrollHeight - this.statusEl.height());
},
mySceneGroup: null,
sceneReAddMySceneGroup: function() {
if (this.obj3d && this.mySceneGroup) {
this.obj3d.add(this.mySceneGroup);
}
this.obj3dmeta.widget.wakeAnimate();
},
sceneRemoveMySceneGroup: function() {
if (this.obj3d && this.mySceneGroup) {
this.obj3d.remove(this.mySceneGroup);
}
this.obj3dmeta.widget.wakeAnimate();
},
sceneAdd: function(obj) {
// this method of adding puts us in the object that contains rendered Gcode
// that's one option, but when we send gcode to workspace we get overwritten
// then
//this.obj3d.add(obj);
// let's add our board content outside the scope of the Gcode content
// so that we have it stay while the Gcode 3D Viewer still functions
if (this.mySceneGroup === null) {
this.mySceneGroup = new THREE.Group();
this.obj3d.add(this.mySceneGroup);
}
this.mySceneGroup.add(obj);
this.obj3dmeta.widget.wakeAnimate();
},
sceneRemove: function(obj) {
if (this.mySceneGroup !== null)
this.mySceneGroup.remove(obj);
this.obj3dmeta.widget.wakeAnimate();
},
onDropped: function(data, info) {
console.info("PCBW", "onDropped. len of file:", data.length, "info:", info);
// we have the data
// double check it's a board file, cuz it could be gcode
var droppedFile = Board.supportedFiles.find(function(f) {
return f.signature.test(data);
});
if (droppedFile !== undefined) {
console.info("PCBW", "we have " + droppedFile.type + " board file!");
// this.colorSignal = 9249571;
// this.colorSmd = 9249571;
localStorage.setItem(this.id + "-lastDropped", data);
localStorage.setItem(this.id + "-lastDropped-info", JSON.stringify(info));
this.fileInfo = info;
console.info("PCBW", "saved brd file to localstorage");
this.open(data, info);
return false;
}
else {
droppedFile = Board.supportedFiles.find(function(f) {
var extRegEx = new RegExp(f.ext + "$", "i");
if (info.name.match(extRegEx)) return f;
else return null;
});
console.log("droppedFile", droppedFile);
if (droppedFile !== undefined) {
if (droppedFile.type == Board.types.eagle)
chilipeppr.publish("/com-chilipeppr-elem-flashmsg/flashmsg", "Error Loading Eagle BRD File", "Looks like you dragged in an Eagle BRD file, but it seems to be in binary. You can open this file in Eagle and then re-save it to a new file to create a text version of your Eagle BRD file.", 15 * 1000);
else if (droppedFile.type == Board.types.kiCad)
chilipeppr.publish("/com-chilipeppr-elem-flashmsg/flashmsg", "Error Loading KiCad File", "Looks like you dragged in a file with kicad_pcb extension, but it seems to be in unsupported format.", 15 * 1000);
return false;
}
}
},
onDragOver: function() {
console.info("PCBW", "onDragOver");
$("#" + this.id).addClass("panel-primary");
},
onDragLeave: function() {
console.info("PCBW", "onDragLeave");
$("#" + this.id).removeClass("panel-primary");
},
raycaster: null,
projector: null, // = new THREE.Projector();
arrowHelper: null,
intersectObjects: [], // contains three.js objects that we want to detect on mouse movement in the 3d viewer
renderArea: null, // cache for renderarea dom element
infoArea: null, // store dom that shows info
infoSignalArea: null,
lastIntersect: null, // last obj we showed info for
hidePopupsElem: null, // quick access to hide checkbox
setupMouseOver: function() {
this.raycaster = new THREE.Raycaster();
//this.projector = new THREE.Projector();
$("#com-chilipeppr-widget-3dviewer-renderArea").mousemove(this.onMouseOver.bind(this));
//$("#com-chilipeppr-widget-3dviewer-renderArea").click(this.onMouseOver.bind(this));
$("#com-chilipeppr-widget-3dviewer-renderArea").mousedown(this.onMouseDown.bind(this));
$("#com-chilipeppr-widget-3dviewer-renderArea").mouseup(this.onMouseUp.bind(this));
$("#com-chilipeppr-widget-3dviewer-renderArea").dblclick(this.onDblClick.bind(this));
this.mouseButtonStatus = "up";
$("#com-chilipeppr-widget-pcb #edit-dimension .edit-popup-close").click(this.onEditPopupClose.bind(this));
this.renderArea = $("#com-chilipeppr-widget-3dviewer-renderArea");
this.infoArea = $(".com-chilipeppr-widget-pcb-info");
this.infoArea.prependTo(this.renderArea);
//this.infoSignalArea = $(".com-chilipeppr-widget-pcb-info-signal");
//this.infoSignalArea.prependTo(this.renderArea);
this.hidePopupsElem = $("#com-chilipeppr-widget-pcb .popups-hide");
console.log("Popup", this.hidePopupsElem);
this.hidePopupsElem.change(function(evt) {
console.log("Popup", this.hidePopupsElem);
if (this.hidePopupsElem.is(":checked")) {
// hide
this.deactivateMouseMove();
}
else {
// unhide
this.reactivateMouseMove();
}
}, this);
},
reactivateMouseMove: function() {
// add mouseover event
//console.log("reactivateMouseMove");
$("#com-chilipeppr-widget-3dviewer-renderArea").mousemove(this.onMouseOver.bind(this));
},
deactivateMouseMove: function() {
//console.log("deactivateMouseMove");
// remove mouseover event
$("#com-chilipeppr-widget-3dviewer-renderArea").unbind("mousemove");
this.hidePopups();
},
hidePopups: function() {
//console.log("hiding popups and resetting opacities");
//this.infoSignalArea.addClass("hidden");
this.infoArea.addClass("hidden");
// reset opacities
if (this.lastIntersect !== null) {
// console.log("lastIntersect:", this.lastIntersect);
// also reset opacity for other items we hilited
// if (this.lastIntersectOtherMaterials !== null) {
// //console.log("lastIntersectOtherMaterials:", this.lastIntersectOtherMaterials);
// this.lastIntersectOtherMaterials.forEach(function(material) {
// material.opacity = material.opacityBackup;
// });
// this.lastIntersectOtherMaterials = [];
// }
this.lastIntersect.object.material.opacity = this.lastIntersect.object.material.opacityBackup;
this.lastIntersect.object.material.transparent = this.lastIntersect.object.material.transparentBackup;
}
},
lastIntersectOtherMaterials: [], // array to hold materials modified by mouseover so we can reset them later to normal opacity
onMouseUp: function(event) {
this.mouseButtonStatus = "up";
},
onMouseDown: function(event) {
this.mouseButtonStatus = "down";
this.hidePopups();
},
onDblClick: function(event) {
var x = event.clientX;
var y = event.clientY;
var modal = $("#com-chilipeppr-widget-pcb #edit-dimension")[0];
modal.style.display = "block";
},
onEditPopupClose: function(event) {
$("#com-chilipeppr-widget-pcb #edit-dimension")[0].style.display = "none";
},
onMouseOver: function(event) {
if (this.mouseButtonStatus == "down") return;
this.obj3dmeta.widget.wakeAnimate();
var vector = new THREE.Vector3();
var containerWidth = this.renderArea.innerWidth();
var containerHeight = this.renderArea.innerHeight();
var x = event.clientX;
var y = event.clientY;
vector.set((event.clientX / containerWidth) * 2 - 1, -(event.clientY / containerHeight) * 2 + 1, 0.5);
var matrix = new THREE.Matrix4();
var matrixInverse = matrix.getInverse(this.obj3dmeta.camera.projectionMatrix);
matrix.multiplyMatrices(this.obj3dmeta.camera.matrixWorld, matrixInverse);
vector.applyProjection(matrix);
vector.sub(this.obj3dmeta.camera.position);
vector.normalize();
this.raycaster.ray.set(this.obj3dmeta.camera.position, vector);
var intersects = this.raycaster.intersectObjects(this.intersectObjects, true);
// reset last object
if (this.lastIntersect !== null) {
// also reset opacity for other items we hilited
// if (this.lastIntersectOtherMaterials !== null) {
// this.lastIntersectOtherMaterials.forEach(function(material) {
// material.opacity = material.opacityBackup;
// });
// this.lastIntersectOtherMaterials = [];
// }
this.lastIntersect.object.material.opacity = this.lastIntersect.object.material.opacityBackup;
this.lastIntersect.object.material.transparent = this.lastIntersect.object.material.transparentBackup;
}
if (intersects.length > 0) {
var obj = intersects[0];
if (obj != this.lastIntersect) {
this.lastIntersect = obj;
x += 30;
if (!obj.object.material.opacityBackup)
obj.object.material.opacityBackup = obj.object.material.opacity;
if (!obj.object.material.transparentBackup)
obj.object.material.transparentBackup = obj.object.material.transparent;
var ud = obj.object.userData;
if (!("type" in ud)) {
ud = obj.object.parent.userData;
}
if (ud.type == "ignore") {
this.hidePopups();
return;
}
var layerName = this.board.layers.find(byKey, ud.layer || 1).name;
if (this.board.type == Board.types.eagle) layerName = ud.layer + " - " + layerName;
if (ud.type == "dimension") {
this.updateInfoArea("Dimension", "Double click to change", [
{ title: "Layer", data: layerName },
{ title: "Wire Width", data: ud.width.toFixed(2) + " mm" },
{ title: "Milling Width", data: ud.millingWidth.toFixed(2) + " mm" },
{ title: "Millin Tool", data: ud.tool },
{ title: "Operation Type", data: ud.dimensionType === 0 ? "Outside path" : ud.dimensionType == 1 ? "Inside path" : "On path" }
]);
}
else if (ud.type == "signal") {
this.updateInfoArea("Signal", "....", [
{ title: "Signal Key", data: ud.signalKey },
{ title: "Signal Name", data: ud.name },
{ title: "Layer", data: layerName },
{ title: "Class", data: ud.netClassName },
{ title: "Clearance", data: ud.clearance + " mm (" + Math.round(ud.clearance / 0.0254) + " mil)" }
]);
}
this.infoArea.removeClass("hidden");
this.infoArea.css("left", x + "px").css("top", y + "px");
obj.object.material.opacity = 0.8;
obj.object.material.transparent = true;
}
}
else {
// hide info area
this.infoArea.addClass("hidden");
}
},
updateInfoArea: function(header, footer, info) {
this.infoArea.find(".info-title").text(header);
this.infoArea.find(".info-footer").text(footer);
for (var i = 0; i <= 6; i++) {
if (i < info.length) {
this.infoArea.find(".row" + i + " .title").text(info[i].title);
this.infoArea.find(".row" + i + " .info").text(info[i].data);
this.infoArea.find(".row" + i).removeClass("hidden");
}
else
this.infoArea.find(".row" + i).addClass("hidden");
}
},
open: function(data, info) {
// TODO: Optimize this method and load settings from storage
// if we are passed the file data, then use that, otherwise look to
// see if we had one saved from before, i.e. this is a browser reload scenario
// and we'd like to show them their recent board file
console.info("PCBW", "open");
var validFile = false;
var file;
if (data) {
console.info("PCBW", "open. loading from passed in data. data.length:", data.length, "info:", info);
file = data;
this.fileInfo = info;
$(this.getElementClass("pcb-draghere")).addClass("hidden");
validFile = true;
}
else {
// try to retrieve the most recent board file
file = localStorage.getItem(this.id + "-lastDropped");
if (file && file.length > 0) {
this.fileInfo = localStorage.getItem(this.id + "-lastDropped-info");
if (this.fileInfo && this.fileInfo.match(/^{/)) {
this.fileInfo = JSON.parse(this.fileInfo);
}
console.info("PCBW", "open. loading data from localStorage. file.length:", file.length, "info:", this.fileInfo);
validFile = true;
}
else
validFile = false;
// else {
// // there's no file, just return
// return;
// }
}
var droppedFile;
if (validFile) {
// make sure this file is a supported board
droppedFile = Board.supportedFiles.find(function(f) {
return f.signature.test(file);
});
if (droppedFile === undefined) {
chilipeppr.publish("/com-chilipeppr-elem-flashmsg/flashmsg", "Error Loading board file", "It looks like you had a previous board file, but it doesn't seem to be the correct format.", 10 * 1000);
validFile = false;