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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/**
* The Link Wizard
*
* @author Andreas Gohr <gohr@cosmocode.de>
*/
var linkwiz = {
wiz: null,
entry: null,
result: null,
timer: null,
sack: null,
textArea: null,
selected: -1,
selection: null,
/**
* Initialize the linkwizard by creating the needed HTML
* and attaching the eventhandlers
*/
init: function(textArea){
// prepare AJAX object
linkwiz.sack = new sack(DOKU_BASE + 'lib/exe/ajax.php');
linkwiz.sack.AjaxFailedAlert = '';
linkwiz.sack.encodeURIString = false;
// create HTML Structure
linkwiz.wiz = document.createElement('div');
linkwiz.wiz.id = 'link__wiz';
linkwiz.wiz.className = 'picker';
linkwiz.wiz.style.top = (findPosY(textArea)+20)+'px';
linkwiz.wiz.style.left = (findPosX(textArea)+80)+'px';
linkwiz.wiz.style.marginLeft = '-10000px';
linkwiz.wiz.innerHTML =
'<div id="link__wiz_header">'+
'<img src="'+DOKU_BASE+'lib/images/close.png" width="16" height="16" align="right" alt="" id="link__wiz_close" />'+
LANG['linkwiz']+'</div>'+
'<div>'+LANG['linkto']+' <input type="text" class="edit" id="link__wiz_entry" autocomplete="off" /></div>'+
'<div id="link__wiz_result"></div>';
textArea.form.parentNode.appendChild(linkwiz.wiz);
linkwiz.textArea = textArea;
linkwiz.result = $('link__wiz_result');
linkwiz.entry = $('link__wiz_entry');
// attach event handlers
var obj;
obj = $('link__wiz_close');
obj.onclick = linkwiz.hide;
linkwiz.sack.elementObj = linkwiz.result;
addEvent(linkwiz.entry,'keyup',linkwiz.onEntry);
addEvent(linkwiz.result,'click',linkwiz.onResultClick);
drag.attach(linkwiz.wiz,$('link__wiz_header'));
},
/**
* handle all keyup events in the entry field
*/
onEntry: function(e){
if(e.keyCode == 37 || e.keyCode == 39){ //left/right
return true; //ignore
}
if(e.keyCode == 27){
linkwiz.hide();
e.preventDefault();
e.stopPropagation();
return false;
}
if(e.keyCode == 38){ //Up
linkwiz.select(linkwiz.selected -1);
e.preventDefault();
e.stopPropagation();
return false;
}
if(e.keyCode == 40){ //Down
linkwiz.select(linkwiz.selected +1);
e.preventDefault();
e.stopPropagation();
return false;
}
if(e.keyCode == 13){ //Enter
if(linkwiz.selected > -1){
var obj = linkwiz.getResult(linkwiz.selected);
if(obj){
var a = obj.getElementsByTagName('A')[0];
linkwiz.resultClick(a);
}
}else if(linkwiz.entry.value){
linkwiz.insertLink(linkwiz.entry.value);
}
e.preventDefault();
e.stopPropagation();
return false;
}
linkwiz.autocomplete();
},
/**
* Get one of the result by index
*
* @param int result div to return
* @returns DOMObject or null
*/
getResult: function(num){
var obj;
var childs = linkwiz.result.getElementsByTagName('DIV');
obj = childs[num];
if(obj){
return obj;
}else{
return null;
}
},
/**
* Select the given result
*/
select: function(num){
if(num < 0){
linkwiz.deselect();
return;
}
var obj = linkwiz.getResult(num);
if(obj){
linkwiz.deselect();
obj.className += ' selected';
// make sure the item is viewable in the scroll view
// FIXME check IE compatibility
if(obj.offsetTop > linkwiz.result.scrollTop + linkwiz.result.clientHeight){
linkwiz.result.scrollTop += obj.clientHeight;
}else if(obj.offsetTop - linkwiz.result.clientHeight < linkwiz.result.scrollTop){ // this works but isn't quite right, fixes welcome
linkwiz.result.scrollTop -= obj.clientHeight;
}
// now recheck - if still not in view, the user used the mouse to scroll
if( (obj.offsetTop > linkwiz.result.scrollTop + linkwiz.result.clientHeight) ||
(obj.offsetTop < linkwiz.result.scrollTop) ){
obj.scrollIntoView();
}
linkwiz.selected = num;
}
},
/**
* deselect a result if any is selected
*/
deselect: function(){
if(linkwiz.selected > -1){
var obj = linkwiz.getResult(linkwiz.selected);
if(obj){
obj.className = obj.className.replace(/ ?selected/,'');
}
}
linkwiz.selected = -1;
},
/**
* Handle clicks in the result set an dispatch them to
* resultClick()
*/
onResultClick: function(e){
if(e.target.tagName != 'A') return;
e.stopPropagation();
e.preventDefault();
linkwiz.resultClick(e.target);
return false;
},
/**
* Handles the "click" on a given result anchor
*/
resultClick: function(a){
var id = a.title;
if(id == '' || id.substr(id.length-1) == ':'){
linkwiz.entry.value = id;
linkwiz.autocomplete_exec();
}else{
linkwiz.entry.value = id;
if(a.nextSibling && a.nextSibling.tagName == 'SPAN'){
linkwiz.insertLink(a.nextSibling.innerHTML);
}else{
linkwiz.insertLink('');
}
}
},
/**
* Insert the id currently in the entry box to the textarea,
* replacing the current selection or at the curso postion.
* When no selection is available the given title will be used
* as link title instead
*/
insertLink: function(title){
if(!linkwiz.entry.value) return;
var sel = getSelection(linkwiz.textArea);
if(sel.start == 0 && sel.end == 0) sel = linkwiz.selection;
var stxt = sel.getText();
if(!stxt && !DOKU_UHC) stxt=title;
// prepend colon inside namespaces for non namespace pages
if(linkwiz.textArea.form['id'].value.indexOf(':') != -1 &&
linkwiz.entry.value.indexOf(':') == -1){
linkwiz.entry.value = ':'+linkwiz.entry.value;
}
var link = '[['+linkwiz.entry.value+'|';
if(stxt) link += stxt;
link += ']]';
var so = linkwiz.entry.value.length+3;
var eo = 2;
pasteText(sel,link,{startofs: so, endofs: eo});
linkwiz.hide();
},
/**
* Start the page/namespace lookup timer
*
* Calls autocomplete_exec when the timer runs out
*/
autocomplete: function(){
if(linkwiz.timer !== null){
window.clearTimeout(linkwiz.timer);
linkwiz.timer = null;
}
linkwiz.timer = window.setTimeout(linkwiz.autocomplete_exec,350);
},
/**
* Executes the AJAX call for the page/namespace lookup
*/
autocomplete_exec: function(){
linkwiz.deselect();
linkwiz.result.innerHTML = '<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="" width="16" height="16" />';
linkwiz.sack.runAJAX('call=linkwiz&q='+encodeURI(linkwiz.entry.value));
},
/**
* Clears the result area
*/
clear: function(){
linkwiz.result.innerHTML = 'Search for a matching page name above, or browse through the pages on the right';
linkwiz.entry.value = '';
},
/**
* Show the linkwizard
*/
show: function(){
linkwiz.selection = getSelection(linkwiz.textArea);
linkwiz.wiz.style.marginLeft = '0px';
linkwiz.entry.focus();
linkwiz.autocomplete();
},
/**
* Hide the link wizard
*/
hide: function(){
linkwiz.wiz.style.marginLeft = '-10000px';
linkwiz.textArea.focus();
},
/**
* Toggle the link wizard
*/
toggle: function(){
if(linkwiz.wiz.style.marginLeft == '-10000px'){
linkwiz.show();
}else{
linkwiz.hide();
}
}
};
|