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