a99aef27e4067ee059ec4344942313c86d540f53
[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 mynode = {
32                                 label: data[i].filename,
33                                 href: data[i].url
34                             };
35                             var tmp = new YAHOO.widget.TextNode(mynode, node, false);
36                             if (data[i].isdir) {
37                                 tmp.isLeaf = false;
38                             } else {
39                                 tmp.isLeaf = true;
40                                 tmp.target = '_blank';
41                             }
42                         }
43                     }
44                     cb();
45                 }
46             },
47             arguments: {
48                 scope: scope
49             },
50             headers: {
51                 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
52             },
53             data: build_querystring(params),
54             context: this
55         };
56         this.y3.io(api, cfg);
57     },
58     init : function(Y){
59         var tree = new YAHOO.widget.TreeView('course-file-tree-view');
60         tree.setDynamicLoad(this.dynload);
61         tree.subscribe("clickEvent", this.onclick);
62         var root = tree.getRoot();
63         var children = root.children;
64         for (i in children) {
65             if (children[i].className == 'file-tree-folder') {
66                 children[i].isLeaf = false;
67             } else {
68                 children[i].isLeaf = true;
69             }
70         }
71         tree.render();
72         this.y3 = Y;
73     }, 
74     dynload: function(node, oncompletecb) {
75         M.core_filetree.request(node.href, node, oncompletecb);
76     },
77     onclick: function(e) {
78         YAHOO.util.Event.preventDefault(e); 
79     },
80     get_param: function(url, name, val) {
81         name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
82         var regexS = "[\\?&]"+name+"=([^&#]*)";
83         var regex = new RegExp( regexS );
84         var results = regex.exec(url);
85         if( results == null ) {
86             return val;
87         } else {
88             return unescape(results[1]);
89         }
90     }
91 }