|
|
| (۲۴ نسخهٔ میانی ویرایش شده توسط ۴ کاربر نشان داده نشد) |
| خط ۱: |
خط ۱: |
| // <source lang="javascript"> | | /** |
| | * EditTools support: add a selector, change <a> into buttons. |
| | * The special characters to insert are defined at [[MediaWiki:Edittools]]. |
| | * |
| | * @author Arnomane, 2006 (on the commons.wikimedia.org/wiki/MediaWiki:Edittools.js) |
| | * @author Kaganer, 2007 (adapting to www.mediawiki.org) |
| | * @author Krinkle, 2012 |
| | * @source www.mediawiki.org/wiki/MediaWiki:Gadget-Edittools.js |
| | * @revision 2012-02-29 |
| | */ |
| | /*jslint browser: true*/ |
| | /*global jQuery, mediaWiki*/ |
| | (function ($, mw) { |
| | "use strict"; |
|
| |
|
| /*
| | var conf, editTools, $sections; |
| EditTools support: add a selector, change into true buttons, enable for all text input fields
| |
| The special characters to insert are defined at [[MediaWiki:Edittools]].
| |
| */
| |
|
| |
|
| // Globals: getElementsByClassName, hookEvent, addEvent (from wikibits.js)
| | conf = { |
| | initialSubset: window.EditTools_initial_subset === undefined ? window.EditTools_initial_subset : 0 |
| | }; |
|
| |
|
| if ( typeof( EditTools_set_focus ) == 'undefined' ) {
| | editTools = { |
| var EditTools_set_focus = true;
| |
| }
| |
|
| |
|
| if ( typeof( EditTools_set_focus_initially ) == 'undefined' ) {
| | /** |
| var EditTools_set_focus_initially = EditTools_set_focus; | | * Creates the selector |
| }
| | */ |
| | setup: function () { |
| | var $container, $select, initial; |
|
| |
|
| if ( typeof( EditTools_initial_subset ) == 'undefined' ) { | | $container = $('#mw-edittools-charinsert'); |
| var EditTools_initial_subset = 0;
| | if (!$container.length) { |
| } | | return; |
| | } |
| | $sections = $container.find('.mw-edittools-section'); |
| | if ($sections.length <= 1) { |
| | // Only care if there is more than one |
| | return; |
| | } |
|
| |
|
| var EditTools = {
| | $select = $('<select>').css('display', 'inline'); |
| createSelector: function() {
| |
| var spec = document.getElementById( 'specialchars' );
| |
| if ( !spec ) {
| |
| return;
| |
| }
| |
| var sb = getElementsByClassName( spec, 'p', 'specialbasic' );
| |
| if ( sb.length <= 1 ) {
| |
| return; // Only care if there is more than one
| |
| }
| |
|
| |
|
| var sel = document.createElement( 'select' );
| | initial = conf.initialSubset; |
| sel.style.display = 'inline';
| | if (isNaN(initial) || initial < 0 || initial >= $select.length) { |
| // sel.setAttribute( 'onchange', 'EditTools.chooseCharSubset( selectedIndex, true );' );
| | initial = 0; |
| // Apparently, this doesn't work on IE6. Use an explicit event handling function instead:
| | } |
| sel.onchange = EditTools.handleOnchange;
| |
|
| |
|
| var initial = EditTools_initial_subset;
| | $sections.each(function (i, el) { |
| if ( isNaN( initial ) || initial < 0 || initial >= sb.length ) {
| | var $section, sectionTitle, $option; |
| initial = 0;
| |
| }
| |
|
| |
|
| for ( var i = 0; i < sb.length; i++ ) {
| | $section = $(el); |
| var o = document.createElement( 'option' );
| | sectionTitle = $section.data('sectionTitle'); |
| // Ugh. We have encoded Unicode characters in the names...
| |
| var id = sb[i].id.replace( /.([0-9A-F][0-9A-F])/g, '%$1' ).replace( /_/g, ' ' );
| |
| if ( i == initial ) {
| |
| o.selected = 'selected';
| |
| }
| |
| o.appendChild( document.createTextNode( decodeURIComponent( id ) ) );
| |
| sel.appendChild( o );
| |
| }
| |
|
| |
|
| spec.insertBefore( sel, spec.firstChild );
| | $option = $('<option>') |
| | .text(sectionTitle) |
| | .prop('value', i) |
| | .prop('selected', i === initial); |
|
| |
|
| EditTools.chooseCharSubset(
| | $select.append($option); |
| initial,
| | }); |
| ( wgAction != 'submit' ) &&
| |
| EditTools_set_focus_initially &&
| |
| ( wgCanonicalNamespace != 'Special' || wgCanonicalSpecialPageName != 'Upload' )
| |
| ); | |
| },
| |
|
| |
|
| handleOnchange: function( evt ) { | | $select.change(editTools.handleOnchange); |
| var e = evt || window.event; // W3C, IE
| | $container.prepend($select); |
| var node = e.target || e.srcElement; // W3C, IE
| |
|
| |
|
| EditTools.chooseCharSubset( node.selectedIndex, true );
| | editTools.chooseSection(initial); |
| return true;
| | }, |
| }, | |
|
| |
|
| chooseCharSubset: function( selected, set_focus ) {
| | /** |
| var sb = getElementsByClassName( document.getElementById( 'specialchars' ), 'p', 'specialbasic' );
| | * Handle onchange event of the <select> |
| EditTools.makeButtons( sb[selected] );
| | * |
| for ( var i = 0; i < sb.length; i++ ) {
| | * @context {Element} |
| sb[i].style.display = i == selected ? 'inline' : 'none';
| | * @param e {jQuery.Event} |
| }
| | */ |
| if ( set_focus && EditTools_set_focus ) {
| | handleOnchange: function () { |
| var txtarea = EditTools.getTextArea();
| | editTools.chooseSection(Number($(this).val())); |
| if ( txtarea ) {
| |
| txtarea.focus();
| |
| }
| |
| }
| |
| },
| |
|
| |
|
| fixateWidth: function() {
| | return true; |
| var edit_bar = document.getElementById( 'specialchars' );
| | }, |
| if ( !edit_bar ) {
| |
| return; | |
| }
| |
| // Try to fixate the width of that bar, otherwise IE6 may make it wider, which will lead to
| |
| // a table re-layout on the upload form resulting in the right column extending beyond the
| |
| // right edge of the window.
| |
| edit_bar.setAttribute( 'width', '' + ( edit_bar.clientWidth || edit_bar.offsetWidth ) );
| |
| edit_bar.style.maxWidth = '' + ( edit_bar.clientWidth || edit_bar.offsetWidth ) + 'px';
| |
| // If we're inside a table, fixate the containing table cell, too.
| |
| var parent = edit_bar.parentNode;
| |
| while ( parent && parent != document.body && parent.nodeName.toLowerCase() != 'td' ) {
| |
| parent = parent.parentNode;
| |
| }
| |
| if ( parent && parent != document.body ) {
| |
| parent.setAttribute( 'width', '' + ( parent.clientWidth || parent.offsetWidth ) );
| |
| parent.style.maxWidth = '' + ( parent.clientWidth || parent.offsetWidth ) + 'px';
| |
| }
| |
| }, | |
|
| |
|
| makeButtons: function( section ) {
| | /** |
| var edit_bar = section || document.getElementById( 'specialchars' );
| | * Toggle the currently visible section |
| if ( !edit_bar ) {
| | * |
| return;
| | * @param sectionNr {Number} |
| } | | * @param setFocus {Boolean} |
| var links = edit_bar.getElementsByTagName( 'a' );
| | */ |
| // 'links' is a *live* collection! | | chooseSection: function (sectionNr) { |
| var b = null;
| | var $choise = $sections.eq(sectionNr); |
| while ( links.length ) {
| | if ($choise.length !== 1) { |
| b = document.createElement( 'input' );
| | return; |
| b.type = 'button';
| | } |
| b.style.fontSize = '0.9em';
| |
| b.style.paddingLeft = '1px';
| |
| b.style.paddingRight = '1px';
| |
| b.style.marginLeft = '1px';
| |
| b.onclick = links[0].onclick;
| |
| b.value = links[0].firstChild.data;
| |
| var parent = links[0].parentNode; | |
| parent.replaceChild( b, links[0] ); // This removes links[0] from links!
| |
| b.blur(); // IE6 insists on marking some buttons as having the focus...
| |
| var margin_added = false;
| |
| // Remove text nodes (nodeType == Node.TEXT_NODE, but IE6 doesn't know that...)
| |
| // Insert some spacing where desired.
| |
| while ( b.nextSibling && b.nextSibling.nodeType == 3 ) {
| |
| if ( !margin_added && b.nextSibling.data.search( /\S/ ) >= 0 ) {
| |
| b.style.marginRight = '4px';
| |
| margin_added = true;
| |
| }
| |
| parent.removeChild( b.nextSibling );
| |
| } | |
| }
| |
| },
| |
|
| |
|
| enableForAllFields: function() {
| | // Making these buttons is a little slow, |
| if ( typeof( insertTags ) != 'function' ) {
| | // If we made them all at once the browser would hang |
| return;
| | // for over 2 seconds, so instead we're doing it on-demand |
| }
| | // for each section. No need to do it twice thoguh, so remember |
| // insertTags from the site-wide /skins-1.5/common/edit.js just inserts in the first | | // in data whether it was done already |
| // textarea in the document. Evidently, that's not good if we have multiple textareas. | | if (!$choise.data('charInsert.buttonsMade')) { |
| // My first idea was to simply add a hidden textarea as the first one, and redefine
| | $choise.data('charInsert.buttonsMade', true); |
| // insertTags such that it copied first the last active textareas contents over to that hidden | | editTools.makeButtons($choise); |
| // field, set the cursor or selection there, let the standard insertTags do its thing, and
| | } |
| // then copy the hidden field's text, cursor position and selection back to the currently
| |
| // active field. Unfortunately, that is just as complex as simply copying the whole code | |
| // from wikibits to here and let it work on the right text field in the first place. | |
| var texts = document.getElementsByTagName( 'textarea' );
| |
| for ( var i = 0; i < texts.length; i++ ) {
| |
| $( texts[i] ).focus( EditTools.registerTextField ); | |
| }
| |
| // While we're at it, also enable it for input fields
| |
| texts = document.getElementsByTagName( 'input' );
| |
| for ( var i = 0; i < texts.length; i++ ) {
| |
| if ( texts[i].type == 'text' ) {
| |
| $( texts[i] ).focus( EditTools.registerTextField ); | |
| }
| |
| }
| |
| insertTags = EditTools.insertTags; // Redefine the global insertTags
| |
| }, | |
|
| |
|
| last_active_textfield: null,
| | $choise.show(); |
| | $sections.not($choise).hide(); |
| | }, |
|
| |
|
| registerTextField: function( evt ) {
| | /** |
| var e = evt || window.event;
| | * Convert the <a onclick> links to buttons in a given section. |
| var node = e.target || e.srcElement;
| | * |
| if ( !node ) {
| | * @param $section {jQuery} |
| return;
| | */ |
| }
| | makeButtons: function ($section) { |
| EditTools.last_active_textfield = node.id;
| | var $links; |
| return true;
| |
| },
| |
|
| |
|
| getTextArea: function() {
| | if (!$section.length) { |
| var txtarea = null;
| | return; |
| if ( EditTools.last_active_textfield && EditTools.last_active_textfield != '' ) | | } |
| txtarea = document.getElementById( EditTools.last_active_textfield );
| |
| if ( !txtarea ) {
| |
| // Fallback option: old behaviour
| |
| if ( document.editform ) {
| |
| txtarea = document.editform.wpTextbox1;
| |
| } else {
| |
| // Some alternate form? Take the first one we can find
| |
| txtarea = document.getElementsByTagName( 'textarea' );
| |
| if ( txtarea.length > 0 ) {
| |
| txtarea = txtarea[0];
| |
| } else {
| |
| txtarea = null;
| |
| }
| |
| }
| |
| }
| |
| return txtarea; | |
| }, | |
|
| |
|
| insertTags: function( tagOpen, tagClose, sampleText ) {
| | $links = $section.find('a'); |
| /* Usability initiative compatibility */
| | $links.each(function (i, a) { |
| if ( typeof $j != 'undefined' && typeof $j.fn.textSelection != 'undefined' ) {
| | var $a, $button, start, end; |
| $j( '#wpTextbox1' ).textSelection( | | $a = $(a); |
| 'encapsulateSelection', { 'pre': tagOpen, 'peri': sampleText, 'post': tagClose }
| | $button = $( '<input>' ).attr( 'type', 'button' ); |
| ); | | start = $a.data( 'mw-charinsert-start' ); |
| return;
| | end = $a.data( 'mw-charinsert-end' ); |
| } | | if ( start !== undefined && end !== undefined ) { |
| var txtarea = EditTools.getTextArea ();
| | $button.click( function( e ) { |
| if ( !txtarea ) {
| | e.preventDefault(); |
| return;
| | editTools.insertTags( start, '', end ) |
| } | | } ); |
| var selText, isSample = false;
| | } else { |
| | $button[0].onclick = a.onclick; |
| | } |
| | $button[0].value = $a.text(); |
| | $a.replaceWith($button); |
| | }); |
| | }, |
|
| |
|
| function checkSelectedText() { | | insertTags: function ( pre, peri, post ) { |
| if ( !selText ) {
| | var current = $( '#wpTextbox1' ); |
| selText = sampleText;
| | if ( current && current.length ) { |
| isSample = true;
| | current.textSelection( |
| } else if ( selText.charAt( selText.length - 1 ) == ' ' ) { // Exclude ending space char
| | 'encapsulateSelection', { pre: pre, peri: peri, post: post } |
| selText = selText.substring( 0, selText.length - 1 );
| | ); |
| tagClose += ' ';
| | } |
| } | | } |
| } | |
|
| |
|
| if ( document.selection && document.selection.createRange ) { // IE/Opera
| | }; |
| // Save window scroll position
| |
| var winScroll = 0;
| |
| if ( document.documentElement && document.documentElement.scrollTop ) {
| |
| winScroll = document.documentElement.scrollTop;
| |
| } else if ( document.body ) {
| |
| 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 );
| |
| // 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;
| |
| }
| |
| }, // end insertTags
| |
|
| |
|
| setup: function() {
| | $(document).ready(editTools.setup); |
| EditTools.fixateWidth();
| |
| EditTools.createSelector();
| |
| EditTools.enableForAllFields();
| |
| }
| |
|
| |
|
| } // end EditTools | | }(jQuery, mediaWiki)); |
| | |
| // Do not use addOnloadHook; it runs *before* the onload event fires. At that time, onclick or
| |
| // onfocus handlers may not yet be set up properly.
| |
| hookEvent( 'load', EditTools.setup );
| |
| | |
| // </source>
| |
/**
* EditTools support: add a selector, change <a> into buttons.
* The special characters to insert are defined at [[MediaWiki:Edittools]].
*
* @author Arnomane, 2006 (on the commons.wikimedia.org/wiki/MediaWiki:Edittools.js)
* @author Kaganer, 2007 (adapting to www.mediawiki.org)
* @author Krinkle, 2012
* @source www.mediawiki.org/wiki/MediaWiki:Gadget-Edittools.js
* @revision 2012-02-29
*/
/*jslint browser: true*/
/*global jQuery, mediaWiki*/
(function ($, mw) {
"use strict";
var conf, editTools, $sections;
conf = {
initialSubset: window.EditTools_initial_subset === undefined ? window.EditTools_initial_subset : 0
};
editTools = {
/**
* Creates the selector
*/
setup: function () {
var $container, $select, initial;
$container = $('#mw-edittools-charinsert');
if (!$container.length) {
return;
}
$sections = $container.find('.mw-edittools-section');
if ($sections.length <= 1) {
// Only care if there is more than one
return;
}
$select = $('<select>').css('display', 'inline');
initial = conf.initialSubset;
if (isNaN(initial) || initial < 0 || initial >= $select.length) {
initial = 0;
}
$sections.each(function (i, el) {
var $section, sectionTitle, $option;
$section = $(el);
sectionTitle = $section.data('sectionTitle');
$option = $('<option>')
.text(sectionTitle)
.prop('value', i)
.prop('selected', i === initial);
$select.append($option);
});
$select.change(editTools.handleOnchange);
$container.prepend($select);
editTools.chooseSection(initial);
},
/**
* Handle onchange event of the <select>
*
* @context {Element}
* @param e {jQuery.Event}
*/
handleOnchange: function () {
editTools.chooseSection(Number($(this).val()));
return true;
},
/**
* Toggle the currently visible section
*
* @param sectionNr {Number}
* @param setFocus {Boolean}
*/
chooseSection: function (sectionNr) {
var $choise = $sections.eq(sectionNr);
if ($choise.length !== 1) {
return;
}
// Making these buttons is a little slow,
// If we made them all at once the browser would hang
// for over 2 seconds, so instead we're doing it on-demand
// for each section. No need to do it twice thoguh, so remember
// in data whether it was done already
if (!$choise.data('charInsert.buttonsMade')) {
$choise.data('charInsert.buttonsMade', true);
editTools.makeButtons($choise);
}
$choise.show();
$sections.not($choise).hide();
},
/**
* Convert the <a onclick> links to buttons in a given section.
*
* @param $section {jQuery}
*/
makeButtons: function ($section) {
var $links;
if (!$section.length) {
return;
}
$links = $section.find('a');
$links.each(function (i, a) {
var $a, $button, start, end;
$a = $(a);
$button = $( '<input>' ).attr( 'type', 'button' );
start = $a.data( 'mw-charinsert-start' );
end = $a.data( 'mw-charinsert-end' );
if ( start !== undefined && end !== undefined ) {
$button.click( function( e ) {
e.preventDefault();
editTools.insertTags( start, '', end )
} );
} else {
$button[0].onclick = a.onclick;
}
$button[0].value = $a.text();
$a.replaceWith($button);
});
},
insertTags: function ( pre, peri, post ) {
var current = $( '#wpTextbox1' );
if ( current && current.length ) {
current.textSelection(
'encapsulateSelection', { pre: pre, peri: peri, post: post }
);
}
}
};
$(document).ready(editTools.setup);
}(jQuery, mediaWiki));