From 9c5a3bddeafbb20c7c689c01d1b5faa8a972bbeb Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Tue, 4 Feb 2014 17:21:42 -0500 Subject: [PATCH] Pull out filepicker JS code #312 --- karmaworld/assets/js/filepicker.js | 104 +++++++++++++++++ karmaworld/templates/courses/course_form.html | 0 karmaworld/templates/partial/filepicker.html | 107 ++---------------- 3 files changed, 111 insertions(+), 100 deletions(-) create mode 100644 karmaworld/assets/js/filepicker.js delete mode 100644 karmaworld/templates/courses/course_form.html diff --git a/karmaworld/assets/js/filepicker.js b/karmaworld/assets/js/filepicker.js new file mode 100644 index 0000000..7bd853d --- /dev/null +++ b/karmaworld/assets/js/filepicker.js @@ -0,0 +1,104 @@ + +/* + * load the file form template and append it to the form container + * takes a fileupload event + */ +makeFileForm = function(upFile) { + var _form = document.getElementById('form-template').cloneNode(deep=true); + // save the Filename to the form name field + $(_form.children[0].children[0].children[1]).val(upFile.filename); // replace with upFile name + _form.style.display = "inline"; + _form.id = null; // clear the unique id + // save the FP url to the form + $(_form.children[0].children[3].children[0]).val(upFile.url); + // save the mimetype to the form + $(_form.children[0].children[3].children[1]).val(upFile.mimetype); + + document.getElementById('forms_container').appendChild(_form); + + if (document.location.host === 'www.karmanotes.org' || + document.location.host === 'karmanotes.org') { + _gat._getTracker()._trackEvent('upload', 'filepicker file drop'); + } + + $('.remove').on('click', function(e){ + e.stopPropagation(); + $(this).parent().parent().remove(); + }); + + $('#save-btn').show(); +} + +var uploaded_files = new Array(); + +$(function(){ + // these are obsolete without the drag-drop widget that we removed from the partial above + // var $dropzone = $('#filepicker_dropzone'); + var $dropzone_result = $('#filepicker_dropzone_result'); + + $('#save-btn').on('click', function(e){ + e.stopPropagation(); + $(this).unbind('click'); + $(this).addClass('disabled'); + + var saveIcon = $('#save-btn-icon'); + saveIcon.removeClass('fa-save'); + saveIcon.addClass('fa-spinner fa-spin'); + + $('#forms_container .inline-form').each(function(i,el){ + var name, tags, fpurl, course; + name = $(el).find('.intext').val(); + fp_file = $(el).find('.fpurl').val(); + tags = $(el).find('.taggit-tags').val(); + course = $(el).find('.course_id').val(); + csrf = $(el).find('.csrf').val(); + email = $('#id_email').val(); + mimetype = $(el).find('.mimetype').val(); + + $.post(upload_post_url, { + 'name': name, + 'fp_file': fp_file, + 'tags': tags, + 'course': course, + 'csrfmiddlewaretoken': csrf, + 'mimetype': mimetype, + 'email': email + }, function(data){ + if (data === 'success') { + // For multiple uploads, we may end up clearing and re- + // writing this multiple times, but show the same list + // each time. + $('#uploaded_files').empty(); + for (var i=0; i < uploaded_files.length; i++) { + $('#uploaded_files').append($('
  • ', {text: uploaded_files[i]})); + } + $('#thank-points').html(uploaded_files.length*5); + $('#success').show(); + $('#save-btn').hide(); + $('#filepicker_row').hide(); + $('#forms_container .inline-form').remove(); + $('#forms_container').hide(); + if (document.location.host === 'www.karmanotes.org' || + document.location.host === 'karmanotes.org') { + _gat._getTracker()._trackEvent('upload', 'upload form submitted'); + } + if (user_is_authenicated) { + setTimeout(function(){ + location.reload(true); + }, 20000); + } + } + }); + // Add the name we've just uploaded to the list + uploaded_files.push(name); + }); + }); + +}); + +var got_file = function(event){ + $('#filepicker_dropzone_result').text(event); + for (var i=0; i < event.fpfiles.length; i++){ + makeFileForm(event.fpfiles[i]); + } +}; diff --git a/karmaworld/templates/courses/course_form.html b/karmaworld/templates/courses/course_form.html deleted file mode 100644 index e69de29..0000000 diff --git a/karmaworld/templates/partial/filepicker.html b/karmaworld/templates/partial/filepicker.html index d00e2f8..b699ada 100644 --- a/karmaworld/templates/partial/filepicker.html +++ b/karmaworld/templates/partial/filepicker.html @@ -72,107 +72,14 @@ + -- 2.25.1