Merge branch 'MDL-36900-master' of git://github.com/FMCorz/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 11 Mar 2013 23:45:59 +0000 (00:45 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 11 Mar 2013 23:45:59 +0000 (00:45 +0100)
files/renderer.php
lang/en/repository.php
lib/form/dndupload.js
lib/form/filemanager.js
lib/javascript-static.js

index 53c74f0..5f9eb61 100644 (file)
@@ -114,7 +114,7 @@ class core_files_renderer extends plugin_renderer_base {
                 array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
                 array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
                 array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
-                array('confirmrenamefile', 'repository')
+                array('confirmrenamefile', 'repository'), array('newfolder', 'repository')
             )
         );
         if (empty($filemanagertemplateloaded)) {
index 38df742..f6063c5 100644 (file)
@@ -152,6 +152,7 @@ $string['manage'] = 'Manage repositories';
 $string['manageurl'] = 'Manage';
 $string['manageuserrepository'] = 'Manage individual repository';
 $string['moving'] = 'Moving';
+$string['newfolder'] = 'New folder';
 $string['newfoldername'] = 'New folder name:';
 $string['noenter'] = 'Nothing entered';
 $string['nofilesattached'] = 'No files attached';
index e6a6ad3..4be0650 100644 (file)
@@ -778,41 +778,17 @@ M.form_dndupload.init = function(Y, options) {
         },
 
         /**
-         * Adds _NUMBER to the end of the filename and increments this number until
-         * a unique name is found
+         * Gets a unique file name
+         *
          * @param string filename
          * @return string the unique filename generated
          */
         generate_unique_name: function(filename) {
-            // Split the filename into the basename + extension.
-            var extension;
-            var basename;
-            var dotpos = filename.lastIndexOf('.');
-            if (dotpos == -1) {
-                basename = filename;
-                extension = '';
-            } else {
-                basename = filename.substr(0, dotpos);
-                extension = filename.substr(dotpos, filename.length);
-            }
-
-            // Look to see if the name already has (NN) at the end of it.
-            var number = 0;
-            var hasnumber = basename.match(/^(.*) \((\d+)\)$/);
-            if (hasnumber !== null) {
-                // Note the current number & remove it from the basename.
-                number = parseInt(hasnumber[2], 10);
-                basename = hasnumber[1];
-            }
-
             // Loop through increating numbers until a unique name is found.
-            var newname;
-            do {
-                number++;
-                newname = basename + ' (' + number + ')' + extension;
-            } while (this.has_name_clash(newname));
-
-            return newname;
+            while (this.has_name_clash(filename)) {
+                filename = increment_filename(filename);
+            }
+            return filename;
         },
 
         /**
index 917ad8c..a63119c 100644 (file)
@@ -302,7 +302,19 @@ M.form_filemanager.init = function(Y, options) {
                                 }
                             }
                         });
-                    }
+                    };
+                    var validate_folder_name = function() {
+                        var valid = false;
+                        var foldername = Y.one('#fm-newname-'+scope.client_id).get('value');
+                        if (foldername.length > 0) {
+                            valid = true;
+                        }
+                        var btn = Y.one('#fm-mkdir-butcreate-'+scope.client_id);
+                        if (btn) {
+                            btn.set('disabled', !valid);
+                        }
+                        return valid;
+                    };
                     if (!this.mkdir_dialog) {
                         var node = Y.Node.createWithFilesSkin(M.form_filemanager.templates.mkdir);
                         this.mkdir_dialog = new Y.Panel({
@@ -314,18 +326,33 @@ M.form_filemanager.init = function(Y, options) {
                             render       : true
                         });
                         this.mkdir_dialog.plug(Y.Plugin.Drag,{handles:['.yui3-widget-hd']});
-                        node.one('.fp-dlg-butcreate').on('click', perform_action, this);
-                        node.one('input').set('id', 'fm-newname-'+this.client_id).
-                            on('keydown', function(e){
-                                if (e.keyCode == 13) {Y.bind(perform_action, this)(e);}
-                            }, this);
+                        node.one('.fp-dlg-butcreate').set('id', 'fm-mkdir-butcreate-'+this.client_id).on('click',
+                                perform_action, this);
+                        node.one('input').set('id', 'fm-newname-'+this.client_id).on('keydown', function(e) {
+                            var valid = Y.bind(validate_folder_name, this)();
+                            if (valid && e.keyCode === 13) {
+                                Y.bind(perform_action, this)(e);
+                            }
+                        }, this);
+                        node.one('#fm-newname-'+this.client_id).on(['keyup', 'change'], function(e) {
+                            Y.bind(validate_folder_name, this)();
+                        }, this);
+
                         node.one('label').set('for', 'fm-newname-' + this.client_id);
                         node.all('.fp-dlg-butcancel').on('click', function(e){e.preventDefault();this.mkdir_dialog.hide();}, this);
                         node.all('.fp-dlg-curpath').set('id', 'fm-curpath-'+this.client_id);
                     }
                     this.mkdir_dialog.show();
-                    Y.one('#fm-newname-'+scope.client_id).focus();
-                    Y.all('#fm-curpath-'+scope.client_id).setContent(Y.Escape.html(this.currentpath))
+
+                    // Default folder name:
+                    var foldername = M.str.repository.newfolder;
+                    while (this.has_folder(foldername)) {
+                        foldername = increment_filename(foldername, true);
+                    }
+                    Y.one('#fm-newname-'+scope.client_id).set('value', foldername);
+                    Y.bind(validate_folder_name, this)();
+                    Y.one('#fm-newname-'+scope.client_id).focus().select();
+                    Y.all('#fm-curpath-'+scope.client_id).setContent(this.currentpath);
                 }, this);
             } else {
                 this.filemanager.addClass('fm-nomkdir');
@@ -990,6 +1017,16 @@ M.form_filemanager.init = function(Y, options) {
         render: function() {
             this.print_path();
             this.view_files();
+        },
+        has_folder: function(foldername) {
+            var element;
+            for (var i in this.options.list) {
+                element = this.options.list[i];
+                if (element.type == 'folder' && element.fullname == foldername) {
+                    return true;
+                }
+            }
+            return false;
         }
     });
 
index b1e2ed4..3930b79 100644 (file)
@@ -1236,6 +1236,41 @@ function getElementsByClassName(oElm, strTagName, name) {
     return (arrReturnElements)
 }
 
+/**
+ * Increment a file name.
+ *
+ * @param string file name.
+ * @param boolean ignoreextension do not extract the extension prior to appending the
+ *                                suffix. Useful when incrementing folder names.
+ * @return string the incremented file name.
+ */
+function increment_filename(filename, ignoreextension) {
+    var extension = '';
+    var basename = filename;
+
+    // Split the file name into the basename + extension.
+    if (!ignoreextension) {
+        var dotpos = filename.lastIndexOf('.');
+        if (dotpos !== -1) {
+            basename = filename.substr(0, dotpos);
+            extension = filename.substr(dotpos, filename.length);
+        }
+    }
+
+    // Look to see if the name already has (NN) at the end of it.
+    var number = 0;
+    var hasnumber = basename.match(/^(.*) \((\d+)\)$/);
+    if (hasnumber !== null) {
+        // Note the current number & remove it from the basename.
+        number = parseInt(hasnumber[2], 10);
+        basename = hasnumber[1];
+    }
+
+    number++;
+    var newname = basename + ' (' + number + ')' + extension;
+    return newname;
+}
+
 /**
  * Return whether we are in right to left mode or not.
  *