MDL-23308, added mimetype icons to course files tree
[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;
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: {
11c17d83 47 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
fb2fb46e
DC
48 },
49 data: build_querystring(params),
50 context: this
51 };
52 this.y3.io(api, cfg);
53 },
4e0969f0 54 init : function(Y){
fb2fb46e
DC
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) {
12fcd26d
DC
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);
fb2fb46e
DC
75 },
76 onclick: function(e) {
77 YAHOO.util.Event.preventDefault(e);
4e0969f0
DC
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 }
fb2fb46e
DC
89 }
90}