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