用Ext3.0的API主页框架做的项目

最近一段时间公司里没有项目,有空就专研ext,顺便用它完成了一个小项目。Ext API的主页就是一个典型的tab框架,通过它可以创建如同FireFox浏览器一样,打开一个页面就往上面添加一个tab,方便用户的使用,可以在同一个浏览器中浏览多个页面。
左边是一个树形结构的菜单,右边是一个tab的页面,左边菜单的代码为
/*!
* Ext JS Library 3.0.0
* Copyright(c) 2006-2009 Ext JS, LLC
* licensing@extjs.com
* http://www.extjs.com/license
*/

Docs.classData ={"id":"apidocs","iconCls":"icon-docs","text":"图书管理系统","singleClickExpand":true,"children":[

{"id":"apidocs3","text":"图书管理","iconCls":"icon-pkg","cls":"package","singleClickExpand":true, children:[

{"href":"output/book.jsp","text":"图书信息管理","id":"book","isClass":true,"iconCls":"icon-cls","cls":"cls","leaf":true}]},


{"id":"apidocs1","iconCls":"icon-docs","text":"系统管理","singleClickExpand":true,"children":[

{"href":"output/company.jsp","text":"分公司管理","id":"company","isClass":true,"iconCls":"icon-cmp","cls":"cls","leaf":true}
,
{"href":"output/department.jsp","text":"部门管理管理","id":"department","isClass":true,"iconCls":"icon-cls","cls":"cls","leaf":true}
,
{"href":"output/user.jsp","text":"用户管理","id":"user","isClass":true,"iconCls":"icon-cmp","cls":"cls","leaf":true}
,
{"href":"output/business.jsp","text":"职务管理","id":"business","isClass":true,"iconCls":"icon-cls","cls":"cls","leaf":true}]},

{"id":"apidocs2","iconCls":"icon-docs","text":"科目和分类管理","singleClickExpand":true,"children":[
{"href":"output/subject.jsp","text":"科目管理","id":"subject","isClass":true,"iconCls":"icon-cls","cls":"cls","leaf":true}
,
{"href":"output/class.jsp","text":"分类管理","id":"class","isClass":true,"iconCls":"icon-cmp","cls":"cls","leaf":true}

]}
]};

Docs.icons = {

"book":"icon-cls"
,
"company":"icon-cmp"
,
"department":"icon-cls"
,
"user":"icon-cmp"
,
"business":"icon-cls"
,
"subject":"icon-cls"
,
"class":"icon-cmp"
};
这是菜单的结构,我没有全部改源码,只改成了连接到我的jsp页面,iconCls属性还是和原来的一样。

/*!
* Ext JS Library 3.0.0
* Copyright(c) 2006-2009 Ext JS, LLC
* licensing@extjs.com
* http://www.extjs.com/license
*/

//这个地方要特别注意,要加上这句话,要不他默认的配置连接地址是到EXT网站上去下载这个图片,所以我们要改这个地址,让他不要到网上去下载。
Ext.BLANK_IMAGE_URL = '/book/resources/images/default/s.gif';

