מדיה ויקי:Gadget-TemplateParamWizard.js: הבדלים בין גרסאות בדף
←using [[mw:CodeEditor|CodeEditor]]: הצגת רשימת התבניות הנתמכות אם אין תבנית מסומנת. |
|||
| שורה 2: | שורה 2: | ||
//Written by [[User:קיפודנחש]] | //Written by [[User:קיפודנחש]] | ||
if($.inArray(mw.config.get('wgAction'), ['edit', 'submit'])+1) | if($.inArray(mw.config.get('wgAction'), ['edit', 'submit'])+1) | ||
$(document).ready(function() { | $(document).ready(function() { | ||
mw.loader.using(['jquery.ui.widget','jquery.ui.autocomplete','jquery.textSelection', 'jquery.ui.dialog'], function() { | mw.loader.using(['jquery.ui.widget','jquery.ui.autocomplete','jquery.textSelection', 'jquery.ui.dialog'], function() { | ||
// template parameter is an object with the following fields: | |||
// desc: desciption string | |||
// select: array of possible values (optional) | // select: array of possible values (optional) | ||
// defval: default value (optional) | // defval: default value (optional) | ||
| שורה 24: | שורה 24: | ||
fieldsBypName, | fieldsBypName, | ||
rtl = $('body').css('direction') == 'rtl'; | rtl = $('body').css('direction') == 'rtl'; | ||
function paramsFromSelection() { | function paramsFromSelection() { | ||
var selection = $("#wpTextbox1").textSelection('getSelection').replace(/(^\{\{|\}\}$)/g, ''); //scrap the first {{ and last }} | var selection = $("#wpTextbox1").textSelection('getSelection').replace(/(^\{\{|\}\}$)/g, ''); //scrap the first {{ and last }} | ||
var specials = []; | var specials = []; | ||
while (true) { //extract inner links, inner templates and inner params - we don't want to sptit those. | while (true) { //extract inner links, inner templates and inner params - we don't want to sptit those. | ||
var match = selection.match(/(\{\{[^{}\]\[]*\}\}|\[\[[^{}\]\[]*\]\]|\[[^{}\]\[]*\](?:[^\]]))/); | var match = selection.match(/(\{\{[^{}\]\[]*\}\}|\[\[[^{}\]\[]*\]\]|\[[^{}\]\[]*\](?:[^\]]))/); | ||
| שורה 52: | שורה 52: | ||
} | } | ||
} | } | ||
function buildParams(data) { | function buildParams(data) { | ||
var lines = data.split("\n"); | var lines = data.split("\n"); | ||
| שורה 71: | שורה 71: | ||
var desc = $.trim(fields[1]); | var desc = $.trim(fields[1]); | ||
var pAttribs = {desc: desc}; | var pAttribs = {desc: desc}; | ||
if (fields.length > 2) | if (fields.length > 2) | ||
pAttribs.options = analyzeOptions($.trim(fields[2])); | pAttribs.options = analyzeOptions($.trim(fields[2])); | ||
templateParams[name] = pAttribs; | templateParams[name] = pAttribs; | ||
} | } | ||
} | } | ||
function analyzeOptions(str) { | function analyzeOptions(str) { | ||
var res = {}, | var res = {}, | ||
| שורה 91: | שורה 91: | ||
return res; | return res; | ||
} | } | ||
function createWikiCode() { | function createWikiCode() { | ||
var par = [template]; | var par = [template]; | ||
for (var i in dialogFields) { | for (var i in dialogFields) { | ||
var | var | ||
field = dialogFields[i], | field = dialogFields[i], | ||
name = $.trim(field[0]), | name = $.trim(field[0]), | ||
| שורה 120: | שורה 120: | ||
$('<div>') | $('<div>') | ||
.dialog( | .dialog( | ||
{title: i18n('preview'), | {title: i18n('preview'), | ||
width: 'auto', | width: 'auto', | ||
height: 'auto', | height: 'auto', | ||
overflow: 'auto', | overflow: 'auto', | ||
modal: true, | |||
position: [60, 60], | position: [60, 60], | ||
buttons: buttons}) | buttons: buttons}) | ||
.append(div); | .append(div); | ||
} | } | ||
}); | }); | ||
} | } | ||
function i18n(key) { | function i18n(key, param) { | ||
if (key == 'templates namespace') | if (key == 'templates namespace') | ||
return mw.config.get('wgFormattedNamespaces')[10]; | return mw.config.get('wgFormattedNamespaces')[10]; | ||
| שורה 139: | שורה 139: | ||
switch (key) { | switch (key) { | ||
case 'explain': return 'השדות המסומנים באדום הם חובה, השאר אופציונליים.' + | case 'explain': return 'השדות המסומנים באדום הם חובה, השאר אופציונליים.' + | ||
'<br />' + 'הקישו על שם הפרמטר לקבלת הסבר עליו, הקישו שוב להסתיר את ההסבר.'; | |||
case 'wizard dialog title': return 'מילוי הפרמטרים עבור תבנית ' + template; | |||
case 'ok': return 'אישור'; | case 'ok': return 'אישור'; | ||
case 'cancel': return 'ביטול' | case 'cancel': return 'ביטול' | ||
| שורה 152: | שורה 153: | ||
case 'choices': return 'אפשרויות'; | case 'choices': return 'אפשרויות'; | ||
case 'button hint': return 'אשף מילוי תבניות'; | case 'button hint': return 'אשף מילוי תבניות'; | ||
case 'able templates category name': return 'תבניות הנתמכות על ידי אשף התבניות'; | |||
case 'template selector title': return 'אנא בחרו תבנית מהרשימה:'; | |||
} | } | ||
default: | default: | ||
| שורה 169: | שורה 172: | ||
case 'choices': return 'Choices'; | case 'choices': return 'Choices'; | ||
case 'button hint': return 'Template parameters wizard'; | case 'button hint': return 'Template parameters wizard'; | ||
case 'albe templates category name': throw('Must define category name for wizard-capable templates'); | |||
case 'template selector title': return 'Please select a template from this list'; | |||
} | } | ||
} | } | ||
return key; | return key; | ||
} | } | ||
function paramPage() { | function paramPage() { | ||
return i18n('templates namespace') + ':' + $.trim(template) + '/' + i18n('params subpage'); | return i18n('templates namespace') + ':' + $.trim(template) + '/' + i18n('params subpage'); | ||
} | } | ||
function updateRawPreview(){ | function updateRawPreview(){ | ||
var canOK = 'enable'; | var canOK = 'enable'; | ||
| שורה 189: | שורה 193: | ||
if (opts && opts.depends) { | if (opts && opts.depends) { | ||
var dep = fieldsBypName[opts.depends]; | var dep = fieldsBypName[opts.depends]; | ||
var depEmpty = (dep && dep.val() && $.trim(dep.val())) ? false : true; | var depEmpty = (dep && dep.val() && $.trim(dep.val())) ? false : true; | ||
var row = rowsBypName[df.data('paramName')]; | var row = rowsBypName[df.data('paramName')]; | ||
if (row) | if (row) | ||
| שורה 198: | שורה 202: | ||
$('#tpw_preview').html(createWikiCode()); | $('#tpw_preview').html(createWikiCode()); | ||
} | } | ||
function toggleDesc() {$(this).next('p').toggleClass('tpw_hidden');} | function toggleDesc() {$(this).next('p').toggleClass('tpw_hidden');} | ||
function createInputField(paramName) { | function createInputField(paramName) { | ||
var options = templateParams[paramName].options || {}, | var options = templateParams[paramName].options || {}, | ||
f, | f, | ||
checkbox = false; | checkbox = false; | ||
if (options.choices) { | if (options.choices) { | ||
var choices = options.choices.split(/\s*,\s*/); | var choices = options.choices.split(/\s*,\s*/); | ||
| שורה 226: | שורה 230: | ||
else | else | ||
f = $('<input>', {type: 'text'}); | f = $('<input>', {type: 'text'}); | ||
if (!checkbox && f.autoCompleteWikiText) // teach the controls to autocomplete. | if (!checkbox && f.autoCompleteWikiText) // teach the controls to autocomplete. | ||
f.autoCompleteWikiText({positionMy: $('body').is('.rtl')? "left top" : "right top"}); | f.autoCompleteWikiText({positionMy: $('body').is('.rtl')? "left top" : "right top"}); | ||
f.css({width: checkbox ? '1em' : '28em'}) | f.css({width: checkbox ? '1em' : '28em'}) | ||
.data({paramName: paramName, options: options}) | .data({paramName: paramName, options: options}) | ||
.bind('paste cut drop input change', updateRawPreview); | .bind('paste cut drop input change', updateRawPreview); | ||
if (options.defval) | if (options.defval) | ||
f.val(options.defval); | f.val(options.defval); | ||
if (options.required) | if (options.required) | ||
f.addClass('tpw_required').css({border: '1px red solid'}); | f.addClass('tpw_required').css({border: '1px red solid'}); | ||
return f; | return f; | ||
} | } | ||
function addRow(paramName, table) { | function addRow(paramName, table) { | ||
var inputField = createInputField(paramName); | var inputField = createInputField(paramName); | ||
| שורה 262: | שורה 266: | ||
fieldsBypName[paramName] = inputField; | fieldsBypName[paramName] = inputField; | ||
} | } | ||
function injectResults() { | function injectResults() { | ||
$("#wpTextbox1").textSelection('encapsulateSelection', {replace: true, peri: createWikiCode()}); | $("#wpTextbox1").textSelection('encapsulateSelection', {replace: true, peri: createWikiCode()}); | ||
} | } | ||
function buildDialog(data) { | function buildDialog(data) { | ||
$('.tpw_disposable').remove(); | $('.tpw_disposable').remove(); | ||
| שורה 274: | שורה 278: | ||
var dialog = $('<div>', {'class': 'tpw_disposable'}) | var dialog = $('<div>', {'class': 'tpw_disposable'}) | ||
.dialog({height: 'auto', | .dialog({height: 'auto', | ||
title: i18n('wizard dialog title', template), | |||
width: 'auto', | width: 'auto', | ||
overflow: 'auto', | overflow: 'auto', | ||
| שורה 287: | שורה 292: | ||
for (var paramName in templateParams) | for (var paramName in templateParams) | ||
addRow(paramName, table); | addRow(paramName, table); | ||
var buttons = {}; // we need to do it this way, because with literal object, the keys must be literal. | var buttons = {}; // we need to do it this way, because with literal object, the keys must be literal. | ||
buttons[i18n('ok')] = function() {injectResults(); dialog.dialog('close'); }; | buttons[i18n('ok')] = function() {injectResults(); dialog.dialog('close'); }; | ||
| שורה 295: | שורה 300: | ||
$('.ui-dialog-buttonpane').css({backgroundColor: '#E0E0E0'}); | $('.ui-dialog-buttonpane').css({backgroundColor: '#E0E0E0'}); | ||
$('.ui-dialog-buttonpane').css({direction: 'ltr'}); | $('.ui-dialog-buttonpane').css({direction: 'ltr'}); | ||
$('.ui-dialog-buttonpane | $('.ui-dialog-buttonpane button').css({float: 'right'}); // jQuery has problems with rtl dialogs + ie is braindamaged. | ||
updateRawPreview(); | updateRawPreview(); | ||
} | } | ||
| שורה 304: | שורה 309: | ||
dialogFields = []; | dialogFields = []; | ||
rowsBypName = {}; | rowsBypName = {}; | ||
fieldsBypName = {}; | fieldsBypName = {}; | ||
} | } | ||
function reportError(a,b,error) { | function reportError(a,b,error) { | ||
if (error == "Not Found") | if (error == "Not Found") | ||
| שורה 319: | שורה 324: | ||
alert('טעות בהפעלת האשף.' + '\n' + error); | alert('טעות בהפעלת האשף.' + '\n' + error); | ||
} | } | ||
var allTemplates; | |||
function pickTemplate() { | |||
var selector = $('<select>'); | |||
for (var i in allTemplates) | |||
selector.append($('<option>', {value: allTemplates[i], text: allTemplates[i]})); | |||
var templateSelector = $('<div>').dialog({ | |||
title: i18n('template selector title'), | |||
height: 'auto', | |||
width: 'auto', | |||
modal: true, | |||
buttons: [ | |||
{text: i18n('ok'), click: function(){template = selector.val(); fireDialog(); templateSelector.dialog("close")}}, | |||
{text: i18n('cancel'), click: function(){templateSelector.dialog("close")}} | |||
] | |||
}).append(selector); | |||
$('.ui-dialog-buttonpane button').css({float: 'right'}); | |||
} | |||
function findTemplate(data) { | |||
if (data) { | |||
$(data.query.categorymembers).each(function(){ | |||
allTemplates.push(this.title.replace(/.*:/, '')); | |||
}); | |||
if (!data['query-continue'] && allTemplates.length) | |||
pickTemplate(); | |||
} | |||
if (!data || data['query-continue']) { | |||
var params = { | |||
action: 'query', | |||
list: 'categorymembers', | |||
cmtitle: 'Category:' + i18n('able templates category name'), | |||
cmlimit: 500, | |||
format: 'json' | |||
}; | |||
if (data && data['query-continue']) | |||
params.cmcontinue = data['query-continue'].categorymembers.cmcontinue; | |||
$.ajax({ | |||
url: mw.util.wikiScript('api'), | |||
data: params, | |||
type: 'post', | |||
success: findTemplate, | |||
error: reportError | |||
}); | |||
} | |||
} | |||
function fireDialog() { | function fireDialog() { | ||
$.ajax({ | $.ajax({ | ||
url: mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode(paramPage()) + '&action=raw&ctype=text/x-wiki', | url: mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode(paramPage()) + '&action=raw&ctype=text/x-wiki', | ||
| שורה 339: | שורה 383: | ||
"</style> " | "</style> " | ||
).appendTo("head"); | ).appendTo("head"); | ||
function doIt() { | |||
init(); | |||
var match = $("#wpTextbox1").textSelection('getSelection').match(/^\{\{([^|}]*)/); | |||
template = match ? $.trim(match[1]) : null; | |||
if (template) | |||
fireDialor(); | |||
else { | |||
allTemplates = []; | |||
findTemplate(); | |||
} | |||
} | |||
setTimeout(function() { | setTimeout(function() { | ||
| שורה 351: | שורה 408: | ||
type: 'button', | type: 'button', | ||
icon: buttonImage, | icon: buttonImage, | ||
action: {type: 'callback', execute: | action: {type: 'callback', execute: doIt} | ||
} | } | ||
} | } | ||
| שורה 359: | שורה 416: | ||
$('<img>', {src: buttonImage, title: i18n('button hint'), 'class': 'mw-toolbar-editbutton'}) | $('<img>', {src: buttonImage, title: i18n('button hint'), 'class': 'mw-toolbar-editbutton'}) | ||
.css({cursor: 'pointer'}) | .css({cursor: 'pointer'}) | ||
.click( | .click(doIt) | ||
); | ); | ||
}, 120); | }, 120); | ||
}); | }); | ||
}); | }); | ||