]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-16597, added more docs, clean up code and added maxbytes support"
authorDongsheng Cai <unoter@gmail.com>
Mon, 7 Dec 2009 05:01:09 +0000 (05:01 +0000)
committerDongsheng Cai <unoter@gmail.com>
Mon, 7 Dec 2009 05:01:09 +0000 (05:01 +0000)
lib/form/filemanager.js
lib/form/filemanager.php
repository/repository.src.js
repository/repository_ajax.php
theme/standard/styles_layout.css

index 6dbab8ff0025821c5c88434b9bbffcec8929c4e5..77525635e8f332e482cadbc8b45ce5a3fd68b36d 100644 (file)
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This file contains javascript code used to manage files in draft area
+ *
+ * @since 2.0
+ * @package filemanager
+ * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
 /**
- * This file is part of Moodle - http://moodle.org/
- * File manager
- * @copyright  1999 onwards Dongsheng Cai <dongsheng@moodle.com>
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * Namespaces used by filemanager
  */
+YAHOO.namespace('moodle.filemanager');
 
+// three dialog box we will used later
+YAHOO.moodle.filemanager.movefile_dialog = null;
+YAHOO.moodle.filemanager.rename_dialog = null;
+YAHOO.moodle.filemanager.mkdir_dialog  = null;
+
+
+// an object used to record filemanager instances' data,
+// we use it quite often
 var fm_cfg = {};
-var fm_move_dlg = null;
-var fm_rename_dlg = null;
-var fm_mkdir_dlg  = null;
+fm_cfg.api = moodle_cfg.wwwroot + '/files/files_ajax.php';
 
 // initialize file manager
 var filemanager = (function(){
     function _filemanager() {
         this.init = function(client_id, options) {
             this.client_id = client_id;
+
+            // setup move file dialog
+            var dialog = null;
+            if (!YAHOO.moodle.filemanager.movefile_dialog) {
+                dialog = document.createElement('DIV');
+                dialog.id = 'fm-move-dlg';
+                document.body.appendChild(dialog);
+                YAHOO.moodle.filemanager.movefile_dialog = new YAHOO.widget.Dialog("fm-move-dlg", {
+                     width : "600px",
+                     fixedcenter : true,
+                     visible : false,
+                     constraintoviewport : true
+                     });
+            } else {
+                dialog = document.getElementById('fm-move-div');
+            }
+
+            dialog.innerHTML = '<div class="hd"></div><div class="bd"><div id="fm-move-div">'+mstr.repository.nopathselected+'</div><div id="fm-tree"></div></div>';
+
+            YAHOO.moodle.filemanager.movefile_dialog.render();
+            // generate filemanager html
             html_compiler(client_id, options);
         }
     }
     return _filemanager;
 })();
 
