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