Pull out filepicker JS code #312
authorCharles Connell <charles@connells.org>
Tue, 4 Feb 2014 22:21:42 +0000 (17:21 -0500)
committerCharles Connell <charles@connells.org>
Fri, 7 Feb 2014 21:47:13 +0000 (16:47 -0500)
karmaworld/assets/js/filepicker.js [new file with mode: 0644]
karmaworld/templates/courses/course_form.html [deleted file]
karmaworld/templates/partial/filepicker.html

diff --git a/karmaworld/assets/js/filepicker.js b/karmaworld/assets/js/filepicker.js
new file mode 100644 (file)
index 0000000..7bd853d
--- /dev/null
@@ -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($('<li>', {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 (file)
index e69de29..0000000
index d00e2f8ab3de63f13916f9ed19a0a10fb90b75f9..b699ada9db072efe0e74e9583441a38c853f4924 100644 (file)
   </div>
 
   <script>
-    var uploaded_files = new Array();
-    // 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');
-
-
-    /*
-     *  load the file form template and append it to the form container
-     *  takes a fileupload event
-     */
-    var 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();
-    };
-
-    $('#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('{% url 'upload_post' %}', {
-            '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($('<li>', {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_authenticated %}
-                setTimeout(function(){
-                  location.reload(true);
-                }, 20000);
-              {% endif %}
-            }
-          });
-          // Add the name we've just uploaded to the list
-          uploaded_files.push(name);
-      });
-    });
-    
-    var got_file = function(event){
-      $dropzone_result.text(event);
-        for (var i=0; i < event.fpfiles.length; i++){
-          makeFileForm(event.fpfiles[i]);
-        }
-    };
+    var upload_post_url = '{% url 'upload_post' %}';
+    {% if user.is_authenticated %}
+      var user_is_authenicated = true;
+    {% else %}
+      var user_is_authenicated = false;
+    {% endif %}
   </script>
+  <script src="{{ STATIC_URL }}js/filepicker.js"></script>
 
 
 </section>