PDA

View Full Version : Simple plugin for toggling Ext.Button for GridPanel / DataView selections



jasonb885
30 Sep 2010, 4:59 PM
Simple plugin that will toggle buttons based upon whether a row is selected. The Record's data is passed to allow for more granular control. Been using this for quite a while now without any problems. Presented here for your enjoyment under a BSD license.

Revisions available on gist.github.

Eventually I need to hack up an actual working example. Also considering making available the active selection to the button handler.

Naturally suggestions and improvements welcome.

http://gist.github.com/598187



/*
Copyright (c) 2010, Jason Boxman <jasonb@edseek.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

liaison.klass.plugin.TopToolbar = Ext.extend(Object, {
constructor:function(config) {
Ext.apply(this, config);
},

init:function(p) {
Ext.applyIf(p, liaison.klass.plugin.TopToolbar.Methods.prototype);

if(p.disableToolbar) return;

p.on('render', this.onRender, p);
},

onRender:function() {
var btns = this.tbarButtons,
tb = this.getTopToolbar();

Ext.each(btns, function(config) {
var o = Ext.isString(config) ?
(Ext.ComponentMgr.isRegistered(config) ? {xtype:config} : config) : config;
tb.add(o);
}, this);

var btns = tb.items.items;
Ext.each(btns, function(btn) {
Ext.applyIf(btn, {
handler:this.btnClickHandler,
scope:this
});
}, this);
}
});
Ext.preg('top-tbar', liaison.klass.plugin.TopToolbar);

liaison.klass.plugin.TopToolbar.Methods = function() {};
liaison.klass.plugin.TopToolbar.Methods.prototype = {

buttonToggle:function() {
if(!Ext.isDefined(this.getTopToolbar())) return;

var a = Ext.toArray(arguments),
tb = this.getTopToolbar(),
btns = tb.items.items,
useSm = a.length == 1 ? true : false,
op = useSm ? a[0].hasSelection() : a[1].length > 0,
fnName = op ? 'enable' : 'disable',
recordData;

if(op) recordData = useSm ? a[0].getSelected().data : a[0].getRecord(a[1][0]).data;

Ext.each(btns, function(btn) {
if(btn.toggleOnSelectionChange) {
this.toggleButtonFn(fnName, btn, recordData) ? btn[fnName]() : btn.disable();
}
}, this);
},

toggleButtonFn:function(fnName, btn, selections) {
return true;
},

btnClickHandler:function(btn, e) {
if(!Ext.isDefined(btn.buttonType)) return;
if(Ext.isDefined(this[btn.buttonType+'BtnHandler'])) this[btn.buttonType+'BtnHandler'](btn);
}
};


Possible usage.



aPanel = new Ext.GridPanel({
... panel config ...

plugins:['top-tbar'],

// add toolbar
tbar:[],

tbarButtons:[{xtype:'button', disabled:true, text:'Edit', toggleOnSelectionChange:true}]

// return true to enable button for current selection
toggleButtonFn:function(wantState, btn, recordData) {
if(wantState == 'enable') {
if(!recordData.immutable) return true;
}
return false;
},

editBtnHandler:function(btn) {
console.log(btn);
}
});