How to determine when a toolbar has been clicked in ExtJS 7.0.0 - extjs

I want to detect clicks on my toolbar, alternatively focus of toolbar.
The use-case has been extracted from a LiveSearchGrid which has a toolbar, the one seen in the code. The code provided renders fine, but no detect of click, focus, or anything else. Just nothing.
See below:
<div id="toolbar"></div>
<script type="text/javascript">
Ext.create('Ext.toolbar.Toolbar', {
renderTo: 'toolbar',
name: 'searchBar',
focusEl: 'toolbar',
listeners: {
focusenter: function () {
console.log('focusenter')
},
focus: function () {
console.log('focus')
}
},
items: [
{
xtype: 'tbtext',
html: 'Search',
listeners: {
focusenter: function () {
console.log('focusenter')
}
}
},
'Case Sensitive'
]
})
</script>
The following is plain JavaScript which solves my problem.
document.getElementById('toolbar').onclick = function () {
console.log('hello world');
}
What am I doing wrong?

You can set a DOM listener on the underlying Ext.Element using the element property of the listener config
Ext.create('Ext.toolbar.Toolbar', {
renderTo: 'toolbar',
name: 'searchBar',
focusEl: 'toolbar',
listeners: {
click: {
element: 'element', // bind to the underlying element
fn: function() {
console.log('Toolbar element was clicked!');
}
},
focusenter: {
element: 'element', // bind to the underlying element
fn: function() {
console.log('Toolbar element was focused!');
}
}
},
items: []
});
That property can be set to element or bodyElement

Related

ExtJS7: Anchor tag not honoring ext routes

If a ext view route is used with anchor tag, clicking on that link always opens a new tab. This force realod entire ext app in new tab. Is there a way to force anchor tag or anchor in html to redire ct to that view instead within app
{
xtype: 'component',
autoEl: {
tag: 'a',
html: 'Some Action',
href: '#someroute'
class: 'link-some-action'
},
listeners: {
click: function(){
console.warn("I AM TRAPPED");
}
}
}
OR
{
xtype: 'box',
html: ' Saome Action ',
listeners: {
click: function(){
console.warn("I AM TRAPPED");
}
}
}
In both case as illustrated, clicking on element opens a new tab, hence forcing app to load again
in your case, you should add a listener to el, here is fiddle
Ext.define('Controller', {
extend: 'Ext.app.ViewController',
alias: 'controller.mycontroller',
onElClick:function(){
console.log("I AM FREE");
this.redirectTo("test")
}
});
Ext.create('Ext.panel.Panel', {
style: "border: 1px solid red",
height: 100,
controller:"mycontroller",
title: "test",
renderTo: Ext.getBody(),
items: [{
xtype: 'component',
autoEl: {
tag: 'a',
html: 'Some Action',
href: '#someroute',
class: 'link-some-action'
},
listeners: {
el: {
click: "onElClick"
}
}
}]
})

How to bind listeners in ExtJs?

I am use extjs 6.5.3 modern toolkit and a am puzzled by the following question: how to bind listeners from viewModel to view?
example fiddle: https://fiddle.sencha.com/#view/editor&fiddle/30en
code:
Ext.application({
name: 'Fiddle',
launch: function () {
let container1 = Ext.create("Ext.panel.Panel", {
width: 640,
height: 480,
html: 'hello world!',
viewModel: {
data: {
// not works
listeners: {
onPlay: function () {
Ext.Msg.alert('onPlay', 'onPlay!');
}
},
title: 'test'
}
},
// works
//listeners: {
// onPlay: function () {
// Ext.Msg.alert('onPlay', 'onPlay!');
// }
//},
bind: {
listeners: '{listeners}',
title: '{title}'
},
controller: {
init: function () {
this.getView().fireEvent('onPlay', this.getView());
}
}
});
Ext.Viewport.add({
xtype: 'container',
padding: 10,
items: [container1]
});
}
});
Your code correctly binds the onPlay listener but does not work because the binding happens after your controller initialized. If you fire the onPlay event later e.g. using a timer, the alert will be shown. Check https://fiddle.sencha.com/#view/editor&fiddle/30et

