MDL-23308, added mimetype icons to course files tree
[moodle.git] / files / module.js
index 49274e1..0e764fe 100644 (file)
@@ -2,11 +2,16 @@
 // Author: Dongsheng Cai <dongsheng@moodle.com>
 M.core_filetree = {
     y3: null,
-    api: M.cfg.wwwroot+'/files/files_ajax.php',
+    api: M.cfg.wwwroot+'/files/filebrowser_ajax.php',
     request: function(url, node, cb) {
         var api = this.api + '?action=getfiletree';
         var params = [];
-        params['fileurl'] = url;
+        params['contextid'] = this.get_param(url, 'contextid', -1);
+        params['component'] = this.get_param(url, 'component', null);
+        params['filearea'] = this.get_param(url, 'filearea', null);
+        params['itemid'] = this.get_param(url, 'itemid', -1);
+        params['filepath'] = this.get_param(url, 'filepath', null);
+        params['filename'] = this.get_param(url, 'filename', null);
         var scope = this;
         params['sesskey']=M.cfg.sesskey;
         var cfg = {
@@ -23,11 +28,7 @@ M.core_filetree = {
                         node.isLeaf = true;
                     } else {
                         for (i in data) {
-                            var mynode = {
-                                label: data[i].filename,
-                                href: data[i].url
-                            };
-                            var tmp = new YAHOO.widget.TextNode(mynode, node, false);
+                            var tmp = new YAHOO.widget.HTMLNode('<div>'+data[i].icon+'&nbsp;<a href="'+data[i].url+'">'+data[i].filename+'</a></div>', node, false);
                             if (data[i].isdir) {
                                 tmp.isLeaf = false;
                             } else {
@@ -43,14 +44,14 @@ M.core_filetree = {
                 scope: scope
             },
             headers: {
-                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
+                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
             },
             data: build_querystring(params),
             context: this
         };
         this.y3.io(api, cfg);
     },
-    init : function(Y, options){
+    init : function(Y){
         var tree = new YAHOO.widget.TreeView('course-file-tree-view');
         tree.setDynamicLoad(this.dynload);
         tree.subscribe("clickEvent", this.onclick);
@@ -67,9 +68,23 @@ M.core_filetree = {
         this.y3 = Y;
     }, 
     dynload: function(node, oncompletecb) {
-        M.core_filetree.request(node.href, node, oncompletecb);
+        var tmp = document.createElement('p');
+        tmp.innerHTML = node.html;
+        var links = tmp.getElementsByTagName('a');
+        M.core_filetree.request(links[0].href, node, oncompletecb);
     },
     onclick: function(e) {
         YAHOO.util.Event.preventDefault(e); 
+    },
+    get_param: function(url, name, val) {
+        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
+        var regexS = "[\\?&]"+name+"=([^&#]*)";
+        var regex = new RegExp( regexS );
+        var results = regex.exec(url);
+        if( results == null ) {
+            return val;
+        } else {
+            return unescape(results[1]);
+        }
     }
 }