[gen] Ajaxified query result and tied object rows.

This commit is contained in:
Gaetan Delannay 2015-02-04 09:27:07 +01:00
parent 1bd6cf29a3
commit 45192ca4bc
7 changed files with 142 additions and 71 deletions

View file

@ -97,6 +97,28 @@ class Field:
context[k] = ctx[k]
return self.pxRender(context).encode('utf-8')
# Show the field content for some object on a list of referred objects
pxRenderAsTied = Px('''
<!-- The "title" field -->
<x if="refField.name == 'title'">
<x if="mayView">
<x if="not field.menuUrlMethod">:field.pxObjectTitle</x>
<a if="field.menuUrlMethod"
var2="info=field.getMenuUrl(zobj, tied)"
href=":info[0]" target=":info[1]">:tied.title</a>
<x if="tied.o.mayAct()">:field.pxObjectActions</x>
</x>
<div if="not mayView">
<img src=":url('fake')" style="margin-right: 5px"/>
<x>:_('unauthorized')</x></div>
</x>
<!-- Any other field -->
<x if="(refField.name != 'title') and mayView">
<x var="zobj=tied.o; obj=tied; layoutType='cell';
innerRef=True; field=refField"
if="field.isShowable(zobj, 'result')">:field.pxRender</x>
</x>''')
# Show the field content for some object on a list of results
pxRenderAsResult = Px('''
<!-- Title -->
@ -118,7 +140,7 @@ class Field:
<!-- Actions -->
<div if="not inPopup and uiSearch.showActions and zobj.mayAct()"
class="objectActions" style=":'display:%s' % uiSearch.showActions"
var2="layoutType='buttons'" >
var2="layoutType='buttons'">
<!-- Edit -->
<a if="zobj.mayEdit()"
var2="linkInPopup=inPopup or (target.target != '_self')"

View file

@ -31,19 +31,18 @@ class Action(Field):
label=_(field.labelId);
descr=field.hasDescr and _(field.descrId) or None;
smallButtons=smallButtons|False;
css=ztool.getButtonCss(label, smallButtons)"
css=ztool.getButtonCss(label, smallButtons);
back=(layoutType == 'cell') and q(zobj.id) or 'null'"
id=":formId" action=":zobj.absolute_url() + '/onExecuteAction'"
style="display:inline">
<input type="hidden" name="fieldName" value=":name"/>
<input type="hidden" name="comment" value=""/>
<input if="field.confirm" type="button" class=":css" title=":descr"
var="labelConfirm=_(field.labelId + '_confirm');
commentParam=(field.confirm == 'text') and 'true' or 'false'"
<input type="button" class=":css" title=":descr"
var="textConfirm=field.confirm and _(field.labelId+'_confirm') or '';
showComment=(field.confirm == 'text') and 'true' or 'false'"
value=":label" style=":url(field.icon, bg=True)"
onclick=":'askConfirm(%s,%s,%s,%s)' % (q('form'), q(formId), \
q(labelConfirm), commentParam)"/>
<input if="not field.confirm" type="submit" class=":css" name="do"
value=":label" title=":descr" style=":url(field.icon, bg=True)"/>
onclick=":'submitForm(%s,%s,%s,%s)' % (q(formId), q(textConfirm), \
showComment, back)"/>
</form>''')
# It is not possible to edit an action, not to search it
@ -145,6 +144,8 @@ class Action(Field):
suffix = successfull and 'done' or 'ko'
msg = obj.translate('action_%s' % suffix)
if (self.result == 'computation') or not successfull:
# If we are called from an Ajax request, simply return msg
if hasattr(rq, 'pxContext') and rq.pxContext['ajax']: return msg
obj.say(msg)
return obj.goto(obj.getUrl(rq['HTTP_REFERER']))
elif self.result == 'file':

View file

