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
|
var OKUpload = function(){
this.action = "/_services/image"
}
OKUpload.prototype.bind = function(rapper){
var uploader = this
$(".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("")
if (! url) return
Parser.parse( url, function(media){
console.log(url, media)
if (media.type == "image") {
uploader.add(url)
}
else {
uploader.addVideo(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.*')) {
continue;
}
this.upload(f)
}
}
OKUpload.prototype.upload = function(f){
var fd = new FormData()
fd.append('image', f)
var request = $.ajax({
url: this.action,
type: "post",
data: fd,
dataType: "json",
processData: false,
contentType: false,
})
request.done(this.success.bind(this))
}
OKUpload.prototype.success = function(data){
if (data.error) {
console.log(data.error)
return
}
var url = data[0].extra.Location
this.add(url)
}
OKUpload.prototype.add = function(media){
console.log(media)
}
OKUpload.prototype.addVideo = 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)
}
}
|