Skip to content

Commit

Permalink
Updated the code so it will work with or without Grappelli installed
Browse files Browse the repository at this point in the history
  • Loading branch information
epicserve committed Aug 27, 2012
1 parent 85fb208 commit ff6043d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 29 deletions.
2 changes: 2 additions & 0 deletions categories/editor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
MEDIA_PATH = getattr(settings, 'EDITOR_MEDIA_PATH', '%seditor/' % STATIC_URL)

TREE_INITIAL_STATE = getattr(settings, 'EDITOR_TREE_INITIAL_STATE', 'collapsed')

IS_GRAPPELLI_INSTALLED = True if 'grappelli' in settings.INSTALLED_APPS else False
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends "admin/change_list.html" %}
{% load adminmedia admin_list i18n admin_tree_list %}
{% block extrahead %}
{{block.super}}
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var srchString = document.getElementById('grp-changelist-search').value;
if (srchString == '')
treeTable = $("#result_list").treeTable({initialState : "{{ EDITOR_TREE_INITIAL_STATE }}"});
else
treeTable = $("#result_list").treeTable({initialState : "expanded"});
function toggleChildren(index, value) {
var row = value.parentNode.parentNode;
if (row.className.match(/child-of-node-\d+/)) {
value.checked = /child-of-node-(\d+)/.exec(row.className);
}
if($(row).hasClass("parent")) {
$("table.treeTable tbody tr.child-of-" + row.id + " input").each(toggleChildren);
}
$("tr input.action-select").actions();
};
$('.action-select').each(
function(){
$(this).bind('click', function(event){
// Get the tr from checkbox -> td -> tr
var row = event.currentTarget.parentNode.parentNode;
$("table.treeTable tbody tr.child-of-" + row.id + " input").each(toggleChildren);
});
});
});
})(django.jQuery);
</script>
{% endblock %}
{% block result_list %}
{% result_tree_list cl %}
{% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% if result_hidden_fields %}
<div class="hiddenfields"> {# DIV for HTML validation #}
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
</div>
{% endif %}

{% if results %}
<div class="grp-module grp-changelist-results">
<table cellspacing="0" id="result_list">
<thead>
<tr>
{% for header in result_headers %}
<th scope="col" {{ header.class_attrib }}>
{% if header.sortable %}<a href="{{ header.url }}">{% endif %}
{{ header.text|capfirst }}
{% if header.sortable %}</a>{% endif %}
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for result in results %}
<tr id="node-{{ result.pk }}" class="grp-row {% cycle 'grp-row-even' 'grp-row-odd' %}{% if result.parent_pk %} child-of-node-{{result.parent_pk}}{% endif %}">{% for item in result %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
5 changes: 3 additions & 2 deletions categories/editor/templates/admin/editor/tree_editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var srchString = document.getElementById('grp-changelist-search').value;
var srchString = document.getElementById('searchbar').value;
if (srchString == '')
treeTable = $("#result_list").treeTable({initialState : "{{ EDITOR_TREE_INITIAL_STATE }}"});
else
Expand Down Expand Up @@ -33,6 +33,7 @@
</script>
{% endblock %}
{% block result_list %}
{% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
{% result_tree_list cl %}
{% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
{% endblock %}
{% endblock %}
42 changes: 19 additions & 23 deletions categories/editor/templates/admin/editor/tree_list_results.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
{% if result_hidden_fields %}
<div class="hiddenfields"> {# DIV for HTML validation #}
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
</div>
<div class="hiddenfields"> {# DIV for HTML validation #}
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
</div>
{% endif %}

{% if results %}
<div class="grp-module grp-changelist-results">
<table cellspacing="0" id="result_list">
<thead>
<tr>
{% for header in result_headers %}
<th scope="col" {{ header.class_attrib }}>
{% if header.sortable %}<a href="{{ header.url }}">{% endif %}
{{ header.text|capfirst }}
{% if header.sortable %}</a>{% endif %}
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for result in results %}
<tr id="node-{{ result.pk }}" class="grp-row {% cycle 'grp-row-even' 'grp-row-odd' %}{% if result.parent_pk %} child-of-node-{{result.parent_pk}}{% endif %}">{% for item in result %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
</table>
<div class="results">
<table cellspacing="0" id="result_list">
<thead>
<tr>
{% for header in result_headers %}<th{{ header.class_attrib }}>
{% if header.sortable %}<a href="{{ header.url }}">{% endif %}
{{ header.text|capfirst }}
{% if header.sortable %}</a>{% endif %}</th>{% endfor %}
</tr>
</thead>
<tbody>
{% for result in results %}
<tr id="node-{{ result.pk }}" class="{% cycle 'row1' 'row2' %}{% if result.parent_pk %} child-of-node-{{result.parent_pk}}{% endif %}">{% for item in result %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
8 changes: 7 additions & 1 deletion categories/editor/templatetags/admin_tree_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
from django.utils.html import escape, conditional_escape
from django.utils.safestring import mark_safe

from categories.editor import settings

register = Library()

TREE_LIST_RESULTS_TEMPLATE = 'admin/editor/tree_list_results.html'
if settings.IS_GRAPPELLI_INSTALLED:
TREE_LIST_RESULTS_TEMPLATE = 'admin/editor/grappelli_tree_list_results.html'


def items_for_tree_result(cl, result, form):
"""
Expand Down Expand Up @@ -145,4 +151,4 @@ def result_tree_list(cl):
from django.contrib.admin.templatetags.admin_list import result_hidden_fields
result['result_hidden_fields'] = list(result_hidden_fields(cl))
return result
result_tree_list = register.inclusion_tag("admin/editor/tree_list_results.html")(result_tree_list)
result_tree_list = register.inclusion_tag(TREE_LIST_RESULTS_TEMPLATE)(result_tree_list)
11 changes: 8 additions & 3 deletions categories/editor/tree_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,15 @@ def __init__(self, *args, **kwargs):
self.list_display.remove('action_checkbox')

opts = self.model._meta

grappelli_prefix = ""
if settings.IS_GRAPPELLI_INSTALLED:
grappelli_prefix = "grappelli_"

self.change_list_template = [
'admin/%s/%s/editor/tree_editor.html' % (opts.app_label, opts.object_name.lower()),
'admin/%s/editor/tree_editor.html' % opts.app_label,
'admin/editor/tree_editor.html',
'admin/%s/%s/editor/%stree_editor.html' % (opts.app_label, opts.object_name.lower(), grappelli_prefix),
'admin/%s/editor/%stree_editor.html' % (opts.app_label, grappelli_prefix),
'admin/editor/%stree_editor.html' % grappelli_prefix,
]

def get_changelist(self, request, **kwargs):
Expand Down

0 comments on commit ff6043d

Please sign in to comment.