ExtJS: How bind context menu action in controller?

Fiddle: https://fiddle.sencha.com/#fiddle/q02
Ext.define('EController', {
extend: 'Ext.app.ViewController',
alias: 'controller.test',
control: {
'#myAction': {
click: function() {
alert('My action')
}
}
}
});
var sellAction = Ext.create('Ext.Action', {
text: 'My action',
itemId: 'myAction'
});
var contextMenu = Ext.create('Ext.menu.Menu', {
items: [
sellAction
]
});
var grid = Ext.create('Ext.grid.Panel', {
controller:'test',
...................................
dockedItems: [{
xtype: 'toolbar',
items: [
sellAction
]
}],
Alert fires in toolbar button, but do not work in context menu.
How add same listener for context menu button and toolbar button? (Best way)
You can't. Simply because, unlike the toolbar, the context menu is not a part of the grid — it is just shown by the grid at e.getXY(). Therefore your control directive cannot reach the context menu.
An alternative would be to use action handlers instead of controller's control:
var sellAction = Ext.create('Ext.Action', {
text: 'My action',
itemId: 'myAction',
handler: function() {
alert('My action')
}
});
Fiddle fork: https://fiddle.sencha.com/#fiddle/q0d

can i hide extjs toolbar using action in other controller?

here is part of toolbar (buttons are not previewed) as usual:
VIEW
Ext.define('TEST.view.desktop.Toolbar', {
extend: 'Ext.panel.Panel',
alias: 'widget.testtoolbarX',
initComponent: function() {
debugger;
var me = this;
Ext.applyIf(me, {
dockedItems: [{
xtype: 'toolbar',
dock: 'top',
action: 'toolbarMouseOut',
iam trying to get action in controller, it works with buttons fine but not with whole toolbar
if i'm coding like this it works (but i dont need it)
CONTROLLER:
init: function() {
var me = this,
app = me.getApplication();
me.control({
'[xtype=testtoolbarX] button[action=toolbarMouseOut]': {
mouseout: me.onHideToolbar
},
I need it so, but iam not sure if toolbar is right name here. i tried everything and it still not going.
init: function() {
debugger;
var me = this,
app = me.getApplication();
me.control({
'[xtype=testtoolbarX] toolbar[action=toolbarMouseOut]': {
mouseout: me.onHideToolbar
},
please help me how can i react on MOUSEOUT in body of whole Toolbar??
As I mentioned in my comment, there's no mouseout event defined for the toolbar object itself. However, you can listen for that event on the el. Declaring it like this works:
{
xtype: 'toolbar',
dock: 'top',
listeners: {
el: {
mouseout: function() {
console.log('Mouseout on toolbar!');
}
}
},
items: []
}

click listener for tab panel in EXTJS

i use a tab panel in extjs. I want to display an alert when clicked on a tab. But i am not sure how.
This is what i do now:
{
xtype: 'tabpanel',
activeTab: 0,
region: 'center',
items: [
{
xtype: 'panel',
title: 'All',
items: [grid]
},
{
xtype: 'panel',
title: 'Closed'
},
{
xtype: 'panel',
title: 'Open'
}
],
listeners: {
click: function () {
alert('test');
}
}
}
How can is display All, Closed or Open when there is clicked on that tab?
There is no event for tab click in TabPanel, however you can bind into click event on each tab:
Ext.createWidget('tabpanel', {
items: [...],
listeners: {
render: function() {
this.items.each(function(i){
i.tab.on('click', function(){
alert(i.title);
});
});
}
}
});
Notice: this is ExtJS 4 based code.
I manage to do this by using tabchange event. In example below I used newCard.xtype property where value of xtype (i.e. task-archive) is just my panel with controls and corresponding xtype property.
Ext.define('ComplexBrigade.controller.taskArchive.TaskArchive', {
extend: 'Ext.app.Controller',
init: function() {
this.control({
'#tabWorks': {
tabchange: this.doTest
}
});
},
doTest: function ( tabPanel, newCard, oldCard, eOpts) {
switch (newCard.xtype) {
case "task-archive":
console.log("task-archive");
break;
case "task-creation":
console.log("task-creation");
break;
}
}
});

Resources