[gen] Optimized Ref: do not ajax-call the Ref view macro when displaying a complete page; added Ref.changeOrder to enable/disable (independently from write permission) the possibility to sort or change order of referred objects.

This commit is contained in:
Gaetan Delannay 2012-10-07 11:26:55 +02:00
parent 82a8ac99b4
commit 872b77208b
2 changed files with 19 additions and 22 deletions

View file

@ -1766,7 +1766,7 @@ class Ref(Type):
maxChars=None, colspan=1, master=None, masterValue=None,
focus=False, historized=False, mapping=None, label=None,
queryable=False, queryFields=None, queryNbCols=1,
navigable=False, searchSelect=None):
navigable=False, searchSelect=None, changeOrder=True):
self.klass = klass
self.attribute = attribute
# May the user add new objects through this ref ?
@ -1839,6 +1839,9 @@ class Ref(Type):
# in the search screen. Those values are returned by self.searchSelect,
# which must be a static method accepting the tool as single arg.
self.searchSelect = searchSelect
# If changeOrder is False, it even if the user has the right to modify
# the field, it will not be possible to move objects or sort them.
self.changeOrder = changeOrder
Type.__init__(self, validator, multiplicity, index, default, optional,
editDefault, show, page, group, layouts, move, indexed,
False, specificReadPermission, specificWritePermission,
@ -2084,6 +2087,13 @@ class Ref(Type):
raise Unauthorized("User can't write Ref field '%s' (%s)." % \
(self.name, may.msg))
def changeOrderEnabled(self, obj):
'''Is changeOrder enabled?'''
if isinstance(self.changeOrder, bool):
return self.changeOrder
else:
return self.callMethod(obj, self.changeOrder)
def autoref(klass, field):
'''klass.field is a Ref to p_klass. This kind of auto-reference can't be
declared in the "normal" way, like this:

View file

@ -22,7 +22,7 @@
<table class="noStyle">
<tr>
<tal:comment replace="nothing">Arrows for moving objects up or down</tal:comment>
<td tal:condition="python: not appyType['isBack'] and (len(objs)&gt;1) and context.allows('Modify portal content')">
<td tal:condition="python: not appyType['isBack'] and (len(objs)&gt;1) and changeOrder and context.allows('Modify portal content')">
<tal:moveRef define="objectIndex python: contextObj.getAppyRefIndex(fieldName, obj);
ajaxBaseCall python: navBaseCall.replace('**v**', '\'%s\',\'ChangeRefOrder\', {\'refObjectUid\':\'%s\', \'move\':\'**v**\'}' % (startNumber, obj.UID()))">
<tal:comment replace="nothing">Move up</tal:comment>
@ -80,7 +80,7 @@
</tal:comment>
<metal:sortIcons define-macro="sortIcons"
tal:define="ajaxBaseCall python: navBaseCall.replace('**v**', '\'%s\',\'SortReference\', {\'sortKey\':\'%s\', \'reverse\':\'**v**\'}' % (startNumber, widget['name']))"
tal:condition="python: canWrite and tool.isSortable(widget['name'], objs[0].meta_type, 'ref')">
tal:condition="python: changeOrder and canWrite and tool.isSortable(widget['name'], objs[0].meta_type, 'ref')">
<img style="cursor:pointer"
tal:attributes="src string:$appUrl/ui/sortAsc.png;
onClick python: ajaxBaseCall.replace('**v**', 'False')"/>
@ -90,24 +90,9 @@
</metal:sortIcons>
<tal:comment replace="nothing">View macro for a Ref.</tal:comment>
<metal:view metal:define-macro="view"
tal:define="singleRef python: widget['multiplicity'][1] == 1">
<tal:comment replace="nothing">
For performance reasons, multivalued references are called via Ajax, while single-valued aren't.
</tal:comment>
<tal:ajax condition="not: singleRef">
<div tal:define= "innerRef innerRef|python:False;
ajaxHookId python: contextObj.UID() + name"
tal:attributes = "id ajaxHookId">
<script name="appyHook" tal:content="python: 'askRefField(\'%s\',\'%s\',\'%s\',\'%s\',0)' % (ajaxHookId, contextObj.absolute_url(), name, innerRef)">
</script>
</div>
</tal:ajax>
<div tal:condition="singleRef">
<tal:request define="dummy python: request.set('fieldName', widget['name'])">
<metal:ref use-macro="app/ui/widgets/ref/macros/viewContent"/>
</tal:request>
</div>
<metal:view define-macro="view"
tal:define="dummy python: request.set('fieldName', widget['name'])">
<metal:ref use-macro="app/ui/widgets/ref/macros/viewContent"/>
</metal:view>
<tal:comment replace="nothing">
@ -132,7 +117,9 @@
showPlusIcon python: contextObj.mayAddReference(fieldName);
atMostOneRef python: (appyType['multiplicity'][1] == 1) and (len(objs)&lt;=1);
addConfirmMsg python: appyType['addConfirm'] and _('%s_addConfirm' % appyType['labelId']) or '';
navBaseCall python: 'askRefField(\'%s\',\'%s\',\'%s\',\'%s\',**v**)' % (ajaxHookId, contextObj.absolute_url(), fieldName, innerRef)">
navBaseCall python: 'askRefField(\'%s\',\'%s\',\'%s\',\'%s\',**v**)' % (ajaxHookId, contextObj.absolute_url(), fieldName, innerRef);
changeOrder python: contextObj.callField(fieldName, 'changeOrderEnabled', contextObj)"
tal:attributes="id ajaxHookId">
<tal:comment replace="nothing">This macro displays the Reference widget on a "view" page.