forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 279
/
VideoViewer.js
320 lines (271 loc) · 11.3 KB
/
VideoViewer.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
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
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
define(function (require, exports, module) {
"use strict";
var DocumentManager = require("document/DocumentManager"),
VideoViewTemplate = require("text!htmlContent/video-view.html"),
ProjectManager = require("project/ProjectManager"),
PreferencesManager = require("preferences/PreferencesManager"),
MainViewFactory = require("view/MainViewFactory"),
Strings = require("strings"),
Filer = require("filesystem/impls/filer/BracketsFiler"),
Path = Filer.Path,
StringUtils = require("utils/StringUtils"),
FileSystem = require("filesystem/FileSystem"),
UrlCache = require("filesystem/impls/filer/UrlCache"),
FileUtils = require("file/FileUtils"),
Content = require("filesystem/impls/filer/lib/content"),
_ = require("thirdparty/lodash"),
Mustache = require("thirdparty/mustache/mustache"),
StartupState = require("bramble/StartupState");
var _viewers = {};
// Get a URL out of the cache
function _getVideoUrl(file) {
return UrlCache.getUrl(file.fullPath);
}
// Get a URL out of the cache that you can use in the project HTML
function _getLocalVideoUrl(file) {
var root = StartupState.project("root");
return encodeURI(file.fullPath.replace(root,"").replace("/",""));
}
/**
* Check if it's a video file
*/
function isVideo(fullPath) {
return Content.isVideo(Path.extname(fullPath));
}
/**
* VideoView objects are constructed when a video is opened
* @see {@link Pane} for more information about where VideoViews are rendered
*
* @constructor
* @param {!File} file - The video file object to render
* @param {!jQuery} container - The container to render the video view in
*/
function VideoView(file, $container) {
var that = this;
this.file = file;
this.container = $container;
// Set defaults, and keep track of which video element attributes are enabled.
this.videoTagSettings = {
controls: true,
autoplay: false,
loop: false,
muted: false
};
// Gets the video type for sample markup
this.videoType = Content.mimeFromExt(Path.extname(this.file.fullPath));
this.$el = $(Mustache.render(VideoViewTemplate, {
videoUrl: _getVideoUrl(file),
videoType: this.videoType,
Strings: Strings
}));
$container.append(this.$el);
this._naturalWidth = 0;
this._naturalHeight = 0;
this.relPath = ProjectManager.makeProjectRelativeIfPossible(this.file.fullPath);
this.$videoEl = this.$el.find("video");
this.$videoWrapperEl = this.$el.find(".video-wrapper");
this.$videoMarkupEl = this.$el.find("pre");
this.$videoData = this.$el.find(".video-data-content");
this.$videoEl.one("canplay", _.bind(this._onVideoLoaded, this));
this.$videoEl.on("error", _.bind(console.error, console));
this.$el.find(".tag-options").on("change", "input", function(){
var attribute = $(this).attr("setting");
var isEnabled = $(this).is(":checked");
that.videoTagSettings[attribute] = isEnabled;
that.updateVideoTagMarkup(true);
});
this.$videoEl.find("source").attr("src", _getVideoUrl(this.file));
this.updateVideoTagMarkup();
_viewers[file.fullPath] = this;
}
// Updates the markup used in the preview & the sample markup below
VideoView.prototype.updateVideoTagMarkup = function(reload){
var videoTagAttributesString = "";
for(var k in this.videoTagSettings) {
if(this.videoTagSettings[k]) {
videoTagAttributesString = videoTagAttributesString + " " + k;
this.$videoEl.attr(k,"");
} else {
this.$videoEl.removeAttr(k);
}
}
var videoTagMarkup =
'<video'+ videoTagAttributesString +'>\n' +
' <source src="'+ _getLocalVideoUrl(this.file) + '" type="' + this.videoType + '">\n'+
'</video>';
this.$videoMarkupEl.text(videoTagMarkup);
// Reloads the video when one of the attributes is changed
// so that the preview reflects the changes
if(reload) {
this.$videoMarkupEl.one("animationend",function(){
$(this).removeClass("pop");
});
this.$videoMarkupEl.addClass("pop");
var videoWrapperHeight = this.$videoWrapperEl.height();
this.$videoWrapperEl.css("min-height", videoWrapperHeight);
this.$videoEl.one("canplay", _.bind(this._onVideoReloaded, this));
this.$videoEl.find("source").attr("src", _getVideoUrl(this.file));
this.$videoEl[0].load();
}
};
/**
* DocumentManger.fileNameChange handler - when an video is renamed, we must
* update the view
*
* @param {jQuery.Event} e - event
* @param {!string} oldPath - the name of the file that's changing changing
* @param {!string} newPath - the name of the file that's changing changing
* @private
*/
VideoView.prototype._onFilenameChange = function (e, oldPath, newPath) {
/*
* File objects are already updated when the event is triggered
* so we just need to see if the file has the same path as our video
*/
if (this.file.fullPath === newPath) {
this.relPath = ProjectManager.makeProjectRelativeIfPossible(newPath);
}
this.updateVideoTagMarkup(true);
};
/* Removes min-height property */
VideoView.prototype._onVideoReloaded = function (e) {
this.$videoWrapperEl.css("min-height", "auto");
};
/**
* <video>.on("canplay") handler - updates content of the video view
* initializes computed values
* installs event handlers
* @param {Event} e - event
* @private
*/
VideoView.prototype._onVideoLoaded = function (e) {
this._naturalWidth = e.target.videoWidth;
this._naturalHeight = e.target.videoHeight;
var extension = FileUtils.getFileExtension(this.file.fullPath);
var stringFormat = Strings.IMAGE_DIMENSIONS_1;
var dimensionString = StringUtils.format(stringFormat, this._naturalWidth, this._naturalHeight);
var that = this;
this.file.stat(function (err, stat) {
var sizeString = "";
if (stat.size) {
sizeString = " <span class='divider'>•</span> " + StringUtils.prettyPrintBytes(stat.size, 2);
dimensionString = dimensionString + sizeString;
}
that.$videoData.html(dimensionString);
});
// Update the page if the file is renamed
this.fileChangeHandler = _.bind(this._onFilenameChange, this);
DocumentManager.on("fileNameChange", this.fileChangeHandler);
};
/**
* View Interface functions
*/
/*
* Retrieves the file object for this view
* return {!File} the file object for this view
*/
VideoView.prototype.getFile = function () {
return this.file;
};
/*
* Updates the layout of the view
*/
VideoView.prototype.updateLayout = function () {
return;
};
/*
* Destroys the view
*/
VideoView.prototype.destroy = function () {
delete _viewers[this.file.fullPath];
DocumentManager.off("fileNameChange", this.fileChangeHandler);
this.$el.remove();
};
/*
* Refreshes the video preview with what's on disk
*/
VideoView.prototype.refresh = function () {
// Update the DOM node with the src URL
this.$videoEl.find("source").attr("src", _getVideoUrl(this.file));
};
/*
* Creates a video view object and adds it to the specified pane
* @param {!File} file - the file to create an video of
* @param {!Pane} pane - the pane in which to host the view
* @return {jQuery.Promise}
*/
function _createVideoView(file, pane) {
var view = pane.getViewForPath(file.fullPath);
if (view) {
pane.showView(view);
} else {
view = new VideoView(file, pane.$content);
pane.addView(view, true);
}
return new $.Deferred().resolve(file).promise();
}
/**
* Handles file system change events so we can refresh
* video viewers for the files that changed on disk due to external editors
* @param {jQuery.event} event - event object
* @param {?File} file - file object that changed
* @param {Array.<FileSystemEntry>=} added If entry is a Directory, contains zero or more added children
* @param {Array.<FileSystemEntry>=} removed If entry is a Directory, contains zero or more removed children
*/
function _handleFileSystemChange(event, entry, added, removed) {
// this may have been called because files were added
// or removed to the file system. We don't care about those
if (!entry || entry.isDirectory) {
return;
}
// Look for a viewer for the changed file
var viewer = _viewers[entry.fullPath];
// viewer found, call its refresh method
if (viewer) {
viewer.refresh();
}
}
/*
* Install an event listener to receive all file system change events
* so we can refresh the view when changes are made to the video in an external editor
*/
FileSystem.on("change", _handleFileSystemChange);
/*
* Initialization, register our view factory
*/
MainViewFactory.registerViewFactory({
canOpenFile: function (fullPath) {
return isVideo(fullPath);
},
openFile: function (file, pane) {
return _createVideoView(file, pane);
}
});
/*
* This is for extensions that want to create a
* view factory based on ImageViewer
*/
exports.VideoView = VideoView;
});