-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-MyVideoPlayer.js
149 lines (127 loc) · 4.06 KB
/
MMM-MyVideoPlayer.js
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
/* Magic Mirror
* Module: MMM-VideoPlayer
*
* By justjim1220 (Jim Hallock)
*
* MIT Licensed.
*
* Brought to you by the makers of Cheyenne Cigars
* and my very own homemade Southern Sweet Tea.
*/
Module.register("MMM-MyVideoPlayer", {
// Default module config.
defaults: {
initialLoadDelay: 5150,
videoDir: "videos",
posterDir: "posters",
posterSize: {width:90, height:51},
menuDirection: "row",
playerSize: {width:930, height:523},
playerBackground: "MVPsplash.png",
},
self:null,
player:null,
videos:null,
posters:null,
requiresversion: "2.1.0",
getStyles: function () {
return ["MMM-MyVideoPlayer.css"];
},
// Define start sequence.
start: function () {
Log.info("Starting module: " + this.name);
self=this;
"use strict";
self.sendSocketNotification("CONFIG",self.config);
self.getVideos();
self.getPosters();
},
getVideos: function () {
this.sendSocketNotification("GET_VIDEOS");
},
getPosters: function () {
this.sendSocketNotification("GET_POSTERS");
},
socketNotificationReceived: function (notification, payload) {
Log.log("notification received="+notification);
if (notification === "BUTTON_PRESSED") {
this.processInfo(payload);
}
else if(notification=="Videos"){
this.videos=payload;
if(this.videos!=null){
Log.log("videos files="+this.videos);
}
else
Log.error("===>no video filenames returned, check the videosDir config entry");
}
else if(notification=="Posters"){
this.posters=payload
if(self.posters!=null){
Log.log("posters files="+this.posters);
self.buildPosterHash(this.posters);
self.updateDom(self.config.initialLoadDelay);
}
else
Log.error("===>no video icon filenames returned, check the postersDir config entry");
}
},
buildPosterHash: function(posters) {
self.Hash={};
for(var i=0; i<posters.length; i++) {
var work = posters[i];
self.Hash[work.substring(0, work.indexOf('.'))] = work;
}
},
createVideoButton: function(video, height, width) {
let button = document.createElement("button");
button.className = "button";
button.setAttribute("data-video-src", "modules/" + self.name + "/" + this.config.videoDir + "/" + video);
button.addEventListener("click", self.swapVideo);
let img = document.createElement("img");
img.src = "modules/" + self.name + "/" + this.config.posterDir + "/" + self.Hash[video.substring(0, video.indexOf('.'))];
img.width = width;
img.height = height;
button.appendChild(img);
return button;
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if(self.videos!=null){
var menu = document.createElement("span");
menu.className = "navMenu";
menu.id = this.identifier + "_menu";
menu.style.flexDirection = this.config.direction;
wrapper.appendChild(menu);
//<video controls
let video=document.createElement("video");
video.width = self.config.playerSize.width;
video.height = self.config.playerSize.height;
video.id = "player";
video.poster = "modules/" + this.name + "/" + self.config.playerBackground;
video.addEventListener("ended", function() {
video.load();
video.controls = false;
});
self.player = video;
wrapper.appendChild(video);
let videoselect = document.createElement("div");
videoselect.id = "videoSelect";
wrapper.appendChild(videoselect);
for(var i=0; i<self.videos.length ;i++){
videoselect.appendChild(this.createVideoButton(self.videos[i], self.config.posterSize.height, self.config.posterSize.width, video));
}
console.log(wrapper.innerHTML);
}
return wrapper;
},
swapVideo: function () {
Log.log("in handler for button="+this.getAttribute("data-video-src"));
self.player.src = this.getAttribute("data-video-src");
self.player.load();
self.player.controls = true;
self.player.play();
},
});