I have few ExtJS components extended (Window,DataView etc) using Ext.extend method. I would like to add few additional properties to the extended. How do I add these into my component?
ExWindow = Ext.extend(Ext.Window,{
border:false,
initComponent: function() {
// Component Configuration...
var config = {
height: 500,
width: 500,
items: [{
xtype: 'simplepanel'
}]
};
Ext.apply(this, Ext.apply(this.initialConfig, config));
ExWindow.superclass.initComponent.apply(this, arguments);
},
onRender:function() {
ExWindow.superclass.onRender.apply(this, arguments);
}
});
ExWindow = Ext.extend(Ext.Window,{
border:false,
newPublicProperty: 0,
newPublicArray: new Array(),
initComponent: function() {
// Component Configuration...
var config = {
height: 500,
width: 500,
items: [{
xtype: 'simplepanel'
}]
};
Ext.apply(this, Ext.apply(this.initialConfig, config));
ExWindow.superclass.initComponent.apply(this, arguments);
},
onRender:function() {
ExWindow.superclass.onRender.apply(this, arguments);
},
newPublicMethod:function() {
}
});
newPublicProperty, newPublicArray, newPublicMethod - new additional properties of object.
Related
My aim is simple, for some needs, I have to test the "pop-up function" in ExtJS via the widget.window.
I've created a button in HTML and a pop-u in a JS file, when I click it, everything works fine, the pop-up is well displayed.
The HTML button is coded this way :
<input type="button" id="popup-map" value="Pop Up"/>
And the JS refers to the button this way :
Ext.application({
name: 'popup',
launch: function() {
var popup,
button = Ext.get('popup-map');
button.on('click', function(){
if (!popup) {
popup = Ext.create('widget.window', {
title: 'Pop-Up',
header: {
titlePosition: 2,
titleAlign: 'center'
},
border: false,
closable: true,
closeAction: 'hide',
width: 800,
minWidth: 400,
maxWidth: 1200,
height: 500,
minHeight: 550,
maxHeight: 800,
tools: [{type: 'help'}],
layout: {
type: 'border',
padding: 2
},
items: [
{
region: 'center',
xtype: 'tabpanel',
items: [
mappanel,
{
title: 'Description',
html: 'Attributs de l\'objet sous forme de tableau'
}
]
}
]
});
}
button.dom.disabled = true;
if (popup.isVisible()) {
popup.hide(this, function() {
button.dom.disabled = false;
});
} else {
popup.show(this, function() {
button.dom.disabled = false;
});
}
});
Problem, if I have two buttons that contains the id "popup-map", only the first one declared is working. I guess it's pretty normal the way I've coded it.
How can I call the popup contains in the JS file by clicking several buttons in HTML ?
Thanks :-)
Use a CSS class instead of a duplicated id. Duplicated ids are bad, you know that... Then use Ext.query instead of Ext.get. Your code should look something like this:
Ext.onReady(function() {
var popup;
function handler(button) {
if (!popup) {
// ...
}
// you've got button and popup, do your things
}
// adds the handler to every button with class 'popup-map' on the page
Ext.query('button.popup-map', function(button) {
button.on('click', handler);
});
});
I'm using Ext.onReady to wait for the DOM to be ready before searching for buttons on the page. That also gives us a closure for our local variables popup and handler.
Thanks to #rixo, here's the code working.
I've created a empty css class called customizer.
Ext.onReady(function() {
var popup, popup_visible;
function popup_constructor() {
//alert(this.getAttribute('pwet'));
if (!popup) {
popup = Ext.create('widget.window', {
title: 'Pop-Up',
id: 'popup',
header: {
titlePosition: 2,
titleAlign: 'center',
height: 30
},
border: false,
closable: true,
closeAction: 'hide',
width: 800,
minWidth: 400,
maxWidth: 1200,
height: 500,
minHeight: 550,
maxHeight: 800,
tools: [{type: 'help'}],
layout: {
type: 'border',
padding: 10
},
items: [
{
region: 'center',
xtype: 'tabpanel',
plain: true,
items: [
{
title: 'Carte',
html: 'On mettra la carte ici',
border: false,
},
{
title: 'Description',
html: 'Attributs de l\'objet sous forme de tableau',
border: false,
}
]
}
]
});
}
popup_visible = true;
if (popup.isVisible())
{
popup.hide(this, function() {
popup_visible = false;
});
}
else
{
popup.show(this, function() {
popup_visible = false;
});
}
}
var popup_show = Ext.query('.customizer');
Ext.each(popup_show, function (item) {
item = Ext.get(item);
item.on('click', popup_constructor);
}, this);
});
I have added an input field to Window's title bar (header). On Chrome selecting and editing the input field works, and I can still drag the window around. On Firefox I can drag the window around the viewport, but I am unable to select the input field and edit it. How should I correct this code so that it would work on both browsers?
Quick'n'dirty demonstration of the problem:
Ext.define('Demo.DemoWindow', {
extend: 'Ext.window.Window',
xtype: 'demowindow',
height: 300,
width: 400,
title: 'Window',
autoShow: true,
items: [{
xtype: 'button',
text : 'Press!',
listeners: {
click: function() {
var win = this.up('window');
var header = win.getHeader();
header.setTitle('');
var killDrag = false;
var dragEvent = win.dd.on({
beforedragstart: function(dd, e) {
if (killDrag) {
return false;
}
}
});
var field = Ext.create('Ext.form.field.Text', {
name: 'Title',
allowBlank: false,
value: 'Type here something!',
listeners: {
el: {
delegate: 'input',
mouseout: function() {
killDrag = false;
},
mouseenter: function() {
killDrag = true;
}
}
}
});
header.insert(0, field);
}
}
}]
});
Ext.application({
name: 'Demo',
launch: function() {
Ext.create('Ext.container.Viewport', {
layout: 'absolute',
items: [
{
xtype: 'demowindow',
x: 20,
y: 20,
}
]
});
}
});
Using the mouseover event instead of mouseenter seems to work well with both.
I have a Panel with two buttons that is used inside another panel. Right now it renders the buttons correctly but I'd like to add a margin between them. How to configure layout properly to get this ?
return Ext.create('Ext.panel.Panel', {
width: 220,
height: 35,
border: false,
collapsible: false,
renderTo: renderTo,
items : [
{
xtype: 'button',
scale: 'medium',
text: this.exportButtonText,
handler: function() {
var form = this.form.getForm();
if (form.isValid()) {
var values = form.getValues();
form.reset();
this.plugin.printSettings = values;
this.progressBar.show();
this.plugin.doPrint();
}
},
scope: this
},
{
xtype: 'button',
scale: 'medium',
text: this.cancelButtonText,
handler: function() {
me.close();
},
scope: this
}
]
});
Or add this to left button definition:
{
margin: '0 5px 0 0'
}
Add this in between them:
{
xtype: 'tbspacer',
flex: 1
}
This code is working in Extjs 4.0.2a
but when converted to 4.1 it no longer works and gives an error
Uncaught TypeError: Cannot call method 'query' of undefined
Ext.onReady(function() {
var panel = new Ext.Panel({
renderTo: divtag,
draggable: {
insertProxy: false,
onDrag: function(e) {
var el = this.proxy.getEl();
this.x = el.getLeft(true);
this.y = el.getTop(true);
},
endDrag: function(e) {
this.panel.setPosition(this.x, this.y);
}
},
title: 'Panel',
width: 200,
height: 100,
x: 20,
y: 20
});
});
Apparently there is a bug in this version of Ext. It wont work even if you try default D'n'D for panel like this:
Ext.onReady(function() {
var panel = new Ext.Panel({
renderTo: 'divtag',
draggable: true,
title: 'Panel',
width: 200,
height:100,
x: 20,
y: 20
}); //panel.show(); });
});
I menage to patch the code to work the way you want it, this code should work:
Ext.onReady(function() {
var panel = new Ext.Panel({
renderTo: 'divtag',
draggable: {
insertProxy: false,
onDrag: function(e) {
var el = this.proxy.getEl();
this.x = el.getX();
this.y = el.getY();
},
endDrag: function(e) {
panel.setPosition(this.x,this.y);
},
alignElWithMouse: function() {
panel.dd.superclass.superclass.alignElWithMouse.apply(panel.dd, arguments);
this.proxy.sync();
}
},
title: 'Panel',
width: 200,
height:100,
x: 20,
y: 20
}); //panel.show(); });
});
As a side note I should probably advice you not to do this anyway, because you can define your own DD for panel that you can use, and even better Ext already have one defined, so you can just override Ext panel to use default Ext.util.ComponentDragger, or in code, I advice you to do this:
Ext.override(Ext.panel.Panel, {
initDraggable: function() {
var me = this,
ddConfig;
if (!me.header) {
me.updateHeader(true);
}
if (me.header) {
ddConfig = Ext.applyIf({
el: me.el,
delegate: '#' + me.header.id
}, me.draggable);
// Add extra configs if Window is specified to be constrained
if (me.constrain || me.constrainHeader) {
ddConfig.constrain = me.constrain;
ddConfig.constrainDelegate = me.constrainHeader;
ddConfig.constrainTo = me.constrainTo || me.container;
}
me.dd = Ext.create('Ext.util.ComponentDragger', this, ddConfig);
me.relayEvents(me.dd, ['dragstart', 'drag', 'dragend']);
}
}
});
var panel = Ext.create('Ext.panel.Panel', {
id: 'test',
renderTo: 'divtag',
draggable: true,
floating: true,
title: 'Panel',
width: 200,
height:100,
x: 20,
y: 20
});
Code for a initDraggable function in panel override is taken from current stable version of Ext.window.Window.initDraggable method.
I was able to get it working in 4.1: you have to add quotes around the id of the renderTo element, like:
renderTo : 'divtag',
Without quotes it was looking for an undefined variable named divtag.
Once I ran that I got no errors, and then I just did panel.show() to render it.
Just a suggestion: a Window component is a specialized Panel that has a floating mixin - might be all you need.
I have a some class
AddOrgWindowUI = Ext.extend(Ext.Window, {
title: 'form',
width: 400,
height: 198,
layout: 'form',
padding: 5,
initComponent: function() {
this.items = [
{
xtype: 'textfield',
fieldLabel: 'parapapa',
anchor: '95%',
value: m,
emptyText: 'perapapa'
}
];
AddOrgWindowUI.superclass.initComponent.call(this);
}});
when I create an object var AddOrgWindowForm = new AddOrgWindowUI('aaa'); I want to get arg ('aaa') to my new form value (value m). How get it?
Im trying initComponent: function(m) { and thats not working.
The initComponent function is called internally on one of the base classes of Ext.Window. You shouldn't try to call it directly. That is why it won't handle your own parameters.
So I recommend you to use the standard form parameters when extending ExtJS classes.
It is as simple as initializing the object with the property or methods you want to override (or insert in case the property is not in there already). And then just using the this keyword to access them.
This is possible because for every Ext.Component and its subclasses, the first parameter passed to the constructor should be an object, and every member in that object will be copied to the new object constructed. And most ExtJS classes extend directly or indirectly from Ext.Component, and you are extending from Ext.Window which extends from Ext.Component too.
Here you have your example fixed:
var AddOrgWindowUI = Ext.extend(Ext.Window, {
title: 'form',
width: 400,
height: 198,
layout: 'form',
padding: 5,
initComponent: function() {
this.items = [
{
xtype: 'textfield',
fieldLabel: 'parapapa',
anchor: '95%',
value: this.initialValue,
emptyText: 'perapapa'
}
];
AddOrgWindowUI.superclass.initComponent.call(this);
}
});
function test() {
var AddOrgWindowForm = new AddOrgWindowUI({initialValue:'aaa'});
AddOrgWindowForm.show();
}
pass m as an arg of initComponent:
edit:
AddOrgWindowUI = function(input) {
var m = input;
return Ext.extend(Ext.Window, {
title: 'form',
width: 400,
height: 198,
layout: 'form',
padding: 5,
initComponent: function() {
this.items = [
{
xtype: 'textfield',
fieldLabel: 'parapapa',
anchor: '95%',
value: m,
emptyText: 'perapapa'
}
];
AddOrgWindowUI.superclass.initComponent.call(this);
}
});
}