org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/Scrollbar.js
author Eugene Ostroukhov <eugeneo@symbian.org>
Tue, 16 Mar 2010 18:01:11 -0700
changeset 273 b1f63c2c240c
parent 211 7b148352159e
permissions -rw-r--r--
Bug 2072 - Update .js files with EPL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
210
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     1
/**
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     2
 * Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies).
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     3
 * All rights reserved.
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     4
 * This component and the accompanying materials are made available
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     6
 * which accompanies this distribution, and is available
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     8
 *
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     9
 * Initial Contributors:
273
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    10
 * Nokia Corporation - initial contribution.
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    11
 * 
210
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
    12
 * Contributors:
273
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    13
 * 
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    14
 * Description:
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    15
 * 
210
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
    16
 */
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    17
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    18
///////////////////////////////////////////////////////////////////////////////
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    19
// The Scrollbar class is an implementation of a user interface element that
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    20
// indicates the current viewport position in a document.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    21
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    22
// Constructor.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    23
function Scrollbar(parentElement) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    24
    uiLogger.debug("Scrollbar(" + parentElement + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    25
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    26
    // get the parent element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    27
    this.parentElement = parentElement;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    28
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    29
    // create the root element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    30
    this.rootElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    31
    this.rootElement.className = "Scrollbar";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    32
    this.rootElement.style.visibility = "hidden";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    33
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    34
    // create the scrollbar
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    35
    // the scrollbar consists of a root element with six children
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    36
    // (three track elements and three thumb elements)
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    37
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    38
    // track
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    39
    this.trackTopElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    40
    this.trackTopElement.className = "ScrollbarTrackTop";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    41
    this.trackMiddleElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    42
    this.trackMiddleElement.className = "ScrollbarTrackMiddle";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    43
    this.trackBottomElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    44
    this.trackBottomElement.className = "ScrollbarTrackBottom";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    45
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    46
    // thumb
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    47
    this.thumbTopElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    48
    this.thumbTopElement.className = "ScrollbarThumbTop";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    49
    this.thumbMiddleElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    50
    this.thumbMiddleElement.className = "ScrollbarThumbMiddle";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    51
    this.thumbBottomElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    52
    this.thumbBottomElement.className = "ScrollbarThumbBottom";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    53
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    54
    // assemble and attach the scrollbar
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    55
    this.rootElement.appendChild(this.trackTopElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    56
    this.rootElement.appendChild(this.trackMiddleElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    57
    this.rootElement.appendChild(this.trackBottomElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    58
    this.rootElement.appendChild(this.thumbTopElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    59
    this.rootElement.appendChild(this.thumbMiddleElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    60
    this.rootElement.appendChild(this.thumbBottomElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    61
    this.parentElement.appendChild(this.rootElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    62
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    63
    // bring the scrollbar up to date
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    64
    this.update(0, 100, 100);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    65
}
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    66
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    67
// Parent element for the scrollbar.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    68
Scrollbar.prototype.parentElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    69
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    70
// Root HTML element in the scrollbar.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    71
Scrollbar.prototype.rootElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    72
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    73
// Scrollbar track top element.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    74
Scrollbar.prototype.trackTopElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    75
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    76
// Scrollbar track middle element.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    77
Scrollbar.prototype.trackMiddleElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    78
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    79
// Scrollbar track bottom element.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    80
Scrollbar.prototype.trackBottomElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    81
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    82
// Scrollbar thumb top element.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    83
Scrollbar.prototype.thumbTopElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    84
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    85
// Scrollbar thumb middle element.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    86
Scrollbar.prototype.thumbMiddleElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    87
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    88
// Scrollbar thumb bottom element.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    89
Scrollbar.prototype.thumbBottomElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    90
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    91
// Is the scrollbar needed?
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    92
Scrollbar.prototype.scrollbarNeeded = false;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    93
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    94
// Updates the scrollbar.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    95
Scrollbar.prototype.update = function(scrollY, viewportHeight, documentHeight) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    96
    // figure out current heights
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    97
    var scrollbarHeight = this.rootElement.clientHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    98
    var trackTopHeight = this.trackTopElement.clientHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    99
    var trackBottomHeight = this.trackBottomElement.clientHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   100
    var thumbTopHeight = this.thumbTopElement.clientHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   101
    var thumbBottomHeight = this.thumbBottomElement.clientHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   102
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   103
    // scrollable height is the larger of document and viewport heights
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   104
    var scrollableHeight = documentHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   105
    var scrollbarNeeded = true;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   106
    if (viewportHeight >= documentHeight) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   107
        scrollableHeight = viewportHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   108
        scrollbarNeeded = false;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   109
    }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   110
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   111
    // show or hide scrollbar?
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   112
    if (scrollbarNeeded != this.scrollbarNeeded) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   113
        this.scrollbarNeeded = scrollbarNeeded;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   114
        this.rootElement.style.visibility = scrollbarNeeded ? "visible" : "hidden";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   115
    }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   116
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   117
    // calculate thumb top position...
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   118
    var thumbTopPct = scrollY / scrollableHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   119
    var thumbTop = scrollbarHeight * thumbTopPct;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   120
    // ...and bottom position...
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   121
    var thumbBottomPct = (scrollY + viewportHeight) / scrollableHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   122
    var thumbBottom = scrollbarHeight * thumbBottomPct;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   123
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   124
    // ...and thumb height
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   125
    var thumbHeight = thumbBottom - thumbTop;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   126
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   127
    // ensure that the thumb is not too small
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   128
    var thumbMinHeight = thumbTopHeight + thumbBottomHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   129
    if (thumbHeight < thumbMinHeight) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   130
        var underflow = thumbMinHeight - thumbHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   131
        // adjust thumb top pos assuming a shorter scrollbar track
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   132
        var thumbMid = (scrollbarHeight - underflow) * ((thumbTopPct + thumbBottomPct) / 2) + (underflow / 2);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   133
        thumbTop = thumbMid - (thumbMinHeight / 2);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   134
        thumbBottom = thumbTop + thumbMinHeight;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   135
        thumbHeight = thumbBottom - thumbTop;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   136
    }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   137
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   138
    // position and size track element (add 1 to the middle section height for rounding errors)
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   139
    this.trackTopElement.style.top = "0px";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   140
    this.trackMiddleElement.style.top = Math.round(trackTopHeight) + "px";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   141
    this.trackMiddleElement.style.height = Math.round(scrollbarHeight - trackTopHeight - trackBottomHeight + 1) + "px";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   142
    this.trackBottomElement.style.top = Math.round(scrollbarHeight - trackTopHeight) + "px";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   143
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   144
    // position and size thumb element (add 1 to the middle section height for rounding errors)
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   145
    this.thumbTopElement.style.top = Math.round(thumbTop) + "px";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   146
    this.thumbMiddleElement.style.top = Math.round(thumbTop + thumbTopHeight) + "px";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   147
    this.thumbMiddleElement.style.height = Math.round(thumbHeight - thumbTopHeight - thumbBottomHeight + 1) + "px";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   148
    this.thumbBottomElement.style.top = Math.round(thumbBottom - thumbBottomHeight) + "px";
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   149
};