Floating.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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-util-Floating'>/**
  19. </span> * A mixin to add floating capability to a Component.
  20. */
  21. Ext.define('Ext.util.Floating', {
  22. uses: ['Ext.Layer', 'Ext.window.Window'],
  23. <span id='Ext-util-Floating-cfg-focusOnToFront'> /**
  24. </span> * @cfg {Boolean} focusOnToFront
  25. * Specifies whether the floated component should be automatically {@link Ext.Component#method-focus focused} when
  26. * it is {@link #toFront brought to the front}.
  27. */
  28. focusOnToFront: true,
  29. <span id='Ext-util-Floating-cfg-shadow'> /**
  30. </span> * @cfg {String/Boolean} shadow
  31. * Specifies whether the floating component should be given a shadow. Set to true to automatically create an
  32. * {@link Ext.Shadow}, or a string indicating the shadow's display {@link Ext.Shadow#mode}. Set to false to
  33. * disable the shadow.
  34. */
  35. shadow: 'sides',
  36. <span id='Ext-util-Floating-cfg-shadowOffset'> /**
  37. </span> * @cfg {String/Boolean} shadowOffset
  38. * Number of pixels to offset the shadow.
  39. */
  40. constructor: function (dom) {
  41. var me = this;
  42. me.el = new Ext.Layer(Ext.apply({
  43. hideMode : me.hideMode,
  44. hidden : me.hidden,
  45. shadow : (typeof me.shadow != 'undefined') ? me.shadow : 'sides',
  46. shadowOffset : me.shadowOffset,
  47. constrain : false,
  48. shim : (me.shim === false) ? false : undefined
  49. }, me.floating), dom);
  50. // release config object (if it was one)
  51. me.floating = true;
  52. // Register with the configured ownerCt.
  53. // With this we acquire a floatParent for relative positioning, and a zIndexParent which is an
  54. // ancestor floater which provides zIndex management.
  55. me.registerWithOwnerCt();
  56. },
  57. registerWithOwnerCt: function() {
  58. var me = this;
  59. if (me.zIndexParent) {
  60. me.zIndexParent.unregisterFloatingItem(me);
  61. }
  62. // Acquire a zIndexParent by traversing the ownerCt axis for the nearest floating ancestor
  63. me.zIndexParent = me.up('[floating]');
  64. me.setFloatParent(me.ownerCt);
  65. delete me.ownerCt;
  66. if (me.zIndexParent) {
  67. me.zIndexParent.registerFloatingItem(me);
  68. } else {
  69. Ext.WindowManager.register(me);
  70. }
  71. },
  72. setFloatParent: function(floatParent) {
  73. var me = this;
  74. // Remove listeners from previous floatParent
  75. if (me.floatParent) {
  76. me.mun(me.floatParent, {
  77. hide: me.onFloatParentHide,
  78. show: me.onFloatParentShow,
  79. scope: me
  80. });
  81. }
  82. me.floatParent = floatParent;
  83. // Floating Components as children of Containers must hide when their parent hides.
  84. if (floatParent) {
  85. me.mon(me.floatParent, {
  86. hide: me.onFloatParentHide,
  87. show: me.onFloatParentShow,
  88. scope: me
  89. });
  90. }
  91. // If a floating Component is configured to be constrained, but has no configured
  92. // constrainTo setting, set its constrainTo to be it's ownerCt before rendering.
  93. if ((me.constrain || me.constrainHeader) &amp;&amp; !me.constrainTo) {
  94. me.constrainTo = floatParent ? floatParent.getTargetEl() : me.container;
  95. }
  96. },
  97. onAfterFloatLayout: function(){
  98. this.syncShadow();
  99. },
  100. onFloatParentHide: function() {
  101. var me = this;
  102. if (me.hideOnParentHide !== false &amp;&amp; me.isVisible()) {
  103. me.hide();
  104. me.showOnParentShow = true;
  105. }
  106. },
  107. onFloatParentShow: function() {
  108. if (this.showOnParentShow) {
  109. delete this.showOnParentShow;
  110. this.show();
  111. }
  112. },
  113. // private
  114. // z-index is managed by the zIndexManager and may be overwritten at any time.
  115. // Returns the next z-index to be used.
  116. // If this is a Container, then it will have rebased any managed floating Components,
  117. // and so the next available z-index will be approximately 10000 above that.
  118. setZIndex: function(index) {
  119. var me = this;
  120. me.el.setZIndex(index);
  121. // Next item goes 10 above;
  122. index += 10;
  123. // When a Container with floating descendants has its z-index set, it rebases any floating descendants it is managing.
  124. // The returned value is a round number approximately 10000 above the last z-index used.
  125. if (me.floatingDescendants) {
  126. index = Math.floor(me.floatingDescendants.setBase(index) / 100) * 100 + 10000;
  127. }
  128. return index;
  129. },
  130. <span id='Ext-util-Floating-method-doConstrain'> /**
  131. </span> * Moves this floating Component into a constrain region.
  132. *
  133. * By default, this Component is constrained to be within the container it was added to, or the element it was
  134. * rendered to.
  135. *
  136. * An alternative constraint may be passed.
  137. * @param {String/HTMLElement/Ext.Element/Ext.util.Region} [constrainTo] The Element or {@link Ext.util.Region Region}
  138. * into which this Component is to be constrained. Defaults to the element into which this floating Component
  139. * was rendered.
  140. */
  141. doConstrain: function(constrainTo) {
  142. var me = this,
  143. // Calculate the constrain vector to coerce our position to within our
  144. // constrainTo setting. getConstrainVector will provide a default constraint
  145. // region if there is no explicit constrainTo, *and* there is no floatParent owner Component.
  146. vector = me.getConstrainVector(constrainTo),
  147. xy;
  148. if (vector) {
  149. xy = me.getPosition(!!me.floatParent);
  150. xy[0] += vector[0];
  151. xy[1] += vector[1];
  152. me.setPosition(xy);
  153. }
  154. },
  155. <span id='Ext-util-Floating-method-getConstrainVector'> /**
  156. </span> * Gets the x/y offsets to constrain this float
  157. * @private
  158. * @param {String/HTMLElement/Ext.Element/Ext.util.Region} [constrainTo] The Element or {@link Ext.util.Region Region}
  159. * into which this Component is to be constrained.
  160. * @return {Number[]} The x/y constraints
  161. */
  162. getConstrainVector: function(constrainTo){
  163. var me = this;
  164. if (me.constrain || me.constrainHeader) {
  165. constrainTo = constrainTo || (me.floatParent &amp;&amp; me.floatParent.getTargetEl()) || me.container || me.el.getScopeParent();
  166. return (me.constrainHeader ? me.header.el : me.el).getConstrainVector(constrainTo);
  167. }
  168. },
  169. <span id='Ext-util-Floating-method-alignTo'> /**
  170. </span> * Aligns this floating Component to the specified element
  171. *
  172. * @param {Ext.Component/Ext.Element/HTMLElement/String} element
  173. * The element or {@link Ext.Component} to align to. If passing a component, it must be a
  174. * component instance. If a string id is passed, it will be used as an element id.
  175. * @param {String} [position=&quot;tl-bl?&quot;] The position to align to
  176. * (see {@link Ext.Element#alignTo} for more details).
  177. * @param {Number[]} [offsets] Offset the positioning by [x, y]
  178. * @return {Ext.Component} this
  179. */
  180. alignTo: function(element, position, offsets) {
  181. // element may be a Component, so first attempt to use its el to align to.
  182. // When aligning to an Element's X,Y position, we must use setPagePosition which disregards any floatParent
  183. this.setPagePosition(this.el.getAlignToXY(element.el || element, position, offsets));
  184. return this;
  185. },
  186. <span id='Ext-util-Floating-method-toFront'> /**
  187. </span> * Brings this floating Component to the front of any other visible, floating Components managed by the same
  188. * {@link Ext.ZIndexManager ZIndexManager}
  189. *
  190. * If this Component is modal, inserts the modal mask just below this Component in the z-index stack.
  191. *
  192. * @param {Boolean} [preventFocus=false] Specify `true` to prevent the Component from being focused.
  193. * @return {Ext.Component} this
  194. */
  195. toFront: function(preventFocus) {
  196. var me = this;
  197. // Find the floating Component which provides the base for this Component's zIndexing.
  198. // That must move to front to then be able to rebase its zIndex stack and move this to the front
  199. if (me.zIndexParent &amp;&amp; me.bringParentToFront !== false) {
  200. me.zIndexParent.toFront(true);
  201. }
  202. if (!Ext.isDefined(preventFocus)) {
  203. preventFocus = !me.focusOnToFront;
  204. }
  205. if (preventFocus) {
  206. me.preventFocusOnActivate = true;
  207. }
  208. if (me.zIndexManager.bringToFront(me)) {
  209. if (!preventFocus) {
  210. // Kick off a delayed focus request.
  211. // If another floating Component is toFronted before the delay expires
  212. // this will not receive focus.
  213. me.focus(false, true);
  214. }
  215. }
  216. delete me.preventFocusOnActivate;
  217. return me;
  218. },
  219. <span id='Ext-util-Floating-method-setActive'> /**
  220. </span> * This method is called internally by {@link Ext.ZIndexManager} to signal that a floating Component has either been
  221. * moved to the top of its zIndex stack, or pushed from the top of its zIndex stack.
  222. *
  223. * If a _Window_ is superceded by another Window, deactivating it hides its shadow.
  224. *
  225. * This method also fires the {@link Ext.Component#activate activate} or
  226. * {@link Ext.Component#deactivate deactivate} event depending on which action occurred.
  227. *
  228. * @param {Boolean} [active=false] True to activate the Component, false to deactivate it.
  229. * @param {Ext.Component} [newActive] The newly active Component which is taking over topmost zIndex position.
  230. */
  231. setActive: function(active, newActive) {
  232. var me = this;
  233. if (active) {
  234. if (me.el.shadow &amp;&amp; !me.maximized) {
  235. me.el.enableShadow(true);
  236. }
  237. if (me.modal &amp;&amp; !me.preventFocusOnActivate) {
  238. me.focus(false, true);
  239. }
  240. me.fireEvent('activate', me);
  241. } else {
  242. // Only the *Windows* in a zIndex stack share a shadow. All other types of floaters
  243. // can keep their shadows all the time
  244. if (me.isWindow &amp;&amp; (newActive &amp;&amp; newActive.isWindow)) {
  245. me.el.disableShadow();
  246. }
  247. me.fireEvent('deactivate', me);
  248. }
  249. },
  250. <span id='Ext-util-Floating-method-toBack'> /**
  251. </span> * Sends this Component to the back of (lower z-index than) any other visible windows
  252. * @return {Ext.Component} this
  253. */
  254. toBack: function() {
  255. this.zIndexManager.sendToBack(this);
  256. return this;
  257. },
  258. <span id='Ext-util-Floating-method-center'> /**
  259. </span> * Center this Component in its container.
  260. * @return {Ext.Component} this
  261. */
  262. center: function() {
  263. var me = this,
  264. xy;
  265. if (me.isVisible()) {
  266. xy = me.el.getAlignToXY(me.container, 'c-c');
  267. me.setPagePosition(xy);
  268. } else {
  269. me.needsCenter = true;
  270. }
  271. return me;
  272. },
  273. onFloatShow: function() {
  274. if (this.needsCenter) {
  275. this.center();
  276. }
  277. delete this.needsCenter;
  278. },
  279. // private
  280. syncShadow : function() {
  281. if (this.floating) {
  282. this.el.sync(true);
  283. }
  284. },
  285. // private
  286. fitContainer: function() {
  287. var me = this,
  288. parent = me.floatParent,
  289. container = parent ? parent.getTargetEl() : me.container;
  290. me.setSize(container.getViewSize(false));
  291. me.setPosition.apply(me, parent ? [0, 0] : container.getXY());
  292. }
  293. });
  294. </pre>
  295. </body>
  296. </html>