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
|
var OKUpload = function(){
this.action = this.imageAction = "/_services/s3/image"
this.videoAction = "/_services/s3/video"
this.audioAction = "/_services/s3/audio"
}
OKUpload.prototype.bind = function(rapper){
var uploader = this
this.rapper = rapper
this.$progress = $("<div class='progress'>")
this.xhrCount = 0
this.loadCount = 0
$(this.rapper).append(this.$progress)
$(".add-image-button input", rapper).change( uploader.handleFileSelect.bind(uploader) )
$(".add-url", rapper).on("keydown blur", pressEnter( function(e){
var url = $(this).val()
$(this).val("")
uploader.parse(url)
}))
}
OKUpload.prototype.parse = function(url){
if (! url) return
var uploader = this
Parser.parse( url, function(media){
console.log(url, media)
if (! media) {
alert("Not a valid link")
}
else if (media.type == "image") {
uploader.add(media)
}
else {
uploader.addMedia(media)
}
})
}
OKUpload.prototype.handleFileSelect = function(e) {
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer ? e.dataTransfer.files : e.target.files;
for (var i = 0, f; f = files[i]; i++) {
if ( ! f.type.match('image.*') && ! f.type.match('video.*') ) {
continue;
}
this.upload(f)
}
}
OKUpload.prototype.upload = function(f){
var field, action
if ( f.type.match('video.*') ) {
field = 'video'
action = this.videoAction
}
else if ( f.type.match('audio.*') ) {
field = 'audio'
action = this.audioAction
}
else {
field = 'image'
action = this.imageAction || this.action
}
this.xhrCount += 1
this.$progress.addClass("loading")
var $loader = $("<div class='xhr'>")
var $loading_bar = $("<div>")
$loading_bar.css("width", "0%")
$loader.append( $loading_bar )
this.$progress.append($loader)
var fd = new FormData()
fd.append(field, f)
var request = new XMLHttpRequest()
request.open("POST", action, true)
request.addEventListener("progress", updateProgress.bind(this));
request.addEventListener("load", transferComplete.bind(this));
request.addEventListener("error", transferError.bind(this));
request.addEventListener("abort", transferAbort.bind(this));
function updateProgress (e) {
if (e.lengthComputable) {
var percentComplete = Math.round( 100 * e.loaded / e.total )
$loading_bar.css("width", percentComplete + "%")
}
}
function transferComplete (data) {
this.loadCount += 1
this.hideUploadBars()
if (request.readyState == 4 && request.status == 200) {
var responseData
try {
responseData = JSON.parse( request.responseText )
this.success( responseData )
}
catch (e) {
console.log(request.responseText)
console.log("ERROR PARSING JSON")
}
}
console.log(arguments, request)
}
function transferError (data) {
this.loadCount += 1
this.hideUploadBars()
console.log(arguments)
}
function transferAbort (data) {
this.loadCount += 1
this.hideUploadBars()
console.log(arguments)
}
request.send(fd)
/*
var request = $.ajax({
url: this.action,
type: "post",
data: fd,
dataType: "json",
processData: false,
contentType: false,
})
request.done(this.success.bind(this))
*/
}
OKUpload.prototype.hideUploadBars = function () {
if (this.xhrCount == this.loadCount) {
this.$progress.removeClass("loading")
setTimeout(function(){
this.$progress.empty()
}.bind(this), 300)
}
}
OKUpload.prototype.success = function(data){
if (data.error) {
console.log(data.error)
return
}
var url = data[0].extra.Location.replace(/%2F/, '\/')
this.parse(url)
}
OKUpload.prototype.add = function(media){
console.log(media)
}
OKUpload.prototype.addMedia = function(media){
console.log(media)
}
OKUpload.prototype.addAudio = function(media){
console.log(media)
}
OKUpload.prototype.error = function(error){
throw error
}
function pressEnter(fn){
return function(e){
if (e.keyCode && e.keyCode !== 13) return
e.preventDefault()
fn.apply(this)
}
}
|