Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / src / core / Element.scroll-more.js
1 /*!
2  * Ext JS Library 3.2.1
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.Element
9  */
10 Ext.Element.addMethods({
11     /**
12      * Scrolls this element the specified scroll point. It does NOT do bounds checking so if you scroll to a weird value it will try to do it. For auto bounds checking, use scroll().
13      * @param {String} side Either "left" for scrollLeft values or "top" for scrollTop values.
14      * @param {Number} value The new scroll value
15      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
16      * @return {Element} this
17      */
18     scrollTo : function(side, value, animate){
19         var top = /top/i.test(side), //check if we're scrolling top or left
20                 me = this,
21                 dom = me.dom,
22             prop;
23         if (!animate || !me.anim) {
24             prop = 'scroll' + (top ? 'Top' : 'Left'), // just setting the value, so grab the direction
25             dom[prop] = value;
26         }else{
27             prop = 'scroll' + (top ? 'Left' : 'Top'), // if scrolling top, we need to grab scrollLeft, if left, scrollTop
28             me.anim({scroll: {to: top ? [dom[prop], value] : [value, dom[prop]]}},
29                          me.preanim(arguments, 2), 'scroll');
30         }
31         return me;
32     },
33     
34     /**
35      * Scrolls this element into view within the passed container.
36      * @param {Mixed} container (optional) The container element to scroll (defaults to document.body).  Should be a
37      * string (id), dom node, or Ext.Element.
38      * @param {Boolean} hscroll (optional) False to disable horizontal scroll (defaults to true)
39      * @return {Ext.Element} this
40      */
41     scrollIntoView : function(container, hscroll){
42         var c = Ext.getDom(container) || Ext.getBody().dom,
43                 el = this.dom,
44                 o = this.getOffsetsTo(c),
45             l = o[0] + c.scrollLeft,
46             t = o[1] + c.scrollTop,
47             b = t + el.offsetHeight,
48             r = l + el.offsetWidth,
49                 ch = c.clientHeight,
50                 ct = parseInt(c.scrollTop, 10),
51                 cl = parseInt(c.scrollLeft, 10),
52                 cb = ct + ch,
53                 cr = cl + c.clientWidth;
54
55         if (el.offsetHeight > ch || t < ct) {
56                 c.scrollTop = t;
57         } else if (b > cb){
58             c.scrollTop = b-ch;
59         }
60         c.scrollTop = c.scrollTop; // corrects IE, other browsers will ignore
61
62         if(hscroll !== false){
63                         if(el.offsetWidth > c.clientWidth || l < cl){
64                 c.scrollLeft = l;
65             }else if(r > cr){
66                 c.scrollLeft = r - c.clientWidth;
67             }
68             c.scrollLeft = c.scrollLeft;
69         }
70         return this;
71     },
72
73     // private
74     scrollChildIntoView : function(child, hscroll){
75         Ext.fly(child, '_scrollChildIntoView').scrollIntoView(this, hscroll);
76     },
77     
78     /**
79      * Scrolls this element the specified direction. Does bounds checking to make sure the scroll is
80      * within this element's scrollable range.
81      * @param {String} direction Possible values are: "l" (or "left"), "r" (or "right"), "t" (or "top", or "up"), "b" (or "bottom", or "down").
82      * @param {Number} distance How far to scroll the element in pixels
83      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
84      * @return {Boolean} Returns true if a scroll was triggered or false if the element
85      * was scrolled as far as it could go.
86      */
87      scroll : function(direction, distance, animate){
88          if(!this.isScrollable()){
89              return;
90          }
91          var el = this.dom,
92             l = el.scrollLeft, t = el.scrollTop,
93             w = el.scrollWidth, h = el.scrollHeight,
94             cw = el.clientWidth, ch = el.clientHeight,
95             scrolled = false, v,
96             hash = {
97                 l: Math.min(l + distance, w-cw),
98                 r: v = Math.max(l - distance, 0),
99                 t: Math.max(t - distance, 0),
100                 b: Math.min(t + distance, h-ch)
101             };
102             hash.d = hash.b;
103             hash.u = hash.t;
104             
105          direction = direction.substr(0, 1);
106          if((v = hash[direction]) > -1){
107             scrolled = true;
108             this.scrollTo(direction == 'l' || direction == 'r' ? 'left' : 'top', v, this.preanim(arguments, 2));
109          }
110          return scrolled;
111     }
112 });