Skip to content

Commit

Permalink
making jqgrid view generic for both collections and random lists
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhuang committed Jun 10, 2013
1 parent 741f3b2 commit 43ef015
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 30 deletions.
7 changes: 7 additions & 0 deletions deme_django/cms/templates/collection/show.html
Expand Up @@ -2,6 +2,10 @@
{% load item_tags %}
{% block content %}


{% listgridbox base_item_type item %}

{% comment %}
<script type="text/javascript">
function openDialog(name)
{
Expand Down Expand Up @@ -31,6 +35,8 @@
<input type="hidden" name="item" value="{{ cur_agent.pk }}" />
</form>



<table id="jqgrid_list_collection_{{ item.pk }}"></table>
<div id="jqgrid_pager_collection_{{ item.pk }}"></div>
<script type="text/javascript">
Expand Down Expand Up @@ -89,6 +95,7 @@
{% endif %}
});
</script>
{% endcomment %}

{% endblock content %}

2 changes: 1 addition & 1 deletion deme_django/cms/templates/item/list_bare.html
Expand Up @@ -37,7 +37,7 @@
<div class="accordion-heading">
<a href="#list-advanced" class="accordion-toggle" data-toggle="collapse"><i class="glyphicon glyphicon-cog"></i> Advanced</a>
</div>
<div id="list-advanced" class="accordion-body collapse {% if inactive %}in{% endif %}">
<div id="list-advanced" class="accordion-body collapse {% if inactive or collection %}in{% endif %}">
<div class="accordion-inner">
<div class="row">
<label class="col col-lg-3 control-label" for="collection">
Expand Down
82 changes: 82 additions & 0 deletions deme_django/cms/templates/templatetags/listgridbox.html
@@ -0,0 +1,82 @@
{% load item_tags %}
<table id="jqgrid_list{{identifier}}"></table>
<div id="jqgrid_pager{{identifier}}"></div>
<script type="text/javascript">

if (typeof(openDialog) == 'undefined') {
function openDialog(name)
{
var dialogBox = $("#" + name);
dialogBox.dialog({
autoOpen: false,
bgiframe: true,
modal: true,
close: function(event, ui) {dialogBox.dialog('destroy')}
});
dialogBox.dialog('open');
}
}

$(document).ready(function () {
$("#jqgrid_list{{identifier}}").jqGrid({
url: '{{url}}',
postData: {{post_data_json}},
datatype: "json",
colNames: {{col_names_json}},
colModel: {{col_model_json}},
rowNum: 10,
rowList: [10,20,50,100],
viewrecords: true,
pager: '#jqgrid_pager{{identifier}}',
height: "100%",
autowidth: true,
multiselect: true,
{% if collection %}
{% ifagentcan 'modify_membership' item %}
gridComplete: function(){
var ids = $("#jqgrid_list{{ identifier }}").getDataIDs();
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
var deleteButton = '<button type="button" onclick="$(\'#removememberform{{identifier}}\')[0].elements[0].value = '+id+'; $(\'#removememberform{{identifier}}\')[0].submit();"><span class="ui-icon ui-icon-trash"></span></button>';

$("#jqgrid_list{{ identifier }}").setRowData(id, {actions: deleteButton});
}
},
{% endifagentcan %}
{% endif %}
});
$("#jqgrid_list{{identifier}}").jqGrid('navGrid','#jqgrid_pager{{identifier}}',{edit:false,add:false,del:false});
$("#jqgrid_list{{identifier}}").jqGrid('navButtonAdd',"#jqgrid_pager{{identifier}}",{caption:"Columns",title:"Choose columns",buttonicon:"ui-icon-gear",onClickButton:function(){$("#jqgrid_list{{identifier}}").jqGrid('columnChooser',{});}});
{% if collection %}
{% ifagentcan 'modify_membership' collection %}
$("#jqgrid_list{{identifier}}").jqGrid('navButtonAdd',"#jqgrid_pager{{identifier}}",{caption:"Add",title:"Add an item to {% viewable_name collection %}",buttonicon:"ui-icon-circle-plus",onClickButton:function(){openDialog('addmember{{collection.pk}}');}})
{% endifagentcan %}

{% if cur_agent_in_collection %}
{% ifagentcan 'remove_self' collection %}
$("#jqgrid_list{{identifier}}").jqGrid('navButtonAdd',"#jqgrid_pager{{identifier}}",{caption:"Leave",title:"Remove my membership in {% viewable_name collection %}",buttonicon:"ui-icon-person",onClickButton:function(){$('#removeselfform{{identifier}}')[0].submit();}})
{% endifagentcan %}
{% else %}
{% ifagentcan 'add_self' collection %}
$("#jqgrid_list{{identifier}}").jqGrid('navButtonAdd',"#jqgrid_pager{{identifier}}",{caption:"Join",title:"Become a member of {% viewable_name collection %}",buttonicon:"ui-icon-person",onClickButton:function(){$('#addselfform{{identifier}}')[0].submit();}})
{% endifagentcan %}
{% endif %}
{% endif %}
});
</script>

{% if collection %}
<form id="removememberform{{identifier}}" method="post" enctype="multipart/form-data" action="{% url item_url viewer=viewer_name,action="removemember",noun=collection.pk %}?redirect={{ full_path|urlencode }}" style="display: none;">
<input type="hidden" name="item" value="" />
</form>