Docs = {};
ApiPanel = function() {
ApiPanel.superclass.constructor.call(this, {
id:'api-tree',
region:'west',
split:true,
width: 220,
minSize: 175,
maxSize: 450,
collapsible: true,
margins:'0 0 5 5',
cmargins:'0 0 0 0',
rootVisible:false,
lines:false,
autoScroll:true,
animCollapse:false,
animate: false,
collapseMode:'mini',
loader: new Ext.tree.TreeLoader({
preloadChildren: true,
clearOnLoad: false
}),
root: new Ext.tree.AsyncTreeNode({
id:'root',
expanded:true,
children:[Docs.classData]
}),
collapseFirst:false
});
this.getSelectionModel().on('beforeselect', function(sm, node){
return node.isLeaf();
});
};
Ext.extend(ApiPanel, Ext.tree.TreePanel, {
//当选择了右边的tap就自动选择左边的菜单
selectClass : function(cls){
if(cls){
var parts = cls.split('.');
var last = parts.length-1;
var res = [];
var pkg = [];
for(var i = 0; i < last; i++){
var p = parts[i];
var fc = p.charAt(0);
var staticCls = fc.toUpperCase() == fc;
if(p == 'Ext' || !staticCls){
pkg.push(p);
res[i] = 'pkg-'+pkg.join('.');
}else if(staticCls){
--last;
res.splice(i, 1);
}
}
res[last] = cls
}
}
});
DocPanel = Ext.extend(Ext.Panel, {
closable: true,
autoScroll:true,
initComponent : function(){
var ps = this.cclass.split('.');
this.title = ps[ps.length-1];
DocPanel.superclass.initComponent.call(this);
},
scrollToMember : function(member){
var el = Ext.fly(this.cclass + '-' + member);
if(el){
var top = (el.getOffsetsTo(this.body)[1]) + this.body.dom.scrollTop;
this.body.scrollTo('top', top-25, {duration:.75, callback: this.hlMember.createDelegate(this, [member])});
}
},
scrollToSection : function(id){
var el = Ext.getDom(id);
if(el){
var top = (Ext.fly(el).getOffsetsTo(this.body)[1]) + this.body.dom.scrollTop;
this.body.scrollTo('top', top-25, {duration:.5, callback: function(){
Ext.fly(el).next('h2').pause(.2).highlight('#8DB2E3', {attr:'color'});
}});
}
},
hlMember : function(member){
var el = Ext.fly(this.cclass + '-' + member);
if(el){
el.up('tr').highlight('#cadaf9');
}
}
});
MainPanel = function(){
MainPanel.superclass.constructor.call(this, {
id:'doc-body',
region:'center',
margins:'0 5 5 0',
resizeTabs: true,
minTabWidth: 135,
tabWidth: 135,
closable: true,
enableTabScroll: true,
activeTab: 0,
items: {
id:'welcome-panel',
title: '欢迎',
autoLoad: {url: 'welcome.html', callback: this.initSearch, scope: this},
iconCls:'icon-docs',
autoScroll: true,
tbar: [
]
}
});
};

