StatusBar.js 16 KB

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