3d6086f5ed0cb94aa3977fd9b09720c90c3a71ff.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.Toolbar
  8. * @extends Ext.BoxComponent Basic Toolbar class.
  9. * @constructor Creates a new Toolbar
  10. * @param {Object/Array}
  11. * config A config object or an array of buttons to add
  12. */
  13. Ext.Toolbar = function(config) {
  14. if (config instanceof Array) {
  15. config = {
  16. buttons : config
  17. };
  18. }
  19. Ext.Toolbar.superclass.constructor.call(this, config);
  20. };
  21. (function() {
  22. var T = Ext.Toolbar;
  23. Ext.extend(T, Ext.BoxComponent, {
  24. trackMenus : true,
  25. // private
  26. initComponent : function() {
  27. T.superclass.initComponent.call(this);
  28. if (this.items) {
  29. this.buttons = this.items;
  30. }
  31. this.items = new Ext.util.MixedCollection(false,
  32. function(o) {
  33. return o.itemId || o.id || Ext.id();
  34. });
  35. },
  36. // private
  37. autoCreate : {
  38. cls : 'x-toolbar x-small-editor',
  39. html : '<table cellspacing="0"><tr></tr></table>'
  40. },
  41. // private
  42. onRender : function(ct, position) {
  43. this.el = ct.createChild(this.autoCreate, position);
  44. this.tr = this.el.child("tr", true);
  45. },
  46. // private
  47. afterRender : function() {
  48. T.superclass.afterRender.call(this);
  49. if (this.buttons) {
  50. this.add.apply(this, this.buttons);
  51. delete this.buttons;
  52. }
  53. },
  54. /**
  55. * Adds element(s) to the toolbar -- this function takes a
  56. * variable number of arguments of mixed type and adds them to
  57. * the toolbar.
  58. *
  59. * @param {Mixed}
  60. * arg1 The following types of arguments are all
  61. * valid:<br />
  62. * <ul>
  63. * <li>{@link Ext.Toolbar.Button} config: A valid
  64. * button config object (equivalent to
  65. * {@link #addButton})</li>
  66. * <li>HtmlElement: Any standard HTML element
  67. * (equivalent to {@link #addElement})</li>
  68. * <li>Field: Any form field (equivalent to
  69. * {@link #addField})</li>
  70. * <li>Item: Any subclass of
  71. * {@link Ext.Toolbar.Item} (equivalent to
  72. * {@link #addItem})</li>
  73. * <li>String: Any generic string (gets wrapped in a
  74. * {@link Ext.Toolbar.TextItem}, equivalent to
  75. * {@link #addText}). Note that there are a few
  76. * special strings that are treated differently as
  77. * explained next.</li>
  78. * <li>'separator' or '-': Creates a separator
  79. * element (equivalent to {@link #addSeparator})</li>
  80. * <li>' ': Creates a spacer element (equivalent to
  81. * {@link #addSpacer})</li>
  82. * <li>'->': Creates a fill element (equivalent to
  83. * {@link #addFill})</li>
  84. * </ul>
  85. * @param {Mixed}
  86. * arg2
  87. * @param {Mixed}
  88. * etc.
  89. */
  90. add : function() {
  91. var a = arguments, l = a.length;
  92. for (var i = 0; i < l; i++) {
  93. var el = a[i];
  94. if (el.isFormField) { // some kind of form field
  95. this.addField(el);
  96. } else if (el.render) { // some kind of Toolbar.Item
  97. this.addItem(el);
  98. } else if (typeof el == "string") { // string
  99. if (el == "separator" || el == "-") {
  100. this.addSeparator();
  101. } else if (el == " ") {
  102. this.addSpacer();
  103. } else if (el == "->") {
  104. this.addFill();
  105. } else {
  106. this.addText(el);
  107. }
  108. } else if (el.tagName) { // element
  109. this.addElement(el);
  110. } else if (typeof el == "object") { // must be button
  111. // config?
  112. if (el.xtype) {
  113. this.addField(Ext.ComponentMgr.create(el,
  114. 'button'));
  115. } else {
  116. this.addButton(el);
  117. }
  118. }
  119. }
  120. },
  121. /**
  122. * Adds a separator
  123. *
  124. * @return {Ext.Toolbar.Item} The separator item
  125. */
  126. addSeparator : function() {
  127. return this.addItem(new T.Separator());
  128. },
  129. /**
  130. * Adds a spacer element
  131. *
  132. * @return {Ext.Toolbar.Spacer} The spacer item
  133. */
  134. addSpacer : function() {
  135. return this.addItem(new T.Spacer());
  136. },
  137. /**
  138. * Adds a fill element that forces subsequent additions to the
  139. * right side of the toolbar
  140. *
  141. * @return {Ext.Toolbar.Fill} The fill item
  142. */
  143. addFill : function() {
  144. return this.addItem(new T.Fill());
  145. },
  146. /**
  147. * Adds any standard HTML element to the toolbar
  148. *
  149. * @param {Mixed}
  150. * el The element or id of the element to add
  151. * @return {Ext.Toolbar.Item} The element's item
  152. */
  153. addElement : function(el) {
  154. return this.addItem(new T.Item(el));
  155. },
  156. /**
  157. * Adds any Toolbar.Item or subclass
  158. *
  159. * @param {Ext.Toolbar.Item}
  160. * item
  161. * @return {Ext.Toolbar.Item} The item
  162. */
  163. addItem : function(item) {
  164. var td = this.nextBlock();
  165. this.initMenuTracking(item);
  166. item.render(td);
  167. this.items.add(item);
  168. return item;
  169. },
  170. /**
  171. * Adds a button (or buttons). See {@link Ext.Toolbar.Button}
  172. * for more info on the config.
  173. *
  174. * @param {Object/Array}
  175. * config A button config or array of configs
  176. * @return {Ext.Toolbar.Button/Array}
  177. */
  178. addButton : function(config) {
  179. if (config instanceof Array) {
  180. var buttons = [];
  181. for (var i = 0, len = config.length; i < len; i++) {
  182. buttons.push(this.addButton(config[i]));
  183. }
  184. return buttons;
  185. }
  186. var b = config;
  187. if (!(config instanceof T.Button)) {
  188. b = config.split
  189. ? new T.SplitButton(config)
  190. : new T.Button(config);
  191. }
  192. var td = this.nextBlock();
  193. this.initMenuTracking(b);
  194. b.render(td);
  195. this.items.add(b);
  196. return b;
  197. },
  198. // private
  199. initMenuTracking : function(item) {
  200. if (this.trackMenus && item.menu) {
  201. item.on({
  202. 'menutriggerover' : this.onButtonTriggerOver,
  203. 'menushow' : this.onButtonMenuShow,
  204. 'menuhide' : this.onButtonMenuHide,
  205. scope : this
  206. })
  207. }
  208. },
  209. /**
  210. * Adds text to the toolbar
  211. *
  212. * @param {String}
  213. * text The text to add
  214. * @return {Ext.Toolbar.Item} The element's item
  215. */
  216. addText : function(text) {
  217. return this.addItem(new T.TextItem(text));
  218. },
  219. /**
  220. * Inserts any {@link Ext.Toolbar.Item}/{@link Ext.Toolbar.Button}
  221. * at the specified index.
  222. *
  223. * @param {Number}
  224. * index The index where the item is to be inserted
  225. * @param {Object/Ext.Toolbar.Item/Ext.Toolbar.Button/Array}
  226. * item The button, or button config object to be
  227. * inserted, or an array of buttons/configs.
  228. * @return {Ext.Toolbar.Button/Item}
  229. */
  230. insertButton : function(index, item) {
  231. if (item instanceof Array) {
  232. var buttons = [];
  233. for (var i = 0, len = item.length; i < len; i++) {
  234. buttons.push(this.insertButton(index + i, item[i]));
  235. }
  236. return buttons;
  237. }
  238. if (!(item instanceof T.Button)) {
  239. item = new T.Button(item);
  240. }
  241. var td = document.createElement("td");
  242. this.tr.insertBefore(td, this.tr.childNodes[index]);
  243. this.initMenuTracking(item);
  244. item.render(td);
  245. this.items.insert(index, item);
  246. return item;
  247. },
  248. /**
  249. * Adds a new element to the toolbar from the passed
  250. * {@link Ext.DomHelper} config
  251. *
  252. * @param {Object}
  253. * config
  254. * @return {Ext.Toolbar.Item} The element's item
  255. */
  256. addDom : function(config, returnEl) {
  257. var td = this.nextBlock();
  258. Ext.DomHelper.overwrite(td, config);
  259. var ti = new T.Item(td.firstChild);
  260. ti.render(td);
  261. this.items.add(ti);
  262. return ti;
  263. },
  264. /**
  265. * Adds a dynamically rendered Ext.form field (TextField,
  266. * ComboBox, etc). Note: the field should not have been rendered
  267. * yet. For a field that has already been rendered, use
  268. * {@link #addElement}.
  269. *
  270. * @param {Ext.form.Field}
  271. * field
  272. * @return {Ext.ToolbarItem}
  273. */
  274. addField : function(field) {
  275. var td = this.nextBlock();
  276. field.render(td);
  277. var ti = new T.Item(td.firstChild);
  278. ti.render(td);
  279. this.items.add(ti);
  280. return ti;
  281. },
  282. // private
  283. nextBlock : function() {
  284. var td = document.createElement("td");
  285. this.tr.appendChild(td);
  286. return td;
  287. },
  288. // private
  289. onDestroy : function() {
  290. Ext.Toolbar.superclass.onDestroy.call(this);
  291. if (this.rendered) {
  292. if (this.items) { // rendered?
  293. Ext.destroy.apply(Ext, this.items.items);
  294. }
  295. Ext.Element.uncache(this.tr);
  296. }
  297. },
  298. // private
  299. onDisable : function() {
  300. this.items.each(function(item) {
  301. if (item.disable) {
  302. item.disable();
  303. }
  304. });
  305. },
  306. // private
  307. onEnable : function() {
  308. this.items.each(function(item) {
  309. if (item.enable) {
  310. item.enable();
  311. }
  312. });
  313. },
  314. // private
  315. onButtonTriggerOver : function(btn) {
  316. if (this.activeMenuBtn && this.activeMenuBtn != btn) {
  317. this.activeMenuBtn.hideMenu();
  318. btn.showMenu();
  319. this.activeMenuBtn = btn;
  320. }
  321. },
  322. // private
  323. onButtonMenuShow : function(btn) {
  324. this.activeMenuBtn = btn;
  325. },
  326. // private
  327. onButtonMenuHide : function(btn) {
  328. delete this.activeMenuBtn;
  329. }
  330. });
  331. Ext.reg('toolbar', Ext.Toolbar);
  332. /**
  333. * @class Ext.Toolbar.Item The base class that other classes should extend
  334. * in order to get some basic common toolbar item functionality.
  335. * @constructor Creates a new Item
  336. * @param {HTMLElement}
  337. * el
  338. */
  339. T.Item = function(el) {
  340. this.el = Ext.getDom(el);
  341. this.id = Ext.id(this.el);
  342. this.hidden = false;
  343. };
  344. T.Item.prototype = {
  345. /**
  346. * Get this item's HTML Element
  347. *
  348. * @return {HTMLElement}
  349. */
  350. getEl : function() {
  351. return this.el;
  352. },
  353. // private
  354. render : function(td) {
  355. this.td = td;
  356. td.appendChild(this.el);
  357. },
  358. /**
  359. * Removes and destroys this item.
  360. */
  361. destroy : function() {
  362. if (this.td && this.td.parentNode) {
  363. this.td.parentNode.removeChild(this.td);
  364. }
  365. },
  366. /**
  367. * Shows this item.
  368. */
  369. show : function() {
  370. this.hidden = false;
  371. this.td.style.display = "";
  372. },
  373. /**
  374. * Hides this item.
  375. */
  376. hide : function() {
  377. this.hidden = true;
  378. this.td.style.display = "none";
  379. },
  380. /**
  381. * Convenience function for boolean show/hide.
  382. *
  383. * @param {Boolean}
  384. * visible true to show/false to hide
  385. */
  386. setVisible : function(visible) {
  387. if (visible) {
  388. this.show();
  389. } else {
  390. this.hide();
  391. }
  392. },
  393. /**
  394. * Try to focus this item
  395. */
  396. focus : function() {
  397. Ext.fly(this.el).focus();
  398. },
  399. /**
  400. * Disables this item.
  401. */
  402. disable : function() {
  403. Ext.fly(this.td).addClass("x-item-disabled");
  404. this.disabled = true;
  405. this.el.disabled = true;
  406. },
  407. /**
  408. * Enables this item.
  409. */
  410. enable : function() {
  411. Ext.fly(this.td).removeClass("x-item-disabled");
  412. this.disabled = false;
  413. this.el.disabled = false;
  414. }
  415. };
  416. Ext.reg('tbitem', T.Item);
  417. /**
  418. * @class Ext.Toolbar.Separator
  419. * @extends Ext.Toolbar.Item A simple toolbar separator class
  420. * @constructor Creates a new Separator
  421. */
  422. T.Separator = function() {
  423. var s = document.createElement("span");
  424. s.className = "ytb-sep";
  425. T.Separator.superclass.constructor.call(this, s);
  426. };
  427. Ext.extend(T.Separator, T.Item, {
  428. enable : Ext.emptyFn,
  429. disable : Ext.emptyFn,
  430. focus : Ext.emptyFn
  431. });
  432. Ext.reg('tbseparator', T.Separator);
  433. /**
  434. * @class Ext.Toolbar.Spacer
  435. * @extends Ext.Toolbar.Item A simple element that adds extra horizontal
  436. * space to a toolbar.
  437. * @constructor Creates a new Spacer
  438. */
  439. T.Spacer = function() {
  440. var s = document.createElement("div");
  441. s.className = "ytb-spacer";
  442. T.Spacer.superclass.constructor.call(this, s);
  443. };
  444. Ext.extend(T.Spacer, T.Item, {
  445. enable : Ext.emptyFn,
  446. disable : Ext.emptyFn,
  447. focus : Ext.emptyFn
  448. });
  449. Ext.reg('tbspacer', T.Spacer);
  450. /**
  451. * @class Ext.Toolbar.Fill
  452. * @extends Ext.Toolbar.Spacer A simple element that adds a greedy (100%
  453. * width) horizontal space to a toolbar.
  454. * @constructor Creates a new Spacer
  455. */
  456. T.Fill = Ext.extend(T.Spacer, {
  457. // private
  458. render : function(td) {
  459. td.style.width = '100%';
  460. T.Fill.superclass.render.call(this, td);
  461. }
  462. });
  463. Ext.reg('tbfill', T.Fill);
  464. /**
  465. * @class Ext.Toolbar.TextItem
  466. * @extends Ext.Toolbar.Item A simple class that renders text directly into
  467. * a toolbar.
  468. * @constructor Creates a new TextItem
  469. * @param {String}
  470. * text
  471. */
  472. T.TextItem = function(text) {
  473. var s = document.createElement("span");
  474. s.className = "ytb-text";
  475. s.innerHTML = text;
  476. T.TextItem.superclass.constructor.call(this, s);
  477. };
  478. Ext.extend(T.TextItem, T.Item, {
  479. enable : Ext.emptyFn,
  480. disable : Ext.emptyFn,
  481. focus : Ext.emptyFn
  482. });
  483. Ext.reg('tbtext', T.TextItem);
  484. /**
  485. * @class Ext.Toolbar.Button
  486. * @extends Ext.Button A button that renders into a toolbar.
  487. * @constructor Creates a new Button
  488. * @param {Object}
  489. * config A standard {@link Ext.Button} config object
  490. */
  491. T.Button = Ext.extend(Ext.Button, {
  492. hideParent : true,
  493. onDestroy : function() {
  494. T.Button.superclass.onDestroy.call(this);
  495. if (this.container) {
  496. this.container.remove();
  497. }
  498. }
  499. });
  500. Ext.reg('tbbutton', T.Button);
  501. /**
  502. * @class Ext.Toolbar.SplitButton
  503. * @extends Ext.SplitButton A menu button that renders into a toolbar.
  504. * @constructor Creates a new SplitButton
  505. * @param {Object}
  506. * config A standard {@link Ext.SplitButton} config object
  507. */
  508. T.SplitButton = Ext.extend(Ext.SplitButton, {
  509. hideParent : true,
  510. onDestroy : function() {
  511. T.SplitButton.superclass.onDestroy.call(this);
  512. if (this.container) {
  513. this.container.remove();
  514. }
  515. }
  516. });
  517. Ext.reg('tbsplit', T.SplitButton);
  518. // backwards compat
  519. T.MenuButton = T.SplitButton;
  520. })();