[gen] Allow an ajax request to return a message, carried as a custom HTTP header and retrieved by appy.js to display a message.

This commit is contained in:
Gaetan Delannay 2014-11-10 13:34:52 +01:00
parent 8d03f6ca9c
commit 4a393e76af
5 changed files with 33 additions and 15 deletions

View file

@ -52,12 +52,13 @@ class Action(Field):
pxEdit = pxSearch = ''
def __init__(self, validator=None, multiplicity=(1,1), default=None,
show=True, page='main', group=None, layouts=None, move=0,
indexed=False, searchable=False, specificReadPermission=False,
specificWritePermission=False, width=None, height=None,
maxChars=None, colspan=1, action=None, result='computation',
confirm=False, master=None, masterValue=None, focus=False,
historized=False, mapping=None, label=None):
show=('view', 'result'), page='main', group=None, layouts=None,
move=0, indexed=False, searchable=False,
specificReadPermission=False, specificWritePermission=False,
width=None, height=None, maxChars=None, colspan=1, action=None,
result='computation', confirm=False, master=None,
masterValue=None, focus=False, historized=False, mapping=None,
label=None):
# Can be a single method or a list/tuple of methods
self.action = action
# For the 'result' param:

View file

@ -1284,9 +1284,10 @@ class ToolMixin(BaseMixin):
chunk. This method executes this action.'''
if action.startswith(':'):
# The action corresponds to a method on Appy p_obj.
getattr(obj, action[1:])()
msg = getattr(obj, action[1:])()
else:
# The action must be executed on p_field if present, on obj.o else.
if field: getattr(field, action)(obj.o)
else: getattr(obj.o, action)()
if field: msg = getattr(field, action)(obj.o)
else: msg = getattr(obj.o, action)()
return msg
# ------------------------------------------------------------------------------

View file

@ -148,6 +148,9 @@ function getAjaxChunk(pos) {
for (var i=0; i<innerScripts.length; i++) {
eval(innerScripts[i].innerHTML);
}
// Display the Appy message if present
var msg = xhrObjects[pos].xhr.getResponseHeader('Appy-Message');
if (msg) showAppyMessage(decodeURIComponent(escape(msg)));
}
if (responseOk) xhrObjects[pos].freed = 1;
}
@ -814,6 +817,15 @@ function backFromPopup() {
window.parent.location = window.parent.location;
}
function showAppyMessage(message) {
// Fill the message zone with the message to display
var messageZone = document.getElementById('appyMessageContent');
messageZone.innerHTML = message;
// Display the message zone
var messageDiv = document.getElementById('appyMessage');
messageDiv.style.display = 'block';
}
// Function triggered when an action needs to be confirmed by the user
function askConfirm(actionType, action, msg, showComment) {
/* Store the actionType (send a form, call an URL or call a script) and the

View file

@ -239,14 +239,16 @@ class ToolWrapper(AbstractWrapper):
# The message that is shown when a user triggers an action.
pxMessage = Px('''
<div var="messages=ztool.consumeMessages()" if="messages"
class=":inPopup and 'messagePopup message' or 'message'">
<div class=":inPopup and 'messagePopup message' or 'message'"
style="display:none" id="appyMessage">
<!-- The icon for closing the message -->
<img src=":url('close')" align=":dright" class="clickable"
onclick="this.parentNode.style.display='none'"/>
<!-- The message content -->
<x>::messages</x>
</div>''')
<div id="appyMessageContent"></div>
</div>
<script type="text/javascript" var="messages=ztool.consumeMessages()"
if="messages">::'showAppyMessage(%s)' % q(messages)</script>''')
# The page footer.
pxFooter = Px('''

View file

@ -632,10 +632,12 @@ class AbstractWrapper(object):
x=resp.setHeader('Content-type', ztool.xhtmlEncoding);
x=resp.setHeader('Expires', 'Thu, 11 Dec 1975 12:05:00 GMT+2');
x=resp.setHeader('Content-Language', lang);
x=resp.setHeader('CacheControl', 'no-cache')">
x=resp.setHeader('Cache-Control', 'no-cache')">
<!-- If an action is defined, execute it on p_zobj or on p_field. -->
<x if="action" var2="x=ztool.executeAjaxAction(action, obj, field)"></x>
<x if="action"
var2="msg=ztool.executeAjaxAction(action, obj, field) or '';
x=resp.setHeader('Appy-Message', msg)"></x>
<!-- Then, call the PX on p_obj or on p_field. -->
<x if="not field">:getattr(obj, px[0])</x>