App.js 6.7 KB

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