Registry.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Registry Provides easy access to all drag drop components that
  8. * are registered on a page. Items can be retrieved either directly by
  9. * DOM node id, or by passing in the drag drop event that occurred and
  10. * looking up the event target.
  11. * @singleton
  12. */
  13. Ext.dd.Registry = function() {
  14. var elements = {};
  15. var handles = {};
  16. var autoIdSeed = 0;
  17. var getId = function(el, autogen) {
  18. if (typeof el == "string") {
  19. return el;
  20. }
  21. var id = el.id;
  22. if (!id && autogen !== false) {
  23. id = "extdd-" + (++autoIdSeed);
  24. el.id = id;
  25. }
  26. return id;
  27. };
  28. return {
  29. /**
  30. * Resgister a drag drop element
  31. *
  32. * @param {String/HTMLElement)
  33. * element The id or DOM node to register
  34. * @param {Object}
  35. * data (optional) An custom data object that will be passed
  36. * between the elements that are involved in drag drop
  37. * operations. You can populate this object with any
  38. * arbitrary properties that your own code knows how to
  39. * interpret, plus there are some specific properties known
  40. * to the Registry that should be populated in the data
  41. * object (if applicable):
  42. *
  43. * <pre>
  44. * Value Description
  45. * <br />
  46. * --------- ------------------------------------------
  47. * <br />
  48. * handles Array of DOM nodes that trigger dragging
  49. * <br />
  50. * for the element being registered
  51. * <br />
  52. * isHandle
  53. * True if the element passed in triggers
  54. * <br />
  55. * dragging itself, else false
  56. *
  57. * </pre>
  58. */
  59. register : function(el, data) {
  60. data = data || {};
  61. if (typeof el == "string") {
  62. el = document.getElementById(el);
  63. }
  64. data.ddel = el;
  65. elements[getId(el)] = data;
  66. if (data.isHandle !== false) {
  67. handles[data.ddel.id] = data;
  68. }
  69. if (data.handles) {
  70. var hs = data.handles;
  71. for (var i = 0, len = hs.length; i < len; i++) {
  72. handles[getId(hs[i])] = data;
  73. }
  74. }
  75. },
  76. /**
  77. * Unregister a drag drop element
  78. *
  79. * @param {String/HTMLElement)
  80. * element The id or DOM node to unregister
  81. */
  82. unregister : function(el) {
  83. var id = getId(el, false);
  84. var data = elements[id];
  85. if (data) {
  86. delete elements[id];
  87. if (data.handles) {
  88. var hs = data.handles;
  89. for (var i = 0, len = hs.length; i < len; i++) {
  90. delete handles[getId(hs[i], false)];
  91. }
  92. }
  93. }
  94. },
  95. /**
  96. * Returns the handle registered for a DOM Node by id
  97. *
  98. * @param {String/HTMLElement}
  99. * id The DOM node or id to look up
  100. * @return {Object} handle The custom handle data
  101. */
  102. getHandle : function(id) {
  103. if (typeof id != "string") { // must be element?
  104. id = id.id;
  105. }
  106. return handles[id];
  107. },
  108. /**
  109. * Returns the handle that is registered for the DOM node that is the
  110. * target of the event
  111. *
  112. * @param {Event}
  113. * e The event
  114. * @return {Object} handle The custom handle data
  115. */
  116. getHandleFromEvent : function(e) {
  117. var t = Ext.lib.Event.getTarget(e);
  118. return t ? handles[t.id] : null;
  119. },
  120. /**
  121. * Returns a custom data object that is registered for a DOM node by id
  122. *
  123. * @param {String/HTMLElement}
  124. * id The DOM node or id to look up
  125. * @return {Object} data The custom data
  126. */
  127. getTarget : function(id) {
  128. if (typeof id != "string") { // must be element?
  129. id = id.id;
  130. }
  131. return elements[id];
  132. },
  133. /**
  134. * Returns a custom data object that is registered for the DOM node that
  135. * is the target of the event
  136. *
  137. * @param {Event}
  138. * e The event
  139. * @return {Object} data The custom data
  140. */
  141. getTargetFromEvent : function(e) {
  142. var t = Ext.lib.Event.getTarget(e);
  143. return t ? elements[t.id] || handles[t.id] : null;
  144. }
  145. };
  146. }();