forked from splitbrain/dokuwiki-plugin-searchjump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
108 lines (91 loc) · 3.03 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
var sjump = {
$found: null,
current: 0,
dialog: null,
/**
* create and display search jump dialog
*/
init: function () {
sjump.$found = jQuery('span.search_hit');
if (!sjump.$found.length) return;
sjump.dialog = document.createElement('div');
sjump.dialog.id = 'search__jump';
var prev = document.createElement('img');
prev.src = DOKU_BASE + 'lib/plugins/searchjump/pix/up.gif';
prev.title = LANG.plugins.searchjump['up']+' [k]';
prev.onclick = function () {
sjump.jump_by(-1);
};
var close = document.createElement('img');
close.src = DOKU_BASE + 'lib/plugins/searchjump/pix/close.gif';
close.title = LANG.plugins.searchjump['close']+' [x]';
close.onclick = sjump.close;
var next = document.createElement('img');
next.src = DOKU_BASE + 'lib/plugins/searchjump/pix/down.gif';
next.title = LANG.plugins.searchjump['down']+' [j]';
next.onclick = function () {
sjump.jump_by(1);
};
sjump.dialog.appendChild(prev);
sjump.dialog.appendChild(close);
sjump.dialog.appendChild(next);
jQuery(document).bind('keypress', sjump.keyhandle);
// for the dokuwiki template:
var $page = jQuery('#dokuwiki__content').find('div.page');
if($page.length) {
jQuery(sjump.dialog).css('left', $page.offset().left + 'px');
}
document.body.appendChild(sjump.dialog);
sjump.jump_by(0);
},
/**
* Jump to the next or previous found
*/
jump_by: function (diff) {
// manage document wraps
sjump.current += diff;
if (sjump.current >= sjump.$found.length) {
alert(LANG.plugins.searchjump['start']);
sjump.current = 0;
}
if (sjump.current < 0) {
alert(LANG.plugins.searchjump['end']);
sjump.current = sjump.$found.length - 1;
}
// move position of dialog
jQuery(sjump.dialog).css('top', jQuery(sjump.$found[sjump.current]).offset().top + 'px');
// jump
sjump.$found[sjump.current].scrollIntoView();
jQuery(sjump.$found[sjump.current]).delay(50).fadeOut().fadeIn();
},
/**
* close dialog and remove highlight
*/
close: function () {
// remove key handler
jQuery(document).unbind('keypress', sjump.keyhandle);
// remove hilighting
sjump.$found.removeClass('search_hit');
// close dialog
document.body.removeChild(sjump.dialog);
},
/**
* Handles keypresses for next prev
*
* @param {Event} e
*/
keyhandle: function (e) {
if (e.keyCode == 106) { // j
sjump.jump_by(1);
} else if (e.keyCode == 107) { // k
sjump.jump_by(-1);
} else if (e.keyCode == 120) { // x
sjump.close();
} else {
return;
}
e.preventDefault();
e.stopPropagation();
}
};
jQuery(sjump.init);