{% newmemberdialog %}

<form id="removeselfform{{identifier}}" method="post" enctype="multipart/form-data" action="{% url item_url viewer=viewer_name,action="removemember",noun=collection.pk %}?redirect={{ full_path|urlencode }}" class="item_form">
<input type="hidden" name="item" value="{{ cur_agent.pk }}" />
</form>

<form id="addselfform{{identifier}}" method="post" enctype="multipart/form-data" action="{% url item_url viewer=viewer_name,action="addmember",noun=collection.pk %}?redirect={{ full_path|urlencode }}" class="item_form">
<input type="hidden" name="item" value="{{ cur_agent.pk }}" />
</form>
{% endif %}
69 changes: 40 additions & 29 deletions deme_django/cms/templatetags/item_tags.py
Expand Up @@ -1193,19 +1193,30 @@ def metadata_action_notices(parser, token):


class ListGridBox(template.Node):
def __init__(self, item_type):
def __init__(self, item_type, collection_var=None):
self.item_type = template.Variable(item_type)
if collection_var:
self.collection_var = template.Variable(collection_var)
else:
self.collection_var = None

def __repr__(self):
return "<ListGridBox>"

def render(self, context):
import random

viewer = context['_viewer']
item_type = self.item_type.resolve(context)
filter_string = viewer.request.GET.get('filter', '')
q = viewer.request.GET.get('q', '')
inactive = viewer.request.GET.get('inactive', '0') == '1'
collection = viewer.request.GET.get('collection', '')
collection_item = None
if self.collection_var:
collection_item = self.collection_var.resolve(context)
collection = collection_item.pk
else:
collection = viewer.request.GET.get('collection', '')
fields = []
possible_abilities = all_possible_item_abilities(item_type)
for field in item_type._meta.fields:
Expand All @@ -1232,41 +1243,41 @@ def render(self, context):
post_data['searchString'] = q
if collection:
post_data['collection'] = collection
col_names.append('Actions')
col_model.append({'name': 'actions', 'index': 'actions', 'sortable': 'false'})
if inactive:
post_data['inactive'] = inactive
url = reverse('item_type_url', kwargs={'viewer': item_type.__name__.lower(), 'action': 'grid', 'format': 'json'})
r = []
r.append("""<table id="jqgrid_list"></table>""")
r.append("""<div id="jqgrid_pager"></div>""")
r.append("""<script type="text/javascript">""")
r.append("""$(document).ready(function () {""")
r.append(""" $("#jqgrid_list").jqGrid({""")
r.append(""" url: '%s',""" % url)
r.append(""" postData: %s,""" % simplejson.dumps(post_data))
r.append(""" datatype: "json",""")
r.append(""" colNames: %s,""" % simplejson.dumps(col_names))
r.append(""" colModel: %s,""" % simplejson.dumps(col_model))
r.append(""" rowNum: 10,""")
r.append(""" rowList: [10,20,50,100],""")
r.append(""" viewrecords: true,""")
r.append(""" pager: '#jqgrid_pager',""")
r.append(""" height: "100%",""")
r.append(""" autowidth: true,""")
#r.append(""" multiselect: true,""")
r.append(""" });""")
r.append(""" $("#jqgrid_list").jqGrid('navGrid','#jqgrid_pager',{edit:false,add:false,del:false});""")
r.append(""" $("#jqgrid_list").jqGrid('navButtonAdd',"#jqgrid_pager",{caption:"Columns",title:"Choose columns",buttonicon:"ui-icon-gear",onClickButton:function(){$("#jqgrid_list").jqGrid('columnChooser',{});}});""")
r.append("""});""")
r.append("""</script>""")
return '\n'.join(r)

if collection:
identifier = 'c' + str(collection)
else:
identifier = random.randint(0,100000)

data = context
data['post_data_json'] = simplejson.dumps(post_data)
data['col_names_json'] = simplejson.dumps(col_names)
data['col_model_json'] = simplejson.dumps(col_model)
data['identifier'] = identifier
data['url'] = url
data['collection'] = collection_item
data['item'] = collection_item
data.autoescape = False
t = template.loader.get_template('templatetags/listgridbox.html')
return t.render(data)


@register.tag
def listgridbox(parser, token):
bits = list(token.split_contents())
if len(bits) != 2:
raise template.TemplateSyntaxError, "%r takes one arguments" % bits[0]
return ListGridBox(bits[1])
collection = None
if len(bits) > 2:
collection = bits[2]
if len(bits) < 2:
raise template.TemplateSyntaxError, "%r takes one at least one argument" % bits[0]
if len(bits) > 3:
raise template.TemplateSyntaxError, "%r takes at most two argument" % bits[0]
return ListGridBox(bits[1], collection)


class SubclassFieldsBox(template.Node):
Expand Down
1 change: 1 addition & 0 deletions deme_django/cms/views.py
Expand Up @@ -1070,6 +1070,7 @@ def item_show_html(self):
self.context['action_title'] = ''
self.require_ability('view ', self.item, wildcard_suffix=True)
self.context['cur_agent_in_collection'] = self.item.child_memberships.filter(active=True, item=self.cur_agent).exists()
self.context['base_item_type'] = Item
template = loader.get_template('collection/show.html')
return HttpResponse(template.render(self.context))

Expand Down

0 comments on commit 43ef015

Please sign in to comment.