-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathotinput.js
147 lines (143 loc) · 5.45 KB
/
otinput.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
/*! otinput v1.0.0 */
(function(){
'use strict';
oTinput.prototype.getSupportedFormats = function(){
var potentialFormatsAudio = ['mp3', 'ogg', 'webm', 'wav'];
var potentialFormatsVideo = ['mp4', 'ogg', 'webm'];
var isFormatSupported = this.isFormatSupported || oTinput.isFormatSupported;
var audio = $.map( potentialFormatsAudio, function( format, i ) {
if (isFormatSupported(format)){
return format;
}
});
var video = $.map( potentialFormatsVideo, function( format, i ) {
if (isFormatSupported(format)){
return format;
}
});
return {
audio: audio,
video: video
};
};
oTinput.prototype.isFormatSupported = function( format ){
var a;
if (typeof format !== 'string') {
var fileType = format.type.split("/")[0];
a = document.createElement(fileType);
return !!(a.canPlayType && a.canPlayType(format.type).replace(/no/, ''));
}
a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/'+format+';').replace(/no/, ''));
};
oTinput.getSupportedFormats = oTinput.prototype.getSupportedFormats;
oTinput.isFormatSupported = oTinput.prototype.isFormatSupported;
oTinput.prototype.parseYoutubeURL = function(url){
if (url.match) {
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[2].length===11){
return match[2];
}
}
return false;
};
oTinput.parseYoutubeURL = oTinput.prototype.parseYoutubeURL;
function oTinput(config){
var that = this;
this._text = config.text || {};
this._onFileChange = config.onFileChange || function(){};
this._onFileError = config.onFileError || function(){};
this._onURLSubmit = config.onURLSubmit || function(){};
this._onURLError = config.onURLError || function(){};
this._dragover = config.onDragover || function(){};
this._dragleave = config.onDragleave || function(){};
this.element = this._setupElement(config.element);
this._setupMouseEvents();
$(this.element).find('input[type="file"]').change(function(){
that._reactToFile(this);
});
$(this.element).find('.ext-input-field input').on('submit',function(){
that._reactToURL( $(this).val() );
}).keypress(function(e){
if (e.which === 13) {
that._reactToURL( $(this).val() );
return false;
}
});
}
window.oTinput = oTinput;
oTinput.prototype._setupElement = function(element){
var that = this;
if (typeof element === 'undefined') {
throw('must specify container element');
}
var buttonText = this._text.button || 'Choose audio (or video) file';
var button = '<button class="btn-file-input" style="width: 100%;">'+buttonText+'</button>';
var fileInputStyle = [
'position: absolute',
'top: 0',
'left: 0',
'opacity: 0',
'width: 100%'
].join(';');
var fileInput = '<input type="file" accept="audio/*, video/*" style="'+fileInputStyle+'">';
var wrapperStyle = 'position: relative; overflow: hidden;';
var wrapper = '<div class="file-input-wrapper" style="'+wrapperStyle+'">'+button+fileInput+'</div>';
var altButtonText = this._text.altButton || 'Enter file URL';
var altButton = '<button class="alt-input-button">'+altButtonText+'</button>';
var urlInputText = this._text.altInputText || 'Enter URL of audio or video file, or YouTube video:';
var urlInputClose = this._text.closeAlt || 'close';
var urlInput = '<div class="ext-input-field" style="display: none;"><div class="close-ext-input">'+urlInputClose+'</div><label>'+urlInputText+'<input type="text"></label><div class="ext-input-warning"></div></div>';
$(element).html( wrapper + altButton + urlInput );
return $(element)[0];
};
oTinput.prototype._setupMouseEvents = function(){
var that = this;
var element = this.element;
var buttonEl = $(element).find('.file-input-wrapper')[0];
buttonEl.addEventListener('dragover', function(){
that._dragover();
}, false);
buttonEl.addEventListener('dragleave', function(){
that._dragleave();
}, false);
$(element).find('.alt-input-button').click(function(){
that.showURLInput();
});
$(element).find('.close-ext-input').click(function(){
that.showFileInput();
});
};
oTinput.prototype._reactToFile = function(input){
var file = input.files[0];
if ( this.isFormatSupported(file) ) {
this._onFileChange( file );
} else {
var err = new Error('Filetype '+file.type+' not supported by this browser');
this._onFileError(err, file);
}
};
oTinput.prototype._reactToURL = function(url){
var input = url.replace(/\s/g,'');
if (this.parseYoutubeURL(input)){
return this._onURLSubmit( input );
}
var formatArr = input.split('.');
var format = formatArr[formatArr.length-1];
if ( this.isFormatSupported(format) ) {
this._onURLSubmit( input );
} else {
var err = new Error('Filetype '+format+' not supported by this browser');
this._onURLError(err, url);
}
};
oTinput.prototype.showURLInput = function(){
$(this.element).find('.ext-input-field').show().find('input').focus();
$(this.element).addClass('ext-input-active');
};
oTinput.prototype.showFileInput = function(){
$(this.element).find('.ext-input-field').hide();
$(this.element).removeClass('ext-input-active');
};
}());