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