PageRenderTime 166ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/ext-4.0.7/src/panel/AbstractPanel.js

https://bitbucket.org/srogerf/javascript
JavaScript | 572 lines | 305 code | 77 blank | 190 comment | 82 complexity | 1a386d8faf8f16cfd9fdcf9f581808d0 MD5 | raw file
  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.panel.AbstractPanel
  11. * @extends Ext.container.Container
  12. * A base class which provides methods common to Panel classes across the Sencha product range.
  13. * @private
  14. */
  15. Ext.define('Ext.panel.AbstractPanel', {
  16. /* Begin Definitions */
  17. extend: 'Ext.container.Container',
  18. requires: ['Ext.util.MixedCollection', 'Ext.Element', 'Ext.toolbar.Toolbar'],
  19. /* End Definitions */
  20. /**
  21. * @cfg {String} [baseCls='x-panel']
  22. * The base CSS class to apply to this panel's element.
  23. */
  24. baseCls : Ext.baseCSSPrefix + 'panel',
  25. /**
  26. * @cfg {Number/String} bodyPadding
  27. * A shortcut for setting a padding style on the body element. The value can either be
  28. * a number to be applied to all sides, or a normal css string describing padding.
  29. */
  30. /**
  31. * @cfg {Boolean} bodyBorder
  32. * A shortcut to add or remove the border on the body of a panel. This only applies to a panel
  33. * which has the {@link #frame} configuration set to `true`.
  34. */
  35. /**
  36. * @cfg {String/Object/Function} bodyStyle
  37. * Custom CSS styles to be applied to the panel's body element, which can be supplied as a valid CSS style string,
  38. * an object containing style property name/value pairs or a function that returns such a string or object.
  39. * For example, these two formats are interpreted to be equivalent:<pre><code>
  40. bodyStyle: 'background:#ffc; padding:10px;'
  41. bodyStyle: {
  42. background: '#ffc',
  43. padding: '10px'
  44. }
  45. * </code></pre>
  46. */
  47. /**
  48. * @cfg {String/String[]} bodyCls
  49. * A CSS class, space-delimited string of classes, or array of classes to be applied to the panel's body element.
  50. * The following examples are all valid:<pre><code>
  51. bodyCls: 'foo'
  52. bodyCls: 'foo bar'
  53. bodyCls: ['foo', 'bar']
  54. * </code></pre>
  55. */
  56. isPanel: true,
  57. componentLayout: 'dock',
  58. /**
  59. * @cfg {Object} defaultDockWeights
  60. * This object holds the default weights applied to dockedItems that have no weight. These start with a
  61. * weight of 1, to allow negative weights to insert before top items and are odd numbers
  62. * so that even weights can be used to get between different dock orders.
  63. *
  64. * To make default docking order match border layout, do this:
  65. * <pre><code>
  66. Ext.panel.AbstractPanel.prototype.defaultDockWeights = { top: 1, bottom: 3, left: 5, right: 7 };</code></pre>
  67. * Changing these defaults as above or individually on this object will effect all Panels.
  68. * To change the defaults on a single panel, you should replace the entire object:
  69. * <pre><code>
  70. initComponent: function () {
  71. // NOTE: Don't change members of defaultDockWeights since the object is shared.
  72. this.defaultDockWeights = { top: 1, bottom: 3, left: 5, right: 7 };
  73. this.callParent();
  74. }</code></pre>
  75. *
  76. * To change only one of the default values, you do this:
  77. * <pre><code>
  78. initComponent: function () {
  79. // NOTE: Don't change members of defaultDockWeights since the object is shared.
  80. this.defaultDockWeights = Ext.applyIf({ top: 10 }, this.defaultDockWeights);
  81. this.callParent();
  82. }</code></pre>
  83. */
  84. defaultDockWeights: { top: 1, left: 3, right: 5, bottom: 7 },
  85. renderTpl: [
  86. '<div id="{id}-body" class="{baseCls}-body<tpl if="bodyCls"> {bodyCls}</tpl>',
  87. ' {baseCls}-body-{ui}<tpl if="uiCls">',
  88. '<tpl for="uiCls"> {parent.baseCls}-body-{parent.ui}-{.}</tpl>',
  89. '</tpl>"<tpl if="bodyStyle"> style="{bodyStyle}"</tpl>>',
  90. '</div>'
  91. ],
  92. // TODO: Move code examples into product-specific files. The code snippet below is Touch only.
  93. /**
  94. * @cfg {Object/Object[]} dockedItems
  95. * A component or series of components to be added as docked items to this panel.
  96. * The docked items can be docked to either the top, right, left or bottom of a panel.
  97. * This is typically used for things like toolbars or tab bars:
  98. * <pre><code>
  99. var panel = new Ext.panel.Panel({
  100. fullscreen: true,
  101. dockedItems: [{
  102. xtype: 'toolbar',
  103. dock: 'top',
  104. items: [{
  105. text: 'Docked to the top'
  106. }]
  107. }]
  108. });</code></pre>
  109. */
  110. border: true,
  111. initComponent : function() {
  112. var me = this;
  113. me.addEvents(
  114. /**
  115. * @event bodyresize
  116. * Fires after the Panel has been resized.
  117. * @param {Ext.panel.Panel} p the Panel which has been resized.
  118. * @param {Number} width The Panel body's new width.
  119. * @param {Number} height The Panel body's new height.
  120. */
  121. 'bodyresize'
  122. // // inherited
  123. // 'activate',
  124. // // inherited
  125. // 'deactivate'
  126. );
  127. me.addChildEls('body');
  128. //!frame
  129. //!border
  130. if (me.frame && me.border && me.bodyBorder === undefined) {
  131. me.bodyBorder = false;
  132. }
  133. if (me.frame && me.border && (me.bodyBorder === false || me.bodyBorder === 0)) {
  134. me.manageBodyBorders = true;
  135. }
  136. me.callParent();
  137. },
  138. // @private
  139. initItems : function() {
  140. var me = this,
  141. items = me.dockedItems;
  142. me.callParent();
  143. me.dockedItems = Ext.create('Ext.util.MixedCollection', false, me.getComponentId);
  144. if (items) {
  145. me.addDocked(items);
  146. }
  147. },
  148. /**
  149. * Finds a docked component by id, itemId or position. Also see {@link #getDockedItems}
  150. * @param {String/Number} comp The id, itemId or position of the docked component (see {@link #getComponent} for details)
  151. * @return {Ext.Component} The docked component (if found)
  152. */
  153. getDockedComponent: function(comp) {
  154. if (Ext.isObject(comp)) {
  155. comp = comp.getItemId();
  156. }
  157. return this.dockedItems.get(comp);
  158. },
  159. /**
  160. * Attempts a default component lookup (see {@link Ext.container.Container#getComponent}). If the component is not found in the normal
  161. * items, the dockedItems are searched and the matched component (if any) returned (see {@link #getDockedComponent}). Note that docked
  162. * items will only be matched by component id or itemId -- if you pass a numeric index only non-docked child components will be searched.
  163. * @param {String/Number} comp The component id, itemId or position to find
  164. * @return {Ext.Component} The component (if found)
  165. */
  166. getComponent: function(comp) {
  167. var component = this.callParent(arguments);
  168. if (component === undefined && !Ext.isNumber(comp)) {
  169. // If the arg is a numeric index skip docked items
  170. component = this.getDockedComponent(comp);
  171. }
  172. return component;
  173. },
  174. /**
  175. * Parses the {@link bodyStyle} config if available to create a style string that will be applied to the body element.
  176. * This also includes {@link bodyPadding} and {@link bodyBorder} if available.
  177. * @return {String} A CSS style string with body styles, padding and border.
  178. * @private
  179. */
  180. initBodyStyles: function() {
  181. var me = this,
  182. bodyStyle = me.bodyStyle,
  183. styles = [],
  184. Element = Ext.Element,
  185. prop;
  186. if (Ext.isFunction(bodyStyle)) {
  187. bodyStyle = bodyStyle();
  188. }
  189. if (Ext.isString(bodyStyle)) {
  190. styles = bodyStyle.split(';');
  191. } else {
  192. for (prop in bodyStyle) {
  193. if (bodyStyle.hasOwnProperty(prop)) {
  194. styles.push(prop + ':' + bodyStyle[prop]);
  195. }
  196. }
  197. }
  198. if (me.bodyPadding !== undefined) {
  199. styles.push('padding: ' + Element.unitizeBox((me.bodyPadding === true) ? 5 : me.bodyPadding));
  200. }
  201. if (me.frame && me.bodyBorder) {
  202. if (!Ext.isNumber(me.bodyBorder)) {
  203. me.bodyBorder = 1;
  204. }
  205. styles.push('border-width: ' + Element.unitizeBox(me.bodyBorder));
  206. }
  207. delete me.bodyStyle;
  208. return styles.length ? styles.join(';') : undefined;
  209. },
  210. /**
  211. * Parse the {@link bodyCls} config if available to create a comma-delimited string of
  212. * CSS classes to be applied to the body element.
  213. * @return {String} The CSS class(es)
  214. * @private
  215. */
  216. initBodyCls: function() {
  217. var me = this,
  218. cls = '',
  219. bodyCls = me.bodyCls;
  220. if (bodyCls) {
  221. Ext.each(bodyCls, function(v) {
  222. cls += " " + v;
  223. });
  224. delete me.bodyCls;
  225. }
  226. return cls.length > 0 ? cls : undefined;
  227. },
  228. /**
  229. * Initialized the renderData to be used when rendering the renderTpl.
  230. * @return {Object} Object with keys and values that are going to be applied to the renderTpl
  231. * @private
  232. */
  233. initRenderData: function() {
  234. return Ext.applyIf(this.callParent(), {
  235. bodyStyle: this.initBodyStyles(),
  236. bodyCls: this.initBodyCls()
  237. });
  238. },
  239. /**
  240. * Adds docked item(s) to the panel.
  241. * @param {Object/Object[]} component The Component or array of components to add. The components
  242. * must include a 'dock' parameter on each component to indicate where it should be docked ('top', 'right',
  243. * 'bottom', 'left').
  244. * @param {Number} pos (optional) The index at which the Component will be added
  245. */
  246. addDocked : function(items, pos) {
  247. var me = this,
  248. i = 0,
  249. item, length;
  250. items = me.prepareItems(items);
  251. length = items.length;
  252. for (; i < length; i++) {
  253. item = items[i];
  254. item.dock = item.dock || 'top';
  255. // Allow older browsers to target docked items to style without borders
  256. if (me.border === false) {
  257. // item.cls = item.cls || '' + ' ' + me.baseCls + '-noborder-docked-' + item.dock;
  258. }
  259. if (pos !== undefined) {
  260. me.dockedItems.insert(pos + i, item);
  261. }
  262. else {
  263. me.dockedItems.add(item);
  264. }
  265. item.onAdded(me, i);
  266. me.onDockedAdd(item);
  267. }
  268. // Set flag which means that beforeLayout will not veto the layout due to the size not changing
  269. me.componentLayout.childrenChanged = true;
  270. if (me.rendered && !me.suspendLayout) {
  271. me.doComponentLayout();
  272. }
  273. return items;
  274. },
  275. // Placeholder empty functions
  276. onDockedAdd : Ext.emptyFn,
  277. onDockedRemove : Ext.emptyFn,
  278. /**
  279. * Inserts docked item(s) to the panel at the indicated position.
  280. * @param {Number} pos The index at which the Component will be inserted
  281. * @param {Object/Object[]} component. The Component or array of components to add. The components
  282. * must include a 'dock' paramater on each component to indicate where it should be docked ('top', 'right',
  283. * 'bottom', 'left').
  284. */
  285. insertDocked : function(pos, items) {
  286. this.addDocked(items, pos);
  287. },
  288. /**
  289. * Removes the docked item from the panel.
  290. * @param {Ext.Component} item. The Component to remove.
  291. * @param {Boolean} autoDestroy (optional) Destroy the component after removal.
  292. */
  293. removeDocked : function(item, autoDestroy) {
  294. var me = this,
  295. layout,
  296. hasLayout;
  297. if (!me.dockedItems.contains(item)) {
  298. return item;
  299. }
  300. layout = me.componentLayout;
  301. hasLayout = layout && me.rendered;
  302. if (hasLayout) {
  303. layout.onRemove(item);
  304. }
  305. me.dockedItems.remove(item);
  306. item.onRemoved();
  307. me.onDockedRemove(item);
  308. if (autoDestroy === true || (autoDestroy !== false && me.autoDestroy)) {
  309. item.destroy();
  310. } else if (hasLayout) {
  311. // not destroying, make any layout related removals
  312. layout.afterRemove(item);
  313. }
  314. // Set flag which means that beforeLayout will not veto the layout due to the size not changing
  315. me.componentLayout.childrenChanged = true;
  316. if (!me.destroying && !me.suspendLayout) {
  317. me.doComponentLayout();
  318. }
  319. return item;
  320. },
  321. /**
  322. * Retrieve an array of all currently docked Components.
  323. * @param {String} cqSelector A {@link Ext.ComponentQuery ComponentQuery} selector string to filter the returned items.
  324. * @return {Ext.Component[]} An array of components.
  325. */
  326. getDockedItems : function(cqSelector) {
  327. var me = this,
  328. defaultWeight = me.defaultDockWeights,
  329. dockedItems;
  330. if (me.dockedItems && me.dockedItems.items.length) {
  331. // Allow filtering of returned docked items by CQ selector.
  332. if (cqSelector) {
  333. dockedItems = Ext.ComponentQuery.query(cqSelector, me.dockedItems.items);
  334. } else {
  335. dockedItems = me.dockedItems.items.slice();
  336. }
  337. Ext.Array.sort(dockedItems, function(a, b) {
  338. // Docked items are ordered by their visual representation by default (t,l,r,b)
  339. var aw = a.weight || defaultWeight[a.dock],
  340. bw = b.weight || defaultWeight[b.dock];
  341. if (Ext.isNumber(aw) && Ext.isNumber(bw)) {
  342. return aw - bw;
  343. }
  344. return 0;
  345. });
  346. return dockedItems;
  347. }
  348. return [];
  349. },
  350. // inherit docs
  351. addUIClsToElement: function(cls, force) {
  352. var me = this,
  353. result = me.callParent(arguments),
  354. classes = [Ext.baseCSSPrefix + cls, me.baseCls + '-body-' + cls, me.baseCls + '-body-' + me.ui + '-' + cls],
  355. array, i;
  356. if (!force && me.rendered) {
  357. if (me.bodyCls) {
  358. me.body.addCls(me.bodyCls);
  359. } else {
  360. me.body.addCls(classes);
  361. }
  362. } else {
  363. if (me.bodyCls) {
  364. array = me.bodyCls.split(' ');
  365. for (i = 0; i < classes.length; i++) {
  366. if (!Ext.Array.contains(array, classes[i])) {
  367. array.push(classes[i]);
  368. }
  369. }
  370. me.bodyCls = array.join(' ');
  371. } else {
  372. me.bodyCls = classes.join(' ');
  373. }
  374. }
  375. return result;
  376. },
  377. // inherit docs
  378. removeUIClsFromElement: function(cls, force) {
  379. var me = this,
  380. result = me.callParent(arguments),
  381. classes = [Ext.baseCSSPrefix + cls, me.baseCls + '-body-' + cls, me.baseCls + '-body-' + me.ui + '-' + cls],
  382. array, i;
  383. if (!force && me.rendered) {
  384. if (me.bodyCls) {
  385. me.body.removeCls(me.bodyCls);
  386. } else {
  387. me.body.removeCls(classes);
  388. }
  389. } else {
  390. if (me.bodyCls) {
  391. array = me.bodyCls.split(' ');
  392. for (i = 0; i < classes.length; i++) {
  393. Ext.Array.remove(array, classes[i]);
  394. }
  395. me.bodyCls = array.join(' ');
  396. }
  397. }
  398. return result;
  399. },
  400. // inherit docs
  401. addUIToElement: function(force) {
  402. var me = this,
  403. cls = me.baseCls + '-body-' + me.ui,
  404. array;
  405. me.callParent(arguments);
  406. if (!force && me.rendered) {
  407. if (me.bodyCls) {
  408. me.body.addCls(me.bodyCls);
  409. } else {
  410. me.body.addCls(cls);
  411. }
  412. } else {
  413. if (me.bodyCls) {
  414. array = me.bodyCls.split(' ');
  415. if (!Ext.Array.contains(array, cls)) {
  416. array.push(cls);
  417. }
  418. me.bodyCls = array.join(' ');
  419. } else {
  420. me.bodyCls = cls;
  421. }
  422. }
  423. },
  424. // inherit docs
  425. removeUIFromElement: function() {
  426. var me = this,
  427. cls = me.baseCls + '-body-' + me.ui,
  428. array;
  429. me.callParent(arguments);
  430. if (me.rendered) {
  431. if (me.bodyCls) {
  432. me.body.removeCls(me.bodyCls);
  433. } else {
  434. me.body.removeCls(cls);
  435. }
  436. } else {
  437. if (me.bodyCls) {
  438. array = me.bodyCls.split(' ');
  439. Ext.Array.remove(array, cls);
  440. me.bodyCls = array.join(' ');
  441. } else {
  442. me.bodyCls = cls;
  443. }
  444. }
  445. },
  446. // @private
  447. getTargetEl : function() {
  448. return this.body;
  449. },
  450. getRefItems: function(deep) {
  451. var items = this.callParent(arguments),
  452. // deep fetches all docked items, and their descendants using '*' selector and then '* *'
  453. dockedItems = this.getDockedItems(deep ? '*,* *' : undefined),
  454. ln = dockedItems.length,
  455. i = 0,
  456. item;
  457. // Find the index where we go from top/left docked items to right/bottom docked items
  458. for (; i < ln; i++) {
  459. item = dockedItems[i];
  460. if (item.dock === 'right' || item.dock === 'bottom') {
  461. break;
  462. }
  463. }
  464. // Return docked items in the top/left position before our container items, and
  465. // return right/bottom positioned items after our container items.
  466. // See AbstractDock.renderItems() for more information.
  467. return Ext.Array.splice(dockedItems, 0, i).concat(items).concat(dockedItems);
  468. },
  469. beforeDestroy: function(){
  470. var docked = this.dockedItems,
  471. c;
  472. if (docked) {
  473. while ((c = docked.first())) {
  474. this.removeDocked(c, true);
  475. }
  476. }
  477. this.callParent();
  478. },
  479. setBorder: function(border) {
  480. var me = this;
  481. me.border = (border !== undefined) ? border : true;
  482. if (me.rendered) {
  483. me.doComponentLayout();
  484. }
  485. }
  486. });