App.js 6.1 KB

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