Browse Source

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
version-14
schilgod 7 years ago
committed by Faris Ansari
parent
commit
a247dfe2d7
2 changed files with 38 additions and 5 deletions
  1. +31
    -4
      frappe/public/js/frappe/ui/page.js
  2. +7
    -1
      frappe/public/js/legacy/form.js

+ 31
- 4
frappe/public/js/frappe/ui/page.js View File

@@ -275,7 +275,7 @@ frappe.ui.Page = Class.extend({
return $('<li class="divider"></li>').appendTo(this.menu); 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+'"]'); var $group = this.inner_toolbar.find('.btn-group[data-label="'+label+'"]');
if(!$group.length) { if(!$group.length) {
$group = $('<div class="btn-group" data-label="'+label+'" style="margin-left: 10px;">\ $group = $('<div class="btn-group" data-label="'+label+'" style="margin-left: 10px;">\
@@ -286,8 +286,12 @@ frappe.ui.Page = Class.extend({
return $group; 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) { 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) { btn_disable_enable: function(btn, response) {
@@ -312,7 +316,7 @@ frappe.ui.Page = Class.extend({
me.btn_disable_enable(btn, response); me.btn_disable_enable(btn, response);
}; };
if(group) { 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"); $(this.inner_toolbar).removeClass("hide");
return $('<li><a>'+label+'</a></li>') return $('<li><a>'+label+'</a></li>')
.on('click', _action) .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() { clear_inner_toolbar: function() {
this.inner_toolbar.empty().addClass("hide"); this.inner_toolbar.empty().addClass("hide");
}, },
@@ -495,4 +522,4 @@ frappe.ui.Page = Class.extend({


this.wrapper.trigger('view-change'); this.wrapper.trigger('view-change');
}, },
});
});

+ 7
- 1
frappe/public/js/legacy/form.js View File

@@ -4,7 +4,7 @@
/* Form page structure /* Form page structure


+ this.parent (either FormContainer or Dialog) + this.parent (either FormContainer or Dialog)
+ this.wrapper
+ this.wrapper
+ this.toolbar + this.toolbar
+ this.form_wrapper + this.form_wrapper
+ this.head + this.head
@@ -921,12 +921,18 @@ _f.Frm.prototype.add_custom_button = function(label, fn, group) {
return btn; return btn;
}; };


//Remove all custom buttons
_f.Frm.prototype.clear_custom_buttons = function() { _f.Frm.prototype.clear_custom_buttons = function() {
this.page.clear_inner_toolbar(); this.page.clear_inner_toolbar();
this.page.clear_user_actions(); this.page.clear_user_actions();
this.custom_buttons = {}; 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) { _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
if(!this.fetch_dict[link_field]) { if(!this.fetch_dict[link_field]) {
this.fetch_dict[link_field] = {'columns':[], 'fields':[]}; this.fetch_dict[link_field] = {'columns':[], 'fields':[]};


Loading…
Cancel
Save