Skip to main content
summaryrefslogtreecommitdiffstats
blob: 506a3464ebb87bd26ca0685605a2699e30597b69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2005 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intel Corporation - initial API and implementation
 *******************************************************************************/
 
var isMozilla = parent.isMozilla;
var isIE = parent.isIE;

var typein;
var currentId;

function compare(keyword, pattern) {
	var kI = 0, pI = 0;
	var kCh, pCh;

	while (kI < keyword.length && pI < pattern.length) {
		kCh = keyword.charAt(kI).toLowerCase();
		pCh = pattern.charAt(pI).toLowerCase();
		if (kCh > pCh) {
			return 1;
		} else if (kCh < pCh) {
			return -1;
		}
		kI++;
		pI++;
	}
	if (keyword.length >= pattern.length) {
		return 0;
	} else {
		return -1;
	}
}

function searchPattern(pattern) {
	var from = 0;
	var to = ids.length;
	var i;
	var res;

	while (to > from) {
		i = Math.floor((to + from) / 2);
		res = compare(ids[i], pattern);
		if (res == 0) {
			while (i > 0) {
				res = compare(ids[--i], pattern);
				if (res != 0) {
					i++;
					break;
				}
			}
			return ids[i];
		} else if (res < 0) {
			from = i + 1;
		} else {
			to = i;
		}
	}

	return null;
}

function keyDownHandler(e) {
	var key;

	if (isIE) {
		key = window.event.keyCode;
	} else if (isMozilla) {
		key = e.keyCode;
	}

	if (key != 13 && key != 38 && key != 40)
		return true;

	if (isMozilla)
		e.cancelBubble = true;
	else if (isIE)
		window.event.cancelBubble = true;

	if (key == 13) { // enter
		// display topic corresponded to selected list item
		parent.setTypeinValue(parent.getSelection());
		parent.doDisplay();
	} if (key == 38) { // up arrow
		parent.selectNextUp();
	} else if (key == 40) { // down arrow
		parent.selectNextDown();
	}

	return false;
}

/**
  * Select the corresponding item in the index list on typein value change.
  * Check is performed periodically in short interval.
  */
function intervalHandler() {
	if (typein.value != typein.previous) {
		// typein value has been changed
		typein.previous = typein.value;
		var id = searchPattern(typein.value);
		if (id && id != currentId) {
			// the value has became to fit to other item
			if (parent.selectTopicById(id)) {
				currentId = id;
			}
		}
	}
}

/**
 * IndexTypeinFrame onload handler
 */
function onloadHandler() {
	parent.typeinFrame = window;

	typein = document.getElementById("typein");

	typein.value = "";
	typein.previous = "";

	currentId = "";

	if (isMozilla) {
		document.addEventListener("keydown", keyDownHandler, true);
	} else if (isIE) {
		document.onkeydown = keyDownHandler;
	}

	setInterval("intervalHandler()", 200);
}

Back to the top