1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Javascript library for enableing a drag and drop upload interface
21 * @copyright 2011 Davo Smith
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 M.form_dndupload.init = function(Y, options) {
28 var dnduploadhelper = {
31 // URL for upload requests
32 url: M.cfg.wwwroot + '/repository/repository_ajax.php?action=upload',
33 // options may include: itemid, acceptedtypes, maxfiles, maxbytes, clientid, repositoryid, author
35 // itemid used for repository upload
37 // accepted filetypes accepted by this form passed to repository
39 // maximum size of files allowed in this form
41 // unqiue id of this form field used for html elements
43 // upload repository id, used for upload
45 // container which holds the node which recieves drag events
47 // filemanager element we are working with
49 // callback to filepicker element to refesh when uploaded
51 // Nasty hack to distinguish between dragenter(first entry),
52 // dragenter+dragleave(moving between child elements) and dragleave (leaving element)
57 * Initalise the drag and drop upload interface
58 * Note: one and only one of options.filemanager and options.formcallback must be defined
60 * @param Y the YUI object
61 * @param object options {
62 * itemid: itemid used for repository upload in this form
63 * acceptdtypes: accepted filetypes by this form
64 * maxfiles: maximum number of files this form allows
65 * maxbytes: maximum size of files allowed in this form
66 * clientid: unqiue id of this form field used for html elements
67 * containerid: htmlid of container
68 * repositories: array of repository objects passed from filepicker
69 * filemanager: filemanager element we are working with
70 * formcallback: callback to filepicker element to refesh when uploaded
73 init: function(Y, options) {
76 if (!this.browser_supported()) {
77 Y.one('body').addClass('dndnotsupported');
78 return; // Browser does not support the required functionality
80 Y.one('body').addClass('dndsupported');
82 // try and retrieve enabled upload repository
83 this.repositoryid = this.get_upload_repositoryid(options.repositories);
85 if (!this.repositoryid) {
86 return; // no upload repository is enabled to upload to
89 this.options = options;
90 this.acceptedtypes = options.acceptedtypes;
91 this.clientid = options.clientid;
92 this.maxbytes = options.maxbytes;
93 this.itemid = options.itemid;
94 this.author = options.author;
95 this.container = this.Y.one('#'+options.containerid);
97 if (options.filemanager) {
98 // Needed to tell the filemanager to redraw when files uploaded
99 // and to check how many files are already uploaded
100 this.filemanager = options.filemanager;
101 } else if (options.formcallback) {
103 // Needed to tell the filepicker to update when a new
105 this.callback = options.formcallback;
107 if (M.cfg.developerdebug) {
108 alert('dndupload: Need to define either options.filemanager or options.formcallback');
114 this.init_page_events();
118 * Check the browser has the required functionality
119 * @return true if browser supports drag/drop upload
121 browser_supported: function() {
123 if (typeof FileReader == 'undefined') {
126 if (typeof FormData == 'undefined') {
133 * Get upload repoistory from array of enabled repositories
135 * @param array repositories repository objects passed from filepicker
136 * @param returns int id of upload repository or false if not found
138 get_upload_repositoryid: function(repositories) {
139 for (var i in repositories) {
140 if (repositories[i].type == "upload") {
141 return repositories[i].id;
149 * Initialise drag events on node container, all events need
150 * to be processed for drag and drop to work
152 init_events: function() {
153 this.Y.on('dragenter', this.drag_enter, this.container, this);
154 this.Y.on('dragleave', this.drag_leave, this.container, this);
155 this.Y.on('dragover', this.drag_over, this.container, this);
156 this.Y.on('drop', this.drop, this.container, this);
160 * Initialise whole-page events (to show / hide the 'drop files here'
163 init_page_events: function() {
164 this.Y.on('dragenter', this.drag_enter_page, 'body', this);
165 this.Y.on('dragleave', this.drag_leave_page, 'body', this);
169 * Check if the filemanager / filepicker is disabled
170 * @return bool - true if disabled
172 is_disabled: function() {
173 return this.container.hasClass('disabled');
177 * Show the 'drop files here' message when file(s) are dragged
180 drag_enter_page: function(e) {
181 if (this.is_disabled()) {
184 if (!this.has_files(e)) {
188 this.pageentercount++;
189 if (this.pageentercount >= 2) {
190 this.pageentercount = 2;
194 this.show_drop_target();
200 * Hide the 'drop files here' message when file(s) are dragged off
203 drag_leave_page: function(e) {
204 this.pageentercount--;
205 if (this.pageentercount == 1) {
208 this.pageentercount = 0;
210 this.hide_drop_target();
216 * Check if the drag contents are valid and then call
217 * preventdefault / stoppropagation to let the browser know
218 * we will handle this drag/drop
220 * @param e event object
221 * @return boolean true if a valid file drag event
223 check_drag: function(e) {
224 if (this.is_disabled()) {
227 if (!this.has_files(e)) {
238 * Handle a dragenter event, highlight the destination node
239 * when a suitable drag event occurs
241 drag_enter: function(e) {
242 if (!this.check_drag(e)) {
247 if (this.entercount >= 2) {
248 this.entercount = 2; // Just moved over a child element - nothing to do
252 // These lines are needed if the user has dragged something directly
253 // from application onto the 'fileupload' box, without crossing another
254 // part of the page first
255 this.pageentercount = 2;
256 this.show_drop_target();
258 this.show_upload_ready();
263 * Handle a dragleave event, Remove the highlight if dragged from
266 drag_leave: function(e) {
267 if (!this.check_drag(e)) {
272 if (this.entercount == 1) {
273 return false; // Just moved over a child element - nothing to do
277 this.hide_upload_ready();
282 * Handle a dragover event. Required to intercept to prevent the browser from
283 * handling the drag and drop event as normal
285 drag_over: function(e) {
286 if (!this.check_drag(e)) {
294 * Handle a drop event. Remove the highlight and then upload each
295 * of the files (until we reach the file limit, or run out of files)
298 if (!this.check_drag(e, true)) {
303 this.pageentercount = 0;
304 this.hide_upload_ready();
305 this.hide_drop_target();
307 var files = e._event.dataTransfer.files;
308 if (this.filemanager) {
311 options: this.options,
312 repositoryid: this.repositoryid,
313 currentfilecount: this.filemanager.filecount, // All files uploaded.
314 currentfiles: this.filemanager.options.list, // Only the current folder.
315 callback: Y.bind('update_filemanager', this)
317 var uploader = new dnduploader(options);
318 uploader.start_upload();
320 if (files.length >= 1) {
323 options: this.options,
324 repositoryid: this.repositoryid,
327 callback: Y.bind('callback', this)
329 uploader = new dnduploader(options);
330 uploader.start_upload();
338 * Check to see if the drag event has any files in it
340 * @param e event object
341 * @return boolean true if event has files
343 has_files: function(e) {
344 var types = e._event.dataTransfer.types;
345 for (var i=0; i<types.length; i++) {
346 if (types[i] == 'Files') {
354 * Highlight the area where files could be dropped
356 show_drop_target: function() {
357 this.container.addClass('dndupload-ready');
360 hide_drop_target: function() {
361 this.container.removeClass('dndupload-ready');
365 * Highlight the destination node (ready to drop)
367 show_upload_ready: function() {
368 this.container.addClass('dndupload-over');
372 * Remove highlight on destination node
374 hide_upload_ready: function() {
375 this.container.removeClass('dndupload-over');
379 * Tell the attached filemanager element (if any) to refresh on file
382 update_filemanager: function() {
383 if (this.filemanager) {
384 // update the filemanager that we've uploaded the files
385 this.filemanager.filepicker_callback();
390 var dnduploader = function(options) {
391 dnduploader.superclass.constructor.apply(this, arguments);
394 Y.extend(dnduploader, Y.Base, {
395 // The URL to send the upload data to.
396 api: M.cfg.wwwroot+'/repository/repository_ajax.php',
397 // Options passed into the filemanager/filepicker element.
399 // The function to call when all uploads complete.
401 // The list of files dropped onto the element.
403 // The ID of the 'upload' repository.
405 // Array of files already in the current folder (to check for name clashes).
407 // Total number of files already uploaded (to check for exceeding limits).
409 // The list of files to upload.
411 // This list of files with name clashes.
413 // Set to true if the user has clicked on 'overwrite all'.
415 // Set to true if the user has clicked on 'rename all'.
419 * Initialise the settings for the dnduploader
420 * @param object params - includes:
421 * options (copied from the filepicker / filemanager)
422 * repositoryid - ID of the upload repository
423 * callback - the function to call when uploads are complete
424 * currentfiles - the list of files already in the current folder in the filemanager
425 * currentfilecount - the total files already in the filemanager
426 * files - the list of files to upload
429 initializer: function(params) {
430 this.options = params.options;
431 this.repositoryid = params.repositoryid;
432 this.callback = params.callback;
433 this.currentfiles = params.currentfiles;
434 this.currentfilecount = params.currentfilecount;
436 this.initialise_queue(params.files);
440 * Entry point for starting the upload process (starts by processing any
443 start_upload: function() {
444 this.process_renames(); // Automatically calls 'do_upload' once renames complete.
448 * Display a message in a popup
449 * @param string msg - the message to display
450 * @param string type - 'error' or 'info'
452 print_msg: function(msg, type) {
453 var header = M.str.moodle.error;
454 if (type != 'error') {
455 type = 'info'; // one of only two types excepted
456 header = M.str.moodle.info;
459 this.msg_dlg_node = Y.Node.createWithFilesSkin(M.core_filepicker.templates.message);
460 this.msg_dlg_node.generateID();
462 this.msg_dlg = new Y.Panel({
463 srcNode : this.msg_dlg_node,
470 this.msg_dlg.plug(Y.Plugin.Drag,{handles:['#'+this.msg_dlg_node.get('id')+' .yui3-widget-hd']});
471 this.msg_dlg_node.one('.fp-msg-butok').on('click', function(e) {
477 this.msg_dlg.set('headerContent', header);
478 this.msg_dlg_node.removeClass('fp-msg-info').removeClass('fp-msg-error').addClass('fp-msg-'+type)
479 this.msg_dlg_node.one('.fp-msg-text').setContent(msg);
484 * Check the size of each file and add to either the uploadqueue or, if there
485 * is a name clash, the renamequeue
486 * @param FileList files - the files to upload
489 initialise_queue: function(files) {
490 this.uploadqueue = [];
491 this.renamequeue = [];
493 // Loop through the files and find any name clashes with existing files
495 for (i=0; i<files.length; i++) {
496 if (this.options.maxbytes > 0 && files[i].size > this.options.maxbytes) {
497 // Check filesize before attempting to upload
498 this.print_msg(M.util.get_string('uploadformlimit', 'moodle', files[i].name), 'error');
499 this.uploadqueue = []; // No uploads if one file is too big.
503 if (this.has_name_clash(files[i].name)) {
504 this.renamequeue.push(files[i]);
506 if (!this.add_to_upload_queue(files[i], files[i].name, false)) {
514 * Add a single file to the uploadqueue, whilst checking the maxfiles limit
515 * @param File file - the file to add
516 * @param string filename - the name to give the file on upload
517 * @param bool overwrite - true to overwrite the existing file
518 * @return bool true if added successfully
520 add_to_upload_queue: function(file, filename, overwrite) {
522 this.currentfilecount++;
524 if (this.options.maxfiles > 0 && this.currentfilecount > this.options.maxfiles) {
525 // Too many files - abort entire upload.
526 this.uploadqueue = [];
527 this.renamequeue = [];
528 this.print_msg(M.util.get_string('maxfilesreached', 'moodle', this.options.maxfiles), 'error');
531 this.uploadqueue.push({file:file, filename:filename, overwrite:overwrite});
536 * Take the next file from the renamequeue and ask the user what to do with
537 * it. Called recursively until the queue is empty, then calls do_upload.
540 process_renames: function() {
541 if (this.renamequeue.length == 0) {
542 // All rename processing complete - start the actual upload.
546 var multiplefiles = (this.renamequeue.length > 1);
548 // Get the next file from the rename queue.
549 var file = this.renamequeue.shift();
550 // Generate a non-conflicting name for it.
551 var newname = this.generate_unique_name(file.name);
553 // If the user has clicked on overwrite/rename ALL then process
554 // this file, as appropriate, then process the rest of the queue.
555 if (this.overwriteall) {
556 this.add_to_upload_queue(file, file.name, true);
557 this.process_renames();
560 if (this.renameall) {
561 this.add_to_upload_queue(file, newname, false);
562 this.process_renames();
566 // Ask the user what to do with this file.
569 var process_dlg_node;
571 process_dlg_node = Y.Node.createWithFilesSkin(M.core_filepicker.templates.processexistingfilemultiple);
573 process_dlg_node = Y.Node.createWithFilesSkin(M.core_filepicker.templates.processexistingfile);
575 var node = process_dlg_node;
577 var process_dlg = new Y.Panel({
579 headerContent: M.str.repository.fileexistsdialogheader,
587 process_dlg.plug(Y.Plugin.Drag,{handles:['#'+node.get('id')+' .yui3-widget-hd']});
589 // Overwrite original.
590 node.one('.fp-dlg-butoverwrite').on('click', function(e) {
593 self.add_to_upload_queue(file, file.name, true);
594 self.process_renames();
597 // Rename uploaded file.
598 node.one('.fp-dlg-butrename').on('click', function(e) {
601 self.add_to_upload_queue(file, newname, false);
602 self.process_renames();
605 // Cancel all uploads.
606 node.one('.fp-dlg-butcancel').on('click', function(e) {
611 // When we are at the file limit, only allow 'overwrite', not rename.
612 if (this.currentfilecount == this.options.maxfiles) {
613 node.one('.fp-dlg-butrename').setStyle('display', 'none');
615 node.one('.fp-dlg-butrenameall').setStyle('display', 'none');
619 // If there are more files still to go, offer the 'overwrite/rename all' options.
621 // Overwrite all original files.
622 node.one('.fp-dlg-butoverwriteall').on('click', function(e) {
625 this.overwriteall = true;
626 self.add_to_upload_queue(file, file.name, true);
627 self.process_renames();
630 // Rename all new files.
631 node.one('.fp-dlg-butrenameall').on('click', function(e) {
634 this.renameall = true;
635 self.add_to_upload_queue(file, newname, false);
636 self.process_renames();
639 node.one('.fp-dlg-text').setContent(M.util.get_string('fileexists', 'moodle', file.name));
640 process_dlg_node.one('.fp-dlg-butrename').setContent(M.util.get_string('renameto', 'repository', newname));
642 // Destroy the dialog once it has been hidden.
643 process_dlg.after('visibleChange', function(e) {
644 if (!process_dlg.get('visible')) {
645 process_dlg.destroy(true);
653 * Checks if there is already a file with the given name in the current folder
654 * or in the list of already uploading files
655 * @param string filename - the name to test
656 * @return bool true if the name already exists
658 has_name_clash: function(filename) {
659 // Check against the already uploaded files
661 for (i=0; i<this.currentfiles.length; i++) {
662 if (filename == this.currentfiles[i].filename) {
666 // Check against the uploading files that have already been processed
667 for (i=0; i<this.uploadqueue.length; i++) {
668 if (filename == this.uploadqueue[i].filename) {
676 * Adds _NUMBER to the end of the filename and increments this number until
677 * a unique name is found
678 * @param string filename
679 * @return string the unique filename generated
681 generate_unique_name: function(filename) {
682 // Split the filename into the basename + extension.
685 var dotpos = filename.lastIndexOf('.');
690 basename = filename.substr(0, dotpos);
691 extension = filename.substr(dotpos, filename.length);
694 // Look to see if the name already has _NN at the end of it.
696 var hasnumber = basename.match(/^(.*)_(\d+)$/);
697 if (hasnumber != null) {
698 // Note the current number & remove it from the basename.
699 number = parseInt(hasnumber[2]);
700 basename = hasnumber[1];
703 // Loop through increating numbers until a unique name is found.
707 newname = basename + '_' + number + extension;
708 } while (this.has_name_clash(newname));
714 * Upload the next file from the uploadqueue - called recursively after each
715 * upload is complete, then handles the callback to the filemanager/filepicker
716 * @param lastresult - the last result from the server
718 do_upload: function(lastresult) {
719 if (this.uploadqueue.length > 0) {
720 var filedetails = this.uploadqueue.shift();
721 this.upload_file(filedetails.file, filedetails.filename, filedetails.overwrite);
723 this.uploadfinished(lastresult);
728 * Run the callback to the filemanager/filepicker
730 uploadfinished: function(lastresult) {
731 this.callback(lastresult);
735 * Upload a single file via an AJAX call to the 'upload' repository. Automatically
736 * calls do_upload as each upload completes.
737 * @param File file - the file to upload
738 * @param string filename - the name to give the file
739 * @param bool overwrite - true if the existing file should be overwritten
741 upload_file: function(file, filename, overwrite) {
743 // This would be an ideal place to use the Y.io function
744 // however, this does not support data encoded using the
745 // FormData object, which is needed to transfer data from
746 // the DataTransfer object into an XMLHTTPRequest
747 // This can be converted when the YUI issue has been integrated:
748 // http://yuilibrary.com/projects/yui3/ticket/2531274
749 var xhr = new XMLHttpRequest();
751 xhr.onreadystatechange = function() { // Process the server response
752 if (xhr.readyState == 4) {
753 if (xhr.status == 200) {
754 var result = JSON.parse(xhr.responseText);
757 self.print_msg(result.error, 'error'); // TODO add filename?
758 self.uploadfinished();
760 // Only update the filepicker if there were no errors
761 if (result.event == 'fileexists') {
762 // Do not worry about this, as we only care about the last
763 // file uploaded, with the filepicker
764 result.file = result.newfile.filename;
765 result.url = result.newfile.url;
767 result.client_id = self.options.clientid;
770 self.do_upload(result); // continue uploading
772 self.print_msg(M.util.get_string('serverconnection', 'error'), 'error');
773 self.uploadfinished();
778 // Prepare the data to send
779 var formdata = new FormData();
780 formdata.append('action', 'upload');
781 formdata.append('repo_upload_file', file); // The FormData class allows us to attach a file
782 formdata.append('sesskey', M.cfg.sesskey);
783 formdata.append('repo_id', this.repositoryid);
784 formdata.append('itemid', this.options.itemid);
785 if (this.options.author) {
786 formdata.append('author', this.options.author);
788 if (this.options.filemanager) { // Filepickers do not have folders
789 formdata.append('savepath', this.options.filemanager.currentpath);
791 formdata.append('title', filename);
793 formdata.append('overwrite', 1);
796 // Accepted types can be either a string or an array, but an array is
797 // expected in the processing script, so make sure we are sending an array
798 if (this.options.acceptedtypes.constructor == Array) {
799 for (var i=0; i<this.options.acceptedtypes.length; i++) {
800 formdata.append('accepted_types[]', this.options.acceptedtypes[i]);
803 formdata.append('accepted_types[]', this.options.acceptedtypes);
806 // Send the file & required details
807 xhr.open("POST", this.api, true);
813 dnduploadhelper.init(Y, options);