Skip to content

add hideInput option #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions ajax_upload/static/ajax_upload/js/ajax-upload-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var AjaxUploadWidget = global.AjaxUploadWidget = function(element, options) {
this.options = {
hideInput: false,
changeButtonText: 'Change',
removeButtonText: 'Remove',
previewAreaClass: 'ajax-upload-preview-area',
Expand Down Expand Up @@ -48,8 +49,14 @@
this.$changeButton = $('<button type="button" class="btn-change"></button>')
.text(this.options.changeButtonText)
.on('click', function(evt) {
self.$element.show();
$(this).hide();
if(self.options.hideInput){
self.$element.hide();
self.$element.trigger('click');
}
else{
self.$element.show();
$(this).hide();
}
});
this.$element.after(this.$changeButton);

Expand Down Expand Up @@ -133,9 +140,15 @@
this.$element.hide();
} else {
this.$previewArea.slideUp();
this.$changeButton.hide();
this.$removeButton.hide();
this.$element.show();
if(this.options.hideInput){
this.$changeButton.show();
this.$element.hide();
}
else{
this.$changeButton.hide();
this.$element.show();
}
}
};

Expand Down
3 changes: 3 additions & 0 deletions ajax_upload/widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import forms
from django.conf import settings
from django.core.files import File
from django.core.files.storage import DefaultStorage
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
Expand Down Expand Up @@ -43,6 +44,8 @@ def value_from_datadict(self, data, files, name):
elif file_path.startswith(settings.MEDIA_URL):
# Strip and media url to determine the path relative to media root
relative_path = file_path[len(settings.MEDIA_URL):]
if not relative_path.startswith('ajax_uploads'):
return None
try:
uploaded_file = UploadedFile.objects.get(file=relative_path)
except UploadedFile.DoesNotExist:
Expand Down