Spotlight.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.ux.Spotlight
  11. * UX used to provide a spotlight around a specified component/element.
  12. */
  13. Ext.define('Ext.ux.Spotlight', {
  14. extend: 'Object',
  15. /**
  16. * @private
  17. * The baseCls for the spotlight elements
  18. */
  19. baseCls: 'x-spotlight',
  20. /**
  21. * @cfg animate {Boolean} True to animate the spotlight change
  22. * (defaults to true)
  23. */
  24. animate: true,
  25. /**
  26. * @cfg duration {Integer} The duration of the animation, in milliseconds
  27. * (defaults to 250)
  28. */
  29. duration: 250,
  30. /**
  31. * @cfg easing {String} The type of easing for the spotlight animatation
  32. * (defaults to null)
  33. */
  34. easing: null,
  35. /**
  36. * @private
  37. * True if the spotlight is active on the element
  38. */
  39. active: false,
  40. /**
  41. * Create all the elements for the spotlight
  42. */
  43. createElements: function() {
  44. var body = Ext.getBody();
  45. this.right = body.createChild({
  46. cls: this.baseCls
  47. });
  48. this.left = body.createChild({
  49. cls: this.baseCls
  50. });
  51. this.top = body.createChild({
  52. cls: this.baseCls
  53. });
  54. this.bottom = body.createChild({
  55. cls: this.baseCls
  56. });
  57. this.all = Ext.create('Ext.CompositeElement', [this.right, this.left, this.top, this.bottom]);
  58. },
  59. /**
  60. * Show the spotlight
  61. */
  62. show: function(el, callback, scope) {
  63. //get the target element
  64. this.el = Ext.get(el);
  65. //create the elements if they don't already exist
  66. if (!this.right) {
  67. this.createElements();
  68. }
  69. if (!this.active) {
  70. //if the spotlight is not active, show it
  71. this.all.setDisplayed('');
  72. this.active = true;
  73. Ext.EventManager.onWindowResize(this.syncSize, this);
  74. this.applyBounds(this.animate, false);
  75. } else {
  76. //if the spotlight is currently active, just move it
  77. this.applyBounds(false, false);
  78. }
  79. },
  80. /**
  81. * Hide the spotlight
  82. */
  83. hide: function(callback, scope) {
  84. Ext.EventManager.removeResizeListener(this.syncSize, this);
  85. this.applyBounds(this.animate, true);
  86. },
  87. /**
  88. * Resizes the spotlight with the window size.
  89. */
  90. syncSize: function() {
  91. this.applyBounds(false, false);
  92. },
  93. /**
  94. * Resizes the spotlight depending on the arguments
  95. * @param {Boolean} animate True to animate the changing of the bounds
  96. * @param {Boolean} animate True to reverse the animation
  97. */
  98. applyBounds: function(animate, reverse) {
  99. var me = this,
  100. box = me.el.getBox();
  101. //get the current view width and height
  102. var viewWidth = Ext.Element.getViewWidth(true);
  103. var viewHeight = Ext.Element.getViewHeight(true);
  104. var i = 0,
  105. config = false,
  106. from, to;
  107. //where the element should start (if animation)
  108. from = {
  109. right: {
  110. x: box.right,
  111. y: viewHeight,
  112. width: (viewWidth - box.right),
  113. height: 0
  114. },
  115. left: {
  116. x: 0,
  117. y: 0,
  118. width: box.x,
  119. height: 0
  120. },
  121. top: {
  122. x: viewWidth,
  123. y: 0,
  124. width: 0,
  125. height: box.y
  126. },
  127. bottom: {
  128. x: 0,
  129. y: (box.y + box.height),
  130. width: 0,
  131. height: (viewHeight - (box.y + box.height)) + 'px'
  132. }
  133. };
  134. //where the element needs to finish
  135. to = {
  136. right: {
  137. x: box.right,
  138. y: box.y,
  139. width: (viewWidth - box.right) + 'px',
  140. height: (viewHeight - box.y) + 'px'
  141. },
  142. left: {
  143. x: 0,
  144. y: 0,
  145. width: box.x + 'px',
  146. height: (box.y + box.height) + 'px'
  147. },
  148. top: {
  149. x: box.x,
  150. y: 0,
  151. width: (viewWidth - box.x) + 'px',
  152. height: box.y + 'px'
  153. },
  154. bottom: {
  155. x: 0,
  156. y: (box.y + box.height),
  157. width: (box.x + box.width) + 'px',
  158. height: (viewHeight - (box.y + box.height)) + 'px'
  159. }
  160. };
  161. //reverse the objects
  162. if (reverse) {
  163. var clone = Ext.clone(from);
  164. from = to;
  165. to = clone;
  166. delete clone;
  167. }
  168. if (animate) {
  169. Ext.each(['right', 'left', 'top', 'bottom'], function(side) {
  170. me[side].setBox(from[side]);
  171. me[side].animate({
  172. duration: me.duration,
  173. easing: me.easing,
  174. to: to[side]
  175. });
  176. },
  177. this);
  178. } else {
  179. Ext.each(['right', 'left', 'top', 'bottom'], function(side) {
  180. me[side].setBox(Ext.apply(from[side], to[side]));
  181. },
  182. this);
  183. }
  184. },
  185. /**
  186. * Removes all the elements for the spotlight
  187. */
  188. destroy: function() {
  189. Ext.destroy(this.right, this.left, this.top, this.bottom);
  190. delete this.el;
  191. delete this.all;
  192. }
  193. });