StatusBar.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Ext JS Library 3.1.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.ux.StatusBar
  9. * <p>Basic status bar component that can be used as the bottom toolbar of any {@link Ext.Panel}. In addition to
  10. * supporting the standard {@link Ext.Toolbar} interface for adding buttons, menus and other items, the StatusBar
  11. * provides a greedy status element that can be aligned to either side and has convenient methods for setting the
  12. * status text and icon. You can also indicate that something is processing using the {@link #showBusy} method.</p>
  13. * <pre><code>
  14. new Ext.Panel({
  15. title: 'StatusBar',
  16. // etc.
  17. bbar: new Ext.ux.StatusBar({
  18. id: 'my-status',
  19. // defaults to use when the status is cleared:
  20. defaultText: 'Default status text',
  21. defaultIconCls: 'default-icon',
  22. // values to set initially:
  23. text: 'Ready',
  24. iconCls: 'ready-icon',
  25. // any standard Toolbar items:
  26. items: [{
  27. text: 'A Button'
  28. }, '-', 'Plain Text']
  29. })
  30. });
  31. // Update the status bar later in code:
  32. var sb = Ext.getCmp('my-status');
  33. sb.setStatus({
  34. text: 'OK',
  35. iconCls: 'ok-icon',
  36. clear: true // auto-clear after a set interval
  37. });
  38. // Set the status bar to show that something is processing:
  39. sb.showBusy();
  40. // processing....
  41. sb.clearStatus(); // once completeed
  42. </code></pre>
  43. * @extends Ext.Toolbar
  44. * @constructor
  45. * Creates a new StatusBar
  46. * @param {Object/Array} config A config object
  47. */
  48. Ext.ux.StatusBar = Ext.extend(Ext.Toolbar, {
  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 #items} config, or added via {@link #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. * <pre><code>
  57. // Create a left-aligned status bar containing a button,
  58. // separator and text item that will be right-aligned (default):
  59. new Ext.Panel({
  60. title: 'StatusBar',
  61. // etc.
  62. bbar: new Ext.ux.StatusBar({
  63. defaultText: 'Default status text',
  64. id: 'status-id',
  65. items: [{
  66. text: 'A Button'
  67. }, '-', 'Plain Text']
  68. })
  69. });
  70. // By adding the statusAlign config, this will create the
  71. // exact same toolbar, except the status and toolbar item
  72. // layout will be reversed from the previous example:
  73. new Ext.Panel({
  74. title: 'StatusBar',
  75. // etc.
  76. bbar: new Ext.ux.StatusBar({
  77. defaultText: 'Default status text',
  78. id: 'status-id',
  79. statusAlign: 'right',
  80. items: [{
  81. text: 'A Button'
  82. }, '-', 'Plain Text']
  83. })
  84. });
  85. </code></pre>
  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. * <tt>useDefaults:true</tt> option (defaults to '').
  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 <tt>useDefaults:true</tt> option (defaults to '').
  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 <code>{@link #defaultText}</code>
  102. * will be used.
  103. */
  104. /**
  105. * @cfg {String} iconCls
  106. * A CSS class that will be <b>initially</b> set as the status bar icon and is
  107. * expected to provide a background image (defaults to '').
  108. * Example usage:<pre><code>
  109. // Example CSS rule:
  110. .x-statusbar .x-status-custom {
  111. padding-left: 25px;
  112. background: transparent url(images/custom-icon.gif) no-repeat 3px 2px;
  113. }
  114. // Setting a default icon:
  115. var sb = new Ext.ux.StatusBar({
  116. defaultIconCls: 'x-status-custom'
  117. });
  118. // Changing the icon:
  119. sb.setStatus({
  120. text: 'New status',
  121. iconCls: 'x-status-custom'
  122. });
  123. </code></pre>
  124. */
  125. /**
  126. * @cfg {String} cls
  127. * The base class applied to the containing element for this component on render (defaults to 'x-statusbar')
  128. */
  129. cls : 'x-statusbar',
  130. /**
  131. * @cfg {String} busyIconCls
  132. * The default <code>{@link #iconCls}</code> applied when calling
  133. * <code>{@link #showBusy}</code> (defaults to <tt>'x-status-busy'</tt>).
  134. * It can be overridden at any time by passing the <code>iconCls</code>
  135. * argument into <code>{@link #showBusy}</code>.
  136. */
  137. busyIconCls : 'x-status-busy',
  138. /**
  139. * @cfg {String} busyText
  140. * The default <code>{@link #text}</code> applied when calling
  141. * <code>{@link #showBusy}</code> (defaults to <tt>'Loading...'</tt>).
  142. * It can be overridden at any time by passing the <code>text</code>
  143. * argument into <code>{@link #showBusy}</code>.
  144. */
  145. busyText : 'Loading...',
  146. /**
  147. * @cfg {Number} autoClear
  148. * The number of milliseconds to wait after setting the status via
  149. * <code>{@link #setStatus}</code> before automatically clearing the status
  150. * text and icon (defaults to <tt>5000</tt>). Note that this only applies
  151. * when passing the <tt>clear</tt> argument to <code>{@link #setStatus}</code>
  152. * since that is the only way to defer clearing the status. This can
  153. * be overridden by specifying a different <tt>wait</tt> value in
  154. * <code>{@link #setStatus}</code>. Calls to <code>{@link #clearStatus}</code>
  155. * 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. Defaults to
  161. * <tt>'&nbsp;'</tt>). If there are no other items in the toolbar using
  162. * an empty string (<tt>''</tt>) for this value would end up in the toolbar
  163. * height collapsing since the empty string will not maintain the toolbar
  164. * height. Use <tt>''</tt> if the toolbar should collapse in height
  165. * vertically when no text is specified and there are no other items in
  166. * the toolbar.
  167. */
  168. emptyText : '&nbsp;',
  169. // private
  170. activeThreadId : 0,
  171. // private
  172. initComponent : function(){
  173. if(this.statusAlign=='right'){
  174. this.cls += ' x-status-right';
  175. }
  176. Ext.ux.StatusBar.superclass.initComponent.call(this);
  177. },
  178. // private
  179. afterRender : function(){
  180. Ext.ux.StatusBar.superclass.afterRender.call(this);
  181. var right = this.statusAlign == 'right';
  182. this.currIconCls = this.iconCls || this.defaultIconCls;
  183. this.statusEl = new Ext.Toolbar.TextItem({
  184. cls: 'x-status-text ' + (this.currIconCls || ''),
  185. text: this.text || this.defaultText || ''
  186. });
  187. if(right){
  188. this.add('->');
  189. this.add(this.statusEl);
  190. }else{
  191. this.insert(0, this.statusEl);
  192. this.insert(1, '->');
  193. }
  194. // this.statusEl = td.createChild({
  195. // cls: 'x-status-text ' + (this.iconCls || this.defaultIconCls || ''),
  196. // html: this.text || this.defaultText || ''
  197. // });
  198. // this.statusEl.unselectable();
  199. // this.spacerEl = td.insertSibling({
  200. // tag: 'td',
  201. // style: 'width:100%',
  202. // cn: [{cls:'ytb-spacer'}]
  203. // }, right ? 'before' : 'after');
  204. },
  205. /**
  206. * Sets the status {@link #text} and/or {@link #iconCls}. Also supports automatically clearing the
  207. * status that was set after a specified interval.
  208. * @param {Object/String} config A config object specifying what status to set, or a string assumed
  209. * to be the status text (and all other options are defaulted as explained below). A config
  210. * object containing any or all of the following properties can be passed:<ul>
  211. * <li><tt>text</tt> {String} : (optional) The status text to display. If not specified, any current
  212. * status text will remain unchanged.</li>
  213. * <li><tt>iconCls</tt> {String} : (optional) The CSS class used to customize the status icon (see
  214. * {@link #iconCls} for details). If not specified, any current iconCls will remain unchanged.</li>
  215. * <li><tt>clear</tt> {Boolean/Number/Object} : (optional) Allows you to set an internal callback that will
  216. * automatically clear the status text and iconCls after a specified amount of time has passed. If clear is not
  217. * specified, the new status will not be auto-cleared and will stay until updated again or cleared using
  218. * {@link #clearStatus}. If <tt>true</tt> is passed, the status will be cleared using {@link #autoClear},
  219. * {@link #defaultText} and {@link #defaultIconCls} via a fade out animation. If a numeric value is passed,
  220. * it will be used as the callback interval (in milliseconds), overriding the {@link #autoClear} value.
  221. * All other options will be defaulted as with the boolean option. To customize any other options,
  222. * you can pass an object in the format:<ul>
  223. * <li><tt>wait</tt> {Number} : (optional) The number of milliseconds to wait before clearing
  224. * (defaults to {@link #autoClear}).</li>
  225. * <li><tt>anim</tt> {Number} : (optional) False to clear the status immediately once the callback
  226. * executes (defaults to true which fades the status out).</li>
  227. * <li><tt>useDefaults</tt> {Number} : (optional) False to completely clear the status text and iconCls
  228. * (defaults to true which uses {@link #defaultText} and {@link #defaultIconCls}).</li>
  229. * </ul></li></ul>
  230. * Example usage:<pre><code>
  231. // Simple call to update the text
  232. statusBar.setStatus('New status');
  233. // Set the status and icon, auto-clearing with default options:
  234. statusBar.setStatus({
  235. text: 'New status',
  236. iconCls: 'x-status-custom',
  237. clear: true
  238. });
  239. // Auto-clear with custom options:
  240. statusBar.setStatus({
  241. text: 'New status',
  242. iconCls: 'x-status-custom',
  243. clear: {
  244. wait: 8000,
  245. anim: false,
  246. useDefaults: false
  247. }
  248. });
  249. </code></pre>
  250. * @return {Ext.ux.StatusBar} this
  251. */
  252. setStatus : function(o){
  253. o = o || {};
  254. if(typeof o == 'string'){
  255. o = {text:o};
  256. }
  257. if(o.text !== undefined){
  258. this.setText(o.text);
  259. }
  260. if(o.iconCls !== undefined){
  261. this.setIcon(o.iconCls);
  262. }
  263. if(o.clear){
  264. var c = o.clear,
  265. wait = this.autoClear,
  266. defaults = {useDefaults: true, anim: true};
  267. if(typeof c == 'object'){
  268. c = Ext.applyIf(c, defaults);
  269. if(c.wait){
  270. wait = c.wait;
  271. }
  272. }else if(typeof c == 'number'){
  273. wait = c;
  274. c = defaults;
  275. }else if(typeof c == 'boolean'){
  276. c = defaults;
  277. }
  278. c.threadId = this.activeThreadId;
  279. this.clearStatus.defer(wait, this, [c]);
  280. }
  281. return this;
  282. },
  283. /**
  284. * Clears the status {@link #text} and {@link #iconCls}. Also supports clearing via an optional fade out animation.
  285. * @param {Object} config (optional) A config object containing any or all of the following properties. If this
  286. * object is not specified the status will be cleared using the defaults below:<ul>
  287. * <li><tt>anim</tt> {Boolean} : (optional) True to clear the status by fading out the status element (defaults
  288. * to false which clears immediately).</li>
  289. * <li><tt>useDefaults</tt> {Boolean} : (optional) True to reset the text and icon using {@link #defaultText} and
  290. * {@link #defaultIconCls} (defaults to false which sets the text to '' and removes any existing icon class).</li>
  291. * </ul>
  292. * @return {Ext.ux.StatusBar} this
  293. */
  294. clearStatus : function(o){
  295. o = o || {};
  296. if(o.threadId && o.threadId !== this.activeThreadId){
  297. // this means the current call was made internally, but a newer
  298. // thread has set a message since this call was deferred. Since
  299. // we don't want to overwrite a newer message just ignore.
  300. return this;
  301. }
  302. var text = o.useDefaults ? this.defaultText : this.emptyText,
  303. iconCls = o.useDefaults ? (this.defaultIconCls ? this.defaultIconCls : '') : '';
  304. if(o.anim){
  305. // animate the statusEl Ext.Element
  306. this.statusEl.el.fadeOut({
  307. remove: false,
  308. useDisplay: true,
  309. scope: this,
  310. callback: function(){
  311. this.setStatus({
  312. text: text,
  313. iconCls: iconCls
  314. });
  315. this.statusEl.el.show();
  316. }
  317. });
  318. }else{
  319. // hide/show the el to avoid jumpy text or icon
  320. this.statusEl.hide();
  321. this.setStatus({
  322. text: text,
  323. iconCls: iconCls
  324. });
  325. this.statusEl.show();
  326. }
  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.removeClass(this.currIconCls);
  361. this.currIconCls = null;
  362. }
  363. if(cls.length > 0){
  364. this.statusEl.addClass(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(typeof o == 'string'){
  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. });
  393. Ext.reg('statusbar', Ext.ux.StatusBar);