From ca95603d4f4789072757851cb06a07811bc20d1d Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 18 May 2008 18:33:38 +0000 Subject: [PATCH] Cut a few more kb out of wikibits.js... broke edit-page specific things (toolbar, edit box scrolling, insertTags) out to edit.js --- includes/EditPage.php | 2 + includes/SpecialUpload.php | 6 +- skins/common/edit.js | 156 ++++++++++++++++++++++++++++++++++++ skins/common/wikibits.js | 158 ------------------------------------- 4 files changed, 161 insertions(+), 161 deletions(-) create mode 100644 skins/common/edit.js diff --git a/includes/EditPage.php b/includes/EditPage.php index 684ed661b8..04452a5803 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -378,6 +378,8 @@ class EditPage { wfProfileOut( __METHOD__ ); return; } + + $wgOut->addScriptFile( 'edit.js' ); if( wfReadOnly() ) { $this->readOnlyPage( $this->getContent() ); diff --git a/includes/SpecialUpload.php b/includes/SpecialUpload.php index e00030bde6..27cc2e17d6 100644 --- a/includes/SpecialUpload.php +++ b/includes/SpecialUpload.php @@ -911,9 +911,9 @@ class UploadForm { wgAjaxUploadDestCheck = {$adc}; wgAjaxLicensePreview = {$alp}; wgUploadAutoFill = {$autofill}; - - - " ); +" ); + $wgOut->addScriptFile( 'upload.js' ); + $wgOut->addScriptFile( 'edit.js' ); // For support if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) ) { diff --git a/skins/common/edit.js b/skins/common/edit.js new file mode 100644 index 0000000000..945059e07b --- /dev/null +++ b/skins/common/edit.js @@ -0,0 +1,156 @@ +// 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 = document.editform.wpTextbox1; + } 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 ); diff --git a/skins/common/wikibits.js b/skins/common/wikibits.js index 812710b73d..3368c7335c 100644 --- a/skins/common/wikibits.js +++ b/skins/common/wikibits.js @@ -135,65 +135,6 @@ function toggleToc() { var mwEditButtons = []; var mwCustomEditButtons = []; // eg to add in MediaWiki:Common.js -// 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; -} - function escapeQuotes(text) { var re = new RegExp("'","g"); text = text.replace(re,"\\'"); @@ -214,85 +155,6 @@ function escapeQuotesHTML(text) { return text; } -// 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 = document.editform.wpTextbox1; - } 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 += ' ' - } - } - -} - /** * Set the accesskey prefix based on browser detection. @@ -535,24 +397,6 @@ function toggle_element_check(ida,idb) { document.getElementById(idb).checked=false; } -/** - * 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 ); - /* Written by Jonathan Snook, http://www.snook.ca/jonathan Add-ons by Robert Nyman, http://www.robertnyman.com @@ -966,7 +810,6 @@ function runOnloadHook() { updateTooltipAccessKeys( null ); akeytt( null ); - scrollEditBox(); setupCheckboxShiftClick(); sortables_init(); @@ -1003,4 +846,3 @@ function addClickHandler( element, handler ) { //note: all skins should call runOnloadHook() at the end of html output, // so the below should be redundant. It's there just in case. hookEvent("load", runOnloadHook); -hookEvent("load", mwSetupToolbar); -- 2.20.1