Ext.extend(MainPanel, Ext.TabPanel, {
initEvents : function(){
MainPanel.superclass.initEvents.call(this);
this.body.on('click', this.onClick, this);
},
onClick: function(e, target){
if(target = e.getTarget('a:not(.exi)', 3)){
var cls = Ext.fly(target).getAttributeNS('ext', 'cls');
e.stopEvent();
if(cls){
var member = Ext.fly(target).getAttributeNS('ext', 'member');
this.loadClass(target.href, cls, member);
}else if(target.className == 'inner-link'){
this.getActiveTab().scrollToSection(target.href.split('#')[1]);
}else{
window.open(target.href);
}
}else if(target = e.getTarget('.micon', 2)){
e.stopEvent();
var tr = Ext.fly(target.parentNode);
if(tr.hasClass('expandable')){
tr.toggleClass('expanded');
}
}
},
loadClass : function(href, cls, member){
var id = 'docs-' + cls;
var tab = this.getComponent(id);
if(tab){
this.setActiveTab(tab);
if(member){
tab.scrollToMember(member);
}
}else{
var autoLoad = {url: href,scripts: true};
if(member){
autoLoad.callback = function(){
Ext.getCmp(id).scrollToMember(member);
}
}
var p = this.add(new DocPanel({
id: id,
cclass : cls,
autoLoad: autoLoad,
iconCls: Docs.icons[cls]
}));
this.setActiveTab(p);
}
}
});
Ext.onReady(function(){
Ext.QuickTips.init();

var api = new ApiPanel();
var mainPanel = new MainPanel();
api.on('click', function(node, e){
if(node.isLeaf()){
e.stopEvent();
mainPanel.loadClass(node.attributes.href, node.text);
}
});
mainPanel.on('tabchange', function(tp, tab){
api.selectClass(tab.cclass);
});
var hd = new Ext.Panel({
border: false,
layout:'anchor',
region:'north',
cls: 'docs-header',
height:60,
items: [{
xtype:'box',
el:'header',
border:false,
anchor: 'none -25'
},
new Ext.Toolbar({
cls:'top-toolbar',
items:[ ' ',
new Ext.form.TextField({
width: 200,
emptyText:'查询菜单',
listeners:{
render: function(f){
f.el.on('keydown', filterTree, f, {buffer: 350});
}
}
}), ' ', ' ',
{
iconCls: 'icon-expand-all',
tooltip: '全部展开',
handler: function(){ api.root.expand(true); }
}, '-', {
iconCls: 'icon-collapse-all',
tooltip: '全部收缩',
handler: function(){ api.root.collapse(true); }
}/**, '->', {
tooltip:'Hide Inherited Members',
iconCls: 'icon-hide-inherited',
enableToggle: true,
toggleHandler : function(b, pressed){
mainPanel[pressed ? 'addClass' : 'removeClass']('hide-inherited');
}
}, '-', {
tooltip:'Expand All Members',
iconCls: 'icon-expand-members',
enableToggle: true,
toggleHandler : function(b, pressed){
mainPanel[pressed ? 'addClass' : 'removeClass']('full-details');
}
}*/]
})]
})
var viewport = new Ext.Viewport({
layout:'border',
items:[ hd, api, mainPanel ]
});

var page = window.location.href.split('?')[1];
if(page){
var ps = Ext.urlDecode(page);
var cls = ps['class'];
mainPanel.loadClass('output/' + cls + '.html', cls, ps.member);
}
viewport.doLayout();
setTimeout(function(){
Ext.get('loading').remove();
Ext.get('loading-mask').fadeOut({remove:true});
}, 250);
var filter = new Ext.tree.TreeFilter(api, {
clearBlank: true,
autoClear: true
});
var hiddenPkgs = [];
function filterTree(e){
var text = e.target.value;
Ext.each(hiddenPkgs, function(n){
n.ui.show();
});
if(!text){
filter.clear();
return;
}
api.expandAll();
var re = new RegExp('^' + Ext.escapeRe(text), 'i');
filter.filterBy(function(n){
return !n.attributes.isClass || re.test(n.text);
});
hiddenPkgs = [];
api.root.cascade(function(n){
if(!n.attributes.isClass && n.ui.ctNode.offsetHeight < 3){
n.ui.hide();
hiddenPkgs.push(n);
}
});
}
});
Ext.app.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
initComponent : function(){
Ext.app.SearchField.superclass.initComponent.call(this);
this.on('specialkey', function(f, e){
if(e.getKey() == e.ENTER){
this.onTrigger2Click();
}
}, this);
},
validationEvent:false,
validateOnBlur:false,
trigger1Class:'x-form-clear-trigger',
trigger2Class:'x-form-search-trigger',
hideTrigger1:true,
width:180,
hasSearch : false,
paramName : 'query',
onTrigger1Click : function(){
if(this.hasSearch){

this.store.removeAll();
this.el.dom.value = '';
this.triggers[0].hide();
this.hasSearch = false;
this.focus();
}
},
onTrigger2Click : function(){
var v = this.getRawValue();
if(v.length < 1){
this.onTrigger1Click();
return;
}
if(v.length < 2){
return;
}
var o = {start: 0};
this.store.reload({params:o});
this.hasSearch = true;
this.triggers[0].show();
this.focus();
}
});
Ext.ux.SelectBox = function(config){
this.searchResetDelay = 1000;
config = config || {};
config = Ext.apply(config || {}, {
editable: false,
forceSelection: true,
rowHeight: false,
lastSearchTerm: false,
triggerAction: 'all',
mode: 'local'
});
Ext.ux.SelectBox.superclass.constructor.apply(this, arguments);
this.lastSelectedIndex = this.selectedIndex || 0;
};