@ -38,7 +38,7 @@ class Ref(Field):
# defined. If we are on a forward reference, the "nav" parameter is added to
# the URL for allowing to navigate from one object to the next/previous one.
pxObjectTitle = Px('''
<x var="navInfo=field.getNavInfo(zobj, loop.tied.nb + 1 + startNumber, \
<x var="navInfo=field.getNavInfo(zobj, startNumber + currentNumber, \
totalNumber, inPickList);
pageName=field.isBack and field.back.pageName or 'main'">
<x>::tied.o.getSupTitle(navInfo)</x>
@ -106,7 +106,7 @@ class Ref(Field):
</x>
<!-- Edit -->
<a if="not field.noForm and tied.o.mayEdit()"
var2="navInfo=field.getNavInfo(zobj, loop.tied.nb + 1 + startNumber, \
var2="navInfo=field.getNavInfo(zobj, startNumber + currentNumber, \
totalNumber);
linkInPopup=inPopup or (target.target != '_self')"
href=":tied.o.getUrl(mode='edit', page='main', nav=navInfo, \
@ -204,7 +204,7 @@ class Ref(Field):
q(sortConfirm))"/>
</x>''')
# Shows the object number in a numbered list of tied objects.
# Shows the object number in a numbered list of tied objects
pxNumber = Px('''
<x if="not changeNumber">:objectIndex+1</x>
<div if="changeNumber" class="dropdownMenu"
@ -258,7 +258,14 @@ class Ref(Field):
class=":not innerRef and 'list' or ''"
width=":innerRef and '100%' or field.layouts['view'].width"
var2="columns=ztool.getColumnsSpecifiers(tiedClassName, \
field.getAttribute(obj, 'shownInfo'), dir)">
field.getAttribute(obj, 'shownInfo'), dir);
currentNumber=0">
<script>:field.getAjaxData(ajaxHookId, zobj, popup=inPopup, \
checkboxes=checkboxes, startNumber=startNumber, \
totalNumber=totalNumber, sourceId=zobj.id, \
refFieldName=field.name, inPickList=inPickList, \
numbered=numbered, navBaseCall=navBaseCall)</script>
<tr if="field.showHeaders">
<th if="not inPickList and numbered" width=":numbered"></th>
<th if="checkboxes" class="cbCell">
@ -274,41 +281,10 @@ class Ref(Field):
field=refField">:tool.pxShowDetails</x>
</th>
</tr>
<!-- Loop on every (tied or selectable) object. -->
<tr for="tied in objects" valign="top"
class=":loop.tied.odd and 'even' or 'odd'"
var2="tiedUid=tied.o.id;
objectIndex=field.getIndexOf(zobj, tiedUid)|None;
mayView=tied.o.mayView()"
id=":'%s_%s' % (ajaxHookId, tiedUid)">
<td if="not inPickList and numbered">:field.pxNumber</td>
<td if="checkboxes" class="cbCell">
<input if="mayView" type="checkbox" name=":ajaxHookId" checked="checked"
value=":tiedUid" onclick="toggleCb(this)"/>
</td>
<td for="column in columns" width=":column.width" align=":column.align"
var2="refField=column.field">
<!-- The "title" field -->
<x if="refField.name == 'title'">
<x if="mayView">
<x if="not field.menuUrlMethod">:field.pxObjectTitle</x>
<a if="field.menuUrlMethod"
var2="info=field.getMenuUrl(zobj, tied)"
href=":info[0]" target=":info[1]">:tied.title</a>
<x if="tied.o.mayAct()">:field.pxObjectActions</x>
</x>
<div if="not mayView">
<img src=":url('fake')" style="margin-right: 5px"/>
<x>:_('unauthorized')</x></div>
</x>
<!-- Any other field -->
<x if="(refField.name != 'title') and mayView">
<x var="zobj=tied.o; obj=tied; layoutType='cell';
innerRef=True; field=refField"
if="field.isShowable(zobj, 'result')">:field.pxRender</x>
</x>
</td>
</tr>
<!-- Loop on every (tied or selectable) object -->
<x for="tied in objects"
var2="@currentNumber=currentNumber + 1;
rowCss=loop.tied.odd and 'even' or 'odd'">:obj.pxViewAsTied</x>
</table>
<!-- Global actions -->
@ -340,9 +316,9 @@ class Ref(Field):
target=ztool.getLinksTargetInfo(field.klass);
mayEdit=mayEdit|\
not field.isBack and zobj.mayEdit(field.writePermission);
mayUnlink=False;
mayAdd=False;
mayLink=False;
mayUnlink=False;
navBaseCall='askRefField(%s,%s,%s,**v**)' % \
(q(ajaxHookId), q(zobj.absolute_url()), q(innerRef));
changeOrder=False;
@ -437,10 +413,10 @@ class Ref(Field):
tiedClassLabel=_(tiedClassName);
target=ztool.getLinksTargetInfo(field.klass);
mayEdit=not field.isBack and zobj.mayEdit(field.writePermission);
mayUnlink=mayEdit and field.getAttribute(zobj, 'unlink');
mayAdd=mayEdit and field.mayAdd(zobj, checkMayEdit=False);
mayLink=mayEdit and field.mayAdd(zobj, mode='link', \
checkMayEdit=False);
checkMayEdit=False);
mayUnlink=mayEdit and field.getAttribute(zobj, 'unlink');
addConfirmMsg=field.addConfirm and \
_('%s_addConfirm' % field.labelId) or '';
navBaseCall='askRefField(%s,%s,%s,**v**)' % \
@ -1290,6 +1266,26 @@ class Ref(Field):
return "var node=document.getElementById('%s_%s');%s%s" % \
(obj.id, self.name, code % ('objs', 'objs'), poss)
def getAjaxData(self, hook, zobj, **params):
'''Initializes an AjaxData object on the DOM node corresponding to
p_hook = the whole search result.'''
# Complete params with default parameters
params['ajaxHookId'] = hook;
params = sutils.getStringDict(params)
px = hook.endswith('_poss') and 'pxViewPickList' or 'pxView'
px = '%s:%s' % (self.name, px)
return "document.getElementById('%s')['ajax']=new AjaxData('%s', " \
"'%s', %s, null, '%s')" % \
(hook, hook, px, params, zobj.absolute_url())
def getAjaxDataRow(self, obj, parentHook, **params):
'''Initializes an AjaxData object on the DOM node corresponding to
p_hook = a row within the list of referred objects.'''
hook = obj.id
return "document.getElementById('%s')['ajax']=new AjaxData('%s', " \
"'pxViewAsTiedFromAjax',%s,'%s','%s')" % \
(hook, hook, sutils.getStringDict(params), parentHook, obj.url)
def doChangeOrder(self, obj):
'''Moves a referred object up/down/top/bottom.'''
rq = obj.REQUEST
@ -1308,12 +1304,12 @@ class Ref(Field):
elif move == 'bottom':
newIndex = len(uids) - 1
elif move.startswith('index'):
# New index starts at 1 (oldIndex starts at 0).
# New index starts at 1 (oldIndex starts at 0)
try:
newIndex = int(move.split('_')[1]) - 1
except ValueError:
newIndex = -1
# If newIndex is negative, it means that the move can't occur.
# If newIndex is negative, it means that the move can't occur
if newIndex > -1:
uids.remove(uid)
uids.insert(newIndex, uid)
@ -1385,15 +1381,15 @@ class Ref(Field):
else:
return [tool.getObject(rv) for rv in requestValue]
res = []
# No object can be selected if the popup has not been opened yet.
# No object can be selected if the popup has not been opened yet
if 'semantics' not in rq:
# In this case, display already linked objects if any.
# In this case, display already linked objects if any
if not obj.isEmpty(self.name): return self.getValue(obj.o)
return res
uids = rq['selected'].split(',')
tool = obj.tool
if rq['semantics'] == 'checked':
# Simply get the selected objects from their uid.
# Simply get the selected objects from their uid
return [tool.getObject(uid) for uid in uids]
else:
# Replay the search in self.select to get the list of uids that were

View file

@ -428,7 +428,7 @@ class Transition:
msg = self.trigger(name, obj, wf, rq.get('comment', ''), reindex=False)
# Reindex obj if required
if not obj.isTemporary(): obj.reindex()
# If we are called from an Ajax request, return a message
# If we are called from an Ajax request, simply return msg
if hasattr(rq, 'pxContext') and rq.pxContext['ajax']: return msg
# If we are viewing the object and if the logged user looses the
# permission to view it, redirect the user to its home page.

View file

@ -701,7 +701,7 @@ class BaseMixin:
def getFieldValue(self, name, layoutType=None, outerValue=None):
'''Returns the database value of field named p_name for p_self.'''
if layoutType == 'search': return # No object in search screens.
if layoutType == 'search': return # No object in search screens
field = self.getAppyType(name)
if field.type == 'Pod': return
if '*' not in name: return field.getValue(self)

View file

@ -136,14 +136,13 @@ function getAjaxChunk(pos) {
if (xhrObjects[pos].xhr.readyState == 4) {
// We have received the HTML chunk
var hookElem = getAjaxHook(hook);
var responseOk = (xhrObjects[pos].xhr.status == 200);
if (hookElem && responseOk) {
if (hookElem) {
injectChunk(hookElem, xhrObjects[pos].xhr.responseText);
// Call a custom Javascript function if required
if (xhrObjects[pos].onGet) {
xhrObjects[pos].onGet(xhrObjects[pos], hookElem);
}
// Eval inner scripts if any.
// Eval inner scripts if any
var innerScripts = getElementsHavingName('div', 'appyHook');
for (var i=0; i<innerScripts.length; i++) {
eval(innerScripts[i].innerHTML);
@ -152,7 +151,7 @@ function getAjaxChunk(pos) {
var msg = xhrObjects[pos].xhr.getResponseHeader('Appy-Message');
if (msg) showAppyMessage(decodeURIComponent(escape(msg)));
}
if (responseOk) xhrObjects[pos].freed = 1;
xhrObjects[pos].freed = 1;
}
}
}
@ -308,7 +307,7 @@ function askRefField(hookId, objectUrl, innerRef, startNumber, action,
actionParams){
var hookElems = hookId.split('_');
var fieldName = hookElems[1];
// Sends an Ajax request for getting the content of a reference field.
// Sends an Ajax request for getting the content of a reference field
var startKey = hookId + '_startNumber';
var scope = hookElems.pop();
var params = {'innerRef': innerRef, 'scope': scope};
@ -631,12 +630,10 @@ function submitAppyForm(button) {
theForm.submit();
}
// Function used for triggering a workflow transition
function triggerTransition(formId, transitionId, msg, back) {
function submitForm(formId, msg, showComment, back) {
var f = document.getElementById(formId);
f.transition.value = transitionId;
if (!msg) {
/* We must submit the form and either refresh the entire page (back is null)
/* Submit the form and either refresh the entire page (back is null)
or ajax-refresh a given part only (p_back corresponds to the id of the
DOM node to be refreshed. */
if (back) askAjax(back, formId);
@ -646,11 +643,18 @@ function triggerTransition(formId, transitionId, msg, back) {
// Ask a confirmation to the user before proceeding
if (back) {
var js = "askAjax('"+back+"', '"+formId+"');"
askConfirm('script', js, msg, true) }
else askConfirm('form', formId, msg, true);
askConfirm('script', js, msg, showComment) }
else askConfirm('form', formId, msg, showComment);
}
}
// Function used for triggering a workflow transition
function triggerTransition(formId, transitionId, msg, back) {
var f = document.getElementById(formId);
f.transition.value = transitionId;
submitForm(formId, msg, true, back);
}
function onDeleteObject(objectUid) {
f = document.getElementById('deleteForm');
f.objectUid.value = objectUid;

View file

@ -374,9 +374,10 @@ class AbstractWrapper(object):
pxTransitions = Px('''
<form var="transitions=targetObj.getTransitions()" if="transitions"
var2="formId='trigger_%s' % targetObj.id" method="post"
var2="formId='trigger_%s' % targetObj.id;
zobj=targetObj"
id=":formId" action=":targetObj.absolute_url() + '/onTrigger'"
style="display: inline">
style="display: inline" method="post">
<input type="hidden" name="transition"/>
<!-- Input field for storing the comment coming from the popup -->
<textarea id="comment" name="comment" cols="30" rows="3"
@ -575,6 +576,53 @@ class AbstractWrapper(object):
</table>
</form>''')
# The object, as shown in a list of referred (tied) objects
pxViewAsTied = Px('''
<tr valign="top" class=":rowCss"
var2="tiedUid=tied.o.id;
objectIndex=field.getIndexOf(zobj, tiedUid)|None;
mayView=tied.o.mayView()"
id=":tiedUid">
<td if="not inPickList and numbered">:field.pxNumber</td>
<td if="checkboxes" class="cbCell">
<input if="mayView" type="checkbox" name=":ajaxHookId" checked="checked"
value=":tiedUid" onclick="toggleCb(this)"/>
</td>
<td for="column in columns" width=":column.width" align=":column.align"
var2="refField=column.field">:refField.pxRenderAsTied</td>
<!-- Store data in this tr node allowing to ajax-refresh it -->
<script>:field.getAjaxDataRow(tied, ajaxHookId, rowCss=rowCss, \
currentNumber=currentNumber)</script>
</tr>''')
# When calling pxViewAsTied from Ajax, this surrounding PX is called to
# define the appropriate variables based on request values.
pxViewAsTiedFromAjax = Px('''
<x var="dummy=ztool.updatePxContextFromRequest();
tied=obj;
zobj=ztool.getObject(sourceId);
obj=zobj.appy();
field=zobj.getAppyType(refFieldName);
layoutType='view';
render=field.getRenderMode(layoutType);
linkList=field.link == 'list';
numberWidth=len(str(totalNumber));
tiedClassName=ztool.getPortalType(field.klass);
target=ztool.getLinksTargetInfo(field.klass);
mayEdit=not field.isBack and zobj.mayEdit(field.writePermission);
mayLink=not inPickList and mayEdit and \
field.mayAdd(zobj, mode='link', checkMayEdit=False);
mayUnlink=not inPickList and mayEdit and \
field.getAttribute(zobj, 'unlink');
gotoNumber=numbered;
changeOrder=not inPickList and mayEdit and \
field.getAttribute(zobj, 'changeOrder');
changeNumber=not inPickList and numbered and changeOrder and \
(totalNumber &gt; 3);
columns=ztool.getColumnsSpecifiers(tiedClassName, \
field.getAttribute(obj, 'shownInfo'), dir);
showSubTitles=showSubTitles|True">:obj.pxViewAsTied</x>''')
# The object, as shown in a list of query results
pxViewAsResult = Px('''
<tr var2="obj=zobj.appy(); mayView=zobj.mayView();