-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (169 loc) · 4.6 KB
/
script.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
$(function(){
/* binds */
$('#search_suggest').on('click', 'li', function(ev){
$('#txtSearch').val($(ev.target).html())
window.location.href = 'https://'+ $('#l').val() +'.wikipedia.org/wiki/'+ escape($(ev.target).html());
}).on('mouseenter', function(ev){
isOver = true;
}).on('mouseleave', function(){
isOver = false;
if (isBlur) {
$('#search_suggest').hide().html('');
}
});
$('#txtSearch').on('keyup', function(ev){
var code = ev.keyCode || ev.which;
if ($('#txtSearch').val().length > 0 && isKeyChar(code)){
$.ajax({
url: 'https://'+ $('#l').val() +'.wikipedia.org/w/api.php?action=opensearch&search='+ $('#txtSearch').val() +'',
dataType: 'jsonp',
success: function(data){
var options = data[1], // 0 is the sent string. 1 is a list of options.
list = [];
if (options.length > 0 ){
$.each(options, function(i, option){
list.push('<li>'+ option +'</li>')
})
} else {
list.push('<li class="no-option">'+ searchInLangs[$('#l').val()].noResult +'</li>')
}
$('#search_suggest').show().html(list.join(''))
hoverBindForSuggestions()
}
});
} else if ($('#search_suggest').is(':visible')){
var $suggestions = $('#search_suggest'),
$active = $suggestions.find('.active'),
$lis = $suggestions.find('li');
if (code === 38) { // up
if ($lis.index($active) === 0) {
$active.removeClass('active');
$lis.last().addClass('active');
} else {
$active.removeClass('active').prev('li').addClass('active')
}
$('#txtSearch').val($suggestions.find('.active').html())
} else if (code === 40) { // down
if ($lis.index($active) === ($lis.length-1)) {
$active.removeClass('active');
$lis.first().addClass('active');
} else if ($lis.index($active) === -1) {
$lis.first().addClass('active');
} else {
$active.removeClass('active').next('li').addClass('active')
}
$('#txtSearch').val($suggestions.find('.active').html())
}
}
}).on('focus', function(ev){
isBlur = false;
}).on('blur', function(ev){
isBlur = true;
if (!isOver) {
$('#search_suggest').hide().html('');
}
});
$('#langs a:not(.external)').on('click', function(ev){
var $this = $(ev.target),
lang = $this.data('lang-code');
langSwap(lang);
});
/* functions */
function langSwap(lang) {
if (searchInLangs.hasOwnProperty(lang)) {
$('#frmSearch').attr('action', 'http://'+lang+'.wikipedia.org/wiki/Special:Search');
$('#l').val(lang);
$('#cmdSearch').val(searchInLangs[lang].search);
$('#searchtitle').text(searchInLangs[lang].head);
if (window.location.hash !== '#'+ lang) {
window.location.hash = lang;
}
} else {
if (confirm('Invalid lang for this page. Do you wish to be sent to wikipedia.org?')) {
window.location = 'http://wikipedia.org/';
}
}
}
function isKeyChar(code) {
if (code == 32 || code == 8) {
return true; // space or backspace
} else if (code < 47 || (code > 112 && code < 47) ) {
return false;
} else {
return true;
}
}
function hoverBindForSuggestions(){
$('#search_suggest li').off('mouseenter.suggesttion').on('mouseenter.suggesttion', function(ev){
$(ev.target).addClass('active').siblings().removeClass('active');
})
}
/* Setup */
var isOver = false;
var isBlur = true;
var searchInLangs = {
'ar': {
search: 'بحث',
noResult: 'Inga resultat.',
head: 'Wikipedia på arabiska'
},
'bs': {
search: 'Klask',
noResult: 'Inga resultat.',
head: 'Wikipedia på bosniska'
},
'da': {
search: 'Søg',
noResult: 'Inga resultat.',
head: 'Wikipedia på danska'
},
'fi': {
search: 'Haku',
noResult: 'Inga resultat.',
head: 'Wikipedia på finska'
},
'ku': {
search: 'Lêgerîn',
noResult: 'Inga resultat.',
head: 'Wikipedia på kurdiska'
},
'no': {
search: 'Søk',
noResult: 'Inga resultat.',
head: 'Norska Wikipedia'
},
'fa': {
search: 'جستجو',
noResult: 'Inga resultat.',
head: 'Wikipedia på persiska'
},
'se': {
search: 'Oza',
noResult: 'Inga resultat.',
head: 'Wikipedia på samiska'
},
'sr': {
search: 'Претрага',
noResult: 'Inga resultat.',
head: 'Wikipedia på serbiska'
},
'es': {
search: 'Buscar',
noResult: 'Inga resultat.',
head: 'Wikipedia på spanska'
},
'sv': {
search: 'Sök',
noResult: 'Inga resultat.',
head: 'Wikipedia på svenska'
},
'de': {
search: 'Suche',
noResult: 'Inga resultat.',
head: 'Wikipedia på tyska'
}
}
if (window.location.hash.length === 3 && window.location.hash.match(/#[a-z]{2}/g)) {
langSwap(window.location.hash.match(/#([a-z]{2})/g)[0].replace('#',''))
}
});