feat: Handle server and client side primary action in msgprint (#8835)

feat: Handle server and client side primary action in msgprint
This commit is contained in:
Faris Ansari 2019-11-22 15:02:33 +05:30 committed by GitHub
commit 5da2a5b4e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View file

@ -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__'):

View file

@ -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