[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 = '' pxEdit = pxSearch = ''
def __init__(self, validator=None, multiplicity=(1,1), default=None, def __init__(self, validator=None, multiplicity=(1,1), default=None,
show=True, page='main', group=None, layouts=None, move=0, show=('view', 'result'), page='main', group=None, layouts=None,
indexed=False, searchable=False, specificReadPermission=False, move=0, indexed=False, searchable=False,
specificWritePermission=False, width=None, height=None, specificReadPermission=False, specificWritePermission=False,
maxChars=None, colspan=1, action=None, result='computation', width=None, height=None, maxChars=None, colspan=1, action=None,
confirm=False, master=None, masterValue=None, focus=False, result='computation', confirm=False, master=None,
historized=False, mapping=None, label=None): masterValue=None, focus=False, historized=False, mapping=None,
label=None):
# Can be a single method or a list/tuple of methods # Can be a single method or a list/tuple of methods
self.action = action self.action = action
# For the 'result' param: # For the 'result' param:

View file

@ -1284,9 +1284,10 @@ class ToolMixin(BaseMixin):
chunk. This method executes this action.''' chunk. This method executes this action.'''
if action.startswith(':'): if action.startswith(':'):
# The action corresponds to a method on Appy p_obj. # The action corresponds to a method on Appy p_obj.
getattr(obj, action[1:])() msg = getattr(obj, action[1:])()
else: else:
# The action must be executed on p_field if present, on obj.o else. # The action must be executed on p_field if present, on obj.o else.
if field: getattr(field, action)(obj.o) if field: msg = getattr(field, action)(obj.o)
else: getattr(obj.o, action)() 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++) { for (var i=0; i<innerScripts.length; i++) {
eval(innerScripts[i].innerHTML); 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; if (responseOk) xhrObjects[pos].freed = 1;
} }
@ -814,6 +817,15 @@ function backFromPopup() {
window.parent.location = window.parent.location; 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 triggered when an action needs to be confirmed by the user
function askConfirm(actionType, action, msg, showComment) { function askConfirm(actionType, action, msg, showComment) {
/* Store the actionType (send a form, call an URL or call a script) and the /* 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. # The message that is shown when a user triggers an action.
pxMessage = Px(''' pxMessage = Px('''
<div var="messages=ztool.consumeMessages()" if="messages" <div class=":inPopup and 'messagePopup message' or 'message'"
class=":inPopup and 'messagePopup message' or 'message'"> style="display:none" id="appyMessage">
<!-- The icon for closing the message --> <!-- The icon for closing the message -->
<img src=":url('close')" align=":dright" class="clickable" <img src=":url('close')" align=":dright" class="clickable"
onclick="this.parentNode.style.display='none'"/> onclick="this.parentNode.style.display='none'"/>
<!-- The message content --> <!-- The message content -->
<x>::messages</x> <div id="appyMessageContent"></div>
</div>''') </div>
<script type="text/javascript" var="messages=ztool.consumeMessages()"
if="messages">::'showAppyMessage(%s)' % q(messages)</script>''')
# The page footer. # The page footer.
pxFooter = Px(''' pxFooter = Px('''

View file

@ -632,10 +632,12 @@ class AbstractWrapper(object):
x=resp.setHeader('Content-type', ztool.xhtmlEncoding); x=resp.setHeader('Content-type', ztool.xhtmlEncoding);
x=resp.setHeader('Expires', 'Thu, 11 Dec 1975 12:05:00 GMT+2'); x=resp.setHeader('Expires', 'Thu, 11 Dec 1975 12:05:00 GMT+2');
x=resp.setHeader('Content-Language', lang); 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. --> <!-- 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. --> <!-- Then, call the PX on p_obj or on p_field. -->
<x if="not field">:getattr(obj, px[0])</x> <x if="not field">:getattr(obj, px[0])</x>