Skip to content

Commit

Permalink
lazy loading of the menu on page change list
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewschoen committed Dec 16, 2011
1 parent d1f5a55 commit e71cd74
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 5 deletions.
15 changes: 15 additions & 0 deletions cms/admin/pageadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def get_urls(self):
pat(r'^([0-9]+)/remove-delete-state/$', self.remove_delete_state),
pat(r'^([0-9]+)/dialog/copy/$', get_copy_dialog), # copy dialog
pat(r'^([0-9]+)/preview/$', self.preview_page), # copy dialog
pat(r'^([0-9]+)/load-nav/$', self.load_nav), # lazy load navigation
pat(r'^(?P<object_id>\d+)/change_template/$', self.change_template), # copy dialog
)

Expand Down Expand Up @@ -683,6 +684,7 @@ def changelist_view(self, request, extra_context=None):
'has_recover_permission': 'reversion' in settings.INSTALLED_APPS and self.has_recover_permission(request),
'DEBUG': settings.DEBUG,
'site_languages': languages,
'CMS_LAZY_LOAD_MENU': settings.CMS_LAZY_LOAD_MENU,
}
if 'reversion' in settings.INSTALLED_APPS:
context['has_change_permission'] = self.has_change_permission(request)
Expand Down Expand Up @@ -1065,6 +1067,19 @@ def change_innavigation(self, request, page_id):
return admin_utils.render_admin_menu_item(request, page)
return HttpResponseForbidden(_("You do not have permission to change this page's in_navigation status"))

def load_nav(self, request, page_id):
"""
Lazy load navigation tree for given page
"""
page = get_object_or_404(Page, pk=page_id)
return admin_utils.render_admin_menu_item(request, page,
template="admin/cms/page/lazy_menu.html")
# return render_to_response("admin/cms/page/lazy_menu.html", {
# "page":page,
# "children":page.children.all(),
# "filtered":False, #need to actually set this
# }, context_instance=RequestContext(request))

