Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1212 lines
39 KiB

  1. /*!
  2. * Ext JS Library 3.2.0
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.Toolbar
  9. * @extends Ext.Container
  10. * <p>Basic Toolbar class. Although the <tt>{@link Ext.Container#defaultType defaultType}</tt> for Toolbar
  11. * is <tt>{@link Ext.Button button}</tt>, Toolbar elements (child items for the Toolbar container) may
  12. * be virtually any type of Component. Toolbar elements can be created explicitly via their constructors,
  13. * or implicitly via their xtypes, and can be <tt>{@link #add}</tt>ed dynamically.</p>
  14. * <p>Some items have shortcut strings for creation:</p>
  15. * <pre>
  16. <u>Shortcut</u> <u>xtype</u> <u>Class</u> <u>Description</u>
  17. '->' 'tbfill' {@link Ext.Toolbar.Fill} begin using the right-justified button container
  18. '-' 'tbseparator' {@link Ext.Toolbar.Separator} add a vertical separator bar between toolbar items
  19. ' ' 'tbspacer' {@link Ext.Toolbar.Spacer} add horiztonal space between elements
  20. * </pre>
  21. *
  22. * Example usage of various elements:
  23. * <pre><code>
  24. var tb = new Ext.Toolbar({
  25. renderTo: document.body,
  26. width: 600,
  27. height: 100,
  28. items: [
  29. {
  30. // xtype: 'button', // default for Toolbars, same as 'tbbutton'
  31. text: 'Button'
  32. },
  33. {
  34. xtype: 'splitbutton', // same as 'tbsplitbutton'
  35. text: 'Split Button'
  36. },
  37. // begin using the right-justified button container
  38. '->', // same as {xtype: 'tbfill'}, // Ext.Toolbar.Fill
  39. {
  40. xtype: 'textfield',
  41. name: 'field1',
  42. emptyText: 'enter search term'
  43. },
  44. // add a vertical separator bar between toolbar items
  45. '-', // same as {xtype: 'tbseparator'} to create Ext.Toolbar.Separator
  46. 'text 1', // same as {xtype: 'tbtext', text: 'text1'} to create Ext.Toolbar.TextItem
  47. {xtype: 'tbspacer'},// same as ' ' to create Ext.Toolbar.Spacer
  48. 'text 2',
  49. {xtype: 'tbspacer', width: 50}, // add a 50px space
  50. 'text 3'
  51. ]
  52. });
  53. * </code></pre>
  54. * Example adding a ComboBox within a menu of a button:
  55. * <pre><code>
  56. // ComboBox creation
  57. var combo = new Ext.form.ComboBox({
  58. store: new Ext.data.ArrayStore({
  59. autoDestroy: true,
  60. fields: ['initials', 'fullname'],
  61. data : [
  62. ['FF', 'Fred Flintstone'],
  63. ['BR', 'Barney Rubble']
  64. ]
  65. }),
  66. displayField: 'fullname',
  67. typeAhead: true,
  68. mode: 'local',
  69. forceSelection: true,
  70. triggerAction: 'all',
  71. emptyText: 'Select a name...',
  72. selectOnFocus: true,
  73. width: 135,
  74. getListParent: function() {
  75. return this.el.up('.x-menu');
  76. },
  77. iconCls: 'no-icon' //use iconCls if placing within menu to shift to right side of menu
  78. });
  79. // put ComboBox in a Menu
  80. var menu = new Ext.menu.Menu({
  81. id: 'mainMenu',
  82. items: [
  83. combo // A Field in a Menu
  84. ]
  85. });
  86. // add a Button with the menu
  87. tb.add({
  88. text:'Button w/ Menu',
  89. menu: menu // assign menu by instance
  90. });
  91. tb.doLayout();
  92. * </code></pre>
  93. * @constructor
  94. * Creates a new Toolbar
  95. * @param {Object/Array} config A config object or an array of buttons to <tt>{@link #add}</tt>
  96. * @xtype toolbar
  97. */
  98. Ext.Toolbar = function(config){
  99. if(Ext.isArray(config)){
  100. config = {items: config, layout: 'toolbar'};
  101. } else {
  102. config = Ext.apply({
  103. layout: 'toolbar'
  104. }, config);
  105. if(config.buttons) {
  106. config.items = config.buttons;
  107. }
  108. }
  109. Ext.Toolbar.superclass.constructor.call(this, config);
  110. };
  111. (function(){
  112. var T = Ext.Toolbar;
  113. Ext.extend(T, Ext.Container, {
  114. defaultType: 'button',
  115. /**
  116. * @cfg {String/Object} layout
  117. * This class assigns a default layout (<code>layout:'<b>toolbar</b>'</code>).
  118. * Developers <i>may</i> override this configuration option if another layout
  119. * is required (the constructor must be passed a configuration object in this
  120. * case instead of an array).
  121. * See {@link Ext.Container#layout} for additional information.
  122. */
  123. enableOverflow : false,
  124. /**
  125. * @cfg {Boolean} enableOverflow
  126. * Defaults to false. Configure <code>true<code> to make the toolbar provide a button
  127. * which activates a dropdown Menu to show items which overflow the Toolbar's width.
  128. */
  129. /**
  130. * @cfg {String} buttonAlign
  131. * <p>The default position at which to align child items. Defaults to <code>"left"</code></p>
  132. * <p>May be specified as <code>"center"</code> to cause items added before a Fill (A <code>"->"</code>) item
  133. * to be centered in the Toolbar. Items added after a Fill are still right-aligned.</p>
  134. * <p>Specify as <code>"right"</code> to right align all child items.</p>
  135. */
  136. trackMenus : true,
  137. internalDefaults: {removeMode: 'container', hideParent: true},
  138. toolbarCls: 'x-toolbar',
  139. initComponent : function(){
  140. T.superclass.initComponent.call(this);
  141. /**
  142. * @event overflowchange
  143. * Fires after the overflow state has changed.
  144. * @param {Object} c The Container
  145. * @param {Boolean} lastOverflow overflow state
  146. */
  147. this.addEvents('overflowchange');
  148. },
  149. // private
  150. onRender : function(ct, position){
  151. if(!this.el){
  152. if(!this.autoCreate){
  153. this.autoCreate = {
  154. cls: this.toolbarCls + ' x-small-editor'
  155. };
  156. }
  157. this.el = ct.createChild(Ext.apply({ id: this.id },this.autoCreate), position);
  158. Ext.Toolbar.superclass.onRender.apply(this, arguments);
  159. }
  160. },
  161. /**
  162. * <p>Adds element(s) to the toolbar -- this function takes a variable number of
  163. * arguments of mixed type and adds them to the toolbar.</p>
  164. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  165. * @param {Mixed} arg1 The following types of arguments are all valid:<br />
  166. * <ul>
  167. * <li>{@link Ext.Button} config: A valid button config object (equivalent to {@link #addButton})</li>
  168. * <li>HtmlElement: Any standard HTML element (equivalent to {@link #addElement})</li>
  169. * <li>Field: Any form field (equivalent to {@link #addField})</li>
  170. * <li>Item: Any subclass of {@link Ext.Toolbar.Item} (equivalent to {@link #addItem})</li>
  171. * <li>String: Any generic string (gets wrapped in a {@link Ext.Toolbar.TextItem}, equivalent to {@link #addText}).
  172. * Note that there are a few special strings that are treated differently as explained next.</li>
  173. * <li>'-': Creates a separator element (equivalent to {@link #addSeparator})</li>
  174. * <li>' ': Creates a spacer element (equivalent to {@link #addSpacer})</li>
  175. * <li>'->': Creates a fill element (equivalent to {@link #addFill})</li>
  176. * </ul>
  177. * @param {Mixed} arg2
  178. * @param {Mixed} etc.
  179. * @method add
  180. */
  181. // private
  182. lookupComponent : function(c){
  183. if(Ext.isString(c)){
  184. if(c == '-'){
  185. c = new T.Separator();
  186. }else if(c == ' '){
  187. c = new T.Spacer();
  188. }else if(c == '->'){
  189. c = new T.Fill();
  190. }else{
  191. c = new T.TextItem(c);
  192. }
  193. this.applyDefaults(c);
  194. }else{
  195. if(c.isFormField || c.render){ // some kind of form field, some kind of Toolbar.Item
  196. c = this.createComponent(c);
  197. }else if(c.tag){ // DomHelper spec
  198. c = new T.Item({autoEl: c});
  199. }else if(c.tagName){ // element
  200. c = new T.Item({el:c});
  201. }else if(Ext.isObject(c)){ // must be button config?
  202. c = c.xtype ? this.createComponent(c) : this.constructButton(c);
  203. }
  204. }
  205. return c;
  206. },
  207. // private
  208. applyDefaults : function(c){
  209. if(!Ext.isString(c)){
  210. c = Ext.Toolbar.superclass.applyDefaults.call(this, c);
  211. var d = this.internalDefaults;
  212. if(c.events){
  213. Ext.applyIf(c.initialConfig, d);
  214. Ext.apply(c, d);
  215. }else{
  216. Ext.applyIf(c, d);
  217. }
  218. }
  219. return c;
  220. },
  221. /**
  222. * Adds a separator
  223. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  224. * @return {Ext.Toolbar.Item} The separator {@link Ext.Toolbar.Item item}
  225. */
  226. addSeparator : function(){
  227. return this.add(new T.Separator());
  228. },
  229. /**
  230. * Adds a spacer element
  231. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  232. * @return {Ext.Toolbar.Spacer} The spacer item
  233. */
  234. addSpacer : function(){
  235. return this.add(new T.Spacer());
  236. },
  237. /**
  238. * Forces subsequent additions into the float:right toolbar
  239. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  240. */
  241. addFill : function(){
  242. this.add(new T.Fill());
  243. },
  244. /**
  245. * Adds any standard HTML element to the toolbar
  246. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  247. * @param {Mixed} el The element or id of the element to add
  248. * @return {Ext.Toolbar.Item} The element's item
  249. */
  250. addElement : function(el){
  251. return this.addItem(new T.Item({el:el}));
  252. },
  253. /**
  254. * Adds any Toolbar.Item or subclass
  255. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  256. * @param {Ext.Toolbar.Item} item
  257. * @return {Ext.Toolbar.Item} The item
  258. */
  259. addItem : function(item){
  260. return this.add.apply(this, arguments);
  261. },
  262. /**
  263. * Adds a button (or buttons). See {@link Ext.Button} for more info on the config.
  264. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  265. * @param {Object/Array} config A button config or array of configs
  266. * @return {Ext.Button/Array}
  267. */
  268. addButton : function(config){
  269. if(Ext.isArray(config)){
  270. var buttons = [];
  271. for(var i = 0, len = config.length; i < len; i++) {
  272. buttons.push(this.addButton(config[i]));
  273. }
  274. return buttons;
  275. }
  276. return this.add(this.constructButton(config));
  277. },
  278. /**
  279. * Adds text to the toolbar
  280. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  281. * @param {String} text The text to add
  282. * @return {Ext.Toolbar.Item} The element's item
  283. */
  284. addText : function(text){
  285. return this.addItem(new T.TextItem(text));
  286. },
  287. /**
  288. * Adds a new element to the toolbar from the passed {@link Ext.DomHelper} config
  289. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  290. * @param {Object} config
  291. * @return {Ext.Toolbar.Item} The element's item
  292. */
  293. addDom : function(config){
  294. return this.add(new T.Item({autoEl: config}));
  295. },
  296. /**
  297. * Adds a dynamically rendered Ext.form field (TextField, ComboBox, etc). Note: the field should not have
  298. * been rendered yet. For a field that has already been rendered, use {@link #addElement}.
  299. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  300. * @param {Ext.form.Field} field
  301. * @return {Ext.Toolbar.Item}
  302. */
  303. addField : function(field){
  304. return this.add(field);
  305. },
  306. /**
  307. * Inserts any {@link Ext.Toolbar.Item}/{@link Ext.Button} at the specified index.
  308. * <br><p><b>Note</b>: See the notes within {@link Ext.Container#add}.</p>
  309. * @param {Number} index The index where the item is to be inserted
  310. * @param {Object/Ext.Toolbar.Item/Ext.Button/Array} item The button, or button config object to be
  311. * inserted, or an array of buttons/configs.
  312. * @return {Ext.Button/Item}
  313. */
  314. insertButton : function(index, item){
  315. if(Ext.isArray(item)){
  316. var buttons = [];
  317. for(var i = 0, len = item.length; i < len; i++) {
  318. buttons.push(this.insertButton(index + i, item[i]));
  319. }
  320. return buttons;
  321. }
  322. return Ext.Toolbar.superclass.insert.call(this, index, item);
  323. },
  324. // private
  325. trackMenu : function(item, remove){
  326. if(this.trackMenus && item.menu){
  327. var method = remove ? 'mun' : 'mon';
  328. this[method](item, 'menutriggerover', this.onButtonTriggerOver, this);
  329. this[method](item, 'menushow', this.onButtonMenuShow, this);
  330. this[method](item, 'menuhide', this.onButtonMenuHide, this);
  331. }
  332. },
  333. // private
  334. constructButton : function(item){
  335. var b = item.events ? item : this.createComponent(item, item.split ? 'splitbutton' : this.defaultType);
  336. return b;
  337. },
  338. // private
  339. onAdd : function(c){
  340. Ext.Toolbar.superclass.onAdd.call(this);
  341. this.trackMenu(c);
  342. if(this.disabled){
  343. c.disable();
  344. }
  345. },
  346. // private
  347. onRemove : function(c){
  348. Ext.Toolbar.superclass.onRemove.call(this);
  349. this.trackMenu(c, true);
  350. },
  351. // private
  352. onDisable : function(){
  353. this.items.each(function(item){
  354. if(item.disable){
  355. item.disable();
  356. }
  357. });
  358. },
  359. // private
  360. onEnable : function(){
  361. this.items.each(function(item){
  362. if(item.enable){
  363. item.enable();
  364. }
  365. });
  366. },
  367. // private
  368. onButtonTriggerOver : function(btn){
  369. if(this.activeMenuBtn && this.activeMenuBtn != btn){
  370. this.activeMenuBtn.hideMenu();
  371. btn.showMenu();
  372. this.activeMenuBtn = btn;
  373. }
  374. },
  375. // private
  376. onButtonMenuShow : function(btn){
  377. this.activeMenuBtn = btn;
  378. },
  379. // private
  380. onButtonMenuHide : function(btn){
  381. delete this.activeMenuBtn;
  382. }
  383. });
  384. Ext.reg('toolbar', Ext.Toolbar);
  385. /**
  386. * @class Ext.Toolbar.Item
  387. * @extends Ext.BoxComponent
  388. * The base class that other non-interacting Toolbar Item classes should extend in order to
  389. * get some basic common toolbar item functionality.
  390. * @constructor
  391. * Creates a new Item
  392. * @param {HTMLElement} el
  393. * @xtype tbitem
  394. */
  395. T.Item = Ext.extend(Ext.BoxComponent, {
  396. hideParent: true, // Hiding a Toolbar.Item hides its containing TD
  397. enable:Ext.emptyFn,
  398. disable:Ext.emptyFn,
  399. focus:Ext.emptyFn
  400. /**
  401. * @cfg {String} overflowText Text to be used for the menu if the item is overflowed.
  402. */
  403. });
  404. Ext.reg('tbitem', T.Item);
  405. /**
  406. * @class Ext.Toolbar.Separator
  407. * @extends Ext.Toolbar.Item
  408. * A simple class that adds a vertical separator bar between toolbar items
  409. * (css class:<tt>'xtb-sep'</tt>). Example usage:
  410. * <pre><code>
  411. new Ext.Panel({
  412. tbar : [
  413. 'Item 1',
  414. {xtype: 'tbseparator'}, // or '-'
  415. 'Item 2'
  416. ]
  417. });
  418. </code></pre>
  419. * @constructor
  420. * Creates a new Separator
  421. * @xtype tbseparator
  422. */
  423. T.Separator = Ext.extend(T.Item, {
  424. onRender : function(ct, position){
  425. this.el = ct.createChild({tag:'span', cls:'xtb-sep'}, position);
  426. }
  427. });
  428. Ext.reg('tbseparator', T.Separator);
  429. /**
  430. * @class Ext.Toolbar.Spacer
  431. * @extends Ext.Toolbar.Item
  432. * A simple element that adds extra horizontal space between items in a toolbar.
  433. * By default a 2px wide space is added via css specification:<pre><code>
  434. .x-toolbar .xtb-spacer {
  435. width:2px;
  436. }
  437. * </code></pre>
  438. * <p>Example usage:</p>
  439. * <pre><code>
  440. new Ext.Panel({
  441. tbar : [
  442. 'Item 1',
  443. {xtype: 'tbspacer'}, // or ' '
  444. 'Item 2',
  445. // space width is also configurable via javascript
  446. {xtype: 'tbspacer', width: 50}, // add a 50px space
  447. 'Item 3'
  448. ]
  449. });
  450. </code></pre>
  451. * @constructor
  452. * Creates a new Spacer
  453. * @xtype tbspacer
  454. */
  455. T.Spacer = Ext.extend(T.Item, {
  456. /**
  457. * @cfg {Number} width
  458. * The width of the spacer in pixels (defaults to 2px via css style <tt>.x-toolbar .xtb-spacer</tt>).
  459. */
  460. onRender : function(ct, position){
  461. this.el = ct.createChild({tag:'div', cls:'xtb-spacer', style: this.width?'width:'+this.width+'px':''}, position);
  462. }
  463. });
  464. Ext.reg('tbspacer', T.Spacer);
  465. /**
  466. * @class Ext.Toolbar.Fill
  467. * @extends Ext.Toolbar.Spacer
  468. * A non-rendering placeholder item which instructs the Toolbar's Layout to begin using
  469. * the right-justified button container.
  470. * <pre><code>
  471. new Ext.Panel({
  472. tbar : [
  473. 'Item 1',
  474. {xtype: 'tbfill'}, // or '->'
  475. 'Item 2'
  476. ]
  477. });
  478. </code></pre>
  479. * @constructor
  480. * Creates a new Fill
  481. * @xtype tbfill
  482. */
  483. T.Fill = Ext.extend(T.Item, {
  484. // private
  485. render : Ext.emptyFn,
  486. isFill : true
  487. });
  488. Ext.reg('tbfill', T.Fill);
  489. /**
  490. * @class Ext.Toolbar.TextItem
  491. * @extends Ext.Toolbar.Item
  492. * A simple class that renders text directly into a toolbar
  493. * (with css class:<tt>'xtb-text'</tt>). Example usage:
  494. * <pre><code>
  495. new Ext.Panel({
  496. tbar : [
  497. {xtype: 'tbtext', text: 'Item 1'} // or simply 'Item 1'
  498. ]
  499. });
  500. </code></pre>
  501. * @constructor
  502. * Creates a new TextItem
  503. * @param {String/Object} text A text string, or a config object containing a <tt>text</tt> property
  504. * @xtype tbtext
  505. */
  506. T.TextItem = Ext.extend(T.Item, {
  507. /**
  508. * @cfg {String} text The text to be used as innerHTML (html tags are accepted)
  509. */
  510. constructor: function(config){
  511. T.TextItem.superclass.constructor.call(this, Ext.isString(config) ? {text: config} : config);
  512. },
  513. // private
  514. onRender : function(ct, position) {
  515. this.autoEl = {cls: 'xtb-text', html: this.text || ''};
  516. T.TextItem.superclass.onRender.call(this, ct, position);
  517. },
  518. /**
  519. * Updates this item's text, setting the text to be used as innerHTML.
  520. * @param {String} t The text to display (html accepted).
  521. */
  522. setText : function(t) {
  523. if(this.rendered){
  524. this.el.update(t);
  525. }else{
  526. this.text = t;
  527. }
  528. }
  529. });
  530. Ext.reg('tbtext', T.TextItem);
  531. // backwards compat
  532. T.Button = Ext.extend(Ext.Button, {});
  533. T.SplitButton = Ext.extend(Ext.SplitButton, {});
  534. Ext.reg('tbbutton', T.Button);
  535. Ext.reg('tbsplit', T.SplitButton);
  536. })();
  537. /**
  538. * @class Ext.ButtonGroup
  539. * @extends Ext.Panel
  540. * Container for a group of buttons. Example usage:
  541. * <pre><code>
  542. var p = new Ext.Panel({
  543. title: 'Panel with Button Group',
  544. width: 300,
  545. height:200,
  546. renderTo: document.body,
  547. html: 'whatever',
  548. tbar: [{
  549. xtype: 'buttongroup',
  550. {@link #columns}: 3,
  551. title: 'Clipboard',
  552. items: [{
  553. text: 'Paste',
  554. scale: 'large',
  555. rowspan: 3, iconCls: 'add',
  556. iconAlign: 'top',
  557. cls: 'x-btn-as-arrow'
  558. },{
  559. xtype:'splitbutton',
  560. text: 'Menu Button',
  561. scale: 'large',
  562. rowspan: 3,
  563. iconCls: 'add',
  564. iconAlign: 'top',
  565. arrowAlign:'bottom',
  566. menu: [{text: 'Menu Item 1'}]
  567. },{
  568. xtype:'splitbutton', text: 'Cut', iconCls: 'add16', menu: [{text: 'Cut Menu Item'}]
  569. },{
  570. text: 'Copy', iconCls: 'add16'
  571. },{
  572. text: 'Format', iconCls: 'add16'
  573. }]
  574. }]
  575. });
  576. * </code></pre>
  577. * @constructor
  578. * Create a new ButtonGroup.
  579. * @param {Object} config The config object
  580. * @xtype buttongroup
  581. */
  582. Ext.ButtonGroup = Ext.extend(Ext.Panel, {
  583. /**
  584. * @cfg {Number} columns The <tt>columns</tt> configuration property passed to the
  585. * {@link #layout configured layout manager}. See {@link Ext.layout.TableLayout#columns}.
  586. */
  587. /**
  588. * @cfg {String} baseCls Defaults to <tt>'x-btn-group'</tt>. See {@link Ext.Panel#baseCls}.
  589. */
  590. baseCls: 'x-btn-group',
  591. /**
  592. * @cfg {String} layout Defaults to <tt>'table'</tt>. See {@link Ext.Container#layout}.
  593. */
  594. layout:'table',
  595. defaultType: 'button',
  596. /**
  597. * @cfg {Boolean} frame Defaults to <tt>true</tt>. See {@link Ext.Panel#frame}.
  598. */
  599. frame: true,
  600. internalDefaults: {removeMode: 'container', hideParent: true},
  601. initComponent : function(){
  602. this.layoutConfig = this.layoutConfig || {};
  603. Ext.applyIf(this.layoutConfig, {
  604. columns : this.columns
  605. });
  606. if(!this.title){
  607. this.addClass('x-btn-group-notitle');
  608. }
  609. this.on('afterlayout', this.onAfterLayout, this);
  610. Ext.ButtonGroup.superclass.initComponent.call(this);
  611. },
  612. applyDefaults : function(c){
  613. c = Ext.ButtonGroup.superclass.applyDefaults.call(this, c);
  614. var d = this.internalDefaults;
  615. if(c.events){
  616. Ext.applyIf(c.initialConfig, d);
  617. Ext.apply(c, d);
  618. }else{
  619. Ext.applyIf(c, d);
  620. }
  621. return c;
  622. },
  623. onAfterLayout : function(){
  624. var bodyWidth = this.body.getFrameWidth('lr') + this.body.dom.firstChild.offsetWidth;
  625. this.body.setWidth(bodyWidth);
  626. this.el.setWidth(bodyWidth + this.getFrameWidth());
  627. }
  628. /**
  629. * @cfg {Array} tools @hide
  630. */
  631. });
  632. Ext.reg('buttongroup', Ext.ButtonGroup);
  633. /**
  634. * @class Ext.PagingToolbar
  635. * @extends Ext.Toolbar
  636. * <p>As the amount of records increases, the time required for the browser to render
  637. * them increases. Paging is used to reduce the amount of data exchanged with the client.
  638. * Note: if there are more records/rows than can be viewed in the available screen area, vertical
  639. * scrollbars will be added.</p>
  640. * <p>Paging is typically handled on the server side (see exception below). The client sends
  641. * parameters to the server side, which the server needs to interpret and then respond with the
  642. * approprate data.</p>
  643. * <p><b>Ext.PagingToolbar</b> is a specialized toolbar that is bound to a {@link Ext.data.Store}
  644. * and provides automatic paging control. This Component {@link Ext.data.Store#load load}s blocks
  645. * of data into the <tt>{@link #store}</tt> by passing {@link Ext.data.Store#paramNames paramNames} used for
  646. * paging criteria.</p>
  647. * <p>PagingToolbar is typically used as one of the Grid's toolbars:</p>
  648. * <pre><code>
  649. Ext.QuickTips.init(); // to display button quicktips
  650. var myStore = new Ext.data.Store({
  651. reader: new Ext.data.JsonReader({
  652. {@link Ext.data.JsonReader#totalProperty totalProperty}: 'results',
  653. ...
  654. }),
  655. ...
  656. });
  657. var myPageSize = 25; // server script should only send back 25 items at a time
  658. var grid = new Ext.grid.GridPanel({
  659. ...
  660. store: myStore,
  661. bbar: new Ext.PagingToolbar({
  662. {@link #store}: myStore, // grid and PagingToolbar using same store
  663. {@link #displayInfo}: true,
  664. {@link #pageSize}: myPageSize,
  665. {@link #prependButtons}: true,
  666. items: [
  667. 'text 1'
  668. ]
  669. })
  670. });
  671. * </code></pre>
  672. *
  673. * <p>To use paging, pass the paging requirements to the server when the store is first loaded.</p>
  674. * <pre><code>
  675. store.load({
  676. params: {
  677. // specify params for the first page load if using paging
  678. start: 0,
  679. limit: myPageSize,
  680. // other params
  681. foo: 'bar'
  682. }
  683. });
  684. * </code></pre>
  685. *
  686. * <p>If using {@link Ext.data.Store#autoLoad store's autoLoad} configuration:</p>
  687. * <pre><code>
  688. var myStore = new Ext.data.Store({
  689. {@link Ext.data.Store#autoLoad autoLoad}: {params:{start: 0, limit: 25}},
  690. ...
  691. });
  692. * </code></pre>
  693. *
  694. * <p>The packet sent back from the server would have this form:</p>
  695. * <pre><code>
  696. {
  697. "success": true,
  698. "results": 2000,
  699. "rows": [ // <b>*Note:</b> this must be an Array
  700. { "id": 1, "name": "Bill", "occupation": "Gardener" },
  701. { "id": 2, "name": "Ben", "occupation": "Horticulturalist" },
  702. ...
  703. { "id": 25, "name": "Sue", "occupation": "Botanist" }
  704. ]
  705. }
  706. * </code></pre>
  707. * <p><u>Paging with Local Data</u></p>
  708. * <p>Paging can also be accomplished with local data using extensions:</p>
  709. * <div class="mdetail-params"><ul>
  710. * <li><a href="http://extjs.com/forum/showthread.php?t=71532">Ext.ux.data.PagingStore</a></li>
  711. * <li>Paging Memory Proxy (examples/ux/PagingMemoryProxy.js)</li>
  712. * </ul></div>
  713. * @constructor Create a new PagingToolbar
  714. * @param {Object} config The config object
  715. * @xtype paging
  716. */
  717. (function() {
  718. var T = Ext.Toolbar;
  719. Ext.PagingToolbar = Ext.extend(Ext.Toolbar, {
  720. /**
  721. * @cfg {Ext.data.Store} store
  722. * The {@link Ext.data.Store} the paging toolbar should use as its data source (required).
  723. */
  724. /**
  725. * @cfg {Boolean} displayInfo
  726. * <tt>true</tt> to display the displayMsg (defaults to <tt>false</tt>)
  727. */
  728. /**
  729. * @cfg {Number} pageSize
  730. * The number of records to display per page (defaults to <tt>20</tt>)
  731. */
  732. pageSize : 20,
  733. /**
  734. * @cfg {Boolean} prependButtons
  735. * <tt>true</tt> to insert any configured <tt>items</tt> <i>before</i> the paging buttons.
  736. * Defaults to <tt>false</tt>.
  737. */
  738. /**
  739. * @cfg {String} displayMsg
  740. * The paging status message to display (defaults to <tt>'Displaying {0} - {1} of {2}'</tt>).
  741. * Note that this string is formatted using the braced numbers <tt>{0}-{2}</tt> as tokens
  742. * that are replaced by the values for start, end and total respectively. These tokens should
  743. * be preserved when overriding this string if showing those values is desired.
  744. */
  745. displayMsg : 'Displaying {0} - {1} of {2}',
  746. /**
  747. * @cfg {String} emptyMsg
  748. * The message to display when no records are found (defaults to 'No data to display')
  749. */
  750. emptyMsg : 'No data to display',
  751. /**
  752. * @cfg {String} beforePageText
  753. * The text displayed before the input item (defaults to <tt>'Page'</tt>).
  754. */
  755. beforePageText : 'Page',
  756. /**
  757. * @cfg {String} afterPageText
  758. * Customizable piece of the default paging text (defaults to <tt>'of {0}'</tt>). Note that
  759. * this string is formatted using <tt>{0}</tt> as a token that is replaced by the number of
  760. * total pages. This token should be preserved when overriding this string if showing the
  761. * total page count is desired.
  762. */
  763. afterPageText : 'of {0}',
  764. /**
  765. * @cfg {String} firstText
  766. * The quicktip text displayed for the first page button (defaults to <tt>'First Page'</tt>).
  767. * <b>Note</b>: quick tips must be initialized for the quicktip to show.
  768. */
  769. firstText : 'First Page',
  770. /**
  771. * @cfg {String} prevText
  772. * The quicktip text displayed for the previous page button (defaults to <tt>'Previous Page'</tt>).
  773. * <b>Note</b>: quick tips must be initialized for the quicktip to show.
  774. */
  775. prevText : 'Previous Page',
  776. /**
  777. * @cfg {String} nextText
  778. * The quicktip text displayed for the next page button (defaults to <tt>'Next Page'</tt>).
  779. * <b>Note</b>: quick tips must be initialized for the quicktip to show.
  780. */
  781. nextText : 'Next Page',
  782. /**
  783. * @cfg {String} lastText
  784. * The quicktip text displayed for the last page button (defaults to <tt>'Last Page'</tt>).
  785. * <b>Note</b>: quick tips must be initialized for the quicktip to show.
  786. */
  787. lastText : 'Last Page',
  788. /**
  789. * @cfg {String} refreshText
  790. * The quicktip text displayed for the Refresh button (defaults to <tt>'Refresh'</tt>).
  791. * <b>Note</b>: quick tips must be initialized for the quicktip to show.
  792. */
  793. refreshText : 'Refresh',
  794. /**
  795. * <p><b>Deprecated</b>. <code>paramNames</code> should be set in the <b>data store</b>
  796. * (see {@link Ext.data.Store#paramNames}).</p>
  797. * <br><p>Object mapping of parameter names used for load calls, initially set to:</p>
  798. * <pre>{start: 'start', limit: 'limit'}</pre>
  799. * @type Object
  800. * @property paramNames
  801. * @deprecated
  802. */
  803. /**
  804. * The number of records to display per page. See also <tt>{@link #cursor}</tt>.
  805. * @type Number
  806. * @property pageSize
  807. */
  808. /**
  809. * Indicator for the record position. This property might be used to get the active page
  810. * number for example:<pre><code>
  811. * // t is reference to the paging toolbar instance
  812. * var activePage = Math.ceil((t.cursor + t.pageSize) / t.pageSize);
  813. * </code></pre>
  814. * @type Number
  815. * @property cursor
  816. */
  817. initComponent : function(){
  818. var pagingItems = [this.first = new T.Button({
  819. tooltip: this.firstText,
  820. overflowText: this.firstText,
  821. iconCls: 'x-tbar-page-first',
  822. disabled: true,
  823. handler: this.moveFirst,
  824. scope: this
  825. }), this.prev = new T.Button({
  826. tooltip: this.prevText,
  827. overflowText: this.prevText,
  828. iconCls: 'x-tbar-page-prev',
  829. disabled: true,
  830. handler: this.movePrevious,
  831. scope: this
  832. }), '-', this.beforePageText,
  833. this.inputItem = new Ext.form.NumberField({
  834. cls: 'x-tbar-page-number',
  835. allowDecimals: false,
  836. allowNegative: false,
  837. enableKeyEvents: true,
  838. selectOnFocus: true,
  839. submitValue: false,
  840. listeners: {
  841. scope: this,
  842. keydown: this.onPagingKeyDown,
  843. blur: this.onPagingBlur
  844. }
  845. }), this.afterTextItem = new T.TextItem({
  846. text: String.format(this.afterPageText, 1)
  847. }), '-', this.next = new T.Button({
  848. tooltip: this.nextText,
  849. overflowText: this.nextText,
  850. iconCls: 'x-tbar-page-next',
  851. disabled: true,
  852. handler: this.moveNext,
  853. scope: this
  854. }), this.last = new T.Button({
  855. tooltip: this.lastText,
  856. overflowText: this.lastText,
  857. iconCls: 'x-tbar-page-last',
  858. disabled: true,
  859. handler: this.moveLast,
  860. scope: this
  861. }), '-', this.refresh = new T.Button({
  862. tooltip: this.refreshText,
  863. overflowText: this.refreshText,
  864. iconCls: 'x-tbar-loading',
  865. handler: this.doRefresh,
  866. scope: this
  867. })];
  868. var userItems = this.items || this.buttons || [];
  869. if (this.prependButtons) {
  870. this.items = userItems.concat(pagingItems);
  871. }else{
  872. this.items = pagingItems.concat(userItems);
  873. }
  874. delete this.buttons;
  875. if(this.displayInfo){
  876. this.items.push('->');
  877. this.items.push(this.displayItem = new T.TextItem({}));
  878. }
  879. Ext.PagingToolbar.superclass.initComponent.call(this);
  880. this.addEvents(
  881. /**
  882. * @event change
  883. * Fires after the active page has been changed.
  884. * @param {Ext.PagingToolbar} this
  885. * @param {Object} pageData An object that has these properties:<ul>
  886. * <li><code>total</code> : Number <div class="sub-desc">The total number of records in the dataset as
  887. * returned by the server</div></li>
  888. * <li><code>activePage</code> : Number <div class="sub-desc">The current page number</div></li>
  889. * <li><code>pages</code> : Number <div class="sub-desc">The total number of pages (calculated from
  890. * the total number of records in the dataset as returned by the server and the current {@link #pageSize})</div></li>
  891. * </ul>
  892. */
  893. 'change',
  894. /**
  895. * @event beforechange
  896. * Fires just before the active page is changed.
  897. * Return false to prevent the active page from being changed.
  898. * @param {Ext.PagingToolbar} this
  899. * @param {Object} params An object hash of the parameters which the PagingToolbar will send when
  900. * loading the required page. This will contain:<ul>
  901. * <li><code>start</code> : Number <div class="sub-desc">The starting row number for the next page of records to
  902. * be retrieved from the server</div></li>
  903. * <li><code>limit</code> : Number <div class="sub-desc">The number of records to be retrieved from the server</div></li>
  904. * </ul>
  905. * <p>(note: the names of the <b>start</b> and <b>limit</b> properties are determined
  906. * by the store's {@link Ext.data.Store#paramNames paramNames} property.)</p>
  907. * <p>Parameters may be added as required in the event handler.</p>
  908. */
  909. 'beforechange'
  910. );
  911. this.on('afterlayout', this.onFirstLayout, this, {single: true});
  912. this.cursor = 0;
  913. this.bindStore(this.store, true);
  914. },
  915. // private
  916. onFirstLayout : function(){
  917. if(this.dsLoaded){
  918. this.onLoad.apply(this, this.dsLoaded);
  919. }
  920. },
  921. // private
  922. updateInfo : function(){
  923. if(this.displayItem){
  924. var count = this.store.getCount();
  925. var msg = count == 0 ?
  926. this.emptyMsg :
  927. String.format(
  928. this.displayMsg,
  929. this.cursor+1, this.cursor+count, this.store.getTotalCount()
  930. );
  931. this.displayItem.setText(msg);
  932. }
  933. },
  934. // private
  935. onLoad : function(store, r, o){
  936. if(!this.rendered){
  937. this.dsLoaded = [store, r, o];
  938. return;
  939. }
  940. var p = this.getParams();
  941. this.cursor = (o.params && o.params[p.start]) ? o.params[p.start] : 0;
  942. var d = this.getPageData(), ap = d.activePage, ps = d.pages;
  943. this.afterTextItem.setText(String.format(this.afterPageText, d.pages));
  944. this.inputItem.setValue(ap);
  945. this.first.setDisabled(ap == 1);
  946. this.prev.setDisabled(ap == 1);
  947. this.next.setDisabled(ap == ps);
  948. this.last.setDisabled(ap == ps);
  949. this.refresh.enable();
  950. this.updateInfo();
  951. this.fireEvent('change', this, d);
  952. },
  953. // private
  954. getPageData : function(){
  955. var total = this.store.getTotalCount();
  956. return {
  957. total : total,
  958. activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
  959. pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
  960. };
  961. },
  962. /**
  963. * Change the active page
  964. * @param {Integer} page The page to display
  965. */
  966. changePage : function(page){
  967. this.doLoad(((page-1) * this.pageSize).constrain(0, this.store.getTotalCount()));
  968. },
  969. // private
  970. onLoadError : function(){
  971. if(!this.rendered){
  972. return;
  973. }
  974. this.refresh.enable();
  975. },
  976. // private
  977. readPage : function(d){
  978. var v = this.inputItem.getValue(), pageNum;
  979. if (!v || isNaN(pageNum = parseInt(v, 10))) {
  980. this.inputItem.setValue(d.activePage);
  981. return false;
  982. }
  983. return pageNum;
  984. },
  985. onPagingFocus : function(){
  986. this.inputItem.select();
  987. },
  988. //private
  989. onPagingBlur : function(e){
  990. this.inputItem.setValue(this.getPageData().activePage);
  991. },
  992. // private
  993. onPagingKeyDown : function(field, e){
  994. var k = e.getKey(), d = this.getPageData(), pageNum;
  995. if (k == e.RETURN) {
  996. e.stopEvent();
  997. pageNum = this.readPage(d);
  998. if(pageNum !== false){
  999. pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
  1000. this.doLoad(pageNum * this.pageSize);
  1001. }
  1002. }else if (k == e.HOME || k == e.END){
  1003. e.stopEvent();
  1004. pageNum = k == e.HOME ? 1 : d.pages;
  1005. field.setValue(pageNum);
  1006. }else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN){
  1007. e.stopEvent();
  1008. if((pageNum = this.readPage(d))){
  1009. var increment = e.shiftKey ? 10 : 1;
  1010. if(k == e.DOWN || k == e.PAGEDOWN){
  1011. increment *= -1;
  1012. }
  1013. pageNum += increment;
  1014. if(pageNum >= 1 & pageNum <= d.pages){
  1015. field.setValue(pageNum);
  1016. }
  1017. }
  1018. }
  1019. },
  1020. // private
  1021. getParams : function(){
  1022. //retain backwards compat, allow params on the toolbar itself, if they exist.
  1023. return this.paramNames || this.store.paramNames;
  1024. },
  1025. // private
  1026. beforeLoad : function(){
  1027. if(this.rendered && this.refresh){
  1028. this.refresh.disable();
  1029. }
  1030. },
  1031. // private
  1032. doLoad : function(start){
  1033. var o = {}, pn = this.getParams();
  1034. o[pn.start] = start;
  1035. o[pn.limit] = this.pageSize;
  1036. if(this.fireEvent('beforechange', this, o) !== false){
  1037. this.store.load({params:o});
  1038. }
  1039. },
  1040. /**
  1041. * Move to the first page, has the same effect as clicking the 'first' button.
  1042. */
  1043. moveFirst : function(){
  1044. this.doLoad(0);
  1045. },
  1046. /**
  1047. * Move to the previous page, has the same effect as clicking the 'previous' button.
  1048. */
  1049. movePrevious : function(){
  1050. this.doLoad(Math.max(0, this.cursor-this.pageSize));
  1051. },
  1052. /**
  1053. * Move to the next page, has the same effect as clicking the 'next' button.
  1054. */
  1055. moveNext : function(){
  1056. this.doLoad(this.cursor+this.pageSize);
  1057. },
  1058. /**
  1059. * Move to the last page, has the same effect as clicking the 'last' button.
  1060. */
  1061. moveLast : function(){
  1062. var total = this.store.getTotalCount(),
  1063. extra = total % this.pageSize;
  1064. this.doLoad(extra ? (total - extra) : total - this.pageSize);
  1065. },
  1066. /**
  1067. * Refresh the current page, has the same effect as clicking the 'refresh' button.
  1068. */
  1069. doRefresh : function(){
  1070. this.doLoad(this.cursor);
  1071. },
  1072. /**
  1073. * Binds the paging toolbar to the specified {@link Ext.data.Store}
  1074. * @param {Store} store The store to bind to this toolbar
  1075. * @param {Boolean} initial (Optional) true to not remove listeners
  1076. */
  1077. bindStore : function(store, initial){
  1078. var doLoad;
  1079. if(!initial && this.store){
  1080. if(store !== this.store && this.store.autoDestroy){
  1081. this.store.destroy();
  1082. }else{
  1083. this.store.un('beforeload', this.beforeLoad, this);
  1084. this.store.un('load', this.onLoad, this);
  1085. this.store.un('exception', this.onLoadError, this);
  1086. }
  1087. if(!store){
  1088. this.store = null;
  1089. }
  1090. }
  1091. if(store){
  1092. store = Ext.StoreMgr.lookup(store);
  1093. store.on({
  1094. scope: this,
  1095. beforeload: this.beforeLoad,
  1096. load: this.onLoad,
  1097. exception: this.onLoadError
  1098. });
  1099. doLoad = true;
  1100. }
  1101. this.store = store;
  1102. if(doLoad){
  1103. this.onLoad(store, null, {});
  1104. }
  1105. },
  1106. /**
  1107. * Unbinds the paging toolbar from the specified {@link Ext.data.Store} <b>(deprecated)</b>
  1108. * @param {Ext.data.Store} store The data store to unbind
  1109. */
  1110. unbind : function(store){
  1111. this.bindStore(null);
  1112. },
  1113. /**
  1114. * Binds the paging toolbar to the specified {@link Ext.data.Store} <b>(deprecated)</b>
  1115. * @param {Ext.data.Store} store The data store to bind
  1116. */
  1117. bind : function(store){
  1118. this.bindStore(store);
  1119. },
  1120. // private
  1121. onDestroy : function(){
  1122. this.bindStore(null);
  1123. Ext.PagingToolbar.superclass.onDestroy.call(this);
  1124. }
  1125. });
  1126. })();
  1127. Ext.reg('paging', Ext.PagingToolbar);