-
Notifications
You must be signed in to change notification settings - Fork 1
/
icgc.js
122 lines (111 loc) · 4.09 KB
/
icgc.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
icgc={};
icgc.hello = function(){
console.log('icgc :-)');
return false
}
icgc.uid=function(x){
if(!x){x='UID'};
return x+Math.random().toString().slice(2)
}
icgc.removeEl=function(id){
document.getElementById(id).parentNode.removeChild(document.getElementById(id))
}
icgc.getScript = function (url,cb,er){ // load script / JSON
var s = document.createElement('script');
s.src=url; // console.log("URL: "+url);
s.id = this.uid();
if(!!cb){s.onload=cb}
if(!!er){s.onerror=er}
document.head.appendChild(s);
setTimeout('document.head.removeChild(document.getElementById("'+s.id+'"));',30000); // is the waiting still needed ?
return s.id;
}
icgc.xhr=function(url,cb,method,data){
var xhr = new XMLHttpRequest();
if(!method){method="GET"}
if(!cb){cb=function(x){console.log(x.target.response)}}
xhr.open(method,url);//,data); // make sure data is indeed the 3rd input argument
xhr.onload=cb;
xhr.send();
return xhr;
}
icgc.get = function(x,fun){ // submit command path to icgc WebAPI and process it through callback function fun
if(!x){x={path:'releases'}};
var path = x.path;
delete x.path;
if(!fun){fun=function(x){console.log(x)}};
if(!icgc.get.callbacks){icgc.get.callbacks={}};
if(!icgc.get.cache){icgc.get.cache={}};
var uid = this.uid('get');
var Qparms="&"+icgc.parms(x);
this.get.callbacks[uid]={ // this can be used as a cache to avoid repeating calls
path:path,
fun:function(x){icgc.get.cache[path+Qparms]=x;return fun(x)},
t:Date.now()
}
if(!this.get.cache[path+Qparms]){ // this is not in the cache already
//var url='https://script.google.com/macros/s/AKfycbxjJBbr6dXXJGCLsJ-KZ_NacYsaNY-EsQv5HUMYvI_Fq13DNblq/exec?path='+path+'&callback=icgc.get.callbacks.'+uid+'.fun'+Qparms;
var url='https://dcc.icgc.org/api/v1/'+path+'?'+Qparms.slice(1);
console.log(url+" ...");
this.xhr(url,function(x){console.log("... done");fun(JSON.parse(x.target.response))});
//this.getScript(url);
} else {
icgc.get.callbacks[uid].fun(this.get.cache[path+Qparms]);
}
// proxy code at https://script.google.com/a/macros/mathbiol.org/d/17o5B1sXjmUEWRHG_6vHQhmz3qTMPCgpOvlX1kNvDQCkVcrH5ANsi2NrY/edit
return uid;
}
icgc.parms = function(x){ // converts JSON formated parameters into URL call query arguments
var y = '';
for (var f in x){
if(typeof(x[f])=="object"){x[f]=JSON.stringify(x[f])};
y+=f+'='+x[f]+'&';
}
return encodeURI(y.slice(0,y.length-1));
}
icgc.getUrl=function(x){ // generates the URL to ICGC's service, the same used by the Google AppScript proxy
if(!x){x={path:'releases'}};
var path = x.path;
delete x.path;
queryString = icgc.parms(x);
return "https://dcc.icgc.org/api/v1/"+path+"?"+queryString;
}
icgc.openUrl = function(url,op){
switch(op){
case undefined:
break; // move on
case 1: // open in a new window
window.open(url);
break;
case 2: // open in new iframe
4
break;
default: // treating op as the parent DOM element
if(typeof(op)=="object"){var parentEl = op}
else { // op is the id of the parent DOM element
var parentEl = document.getElementById(op);
if(!parentEl){ // if not found create it and drop it in the body
var parentEl = document.createElement('div');
document.body.appendChild(parentEl);
}
}
var div = document.createElement('div');
div.id=icgc.uid();
//div.innerHTML='<a href="'+url+'" target=_blank>Open</a> in new window; <button style="color:red" onclick="icgc.removeEl(\''+div.id+'\')">Remove</button>';
div.innerHTML='<button onclick="window.open(\''+url+'\');" style="color:blue">Open</button> in new window; <button style="color:red" onclick="icgc.removeEl(\''+div.id+'\')">Remove</button>';
parentEl.appendChild(div);
var ifr = document.createElement('iframe');
div.appendChild(ifr);ifr.width="100%";ifr.height="100%";
ifr.src=url;
}
return false;
}
icgc.portal=function(x,id,s){ // open UI to icgc portal with filter, note s which can be "m" or "g" depending on the target representation being for genes or mutation
if(!s){var s = ""} else { s="/"+s }
if(!id){id=this.uid('portal')}
var url = "https://dcc.icgc.org/search"+s+"?"+icgc.parms(x);
if(!!id){
icgc.openUrl(url,id);
}
return url;
}