Clean up a bit of a mess with jsMsg(). Should be much more functional now and accept...
[lhc/web/wiklou.git] / skins / common / wikibits.js
index 812710b..373c8ee 100644 (file)
@@ -3,8 +3,13 @@
 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
 var is_gecko = /gecko/.test( clientPC ) &&
        !/khtml|spoofer|netscape\/7\.0/.test(clientPC);
-var is_safari = clientPC.indexOf('applewebkit') != -1 &&
-       clientPC.indexOf('spoofer') == -1;
+var webkit_match = clientPC.match(/applewebkit\/(\d+)/);
+if (webkit_match) {
+       var is_safari = clientPC.indexOf('applewebkit') != -1 &&
+               clientPC.indexOf('spoofer') == -1;
+       var is_safari_win = is_safari && clientPC.indexOf('windows') != -1;
+       var webkit_version = parseInt(webkit_match[1]);
+}
 var is_khtml = navigator.vendor == 'KDE' ||
        ( document.childNodes && !document.all && !navigator.taintEnabled );
 // For accesskeys; note that FF3+ is included here!
@@ -31,7 +36,11 @@ if (!window.onloadFuncts) {
 
 function addOnloadHook(hookFunct) {
        // Allows add-on scripts to add onload functions
-       onloadFuncts[onloadFuncts.length] = hookFunct;
+       if(!doneOnloadHook) {
+               onloadFuncts[onloadFuncts.length] = hookFunct;
+       } else {
+               hookFunct();  // bug in MSIE script loading
+       }
 }
 
 function hookEvent(hookName, hookFunct) {
@@ -42,6 +51,41 @@ function hookEvent(hookName, hookFunct) {
        }
 }
 
+function importScript(page) {
+       return importScriptURI(wgScript + '?action=raw&ctype=text/javascript&title=' + encodeURIComponent(page.replace(/ /g,'_')));
+}
+var loadedScripts = {}; // included-scripts tracker
+function importScriptURI(url) {
+       if (loadedScripts[url]) {
+               return null;
+       }
+       loadedScripts[url] = true;
+       var s = document.createElement('script');
+       s.setAttribute('src',url);
+       s.setAttribute('type','text/javascript');
+       document.getElementsByTagName('head')[0].appendChild(s);
+       return s;
+}
+function importStylesheet(page) {
+       return importStylesheetURI(wgScript + '?action=raw&ctype=text/css&title=' + encodeURIComponent(page.replace(/ /g,'_')));
+}
+function importStylesheetURI(url) {
+       return document.createStyleSheet ? document.createStyleSheet(url) : appendCSS('@import "' + url + '";');
+}
+function appendCSS(text) {
+       var s = document.createElement('style');
+       s.type = 'text/css';
+       s.rel = 'stylesheet';
+       if (s.styleSheet) s.styleSheet.cssText = text //IE
+       else s.appendChild(document.createTextNode(text + '')) //Safari sometimes borks on null
+       document.getElementsByTagName('head')[0].appendChild(s);
+       return s;
+}
+
 // document.write special stylesheet links
 if (typeof stylepath != 'undefined' && typeof skin != 'undefined') {
        if (is_opera_preseven) {
@@ -135,65 +179,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 +199,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.
@@ -300,9 +206,11 @@ function insertTags(tagOpen, tagClose, sampleText) {
 var tooltipAccessKeyPrefix = 'alt-';
 if (is_opera) {
        tooltipAccessKeyPrefix = 'shift-esc-';
-} else if (is_safari
-          || navigator.userAgent.toLowerCase().indexOf('mac') != -1
-          || navigator.userAgent.toLowerCase().indexOf('konqueror') != -1 ) {
+} else if (!is_safari_win && is_safari && webkit_version > 526) {
+       tooltipAccessKeyPrefix = 'ctrl-alt-';
+} else if (!is_safari_win && (is_safari
+               || clientPC.indexOf('mac') != -1
+               || clientPC.indexOf('konqueror') != -1 )) {
        tooltipAccessKeyPrefix = 'ctrl-';
 } else if (is_ff2) {
        tooltipAccessKeyPrefix = 'alt-shift-';
@@ -535,24 +443,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
@@ -882,7 +772,7 @@ function ts_alternate(table) {
  * Add a cute little box at the top of the screen to inform the user of
  * something, replacing any preexisting message.
  *
- * @param String message HTML to be put inside the right div
+ * @param String -or- Dom Object message HTML to be put inside the right div
  * @param String className   Used in adding a class; should be different for each
  *   call to allow CSS/JS to hide different boxes.  null = no class used.
  * @return Boolean       True on success, false on failure
@@ -920,7 +810,15 @@ function jsMsg( message, className ) {
        if( className ) {
                messageDiv.setAttribute( 'class', 'mw-js-message-'+className );
        }
-       messageDiv.innerHTML = message;
+       
+       if (typeof message === 'object') {
+               while (messageDiv.hasChildNodes()) // Remove old content
+                       messageDiv.removeChild(messageDiv.firstChild);
+               messageDiv.appendChild (message); // Append new content
+       }
+       else {
+               messageDiv.innerHTML = message;
+       }
        return true;
 }
 
@@ -966,7 +864,6 @@ function runOnloadHook() {
 
        updateTooltipAccessKeys( null );
        akeytt( null );
-       scrollEditBox();
        setupCheckboxShiftClick();
        sortables_init();
 
@@ -1003,4 +900,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);