MediaWiki:Common2.js
[http://www.wikizamki.org/links.htm]
(Различия между версиями)
Elden (обсуждение | вклад) |
Elden (обсуждение | вклад) |
||
Строка 223: | Строка 223: | ||
i.style.cursor = 'pointer' | i.style.cursor = 'pointer' | ||
toolbar.appendChild(i) | toolbar.appendChild(i) | ||
+ | |||
+ | |||
+ | |||
+ | var currentFocused; | ||
+ | |||
+ | // this function generates the actual toolbar buttons with localized text | ||
+ | // we use it to avoid creating the toolbar where javascript is not enabled | ||
+ | function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) { | ||
+ | // Don't generate buttons for browsers which don't fully | ||
+ | // support it. | ||
+ | mwEditButtons[mwEditButtons.length] = | ||
+ | {"imageId": imageId, | ||
+ | "imageFile": imageFile, | ||
+ | "speedTip": speedTip, | ||
+ | "tagOpen": tagOpen, | ||
+ | "tagClose": tagClose, | ||
+ | "sampleText": sampleText}; | ||
+ | } | ||
+ | |||
+ | // this function generates the actual toolbar buttons with localized text | ||
+ | // we use it to avoid creating the toolbar where javascript is not enabled | ||
+ | function mwInsertEditButton(parent, item) { | ||
+ | var image = document.createElement("img"); | ||
+ | image.width = 23; | ||
+ | image.height = 22; | ||
+ | image.className = "mw-toolbar-editbutton"; | ||
+ | if (item.imageId) image.id = item.imageId; | ||
+ | image.src = item.imageFile; | ||
+ | image.border = 0; | ||
+ | image.alt = item.speedTip; | ||
+ | image.title = item.speedTip; | ||
+ | image.style.cursor = "pointer"; | ||
+ | image.onclick = function() { | ||
+ | insertTags(item.tagOpen, item.tagClose, item.sampleText); | ||
+ | return false; | ||
+ | }; | ||
+ | |||
+ | parent.appendChild(image); | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | function mwSetupToolbar() { | ||
+ | var toolbar = document.getElementById('toolbar'); | ||
+ | if (!toolbar) { return false; } | ||
+ | |||
+ | var textbox = document.getElementById('wpTextbox1'); | ||
+ | if (!textbox) { return false; } | ||
+ | |||
+ | // Don't generate buttons for browsers which don't fully | ||
+ | // support it. | ||
+ | if (!(document.selection && document.selection.createRange) | ||
+ | && textbox.selectionStart === null) { | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | for (var i = 0; i < mwEditButtons.length; i++) { | ||
+ | mwInsertEditButton(toolbar, mwEditButtons[i]); | ||
+ | } | ||
+ | for (var i = 0; i < mwCustomEditButtons.length; i++) { | ||
+ | mwInsertEditButton(toolbar, mwCustomEditButtons[i]); | ||
+ | } | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | // apply tagOpen/tagClose to selection in textarea, | ||
+ | // use sampleText instead of selection if there is none | ||
+ | function insertTags(tagOpen, tagClose, sampleText) { | ||
+ | var txtarea; | ||
+ | if (document.editform) { | ||
+ | txtarea = currentFocused; | ||
+ | } else { | ||
+ | // some alternate form? take the first one we can find | ||
+ | var areas = document.getElementsByTagName('textarea'); | ||
+ | txtarea = areas[0]; | ||
+ | } | ||
+ | var selText, isSample = false; | ||
+ | |||
+ | if (document.selection && document.selection.createRange) { // IE/Opera | ||
+ | |||
+ | //save window scroll position | ||
+ | if (document.documentElement && document.documentElement.scrollTop) | ||
+ | var winScroll = document.documentElement.scrollTop | ||
+ | else if (document.body) | ||
+ | var winScroll = document.body.scrollTop; | ||
+ | //get current selection | ||
+ | txtarea.focus(); | ||
+ | var range = document.selection.createRange(); | ||
+ | selText = range.text; | ||
+ | //insert tags | ||
+ | checkSelectedText(); | ||
+ | range.text = tagOpen + selText + tagClose; | ||
+ | //mark sample text as selected | ||
+ | if (isSample && range.moveStart) { | ||
+ | if (window.opera) | ||
+ | tagClose = tagClose.replace(/\n/g,''); | ||
+ | range.moveStart('character', - tagClose.length - selText.length); | ||
+ | range.moveEnd('character', - tagClose.length); | ||
+ | } | ||
+ | range.select(); | ||
+ | //restore window scroll position | ||
+ | if (document.documentElement && document.documentElement.scrollTop) | ||
+ | document.documentElement.scrollTop = winScroll | ||
+ | else if (document.body) | ||
+ | document.body.scrollTop = winScroll; | ||
+ | |||
+ | } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla | ||
+ | |||
+ | //save textarea scroll position | ||
+ | var textScroll = txtarea.scrollTop; | ||
+ | //get current selection | ||
+ | txtarea.focus(); | ||
+ | var startPos = txtarea.selectionStart; | ||
+ | var endPos = txtarea.selectionEnd; | ||
+ | selText = txtarea.value.substring(startPos, endPos); | ||
+ | //insert tags | ||
+ | checkSelectedText(); | ||
+ | txtarea.value = txtarea.value.substring(0, startPos) | ||
+ | + tagOpen + selText + tagClose | ||
+ | + txtarea.value.substring(endPos, txtarea.value.length); | ||
+ | //set new selection | ||
+ | if (isSample) { | ||
+ | txtarea.selectionStart = startPos + tagOpen.length; | ||
+ | txtarea.selectionEnd = startPos + tagOpen.length + selText.length; | ||
+ | } else { | ||
+ | txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length; | ||
+ | txtarea.selectionEnd = txtarea.selectionStart; | ||
+ | } | ||
+ | //restore textarea scroll position | ||
+ | txtarea.scrollTop = textScroll; | ||
+ | } | ||
+ | |||
+ | function checkSelectedText(){ | ||
+ | if (!selText) { | ||
+ | selText = sampleText; | ||
+ | isSample = true; | ||
+ | } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char | ||
+ | selText = selText.substring(0, selText.length - 1); | ||
+ | tagClose += ' ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Restore the edit box scroll state following a preview operation, | ||
+ | * and set up a form submission handler to remember this state | ||
+ | */ | ||
+ | function scrollEditBox() { | ||
+ | var editBox = document.getElementById( 'wpTextbox1' ); | ||
+ | var scrollTop = document.getElementById( 'wpScrolltop' ); | ||
+ | var editForm = document.getElementById( 'editform' ); | ||
+ | if( editBox && scrollTop ) { | ||
+ | if( scrollTop.value ) | ||
+ | editBox.scrollTop = scrollTop.value; | ||
+ | addHandler( editForm, 'submit', function() { | ||
+ | document.getElementById( 'wpScrolltop' ).value = document.getElementById( 'wpTextbox1' ).scrollTop; | ||
+ | } ); | ||
+ | } | ||
+ | } | ||
+ | hookEvent( 'load', scrollEditBox ); | ||
+ | hookEvent( 'load', mwSetupToolbar ); | ||
+ | hookEvent( 'load', function() { | ||
+ | if ( document.editform ) { | ||
+ | currentFocused = document.editform.wpTextbox1; | ||
+ | document.editform.wpTextbox1.onfocus = function() { currentFocused = document.editform.wpTextbox1; }; | ||
+ | document.editform.wpSummary.onfocus = function() { currentFocused = document.editform.wpSummary; }; | ||
+ | } | ||
+ | } ); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
} | } | ||
if (wgAction == 'edit' || wgAction == 'submit'){ | if (wgAction == 'edit' || wgAction == 'submit'){ |
Версия 15:46, 21 июля 2009
/* Размещённый здесь JavaScript код будет загружаться всем пользователям при обращении к каждой странице */ //See http://ru.wikipedia.org/wiki/project:code //<source lang=javascript> /* Убираем надпись Заглавная страница */ if (wgPageName=='Заглавная_страница' && document.URL.indexOf('diff=')==-1) document.write('<style> #siteSub, #contentSub, h1.firstHeading { display: none !important; } </style>') importScript_ = importScript importScript = function (page, proj){ if (!proj) importScript_(page) else { if (proj.indexOf('.')==-1) proj += '.wikipedia.org' importScriptURI('http://'+proj+'/w/index.php?action=raw&ctype=text/javascript&title='+encodeURIComponent(page.replace(/ /g,'_'))) } } addLoadEvent = addOnloadHook function ts_parseFloat(n){ if (!n) return 0 n = parseFloat(n.replace(/\./g, '').replace(/,/, '.')) return (isNaN(n) ? 0 : n) } function LinkFA(){ var pLang = document.getElementById('p-lang') if (!pLang) return var iw = pLang.getElementsByTagName('li') for (var i=0; i < iw.length; i++) if (document.getElementById(iw[i].className+'-fa')){ iw[i].className += ' FA' iw[i].title = 'Эта статья является избранной в другом языковом разделе' }else if (document.getElementById(iw[i].className+'-ga')){ iw[i].className += ' GA' iw[i].title = 'Эта статья является хорошей в другом языковом разделе' } } function icqIcons(){ var a, spans = document.getElementById('content').getElementsByTagName('span') for (var i=0; a=spans[i]; i++) if (a.className == 'ICQ') a.style.backgroundImage = "url('http://status.icq.com/online.gif?icq="+a.id+"&img=5&randseed="+Math.floor(Math.random()*10000000)+"')" } function newSectionLink(){ var plus = document.getElementById('ca-addsection') if (!plus) return var custom = document.getElementById('add-custom-section') if (!custom) return plus.firstChild.setAttribute('href', custom.getElementsByTagName('a')[0].href) } function editZeroSection(){ var body = document.getElementById('bodyContent') if (!body) return var h2s = body.getElementsByTagName('H2') var h2 = h2s[0] if (!h2) return if (h2.parentNode.id == 'toctitle') h2 = h2s[1] if (!h2) return var span = h2.firstChild if (!span || span.className != 'editsection') return var zero = span.cloneNode(true) body.insertBefore(zero, body.firstChild) var a = zero.getElementsByTagName('a')[0] if (a.href.indexOf('§ion=T') == -1 ) a.title = a.title.replace(/:.*$/,': 0') else a.title = 'Править секцию: 0' a.setAttribute('href', wgScript + '?title='+encodeURIComponent(wgPageName) + '&action=edit§ion=0') } function mainPage(){ if (wgArticleId == 23 || wgArticleId == 4401){ var li = addPortletLink('p-lang', wgArticlePath.replace(/\$1/, 'Википедия:Список_Википедий'), 'Полный список', 'interwiki-completelist') if (li) li.style.fontWeight = 'bold' var nstab = document.getElementById('ca-nstab-main') || document.getElementById('ca-current') if (nstab && wgUserLanguage == 'ru') nstab.firstChild.firstChild.nodeValue = 'Заглавная' } } //Collapsiblе: [[ВП:СБ]] var NavigationBarShowDefault = 2 var NavigationBarHide = '[скрыть]' var NavigationBarShow = '[показать]' var hasClass = (function (){ var reCache = {} return function (element, className){ return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className) } })() function collapsibleTables(){ var Table, HRow, HCell, btn, a, tblIdx = 0, colTables = [] var allTables = document.getElementsByTagName('table') for (var i=0; Table = allTables[i]; i++){ if (!hasClass(Table, 'collapsible')) continue if (!(HRow=Table.rows[0])) continue if (!(HCell=HRow.getElementsByTagName('th')[0])) continue Table.id = 'collapsibleTable' + tblIdx btn = document.createElement('span') btn.style.cssText = 'float:right; font-weight:normal; font-size:smaller' a = document.createElement('a') a.id = 'collapseButton' + tblIdx a.href = 'javascript:collapseTable(' + tblIdx + ');' a.style.color = HCell.style.color a.appendChild(document.createTextNode(NavigationBarHide)) btn.appendChild(a) HCell.insertBefore(btn, HCell.childNodes[0]) colTables[tblIdx++] = Table } for (var i=0; i < tblIdx; i++) if ((tblIdx > NavigationBarShowDefault && hasClass(colTables[i], 'autocollapse')) || hasClass(colTables[i], 'collapsed')) collapseTable(i) } function collapseTable (idx){ var Table = document.getElementById('collapsibleTable' + idx) var btn = document.getElementById('collapseButton' + idx) if (!Table || !btn) return false var Rows = Table.rows var isShown = (btn.firstChild.data == NavigationBarHide) btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide var disp = isShown ? 'none' : Rows[0].style.display for (var i=1; i < Rows.length; i++) Rows[i].style.display = disp } function collapsibleDivs(){ var navIdx = 0, colNavs = [], i, NavFrame var divs = document.getElementById('content').getElementsByTagName('div') for (i=0; NavFrame = divs[i]; i++) { if (!hasClass(NavFrame, 'NavFrame')) continue NavFrame.id = 'NavFrame' + navIdx var a = document.createElement('a') a.className = 'NavToggle' a.id = 'NavToggle' + navIdx a.href = 'javascript:collapseDiv(' + navIdx + ');' a.appendChild(document.createTextNode(NavigationBarHide)) for (var j=0; j < NavFrame.childNodes.length; j++) if (hasClass(NavFrame.childNodes[j], 'NavHead')) NavFrame.childNodes[j].appendChild(a) colNavs[navIdx++] = NavFrame } for (i=0; i < navIdx; i++) if ((navIdx > NavigationBarShowDefault && !hasClass(colNavs[i], 'expanded')) || hasClass(colNavs[i], 'collapsed')) collapseDiv(i) } function collapseDiv(idx) { var div = document.getElementById('NavFrame' + idx) var btn = document.getElementById('NavToggle' + idx) if (!div || !btn) return false var isShown = (btn.firstChild.data == NavigationBarHide) btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide var disp = isShown ? 'none' : 'block' for (var child = div.firstChild; child != null; child = child.nextSibling) if (hasClass(child, 'NavPic') || hasClass(child, 'NavContent')) child.style.display = disp } function voting8(){ if (votingTrigger = document.getElementById('voting-trigger')) importScriptURI(wgServer+wgScript +'?title=MediaWiki:Voting8.js&action=raw&ctype=text/javascript&cversion=' +encodeURIComponent(votingTrigger.innerHTML.replace(/\D+/g, '.'))) } //Execution if (wgCanonicalNamespace == 'Special'){ switch (wgCanonicalSpecialPageName){ case 'Upload': importScript_('MediaWiki:Upload.js'); break case 'Search': importScript_('MediaWiki:Search.js'); break } }else if (wgAction != 'history'){ addOnloadHook(editZeroSection) addOnloadHook(collapsibleDivs) addOnloadHook(collapsibleTables) addOnloadHook(mainPage) importScript('MediaWiki:Wikiminiatlas.js', 'meta.wikimedia.org') if (navigator.appName=='Microsoft Internet Explorer' && document.createStyleSheet) document.createStyleSheet().addRule('.IPA', 'font-family: "Doulos SIL", "Charis SIL", Gentium, "DejaVu Sans", Code2000, "TITUS Cyberbit Basic", "Arial Unicode MS", "Lucida Sans Unicode", "Chrysanthi Unicode";') if (wgNamespaceNumber==0 || wgNamespaceNumber==100) addOnloadHook(LinkFA) else { addOnloadHook(icqIcons) addOnloadHook(newSectionLink) } if (wgAction=='edit' || wgAction=='submit') importScript_('MediaWiki:Editpage.js') addOnloadHook(voting8) } if (wgUserGroups) for (var i=0; i<wgUserGroups.length; i++) switch (wgUserGroups[i]){ case 'editor': importStylesheet('MediaWiki:Gadget-FlaggedRevs.css'); break case 'sysop': importScript_('MediaWiki:Sysop.js'); break } //Сообщить об ошибке importScript_('MediaWiki:Wikibugs.js') //</source> /* function addWikifButton(){ var toolbar = document.getElementById('toolbar') var textbox = document.getElementById('wpTextbox1') if (!textbox || !toolbar) return var i = document.createElement('img') i.src = 'http://upload.wikimedia.org/wikisource/ru/d/d1/Button-wikifikator.png' i.alt = i.title = 'Викификатор' i.onclick = Wikify i.style.cursor = 'pointer' toolbar.appendChild(i) var currentFocused; // this function generates the actual toolbar buttons with localized text // we use it to avoid creating the toolbar where javascript is not enabled function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) { // Don't generate buttons for browsers which don't fully // support it. mwEditButtons[mwEditButtons.length] = {"imageId": imageId, "imageFile": imageFile, "speedTip": speedTip, "tagOpen": tagOpen, "tagClose": tagClose, "sampleText": sampleText}; } // this function generates the actual toolbar buttons with localized text // we use it to avoid creating the toolbar where javascript is not enabled function mwInsertEditButton(parent, item) { var image = document.createElement("img"); image.width = 23; image.height = 22; image.className = "mw-toolbar-editbutton"; if (item.imageId) image.id = item.imageId; image.src = item.imageFile; image.border = 0; image.alt = item.speedTip; image.title = item.speedTip; image.style.cursor = "pointer"; image.onclick = function() { insertTags(item.tagOpen, item.tagClose, item.sampleText); return false; }; parent.appendChild(image); return true; } function mwSetupToolbar() { var toolbar = document.getElementById('toolbar'); if (!toolbar) { return false; } var textbox = document.getElementById('wpTextbox1'); if (!textbox) { return false; } // Don't generate buttons for browsers which don't fully // support it. if (!(document.selection && document.selection.createRange) && textbox.selectionStart === null) { return false; } for (var i = 0; i < mwEditButtons.length; i++) { mwInsertEditButton(toolbar, mwEditButtons[i]); } for (var i = 0; i < mwCustomEditButtons.length; i++) { mwInsertEditButton(toolbar, mwCustomEditButtons[i]); } return true; } // apply tagOpen/tagClose to selection in textarea, // use sampleText instead of selection if there is none function insertTags(tagOpen, tagClose, sampleText) { var txtarea; if (document.editform) { txtarea = currentFocused; } else { // some alternate form? take the first one we can find var areas = document.getElementsByTagName('textarea'); txtarea = areas[0]; } var selText, isSample = false; if (document.selection && document.selection.createRange) { // IE/Opera //save window scroll position if (document.documentElement && document.documentElement.scrollTop) var winScroll = document.documentElement.scrollTop else if (document.body) var winScroll = document.body.scrollTop; //get current selection txtarea.focus(); var range = document.selection.createRange(); selText = range.text; //insert tags checkSelectedText(); range.text = tagOpen + selText + tagClose; //mark sample text as selected if (isSample && range.moveStart) { if (window.opera) tagClose = tagClose.replace(/\n/g,''); range.moveStart('character', - tagClose.length - selText.length); range.moveEnd('character', - tagClose.length); } range.select(); //restore window scroll position if (document.documentElement && document.documentElement.scrollTop) document.documentElement.scrollTop = winScroll else if (document.body) document.body.scrollTop = winScroll; } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla //save textarea scroll position var textScroll = txtarea.scrollTop; //get current selection txtarea.focus(); var startPos = txtarea.selectionStart; var endPos = txtarea.selectionEnd; selText = txtarea.value.substring(startPos, endPos); //insert tags checkSelectedText(); txtarea.value = txtarea.value.substring(0, startPos) + tagOpen + selText + tagClose + txtarea.value.substring(endPos, txtarea.value.length); //set new selection if (isSample) { txtarea.selectionStart = startPos + tagOpen.length; txtarea.selectionEnd = startPos + tagOpen.length + selText.length; } else { txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length; txtarea.selectionEnd = txtarea.selectionStart; } //restore textarea scroll position txtarea.scrollTop = textScroll; } function checkSelectedText(){ if (!selText) { selText = sampleText; isSample = true; } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char selText = selText.substring(0, selText.length - 1); tagClose += ' ' } } } /** * Restore the edit box scroll state following a preview operation, * and set up a form submission handler to remember this state */ function scrollEditBox() { var editBox = document.getElementById( 'wpTextbox1' ); var scrollTop = document.getElementById( 'wpScrolltop' ); var editForm = document.getElementById( 'editform' ); if( editBox && scrollTop ) { if( scrollTop.value ) editBox.scrollTop = scrollTop.value; addHandler( editForm, 'submit', function() { document.getElementById( 'wpScrolltop' ).value = document.getElementById( 'wpTextbox1' ).scrollTop; } ); } } hookEvent( 'load', scrollEditBox ); hookEvent( 'load', mwSetupToolbar ); hookEvent( 'load', function() { if ( document.editform ) { currentFocused = document.editform.wpTextbox1; document.editform.wpTextbox1.onfocus = function() { currentFocused = document.editform.wpTextbox1; }; document.editform.wpSummary.onfocus = function() { currentFocused = document.editform.wpSummary; }; } } ); } if (wgAction == 'edit' || wgAction == 'submit'){ document.write('<script type="text/javascript" src="http://ru.wikipedia.org/w/index.php?title=MediaWiki:Wikificator.js&action=raw&ctype=text/javascript"><\/script>') addOnloadHook(addWikifButton) } */