Commit e4793d89 authored by François Billioud's avatar François Billioud

change the way dependencies are loaded

parent eb08ca0a
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
/** /**
* load dependencies * load dependencies
*/ */
var ready = includeJS([//files to load var dependenceLoaded = includeJS([//files to load
"unhosted/sjcl.js", "/unhosted/sjcl.js",
"unhosted/jquery.js", "/unhosted/jquery.js",
"unhosted/base64.js" "/unhosted/base64.js"
],[//element to check before being ready ],function() {// return true only if dependencies are loaded
$ return $!==undefined && sjcl!==undefined && Base64!==undefined; // check jQuery, sjcl & Base64
], script);// function to execute then });
function script() { function script() {
...@@ -744,74 +744,58 @@ ...@@ -744,74 +744,58 @@
} }
} }
/*************************************************************************
*************************** other functions *****************************/
/** /*************************************************************************
* function used to address requests to a php file *************************** other functions *****************************/
* @param address : address of the target
* @param data : data to send to the server
* @param instruction : function to execute after receiving the answer
* @return void
*/
function request(address, data, instruction) {
$.ajax({
url: address,
type: "POST",
async: false,
dataType: "text",
data: data,
success: instruction,
error: function(type) {alert("Error "+type.status+" : fail while trying to load "+address);}
});
}
/**
* Create a tree node from data
* @param data : information found in jio.json and needed to create the storage
* @param applicant : (optional) information about the person/application needing this JIO object (allow limited access)
*/
function createStorage(data, applicant) {
switch(data.type) {
case "dav":return new JIO.DAVStorage(data, applicant);break;
case "index":return new JIO.IndexedStorage(data, applicant);break;
case "multiple":return new JIO.MultipleStorage(data, applicant);break;
case "replicate":return new JIO.ReplicateStorage(data, applicant);break;
case "encrypte":return new JIO.CryptedStorage(data,applicant);break;
//etc
default:var waitedNode = null;//create a custom storage from a js file
$.ajax({
url: data.location+"/storage-init.js",//url of the file describing the creation of the storage
type: "GET",
async: false,
dataType: "script",
success: function(script){var CustomStorage = eval(script);waitedNode = new CustomStorage(data)},
error: data.errorHandler || function(type) {alert("Error "+type.status+" : fail while trying to instanciate storage"+data.location);}
});
return waitedNode;
break;
}
}
/** /**
* delegate a function to a non-object element, or to each element of an array of non-object elements * function used to address requests to a php file
* this function is used to delete a file or a list of files for example * @param address : address of the target
* @param element : a non-object element, or an array of non-object elements * @param data : data to send to the server
* @param f : function to apply * @param instruction : function to execute after receiving the answer
*/ * @return void
function oneOrEach(element,f) { */
typeof element != "object" ? f(element) : $.each(element,function(index, fileName) {f(fileName);}) function request(address, data, instruction) {
} $.ajax({
url: address,
type: "POST",
async: false,
dataType: "text",
data: data,
success: instruction,
error: function(type) {alert("Error "+type.status+" : fail while trying to load "+address);}
});
}
/** /**
* return a shallow copy of the object parameter * Create a tree node from data
* @param object : the object to copy * @param data : information found in jio.json and needed to create the storage
* @return a shallow copy of the object * @param applicant : (optional) information about the person/application needing this JIO object (allow limited access)
*/ */
function copy(object) { function createStorage(data, applicant) {
$.extend({}, object); switch(data.type) {
case "dav":return new JIO.DAVStorage(data, applicant);break;
case "local":return new JIO.LocalStorage(data, applicant);break;
case "index":return new JIO.IndexedStorage(data, applicant);break;
case "multiple":return new JIO.MultipleStorage(data, applicant);break;
case "replicate":return new JIO.ReplicateStorage(data, applicant);break;
case "encrypt":return new JIO.CryptedStorage(data,applicant);break;
//etc
default:var waitedNode = null;//create a custom storage from a js file
$.ajax({
url: data.location+"/storage-init.js",//url of the file describing the creation of the storage
type: "GET",
async: false,
dataType: "script",
success: function(script){var CustomStorage = eval(script);waitedNode = new CustomStorage(data)},
error: data.errorHandler || function(type) {alert("Error "+type.status+" : fail while trying to instanciate storage"+data.location);}
});
return waitedNode;
break;
} }
}
window.JIO = JIO;//the name to use for the framework. Ex : JIO.initialize(...), JIO.loadDocument... window.JIO = JIO;//the name to use for the framework. Ex : JIO.initialize(...), JIO.loadDocument...
} }
...@@ -823,16 +807,14 @@ ...@@ -823,16 +807,14 @@
/** /**
* include js files * include js files
* @param url : path or array of paths of the js file(s) * @param url : path or array of paths of the js file(s)
* @param flag : (optional) array of elements allowing to know if dependencies are ready * @param ready : (optional) array of elements allowing to know if dependencies are ready
* @param instruction : (optional) instruction to execute when dependencies are loaded * @param instruction : (optional) instruction to execute when dependencies are loaded
* @return a ready function which returns true only if the dependencies are ready * @return the ready function
* @example : includeJS("jquery.js",[$]); * @example : includeJS("jquery.js",[$]);
*/ */
function includeJS(url,flag, instruction) { function includeJS(url,ready, instruction) {
typeof url != "object" ? includeElement(url) : onEach(url); //path = findURL("jio.js");
var ready = function() { typeof url != "object" ? includeElement(url) : onEach(url,includeElement);
return oneOrEach(flag.slice(),function(element){return typeof element != "undefined"});
}
if(instruction) { if(instruction) {
(function waitUntilLoaded() {!ready() ? setTimeout(waitUntilLoaded,50) : instruction();})() (function waitUntilLoaded() {!ready() ? setTimeout(waitUntilLoaded,50) : instruction();})()
} }
...@@ -841,15 +823,26 @@ ...@@ -841,15 +823,26 @@
function includeElement(element) {//include a js file function includeElement(element) {//include a js file
var head = window.document.getElementsByTagName('head')[0]; var head = window.document.getElementsByTagName('head')[0];
var script = window.document.createElement('script'); var script = window.document.createElement('script');
script.setAttribute('src', url); script.setAttribute('src', element);
script.setAttribute('type', 'text/javascript'); script.setAttribute('type', 'text/javascript');
head.appendChild(script); head.appendChild(script);
} }
function onEach(array,f) { function onEach(array,f) {
var head = array.pop(); var head = array.pop();
if(array.length==0) {return true} if(head===undefined) {return true}
return f(head) && oneOrEach(array); var rep1 = f(head);
var rep2 = onEach(array,f);
return rep1 && rep2;
}
function findURL(fileName) {
var elements = window.document.getElementsByName("script");
var jioRegExp = new RegExp("^((.*)/)?"+fileName+"$");
for(var e in elements) {
if(jioRegExp.test(e.src)) {return e.src.replace(jioRegExp, "$1")}
}
return "";
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment