Component.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /**
  2. * Base class for all Ext components.
  3. *
  4. * The Component base class has built-in support for basic hide/show and enable/disable and size control behavior.
  5. *
  6. * ## xtypes
  7. *
  8. * Every component has a specific xtype, which is its Ext-specific type name, along with methods for checking the xtype
  9. * like {@link #getXType} and {@link #isXType}. See the [Component Guide][1] for more information on xtypes and the
  10. * Component hierarchy.
  11. *
  12. * ## Finding components
  13. *
  14. * All Components are registered with the {@link Ext.ComponentManager} on construction so that they can be referenced at
  15. * any time via {@link Ext#getCmp Ext.getCmp}, passing the {@link #id}.
  16. *
  17. * Additionally the {@link Ext.ComponentQuery} provides a CSS-selectors-like way to look up components by their xtype
  18. * and many other attributes. For example the following code will find all textfield components inside component with
  19. * `id: 'myform'`:
  20. *
  21. * Ext.ComponentQuery.query('#myform textfield');
  22. *
  23. * ## Extending Ext.Component
  24. *
  25. * All subclasses of Component may participate in the automated Ext component
  26. * lifecycle of creation, rendering and destruction which is provided by the {@link Ext.container.Container Container}
  27. * class. Components may be added to a Container through the {@link Ext.container.Container#cfg-items items} config option
  28. * at the time the Container is created, or they may be added dynamically via the
  29. * {@link Ext.container.Container#method-add add} method.
  30. *
  31. * All user-developed visual widgets that are required to participate in automated lifecycle and size management should
  32. * subclass Component.
  33. *
  34. * See the Creating new UI controls chapter in [Component Guide][1] for details on how and to either extend
  35. * or augment Ext JS base classes to create custom Components.
  36. *
  37. * ## The Ext.Component class by itself
  38. *
  39. * Usually one doesn't need to instantiate the Ext.Component class. There are subclasses which implement
  40. * specialized use cases, covering most application needs. However it is possible to instantiate a base
  41. * Component, and it can be rendered to document, or handled by layouts as the child item of a Container:
  42. *
  43. * @example
  44. * Ext.create('Ext.Component', {
  45. * html: 'Hello world!',
  46. * width: 300,
  47. * height: 200,
  48. * padding: 20,
  49. * style: {
  50. * color: '#FFFFFF',
  51. * backgroundColor:'#000000'
  52. * },
  53. * renderTo: Ext.getBody()
  54. * });
  55. *
  56. * The Component above creates its encapsulating `div` upon render, and use the configured HTML as content. More complex
  57. * internal structure may be created using the {@link #renderTpl} configuration, although to display database-derived
  58. * mass data, it is recommended that an ExtJS data-backed Component such as a {@link Ext.view.View View},
  59. * {@link Ext.grid.Panel GridPanel}, or {@link Ext.tree.Panel TreePanel} be used.
  60. *
  61. * [1]: #!/guide/components
  62. */
  63. Ext.define('Ext.Component', {
  64. /* Begin Definitions */
  65. alias: ['widget.component', 'widget.box'],
  66. extend: 'Ext.AbstractComponent',
  67. requires: [
  68. 'Ext.util.DelayedTask'
  69. ],
  70. uses: [
  71. 'Ext.Layer',
  72. 'Ext.resizer.Resizer',
  73. 'Ext.util.ComponentDragger'
  74. ],
  75. mixins: {
  76. floating: 'Ext.util.Floating'
  77. },
  78. statics: {
  79. // Collapse/expand directions
  80. DIRECTION_TOP: 'top',
  81. DIRECTION_RIGHT: 'right',
  82. DIRECTION_BOTTOM: 'bottom',
  83. DIRECTION_LEFT: 'left',
  84. VERTICAL_DIRECTION_Re: /^(?:top|bottom)$/,
  85. // RegExp whih specifies characters in an xtype which must be translated to '-' when generating auto IDs.
  86. // This includes dot, comma and whitespace
  87. INVALID_ID_CHARS_Re: /[\.,\s]/g
  88. },
  89. /* End Definitions */
  90. /**
  91. * @cfg {Boolean/Object} resizable
  92. * Specify as `true` to apply a {@link Ext.resizer.Resizer Resizer} to this Component after rendering.
  93. *
  94. * May also be specified as a config object to be passed to the constructor of {@link Ext.resizer.Resizer Resizer}
  95. * to override any defaults. By default the Component passes its minimum and maximum size, and uses
  96. * `{@link Ext.resizer.Resizer#dynamic}: false`
  97. */
  98. /**
  99. * @cfg {String} resizeHandles
  100. * A valid {@link Ext.resizer.Resizer} handles config string. Only applies when resizable = true.
  101. */
  102. resizeHandles: 'all',
  103. /**
  104. * @cfg {Boolean} [autoScroll=false]
  105. * `true` to use overflow:'auto' on the components layout element and show scroll bars automatically when necessary,
  106. * `false` to clip any overflowing content.
  107. * This should not be combined with {@link #overflowX} or {@link #overflowY}.
  108. */
  109. /**
  110. * @cfg {String} overflowX
  111. * Possible values are:
  112. * * `'auto'` to enable automatic horizontal scrollbar (overflow-x: 'auto').
  113. * * `'scroll'` to always enable horizontal scrollbar (overflow-x: 'scroll').
  114. * The default is overflow-x: 'hidden'. This should not be combined with {@link #autoScroll}.
  115. */
  116. /**
  117. * @cfg {String} overflowY
  118. * Possible values are:
  119. * * `'auto'` to enable automatic vertical scrollbar (overflow-y: 'auto').
  120. * * `'scroll'` to always enable vertical scrollbar (overflow-y: 'scroll').
  121. * The default is overflow-y: 'hidden'. This should not be combined with {@link #autoScroll}.
  122. */
  123. /**
  124. * @cfg {Boolean} floating
  125. * Specify as true to float the Component outside of the document flow using CSS absolute positioning.
  126. *
  127. * Components such as {@link Ext.window.Window Window}s and {@link Ext.menu.Menu Menu}s are floating by default.
  128. *
  129. * Floating Components that are programatically {@link Ext.Component#method-render rendered} will register
  130. * themselves with the global {@link Ext.WindowManager ZIndexManager}
  131. *
  132. * ### Floating Components as child items of a Container
  133. *
  134. * A floating Component may be used as a child item of a Container. This just allows the floating Component to seek
  135. * a ZIndexManager by examining the ownerCt chain.
  136. *
  137. * When configured as floating, Components acquire, at render time, a {@link Ext.ZIndexManager ZIndexManager} which
  138. * manages a stack of related floating Components. The ZIndexManager brings a single floating Component to the top
  139. * of its stack when the Component's {@link #toFront} method is called.
  140. *
  141. * The ZIndexManager is found by traversing up the {@link #ownerCt} chain to find an ancestor which itself is
  142. * floating. This is so that descendant floating Components of floating _Containers_ (Such as a ComboBox dropdown
  143. * within a Window) can have its zIndex managed relative to any siblings, but always **above** that floating
  144. * ancestor Container.
  145. *
  146. * If no floating ancestor is found, a floating Component registers itself with the default {@link Ext.WindowManager
  147. * ZIndexManager}.
  148. *
  149. * Floating components _do not participate in the Container's layout_. Because of this, they are not rendered until
  150. * you explicitly {@link #method-show} them.
  151. *
  152. * After rendering, the ownerCt reference is deleted, and the {@link #floatParent} property is set to the found
  153. * floating ancestor Container. If no floating ancestor Container was found the {@link #floatParent} property will
  154. * not be set.
  155. */
  156. floating: false,
  157. /**
  158. * @cfg {Boolean} toFrontOnShow
  159. * True to automatically call {@link #toFront} when the {@link #method-show} method is called on an already visible,
  160. * floating component.
  161. */
  162. toFrontOnShow: true,
  163. /**
  164. * @property {Ext.ZIndexManager} zIndexManager
  165. * Only present for {@link #floating} Components after they have been rendered.
  166. *
  167. * A reference to the ZIndexManager which is managing this Component's z-index.
  168. *
  169. * The {@link Ext.ZIndexManager ZIndexManager} maintains a stack of floating Component z-indices, and also provides
  170. * a single modal mask which is insert just beneath the topmost visible modal floating Component.
  171. *
  172. * Floating Components may be {@link #toFront brought to the front} or {@link #toBack sent to the back} of the
  173. * z-index stack.
  174. *
  175. * This defaults to the global {@link Ext.WindowManager ZIndexManager} for floating Components that are
  176. * programatically {@link Ext.Component#method-render rendered}.
  177. *
  178. * For {@link #floating} Components which are added to a Container, the ZIndexManager is acquired from the first
  179. * ancestor Container found which is floating. If no floating ancestor is found, the global {@link Ext.WindowManager ZIndexManager} is
  180. * used.
  181. *
  182. * See {@link #floating} and {@link #zIndexParent}
  183. * @readonly
  184. */
  185. /**
  186. * @property {Ext.Container} floatParent
  187. * Only present for {@link #floating} Components which were inserted as child items of Containers.
  188. *
  189. * Floating Components that are programatically {@link Ext.Component#method-render rendered} will not have a `floatParent`
  190. * property.
  191. *
  192. * For {@link #floating} Components which are child items of a Container, the floatParent will be the owning Container.
  193. *
  194. * For example, the dropdown {@link Ext.view.BoundList BoundList} of a ComboBox which is in a Window will have the
  195. * Window as its `floatParent`
  196. *
  197. * See {@link #floating} and {@link #zIndexManager}
  198. * @readonly
  199. */
  200. /**
  201. * @property {Ext.Container} zIndexParent
  202. * Only present for {@link #floating} Components which were inserted as child items of Containers, and which have a floating
  203. * Container in their containment ancestry.
  204. *
  205. * For {@link #floating} Components which are child items of a Container, the zIndexParent will be a floating
  206. * ancestor Container which is responsible for the base z-index value of all its floating descendants. It provides
  207. * a {@link Ext.ZIndexManager ZIndexManager} which provides z-indexing services for all its descendant floating
  208. * Components.
  209. *
  210. * Floating Components that are programatically {@link Ext.Component#method-render rendered} will not have a `zIndexParent`
  211. * property.
  212. *
  213. * For example, the dropdown {@link Ext.view.BoundList BoundList} of a ComboBox which is in a Window will have the
  214. * Window as its `zIndexParent`, and will always show above that Window, wherever the Window is placed in the z-index stack.
  215. *
  216. * See {@link #floating} and {@link #zIndexManager}
  217. * @readonly
  218. */
  219. /**
  220. * @cfg {Boolean/Object} [draggable=false]
  221. * Specify as true to make a {@link #floating} Component draggable using the Component's encapsulating element as
  222. * the drag handle.
  223. *
  224. * This may also be specified as a config object for the {@link Ext.util.ComponentDragger ComponentDragger} which is
  225. * instantiated to perform dragging.
  226. *
  227. * For example to create a Component which may only be dragged around using a certain internal element as the drag
  228. * handle, use the delegate option:
  229. *
  230. * new Ext.Component({
  231. * constrain: true,
  232. * floating: true,
  233. * style: {
  234. * backgroundColor: '#fff',
  235. * border: '1px solid black'
  236. * },
  237. * html: '<h1 style="cursor:move">The title</h1><p>The content</p>',
  238. * draggable: {
  239. * delegate: 'h1'
  240. * }
  241. * }).show();
  242. */
  243. /**
  244. * @cfg {Boolean} [formBind=false]
  245. * When inside FormPanel, any component configured with `formBind: true` will
  246. * be enabled/disabled depending on the validity state of the form.
  247. * See {@link Ext.form.Panel} for more information and example.
  248. */
  249. /**
  250. * @cfg {Number/String} [columnWidth=undefined]
  251. * Defines the column width inside {@link Ext.layout.container.Column column layout}.
  252. *
  253. * Can be specified as a number or as a percentage.
  254. */
  255. /**
  256. * @cfg {String} [region=undefined]
  257. * Defines the region inside {@link Ext.layout.container.Border border layout}.
  258. *
  259. * Possible values:
  260. *
  261. * - center
  262. * - north
  263. * - south
  264. * - east
  265. * - west
  266. */
  267. hideMode: 'display',
  268. bubbleEvents: [],
  269. monPropRe: /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/,
  270. defaultComponentLayoutType: 'autocomponent',
  271. //renderTpl: new Ext.XTemplate(
  272. // '<div id="{id}" class="{baseCls} {cls} {cmpCls}<tpl if="typeof ui !== \'undefined\'"> {uiBase}-{ui}</tpl>"<tpl if="typeof style !== \'undefined\'"> style="{style}"</tpl>></div>', {
  273. // compiled: true,
  274. // disableFormats: true
  275. // }
  276. //),
  277. /**
  278. * Creates new Component.
  279. * @param {Ext.Element/String/Object} config The configuration options may be specified as either:
  280. *
  281. * - **an element** : it is set as the internal element and its id used as the component id
  282. * - **a string** : it is assumed to be the id of an existing element and is used as the component id
  283. * - **anything else** : it is assumed to be a standard config object and is applied to the component
  284. */
  285. constructor: function(config) {
  286. var me = this;
  287. config = config || {};
  288. if (config.initialConfig) {
  289. // Being initialized from an Ext.Action instance...
  290. if (config.isAction) {
  291. me.baseAction = config;
  292. }
  293. config = config.initialConfig;
  294. // component cloning / action set up
  295. }
  296. else if (config.tagName || config.dom || Ext.isString(config)) {
  297. // element object
  298. config = {
  299. applyTo: config,
  300. id: config.id || config
  301. };
  302. }
  303. me.callParent([config]);
  304. // If we were configured from an instance of Ext.Action, (or configured with a baseAction option),
  305. // register this Component as one of its items
  306. if (me.baseAction){
  307. me.baseAction.addComponent(me);
  308. }
  309. },
  310. /**
  311. * The initComponent template method is an important initialization step for a Component. It is intended to be
  312. * implemented by each subclass of Ext.Component to provide any needed constructor logic. The
  313. * initComponent method of the class being created is called first, with each initComponent method
  314. * up the hierarchy to Ext.Component being called thereafter. This makes it easy to implement and,
  315. * if needed, override the constructor logic of the Component at any step in the hierarchy.
  316. *
  317. * The initComponent method **must** contain a call to {@link Ext.Base#callParent callParent} in order
  318. * to ensure that the parent class' initComponent method is also called.
  319. *
  320. * All config options passed to the constructor are applied to `this` before initComponent is called,
  321. * so you can simply access them with `this.someOption`.
  322. *
  323. * The following example demonstrates using a dynamic string for the text of a button at the time of
  324. * instantiation of the class.
  325. *
  326. * Ext.define('DynamicButtonText', {
  327. * extend: 'Ext.button.Button',
  328. *
  329. * initComponent: function() {
  330. * this.text = new Date();
  331. * this.renderTo = Ext.getBody();
  332. * this.callParent();
  333. * }
  334. * });
  335. *
  336. * Ext.onReady(function() {
  337. * Ext.create('DynamicButtonText');
  338. * });
  339. *
  340. * @template
  341. * @protected
  342. */
  343. initComponent: function() {
  344. var me = this;
  345. me.callParent();
  346. if (me.listeners) {
  347. me.on(me.listeners);
  348. me.listeners = null; //change the value to remove any on prototype
  349. }
  350. me.enableBubble(me.bubbleEvents);
  351. me.mons = [];
  352. },
  353. // private
  354. afterRender: function() {
  355. var me = this;
  356. me.callParent();
  357. if (!(me.x && me.y) && (me.pageX || me.pageY)) {
  358. me.setPagePosition(me.pageX, me.pageY);
  359. }
  360. },
  361. /**
  362. * Sets the overflow on the content element of the component.
  363. * @param {Boolean} scroll True to allow the Component to auto scroll.
  364. * @return {Ext.Component} this
  365. */
  366. setAutoScroll : function(scroll) {
  367. var me = this;
  368. me.autoScroll = !!scroll;
  369. // Scrolling styles must be applied to Component's main element.
  370. // Layouts which use an innerCt (Box layout), shrinkwrap the innerCt round overflowing content,
  371. // so the innerCt must be scrolled by the container, it does not scroll content.
  372. if (me.rendered) {
  373. me.getTargetEl().setStyle(me.getOverflowStyle());
  374. }
  375. me.updateLayout();
  376. return me;
  377. },
  378. /**
  379. * Sets the overflow x/y on the content element of the component. The x/y overflow
  380. * values can be any valid CSS overflow (e.g., 'auto' or 'scroll'). By default, the
  381. * value is 'hidden'. Passing null for one of the values will erase the inline style.
  382. * Passing `undefined` will preserve the current value.
  383. *
  384. * @param {String} overflowX The overflow-x value.
  385. * @param {String} overflowY The overflow-y value.
  386. * @return {Ext.Component} this
  387. */
  388. setOverflowXY: function(overflowX, overflowY) {
  389. var me = this,
  390. argCount = arguments.length;
  391. if (argCount) {
  392. me.overflowX = overflowX || '';
  393. if (argCount > 1) {
  394. me.overflowY = overflowY || '';
  395. }
  396. }
  397. // Scrolling styles must be applied to Component's main element.
  398. // Layouts which use an innerCt (Box layout), shrinkwrap the innerCt round overflowing content,
  399. // so the innerCt must be scrolled by the container, it does not scroll content.
  400. if (me.rendered) {
  401. me.getTargetEl().setStyle(me.getOverflowStyle());
  402. }
  403. me.updateLayout();
  404. return me;
  405. },
  406. beforeRender: function () {
  407. var me = this,
  408. floating = me.floating,
  409. cls;
  410. if (floating) {
  411. me.addCls(Ext.baseCSSPrefix + 'layer');
  412. cls = floating.cls;
  413. if (cls) {
  414. me.addCls(cls);
  415. }
  416. }
  417. return me.callParent();
  418. },
  419. afterComponentLayout: function(){
  420. this.callParent(arguments);
  421. if (this.floating) {
  422. this.onAfterFloatLayout();
  423. }
  424. },
  425. // private
  426. makeFloating : function (dom) {
  427. this.mixins.floating.constructor.call(this, dom);
  428. },
  429. wrapPrimaryEl: function (dom) {
  430. if (this.floating) {
  431. this.makeFloating(dom);
  432. } else {
  433. this.callParent(arguments);
  434. }
  435. },
  436. initResizable: function(resizable) {
  437. var me = this;
  438. resizable = Ext.apply({
  439. target: me,
  440. dynamic: false,
  441. constrainTo: me.constrainTo || (me.floatParent ? me.floatParent.getTargetEl() : null),
  442. handles: me.resizeHandles
  443. }, resizable);
  444. resizable.target = me;
  445. me.resizer = new Ext.resizer.Resizer(resizable);
  446. },
  447. getDragEl: function() {
  448. return this.el;
  449. },
  450. initDraggable: function() {
  451. var me = this,
  452. // If we are resizable, and the resizer had to wrap this Component's el (eg an Img)
  453. // Then we have to create a pseudo-Component out of the resizer to drag that,
  454. // otherwise, we just drag this Component
  455. dragTarget = (me.resizer && me.resizer.el !== me.el) ? me.resizerComponent = new Ext.Component({
  456. el: me.resizer.el,
  457. rendered: true,
  458. container: me.container
  459. }) : me,
  460. ddConfig = Ext.applyIf({
  461. el: dragTarget.getDragEl(),
  462. constrainTo: me.constrain ? (me.constrainTo || (me.floatParent ? me.floatParent.getTargetEl() : me.el.getScopeParent())) : undefined
  463. }, me.draggable);
  464. // Add extra configs if Component is specified to be constrained
  465. if (me.constrain || me.constrainDelegate) {
  466. ddConfig.constrain = me.constrain;
  467. ddConfig.constrainDelegate = me.constrainDelegate;
  468. }
  469. me.dd = new Ext.util.ComponentDragger(dragTarget, ddConfig);
  470. },
  471. /**
  472. * Scrolls this Component's {@link #getTargetEl target element} by the passed delta values, optionally animating.
  473. *
  474. * All of the following are equivalent:
  475. *
  476. * comp.scrollBy(10, 10, true);
  477. * comp.scrollBy([10, 10], true);
  478. * comp.scrollBy({ x: 10, y: 10 }, true);
  479. *
  480. * @param {Number/Number[]/Object} deltaX Either the x delta, an Array specifying x and y deltas or
  481. * an object with "x" and "y" properties.
  482. * @param {Number/Boolean/Object} deltaY Either the y delta, or an animate flag or config object.
  483. * @param {Boolean/Object} animate Animate flag/config object if the delta values were passed separately.
  484. */
  485. scrollBy: function(deltaX, deltaY, animate) {
  486. var el;
  487. if ((el = this.getTargetEl()) && el.dom) {
  488. el.scrollBy.apply(el, arguments);
  489. }
  490. },
  491. /**
  492. * This method allows you to show or hide a LoadMask on top of this component.
  493. *
  494. * @param {Boolean/Object/String} load True to show the default LoadMask, a config object that will be passed to the
  495. * LoadMask constructor, or a message String to show. False to hide the current LoadMask.
  496. * @param {Boolean} [targetEl=false] True to mask the targetEl of this Component instead of the `this.el`. For example,
  497. * setting this to true on a Panel will cause only the body to be masked.
  498. * @return {Ext.LoadMask} The LoadMask instance that has just been shown.
  499. */
  500. setLoading : function(load, targetEl) {
  501. var me = this,
  502. config;
  503. if (me.rendered) {
  504. Ext.destroy(me.loadMask);
  505. me.loadMask = null;
  506. if (load !== false && !me.collapsed) {
  507. if (Ext.isObject(load)) {
  508. config = Ext.apply({}, load);
  509. } else if (Ext.isString(load)) {
  510. config = {msg: load};
  511. } else {
  512. config = {};
  513. }
  514. if (targetEl) {
  515. Ext.applyIf(config, {
  516. useTargetEl: true
  517. });
  518. }
  519. me.loadMask = new Ext.LoadMask(me, config);
  520. me.loadMask.show();
  521. }
  522. }
  523. return me.loadMask;
  524. },
  525. beforeSetPosition: function () {
  526. var me = this,
  527. pos = me.callParent(arguments), // pass all args on for signature decoding
  528. adj;
  529. if (pos) {
  530. adj = me.adjustPosition(pos.x, pos.y);
  531. pos.x = adj.x;
  532. pos.y = adj.y;
  533. }
  534. return pos || null;
  535. },
  536. afterSetPosition: function(ax, ay) {
  537. this.onPosition(ax, ay);
  538. this.fireEvent('move', this, ax, ay);
  539. },
  540. /**
  541. * Displays component at specific xy position.
  542. * A floating component (like a menu) is positioned relative to its ownerCt if any.
  543. * Useful for popping up a context menu:
  544. *
  545. * listeners: {
  546. * itemcontextmenu: function(view, record, item, index, event, options) {
  547. * Ext.create('Ext.menu.Menu', {
  548. * width: 100,
  549. * height: 100,
  550. * margin: '0 0 10 0',
  551. * items: [{
  552. * text: 'regular item 1'
  553. * },{
  554. * text: 'regular item 2'
  555. * },{
  556. * text: 'regular item 3'
  557. * }]
  558. * }).showAt(event.getXY());
  559. * }
  560. * }
  561. *
  562. * @param {Number} x The new x position
  563. * @param {Number} y The new y position
  564. * @param {Boolean/Object} [animate] True to animate the Component into its new position. You may also pass an
  565. * animation configuration.
  566. */
  567. showAt: function(x, y, animate) {
  568. var me = this;
  569. if (!me.rendered && (me.autoRender || me.floating)) {
  570. me.doAutoRender();
  571. // forcibly set hidden here, since we still want the initial beforeshow/show event to fire
  572. me.hidden = true;
  573. }
  574. if (me.floating) {
  575. me.setPosition(x, y, animate);
  576. } else {
  577. me.setPagePosition(x, y, animate);
  578. }
  579. me.show();
  580. },
  581. /**
  582. * Sets the page XY position of the component. To set the left and top instead, use {@link #setPosition}.
  583. * This method fires the {@link #event-move} event.
  584. * @param {Number} x The new x position
  585. * @param {Number} y The new y position
  586. * @param {Boolean/Object} [animate] True to animate the Component into its new position. You may also pass an
  587. * animation configuration.
  588. * @return {Ext.Component} this
  589. */
  590. setPagePosition: function(x, y, animate) {
  591. var me = this,
  592. p,
  593. floatParentBox;
  594. if (Ext.isArray(x)) {
  595. y = x[1];
  596. x = x[0];
  597. }
  598. me.pageX = x;
  599. me.pageY = y;
  600. if (me.floating) {
  601. // Floating Components which are registered with a Container have to have their x and y properties made relative
  602. if (me.isContainedFloater()) {
  603. floatParentBox = me.floatParent.getTargetEl().getViewRegion();
  604. if (Ext.isNumber(x) && Ext.isNumber(floatParentBox.left)) {
  605. x -= floatParentBox.left;
  606. }
  607. if (Ext.isNumber(y) && Ext.isNumber(floatParentBox.top)) {
  608. y -= floatParentBox.top;
  609. }
  610. } else {
  611. p = me.el.translatePoints(x, y);
  612. x = p.left;
  613. y = p.top;
  614. }
  615. me.setPosition(x, y, animate);
  616. } else {
  617. p = me.el.translatePoints(x, y);
  618. me.setPosition(p.left, p.top, animate);
  619. }
  620. return me;
  621. },
  622. // Utility method to determine if a Component is floating, and has an owning Container whose coordinate system
  623. // it must be positioned in when using setPosition.
  624. isContainedFloater: function() {
  625. return (this.floating && this.floatParent);
  626. },
  627. /**
  628. * Gets the current box measurements of the component's underlying element.
  629. * @param {Boolean} [local=false] If true the element's left and top are returned instead of page XY.
  630. * @return {Object} box An object in the format {x, y, width, height}
  631. */
  632. getBox : function(local){
  633. var pos = local ? this.getPosition(local) : this.el.getXY(),
  634. size = this.getSize();
  635. size.x = pos[0];
  636. size.y = pos[1];
  637. return size;
  638. },
  639. /**
  640. * Sets the current box measurements of the component's underlying element.
  641. * @param {Object} box An object in the format {x, y, width, height}
  642. * @return {Ext.Component} this
  643. */
  644. updateBox : function(box){
  645. this.setSize(box.width, box.height);
  646. this.setPagePosition(box.x, box.y);
  647. return this;
  648. },
  649. // Include margins
  650. getOuterSize: function() {
  651. var el = this.el;
  652. return {
  653. width: el.getWidth() + el.getMargin('lr'),
  654. height: el.getHeight() + el.getMargin('tb')
  655. };
  656. },
  657. // private
  658. adjustPosition: function(x, y) {
  659. var me = this,
  660. floatParentBox;
  661. // Floating Components being positioned in their ownerCt have to be made absolute.
  662. if (me.isContainedFloater()) {
  663. floatParentBox = me.floatParent.getTargetEl().getViewRegion();
  664. x += floatParentBox.left;
  665. y += floatParentBox.top;
  666. }
  667. return {
  668. x: x,
  669. y: y
  670. };
  671. },
  672. /**
  673. * Gets the current XY position of the component's underlying element.
  674. * @param {Boolean} [local=false] If true the element's left and top are returned instead of page XY.
  675. * @return {Number[]} The XY position of the element (e.g., [100, 200])
  676. */
  677. getPosition: function(local) {
  678. var me = this,
  679. el = me.el,
  680. xy,
  681. isContainedFloater = me.isContainedFloater(),
  682. floatParentBox;
  683. // Local position for non-floaters means element's local position
  684. if ((local === true) && !isContainedFloater) {
  685. return [el.getLocalX(), el.getLocalY()];
  686. }
  687. xy = me.el.getXY();
  688. // Local position for floaters means position relative to the container's target element
  689. if ((local === true) && isContainedFloater) {
  690. floatParentBox = me.floatParent.getTargetEl().getViewRegion();
  691. xy[0] -= floatParentBox.left;
  692. xy[1] -= floatParentBox.top;
  693. }
  694. return xy;
  695. },
  696. getId: function() {
  697. var me = this,
  698. xtype;
  699. if (!me.id) {
  700. xtype = me.getXType();
  701. if (xtype) {
  702. xtype = xtype.replace(Ext.Component.INVALID_ID_CHARS_Re, '-');
  703. } else {
  704. xtype = Ext.name.toLowerCase() + '-comp';
  705. }
  706. me.id = xtype + '-' + me.getAutoId();
  707. }
  708. return me.id;
  709. },
  710. /**
  711. * Shows this Component, rendering it first if {@link #autoRender} or {@link #floating} are `true`.
  712. *
  713. * After being shown, a {@link #floating} Component (such as a {@link Ext.window.Window}), is activated it and
  714. * brought to the front of its {@link #zIndexManager z-index stack}.
  715. *
  716. * @param {String/Ext.Element} [animateTarget=null] **only valid for {@link #floating} Components such as {@link
  717. * Ext.window.Window Window}s or {@link Ext.tip.ToolTip ToolTip}s, or regular Components which have been configured
  718. * with `floating: true`.** The target from which the Component should animate from while opening.
  719. * @param {Function} [callback] A callback function to call after the Component is displayed.
  720. * Only necessary if animation was specified.
  721. * @param {Object} [scope] The scope (`this` reference) in which the callback is executed.
  722. * Defaults to this Component.
  723. * @return {Ext.Component} this
  724. */
  725. show: function(animateTarget, cb, scope) {
  726. var me = this,
  727. rendered = me.rendered;
  728. if (rendered && me.isVisible()) {
  729. if (me.toFrontOnShow && me.floating) {
  730. me.toFront();
  731. }
  732. } else {
  733. if (me.fireEvent('beforeshow', me) !== false) {
  734. // Render on first show if there is an autoRender config, or if this is a floater (Window, Menu, BoundList etc).
  735. me.hidden = false;
  736. if (!rendered && (me.autoRender || me.floating)) {
  737. me.doAutoRender();
  738. rendered = me.rendered;
  739. }
  740. if (rendered) {
  741. me.beforeShow();
  742. me.onShow.apply(me, arguments);
  743. me.afterShow.apply(me, arguments);
  744. }
  745. } else {
  746. me.onShowVeto();
  747. }
  748. }
  749. return me;
  750. },
  751. onShowVeto: Ext.emptyFn,
  752. /**
  753. * Invoked before the Component is shown.
  754. *
  755. * @method
  756. * @template
  757. * @protected
  758. */
  759. beforeShow: Ext.emptyFn,
  760. /**
  761. * Allows addition of behavior to the show operation. After
  762. * calling the superclass's onShow, the Component will be visible.
  763. *
  764. * Override in subclasses where more complex behaviour is needed.
  765. *
  766. * Gets passed the same parameters as #show.
  767. *
  768. * @param {String/Ext.Element} [animateTarget]
  769. * @param {Function} [callback]
  770. * @param {Object} [scope]
  771. *
  772. * @template
  773. * @protected
  774. */
  775. onShow: function() {
  776. var me = this;
  777. me.el.show();
  778. me.callParent(arguments);
  779. // Constraining/containing element may have changed size while this Component was hidden
  780. if (me.floating) {
  781. if (me.maximized) {
  782. me.fitContainer();
  783. }
  784. else if (me.constrain) {
  785. me.doConstrain();
  786. }
  787. }
  788. },
  789. /**
  790. * Invoked after the Component is shown (after #onShow is called).
  791. *
  792. * Gets passed the same parameters as #show.
  793. *
  794. * @param {String/Ext.Element} [animateTarget]
  795. * @param {Function} [callback]
  796. * @param {Object} [scope]
  797. *
  798. * @template
  799. * @protected
  800. */
  801. afterShow: function(animateTarget, cb, scope) {
  802. var me = this,
  803. fromBox,
  804. toBox,
  805. ghostPanel;
  806. // Default to configured animate target if none passed
  807. animateTarget = animateTarget || me.animateTarget;
  808. // Need to be able to ghost the Component
  809. if (!me.ghost) {
  810. animateTarget = null;
  811. }
  812. // If we're animating, kick of an animation of the ghost from the target to the *Element* current box
  813. if (animateTarget) {
  814. animateTarget = animateTarget.el ? animateTarget.el : Ext.get(animateTarget);
  815. toBox = me.el.getBox();
  816. fromBox = animateTarget.getBox();
  817. me.el.addCls(Ext.baseCSSPrefix + 'hide-offsets');
  818. ghostPanel = me.ghost();
  819. ghostPanel.el.stopAnimation();
  820. // Shunting it offscreen immediately, *before* the Animation class grabs it ensure no flicker.
  821. ghostPanel.el.setX(-10000);
  822. ghostPanel.el.animate({
  823. from: fromBox,
  824. to: toBox,
  825. listeners: {
  826. afteranimate: function() {
  827. delete ghostPanel.componentLayout.lastComponentSize;
  828. me.unghost();
  829. me.el.removeCls(Ext.baseCSSPrefix + 'hide-offsets');
  830. me.onShowComplete(cb, scope);
  831. }
  832. }
  833. });
  834. }
  835. else {
  836. me.onShowComplete(cb, scope);
  837. }
  838. },
  839. /**
  840. * Invoked after the #afterShow method is complete.
  841. *
  842. * Gets passed the same `callback` and `scope` parameters that #afterShow received.
  843. *
  844. * @param {Function} [callback]
  845. * @param {Object} [scope]
  846. *
  847. * @template
  848. * @protected
  849. */
  850. onShowComplete: function(cb, scope) {
  851. var me = this;
  852. if (me.floating) {
  853. me.toFront();
  854. me.onFloatShow();
  855. }
  856. Ext.callback(cb, scope || me);
  857. me.fireEvent('show', me);
  858. delete me.hiddenByLayout;
  859. },
  860. /**
  861. * Hides this Component, setting it to invisible using the configured {@link #hideMode}.
  862. * @param {String/Ext.Element/Ext.Component} [animateTarget=null] **only valid for {@link #floating} Components
  863. * such as {@link Ext.window.Window Window}s or {@link Ext.tip.ToolTip ToolTip}s, or regular Components which have
  864. * been configured with `floating: true`.**. The target to which the Component should animate while hiding.
  865. * @param {Function} [callback] A callback function to call after the Component is hidden.
  866. * @param {Object} [scope] The scope (`this` reference) in which the callback is executed.
  867. * Defaults to this Component.
  868. * @return {Ext.Component} this
  869. */
  870. hide: function() {
  871. var me = this;
  872. // Clear the flag which is set if a floatParent was hidden while this is visible.
  873. // If a hide operation was subsequently called, that pending show must be hidden.
  874. me.showOnParentShow = false;
  875. if (!(me.rendered && !me.isVisible()) && me.fireEvent('beforehide', me) !== false) {
  876. me.hidden = true;
  877. if (me.rendered) {
  878. me.onHide.apply(me, arguments);
  879. }
  880. }
  881. return me;
  882. },
  883. /**
  884. * Possibly animates down to a target element.
  885. *
  886. * Allows addition of behavior to the hide operation. After
  887. * calling the superclass’s onHide, the Component will be hidden.
  888. *
  889. * Gets passed the same parameters as #hide.
  890. *
  891. * @param {String/Ext.Element/Ext.Component} [animateTarget]
  892. * @param {Function} [callback]
  893. * @param {Object} [scope]
  894. *
  895. * @template
  896. * @protected
  897. */
  898. onHide: function(animateTarget, cb, scope) {
  899. var me = this,
  900. ghostPanel,
  901. toBox;
  902. // Default to configured animate target if none passed
  903. animateTarget = animateTarget || me.animateTarget;
  904. // Need to be able to ghost the Component
  905. if (!me.ghost) {
  906. animateTarget = null;
  907. }
  908. // If we're animating, kick off an animation of the ghost down to the target
  909. if (animateTarget) {
  910. animateTarget = animateTarget.el ? animateTarget.el : Ext.get(animateTarget);
  911. ghostPanel = me.ghost();
  912. ghostPanel.el.stopAnimation();
  913. toBox = animateTarget.getBox();
  914. toBox.width += 'px';
  915. toBox.height += 'px';
  916. ghostPanel.el.animate({
  917. to: toBox,
  918. listeners: {
  919. afteranimate: function() {
  920. delete ghostPanel.componentLayout.lastComponentSize;
  921. ghostPanel.el.hide();
  922. me.afterHide(cb, scope);
  923. }
  924. }
  925. });
  926. }
  927. me.el.hide();
  928. if (!animateTarget) {
  929. me.afterHide(cb, scope);
  930. }
  931. },
  932. /**
  933. * Invoked after the Component has been hidden.
  934. *
  935. * Gets passed the same `callback` and `scope` parameters that #onHide received.
  936. *
  937. * @param {Function} [callback]
  938. * @param {Object} [scope]
  939. *
  940. * @template
  941. * @protected
  942. */
  943. afterHide: function(cb, scope) {
  944. var me = this;
  945. delete me.hiddenByLayout;
  946. // we are the back-end method of onHide at this level, but our call to our parent
  947. // may need to be async... so callParent won't quite work here...
  948. Ext.AbstractComponent.prototype.onHide.call(this);
  949. Ext.callback(cb, scope || me);
  950. me.fireEvent('hide', me);
  951. },
  952. /**
  953. * Allows addition of behavior to the destroy operation.
  954. * After calling the superclass’s onDestroy, the Component will be destroyed.
  955. *
  956. * @template
  957. * @protected
  958. */
  959. onDestroy: function() {
  960. var me = this;
  961. // Ensure that any ancillary components are destroyed.
  962. if (me.rendered) {
  963. Ext.destroy(
  964. me.proxy,
  965. me.proxyWrap,
  966. me.resizer,
  967. me.resizerComponent
  968. );
  969. }
  970. delete me.focusTask;
  971. me.callParent();
  972. },
  973. deleteMembers: function() {
  974. var args = arguments,
  975. len = args.length,
  976. i = 0;
  977. for (; i < len; ++i) {
  978. delete this[args[i]];
  979. }
  980. },
  981. /**
  982. * Try to focus this component.
  983. * @param {Boolean} [selectText] If applicable, true to also select the text in this component
  984. * @param {Boolean/Number} [delay] Delay the focus this number of milliseconds (true for 10 milliseconds).
  985. * @return {Ext.Component} The focused Component. Usually <code>this</code> Component. Some Containers may
  986. * delegate focus to a descendant Component ({@link Ext.window.Window Window}s can do this through their
  987. * {@link Ext.window.Window#defaultFocus defaultFocus} config option.
  988. */
  989. focus: function(selectText, delay) {
  990. var me = this,
  991. focusEl,
  992. focusElDom,
  993. containerScrollTop;
  994. // If delay is wanted, queue a call to this function.
  995. if (delay) {
  996. if (!me.focusTask) {
  997. me.focusTask = new Ext.util.DelayedTask(me.focus);
  998. }
  999. me.focusTask.delay(Ext.isNumber(delay) ? delay : 10, null, me, [selectText, false]);
  1000. return me;
  1001. }
  1002. if (me.rendered && !me.isDestroyed && me.isVisible(true) && (focusEl = me.getFocusEl())) {
  1003. // getFocusEl might return a Component if a Container wishes to delegate focus to a descendant.
  1004. // Window can do this via its defaultFocus configuration which can reference a Button.
  1005. if (focusEl.isComponent) {
  1006. return focusEl.focus(selectText, delay);
  1007. }
  1008. // If it was an Element with a dom property
  1009. if ((focusElDom = focusEl.dom)) {
  1010. // Not a natural focus holding element, add a tab index to make it programatically focusable.
  1011. if (focusEl.needsTabIndex()) {
  1012. focusElDom.tabIndex = -1;
  1013. }
  1014. if (me.floating) {
  1015. containerScrollTop = me.container.dom.scrollTop;
  1016. }
  1017. // Focus the element.
  1018. // The focusEl has a DOM focus listener on it which invokes the Component's onFocus method
  1019. // to perform Component-specific focus processing
  1020. focusEl.focus();
  1021. if (selectText === true) {
  1022. focusElDom.select();
  1023. }
  1024. }
  1025. // Focusing a floating Component brings it to the front of its stack.
  1026. // this is performed by its zIndexManager. Pass preventFocus true to avoid recursion.
  1027. if (me.floating) {
  1028. me.toFront(true);
  1029. if (containerScrollTop !== undefined) {
  1030. me.container.dom.scrollTop = containerScrollTop;
  1031. }
  1032. }
  1033. }
  1034. return me;
  1035. },
  1036. /**
  1037. * Cancel any deferred focus on this component
  1038. * @protected
  1039. */
  1040. cancelFocus: function() {
  1041. var task = this.focusTask;
  1042. if (task) {
  1043. task.cancel();
  1044. }
  1045. },
  1046. // private
  1047. blur: function() {
  1048. var focusEl;
  1049. if (this.rendered && (focusEl = this.getFocusEl())) {
  1050. focusEl.blur();
  1051. }
  1052. return this;
  1053. },
  1054. getEl: function() {
  1055. return this.el;
  1056. },
  1057. // Deprecate 5.0
  1058. getResizeEl: function() {
  1059. return this.el;
  1060. },
  1061. // Deprecate 5.0
  1062. getPositionEl: function() {
  1063. return this.el;
  1064. },
  1065. // Deprecate 5.0
  1066. getActionEl: function() {
  1067. return this.el;
  1068. },
  1069. // Deprecate 5.0
  1070. getVisibilityEl: function() {
  1071. return this.el;
  1072. },
  1073. // Deprecate 5.0
  1074. onResize: Ext.emptyFn,
  1075. // private
  1076. // Implements an upward event bubbilng policy. By default a Component bubbles events up to its ownerCt
  1077. // Floating Components target the floatParent.
  1078. // Some Component subclasses (such as Menu) might implement a different ownership hierarchy.
  1079. // The up() method uses this to find the immediate owner.
  1080. getBubbleTarget: function() {
  1081. return this.ownerCt || this.floatParent;
  1082. },
  1083. // private
  1084. getContentTarget: function() {
  1085. return this.el;
  1086. },
  1087. /**
  1088. * Clone the current component using the original config values passed into this instance by default.
  1089. * @param {Object} overrides A new config containing any properties to override in the cloned version.
  1090. * An id property can be passed on this object, otherwise one will be generated to avoid duplicates.
  1091. * @return {Ext.Component} clone The cloned copy of this component
  1092. */
  1093. cloneConfig: function(overrides) {
  1094. overrides = overrides || {};
  1095. var id = overrides.id || Ext.id(),
  1096. cfg = Ext.applyIf(overrides, this.initialConfig),
  1097. self;
  1098. cfg.id = id;
  1099. self = Ext.getClass(this);
  1100. // prevent dup id
  1101. return new self(cfg);
  1102. },
  1103. /**
  1104. * Gets the xtype for this component as registered with {@link Ext.ComponentManager}. For a list of all available
  1105. * xtypes, see the {@link Ext.Component} header. Example usage:
  1106. *
  1107. * var t = new Ext.form.field.Text();
  1108. * alert(t.getXType()); // alerts 'textfield'
  1109. *
  1110. * @return {String} The xtype
  1111. */
  1112. getXType: function() {
  1113. return this.self.xtype;
  1114. },
  1115. /**
  1116. * Find a container above this component at any level by a custom function. If the passed function returns true, the
  1117. * container will be returned.
  1118. *
  1119. * See also the {@link Ext.Component#up up} method.
  1120. *
  1121. * @param {Function} fn The custom function to call with the arguments (container, this component).
  1122. * @return {Ext.container.Container} The first Container for which the custom function returns true
  1123. */
  1124. findParentBy: function(fn) {
  1125. var p;
  1126. // Iterate up the ownerCt chain until there's no ownerCt, or we find an ancestor which matches using the selector function.
  1127. for (p = this.getBubbleTarget(); p && !fn(p, this); p = p.getBubbleTarget()) {
  1128. // do nothing
  1129. }
  1130. return p || null;
  1131. },
  1132. /**
  1133. * Find a container above this component at any level by xtype or class
  1134. *
  1135. * See also the {@link Ext.Component#up up} method.
  1136. *
  1137. * @param {String/Ext.Class} xtype The xtype string for a component, or the class of the component directly
  1138. * @return {Ext.container.Container} The first Container which matches the given xtype or class
  1139. */
  1140. findParentByType: function(xtype) {
  1141. return Ext.isFunction(xtype) ?
  1142. this.findParentBy(function(p) {
  1143. return p.constructor === xtype;
  1144. })
  1145. :
  1146. this.up(xtype);
  1147. },
  1148. /**
  1149. * Bubbles up the component/container heirarchy, calling the specified function with each component. The scope
  1150. * (*this*) of function call will be the scope provided or the current component. The arguments to the function will
  1151. * be the args provided or the current component. If the function returns false at any point, the bubble is stopped.
  1152. *
  1153. * @param {Function} fn The function to call
  1154. * @param {Object} [scope] The scope of the function. Defaults to current node.
  1155. * @param {Array} [args] The args to call the function with. Defaults to passing the current component.
  1156. * @return {Ext.Component} this
  1157. */
  1158. bubble: function(fn, scope, args) {
  1159. var p = this;
  1160. while (p) {
  1161. if (fn.apply(scope || p, args || [p]) === false) {
  1162. break;
  1163. }
  1164. p = p.getBubbleTarget();
  1165. }
  1166. return this;
  1167. },
  1168. getProxy: function() {
  1169. var me = this,
  1170. target;
  1171. if (!me.proxy) {
  1172. target = Ext.getBody();
  1173. if (Ext.scopeResetCSS) {
  1174. me.proxyWrap = target = Ext.getBody().createChild({
  1175. cls: Ext.resetCls
  1176. });
  1177. }
  1178. me.proxy = me.el.createProxy(Ext.baseCSSPrefix + 'proxy-el', target, true);
  1179. }
  1180. return me.proxy;
  1181. }
  1182. });