Jump to content

User:Goadeff/common.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Goadeff (talk | contribs) at 23:38, 25 June 2012 (Created page with '    Any JavaScript here will be loaded for all users on every page load.:   /**************************************************************************...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/* 
 
 
Any JavaScript here will be loaded for all users on every page load. 
*/
 
/***************************************************************************
    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();
            }
        });
    }
});
 
/***************************************************************************
   My own bit of tinkering...
***************************************************************************/
function selectionMagic()
{
    oldText = $('#wpTextbox1').textSelection('getSelection');
    newText = $('<div/>').text(oldText).html();
    $('#wpTextbox1').insertAtCursor(newText);
}
 
/***************************************************************************
   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: mw.config.get('stylepath')+'/common/images/Vector_strikeout.png',
                action: { type: 'encapsulate', options: { pre: "<s>", post: "</s>" } }
            }
        }
    });
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main', group: 'format', tools: { "comment": { label: 'Comment', type: 'button', 
            icon: mw.config.get('stylepath')+'/common/images/Toolbaricon_italics_C.jpg',
                action: { type: 'encapsulate', options: { pre: "<!--", post: "-->" } }
            }
        }
    });
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main', group: 'format', tools: { "template": { label: 'Template', type: 'button', 
            icon: mw.config.get('stylepath')+'/common/images/Template_icon.svg',
                action: { type: 'encapsulate', options: { pre: "{{", post: "}}", peri: "Template" } }
            }
        }
    });
 /*
   $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main', group: 'format', tools: { "pre": { label: 'Pre', type: 'button', 
            icon: mw.config.get('stylepath')+'/common/images/Toolbar_pre_vector.png',
                action: { type: 'encapsulate', options: { pre: "<pre>", post: "</pre>", peri: "preformatted text" } }
            }
        }
    });
*/
   $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main', group: 'format', tools: { "pre": { label: 'Pre', type: 'button', 
            icon: mw.config.get('stylepath')+'/common/images/Toolbar_pre_vector.png',
                action: { type: 'callback', execute: function(context) { selectionMagic(); } }
            }
        }
    });
};
 
/* 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.toolbar', function () {
                                $(document).ready( customizeToolbar );
                        } );
                }
        } );
}