Ext.define('Ext.calendar.CalendarPanel', {
extend: 'Ext.panel.Panel',
alias: 'widget.calendarpanel',
requires: [
'Ext.layout.container.Card',
'Ext.calendar.view.Day',
'Ext.calendar.view.Week',
'Ext.calendar.view.Month',
'Ext.calendar.form.EventDetails'
],
showDayView: true,
showWeekView: true,
showMonthView: true,
showNavBar: true,
todayText: 'Today',
showTodayText: true,
showTime: true,
dayText: 'Day',
weekText: 'Week',
monthText: 'Month',
layoutConfig: {
layoutOnCardChange: true,
deferredRender: true
},
startDate: new Date(),
initComponent: function() {
this.tbar = {
cls: 'ext-cal-toolbar',
border: true,
items: ['->',{
id: this.id + '-tb-prev',
handler: this.onPrevClick,
scope: this,
iconCls: 'x-tbar-page-prev'
}]
};
this.viewCount = 0;
if (this.showDayView) {
this.tbar.items.push({
id: this.id + '-tb-day',
text: this.dayText,
handler: this.onDayClick,
scope: this,
toggleGroup: 'tb-views'
});
this.viewCount++;
}
if (this.showWeekView) {
this.tbar.items.push({
id: this.id + '-tb-week',
text: this.weekText,
handler: this.onWeekClick,
scope: this,
toggleGroup: 'tb-views'
});
this.viewCount++;
}
if (this.showMonthView || this.viewCount == 0) {
this.tbar.items.push({
id: this.id + '-tb-month',
text: this.monthText,
handler: this.onMonthClick,
scope: this,
toggleGroup: 'tb-views'
});
this.viewCount++;
this.showMonthView = true;
}
this.tbar.items.push({
id: this.id + '-tb-next',
handler: this.onNextClick,
scope: this,
iconCls: 'x-tbar-page-next'
});
this.tbar.items.push('->');
var idx = this.viewCount - 1;
this.activeItem = this.activeItem === undefined ? idx: (this.activeItem > idx ? idx: this.activeItem);
if (this.showNavBar === false) {
delete this.tbar;
this.addCls('x-calendar-nonav');
}
this.callParent();
this.addEvents({
eventadd: true,
eventupdate: true,
eventdelete: true,
eventcancel: true,
viewchange: true
});
this.layout = 'card';
if (this.showDayView) {
var day = Ext.apply({
xtype: 'dayview',
title: this.dayText,
showToday: this.showToday,
showTodayText: this.showTodayText,
showTime: this.showTime
},
this.dayViewCfg);
day.id = this.id + '-day';
day.store = day.store || this.eventStore;
this.initEventRelay(day);
this.add(day);
}
if (this.showWeekView) {
var wk = Ext.applyIf({
xtype: 'weekview',
title: this.weekText,
showToday: this.showToday,
showTodayText: this.showTodayText,
showTime: this.showTime
},
this.weekViewCfg);
wk.id = this.id + '-week';
wk.store = wk.store || this.eventStore;
this.initEventRelay(wk);
this.add(wk);
}
if (this.showMonthView) {
var month = Ext.applyIf({
xtype: 'monthview',
title: this.monthText,
showToday: this.showToday,
showTodayText: this.showTodayText,
showTime: this.showTime,
listeners: {
'weekclick': {
fn: function(vw, dt) {
this.showWeek(dt);
},
scope: this
}
}
},
this.monthViewCfg);
month.id = this.id + '-month';
month.store = month.store || this.eventStore;
this.initEventRelay(month);
this.add(month);
}
this.add(Ext.applyIf({
xtype: 'eventeditform',
id: this.id + '-edit',
calendarStore: this.calendarStore,
listeners: {
'eventadd': {
scope: this,
fn: this.onEventAdd
},
'eventupdate': {
scope: this,
fn: this.onEventUpdate
},
'eventdelete': {
scope: this,
fn: this.onEventDelete
},
'eventcancel': {
scope: this,
fn: this.onEventCancel
}
}
},
this.editViewCfg));
},
initEventRelay: function(cfg) {
cfg.listeners = cfg.listeners || {};
cfg.listeners.afterrender = {
fn: function(c) {
this.relayEvents(c, ['eventsrendered', 'eventclick', 'eventover', 'eventout', 'dayclick',
'eventmove', 'datechange', 'rangeselect', 'eventdelete', 'eventresize', 'initdrag']);
},
scope: this,
single: true
};
},
afterRender: function() {
this.callParent(arguments);
this.body.addCls('x-cal-body');
Ext.defer(function() {
this.updateNavState();
this.fireViewChange();
}, 10, this);
},
onLayout: function() {
this.callParent();
if (!this.navInitComplete) {
this.updateNavState();
this.navInitComplete = true;
}
},
onEventAdd: function(form, rec) {
rec.data[Ext.calendar.data.EventMappings.IsNew.name] = false;
this.hideEditForm();
this.eventStore.add(rec);
this.eventStore.sync();
this.fireEvent('eventadd', this, rec);
},
onEventUpdate: function(form, rec) {
this.hideEditForm();
rec.commit();
this.eventStore.sync();
this.fireEvent('eventupdate', this, rec);
},
onEventDelete: function(form, rec) {
this.hideEditForm();
this.eventStore.remove(rec);
this.eventStore.sync();
this.fireEvent('eventdelete', this, rec);
},
onEventCancel: function(form, rec) {
this.hideEditForm();
this.fireEvent('eventcancel', this, rec);
},
showEditForm: function(rec) {
this.preEditView = this.layout.getActiveItem().id;
this.setActiveView(this.id + '-edit');
this.layout.getActiveItem().loadRecord(rec);
return this;
},
hideEditForm: function() {
if (this.preEditView) {
this.setActiveView(this.preEditView);
delete this.preEditView;
}
return this;
},
setActiveView: function(id){
var l = this.layout,
tb = this.getDockedItems('toolbar')[0];
if (tb) {
tb[id === this.id+'-edit' ? 'hide' : 'show']();
}
l.setActiveItem(id);
this.doComponentLayout();
this.activeView = l.getActiveItem();
if(id !== this.id+'-edit'){
if(id !== this.preEditView){
l.activeItem.setStartDate(this.startDate, true);
}
this.updateNavState();
}
this.fireViewChange();
},
fireViewChange: function() {
if (this.layout && this.layout.getActiveItem) {
var view = this.layout.getActiveItem();
if (view && view.getViewBounds) {
vb = view.getViewBounds();
var info = {
activeDate: view.getStartDate(),
viewStart: vb.start,
viewEnd: vb.end
};
};
this.fireEvent('viewchange', this, view, info);
}
},
updateNavState: function() {
if (this.showNavBar !== false) {
var item = this.layout.activeItem,
suffix = item.id.split(this.id + '-')[1],
btn = Ext.getCmp(this.id + '-tb-' + suffix);
if (btn) {
btn.toggle(true);
}
}
},
setStartDate: function(dt) {
this.layout.activeItem.setStartDate(dt, true);
this.updateNavState();
this.fireViewChange();
},
showWeek: function(dt) {
this.setActiveView(this.id + '-week');
this.setStartDate(dt);
},
onPrevClick: function() {
this.startDate = this.layout.activeItem.movePrev();
this.updateNavState();
this.fireViewChange();
},
onNextClick: function() {
this.startDate = this.layout.activeItem.moveNext();
this.updateNavState();
this.fireViewChange();
},
onDayClick: function() {
this.setActiveView(this.id + '-day');
},
onWeekClick: function() {
this.setActiveView(this.id + '-week');
},
onMonthClick: function() {
this.setActiveView(this.id + '-month');
},
getActiveView: function() {
return this.layout.activeItem;
}
});