Материал из ЖД cправочник
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 13:54, 28 июня 2011
function WLScript(){
var whenPageLoaded = +(new Date()) - 20000 //add 20 sec just in case
var namespace, $content, mainTab, hideInterfaceCSS
var alreadySorted, alreadyAddedUnwatch, isEnhanced, mm
var inProgress = null, timeoutID = null
mm = {
sortTip:'Сортировать страницы по пространствам',
sortDone:'Изменения уже отсортированы',
unwatchTip:'Добавить (x) ссылки для вычёркивания страниц из списка наблюдения',
unwatchDone:'Для удаления страниц из списка наблюдения используйте появившиеся ссылки (x)',
onlynew:'Только новые',
onlynewTip:'Показать изменения с момента загрузки этой страницы',
expandAll:'Показать/спрятать все свёрнутые правки',
fullPage:'Спрятать/показать элементы интерфейса'
}
namespace = document.getElementById('namespace')
if (!namespace) return
$content = $('#bodyContent')
if (!$content.length){
$content = $('#mw_content, #article').eq(0)
if (!$content.length) $content = $(document.body) // covered all the skin but who knows...
}
//enhanced RC: make (x) appear by clicking on timestamp
isEnhanced = $content.find('ul.special').length == 0
if (isEnhanced) $content.click(toggleXLink)
//find insertion points for links: after "days all"
var linksAt = $('#mw-watchlist-options').find('a[href*=&days=7]').next().next()
if (!linksAt.length) linksAt = $('#mw-watchlist-options').find('hr').last()
//find "Special" tab
mainTab = $('#ca-special, #ca-nstab-special').eq(0)
//"only new" link and tab
addLnk(mm.onlynew, mm.onlynewTip).mousedown(onlyNewEntries).attr('id', 'listSince')
addTab(mm.onlynew, mm.onlynewTip).mousedown(onlyNewEntries)
//"unwatch" link(s)
if (window.unwatchLinksOnLoad) addXLinks()
else addLnk('x' , mm.unwatchTip).click(addXLinks)
//"sort" link
addLnk('↑↓', mm.sortTip).click(sortWatchlist)
//"expand all" link
if ($('#mw-rc-openarrow-0').length) addLnk('±', mm.expandAll).click(expandMultipleEdits)
//"hideInterface" tab
addTab('↸',mm.fullPage).click(hideInterface).attr('href','#')
if (document.cookie.indexOf('wlmax=1') != -1) hideInterface()
function addLnk(txt, tip){
linksAt.before(' | ')
return $('<a href=# title="'+tip+'" style="font-style:italic">'+txt+'</a>').insertBefore(linksAt)
}
function addTab(txt, tip){
var tab = mainTab.clone(true).removeClass('selected').attr('id','')
tab.find('a').text(txt).attr('title', tip).attr('accesskey','')
return tab.appendTo(mainTab.parent())
}
return
function onlyNewEntries(e) {
var url = window.location.href.split('#')[0]
var days = ( +(new Date()) - whenPageLoaded)/(1000 * 3600 * 24)
e.target.href = /[?&]days=/.test(url)
? url.replace(/([?&]days=)[^&]*/, '$1'+days)
: url + (url.indexOf('?') < 0 ? '?':'&') + 'days=' + days
return true
}
function sortWatchlist(e){
e.preventDefault()
if (alreadySorted) return alert(mm.sortDone)
$content.find('h4').each(function(i, H4){ //sort all days separately
sortDay($(H4).next('div, ul'))
})
alreadySorted = true
}
function sortDay(dayDiv){
var i, pgname, rowElem, hiddenDiv, rows = dayDiv.find('a[href*="&action=history"]')
for (i = 0; i < rows.length; i++){
pgname = getTitleFromURL(rows[i].href)
ns = getNSFromTitle(pgname)
if (ns>0) pgname = pgname.substring(getNamespace(ns).length+1) //title w/o prefix
if (ns%2) ns-- //sort talk page as if it was a base page
rows[i].sortkey = zzz(ns) + ':' + pgname //assign custom tag attribute: namespace+title
}
//sort rows array
rows.sort(function(a,b){
if (a.sortkey > b.sortkey) return 1
else if (a.sortkey < b.sortkey) return -1
else return 0
})
//sort rows in HTML, by moving all to the bottom
if (isEnhanced)
for (i=0; i<rows.length; i++){
rowElem = rows.eq(i).closest('table')
hiddenDiv = rowElem.next('div')
dayDiv.append(rowElem, hiddenDiv)
}
else
for (i=0; i<rows.length; i++){
rowElem = rows.eq(i).closest('li')
dayDiv.append(rowElem)
}
}
function expandMultipleEdits(e){
e.preventDefault()
var i = 0, sp, state = $('#mw-rc-openarrow-0')[0].style.display
while (sp=document.getElementById('mw-rc-openarrow-'+(i++).toString()))
if (sp.style.display == state) $(sp.firstChild).click()
}
function addXLinks(e){
if (e) e.preventDefault()
if (alreadyAddedUnwatch) return alert(mm.unwatchDone)
$content.find('a[href*="&action=history"]').each(function(i, lnk){ addXLink(lnk) })
alreadyAddedUnwatch = true
}
function addXLink(lnk){
var x = $('<a class=aj-unwatch style="font-size:smaller" title="'+mw.msg('unwatch')+'" />')
.attr('href', lnk.href.replace(/&curid=\d+/,'').replace(/action=history/,'action=unwatch'))
.click(ajaxUnwatch)
lnk = $(lnk)
if (isEnhanced) lnk.parent().prepend( x.append('(x)'), ' ' )
else lnk.after( ' | ', x.append('x') )
}
function toggleXLink(e){ //add (x) when clicking on timestamp
if ( ! $(e.target).filter('td.mw-enhanced-rc').length ) return
var tbl = $(e.target).parents('table.mw-enhanced-rc')
var x = tbl.find('a.aj-unwatch')
if (x.length) x.remove()
else addXLink(tbl.find('a[href*="&action=history"]')[0])
}
function ajaxUnwatch(e) {
if (inProgress) return false
inProgress = getTitleFromURL(this.href)
timeoutID = window.setTimeout( function() {inProgress = null}, 10000 )
//call server
var req = { action:'watch', format:'json', title:inProgress }
if (/&action=unwatch/.test(this.href)) req.unwatch = ''
$.getJSON( '/w/api.php', req, showUnwatch)
return false
}
function showUnwatch (response) {
if (timeoutID) window.clearTimeout(timeoutID)
response = response.watch
var name = inProgress, state, pg
inProgress = null
if (response.watched !== undefined) state = false
else if (response.unwatched !== undefined) state = true
else return //unrecognized response
//find the full name of "other page"
var ns = getNSFromTitle(name)
var name2 = name
if (ns > 0) name2 = name2.substring(getNamespace(ns).length+1) //remove old prefix
if (ns % 2) ns--; else ns++ //switch to "other" namespace
if (ns > 0) name2 = getNamespace(ns) + ':' + name2 //add new prefix
//now mark all rows that are either name or name2
$content.find('a.aj-unwatch').each(function(i, lnk){
pg = getTitleFromURL(lnk.href)
if (pg != name && pg != name2) return
lnk.title = mw.msg(state?'watch':'unwatch')
$(lnk).parent().find('a').css('text-decoration', state ? 'line-through' : '')
lnk.href = lnk.href.replace(/&action=\w+/, '&action='+ (state?'watch':'unwatch'))
})
}
function hideInterface(e){
if (e) e.preventDefault()
if (!hideInterfaceCSS) hideInterfaceCSS = mw.util.addCSS('\
div#siteNotice, h1#firstHeading, #siteSub, #contentSub, fieldset#mw-watchlist-options,\
div.mw-rc-label-legend, #mw-fr-watchlist-pending-notice {display:none}')
else hideInterfaceCSS.disabled = !hideInterfaceCSS.disabled
document.cookie = 'wlmax=' + (!hideInterfaceCSS.disabled ? '1' : '0;expires=' + (new Date()).toGMTString() + ';;')
var a = mainTab.find('a') //replace "Special" tab text with "Watchlist"
if (hideInterfaceCSS.disabled){ //restore
a.text( a.attr('oldtext') )
}else{//set to "watchlist"
a.attr('oldtext', a.text())
a.text($('h1#firstHeading').text())
}
}
//common functions; need 'namespace' select element
function getNSFromTitle(title){ //returns namespace number
var i = title.indexOf(':')
if (i == -1) return 0
var prefix = title.substring(0,i+1) //including ':'
for (i=2; i < namespace.options.length; i++)
if (namespace.options[i].text+':' == prefix)
return i-1
return 0 // ':' was just a part of the title
}
function getNamespace(ns){ //returns namespace name
if (ns==0) return ''
else return namespace.options[ns+1].text
}
function getTitleFromURL (url){ //gets 'title=' part from a link
var ma = url.match(/(&|\?)title=([^&]+)/)
if (ma) return decodeURIComponent(ma[2]).replace(/_/g,' ')
else return ''
}
function zzz(s){ // 5 -> 005
s = s.toString()
if (s.length==1) return '00'+s
else if (s.length==2) return '0'+s
else return s
}
}
if (wgCanonicalSpecialPageName == 'Watchlist' && wgAction == 'view') WLScript()
