Permalink
Browse files

MDL-33136 upload dnd files one after another

  • Loading branch information...
1 parent f8dfdb5 commit 52474d74970993b59cc7d90b3b8722897d2845c3 @marinaglancy marinaglancy committed Jun 15, 2012
Showing with 89 additions and 31 deletions.
  1. +89 −31 lib/form/dndupload.js
View
120 lib/form/dndupload.js
@@ -30,6 +30,8 @@ M.form_dndupload.init = function(Y, options) {
Y: null,
// URL for upload requests
url: M.cfg.wwwroot + '/repository/repository_ajax.php?action=upload',
+ // options may include: itemid, acceptedtypes, maxfiles, maxbytes, clientid, repositoryid, author
+ options: {},
// itemid used for repository upload
itemid: null,
// accepted filetypes accepted by this form passed to repository
@@ -86,6 +88,7 @@ M.form_dndupload.init = function(Y, options) {
return; // no upload repository is enabled to upload to
}
+ this.options = options;
this.acceptedtypes = options.acceptedtypes;
this.clientid = options.clientid;
this.maxfiles = options.maxfiles;
@@ -301,19 +304,30 @@ M.form_dndupload.init = function(Y, options) {
if (this.filemanager) {
var currentfilecount = this.filemanager.filecount;
if (((currentfilecount + files.length) > this.maxfiles) && (this.maxfiles != -1)) {
- alert(M.util.get_string('maxfilesreached', 'moodle', this.maxfiles));
+ // TODO in case of overwrite we don't need it!
+ alert(M.util.get_string('maxfilesreached', 'moodle', this.maxfiles)); // TODO change to YUI
return false;
}
- this.show_progress_spinner();
- for (var i=0, f; f=files[i]; i++) {
- if (this.upload_file(f)) {
- currentfilecount++;
- }
- }
+ //this.show_progress_spinner();
+ var options = {
+ files:files,
+ options: this.options,
+ repositoryid: this.repositoryid,
+ callback:this.callback ? Y.bind('callback', this) : Y.bind('update_filemanager', this)
+ };
+ var uploader = new dnduploader(options);
+ uploader.doupload();
} else {
- this.show_progress_spinner();
+ //this.show_progress_spinner();
if (files.length >= 1) {
- this.upload_file(files[0]);
+ options = {
+ files:[files[0]],
+ options: this.options,
+ repositoryid: this.repositoryid,
+ callback:this.callback ? Y.bind('callback', this) : Y.bind('update_filemanager', this)
+ };
+ uploader = new dnduploader(options);
+ uploader.doupload();
}
}
@@ -398,16 +412,56 @@ M.form_dndupload.init = function(Y, options) {
// update the filemanager that we've uploaded the files
this.filemanager.filepicker_callback();
}
+ }
+ };
+
+ var dnduploader = function(options) {
+ dnduploader.superclass.constructor.apply(this, arguments);
+ };
+
+ Y.extend(dnduploader, Y.Base, {
+ api: M.cfg.wwwroot+'/repository/repository_ajax.php',
+ options: {},
+ callback: null,
+ files: null,
+ repositoryid: 0,
+ processedfiles: 0,
+
+ initializer: function(params) {
+ this.options = params.options;
+ this.repositoryid = params.repositoryid;
+ this.callback = params.callback;
+ this.files = params.files; // TODO make copy?
+ },
+
+ doupload: function(lastresult) {
+ console.log('doupload : '+this.processedfiles);
+ if (this.files && this.files.length > this.processedfiles) {
+ console.log('uploading file');
+ console.log(this.files[this.processedfiles])
+ this.upload_file(this.files[this.processedfiles]);
+ } else {
+ this.uploadfinished(lastresult);
+ }
+ },
+
+ uploadfinished: function(lastresult) {
+ console.log('upload finished')
+ this.callback(lastresult);
+ },
+
+ add_error: function(text, errorlevel) {
+ // TODO
+ console.log(text);
},
/**
* Upload a single file via an AJAX call to the 'upload' repository
*/
upload_file: function(file) {
- if (file.size > this.maxbytes && this.maxbytes > 0) {
+ if (this.options.maxbytes > 0 && file.size > this.options.maxbytes) {
// Check filesize before attempting to upload
- this.hide_progress_spinner();
- alert(M.util.get_string('uploadformlimit', 'moodle')+"\n'"+file.name+"'");
+ this.add_error(M.util.get_string('uploadformlimit', 'moodle')+"\n'"+file.name+"'"); //TODO proper string
return false;
}
@@ -421,59 +475,63 @@ M.form_dndupload.init = function(Y, options) {
var self = this;
xhr.onreadystatechange = function() { // Process the server response
if (xhr.readyState == 4) {
- self.hide_progress_spinner();
if (xhr.status == 200) {
+ console.log(xhr.responseText)
var result = JSON.parse(xhr.responseText);
if (result) {
+ console.log(result)
if (result.error) {
- alert(result.error);
- } else if (self.callback) {
+ self.add_error(result.error); // TODO add filename?
+ } else {
// Only update the filepicker if there were no errors
if (result.event == 'fileexists') {
// Do not worry about this, as we only care about the last
// file uploaded, with the filepicker
result.file = result.newfile.filename;
result.url = result.newfile.url;
}
- result.client_id = self.clientid;
- self.callback(result);
- } else {
- self.update_filemanager();
+ result.client_id = self.options.clientid;
}
}
+ self.processedfiles++;
+ self.doupload(result); // continue uploading
} else {
- alert(M.util.get_string('serverconnection', 'error'));
+ self.add_error(M.util.get_string('serverconnection', 'error'));
+ this.uploadfinished();
}
}
};
// Prepare the data to send
var formdata = new FormData();
+ formdata.append('action', 'upload');
formdata.append('repo_upload_file', file); // The FormData class allows us to attach a file
formdata.append('sesskey', M.cfg.sesskey);
formdata.append('repo_id', this.repositoryid);
- formdata.append('itemid', this.itemid);
- if (this.author) {
- formdata.append('author', this.author);
+ formdata.append('itemid', this.options.itemid);
+ if (this.options.author) {
+ formdata.append('author', this.options.author);
}
- if (this.filemanager) { // Filepickers do not have folders
- formdata.append('savepath', this.filemanager.currentpath);
+ if (this.options.filemanager) { // Filepickers do not have folders
+ formdata.append('savepath', this.options.filemanager.currentpath);
}
- if (this.acceptedtypes.constructor == Array) {
- for (var i=0; i<this.acceptedtypes.length; i++) {
- formdata.append('accepted_types[]', this.acceptedtypes[i]);
+ if (this.options.acceptedtypes.constructor == Array) { // TODO what is this???
+ for (var i=0; i<this.options.acceptedtypes.length; i++) {
+ formdata.append('accepted_types[]', this.options.acceptedtypes[i]);
}
} else {
- formdata.append('accepted_types[]', this.acceptedtypes);
+ formdata.append('accepted_types[]', this.options.acceptedtypes);
}
// Send the file & required details
- xhr.open("POST", this.url, true);
+ xhr.open("POST", this.api, true);
+ console.log('formdata')
+ console.log(formdata)
xhr.send(formdata);
return true;
}
- };
+ });
dnduploadhelper.init(Y, options);
};

0 comments on commit 52474d7

Please sign in to comment.