Debian Codegraph @

samples/tests/resources/script/scrollbar.js

raw
1 /*
2
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 */
19
20 var svgNS = "http://www.w3.org/2000/svg";
21 var xlinkNS = "http://www.w3.org/1999/xlink";
22
23 function ScrollBar(contentId,
24 steps,
25 scrollBarX,
26 scrollBarY,
27 scrollBarHeight,
28 scrolledHeight,
29 arrowWidth,
30 upArrowId, downArrowId,
31 cursorId, cursorHeight){
32 this.content = document.getElementById(contentId);
33 this.steps = steps;
34 this.curStep = 0;
35 this.scrollBarHeight = scrollBarHeight;
36 this.scrolledHeight = scrolledHeight;
37 this.arrowWidth = arrowWidth;
38 this.cursorHeight = cursorHeight;
39
40 //
41 // First, add the elements which make up the scroll bar
42 //
43 var insertAt = this.content.getParentNode().getParentNode();
44
45 //
46 // Add the up arrow
47 //
48 var upArrow = document.createElementNS(svgNS, 'use');
49 upArrow.setAttributeNS(xlinkNS, 'href', upArrowId);
50 upArrow.setAttributeNS(null, 'x', scrollBarX);
51 upArrow.setAttributeNS(null, 'y', scrollBarY);
52 insertAt.appendChild(upArrow);
53
54 //
55 // Add the down arrow
56 //
57 var downArrow = document.createElementNS(svgNS, 'use');
58 downArrow.setAttributeNS(xlinkNS, 'href', downArrowId);
59 downArrow.setAttributeNS(null, 'x', scrollBarX);
60 downArrow.setAttributeNS(null, 'y', scrollBarY + scrollBarHeight - arrowWidth);
61 insertAt.appendChild(downArrow);
62
63 //
64 // Add the cursor
65 //
66 var cursor = document.createElementNS(svgNS, 'use');
67 cursor.setAttributeNS(xlinkNS, 'href', cursorId);
68 cursor.setAttributeNS(null, 'x', scrollBarX);
69 cursor.setAttributeNS(null, 'y', scrollBarY + arrowWidth);
70
71 this.cursor = document.createElementNS(svgNS, 'g');
72 this.cursor.appendChild(cursor);
73 insertAt.appendChild(this.cursor);
74
75 //
76 // Now, add event handling to scroll the content
77 //
78
79 // Scrolling down means moving the cursor up and
80 // content down (i.e., towards the positive side
81 // of the Y axis).
82
83
84 this.scrollToStep = function(newStep){
85 //
86 // Compute new position for cursor
87 //
88 var cursorPos = newStep*((this.scrollBarHeight - 2*this.arrowWidth - this.cursorHeight) / this.steps);
89 this.cursor.setAttributeNS(null, "transform", "translate(0," + cursorPos + ")");
90
91 //
92 // Compute new content position
93 //
94 var contentPos = - newStep*(this.scrolledHeight / this.steps);
95 this.content.setAttributeNS(null, "transform", "translate(0," + contentPos + ")");
96
97 this.curStep = newStep;
98 }
99
100
101 // Scrolling up means moving the cursor down and
102 // the content up (i.e., towards the negative side
103 // of the Y axis).
104 this.handleScrollUp = function(evt) {
105 if (this.curStep < (this.steps - 1)){
106 this.scrollToStep(this.curStep + 1);
107 }
108 }
109
110 this.handleScrollDown = function(evt) {
111 if (this.curStep > 0){
112 this.scrollToStep(this.curStep - 1);
113 }
114 }
115
116 this.getHandleScrollDownFunction = function() {
117 var thisObject = this;
118 return function(evt) { thisObject.handleScrollDown(evt); }
119 }
120
121 this.getHandleScrollUpFunction = function() {
122 var thisObject = this;
123 return function(evt) { thisObject.handleScrollUp(evt); }
124 }
125
126 upArrow.addEventListener('click', this.getHandleScrollDownFunction(), false);
127 downArrow.addEventListener('click', this.getHandleScrollUpFunction(), false);
128
129 }
130