-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1468 lines (1251 loc) · 60.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">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MMMM</title>
<link rel="stylesheet" href="./mdl/material.css">
<script src="./mdl/material.min.js"></script>
<script src="jquery.js"></script>
<script src="./mdl/dialog-polyfill.js"></script>
<link rel="stylesheet" type="text/css" href="dialog-polyfill.css" />
<style>
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(mdl/material-design-icons/iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */
src: local('Material Icons'),
local('MaterialIcons-Regular'),
url(mdl/material-design-icons/iconfont/MaterialIcons-Regular.woff2) format('woff2'),
url(mdl/material-design-icons/iconfont/MaterialIcons-Regular.woff) format('woff'),
url(mdl/material-design-icons/iconfont/MaterialIcons-Regular.ttf) format('truetype');
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px; /* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
/* Support for all WebKit browsers. */
-webkit-font-smoothing: antialiased;
/* Support for Safari and Chrome. */
text-rendering: optimizeLegibility;
/* Support for Firefox. */
-moz-osx-font-smoothing: grayscale;
/* Support for IE. */
font-feature-settings: 'liga';
}
.demo-card-wide.mdl-card {
width: 384px;
}
.demo-card-wide > .mdl-card__title {
color: #fff;
height: 65px;
background-color: #485cb0;
}
.demo-card-wide > .mdl-card__menu {
color: #fff;
}
.demo-list-control {
width: 300px;
}
.demo-list-radio {
display: inline;
}
.page-content {
margin: auto;
padding-top: 0.25cm;
}
.mdl-grid {
padding: 0!important;
margin: auto;
}
.mdl-cell {
background: #FFF0F5;
padding-bottom: calc(1vh * 8.7);
padding-top: calc(1vh * 0);
text-align: center;
text-transform: none;
font-size: 2.5vw;
color: #000000;
font-weight: lighter;
margin: 7px;
cursor: pointer;
}
.cell--3-col-tablet.mdl-cell--3-col-tablet {
width: calc(33.3333% - 16px);
margin-right: auto;
margin-left: auto;
}
.mdl-cell--1-col, .mdl-cell--1-col-tablet.mdl-cell--1-col-tablet {
width: calc(33.3333% - 16px);
margin-right: auto;
margin-left: auto;
}
.mdl-cell--3-col {
width: calc(100vw - 16px);
margin-right: auto;
margin-left: auto;
}
.more-options {
margin-right: 0;
margin-left: auto;
}
.update-positions-button {
width: calc(100vw - 16px);
margin-right: auto;
margin-left: auto;
margin-top: 5px;
}
#sortable1, #sortable2, #sortable3 {
border: 1px solid #eee;
width: 200px;
min-height: 20px;
list-style-type: none;
margin: 0;
padding: 5px 0 0 0;
float: left;
margin-right: 10px;
}
#sortable1 li, #sortable2 li, #sortable3 li {
margin: 0 5px 5px 5px;
padding: 5px;
font-size: 1.2em;
width: 120px;
}
</style>
<dialog class="mdl-dialog">
<h1 id="dialogTitle" class="mdl-dialog__title">Add Module</h1>
<div class="mdl-dialog__content">
<p id="dialogInstructions">
Paste the module's GitHub URL:
</p>
<!-- Simple Textfield -->
<form action="#" id="dialogTextFieldForm">
<div class="mdl-textfield mdl-js-textfield" id="dialogTextFieldDiv">
<input class="mdl-textfield__input" type="text" id="dialogTextField">
<label class="mdl-textfield__label" for="dialogTextField" id="dialogLabel">https://github.com/EliteByte/MMM-example</label>
</div>
</form>
<!-- Floating Multiline Textfield -->
<form action="#">
<div class="mdl-textfield mdl-js-textfield" id="dialogMultiLineDiv">
<textarea class="mdl-textfield__input" type="text" rows= "3" id="dialogMultiLine"></textarea>
<label class="mdl-textfield__label" for="dialogMultiLine" id="dialogMultiLineLabel">
{ <br/>
  module: "clock", <br/>
  position: "top_left", <br/>
  config: { <br/>
    // The config property is optional. <br/>
    // See 'Configuration options' for more information. <br/>
  } <br/>
} <br/>
</label>
</div>
</form>
</div>
<div class="mdl-dialog__actions">
<button type="button" id="dialogNextButton" class="mdl-button" onclick="dialogNext()">Next</button>
<button type="button" id="dialogCancelButton" class="mdl-button close">Cancel</button>
<br> </br>
<div id="dialogProgress" class="mdl-progress mdl-js-progress"></div>
</div>
</dialog>
</head>
<body>
<!-- Simple header with fixed tabs. -->
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header
mdl-layout--fixed-tabs">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<!-- Title -->
<span class="mdl-layout-title">MMMM</span>
</div>
<!-- Tabs -->
<div class="mdl-layout__tab-bar mdl-js-ripple-effect">
<a id="firstTab" href="#fixed-tab-1" class="mdl-layout__tab is-active">Modules</a>
<a id="secondTab" href="#fixed-tab-2" class="mdl-layout__tab">Config</a>
<a id="thirdTab" href="#fixed-tab-3" class="mdl-layout__tab">Position</a>
</div>
</header>
<div class="mdl-layout__drawer">
<span class="mdl-layout-title">MMMM</span>
</div>
<main class="mdl-layout__content">
<!-- First Tab -->
<section class="mdl-layout__tab-panel is-active" id="fixed-tab-1">
<div align="center" class="page-content">
<!-- Start Module List Card -->
<div align="center" class="demo-card-wide mdl-card mdl-shadow--2dp">
<ul id="sortable1" align="center" class="demo-list-control mdl-list">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text" text-align="center">Modules</h2>
</div>
</ul>
<button id="add-module" type="button" class="mdl-button mdl-button--colored">Add Module</button>
<button id="update-modules" type="button" class="mdl-button mdl-button--colored">Update Modules</button>
</div>
</div>
</section>
<!-- Second Tab -->
<section class="mdl-layout__tab-panel" id="fixed-tab-2">
<div align="center" class="page-content">
<!-- Start Config Card -->
<div align="center" id="configCard" class="demo-card-wide mdl-card mdl-shadow--2dp">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">Config</h2>
</div>
<ul id="configList" class="demo-list-control mdl-list">
<li class="mdl-list__item" id="superman">
<span class="mdl-list__item-primary-content">
<i class="material-icons ">chrome_reader_mode</i>
Enabled
</span>
<span class="mdl-list__item-secondary-action">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="enableButton">
<input type="checkbox" id="enableButton" class="mdl-switch__input" checked />
</label>
</span>
</li>
</ul>
<div class="mdl-card__actions mdl-card--border">
</div>
<button id="updateButton" align="center" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" onclick="updateConfig()">
Update
</button>
<!-- Position Button - Top Right -->
<div class="mdl-card__menu">
<button id="positionButton"
class="mdl-button mdl-js-button mdl-button--icon">
<i class="material-icons">my_location</i>
</button>
</div>
<ul id="positionList" class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect"
for="positionButton">
</ul>
<!-- End Position Button -->
<!-- Remove Module Button - Top Right -->
<div class="mdl-card__menu" style="margin-right: 35px; ">
<button id="removeButton"
class="mdl-button mdl-js-button mdl-button--icon" onclick="removeModule()">
<i class="material-icons">remove_circle_outline</i>
</button>
</div>
<!-- End Remove Module Button -->
<!-- Update Module Button - Top Right -->
<div class="mdl-card__menu" style="margin-right: 70px; ">
<button id="removeButton"
class="mdl-button mdl-js-button mdl-button--icon" onclick="updateModule()">
<i class="material-icons">update</i>
</button>
</div>
<!-- End Update Module Button -->
</div>
<div id="tab-2" class="mdl-js-snackbar mdl-snackbar">
<div class="mdl-snackbar__text"></div>
<button class="mdl-snackbar__action" type="button"></button>
</div>
</div>
</section>
<!-- Third Tab -->
<section class="mdl-layout__tab-panel" align="center" id="fixed-tab-3">
<div id="positionContent" class="page-content positionContent" style="padding-top: 0;" align="center">
<div>
<!-- Raised button with ripple -->
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect update-positions-button" onclick="updateConfigPositions()">
Update
</button>
<div class="demo-grid-ruler mdl-grid">
<button id="pos-top_bar" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--3-col">Top Bar</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-top_bar" id="posList-top_bar">
</ul>
</div>
<div class="demo-grid-2 mdl-grid">
<button id="pos-top_left" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--1-col">Top Left</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-top_left" id="posList-top_left">
</ul>
<button id="pos-top_center" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--1-col">Top Center</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-top_center" id="posList-top_center">
</ul>
<button id="pos-top_right" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--1-col">Top Right</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-top_right" id="posList-top_right">
</ul>
</div>
<div class="demo-grid-ruler mdl-grid">
<button id="pos-upper_third" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--3-col mdl-cell--9-col-tablet">Upper Third</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-upper_third" id="posList-upper_third">
</ul>
</div>
<div class="demo-grid-ruler mdl-grid">
<button id="pos-middle_center" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--3-col mdl-cell--9-col-tablet">Middle Center</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-middle_center" id="posList-middle_center">
</ul>
</div>
<div class="demo-grid-ruler mdl-grid">
<button id="pos-lower_third" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--3-col mdl-cell--9-col-tablet">Lower Third</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-lower_third" id="posList-lower_third">
</ul>
</div>
<div class="demo-grid-2 mdl-grid">
<button id="pos-bottom_left" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--1-col">Bottom Left</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-bottom_left" id="posList-bottom_left">
</ul>
<button id="pos-bottom_center" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--1-col">Bottom Center</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-bottom_center" id="posList-bottom_center">
</ul>
<button id="pos-bottom_right" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--1-col">Bottom Right</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-bottom_right" id="posList-bottom_right">
</ul>
</div>
<div class="demo-grid-ruler mdl-grid">
<button id="pos-bottom_bar" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored mdl-cell mdl-cell--3-col mdl-cell--9-col-tablet">Bottom Bar</button>
<ul class="mdl-menu mdl-menu--top-right mdl-js-menu mdl-js-ripple-effect"
data-mdl-for="pos-bottom_bar" id="posList-bottom_bar">
</ul>
</div>
</div>
<div id="tab-3" class="mdl-js-snackbar mdl-snackbar">
<div class="mdl-snackbar__text"></div>
<button class="mdl-snackbar__action" type="button"></button>
</div>
</div>
</section>
</main>
</div>
<script src="../MagicMirror/config/config.js"></script>
<script src="config/config.js"></script>
<script src="socket.io.js"> </script>
<script type="text/javascript">
<!--
if (screen.width >= 699) {
var positionContent = document.getElementById('positionContent');
var thirdPositions = document.getElementsByClassName("mdl-cell");
var updatedPositionButtons = document.getElementsByClassName("update-positions-button");
positionContent.style.width = "75%";
for (third in thirdPositions) {
thirdPositions[third].width
}
updatedPositionButtons[0].style.width = "100%";
}
//-->
</script>
<script>
/**
* @file MMMM JavaScript Handler
* @author Mario Figueroa <[email protected]>
* @version 0.1 Alpha
*/
var rawModules = config.modules;
var arrayModules = [];
var mapModulePositions = {};
var mapModulePositionsReverse = {};
var updatedPosition = null;
var currentPosition = null;
var updateStatic = false;
var currentModule = null;
var currentModulePos = null;
var fullDynamicConfig = config;
var dynamicConfig = {};
var selectedPosition = null;
var takenPositionsDynamic = mapModulePositions;
var dialogInfo = { page: 1 };
var socketURL = ("http://" + window.location.hostname + ":" + window.location.port);
var socket = io(socketURL, {transports: ['websocket', 'polling', 'flashsocket']});
/**
* Pre-Initialization Functions
*
* @function {Void} [v0.1] checkHash - Check the URL Hash to see if the client has already selected a module.
* @function {Void} [v0.1] parseModules - Concise description of the tag.
*/
/**
* CheckHash - Search for a module in rawModules by it's name. [v0.1]
*
* @executes {Function} populateConfigCard() - If URL does contain a valid hash then it will load up the respective config in Config-Tab.
* @throws {Error} hashNameDNE - If the URL has a hashname that can't be found as a module it will throw an exception.
* @logging {Info} urlDNCHash - If the URL DNC (Does Not Contain) a hash it will just log it.
*/
function checkHash() {
var href = window.location.href;
if(href.indexOf("#") > 0) {
// NOTE: Dissect hash from URL and store as hashName.
var hashName = window.location.hash.substr(1, window.location.hash.lastIndexOf(''));
var modules = document.getElementById('sortable1').childNodes;
if (findModule(hashName)) {
for (li in modules) {
if (modules[li].tagName == 'LI') {
if (modules[li].innerHTML == hashName) {
// NOTE: Paint previously selected module to pink.
modules[li].style.backgroundColor = "#ec4174";
}
}
}
// NOTE: Load previously selected module, in Config-Tab.
populateConfigCard(findModule(hashName));
} else {
// NOTE: Throw exception, hashNameDNE.
console.error("Client Requested a hashname which doesn't exist.");
}
} else {
// NOTE: Log, URL does not contain hash.
console.log("URL Requested, does not contain a known anchor hash.");
}
}
/**
* Parse Modules - Search for a module in rawModules by it's name. [v0.1]
*
* @set {Variable} Sets up an object (mapModulePositions) to contain all modules with a configurable position.
* @format Keys = ["moduleName", "moduleName", ...], Values = ["modulePosition", "modulePosition", ...]
* @set {Variable} Sets up an object (mapModulePositionsReverse) like it's parent (mapModulePositions) but keys/values flipped.
* @format Keys = ["modulePosition", "modulePosition", ...], Values = ["moduleName", "moduleName", ...]
*/
function parseModules() {
for (mod in rawModules) {
if (rawModules[mod].position != null) {
mapModulePositions[rawModules[mod].module] = rawModules[mod].position;
mapModulePositionsReverse[rawModules[mod].position] = rawModules[mod].module;
}
}
}
function checkHelloWorldModule() {
if (findModule("helloworld") == null) {
var referenceConfig = config;
var parsedInfoMMMM = infoMMMM;
parsedInfoMMMM.config.text = ("ModuleManager URL: " + socketURL);
referenceConfig.modules[config.modules.length] = parsedInfoMMMM;
var data = ("var config = " + JSON.stringify(referenceConfig, null, "\t") + ";" + "\n if (typeof module !== 'undefined') {module.exports = config;}");
sendOutData(data).then(function (response) {
if (response == true) {
console.log("Successfully sent out the helloworld config to the server.");
} else {
console.error("[Error] Failed to send out helloworld config to the server.");
}
});
}
}
/**
* End of, Pre-Initialization Functions
*/
/**
* Utility Functions
*
* @function {Object} [v0.1] findModule() - Search for a module in rawModules by it's name.
* @function {Object} [v0.1] findModulePosition() - Search for a module's position in the array rawModules.
* @function {Void} [v0.1] snackBar() - Overlays a message on the bottom of the Client's screen.
* @function {Array} [v0.1] positionsNotTaken() - Loop throught all rawModules positions and take them away from all possible positions; and then return one's left.
*/
/**
* Find Module - Search for a module in rawModules by it's name. [v0.1]
*
* @param {String} name - Name of module, i.e. [alert, weatherforecast, updatenotification]
* @returns {Object} module - A module as an object from the config.js.
* @child {Function} findModulePosition() - Derivative of this function.
*/
function findModule(name) {
for (mod in rawModules) {
if (rawModules[mod].module == name) {
return rawModules[mod];
}
}
}
/**
* Find Module Position - Search for a module in rawModules and get its rawModules {Array} position. [v0.1]
*
* @param {String} name - Name of module, i.e. [alert, weatherforecast, updatenotification]
* @returns {Integer} int - Integer that references the module when doing rawModules[int];
* @parent {Function} findModule() - Anti-Derivative of this function.
*/
function findModulePosition(name) {
var int = -1;
for (mod in rawModules) {
int += 1;
if (rawModules[mod].module == name) {
return int;
}
}
}
/**
* Snack Bar - Presents a MDL snackBar message for a few seconds. [v0.1]
*
* @param {String} msg - The message being displayed.
* @param {Integer} tab - The tab number that you want it to be overlayed on.
* @dependency {JavaScript} mdl - The Material Design Lite package by Google.
*/
function snackBar(msg, tab) {
var snackbarContainer = document.querySelector('#tab-' + tab);
var data = {message: msg};
snackbarContainer.MaterialSnackbar.showSnackbar(data);
}
/**
* Trigger Handicap - Easy function to "handicap" element(s). [v0.1]
*
* @param {[Object]} parts - Array of Objects that will be affected.
* @example triggerHandicap(elementID: "disable | enable | show | hide", ...);
*/
function triggerHandicap(parts) {
var partKeys = Object.keys(parts);
for (part in partKeys) {
var partElement = document.getElementById(partKeys[part]);
switch (parts[partKeys[part]]) {
case "disable":
partElement.disabled = true;
break;
case "enable":
partElement.disabled = false;
break;
case "show":
partElement.style.display = "initial";
break;
case "hide":
partElement.style.display = "none";
break;
default:
console.error("triggerHandicap() was called with key, " + parts[partKeys[part]] + " but the key's value which was passed didn't satisfy any elements. Try, [disable | enable | show | hide]");
}
}
}
/**
* Positions Not Taken - Loop through all rawModules positions and take them away from all possible positions; and then return the one's left. [v0.1]
*
* @returns {Array} allPositions - Returns all possible module positions minus the one's taken.
*/
function positionsNotTaken() {
var allPositions = ["top_bar", "bottom_bar", "top_left", "bottom_left", "top_right", "bottom_right", "top_center", "bottom_center", "upper_third", "middle_center", "lower_third"];
for (pos in mapModulePositions) {
for (var i=allPositions.length-1; i>=0; i--) {
if (allPositions[i] === mapModulePositions[pos]) {
allPositions.splice(i, 1);
}
}
}
return allPositions;
}
function sendOutData(data) {
return new Promise( function (resolve, reject) {
socket.emit('configUpdate', data, function (response) {
resolve(response);
return response;
});
}, function (error) {
console.error("Error emiting data through socket. -> [ " + error + " ]");
return false;
});
try {
} catch (err) {
}
}
function removeModule() {
var moduleName = currentModule.module;
try {
var removedConfig = config;
var index = removedConfig.modules.indexOf(currentModule);
removedConfig.modules.splice(index, 1);
var data = ("var config = " + JSON.stringify(removedConfig, null, "\t") + ";" + "\n if (typeof module !== 'undefined') {module.exports = config;}");
socket.emit('removeModuleConfig', data, function (confirmation) {
if (confirmation == true) {
console.log("Successfully removed " + moduleName + "'s entry from config.js \nMoving onto removing it from the server's version management system. (repos.js)");
socket.emit('removeModule', { 'moduleName': moduleName }, function (secondConfirmation) {
if (secondConfirmation == true) {
console.log("Successfully finished all existence of " + moduleName);
location.reload();
flushHash();
} else {
console.error("Failed removing module from repos.js. \nDetails from server -> " + secondConfirmation);
}
});
} else {
console.error("Failed removing module from config.js. \nDetails from server -> " + secondConfirmation);
}
});
return true;
} catch (err) {
console.error("Failed removing module " + moduleName + " \nDetails -> " + err);
return false;
}
}
function sendOutGitHubRequest(requestType, repoURL) {
return new Promise(function (resolve, reject) {
switch (requestType) {
case "clone":
socket.emit('clone', { type: 'clone', repo: repoURL }, function (response) {
if (response == true) {
resolve(response);
} else {
reject(response);
console.error(response);
snackBar(("Failed cloning the module with error: " + response), 1);
}
});
break;
case "pull":
socket.emit('pull', { type: 'pull', repo: repoURL });
break;
default:
console.error("[Error] Function, sendOutGitHubRequest() failed. \n Caused by an unknown requestType -> " + requestType);
}
return true;
});
}
function updateModule() {
return new Promise(function (resolve, reject) {
socket.emit('updateModule', { module: currentModule.module }, function (response) {
if (response == true) {
console.info("Successfully updated module, " + currentModule.module + ".");
snackBar(("Successfully updated module, " + currentModule.module + "."), 2);
resolve(true);
} else {
console.error("Error after emiting updateModule to server. \nDetails -> " + response);
reject(response);
return response;
}
});
});
}
function flushHash() {
window.location.hash = "";
}
/**
* End of, Utility Functions
*/
/**
* Tab #1 || Modules-Tab Functions
*
* @function {Object} [v0.1] appendModules() - Add/Append all modules that are taken from the config.js.
*/
/**
* Append Modules - Add/Append all modules that are taken from the config.js file. [v0.1]
*/
function appendModules() {
var ul = document.getElementById("sortable1");
var buttonWidthSync = "180px";
var arryNumber = -1;
// NOTE: Go through all rawModules and add them to the module list.
for (mod in rawModules) {
var li = document.createElement("li");
var btn = document.createElement("button");
li.appendChild(btn);
li.className = "mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored";
li.innerHTML = rawModules[mod].module;
li.style = "cursor:pointer";
li.style.fontSize = "15px";
li.style.width = buttonWidthSync;
li.style.textAlign = "center";
arrayModules.push(rawModules[mod].module);
arryNumber = arryNumber + 1;
li.id = arryNumber;
li.addEventListener('click', function(e) {
for (li in this.parentNode.childNodes) {
if (this.parentNode.childNodes[li].tagName == 'LI') {
// NOTE: Reset all module's color to (unselected) blue.
this.parentNode.childNodes[li].style.backgroundColor = "#3b5bb4";
}
}
// NOTE: Set selected module to pink.
this.style.backgroundColor = "#ec4174";
var targetName = arrayModules[this.id];
// NOTE: Set URL hash to module's name i.e. (localhost:1984/#calendar)
var href = window.location.href;
if (href.indexOf("#") > 0) {
href = href.split("#")[0] + "?var=1#" + href.split("#")[1];
window.location.hash = '';
}
location.hash = ("#" + targetName);
// NOTE: Clear all previous config items in Config-Tab, using jQuery.
$("li[id=configItem]").remove();
// NOTE: After 20ms populateConfigCard() and click on the Config-Tab.
setTimeout(function () { populateConfigCard(findModule(targetName));
document.getElementById("secondTab").click();}, 20);
});
ul.appendChild(li);
}
}
function handleDialogue() {
var dialog = document.querySelector('dialog');
var addModuleButton = document.querySelector('#add-module');
var dialogTextFieldDiv = document.getElementById("dialogTextFieldDiv");
var dialogMultiLineLabel = document.getElementById("dialogMultiLineLabel");
var dialogMultiLine = document.getElementById("dialogMultiLine");
var dialogMultiLineDiv = document.getElementById("dialogMultiLineDiv");
triggerHandicap({dialogMultiLine: "hide", dialogMultiLineLabel: "hide"});
if (! dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
addModuleButton.addEventListener('click', function() {
dialog.showModal();
});
dialog.querySelector('.close').addEventListener('click', function() {
dialogPage = 0;
dialogProgress.MaterialProgress.setProgress(0);
dialog.close();
});
document.querySelector('#dialogProgress').addEventListener('mdl-componentupgraded', function() {
this.MaterialProgress.setProgress(0);
});
dialog.style.width = "60vw";
dialogTextFieldDiv.style.width = "100%";
dialogMultiLine.style.width = "100%";
dialogMultiLineDiv.style.width = "100%";
dialogMultiLine.style.height = "27vh";
}
function dialogNext() {
var dialogTextField = document.getElementById("dialogTextField");
var dialogTextFieldForm = document.getElementById("dialogTextFieldForm");
var dialogTitle = document.getElementById("dialogTitle");
var dialogNextButton = document.getElementById("dialogNextButton");
var dialogProgress = document.getElementById("dialogProgress");
var dialogInstructions = document.getElementById("dialogInstructions");
var dialogLabel = document.getElementById("dialogLabel");
var dialogMultiLine = document.getElementById("dialogMultiLine");
var dialogMultiLineLabel = document.getElementById("dialogMultiLineLabel");
var dialogNextButton = document.getElementById("dialogNextButton");
switch (dialogInfo.page) {
case 1:
// NOTE: Set back everthing to normal.
dialogNextButton.innerHTML = "Next";
dialogTitle.innerHTML = "Add Module";
if (dialogTextField.value != null) {
dialogInfo.url = dialogTextField.value;
dialogTextFieldForm.parentNode.removeChild(dialogTextFieldForm);
dialogInstructions.innerHTML = "Paste Module's Default Config:";
triggerHandicap({"dialogMultiLine": "show", "dialogMultiLineLabel": "show"});
dialogInfo.page = dialogInfo.page + 1;
dialogProgress.MaterialProgress.setProgress(dialogInfo.page * 33.33);
}
break;
case 2:
if (dialogMultiLine.value != null) {
dialogInfo.config = dialogMultiLine.value;
dialogMultiLine.parentNode.parentNode.removeChild(dialogMultiLine.parentNode);
dialogNextButton.innerHTML = "Finish";
dialogTitle.innerHTML = "Clone & Apply Module";
dialogInstructions.innerHTML = "Click to download module onto MagicMirror and apply config.";
dialogInfo.page = dialogInfo.page + 1;
dialogProgress.MaterialProgress.setProgress(dialogInfo.page * 33.33);
}
break;
case 3:
sendOutData(parseConfigData(dialogInfo.config)).then(function (response) {
if (response == true) {
console.log("Successfully added new module to the config.js");
sendOutGitHubRequest('clone', dialogInfo.url).then(function (response) {
if (response == true) {
// NOTE: Set URL hash to module's name i.e. (localhost:1984/#calendar)
var href = window.location.href;
var moduleIndex = (config.modules.length - 1);
if (href.indexOf("#") > 0) {
href = href.split("#")[0] + "?var=1#" + href.split("#")[1];
window.location.hash = '';
}
location.hash = ("#" + config.modules[moduleIndex].module);
location.reload();
} else {
console.error("[Error] GitHub Clone Request to download module to Mirror has failed. Check your MagicMirror console. Caught Error -> " + response);
snackBar(("Error sending out GitHub Clone request to server. Check Pi's console. "), 1);
alert(response);
}
});
} else {
console.error("[Error] Data sent to config has failed. Parsed config may be invalid. \nDetails -> " + response);
snackBar(("Error sending out data to config. Double check your pasted config. "), 1);
alert(response);
}
});
break;
default:
break;
}
}
function parseConfigData(text) {
var substringed = text.substring(0, 7);
var cfg = null;
if (substringed == "modules") {
cfg = eval(text);
fullDynamicConfig.modules[fullDynamicConfig.modules.length] = cfg[0];
} else {
cfg = eval('('+text+')');
fullDynamicConfig.modules[fullDynamicConfig.modules.length] = cfg;
}
var data = ("var config = " + JSON.stringify(fullDynamicConfig, null, "\t") + ";" + "\n if (typeof module !== 'undefined') {module.exports = config;}");
return data;
}
// var string = "modules: [ { module: 'MMM-bitcoin', position: 'top_right', config: { fiat: 'usd', showBefore: 'foo', updateInterval: 60000 } }, ]";
// eval('(')
/**
* End of, Modules-Tab Functions
*/
/**
* Tab #2 || Config-Tab Functions
*
* @function {Void} [v0.1] populateConfigCard() - Populate the Config-Tab (Tab-3) with a module's pre-made config.
* @function {Void} [v0.1] populatePositionDropdown() - Populate the dropdown menu on the Config-Tab which lists possible positions for the module.
* @function {Object} [v0.1] updateConfig() - Push fullDynamicConfig Object which containts a constatly updating version of the config.js file.
* @function {Void} [v0.1] clearConfig() - Clear all inputs fromt the Config-Tab Card.
* @function {Void} [v0.1] updateConfigUI() - Clear all config inputs and then re-populate Config-Tab Card with hashName.
* @function {Void} [v0.1] updateConfigPositions() -
* @dependency {JavaScript} [v0.1] mdl - The Material Design Lite package by Google.
*/
/**
* Populate Config Card - Populate the Config-Tab Card with a module's pre-made config. [v0.1]
*
* @param {Object} mod - The message being displayed.
* @exception {Error} notConfigurable - The module that was passed didn't have a position or config to change.
* @exception {Error} positionConfigurable - The module that was passed has a position but no config.
* @dependency {JavaScript} mdl - The Material Design Lite package by Google.
*/
function populateConfigCard(mod) {
var ul = document.getElementById("configList");
updateStatic = false;
currentModule = mod;
if (mod.config) {
triggerHandicap({enableButton: "enable", updateButton: "disable", positionButton: "enable"});
populatePositionDropdown();
var configObjectMap = {};
for (cfg in mod.config) {
var li = document.createElement("li");
li.className = "mdl-list__item lit";
li.id = "configItem";
var spanPrimary = document.createElement("span");
spanPrimary.className = "mdl-list__item-primary-content";
spanPrimary.innerHTML = cfg + ": ";
spanPrimary.style.padding = "10px";
var spanSecondary = document.createElement("span");
spanSecondary.className = "mdl-list__item-secondary-action";
spanSecondary.id = cfg;
if (mod.config[cfg] != "[object Object]") {
var div = document.createElement("div");
div.className = "mdl-textfield mdl-js-textfield mdl-textfield--floating-label is-upgraded";
var input = document.createElement("input");
input.className = "mdl-textfield__input";
input.type = "text"
input.id = cfg;
li.onkeyup = function() {
buttonToggle("updateButton");
}
var label = document.createElement("label");