StatusProxy.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.dd.StatusProxy A specialized drag proxy that supports a drop
  8. * status icon, {@link Ext.Layer} styles and auto-repair. This is the
  9. * default drag proxy used by all Ext.dd components.
  10. * @constructor
  11. * @param {Object}
  12. * config
  13. */
  14. Ext.dd.StatusProxy = function(config) {
  15. Ext.apply(this, config);
  16. this.id = this.id || Ext.id();
  17. this.el = new Ext.Layer({
  18. dh : {
  19. id : this.id,
  20. tag : "div",
  21. cls : "x-dd-drag-proxy " + this.dropNotAllowed,
  22. children : [{
  23. tag : "div",
  24. cls : "x-dd-drop-icon"
  25. }, {
  26. tag : "div",
  27. cls : "x-dd-drag-ghost"
  28. }]
  29. },
  30. shadow : !config || config.shadow !== false
  31. });
  32. this.ghost = Ext.get(this.el.dom.childNodes[1]);
  33. this.dropStatus = this.dropNotAllowed;
  34. };
  35. Ext.dd.StatusProxy.prototype = {
  36. /**
  37. * @cfg {String} dropAllowed The CSS class to apply to the status element
  38. * when drop is allowed (defaults to "x-dd-drop-ok").
  39. */
  40. dropAllowed : "x-dd-drop-ok",
  41. /**
  42. * @cfg {String} dropNotAllowed The CSS class to apply to the status element
  43. * when drop is not allowed (defaults to "x-dd-drop-nodrop").
  44. */
  45. dropNotAllowed : "x-dd-drop-nodrop",
  46. /**
  47. * Updates the proxy's visual element to indicate the status of whether or
  48. * not drop is allowed over the current target element.
  49. *
  50. * @param {String}
  51. * cssClass The css class for the new drop status indicator image
  52. */
  53. setStatus : function(cssClass) {
  54. cssClass = cssClass || this.dropNotAllowed;
  55. if (this.dropStatus != cssClass) {
  56. this.el.replaceClass(this.dropStatus, cssClass);
  57. this.dropStatus = cssClass;
  58. }
  59. },
  60. /**
  61. * Resets the status indicator to the default dropNotAllowed value
  62. *
  63. * @param {Boolean}
  64. * clearGhost True to also remove all content from the ghost,
  65. * false to preserve it
  66. */
  67. reset : function(clearGhost) {
  68. this.el.dom.className = "x-dd-drag-proxy " + this.dropNotAllowed;
  69. this.dropStatus = this.dropNotAllowed;
  70. if (clearGhost) {
  71. this.ghost.update("");
  72. }
  73. },
  74. /**
  75. * Updates the contents of the ghost element
  76. *
  77. * @param {String}
  78. * html The html that will replace the current innerHTML of the
  79. * ghost element
  80. */
  81. update : function(html) {
  82. if (typeof html == "string") {
  83. this.ghost.update(html);
  84. } else {
  85. this.ghost.update("");
  86. html.style.margin = "0";
  87. this.ghost.dom.appendChild(html);
  88. }
  89. },
  90. /**
  91. * Returns the underlying proxy {@link Ext.Layer}
  92. *
  93. * @return {Ext.Layer} el
  94. */
  95. getEl : function() {
  96. return this.el;
  97. },
  98. /**
  99. * Returns the ghost element
  100. *
  101. * @return {Ext.Element} el
  102. */
  103. getGhost : function() {
  104. return this.ghost;
  105. },
  106. /**
  107. * Hides the proxy
  108. *
  109. * @param {Boolean}
  110. * clear True to reset the status and clear the ghost contents,
  111. * false to preserve them
  112. */
  113. hide : function(clear) {
  114. this.el.hide();
  115. if (clear) {
  116. this.reset(true);
  117. }
  118. },
  119. /**
  120. * Stops the repair animation if it's currently running
  121. */
  122. stop : function() {
  123. if (this.anim && this.anim.isAnimated && this.anim.isAnimated()) {
  124. this.anim.stop();
  125. }
  126. },
  127. /**
  128. * Displays this proxy
  129. */
  130. show : function() {
  131. this.el.show();
  132. },
  133. /**
  134. * Force the Layer to sync its shadow and shim positions to the element
  135. */
  136. sync : function() {
  137. this.el.sync();
  138. },
  139. /**
  140. * Causes the proxy to return to its position of origin via an animation.
  141. * Should be called after an invalid drop operation by the item being
  142. * dragged.
  143. *
  144. * @param {Array}
  145. * xy The XY position of the element ([x, y])
  146. * @param {Function}
  147. * callback The function to call after the repair is complete
  148. * @param {Object}
  149. * scope The scope in which to execute the callback
  150. */
  151. repair : function(xy, callback, scope) {
  152. this.callback = callback;
  153. this.scope = scope;
  154. if (xy && this.animRepair !== false) {
  155. this.el.addClass("x-dd-drag-repair");
  156. this.el.hideUnders(true);
  157. this.anim = this.el.shift({
  158. duration : this.repairDuration || .5,
  159. easing : 'easeOut',
  160. xy : xy,
  161. stopFx : true,
  162. callback : this.afterRepair,
  163. scope : this
  164. });
  165. } else {
  166. this.afterRepair();
  167. }
  168. },
  169. // private
  170. afterRepair : function() {
  171. this.hide(true);
  172. if (typeof this.callback == "function") {
  173. this.callback.call(this.scope || this);
  174. }
  175. this.callback = null;
  176. this.scope = null;
  177. }
  178. };