-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
1183 lines (1094 loc) · 57.4 KB
/
index.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
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 lang="en">
<head>
<meta charset="UTF-8">
<title>Scratch Pen</title>
<script src="bower_components/blockly/blockly_compressed.js"></script>
<script src="bower_components/blockly/blocks_compressed.js"></script>
<script src="bower_components/blockly/msg/js/en.js"></script>
<script src="bower_components/blockly/javascript_compressed.js"></script>
<script src="bower_components/blockly/python_compressed.js"></script>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/firebase/firebase.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/moment/moment.js"></script>
<script src="bower_components/js-signals/dist/signals.min.js"></script>
<script src="bower_components/crossroads/dist/crossroads.min.js"></script>
<script src="bower_components/hasher/dist/js/hasher.min.js"></script>
<!-- update the version number as needed -->
<script defer src="/__/firebase/4.6.0/firebase-app.js"></script>
<!-- include only the Firebase features as you need -->
<script defer src="/__/firebase/4.6.0/firebase-auth.js"></script>
<script defer src="/__/firebase/4.6.0/firebase-database.js"></script>
<script defer src="/__/firebase/4.6.0/firebase-messaging.js"></script>
<script defer src="/__/firebase/4.6.0/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.6.0/firebase.js"></script>
<script src="bower_components/awesomplete/awesomplete.min.js"></script>
<!-- initialize the SDK after all desired features are loaded -->
<script defer src="/__/firebase/init.js"></script>
<script src="js/customTextGenerator.js"></script>
<script src="js/customBlocks.js"></script>
<script src="js/authentication.js"></script>
<script src="js/save.js"></script>
<script src="js/dashboard.js"></script>
<script src="js/jquery-css-transform.js"></script>
<script src="js/jquery-animate-css-rotate-scale.js"></script>
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/application.css">
<link rel="stylesheet" href="css/toolkit.css">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="bower_components/awesomplete/awesomplete.css">
</head>
<body>
<div class="main-container">
<!--This is the top toolbar-->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="toolbar-container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">ScratchPen</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a id = "allProjects" href="#">Dashboard</a></li>
<li class="myProjectsBoard"><a id = "myProjects" href="#" >My Projects</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<div class="navbar-right">
<form class="navbar-form navbar-left">
<input class="dropdown-input" placeholder="Search for a project.." />
<button class="dropdown-btn" type="button"><span class="caret"></span></button>
</form>
<label id="userLabel"> </label>
<button type="button" class="btn btn-link navbar-btn" id="join">Join ScratchPen</button>
<button type="button" class="btn btn-primary navbar-btn" id="login">Sign In</button>
<button type="button" class="btn btn-danger navbar-btn" id="logout">Log Out</button>
</div>
</div>
</div>
</nav>
<!--Below is the home page dashboard-->
<div class="jumbotron">
<div class="container text-center">
<h1>ScratchPen</h1>
<p>Code, Learn & Reuse</p>
</div>
</div>
<div class="container dashboard-container">
</div>
<br>
<!--End of dashboard-->
<!--Thumbnail start-->
<div class="row project-list">
<div class="col-sm-6 col-md-3 fixed-create-block">
<div class="thumbnail">
<img id="newProject" class="newProjectThumbnail" src="images/add_circle_grey.png" alt="...">
<div class="caption">
<h3>Create A New Project</h3>
</div>
</div>
</div>
</div>
<!--Thumbnail End-->
<div id="scratch">
<div class="project-info">
<div><h1 class="project-info-name"></h1></div>
<div><h4 class="project-info-user">Created By - </h4></div>
</div>
<div class="scratchContainer">
<div class="alert alert-danger notification" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
</div>
<div id="blocklyDiv"></div>
<div id="blocklyDivAnim"></div>
<div class="col-sm-3 col-md-2 sidebar scratch-options">
<ul class="nav nav-sidebar">
<li class="active"><a href="#">New Project <span class="sr-only">(current)</span></a></li>
<li><a id="save" href="#">Save Project</a></li>
<li><a id="saveVersion" href="#">Save Version</a></li>
<li><a id="uploadVersion" href="#">Upload from Version</a></li>
<li><a id="import" href="#">Import</a></li>
<li><a id="export" href="#">Export</a></li>
</ul>
<ul class="nav nav-sidebar">
<li><a id="animateCode">Play</a></li>
<li><a id="codeReuse" href="#">Code reuse</a></li>
<li><a id="switchLang">Switch code (to Python)</a></li>
<li><a id="animMode" href="#">Switch to animation</a></li>
<li><a id="defaultMode" href="#">Switch to default</a></li>
<!--<li><a href="">One more nav</a></li>-->
<!--<li><a href="">Another nav item</a></li>-->
<!--<li><a href="">More navigation</a></li>-->
</ul>
<!--<ul class="nav nav-sidebar">-->
<!--<li><a href="">Nav item again</a></li>-->
<!--<li><a href="">One more nav</a></li>-->
<!--<li><a href="">Another nav item</a></li>-->
<!--</ul>-->
</div>
<div class="text-container">
<pre id="textGenerator" class="prettyprint lang-js"></pre>
<pre id="textGeneratorAnim" class="prettyprint lang-js"></pre>
<!--<textarea id="textGenerator"></textarea>-->
<!--<div class="play-code"></div>-->
<!--<div class="save-code"></div>-->
<!--<div class="load-code"></div>-->
<div class="playground-container">
<div class="playground" id="playgroundObj"></div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="saveModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="modalLabel">Enter Project Details</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="text" id="projectName" class="form-control" placeholder="Enter project name" aria-describedby="sizing-addon1">
</div>
<span></span>
<div class="form-group">
<input type="text" id="projectDesc" class="form-control" placeholder="Enter project description" aria-describedby="sizing-addon1">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="saveProject" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!----End Modal ---->
<!-- Modal -->
<div class="modal fade" id="openVersionModal" tabindex="-1" role="dialog" aria-labelledby="modalLabelVersion">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="modalLabelVersion">Select version from available versions</h4>
</div>
<div class="modal-body">
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" id="menu2" type="button" data-toggle="dropdown">
<span id="versionSelected">Select version</span><span class="caret"></span></button>
<ul class="dropdown-menu version-list" role="menu" aria-labelledby="menu2">
</ul>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="openVersion" class="btn btn-primary">Load version</button>
</div>
</div>
</div>
</div>
<!----End Modal ---->
<!-- Modal -->
<div class="modal fade" id="exportModal" tabindex="-1" role="dialog" aria-labelledby="modalLabelExport">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="modalLabel">Exporting project</h4>
</div>
<div class="modal-body">
<div class="form-group">
<p>Copy the xml code below and share it with your friends!</p>
<input type="text" id="exportName" class="form-control" aria-describedby="sizing-addon1">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!----End Modal ---->
<!-- Modal -->
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="modalLabelExport">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="modalLabel">Import project</h4>
</div>
<div class="modal-body">
<div class="form-group">
<p>Paste the blockly xml code</p>
<input type="text" id="importData" class="form-control" aria-describedby="sizing-addon1">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="importProject" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!----End Modal ---->
<!-- Modal -->
<div class="modal fade" id="reuseModal" tabindex="-1" role="dialog" aria-labelledby="modalLabelVersion">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="modalLabelVersion">Reuse Projects</h4>
</div>
<div class="modal-body">
<!-- Large button group -->
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">
<span id="reuseSelected">Select project for reuse</span><span class="caret"></span></button>
<ul class="dropdown-menu blockList" role="menu" aria-labelledby="menu1">
</ul>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="reuseBlocks" class="btn btn-primary">Reuse Blocks</button>
</div>
</div>
</div>
</div>
<!----End Modal ---->
</div>
</div>
<div class="container-fluid container-fill-height create-container" id="create">
<div style="margin-top: 100px">
<form role="form" class="m-x-auto text-center app-login-form create-login-form">
<a href="../index.html" class="app-brand m-b-lg">
<img src="/images/lock.svg" alt="lock">
</a>
<div class="form-group">
<input class="form-control" id="email" placeholder="email">
</div>
<div class="form-group m-b-md">
<input class="form-control" type="password" id="password" placeholder="Password">
</div>
<div class="m-b-lg">
<button type="button" class="btn btn-primary" id="sign-up">Create</button>
<button type="button" class="btn btn-danger" id="cancel1">Cancel</button>
</div>
</form>
</div>
</div>
<div class="container-fluid container-fill-height signin-contatiner" id="signin">
<div style="margin-top: 100px">
<form role="form" class="m-x-auto text-center app-login-form signin-login-form">
<a href="#" class="app-brand m-b-lg">
<img src="/images/lock.svg" alt="brand">
</a>
<div class="form-group">
<button type="button" class="loginBtn loginBtn--facebook" id="facebook-login">Login with Facebook</button>
</div>
<div class="form-group m-b-md">
<button type="button" class="loginBtn loginBtn--google" id="google-login">Login with Google</button>
</div>
<div class="form-group">
<input class="form-control" id="email_signin" placeholder="Email">
</div>
<div class="form-group m-b-md">
<input type="password" class="form-control" id="password_signin" placeholder="Password">
</div>
<div class="m-b-lg">
<button type="button" class="btn btn-primary" id="sign-in">Log In</button>
<button type="button" class="btn btn-default" id="signupButton">Sign up</button>
<button type="button" class="btn btn-danger" id="cancel2">Cancel</button>
</div>
<!-- <footer class="screen-login">
<a href="#" class="text-muted">Forgot password</a>
</footer> -->
</form>
</div>
</div>
<div class="comments">
<div class="container">
<div class="comment-box">
<div class="comment-form">
<div class="header">Add Your Comment</div>
<form>
<div>
<input type="text" id="yourName" placeholder="Name" />
</div>
<div>
<textarea id="comment" rows="3" cols="30" placeholder="Comment"></textarea>
</div>
<div class="alert alert-danger notification" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
</div>
<button type="button" class="scratch-options" id="commentButton">COMMENT</button>
</form>
</div>
<div>
<h4 class="header">Comments</h4>
<div id="commentsList"></div>
</div>
</div>
</div>
</div>
</div>
<!--This is the main script tap.Add to new functions at the end in function document.addEventListner-->
<script>
// execute/clear BS loaders for docs
$(function() {
if (window.BS && window.BS.loader && window.BS.loader.length) {
while (BS.loader.length) {
(BS.loader.pop())()
}
}
});
var config = {
apiKey: "AIzaSyD-8lQUILli9sLB30y-cekNONwjIBzdiOg",
authDomain: "scratchpen-de051.firebaseapp.com",
databaseURL: "https://scratchpen-de051.firebaseio.com",
projectId: "scratchpen-de051",
strageBucket: "scratchpen-de051.appspot.com",
messagingSenderId: "355301139728"
};
firebase.initializeApp(config);
// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();
//Entry point into the application
document.addEventListener('DOMContentLoaded', function() {
var $ = jQuery;
//authentication.init();
var database = firebase.database();
var workspace = Blockly.inject('blocklyDiv', {
toolbox: tools.toolbox
});
var animWorkSpace = Blockly.inject('blocklyDivAnim', {
toolbox: tools.animTools
});
var globalVar = {
setStorageValue: function(key, value) {
sessionStorage.setItem(key, value);
},
getStorageValue: function(key) {
return sessionStorage.getItem(key)
},
fbDatabase: database,
generateUniqueID: function() {
return Math.random().toString(36).substr(2, 9);
},
currentProjectId: null,
getCurrentProjectId: function() {
return this.currentProjectId
},
currentProjectName: null,
getCurrentProjectName: function() {
return this.currentProjectName
},
workspace: workspace,
animWorkSpace: animWorkSpace,
getTimeStamp: function() {
return Date.now();
},
user: null, //Storing logged in user
projectsInDOM: [],
projectType: "default",
lang: "js"
};
authentication.globalVar = globalVar;
saveObj.globarVar = globalVar;
authentication.addClickListner();
dash.globalVar = globalVar;
Blockly.Blocks['move_to_right'] = {
init: function() {
this.appendDummyInput().appendField('Move to right').appendField(new Blockly.FieldNumber('10'),
'FIELDNAME');
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
Blockly.Blocks['move_to_left'] = {
init: function() {
this.appendDummyInput().appendField('Move to left').appendField(new Blockly.FieldNumber('10'),
'FIELDNAME');
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
Blockly.Blocks['move_up'] = {
init: function() {
this.appendDummyInput().appendField('Move up ').appendField(new Blockly.FieldNumber('10'), 'FIELDNAME');
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
Blockly.Blocks['move_down'] = {
init: function() {
this.appendDummyInput().appendField('Move down ').appendField(new Blockly.FieldNumber('10'), 'FIELDNAME');
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
Blockly.Blocks['turn_clockwise'] = {
init: function() {
this.appendDummyInput().appendField('Turn clockwise ').appendField(new Blockly.FieldNumber('10'), 'FIELDNAME').appendField(' deg');
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
Blockly.Blocks['turn_anticlockwise'] = {
init: function() {
this.appendDummyInput().appendField('Turn anticlockwise ').appendField(new Blockly.FieldNumber('10'), 'FIELDNAME').appendField(' deg');
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(160);
this.setTooltip('Returns number of letters in the provided text.');
this.setHelpUrl('http://www.w3schools.com/jsref/jsref_length_string.asp');
}
};
//Firebase session details
firebase.auth().onAuthStateChanged(function(user) {
crossroads.addRoute("main", function() {
//console.log("I am in the routes");
});
hasher.init();
hasher.setHash("main");
if (user) {
globalVar.user = user;
if (!globalVar.getStorageValue("email")) {
globalVar.setStorageValue("email", user.email)
}
$(".myProjectsBoard").show();
showMyProjects();
} else {
authentication.init();
dash.init();
console.log("not logged in");
$(".myProjectsBoard").hide();
}
});
var saveChangesModal = function() {
var projectName = $("#projectName")[0].value;
var projectDesc = $("#projectDesc")[0].value;
var projectType = globalVar.projectType;
var uid = globalVar.generateUniqueID(); //Unique Id for Project
var versionData = {};
var timeStamp = globalVar.getTimeStamp();
versionData[timeStamp] = saveObj.blockToXml(true, globalVar.projectType); //Timestamp is unique ID for version
globalVar.fbDatabase.ref("/Projects/" + uid).set({
"name": projectName,
"desc": projectDesc,
"timeStamp": timeStamp,
"author": globalVar.getStorageValue("email"),
"versions": versionData,
"projectType": projectType
}).then(function() {
$("#saveModal").modal('hide');
globalVar.currentProject = projectName;
globalVar.currentProjectId = uid;
var updates = {};
updates['/Users/' + globalVar.user.uid + '/projects/' + uid] = {
"name": projectName,
"desc": projectDesc,
"timestamp": timeStamp,
"projectType": globalVar.projectType
};
return firebase.database().ref().update(updates);
}).catch(function(error) {
//console.log("error saving project" ,error);
});
};
var saveToolbar = function() {
if ($("#login").is(":visible")) {
$(".notification").html("Please login to save").show();
setTimeout(function() {
$(".notification").fadeOut("slow");
}, 1000);
} else {
$("#saveModal").modal();
}
};
var blockToText = function() {
var code = Blockly.JavaScript.workspaceToCode(workspace);
if (globalVar.lang === "py") {
code = Blockly.Python.workspaceToCode(workspace);
}
document.getElementById('textGenerator').innerText = code; //puts block code into text editor
//console.log(code);
};
var blockToTextAnim = function(){
document.getElementById('textGeneratorAnim').innerText = Blockly.JavaScript.workspaceToCode(animWorkSpace);
};
var saveVersion = function() {
var newVersion = Date.now();
// Write the new post's data simultaneously in the posts list and the user's post list.
var updates = {};
//var ref = firebase.database().ref('/Projects/' + globalVar.currentProjectId + '/versions/');
updates['/Projects/' + globalVar.currentProjectId + '/versions/' + newVersion] = saveObj.blockToXml(true);
return firebase.database().ref().update(updates);
};
var openProject = function(event) {
var projectID = event.currentTarget.id.split("_")[0];
var name = event.currentTarget.parentElement.parentElement.children[0].innerText;
var type = event.currentTarget.parentElement.parentElement.children[1].innerText;
commonOpenProject(projectID, name, globalVar.user.email, type);
};
function commonOpenProject(projectID, name, userName, projectType) {
globalVar.lang = "js";
$(".project-info-name").text(name);
$(".project-info-user").text("Created By - " + userName);
globalVar.currentProjectId = projectID;
var ref = firebase.app().database().ref();
var projRef = ref.child('Projects/' + projectID + '/versions');
projRef.once('value', function(snap) {
//console.log(snap);
//loading most recent from version//
var versions = Object.keys(snap.val());
var versionsLen = Object.keys(snap.val()).length;
var mostRecent = versions[versionsLen - 1];
$(".project-list").hide();
$("#scratch").show();
//loading blocks into DOM
var xml = Blockly.Xml.textToDom(snap.val()[mostRecent]);
if(projectType === 'animation') {
aniWorkspace();
Blockly.mainWorkspace.clear();
Blockly.Xml.domToWorkspace(xml, animWorkSpace);
}
else{
defaultWorkspace();
Blockly.mainWorkspace.clear();
Blockly.Xml.domToWorkspace(xml, workspace);
}
});
var commentRef = ref.child('Projects/' + projectID + '/comments');
commentRef.on("child_added", function(snapshot) {
let comment = snapshot.val();
addComment(comment.name, comment.comment, comment.time);
});
$(".comments").show();
$(".jumbotron").hide();
$(".version-list").empty();
}
var openNewProject = function() {
//Resetting blocks
$("#scratch").show();
$(".project-list").hide();
$(".jumbotron").hide();
Blockly.mainWorkspace.clear();
$(".comments").show();
$(".version-list").empty();
defaultWorkspace();
};
var openFromVersion = function() {
var projectID = globalVar.currentProjectId;
var ref = firebase.app().database().ref();
var projRef = ref.child('Projects/' + projectID + '/versions');
$(".version-list").empty();
projRef.once('value', function(snap) {
//console.log(snap.val());
var versionObj = snap.val();
var list = $(".version-list");
for (var obj in versionObj) {
list.append("<li role=\"presentation\"><a role=\"menuitem\" tabindex=\"-1\" href=\"#\">" + moment(Number(obj)).format("DD/MM/YYYY h:mm:ss") + " - " + obj + "</a></li>");
}
}).then(function() {
$("#openVersionModal").modal();
$('.dropdown-menu a').click(function() {
$('#versionSelected').text($(this).text());
})
});
};
//opening the selected version
//Reusing int the project
var versionOpenModal = function() {
var version = (document.getElementById("versionSelected").innerHTML).split(" - ")[1];
var selectedXML;
if (version) {
var projectID = globalVar.currentProjectId;
var ref = firebase.app().database().ref();
var projRef = ref.child('Projects/' + projectID + '/versions');
projRef.once('value', function(snap) {
//console.log(snap.val());
var versionObj = snap.val();
var list = $(".version-list");
selectedXML = versionObj[version];
}).then(function() {
//loading blocks into DOM
$("#openVersionModal").modal('hide');
$("#scratch").show();
Blockly.mainWorkspace.clear();
var xml = Blockly.Xml.textToDom(selectedXML);
Blockly.Xml.domToWorkspace(xml, workspace);
$(".version-list").empty();
});
} else {
//console.log("Please enter correct xml format");
}
};
//Exporting project
var exportToolbar = function() {
if ($("#login").is(":visible")) {
$(".notification").html("Please login to save").show();
setTimeout(function() {
$(".notification").fadeOut("slow");
}, 1000);
} else {
$("#exportModal").modal();
var exportObj = saveObj.blockToXml(true);
$("#exportName").attr("value", exportObj);
}
};
//Importing project
var importToolbar = function() {
if ($("#login").is(":visible")) {
$(".notification").html("Please login to save").show();
setTimeout(function() {
$(".notification").fadeOut("slow");
}, 1000);
} else {
$("#importModal").modal();
}
};
//Importing the project
var importChangesModal = function() {
var importData = $("#importData")[0].value;
if (importData) {
//loading blocks into DOM
$("#importModal").modal('hide');
Blockly.mainWorkspace.clear();
$("#scratch").show();
//console.log(importData);
//Blockly.mainWorkspace.clear();
var xml = Blockly.Xml.textToDom(importData);
//console.log(xml);
Blockly.Xml.domToWorkspace(xml, workspace);
//console.log("hello");
} else {
//console.log("Please enter correct xml format");
}
};
//Autocomplete
var searchProjects;
var allProjects = function() {
searchProjects = {};
var projectNames = [];
var projectXML = [];
var ref = firebase.app().database().ref();
var searchRef = ref.child('Projects');
var i = 0;
searchRef.once('value', function(snap) {
var searchObj = snap.val();
for (var obj in searchObj) {
var latestVersion;
for (var version in searchObj[obj].versions) {
latestVersion = searchObj[obj].versions[version];
}
projectNames.push(searchObj[obj].name);
projectXML.push(latestVersion);
searchProjects[searchObj[obj].name] = Object.keys(searchObj)[i];
i++;
}
});
//console.log(searchProjects);
return [{
projects: projectNames,
xmls: projectXML
}]
};
//getting the list
var comboplete = new Awesomplete('input.dropdown-input', {
list: allProjects()[0].projects,
minChars: 1,
"awesomplete-selectcomplete": function(evnt) {
console.log(evnt.text);
}
});
//action listener for dropdown button
Awesomplete.$('.dropdown-btn').addEventListener("click", function() {
if (comboplete.ul.childNodes != null && comboplete.ul.childNodes.length === 0) {
comboplete.minChars = 0;
comboplete.evaluate();
} else if (comboplete.ul.hasAttribute('hidden')) {
comboplete.open();
} else {
comboplete.close();
}
});
//For loading the project once you select a project from the list
Awesomplete.$('.dropdown-input').addEventListener("awesomplete-select", function(event) {
$(".project-list").hide();
Blockly.mainWorkspace.clear();
$("#scratch").show();
$("#signin").hide();
$("#create").hide();
$("#importModal").modal('hide');
$(".jumbotron").hide();
$(".dashboard-container").hide();
//loading blocks into DOM
commonOpenProject(searchProjects[event.text.label],event.text.label);
});
//comment part
const timeStamp = () => {
let options = {
month: '2-digit',
day: '2-digit',
year: '2-digit',
hour: '2-digit',
minute: '2-digit'
};
let now = new Date().toLocaleString('en-US', options);
return now;
};
var postComment = function(e) {
if ($("#login").is(":visible")) {
$(".notification").html("Please login to comment").show();
setTimeout(function() {
$(".notification").fadeOut("slow");
}, 1000);
} else {
if (globalVar.currentProjectId == null) {
$(".notification").html("Please save the project to comment").show();
setTimeout(function() {
$(".notification").fadeOut("slow");
}, 1000);
} else {
var ref = firebase.app().database().ref();
var commentRef = ref.child('Projects/' + globalVar.currentProjectId + "/comments");
e.preventDefault();
let name = document.getElementById("yourName").value;
let comment = document.getElementById("comment").value;
if (name && comment) {
commentRef.push({
name: name,
comment: comment,
time: timeStamp()
});
}
commentRef.on("child_added", function(snapshot) {
let comment = snapshot.val();
addComment(comment.name, comment.comment, comment.time);
});
document.getElementById("yourName").value = '';
document.getElementById("comment").value = '';
}
}
};
const addComment = (name, comment, timeStamp) => {
let comments = document.getElementById("commentsList");
comments.innerHTML = `<hr><h4>${name} says<span>${timeStamp}</span></h4><p>${comment}</p>${comments.innerHTML}`;
}
//Code Reuse
var reuseToolbar = function() {
var blockList = [];
projectMapper = {};
var ref = firebase.app().database().ref();
var projRef = ref.child('Projects/');
projRef.once('value', function(snap) {
var projectObj = snap.val();
for (var obj in projectObj) {
blockList.push(projectObj[obj].name);
var latestVersion;
for (var version in projectObj[obj].versions) {
latestVersion = projectObj[obj].versions[version];
}
projectMapper[projectObj[obj].name] = latestVersion;
}
var list = $(".blockList");
for (var obj in blockList) {
list.append("<li role=\"presentation\"><a role=\"menuitem\" tabindex=\"-1\" href=\"#\">" + blockList[obj] + "</a></li>");
}
}).then(function() {
$("#reuseModal").modal();
$('.dropdown-menu a').click(function() {
$('#reuseSelected').text($(this).text());
})
});
};
//Reusing int the project
var reuseChangesModal = function() {
var blockID = document.getElementById("reuseSelected").innerHTML
if (blockID) {
//loading blocks into DOM
$("#reuseModal").modal('hide');
$("#scratch").show();
//Blockly.mainWorkspace.clear();
var xml = Blockly.Xml.textToDom(projectMapper[blockID]);
Blockly.Xml.domToWorkspace(xml, workspace);
$(".blockList").empty();
} else {
//console.log("Please enter correct xml format");
}
};
//logout functionality
var logoutChanges= function(){
$(".jumbotron").show();
$(".dashboard-container").show();
document.getElementById('sign-in').disabled = false;
$(".project-list").empty();
$(".version-list").empty();
$(".scratch").hide();
$(".comments").hide();
};
var aniWorkspace = function(){
//Blockly.mainWorkspace.clear();
$("#blocklyDiv, #animMode, #textGenerator").hide();
$("#blocklyDivAnim, #defaultMode, #textGeneratorAnim").show();
globalVar.projectType = "animation";
};
var defaultWorkspace = function () {
$("#blocklyDiv, #animMode, #textGenerator").show();
$("#blocklyDivAnim, #defaultMode, #textGeneratorAnim").hide();
globalVar.projectType = "default";
};
var animateCode = function() {
var code = $("#textGenerator").text();
var animObj = {};
var loopRef = {};
var blockRef = {};
var blocksInLoop = {};
var codeFragments = code.split("\n");
var el = $("#playgroundObj")[0];
for (var i = 0; i < codeFragments.length; i++) {
if(codeFragments[i].indexOf("for (var") !== -1) {
loopRef[i] = i;
//loopRef[i] = codeFragments[i].match(/(<\s\d+)/)[0].split(" ")[1];
blocksInLoop[i] = [codeFragments[i].match(/(<\s\d+)/)[0].split(" ")[1]];
}
if (codeFragments[i].indexOf("!move object to right") !== -1) {
if(loopRef[i-1] !== undefined){
loopRef[i] = loopRef[i-1];
blocksInLoop[loopRef[i-1]].push("MR " + Number(codeFragments[i].match(/\d+/)[0]));
}
else {
$(el).animate({
"marginLeft": "+=" + Number(codeFragments[i].match(/\d+/)[0])
}, 800);
}
}
if (codeFragments[i].indexOf("!move object to left") !== -1) {
if(loopRef[i-1] !== undefined ){
loopRef[i] = loopRef[i-1];
blocksInLoop[loopRef[i-1]].push("ML " + Number(codeFragments[i].match(/\d+/)[0]));
}
else {
$(el).animate({
"marginLeft": "-=" + Number(codeFragments[i].match(/\d+/)[0])
}, 800);
}
}
if (codeFragments[i].indexOf("!move object up") !== -1) {
if(loopRef[i-1] !== undefined ){
loopRef[i] = loopRef[i-1];
blocksInLoop[loopRef[i-1]].push("MU " + Number(codeFragments[i].match(/\d+/)[0]));
}
else {
$(el).animate({
"marginTop": "-=" + Number(codeFragments[i].match(/\d+/)[0])
}, 800);
}
}
if (codeFragments[i].indexOf("!move object down") !== -1) {
if(loopRef[i-1] !== undefined ){
loopRef[i] = loopRef[i-1];
blocksInLoop[loopRef[i-1]].push("MD " + Number(codeFragments[i].match(/\d+/)[0]));
}
else {
$(el).animate({
"marginTop": "+=" + Number(codeFragments[i].match(/\d+/)[0])
}, 800);
}
}
if(codeFragments[i].indexOf("!rotate object clockwise") !== -1 ) {
if(loopRef[i-1] !== undefined ){