Merge branch 'MOODLE_23_STABLE' into install_23_STABLE
[moodle.git] / files / module.js
CommitLineData
fb2fb46e
DC
1// File Tree Viewer
2// Author: Dongsheng Cai <dongsheng@moodle.com>
3M.core_filetree = {
4 y3: null,
64f93798 5 api: M.cfg.wwwroot+'/files/filebrowser_ajax.php',
fb2fb46e
DC
6 request: function(url, node, cb) {
7 var api = this.api + '?action=getfiletree';
8 var params = [];
4e0969f0
DC
9 params['contextid'] = this.get_param(url, 'contextid', -1);
10 params['component'] = this.get_param(url, 'component', null);
11 params['filearea'] = this.get_param(url, 'filearea', null);
12 params['itemid'] = this.get_param(url, 'itemid', -1);
13 params['filepath'] = this.get_param(url, 'filepath', null);
14 params['filename'] = this.get_param(url, 'filename', null);
fb2fb46e
DC
15 var scope = this;
16 params['sesskey']=M.cfg.sesskey;
17 var cfg = {
18 method: 'POST',
19 on: {
20 complete: function(id,o,p) {
21 try {
22 var data = this.y3.JSON.parse(o.responseText);
23 } catch(e) {
24 alert(e.toString());
25 return;
26 }
fb2fb46e
DC
27 if (data && data.length==0) {
28 node.isLeaf = true;
29 } else {
30 for (i in data) {
12fcd26d 31 var tmp = new YAHOO.widget.HTMLNode('<div>'+data[i].icon+'&nbsp;<a href="'+data[i].url+'">'+data[i].filename+'</a></div>', node, false);
fb2fb46e
DC
32 if (data[i].isdir) {
33 tmp.isLeaf = false;
3ac06cc2 34 tmp.isDir = true;
fb2fb46e
DC
35 } else {
36 tmp.isLeaf = true;
3ac06cc2 37 tmp.isFile = true;
fb2fb46e
DC
38 }
39 }
40 }
41 cb();
42 }
43 },
44 arguments: {
45 scope: scope
46 },
47 headers: {
11c17d83 48 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
fb2fb46e
DC
49 },
50 data: build_querystring(params),
51 context: this
52 };
53 this.y3.io(api, cfg);
54 },
4e0969f0 55 init : function(Y){
fb2fb46e
DC
56 var tree = new YAHOO.widget.TreeView('course-file-tree-view');
57 tree.setDynamicLoad(this.dynload);
58 tree.subscribe("clickEvent", this.onclick);
59 var root = tree.getRoot();
60 var children = root.children;
61 for (i in children) {
62 if (children[i].className == 'file-tree-folder') {
63 children[i].isLeaf = false;
3ac06cc2 64 children[i].isDir = true;
fb2fb46e
DC
65 } else {
66 children[i].isLeaf = true;
3ac06cc2 67 children[i].isFile = true;
fb2fb46e
DC
68 }
69 }
70 tree.render();
71 this.y3 = Y;
cb9f4e27 72 },
fb2fb46e 73 dynload: function(node, oncompletecb) {
12fcd26d
DC
74 var tmp = document.createElement('p');
75 tmp.innerHTML = node.html;
76 var links = tmp.getElementsByTagName('a');
3ac06cc2
DC
77 var link = links[0].href;
78 M.core_filetree.request(link, node, oncompletecb);
fb2fb46e
DC
79 },
80 onclick: function(e) {
cb9f4e27 81 YAHOO.util.Event.preventDefault(e);
3ac06cc2
DC
82 if (e.node.isFile) {
83 var tmp = document.createElement('p');
84 tmp.innerHTML = e.node.html;
85 var links = tmp.getElementsByTagName('a');
86 var link = links[0].href;
87 window.location = link;
88 }
4e0969f0
DC
89 },
90 get_param: function(url, name, val) {
91 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
92 var regexS = "[\\?&]"+name+"=([^&#]*)";
93 var regex = new RegExp( regexS );
94 var results = regex.exec(url);
95 if( results == null ) {
96 return val;
97 } else {
98 return unescape(results[1]);
99 }
fb2fb46e 100 }
cb9f4e27 101};