Content deleted Content added
LinguistAtLarge (talk | contribs) fix |
LinguistAtLarge (talk | contribs) tweak |
||
Line 51: | Line 51: | ||
if (is_afd_closed(the_anchor)) |
if (is_afd_closed(the_anchor)) |
||
{ |
{ |
||
search_elements[i].innerHTML = search_elements[i].innerHTML + ' |
search_elements[i].innerHTML = search_elements[i].innerHTML + ' <em>[closed]</em>'; |
||
} |
} |
||
} |
} |
Revision as of 23:38, 6 April 2009
// ###################################################################
// add some keyboard shortcuts on AfD pages
addOnloadHook(function()
{
var debug = true;
var loc = window.location.href;
if (loc.indexOf('Wikipedia:Articles_for_deletion') == -1 && loc.indexOf('User:LinguistAtLarge/Today\'s_AfD') == -1)
{
return;
}
if (debug) { alert('Start: adding AfD keyboard shortcuts'); }
// see if an afd is closed by passing the a name for it
var is_afd_closed = function(afd_anchor)
{
var el = document.getElementById(afd_anchor);
var parent_class = '';
if (el && el.parentNode)
{
parent_class = el.parentNode.getAttribute('class');
}
if (parent_class && parent_class.indexOf('xfd-closed') != -1)
{
return true;
}
return false;
}
// get an anchor for each afd
var afd_anchors = (function()
{
var base_element = window.document.getElementById('toc');
var search_elements = base_element.getElementsByTagName('a');
var len = search_elements.length;
var afd_ids = [];
var href = '';
var pos = -1;
var the_anchor = '';
for(var i = 0; i < len; ++i)
{
href = search_elements[i].href;
pos = href.indexOf('#');
if (pos != -1)
{
the_anchor = href.substr(pos + 1);
afd_ids.push(the_anchor);
if (is_afd_closed(the_anchor))
{
search_elements[i].innerHTML = search_elements[i].innerHTML + ' <em>[closed]</em>';
}
}
}
return afd_ids;
})();
if (debug) { alert('AfD shortcut anchors: ' + afd_anchors); }
// jump to an afd anchor
var jumptoanchor = function(anchor)
{
var el = document.getElementById(anchor);
// if the afd is closed, scroll to the top of the div, instead of the a name (only applies to scrollIntoView)
if (is_afd_closed(anchor))
{
el = el.parentNode;
}
if (el && el.scrollIntoView)
{
el.scrollIntoView();
}
else
{
window.location.hash = '#' + anchor;
}
}
// handle keydown events
var onkeydown_handler = function(e)
{
var e = e || window.event;
var key = e.which || e.keyCode || e.charCode || null;
var target = (e.srcElement || e.target || null);
var shifted = (e.shiftKey || (e.modifiers && (e.modifiers & 4)));
var ctrled = (e.ctrlKey || (e.modifiers && (e.modifiers & 2)));
var tag_name = target.tagName.toLowerCase();
var inputting = (tag_name == 'textarea' || tag_name == 'input' || tag_name == 'select') ? true : false;
if (shifted || ctrled || inputting)
{
return;
}
if (typeof this.curr_afd_anchor_index == 'undefined')
{
this.last_afd_anchor_index = afd_anchors.length - 1;
// set this to the fragment if it exists on page load
this.curr_afd_anchor_index = -1;
var curr_afd_anchor = '';
if (window.location.hash)
{
curr_afd_anchor = window.location.hash.substring(1);
}
var len = afd_anchors.length;
for (var i = 0; i < len; ++i)
{
if (afd_anchors[i] == curr_afd_anchor)
{
this.curr_afd_anchor_index = i;
break;
}
}
}
switch (key)
{
// first afd 'f'
case 70:
{
if (this.curr_afd_anchor_index != 0)
{
this.curr_afd_anchor_index = 0;
jumptoanchor(afd_anchors[0]);
}
break;
}
// last afd 'l'
case 76:
{
if (this.curr_afd_anchor_index != this.last_afd_anchor_index)
{
this.curr_afd_anchor_index = this.last_afd_anchor_index;
jumptoanchor(afd_anchors[this.last_afd_anchor_index]);
}
break;
}
// next afd 'n'
case 78:
{
if (this.curr_afd_anchor_index < this.last_afd_anchor_index)
{
++this.curr_afd_anchor_index;
jumptoanchor(afd_anchors[this.curr_afd_anchor_index]);
}
break;
}
// previous afd 'p'
case 80:
{
if (this.curr_afd_anchor_index > 0)
{
--this.curr_afd_anchor_index;
jumptoanchor(afd_anchors[this.curr_afd_anchor_index]);
}
break;
}
}
return true;
}
// set up the keydown handler
if (debug) { alert('Adding keydown handler'); }
addHandler(document, 'keydown', onkeydown_handler);
// add shortcut keys reminder at the bottom of the page
if (debug) { alert('Adding shortcut keys reminder at bottom of page'); }
var newel = document.createElement('div');
newel.innerHTML = 'Shortcut keys: <b>f</b> - first afd, <b>n</b> - next afd, <b>p</b> - previous afd, <b>l</b> - last afd';
newel.setAttribute('class', 'catlinks');
document.getElementById('bodyContent').appendChild(newel);
if (debug) { alert('End: adding AfD keyboard shortcuts'); }
});