Add function to remove specific custom buttons by label (#4498)

* Add function to remove specific custom buttons by label

* [fix] codacy errors

* rename remove_custom_buttons function to remove_custom_button

* seperate add and get inner button functions

* [fix] codacy errors

* More flexible API

* Alias Form method
This commit is contained in:
schilgod 2017-12-07 16:56:12 +08:00 committed by Faris Ansari
parent af0d0972aa
commit a247dfe2d7
2 changed files with 38 additions and 5 deletions

View file

@ -275,7 +275,7 @@ frappe.ui.Page = Class.extend({
return $('<li class="divider"></li>').appendTo(this.menu);
},
get_inner_group_button: function(label) {
get_or_add_inner_group_button: function(label) {
var $group = this.inner_toolbar.find('.btn-group[data-label="'+label+'"]');
if(!$group.length) {
$group = $('<div class="btn-group" data-label="'+label+'" style="margin-left: 10px;">\
@ -286,8 +286,12 @@ frappe.ui.Page = Class.extend({
return $group;
},
get_inner_group_button: function(label) {
return this.inner_toolbar.find('.btn-group[data-label="'+label+'"]');
},
set_inner_btn_group_as_primary: function(label) {
this.get_inner_group_button(label).find("button").removeClass("btn-default").addClass("btn-primary");
this.get_or_add_inner_group_button(label).find("button").removeClass("btn-default").addClass("btn-primary");
},
btn_disable_enable: function(btn, response) {
@ -312,7 +316,7 @@ frappe.ui.Page = Class.extend({
me.btn_disable_enable(btn, response);
};
if(group) {
var $group = this.get_inner_group_button(group);
var $group = this.get_or_add_inner_group_button(group);
$(this.inner_toolbar).removeClass("hide");
return $('<li><a>'+label+'</a></li>')
.on('click', _action)
@ -324,6 +328,29 @@ frappe.ui.Page = Class.extend({
}
},
remove_inner_button: function(label, group) {
if (typeof label === 'string') {
label = [label];
}
// translate
label = label.map(l => __(l));
if (group) {
var $group = this.get_inner_group_button(__(group));
if($group.length) {
$group.find('.dropdown-menu li a')
.filter((i, btn) => label.includes($(btn).text()))
.remove();
}
if ($group.find('.dropdown-menu li a').length === 0) $group.remove();
} else {
this.inner_toolbar.find('button')
.filter((i, btn) => label.includes($(btn).text()))
.remove();
}
},
clear_inner_toolbar: function() {
this.inner_toolbar.empty().addClass("hide");
},
@ -495,4 +522,4 @@ frappe.ui.Page = Class.extend({
this.wrapper.trigger('view-change');
},
});
});

View file

@ -4,7 +4,7 @@
/* Form page structure
+ this.parent (either FormContainer or Dialog)
+ this.wrapper
+ this.wrapper
+ this.toolbar
+ this.form_wrapper
+ this.head
@ -921,12 +921,18 @@ _f.Frm.prototype.add_custom_button = function(label, fn, group) {
return btn;
};
//Remove all custom buttons
_f.Frm.prototype.clear_custom_buttons = function() {
this.page.clear_inner_toolbar();
this.page.clear_user_actions();
this.custom_buttons = {};
};
//Remove specific custom button by button Label
_f.Frm.prototype.remove_custom_button = function(label, group) {
this.page.remove_inner_button(label, group);
};
_f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
if(!this.fetch_dict[link_field]) {
this.fetch_dict[link_field] = {'columns':[], 'fields':[]};