Dateigrösse: 5.87 kb
1 // this function generates the actual toolbar buttons with localized text 2 // we use it to avoid creating the toolbar where javascript is not enabled 3 var x = 0; 4 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) { 5 // Don't generate buttons for browsers 6 // which don't fully support it. 7 mwEditButtons[mwEditButtons.length] = 8 {"imageId": imageId, 9 "imageFile": imageFile, 10 "speedTip": speedTip, 11 "tagOpen": tagOpen, 12 "tagClose": tagClose, 13 "sampleText": sampleText 14 }; 15 var wahr = typeof mwEditButtons[x]; 16 //alert(mwEditButtons[x].speedTip + "\n\n" + mwEditButtons.length + "\n\n" + wahr); 17 x++; // ++x 18 } 19 20 // this function generates the actual toolbar buttons with localized text 21 // we use it to avoid creating the toolbar where javascript is not enabled 22 function mwInsertEditButton(parent, item) { 23 var image = document.createElement("img"); 24 image.width = 23; 25 image.height = 22; 26 image.className = "mw-toolbar-editbutton"; 27 if (item.imageId) image.id = item.imageId; 28 image.src = item.imageFile; 29 image.border = 0; 30 image.alt = item.speedTip; 31 image.title = item.speedTip; 32 image.style.cursor = "pointer"; 33 image.onclick = function() { 34 insertTags(item.tagOpen, item.tagClose, item.sampleText); 35 return false; 36 }; 37 38 parent.appendChild(image); 39 return true; 40 } 41 42 function mwSetupToolbar() { 43 var toolbar = document.getElementById('toolbar'); 44 if (!toolbar) { return false; } 45 46 var textbox = document.getElementById('wpTextbox1'); 47 if (!textbox) { return false; } 48 49 // Don't generate buttons for browsers which don't fully 50 // support it. 51 if (!(document.selection && document.selection.createRange) 52 && textbox.selectionStart === null) { 53 return false; 54 } 55 56 for (var i = 0; i < mwEditButtons.length; i++) { 57 mwInsertEditButton(toolbar, mwEditButtons[i]); 58 } 59 /*** mwCustomEditButtons[] wird nicht verwendet ***/ 60 for (var i = 0; i < mwCustomEditButtons.length; i++) { 61 mwInsertEditButton(toolbar, mwCustomEditButtons[i]); 62 } 63 // Zeile 62: Mike's Eintrag, um Icon-Leiste per onclick zu aktivieren. 64 document.getElementById("ab").style.display="none"; 65 return true; 66 } 67 68 // apply tagOpen/tagClose to selection in textarea, 69 // use sampleText instead of selection if there is none 70 function insertTags(tagOpen, tagClose, sampleText) { 71 var txtarea; 72 if (document.editform) { 73 txtarea = document.editform.wpTextbox1; 74 } else { 75 // some alternate form? take the first one we can find 76 var areas = document.getElementsByTagName('textarea'); 77 txtarea = areas[0]; 78 } 79 var selText, isSample = false; 80 81 if (document.selection && document.selection.createRange) { // IE/Opera 82 83 //save window scroll position 84 if (document.documentElement && document.documentElement.scrollTop) 85 var winScroll = document.documentElement.scrollTop 86 else if (document.body) 87 var winScroll = document.body.scrollTop; 88 //get current selection 89 txtarea.focus(); 90 var range = document.selection.createRange(); 91 selText = range.text; 92 //insert tags 93 checkSelectedText(); 94 range.text = tagOpen + selText + tagClose; 95 //mark sample text as selected 96 if (isSample && range.moveStart) { 97 if (window.opera) 98 tagClose = tagClose.replace(/\n/g,''); 99 range.moveStart('character', - tagClose.length - selText.length); 100 range.moveEnd('character', - tagClose.length); 101 } 102 range.select(); 103 //restore window scroll position 104 if (document.documentElement && document.documentElement.scrollTop) 105 document.documentElement.scrollTop = winScroll 106 else if (document.body) 107 document.body.scrollTop = winScroll; 108 109 } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla 110 111 //save textarea scroll position 112 var textScroll = txtarea.scrollTop; 113 //get current selection 114 txtarea.focus(); 115 var startPos = txtarea.selectionStart; 116 var endPos = txtarea.selectionEnd; 117 selText = txtarea.value.substring(startPos, endPos); 118 //insert tags 119 checkSelectedText(); 120 txtarea.value = txtarea.value.substring(0, startPos) 121 + tagOpen + selText + tagClose 122 + txtarea.value.substring(endPos, txtarea.value.length); 123 //set new selection 124 if (isSample) { 125 txtarea.selectionStart = startPos + tagOpen.length; 126 txtarea.selectionEnd = startPos + tagOpen.length + selText.length; 127 } else { 128 txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length; 129 txtarea.selectionEnd = txtarea.selectionStart; 130 } 131 //restore textarea scroll position 132 txtarea.scrollTop = textScroll; 133 } 134 135 function checkSelectedText(){ 136 if (!selText) { 137 selText = sampleText; 138 isSample = true; 139 } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char 140 selText = selText.substring(0, selText.length - 1); 141 tagClose += ' ' 142 } 143 } 144 145 } 146 147 /** 148 * Restore the edit box scroll state following a preview operation, 149 * and set up a form submission handler to remember this state 150 */ 151 function scrollEditBox() { 152 var editBox = document.getElementById( 'wpTextbox1' ); 153 var scrollTop = document.getElementById( 'wpScrolltop' ); 154 var editForm = document.getElementById( 'editform' ); 155 if( editBox && scrollTop ) { 156 if( scrollTop.value ) 157 editBox.scrollTop = scrollTop.value; 158 addHandler( editForm, 'submit', function() { 159 document.getElementById( 'wpScrolltop' ).value = document.getElementById( 'wpTextbox1' ).scrollTop; 160 } ); 161 } 162 } 163 164 /** 165 * Starten der beiden Funktionen beim Laden der Seite 166 */ 167 //hookEvent( 'load', scrollEditBox ); 168 //hookEvent( 'load', mwSetupToolbar ); 169 170 171 /** 172 * alternativ: Starten der beiden Funktionen beim Aufruf der Funktion ab(); zB. mit onclick 173 */ 174 ab = function() { 175 //scrollEditBox(); // nicht nötig, solange Formular nicht gesendet werden soll 176 mwSetupToolbar(); 177 }