StatusBar.js 16 KB

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