lib.resources.js
1.9 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
lib.resources=new function(){
this.registry={};
this.resourceNames={};
this.getUrl=function(url,evtName,boundTo,cacheTimeout){
var rid=lib.utils.SHA1(url);
if(this.registry[rid]!=undefined){
var resource=this.registry[rid];
this._fireDataEvent(evtName,resource);
}else{
$.get(url,{},(function(id,evtName){
return function(data,textStatus){
events.trigger('LIB.RESOURCES:Resource_Fetched',{data:data,status:textStatus});
lib.resources._setResource(id,data,evtName);
}
})(rid,evtName),'text');
}
}
this._fireDataEvent=function(evtName,resource){
if(typeof(evtName)=='function'){
evtName(resource.data);
}else{
events.trigger(evtName,{data:resource.data});
}
}
this._setResource=function(id,data,evtName){
if(this.registry[id]==undefined)this.registry[id]={};
data=(new DOMParser()).parseFromString(data, "text/xml");
this.registry[id].data=data;
this._fireDataEvent(evtName,this.registry[id]);
}
this.resourceLoaded=function(url){
var rid=lib.utils.SHA1(url);
return this.registry[rid]!=undefined;
}
this.getResourceFromUrl=function(url){
var rid=lib.utils.SHA1(url);
if(this.registry[rid]!=undefined)return this.registry[rid];
return undefined
}
this.getResource=function(resourceName){
if(this.resourceNames[resourceName]!=undefined)return this.getUrl(this.resourceNames[resourceName]);
return null;
}
this.setResourceUrl=function(resourceName,url){
this.resourceNames[resourceName]=url;
}
this.getResourceUrl=function(resourceName){
if(this.resourceNames[resourceName]!=undefined)return this.resourceNames[resourceName];
return null;
}
this.clearResourceCache=function(id){
if(id!=undefined){
if(this.registry[id]!=undefined)delete(this.registry[id]);
events.trigger('LIB.RESOURCES:Resource_Cache_Cleared['+id+']');
}else{
this.registry={};
events.trigger('LIB.RESOURCES:Resource_Cache_Cleared[ALL]');
}
}
}