Repository navigation
Expand file tree
/
Copy pathFileviewer.js
More file actions
75 lines (69 loc) · 1.57 KB
/
Copy pathFileviewer.js
File metadata and controls
75 lines (69 loc) · 1.57 KB
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
function Fileviewer(i) {
this.o = {
apiKey:false,
doc:false,
target:false
}
this.opts = $.extend({}, this.o, i);
this.baseUrl = 'https://viewer.filelabel.co/?';
this.url = 'https://viewer.filelabel.co/api/?';
this.fileViewerToken = false;
this.init = function(){
var that = this;
this.auth = this.auth(function(){
that.load(this.fileViewerToken);
});
}
this.auth = function(callback){
if(this.fileViewerToken) {
return this.fileViewerToken;
} else {
this.getResponse({
'apiKey':this.opts.apiKey,
'action':'auth'
}, function(response){
this.fileViewerToken = response.output.accessToken;
$('body').trigger('fileviewerAuth');
callback();
});
}
}
this.getResponse = function(params, callback) {
var url = this.url+$.param(params);
var that = this;
$.getJSON(url, function(response){
if(that.processResponse(response)){
if(typeof callback !== 'undefined') {
callback(response);
}
}
});
}
this.load = function(token){
if(token) {
if(this.opts.doc) {
var url = this.baseUrl+'accessToken='+token+'&doc='+encodeURIComponent(this.opts.doc);
if(this.opts.target) {
$(this.opts.target).html('<iframe src="'+url+'"></iframe>');
} else {
window.location.href = url;
}
}
}
}
this.processResponse = function(response) {
var error = '';
if(response.hasOwnProperty('error')) {
$.each(response.error, function(key, value) {
error += value+"\n";
});
}
if(error != '') {
alert(error);
return false;
} else {
return true;
}
}
this.init();
}