[619] | 1 | /* |
---|
| 2 | * Ext JS Library 2.2.1 |
---|
| 3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
| 4 | * licensing@extjs.com |
---|
| 5 | * |
---|
| 6 | * http://extjs.com/license |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * @class Ext.dd.ScrollManager |
---|
| 11 | * <p>Provides automatic scrolling of overflow regions in the page during drag operations.</p> |
---|
| 12 | * <p>The ScrollManager configs will be used as the defaults for any scroll container registered with it, |
---|
| 13 | * but you can also override most of the configs per scroll container by adding a |
---|
| 14 | * <tt>ddScrollConfig</tt> object to the target element that contains these properties: {@link #hthresh}, |
---|
| 15 | * {@link #vthresh}, {@link #increment} and {@link #frequency}. Example usage: |
---|
| 16 | * <pre><code> |
---|
| 17 | var el = Ext.get('scroll-ct'); |
---|
| 18 | el.ddScrollConfig = { |
---|
| 19 | vthresh: 50, |
---|
| 20 | hthresh: -1, |
---|
| 21 | frequency: 100, |
---|
| 22 | increment: 200 |
---|
| 23 | }; |
---|
| 24 | Ext.dd.ScrollManager.register(el); |
---|
| 25 | </code></pre> |
---|
| 26 | * <b>Note: This class uses "Point Mode" and is untested in "Intersect Mode".</b> |
---|
| 27 | * @singleton |
---|
| 28 | */ |
---|
| 29 | Ext.dd.ScrollManager = function(){ |
---|
| 30 | var ddm = Ext.dd.DragDropMgr; |
---|
| 31 | var els = {}; |
---|
| 32 | var dragEl = null; |
---|
| 33 | var proc = {}; |
---|
| 34 | |
---|
| 35 | var onStop = function(e){ |
---|
| 36 | dragEl = null; |
---|
| 37 | clearProc(); |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | var triggerRefresh = function(){ |
---|
| 41 | if(ddm.dragCurrent){ |
---|
| 42 | ddm.refreshCache(ddm.dragCurrent.groups); |
---|
| 43 | } |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | var doScroll = function(){ |
---|
| 47 | if(ddm.dragCurrent){ |
---|
| 48 | var dds = Ext.dd.ScrollManager; |
---|
| 49 | var inc = proc.el.ddScrollConfig ? |
---|
| 50 | proc.el.ddScrollConfig.increment : dds.increment; |
---|
| 51 | if(!dds.animate){ |
---|
| 52 | if(proc.el.scroll(proc.dir, inc)){ |
---|
| 53 | triggerRefresh(); |
---|
| 54 | } |
---|
| 55 | }else{ |
---|
| 56 | proc.el.scroll(proc.dir, inc, true, dds.animDuration, triggerRefresh); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | var clearProc = function(){ |
---|
| 62 | if(proc.id){ |
---|
| 63 | clearInterval(proc.id); |
---|
| 64 | } |
---|
| 65 | proc.id = 0; |
---|
| 66 | proc.el = null; |
---|
| 67 | proc.dir = ""; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | var startProc = function(el, dir){ |
---|
| 71 | clearProc(); |
---|
| 72 | proc.el = el; |
---|
| 73 | proc.dir = dir; |
---|
| 74 | var freq = (el.ddScrollConfig && el.ddScrollConfig.frequency) ? |
---|
| 75 | el.ddScrollConfig.frequency : Ext.dd.ScrollManager.frequency; |
---|
| 76 | proc.id = setInterval(doScroll, freq); |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | var onFire = function(e, isDrop){ |
---|
| 80 | if(isDrop || !ddm.dragCurrent){ return; } |
---|
| 81 | var dds = Ext.dd.ScrollManager; |
---|
| 82 | if(!dragEl || dragEl != ddm.dragCurrent){ |
---|
| 83 | dragEl = ddm.dragCurrent; |
---|
| 84 | // refresh regions on drag start |
---|
| 85 | dds.refreshCache(); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | var xy = Ext.lib.Event.getXY(e); |
---|
| 89 | var pt = new Ext.lib.Point(xy[0], xy[1]); |
---|
| 90 | for(var id in els){ |
---|
| 91 | var el = els[id], r = el._region; |
---|
| 92 | var c = el.ddScrollConfig ? el.ddScrollConfig : dds; |
---|
| 93 | if(r && r.contains(pt) && el.isScrollable()){ |
---|
| 94 | if(r.bottom - pt.y <= c.vthresh){ |
---|
| 95 | if(proc.el != el){ |
---|
| 96 | startProc(el, "down"); |
---|
| 97 | } |
---|
| 98 | return; |
---|
| 99 | }else if(r.right - pt.x <= c.hthresh){ |
---|
| 100 | if(proc.el != el){ |
---|
| 101 | startProc(el, "left"); |
---|
| 102 | } |
---|
| 103 | return; |
---|
| 104 | }else if(pt.y - r.top <= c.vthresh){ |
---|
| 105 | if(proc.el != el){ |
---|
| 106 | startProc(el, "up"); |
---|
| 107 | } |
---|
| 108 | return; |
---|
| 109 | }else if(pt.x - r.left <= c.hthresh){ |
---|
| 110 | if(proc.el != el){ |
---|
| 111 | startProc(el, "right"); |
---|
| 112 | } |
---|
| 113 | return; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | clearProc(); |
---|
| 118 | }; |
---|
| 119 | |
---|
| 120 | ddm.fireEvents = ddm.fireEvents.createSequence(onFire, ddm); |
---|
| 121 | ddm.stopDrag = ddm.stopDrag.createSequence(onStop, ddm); |
---|
| 122 | |
---|
| 123 | return { |
---|
| 124 | /** |
---|
| 125 | * Registers new overflow element(s) to auto scroll |
---|
| 126 | * @param {Mixed/Array} el The id of or the element to be scrolled or an array of either |
---|
| 127 | */ |
---|
| 128 | register : function(el){ |
---|
| 129 | if(Ext.isArray(el)){ |
---|
| 130 | for(var i = 0, len = el.length; i < len; i++) { |
---|
| 131 | this.register(el[i]); |
---|
| 132 | } |
---|
| 133 | }else{ |
---|
| 134 | el = Ext.get(el); |
---|
| 135 | els[el.id] = el; |
---|
| 136 | } |
---|
| 137 | }, |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Unregisters overflow element(s) so they are no longer scrolled |
---|
| 141 | * @param {Mixed/Array} el The id of or the element to be removed or an array of either |
---|
| 142 | */ |
---|
| 143 | unregister : function(el){ |
---|
| 144 | if(Ext.isArray(el)){ |
---|
| 145 | for(var i = 0, len = el.length; i < len; i++) { |
---|
| 146 | this.unregister(el[i]); |
---|
| 147 | } |
---|
| 148 | }else{ |
---|
| 149 | el = Ext.get(el); |
---|
| 150 | delete els[el.id]; |
---|
| 151 | } |
---|
| 152 | }, |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * The number of pixels from the top or bottom edge of a container the pointer needs to be to |
---|
| 156 | * trigger scrolling (defaults to 25) |
---|
| 157 | * @type Number |
---|
| 158 | */ |
---|
| 159 | vthresh : 25, |
---|
| 160 | /** |
---|
| 161 | * The number of pixels from the right or left edge of a container the pointer needs to be to |
---|
| 162 | * trigger scrolling (defaults to 25) |
---|
| 163 | * @type Number |
---|
| 164 | */ |
---|
| 165 | hthresh : 25, |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * The number of pixels to scroll in each scroll increment (defaults to 50) |
---|
| 169 | * @type Number |
---|
| 170 | */ |
---|
| 171 | increment : 100, |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * The frequency of scrolls in milliseconds (defaults to 500) |
---|
| 175 | * @type Number |
---|
| 176 | */ |
---|
| 177 | frequency : 500, |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * True to animate the scroll (defaults to true) |
---|
| 181 | * @type Boolean |
---|
| 182 | */ |
---|
| 183 | animate: true, |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * The animation duration in seconds - |
---|
| 187 | * MUST BE less than Ext.dd.ScrollManager.frequency! (defaults to .4) |
---|
| 188 | * @type Number |
---|
| 189 | */ |
---|
| 190 | animDuration: .4, |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | * Manually trigger a cache refresh. |
---|
| 194 | */ |
---|
| 195 | refreshCache : function(){ |
---|
| 196 | for(var id in els){ |
---|
| 197 | if(typeof els[id] == 'object'){ // for people extending the object prototype |
---|
| 198 | els[id]._region = els[id].getRegion(); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | }; |
---|
| 203 | }(); |
---|