App.js
6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Ext.App
* @extends Ext.util.Observable
* @author Chris Scott
*/
Ext.define('Ext.App', {
extend: 'Ext.util.Observable',
/***
* response status codes.
*/
STATUS_EXCEPTION : 'exception',
STATUS_VALIDATION_ERROR : "validation",
STATUS_ERROR: "error",
STATUS_NOTICE: "notice",
STATUS_OK: "ok",
STATUS_HELP: "help",
/**
* @cfg {Object} api
* remoting api. should be defined in your own config js.
*/
api: {
url: null,
type: null,
actions: {}
},
// private, ref to message-box Element.
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);
},
// @protected, onReady, executes when Ext.onReady fires.
onReady : function() {
// create the msgBox container. used for App.setAlert
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() {
/*
* set days to be however long you think cookies should last
*/
var days = ''; // expires when browser closes
if(days){
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var exptime = "; expires="+date.toGMTString();
} else {
var exptime = null;
}
// register provider with state manager.
Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', {
path: '/',
expires: exptime,
domain: null,
secure: false
}));
},
/**
* registerView
* register an application view component.
* @param {Object} view
*/
registerView : function(view) {
this.views.push(view);
},
/**
* getViews
* return list of registered views
*/
getViews : function() {
return this.views;
},
/**
* registerActions
* registers new actions for API
* @param {Object} actions
*/
registerActions : function(actions) {
Ext.apply(this.api.actions, actions);
},
/**
* getAPI
* return Ext Remoting api
*/
getAPI : function() {
return this.api;
},
/***
* setAlert
* show the message box. Aliased to addMessage
* @param {String} msg
* @param {Bool} status
*/
setAlert : function(status, msg) {
this.addMessage(status, msg);
},
/***
* adds a message to queue.
* @param {String} msg
* @param {Bool} status
*/
addMessage : function(status, msg) {
var delay = 3; // <-- default delay of msg box is 1 second.
if (status == false) {
delay = 5; // <-- when status is error, msg box delay is 3 seconds.
}
// add some smarts to msg's duration (div by 13.3 between 3 & 9 seconds)
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
*/
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
* @param {Object} status
*/
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, alias for Ext.state.Manager.set
* @param {Object} key
* @param {Object} value
*/
setViewState : function(key, value) {
Ext.state.Manager.set(key, value);
},
/***
* getViewState, aliaz for Ext.state.Manager.get
* @param {Object} cmd
*/
getViewState : function(key) {
return Ext.state.Manager.get(key);
},
/**
* t
* translation function. needs to be implemented. simply echos supplied word back currently.
* @param {String} to translate
* @return {String} translated.
*/
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);
}
});