Difference between revisions of "MediaWiki:Edittools.js"
From Biowikifarm Metawiki
(Code from http://species-id.net/wiki/MediaWiki:Edittools.js - which in turn modified code from en.wikipedia.org) |
m (local get of MediaWiki:Edittools.css) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | /* | ||
+ | EditTools support | ||
− | + | Add a selector, change into true buttons, enable for all text input fields | |
− | + | If enabled in preferences, the script puts the buttons into the WikiEditor Toolbar | |
− | + | The special characters to insert are defined at [[MediaWiki:Edittools]]. | |
− | // < | + | */ |
+ | // <nowiki> | ||
+ | /*jshint curly:false, white:true, indent:2 */ | ||
+ | /*global importStylesheetURI:false, jQuery:false, mediaWiki:false */ | ||
− | + | (function ($, mw) { | |
− | + | "use strict"; | |
− | + | var EditTools; | |
− | + | // mw.loader.load('//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css', 'text/css'); | |
− | + | mw.loader.load( | |
− | + | mw.config.get('wgServer') | |
− | + | + mw.config.get('wgScript') + '?title=MediaWiki:Edittools.css&action=raw&ctype=text/css', 'text/css' | |
− | + | ); | |
− | + | window.EditTools = EditTools = { | |
− | + | createSelector: function () { | |
− | + | var $spec = $('#specialchars'); | |
− | + | var $sb = $('#specialchars p.specialbasic'); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | // Only care if there is more than one | |
+ | if (!$spec.length || $sb.length <= 1) | ||
+ | return; | ||
− | + | var $sel = $('<select>'); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | $sel.change(function () { | |
− | + | EditTools.chooseCharSubset(); | |
− | + | }); | |
+ | $sb.each(function (i) { | ||
+ | var id = $(this).attr('id').replace(/.([0-9A-F][0-9A-F])/g, '%$1').replace(/_/g, ' '); | ||
+ | $sel.append( | ||
+ | $('<option value="' + i + '"></option>').text(decodeURIComponent(id)) | ||
+ | ); | ||
+ | }); | ||
− | + | $spec.prepend($sel); | |
− | + | this.chooseCharSubset(); | |
− | + | }, | |
− | + | ||
− | + | ||
− | + | chooseCharSubset: function () { | |
− | + | var $sb = $('#specialchars p.specialbasic'); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | var id = $('#specialchars select').val(); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | var $wanted = $sb.eq(id); | |
− | + | this.makeButtons($wanted); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | $sb.hide(); | |
− | + | ||
− | + | ||
− | + | $wanted.css('display', 'inline'); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | }, | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | bindOnClick: function ($button, self) { | |
− | + | var onclick = self.getAttribute("onclick"), | |
− | + | $self = $( self ), | |
− | + | start, | |
− | + | end; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | start = $self.data( 'mw-charinsert-start' ), | ||
+ | end = $self.data( 'mw-charinsert-end' ); | ||
+ | if ( start !== undefined && end !== undefined ) { | ||
+ | $button.click( function( e ) { | ||
+ | e.preventDefault(); | ||
+ | mw.toolbar.insertTags( start, end, '' ); | ||
+ | } ); | ||
+ | } else { | ||
+ | if (typeof onclick !== 'function') { | ||
+ | // if onclick is not a function, it's not IE7, so use setAttribute | ||
+ | // for FF,IE8,Chrome | ||
+ | $button[0].setAttribute('onclick', onclick); | ||
+ | } else { | ||
+ | // if onclick is a function, use the IE7 method and call onclick() in the anonymous function | ||
+ | // for IE7 | ||
+ | $button[0].onclick = function () { | ||
+ | onclick(); | ||
+ | }; | ||
+ | } | ||
+ | } | ||
+ | }, | ||
− | + | makeButtons: function ($wanted) { | |
− | + | var $links = $wanted.find('a'); | |
− | + | var self = this; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | $links.each(function () { | |
+ | var $button = $('<button type="button">'); | ||
+ | $button.text($(this).text()); | ||
− | + | self.bindOnClick($button, this); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | $(this).replaceWith($button); | |
− | + | $(this).blur(); | |
− | + | }); | |
− | + | $wanted.contents().not('button').remove(); | |
− | + | }, | |
− | + | makeToolbarButtons: function () { | |
− | + | var self = this; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | // Add Edittool section | |
− | + | $('#wpTextbox1').wikiEditor('addToToolbar', { | |
− | + | sections: { | |
+ | 'Edittools': { | ||
+ | type: 'booklet', | ||
+ | label: 'Edittools', | ||
+ | pages: { | ||
+ | 'Edittools1': { | ||
+ | layout: 'characters', | ||
+ | label: 'Edittools2' | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | var $section = $('.page-Edittools1 div').addClass('com-editbuttons'); | ||
+ | var $links = $('#specialchars p.specialbasic').eq(0).find('a'); | ||
+ | $links.each(function () { | ||
+ | var $button = $('<span>'); | ||
+ | $button.text($(this).text()); | ||
− | + | self.bindOnClick($button, this); | |
+ | $section.append($button); | ||
+ | }); | ||
+ | $('.mw-editTools').remove(); | ||
+ | }, | ||
− | + | last_active_textfield: null, | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | enableForAllFields: function () { | |
− | + | $('textarea, input').focus(function () { | |
− | + | EditTools.last_active_textfield = this.id; | |
− | + | }); | |
− | + | }, | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | getTextArea: function () { | |
− | + | var $txtarea = {}; | |
− | + | if (EditTools.last_active_textfield !== null) $txtarea = $('#' + EditTools.last_active_textfield).eq(0); | |
− | + | if ($txtarea.length !== 1) { | |
− | + | $txtarea = $('#bodyContent textarea').eq(0); | |
− | + | } | |
− | + | return $txtarea; | |
+ | }, | ||
− | + | registerTextField: function (evt) { | |
+ | var e = evt || window.event; | ||
+ | var node = e.target || e.srcElement; | ||
+ | if (!node) | ||
+ | return; | ||
+ | EditTools.last_active_textfield = node.id; | ||
+ | return true; | ||
+ | }, | ||
− | // | + | setup: function () { |
− | // | + | // Decide whether to use the toolbar or the bottom div |
− | + | if (window.oldEdittools || mw.user.options.get('gadget-OldEdittools') === "1" || $('#wpUploadDescription').length || !$.wikiEditor || !$.wikiEditor.isSupported()) { | |
+ | EditTools.createSelector(); | ||
+ | EditTools.enableForAllFields(); | ||
+ | } else { | ||
+ | EditTools.makeToolbarButtons(); | ||
+ | EditTools.enableForAllFields(); | ||
+ | } | ||
+ | if ( !mw.toolbar ) { | ||
+ | // `.insertTags()` required on, e.g. [[Special:Upload]] | ||
+ | mw.loader.load('mediawiki.toolbar'); | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | $(function () { | ||
+ | // Don't do anything if no edittools present. | ||
+ | if (!$('#specialchars').length) | ||
+ | return; | ||
− | // </ | + | mw.loader.using('mediawiki.user', function () { |
+ | // Check user preferences | ||
+ | if (mw.user.options.get('usebetatoolbar') === 1 && "1" !== mw.user.options.get('gadget-OldEdittools')) { | ||
+ | mw.loader.using(['ext.wikiEditor.toolbar', 'jquery.wikiEditor.toolbar'], EditTools.setup); | ||
+ | } else { | ||
+ | EditTools.setup(); | ||
+ | } | ||
+ | }); | ||
+ | }); | ||
+ | }(jQuery, mediaWiki)); | ||
+ | // </nowiki> |
Latest revision as of 10:27, 27 October 2017
/*
EditTools support
Add a selector, change into true buttons, enable for all text input fields
If enabled in preferences, the script puts the buttons into the WikiEditor Toolbar
The special characters to insert are defined at [[MediaWiki:Edittools]].
*/
// <nowiki>
/*jshint curly:false, white:true, indent:2 */
/*global importStylesheetURI:false, jQuery:false, mediaWiki:false */
(function ($, mw) {
"use strict";
var EditTools;
// mw.loader.load('//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css', 'text/css');
mw.loader.load(
mw.config.get('wgServer')
+ mw.config.get('wgScript') + '?title=MediaWiki:Edittools.css&action=raw&ctype=text/css', 'text/css'
);
window.EditTools = EditTools = {
createSelector: function () {
var $spec = $('#specialchars');
var $sb = $('#specialchars p.specialbasic');
// Only care if there is more than one
if (!$spec.length || $sb.length <= 1)
return;
var $sel = $('<select>');
$sel.change(function () {
EditTools.chooseCharSubset();
});
$sb.each(function (i) {
var id = $(this).attr('id').replace(/.([0-9A-F][0-9A-F])/g, '%$1').replace(/_/g, ' ');
$sel.append(
$('<option value="' + i + '"></option>').text(decodeURIComponent(id))
);
});
$spec.prepend($sel);
this.chooseCharSubset();
},
chooseCharSubset: function () {
var $sb = $('#specialchars p.specialbasic');
var id = $('#specialchars select').val();
var $wanted = $sb.eq(id);
this.makeButtons($wanted);
$sb.hide();
$wanted.css('display', 'inline');
},
bindOnClick: function ($button, self) {
var onclick = self.getAttribute("onclick"),
$self = $( self ),
start,
end;
start = $self.data( 'mw-charinsert-start' ),
end = $self.data( 'mw-charinsert-end' );
if ( start !== undefined && end !== undefined ) {
$button.click( function( e ) {
e.preventDefault();
mw.toolbar.insertTags( start, end, '' );
} );
} else {
if (typeof onclick !== 'function') {
// if onclick is not a function, it's not IE7, so use setAttribute
// for FF,IE8,Chrome
$button[0].setAttribute('onclick', onclick);
} else {
// if onclick is a function, use the IE7 method and call onclick() in the anonymous function
// for IE7
$button[0].onclick = function () {
onclick();
};
}
}
},
makeButtons: function ($wanted) {
var $links = $wanted.find('a');
var self = this;
$links.each(function () {
var $button = $('<button type="button">');
$button.text($(this).text());
self.bindOnClick($button, this);
$(this).replaceWith($button);
$(this).blur();
});
$wanted.contents().not('button').remove();
},
makeToolbarButtons: function () {
var self = this;
// Add Edittool section
$('#wpTextbox1').wikiEditor('addToToolbar', {
sections: {
'Edittools': {
type: 'booklet',
label: 'Edittools',
pages: {
'Edittools1': {
layout: 'characters',
label: 'Edittools2'
}
}
}
}
});
var $section = $('.page-Edittools1 div').addClass('com-editbuttons');
var $links = $('#specialchars p.specialbasic').eq(0).find('a');
$links.each(function () {
var $button = $('<span>');
$button.text($(this).text());
self.bindOnClick($button, this);
$section.append($button);
});
$('.mw-editTools').remove();
},
last_active_textfield: null,
enableForAllFields: function () {
$('textarea, input').focus(function () {
EditTools.last_active_textfield = this.id;
});
},
getTextArea: function () {
var $txtarea = {};
if (EditTools.last_active_textfield !== null) $txtarea = $('#' + EditTools.last_active_textfield).eq(0);
if ($txtarea.length !== 1) {
$txtarea = $('#bodyContent textarea').eq(0);
}
return $txtarea;
},
registerTextField: function (evt) {
var e = evt || window.event;
var node = e.target || e.srcElement;
if (!node)
return;
EditTools.last_active_textfield = node.id;
return true;
},
setup: function () {
// Decide whether to use the toolbar or the bottom div
if (window.oldEdittools || mw.user.options.get('gadget-OldEdittools') === "1" || $('#wpUploadDescription').length || !$.wikiEditor || !$.wikiEditor.isSupported()) {
EditTools.createSelector();
EditTools.enableForAllFields();
} else {
EditTools.makeToolbarButtons();
EditTools.enableForAllFields();
}
if ( !mw.toolbar ) {
// `.insertTags()` required on, e.g. [[Special:Upload]]
mw.loader.load('mediawiki.toolbar');
}
}
};
$(function () {
// Don't do anything if no edittools present.
if (!$('#specialchars').length)
return;
mw.loader.using('mediawiki.user', function () {
// Check user preferences
if (mw.user.options.get('usebetatoolbar') === 1 && "1" !== mw.user.options.get('gadget-OldEdittools')) {
mw.loader.using(['ext.wikiEditor.toolbar', 'jquery.wikiEditor.toolbar'], EditTools.setup);
} else {
EditTools.setup();
}
});
});
}(jQuery, mediaWiki));
// </nowiki>