Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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-host 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(); | ||
})(); |