Skip to content

Commit

Permalink
Merge pull request #463 from webu/bugfix/js-m2m
Browse files Browse the repository at this point in the history
generic JS usable when a row is duplicated when file is a part of a ManyToMany
  • Loading branch information
yakky committed Jan 10, 2015
2 parents 31b78d8 + 7b5fcc1 commit ffc3480
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
5 changes: 4 additions & 1 deletion filer/fields/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def obj_for_value(self, value):
return obj

class Media:
js = (filer_settings.FILER_STATICMEDIA_PREFIX + 'js/popup_handling.js',)
js = (
filer_settings.FILER_STATICMEDIA_PREFIX + 'js/popup_handling.js',
filer_settings.FILER_STATICMEDIA_PREFIX + 'js/widget.js',
)


class AdminFileFormField(forms.ModelChoiceField):
Expand Down
2 changes: 1 addition & 1 deletion filer/static/filer/js/popup_handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
document.getElementById(id_name).innerHTML = chosenName;
win.close();
};
})(jQuery);
})(django.jQuery);
20 changes: 20 additions & 0 deletions filer/static/filer/js/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function($) {
var filer_clear = function(e){
var clearer = $(this),
hidden_input = clearer.closest('.filerFile').find('input.vForeignKeyRawIdAdminField'),
base_id = '#'+hidden_input.attr('id'),
thumbnail = $(base_id+'_thumbnail_img'),
description = $(base_id+'_description_txt'),
static_prefix = clearer.attr('src').replace('admin/img/icon_deletelink.gif', 'filer/');
clearer.hide();
hidden_input.removeAttr("value");
thumbnail.attr("src", static_prefix+"icons/nofile_48x48.png");
description.html("");
}

$(document).ready(function(){
$('.filerFile .vForeignKeyRawIdAdminField').attr('type', 'hidden');
//if this file is included multiple time, we ensure that filer_clear is attached only once.
$(document).off('click.filer', '.filerFile .filerClearer', filer_clear).on('click.filer', '.filerFile .filerClearer', filer_clear);
});
})(django.jQuery);
27 changes: 12 additions & 15 deletions filer/templates/admin/filer/widgets/admin_file.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load i18n filer_admin_tags %}{% spaceless %}
<span class="filerFile">
{% if object %}
{% if object.icons.32 %}
<a href="{{ object.url }}" target="_blank"><img id="{{ thumb_id }}" src="{{ object.icons.48 }}" alt="{{ object.label }}" /></a>
Expand All @@ -14,22 +15,18 @@
<a href="{{ lookup_url }}" class="related-lookup" id="lookup_id_{{ lookup_name }}" title="{% trans 'Lookup' %}" onclick="return showRelatedObjectLookupPopup(this);">
<img src="{% admin_icon_base %}icon_searchbox.png" width="16" height="16" alt="{% trans 'Lookup' %}" />
</a>
<img id="{{ clear_id }}" src="{% admin_icon_base %}icon_deletelink.gif" width="10" height="10" alt="{% trans 'Clear' %}" title="{% trans 'Clear' %}"{% if not object %} style="display: none;"{% endif %} />
<img id="{{ clear_id }}" class="filerClearer" src="{% admin_icon_base %}icon_deletelink.gif" width="10" height="10" alt="{% trans 'Clear' %}" title="{% trans 'Clear' %}"{% if not object %} style="display: none;"{% endif %} />
<br />
{{ hidden_input }}
<script type="text/javascript">
django.jQuery("#{{ id }}").hide();
django.jQuery("#{{ clear_id }}").click(function(){
django.jQuery("#{{ id }}").removeAttr("value");
django.jQuery("#{{ thumb_id }}").attr("src", "{{ filer_static_prefix }}icons/nofile_48x48.png");
django.jQuery("#{{ span_id }}").html("");
django.jQuery("#{{ clear_id }}").hide();
});
django.jQuery(document).ready(function(){
var plus = django.jQuery("#add_{{ id }}");
if (plus.length){
plus.remove();
}
});
<script type="text/javascript" id="{{id}}_javascript">
django.jQuery(document).ready(function(){
var plus = django.jQuery("#add_{{ id }}");
if (plus.length){
plus.remove();
}
{# Delete this javascript once loaded to avoid the "add new" link duplicates it #}
django.jQuery('#{{id}}_javascript').remove();
});
</script>
</span>
{% endspaceless %}

0 comments on commit ffc3480

Please sign in to comment.