StatusBar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * Basic status bar component that can be used as the bottom toolbar of any {@link Ext.Panel}. In addition to
  3. * supporting the standard {@link Ext.toolbar.Toolbar} interface for adding buttons, menus and other items, the StatusBar
  4. * provides a greedy status element that can be aligned to either side and has convenient methods for setting the
  5. * status text and icon. You can also indicate that something is processing using the {@link #showBusy} method.
  6. *
  7. * Ext.create('Ext.Panel', {
  8. * title: 'StatusBar',
  9. * // etc.
  10. * bbar: Ext.create('Ext.ux.StatusBar', {
  11. * id: 'my-status',
  12. *
  13. * // defaults to use when the status is cleared:
  14. * defaultText: 'Default status text',
  15. * defaultIconCls: 'default-icon',
  16. *
  17. * // values to set initially:
  18. * text: 'Ready',
  19. * iconCls: 'ready-icon',
  20. *
  21. * // any standard Toolbar items:
  22. * items: [{
  23. * text: 'A Button'
  24. * }, '-', 'Plain Text']
  25. * })
  26. * });
  27. *
  28. * // Update the status bar later in code:
  29. * var sb = Ext.getCmp('my-status');
  30. * sb.setStatus({
  31. * text: 'OK',
  32. * iconCls: 'ok-icon',
  33. * clear: true // auto-clear after a set interval
  34. * });
  35. *
  36. * // Set the status bar to show that something is processing:
  37. * sb.showBusy();
  38. *
  39. * // processing....
  40. *
  41. * sb.clearStatus(); // once completeed
  42. *
  43. */
  44. Ext.define('Ext.ux.statusbar.StatusBar', {
  45. extend: 'Ext.toolbar.Toolbar',
  46. alternateClassName: 'Ext.ux.StatusBar',
  47. alias: 'widget.statusbar',
  48. requires: ['Ext.toolbar.TextItem'],
  49. /**
  50. * @cfg {String} statusAlign
  51. * The alignment of the status element within the overall StatusBar layout. When the StatusBar is rendered,
  52. * it creates an internal div containing the status text and icon. Any additional Toolbar items added in the
  53. * StatusBar's {@link #cfg-items} config, or added via {@link #method-add} or any of the supported add* methods, will be
  54. * rendered, in added order, to the opposite side. The status element is greedy, so it will automatically
  55. * expand to take up all sapce left over by any other items. Example usage:
  56. *
  57. * // Create a left-aligned status bar containing a button,
  58. * // separator and text item that will be right-aligned (default):
  59. * Ext.create('Ext.Panel', {
  60. * title: 'StatusBar',
  61. * // etc.
  62. * bbar: Ext.create('Ext.ux.statusbar.StatusBar', {
  63. * defaultText: 'Default status text',
  64. * id: 'status-id',
  65. * items: [{
  66. * text: 'A Button'
  67. * }, '-', 'Plain Text']
  68. * })
  69. * });
  70. *
  71. * // By adding the statusAlign config, this will create the
  72. * // exact same toolbar, except the status and toolbar item
  73. * // layout will be reversed from the previous example:
  74. * Ext.create('Ext.Panel', {
  75. * title: 'StatusBar',
  76. * // etc.
  77. * bbar: Ext.create('Ext.ux.statusbar.StatusBar', {
  78. * defaultText: 'Default status text',
  79. * id: 'status-id',
  80. * statusAlign: 'right',
  81. * items: [{
  82. * text: 'A Button'
  83. * }, '-', 'Plain Text']
  84. * })
  85. * });
  86. */
  87. /**
  88. * @cfg {String} [defaultText='']
  89. * The default {@link #text} value. This will be used anytime the status bar is cleared with the
  90. * `useDefaults:true` option.
  91. */
  92. /**
  93. * @cfg {String} [defaultIconCls='']
  94. * The default {@link #iconCls} value (see the iconCls docs for additional details about customizing the icon).
  95. * This will be used anytime the status bar is cleared with the `useDefaults:true` option.
  96. */
  97. /**
  98. * @cfg {String} text
  99. * A string that will be <b>initially</b> set as the status message. This string
  100. * will be set as innerHTML (html tags are accepted) for the toolbar item.
  101. * If not specified, the value set for {@link #defaultText} will be used.
  102. */
  103. /**
  104. * @cfg {String} [iconCls='']
  105. * A CSS class that will be **initially** set as the status bar icon and is
  106. * expected to provide a background image.
  107. *
  108. * Example usage:
  109. *
  110. * // Example CSS rule:
  111. * .x-statusbar .x-status-custom {
  112. * padding-left: 25px;
  113. * background: transparent url(images/custom-icon.gif) no-repeat 3px 2px;
  114. * }
  115. *
  116. * // Setting a default icon:
  117. * var sb = Ext.create('Ext.ux.statusbar.StatusBar', {
  118. * defaultIconCls: 'x-status-custom'
  119. * });
  120. *
  121. * // Changing the icon:
  122. * sb.setStatus({
  123. * text: 'New status',
  124. * iconCls: 'x-status-custom'
  125. * });
  126. */
  127. /**
  128. * @cfg {String} cls
  129. * The base class applied to the containing element for this component on render.
  130. */
  131. cls : 'x-statusbar',
  132. /**
  133. * @cfg {String} busyIconCls
  134. * The default {@link #iconCls} applied when calling {@link #showBusy}.
  135. * It can be overridden at any time by passing the `iconCls` argument into {@link #showBusy}.
  136. */
  137. busyIconCls : 'x-status-busy',
  138. /**
  139. * @cfg {String} busyText
  140. * The default {@link #text} applied when calling {@link #showBusy}.
  141. * It can be overridden at any time by passing the `text` argument into {@link #showBusy}.
  142. */
  143. busyText : 'Loading...',
  144. /**
  145. * @cfg {Number} autoClear
  146. * The number of milliseconds to wait after setting the status via
  147. * {@link #setStatus} before automatically clearing the status text and icon.
  148. * Note that this only applies when passing the `clear` argument to {@link #setStatus}
  149. * since that is the only way to defer clearing the status. This can
  150. * be overridden by specifying a different `wait` value in {@link #setStatus}.
  151. * Calls to {@link #clearStatus} always clear the status bar immediately and ignore this value.
  152. */
  153. autoClear : 5000,
  154. /**
  155. * @cfg {String} emptyText
  156. * The text string to use if no text has been set. If there are no other items in
  157. * the toolbar using an empty string (`''`) for this value would end up in the toolbar
  158. * height collapsing since the empty string will not maintain the toolbar height.
  159. * Use `''` if the toolbar should collapse in height vertically when no text is
  160. * specified and there are no other items in the toolbar.
  161. */
  162. emptyText : '&#160;',
  163. // private
  164. activeThreadId : 0,
  165. // private
  166. initComponent : function(){
  167. var right = this.statusAlign === 'right';
  168. this.callParent(arguments);
  169. this.currIconCls = this.iconCls || this.defaultIconCls;
  170. this.statusEl = Ext.create('Ext.toolbar.TextItem', {
  171. cls: 'x-status-text ' + (this.currIconCls || ''),
  172. text: this.text || this.defaultText || ''
  173. });
  174. if (right) {
  175. this.cls += ' x-status-right';
  176. this.add('->');
  177. this.add(this.statusEl);
  178. } else {
  179. this.insert(0, this.statusEl);
  180. this.insert(1, '->');
  181. }
  182. },
  183. /**
  184. * Sets the status {@link #text} and/or {@link #iconCls}. Also supports automatically clearing the
  185. * status that was set after a specified interval.
  186. *
  187. * Example usage:
  188. *
  189. * // Simple call to update the text
  190. * statusBar.setStatus('New status');
  191. *
  192. * // Set the status and icon, auto-clearing with default options:
  193. * statusBar.setStatus({
  194. * text: 'New status',
  195. * iconCls: 'x-status-custom',
  196. * clear: true
  197. * });
  198. *
  199. * // Auto-clear with custom options:
  200. * statusBar.setStatus({
  201. * text: 'New status',
  202. * iconCls: 'x-status-custom',
  203. * clear: {
  204. * wait: 8000,
  205. * anim: false,
  206. * useDefaults: false
  207. * }
  208. * });
  209. *
  210. * @param {Object/String} config A config object specifying what status to set, or a string assumed
  211. * to be the status text (and all other options are defaulted as explained below). A config
  212. * object containing any or all of the following properties can be passed:
  213. *
  214. * @param {String} config.text The status text to display. If not specified, any current
  215. * status text will remain unchanged.
  216. *
  217. * @param {String} config.iconCls The CSS class used to customize the status icon (see
  218. * {@link #iconCls} for details). If not specified, any current iconCls will remain unchanged.
  219. *
  220. * @param {Boolean/Number/Object} config.clear Allows you to set an internal callback that will
  221. * automatically clear the status text and iconCls after a specified amount of time has passed. If clear is not
  222. * specified, the new status will not be auto-cleared and will stay until updated again or cleared using
  223. * {@link #clearStatus}. If `true` is passed, the status will be cleared using {@link #autoClear},
  224. * {@link #defaultText} and {@link #defaultIconCls} via a fade out animation. If a numeric value is passed,
  225. * it will be used as the callback interval (in milliseconds), overriding the {@link #autoClear} value.
  226. * All other options will be defaulted as with the boolean option. To customize any other options,
  227. * you can pass an object in the format:
  228. *
  229. * @param {Number} config.clear.wait The number of milliseconds to wait before clearing
  230. * (defaults to {@link #autoClear}).
  231. * @param {Boolean} config.clear.anim False to clear the status immediately once the callback
  232. * executes (defaults to true which fades the status out).
  233. * @param {Boolean} config.clear.useDefaults False to completely clear the status text and iconCls
  234. * (defaults to true which uses {@link #defaultText} and {@link #defaultIconCls}).
  235. *
  236. * @return {Ext.ux.statusbar.StatusBar} this
  237. */
  238. setStatus : function(o) {
  239. var me = this;
  240. o = o || {};
  241. Ext.suspendLayouts();
  242. if (Ext.isString(o)) {
  243. o = {text:o};
  244. }
  245. if (o.text !== undefined) {
  246. me.setText(o.text);
  247. }
  248. if (o.iconCls !== undefined) {
  249. me.setIcon(o.iconCls);
  250. }
  251. if (o.clear) {
  252. var c = o.clear,
  253. wait = me.autoClear,
  254. defaults = {useDefaults: true, anim: true};
  255. if (Ext.isObject(c)) {
  256. c = Ext.applyIf(c, defaults);
  257. if (c.wait) {
  258. wait = c.wait;
  259. }
  260. } else if (Ext.isNumber(c)) {
  261. wait = c;
  262. c = defaults;
  263. } else if (Ext.isBoolean(c)) {
  264. c = defaults;
  265. }
  266. c.threadId = this.activeThreadId;
  267. Ext.defer(me.clearStatus, wait, me, [c]);
  268. }
  269. Ext.resumeLayouts(true);
  270. return me;
  271. },
  272. /**
  273. * Clears the status {@link #text} and {@link #iconCls}. Also supports clearing via an optional fade out animation.
  274. *
  275. * @param {Object} [config] A config object containing any or all of the following properties. If this
  276. * object is not specified the status will be cleared using the defaults below:
  277. * @param {Boolean} config.anim True to clear the status by fading out the status element (defaults
  278. * to false which clears immediately).
  279. * @param {Boolean} config.useDefaults True to reset the text and icon using {@link #defaultText} and
  280. * {@link #defaultIconCls} (defaults to false which sets the text to '' and removes any existing icon class).
  281. *
  282. * @return {Ext.ux.statusbar.StatusBar} this
  283. */
  284. clearStatus : function(o) {
  285. o = o || {};
  286. var me = this,
  287. statusEl = me.statusEl;
  288. if (o.threadId && o.threadId !== me.activeThreadId) {
  289. // this means the current call was made internally, but a newer
  290. // thread has set a message since this call was deferred. Since
  291. // we don't want to overwrite a newer message just ignore.
  292. return me;
  293. }
  294. var text = o.useDefaults ? me.defaultText : me.emptyText,
  295. iconCls = o.useDefaults ? (me.defaultIconCls ? me.defaultIconCls : '') : '';
  296. if (o.anim) {
  297. // animate the statusEl Ext.Element
  298. statusEl.el.puff({
  299. remove: false,
  300. useDisplay: true,
  301. callback: function() {
  302. statusEl.el.show();
  303. me.setStatus({
  304. text: text,
  305. iconCls: iconCls
  306. });
  307. }
  308. });
  309. } else {
  310. me.setStatus({
  311. text: text,
  312. iconCls: iconCls
  313. });
  314. }
  315. return me;
  316. },
  317. /**
  318. * Convenience method for setting the status text directly. For more flexible options see {@link #setStatus}.
  319. * @param {String} text (optional) The text to set (defaults to '')
  320. * @return {Ext.ux.statusbar.StatusBar} this
  321. */
  322. setText : function(text) {
  323. var me = this;
  324. me.activeThreadId++;
  325. me.text = text || '';
  326. if (me.rendered) {
  327. me.statusEl.setText(me.text);
  328. }
  329. return me;
  330. },
  331. /**
  332. * Returns the current status text.
  333. * @return {String} The status text
  334. */
  335. getText : function(){
  336. return this.text;
  337. },
  338. /**
  339. * Convenience method for setting the status icon directly. For more flexible options see {@link #setStatus}.
  340. * See {@link #iconCls} for complete details about customizing the icon.
  341. * @param {String} iconCls (optional) The icon class to set (defaults to '', and any current icon class is removed)
  342. * @return {Ext.ux.statusbar.StatusBar} this
  343. */
  344. setIcon : function(cls) {
  345. var me = this;
  346. me.activeThreadId++;
  347. cls = cls || '';
  348. if (me.rendered) {
  349. if (me.currIconCls) {
  350. me.statusEl.removeCls(me.currIconCls);
  351. me.currIconCls = null;
  352. }
  353. if (cls.length > 0) {
  354. me.statusEl.addCls(cls);
  355. me.currIconCls = cls;
  356. }
  357. } else {
  358. me.currIconCls = cls;
  359. }
  360. return me;
  361. },
  362. /**
  363. * Convenience method for setting the status text and icon to special values that are pre-configured to indicate
  364. * a "busy" state, usually for loading or processing activities.
  365. *
  366. * @param {Object/String} config (optional) A config object in the same format supported by {@link #setStatus}, or a
  367. * string to use as the status text (in which case all other options for setStatus will be defaulted). Use the
  368. * `text` and/or `iconCls` properties on the config to override the default {@link #busyText}
  369. * and {@link #busyIconCls} settings. If the config argument is not specified, {@link #busyText} and
  370. * {@link #busyIconCls} will be used in conjunction with all of the default options for {@link #setStatus}.
  371. * @return {Ext.ux.statusbar.StatusBar} this
  372. */
  373. showBusy : function(o){
  374. if (Ext.isString(o)) {
  375. o = { text: o };
  376. }
  377. o = Ext.applyIf(o || {}, {
  378. text: this.busyText,
  379. iconCls: this.busyIconCls
  380. });
  381. return this.setStatus(o);
  382. }
  383. });