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