Jump to content

User:Goadeff/common.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function CustomizeModificationsOfSidebar() {
    ModifySidebar("add", "navigation", "User/common.js", "http://en.wikipedia.org/wiki/User:Goadeff/common.js");
    ModifySidebar("add", "navigation", "User/common.css", "http://en.wikipedia.org/wiki/User:Goadeff/common.css");
}
$(CustomizeModificationsOfSidebar)

/***************************************************************************
 BEGIN jQuery extensions
   Lifted from: https://github.com/Kasheftin/jquery-textarea-caret:
***************************************************************************/
jQuery.fn.extend({
    insertAtCursor: function(myValue) {
        return this.each(function(i) {
            if (document.selection) {
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            }
            else if (this.selectionStart || this.selectionStart == "0") {
                var startPos = this.selectionStart;
                var endPos = this.selectionEnd;
                var scrollTop = this.scrollTop;
                this.value = this.value.substring(0,startPos) + myValue + this.value.substring(endPos,this.value.length);
                this.focus();
                this.selectionStart = startPos + myValue.length;
                this.selectionEnd = startPos + myValue.length;
                this.scrollTop = scrollTop;
            }
                else {
                this.value += myValue;
                this.focus();
            }
        });
    },
    insertAroundCursor: function(myValueBefore,myValueAfter) {
        return this.each(function(i) {
                if (document.selection) {
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValueBefore + sel.text + myValueAfter;
                this.focus();
            }
            else if (this.selectionStart || this.selectionStart == "0") {
                var startPos = this.selectionStart;
                var endPos = this.selectionEnd;
                var scrollTop = this.scrollTop;
                this.value = this.value.substring(0,startPos) + myValueBefore + this.value.substring(startPos,endPos) + myValueAfter + this.value.substring(endPos,this.value.length);
                this.focus();
                this.selectionStart = startPos + myValueBefore.length;
                this.selectionEnd = endPos + myValueBefore.length;
                this.scrollTop = scrollTop;
            }
            else {
                this.value += myValueBefore + myValueAfter;
                this.focus();
            }
        });
    }
});
/***************************************************************************
END jQuery extensions
***************************************************************************/
  
/***************************************************************************
BEGIN My own bit of tinkering...
***************************************************************************/
function selectionMagic()
{
    oldText = $('#wpTextbox1').textSelection('getSelection');
    newText = $('<div/>').text(oldText).html();
    $('#wpTextbox1').insertAtCursor(newText);
}
/***************************************************************************
END of my own bit of tinkering...
***************************************************************************/
 
/***************************************************************************
   Modifying newer WikiEditor toolbar
   ref: http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization/Library
***************************************************************************/
var customizeToolbar = function() {
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main', group: 'format', tools: { "strikethrough": { label: 'Strike', type: 'button', 
            icon: '/media/wikipedia/commons/6/6d/Vector_strikeout.png',
                action: { type: 'encapsulate', options: { pre: "<s>", post: "</s>" } }
            }
        }
    });
   $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main', group: 'format', tools: { "pre": { label: 'Pre', type: 'button', 
            icon: '/media/wikipedia/commons/b/b9/Toolbar_pre_vector.png',
                action: { type: 'encapsulate', options: { pre: "<pre>", post: "</pre>", peri: "preformatted text" } }
            }
        }
    });
};
 
/* Check if we are in edit mode and the required modules are available and then customize the toolbar */
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) !== -1 ) {
        mw.loader.using( 'user.options', function () {
                if ( mw.user.options.get('usebetatoolbar') ) {
                        mw.loader.using( 'ext.wikiEditor', function () {
                                $(document).ready( customizeToolbar );
                        } );
                }
        } );
}
/***************************************************************************
    Modifying the sidebar -- I favor using DynamicSidebar extension instead, but this still may be usefull...
    ref: http://www.mediawiki.org/wiki/Manual:Interface/Sidebar#Add_or_remove_sections_.28JavaScript.29
    I'm putting the ModifySidebar function here, and users make their own customizations in User/common.js
***************************************************************************/
function ModifySidebar(action, section, name, link) {
    try {
        switch (section) {
          case "languages":
            var target = "p-lang";
            break;
          case "toolbox":
            var target = "p-tb";
            break;
          case "navigation":
            var target = "p-navigation";
            break;
          default:
            var target = "p-" + section;
            break;
        }
 
        if (action == "add") {
            var node = document.getElementById(target)
                               .getElementsByTagName('div')[0]
                               .getElementsByTagName('ul')[0];
 
            var aNode = document.createElement('a');
            var liNode = document.createElement('li');
 
            aNode.appendChild(document.createTextNode(name));
            aNode.setAttribute('href', link);
            liNode.appendChild(aNode);
            liNode.className='plainlinks';
            node.appendChild(liNode);
        }
 
        if (action == "remove") {
            var list = document.getElementById(target)
                               .getElementsByTagName('div')[0]
                               .getElementsByTagName('ul')[0];
 
            var listelements = list.getElementsByTagName('li');
 
            for (var i = 0; i < listelements.length; i++) {
                if (listelements[i].getElementsByTagName('a')[0].innerHTML == name ||
                    listelements[i].getElementsByTagName('a')[0].href == link) {
 
                    list.removeChild(listelements[i]);
                }
            }
        }
 
    } catch(e) {
      // lets just ignore what's happened
      return;
    }
}

;