diff static/js/markitup/sets/markdown/set.js @ 722:71d17d267e27

Added an ajax form to upload photos & update post box w/image code.
author Brian Neal <bgneal@gmail.com>
date Sat, 21 Sep 2013 17:00:49 -0500
parents ad69236e8501
children 682b159f3763
line wrap: on
line diff
--- a/static/js/markitup/sets/markdown/set.js	Thu Sep 19 20:00:10 2013 -0500
+++ b/static/js/markitup/sets/markdown/set.js	Sat Sep 21 17:00:49 2013 -0500
@@ -126,4 +126,55 @@
        }
        return false;
    });
+
+   var $photoProgress = $('#photo-upload-progress');
+   var $photoForm = $('#photo-upload-form');
+   var $postBox = $('#id_body');
+   var $photoUploadSubmit = $('#photo-upload-submit');
+
+   $photoForm.ajaxForm({
+      beforeSubmit: function(arr, $form, options) {
+         var fileObj = null;
+         $.each(arr, function(index, val) {
+            if (val.name == 'image_file') {
+               fileObj = val.value;
+            }
+         });
+         if (!fileObj) {
+            alert("Please choose a file to upload.");
+            return false;
+         }
+         $photoUploadSubmit.attr('disabled', 'disabled').val('Uploading...');
+         return true;
+      },
+      beforeSend: function() {
+         $photoProgress.progressbar({value: 0});
+      },
+      uploadProgress: function(event, position, total, percentComplete) {
+         if (percentComplete < 100) {
+            $photoProgress.progressbar({value: percentComplete});
+         }
+         else {
+            $photoProgress.progressbar({value: false});
+         }
+      },
+      success: function(resp, statusText, xhr, $form) {
+         $photoProgress.progressbar({value: 100});
+         if (resp.success) {
+            $postBox.val($postBox.val() + '\n![image](' + resp.url + ')');
+            alert("Success! The image code was added to your post.");
+         }
+         else {
+            alert('Error: ' + resp.msg);
+         }
+      },
+      complete: function(xhr) {
+         $photoProgress.progressbar({value: 0});
+         $photoForm.clearForm();
+         $photoUploadSubmit.removeAttr('disabled').val('Upload photo');
+      },
+      error: function(xhr, textStatus, ex) {
+         alert('Oops, there was an error: ' + ex);
+      }
+   });
 });