@create_on_success
def add_plugin(self, request):
'''
Expand Down
3 changes: 2 additions & 1 deletion cms/conf/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,5 @@

# they are missing in the permission-merge2 branch
CMS_PLUGIN_PROCESSORS = tuple()
CMS_PLUGIN_CONTEXT_PROCESSORS = tuple()
CMS_PLUGIN_CONTEXT_PROCESSORS = tuple()
CMS_LAZY_LOAD_MENU = False
2 changes: 1 addition & 1 deletion cms/static/cms/js/change_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
theme_name: "default",
a_class: "title"
},
cookies : {},
cookies : MENU_COOKIES,
callback: {
beforemove : function(what, where, position, tree) {
item_id = what.id.split("page_")[1];
Expand Down
17 changes: 17 additions & 0 deletions cms/static/cms/js/libs/lazyloadmenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(function($) {
$(function() {
admin_base_url = document.URL.split("/cms/page/")[0] + "/";
$(".tree-default li").click(function(event) {
var target = event.target;
var page_id = $(target).attr("id").split("page_")[1];
var load_nav_url = admin_base_url + "cms/page/" + page_id + "/load-nav/";
if($(target).hasClass("closed")) {
if($(target).children('ul').children('li').length == 0) {
$.get(load_nav_url, {}, function(r, status) {
$(target).children('ul').append(r);
});
}
}
});
});
})(jQuery.noConflict());
8 changes: 8 additions & 0 deletions cms/templates/admin/cms/page/change_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

{% block coltype %}flex{% endblock %}
{% block extrahead %}
<script type="text/javascript">
{% if CMS_LAZY_LOAD_MENU %}
MENU_COOKIES = false;
{% else %}
MENU_COOKIES = true;
{% endif %}
</script>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}cms/css/pages.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}cms/jstree/tree_component.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}cms/css/jquery.dialog.css" />
Expand All @@ -34,6 +41,7 @@
<script type="text/javascript" src="{{ STATIC_URL }}cms/js/libs/classy.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}cms/js/plugins/cms.base.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}cms/js/change_list.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}cms/js/libs/lazyloadmenu.js"></script>


{% if cl.is_filtered %}
Expand Down
12 changes: 12 additions & 0 deletions cms/templates/admin/cms/page/lazy_child_menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% load cms_admin i18n adminmedia %}<li id="page_{{page.pk}}" class="{% if cl.is_filtered %}leaf{% endif %}{% if has_move_page_permission %} moveable{% endif %}{%if page.children.all %} closed{% endif %}"{% if metadata %} mdata="{{ metadata }}{% endif %}" rel="{% ifequal page.level 0 %}topnode{% else %}node{% endifequal %}">

{% include 'admin/cms/page/menu_item.html' %}

{% with page.children as children %}
{% if children %}<ul{% if page.last %} class="last"{% endif %}>{% for child in children.all %}
{# show_lazy_admin_menu child #}{% endfor %}
</ul>{% endif %}
{% endwith %}
</li>


8 changes: 8 additions & 0 deletions cms/templates/admin/cms/page/lazy_menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% load cms_admin i18n adminmedia %}
{% with page.children.all as children %}
{% if children %}
{% for child in children %}
{% show_lazy_admin_menu child %}
{% endfor %}
{% endif %}
{% endwith %}
5 changes: 4 additions & 1 deletion cms/templates/admin/cms/page/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

{% with page.childrens as children %}
{% if children %}<ul{% if page.last %} class="last"{% endif %}>{% for child in children %}
{% show_admin_menu child %}{% endfor %}
{% if not CMS_LAZY_LOAD_MENU %}
{% show_admin_menu child %}
{% endif %}
{% endfor %}
</ul>{% endif %}
{% endwith %}
</li>
Expand Down
33 changes: 33 additions & 0 deletions cms/templatetags/cms_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ def get_context(self, context, page):
return context
register.tag(ShowAdminMenu)

class ShowLazyAdminMenu(InclusionTag):
name = 'show_lazy_admin_menu'
template = 'admin/cms/page/lazy_child_menu.html'

options = Options(
Argument('page')
)

def get_context(self, context, page):
request = context['request']


if context.has_key("cl"):
filtered = context['cl'].is_filtered()
elif context.has_key('filtered'):
filtered = context['filtered']



# following function is newly used for getting the context per item (line)
# if something more will be required, then get_admin_menu_item_context
# function have to be updated.
# This is done because item can be reloaded after some action over ajax.
context.update(get_admin_menu_item_context(request, page, filtered))

# this here is just context specific for menu rendering - items itself does
# not use any of following variables
#context.update({
# 'no_children': no_children,
#})
return context
register.tag(ShowLazyAdminMenu)


class CleanAdminListFilter(InclusionTag):
"""
Expand Down
7 changes: 5 additions & 2 deletions cms/utils/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ def get_admin_menu_item_context(request, page, filtered=False):
return context


def render_admin_menu_item(request, page):
def render_admin_menu_item(request, page, template=None):
"""
Renders requested page item for the tree. This is used in case when item
must be reloaded over ajax.
"""
if not template:
template = "admin/cms/page/menu_item.html"

if not page.pk:
return HttpResponse(NOT_FOUND_RESPONSE) # Not found - tree will remove item

Expand All @@ -93,4 +96,4 @@ def render_admin_menu_item(request, page):

filtered = 'filtered' in request.REQUEST
context.update(get_admin_menu_item_context(request, page, filtered))
return render_to_response('admin/cms/page/menu_item.html', context)
return render_to_response(template, context)
8 changes: 8 additions & 0 deletions docs/getting_started/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ Default: ``True``
If set to ``False``, frontend editing is not available for models using
:class:`cms.models.fields.PlaceholderField`.

CMS_LAZY_LOAD_MENU
==================

Default: ``False``

If set to ``True``, it will enabled lazy loading of the menu in the page
change list in the admin. This is useful for sites with a lot of menu items as loading a large menu tree affects site performance

********************
Editor configuration
********************
Expand Down

0 comments on commit e71cd74

Please sign in to comment.