Registry.js 3.8 KB

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