Toolbar.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-toolbar-Toolbar-method-constructor'><span id='Ext-toolbar-Toolbar'>/**
  19. </span></span> * Basic Toolbar class. Although the {@link Ext.container.Container#defaultType defaultType} for
  20. * Toolbar is {@link Ext.button.Button button}, Toolbar elements (child items for the Toolbar container)
  21. * may be virtually any type of Component. Toolbar elements can be created explicitly via their
  22. * constructors, or implicitly via their xtypes, and can be {@link #method-add}ed dynamically.
  23. *
  24. * ## Some items have shortcut strings for creation:
  25. *
  26. * | Shortcut | xtype | Class | Description
  27. * |:---------|:--------------|:------------------------------|:---------------------------------------------------
  28. * | `-&gt;` | `tbfill` | {@link Ext.toolbar.Fill} | begin using the right-justified button container
  29. * | `-` | `tbseparator` | {@link Ext.toolbar.Separator} | add a vertical separator bar between toolbar items
  30. * | ` ` | `tbspacer` | {@link Ext.toolbar.Spacer} | add horiztonal space between elements
  31. *
  32. * @example
  33. * Ext.create('Ext.toolbar.Toolbar', {
  34. * renderTo: document.body,
  35. * width : 500,
  36. * items: [
  37. * {
  38. * // xtype: 'button', // default for Toolbars
  39. * text: 'Button'
  40. * },
  41. * {
  42. * xtype: 'splitbutton',
  43. * text : 'Split Button'
  44. * },
  45. * // begin using the right-justified button container
  46. * '-&gt;', // same as { xtype: 'tbfill' }
  47. * {
  48. * xtype : 'textfield',
  49. * name : 'field1',
  50. * emptyText: 'enter search term'
  51. * },
  52. * // add a vertical separator bar between toolbar items
  53. * '-', // same as {xtype: 'tbseparator'} to create Ext.toolbar.Separator
  54. * 'text 1', // same as {xtype: 'tbtext', text: 'text1'} to create Ext.toolbar.TextItem
  55. * { xtype: 'tbspacer' },// same as ' ' to create Ext.toolbar.Spacer
  56. * 'text 2',
  57. * { xtype: 'tbspacer', width: 50 }, // add a 50px space
  58. * 'text 3'
  59. * ]
  60. * });
  61. *
  62. * Toolbars have {@link #method-enable} and {@link #method-disable} methods which when called, will
  63. * enable/disable all items within your toolbar.
  64. *
  65. * @example
  66. * Ext.create('Ext.toolbar.Toolbar', {
  67. * renderTo: document.body,
  68. * width : 400,
  69. * items: [
  70. * {
  71. * text: 'Button'
  72. * },
  73. * {
  74. * xtype: 'splitbutton',
  75. * text : 'Split Button'
  76. * },
  77. * '-&gt;',
  78. * {
  79. * xtype : 'textfield',
  80. * name : 'field1',
  81. * emptyText: 'enter search term'
  82. * }
  83. * ]
  84. * });
  85. *
  86. * Example
  87. *
  88. * @example
  89. * var enableBtn = Ext.create('Ext.button.Button', {
  90. * text : 'Enable All Items',
  91. * disabled: true,
  92. * scope : this,
  93. * handler : function() {
  94. * //disable the enable button and enable the disable button
  95. * enableBtn.disable();
  96. * disableBtn.enable();
  97. *
  98. * //enable the toolbar
  99. * toolbar.enable();
  100. * }
  101. * });
  102. *
  103. * var disableBtn = Ext.create('Ext.button.Button', {
  104. * text : 'Disable All Items',
  105. * scope : this,
  106. * handler : function() {
  107. * //enable the enable button and disable button
  108. * disableBtn.disable();
  109. * enableBtn.enable();
  110. *
  111. * //disable the toolbar
  112. * toolbar.disable();
  113. * }
  114. * });
  115. *
  116. * var toolbar = Ext.create('Ext.toolbar.Toolbar', {
  117. * renderTo: document.body,
  118. * width : 400,
  119. * margin : '5 0 0 0',
  120. * items : [enableBtn, disableBtn]
  121. * });
  122. *
  123. * Adding items to and removing items from a toolbar is as simple as calling the {@link #method-add}
  124. * and {@link #method-remove} methods. There is also a {@link #removeAll} method
  125. * which remove all items within the toolbar.
  126. *
  127. * @example
  128. * var toolbar = Ext.create('Ext.toolbar.Toolbar', {
  129. * renderTo: document.body,
  130. * width : 700,
  131. * items: [
  132. * {
  133. * text: 'Example Button'
  134. * }
  135. * ]
  136. * });
  137. *
  138. * var addedItems = [];
  139. *
  140. * Ext.create('Ext.toolbar.Toolbar', {
  141. * renderTo: document.body,
  142. * width : 700,
  143. * margin : '5 0 0 0',
  144. * items : [
  145. * {
  146. * text : 'Add a button',
  147. * scope : this,
  148. * handler: function() {
  149. * var text = prompt('Please enter the text for your button:');
  150. * addedItems.push(toolbar.add({
  151. * text: text
  152. * }));
  153. * }
  154. * },
  155. * {
  156. * text : 'Add a text item',
  157. * scope : this,
  158. * handler: function() {
  159. * var text = prompt('Please enter the text for your item:');
  160. * addedItems.push(toolbar.add(text));
  161. * }
  162. * },
  163. * {
  164. * text : 'Add a toolbar separator',
  165. * scope : this,
  166. * handler: function() {
  167. * addedItems.push(toolbar.add('-'));
  168. * }
  169. * },
  170. * {
  171. * text : 'Add a toolbar spacer',
  172. * scope : this,
  173. * handler: function() {
  174. * addedItems.push(toolbar.add('-&gt;'));
  175. * }
  176. * },
  177. * '-&gt;',
  178. * {
  179. * text : 'Remove last inserted item',
  180. * scope : this,
  181. * handler: function() {
  182. * if (addedItems.length) {
  183. * toolbar.remove(addedItems.pop());
  184. * } else if (toolbar.items.length) {
  185. * toolbar.remove(toolbar.items.last());
  186. * } else {
  187. * alert('No items in the toolbar');
  188. * }
  189. * }
  190. * },
  191. * {
  192. * text : 'Remove all items',
  193. * scope : this,
  194. * handler: function() {
  195. * toolbar.removeAll();
  196. * }
  197. * }
  198. * ]
  199. * });
  200. *
  201. * @constructor
  202. * Creates a new Toolbar
  203. * @param {Object/Object[]} config A config object or an array of buttons to {@link #method-add}
  204. * @docauthor Robert Dougan &lt;rob@sencha.com&gt;
  205. */
  206. Ext.define('Ext.toolbar.Toolbar', {
  207. extend: 'Ext.container.Container',
  208. requires: [
  209. 'Ext.toolbar.Fill',
  210. 'Ext.layout.container.HBox',
  211. 'Ext.layout.container.VBox'
  212. ],
  213. uses: [
  214. 'Ext.toolbar.Separator'
  215. ],
  216. alias: 'widget.toolbar',
  217. alternateClassName: 'Ext.Toolbar',
  218. <span id='Ext-toolbar-Toolbar-property-isToolbar'> /**
  219. </span> * @property {Boolean} isToolbar
  220. * `true` in this class to identify an object as an instantiated Toolbar, or subclass thereof.
  221. */
  222. isToolbar: true,
  223. baseCls : Ext.baseCSSPrefix + 'toolbar',
  224. ariaRole : 'toolbar',
  225. defaultType: 'button',
  226. <span id='Ext-toolbar-Toolbar-cfg-vertical'> /**
  227. </span> * @cfg {Boolean} vertical
  228. * Set to `true` to make the toolbar vertical. The layout will become a `vbox`.
  229. */
  230. vertical: false,
  231. <span id='Ext-toolbar-Toolbar-cfg-layout'> /**
  232. </span> * @cfg {String/Object} layout
  233. * This class assigns a default layout (`layout: 'hbox'`).
  234. * Developers _may_ override this configuration option if another layout
  235. * is required (the constructor must be passed a configuration object in this
  236. * case instead of an array).
  237. * See {@link Ext.container.Container#layout} for additional information.
  238. */
  239. <span id='Ext-toolbar-Toolbar-cfg-enableOverflow'> /**
  240. </span> * @cfg {Boolean} enableOverflow
  241. * Configure true to make the toolbar provide a button which activates a dropdown Menu to show
  242. * items which overflow the Toolbar's width.
  243. */
  244. enableOverflow: false,
  245. <span id='Ext-toolbar-Toolbar-cfg-menuTriggerCls'> /**
  246. </span> * @cfg {String} menuTriggerCls
  247. * Configure the icon class of the overflow button.
  248. */
  249. menuTriggerCls: Ext.baseCSSPrefix + 'toolbar-more-icon',
  250. // private
  251. trackMenus: true,
  252. itemCls: Ext.baseCSSPrefix + 'toolbar-item',
  253. statics: {
  254. shortcuts: {
  255. '-' : 'tbseparator',
  256. ' ' : 'tbspacer'
  257. },
  258. shortcutsHV: {
  259. // horizontal
  260. 0: {
  261. '-&gt;': { xtype: 'tbfill', height: 0 }
  262. },
  263. // vertical
  264. 1: {
  265. '-&gt;': { xtype: 'tbfill', width: 0 }
  266. }
  267. }
  268. },
  269. initComponent: function() {
  270. var me = this,
  271. keys;
  272. // check for simplified (old-style) overflow config:
  273. if (!me.layout &amp;&amp; me.enableOverflow) {
  274. me.layout = { overflowHandler: 'Menu' };
  275. }
  276. if (me.dock === 'right' || me.dock === 'left') {
  277. me.vertical = true;
  278. }
  279. me.layout = Ext.applyIf(Ext.isString(me.layout) ? {
  280. type: me.layout
  281. } : me.layout || {}, {
  282. type: me.vertical ? 'vbox' : 'hbox',
  283. align: me.vertical ? 'stretchmax' : 'middle'
  284. });
  285. if (me.vertical) {
  286. me.addClsWithUI('vertical');
  287. }
  288. // @TODO: remove this hack and implement a more general solution
  289. if (me.ui === 'footer') {
  290. me.ignoreBorderManagement = true;
  291. }
  292. me.callParent();
  293. <span id='Ext-toolbar-Toolbar-event-overflowchange'> /**
  294. </span> * @event overflowchange
  295. * Fires after the overflow state has changed.
  296. * @param {Object} c The Container
  297. * @param {Boolean} lastOverflow overflow state
  298. */
  299. me.addEvents('overflowchange');
  300. },
  301. getRefItems: function(deep) {
  302. var me = this,
  303. items = me.callParent(arguments),
  304. layout = me.layout,
  305. handler;
  306. if (deep &amp;&amp; me.enableOverflow) {
  307. handler = layout.overflowHandler;
  308. if (handler &amp;&amp; handler.menu) {
  309. items = items.concat(handler.menu.getRefItems(deep));
  310. }
  311. }
  312. return items;
  313. },
  314. <span id='Ext-toolbar-Toolbar-method-add'> /**
  315. </span> * Adds element(s) to the toolbar -- this function takes a variable number of
  316. * arguments of mixed type and adds them to the toolbar.
  317. *
  318. * **Note**: See the notes within {@link Ext.container.Container#method-add}.
  319. *
  320. * @param {Object...} args The following types of arguments are all valid:
  321. *
  322. * - `{@link Ext.button.Button config}`: A valid button config object
  323. * - `HtmlElement`: Any standard HTML element
  324. * - `Field`: Any form field
  325. * - `Item`: Any subclass of {@link Ext.toolbar.Item}
  326. * - `String`: Any generic string (gets wrapped in a {@link Ext.toolbar.TextItem}).
  327. *
  328. * Note that there are a few special strings that are treated differently as explained next:
  329. *
  330. * - `'-'`: Creates a separator element
  331. * - `' '`: Creates a spacer element
  332. * - `'-&gt;'`: Creates a fill element
  333. *
  334. * @method add
  335. */
  336. // private
  337. lookupComponent: function(c) {
  338. if (typeof c == 'string') {
  339. var T = Ext.toolbar.Toolbar,
  340. shortcut = T.shortcutsHV[this.vertical ? 1 : 0][c] || T.shortcuts[c];
  341. if (typeof shortcut == 'string') {
  342. c = {
  343. xtype: shortcut
  344. };
  345. } else if (shortcut) {
  346. c = Ext.apply({}, shortcut);
  347. } else {
  348. c = {
  349. xtype: 'tbtext',
  350. text: c
  351. };
  352. }
  353. this.applyDefaults(c);
  354. }
  355. return this.callParent(arguments);
  356. },
  357. // private
  358. applyDefaults: function(c) {
  359. if (!Ext.isString(c)) {
  360. c = this.callParent(arguments);
  361. }
  362. return c;
  363. },
  364. // private
  365. trackMenu: function(item, remove) {
  366. if (this.trackMenus &amp;&amp; item.menu) {
  367. var method = remove ? 'mun' : 'mon',
  368. me = this;
  369. me[method](item, 'mouseover', me.onButtonOver, me);
  370. me[method](item, 'menushow', me.onButtonMenuShow, me);
  371. me[method](item, 'menuhide', me.onButtonMenuHide, me);
  372. }
  373. },
  374. // private
  375. constructButton: function(item) {
  376. return item.events ? item
  377. : Ext.widget(item.split ? 'splitbutton' : this.defaultType, item);
  378. },
  379. // private
  380. onBeforeAdd: function(component) {
  381. if (component.is('field') || (component.is('button') &amp;&amp; this.ui != 'footer')) {
  382. component.ui = component.ui + '-toolbar';
  383. }
  384. // Any separators needs to know if is vertical or not
  385. if (component instanceof Ext.toolbar.Separator) {
  386. component.setUI((this.vertical) ? 'vertical' : 'horizontal');
  387. }
  388. this.callParent(arguments);
  389. },
  390. // private
  391. onAdd: function(component) {
  392. this.callParent(arguments);
  393. this.trackMenu(component);
  394. },
  395. // private
  396. onRemove: function(c) {
  397. this.callParent(arguments);
  398. this.trackMenu(c, true);
  399. },
  400. getChildItemsToDisable: function() {
  401. return this.items.getRange();
  402. },
  403. // private
  404. onButtonOver: function(btn){
  405. if (this.activeMenuBtn &amp;&amp; this.activeMenuBtn != btn) {
  406. this.activeMenuBtn.hideMenu();
  407. btn.showMenu();
  408. this.activeMenuBtn = btn;
  409. }
  410. },
  411. // private
  412. onButtonMenuShow: function(btn) {
  413. this.activeMenuBtn = btn;
  414. },
  415. // private
  416. onButtonMenuHide: function(btn) {
  417. delete this.activeMenuBtn;
  418. }
  419. });</pre>
  420. </body>
  421. </html>