Merge branch 'MOODLE_23_STABLE' into install_23_STABLE
[moodle.git] / files / module.js
1 // File Tree Viewer
2 // Author: Dongsheng Cai <dongsheng@moodle.com>
3 M.core_filetree = {
4     y3: null,
5     api: M.cfg.wwwroot+'/files/filebrowser_ajax.php',
6     request: function(url, node, cb) {
7         var api = this.api + '?action=getfiletree';
8         var params = [];
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);
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                     }
27                     if (data && data.length==0) {
28                         node.isLeaf = true;
29                     } else {
30                         for (i in data) {
31                             var tmp = new YAHOO.widget.HTMLNode('<div>'+data[i].icon+'&nbsp;<a href="'+data[i].url+'">'+data[i].filename+'</a></div>', node, false);
32                             if (data[i].isdir) {
33                                 tmp.isLeaf = false;
34                                 tmp.isDir = true;
35                             } else {
36                                 tmp.isLeaf = true;
37                                 tmp.isFile = true;
38                             }
39                         }
40                     }
41                     cb();
42                 }
43             },
44             arguments: {
45                 scope: scope
46             },
47             headers: {
48                 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
49             },
50             data: build_querystring(params),
51             context: this
52         };
53         this.y3.io(api, cfg);
54     },
55     init : function(Y){
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;
64                 children[i].isDir = true;
65             } else {
66                 children[i].isLeaf = true;
67                 children[i].isFile = true;
68             }
69         }
70         tree.render();
71         this.y3 = Y;
72     },
73     dynload: function(node, oncompletecb) {
74         var tmp = document.createElement('p');
75         tmp.innerHTML = node.html;
76         var links = tmp.getElementsByTagName('a');
77         var link = links[0].href;
78         M.core_filetree.request(link, node, oncompletecb);
79     },
80     onclick: function(e) {
81         YAHOO.util.Event.preventDefault(e);
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         }
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         }
100     }
101 };