-
Notifications
You must be signed in to change notification settings - Fork 7
/
OpenPhoto.js
54 lines (49 loc) · 1.61 KB
/
OpenPhoto.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
/*
* This is not producion quality code :)
* Author: Jaisen Mathai <jaisen@jmathai.com>
*/
(function() {
if ( typeof(OpenPhoto) === "undefined") {
OpenPhoto = {};
}
// GLOBAL
functions = [];
// PRIVATE
var log = function(msg) { if(typeof(console) !== 'undefined') { console.log(msg); } };
var host = (function() {
var scripts = document.getElementsByTagName('script'),
script;
for(var i=0; i<scripts.length; i++) {
script = scripts[i];
if(script.src !== "undefined" && script.src.indexOf('OpenPhoto.js') !== -1) {
return script.getAttribute("data-site") || log("No data-site attribute on the script tag");
}
}
})();
var generateUrl = function(endpoint) {
return host+endpoint;
};
function Api() {
this.load = function(endpoint) {
if(arguments.length > 0) {
var scriptId = 'OpenPhotoScriptId' + parseInt(Math.random()*100000);
var callback = arguments[1] || null;
var cb;
if(typeof callback === "function") {
cb = "OpenPhoto"+parseInt(Math.random()*100000);
functions[cb] = callback;
callback = "functions['"+cb+"']";
}
var url = generateUrl(endpoint);
var head = document.getElementsByTagName('head').item(0);
var scriptTag = document.getElementById(scriptId);
script = document.createElement('SCRIPT');
script.src = url + (callback !== null ? '&callback='+callback : '');
script.type = 'text/javascript';
script.id = scriptId;
head.appendChild(script);
}
};
}
OpenPhoto.Api = new Api();
})();