Difference between revisions of "MediaWiki:Select category.js"
Jump to navigation
Jump to search
(Created page with "// <source lang="javascript"> /* EditTools support: add a selector, change into true buttons, enable for all text input fields If enabled in preferences, the script puts...") |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
/* |
/* |
||
− | + | SelectCategory 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 |
If enabled in preferences, the script puts the buttons into the WikiEditor Toolbar |
||
− | The special characters to insert are defined at [[MediaWiki: |
+ | The special characters to insert are defined at [[MediaWiki:SelectCategory]]. |
*/ |
*/ |
||
Line 13: | Line 13: | ||
(function($, mw) { |
(function($, mw) { |
||
"use strict"; |
"use strict"; |
||
− | var |
+ | var SelectCategory, insertTags; |
importStylesheetURI('//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css'); |
importStylesheetURI('//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css'); |
||
window.insertTags = insertTags = function(tagOpen, tagClose, sampleText) { |
window.insertTags = insertTags = function(tagOpen, tagClose, sampleText) { |
||
− | var $txtarea = |
+ | var $txtarea = SelectCategory.getTextArea(); |
if ($txtarea.length !== 1) return; |
if ($txtarea.length !== 1) return; |
||
Line 30: | Line 30: | ||
} |
} |
||
}; |
}; |
||
− | window. |
+ | window.SelectCategory = SelectCategory = { |
createSelector: function() { |
createSelector: function() { |
||
var $spec = $('#specialchars'); |
var $spec = $('#specialchars'); |
||
Line 41: | Line 41: | ||
$sel.change(function() { |
$sel.change(function() { |
||
− | + | SelectCategory.chooseCharSubset(); |
|
}); |
}); |
||
Line 103: | Line 103: | ||
$('#wpTextbox1').wikiEditor('addToToolbar', { |
$('#wpTextbox1').wikiEditor('addToToolbar', { |
||
'sections': { |
'sections': { |
||
− | ' |
+ | 'Category': { |
'type': 'booklet', |
'type': 'booklet', |
||
− | 'label': ' |
+ | 'label': 'Category', |
'pages': { |
'pages': { |
||
− | ' |
+ | 'Selectcategory1': { |
'layout': 'characters', |
'layout': 'characters', |
||
− | 'label': ' |
+ | // 'label': 'Category' |
} |
} |
||
} |
} |
||
Line 117: | Line 117: | ||
− | var $section = $('.page- |
+ | var $section = $('.page-Selectcategory1 div'); |
var $links = $('#specialchars p.specialbasic').eq(0).find('a'); |
var $links = $('#specialchars p.specialbasic').eq(0).find('a'); |
||
$links.each(function() { |
$links.each(function() { |
||
Line 126: | Line 126: | ||
$section.append($button); |
$section.append($button); |
||
}); |
}); |
||
− | $('.mw- |
+ | $('.mw-selectCategory').remove(); |
}, |
}, |
||
Line 133: | Line 133: | ||
enableForAllFields: function() { |
enableForAllFields: function() { |
||
$('textarea, input').focus(function() { |
$('textarea, input').focus(function() { |
||
− | + | SelectCategory.last_active_textfield = this.id; |
|
}); |
}); |
||
}, |
}, |
||
Line 139: | Line 139: | ||
getTextArea: function() { |
getTextArea: function() { |
||
var $txtarea = {}; |
var $txtarea = {}; |
||
− | if ( |
+ | if (SelectCategory.last_active_textfield !== null) $txtarea = $('#' + SelectCategory.last_active_textfield).eq(0); |
if ($txtarea.length !== 1) { |
if ($txtarea.length !== 1) { |
||
$txtarea = $('#bodyContent textarea').eq(0); |
$txtarea = $('#bodyContent textarea').eq(0); |
||
Line 150: | Line 150: | ||
var node = e.target || e.srcElement; |
var node = e.target || e.srcElement; |
||
if (!node) return; |
if (!node) return; |
||
− | + | SelectCategory.last_active_textfield = node.id; |
|
return true; |
return true; |
||
}, |
}, |
||
Line 156: | Line 156: | ||
setup: function() { |
setup: function() { |
||
//Decide whether to use the toolbar or the bottom div |
//Decide whether to use the toolbar or the bottom div |
||
− | if (window. |
+ | if (window.oldSelectcategory || "1" === mw.user.options.get('gadget-OldSelectcategory') || $('#wpUploadDescription').length || !$.wikiEditor || !$.wikiEditor.isSupported()) { |
− | + | SelectCategory.createSelector(); |
|
− | + | SelectCategory.enableForAllFields(); |
|
} else { |
} else { |
||
− | + | SelectCategory.makeToolbarButtons(); |
|
− | + | SelectCategory.enableForAllFields(); |
|
} |
} |
||
} |
} |
||
}; |
}; |
||
$(document).ready(function() { |
$(document).ready(function() { |
||
− | // Don't do anything if no |
+ | // Don't do anything if no selectcategory present. |
if ($('#specialchars').length !== 1) return; |
if ($('#specialchars').length !== 1) return; |
||
mw.loader.using('mediawiki.user', function() { |
mw.loader.using('mediawiki.user', function() { |
||
// Check user preferences |
// Check user preferences |
||
− | if (1 === mw.user.options.get('usebetatoolbar') && "1" !== mw.user.options.get('gadget- |
+ | if (1 === mw.user.options.get('usebetatoolbar') && "1" !== mw.user.options.get('gadget-OldSelectcategory')) { |
− | mw.loader.using(['ext.wikiEditor.toolbar', 'jquery.wikiEditor.toolbar'], |
+ | mw.loader.using(['ext.wikiEditor.toolbar', 'jquery.wikiEditor.toolbar'], SelectCategory.setup); |
} else { |
} else { |
||
− | + | SelectCategory.setup(); |
|
} |
} |
||
}); |
}); |
Latest revision as of 14:14, 19 July 2012
// <source lang="javascript"> /* SelectCategory 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:SelectCategory]]. */ /*jshint curly:false */ /*global importStylesheetURI:false, jQuery:false, mediaWiki:false */ (function($, mw) { "use strict"; var SelectCategory, insertTags; importStylesheetURI('//commons.wikimedia.org/?title=MediaWiki:Edittools.css&action=raw&ctype=text/css'); window.insertTags = insertTags = function(tagOpen, tagClose, sampleText) { var $txtarea = SelectCategory.getTextArea(); if ($txtarea.length !== 1) return; /* Usability initiative compatibility */ if (typeof $.fn.textSelection !== 'undefined') { $txtarea.textSelection('encapsulateSelection', { 'pre': tagOpen, 'peri': sampleText, 'post': tagClose }); return; } }; window.SelectCategory = SelectCategory = { 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() { SelectCategory.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 + '>' + decodeURIComponent(id) + '</option>'); }); $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, _this) { var onclick = _this.getAttribute("onclick"); // if onclick is not a function, it's not IE7, so use setAttribute if ('function' !== typeof onclick) { $button[0].setAttribute('onclick', onclick); // for FF,IE8,Chrome // if onclick is a function, use the IE7 method and call onclick() in the anonymous function } else { $button[0].onclick = function() { onclick(); }; // for IE7 } }, makeButtons: function($wanted) { var $links = $wanted.find('a'); var _this = this; $links.each(function() { var $button = $('<button type="button">'); $button.text($(this).text()); _this.bindOnClick($button, this); $(this).replaceWith($button); $(this).blur(); }); $wanted.contents().not('button').remove(); }, makeToolbarButtons: function() { var _this = this; // Add Edittool section $('#wpTextbox1').wikiEditor('addToToolbar', { 'sections': { 'Category': { 'type': 'booklet', 'label': 'Category', 'pages': { 'Selectcategory1': { 'layout': 'characters', // 'label': 'Category' } } } } }); var $section = $('.page-Selectcategory1 div'); var $links = $('#specialchars p.specialbasic').eq(0).find('a'); $links.each(function() { var $button = $('<span>'); $button.text($(this).text()); _this.bindOnClick($button, this); $section.append($button); }); $('.mw-selectCategory').remove(); }, last_active_textfield: null, enableForAllFields: function() { $('textarea, input').focus(function() { SelectCategory.last_active_textfield = this.id; }); }, getTextArea: function() { var $txtarea = {}; if (SelectCategory.last_active_textfield !== null) $txtarea = $('#' + SelectCategory.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; SelectCategory.last_active_textfield = node.id; return true; }, setup: function() { //Decide whether to use the toolbar or the bottom div if (window.oldSelectcategory || "1" === mw.user.options.get('gadget-OldSelectcategory') || $('#wpUploadDescription').length || !$.wikiEditor || !$.wikiEditor.isSupported()) { SelectCategory.createSelector(); SelectCategory.enableForAllFields(); } else { SelectCategory.makeToolbarButtons(); SelectCategory.enableForAllFields(); } } }; $(document).ready(function() { // Don't do anything if no selectcategory present. if ($('#specialchars').length !== 1) return; mw.loader.using('mediawiki.user', function() { // Check user preferences if (1 === mw.user.options.get('usebetatoolbar') && "1" !== mw.user.options.get('gadget-OldSelectcategory')) { mw.loader.using(['ext.wikiEditor.toolbar', 'jquery.wikiEditor.toolbar'], SelectCategory.setup); } else { SelectCategory.setup(); } }); }); // </source> }(jQuery, mediaWiki));