-filemanager.url = moodle_cfg.wwwroot + '/files/files_ajax.php';
-filemanager.fileicon = moodle_cfg.wwwroot + '/pix/i/settings.gif';
-
-// callback function for file picker
-function filemanager_callback(obj) {
-    refresh_filemanager(obj.filepath, fm_cfg[obj.client_id]);
-    fm_cfg[obj.client_id].currentfiles++;
 
-    if (fm_cfg[obj.client_id].currentfiles>=fm_cfg[obj.client_id].maxfiles && fm_cfg[obj.client_id].maxfiles!=-1) {
-        var btn = document.getElementById('btnadd-'+obj.client_id);
-        if (btn)
-            btn.style.display = 'none';
+/**
+ * This function will be called by filepicker once it got the file successfully
+ */
+function filemanager_callback(params) {
+    var client_id = params.client_id;
+    fm_refresh(params.filepath, fm_cfg[client_id]);
+    fm_cfg[client_id].currentfiles++;
+    console.info(fm_cfg[client_id].currentfiles);
+
+    if (fm_cfg[client_id].currentfiles>=fm_cfg[client_id].maxfiles
+            && fm_cfg[client_id].maxfiles!=-1) {
+        var addfilebutton = document.getElementById('btnadd-'+client_id);
+        if (addfilebutton) {
+            addfilebutton.style.display = 'none';
+        }
     }
 }
 
-// setup options for file picker
-function fm_launch_filepicker(el_id, options) {
+/**
+ * Setup options to launch file picker.
+ * Fired by add file button.
+ */
+function fm_launch_filepicker(target, options) {
     var picker = document.createElement('DIV');
     picker.id = 'file-picker-'+options.client_id;
     picker.className = 'file-picker';
     document.body.appendChild(picker);
-    var el=document.getElementById(el_id);
+
+    var target=document.getElementById(target);
     var params = {};
     params.env = 'filemanager';
     params.itemid = options.itemid;
     params.maxfiles = options.maxfiles;
     params.maxbytes = options.maxbytes;
     params.savepath = options.savepath;
-    params.target = el;
+    params.target = target;
+    // setup filemanager callback
     params.callback = filemanager_callback;
     var fp = open_filepicker(options.client_id, params);
     return false;
 }
 
-// create a new folder in draft area
-function mkdir(e, client_id, itemid) {
-    var mkdir_cb = {
+/**
+ * This function will create a dialog of creating new folder
+ * Fired by 'make a folder' button
+ */
+function fm_create_folder(e, client_id, itemid) {
+    // deal with ajax response
+    var mkdir_ajax_callback = {
         success: function(o) {
             var result = json_decode(o.responseText);
-            fm_mkdir_dlg.hide();
-            refresh_filemanager(result.filepath, fm_cfg[client_id]);
+            YAHOO.moodle.filemanager.mkdir_dialog.hide();
+            fm_refresh(result.filepath, fm_cfg[client_id]);
         }
     }
-    var perform = function(e) {
+    // a function used to perform an ajax request
+    var perform_ajax_mkdir = function(e) {
         var foldername = document.getElementById('fm-newname').value;
         if (!foldername) {
             return;
@@ -75,9 +138,10 @@ function mkdir(e, client_id, itemid) {
         params['sesskey'] = moodle_cfg.sesskey;
         params['filepath'] = fm_cfg[client_id].currentpath;
         var trans = YAHOO.util.Connect.asyncRequest('POST',
-            filemanager.url+'?action=mkdir', mkdir_cb, build_querystring(params));
+            fm_cfg.api+'?action=mkdir', mkdir_ajax_callback, build_querystring(params));
         YAHOO.util.Event.preventDefault(e);
     }
+    // create dialog html element
     if (!document.getElementById('fm-mkdir-dlg')) {
         var el = document.createElement('DIV');
         el.id = 'fm-mkdir-dlg';
@@ -85,7 +149,7 @@ function mkdir(e, client_id, itemid) {
         document.body.appendChild(el);
         var x = YAHOO.util.Event.getPageX(e);
         var y = YAHOO.util.Event.getPageY(e);
-        fm_mkdir_dlg = new YAHOO.widget.Dialog("fm-mkdir-dlg", {
+        YAHOO.moodle.filemanager.mkdir_dialog = new YAHOO.widget.Dialog("fm-mkdir-dlg", {
              width: "300px",
              visible: true,
              x:y,
@@ -94,14 +158,15 @@ function mkdir(e, client_id, itemid) {
              });
 
     }
-    var buttons = [ { text:mstr.moodle.ok, handler:perform, isDefault:true },
+    var buttons = [ { text:mstr.moodle.ok, handler:perform_ajax_mkdir, isDefault:true },
                       { text:mstr.moodle.cancel, handler:function(){this.cancel();}}];
 
-    fm_mkdir_dlg.cfg.queueProperty("buttons", buttons);
-    fm_mkdir_dlg.render();
-    fm_mkdir_dlg.show();
+    YAHOO.moodle.filemanager.mkdir_dialog.cfg.queueProperty("buttons", buttons);
+    YAHOO.moodle.filemanager.mkdir_dialog.render();
+    YAHOO.moodle.filemanager.mkdir_dialog.show();
 
-    var k1 = new YAHOO.util.KeyListener(document.getElementById('fm-mkdir-dlg'), {keys:13}, {fn:function(){perform();}, correctScope: true});
+    // presss 'enter' key to perform ajax request
+    var k1 = new YAHOO.util.KeyListener(document.getElementById('fm-mkdir-dlg'), {keys:13}, {fn:function(){perform_ajax_mkdir();}, correctScope: true});
     k1.enable();
 
     document.getElementById('fm-newname').value = '';
@@ -112,7 +177,9 @@ function html_compiler(client_id, options) {
     var list = options.list;
     var breadcrumb = document.getElementById('fm-path-'+client_id);
     var count = 0;
+    // build breadcrumb
     if (options.path) {
+        // empty breadcrumb
         breadcrumb.innerHTML = '';
         var count = 0;
         for(var p in options.path) {
@@ -124,40 +191,47 @@ function html_compiler(client_id, options) {
                 sep.innerHTML = ' ▶ ';
             }
             count++;
+
             var pathid  = 'fm-path-node-'+client_id;
             pathid += ('-'+count);
 
-            var el = document.createElement('A');
-            el.id = pathid;
-            el.innerHTML = options.path[p].name;
-            el.href = '###';
+            var pathnode = document.createElement('A');
+            pathnode.id = pathid;
+            pathnode.innerHTML = options.path[p].name;
+            pathnode.href = '###';
             breadcrumb.appendChild(sep);
-            breadcrumb.appendChild(el);
+            breadcrumb.appendChild(pathnode);
 
             var args = {};
             args.itemid = options.itemid;
             args.requestpath = options.path[p].path;
             args.client_id = client_id;
 
-            YAHOO.util.Event.addListener(pathid, 'click', click_breadcrumb, args);
+            YAHOO.util.Event.addListener(pathid, 'click', fm_click_breadcrumb, args);
         }
     }
-    var template = document.getElementById('fm-tmpl');
+    var template = document.getElementById('fm-template');
     var container = document.getElementById('filemanager-' + client_id);
-    var listhtml = '<ul id="draftfiles-'+client_id+'">';
+    var listhtml = '';
 
+    // folder list items
     var folder_ids = [];
+    var folder_data = {};
 
+    // normal file list items
     var file_ids   = [];
     var file_data  = {};
-    var folder_data = {};
+
+    // archives list items
+    var zip_ids    = [];
+    var zip_data = {};
+
     var html_ids = [];
     var html_data = {};
-    var zip_data = {};
+
     file_data.itemid = folder_data.itemid = zip_data.itemid = options.itemid;
     file_data.client_id = folder_data.client_id = zip_data.client_id = options.client_id;
 
-    var zip_ids    = [];
     var foldername_ids = [];
     if (list.length == 0) {
         // hide file browser and breadcrumb
@@ -170,12 +244,17 @@ function html_compiler(client_id, options) {
         container.style.display='block';
         breadcrumb.style.display='block';
     }
-    count = 0;
+
+    var count = 0;
     for(var i in list) {
         count++;
+        // the li html element
         var htmlid = 'fileitem-'+client_id+'-'+count;
+        // link to file
         var fileid = 'filename-'+client_id+'-'+count;
+        // file menu
         var action = 'action-'  +client_id+'-'+count;
+
         var html = template.innerHTML;
 
         html_ids.push(htmlid);
@@ -184,11 +263,14 @@ function html_compiler(client_id, options) {
         list[i].htmlid = htmlid;
         list[i].fileid = fileid;
         list[i].action = action;
+
         var url = "###";
+        // check main file
         var ismainfile = false;
         if (fm_cfg[client_id].mainfilename && (fm_cfg[client_id].mainfilename.toLowerCase() == list[i].fullname.toLowerCase())) {
             ismainfile = true;
         }
+
         switch (list[i].type) {
             case 'folder':
                 foldername_ids.push(fileid);
@@ -212,46 +294,38 @@ function html_compiler(client_id, options) {
             break;
         }
         var fullname = list[i].fullname;
+
+        // add green tick to main file
         if (ismainfile) {
             fullname = "<strong>"+list[i].fullname+"</strong> <img src='"+moodle_cfg.wwwroot+"/pix/i/tick_green_small.gif"+"' />";
         }
+
         html = html.replace('___fullname___', '<a href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a>');
-        html = html.replace('___action___', '<a style="display:none" href="###" id="'+action+'"><img alt="▶" src="'+filemanager.fileicon+'" /></a>');
+        html = html.replace('___action___', '<a style="display:none" href="###" id="'+action+'"><img alt="▶" src="'+moodle_cfg.wwwroot+'/pix/i/settings.gif'+'" /></a>');
         html = '<li id="'+htmlid+'">'+html+'</li>';
         listhtml += html;
     }
-    container.innerHTML = (listhtml+'</ul>');
 
-    options.client_id=client_id;
+    container.innerHTML = '<ul id="draftfiles-'+client_id+'">' + listhtml + '</ul>';
 
-    YAHOO.util.Event.addListener(file_ids,   'click', create_filemenu, file_data);
-    YAHOO.util.Event.addListener(folder_ids, 'click', create_foldermenu, folder_data);
-    YAHOO.util.Event.addListener(zip_ids,    'click', create_zipmenu, zip_data);
+    // click normal file menu
+    YAHOO.util.Event.addListener(file_ids,   'click', fm_create_filemenu, file_data);
+    // click folder menu
+    YAHOO.util.Event.addListener(folder_ids, 'click', fm_create_foldermenu, folder_data);
+    // click archievs menu
+    YAHOO.util.Event.addListener(zip_ids,    'click', fm_create_zipmenu, zip_data);
+    // when mouse moveover every menu
     YAHOO.util.Event.addListener(html_ids,   'mouseover', fm_mouseover_menu, html_data);
     YAHOO.util.Event.addListener(html_ids,   'mouseout', fm_mouseout_menu, html_data);
-
-    YAHOO.util.Event.addListener(foldername_ids,'click', click_folder, folder_data);
-}
-
-function fm_mouseover_menu(ev, args) {
-    this.style.backgroundColor = '#0066EE';
-    var menu = args[this.id];
-    menu = document.getElementById(menu);
-    menu.style.display = 'inline';
+    // click folder name
+    YAHOO.util.Event.addListener(foldername_ids,'click', fm_click_folder, folder_data);
 }
 
-function fm_mouseout_menu(ev, args) {
-    this.style.backgroundColor = 'transparent';
-    var menu = args[this.id];
-    menu = document.getElementById(menu);
-    menu.style.display = 'none';
-}
-
-function click_breadcrumb(ev, args) {
+function fm_refresh(path, args) {
     var params = [];
     params['itemid'] = args.itemid;
+    params['filepath'] = path;
     params['sesskey'] = moodle_cfg.sesskey;
-    params['filepath'] = args.requestpath;
     this.cb = {
         success: function(o) {
             var data = json_decode(o.responseText);
@@ -264,22 +338,33 @@ function click_breadcrumb(ev, args) {
     this.cb.options = args;
     this.cb.client_id = args.client_id;
 
-    fm_cfg[args.client_id].currentpath = args.requestpath;
+    fm_cfg[args.client_id].currentpath = params['filepath'];
     fm_loading('filemanager-'+args.client_id, 'fm-prgressbar');
     var trans = YAHOO.util.Connect.asyncRequest('POST',
-        filemanager.url+'?action=list', this.cb, build_querystring(params));
+        fm_cfg.api+'?action=list', this.cb, build_querystring(params));
 }
 
-function click_folder(ev, args) {
-    var file = args[this.id];
-    refresh_filemanager(file.filepath, args);
+// display menu when mouse over
+function fm_mouseover_menu(ev, args) {
+    this.style.backgroundColor = '#0066EE';
+    var menu = args[this.id];
+    menu = document.getElementById(menu);
+    menu.style.display = 'inline';
+}
+
+// hide menu when mouse over
+function fm_mouseout_menu(ev, args) {
+    this.style.backgroundColor = 'transparent';
+    var menu = args[this.id];
+    menu = document.getElementById(menu);
+    menu.style.display = 'none';
 }
 
-function refresh_filemanager(path, args) {
+function fm_click_breadcrumb(ev, args) {
     var params = [];
     params['itemid'] = args.itemid;
-    params['filepath'] = path;
     params['sesskey'] = moodle_cfg.sesskey;
+    params['filepath'] = args.requestpath;
     this.cb = {
         success: function(o) {
             var data = json_decode(o.responseText);
@@ -292,20 +377,26 @@ function refresh_filemanager(path, args) {
     this.cb.options = args;
     this.cb.client_id = args.client_id;
 
-    fm_cfg[args.client_id].currentpath = params['filepath'];
+    fm_cfg[args.client_id].currentpath = args.requestpath;
     fm_loading('filemanager-'+args.client_id, 'fm-prgressbar');
     var trans = YAHOO.util.Connect.asyncRequest('POST',
-        filemanager.url+'?action=list', this.cb, build_querystring(params));
+        fm_cfg.api+'?action=list', this.cb, build_querystring(params));
 }
 
-function create_foldermenu(e, data) {
+function fm_click_folder(ev, args) {
+    var file = args[this.id];
+    fm_refresh(file.filepath, args);
+}
+
+function fm_create_foldermenu(e, data) {
     var file = data[this.id];
+    // an extra menu item for folder to zip it
     this.zip = function(type, ev, obj) {
         this.cb = {
             success: function(o) {
                  var result = json_decode(o.responseText);
                  if (result) {
-                     refresh_filemanager(result.filepath, fm_cfg[this.client_id]);
+                     fm_refresh(result.filepath, fm_cfg[this.client_id]);
                  }
             }
         }
@@ -318,32 +409,32 @@ function create_foldermenu(e, data) {
         params['sesskey'] = moodle_cfg.sesskey;
         fm_loading('filemanager-'+obj.client_id, 'fm-prgressbar');
         var trans = YAHOO.util.Connect.asyncRequest('POST',
-            filemanager.url+'?action=zip', this.cb, build_querystring(params));
+            fm_cfg.api+'?action=zip', this.cb, build_querystring(params));
     }
     this.zip.file = file;
     var menuitems = [
         {text: mstr.editor.zip, onclick: {fn: this.zip, obj: data, scope: this.zip}},
         ];
-    create_menu(e, 'foldermenu', menuitems, file, data);
+    fm_create_menu(e, 'foldermenu', menuitems, file, data);
 }
 
-function create_filemenu(e, data) {
+function fm_create_filemenu(e, data) {
     var file = data[this.id];
 
     var menuitems = [
         {text: mstr.moodle.download, url:file.url}
         ];
-    create_menu(e, 'filemenu', menuitems, file, data);
+    fm_create_menu(e, 'filemenu', menuitems, file, data);
 }
 
-function create_zipmenu(e, data) {
+function fm_create_zipmenu(e, data) {
     var file = data[this.id];
     this.unzip = function(type, ev, obj) {
         this.cb = {
             success:function(o) {
                 var result = json_decode(o.responseText);
                 if (result) {
-                    refresh_filemanager(result.filepath, fm_cfg[this.client_id]);
+                    fm_refresh(result.filepath, fm_cfg[this.client_id]);
                 }
             }
         }
@@ -355,7 +446,7 @@ function create_zipmenu(e, data) {
         params['sesskey'] = moodle_cfg.sesskey;
         fm_loading('filemanager-'+obj.client_id, 'fm-prgressbar');
         var trans = YAHOO.util.Connect.asyncRequest('POST',
-            filemanager.url+'?action=unzip', this.cb, build_querystring(params));
+            fm_cfg.api+'?action=unzip', this.cb, build_querystring(params));
     }
     this.unzip.file = file;
 
@@ -363,10 +454,10 @@ function create_zipmenu(e, data) {
         {text: mstr.moodle.download, url:file.url},
         {text: mstr.moodle.unzip, onclick: {fn: this.unzip, obj: data, scope: this.unzip}}
         ];
-    create_menu(e, 'zipmenu', menuitems, file, data);
+    fm_create_menu(e, 'zipmenu', menuitems, file, data);
 }
 
-function create_menu(ev, menuid, menuitems, file, options) {
+function fm_create_menu(ev, menuid, menuitems, file, options) {
     var position = YAHOO.util.Event.getXY(ev);
     var el = document.getElementById(menuid);
     var menu = new YAHOO.widget.Menu(menuid, {xy:position});
@@ -387,7 +478,7 @@ function create_menu(ev, menuid, menuitems, file, options) {
             params['sesskey'] = moodle_cfg.sesskey;
             fm_loading('filemanager-'+this.client_id, 'fm-prgressbar');
             var trans = YAHOO.util.Connect.asyncRequest('POST',
-                filemanager.url+'?action=delete', this.cb, build_querystring(params));
+                fm_cfg.api+'?action=delete', this.cb, build_querystring(params));
         }
         var dlg = confirm_dialog(ev, args);
         dlg.file = file;
@@ -400,11 +491,17 @@ function create_menu(ev, menuid, menuitems, file, options) {
                     alert(mstr.error.cannotdeletefile);
                 }
                 fm_cfg[this.client_id].currentfiles--;
+                console.info('delete callback: '+fm_cfg[this.client_id].currentfiles);
                 if (fm_cfg[this.client_id].currentfiles<fm_cfg[this.client_id].maxfiles) {
                     var btn = document.getElementById('btnadd-'+this.client_id);
                     btn.style.display = 'inline';
+                    btn.onclick = function(e) {
+                        this.options.savepath = this.options.currentpath;
+                        fm_launch_filepicker(this.options.target, this.options);
+                    }
+                    btn.options = fm_cfg[this.client_id];
                 }
-                refresh_filemanager(result.filepath, fm_cfg[this.client_id]);
+                fm_refresh(result.filepath, fm_cfg[this.client_id]);
             }
         }
         dlg.cb.file = this.file;
@@ -423,7 +520,7 @@ function create_menu(ev, menuid, menuitems, file, options) {
                     // update filename
                     file.fullname = this.newfilename;
                     file.filepath = result.filepath;
-                    fm_rename_dlg.hide();
+                    YAHOO.moodle.filemanager.rename_dialog.hide();
                 }
             }
         }
@@ -450,7 +547,7 @@ function create_menu(ev, menuid, menuitems, file, options) {
             params['sesskey'] = moodle_cfg.sesskey;
             rename_cb.newfilename = newfilename;
             var trans = YAHOO.util.Connect.asyncRequest('POST',
-                filemanager.url+'?action='+action, rename_cb, build_querystring(params));
+                fm_cfg.api+'?action='+action, rename_cb, build_querystring(params));
         }
 
         var scope = document.getElementById('fm-rename-dlg');
@@ -459,7 +556,7 @@ function create_menu(ev, menuid, menuitems, file, options) {
             el.id = 'fm-rename-dlg';
             el.innerHTML = '<div class="hd">'+mstr.repository.enternewname+'</div><div class="bd"><input type="text" id="fm-rename-input" /></div>';
             document.body.appendChild(el);
-            fm_rename_dlg = new YAHOO.widget.Dialog("fm-rename-dlg", {
+            YAHOO.moodle.filemanager.rename_dialog = new YAHOO.widget.Dialog("fm-rename-dlg", {
                  width: "300px",
                  fixedcenter: true,
                  visible: true,
@@ -470,9 +567,9 @@ function create_menu(ev, menuid, menuitems, file, options) {
         var buttons = [ { text:mstr.moodle.rename, handler:perform, isDefault:true },
                           { text:mstr.moodle.cancel, handler:function(){this.cancel();}}];
 
-        fm_rename_dlg.cfg.queueProperty("buttons", buttons);
-        fm_rename_dlg.render();
-        fm_rename_dlg.show();
+        YAHOO.moodle.filemanager.rename_dialog.cfg.queueProperty("buttons", buttons);
+        YAHOO.moodle.filemanager.rename_dialog.render();
+        YAHOO.moodle.filemanager.rename_dialog.show();
 
         var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){perform();}, correctScope: true});
         k1.enable();
@@ -496,7 +593,7 @@ function create_menu(ev, menuid, menuitems, file, options) {
                     if (result) {
                         p = result.filepath;
                     }
-                    refresh_filemanager(result.filepath, fm_cfg[obj.client_id]);
+                    fm_refresh(result.filepath, fm_cfg[obj.client_id]);
                     this.dlg.cancel();
                 }
             }
@@ -516,13 +613,13 @@ function create_menu(ev, menuid, menuitems, file, options) {
             params['newfilepath'] = tree.targetpath;
             fm_loading('filemanager-'+obj.client_id, 'fm-prgressbar');
             var trans = YAHOO.util.Connect.asyncRequest('POST',
-                filemanager.url+'?action='+action, cb, build_querystring(params));
+                fm_cfg.api+'?action='+action, cb, build_querystring(params));
         }
 
         var buttons = [ { text:mstr.moodle.move, handler:this.asyncMove, isDefault:true },
                           { text:mstr.moodle.cancel, handler:function(){this.cancel();}}];
 
-        fm_move_dlg.cfg.queueProperty("buttons", buttons);
+        YAHOO.moodle.filemanager.movefile_dialog.cfg.queueProperty("buttons", buttons);
 
 
         tree.subscribe("dblClickEvent", function(e) {
@@ -554,13 +651,13 @@ function create_menu(ev, menuid, menuitems, file, options) {
             params['filepath'] = node.data.path;
             params['sesskey'] = moodle_cfg.sesskey;
             var trans = YAHOO.util.Connect.asyncRequest('POST',
-                filemanager.url+'?action=dir', this.cb, build_querystring(params));
+                fm_cfg.api+'?action=dir', this.cb, build_querystring(params));
             this.cb.complete = onCompleteCallback;
             this.cb.itemid = node.data.itemid;
         }
         this.loadDataForNode.itemid = obj.itemid;
 
-        fm_move_dlg.subscribe('show', function(){
+        YAHOO.moodle.filemanager.movefile_dialog.subscribe('show', function(){
 
             var el = document.getElementById('fm-move-div');
             el.innerHTML = '<div class="hd"></div><div class="bd"><div id="fm-move-div">'+mstr.repository.nopathselected+'</div><div id="fm-tree"></div></div>';
@@ -574,8 +671,8 @@ function create_menu(ev, menuid, menuitems, file, options) {
 
         }, this, true);
 
-        fm_move_dlg.render();
-        fm_move_dlg.show();
+        YAHOO.moodle.filemanager.movefile_dialog.render();
+        YAHOO.moodle.filemanager.movefile_dialog.show();
     }
     this.move.file = file;
     var shared_items = [
@@ -593,7 +690,7 @@ function create_menu(ev, menuid, menuitems, file, options) {
                 document.getElementById(fm_cfg[obj.client_id].mainfile+'-label').innerHTML = mainfile.value;
             }
             fm_cfg[obj.client_id].mainfilename = this.file.fullname;
-            refresh_filemanager(fm_cfg[obj.client_id].currentpath, fm_cfg[obj.client_id]);
+            fm_refresh(fm_cfg[obj.client_id].currentpath, fm_cfg[obj.client_id]);
 
         }
         this.set_mainfile.file = file;
@@ -606,18 +703,24 @@ function create_menu(ev, menuid, menuitems, file, options) {
     });
 }
 
-function setup_filebrowser(client_id, options) {
+/**
+ * setup file manager options and initialize filemanger itself
+ */
+function launch_filemanager(client_id, options) {
+    // setup options & parameters
     if (!options) {
         options = {};
     }
     fm_cfg[client_id] = {};
     fm_cfg[client_id] = options;
-    fm_cfg[client_id].mainfile = options.mainfile;
     fm_cfg[client_id].currentpath = '/';
-    fm_cfg[client_id].currentfiles = 0;
-    // XXX: When editing existing folder, currentfiles shouldn't
-    // be 0
-    fm_cfg[client_id].maxfiles = options.maxfiles;
+    if (fm_cfg[client_id].filecount) {
+        fm_cfg[client_id].currentfiles = fm_cfg[client_id].filecount;
+    } else {
+        fm_cfg[client_id].currentfiles = 0;
+    }
+    //
+    console.info(fm_cfg[client_id].currentfiles);
     if (options.mainfile) {
         var mainfilename = document.getElementById(options.mainfile+'-id');
         if (mainfilename.value) {
@@ -629,77 +732,72 @@ function setup_filebrowser(client_id, options) {
             fm_cfg[client_id].mainfilename = '';
         }
     }
+    // setup filemanager
     var fm = new filemanager();
     fm.init(client_id, options);
-    setup_buttons(client_id, options);
+    // setup toolbar
+    fm_setup_buttons(client_id, options);
 }
 
-function setup_buttons(client_id, options) {
-    //var fileadd = new YAHOO.widget.Button("btnadd-"+client_id);
-    var fileadd = document.getElementById("btnadd-"+client_id);;
-    var foldercreate = document.getElementById("btncrt-"+client_id);
-    var folderdownload = document.getElementById("btndwn-"+client_id);
-
-    var el = null;
-    if (!fm_move_dlg) {
-        el = document.createElement('DIV');
-        el.id = 'fm-move-dlg';
-        document.body.appendChild(el);
-        fm_move_dlg = new YAHOO.widget.Dialog("fm-move-dlg", {
-             width : "600px",
-             fixedcenter : true,
-             visible : false,
-             constraintoviewport : true
-             });
-    } else {
-        el = document.getElementById('fm-move-div');
-    }
-
-    el.innerHTML = '<div class="hd"></div><div class="bd"><div id="fm-move-div">'+mstr.repository.nopathselected+'</div><div id="fm-tree"></div></div>';
-
-    fm_move_dlg.render();
+/**
+ * Set up buttons
+ */
+function fm_setup_buttons(client_id, options) {
+    var button_download = document.getElementById("btndwn-"+client_id);
+    var button_create   = document.getElementById("btncrt-"+client_id);
+    var button_addfile  = document.getElementById("btnadd-"+client_id);
 
+    // setup 'add file' button
     // if maxfiles == -1, the no limit
-    if (fm_cfg[client_id].filecount >= fm_cfg[client_id].maxfiles && fm_cfg[client_id].maxfiles!=-1) {
-        fileadd.style.display = 'none';
+    if (fm_cfg[client_id].filecount >= fm_cfg[client_id].maxfiles
+            && fm_cfg[client_id].maxfiles!=-1) {
+        button_addfile.style.display = 'none';
     } else {
-        fm_cfg[client_id].currentfiles = fm_cfg[client_id].filecount;
-        fileadd.onclick = function(e) {
+        button_addfile.onclick = function(e) {
             this.options.savepath = this.options.currentpath;
             fm_launch_filepicker(this.options.target, this.options);
         }
-        fileadd.options = fm_cfg[client_id];
+        button_addfile.options = fm_cfg[client_id];
     }
+
+    // setup 'make a folder' button
     if (fm_cfg[client_id].subdirs) {
-        foldercreate.onclick = function(e) {
-            mkdir(e, this.options.client_id, this.options.itemid);
+        button_create.onclick = function(e) {
+            fm_create_folder(e, this.options.client_id, this.options.itemid);
         }
-        foldercreate.options = fm_cfg[client_id];
+        button_create.options = fm_cfg[client_id];
     } else {
-        foldercreate.style.display = 'none';
+        button_create.style.display = 'none';
     }
-    folderdownload.onclick = function() {
-        var cb = {
+
+    // setup 'download this folder' button
+    // NOTE: popup window must be enabled to perform download process
+    button_download.onclick = function() {
+        var downloaddir_callback = {
             success:function(o) {
                 var result = json_decode(o.responseText);
-                refresh_filemanager(result.filepath, fm_cfg[this.client_id]);
+                fm_refresh(result.filepath, fm_cfg[this.client_id]);
                 var win = window.open(result.fileurl, 'fm-download-folder');
                 if (!win) {
                     alert(mstr.repository.popupblockeddownload);
                 }
             }
         };
-        cb.client_id = this.options.client_id;
+        downloaddir_callback.client_id = this.options.client_id;
         var params = [];
         params['itemid'] = this.options.itemid;
         params['sesskey'] = moodle_cfg.sesskey;
         params['filepath'] = this.options.currentpath;
+        // perform downloaddir ajax request
         var trans = YAHOO.util.Connect.asyncRequest('POST',
-            filemanager.url+'?action=downloaddir', cb, build_querystring(params));
+            fm_cfg.api+'?action=downloaddir', downloaddir_callback, build_querystring(params));
     }
-    folderdownload.options = fm_cfg[client_id];
+    button_download.options = fm_cfg[client_id];
 }
 
+/**
+ * Print a progress bar
+ */
 function fm_loading(container, id) {
 
     if (!document.getElementById(id)) {
index 1625cd7578f75aa9c6fd9ab7fc3bd1261aa05c29..9a8522f64655b07bfdef9b508ae26e8e09d9afe7 100644 (file)
@@ -210,7 +210,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
             $CFG->filemanagerjsloaded = true;
             // print html template
             $html .= <<<FMHTML
-<div id="fm-tmpl" style="display:none"><div class="fm-file-menu">___action___</div> <div class="fm-file-name">___fullname___</div></div>
+<div id="fm-template" style="display:none"><div class="fm-file-menu">___action___</div> <div class="fm-file-name">___fullname___</div></div>
 FMHTML;
         }
 
@@ -239,7 +239,7 @@ FMHTML;
 
         $html .= $PAGE->requires->js_function_call('destroy_item', array("nonjs-filemanager-{$client_id}"))->asap();
         $html .= $PAGE->requires->js_function_call('show_item', array("filemanager-wrapper-{$client_id}"))->asap();
-        $PAGE->requires->js_function_call('setup_filebrowser', array($client_id, $options))->on_dom_ready();
+        $PAGE->requires->js_function_call('launch_filemanager', array($client_id, $options))->on_dom_ready();
 
         return $html;
     }
index f5df685b13619482c0a88b82f3f9dcfaead0dcee..b9f8a99b5188a6209111c6aa54d26570ef16abfd 100644 (file)
@@ -1045,6 +1045,7 @@ repository_client.download = function(client_id, repo_id) {
     params['ctx_id']=fp_config.contextid;
     params['repo_id']=repo_id;
     params['client_id']=client_id;
+    params['maxbytes']=fp.maxbytes;
     var trans = YAHOO.util.Connect.asyncRequest('POST',
             MOODLE.repository.api+'?action=download',
             repository_client.download_cb,
@@ -1247,7 +1248,7 @@ var mdl_in_array = function(el, arr) {
     return false;
 }
 
-// will be called by filemanager or htmleditor
+// will be called by filemanager or editor to launch file picker
 function open_filepicker(id, params) {
     var r = repository_client.fp[id];
     if(!r) {
@@ -1294,7 +1295,12 @@ function open_filepicker(id, params) {
         // unlimited
         params.maxfiles = -1;
     }
+    if (!params.maxbytes) {
+        // unlimited
+        params.maxbytes = -1;
+    }
     r.maxfiles = params.maxfiles;
+    r.maxbytes = params.maxbytes;
     return r;
 }
 
index b96f6033c01d4d932f0a172274ed330da23663e3..d74491d793734d19e0102f0869763d0bccf6bec9 100755 (executable)
@@ -42,6 +42,7 @@
     $title     = optional_param('title', '', PARAM_FILE);           // new file name
     $page      = optional_param('page', '', PARAM_RAW);             // page
     $repo_id   = optional_param('repo_id', 1, PARAM_INT);           // repository ID
+    $maxbytes  = optional_param('maxbytes', -1, PARAM_INT);           // repository ID
     $req_path  = optional_param('p', '', PARAM_RAW);                // path
     $save_path = optional_param('savepath', '/', PARAM_PATH);
     $search_text   = optional_param('s', '', PARAM_CLEANHTML);
@@ -236,6 +237,11 @@ EOD;
                     $info['file'] = $fileinfo['title'];
                     $info['id'] = $itemid;
                     $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title'];
+                    $filesize = $fileinfo->get_filesize();
+                    if (($maxbytes!==-1) && ($filesize > $maxbytes)) {
+                        $fileinfo->delete();
+                        throw new file_exception('maxbytes');
+                    }
                     die(json_encode($info));
                 }
 
@@ -261,11 +267,15 @@ EOD;
                     die(json_encode($info));
                 }
 
+                // get the file location
                 $filepath = $repo->get_file($file, $title, $itemid, $save_path);
                 if ($filepath === false) {
                     $err->e = get_string('cannotdownload', 'repository');
                     die(json_encode($err));
                 }
+                if (($maxbytes!==-1) && (filesize($filepath) > $maxbytes)) {
+                    throw new file_exception('maxbytes');
+                }
                 $info = repository::move_to_filepool($filepath, $title, $itemid, $save_path);
                 $info['client_id'] = $client_id;
                 echo json_encode($info);
index 9495040d3ca719c7e2d2d7522daa61324a485f95..99505addf4c5aea5dbbc61d87d8b964f76e172a4 100644 (file)
@@ -5875,12 +5875,15 @@ wikiadminactions {
 .filemanager-toolbar{
     margin: 5px 0;
 }
-.filemanager-toolbar a{
+.filemanager-toolbar a:link, .filemanager-toolbar a:visited {
     border: 1px solid grey;
+    background: #ccc;
+    color: black;
     padding: 3px;
 }
 .filemanager-toolbar a:hover {
-    background: #CCC;
+    background: white;
+    text-decoration: none;
 }
 .fm-breadcrumb {
     margin:0;