1var sjump = { 2 $found: null, 3 current: 0, 4 dialog: null, 5 6 /** 7 * create and display search jump dialog 8 */ 9 init: function () { 10 sjump.$found = jQuery('span.mark'); 11 if (!sjump.$found.length) return; 12 13 14 sjump.dialog = document.createElement('div'); 15 sjump.dialog.id = 'search__jump'; 16 17 var prev = document.createElement('img'); 18 prev.src = DOKU_BASE + 'lib/plugins/searchjump/pix/up.gif'; 19 prev.title = LANG.plugins.searchjump['up']+' [k]'; 20 prev.onclick = function () { 21 sjump.jump_by(-1); 22 }; 23 24 var close = document.createElement('img'); 25 close.src = DOKU_BASE + 'lib/plugins/searchjump/pix/close.gif'; 26 close.title = LANG.plugins.searchjump['close']+' [x]'; 27 close.onclick = sjump.close; 28 29 var next = document.createElement('img'); 30 next.src = DOKU_BASE + 'lib/plugins/searchjump/pix/down.gif'; 31 next.title = LANG.plugins.searchjump['down']+' [j]'; 32 next.onclick = function () { 33 sjump.jump_by(1); 34 }; 35 36 sjump.dialog.appendChild(prev); 37 sjump.dialog.appendChild(close); 38 sjump.dialog.appendChild(next); 39 40 jQuery(document).bind('keypress', sjump.keyhandle); 41 42 // for the dokuwiki template: 43 var $page = jQuery('#dokuwiki__content').find('div.page'); 44 if($page.length) { 45 jQuery(sjump.dialog).css('left', $page.offset().left + 'px'); 46 } 47 48 document.body.appendChild(sjump.dialog); 49 sjump.jump_by(0); 50 }, 51 52 /** 53 * Jump to the next or previous found 54 */ 55 jump_by: function (diff) { 56 // manage document wraps 57 sjump.current += diff; 58 if (sjump.current >= sjump.$found.length) { 59 alert(LANG.plugins.searchjump['start']); 60 sjump.current = 0; 61 } 62 if (sjump.current < 0) { 63 alert(LANG.plugins.searchjump['end']); 64 sjump.current = sjump.$found.length - 1; 65 } 66 67 // move position of dialog 68 jQuery(sjump.dialog).css('top', jQuery(sjump.$found[sjump.current]).offset().top + 'px'); 69 // jump 70 sjump.$found[sjump.current].scrollIntoView(); 71 jQuery(sjump.$found[sjump.current]).delay(50).fadeOut().fadeIn(); 72 }, 73 74 /** 75 * close dialog and remove highlight 76 */ 77 close: function () { 78 // remove key handler 79 jQuery(document).unbind('keypress', sjump.keyhandle); 80 81 // remove hilighting 82 sjump.$found.removeClass('search_hit'); 83 84 // close dialog 85 document.body.removeChild(sjump.dialog); 86 }, 87 88 /** 89 * Handles keypresses for next prev 90 * 91 * @param {Event} e 92 */ 93 keyhandle: function (e) { 94 if (e.keyCode == 106) { // j 95 sjump.jump_by(1); 96 } else if (e.keyCode == 107) { // k 97 sjump.jump_by(-1); 98 } else if (e.keyCode == 120) { // x 99 sjump.close(); 100 } else { 101 return; 102 } 103 e.preventDefault(); 104 e.stopPropagation(); 105 } 106}; 107 108jQuery(sjump.init); 109