Ext.define('Ext.calendar.App', {
requires: [
'Ext.Viewport',
'Ext.layout.container.Border',
'Ext.picker.Date',
'Ext.calendar.util.Date',
'Ext.calendar.CalendarPanel',
'Ext.calendar.data.MemoryCalendarStore',
'Ext.calendar.data.MemoryEventStore',
'Ext.calendar.data.Events',
'Ext.calendar.data.Calendars',
'Ext.calendar.form.EventWindow'
],
constructor : function() {
this.checkScrollOffset();
this.calendarStore = Ext.create('Ext.calendar.data.MemoryCalendarStore', {
data: Ext.calendar.data.Calendars.getData()
});
this.eventStore = Ext.create('Ext.calendar.data.MemoryEventStore', {
data: Ext.calendar.data.Events.getData()
});
Ext.create('Ext.Viewport', {
layout: 'border',
renderTo: 'calendar-ct',
items: [{
id: 'app-header',
region: 'north',
height: 35,
border: false,
contentEl: 'app-header-content'
},{
id: 'app-center',
title: '...',
region: 'center',
layout: 'border',
listeners: {
'afterrender': function(){
Ext.getCmp('app-center').header.addCls('app-center-header');
}
},
items: [{
id:'app-west',
region: 'west',
width: 179,
border: false,
items: [{
xtype: 'datepicker',
id: 'app-nav-picker',
cls: 'ext-cal-nav-picker',
listeners: {
'select': {
fn: function(dp, dt){
Ext.getCmp('app-calendar').setStartDate(dt);
},
scope: this
}
}
}]
},{
xtype: 'calendarpanel',
eventStore: this.eventStore,
calendarStore: this.calendarStore,
border: false,
id:'app-calendar',
region: 'center',
activeItem: 3,
monthViewCfg: {
showHeader: true,
showWeekLinks: true,
showWeekNumbers: true
},
listeners: {
'eventclick': {
fn: function(vw, rec, el){
this.showEditWindow(rec, el);
this.clearMsg();
},
scope: this
},
'eventover': function(vw, rec, el){
},
'eventout': function(vw, rec, el){
},
'eventadd': {
fn: function(cp, rec){
this.showMsg('Event '+ rec.data.Title +' was added');
},
scope: this
},
'eventupdate': {
fn: function(cp, rec){
this.showMsg('Event '+ rec.data.Title +' was updated');
},
scope: this
},
'eventcancel': {
fn: function(cp, rec){
},
scope: this
},
'viewchange': {
fn: function(p, vw, dateInfo){
if(this.editWin){
this.editWin.hide();
}
if(dateInfo){
Ext.getCmp('app-nav-picker').setValue(dateInfo.activeDate);
this.updateTitle(dateInfo.viewStart, dateInfo.viewEnd);
}
},
scope: this
},
'dayclick': {
fn: function(vw, dt, ad, el){
this.showEditWindow({
StartDate: dt,
IsAllDay: ad
}, el);
this.clearMsg();
},
scope: this
},
'rangeselect': {
fn: function(win, dates, onComplete){
this.showEditWindow(dates);
this.editWin.on('hide', onComplete, this, {single:true});
this.clearMsg();
},
scope: this
},
'eventmove': {
fn: function(vw, rec){
var mappings = Ext.calendar.data.EventMappings,
time = rec.data[mappings.IsAllDay.name] ? '' : ' \\a\\t g:i a';
rec.commit();
this.showMsg('Event '+ rec.data[mappings.Title.name] +' was moved to '+
Ext.Date.format(rec.data[mappings.StartDate.name], ('F jS'+time)));
},
scope: this
},
'eventresize': {
fn: function(vw, rec){
rec.commit();
this.showMsg('Event '+ rec.data.Title +' was updated');
},
scope: this
},
'eventdelete': {
fn: function(win, rec){
this.eventStore.remove(rec);
this.showMsg('Event '+ rec.data.Title +' was deleted');
},
scope: this
},
'initdrag': {
fn: function(vw){
if(this.editWin && this.editWin.isVisible()){
this.editWin.hide();
}
},
scope: this
}
}
}]
}]
});
},
showEditWindow : function(rec, animateTarget){
if(!this.editWin){
this.editWin = Ext.create('Ext.calendar.form.EventWindow', {
calendarStore: this.calendarStore,
listeners: {
'eventadd': {
fn: function(win, rec){
win.hide();
rec.data.IsNew = false;
this.eventStore.add(rec);
this.eventStore.sync();
this.showMsg('Event '+ rec.data.Title +' was added');
},
scope: this
},
'eventupdate': {
fn: function(win, rec){
win.hide();
rec.commit();
this.eventStore.sync();
this.showMsg('Event '+ rec.data.Title +' was updated');
},
scope: this
},
'eventdelete': {
fn: function(win, rec){
this.eventStore.remove(rec);
this.eventStore.sync();
win.hide();
this.showMsg('Event '+ rec.data.Title +' was deleted');
},
scope: this
},
'editdetails': {
fn: function(win, rec){
win.hide();
Ext.getCmp('app-calendar').showEditForm(rec);
}
}
}
});
}
this.editWin.show(rec, animateTarget);
},
updateTitle: function(startDt, endDt){
var p = Ext.getCmp('app-center'),
fmt = Ext.Date.format;
if(Ext.Date.clearTime(startDt).getTime() == Ext.Date.clearTime(endDt).getTime()){
p.setTitle(fmt(startDt, 'F j, Y'));
}
else if(startDt.getFullYear() == endDt.getFullYear()){
if(startDt.getMonth() == endDt.getMonth()){
p.setTitle(fmt(startDt, 'F j') + ' - ' + fmt(endDt, 'j, Y'));
}
else{
p.setTitle(fmt(startDt, 'F j') + ' - ' + fmt(endDt, 'F j, Y'));
}
}
else{
p.setTitle(fmt(startDt, 'F j, Y') + ' - ' + fmt(endDt, 'F j, Y'));
}
},
showMsg: function(msg){
Ext.fly('app-msg').update(msg).removeCls('x-hidden');
},
clearMsg: function(){
Ext.fly('app-msg').update('').addCls('x-hidden');
},
checkScrollOffset: function() {
var scrollbarWidth = Ext.getScrollbarSize ? Ext.getScrollbarSize().width : Ext.getScrollBarWidth();
if (scrollbarWidth < 3) {
Ext.getBody().addCls('x-no-scrollbar');
}
if (Ext.isWindows) {
Ext.getBody().addCls('x-win');
}
}
},
function() {
Ext.form.Basic.override({
reset: function() {
var me = this;
me.getFields().each(function(f) {
f.reset();
});
return me;
}
});
Ext.data.MemoryProxy.override({
updateOperation: function(operation, callback, scope) {
operation.setCompleted();
operation.setSuccessful();
Ext.callback(callback, scope || me, [operation]);
},
create: function() {
this.updateOperation.apply(this, arguments);
},
update: function() {
this.updateOperation.apply(this, arguments);
},
destroy: function() {
this.updateOperation.apply(this, arguments);
}
});
});