Spotlight.js 5.5 KB

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