diff --git a/frappe/__init__.py b/frappe/__init__.py index 6424dcd9b5..1ee5db416b 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -290,7 +290,7 @@ def log(msg): debug_log.append(as_unicode(msg)) -def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, alert=False): +def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, alert=False, primary_action=None): """Print a message to the user (via HTTP response). Messages are sent in the `__server_messages` property in the response JSON and shown in a pop-up / modal. @@ -299,6 +299,7 @@ def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, :param title: [optional] Message title. :param raise_exception: [optional] Raise given exception and show message. :param as_table: [optional] If `msg` is a list of lists, render as HTML table. + :param primary_action: [optional] Bind a primary server/client side action. """ from frappe.utils import encode @@ -338,6 +339,9 @@ def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, if alert: out.alert = 1 + if primary_action: + out.primary_action = primary_action + message_log.append(json.dumps(out)) if raise_exception and hasattr(raise_exception, '__name__'): diff --git a/frappe/public/js/frappe/ui/messages.js b/frappe/public/js/frappe/ui/messages.js index f804648834..26bee50f18 100644 --- a/frappe/public/js/frappe/ui/messages.js +++ b/frappe/public/js/frappe/ui/messages.js @@ -122,8 +122,34 @@ frappe.msgprint = function(msg, title) { // setup and bind an action to the primary button if (data.primary_action) { - frappe.msg_dialog.set_primary_action(__(data.primary_action.label || "Done"), - data.primary_action.action); + if (data.primary_action.server_action && typeof data.primary_action.server_action === 'string') { + data.primary_action.action = () => { + frappe.call({ + method: data.primary_action.server_action, + args: { + args: data.primary_action.args + } + }); + } + } + + if (data.primary_action.client_action && typeof data.primary_action.client_action === 'string') { + let parts = data.primary_action.client_action.split('.'); + let obj = window; + for (let part of parts) { + obj = obj[part]; + } + data.primary_action.action = () => { + if (typeof obj === 'function') { + obj(data.primary_action.args); + } + } + } + + frappe.msg_dialog.set_primary_action( + __(data.primary_action.label || "Done"), + data.primary_action.action + ); } // class "msgprint" is used in tests