forked from mozilla/popcorn-editor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PopcornEditor.js
200 lines (184 loc) · 5.92 KB
/
PopcornEditor.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
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
/**
* iframe embedding API for PopcornEditor
*
* @todo message handler should allow cross-site embedding for different security
* @todo add a way to return status/data from listeners?
*/
function PopcornEditor() {
var self = this,
listeners = {};
this.init = function (el, url) {
var editor = (typeof el === 'string') ? document.getElementById(el) : el,
url = url || 'PopcornEditor/editor.html',
loadingDiv = null,
loadingSpinner = null;
window.addEventListener('message', onmessage);
this.listen('loaded', function () {
document.querySelector('#loading').remove();
});
loadingDiv = document.createElement('div');
loadingDiv.setAttribute('id', 'loading');
loadingDiv.style.width = '100%';
loadingDiv.style.height = '100%';
loadingDiv.style.backgroundColor = 'white';
loadingDiv.style.display = 'flex';
loadingDiv.style.justifyContent = 'center';
loadingSpinner = document.createElement('img');
loadingSpinner.setAttribute('class', 'loading-spinner');
loadingSpinner.setAttribute('src', 'PopcornEditor/resources/ring-alt.svg');
loadingSpinner.style.width = '30%';
loadingDiv.appendChild(loadingSpinner);
editor.appendChild(loadingDiv);
this.iframe = document.createElement('iframe'),
this.iframe.setAttribute('src', url);
this.iframe.setAttribute('frameborder', '0');
this.iframe.style.width = '100%';
this.iframe.style.height = '100%';
editor.appendChild(this.iframe);
};
this.close = function () {
window.removeEventListener('message', onmessage);
this.iframe.parent.removeChild(this.iframe);
this.iframe = null;
for (key in listeners) {
delete listeners[key];
}
};
/**
* Sets the given handler as the handler for the event
*
* @param eventName : [string] name of the event (must be in events)
* @param handler : [function] takes event
*/
this.listen = function (eventName, handler) {
if (listeners[eventName] === undefined) {
listeners[eventName] = [handler];
} else {
listeners[eventName].push(handler);
}
}
/**
* Remove an event listener
*
* @param eventName : [string] name of the event (must be in events)
* @param handler : [function] handler to remove
*/
this.unlisten = function (eventName, handler) {
if (listeners[eventName] !== undefined) {
var found = listeners[eventName].indexOf(handler);
if (found !== -1) {
listeners[eventName].splice(found, 1);
}
}
}
/**
* Loads the popcorn json blob into the editor
*
* @param data : [object] json object
*/
this.loadInfo = function (data) {
this.iframe.contentWindow.postMessage({
data: data,
type: 'load'
}, window.location.origin);
};
/**
* Given a javascript object which fits the schema defined below, popcorn
* editor will load that video into the editor.
*
* @param video : javascript object of video
*/
this.createTemplate = function (video) {
var videoUrl = video.url;
data = {
"template": "basic",
"background": "#FFFFFF",
"data": {
"targets": [{
"id": "Target0",
"name": "video-container",
"element": "video-container",
}],
"media": [{
"id": "Media0",
"name": "Media0",
"url": "#t=,30",
"target": "video",
"duration": video.duration,
"popcornOptions": {
"frameAnimation": true,
},
"controls": true,
"tracks": [{
"name": "",
"id": "0",
"order": 0,
"trackEvents": [{
"id": "TrackEvent0",
"type": "sequencer",
"popcornOptions": {
"start": 0,
"source": [video.url],
"fallback": "",
"denied": false,
"end": video.duration,
"from": 0,
"title": video.title,
"type": "AirMozilla",
"thumbnailSrc": video.thumbnail,
"duration": video.duration,
"linkback": "",
"contentType": "",
"hidden": false,
"target": "video-container",
"left": 0,
"top": 0,
"width": 100,
"height": 100,
"volume": 100,
"mute": false,
"zindex": 1000,
"id": "TrackEvent0"
},
"track": "0",
"name": "TrackEvent0"
}]
}],
"clipData": {
},
"currentTime": 0,
}]
},
"tags": ["popcorn"],
}
// Need to dynamically set the clipdata key
data.data.media[0].clipData[videoUrl] = {
"type": video.type,
"title": video.title,
"source": video.url,
"thumbnail": video.thumbnail,
"duration": video.duration
}
return data;
};
function onmessage (e) {
if (e.source !== self.iframe.contentWindow) {
return;
}
if (e.origin !== window.location.origin) {
return;
}
for (key in listeners) {
if (e.data.type === key) {
listeners[key].forEach(function(handler) {
handler(e.data.data);
});
}
}
}
}
// List of events that PopcornEditor supports
PopcornEditor.events = {
save: 'save',
loaded: 'loaded'
};