'+M.util.get_string('actionchoice', 'moodle', file.name)+'';
content += '';
for (var i=0; i';
content += '
';
}
content += '
';
var Y = this.Y;
var self = this;
var panel = new M.core.dialogue({
bodyContent: content,
width: '350px',
modal: true,
visible: false,
render: true,
align: {
node: null,
points: [Y.WidgetPositionAlign.CC, Y.WidgetPositionAlign.CC]
}
});
panel.show();
// When the panel is hidden - destroy it and then check for other pending uploads
panel.after("visibleChange", function(e) {
if (!panel.get('visible')) {
panel.destroy(true);
self.check_upload_queue();
}
});
// Add the submit/cancel buttons to the bottom of the dialog.
panel.addButton({
label: M.util.get_string('upload', 'moodle'),
action: function(e) {
e.preventDefault();
// Find out which module was selected
var module = false;
var div = Y.one('#dndupload_handlers'+uploadid);
div.all('input').each(function(input) {
if (input.get('checked')) {
module = input.get('value');
}
});
if (!module) {
return;
}
panel.hide();
// Remember this selection for next time
self.lastselected[extension] = module;
// Do the upload
self.upload_file(file, section, sectionnumber, module);
},
section: Y.WidgetStdMod.FOOTER
});
panel.addButton({
label: M.util.get_string('cancel', 'moodle'),
action: function(e) {
e.preventDefault();
panel.hide();
},
section: Y.WidgetStdMod.FOOTER
});
},
/**
* Check to see if there are any other dialog boxes to show, now that the current one has
* been dealt with
*/
check_upload_queue: function() {
this.uploaddialog = false;
if (this.uploadqueue.length == 0) {
return;
}
var details = this.uploadqueue.shift();
if (details.isfile) {
this.file_handler_dialog(details.handlers, details.extension, details.file, details.section, details.sectionnumber);
} else {
this.handle_item(details.type, details.contents, details.section, details.sectionnumber);
}
},
/**
* Do the file upload: show the dummy element, use an AJAX call to send the data
* to the server, update the progress bar for the file, then replace the dummy
* element with the real information once the AJAX call completes
* @param file the details of the file, taken from the FileList in the drop event
* @param section the DOM element representing the selected course section
* @param sectionnumber the number of the selected course section
*/
upload_file: function(file, section, sectionnumber, module) {
// This would be an ideal place to use the Y.io function
// however, this does not support data encoded using the
// FormData object, which is needed to transfer data from
// the DataTransfer object into an XMLHTTPRequest
// This can be converted when the YUI issue has been integrated:
// http://yuilibrary.com/projects/yui3/ticket/2531274
var xhr = new XMLHttpRequest();
var self = this;
if (this.maxbytes > 0 && file.size > this.maxbytes) {
new M.core.alert({message: M.util.get_string('namedfiletoolarge', 'moodle', {filename: file.name})});
return;
}
// Add the file to the display
var resel = this.add_resource_element(file.name, section, module);
// Update the progress bar as the file is uploaded
xhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
var percentage = Math.round((e.loaded * 100) / e.total);
resel.progress.style.width = percentage + '%';
}
}, false);
// Wait for the AJAX call to complete, then update the
// dummy element with the returned details
xhr.onreadystatechange = function() {
if (xhr.readyState == 1) {
this.originalUnloadEvent = window.onbeforeunload;
self.reportUploadDirtyState(true);
}
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var result = JSON.parse(xhr.responseText);
if (result) {
if (result.error == 0) {
// All OK - replace the dummy element.
resel.li.outerHTML = result.fullcontent;
if (self.Y.UA.gecko > 0) {
// Fix a Firefox bug which makes sites with a '~' in their wwwroot
// log the user out when clicking on the link (before refreshing the page).
resel.li.outerHTML = unescape(resel.li.outerHTML);
}
self.add_editing(result.elementid);
// Fire the content updated event.
require(['core/event', 'jquery'], function(event, $) {
event.notifyFilterContentUpdated($(result.fullcontent));
});
} else {
// Error - remove the dummy element
resel.parent.removeChild(resel.li);
new M.core.alert({message: result.error});
}
}
} else {
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
}
self.reportUploadDirtyState(false);
}
};
// Prepare the data to send
var formData = new FormData();
try {
formData.append('repo_upload_file', file);
} catch (e) {
// Edge throws an error at this point if we try to upload a folder.
resel.parent.removeChild(resel.li);
new M.core.alert({message: M.util.get_string('filereaderror', 'moodle', file.name)});
return;
}
formData.append('sesskey', M.cfg.sesskey);
formData.append('course', this.courseid);
formData.append('section', sectionnumber);
formData.append('module', module);
formData.append('type', 'Files');
// Try reading the file to check it is not a folder, before sending it to the server.
var reader = new FileReader();
reader.onload = function() {
// File was read OK - send it to the server.
xhr.open("POST", self.url, true);
xhr.send(formData);
};
reader.onerror = function() {
// Unable to read the file (it is probably a folder) - display an error message.
resel.parent.removeChild(resel.li);
new M.core.alert({message: M.util.get_string('filereaderror', 'moodle', file.name)});
};
if (file.size > 0) {
// If this is a non-empty file, try reading the first few bytes.
// This will trigger reader.onerror() for folders and reader.onload() for ordinary, readable files.
reader.readAsText(file.slice(0, 5));
} else {
// If you call slice() on a 0-byte folder, before calling readAsText, then Firefox triggers reader.onload(),
// instead of reader.onerror().
// So, for 0-byte files, just call readAsText on the whole file (and it will trigger load/error functions as expected).
reader.readAsText(file);
}
},
/**
* Show a dialog box to gather the name of the resource / activity to be created
* from the uploaded content
* @param type the details of the type of content
* @param contents the contents to be uploaded
* @section the DOM element for the section being uploaded to
* @sectionnumber the number of the section being uploaded to
*/
handle_item: function(type, contents, section, sectionnumber) {
if (type.handlers.length == 0) {
// Nothing to handle this - should not have got here
return;
}
if (type.handlers.length == 1 && type.handlers[0].noname) {
// Only one handler and it doesn't need a name (i.e. a label).
this.upload_item('', type.type, contents, section, sectionnumber, type.handlers[0].module);
this.check_upload_queue();
return;
}
if (this.uploaddialog) {
var details = new Object();
details.isfile = false;
details.type = type;
details.contents = contents;
details.section = section;
details.setcionnumber = sectionnumber;
this.uploadqueue.push(details);
return;
}
this.uploaddialog = true;
var timestamp = new Date().getTime();
var uploadid = Math.round(Math.random()*100000)+'-'+timestamp;
var nameid = 'dndupload_handler_name'+uploadid;
var content = '';
if (type.handlers.length > 1) {
content += ''+type.handlermessage+'
';
content += '';
var sel = type.handlers[0].module;
for (var i=0; i';
content += '
';
}
content += '
';
}
var disabled = (type.handlers[0].noname) ? ' disabled = "disabled" ' : '';
content += '';
content += ' ';
var Y = this.Y;
var self = this;
var panel = new M.core.dialogue({
bodyContent: content,
width: '350px',
modal: true,
visible: true,
render: true,
align: {
node: null,
points: [Y.WidgetPositionAlign.CC, Y.WidgetPositionAlign.CC]
}
});
// When the panel is hidden - destroy it and then check for other pending uploads
panel.after("visibleChange", function(e) {
if (!panel.get('visible')) {
panel.destroy(true);
self.check_upload_queue();
}
});
var namefield = Y.one('#'+nameid);
var submit = function(e) {
e.preventDefault();
var name = Y.Lang.trim(namefield.get('value'));
var module = false;
var noname = false;
if (type.handlers.length > 1) {
// Find out which module was selected
var div = Y.one('#dndupload_handlers'+uploadid);
div.all('input').each(function(input) {
if (input.get('checked')) {
var idx = input.get('value');
module = type.handlers[idx].module;
noname = type.handlers[idx].noname;
}
});
if (!module) {
return;
}
} else {
module = type.handlers[0].module;
noname = type.handlers[0].noname;
}
if (name == '' && !noname) {
return;
}
if (noname) {
name = '';
}
panel.hide();
// Do the upload
self.upload_item(name, type.type, contents, section, sectionnumber, module);
};
// Add the submit/cancel buttons to the bottom of the dialog.
panel.addButton({
label: M.util.get_string('upload', 'moodle'),
action: submit,
section: Y.WidgetStdMod.FOOTER,
name: 'submit'
});
panel.addButton({
label: M.util.get_string('cancel', 'moodle'),
action: function(e) {
e.preventDefault();
panel.hide();
},
section: Y.WidgetStdMod.FOOTER
});
var submitbutton = panel.getButton('submit').button;
namefield.on('key', submit, 'enter'); // Submit the form if 'enter' pressed
namefield.after('keyup', function() {
if (Y.Lang.trim(namefield.get('value')) == '') {
submitbutton.disable();
} else {
submitbutton.enable();
}
});
// Enable / disable the 'name' box, depending on the handler selected.
for (i=0; i 0) {
// Fix a Firefox bug which makes sites with a '~' in their wwwroot
// log the user out when clicking on the link (before refreshing the page).
resel.li.outerHTML = unescape(resel.li.outerHTML);
}
self.add_editing(result.elementid);
} else {
// Error - remove the dummy element
resel.parent.removeChild(resel.li);
new M.core.alert({message: result.error});
}
}
} else {
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
}
self.reportUploadDirtyState(false);
}
};
// Prepare the data to send
var formData = new FormData();
formData.append('contents', contents);
formData.append('displayname', name);
formData.append('sesskey', M.cfg.sesskey);
formData.append('course', this.courseid);
formData.append('section', sectionnumber);
formData.append('type', type);
formData.append('module', module);
// Send the data
xhr.open("POST", this.url, true);
xhr.send(formData);
},
/**
* Call the AJAX course editing initialisation to add the editing tools
* to the newly-created resource link
* @param elementid the id of the DOM element containing the new resource link
* @param sectionnumber the number of the selected course section
*/
add_editing: function(elementid) {
var node = Y.one('#' + elementid);
YUI().use('moodle-course-coursebase', function(Y) {
Y.log("Invoking setup_for_resource", 'debug', 'coursedndupload');
M.course.coursebase.invoke_function('setup_for_resource', node);
});
if (M.core.actionmenu && M.core.actionmenu.newDOMNode) {
M.core.actionmenu.newDOMNode(node);
}
},
/**
* Set the event to prevent user navigate away when upload progress still running.
*
* @param {bool} enable true if upload progress is running, false otherwise
*/
reportUploadDirtyState: function(enable) {
if (!enable) {
window.onbeforeunload = this.originalUnloadEvent;
} else {
window.onbeforeunload = function(e) {
var warningMessage = M.util.get_string('changesmadereallygoaway', 'moodle');
if (e) {
e.returnValue = warningMessage;
}
return warningMessage;
};
}
}
};