Ext.extend(Ext.ux.SelectBox, Ext.form.ComboBox, {
lazyInit: false,
initEvents : function(){
Ext.ux.SelectBox.superclass.initEvents.apply(this, arguments);
this.el.on('keydown', this.keySearch, this, true);
this.cshTask = new Ext.util.DelayedTask(this.clearSearchHistory, this);
},
keySearch : function(e, target, options) {
var raw = e.getKey();
var key = String.fromCharCode(raw);
var startIndex = 0;
if( !this.store.getCount() ) {
return;
}
switch(raw) {
case Ext.EventObject.HOME:
e.stopEvent();
this.selectFirst();
return;
case Ext.EventObject.END:
e.stopEvent();
this.selectLast();
return;
case Ext.EventObject.PAGEDOWN:
this.selectNextPage();
e.stopEvent();
return;
case Ext.EventObject.PAGEUP:
this.selectPrevPage();
e.stopEvent();
return;
}
// skip special keys other than the shift key
if( (e.hasModifier() && !e.shiftKey) || e.isNavKeyPress() || e.isSpecialKey() ) {
return;
}
if( this.lastSearchTerm == key ) {
startIndex = this.lastSelectedIndex;
}
this.search(this.displayField, key, startIndex);
this.cshTask.delay(this.searchResetDelay);
},
onRender : function(ct, position) {
this.store.on('load', this.calcRowsPerPage, this);
Ext.ux.SelectBox.superclass.onRender.apply(this, arguments);
if( this.mode == 'local' ) {
this.calcRowsPerPage();
}
},
onSelect : function(record, index, skipCollapse){
if(this.fireEvent('beforeselect', this, record, index) !== false){
this.setValue(record.data[this.valueField || this.displayField]);
if( !skipCollapse ) {
this.collapse();
}
this.lastSelectedIndex = index + 1;
this.fireEvent('select', this, record, index);
}
},
render : function(ct) {
Ext.ux.SelectBox.superclass.render.apply(this, arguments);
if( Ext.isSafari ) {
this.el.swallowEvent('mousedown', true);
}
this.el.unselectable();
this.innerList.unselectable();
this.trigger.unselectable();
this.innerList.on('mouseup', function(e, target, options) {
if( target.id && target.id == this.innerList.id ) {
return;
}
this.onViewClick();
}, this);
this.innerList.on('mouseover', function(e, target, options) {
if( target.id && target.id == this.innerList.id ) {
return;
}
this.lastSelectedIndex = this.view.getSelectedIndexes()[0] + 1;
this.cshTask.delay(this.searchResetDelay);
}, this);
this.trigger.un('click', this.onTriggerClick, this);
this.trigger.on('mousedown', function(e, target, options) {
e.preventDefault();
this.onTriggerClick();
}, this);
this.on('collapse', function(e, target, options) {
Ext.getDoc().un('mouseup', this.collapseIf, this);
}, this, true);
this.on('expand', function(e, target, options) {
Ext.getDoc().on('mouseup', this.collapseIf, this);
}, this, true);
},
clearSearchHistory : function() {
this.lastSelectedIndex = 0;
this.lastSearchTerm = false;
},
selectFirst : function() {
this.focusAndSelect(this.store.data.first());
},
selectLast : function() {
this.focusAndSelect(this.store.data.last());
},
selectPrevPage : function() {
if( !this.rowHeight ) {
return;
}
var index = Math.max(this.selectedIndex-this.rowsPerPage, 0);
this.focusAndSelect(this.store.getAt(index));
},
selectNextPage : function() {
if( !this.rowHeight ) {
return;
}
var index = Math.min(this.selectedIndex+this.rowsPerPage, this.store.getCount() - 1);
this.focusAndSelect(this.store.getAt(index));
},
search : function(field, value, startIndex) {
field = field || this.displayField;
this.lastSearchTerm = value;
var index = this.store.find.apply(this.store, arguments);
if( index !== -1 ) {
this.focusAndSelect(index);
}
},
focusAndSelect : function(record) {
var index = typeof record === 'number' ? record : this.store.indexOf(record);
this.select(index, this.isExpanded());
this.onSelect(this.store.getAt(record), index, this.isExpanded());
},
calcRowsPerPage : function() {
if( this.store.getCount() ) {
this.rowHeight = Ext.fly(this.view.getNode(0)).getHeight();
this.rowsPerPage = this.maxHeight / this.rowHeight;
} else {
this.rowHeight = false;
}
}
});
Ext.Ajax.on('requestcomplete', function(ajax, xhr, o){
if(typeof urchinTracker == 'function' && o && o.url){
urchinTracker(o.url);
}
});

这个添加tab也只修改了一些地方,还没有简化到最优。由于项目大于10M没法上传,如有需要请留言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值