forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.js
101 lines (101 loc) · 3.67 KB
/
module.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
// File Tree Viewer
// Author: Dongsheng Cai <[email protected]>
M.core_filetree = {
y3: null,
api: M.cfg.wwwroot+'/files/filebrowser_ajax.php',
request: function(url, node, cb) {
var api = this.api + '?action=getfiletree';
var params = [];
params['contextid'] = this.get_param(url, 'contextid', -1);
params['component'] = this.get_param(url, 'component', null);
params['filearea'] = this.get_param(url, 'filearea', null);
params['itemid'] = this.get_param(url, 'itemid', -1);
params['filepath'] = this.get_param(url, 'filepath', null);
params['filename'] = this.get_param(url, 'filename', null);
var scope = this;
params['sesskey']=M.cfg.sesskey;
var cfg = {
method: 'POST',
on: {
complete: function(id,o,p) {
try {
var data = this.y3.JSON.parse(o.responseText);
} catch(e) {
alert(e.toString());
return;
}
if (data && data.length==0) {
node.isLeaf = true;
} else {
for (i in data) {
var tmp = new YAHOO.widget.HTMLNode('<div>'+data[i].icon+' <a href="'+data[i].url+'">'+data[i].filename+'</a></div>', node, false);
if (data[i].isdir) {
tmp.isLeaf = false;
tmp.isDir = true;
} else {
tmp.isLeaf = true;
tmp.isFile = true;
}
}
}
cb();
}
},
arguments: {
scope: scope
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
data: build_querystring(params),
context: this
};
this.y3.io(api, cfg);
},
init : function(Y){
var tree = new YAHOO.widget.TreeView('course-file-tree-view');
tree.setDynamicLoad(this.dynload);
tree.subscribe("clickEvent", this.onclick);
var root = tree.getRoot();
var children = root.children;
for (i in children) {
if (children[i].className == 'file-tree-folder') {
children[i].isLeaf = false;
children[i].isDir = true;
} else {
children[i].isLeaf = true;
children[i].isFile = true;
}
}
tree.render();
this.y3 = Y;
},
dynload: function(node, oncompletecb) {
var tmp = document.createElement('p');
tmp.innerHTML = node.html;
var links = tmp.getElementsByTagName('a');
var link = links[0].href;
M.core_filetree.request(link, node, oncompletecb);
},
onclick: function(e) {
YAHOO.util.Event.preventDefault(e);
if (e.node.isFile) {
var tmp = document.createElement('p');
tmp.innerHTML = e.node.html;
var links = tmp.getElementsByTagName('a');
var link = links[0].href;
window.location = link;
}
},
get_param: function(url, name, val) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec(url);
if( results == null ) {
return val;
} else {
return unescape(results[1]);
}
}
};