not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / scriptengines / webkit / dashboard / AppleClasses / AppleScrollArea.js
blob8371d40e1e38a6f6be98072426f3245d21d4c23f
1 /*
2  * Copyright 2008 Stefan Buller <hikingpete@cain.afraid.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) version 3, or any
8  * later version accepted by the membership of KDE e.V. (or its
9  * successor approved by the membership of KDE e.V.), which shall
10  * act as a proxy defined in Section 6 of version 3 of the license.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public 
18  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  */
21 function AppleScrollArea(content) {
22         for (var i = 0; i < arguments.length; i++) {
23                 this.addScrollbar(arguments[i]);
24         }
25         var that=this;
26         var handler=function(e) {
27                 switch(e.which) {
28                         case 37: //left
29                                 that.horizontalScrollTo(that.content.scrollLeft -
30                                                 that.singlepressScrollPixels);
31                                 break;
32                         case 38: //up
33                                 that.verticalScrollTo(that.content.scrollTop -
34                                                 that.singlepressScrollPixels);
35                                 break;
36                         case 39: //right
37                                 that.horizontalScrollTo(that.content.scrollLeft +
38                                                 that.singlepressScrollPixels);
39                                 break;
40                         case 40: //down
41                                 that.verticalScrollTo(that.content.scrollTop +
42                                                 that.singlepressScrollPixels);
43                                 break;
44                         default:
45                                 //the event may contiue to propagate
46                                 return true;
47                 }
48                 //arrest propagation
49                 return false;
50         }
51         content.addEventListener('keydown',handler,false);
53         //Apple mandated properties that must be reacted to.
54         this.scrollsVertically = false;
55         this.scrollsHorizontally = false;
56         this.singlepressScrollPixels = 10; //Somebody change this. Please.
58         //Apple mandated properties that are `read only'.
59         this.viewHeight = content.clientHeight;
60         this.viewToContentHeightRatio = content.clientHeight / content.scrollHeight;
61         this.viewWidth = content.clientWidth;
62         this.viewToContentWidthRatio = content.clientWidth / content.scrollWidth;
63         //I'm worried that the scrollHeight/Width could change on me. If that turns
64         //out to be a problem, then getters would be the way to go.
66         //extras
67         this.scrollbars = [];
68         this.content = content;
71 AppleScrollArea.prototype.addScrollbar = function(scrollbar) {
72         this.scrollbars.push(scrollbar);
73         scrollbar.setScrollArea(this);
76 AppleScrollArea.prototype.removeScrollbar = function(scrollbar) {
77         this.scrollbars.filter(function(element){return (element === scrollbar);});
78         scrollbar.setScrollArea(null); //Just a guess. This might not be right.
81 AppleScrollArea.prototype.remove = function() {
82         //Remove the div, or remove the effects of AppleScrollArea?
83         //Perhaps this can all be replaced with a simple removeChild()
84         content.scrollTop = 0;
85         content.scrollLeft = 0;
86         for (var i = 0; i < this.scrollbars.length; i++) {
87                 this.scrollbars[i].remove();
88                 delete this.scrollbars[i];
89         }
90         delete this;
93 AppleScrollArea.prototype.reveal = function(element) {
94         //First we find it
95         var distX = 0;
96         var distY = 0;
97         var el = element;
98         while (el !== this.content) {
99                 distX += (+el.offsetTop);
100                 distY += (+el.offsetLeft);
101                 el = el.parentNode;
102                 if (el == null) {
103                         throw "Target element not in ScrollArea.";
104                 }
105         }
107         this.verticalScrollTo(distY);
108         this.horizontalScrollTo(distX);
111 AppleScrollArea.prototype.focus = function() {
112         this.content.focus();
115 AppleScrollArea.prototype.blur = function() {
116         this.content.blur();
119 AppleScrollArea.prototype.verticalScrollTo = function(position) {
120         this.scrollTop = position;
123 AppleScrollArea.prototype.horizontalScrollTo = function(position) {
124         this.scrollLeft = position;