Skip to content

Commit

Permalink
Adding OpenPhoto.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai committed Dec 30, 2011
1 parent c6ae2f0 commit 5924457
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions html/js/OpenPhoto.js
@@ -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();
})();

0 comments on commit 5924457

Please sign in to comment.