Ext.define('Ext.App', {
extend: 'Ext.util.Observable',
STATUS_EXCEPTION : 'exception',
STATUS_VALIDATION_ERROR : "validation",
STATUS_ERROR: "error",
STATUS_NOTICE: "notice",
STATUS_OK: "ok",
STATUS_HELP: "help",
api: {
url: null,
type: null,
actions: {}
},
msgCt : null,
constructor: function(config) {
this.views = [];
this.initStateProvider();
Ext.apply(this, config);
if (!this.api.actions) {
this.api.actions = {};
}
Ext.onReady(this.onReady, this);
Ext.App.superclass.constructor.apply(this, arguments);
},
onReady : function() {
this.msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
this.msgCt.setStyle('position', 'absolute');
this.msgCt.setStyle('z-index', 9999);
this.msgCt.setWidth(300);
},
initStateProvider : function() {
var days = '';
if(days){
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var exptime = "; expires="+date.toGMTString();
} else {
var exptime = null;
}
Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', {
path: '/',
expires: exptime,
domain: null,
secure: false
}));
},
registerView : function(view) {
this.views.push(view);
},
getViews : function() {
return this.views;
},
registerActions : function(actions) {
Ext.apply(this.api.actions, actions);
},
getAPI : function() {
return this.api;
},
setAlert : function(status, msg) {
this.addMessage(status, msg);
},
addMessage : function(status, msg) {
var delay = 3;
if (status == false) {
delay = 5;
}
delay = msg.length / 13.3;
if (delay < 3) {
delay = 3;
}
else if (delay > 9) {
delay = 9;
}
this.msgCt.alignTo(document, 't-t');
Ext.DomHelper.append(this.msgCt, {html:this.buildMessageBox(status, String.format.apply(String, Array.prototype.slice.call(arguments, 1)))}, true).slideIn('t').pause(delay).ghost("t", {remove:true});
},
buildMessageBox : function(title, msg) {
switch (title) {
case true:
title = this.STATUS_OK;
break;
case false:
title = this.STATUS_ERROR;
break;
}
return [
'<div class="app-msg">',
'<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
'<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3 class="x-icon-text icon-status-' + title + '">', title, '</h3>', msg, '</div></div></div>',
'<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
'</div>'
].join('');
},
decodeStatusIcon : function(status) {
var iconCls = '';
switch (status) {
case true:
case this.STATUS_OK:
iconCls = this.ICON_OK;
break;
case this.STATUS_NOTICE:
iconCls = this.ICON_NOTICE;
break;
case false:
case this.STATUS_ERROR:
iconCls = this.ICON_ERROR;
break;
case this.STATUS_HELP:
iconCls = this.ICON_HELP;
break;
}
return iconCls;
},
setViewState : function(key, value) {
Ext.state.Manager.set(key, value);
},
getViewState : function(key) {
return Ext.state.Manager.get(key);
},
t : function(words) {
return words;
},
handleResponse : function(res) {
if (res.type == this.STATUS_EXCEPTION) {
return this.handleException(res);
}
if (res.message.length > 0) {
this.setAlert(res.status, res.message);
}
},
handleException : function(res) {
Ext.MessageBox.alert(res.type.toUpperCase(), res.message);
}
});