-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg3client.js
More file actions
76 lines (66 loc) · 1.88 KB
/
g3client.js
File metadata and controls
76 lines (66 loc) · 1.88 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
/**
* Gallery 3 JS Client
* (c) Ferimer, servicios informaticos, 2013 - All rights reserved
* License: GNU Affero V3 (see LICENSE file)
* Fernando Rodríguez Sela <fernando@ferimer.es>
*/
function g3client(url, apikey) {
this.gallerydata = {
g3url: url,
g3apikey: apikey
};
this.onalbum = function() {};
this.onphoto = function() {};
this.onmovie = function() {};
function _g3send(method, url, gobj, onresource, isImage) {
var req = new XMLHttpRequest();
function reqG3Listener () {
if (onresource && typeof(onresource) == 'function') {
return onresource(req.response);
}
if (!req.response.entity || !req.response.entity.type) {
return;
}
switch(req.response.entity.type) {
case 'album':
gobj.onalbum(req.response);
break;
case 'photo':
gobj.onphoto(req.response);
break;
case 'movie':
gobj.onmovie(req.response);
break;
default:
console.log('Not recognized');
}
};
req.onload = reqG3Listener;
req.open(method, url, true);
req.setRequestHeader('X-Gallery-Request-Key', gobj.gallerydata.g3apikey);
req.setRequestHeader('X-Gallery-Request-Method', method);
if (!onresource || !isImage) {
req.responseType = 'json';
} else {
req.responseType = 'blob';
}
req.send();
}
this.g3GET = function g3getGET(url) {
_g3send("get", url, this);
}
this.g3RESOURCE = function g3getRESOURCE(url, callback, isImage) {
_g3send("get", url, this, callback, isImage);
}
}
g3client.prototype = {
getItem: function getItem(itemIdOrURL = 1) {
if (isNaN(itemIdOrURL)) {
this.g3GET(itemIdOrURL);
}
this.g3GET(this.gallerydata.g3url + '/rest/item/' + itemIdOrURL);
},
getResource: function getResource(url, callback, isImage = true) {
this.g3RESOURCE(url, callback, isImage);
}
};