如果要求的UI控件不需要其他的细节的控件,也就是,仅仅是封装某部分的HTML元素的话,还要听凭布局管理器提供的大小尺寸、布局的调控,那么这个的扩展对象就是Ext.BoxComponent。例如,假设一个Logger类打算是简单地显示log信息,就可以这样定义:
autoHide:false,
onRender: function() {
Ext.ux.Logger.superclass.onRender.apply(this, arguments);
this.contextMenu = new Ext.menu.Menu({
items: [new Ext.menu.CheckItem({
id: 'debug',
text: 'Debug',
checkHandler: Ext.ux.Logger.prototype.onMenuCheck,
scope: this
}), new Ext.menu.CheckItem({
id: 'info',
text: 'Info',
checkHandler: Ext.ux.Logger.prototype.onMenuCheck,
scope: this
}), new Ext.menu.CheckItem({
id: 'warning',
text: 'Warning',
checkHandler: Ext.ux.Logger.prototype.onMenuCheck,
scope: this
}), new Ext.menu.CheckItem({
id: 'error',
text: 'Error',
checkHandler: Ext.ux.Logger.prototype.onMenuCheck,
scope: this
})]
});
this.el.on('contextmenu', this.onContextMenu, this, { stopEvent: true });
},
onContextMenu: function(e) {
this.contextMenu.logger = this;
this.contextMenu.showAt(e.getXY());
},
onMenuCheck: function(checkItem, state) {
var logger = checkItem.parentMenu.logger;
var cls = 'x-log-show-' + checkItem.id;
if (state) {
logger.el.addClass(cls);
} else {
logger.el.removeClass(cls);
}
},
debug: function(msg) {
this.tpl.insertFirst(this.el, ['debug', msg, new Date()]);
this.el.scrollTo("top", 0, true);
},
info: function(msg) {
this.tpl.insertFirst(this.el, ['info', msg, new Date()]);
this.el.scrollTo("top", 0, true);
},
warning: function(msg) {
this.tpl.insertFirst(this.el, ['warning', msg, new Date()]);
this.el.scrollTo("top", 0, true);
},
error: function(msg) {
this.tpl.insertFirst(this.el, ['error', msg, new Date()]);
this.el.scrollTo("top", 0, true);
}
});
还有一些样式需要加入:
怎么使用?我们将logger放进Window中试试!
运行截图如:
Logger用法:右键面板探出菜单,以显示内容。
我们把log的信息的HTML列表均放置在一个BoxComponent中。我们在onRender的阶段加入处理,使得右键菜单可以根据CSS样式类的名称操控logged条目的可见性。 位于该层次的对象还提供了特别的模板方法:
- onResize 此时此刻,BoxComponent的大小已经发生变化,此时可执行剩余的任务。
- onPosition 此时此刻,BoxComponent的定位已经发生变化,此时可执行剩余的任务。
此处披露的内容是《ExtJS 3详解与实践》
的补充内容,完整的资料敬请参阅《ExtJS 3 详解与实践》
一书的全面介绍。