StatusBar.js 15 KB

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